]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb.c
tdb2: use vasprintf.
[ccan] / ccan / tdb2 / tdb.c
1 #include "private.h"
2 #include <ccan/asprintf/asprintf.h>
3 #include <ccan/tdb2/tdb2.h>
4 #include <assert.h>
5 #include <stdarg.h>
6
7 /* The null return. */
8 struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
9
10 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
11 static struct tdb_context *tdbs = NULL;
12
13 static bool tdb_already_open(dev_t device, ino_t ino)
14 {
15         struct tdb_context *i;
16         
17         for (i = tdbs; i; i = i->next) {
18                 if (i->device == device && i->inode == ino) {
19                         return true;
20                 }
21         }
22
23         return false;
24 }
25
26 static bool read_all(int fd, void *buf, size_t len)
27 {
28         while (len) {
29                 ssize_t ret;
30                 ret = read(fd, buf, len);
31                 if (ret < 0)
32                         return false;
33                 if (ret == 0) {
34                         /* ETOOSHORT? */
35                         errno = EWOULDBLOCK;
36                         return false;
37                 }
38                 buf = (char *)buf + ret;
39                 len -= ret;
40         }
41         return true;
42 }
43
44 static uint64_t random_number(struct tdb_context *tdb)
45 {
46         int fd;
47         uint64_t ret = 0;
48         struct timeval now;
49
50         fd = open("/dev/urandom", O_RDONLY);
51         if (fd >= 0) {
52                 if (read_all(fd, &ret, sizeof(ret))) {
53                         close(fd);
54                         return ret;
55                 }
56                 close(fd);
57         }
58         /* FIXME: Untested!  Based on Wikipedia protocol description! */
59         fd = open("/dev/egd-pool", O_RDWR);
60         if (fd >= 0) {
61                 /* Command is 1, next byte is size we want to read. */
62                 char cmd[2] = { 1, sizeof(uint64_t) };
63                 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
64                         char reply[1 + sizeof(uint64_t)];
65                         int r = read(fd, reply, sizeof(reply));
66                         if (r > 1) {
67                                 /* Copy at least some bytes. */
68                                 memcpy(&ret, reply+1, r - 1);
69                                 if (reply[0] == sizeof(uint64_t)
70                                     && r == sizeof(reply)) {
71                                         close(fd);
72                                         return ret;
73                                 }
74                         }
75                 }
76                 close(fd);
77         }
78
79         /* Fallback: pid and time. */
80         gettimeofday(&now, NULL);
81         ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
82         tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
83                    "tdb_open: random from getpid and time");
84         return ret;
85 }
86
87 struct new_database {
88         struct tdb_header hdr;
89         struct tdb_freetable ftable;
90 };
91
92 /* initialise a new database */
93 static enum TDB_ERROR tdb_new_database(struct tdb_context *tdb,
94                                        struct tdb_attribute_seed *seed,
95                                        struct tdb_header *hdr)
96 {
97         /* We make it up in memory, then write it out if not internal */
98         struct new_database newdb;
99         unsigned int magic_len;
100         ssize_t rlen;
101         enum TDB_ERROR ecode;
102
103         /* Fill in the header */
104         newdb.hdr.version = TDB_VERSION;
105         if (seed)
106                 newdb.hdr.hash_seed = seed->seed;
107         else
108                 newdb.hdr.hash_seed = random_number(tdb);
109         newdb.hdr.hash_test = TDB_HASH_MAGIC;
110         newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
111                                          sizeof(newdb.hdr.hash_test),
112                                          newdb.hdr.hash_seed,
113                                          tdb->hash_priv);
114         newdb.hdr.recovery = 0;
115         memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
116         /* Initial hashes are empty. */
117         memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
118
119         /* Free is empty. */
120         newdb.hdr.free_table = offsetof(struct new_database, ftable);
121         memset(&newdb.ftable, 0, sizeof(newdb.ftable));
122         ecode = set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
123                            sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
124                            sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
125                            0);
126         if (ecode != TDB_SUCCESS) {
127                 return ecode;
128         }
129
130         /* Magic food */
131         memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
132         strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
133
134         /* This creates an endian-converted database, as if read from disk */
135         magic_len = sizeof(newdb.hdr.magic_food);
136         tdb_convert(tdb,
137                     (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
138
139         *hdr = newdb.hdr;
140
141         if (tdb->flags & TDB_INTERNAL) {
142                 tdb->map_size = sizeof(newdb);
143                 tdb->map_ptr = malloc(tdb->map_size);
144                 if (!tdb->map_ptr) {
145                         return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
146                                           "tdb_new_database:"
147                                           " failed to allocate");
148                 }
149                 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
150                 return TDB_SUCCESS;
151         }
152         if (lseek(tdb->fd, 0, SEEK_SET) == -1) {
153                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
154                                   "tdb_new_database:"
155                                   " failed to seek: %s", strerror(errno));
156         }
157
158         if (ftruncate(tdb->fd, 0) == -1) {
159                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
160                                   "tdb_new_database:"
161                                   " failed to truncate: %s", strerror(errno));
162         }
163
164         rlen = write(tdb->fd, &newdb, sizeof(newdb));
165         if (rlen != sizeof(newdb)) {
166                 if (rlen >= 0)
167                         errno = ENOSPC;
168                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
169                                   "tdb_new_database: %zi writing header: %s",
170                                   rlen, strerror(errno));
171         }
172         return TDB_SUCCESS;
173 }
174
175 struct tdb_context *tdb_open(const char *name, int tdb_flags,
176                              int open_flags, mode_t mode,
177                              union tdb_attribute *attr)
178 {
179         struct tdb_context *tdb;
180         struct stat st;
181         int saved_errno = 0;
182         uint64_t hash_test;
183         unsigned v;
184         ssize_t rlen;
185         struct tdb_header hdr;
186         struct tdb_attribute_seed *seed = NULL;
187         tdb_bool_err berr;
188         enum TDB_ERROR ecode;
189
190         tdb = malloc(sizeof(*tdb));
191         if (!tdb) {
192                 /* Can't log this */
193                 errno = ENOMEM;
194                 return NULL;
195         }
196         tdb->name = NULL;
197         tdb->map_ptr = NULL;
198         tdb->direct_access = 0;
199         tdb->fd = -1;
200         tdb->map_size = sizeof(struct tdb_header);
201         tdb->flags = tdb_flags;
202         tdb->logfn = NULL;
203         tdb->transaction = NULL;
204         tdb->stats = NULL;
205         tdb->access = NULL;
206         tdb_hash_init(tdb);
207         tdb_io_init(tdb);
208         tdb_lock_init(tdb);
209
210         while (attr) {
211                 switch (attr->base.attr) {
212                 case TDB_ATTRIBUTE_LOG:
213                         tdb->logfn = attr->log.log_fn;
214                         tdb->log_private = attr->log.log_private;
215                         break;
216                 case TDB_ATTRIBUTE_HASH:
217                         tdb->khash = attr->hash.hash_fn;
218                         tdb->hash_priv = attr->hash.hash_private;
219                         break;
220                 case TDB_ATTRIBUTE_SEED:
221                         seed = &attr->seed;
222                         break;
223                 case TDB_ATTRIBUTE_STATS:
224                         tdb->stats = &attr->stats;
225                         /* They have stats we don't know about?  Tell them. */
226                         if (tdb->stats->size > sizeof(attr->stats))
227                                 tdb->stats->size = sizeof(attr->stats);
228                         break;
229                 default:
230                         ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
231                                            TDB_LOG_USE_ERROR,
232                                            "tdb_open:"
233                                            " unknown attribute type %u",
234                                            attr->base.attr);
235                         goto fail;
236                 }
237                 attr = attr->base.next;
238         }
239
240         if ((open_flags & O_ACCMODE) == O_WRONLY) {
241                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
242                                    "tdb_open: can't open tdb %s write-only",
243                                    name);
244                 goto fail;
245         }
246
247         if ((open_flags & O_ACCMODE) == O_RDONLY) {
248                 tdb->read_only = true;
249                 tdb->mmap_flags = PROT_READ;
250         } else {
251                 tdb->read_only = false;
252                 tdb->mmap_flags = PROT_READ | PROT_WRITE;
253         }
254
255         /* internal databases don't need any of the rest. */
256         if (tdb->flags & TDB_INTERNAL) {
257                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
258                 ecode = tdb_new_database(tdb, seed, &hdr);
259                 if (ecode != TDB_SUCCESS) {
260                         goto fail;
261                 }
262                 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
263                 tdb->hash_seed = hdr.hash_seed;
264                 tdb_ftable_init(tdb);
265                 return tdb;
266         }
267
268         if ((tdb->fd = open(name, open_flags, mode)) == -1) {
269                 /* errno set by open(2) */
270                 saved_errno = errno;
271                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
272                                    "tdb_open: could not open file %s: %s",
273                                    name, strerror(errno));
274                 goto fail;
275         }
276
277         /* on exec, don't inherit the fd */
278         v = fcntl(tdb->fd, F_GETFD, 0);
279         fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
280
281         /* ensure there is only one process initialising at once */
282         ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
283         if (ecode != TDB_SUCCESS) {
284                 goto fail;
285         }
286
287         /* If they used O_TRUNC, read will return 0. */
288         rlen = read(tdb->fd, &hdr, sizeof(hdr));
289         if (rlen == 0 && (open_flags & O_CREAT)) {
290                 ecode = tdb_new_database(tdb, seed, &hdr);
291                 if (ecode != TDB_SUCCESS) {
292                         goto fail;
293                 }
294         } else if (rlen < 0) {
295                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
296                                    "tdb_open: error %s reading %s",
297                                    strerror(errno), name);
298                 goto fail;
299         } else if (rlen < sizeof(hdr)
300                    || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
301                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
302                                    "tdb_open: %s is not a tdb file", name);
303                 goto fail;
304         }
305
306         if (hdr.version != TDB_VERSION) {
307                 if (hdr.version == bswap_64(TDB_VERSION))
308                         tdb->flags |= TDB_CONVERT;
309                 else {
310                         /* wrong version */
311                         ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
312                                            "tdb_open:"
313                                            " %s is unknown version 0x%llx",
314                                            name, (long long)hdr.version);
315                         goto fail;
316                 }
317         }
318
319         tdb_convert(tdb, &hdr, sizeof(hdr));
320         tdb->hash_seed = hdr.hash_seed;
321         hash_test = TDB_HASH_MAGIC;
322         hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
323         if (hdr.hash_test != hash_test) {
324                 /* wrong hash variant */
325                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
326                                    "tdb_open:"
327                                    " %s uses a different hash function",
328                                    name);
329                 goto fail;
330         }
331
332         if (fstat(tdb->fd, &st) == -1) {
333                 saved_errno = errno;
334                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
335                                    "tdb_open: could not stat open %s: %s",
336                                    name, strerror(errno));
337                 goto fail;
338         }
339
340         /* Is it already in the open list?  If so, fail. */
341         if (tdb_already_open(st.st_dev, st.st_ino)) {
342                 /* FIXME */
343                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_ERROR,
344                                    "tdb_open: %s (%d,%d) is already open"
345                                    " in this process",
346                                    name, (int)st.st_dev, (int)st.st_ino);
347                 goto fail;
348         }
349
350         tdb->name = strdup(name);
351         if (!tdb->name) {
352                 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
353                                    "tdb_open: failed to allocate name");
354                 goto fail;
355         }
356
357         tdb->device = st.st_dev;
358         tdb->inode = st.st_ino;
359         tdb_unlock_open(tdb);
360
361         /* This make sure we have current map_size and mmap. */
362         tdb->methods->oob(tdb, tdb->map_size + 1, true);
363
364         /* Now it's fully formed, recover if necessary. */
365         berr = tdb_needs_recovery(tdb);
366         if (unlikely(berr != false)) {
367                 if (berr < 0) {
368                         ecode = berr;
369                         goto fail;
370                 }
371                 ecode = tdb_lock_and_recover(tdb);
372                 if (ecode != TDB_SUCCESS) {
373                         goto fail;
374                 }
375         }
376
377         ecode = tdb_ftable_init(tdb);
378         if (ecode != TDB_SUCCESS) {
379                 goto fail;
380         }
381
382         tdb->next = tdbs;
383         tdbs = tdb;
384         return tdb;
385
386  fail:
387         /* Map ecode to some logical errno. */
388         if (!saved_errno) {
389                 switch (ecode) {
390                 case TDB_ERR_CORRUPT:
391                 case TDB_ERR_IO:
392                         saved_errno = EIO;
393                         break;
394                 case TDB_ERR_LOCK:
395                         saved_errno = EWOULDBLOCK;
396                         break;
397                 case TDB_ERR_OOM:
398                         saved_errno = ENOMEM;
399                         break;
400                 case TDB_ERR_EINVAL:
401                         saved_errno = EINVAL;
402                         break;
403                 default:
404                         saved_errno = EINVAL;
405                         break;
406                 }
407         }
408
409 #ifdef TDB_TRACE
410         close(tdb->tracefd);
411 #endif
412         if (tdb->map_ptr) {
413                 if (tdb->flags & TDB_INTERNAL) {
414                         free(tdb->map_ptr);
415                 } else
416                         tdb_munmap(tdb);
417         }
418         free(tdb->lockrecs);
419         free((char *)tdb->name);
420         if (tdb->fd != -1)
421                 if (close(tdb->fd) != 0)
422                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
423                                    "tdb_open: failed to close tdb->fd"
424                                    " on error: %s", strerror(errno));
425         free(tdb);
426         errno = saved_errno;
427         return NULL;
428 }
429
430 static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb,
431                                      tdb_off_t off,
432                                      tdb_len_t keylen,
433                                      tdb_len_t datalen,
434                                      struct tdb_used_record *rec,
435                                      uint64_t h)
436 {
437         uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
438         enum TDB_ERROR ecode;
439
440         ecode = set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
441                            keylen + dataroom, h);
442         if (ecode == TDB_SUCCESS) {
443                 ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
444         }
445         return ecode;
446 }
447
448 static enum TDB_ERROR replace_data(struct tdb_context *tdb,
449                                    struct hash_info *h,
450                                    struct tdb_data key, struct tdb_data dbuf,
451                                    tdb_off_t old_off, tdb_len_t old_room,
452                                    bool growing)
453 {
454         tdb_off_t new_off;
455         enum TDB_ERROR ecode;
456
457         /* Allocate a new record. */
458         new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
459                         growing);
460         if (TDB_OFF_IS_ERR(new_off)) {
461                 return new_off;
462         }
463
464         /* We didn't like the existing one: remove it. */
465         if (old_off) {
466                 add_stat(tdb, frees, 1);
467                 ecode = add_free_record(tdb, old_off,
468                                         sizeof(struct tdb_used_record)
469                                         + key.dsize + old_room);
470                 if (ecode == TDB_SUCCESS)
471                         ecode = replace_in_hash(tdb, h, new_off);
472         } else {
473                 ecode = add_to_hash(tdb, h, new_off);
474         }
475         if (ecode != TDB_SUCCESS) {
476                 return ecode;
477         }
478
479         new_off += sizeof(struct tdb_used_record);
480         ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
481         if (ecode != TDB_SUCCESS) {
482                 return ecode;
483         }
484
485         new_off += key.dsize;
486         ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
487         if (ecode != TDB_SUCCESS) {
488                 return ecode;
489         }
490
491         /* FIXME: tdb_increment_seqnum(tdb); */
492         return TDB_SUCCESS;
493 }
494
495 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
496                          struct tdb_data key, struct tdb_data dbuf, int flag)
497 {
498         struct hash_info h;
499         tdb_off_t off;
500         tdb_len_t old_room = 0;
501         struct tdb_used_record rec;
502         enum TDB_ERROR ecode;
503
504         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
505         if (TDB_OFF_IS_ERR(off)) {
506                 return off;
507         }
508
509         /* Now we have lock on this hash bucket. */
510         if (flag == TDB_INSERT) {
511                 if (off) {
512                         ecode = TDB_ERR_EXISTS;
513                         goto out;
514                 }
515         } else {
516                 if (off) {
517                         old_room = rec_data_length(&rec)
518                                 + rec_extra_padding(&rec);
519                         if (old_room >= dbuf.dsize) {
520                                 /* Can modify in-place.  Easy! */
521                                 ecode = update_rec_hdr(tdb, off,
522                                                        key.dsize, dbuf.dsize,
523                                                        &rec, h.h);
524                                 if (ecode != TDB_SUCCESS) {
525                                         goto out;
526                                 }
527                                 ecode = tdb->methods->twrite(tdb,
528                                                              off + sizeof(rec)
529                                                              + key.dsize,
530                                                              dbuf.dptr,
531                                                              dbuf.dsize);
532                                 if (ecode != TDB_SUCCESS) {
533                                         goto out;
534                                 }
535                                 tdb_unlock_hashes(tdb, h.hlock_start,
536                                                   h.hlock_range, F_WRLCK);
537                                 return TDB_SUCCESS;
538                         }
539                 } else {
540                         if (flag == TDB_MODIFY) {
541                                 /* if the record doesn't exist and we
542                                    are in TDB_MODIFY mode then we should fail
543                                    the store */
544                                 ecode = TDB_ERR_NOEXIST;
545                                 goto out;
546                         }
547                 }
548         }
549
550         /* If we didn't use the old record, this implies we're growing. */
551         ecode = replace_data(tdb, &h, key, dbuf, off, old_room, off);
552 out:
553         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
554         return ecode;
555 }
556
557 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
558                           struct tdb_data key, struct tdb_data dbuf)
559 {
560         struct hash_info h;
561         tdb_off_t off;
562         struct tdb_used_record rec;
563         tdb_len_t old_room = 0, old_dlen;
564         unsigned char *newdata;
565         struct tdb_data new_dbuf;
566         enum TDB_ERROR ecode;
567
568         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
569         if (TDB_OFF_IS_ERR(off)) {
570                 return off;
571         }
572
573         if (off) {
574                 old_dlen = rec_data_length(&rec);
575                 old_room = old_dlen + rec_extra_padding(&rec);
576
577                 /* Fast path: can append in place. */
578                 if (rec_extra_padding(&rec) >= dbuf.dsize) {
579                         ecode = update_rec_hdr(tdb, off, key.dsize,
580                                                old_dlen + dbuf.dsize, &rec,
581                                                h.h);
582                         if (ecode != TDB_SUCCESS) {
583                                 goto out;
584                         }
585
586                         off += sizeof(rec) + key.dsize + old_dlen;
587                         ecode = tdb->methods->twrite(tdb, off, dbuf.dptr,
588                                                      dbuf.dsize);
589                         goto out;
590                 }
591
592                 /* Slow path. */
593                 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
594                 if (!newdata) {
595                         ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
596                                            "tdb_append:"
597                                            " failed to allocate %zu bytes",
598                                            (size_t)(key.dsize + old_dlen
599                                                     + dbuf.dsize));
600                         goto out;
601                 }
602                 ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
603                                             newdata, old_dlen);
604                 if (ecode != TDB_SUCCESS) {
605                         goto out_free_newdata;
606                 }
607                 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
608                 new_dbuf.dptr = newdata;
609                 new_dbuf.dsize = old_dlen + dbuf.dsize;
610         } else {
611                 newdata = NULL;
612                 new_dbuf = dbuf;
613         }
614
615         /* If they're using tdb_append(), it implies they're growing record. */
616         ecode = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
617
618 out_free_newdata:
619         free(newdata);
620 out:
621         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
622         return ecode;
623 }
624
625 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
626                          struct tdb_data *data)
627 {
628         tdb_off_t off;
629         struct tdb_used_record rec;
630         struct hash_info h;
631         enum TDB_ERROR ecode;
632
633         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
634         if (TDB_OFF_IS_ERR(off)) {
635                 return off;
636         }
637
638         if (!off) {
639                 ecode = TDB_ERR_NOEXIST;
640         } else {
641                 data->dsize = rec_data_length(&rec);
642                 data->dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
643                                             data->dsize);
644                 if (TDB_PTR_IS_ERR(data->dptr)) {
645                         ecode = TDB_PTR_ERR(data->dptr);
646                 } else
647                         ecode = TDB_SUCCESS;
648         }
649
650         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
651         return ecode;
652 }
653
654 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
655 {
656         tdb_off_t off;
657         struct tdb_used_record rec;
658         struct hash_info h;
659         enum TDB_ERROR ecode;
660
661         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
662         if (TDB_OFF_IS_ERR(off)) {
663                 return off;
664         }
665
666         if (!off) {
667                 ecode = TDB_ERR_NOEXIST;
668                 goto unlock;
669         }
670
671         ecode = delete_from_hash(tdb, &h);
672         if (ecode != TDB_SUCCESS) {
673                 goto unlock;
674         }
675
676         /* Free the deleted entry. */
677         add_stat(tdb, frees, 1);
678         ecode = add_free_record(tdb, off,
679                                 sizeof(struct tdb_used_record)
680                                 + rec_key_length(&rec)
681                                 + rec_data_length(&rec)
682                                 + rec_extra_padding(&rec));
683
684 unlock:
685         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
686         return ecode;
687 }
688
689 int tdb_close(struct tdb_context *tdb)
690 {
691         struct tdb_context **i;
692         int ret = 0;
693
694         tdb_trace(tdb, "tdb_close");
695
696         if (tdb->transaction) {
697                 tdb_transaction_cancel(tdb);
698         }
699
700         if (tdb->map_ptr) {
701                 if (tdb->flags & TDB_INTERNAL)
702                         free(tdb->map_ptr);
703                 else
704                         tdb_munmap(tdb);
705         }
706         free((char *)tdb->name);
707         if (tdb->fd != -1) {
708                 ret = close(tdb->fd);
709                 tdb->fd = -1;
710         }
711         free(tdb->lockrecs);
712
713         /* Remove from contexts list */
714         for (i = &tdbs; *i; i = &(*i)->next) {
715                 if (*i == tdb) {
716                         *i = tdb->next;
717                         break;
718                 }
719         }
720
721 #ifdef TDB_TRACE
722         close(tdb->tracefd);
723 #endif
724         free(tdb);
725
726         return ret;
727 }
728
729 const char *tdb_errorstr(enum TDB_ERROR ecode)
730 {
731         /* Gcc warns if you miss a case in the switch, so use that. */
732         switch (ecode) {
733         case TDB_SUCCESS: return "Success";
734         case TDB_ERR_CORRUPT: return "Corrupt database";
735         case TDB_ERR_IO: return "IO Error";
736         case TDB_ERR_LOCK: return "Locking error";
737         case TDB_ERR_OOM: return "Out of memory";
738         case TDB_ERR_EXISTS: return "Record exists";
739         case TDB_ERR_EINVAL: return "Invalid parameter";
740         case TDB_ERR_NOEXIST: return "Record does not exist";
741         case TDB_ERR_RDONLY: return "write not permitted";
742         }
743         return "Invalid error code";
744 }
745
746 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
747                                enum TDB_ERROR ecode,
748                                enum tdb_log_level level,
749                                const char *fmt, ...)
750 {
751         char *message;
752         va_list ap;
753         size_t len;
754         /* tdb_open paths care about errno, so save it. */
755         int saved_errno = errno;
756
757         if (!tdb->logfn)
758                 return ecode;
759
760         va_start(ap, fmt);
761         len = vasprintf(&message, fmt, ap);
762         va_end(ap);
763
764         if (len < 0) {
765                 tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
766                            "out of memory formatting message:");
767                 tdb->logfn(tdb, level, tdb->log_private, fmt);
768         } else {
769                 tdb->logfn(tdb, level, tdb->log_private, message);
770                 free(message);
771         }
772         errno = saved_errno;
773         return ecode;
774 }