]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb.c
tdb2: split expand into functions and test separately.
[ccan] / ccan / tdb2 / tdb.c
1 #include "private.h"
2 #include <ccan/tdb2/tdb2.h>
3 #include <ccan/hash/hash.h>
4 #include <ccan/build_assert/build_assert.h>
5 #include <ccan/likely/likely.h>
6 #include <assert.h>
7
8 /* The null return. */
9 struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
10
11 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
12 static struct tdb_context *tdbs = NULL;
13
14 PRINTF_ATTRIBUTE(4, 5) static void
15 null_log_fn(struct tdb_context *tdb,
16             enum tdb_debug_level level, void *priv,
17             const char *fmt, ...)
18 {
19 }
20
21 /* We do a lot of work assuming our copy of the header volatile area
22  * is uptodate, and usually it is.  However, once we grab a lock, we have to
23  * re-check it. */
24 bool header_changed(struct tdb_context *tdb)
25 {
26         uint64_t gen;
27
28         if (!(tdb->flags & TDB_NOLOCK) && tdb->header_uptodate) {
29                 tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
30                          "warning: header uptodate already\n");
31         }
32
33         /* We could get a partial update if we're not holding any locks. */
34         assert((tdb->flags & TDB_NOLOCK) || tdb_has_locks(tdb));
35
36         tdb->header_uptodate = true;
37         gen = tdb_read_off(tdb, offsetof(struct tdb_header, v.generation));
38         if (unlikely(gen != tdb->header.v.generation)) {
39                 tdb_read_convert(tdb, offsetof(struct tdb_header, v),
40                                  &tdb->header.v, sizeof(tdb->header.v));
41                 return true;
42         }
43         return false;
44 }
45
46 int write_header(struct tdb_context *tdb)
47 {
48         assert(tdb_read_off(tdb, offsetof(struct tdb_header, v.generation))
49                == tdb->header.v.generation);
50         tdb->header.v.generation++;
51         return tdb_write_convert(tdb, offsetof(struct tdb_header, v),
52                                  &tdb->header.v, sizeof(tdb->header.v));
53 }
54
55 static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed,
56                              void *arg)
57 {
58         return hash64_stable((const unsigned char *)key, length, seed);
59 }
60
61 uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len)
62 {
63         return tdb->khash(ptr, len, tdb->header.hash_seed, tdb->hash_priv);
64 }
65
66 static bool tdb_already_open(dev_t device, ino_t ino)
67 {
68         struct tdb_context *i;
69         
70         for (i = tdbs; i; i = i->next) {
71                 if (i->device == device && i->inode == ino) {
72                         return true;
73                 }
74         }
75
76         return false;
77 }
78
79 static uint64_t random_number(struct tdb_context *tdb)
80 {
81         int fd;
82         uint64_t ret = 0;
83         struct timeval now;
84
85         fd = open("/dev/urandom", O_RDONLY);
86         if (fd >= 0) {
87                 if (tdb_read_all(fd, &ret, sizeof(ret))) {
88                         tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
89                                  "tdb_open: random from /dev/urandom\n");
90                         close(fd);
91                         return ret;
92                 }
93                 close(fd);
94         }
95         /* FIXME: Untested!  Based on Wikipedia protocol description! */
96         fd = open("/dev/egd-pool", O_RDWR);
97         if (fd >= 0) {
98                 /* Command is 1, next byte is size we want to read. */
99                 char cmd[2] = { 1, sizeof(uint64_t) };
100                 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
101                         char reply[1 + sizeof(uint64_t)];
102                         int r = read(fd, reply, sizeof(reply));
103                         if (r > 1) {
104                                 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
105                                          "tdb_open: %u random bytes from"
106                                          " /dev/egd-pool\n", r-1);
107                                 /* Copy at least some bytes. */
108                                 memcpy(&ret, reply+1, r - 1);
109                                 if (reply[0] == sizeof(uint64_t)
110                                     && r == sizeof(reply)) {
111                                         close(fd);
112                                         return ret;
113                                 }
114                         }
115                 }
116                 close(fd);
117         }
118
119         /* Fallback: pid and time. */
120         gettimeofday(&now, NULL);
121         ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
122         tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
123                  "tdb_open: random from getpid and time\n");
124         return ret;
125 }
126
127 struct new_database {
128         struct tdb_header hdr;
129         struct tdb_used_record hrec;
130         tdb_off_t hash[1ULL << INITIAL_HASH_BITS];
131         struct tdb_used_record frec;
132         tdb_off_t free[INITIAL_FREE_BUCKETS + 1]; /* One overflow bucket */
133 };
134
135 /* initialise a new database */
136 static int tdb_new_database(struct tdb_context *tdb)
137 {
138         /* We make it up in memory, then write it out if not internal */
139         struct new_database newdb;
140         unsigned int magic_off = offsetof(struct tdb_header, magic_food);
141
142         /* Fill in the header */
143         newdb.hdr.version = TDB_VERSION;
144         newdb.hdr.hash_seed = random_number(tdb);
145         newdb.hdr.hash_test = TDB_HASH_MAGIC;
146         newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
147                                          sizeof(newdb.hdr.hash_test),
148                                          newdb.hdr.hash_seed,
149                                          tdb->hash_priv);
150
151         newdb.hdr.v.generation = 0;
152
153         /* The initial zone must cover the initial database size! */
154         BUILD_ASSERT((1ULL << INITIAL_ZONE_BITS) >= sizeof(newdb));
155
156         /* Free array has 1 zone, 10 buckets.  All buckets empty. */
157         newdb.hdr.v.num_zones = 1;
158         newdb.hdr.v.zone_bits = INITIAL_ZONE_BITS;
159         newdb.hdr.v.free_buckets = INITIAL_FREE_BUCKETS;
160         newdb.hdr.v.free_off = offsetof(struct new_database, free);
161         set_header(tdb, &newdb.frec, 0,
162                    sizeof(newdb.free), sizeof(newdb.free), 0);
163         memset(newdb.free, 0, sizeof(newdb.free));
164
165         /* Initial hashes are empty. */
166         newdb.hdr.v.hash_bits = INITIAL_HASH_BITS;
167         newdb.hdr.v.hash_off = offsetof(struct new_database, hash);
168         set_header(tdb, &newdb.hrec, 0,
169                    sizeof(newdb.hash), sizeof(newdb.hash), 0);
170         memset(newdb.hash, 0, sizeof(newdb.hash));
171
172         /* Magic food */
173         memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
174         strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
175
176         /* This creates an endian-converted database, as if read from disk */
177         tdb_convert(tdb,
178                     (char *)&newdb.hdr + magic_off,
179                     sizeof(newdb) - magic_off);
180
181         tdb->header = newdb.hdr;
182
183         if (tdb->flags & TDB_INTERNAL) {
184                 tdb->map_size = sizeof(newdb);
185                 tdb->map_ptr = malloc(tdb->map_size);
186                 if (!tdb->map_ptr) {
187                         tdb->ecode = TDB_ERR_OOM;
188                         return -1;
189                 }
190                 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
191                 return 0;
192         }
193         if (lseek(tdb->fd, 0, SEEK_SET) == -1)
194                 return -1;
195
196         if (ftruncate(tdb->fd, 0) == -1)
197                 return -1;
198
199         if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) {
200                 tdb->ecode = TDB_ERR_IO;
201                 return -1;
202         }
203         return 0;
204 }
205
206 struct tdb_context *tdb_open(const char *name, int tdb_flags,
207                              int open_flags, mode_t mode,
208                              union tdb_attribute *attr)
209 {
210         struct tdb_context *tdb;
211         struct stat st;
212         int save_errno;
213         uint64_t hash_test;
214         unsigned v;
215
216         tdb = malloc(sizeof(*tdb));
217         if (!tdb) {
218                 /* Can't log this */
219                 errno = ENOMEM;
220                 goto fail;
221         }
222         tdb->name = NULL;
223         tdb->map_ptr = NULL;
224         tdb->fd = -1;
225         /* map_size will be set below. */
226         tdb->ecode = TDB_SUCCESS;
227         /* header will be read in below. */
228         tdb->header_uptodate = false;
229         tdb->flags = tdb_flags;
230         tdb->log = null_log_fn;
231         tdb->log_priv = NULL;
232         tdb->khash = jenkins_hash;
233         tdb->hash_priv = NULL;
234         tdb->transaction = NULL;
235         /* last_zone will be set below. */
236         tdb_io_init(tdb);
237         tdb_lock_init(tdb);
238
239         /* FIXME */
240         if (attr) {
241                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
242                          "tdb_open: attributes not yet supported\n");
243                 errno = EINVAL;
244                 goto fail;
245         }
246
247         if ((open_flags & O_ACCMODE) == O_WRONLY) {
248                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
249                          "tdb_open: can't open tdb %s write-only\n", name);
250                 errno = EINVAL;
251                 goto fail;
252         }
253
254         if ((open_flags & O_ACCMODE) == O_RDONLY) {
255                 tdb->read_only = true;
256                 /* read only databases don't do locking */
257                 tdb->flags |= TDB_NOLOCK;
258         } else
259                 tdb->read_only = false;
260
261         /* internal databases don't need any of the rest. */
262         if (tdb->flags & TDB_INTERNAL) {
263                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
264                 if (tdb_new_database(tdb) != 0) {
265                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
266                                  "tdb_open: tdb_new_database failed!");
267                         goto fail;
268                 }
269                 TEST_IT(tdb->flags & TDB_CONVERT);
270                 tdb_convert(tdb, &tdb->header, sizeof(tdb->header));
271                 return tdb;
272         }
273
274         if ((tdb->fd = open(name, open_flags, mode)) == -1) {
275                 tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
276                          "tdb_open: could not open file %s: %s\n",
277                          name, strerror(errno));
278                 goto fail;      /* errno set by open(2) */
279         }
280
281         /* on exec, don't inherit the fd */
282         v = fcntl(tdb->fd, F_GETFD, 0);
283         fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
284
285         /* ensure there is only one process initialising at once */
286         if (tdb_lock_open(tdb) == -1) {
287                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
288                          "tdb_open: failed to get open lock on %s: %s\n",
289                          name, strerror(errno));
290                 goto fail;      /* errno set by tdb_brlock */
291         }
292
293         if (!tdb_pread_all(tdb->fd, &tdb->header, sizeof(tdb->header), 0)
294             || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) {
295                 if (!(open_flags & O_CREAT) || tdb_new_database(tdb) == -1) {
296                         if (errno == 0) {
297                                 errno = EIO; /* ie bad format or something */
298                         }
299                         goto fail;
300                 }
301         } else if (tdb->header.version != TDB_VERSION) {
302                 if (tdb->header.version == bswap_64(TDB_VERSION))
303                         tdb->flags |= TDB_CONVERT;
304                 else {
305                         /* wrong version */
306                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
307                                  "tdb_open: %s is unknown version 0x%llx\n",
308                                  name, (long long)tdb->header.version);
309                         errno = EIO;
310                         goto fail;
311                 }
312         }
313
314         tdb_convert(tdb, &tdb->header, sizeof(tdb->header));
315         hash_test = TDB_HASH_MAGIC;
316         hash_test = tdb->khash(&hash_test, sizeof(hash_test),
317                                tdb->header.hash_seed, tdb->hash_priv);
318         if (tdb->header.hash_test != hash_test) {
319                 /* wrong hash variant */
320                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
321                          "tdb_open: %s uses a different hash function\n",
322                          name);
323                 errno = EIO;
324                 goto fail;
325         }
326
327         if (fstat(tdb->fd, &st) == -1)
328                 goto fail;
329
330         /* Is it already in the open list?  If so, fail. */
331         if (tdb_already_open(st.st_dev, st.st_ino)) {
332                 /* FIXME */
333                 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
334                          "tdb_open: %s (%d,%d) is already open in this process\n",
335                          name, (int)st.st_dev, (int)st.st_ino);
336                 errno = EBUSY;
337                 goto fail;
338         }
339
340         tdb->name = strdup(name);
341         if (!tdb->name) {
342                 errno = ENOMEM;
343                 goto fail;
344         }
345
346         tdb->map_size = st.st_size;
347         tdb->device = st.st_dev;
348         tdb->inode = st.st_ino;
349         tdb_mmap(tdb);
350         tdb_unlock_open(tdb);
351         tdb_zone_init(tdb);
352
353         tdb->next = tdbs;
354         tdbs = tdb;
355         return tdb;
356
357  fail:
358         save_errno = errno;
359
360         if (!tdb)
361                 return NULL;
362
363 #ifdef TDB_TRACE
364         close(tdb->tracefd);
365 #endif
366         if (tdb->map_ptr) {
367                 if (tdb->flags & TDB_INTERNAL) {
368                         free(tdb->map_ptr);
369                 } else
370                         tdb_munmap(tdb);
371         }
372         free((char *)tdb->name);
373         if (tdb->fd != -1)
374                 if (close(tdb->fd) != 0)
375                         tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
376                                  "tdb_open: failed to close tdb->fd"
377                                  " on error!\n");
378         free(tdb);
379         errno = save_errno;
380         return NULL;
381 }
382
383 static tdb_off_t hash_off(struct tdb_context *tdb, uint64_t list)
384 {
385         return tdb->header.v.hash_off
386                 + ((list & ((1ULL << tdb->header.v.hash_bits) - 1))
387                    * sizeof(tdb_off_t));
388 }
389
390 /* Returns 0 if the entry is a zero (definitely not a match).
391  * Returns a valid entry offset if it's a match.  Fills in rec.
392  * Otherwise returns TDB_OFF_ERR: keep searching. */
393 static tdb_off_t entry_matches(struct tdb_context *tdb,
394                                uint64_t list,
395                                uint64_t hash,
396                                const struct tdb_data *key,
397                                struct tdb_used_record *rec)
398 {
399         tdb_off_t off;
400         uint64_t keylen;
401         const unsigned char *rkey;
402
403         off = tdb_read_off(tdb, tdb->header.v.hash_off
404                            + list * sizeof(tdb_off_t));
405         if (off == 0 || off == TDB_OFF_ERR)
406                 return off;
407
408 #if 0 /* FIXME: Check other bits. */
409         unsigned int bits, bitmask, hoffextra;
410         /* Bottom three bits show how many extra hash bits. */
411         bits = (off & ((1 << TDB_EXTRA_HASHBITS_NUM) - 1)) + 1;
412         bitmask = (1 << bits)-1;
413         hoffextra = ((off >> TDB_EXTRA_HASHBITS_NUM) & bitmask);
414         uint64_t hextra = hash >> tdb->header.v.hash_bits;
415         if ((hextra & bitmask) != hoffextra) 
416                 return TDB_OFF_ERR;
417         off &= ~...;
418 #endif
419
420         if (tdb_read_convert(tdb, off, rec, sizeof(*rec)) == -1)
421                 return TDB_OFF_ERR;
422
423         /* FIXME: check extra bits in header! */
424         keylen = rec_key_length(rec);
425         if (keylen != key->dsize)
426                 return TDB_OFF_ERR;
427
428         rkey = tdb_access_read(tdb, off + sizeof(*rec), keylen, false);
429         if (!rkey)
430                 return TDB_OFF_ERR;
431         if (memcmp(rkey, key->dptr, keylen) != 0)
432                 off = TDB_OFF_ERR;
433         tdb_access_release(tdb, rkey);
434         return off;
435 }
436
437 /* FIXME: Optimize? */
438 static void unlock_lists(struct tdb_context *tdb,
439                          tdb_off_t list, tdb_len_t num,
440                          int ltype)
441 {
442         tdb_off_t i;
443
444         for (i = list; i < list + num; i++)
445                 tdb_unlock_list(tdb, i, ltype);
446 }
447
448 /* FIXME: Optimize? */
449 static int lock_lists(struct tdb_context *tdb,
450                       tdb_off_t list, tdb_len_t num,
451                       int ltype)
452 {
453         tdb_off_t i;
454
455         for (i = list; i < list + num; i++) {
456                 if (tdb_lock_list(tdb, i, ltype, TDB_LOCK_WAIT) != 0) {
457                         unlock_lists(tdb, list, i - list, ltype);
458                         return -1;
459                 }
460         }
461         return 0;
462 }
463
464 /* We lock hashes up to the next empty offset.  We already hold the
465  * lock on the start bucket, but we may need to release and re-grab
466  * it.  If we fail, we hold no locks at all! */
467 static tdb_len_t relock_hash_to_zero(struct tdb_context *tdb,
468                                      tdb_off_t start, int ltype)
469 {
470         tdb_len_t num, len, pre_locks;
471
472 again:
473         num = 1ULL << tdb->header.v.hash_bits;
474         len = tdb_find_zero_off(tdb, hash_off(tdb, start), num - start);
475         if (unlikely(len == num - start)) {
476                 /* We hit the end of the hash range.  Drop lock: we have
477                    to lock start of hash first. */
478                 tdb_unlock_list(tdb, start, ltype);
479                 /* Grab something, so header is stable. */
480                 if (tdb_lock_list(tdb, 0, ltype, TDB_LOCK_WAIT))
481                         return TDB_OFF_ERR;
482                 len = tdb_find_zero_off(tdb, hash_off(tdb, 0), num);
483                 if (lock_lists(tdb, 1, len, ltype) == -1) {
484                         tdb_unlock_list(tdb, 0, ltype);
485                         return TDB_OFF_ERR;
486                 }
487                 pre_locks = len;
488                 len = num - start;
489         } else {
490                 /* We already have lock on start. */
491                 start++;
492                 pre_locks = 0;
493         }
494         if (unlikely(lock_lists(tdb, start, len, ltype) == -1)) {
495                 if (pre_locks)
496                         unlock_lists(tdb, 0, pre_locks, ltype);
497                 else
498                         tdb_unlock_list(tdb, start, ltype);
499                 return TDB_OFF_ERR;
500         }
501
502         /* Now, did we lose the race, and it's not zero any more? */
503         if (unlikely(tdb_read_off(tdb, hash_off(tdb, pre_locks + len)) != 0)) {
504                 unlock_lists(tdb, 0, pre_locks, ltype);
505                 /* Leave the start locked, as expected. */
506                 unlock_lists(tdb, start + 1, len - 1, ltype);
507                 goto again;
508         }
509
510         return pre_locks + len;
511 }
512
513 /* FIXME: modify, don't rewrite! */
514 static int update_rec_hdr(struct tdb_context *tdb,
515                           tdb_off_t off,
516                           tdb_len_t keylen,
517                           tdb_len_t datalen,
518                           struct tdb_used_record *rec,
519                           uint64_t h)
520 {
521         uint64_t room = rec_data_length(rec) + rec_extra_padding(rec);
522
523         if (set_header(tdb, rec, keylen, datalen, room - datalen, h))
524                 return -1;
525
526         return tdb_write_convert(tdb, off, rec, sizeof(*rec));
527 }
528
529 /* If we fail, others will try after us. */
530 static void enlarge_hash(struct tdb_context *tdb)
531 {
532         tdb_off_t newoff, oldoff, i;
533         tdb_len_t hlen;
534         uint64_t h, num = 1ULL << tdb->header.v.hash_bits;
535         struct tdb_used_record pad, *r;
536
537         /* FIXME: We should do this without holding locks throughout. */
538         if (tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false) == -1)
539                 return;
540
541         /* Someone else enlarged for us?  Nothing to do. */
542         if ((1ULL << tdb->header.v.hash_bits) != num)
543                 goto unlock;
544
545         /* Allocate our new array. */
546         hlen = num * sizeof(tdb_off_t) * 2;
547         newoff = alloc(tdb, 0, hlen, 0, false);
548         if (unlikely(newoff == TDB_OFF_ERR))
549                 goto unlock;
550         if (unlikely(newoff == 0)) {
551                 if (tdb_expand(tdb, 0, hlen, false) == -1)
552                         goto unlock;
553                 newoff = alloc(tdb, 0, hlen, 0, false);
554                 if (newoff == TDB_OFF_ERR || newoff == 0)
555                         goto unlock;
556         }
557         /* Step over record header! */
558         newoff += sizeof(struct tdb_used_record);
559
560         /* Starts all zero. */
561         if (zero_out(tdb, newoff, hlen) == -1)
562                 goto unlock;
563
564         /* FIXME: If the space before is empty, we know this is in its ideal
565          * location.  Or steal a bit from the pointer to avoid rehash. */
566         for (i = tdb_find_nonzero_off(tdb, hash_off(tdb, 0), num);
567              i < num;
568              i += tdb_find_nonzero_off(tdb, hash_off(tdb, i), num - i)) {
569                 tdb_off_t off;
570                 off = tdb_read_off(tdb, hash_off(tdb, i));
571                 if (unlikely(off == TDB_OFF_ERR))
572                         goto unlock;
573                 if (unlikely(!off)) {
574                         tdb->ecode = TDB_ERR_CORRUPT;
575                         tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
576                                  "find_bucket_and_lock: zero hash bucket!\n");
577                         goto unlock;
578                 }
579                 h = hash_record(tdb, off);
580                 /* FIXME: Encode extra hash bits! */
581                 if (tdb_write_off(tdb, newoff
582                                   + (h & ((num * 2) - 1)) * sizeof(uint64_t),
583                                   off) == -1)
584                         goto unlock;
585         }
586
587         /* Free up old hash. */
588         oldoff = tdb->header.v.hash_off - sizeof(*r);
589         r = tdb_get(tdb, oldoff, &pad, sizeof(*r));
590         if (!r)
591                 goto unlock;
592         add_free_record(tdb, oldoff,
593                         sizeof(*r)+rec_data_length(r)+rec_extra_padding(r));
594
595         /* Now we write the modified header. */
596         tdb->header.v.hash_bits++;
597         tdb->header.v.hash_off = newoff;
598         write_header(tdb);
599 unlock:
600         tdb_allrecord_unlock(tdb, F_WRLCK);
601 }
602
603 int tdb_store(struct tdb_context *tdb,
604               struct tdb_data key, struct tdb_data dbuf, int flag)
605 {
606         tdb_off_t new_off, off, old_bucket, start, num_locks = 1;
607         struct tdb_used_record rec;
608         uint64_t h;
609         bool growing = false;
610
611         h = tdb_hash(tdb, key.dptr, key.dsize);
612
613         /* FIXME: can we avoid locks for some fast paths? */
614         start = tdb_lock_list(tdb, h, F_WRLCK, TDB_LOCK_WAIT);
615         if (start == TDB_OFF_ERR)
616                 return -1;
617
618         /* Fast path. */
619         old_bucket = start;
620         off = entry_matches(tdb, start, h, &key, &rec);
621         if (unlikely(off == TDB_OFF_ERR)) {
622                 /* Slow path, need to grab more locks and search. */
623                 tdb_off_t i;
624
625                 /* Warning: this may drop the lock!  Does that on error. */
626                 num_locks = relock_hash_to_zero(tdb, start, F_WRLCK);
627                 if (num_locks == TDB_OFF_ERR)
628                         return -1;
629
630                 for (i = start; i < start + num_locks; i++) {
631                         off = entry_matches(tdb, i, h, &key, &rec);
632                         /* Empty entry or we found it? */
633                         if (off == 0 || off != TDB_OFF_ERR) {
634                                 old_bucket = i;
635                                 break;
636                         }
637                 }
638                 if (i == start + num_locks)
639                         off = 0;
640         }
641
642         /* Now we have lock on this hash bucket. */
643         if (flag == TDB_INSERT) {
644                 if (off) {
645                         tdb->ecode = TDB_ERR_EXISTS;
646                         goto fail;
647                 }
648         } else {
649                 if (off) {
650                         if (rec_data_length(&rec) + rec_extra_padding(&rec)
651                             >= dbuf.dsize) {
652                                 new_off = off;
653                                 if (update_rec_hdr(tdb, off,
654                                                    key.dsize, dbuf.dsize,
655                                                    &rec, h))
656                                         goto fail;
657                                 goto write;
658                         }
659                         /* FIXME: See if right record is free? */
660                         /* Hint to allocator that we've realloced. */
661                         growing = true;
662                 } else {
663                         if (flag == TDB_MODIFY) {
664                                 /* if the record doesn't exist and we
665                                    are in TDB_MODIFY mode then we should fail
666                                    the store */
667                                 tdb->ecode = TDB_ERR_NOEXIST;
668                                 goto fail;
669                         }
670                 }
671         }
672
673         /* Allocate a new record. */
674         new_off = alloc(tdb, key.dsize, dbuf.dsize, h, growing);
675         if (new_off == 0) {
676                 unlock_lists(tdb, start, num_locks, F_WRLCK);
677                 /* Expand, then try again... */
678                 if (tdb_expand(tdb, key.dsize, dbuf.dsize, growing) == -1)
679                         return -1;
680                 return tdb_store(tdb, key, dbuf, flag);
681         }
682
683         /* We didn't like the existing one: remove it. */
684         if (off) {
685                 add_free_record(tdb, off, sizeof(struct tdb_used_record)
686                                 + rec_key_length(&rec)
687                                 + rec_data_length(&rec)
688                                 + rec_extra_padding(&rec));
689         }
690
691 write:
692         /* FIXME: Encode extra hash bits! */
693         if (tdb_write_off(tdb, hash_off(tdb, old_bucket), new_off) == -1)
694                 goto fail;
695
696         off = new_off + sizeof(struct tdb_used_record);
697         if (tdb->methods->write(tdb, off, key.dptr, key.dsize) == -1)
698                 goto fail;
699         off += key.dsize;
700         if (tdb->methods->write(tdb, off, dbuf.dptr, dbuf.dsize) == -1)
701                 goto fail;
702
703         /* FIXME: tdb_increment_seqnum(tdb); */
704         unlock_lists(tdb, start, num_locks, F_WRLCK);
705
706         /* FIXME: by simple simulation, this approximated 60% full.
707          * Check in real case! */
708         if (unlikely(num_locks > 4 * tdb->header.v.hash_bits - 31))
709                 enlarge_hash(tdb);
710
711         return 0;
712
713 fail:
714         unlock_lists(tdb, start, num_locks, F_WRLCK);
715         return -1;
716 }
717
718 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
719 {
720         tdb_off_t off, start, num_locks = 1;
721         struct tdb_used_record rec;
722         uint64_t h;
723         struct tdb_data ret;
724
725         h = tdb_hash(tdb, key.dptr, key.dsize);
726
727         /* FIXME: can we avoid locks for some fast paths? */
728         start = tdb_lock_list(tdb, h, F_RDLCK, TDB_LOCK_WAIT);
729         if (start == TDB_OFF_ERR)
730                 return tdb_null;
731
732         /* Fast path. */
733         off = entry_matches(tdb, start, h, &key, &rec);
734         if (unlikely(off == TDB_OFF_ERR)) {
735                 /* Slow path, need to grab more locks and search. */
736                 tdb_off_t i;
737
738                 /* Warning: this may drop the lock!  Does that on error. */
739                 num_locks = relock_hash_to_zero(tdb, start, F_RDLCK);
740                 if (num_locks == TDB_OFF_ERR)
741                         return tdb_null;
742
743                 for (i = start; i < start + num_locks; i++) {
744                         off = entry_matches(tdb, i, h, &key, &rec);
745                         /* Empty entry or we found it? */
746                         if (off == 0 || off != TDB_OFF_ERR)
747                                 break;
748                 }
749                 if (i == start + num_locks)
750                         off = 0;
751         }
752
753         if (!off) {
754                 unlock_lists(tdb, start, num_locks, F_RDLCK);
755                 tdb->ecode = TDB_ERR_NOEXIST;
756                 return tdb_null;
757         }
758
759         ret.dsize = rec_data_length(&rec);
760         ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
761                                   ret.dsize);
762         unlock_lists(tdb, start, num_locks, F_RDLCK);
763         return ret;
764 }
765
766 static int hash_add(struct tdb_context *tdb, uint64_t h, tdb_off_t off)
767 {
768         tdb_off_t i, hoff, len, num;
769
770         /* Look for next space. */
771         i = (h & ((1ULL << tdb->header.v.hash_bits) - 1));
772         len = (1ULL << tdb->header.v.hash_bits) - i;
773         num = tdb_find_zero_off(tdb, hash_off(tdb, i), len);
774
775         if (unlikely(num == len)) {
776                 /* We wrapped.  Look through start of hash table. */
777                 hoff = hash_off(tdb, 0);
778                 len = (1ULL << tdb->header.v.hash_bits);
779                 num = tdb_find_zero_off(tdb, hoff, len);
780                 if (i == len) {
781                         tdb->ecode = TDB_ERR_CORRUPT;
782                         tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
783                                  "hash_add: full hash table!\n");
784                         return -1;
785                 }
786         }
787         /* FIXME: Encode extra hash bits! */
788         return tdb_write_off(tdb, hash_off(tdb, i + num), off);
789 }
790
791 int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
792 {
793         tdb_off_t i, old_bucket, off, start, num_locks = 1;
794         struct tdb_used_record rec;
795         uint64_t h;
796
797         h = tdb_hash(tdb, key.dptr, key.dsize);
798
799         /* FIXME: can we avoid locks for some fast paths? */
800         start = tdb_lock_list(tdb, h, F_WRLCK, TDB_LOCK_WAIT);
801         if (start == TDB_OFF_ERR)
802                 return -1;
803
804         /* Fast path. */
805         old_bucket = start;
806         off = entry_matches(tdb, start, h, &key, &rec);
807         if (off && off != TDB_OFF_ERR) {
808                 /* We can only really fastpath delete if next bucket
809                  * is 0.  Note that we haven't locked it, but our lock
810                  * on this bucket stops anyone overflowing into it
811                  * while we look. */
812                 if (tdb_read_off(tdb, hash_off(tdb, h+1)) == 0)
813                         goto delete;
814                 /* Slow path. */
815                 off = TDB_OFF_ERR;
816         }
817
818         if (unlikely(off == TDB_OFF_ERR)) {
819                 /* Slow path, need to grab more locks and search. */
820                 tdb_off_t i;
821
822                 /* Warning: this may drop the lock!  Does that on error. */
823                 num_locks = relock_hash_to_zero(tdb, start, F_WRLCK);
824                 if (num_locks == TDB_OFF_ERR)
825                         return -1;
826
827                 for (i = start; i < start + num_locks; i++) {
828                         off = entry_matches(tdb, i, h, &key, &rec);
829                         /* Empty entry or we found it? */
830                         if (off == 0 || off != TDB_OFF_ERR) {
831                                 old_bucket = i;
832                                 break;
833                         }
834                 }
835                 if (i == start + num_locks)
836                         off = 0;
837         }
838
839         if (!off) {
840                 unlock_lists(tdb, start, num_locks, F_WRLCK);
841                 tdb->ecode = TDB_ERR_NOEXIST;
842                 return -1;
843         }
844
845 delete:
846         /* This actually unlinks it. */
847         if (tdb_write_off(tdb, hash_off(tdb, old_bucket), 0) == -1)
848                 goto unlock_err;
849
850         /* Rehash anything following. */
851         for (i = hash_off(tdb, old_bucket+1);
852              i != hash_off(tdb, h + num_locks);
853              i += sizeof(tdb_off_t)) {
854                 tdb_off_t off2;
855                 uint64_t h2;
856
857                 off2 = tdb_read_off(tdb, i);
858                 if (unlikely(off2 == TDB_OFF_ERR))
859                         goto unlock_err;
860
861                 /* Maybe use a bit to indicate it is in ideal place? */
862                 h2 = hash_record(tdb, off2);
863                 /* Is it happy where it is? */
864                 if (hash_off(tdb, h2) == i)
865                         continue;
866
867                 /* Remove it. */
868                 if (tdb_write_off(tdb, i, 0) == -1)
869                         goto unlock_err;
870
871                 /* Rehash it. */
872                 if (hash_add(tdb, h2, off2) == -1)
873                         goto unlock_err;
874         }
875
876         /* Free the deleted entry. */
877         if (add_free_record(tdb, off,
878                             sizeof(struct tdb_used_record)
879                             + rec_key_length(&rec)
880                             + rec_data_length(&rec)
881                             + rec_extra_padding(&rec)) != 0)
882                 goto unlock_err;
883
884         unlock_lists(tdb, start, num_locks, F_WRLCK);
885         return 0;
886
887 unlock_err:
888         unlock_lists(tdb, start, num_locks, F_WRLCK);
889         return -1;
890 }
891
892 int tdb_close(struct tdb_context *tdb)
893 {
894         struct tdb_context **i;
895         int ret = 0;
896
897         /* FIXME:
898         if (tdb->transaction) {
899                 tdb_transaction_cancel(tdb);
900         }
901         */
902         tdb_trace(tdb, "tdb_close");
903
904         if (tdb->map_ptr) {
905                 if (tdb->flags & TDB_INTERNAL)
906                         free(tdb->map_ptr);
907                 else
908                         tdb_munmap(tdb);
909         }
910         free((char *)tdb->name);
911         if (tdb->fd != -1) {
912                 ret = close(tdb->fd);
913                 tdb->fd = -1;
914         }
915         free(tdb->lockrecs);
916
917         /* Remove from contexts list */
918         for (i = &tdbs; *i; i = &(*i)->next) {
919                 if (*i == tdb) {
920                         *i = tdb->next;
921                         break;
922                 }
923         }
924
925 #ifdef TDB_TRACE
926         close(tdb->tracefd);
927 #endif
928         free(tdb);
929
930         return ret;
931 }
932
933 enum TDB_ERROR tdb_error(struct tdb_context *tdb)
934 {
935         return tdb->ecode;
936 }