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