ntdb: inline oob check
[kai/samba-autobuild/.git] / lib / ntdb / 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(ntdb_len_t data_len)
32 {
33         unsigned int bucket;
34
35         /* We can't have records smaller than this. */
36         assert(data_len >= NTDB_MIN_DATA_LEN);
37
38         /* Ignoring the header... */
39         if (data_len - NTDB_MIN_DATA_LEN <= 64) {
40                 /* 0 in bucket 0, 8 in bucket 1... 64 in bucket 8. */
41                 bucket = (data_len - NTDB_MIN_DATA_LEN) / 8;
42         } else {
43                 /* After that we go power of 2. */
44                 bucket = fls64(data_len - NTDB_MIN_DATA_LEN) + 2;
45         }
46
47         if (unlikely(bucket >= NTDB_FREE_BUCKETS))
48                 bucket = NTDB_FREE_BUCKETS - 1;
49         return bucket;
50 }
51
52 ntdb_off_t first_ftable(struct ntdb_context *ntdb)
53 {
54         return ntdb_read_off(ntdb, offsetof(struct ntdb_header, free_table));
55 }
56
57 ntdb_off_t next_ftable(struct ntdb_context *ntdb, ntdb_off_t ftable)
58 {
59         return ntdb_read_off(ntdb, ftable + offsetof(struct ntdb_freetable,next));
60 }
61
62 enum NTDB_ERROR ntdb_ftable_init(struct ntdb_context *ntdb)
63 {
64         /* Use reservoir sampling algorithm to select a free list at random. */
65         unsigned int rnd, max = 0, count = 0;
66         ntdb_off_t off;
67
68         ntdb->ftable_off = off = first_ftable(ntdb);
69         ntdb->ftable = 0;
70
71         while (off) {
72                 if (NTDB_OFF_IS_ERR(off)) {
73                         return NTDB_OFF_TO_ERR(off);
74                 }
75
76                 rnd = random();
77                 if (rnd >= max) {
78                         ntdb->ftable_off = off;
79                         ntdb->ftable = count;
80                         max = rnd;
81                 }
82
83                 off = next_ftable(ntdb, off);
84                 count++;
85         }
86         return NTDB_SUCCESS;
87 }
88
89 /* Offset of a given bucket. */
90 ntdb_off_t bucket_off(ntdb_off_t ftable_off, unsigned bucket)
91 {
92         return ftable_off + offsetof(struct ntdb_freetable, buckets)
93                 + bucket * sizeof(ntdb_off_t);
94 }
95
96 /* Returns free_buckets + 1, or list number to search, or -ve error. */
97 static ntdb_off_t find_free_head(struct ntdb_context *ntdb,
98                                 ntdb_off_t ftable_off,
99                                 ntdb_off_t bucket)
100 {
101         /* Speculatively search for a non-zero bucket. */
102         return ntdb_find_nonzero_off(ntdb, bucket_off(ftable_off, 0),
103                                     bucket, NTDB_FREE_BUCKETS);
104 }
105
106 static void check_list(struct ntdb_context *ntdb, ntdb_off_t b_off)
107 {
108 #ifdef CCAN_NTDB_DEBUG
109         ntdb_off_t off, prev = 0, first;
110         struct ntdb_free_record r;
111
112         first = off = (ntdb_read_off(ntdb, b_off) & NTDB_OFF_MASK);
113         while (off != 0) {
114                 ntdb_read_convert(ntdb, off, &r, sizeof(r));
115                 if (frec_magic(&r) != NTDB_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                 ntdb_read_convert(ntdb, 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 NTDB_ERROR remove_from_list(struct ntdb_context *ntdb,
133                                        ntdb_off_t b_off, ntdb_off_t r_off,
134                                        const struct ntdb_free_record *r)
135 {
136         ntdb_off_t off, prev_next, head;
137         enum NTDB_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 ntdb_write_off(ntdb, b_off, 0);
142
143         /* off = &r->prev->next */
144         off = frec_prev(r) + offsetof(struct ntdb_free_record, next);
145
146         /* Get prev->next */
147         prev_next = ntdb_read_off(ntdb, off);
148         if (NTDB_OFF_IS_ERR(prev_next))
149                 return NTDB_OFF_TO_ERR(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 = ntdb_read_off(ntdb, b_off);
155                 if (NTDB_OFF_IS_ERR(head))
156                         return NTDB_OFF_TO_ERR(head);
157
158                 if ((head & NTDB_OFF_MASK) != r_off) {
159                         return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_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 & ~NTDB_OFF_MASK) | r->next);
167                 ecode = ntdb_write_off(ntdb, b_off, head);
168                 if (ecode != NTDB_SUCCESS)
169                         return ecode;
170         } else {
171                 /* r->prev->next = r->next */
172                 ecode = ntdb_write_off(ntdb, off, r->next);
173                 if (ecode != NTDB_SUCCESS)
174                         return ecode;
175         }
176
177         /* If we were the tail, off = &head->prev. */
178         if (r->next == 0) {
179                 head = ntdb_read_off(ntdb, b_off);
180                 if (NTDB_OFF_IS_ERR(head))
181                         return NTDB_OFF_TO_ERR(head);
182                 head &= NTDB_OFF_MASK;
183                 off = head + offsetof(struct ntdb_free_record, magic_and_prev);
184         } else {
185                 /* off = &r->next->prev */
186                 off = r->next + offsetof(struct ntdb_free_record,
187                                          magic_and_prev);
188         }
189
190 #ifdef CCAN_NTDB_DEBUG
191         /* *off == r */
192         if ((ntdb_read_off(ntdb, off) & NTDB_OFF_MASK) != r_off) {
193                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_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 ntdb_write_off(ntdb, 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 NTDB_ERROR enqueue_in_free(struct ntdb_context *ntdb,
206                                       ntdb_off_t b_off,
207                                       ntdb_off_t off,
208                                       ntdb_len_t len,
209                                       bool *coalesce)
210 {
211         struct ntdb_free_record new;
212         enum NTDB_ERROR ecode;
213         ntdb_off_t prev, head;
214         uint64_t magic = (NTDB_FREE_MAGIC << (64 - NTDB_OFF_UPPER_STEAL));
215
216         head = ntdb_read_off(ntdb, b_off);
217         if (NTDB_OFF_IS_ERR(head))
218                 return NTDB_OFF_TO_ERR(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)ntdb->ftable
222                               << (64 - NTDB_OFF_UPPER_STEAL))
223                 | len;
224
225         /* new->next = head. */
226         new.next = (head & NTDB_OFF_MASK);
227
228         /* First element?  Prev points to ourselves. */
229         if (!new.next) {
230                 new.magic_and_prev = (magic | off);
231         } else {
232                 /* new->prev = next->prev */
233                 prev = ntdb_read_off(ntdb,
234                                     new.next + offsetof(struct ntdb_free_record,
235                                                         magic_and_prev));
236                 new.magic_and_prev = prev;
237                 if (frec_magic(&new) != NTDB_FREE_MAGIC) {
238                         return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
239                                           "enqueue_in_free: %llu bad head"
240                                           " prev %llu",
241                                           (long long)new.next,
242                                           (long long)prev);
243                 }
244                 /* next->prev = new. */
245                 ecode = ntdb_write_off(ntdb, new.next
246                                       + offsetof(struct ntdb_free_record,
247                                                  magic_and_prev),
248                                       off | magic);
249                 if (ecode != NTDB_SUCCESS) {
250                         return ecode;
251                 }
252
253 #ifdef CCAN_NTDB_DEBUG
254                 prev = ntdb_read_off(ntdb, frec_prev(&new)
255                                     + offsetof(struct ntdb_free_record, next));
256                 if (prev != 0) {
257                         return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
258                                           "enqueue_in_free:"
259                                           " %llu bad tail next ptr %llu",
260                                           (long long)frec_prev(&new)
261                                           + offsetof(struct ntdb_free_record,
262                                                      next),
263                                           (long long)prev);
264                 }
265 #endif
266         }
267
268         /* Update enqueue count, but don't set high bit: see NTDB_OFF_IS_ERR */
269         if (*coalesce)
270                 head += (1ULL << (64 - NTDB_OFF_UPPER_STEAL));
271         head &= ~(NTDB_OFF_MASK | (1ULL << 63));
272         head |= off;
273
274         ecode = ntdb_write_off(ntdb, b_off, head);
275         if (ecode != NTDB_SUCCESS) {
276                 return ecode;
277         }
278
279         /* It's time to coalesce if counter wrapped. */
280         if (*coalesce)
281                 *coalesce = ((head & ~NTDB_OFF_MASK) == 0);
282
283         return ntdb_write_convert(ntdb, off, &new, sizeof(new));
284 }
285
286 static ntdb_off_t ftable_offset(struct ntdb_context *ntdb, unsigned int ftable)
287 {
288         ntdb_off_t off;
289         unsigned int i;
290
291         if (likely(ntdb->ftable == ftable))
292                 return ntdb->ftable_off;
293
294         off = first_ftable(ntdb);
295         for (i = 0; i < ftable; i++) {
296                 if (NTDB_OFF_IS_ERR(off)) {
297                         break;
298                 }
299                 off = next_ftable(ntdb, off);
300         }
301         return off;
302 }
303
304 /* Note: we unlock the current bucket if fail (-ve), or coalesce (+ve) and
305  * need to blatt the *protect record (which is set to an error). */
306 static ntdb_len_t coalesce(struct ntdb_context *ntdb,
307                           ntdb_off_t off, ntdb_off_t b_off,
308                           ntdb_len_t data_len,
309                           ntdb_off_t *protect)
310 {
311         ntdb_off_t end;
312         struct ntdb_free_record rec;
313         enum NTDB_ERROR ecode;
314
315         ntdb->stats.alloc_coalesce_tried++;
316         end = off + sizeof(struct ntdb_used_record) + data_len;
317
318         while (end < ntdb->file->map_size) {
319                 const struct ntdb_free_record *r;
320                 ntdb_off_t nb_off;
321                 unsigned ftable, bucket;
322
323                 r = ntdb_access_read(ntdb, end, sizeof(*r), true);
324                 if (NTDB_PTR_IS_ERR(r)) {
325                         ecode = NTDB_PTR_ERR(r);
326                         goto err;
327                 }
328
329                 if (frec_magic(r) != NTDB_FREE_MAGIC
330                     || frec_ftable(r) == NTDB_FTABLE_NONE) {
331                         ntdb_access_release(ntdb, r);
332                         break;
333                 }
334
335                 ftable = frec_ftable(r);
336                 bucket = size_to_bucket(frec_len(r));
337                 nb_off = ftable_offset(ntdb, ftable);
338                 if (NTDB_OFF_IS_ERR(nb_off)) {
339                         ntdb_access_release(ntdb, r);
340                         ecode = NTDB_OFF_TO_ERR(nb_off);
341                         goto err;
342                 }
343                 nb_off = bucket_off(nb_off, bucket);
344                 ntdb_access_release(ntdb, r);
345
346                 /* We may be violating lock order here, so best effort. */
347                 if (ntdb_lock_free_bucket(ntdb, nb_off, NTDB_LOCK_NOWAIT)
348                     != NTDB_SUCCESS) {
349                         ntdb->stats.alloc_coalesce_lockfail++;
350                         break;
351                 }
352
353                 /* Now we have lock, re-check. */
354                 ecode = ntdb_read_convert(ntdb, end, &rec, sizeof(rec));
355                 if (ecode != NTDB_SUCCESS) {
356                         ntdb_unlock_free_bucket(ntdb, nb_off);
357                         goto err;
358                 }
359
360                 if (unlikely(frec_magic(&rec) != NTDB_FREE_MAGIC)) {
361                         ntdb->stats.alloc_coalesce_race++;
362                         ntdb_unlock_free_bucket(ntdb, nb_off);
363                         break;
364                 }
365
366                 if (unlikely(frec_ftable(&rec) != ftable)
367                     || unlikely(size_to_bucket(frec_len(&rec)) != bucket)) {
368                         ntdb->stats.alloc_coalesce_race++;
369                         ntdb_unlock_free_bucket(ntdb, nb_off);
370                         break;
371                 }
372
373                 /* Did we just mess up a record you were hoping to use? */
374                 if (end == *protect) {
375                         ntdb->stats.alloc_coalesce_iterate_clash++;
376                         *protect = NTDB_ERR_TO_OFF(NTDB_ERR_NOEXIST);
377                 }
378
379                 ecode = remove_from_list(ntdb, nb_off, end, &rec);
380                 check_list(ntdb, nb_off);
381                 if (ecode != NTDB_SUCCESS) {
382                         ntdb_unlock_free_bucket(ntdb, nb_off);
383                         goto err;
384                 }
385
386                 end += sizeof(struct ntdb_used_record) + frec_len(&rec);
387                 ntdb_unlock_free_bucket(ntdb, nb_off);
388                 ntdb->stats.alloc_coalesce_num_merged++;
389         }
390
391         /* Didn't find any adjacent free? */
392         if (end == off + sizeof(struct ntdb_used_record) + data_len)
393                 return 0;
394
395         /* Before we expand, check this isn't one you wanted protected? */
396         if (off == *protect) {
397                 *protect = NTDB_ERR_TO_OFF(NTDB_ERR_EXISTS);
398                 ntdb->stats.alloc_coalesce_iterate_clash++;
399         }
400
401         /* OK, expand initial record */
402         ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec));
403         if (ecode != NTDB_SUCCESS) {
404                 goto err;
405         }
406
407         if (frec_len(&rec) != data_len) {
408                 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
409                                    "coalesce: expected data len %zu not %zu",
410                                    (size_t)data_len, (size_t)frec_len(&rec));
411                 goto err;
412         }
413
414         ecode = remove_from_list(ntdb, b_off, off, &rec);
415         check_list(ntdb, b_off);
416         if (ecode != NTDB_SUCCESS) {
417                 goto err;
418         }
419
420         /* Try locking violation first.  We don't allow coalesce recursion! */
421         ecode = add_free_record(ntdb, off, end - off, NTDB_LOCK_NOWAIT, false);
422         if (ecode != NTDB_SUCCESS) {
423                 /* Need to drop lock.  Can't rely on anything stable. */
424                 ntdb->stats.alloc_coalesce_lockfail++;
425                 *protect = NTDB_ERR_TO_OFF(NTDB_ERR_CORRUPT);
426
427                 /* We have to drop this to avoid deadlocks, so make sure record
428                  * doesn't get coalesced by someone else! */
429                 rec.ftable_and_len = (NTDB_FTABLE_NONE
430                                       << (64 - NTDB_OFF_UPPER_STEAL))
431                         | (end - off - sizeof(struct ntdb_used_record));
432                 ecode = ntdb_write_off(ntdb,
433                                       off + offsetof(struct ntdb_free_record,
434                                                      ftable_and_len),
435                                       rec.ftable_and_len);
436                 if (ecode != NTDB_SUCCESS) {
437                         goto err;
438                 }
439
440                 ntdb_unlock_free_bucket(ntdb, b_off);
441
442                 ecode = add_free_record(ntdb, off, end - off, NTDB_LOCK_WAIT,
443                                         false);
444                 if (ecode != NTDB_SUCCESS) {
445                         return NTDB_ERR_TO_OFF(ecode);
446                 }
447         } else if (NTDB_OFF_IS_ERR(*protect)) {
448                 /* For simplicity, we always drop lock if they can't continue */
449                 ntdb_unlock_free_bucket(ntdb, b_off);
450         }
451         ntdb->stats.alloc_coalesce_succeeded++;
452
453         /* Return usable length. */
454         return end - off - sizeof(struct ntdb_used_record);
455
456 err:
457         /* To unify error paths, we *always* unlock bucket on error. */
458         ntdb_unlock_free_bucket(ntdb, b_off);
459         return NTDB_ERR_TO_OFF(ecode);
460 }
461
462 /* List is locked: we unlock it. */
463 static enum NTDB_ERROR coalesce_list(struct ntdb_context *ntdb,
464                                     ntdb_off_t ftable_off,
465                                     ntdb_off_t b_off,
466                                     unsigned int limit)
467 {
468         enum NTDB_ERROR ecode;
469         ntdb_off_t off;
470
471         off = ntdb_read_off(ntdb, b_off);
472         if (NTDB_OFF_IS_ERR(off)) {
473                 ecode = NTDB_OFF_TO_ERR(off);
474                 goto unlock_err;
475         }
476         /* A little bit of paranoia: counter should be 0. */
477         off &= NTDB_OFF_MASK;
478
479         while (off && limit--) {
480                 struct ntdb_free_record rec;
481                 ntdb_len_t coal;
482                 ntdb_off_t next;
483
484                 ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec));
485                 if (ecode != NTDB_SUCCESS)
486                         goto unlock_err;
487
488                 next = rec.next;
489                 coal = coalesce(ntdb, off, b_off, frec_len(&rec), &next);
490                 if (NTDB_OFF_IS_ERR(coal)) {
491                         /* This has already unlocked on error. */
492                         return NTDB_OFF_TO_ERR(coal);
493                 }
494                 if (NTDB_OFF_IS_ERR(next)) {
495                         /* Coalescing had to unlock, so stop. */
496                         return NTDB_SUCCESS;
497                 }
498                 /* Keep going if we're doing well... */
499                 limit += size_to_bucket(coal / 16 + NTDB_MIN_DATA_LEN);
500                 off = next;
501         }
502
503         /* Now, move those elements to the tail of the list so we get something
504          * else next time. */
505         if (off) {
506                 struct ntdb_free_record oldhrec, newhrec, oldtrec, newtrec;
507                 ntdb_off_t oldhoff, oldtoff, newtoff;
508
509                 /* The record we were up to is the new head. */
510                 ecode = ntdb_read_convert(ntdb, off, &newhrec, sizeof(newhrec));
511                 if (ecode != NTDB_SUCCESS)
512                         goto unlock_err;
513
514                 /* Get the new tail. */
515                 newtoff = frec_prev(&newhrec);
516                 ecode = ntdb_read_convert(ntdb, newtoff, &newtrec,
517                                          sizeof(newtrec));
518                 if (ecode != NTDB_SUCCESS)
519                         goto unlock_err;
520
521                 /* Get the old head. */
522                 oldhoff = ntdb_read_off(ntdb, b_off);
523                 if (NTDB_OFF_IS_ERR(oldhoff)) {
524                         ecode = NTDB_OFF_TO_ERR(oldhoff);
525                         goto unlock_err;
526                 }
527
528                 /* This could happen if they all coalesced away. */
529                 if (oldhoff == off)
530                         goto out;
531
532                 ecode = ntdb_read_convert(ntdb, oldhoff, &oldhrec,
533                                          sizeof(oldhrec));
534                 if (ecode != NTDB_SUCCESS)
535                         goto unlock_err;
536
537                 /* Get the old tail. */
538                 oldtoff = frec_prev(&oldhrec);
539                 ecode = ntdb_read_convert(ntdb, oldtoff, &oldtrec,
540                                          sizeof(oldtrec));
541                 if (ecode != NTDB_SUCCESS)
542                         goto unlock_err;
543
544                 /* Old tail's next points to old head. */
545                 oldtrec.next = oldhoff;
546
547                 /* Old head's prev points to old tail. */
548                 oldhrec.magic_and_prev
549                         = (NTDB_FREE_MAGIC << (64 - NTDB_OFF_UPPER_STEAL))
550                         | oldtoff;
551
552                 /* New tail's next is 0. */
553                 newtrec.next = 0;
554
555                 /* Write out the modified versions. */
556                 ecode = ntdb_write_convert(ntdb, oldtoff, &oldtrec,
557                                           sizeof(oldtrec));
558                 if (ecode != NTDB_SUCCESS)
559                         goto unlock_err;
560
561                 ecode = ntdb_write_convert(ntdb, oldhoff, &oldhrec,
562                                           sizeof(oldhrec));
563                 if (ecode != NTDB_SUCCESS)
564                         goto unlock_err;
565
566                 ecode = ntdb_write_convert(ntdb, newtoff, &newtrec,
567                                           sizeof(newtrec));
568                 if (ecode != NTDB_SUCCESS)
569                         goto unlock_err;
570
571                 /* And finally link in new head. */
572                 ecode = ntdb_write_off(ntdb, b_off, off);
573                 if (ecode != NTDB_SUCCESS)
574                         goto unlock_err;
575         }
576 out:
577         ntdb_unlock_free_bucket(ntdb, b_off);
578         return NTDB_SUCCESS;
579
580 unlock_err:
581         ntdb_unlock_free_bucket(ntdb, b_off);
582         return ecode;
583 }
584
585 /* List must not be locked if coalesce_ok is set. */
586 enum NTDB_ERROR add_free_record(struct ntdb_context *ntdb,
587                                ntdb_off_t off, ntdb_len_t len_with_header,
588                                enum ntdb_lock_flags waitflag,
589                                bool coalesce_ok)
590 {
591         ntdb_off_t b_off;
592         ntdb_len_t len;
593         enum NTDB_ERROR ecode;
594
595         assert(len_with_header >= sizeof(struct ntdb_free_record));
596
597         len = len_with_header - sizeof(struct ntdb_used_record);
598
599         b_off = bucket_off(ntdb->ftable_off, size_to_bucket(len));
600         ecode = ntdb_lock_free_bucket(ntdb, b_off, waitflag);
601         if (ecode != NTDB_SUCCESS) {
602                 return ecode;
603         }
604
605         ecode = enqueue_in_free(ntdb, b_off, off, len, &coalesce_ok);
606         check_list(ntdb, b_off);
607
608         /* Coalescing unlocks free list. */
609         if (!ecode && coalesce_ok)
610                 ecode = coalesce_list(ntdb, ntdb->ftable_off, b_off, 2);
611         else
612                 ntdb_unlock_free_bucket(ntdb, b_off);
613         return ecode;
614 }
615
616 static size_t adjust_size(size_t keylen, size_t datalen)
617 {
618         size_t size = keylen + datalen;
619
620         if (size < NTDB_MIN_DATA_LEN)
621                 size = NTDB_MIN_DATA_LEN;
622
623         /* Round to next uint64_t boundary. */
624         return (size + (sizeof(uint64_t) - 1ULL)) & ~(sizeof(uint64_t) - 1ULL);
625 }
626
627 /* If we have enough left over to be useful, split that off. */
628 static size_t record_leftover(size_t keylen, size_t datalen,
629                               bool want_extra, size_t total_len)
630 {
631         ssize_t leftover;
632
633         if (want_extra)
634                 datalen += datalen / 2;
635         leftover = total_len - adjust_size(keylen, datalen);
636
637         if (leftover < (ssize_t)sizeof(struct ntdb_free_record))
638                 return 0;
639
640         return leftover;
641 }
642
643 /* We need size bytes to put our key and data in. */
644 static ntdb_off_t lock_and_alloc(struct ntdb_context *ntdb,
645                                 ntdb_off_t ftable_off,
646                                 ntdb_off_t bucket,
647                                 size_t keylen, size_t datalen,
648                                 bool want_extra,
649                                 unsigned magic,
650                                 unsigned hashlow)
651 {
652         ntdb_off_t off, b_off,best_off;
653         struct ntdb_free_record best = { 0 };
654         double multiplier;
655         size_t size = adjust_size(keylen, datalen);
656         enum NTDB_ERROR ecode;
657
658         ntdb->stats.allocs++;
659         b_off = bucket_off(ftable_off, bucket);
660
661         /* FIXME: Try non-blocking wait first, to measure contention. */
662         /* Lock this bucket. */
663         ecode = ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT);
664         if (ecode != NTDB_SUCCESS) {
665                 return NTDB_ERR_TO_OFF(ecode);
666         }
667
668         best.ftable_and_len = -1ULL;
669         best_off = 0;
670
671         /* Get slack if we're after extra. */
672         if (want_extra)
673                 multiplier = 1.5;
674         else
675                 multiplier = 1.0;
676
677         /* Walk the list to see if any are large enough, getting less fussy
678          * as we go. */
679         off = ntdb_read_off(ntdb, b_off);
680         if (NTDB_OFF_IS_ERR(off)) {
681                 ecode = NTDB_OFF_TO_ERR(off);
682                 goto unlock_err;
683         }
684         off &= NTDB_OFF_MASK;
685
686         while (off) {
687                 const struct ntdb_free_record *r;
688                 ntdb_len_t len;
689                 ntdb_off_t next;
690
691                 r = ntdb_access_read(ntdb, off, sizeof(*r), true);
692                 if (NTDB_PTR_IS_ERR(r)) {
693                         ecode = NTDB_PTR_ERR(r);
694                         goto unlock_err;
695                 }
696
697                 if (frec_magic(r) != NTDB_FREE_MAGIC) {
698                         ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
699                                            "lock_and_alloc:"
700                                            " %llu non-free 0x%llx",
701                                            (long long)off,
702                                            (long long)r->magic_and_prev);
703                         ntdb_access_release(ntdb, r);
704                         goto unlock_err;
705                 }
706
707                 if (frec_len(r) >= size && frec_len(r) < frec_len(&best)) {
708                         best_off = off;
709                         best = *r;
710                 }
711
712                 if (frec_len(&best) <= size * multiplier && best_off) {
713                         ntdb_access_release(ntdb, r);
714                         break;
715                 }
716
717                 multiplier *= 1.01;
718
719                 next = r->next;
720                 len = frec_len(r);
721                 ntdb_access_release(ntdb, r);
722                 off = next;
723         }
724
725         /* If we found anything at all, use it. */
726         if (best_off) {
727                 struct ntdb_used_record rec;
728                 size_t leftover;
729
730                 /* We're happy with this size: take it. */
731                 ecode = remove_from_list(ntdb, b_off, best_off, &best);
732                 check_list(ntdb, b_off);
733                 if (ecode != NTDB_SUCCESS) {
734                         goto unlock_err;
735                 }
736
737                 leftover = record_leftover(keylen, datalen, want_extra,
738                                            frec_len(&best));
739
740                 assert(keylen + datalen + leftover <= frec_len(&best));
741                 /* We need to mark non-free before we drop lock, otherwise
742                  * coalesce() could try to merge it! */
743                 ecode = set_header(ntdb, &rec, magic, keylen, datalen,
744                                    frec_len(&best) - leftover, hashlow);
745                 if (ecode != NTDB_SUCCESS) {
746                         goto unlock_err;
747                 }
748
749                 ecode = ntdb_write_convert(ntdb, best_off, &rec, sizeof(rec));
750                 if (ecode != NTDB_SUCCESS) {
751                         goto unlock_err;
752                 }
753
754                 /* For futureproofing, we put a 0 in any unused space. */
755                 if (rec_extra_padding(&rec)) {
756                         ecode = ntdb->io->twrite(ntdb, best_off + sizeof(rec)
757                                                 + keylen + datalen, "", 1);
758                         if (ecode != NTDB_SUCCESS) {
759                                 goto unlock_err;
760                         }
761                 }
762
763                 /* Bucket of leftover will be <= current bucket, so nested
764                  * locking is allowed. */
765                 if (leftover) {
766                         ntdb->stats.alloc_leftover++;
767                         ecode = add_free_record(ntdb,
768                                                 best_off + sizeof(rec)
769                                                 + frec_len(&best) - leftover,
770                                                 leftover, NTDB_LOCK_WAIT, false);
771                         if (ecode != NTDB_SUCCESS) {
772                                 best_off = NTDB_ERR_TO_OFF(ecode);
773                         }
774                 }
775                 ntdb_unlock_free_bucket(ntdb, b_off);
776
777                 return best_off;
778         }
779
780         ntdb_unlock_free_bucket(ntdb, b_off);
781         return 0;
782
783 unlock_err:
784         ntdb_unlock_free_bucket(ntdb, b_off);
785         return NTDB_ERR_TO_OFF(ecode);
786 }
787
788 /* Get a free block from current free list, or 0 if none, -ve on error. */
789 static ntdb_off_t get_free(struct ntdb_context *ntdb,
790                           size_t keylen, size_t datalen, bool want_extra,
791                           unsigned magic, unsigned hashlow)
792 {
793         ntdb_off_t off, ftable_off;
794         ntdb_off_t start_b, b, ftable;
795         bool wrapped = false;
796
797         /* If they are growing, add 50% to get to higher bucket. */
798         if (want_extra)
799                 start_b = size_to_bucket(adjust_size(keylen,
800                                                      datalen + datalen / 2));
801         else
802                 start_b = size_to_bucket(adjust_size(keylen, datalen));
803
804         ftable_off = ntdb->ftable_off;
805         ftable = ntdb->ftable;
806         while (!wrapped || ftable_off != ntdb->ftable_off) {
807                 /* Start at exact size bucket, and search up... */
808                 for (b = find_free_head(ntdb, ftable_off, start_b);
809                      b < NTDB_FREE_BUCKETS;
810                      b = find_free_head(ntdb, ftable_off, b + 1)) {
811                         /* Try getting one from list. */
812                         off = lock_and_alloc(ntdb, ftable_off,
813                                              b, keylen, datalen, want_extra,
814                                              magic, hashlow);
815                         if (NTDB_OFF_IS_ERR(off))
816                                 return off;
817                         if (off != 0) {
818                                 if (b == start_b)
819                                         ntdb->stats.alloc_bucket_exact++;
820                                 if (b == NTDB_FREE_BUCKETS - 1)
821                                         ntdb->stats.alloc_bucket_max++;
822                                 /* Worked?  Stay using this list. */
823                                 ntdb->ftable_off = ftable_off;
824                                 ntdb->ftable = ftable;
825                                 return off;
826                         }
827                         /* Didn't work.  Try next bucket. */
828                 }
829
830                 if (NTDB_OFF_IS_ERR(b)) {
831                         return b;
832                 }
833
834                 /* Hmm, try next table. */
835                 ftable_off = next_ftable(ntdb, ftable_off);
836                 if (NTDB_OFF_IS_ERR(ftable_off)) {
837                         return ftable_off;
838                 }
839                 ftable++;
840
841                 if (ftable_off == 0) {
842                         wrapped = true;
843                         ftable_off = first_ftable(ntdb);
844                         if (NTDB_OFF_IS_ERR(ftable_off)) {
845                                 return ftable_off;
846                         }
847                         ftable = 0;
848                 }
849         }
850
851         return 0;
852 }
853
854 enum NTDB_ERROR set_header(struct ntdb_context *ntdb,
855                           struct ntdb_used_record *rec,
856                           unsigned magic, uint64_t keylen, uint64_t datalen,
857                           uint64_t actuallen, unsigned hashlow)
858 {
859         uint64_t keybits = (fls64(keylen) + 1) / 2;
860
861         /* Use bottom bits of hash, so it's independent of hash table size. */
862         rec->magic_and_meta = (hashlow & ((1 << 11)-1))
863                 | ((actuallen - (keylen + datalen)) << 11)
864                 | (keybits << 43)
865                 | ((uint64_t)magic << 48);
866         rec->key_and_data_len = (keylen | (datalen << (keybits*2)));
867
868         /* Encoding can fail on big values. */
869         if (rec_key_length(rec) != keylen
870             || rec_data_length(rec) != datalen
871             || rec_extra_padding(rec) != actuallen - (keylen + datalen)) {
872                 return ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
873                                   "Could not encode k=%llu,d=%llu,a=%llu",
874                                   (long long)keylen, (long long)datalen,
875                                   (long long)actuallen);
876         }
877         return NTDB_SUCCESS;
878 }
879
880 /* You need 'size', this tells you how much you should expand by. */
881 ntdb_off_t ntdb_expand_adjust(ntdb_off_t map_size, ntdb_off_t size)
882 {
883         ntdb_off_t new_size, top_size;
884
885         /* limit size in order to avoid using up huge amounts of memory for
886          * in memory tdbs if an oddball huge record creeps in */
887         if (size > 100 * 1024) {
888                 top_size = map_size + size * 2;
889         } else {
890                 top_size = map_size + size * 100;
891         }
892
893         /* always make room for at least top_size more records, and at
894            least 25% more space. if the DB is smaller than 100MiB,
895            otherwise grow it by 10% only. */
896         if (map_size > 100 * 1024 * 1024) {
897                 new_size = map_size * 1.10;
898         } else {
899                 new_size = map_size * 1.25;
900         }
901
902         if (new_size < top_size)
903                 new_size = top_size;
904
905         /* We always make the file a multiple of transaction page
906          * size.  This guarantees that the transaction recovery area
907          * is always aligned, otherwise the transaction code can overwrite
908          * itself. */
909         new_size = (new_size + NTDB_PGSIZE-1) & ~(NTDB_PGSIZE-1);
910         return new_size - map_size;
911 }
912
913 /* Expand the database. */
914 static enum NTDB_ERROR ntdb_expand(struct ntdb_context *ntdb, ntdb_len_t size)
915 {
916         uint64_t old_size;
917         ntdb_len_t wanted;
918         enum NTDB_ERROR ecode;
919
920         /* Need to hold a hash lock to expand DB: transactions rely on it. */
921         if (!(ntdb->flags & NTDB_NOLOCK)
922             && !ntdb->file->allrecord_lock.count && !ntdb_has_hash_locks(ntdb)) {
923                 return ntdb_logerr(ntdb, NTDB_ERR_LOCK, NTDB_LOG_ERROR,
924                                   "ntdb_expand: must hold lock during expand");
925         }
926
927         /* Only one person can expand file at a time. */
928         ecode = ntdb_lock_expand(ntdb, F_WRLCK);
929         if (ecode != NTDB_SUCCESS) {
930                 return ecode;
931         }
932
933         /* Someone else may have expanded the file, so retry. */
934         old_size = ntdb->file->map_size;
935         ntdb_oob(ntdb, ntdb->file->map_size, 1, true);
936         if (ntdb->file->map_size != old_size) {
937                 ntdb_unlock_expand(ntdb, F_WRLCK);
938                 return NTDB_SUCCESS;
939         }
940
941         /* We need room for the record header too. */
942         size = adjust_size(0, sizeof(struct ntdb_used_record) + size);
943         /* Overallocate. */
944         wanted = ntdb_expand_adjust(old_size, size);
945
946         ecode = ntdb->io->expand_file(ntdb, wanted);
947         if (ecode != NTDB_SUCCESS) {
948                 ntdb_unlock_expand(ntdb, F_WRLCK);
949                 return ecode;
950         }
951
952         /* We need to drop this lock before adding free record. */
953         ntdb_unlock_expand(ntdb, F_WRLCK);
954
955         ntdb->stats.expands++;
956         return add_free_record(ntdb, old_size, wanted, NTDB_LOCK_WAIT, true);
957 }
958
959 /* This won't fail: it will expand the database if it has to. */
960 ntdb_off_t alloc(struct ntdb_context *ntdb, size_t keylen, size_t datalen,
961                 uint64_t hash, unsigned magic, bool growing)
962 {
963         ntdb_off_t off;
964
965         /* We can't hold pointers during this: we could unmap! */
966         assert(!ntdb->direct_access);
967
968         for (;;) {
969                 enum NTDB_ERROR ecode;
970                 off = get_free(ntdb, keylen, datalen, growing, magic, hash);
971                 if (likely(off != 0))
972                         break;
973
974                 ecode = ntdb_expand(ntdb, adjust_size(keylen, datalen));
975                 if (ecode != NTDB_SUCCESS) {
976                         return NTDB_ERR_TO_OFF(ecode);
977                 }
978         }
979
980         return off;
981 }