]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb.c
b5e5457691257cbc8034a393f9d2bdeaf091a3cf
[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         newdb.hdr.features_used = newdb.hdr.features_offered = TDB_FEATURE_MASK;
116         memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
117         /* Initial hashes are empty. */
118         memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
119
120         /* Free is empty. */
121         newdb.hdr.free_table = offsetof(struct new_database, ftable);
122         memset(&newdb.ftable, 0, sizeof(newdb.ftable));
123         ecode = set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
124                            sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
125                            sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
126                            0);
127         if (ecode != TDB_SUCCESS) {
128                 return ecode;
129         }
130
131         /* Magic food */
132         memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
133         strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
134
135         /* This creates an endian-converted database, as if read from disk */
136         magic_len = sizeof(newdb.hdr.magic_food);
137         tdb_convert(tdb,
138                     (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
139
140         *hdr = newdb.hdr;
141
142         if (tdb->flags & TDB_INTERNAL) {
143                 tdb->map_size = sizeof(newdb);
144                 tdb->map_ptr = malloc(tdb->map_size);
145                 if (!tdb->map_ptr) {
146                         return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
147                                           "tdb_new_database:"
148                                           " failed to allocate");
149                 }
150                 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
151                 return TDB_SUCCESS;
152         }
153         if (lseek(tdb->fd, 0, SEEK_SET) == -1) {
154                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
155                                   "tdb_new_database:"
156                                   " failed to seek: %s", strerror(errno));
157         }
158
159         if (ftruncate(tdb->fd, 0) == -1) {
160                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
161                                   "tdb_new_database:"
162                                   " failed to truncate: %s", strerror(errno));
163         }
164
165         rlen = write(tdb->fd, &newdb, sizeof(newdb));
166         if (rlen != sizeof(newdb)) {
167                 if (rlen >= 0)
168                         errno = ENOSPC;
169                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
170                                   "tdb_new_database: %zi writing header: %s",
171                                   rlen, strerror(errno));
172         }
173         return TDB_SUCCESS;
174 }
175
176 struct tdb_context *tdb_open(const char *name, int tdb_flags,
177                              int open_flags, mode_t mode,
178                              union tdb_attribute *attr)
179 {
180         struct tdb_context *tdb;
181         struct stat st;
182         int saved_errno = 0;
183         uint64_t hash_test;
184         unsigned v;
185         ssize_t rlen;
186         struct tdb_header hdr;
187         struct tdb_attribute_seed *seed = NULL;
188         tdb_bool_err berr;
189         enum TDB_ERROR ecode;
190
191         tdb = malloc(sizeof(*tdb));
192         if (!tdb) {
193                 /* Can't log this */
194                 errno = ENOMEM;
195                 return NULL;
196         }
197         tdb->name = NULL;
198         tdb->map_ptr = NULL;
199         tdb->direct_access = 0;
200         tdb->fd = -1;
201         tdb->map_size = sizeof(struct tdb_header);
202         tdb->flags = tdb_flags;
203         tdb->logfn = NULL;
204         tdb->transaction = NULL;
205         tdb->stats = NULL;
206         tdb->access = NULL;
207         tdb_hash_init(tdb);
208         tdb_io_init(tdb);
209         tdb_lock_init(tdb);
210
211         while (attr) {
212                 switch (attr->base.attr) {
213                 case TDB_ATTRIBUTE_LOG:
214                         tdb->logfn = attr->log.log_fn;
215                         tdb->log_private = attr->log.log_private;
216                         break;
217                 case TDB_ATTRIBUTE_HASH:
218                         tdb->khash = attr->hash.hash_fn;
219                         tdb->hash_priv = attr->hash.hash_private;
220                         break;
221                 case TDB_ATTRIBUTE_SEED:
222                         seed = &attr->seed;
223                         break;
224                 case TDB_ATTRIBUTE_STATS:
225                         tdb->stats = &attr->stats;
226                         /* They have stats we don't know about?  Tell them. */
227                         if (tdb->stats->size > sizeof(attr->stats))
228                                 tdb->stats->size = sizeof(attr->stats);
229                         break;
230                 default:
231                         ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
232                                            TDB_LOG_USE_ERROR,
233                                            "tdb_open:"
234                                            " unknown attribute type %u",
235                                            attr->base.attr);
236                         goto fail;
237                 }
238                 attr = attr->base.next;
239         }
240
241         if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
242                           | TDB_NOSYNC)) {
243                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
244                                    "tdb_open: unknown flags %u", tdb_flags);
245                 goto fail;
246         }
247
248         if ((open_flags & O_ACCMODE) == O_WRONLY) {
249                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
250                                    "tdb_open: can't open tdb %s write-only",
251                                    name);
252                 goto fail;
253         }
254
255         if ((open_flags & O_ACCMODE) == O_RDONLY) {
256                 tdb->read_only = true;
257                 tdb->mmap_flags = PROT_READ;
258         } else {
259                 tdb->read_only = false;
260                 tdb->mmap_flags = PROT_READ | PROT_WRITE;
261         }
262
263         /* internal databases don't need any of the rest. */
264         if (tdb->flags & TDB_INTERNAL) {
265                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
266                 ecode = tdb_new_database(tdb, seed, &hdr);
267                 if (ecode != TDB_SUCCESS) {
268                         goto fail;
269                 }
270                 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
271                 tdb->hash_seed = hdr.hash_seed;
272                 tdb_ftable_init(tdb);
273                 return tdb;
274         }
275
276         if ((tdb->fd = open(name, open_flags, mode)) == -1) {
277                 /* errno set by open(2) */
278                 saved_errno = errno;
279                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
280                                    "tdb_open: could not open file %s: %s",
281                                    name, strerror(errno));
282                 goto fail;
283         }
284
285         /* on exec, don't inherit the fd */
286         v = fcntl(tdb->fd, F_GETFD, 0);
287         fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
288
289         /* ensure there is only one process initialising at once */
290         ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
291         if (ecode != TDB_SUCCESS) {
292                 goto fail;
293         }
294
295         /* If they used O_TRUNC, read will return 0. */
296         rlen = read(tdb->fd, &hdr, sizeof(hdr));
297         if (rlen == 0 && (open_flags & O_CREAT)) {
298                 ecode = tdb_new_database(tdb, seed, &hdr);
299                 if (ecode != TDB_SUCCESS) {
300                         goto fail;
301                 }
302         } else if (rlen < 0) {
303                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
304                                    "tdb_open: error %s reading %s",
305                                    strerror(errno), name);
306                 goto fail;
307         } else if (rlen < sizeof(hdr)
308                    || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
309                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
310                                    "tdb_open: %s is not a tdb file", name);
311                 goto fail;
312         }
313
314         if (hdr.version != TDB_VERSION) {
315                 if (hdr.version == bswap_64(TDB_VERSION))
316                         tdb->flags |= TDB_CONVERT;
317                 else {
318                         /* wrong version */
319                         ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
320                                            "tdb_open:"
321                                            " %s is unknown version 0x%llx",
322                                            name, (long long)hdr.version);
323                         goto fail;
324                 }
325         }
326
327         tdb_convert(tdb, &hdr, sizeof(hdr));
328         tdb->hash_seed = hdr.hash_seed;
329         hash_test = TDB_HASH_MAGIC;
330         hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
331         if (hdr.hash_test != hash_test) {
332                 /* wrong hash variant */
333                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
334                                    "tdb_open:"
335                                    " %s uses a different hash function",
336                                    name);
337                 goto fail;
338         }
339
340         if (fstat(tdb->fd, &st) == -1) {
341                 saved_errno = errno;
342                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
343                                    "tdb_open: could not stat open %s: %s",
344                                    name, strerror(errno));
345                 goto fail;
346         }
347
348         /* Is it already in the open list?  If so, fail. */
349         if (tdb_already_open(st.st_dev, st.st_ino)) {
350                 /* FIXME */
351                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_ERROR,
352                                    "tdb_open: %s (%d,%d) is already open"
353                                    " in this process",
354                                    name, (int)st.st_dev, (int)st.st_ino);
355                 goto fail;
356         }
357
358         tdb->name = strdup(name);
359         if (!tdb->name) {
360                 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
361                                    "tdb_open: failed to allocate name");
362                 goto fail;
363         }
364
365         /* Clear any features we don't understand. */
366         if ((open_flags & O_ACCMODE) != O_RDONLY) {
367                 hdr.features_used &= TDB_FEATURE_MASK;
368                 if (tdb_write_convert(tdb, offsetof(struct tdb_header,
369                                                     features_used),
370                                       &hdr.features_used,
371                                       sizeof(hdr.features_used)) == -1)
372                         goto fail;
373         }
374
375         tdb->device = st.st_dev;
376         tdb->inode = st.st_ino;
377         tdb_unlock_open(tdb);
378
379         /* This make sure we have current map_size and mmap. */
380         tdb->methods->oob(tdb, tdb->map_size + 1, true);
381
382         /* Now it's fully formed, recover if necessary. */
383         berr = tdb_needs_recovery(tdb);
384         if (unlikely(berr != false)) {
385                 if (berr < 0) {
386                         ecode = berr;
387                         goto fail;
388                 }
389                 ecode = tdb_lock_and_recover(tdb);
390                 if (ecode != TDB_SUCCESS) {
391                         goto fail;
392                 }
393         }
394
395         ecode = tdb_ftable_init(tdb);
396         if (ecode != TDB_SUCCESS) {
397                 goto fail;
398         }
399
400         tdb->next = tdbs;
401         tdbs = tdb;
402         return tdb;
403
404  fail:
405         /* Map ecode to some logical errno. */
406         if (!saved_errno) {
407                 switch (ecode) {
408                 case TDB_ERR_CORRUPT:
409                 case TDB_ERR_IO:
410                         saved_errno = EIO;
411                         break;
412                 case TDB_ERR_LOCK:
413                         saved_errno = EWOULDBLOCK;
414                         break;
415                 case TDB_ERR_OOM:
416                         saved_errno = ENOMEM;
417                         break;
418                 case TDB_ERR_EINVAL:
419                         saved_errno = EINVAL;
420                         break;
421                 default:
422                         saved_errno = EINVAL;
423                         break;
424                 }
425         }
426
427 #ifdef TDB_TRACE
428         close(tdb->tracefd);
429 #endif
430         if (tdb->map_ptr) {
431                 if (tdb->flags & TDB_INTERNAL) {
432                         free(tdb->map_ptr);
433                 } else
434                         tdb_munmap(tdb);
435         }
436         free(tdb->lockrecs);
437         free((char *)tdb->name);
438         if (tdb->fd != -1)
439                 if (close(tdb->fd) != 0)
440                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
441                                    "tdb_open: failed to close tdb->fd"
442                                    " on error: %s", strerror(errno));
443         free(tdb);
444         errno = saved_errno;
445         return NULL;
446 }
447
448 static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb,
449                                      tdb_off_t off,
450                                      tdb_len_t keylen,
451                                      tdb_len_t datalen,
452                                      struct tdb_used_record *rec,
453                                      uint64_t h)
454 {
455         uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
456         enum TDB_ERROR ecode;
457
458         ecode = set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
459                            keylen + dataroom, h);
460         if (ecode == TDB_SUCCESS) {
461                 ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
462         }
463         return ecode;
464 }
465
466 static enum TDB_ERROR replace_data(struct tdb_context *tdb,
467                                    struct hash_info *h,
468                                    struct tdb_data key, struct tdb_data dbuf,
469                                    tdb_off_t old_off, tdb_len_t old_room,
470                                    bool growing)
471 {
472         tdb_off_t new_off;
473         enum TDB_ERROR ecode;
474
475         /* Allocate a new record. */
476         new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
477                         growing);
478         if (TDB_OFF_IS_ERR(new_off)) {
479                 return new_off;
480         }
481
482         /* We didn't like the existing one: remove it. */
483         if (old_off) {
484                 add_stat(tdb, frees, 1);
485                 ecode = add_free_record(tdb, old_off,
486                                         sizeof(struct tdb_used_record)
487                                         + key.dsize + old_room);
488                 if (ecode == TDB_SUCCESS)
489                         ecode = replace_in_hash(tdb, h, new_off);
490         } else {
491                 ecode = add_to_hash(tdb, h, new_off);
492         }
493         if (ecode != TDB_SUCCESS) {
494                 return ecode;
495         }
496
497         new_off += sizeof(struct tdb_used_record);
498         ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
499         if (ecode != TDB_SUCCESS) {
500                 return ecode;
501         }
502
503         new_off += key.dsize;
504         ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
505         if (ecode != TDB_SUCCESS) {
506                 return ecode;
507         }
508
509         /* FIXME: tdb_increment_seqnum(tdb); */
510         return TDB_SUCCESS;
511 }
512
513 static enum TDB_ERROR update_data(struct tdb_context *tdb,
514                                   tdb_off_t off,
515                                   struct tdb_data dbuf,
516                                   tdb_len_t extra)
517 {
518         enum TDB_ERROR ecode;
519
520         ecode = tdb->methods->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
521         if (ecode == TDB_SUCCESS && extra) {
522                 /* Put a zero in; future versions may append other data. */
523                 ecode = tdb->methods->twrite(tdb, off + dbuf.dsize, "", 1);
524         }
525         return ecode;
526 }
527
528 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
529                          struct tdb_data key, struct tdb_data dbuf, int flag)
530 {
531         struct hash_info h;
532         tdb_off_t off;
533         tdb_len_t old_room = 0;
534         struct tdb_used_record rec;
535         enum TDB_ERROR ecode;
536
537         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
538         if (TDB_OFF_IS_ERR(off)) {
539                 return off;
540         }
541
542         /* Now we have lock on this hash bucket. */
543         if (flag == TDB_INSERT) {
544                 if (off) {
545                         ecode = TDB_ERR_EXISTS;
546                         goto out;
547                 }
548         } else {
549                 if (off) {
550                         old_room = rec_data_length(&rec)
551                                 + rec_extra_padding(&rec);
552                         if (old_room >= dbuf.dsize) {
553                                 /* Can modify in-place.  Easy! */
554                                 ecode = update_rec_hdr(tdb, off,
555                                                        key.dsize, dbuf.dsize,
556                                                        &rec, h.h);
557                                 if (ecode != TDB_SUCCESS) {
558                                         goto out;
559                                 }
560                                 ecode = update_data(tdb,
561                                                     off + sizeof(rec)
562                                                     + key.dsize, dbuf,
563                                                     old_room - dbuf.dsize);
564                                 if (ecode != TDB_SUCCESS) {
565                                         goto out;
566                                 }
567                                 tdb_unlock_hashes(tdb, h.hlock_start,
568                                                   h.hlock_range, F_WRLCK);
569                                 return TDB_SUCCESS;
570                         }
571                 } else {
572                         if (flag == TDB_MODIFY) {
573                                 /* if the record doesn't exist and we
574                                    are in TDB_MODIFY mode then we should fail
575                                    the store */
576                                 ecode = TDB_ERR_NOEXIST;
577                                 goto out;
578                         }
579                 }
580         }
581
582         /* If we didn't use the old record, this implies we're growing. */
583         ecode = replace_data(tdb, &h, key, dbuf, off, old_room, off);
584 out:
585         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
586         return ecode;
587 }
588
589 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
590                           struct tdb_data key, struct tdb_data dbuf)
591 {
592         struct hash_info h;
593         tdb_off_t off;
594         struct tdb_used_record rec;
595         tdb_len_t old_room = 0, old_dlen;
596         unsigned char *newdata;
597         struct tdb_data new_dbuf;
598         enum TDB_ERROR ecode;
599
600         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
601         if (TDB_OFF_IS_ERR(off)) {
602                 return off;
603         }
604
605         if (off) {
606                 old_dlen = rec_data_length(&rec);
607                 old_room = old_dlen + rec_extra_padding(&rec);
608
609                 /* Fast path: can append in place. */
610                 if (rec_extra_padding(&rec) >= dbuf.dsize) {
611                         ecode = update_rec_hdr(tdb, off, key.dsize,
612                                                old_dlen + dbuf.dsize, &rec,
613                                                h.h);
614                         if (ecode != TDB_SUCCESS) {
615                                 goto out;
616                         }
617
618                         off += sizeof(rec) + key.dsize + old_dlen;
619                         ecode = update_data(tdb, off, dbuf,
620                                             rec_extra_padding(&rec));
621                         goto out;
622                 }
623
624                 /* Slow path. */
625                 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
626                 if (!newdata) {
627                         ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
628                                            "tdb_append:"
629                                            " failed to allocate %zu bytes",
630                                            (size_t)(key.dsize + old_dlen
631                                                     + dbuf.dsize));
632                         goto out;
633                 }
634                 ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
635                                             newdata, old_dlen);
636                 if (ecode != TDB_SUCCESS) {
637                         goto out_free_newdata;
638                 }
639                 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
640                 new_dbuf.dptr = newdata;
641                 new_dbuf.dsize = old_dlen + dbuf.dsize;
642         } else {
643                 newdata = NULL;
644                 new_dbuf = dbuf;
645         }
646
647         /* If they're using tdb_append(), it implies they're growing record. */
648         ecode = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
649
650 out_free_newdata:
651         free(newdata);
652 out:
653         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
654         return ecode;
655 }
656
657 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
658                          struct tdb_data *data)
659 {
660         tdb_off_t off;
661         struct tdb_used_record rec;
662         struct hash_info h;
663         enum TDB_ERROR ecode;
664
665         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
666         if (TDB_OFF_IS_ERR(off)) {
667                 return off;
668         }
669
670         if (!off) {
671                 ecode = TDB_ERR_NOEXIST;
672         } else {
673                 data->dsize = rec_data_length(&rec);
674                 data->dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
675                                             data->dsize);
676                 if (TDB_PTR_IS_ERR(data->dptr)) {
677                         ecode = TDB_PTR_ERR(data->dptr);
678                 } else
679                         ecode = TDB_SUCCESS;
680         }
681
682         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
683         return ecode;
684 }
685
686 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
687 {
688         tdb_off_t off;
689         struct tdb_used_record rec;
690         struct hash_info h;
691         enum TDB_ERROR ecode;
692
693         off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
694         if (TDB_OFF_IS_ERR(off)) {
695                 return off;
696         }
697
698         if (!off) {
699                 ecode = TDB_ERR_NOEXIST;
700                 goto unlock;
701         }
702
703         ecode = delete_from_hash(tdb, &h);
704         if (ecode != TDB_SUCCESS) {
705                 goto unlock;
706         }
707
708         /* Free the deleted entry. */
709         add_stat(tdb, frees, 1);
710         ecode = add_free_record(tdb, off,
711                                 sizeof(struct tdb_used_record)
712                                 + rec_key_length(&rec)
713                                 + rec_data_length(&rec)
714                                 + rec_extra_padding(&rec));
715
716 unlock:
717         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
718         return ecode;
719 }
720
721 int tdb_close(struct tdb_context *tdb)
722 {
723         struct tdb_context **i;
724         int ret = 0;
725
726         tdb_trace(tdb, "tdb_close");
727
728         if (tdb->transaction) {
729                 tdb_transaction_cancel(tdb);
730         }
731
732         if (tdb->map_ptr) {
733                 if (tdb->flags & TDB_INTERNAL)
734                         free(tdb->map_ptr);
735                 else
736                         tdb_munmap(tdb);
737         }
738         free((char *)tdb->name);
739         if (tdb->fd != -1) {
740                 ret = close(tdb->fd);
741                 tdb->fd = -1;
742         }
743         free(tdb->lockrecs);
744
745         /* Remove from contexts list */
746         for (i = &tdbs; *i; i = &(*i)->next) {
747                 if (*i == tdb) {
748                         *i = tdb->next;
749                         break;
750                 }
751         }
752
753 #ifdef TDB_TRACE
754         close(tdb->tracefd);
755 #endif
756         free(tdb);
757
758         return ret;
759 }
760
761 unsigned int tdb_get_flags(struct tdb_context *tdb)
762 {
763         return tdb->flags;
764 }
765
766 void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
767 {
768         if (tdb->flags & TDB_INTERNAL) {
769                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
770                            "tdb_add_flag: internal db");
771                 return;
772         }
773         switch (flag) {
774         case TDB_NOLOCK:
775                 tdb->flags |= TDB_NOLOCK;
776                 break;
777         case TDB_NOMMAP:
778                 tdb->flags |= TDB_NOMMAP;
779                 tdb_munmap(tdb);
780                 break;
781         case TDB_NOSYNC:
782                 tdb->flags |= TDB_NOSYNC;
783                 break;
784         default:
785                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
786                            "tdb_add_flag: Unknown flag %u", flag);
787         }
788 }
789
790 void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
791 {
792         if (tdb->flags & TDB_INTERNAL) {
793                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
794                            "tdb_remove_flag: internal db");
795                 return;
796         }
797         switch (flag) {
798         case TDB_NOLOCK:
799                 tdb->flags &= ~TDB_NOLOCK;
800                 break;
801         case TDB_NOMMAP:
802                 tdb->flags &= ~TDB_NOMMAP;
803                 tdb_mmap(tdb);
804                 break;
805         case TDB_NOSYNC:
806                 tdb->flags &= ~TDB_NOSYNC;
807                 break;
808         default:
809                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
810                            "tdb_remove_flag: Unknown flag %u", flag);
811         }
812 }
813
814 const char *tdb_errorstr(enum TDB_ERROR ecode)
815 {
816         /* Gcc warns if you miss a case in the switch, so use that. */
817         switch (ecode) {
818         case TDB_SUCCESS: return "Success";
819         case TDB_ERR_CORRUPT: return "Corrupt database";
820         case TDB_ERR_IO: return "IO Error";
821         case TDB_ERR_LOCK: return "Locking error";
822         case TDB_ERR_OOM: return "Out of memory";
823         case TDB_ERR_EXISTS: return "Record exists";
824         case TDB_ERR_EINVAL: return "Invalid parameter";
825         case TDB_ERR_NOEXIST: return "Record does not exist";
826         case TDB_ERR_RDONLY: return "write not permitted";
827         }
828         return "Invalid error code";
829 }
830
831 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
832                                enum TDB_ERROR ecode,
833                                enum tdb_log_level level,
834                                const char *fmt, ...)
835 {
836         char *message;
837         va_list ap;
838         size_t len;
839         /* tdb_open paths care about errno, so save it. */
840         int saved_errno = errno;
841
842         if (!tdb->logfn)
843                 return ecode;
844
845         va_start(ap, fmt);
846         len = vasprintf(&message, fmt, ap);
847         va_end(ap);
848
849         if (len < 0) {
850                 tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
851                            "out of memory formatting message:");
852                 tdb->logfn(tdb, level, tdb->log_private, fmt);
853         } else {
854                 tdb->logfn(tdb, level, tdb->log_private, message);
855                 free(message);
856         }
857         errno = saved_errno;
858         return ecode;
859 }