]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/open.c
tdb2: make jenkins_hash function non-static, rename to tdb_jenkins_hash.
[ccan] / ccan / tdb2 / open.c
1  /*
2    Trivial Database 2: opening and closing TDBs
3    Copyright (C) Rusty Russell 2010
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 3 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "private.h"
19 #include <assert.h>
20
21 /* all tdbs, to detect double-opens (fcntl file don't nest!) */
22 static struct tdb_context *tdbs = NULL;
23
24 static struct tdb_file *find_file(dev_t device, ino_t ino)
25 {
26         struct tdb_context *i;
27
28         for (i = tdbs; i; i = i->next) {
29                 if (i->file->device == device && i->file->inode == ino) {
30                         i->file->refcnt++;
31                         return i->file;
32                 }
33         }
34         return NULL;
35 }
36
37 static bool read_all(int fd, void *buf, size_t len)
38 {
39         while (len) {
40                 ssize_t ret;
41                 ret = read(fd, buf, len);
42                 if (ret < 0)
43                         return false;
44                 if (ret == 0) {
45                         /* ETOOSHORT? */
46                         errno = EWOULDBLOCK;
47                         return false;
48                 }
49                 buf = (char *)buf + ret;
50                 len -= ret;
51         }
52         return true;
53 }
54
55 static uint64_t random_number(struct tdb_context *tdb)
56 {
57         int fd;
58         uint64_t ret = 0;
59         struct timeval now;
60
61         fd = open("/dev/urandom", O_RDONLY);
62         if (fd >= 0) {
63                 if (read_all(fd, &ret, sizeof(ret))) {
64                         close(fd);
65                         return ret;
66                 }
67                 close(fd);
68         }
69         /* FIXME: Untested!  Based on Wikipedia protocol description! */
70         fd = open("/dev/egd-pool", O_RDWR);
71         if (fd >= 0) {
72                 /* Command is 1, next byte is size we want to read. */
73                 char cmd[2] = { 1, sizeof(uint64_t) };
74                 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
75                         char reply[1 + sizeof(uint64_t)];
76                         int r = read(fd, reply, sizeof(reply));
77                         if (r > 1) {
78                                 /* Copy at least some bytes. */
79                                 memcpy(&ret, reply+1, r - 1);
80                                 if (reply[0] == sizeof(uint64_t)
81                                     && r == sizeof(reply)) {
82                                         close(fd);
83                                         return ret;
84                                 }
85                         }
86                 }
87                 close(fd);
88         }
89
90         /* Fallback: pid and time. */
91         gettimeofday(&now, NULL);
92         ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
93         tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
94                    "tdb_open: random from getpid and time");
95         return ret;
96 }
97
98 struct new_database {
99         struct tdb_header hdr;
100         struct tdb_freetable ftable;
101 };
102
103 /* initialise a new database */
104 static enum TDB_ERROR tdb_new_database(struct tdb_context *tdb,
105                                        struct tdb_attribute_seed *seed,
106                                        struct tdb_header *hdr)
107 {
108         /* We make it up in memory, then write it out if not internal */
109         struct new_database newdb;
110         unsigned int magic_len;
111         ssize_t rlen;
112         enum TDB_ERROR ecode;
113
114         /* Fill in the header */
115         newdb.hdr.version = TDB_VERSION;
116         if (seed)
117                 newdb.hdr.hash_seed = seed->seed;
118         else
119                 newdb.hdr.hash_seed = random_number(tdb);
120         newdb.hdr.hash_test = TDB_HASH_MAGIC;
121         newdb.hdr.hash_test = tdb->hash_fn(&newdb.hdr.hash_test,
122                                            sizeof(newdb.hdr.hash_test),
123                                            newdb.hdr.hash_seed,
124                                            tdb->hash_data);
125         newdb.hdr.recovery = 0;
126         newdb.hdr.features_used = newdb.hdr.features_offered = TDB_FEATURE_MASK;
127         newdb.hdr.seqnum = 0;
128         memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
129         /* Initial hashes are empty. */
130         memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
131
132         /* Free is empty. */
133         newdb.hdr.free_table = offsetof(struct new_database, ftable);
134         memset(&newdb.ftable, 0, sizeof(newdb.ftable));
135         ecode = set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
136                            sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
137                            sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
138                            0);
139         if (ecode != TDB_SUCCESS) {
140                 return ecode;
141         }
142
143         /* Magic food */
144         memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
145         strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
146
147         /* This creates an endian-converted database, as if read from disk */
148         magic_len = sizeof(newdb.hdr.magic_food);
149         tdb_convert(tdb,
150                     (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
151
152         *hdr = newdb.hdr;
153
154         if (tdb->flags & TDB_INTERNAL) {
155                 tdb->file->map_size = sizeof(newdb);
156                 tdb->file->map_ptr = malloc(tdb->file->map_size);
157                 if (!tdb->file->map_ptr) {
158                         return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
159                                           "tdb_new_database:"
160                                           " failed to allocate");
161                 }
162                 memcpy(tdb->file->map_ptr, &newdb, tdb->file->map_size);
163                 return TDB_SUCCESS;
164         }
165         if (lseek(tdb->file->fd, 0, SEEK_SET) == -1) {
166                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
167                                   "tdb_new_database:"
168                                   " failed to seek: %s", strerror(errno));
169         }
170
171         if (ftruncate(tdb->file->fd, 0) == -1) {
172                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
173                                   "tdb_new_database:"
174                                   " failed to truncate: %s", strerror(errno));
175         }
176
177         rlen = write(tdb->file->fd, &newdb, sizeof(newdb));
178         if (rlen != sizeof(newdb)) {
179                 if (rlen >= 0)
180                         errno = ENOSPC;
181                 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
182                                   "tdb_new_database: %zi writing header: %s",
183                                   rlen, strerror(errno));
184         }
185         return TDB_SUCCESS;
186 }
187
188 static enum TDB_ERROR tdb_new_file(struct tdb_context *tdb)
189 {
190         tdb->file = malloc(sizeof(*tdb->file));
191         if (!tdb->file)
192                 return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
193                                   "tdb_open: cannot alloc tdb_file structure");
194         tdb->file->num_lockrecs = 0;
195         tdb->file->lockrecs = NULL;
196         tdb->file->allrecord_lock.count = 0;
197         tdb->file->refcnt = 1;
198         return TDB_SUCCESS;
199 }
200
201 enum TDB_ERROR tdb_set_attribute(struct tdb_context *tdb,
202                                  const union tdb_attribute *attr)
203 {
204         switch (attr->base.attr) {
205         case TDB_ATTRIBUTE_LOG:
206                 tdb->log_fn = attr->log.fn;
207                 tdb->log_data = attr->log.data;
208                 break;
209         case TDB_ATTRIBUTE_HASH:
210         case TDB_ATTRIBUTE_SEED:
211         case TDB_ATTRIBUTE_OPENHOOK:
212                 return tdb->last_error
213                         = tdb_logerr(tdb, TDB_ERR_EINVAL,
214                                      TDB_LOG_USE_ERROR,
215                                      "tdb_set_attribute:"
216                                      " cannot set %s after opening",
217                                      attr->base.attr == TDB_ATTRIBUTE_HASH
218                                      ? "TDB_ATTRIBUTE_HASH"
219                                      : attr->base.attr == TDB_ATTRIBUTE_SEED
220                                      ? "TDB_ATTRIBUTE_SEED"
221                                      : "TDB_ATTRIBUTE_OPENHOOK");
222         case TDB_ATTRIBUTE_STATS:
223                 return tdb->last_error
224                         = tdb_logerr(tdb, TDB_ERR_EINVAL,
225                                      TDB_LOG_USE_ERROR,
226                                      "tdb_set_attribute:"
227                                      " cannot set TDB_ATTRIBUTE_STATS");
228         case TDB_ATTRIBUTE_FLOCK:
229                 tdb->lock_fn = attr->flock.lock;
230                 tdb->unlock_fn = attr->flock.unlock;
231                 tdb->lock_data = attr->flock.data;
232                 break;
233         default:
234                 return tdb->last_error
235                         = tdb_logerr(tdb, TDB_ERR_EINVAL,
236                                      TDB_LOG_USE_ERROR,
237                                      "tdb_set_attribute:"
238                                      " unknown attribute type %u",
239                                      attr->base.attr);
240         }
241         return TDB_SUCCESS;
242 }
243
244 enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb,
245                                  union tdb_attribute *attr)
246 {
247         switch (attr->base.attr) {
248         case TDB_ATTRIBUTE_LOG:
249                 if (!tdb->log_fn)
250                         return tdb->last_error = TDB_ERR_NOEXIST;
251                 attr->log.fn = tdb->log_fn;
252                 attr->log.data = tdb->log_data;
253                 break;
254         case TDB_ATTRIBUTE_HASH:
255                 attr->hash.fn = tdb->hash_fn;
256                 attr->hash.data = tdb->hash_data;
257                 break;
258         case TDB_ATTRIBUTE_SEED:
259                 attr->seed.seed = tdb->hash_seed;
260                 break;
261         case TDB_ATTRIBUTE_OPENHOOK:
262                 if (!tdb->openhook)
263                         return tdb->last_error = TDB_ERR_NOEXIST;
264                 attr->openhook.fn = tdb->openhook;
265                 attr->openhook.data = tdb->openhook_data;
266                 break;
267         case TDB_ATTRIBUTE_STATS: {
268                 size_t size = attr->stats.size;
269                 if (size > tdb->stats.size)
270                         size = tdb->stats.size;
271                 memcpy(&attr->stats, &tdb->stats, size);
272                 break;
273         }
274         case TDB_ATTRIBUTE_FLOCK:
275                 attr->flock.lock = tdb->lock_fn;
276                 attr->flock.unlock = tdb->unlock_fn;
277                 attr->flock.data = tdb->lock_data;
278                 break;
279         default:
280                 return tdb->last_error
281                         = tdb_logerr(tdb, TDB_ERR_EINVAL,
282                                      TDB_LOG_USE_ERROR,
283                                      "tdb_get_attribute:"
284                                      " unknown attribute type %u",
285                                      attr->base.attr);
286         }
287         attr->base.next = NULL;
288         return TDB_SUCCESS;
289 }
290
291 void tdb_unset_attribute(struct tdb_context *tdb,
292                          enum tdb_attribute_type type)
293 {
294         switch (type) {
295         case TDB_ATTRIBUTE_LOG:
296                 tdb->log_fn = NULL;
297                 break;
298         case TDB_ATTRIBUTE_OPENHOOK:
299                 tdb->openhook = NULL;
300                 break;
301         case TDB_ATTRIBUTE_HASH:
302         case TDB_ATTRIBUTE_SEED:
303                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
304                            "tdb_unset_attribute: cannot unset %s after opening",
305                            type == TDB_ATTRIBUTE_HASH
306                            ? "TDB_ATTRIBUTE_HASH"
307                            : "TDB_ATTRIBUTE_SEED");
308                 break;
309         case TDB_ATTRIBUTE_STATS:
310                 tdb_logerr(tdb, TDB_ERR_EINVAL,
311                            TDB_LOG_USE_ERROR,
312                            "tdb_unset_attribute:"
313                            "cannot unset TDB_ATTRIBUTE_STATS");
314                 break;
315         case TDB_ATTRIBUTE_FLOCK:
316                 tdb->lock_fn = tdb_fcntl_lock;
317                 tdb->unlock_fn = tdb_fcntl_unlock;
318                 break;
319         default:
320                 tdb_logerr(tdb, TDB_ERR_EINVAL,
321                            TDB_LOG_USE_ERROR,
322                            "tdb_unset_attribute: unknown attribute type %u",
323                            type);
324         }
325 }
326
327 struct tdb_context *tdb_open(const char *name, int tdb_flags,
328                              int open_flags, mode_t mode,
329                              union tdb_attribute *attr)
330 {
331         struct tdb_context *tdb;
332         struct stat st;
333         int saved_errno = 0;
334         uint64_t hash_test;
335         unsigned v;
336         ssize_t rlen;
337         struct tdb_header hdr;
338         struct tdb_attribute_seed *seed = NULL;
339         tdb_bool_err berr;
340         enum TDB_ERROR ecode;
341         int openlock;
342
343         tdb = malloc(sizeof(*tdb) + (name ? strlen(name) + 1 : 0));
344         if (!tdb) {
345                 /* Can't log this */
346                 errno = ENOMEM;
347                 return NULL;
348         }
349         /* Set name immediately for logging functions. */
350         if (name) {
351                 tdb->name = strcpy((char *)(tdb + 1), name);
352         } else {
353                 tdb->name = NULL;
354         }
355         tdb->flags = tdb_flags;
356         tdb->log_fn = NULL;
357         tdb->open_flags = open_flags;
358         tdb->last_error = TDB_SUCCESS;
359         tdb->file = NULL;
360         tdb->openhook = NULL;
361         tdb->lock_fn = tdb_fcntl_lock;
362         tdb->unlock_fn = tdb_fcntl_unlock;
363         tdb->hash_fn = tdb_jenkins_hash;
364         memset(&tdb->stats, 0, sizeof(tdb->stats));
365         tdb->stats.base.attr = TDB_ATTRIBUTE_STATS;
366         tdb->stats.size = sizeof(tdb->stats);
367         tdb_io_init(tdb);
368         tdb->tdb2.direct_access = 0;
369         tdb->tdb2.transaction = NULL;
370         tdb->tdb2.access = NULL;
371
372         while (attr) {
373                 switch (attr->base.attr) {
374                 case TDB_ATTRIBUTE_HASH:
375                         tdb->hash_fn = attr->hash.fn;
376                         tdb->hash_data = attr->hash.data;
377                         break;
378                 case TDB_ATTRIBUTE_SEED:
379                         seed = &attr->seed;
380                         break;
381                 case TDB_ATTRIBUTE_OPENHOOK:
382                         tdb->openhook = attr->openhook.fn;
383                         tdb->openhook_data = attr->openhook.data;
384                         break;
385                 default:
386                         /* These are set as normal. */
387                         ecode = tdb_set_attribute(tdb, attr);
388                         if (ecode != TDB_SUCCESS)
389                                 goto fail;
390                 }
391                 attr = attr->base.next;
392         }
393
394         if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
395                           | TDB_NOSYNC | TDB_SEQNUM | TDB_ALLOW_NESTING
396                           | TDB_RDONLY)) {
397                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
398                                    "tdb_open: unknown flags %u", tdb_flags);
399                 goto fail;
400         }
401
402         if ((open_flags & O_ACCMODE) == O_WRONLY) {
403                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
404                                    "tdb_open: can't open tdb %s write-only",
405                                    name);
406                 goto fail;
407         }
408
409         if ((open_flags & O_ACCMODE) == O_RDONLY) {
410                 openlock = F_RDLCK;
411                 tdb->flags |= TDB_RDONLY;
412         } else {
413                 if (tdb_flags & TDB_RDONLY) {
414                         ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
415                                            TDB_LOG_USE_ERROR,
416                                            "tdb_open: can't use TDB_RDONLY"
417                                            " without O_RDONLY");
418                         goto fail;
419                 }
420                 openlock = F_WRLCK;
421         }
422
423         /* internal databases don't need any of the rest. */
424         if (tdb->flags & TDB_INTERNAL) {
425                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
426                 ecode = tdb_new_file(tdb);
427                 if (ecode != TDB_SUCCESS) {
428                         goto fail;
429                 }
430                 tdb->file->fd = -1;
431                 ecode = tdb_new_database(tdb, seed, &hdr);
432                 if (ecode != TDB_SUCCESS) {
433                         goto fail;
434                 }
435                 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
436                 tdb->hash_seed = hdr.hash_seed;
437                 tdb_ftable_init(tdb);
438                 return tdb;
439         }
440
441         if (stat(name, &st) != -1)
442                 tdb->file = find_file(st.st_dev, st.st_ino);
443
444         if (!tdb->file) {
445                 int fd;
446
447                 if ((fd = open(name, open_flags, mode)) == -1) {
448                         /* errno set by open(2) */
449                         saved_errno = errno;
450                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
451                                    "tdb_open: could not open file %s: %s",
452                                    name, strerror(errno));
453                         goto fail_errno;
454                 }
455
456                 /* on exec, don't inherit the fd */
457                 v = fcntl(fd, F_GETFD, 0);
458                 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
459
460                 if (fstat(fd, &st) == -1) {
461                         saved_errno = errno;
462                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
463                                    "tdb_open: could not stat open %s: %s",
464                                    name, strerror(errno));
465                         close(fd);
466                         goto fail_errno;
467                 }
468
469                 ecode = tdb_new_file(tdb);
470                 if (ecode != TDB_SUCCESS) {
471                         close(fd);
472                         goto fail;
473                 }
474
475                 tdb->file->fd = fd;
476                 tdb->file->device = st.st_dev;
477                 tdb->file->inode = st.st_ino;
478                 tdb->file->map_ptr = NULL;
479                 tdb->file->map_size = sizeof(struct tdb_header);
480         }
481
482         /* ensure there is only one process initialising at once */
483         ecode = tdb_lock_open(tdb, openlock, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
484         if (ecode != TDB_SUCCESS) {
485                 saved_errno = errno;
486                 goto fail_errno;
487         }
488
489         /* call their open hook if they gave us one. */
490         if (tdb->openhook) {
491                 ecode = tdb->openhook(tdb->file->fd, tdb->openhook_data);
492                 if (ecode != TDB_SUCCESS) {
493                         tdb_logerr(tdb, ecode, TDB_LOG_ERROR,
494                                    "tdb_open: open hook failed");
495                         goto fail;
496                 }
497                 open_flags |= O_CREAT;
498         }
499
500         /* If they used O_TRUNC, read will return 0. */
501         rlen = pread(tdb->file->fd, &hdr, sizeof(hdr), 0);
502         if (rlen == 0 && (open_flags & O_CREAT)) {
503                 ecode = tdb_new_database(tdb, seed, &hdr);
504                 if (ecode != TDB_SUCCESS) {
505                         goto fail;
506                 }
507         } else if (rlen < 0) {
508                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
509                                    "tdb_open: error %s reading %s",
510                                    strerror(errno), name);
511                 goto fail;
512         } else if (rlen < sizeof(hdr)
513                    || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
514                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
515                                    "tdb_open: %s is not a tdb file", name);
516                 goto fail;
517         }
518
519         if (hdr.version != TDB_VERSION) {
520                 if (hdr.version == bswap_64(TDB_VERSION))
521                         tdb->flags |= TDB_CONVERT;
522                 else {
523                         /* wrong version */
524                         ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
525                                            "tdb_open:"
526                                            " %s is unknown version 0x%llx",
527                                            name, (long long)hdr.version);
528                         goto fail;
529                 }
530         } else if (tdb->flags & TDB_CONVERT) {
531                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
532                                    "tdb_open:"
533                                    " %s does not need TDB_CONVERT",
534                                    name);
535                 goto fail;
536         }
537
538         tdb_convert(tdb, &hdr, sizeof(hdr));
539         tdb->hash_seed = hdr.hash_seed;
540         hash_test = TDB_HASH_MAGIC;
541         hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
542         if (hdr.hash_test != hash_test) {
543                 /* wrong hash variant */
544                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
545                                    "tdb_open:"
546                                    " %s uses a different hash function",
547                                    name);
548                 goto fail;
549         }
550
551         /* Clear any features we don't understand. */
552         if ((open_flags & O_ACCMODE) != O_RDONLY) {
553                 hdr.features_used &= TDB_FEATURE_MASK;
554                 ecode = tdb_write_convert(tdb, offsetof(struct tdb_header,
555                                                         features_used),
556                                           &hdr.features_used,
557                                           sizeof(hdr.features_used));
558                 if (ecode != TDB_SUCCESS)
559                         goto fail;
560         }
561
562         tdb_unlock_open(tdb, openlock);
563
564         /* This make sure we have current map_size and mmap. */
565         ecode = tdb->tdb2.io->oob(tdb, tdb->file->map_size + 1, true);
566         if (unlikely(ecode != TDB_SUCCESS))
567                 goto fail;
568
569         /* Now it's fully formed, recover if necessary. */
570         berr = tdb_needs_recovery(tdb);
571         if (unlikely(berr != false)) {
572                 if (berr < 0) {
573                         ecode = berr;
574                         goto fail;
575                 }
576                 ecode = tdb_lock_and_recover(tdb);
577                 if (ecode != TDB_SUCCESS) {
578                         goto fail;
579                 }
580         }
581
582         ecode = tdb_ftable_init(tdb);
583         if (ecode != TDB_SUCCESS) {
584                 goto fail;
585         }
586
587         tdb->next = tdbs;
588         tdbs = tdb;
589         return tdb;
590
591  fail:
592         /* Map ecode to some logical errno. */
593         switch (ecode) {
594         case TDB_ERR_CORRUPT:
595         case TDB_ERR_IO:
596                 saved_errno = EIO;
597                 break;
598         case TDB_ERR_LOCK:
599                 saved_errno = EWOULDBLOCK;
600                 break;
601         case TDB_ERR_OOM:
602                 saved_errno = ENOMEM;
603                 break;
604         case TDB_ERR_EINVAL:
605                 saved_errno = EINVAL;
606                 break;
607         default:
608                 saved_errno = EINVAL;
609                 break;
610         }
611
612 fail_errno:
613 #ifdef TDB_TRACE
614         close(tdb->tracefd);
615 #endif
616         if (tdb->file) {
617                 tdb_lock_cleanup(tdb);
618                 if (--tdb->file->refcnt == 0) {
619                         assert(tdb->file->num_lockrecs == 0);
620                         if (tdb->file->map_ptr) {
621                                 if (tdb->flags & TDB_INTERNAL) {
622                                         free(tdb->file->map_ptr);
623                                 } else
624                                         tdb_munmap(tdb->file);
625                         }
626                         if (close(tdb->file->fd) != 0)
627                                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
628                                            "tdb_open: failed to close tdb fd"
629                                            " on error: %s", strerror(errno));
630                         free(tdb->file->lockrecs);
631                         free(tdb->file);
632                 }
633         }
634
635         free(tdb);
636         errno = saved_errno;
637         return NULL;
638 }
639
640 int tdb_close(struct tdb_context *tdb)
641 {
642         int ret = 0;
643         struct tdb_context **i;
644
645         tdb_trace(tdb, "tdb_close");
646
647         if (tdb->tdb2.transaction) {
648                 tdb_transaction_cancel(tdb);
649         }
650
651         if (tdb->file->map_ptr) {
652                 if (tdb->flags & TDB_INTERNAL)
653                         free(tdb->file->map_ptr);
654                 else
655                         tdb_munmap(tdb->file);
656         }
657         if (tdb->file) {
658                 tdb_lock_cleanup(tdb);
659                 if (--tdb->file->refcnt == 0) {
660                         ret = close(tdb->file->fd);
661                         free(tdb->file->lockrecs);
662                         free(tdb->file);
663                 }
664         }
665
666         /* Remove from tdbs list */
667         for (i = &tdbs; *i; i = &(*i)->next) {
668                 if (*i == tdb) {
669                         *i = tdb->next;
670                         break;
671                 }
672         }
673
674 #ifdef TDB_TRACE
675         close(tdb->tracefd);
676 #endif
677         free(tdb);
678
679         return ret;
680 }
681
682 void tdb_foreach_(int (*fn)(struct tdb_context *, void *), void *p)
683 {
684         struct tdb_context *i;
685
686         for (i = tdbs; i; i = i->next) {
687                 if (fn(i, p) != 0)
688                         break;
689         }
690 }