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