]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb.c
8bebe2af206b2a361734a687f062722fc2c8a626
[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->ecode = TDB_SUCCESS;
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                         tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
231                                    "tdb_open: unknown attribute type %u",
232                                    attr->base.attr);
233                         goto fail;
234                 }
235                 attr = attr->base.next;
236         }
237
238         if ((open_flags & O_ACCMODE) == O_WRONLY) {
239                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
240                            "tdb_open: can't open tdb %s write-only", name);
241                 goto fail;
242         }
243
244         if ((open_flags & O_ACCMODE) == O_RDONLY) {
245                 tdb->read_only = true;
246                 /* read only databases don't do locking */
247                 tdb->flags |= TDB_NOLOCK;
248                 tdb->mmap_flags = PROT_READ;
249         } else {
250                 tdb->read_only = false;
251                 tdb->mmap_flags = PROT_READ | PROT_WRITE;
252         }
253
254         /* internal databases don't need any of the rest. */
255         if (tdb->flags & TDB_INTERNAL) {
256                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
257                 if (tdb_new_database(tdb, seed, &hdr) != 0) {
258                         goto fail;
259                 }
260                 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
261                 tdb->hash_seed = hdr.hash_seed;
262                 tdb_ftable_init(tdb);
263                 return tdb;
264         }
265
266         if ((tdb->fd = open(name, open_flags, mode)) == -1) {
267                 /* errno set by open(2) */
268                 saved_errno = errno;
269                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
270                            "tdb_open: could not open file %s: %s",
271                            name, strerror(errno));
272                 goto fail;
273         }
274
275         /* on exec, don't inherit the fd */
276         v = fcntl(tdb->fd, F_GETFD, 0);
277         fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
278
279         /* ensure there is only one process initialising at once */
280         tdb->ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
281         if (tdb->ecode != TDB_SUCCESS) {
282                 goto fail;
283         }
284
285         /* If they used O_TRUNC, read will return 0. */
286         rlen = read(tdb->fd, &hdr, sizeof(hdr));
287         if (rlen == 0 && (open_flags & O_CREAT)) {
288                 if (tdb_new_database(tdb, seed, &hdr) == -1) {
289                         goto fail;
290                 }
291         } else if (rlen < 0) {
292                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
293                            "tdb_open: error %s reading %s",
294                            strerror(errno), name);
295                 goto fail;
296         } else if (rlen < sizeof(hdr)
297                    || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
298                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
299                            "tdb_open: %s is not a tdb file", name);
300                 goto fail;
301         }
302
303         if (hdr.version != TDB_VERSION) {
304                 if (hdr.version == bswap_64(TDB_VERSION))
305                         tdb->flags |= TDB_CONVERT;
306                 else {
307                         /* wrong version */
308                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
309                                    "tdb_open: %s is unknown version 0x%llx",
310                                    name, (long long)hdr.version);
311                         goto fail;
312                 }
313         }
314
315         tdb_convert(tdb, &hdr, sizeof(hdr));
316         tdb->hash_seed = hdr.hash_seed;
317         hash_test = TDB_HASH_MAGIC;
318         hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
319         if (hdr.hash_test != hash_test) {
320                 /* wrong hash variant */
321                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
322                            "tdb_open: %s uses a different hash function",
323                            name);
324                 goto fail;
325         }
326
327         if (fstat(tdb->fd, &st) == -1) {
328                 saved_errno = errno;
329                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
330                            "tdb_open: could not stat open %s: %s",
331                            name, strerror(errno));
332                 goto fail;
333         }
334
335         /* Is it already in the open list?  If so, fail. */
336         if (tdb_already_open(st.st_dev, st.st_ino)) {
337                 /* FIXME */
338                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_ERROR,
339                            "tdb_open: %s (%d,%d) is already open in this"
340                            " process",
341                            name, (int)st.st_dev, (int)st.st_ino);
342                 goto fail;
343         }
344
345         tdb->name = strdup(name);
346         if (!tdb->name) {
347                 tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
348                            "tdb_open: failed to allocate name");
349                 goto fail;
350         }
351
352         tdb->device = st.st_dev;
353         tdb->inode = st.st_ino;
354         tdb_unlock_open(tdb);
355
356         /* This make sure we have current map_size and mmap. */
357         tdb->methods->oob(tdb, tdb->map_size + 1, true);
358
359         /* Now it's fully formed, recover if necessary. */
360         berr = tdb_needs_recovery(tdb);
361         if (unlikely(berr != false)) {
362                 if (berr < 0) {
363                         ecode = berr;
364                         goto fail;
365                 }
366                 ecode = tdb_lock_and_recover(tdb);
367                 if (ecode != TDB_SUCCESS) {
368                         tdb->ecode = ecode;
369                         goto fail;
370                 }
371         }
372
373         tdb->ecode = tdb_ftable_init(tdb);
374         if (tdb->ecode != TDB_SUCCESS) {
375                 goto fail;
376         }
377
378         tdb->next = tdbs;
379         tdbs = tdb;
380         return tdb;
381
382  fail:
383         /* Map ecode to some logical errno. */
384         if (!saved_errno) {
385                 switch (tdb->ecode) {
386                 case TDB_ERR_CORRUPT:
387                 case TDB_ERR_IO:
388                         saved_errno = EIO;
389                         break;
390                 case TDB_ERR_LOCK:
391                         saved_errno = EWOULDBLOCK;
392                         break;
393                 case TDB_ERR_OOM:
394                         saved_errno = ENOMEM;
395                         break;
396                 case TDB_ERR_EINVAL:
397                         saved_errno = EINVAL;
398                         break;
399                 default:
400                         saved_errno = EINVAL;
401                         break;
402                 }
403         }
404
405 #ifdef TDB_TRACE
406         close(tdb->tracefd);
407 #endif
408         if (tdb->map_ptr) {
409                 if (tdb->flags & TDB_INTERNAL) {
410                         free(tdb->map_ptr);
411                 } else
412                         tdb_munmap(tdb);
413         }
414         free(tdb->lockrecs);
415         free((char *)tdb->name);
416         if (tdb->fd != -1)
417                 if (close(tdb->fd) != 0)
418                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
419                                    "tdb_open: failed to close tdb->fd"
420                                    " on error: %s", strerror(errno));
421         free(tdb);
422         errno = saved_errno;
423         return NULL;
424 }
425
426 static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb,
427                                      tdb_off_t off,
428                                      tdb_len_t keylen,
429                                      tdb_len_t datalen,
430                                      struct tdb_used_record *rec,
431                                      uint64_t h)
432 {
433         uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
434         enum TDB_ERROR ecode;
435
436         ecode = set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
437                            keylen + dataroom, h);
438         if (ecode == TDB_SUCCESS) {
439                 ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
440         }
441         return ecode;
442 }
443
444 static enum TDB_ERROR replace_data(struct tdb_context *tdb,
445                                    struct hash_info *h,
446                                    struct tdb_data key, struct tdb_data dbuf,
447                                    tdb_off_t old_off, tdb_len_t old_room,
448                                    bool growing)
449 {
450         tdb_off_t new_off;
451         enum TDB_ERROR ecode;
452
453         /* Allocate a new record. */
454         new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
455                         growing);
456         if (TDB_OFF_IS_ERR(new_off)) {
457                 return new_off;
458         }
459
460         /* We didn't like the existing one: remove it. */
461         if (old_off) {
462                 add_stat(tdb, frees, 1);
463                 ecode = add_free_record(tdb, old_off,
464                                         sizeof(struct tdb_used_record)
465                                         + key.dsize + old_room);
466                 if (ecode == TDB_SUCCESS)
467                         ecode = replace_in_hash(tdb, h, new_off);
468         } else {
469                 ecode = add_to_hash(tdb, h, new_off);
470         }
471         if (ecode != TDB_SUCCESS) {
472                 return ecode;
473         }
474
475         new_off += sizeof(struct tdb_used_record);
476         ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
477         if (ecode != TDB_SUCCESS) {
478                 return ecode;
479         }
480
481         new_off += key.dsize;
482         ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
483         if (ecode != TDB_SUCCESS) {
484                 return ecode;
485         }
486
487         /* FIXME: tdb_increment_seqnum(tdb); */
488         return TDB_SUCCESS;
489 }
490
491 int tdb_store(struct tdb_context *tdb,
492               struct tdb_data key, struct tdb_data dbuf, int flag)
493 {
494         struct hash_info h;
495         tdb_off_t off;
496         tdb_len_t old_room = 0;
497         struct tdb_used_record rec;
498         enum TDB_ERROR ecode;
499
500         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
501         if (TDB_OFF_IS_ERR(off)) {
502                 tdb->ecode = off;
503                 return -1;
504         }
505
506         /* Now we have lock on this hash bucket. */
507         if (flag == TDB_INSERT) {
508                 if (off) {
509                         tdb->ecode = TDB_ERR_EXISTS;
510                         goto fail;
511                 }
512         } else {
513                 if (off) {
514                         old_room = rec_data_length(&rec)
515                                 + rec_extra_padding(&rec);
516                         if (old_room >= dbuf.dsize) {
517                                 /* Can modify in-place.  Easy! */
518                                 ecode = update_rec_hdr(tdb, off,
519                                                        key.dsize, dbuf.dsize,
520                                                        &rec, h.h);
521                                 if (ecode != TDB_SUCCESS) {
522                                         tdb->ecode = ecode;
523                                         goto fail;
524                                 }
525                                 ecode = tdb->methods->twrite(tdb,
526                                                              off + sizeof(rec)
527                                                              + key.dsize,
528                                                              dbuf.dptr,
529                                                              dbuf.dsize);
530                                 if (ecode != TDB_SUCCESS) {
531                                         tdb->ecode = ecode;
532                                         goto fail;
533                                 }
534                                 tdb_unlock_hashes(tdb, h.hlock_start,
535                                                   h.hlock_range, F_WRLCK);
536                                 return 0;
537                         }
538                 } else {
539                         if (flag == TDB_MODIFY) {
540                                 /* if the record doesn't exist and we
541                                    are in TDB_MODIFY mode then we should fail
542                                    the store */
543                                 tdb->ecode = TDB_ERR_NOEXIST;
544                                 goto fail;
545                         }
546                 }
547         }
548
549         /* If we didn't use the old record, this implies we're growing. */
550         ecode = replace_data(tdb, &h, key, dbuf, off, old_room, off);
551         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
552         if (ecode != TDB_SUCCESS) {
553                 tdb->ecode = ecode;
554                 return -1;
555         }
556         return 0;
557
558 fail:
559         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
560         return -1;
561 }
562
563 int tdb_append(struct tdb_context *tdb,
564                struct tdb_data key, struct tdb_data dbuf)
565 {
566         struct hash_info h;
567         tdb_off_t off;
568         struct tdb_used_record rec;
569         tdb_len_t old_room = 0, old_dlen;
570         unsigned char *newdata;
571         struct tdb_data new_dbuf;
572         enum TDB_ERROR ecode;
573
574         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
575         if (TDB_OFF_IS_ERR(off)) {
576                 tdb->ecode = off;
577                 return -1;
578         }
579
580         if (off) {
581                 old_dlen = rec_data_length(&rec);
582                 old_room = old_dlen + rec_extra_padding(&rec);
583
584                 /* Fast path: can append in place. */
585                 if (rec_extra_padding(&rec) >= dbuf.dsize) {
586                         ecode = update_rec_hdr(tdb, off, key.dsize,
587                                                old_dlen + dbuf.dsize, &rec,
588                                                h.h);
589                         if (ecode != TDB_SUCCESS) {
590                                 tdb->ecode = ecode;
591                                 goto fail;
592                         }
593
594                         off += sizeof(rec) + key.dsize + old_dlen;
595                         ecode = tdb->methods->twrite(tdb, off, dbuf.dptr,
596                                                      dbuf.dsize);
597                         if (ecode != TDB_SUCCESS) {
598                                 tdb->ecode = ecode;
599                                 goto fail;
600                         }
601
602                         /* FIXME: tdb_increment_seqnum(tdb); */
603                         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
604                                           F_WRLCK);
605                         return 0;
606                 }
607
608                 /* Slow path. */
609                 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
610                 if (!newdata) {
611                         tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
612                                    "tdb_append: failed to allocate %zu bytes",
613                                    (size_t)(key.dsize+old_dlen+dbuf.dsize));
614                         goto fail;
615                 }
616                 ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
617                                             newdata, old_dlen);
618                 if (ecode != TDB_SUCCESS) {
619                         tdb->ecode = ecode;
620                         free(newdata);
621                         goto fail;
622                 }
623                 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
624                 new_dbuf.dptr = newdata;
625                 new_dbuf.dsize = old_dlen + dbuf.dsize;
626         } else {
627                 newdata = NULL;
628                 new_dbuf = dbuf;
629         }
630
631         /* If they're using tdb_append(), it implies they're growing record. */
632         ecode = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
633         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
634         free(newdata);
635
636         if (ecode != TDB_SUCCESS) {
637                 tdb->ecode = ecode;
638                 return -1;
639         }
640         return 0;
641
642 fail:
643         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
644         return -1;
645 }
646
647 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
648 {
649         tdb_off_t off;
650         struct tdb_used_record rec;
651         struct hash_info h;
652         struct tdb_data ret;
653
654         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
655         if (TDB_OFF_IS_ERR(off)) {
656                 tdb->ecode = off;
657                 return tdb_null;
658         }
659
660         if (!off) {
661                 tdb->ecode = TDB_ERR_NOEXIST;
662                 ret = tdb_null;
663         } else {
664                 ret.dsize = rec_data_length(&rec);
665                 ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
666                                           ret.dsize);
667                 if (TDB_PTR_IS_ERR(ret.dptr)) {
668                         tdb->ecode = TDB_PTR_ERR(ret.dptr);
669                         ret = tdb_null;
670                 }
671         }
672
673         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
674         return ret;
675 }
676
677 int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
678 {
679         tdb_off_t off;
680         struct tdb_used_record rec;
681         struct hash_info h;
682         enum TDB_ERROR ecode;
683
684         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
685         if (TDB_OFF_IS_ERR(off)) {
686                 tdb->ecode = off;
687                 return -1;
688         }
689
690         if (!off) {
691                 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
692                 tdb->ecode = TDB_ERR_NOEXIST;
693                 return -1;
694         }
695
696         ecode = delete_from_hash(tdb, &h);
697         if (ecode != TDB_SUCCESS) {
698                 tdb->ecode = ecode;
699                 goto unlock_err;
700         }
701
702         /* Free the deleted entry. */
703         add_stat(tdb, frees, 1);
704         ecode = add_free_record(tdb, off,
705                                 sizeof(struct tdb_used_record)
706                                 + rec_key_length(&rec)
707                                 + rec_data_length(&rec)
708                                 + rec_extra_padding(&rec));
709         if (ecode != TDB_SUCCESS) {
710                 tdb->ecode = ecode;
711                 goto unlock_err;
712         }
713
714         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
715         return 0;
716
717 unlock_err:
718         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
719         return -1;
720 }
721
722 int tdb_close(struct tdb_context *tdb)
723 {
724         struct tdb_context **i;
725         int ret = 0;
726
727         tdb_trace(tdb, "tdb_close");
728
729         if (tdb->transaction) {
730                 tdb_transaction_cancel(tdb);
731         }
732
733         if (tdb->map_ptr) {
734                 if (tdb->flags & TDB_INTERNAL)
735                         free(tdb->map_ptr);
736                 else
737                         tdb_munmap(tdb);
738         }
739         free((char *)tdb->name);
740         if (tdb->fd != -1) {
741                 ret = close(tdb->fd);
742                 tdb->fd = -1;
743         }
744         free(tdb->lockrecs);
745
746         /* Remove from contexts list */
747         for (i = &tdbs; *i; i = &(*i)->next) {
748                 if (*i == tdb) {
749                         *i = tdb->next;
750                         break;
751                 }
752         }
753
754 #ifdef TDB_TRACE
755         close(tdb->tracefd);
756 #endif
757         free(tdb);
758
759         return ret;
760 }
761
762 enum TDB_ERROR tdb_error(const struct tdb_context *tdb)
763 {
764         return tdb->ecode;
765 }
766
767 const char *tdb_errorstr(const struct tdb_context *tdb)
768 {
769         /* Gcc warns if you miss a case in the switch, so use that. */
770         switch (tdb->ecode) {
771         case TDB_SUCCESS: return "Success";
772         case TDB_ERR_CORRUPT: return "Corrupt database";
773         case TDB_ERR_IO: return "IO Error";
774         case TDB_ERR_LOCK: return "Locking error";
775         case TDB_ERR_OOM: return "Out of memory";
776         case TDB_ERR_EXISTS: return "Record exists";
777         case TDB_ERR_EINVAL: return "Invalid parameter";
778         case TDB_ERR_NOEXIST: return "Record does not exist";
779         case TDB_ERR_RDONLY: return "write not permitted";
780         }
781         return "Invalid error code";
782 }
783
784 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
785                                enum TDB_ERROR ecode,
786                                enum tdb_log_level level,
787                                const char *fmt, ...)
788 {
789         char *message;
790         va_list ap;
791         size_t len;
792         /* tdb_open paths care about errno, so save it. */
793         int saved_errno = errno;
794
795         tdb->ecode = ecode;
796
797         if (!tdb->logfn)
798                 return ecode;
799
800         /* FIXME: Doesn't assume asprintf. */
801         va_start(ap, fmt);
802         len = vsnprintf(NULL, 0, fmt, ap);
803         va_end(ap);
804
805         message = malloc(len + 1);
806         if (!message) {
807                 tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
808                            "out of memory formatting message:");
809                 tdb->logfn(tdb, level, tdb->log_private, fmt);
810                 return ecode;
811         }
812         va_start(ap, fmt);
813         len = vsprintf(message, fmt, ap);
814         va_end(ap);
815         tdb->logfn(tdb, level, tdb->log_private, message);
816         free(message);
817         errno = saved_errno;
818         return ecode;
819 }