tdb2: use counters to decide when to coalesce records.
[ccan] / ccan / tdb2 / free.c
1  /*
2    Trivial Database 2: free list/block handling
3    Copyright (C) Rusty Russell 2010
4
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.
9
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.
14
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/>.
17 */
18 #include "private.h"
19 #include <ccan/likely/likely.h>
20 #include <ccan/ilog/ilog.h>
21 #include <time.h>
22 #include <assert.h>
23 #include <limits.h>
24
25 static unsigned fls64(uint64_t val)
26 {
27         return ilog64(val);
28 }
29
30 /* In which bucket would we find a particular record size? (ignoring header) */
31 unsigned int size_to_bucket(tdb_len_t data_len)
32 {
33         unsigned int bucket;
34
35         /* We can't have records smaller than this. */
36         assert(data_len >= TDB_MIN_DATA_LEN);
37
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;
42         } else {
43                 /* After that we go power of 2. */
44                 bucket = fls64(data_len - TDB_MIN_DATA_LEN) + 2;
45         }
46
47         if (unlikely(bucket >= TDB_FREE_BUCKETS))
48                 bucket = TDB_FREE_BUCKETS - 1;
49         return bucket;
50 }
51
52 tdb_off_t first_ftable(struct tdb_context *tdb)
53 {
54         return tdb_read_off(tdb, offsetof(struct tdb_header, free_table));
55 }
56
57 tdb_off_t next_ftable(struct tdb_context *tdb, tdb_off_t ftable)
58 {
59         return tdb_read_off(tdb, ftable + offsetof(struct tdb_freetable,next));
60 }
61
62 enum TDB_ERROR tdb_ftable_init(struct tdb_context *tdb)
63 {
64         /* Use reservoir sampling algorithm to select a free list at random. */
65         unsigned int rnd, max = 0, count = 0;
66         tdb_off_t off;
67
68         tdb->ftable_off = off = first_ftable(tdb);
69         tdb->ftable = 0;
70
71         while (off) {
72                 if (TDB_OFF_IS_ERR(off)) {
73                         return off;
74                 }
75
76                 rnd = random();
77                 if (rnd >= max) {
78                         tdb->ftable_off = off;
79                         tdb->ftable = count;
80                         max = rnd;
81                 }
82
83                 off = next_ftable(tdb, off);
84                 count++;
85         }
86         return TDB_SUCCESS;
87 }
88
89 /* Offset of a given bucket. */
90 tdb_off_t bucket_off(tdb_off_t ftable_off, unsigned bucket)
91 {
92         return ftable_off + offsetof(struct tdb_freetable, buckets)
93                 + bucket * sizeof(tdb_off_t);
94 }
95
96 /* Returns free_buckets + 1, or list number to search, or -ve error. */
97 static tdb_off_t find_free_head(struct tdb_context *tdb,
98                                 tdb_off_t ftable_off,
99                                 tdb_off_t bucket)
100 {
101         /* Speculatively search for a non-zero bucket. */
102         return tdb_find_nonzero_off(tdb, bucket_off(ftable_off, 0),
103                                     bucket, TDB_FREE_BUCKETS);
104 }
105
106 static void check_list(struct tdb_context *tdb, tdb_off_t b_off)
107 {
108 #ifdef CCAN_TDB2_DEBUG
109         tdb_off_t off, prev = 0, first;
110         struct tdb_free_record r;
111
112         first = off = (tdb_read_off(tdb, b_off) & TDB_OFF_MASK);
113         while (off != 0) {
114                 tdb_read_convert(tdb, off, &r, sizeof(r));
115                 if (frec_magic(&r) != TDB_FREE_MAGIC)
116                         abort();
117                 if (prev && frec_prev(&r) != prev)
118                         abort();
119                 prev = off;
120                 off = r.next;
121         }
122
123         if (first) {
124                 tdb_read_convert(tdb, first, &r, sizeof(r));
125                 if (frec_prev(&r) != prev)
126                         abort();
127         }
128 #endif
129 }
130
131 /* Remove from free bucket. */
132 static enum TDB_ERROR remove_from_list(struct tdb_context *tdb,
133                                        tdb_off_t b_off, tdb_off_t r_off,
134                                        const struct tdb_free_record *r)
135 {
136         tdb_off_t off, prev_next, head;
137         enum TDB_ERROR ecode;
138
139         /* Is this only element in list?  Zero out bucket, and we're done. */
140         if (frec_prev(r) == r_off)
141                 return tdb_write_off(tdb, b_off, 0);
142
143         /* off = &r->prev->next */
144         off = frec_prev(r) + offsetof(struct tdb_free_record, next);
145
146         /* Get prev->next */
147         prev_next = tdb_read_off(tdb, off);
148         if (TDB_OFF_IS_ERR(prev_next))
149                 return prev_next;
150
151         /* If prev->next == 0, we were head: update bucket to point to next. */
152         if (prev_next == 0) {
153                 /* We must preserve upper bits. */
154                 head = tdb_read_off(tdb, b_off);
155                 if (TDB_OFF_IS_ERR(head))
156                         return head;
157
158                 if ((head & TDB_OFF_MASK) != r_off) {
159                         return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
160                                           "remove_from_list:"
161                                           " %llu head %llu on list %llu",
162                                           (long long)r_off,
163                                           (long long)head,
164                                           (long long)b_off);
165                 }
166                 head = ((head & ~TDB_OFF_MASK) | r->next);
167                 ecode = tdb_write_off(tdb, b_off, head);
168                 if (ecode != TDB_SUCCESS)
169                         return ecode;
170         } else {
171                 /* r->prev->next = r->next */
172                 ecode = tdb_write_off(tdb, off, r->next);
173                 if (ecode != TDB_SUCCESS)
174                         return ecode;
175         }
176
177         /* If we were the tail, off = &head->prev. */
178         if (r->next == 0) {
179                 head = tdb_read_off(tdb, b_off);
180                 if (TDB_OFF_IS_ERR(head))
181                         return head;
182                 head &= TDB_OFF_MASK;
183                 off = head + offsetof(struct tdb_free_record, magic_and_prev);
184         } else {
185                 /* off = &r->next->prev */
186                 off = r->next + offsetof(struct tdb_free_record,
187                                          magic_and_prev);
188         }
189
190 #ifdef CCAN_TDB2_DEBUG
191         /* *off == r */
192         if ((tdb_read_off(tdb, off) & TDB_OFF_MASK) != r_off) {
193                 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
194                                   "remove_from_list:"
195                                   " %llu bad prev in list %llu",
196                                   (long long)r_off, (long long)b_off);
197         }
198 #endif
199         /* r->next->prev = r->prev */
200         return tdb_write_off(tdb, off, r->magic_and_prev);
201 }
202
203 /* Enqueue in this free bucket: sets coalesce if we've added 128
204  * entries to it. */
205 static enum TDB_ERROR enqueue_in_free(struct tdb_context *tdb,
206                                       tdb_off_t b_off,
207                                       tdb_off_t off,
208                                       tdb_len_t len,
209                                       bool *coalesce)
210 {
211         struct tdb_free_record new;
212         enum TDB_ERROR ecode;
213         tdb_off_t prev, head;
214         uint64_t magic = (TDB_FREE_MAGIC << (64 - TDB_OFF_UPPER_STEAL));
215
216         head = tdb_read_off(tdb, b_off);
217         if (TDB_OFF_IS_ERR(head))
218                 return head;
219
220         /* We only need to set ftable_and_len; rest is set in enqueue_in_free */
221         new.ftable_and_len = ((uint64_t)tdb->ftable << (64 - TDB_OFF_UPPER_STEAL))
222                 | len;
223
224         /* new->next = head. */
225         new.next = (head & TDB_OFF_MASK);
226
227         /* First element?  Prev points to ourselves. */
228         if (!new.next) {
229                 new.magic_and_prev = (magic | off);
230         } else {
231                 /* new->prev = next->prev */
232                 prev = tdb_read_off(tdb,
233                                     new.next + offsetof(struct tdb_free_record,
234                                                         magic_and_prev));
235                 new.magic_and_prev = prev;
236                 if (frec_magic(&new) != TDB_FREE_MAGIC) {
237                         return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
238                                           "enqueue_in_free: %llu bad head"
239                                           " prev %llu",
240                                           (long long)new.next,
241                                           (long long)prev);
242                 }
243                 /* next->prev = new. */
244                 ecode = tdb_write_off(tdb, new.next
245                                       + offsetof(struct tdb_free_record,
246                                                  magic_and_prev),
247                                       off | magic);
248                 if (ecode != TDB_SUCCESS) {
249                         return ecode;
250                 }
251
252 #ifdef CCAN_TDB2_DEBUG
253                 prev = tdb_read_off(tdb, frec_prev(&new)
254                                     + offsetof(struct tdb_free_record, next));
255                 if (prev != 0) {
256                         return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
257                                           "enqueue_in_free:"
258                                           " %llu bad tail next ptr %llu",
259                                           (long long)frec_prev(&new)
260                                           + offsetof(struct tdb_free_record,
261                                                      next),
262                                           (long long)prev);
263                 }
264 #endif
265         }
266
267         /* Update enqueue count, but don't set high bit: see TDB_OFF_IS_ERR */
268         if (*coalesce)
269                 head += (1ULL << (64 - TDB_OFF_UPPER_STEAL));
270         head &= ~(TDB_OFF_MASK | (1ULL << 63));
271         head |= off;
272
273         ecode = tdb_write_off(tdb, b_off, head);
274         if (ecode != TDB_SUCCESS) {
275                 return ecode;
276         }
277
278         /* It's time to coalesce if counter wrapped. */
279         if (*coalesce)
280                 *coalesce = ((head & ~TDB_OFF_MASK) == 0);
281
282         return tdb_write_convert(tdb, off, &new, sizeof(new));
283 }
284
285 static tdb_off_t ftable_offset(struct tdb_context *tdb, unsigned int ftable)
286 {
287         tdb_off_t off;
288         unsigned int i;
289
290         if (likely(tdb->ftable == ftable))
291                 return tdb->ftable_off;
292
293         off = first_ftable(tdb);
294         for (i = 0; i < ftable; i++) {
295                 if (TDB_OFF_IS_ERR(off)) {
296                         break;
297                 }
298                 off = next_ftable(tdb, off);
299         }
300         return off;
301 }
302
303 /* Note: we unlock the current bucket if fail (-ve), or coalesce (+ve) and
304  * need to blatt the *protect record (which is set to an error). */
305 static tdb_len_t coalesce(struct tdb_context *tdb,
306                           tdb_off_t off, tdb_off_t b_off,
307                           tdb_len_t data_len,
308                           tdb_off_t *protect)
309 {
310         tdb_off_t end;
311         struct tdb_free_record rec;
312         enum TDB_ERROR ecode;
313
314         tdb->stats.alloc_coalesce_tried++;
315         end = off + sizeof(struct tdb_used_record) + data_len;
316
317         while (end < tdb->file->map_size) {
318                 const struct tdb_free_record *r;
319                 tdb_off_t nb_off;
320                 unsigned ftable, bucket;
321
322                 r = tdb_access_read(tdb, end, sizeof(*r), true);
323                 if (TDB_PTR_IS_ERR(r)) {
324                         ecode = TDB_PTR_ERR(r);
325                         goto err;
326                 }
327
328                 if (frec_magic(r) != TDB_FREE_MAGIC
329                     || frec_ftable(r) == TDB_FTABLE_NONE) {
330                         tdb_access_release(tdb, r);
331                         break;
332                 }
333
334                 ftable = frec_ftable(r);
335                 bucket = size_to_bucket(frec_len(r));
336                 nb_off = ftable_offset(tdb, ftable);
337                 if (TDB_OFF_IS_ERR(nb_off)) {
338                         tdb_access_release(tdb, r);
339                         ecode = nb_off;
340                         goto err;
341                 }
342                 nb_off = bucket_off(nb_off, bucket);
343                 tdb_access_release(tdb, r);
344
345                 /* We may be violating lock order here, so best effort. */
346                 if (tdb_lock_free_bucket(tdb, nb_off, TDB_LOCK_NOWAIT)
347                     != TDB_SUCCESS) {
348                         tdb->stats.alloc_coalesce_lockfail++;
349                         break;
350                 }
351
352                 /* Now we have lock, re-check. */
353                 ecode = tdb_read_convert(tdb, end, &rec, sizeof(rec));
354                 if (ecode != TDB_SUCCESS) {
355                         tdb_unlock_free_bucket(tdb, nb_off);
356                         goto err;
357                 }
358
359                 if (unlikely(frec_magic(&rec) != TDB_FREE_MAGIC)) {
360                         tdb->stats.alloc_coalesce_race++;
361                         tdb_unlock_free_bucket(tdb, nb_off);
362                         break;
363                 }
364
365                 if (unlikely(frec_ftable(&rec) != ftable)
366                     || unlikely(size_to_bucket(frec_len(&rec)) != bucket)) {
367                         tdb->stats.alloc_coalesce_race++;
368                         tdb_unlock_free_bucket(tdb, nb_off);
369                         break;
370                 }
371
372                 /* Did we just mess up a record you were hoping to use? */
373                 if (end == *protect)
374                         *protect = TDB_ERR_NOEXIST;
375
376                 ecode = remove_from_list(tdb, nb_off, end, &rec);
377                 check_list(tdb, nb_off);
378                 if (ecode != TDB_SUCCESS) {
379                         tdb_unlock_free_bucket(tdb, nb_off);
380                         goto err;
381                 }
382
383                 end += sizeof(struct tdb_used_record) + frec_len(&rec);
384                 tdb_unlock_free_bucket(tdb, nb_off);
385                 tdb->stats.alloc_coalesce_num_merged++;
386         }
387
388         /* Didn't find any adjacent free? */
389         if (end == off + sizeof(struct tdb_used_record) + data_len)
390                 return 0;
391
392         /* Before we expand, check this isn't one you wanted protected? */
393         if (off == *protect)
394                 *protect = TDB_ERR_EXISTS;
395
396         /* OK, expand initial record */
397         ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
398         if (ecode != TDB_SUCCESS) {
399                 goto err;
400         }
401
402         if (frec_len(&rec) != data_len) {
403                 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
404                                    "coalesce: expected data len %zu not %zu",
405                                    (size_t)data_len, (size_t)frec_len(&rec));
406                 goto err;
407         }
408
409         ecode = remove_from_list(tdb, b_off, off, &rec);
410         check_list(tdb, b_off);
411         if (ecode != TDB_SUCCESS) {
412                 goto err;
413         }
414
415         /* Try locking violation first.  We don't allow coalesce recursion! */
416         ecode = add_free_record(tdb, off, end - off, TDB_LOCK_NOWAIT, false);
417         if (ecode != TDB_SUCCESS) {
418                 /* Need to drop lock.  Can't rely on anything stable. */
419                 *protect = TDB_ERR_CORRUPT;
420
421                 /* We have to drop this to avoid deadlocks, so make sure record
422                  * doesn't get coalesced by someone else! */
423                 rec.ftable_and_len = (TDB_FTABLE_NONE
424                                       << (64 - TDB_OFF_UPPER_STEAL))
425                         | (end - off - sizeof(struct tdb_used_record));
426                 ecode = tdb_write_off(tdb,
427                                       off + offsetof(struct tdb_free_record,
428                                                      ftable_and_len),
429                                       rec.ftable_and_len);
430                 if (ecode != TDB_SUCCESS) {
431                         goto err;
432                 }
433
434                 tdb->stats.alloc_coalesce_succeeded++;
435                 tdb_unlock_free_bucket(tdb, b_off);
436
437                 ecode = add_free_record(tdb, off, end - off, TDB_LOCK_WAIT,
438                                         false);
439                 if (ecode != TDB_SUCCESS) {
440                         return ecode;
441                 }
442         } else if (TDB_OFF_IS_ERR(*protect)) {
443                 /* For simplicity, we always drop lock if they can't continue */
444                 tdb_unlock_free_bucket(tdb, b_off);
445         }
446
447         /* Return usable length. */
448         return end - off - sizeof(struct tdb_used_record);
449
450 err:
451         /* To unify error paths, we *always* unlock bucket on error. */
452         tdb_unlock_free_bucket(tdb, b_off);
453         return ecode;
454 }
455
456 /* List is locked: we unlock it. */
457 static enum TDB_ERROR coalesce_list(struct tdb_context *tdb,
458                                     tdb_off_t ftable_off, tdb_off_t b_off)
459 {
460         enum TDB_ERROR ecode;
461         tdb_off_t off;
462
463         off = tdb_read_off(tdb, b_off);
464         if (TDB_OFF_IS_ERR(off)) {
465                 ecode = off;
466                 goto unlock_err;
467         }
468         /* A little bit of paranoia */
469         off &= TDB_OFF_MASK;
470
471         while (off) {
472                 struct tdb_free_record rec;
473                 tdb_len_t coal;
474                 tdb_off_t next;
475
476                 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
477                 if (ecode != TDB_SUCCESS)
478                         goto unlock_err;
479
480                 next = rec.next;
481                 coal = coalesce(tdb, off, b_off, frec_len(&rec), &next);
482                 if (TDB_OFF_IS_ERR(coal)) {
483                         /* This has already unlocked on error. */
484                         return coal;
485                 }
486                 if (TDB_OFF_IS_ERR(next)) {
487                         /* Coalescing had to unlock, so stop. */
488                         return TDB_SUCCESS;
489                 }
490                 off = next;
491         }
492
493         tdb_unlock_free_bucket(tdb, b_off);
494         return TDB_SUCCESS;
495
496 unlock_err:
497         tdb_unlock_free_bucket(tdb, b_off);
498         return ecode;
499 }
500
501 /* List must not be locked if coalesce_ok is set. */
502 enum TDB_ERROR add_free_record(struct tdb_context *tdb,
503                                tdb_off_t off, tdb_len_t len_with_header,
504                                enum tdb_lock_flags waitflag,
505                                bool coalesce)
506 {
507         tdb_off_t b_off;
508         tdb_len_t len;
509         enum TDB_ERROR ecode;
510
511         assert(len_with_header >= sizeof(struct tdb_free_record));
512
513         len = len_with_header - sizeof(struct tdb_used_record);
514
515         b_off = bucket_off(tdb->ftable_off, size_to_bucket(len));
516         ecode = tdb_lock_free_bucket(tdb, b_off, waitflag);
517         if (ecode != TDB_SUCCESS) {
518                 return ecode;
519         }
520
521         ecode = enqueue_in_free(tdb, b_off, off, len, &coalesce);
522         check_list(tdb, b_off);
523
524         /* Coalescing unlocks free list. */
525         if (!ecode && coalesce)
526                 ecode = coalesce_list(tdb, tdb->ftable_off, b_off);
527         else
528                 tdb_unlock_free_bucket(tdb, b_off);
529         return ecode;
530 }
531
532 static size_t adjust_size(size_t keylen, size_t datalen)
533 {
534         size_t size = keylen + datalen;
535
536         if (size < TDB_MIN_DATA_LEN)
537                 size = TDB_MIN_DATA_LEN;
538
539         /* Round to next uint64_t boundary. */
540         return (size + (sizeof(uint64_t) - 1ULL)) & ~(sizeof(uint64_t) - 1ULL);
541 }
542
543 /* If we have enough left over to be useful, split that off. */
544 static size_t record_leftover(size_t keylen, size_t datalen,
545                               bool want_extra, size_t total_len)
546 {
547         ssize_t leftover;
548
549         if (want_extra)
550                 datalen += datalen / 2;
551         leftover = total_len - adjust_size(keylen, datalen);
552
553         if (leftover < (ssize_t)sizeof(struct tdb_free_record))
554                 return 0;
555
556         return leftover;
557 }
558
559 /* We need size bytes to put our key and data in. */
560 static tdb_off_t lock_and_alloc(struct tdb_context *tdb,
561                                 tdb_off_t ftable_off,
562                                 tdb_off_t bucket,
563                                 size_t keylen, size_t datalen,
564                                 bool want_extra,
565                                 unsigned magic,
566                                 unsigned hashlow)
567 {
568         tdb_off_t off, b_off,best_off;
569         struct tdb_free_record best = { 0 };
570         double multiplier;
571         size_t size = adjust_size(keylen, datalen);
572         enum TDB_ERROR ecode;
573
574         tdb->stats.allocs++;
575         b_off = bucket_off(ftable_off, bucket);
576
577         /* FIXME: Try non-blocking wait first, to measure contention. */
578         /* Lock this bucket. */
579         ecode = tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT);
580         if (ecode != TDB_SUCCESS) {
581                 return ecode;
582         }
583
584         best.ftable_and_len = -1ULL;
585         best_off = 0;
586
587         /* Get slack if we're after extra. */
588         if (want_extra)
589                 multiplier = 1.5;
590         else
591                 multiplier = 1.0;
592
593         /* Walk the list to see if any are large enough, getting less fussy
594          * as we go. */
595         off = tdb_read_off(tdb, b_off);
596         if (TDB_OFF_IS_ERR(off)) {
597                 ecode = off;
598                 goto unlock_err;
599         }
600         off &= TDB_OFF_MASK;
601
602         while (off) {
603                 const struct tdb_free_record *r;
604                 tdb_len_t len;
605                 tdb_off_t next;
606
607                 r = tdb_access_read(tdb, off, sizeof(*r), true);
608                 if (TDB_PTR_IS_ERR(r)) {
609                         ecode = TDB_PTR_ERR(r);
610                         goto unlock_err;
611                 }
612
613                 if (frec_magic(r) != TDB_FREE_MAGIC) {
614                         ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
615                                            "lock_and_alloc:"
616                                            " %llu non-free 0x%llx",
617                                            (long long)off,
618                                            (long long)r->magic_and_prev);
619                         tdb_access_release(tdb, r);
620                         goto unlock_err;
621                 }
622
623                 if (frec_len(r) >= size && frec_len(r) < frec_len(&best)) {
624                         best_off = off;
625                         best = *r;
626                 }
627
628                 if (frec_len(&best) <= size * multiplier && best_off) {
629                         tdb_access_release(tdb, r);
630                         break;
631                 }
632
633                 multiplier *= 1.01;
634
635                 next = r->next;
636                 len = frec_len(r);
637                 tdb_access_release(tdb, r);
638                 off = next;
639         }
640
641         /* If we found anything at all, use it. */
642         if (best_off) {
643                 struct tdb_used_record rec;
644                 size_t leftover;
645
646                 /* We're happy with this size: take it. */
647                 ecode = remove_from_list(tdb, b_off, best_off, &best);
648                 check_list(tdb, b_off);
649                 if (ecode != TDB_SUCCESS) {
650                         goto unlock_err;
651                 }
652
653                 leftover = record_leftover(keylen, datalen, want_extra,
654                                            frec_len(&best));
655
656                 assert(keylen + datalen + leftover <= frec_len(&best));
657                 /* We need to mark non-free before we drop lock, otherwise
658                  * coalesce() could try to merge it! */
659                 ecode = set_header(tdb, &rec, magic, keylen, datalen,
660                                    frec_len(&best) - leftover, hashlow);
661                 if (ecode != TDB_SUCCESS) {
662                         goto unlock_err;
663                 }
664
665                 ecode = tdb_write_convert(tdb, best_off, &rec, sizeof(rec));
666                 if (ecode != TDB_SUCCESS) {
667                         goto unlock_err;
668                 }
669
670                 /* For futureproofing, we put a 0 in any unused space. */
671                 if (rec_extra_padding(&rec)) {
672                         ecode = tdb->methods->twrite(tdb, best_off + sizeof(rec)
673                                                      + keylen + datalen, "", 1);
674                         if (ecode != TDB_SUCCESS) {
675                                 goto unlock_err;
676                         }
677                 }
678
679                 /* Bucket of leftover will be <= current bucket, so nested
680                  * locking is allowed. */
681                 if (leftover) {
682                         tdb->stats.alloc_leftover++;
683                         ecode = add_free_record(tdb,
684                                                 best_off + sizeof(rec)
685                                                 + frec_len(&best) - leftover,
686                                                 leftover, TDB_LOCK_WAIT, false);
687                         if (ecode != TDB_SUCCESS) {
688                                 best_off = ecode;
689                         }
690                 }
691                 tdb_unlock_free_bucket(tdb, b_off);
692
693                 return best_off;
694         }
695
696         tdb_unlock_free_bucket(tdb, b_off);
697         return 0;
698
699 unlock_err:
700         tdb_unlock_free_bucket(tdb, b_off);
701         return ecode;
702 }
703
704 /* Get a free block from current free list, or 0 if none, -ve on error. */
705 static tdb_off_t get_free(struct tdb_context *tdb,
706                           size_t keylen, size_t datalen, bool want_extra,
707                           unsigned magic, unsigned hashlow)
708 {
709         tdb_off_t off, ftable_off;
710         tdb_off_t start_b, b, ftable;
711         bool wrapped = false;
712
713         /* If they are growing, add 50% to get to higher bucket. */
714         if (want_extra)
715                 start_b = size_to_bucket(adjust_size(keylen,
716                                                      datalen + datalen / 2));
717         else
718                 start_b = size_to_bucket(adjust_size(keylen, datalen));
719
720         ftable_off = tdb->ftable_off;
721         ftable = tdb->ftable;
722         while (!wrapped || ftable_off != tdb->ftable_off) {
723                 /* Start at exact size bucket, and search up... */
724                 for (b = find_free_head(tdb, ftable_off, start_b);
725                      b < TDB_FREE_BUCKETS;
726                      b = find_free_head(tdb, ftable_off, b + 1)) {
727                         /* Try getting one from list. */
728                         off = lock_and_alloc(tdb, ftable_off,
729                                              b, keylen, datalen, want_extra,
730                                              magic, hashlow);
731                         if (TDB_OFF_IS_ERR(off))
732                                 return off;
733                         if (off != 0) {
734                                 if (b == start_b)
735                                         tdb->stats.alloc_bucket_exact++;
736                                 if (b == TDB_FREE_BUCKETS - 1)
737                                         tdb->stats.alloc_bucket_max++;
738                                 /* Worked?  Stay using this list. */
739                                 tdb->ftable_off = ftable_off;
740                                 tdb->ftable = ftable;
741                                 return off;
742                         }
743                         /* Didn't work.  Try next bucket. */
744                 }
745
746                 if (TDB_OFF_IS_ERR(b)) {
747                         return b;
748                 }
749
750                 /* Hmm, try next table. */
751                 ftable_off = next_ftable(tdb, ftable_off);
752                 if (TDB_OFF_IS_ERR(ftable_off)) {
753                         return ftable_off;
754                 }
755                 ftable++;
756
757                 if (ftable_off == 0) {
758                         wrapped = true;
759                         ftable_off = first_ftable(tdb);
760                         if (TDB_OFF_IS_ERR(ftable_off)) {
761                                 return ftable_off;
762                         }
763                         ftable = 0;
764                 }
765         }
766
767         return 0;
768 }
769
770 enum TDB_ERROR set_header(struct tdb_context *tdb,
771                           struct tdb_used_record *rec,
772                           unsigned magic, uint64_t keylen, uint64_t datalen,
773                           uint64_t actuallen, unsigned hashlow)
774 {
775         uint64_t keybits = (fls64(keylen) + 1) / 2;
776
777         /* Use bottom bits of hash, so it's independent of hash table size. */
778         rec->magic_and_meta = (hashlow & ((1 << 11)-1))
779                 | ((actuallen - (keylen + datalen)) << 11)
780                 | (keybits << 43)
781                 | ((uint64_t)magic << 48);
782         rec->key_and_data_len = (keylen | (datalen << (keybits*2)));
783
784         /* Encoding can fail on big values. */
785         if (rec_key_length(rec) != keylen
786             || rec_data_length(rec) != datalen
787             || rec_extra_padding(rec) != actuallen - (keylen + datalen)) {
788                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
789                                   "Could not encode k=%llu,d=%llu,a=%llu",
790                                   (long long)keylen, (long long)datalen,
791                                   (long long)actuallen);
792         }
793         return TDB_SUCCESS;
794 }
795
796 /* Expand the database. */
797 static enum TDB_ERROR tdb_expand(struct tdb_context *tdb, tdb_len_t size)
798 {
799         uint64_t old_size, rec_size, map_size;
800         tdb_len_t wanted;
801         enum TDB_ERROR ecode;
802
803         /* Need to hold a hash lock to expand DB: transactions rely on it. */
804         if (!(tdb->flags & TDB_NOLOCK)
805             && !tdb->file->allrecord_lock.count && !tdb_has_hash_locks(tdb)) {
806                 return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
807                                   "tdb_expand: must hold lock during expand");
808         }
809
810         /* Only one person can expand file at a time. */
811         ecode = tdb_lock_expand(tdb, F_WRLCK);
812         if (ecode != TDB_SUCCESS) {
813                 return ecode;
814         }
815
816         /* Someone else may have expanded the file, so retry. */
817         old_size = tdb->file->map_size;
818         tdb->methods->oob(tdb, tdb->file->map_size + 1, true);
819         if (tdb->file->map_size != old_size) {
820                 tdb_unlock_expand(tdb, F_WRLCK);
821                 return TDB_SUCCESS;
822         }
823
824         /* limit size in order to avoid using up huge amounts of memory for
825          * in memory tdbs if an oddball huge record creeps in */
826         if (size > 100 * 1024) {
827                 rec_size = size * 2;
828         } else {
829                 rec_size = size * 100;
830         }
831
832         /* always make room for at least rec_size more records, and at
833            least 25% more space. if the DB is smaller than 100MiB,
834            otherwise grow it by 10% only. */
835         if (old_size > 100 * 1024 * 1024) {
836                 map_size = old_size / 10;
837         } else {
838                 map_size = old_size / 4;
839         }
840
841         if (map_size > rec_size) {
842                 wanted = map_size;
843         } else {
844                 wanted = rec_size;
845         }
846
847         /* We need room for the record header too. */
848         wanted = adjust_size(0, sizeof(struct tdb_used_record) + wanted);
849
850         ecode = tdb->methods->expand_file(tdb, wanted);
851         if (ecode != TDB_SUCCESS) {
852                 tdb_unlock_expand(tdb, F_WRLCK);
853                 return ecode;
854         }
855
856         /* We need to drop this lock before adding free record. */
857         tdb_unlock_expand(tdb, F_WRLCK);
858
859         tdb->stats.expands++;
860         return add_free_record(tdb, old_size, wanted, TDB_LOCK_WAIT, true);
861 }
862
863 /* This won't fail: it will expand the database if it has to. */
864 tdb_off_t alloc(struct tdb_context *tdb, size_t keylen, size_t datalen,
865                 uint64_t hash, unsigned magic, bool growing)
866 {
867         tdb_off_t off;
868
869         /* We can't hold pointers during this: we could unmap! */
870         assert(!tdb->direct_access);
871
872         for (;;) {
873                 enum TDB_ERROR ecode;
874                 off = get_free(tdb, keylen, datalen, growing, magic, hash);
875                 if (likely(off != 0))
876                         break;
877
878                 ecode = tdb_expand(tdb, adjust_size(keylen, datalen));
879                 if (ecode != TDB_SUCCESS) {
880                         return ecode;
881                 }
882         }
883
884         return off;
885 }