4f9a52a1873d4e64a5f399b12dddb825b0bc3a95
[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 int 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 (off == TDB_OFF_ERR)
73                         return -1;
74
75                 rnd = random();
76                 if (rnd >= max) {
77                         tdb->ftable_off = off;
78                         tdb->ftable = count;
79                         max = rnd;
80                 }
81
82                 off = next_ftable(tdb, off);
83                 count++;
84         }
85         return 0;
86 }
87
88 /* Offset of a given bucket. */
89 tdb_off_t bucket_off(tdb_off_t ftable_off, unsigned bucket)
90 {
91         return ftable_off + offsetof(struct tdb_freetable, buckets)
92                 + bucket * sizeof(tdb_off_t);
93 }
94
95 /* Returns free_buckets + 1, or list number to search. */
96 static tdb_off_t find_free_head(struct tdb_context *tdb,
97                                 tdb_off_t ftable_off,
98                                 tdb_off_t bucket)
99 {
100         /* Speculatively search for a non-zero bucket. */
101         return tdb_find_nonzero_off(tdb, bucket_off(ftable_off, 0),
102                                     bucket, TDB_FREE_BUCKETS);
103 }
104
105 /* Remove from free bucket. */
106 static int remove_from_list(struct tdb_context *tdb,
107                             tdb_off_t b_off, tdb_off_t r_off,
108                             const struct tdb_free_record *r)
109 {
110         tdb_off_t off;
111         enum TDB_ERROR ecode;
112
113         /* Front of list? */
114         if (frec_prev(r) == 0) {
115                 off = b_off;
116         } else {
117                 off = frec_prev(r) + offsetof(struct tdb_free_record, next);
118         }
119
120 #ifdef CCAN_TDB2_DEBUG
121         if (tdb_read_off(tdb, off) != r_off) {
122                 tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
123                          "remove_from_list: %llu bad prev in list %llu",
124                          (long long)r_off, (long long)b_off);
125                 return -1;
126         }
127 #endif
128
129         /* r->prev->next = r->next */
130         ecode = tdb_write_off(tdb, off, r->next);
131         if (ecode != TDB_SUCCESS) {
132                 tdb->ecode = ecode;
133                 return -1;
134         }
135
136         if (r->next != 0) {
137                 off = r->next + offsetof(struct tdb_free_record,magic_and_prev);
138                 /* r->next->prev = r->prev */
139
140 #ifdef CCAN_TDB2_DEBUG
141                 if (tdb_read_off(tdb, off) & TDB_OFF_MASK != r_off) {
142                         tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
143                                    "remove_from_list: %llu bad list %llu",
144                                    (long long)r_off, (long long)b_off);
145                         return -1;
146                 }
147 #endif
148
149                 ecode = tdb_write_off(tdb, off, r->magic_and_prev);
150                 if (ecode != TDB_SUCCESS) {
151                         tdb->ecode = ecode;
152                         return -1;
153                 }
154         }
155         return 0;
156 }
157
158 /* Enqueue in this free bucket. */
159 static int enqueue_in_free(struct tdb_context *tdb,
160                            tdb_off_t b_off,
161                            tdb_off_t off,
162                            tdb_len_t len)
163 {
164         struct tdb_free_record new;
165         enum TDB_ERROR ecode;
166         uint64_t magic = (TDB_FREE_MAGIC << (64 - TDB_OFF_UPPER_STEAL));
167
168         /* We only need to set ftable_and_len; rest is set in enqueue_in_free */
169         new.ftable_and_len = ((uint64_t)tdb->ftable << (64 - TDB_OFF_UPPER_STEAL))
170                 | len;
171         /* prev = 0. */
172         new.magic_and_prev = magic;
173
174         /* new->next = head. */
175         new.next = tdb_read_off(tdb, b_off);
176         if (new.next == TDB_OFF_ERR)
177                 return -1;
178
179         if (new.next) {
180 #ifdef CCAN_TDB2_DEBUG
181                 if (tdb_read_off(tdb,
182                                  new.next + offsetof(struct tdb_free_record,
183                                                      magic_and_prev))
184                     != magic) {
185                         tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
186                                    "enqueue_in_free: %llu bad head"
187                                    " prev %llu",
188                                    (long long)new.next, (long long)b_off);
189                         return -1;
190                 }
191 #endif
192                 /* next->prev = new. */
193                 ecode = tdb_write_off(tdb, new.next
194                                       + offsetof(struct tdb_free_record,
195                                                  magic_and_prev),
196                                       off | magic);
197                 if (ecode != TDB_SUCCESS) {
198                         tdb->ecode = ecode;
199                         return -1;
200                 }
201         }
202         /* head = new */
203         ecode = tdb_write_off(tdb, b_off, off);
204         if (ecode != TDB_SUCCESS) {
205                 tdb->ecode = ecode;
206                 return -1;
207         }
208
209         ecode = tdb_write_convert(tdb, off, &new, sizeof(new));
210         if (ecode != TDB_SUCCESS) {
211                 tdb->ecode = ecode;
212                 return -1;
213         }
214         return 0;
215 }
216
217 /* List need not be locked. */
218 int add_free_record(struct tdb_context *tdb,
219                     tdb_off_t off, tdb_len_t len_with_header)
220 {
221         tdb_off_t b_off;
222         tdb_len_t len;
223         int ret;
224         enum TDB_ERROR ecode;
225
226         assert(len_with_header >= sizeof(struct tdb_free_record));
227
228         len = len_with_header - sizeof(struct tdb_used_record);
229
230         b_off = bucket_off(tdb->ftable_off, size_to_bucket(len));
231         ecode = tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT);
232         if (ecode != TDB_SUCCESS) {
233                 tdb->ecode = ecode;
234                 return -1;
235         }
236
237         ret = enqueue_in_free(tdb, b_off, off, len);
238         tdb_unlock_free_bucket(tdb, b_off);
239         return ret;
240 }
241
242 static size_t adjust_size(size_t keylen, size_t datalen)
243 {
244         size_t size = keylen + datalen;
245
246         if (size < TDB_MIN_DATA_LEN)
247                 size = TDB_MIN_DATA_LEN;
248
249         /* Round to next uint64_t boundary. */
250         return (size + (sizeof(uint64_t) - 1ULL)) & ~(sizeof(uint64_t) - 1ULL);
251 }
252
253 /* If we have enough left over to be useful, split that off. */
254 static size_t record_leftover(size_t keylen, size_t datalen,
255                               bool want_extra, size_t total_len)
256 {
257         ssize_t leftover;
258
259         if (want_extra)
260                 datalen += datalen / 2;
261         leftover = total_len - adjust_size(keylen, datalen);
262
263         if (leftover < (ssize_t)sizeof(struct tdb_free_record))
264                 return 0;
265
266         return leftover;
267 }
268
269 static tdb_off_t ftable_offset(struct tdb_context *tdb, unsigned int ftable)
270 {
271         tdb_off_t off;
272         unsigned int i;
273
274         if (likely(tdb->ftable == ftable))
275                 return tdb->ftable_off;
276
277         off = first_ftable(tdb);
278         for (i = 0; i < ftable; i++)
279                 off = next_ftable(tdb, off);
280         return off;
281 }
282
283 /* Note: we unlock the current bucket if we coalesce or fail. */
284 static int coalesce(struct tdb_context *tdb,
285                     tdb_off_t off, tdb_off_t b_off, tdb_len_t data_len)
286 {
287         tdb_off_t end;
288         struct tdb_free_record rec;
289         enum TDB_ERROR ecode;
290
291         add_stat(tdb, alloc_coalesce_tried, 1);
292         end = off + sizeof(struct tdb_used_record) + data_len;
293
294         while (end < tdb->map_size) {
295                 const struct tdb_free_record *r;
296                 tdb_off_t nb_off;
297                 unsigned ftable, bucket;
298
299                 r = tdb_access_read(tdb, end, sizeof(*r), true);
300                 if (TDB_PTR_IS_ERR(r)) {
301                         tdb->ecode = TDB_PTR_ERR(r);
302                         goto err;
303                 }
304
305                 if (frec_magic(r) != TDB_FREE_MAGIC
306                     || frec_ftable(r) == TDB_FTABLE_NONE) {
307                         tdb_access_release(tdb, r);
308                         break;
309                 }
310
311                 ftable = frec_ftable(r);
312                 bucket = size_to_bucket(frec_len(r));
313                 nb_off = bucket_off(ftable_offset(tdb, ftable), bucket);
314                 tdb_access_release(tdb, r);
315
316                 /* We may be violating lock order here, so best effort. */
317                 if (tdb_lock_free_bucket(tdb, nb_off, TDB_LOCK_NOWAIT)
318                     != TDB_SUCCESS) {
319                         add_stat(tdb, alloc_coalesce_lockfail, 1);
320                         break;
321                 }
322
323                 /* Now we have lock, re-check. */
324                 ecode = tdb_read_convert(tdb, end, &rec, sizeof(rec));
325                 if (ecode != TDB_SUCCESS) {
326                         tdb->ecode = ecode;
327                         tdb_unlock_free_bucket(tdb, nb_off);
328                         goto err;
329                 }
330
331                 if (unlikely(frec_magic(&rec) != TDB_FREE_MAGIC)) {
332                         add_stat(tdb, alloc_coalesce_race, 1);
333                         tdb_unlock_free_bucket(tdb, nb_off);
334                         break;
335                 }
336
337                 if (unlikely(frec_ftable(&rec) != ftable)
338                     || unlikely(size_to_bucket(frec_len(&rec)) != bucket)) {
339                         add_stat(tdb, alloc_coalesce_race, 1);
340                         tdb_unlock_free_bucket(tdb, nb_off);
341                         break;
342                 }
343
344                 if (remove_from_list(tdb, nb_off, end, &rec) == -1) {
345                         tdb_unlock_free_bucket(tdb, nb_off);
346                         goto err;
347                 }
348
349                 end += sizeof(struct tdb_used_record) + frec_len(&rec);
350                 tdb_unlock_free_bucket(tdb, nb_off);
351                 add_stat(tdb, alloc_coalesce_num_merged, 1);
352         }
353
354         /* Didn't find any adjacent free? */
355         if (end == off + sizeof(struct tdb_used_record) + data_len)
356                 return 0;
357
358         /* OK, expand initial record */
359         ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
360         if (ecode != TDB_SUCCESS) {
361                 tdb->ecode = ecode;
362                 goto err;
363         }
364
365         if (frec_len(&rec) != data_len) {
366                 tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
367                            "coalesce: expected data len %zu not %zu",
368                            (size_t)data_len, (size_t)frec_len(&rec));
369                 goto err;
370         }
371
372         if (remove_from_list(tdb, b_off, off, &rec) == -1)
373                 goto err;
374
375         /* We have to drop this to avoid deadlocks, so make sure record
376          * doesn't get coalesced by someone else! */
377         rec.ftable_and_len = (TDB_FTABLE_NONE << (64 - TDB_OFF_UPPER_STEAL))
378                 | (end - off - sizeof(struct tdb_used_record));
379         ecode = tdb_write_off(tdb, off + offsetof(struct tdb_free_record,
380                                                   ftable_and_len),
381                               rec.ftable_and_len);
382         if (ecode != TDB_SUCCESS) {
383                 tdb->ecode = ecode;
384                 goto err;
385         }
386
387         add_stat(tdb, alloc_coalesce_succeeded, 1);
388         tdb_unlock_free_bucket(tdb, b_off);
389
390         if (add_free_record(tdb, off, end - off) == -1)
391                 return -1;
392         return 1;
393
394 err:
395         /* To unify error paths, we *always* unlock bucket on error. */
396         tdb_unlock_free_bucket(tdb, b_off);
397         return -1;
398 }
399
400 /* We need size bytes to put our key and data in. */
401 static tdb_off_t lock_and_alloc(struct tdb_context *tdb,
402                                 tdb_off_t ftable_off,
403                                 tdb_off_t bucket,
404                                 size_t keylen, size_t datalen,
405                                 bool want_extra,
406                                 unsigned magic,
407                                 unsigned hashlow)
408 {
409         tdb_off_t off, b_off,best_off;
410         struct tdb_free_record best = { 0 };
411         double multiplier;
412         size_t size = adjust_size(keylen, datalen);
413         enum TDB_ERROR ecode;
414
415         add_stat(tdb, allocs, 1);
416 again:
417         b_off = bucket_off(ftable_off, bucket);
418
419         /* FIXME: Try non-blocking wait first, to measure contention. */
420         /* Lock this bucket. */
421         ecode = tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT);
422         if (ecode != TDB_SUCCESS) {
423                 tdb->ecode = ecode;
424                 return TDB_OFF_ERR;
425         }
426
427         best.ftable_and_len = -1ULL;
428         best_off = 0;
429
430         /* Get slack if we're after extra. */
431         if (want_extra)
432                 multiplier = 1.5;
433         else
434                 multiplier = 1.0;
435
436         /* Walk the list to see if any are large enough, getting less fussy
437          * as we go. */
438         off = tdb_read_off(tdb, b_off);
439         if (unlikely(off == TDB_OFF_ERR))
440                 goto unlock_err;
441
442         while (off) {
443                 const struct tdb_free_record *r;
444                 tdb_len_t len;
445                 tdb_off_t next;
446
447                 r = tdb_access_read(tdb, off, sizeof(*r), true);
448                 if (TDB_PTR_IS_ERR(r)) {
449                         tdb->ecode = TDB_PTR_ERR(r);
450                         goto unlock_err;
451                 }
452
453                 if (frec_magic(r) != TDB_FREE_MAGIC) {
454                         tdb_access_release(tdb, r);
455                         tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
456                                  "lock_and_alloc: %llu non-free 0x%llx",
457                                  (long long)off, (long long)r->magic_and_prev);
458                         goto unlock_err;
459                 }
460
461                 if (frec_len(r) >= size && frec_len(r) < frec_len(&best)) {
462                         best_off = off;
463                         best = *r;
464                 }
465
466                 if (frec_len(&best) < size * multiplier && best_off) {
467                         tdb_access_release(tdb, r);
468                         break;
469                 }
470
471                 multiplier *= 1.01;
472
473                 next = r->next;
474                 len = frec_len(r);
475                 tdb_access_release(tdb, r);
476
477                 /* Since we're going slow anyway, try coalescing here. */
478                 switch (coalesce(tdb, off, b_off, len)) {
479                 case -1:
480                         /* This has already unlocked on error. */
481                         return -1;
482                 case 1:
483                         /* This has unlocked list, restart. */
484                         goto again;
485                 }
486                 off = next;
487         }
488
489         /* If we found anything at all, use it. */
490         if (best_off) {
491                 struct tdb_used_record rec;
492                 size_t leftover;
493
494                 /* We're happy with this size: take it. */
495                 if (remove_from_list(tdb, b_off, best_off, &best) != 0)
496                         goto unlock_err;
497
498                 leftover = record_leftover(keylen, datalen, want_extra,
499                                            frec_len(&best));
500
501                 assert(keylen + datalen + leftover <= frec_len(&best));
502                 /* We need to mark non-free before we drop lock, otherwise
503                  * coalesce() could try to merge it! */
504                 if (set_header(tdb, &rec, magic, keylen, datalen,
505                                frec_len(&best) - leftover, hashlow) != 0)
506                         goto unlock_err;
507
508                 ecode = tdb_write_convert(tdb, best_off, &rec, sizeof(rec));
509                 if (ecode != TDB_SUCCESS) {
510                         tdb->ecode = ecode;
511                         goto unlock_err;
512                 }
513
514                 /* Bucket of leftover will be <= current bucket, so nested
515                  * locking is allowed. */
516                 if (leftover) {
517                         add_stat(tdb, alloc_leftover, 1);
518                         if (add_free_record(tdb,
519                                             best_off + sizeof(rec)
520                                             + frec_len(&best) - leftover,
521                                             leftover))
522                                 best_off = TDB_OFF_ERR;
523                 }
524                 tdb_unlock_free_bucket(tdb, b_off);
525
526                 return best_off;
527         }
528
529         tdb_unlock_free_bucket(tdb, b_off);
530         return 0;
531
532 unlock_err:
533         tdb_unlock_free_bucket(tdb, b_off);
534         return TDB_OFF_ERR;
535 }
536
537 /* Get a free block from current free list, or 0 if none. */
538 static tdb_off_t get_free(struct tdb_context *tdb,
539                           size_t keylen, size_t datalen, bool want_extra,
540                           unsigned magic, unsigned hashlow)
541 {
542         tdb_off_t off, ftable_off;
543         unsigned start_b, b, ftable;
544         bool wrapped = false;
545
546         /* If they are growing, add 50% to get to higher bucket. */
547         if (want_extra)
548                 start_b = size_to_bucket(adjust_size(keylen,
549                                                      datalen + datalen / 2));
550         else
551                 start_b = size_to_bucket(adjust_size(keylen, datalen));
552
553         ftable_off = tdb->ftable_off;
554         ftable = tdb->ftable;
555         while (!wrapped || ftable_off != tdb->ftable_off) {
556                 /* Start at exact size bucket, and search up... */
557                 for (b = find_free_head(tdb, ftable_off, start_b);
558                      b < TDB_FREE_BUCKETS;
559                      b = find_free_head(tdb, ftable_off, b + 1)) {
560                         /* Try getting one from list. */
561                         off = lock_and_alloc(tdb, ftable_off,
562                                              b, keylen, datalen, want_extra,
563                                              magic, hashlow);
564                         if (off == TDB_OFF_ERR)
565                                 return TDB_OFF_ERR;
566                         if (off != 0) {
567                                 if (b == start_b)
568                                         add_stat(tdb, alloc_bucket_exact, 1);
569                                 if (b == TDB_FREE_BUCKETS - 1)
570                                         add_stat(tdb, alloc_bucket_max, 1);
571                                 /* Worked?  Stay using this list. */
572                                 tdb->ftable_off = ftable_off;
573                                 tdb->ftable = ftable;
574                                 return off;
575                         }
576                         /* Didn't work.  Try next bucket. */
577                 }
578
579                 /* Hmm, try next table. */
580                 ftable_off = next_ftable(tdb, ftable_off);
581                 ftable++;
582
583                 if (ftable_off == 0) {
584                         wrapped = true;
585                         ftable_off = first_ftable(tdb);
586                         ftable = 0;
587                 }
588         }
589
590         return 0;
591 }
592
593 int set_header(struct tdb_context *tdb,
594                struct tdb_used_record *rec,
595                unsigned magic, uint64_t keylen, uint64_t datalen,
596                uint64_t actuallen, unsigned hashlow)
597 {
598         uint64_t keybits = (fls64(keylen) + 1) / 2;
599
600         /* Use bottom bits of hash, so it's independent of hash table size. */
601         rec->magic_and_meta = (hashlow & ((1 << 11)-1))
602                 | ((actuallen - (keylen + datalen)) << 11)
603                 | (keybits << 43)
604                 | ((uint64_t)magic << 48);
605         rec->key_and_data_len = (keylen | (datalen << (keybits*2)));
606
607         /* Encoding can fail on big values. */
608         if (rec_key_length(rec) != keylen
609             || rec_data_length(rec) != datalen
610             || rec_extra_padding(rec) != actuallen - (keylen + datalen)) {
611                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
612                          "Could not encode k=%llu,d=%llu,a=%llu",
613                          (long long)keylen, (long long)datalen,
614                          (long long)actuallen);
615                 return -1;
616         }
617         return 0;
618 }
619
620 /* Expand the database. */
621 static int tdb_expand(struct tdb_context *tdb, tdb_len_t size)
622 {
623         uint64_t old_size;
624         tdb_len_t wanted;
625         enum TDB_ERROR ecode;
626
627         /* We need room for the record header too. */
628         wanted = sizeof(struct tdb_used_record) + size;
629
630         /* Need to hold a hash lock to expand DB: transactions rely on it. */
631         if (!(tdb->flags & TDB_NOLOCK)
632             && !tdb->allrecord_lock.count && !tdb_has_hash_locks(tdb)) {
633                 tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
634                            "tdb_expand: must hold lock during expand");
635                 return -1;
636         }
637
638         /* always make room for at least 100 more records, and at
639            least 25% more space. */
640         if (size * TDB_EXTENSION_FACTOR > tdb->map_size / 4)
641                 wanted = size * TDB_EXTENSION_FACTOR;
642         else
643                 wanted = tdb->map_size / 4;
644         wanted = adjust_size(0, wanted);
645
646         /* Only one person can expand file at a time. */
647         ecode = tdb_lock_expand(tdb, F_WRLCK);
648         if (ecode != TDB_SUCCESS) {
649                 tdb->ecode = ecode;
650                 return -1;
651         }
652
653         /* Someone else may have expanded the file, so retry. */
654         old_size = tdb->map_size;
655         tdb->methods->oob(tdb, tdb->map_size + 1, true);
656         if (tdb->map_size != old_size) {
657                 tdb_unlock_expand(tdb, F_WRLCK);
658                 return 0;
659         }
660
661         ecode = tdb->methods->expand_file(tdb, wanted);
662         if (ecode != TDB_SUCCESS) {
663                 tdb->ecode = ecode;
664                 tdb_unlock_expand(tdb, F_WRLCK);
665                 return -1;
666         }
667
668         /* We need to drop this lock before adding free record. */
669         tdb_unlock_expand(tdb, F_WRLCK);
670
671         add_stat(tdb, expands, 1);
672         return add_free_record(tdb, old_size, wanted);
673 }
674
675 /* This won't fail: it will expand the database if it has to. */
676 tdb_off_t alloc(struct tdb_context *tdb, size_t keylen, size_t datalen,
677                 uint64_t hash, unsigned magic, bool growing)
678 {
679         tdb_off_t off;
680
681         /* We can't hold pointers during this: we could unmap! */
682         assert(!tdb->direct_access);
683
684         for (;;) {
685                 off = get_free(tdb, keylen, datalen, growing, magic, hash);
686                 if (likely(off != 0))
687                         break;
688
689                 if (tdb_expand(tdb, adjust_size(keylen, datalen)))
690                         return TDB_OFF_ERR;
691         }
692
693         return off;
694 }