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