tdb2: tdb_set_attribute, tdb_unset_attribute and tdb_get_attribute
[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: could 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));
325         if (!tdb) {
326                 /* Can't log this */
327                 errno = ENOMEM;
328                 return NULL;
329         }
330         tdb->name = NULL;
331         tdb->direct_access = 0;
332         tdb->flags = tdb_flags;
333         tdb->log_fn = NULL;
334         tdb->transaction = NULL;
335         tdb->stats = NULL;
336         tdb->access = NULL;
337         tdb->last_error = TDB_SUCCESS;
338         tdb->file = NULL;
339         tdb->lock_fn = tdb_fcntl_lock;
340         tdb->unlock_fn = tdb_fcntl_unlock;
341         tdb->hash_fn = jenkins_hash;
342         tdb_io_init(tdb);
343
344         while (attr) {
345                 switch (attr->base.attr) {
346                 case TDB_ATTRIBUTE_HASH:
347                         tdb->hash_fn = attr->hash.fn;
348                         tdb->hash_data = attr->hash.data;
349                         break;
350                 case TDB_ATTRIBUTE_SEED:
351                         seed = &attr->seed;
352                         break;
353                 case TDB_ATTRIBUTE_STATS:
354                         tdb->stats = &attr->stats;
355                         /* They have stats we don't know about?  Tell them. */
356                         if (tdb->stats->size > sizeof(attr->stats))
357                                 tdb->stats->size = sizeof(attr->stats);
358                         break;
359                 case TDB_ATTRIBUTE_OPENHOOK:
360                         openhook = &attr->openhook;
361                         break;
362                 default:
363                         /* These are set as normal. */
364                         ecode = tdb_set_attribute(tdb, attr);
365                         if (ecode != TDB_SUCCESS)
366                                 goto fail;
367                 }
368                 attr = attr->base.next;
369         }
370
371         if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
372                           | TDB_NOSYNC | TDB_SEQNUM)) {
373                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
374                                    "tdb_open: unknown flags %u", tdb_flags);
375                 goto fail;
376         }
377
378         if ((open_flags & O_ACCMODE) == O_WRONLY) {
379                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
380                                    "tdb_open: can't open tdb %s write-only",
381                                    name);
382                 goto fail;
383         }
384
385         if ((open_flags & O_ACCMODE) == O_RDONLY) {
386                 tdb->read_only = true;
387                 tdb->mmap_flags = PROT_READ;
388         } else {
389                 tdb->read_only = false;
390                 tdb->mmap_flags = PROT_READ | PROT_WRITE;
391         }
392
393         /* internal databases don't need any of the rest. */
394         if (tdb->flags & TDB_INTERNAL) {
395                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
396                 ecode = tdb_new_file(tdb);
397                 if (ecode != TDB_SUCCESS) {
398                         goto fail;
399                 }
400                 tdb->file->fd = -1;
401                 ecode = tdb_new_database(tdb, seed, &hdr);
402                 if (ecode != TDB_SUCCESS) {
403                         goto fail;
404                 }
405                 if (name) {
406                         tdb->name = strdup(name);
407                         if (!tdb->name) {
408                                 ecode = tdb_logerr(tdb, TDB_ERR_OOM,
409                                                    TDB_LOG_ERROR,
410                                                    "tdb_open: failed to"
411                                                    " allocate name");
412                                 goto fail;
413                         }
414                 }
415                 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
416                 tdb->hash_seed = hdr.hash_seed;
417                 tdb_ftable_init(tdb);
418                 return tdb;
419         }
420
421         if (stat(name, &st) != -1)
422                 tdb->file = find_file(st.st_dev, st.st_ino);
423
424         if (!tdb->file) {
425                 int fd;
426
427                 if ((fd = open(name, open_flags, mode)) == -1) {
428                         /* errno set by open(2) */
429                         saved_errno = errno;
430                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
431                                    "tdb_open: could not open file %s: %s",
432                                    name, strerror(errno));
433                         goto fail_errno;
434                 }
435
436                 /* on exec, don't inherit the fd */
437                 v = fcntl(fd, F_GETFD, 0);
438                 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
439
440                 if (fstat(fd, &st) == -1) {
441                         saved_errno = errno;
442                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
443                                    "tdb_open: could not stat open %s: %s",
444                                    name, strerror(errno));
445                         goto fail_errno;
446                 }
447
448                 ecode = tdb_new_file(tdb);
449                 if (ecode != TDB_SUCCESS)
450                         goto fail;
451
452                 tdb->file->next = files;
453                 tdb->file->fd = fd;
454                 tdb->file->device = st.st_dev;
455                 tdb->file->inode = st.st_ino;
456                 tdb->file->map_ptr = NULL;
457                 tdb->file->map_size = sizeof(struct tdb_header);
458         }
459
460         /* ensure there is only one process initialising at once */
461         ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
462         if (ecode != TDB_SUCCESS) {
463                 saved_errno = errno;
464                 goto fail_errno;
465         }
466
467         /* call their open hook if they gave us one. */
468         if (openhook) {
469                 ecode = openhook->fn(tdb->file->fd, openhook->data);
470                 if (ecode != TDB_SUCCESS) {
471                         tdb_logerr(tdb, ecode, TDB_LOG_ERROR,
472                                    "tdb_open: open hook failed");
473                         goto fail;
474                 }
475                 open_flags |= O_CREAT;
476         }
477
478         /* If they used O_TRUNC, read will return 0. */
479         rlen = pread(tdb->file->fd, &hdr, sizeof(hdr), 0);
480         if (rlen == 0 && (open_flags & O_CREAT)) {
481                 ecode = tdb_new_database(tdb, seed, &hdr);
482                 if (ecode != TDB_SUCCESS) {
483                         goto fail;
484                 }
485         } else if (rlen < 0) {
486                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
487                                    "tdb_open: error %s reading %s",
488                                    strerror(errno), name);
489                 goto fail;
490         } else if (rlen < sizeof(hdr)
491                    || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
492                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
493                                    "tdb_open: %s is not a tdb file", name);
494                 goto fail;
495         }
496
497         if (hdr.version != TDB_VERSION) {
498                 if (hdr.version == bswap_64(TDB_VERSION))
499                         tdb->flags |= TDB_CONVERT;
500                 else {
501                         /* wrong version */
502                         ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
503                                            "tdb_open:"
504                                            " %s is unknown version 0x%llx",
505                                            name, (long long)hdr.version);
506                         goto fail;
507                 }
508         }
509
510         tdb_convert(tdb, &hdr, sizeof(hdr));
511         tdb->hash_seed = hdr.hash_seed;
512         hash_test = TDB_HASH_MAGIC;
513         hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
514         if (hdr.hash_test != hash_test) {
515                 /* wrong hash variant */
516                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
517                                    "tdb_open:"
518                                    " %s uses a different hash function",
519                                    name);
520                 goto fail;
521         }
522
523         tdb->name = strdup(name);
524         if (!tdb->name) {
525                 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
526                                    "tdb_open: failed to allocate name");
527                 goto fail;
528         }
529
530         /* Clear any features we don't understand. */
531         if ((open_flags & O_ACCMODE) != O_RDONLY) {
532                 hdr.features_used &= TDB_FEATURE_MASK;
533                 if (tdb_write_convert(tdb, offsetof(struct tdb_header,
534                                                     features_used),
535                                       &hdr.features_used,
536                                       sizeof(hdr.features_used)) == -1)
537                         goto fail;
538         }
539
540         tdb_unlock_open(tdb);
541
542         /* This make sure we have current map_size and mmap. */
543         tdb->methods->oob(tdb, tdb->file->map_size + 1, true);
544
545         /* Now it's fully formed, recover if necessary. */
546         berr = tdb_needs_recovery(tdb);
547         if (unlikely(berr != false)) {
548                 if (berr < 0) {
549                         ecode = berr;
550                         goto fail;
551                 }
552                 ecode = tdb_lock_and_recover(tdb);
553                 if (ecode != TDB_SUCCESS) {
554                         goto fail;
555                 }
556         }
557
558         ecode = tdb_ftable_init(tdb);
559         if (ecode != TDB_SUCCESS) {
560                 goto fail;
561         }
562
563         /* Add to linked list if we're new. */
564         if (tdb->file->refcnt == 1)
565                 files = tdb->file;
566         return tdb;
567
568  fail:
569         /* Map ecode to some logical errno. */
570         switch (ecode) {
571         case TDB_ERR_CORRUPT:
572         case TDB_ERR_IO:
573                 saved_errno = EIO;
574                 break;
575         case TDB_ERR_LOCK:
576                 saved_errno = EWOULDBLOCK;
577                 break;
578         case TDB_ERR_OOM:
579                 saved_errno = ENOMEM;
580                 break;
581         case TDB_ERR_EINVAL:
582                 saved_errno = EINVAL;
583                 break;
584         default:
585                 saved_errno = EINVAL;
586                 break;
587         }
588
589 fail_errno:
590 #ifdef TDB_TRACE
591         close(tdb->tracefd);
592 #endif
593         free(cast_const(char *, tdb->name));
594         if (tdb->file) {
595                 tdb_lock_cleanup(tdb);
596                 if (--tdb->file->refcnt == 0) {
597                         assert(tdb->file->num_lockrecs == 0);
598                         if (tdb->file->map_ptr) {
599                                 if (tdb->flags & TDB_INTERNAL) {
600                                         free(tdb->file->map_ptr);
601                                 } else
602                                         tdb_munmap(tdb->file);
603                         }
604                         if (close(tdb->file->fd) != 0)
605                                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
606                                            "tdb_open: failed to close tdb fd"
607                                            " on error: %s", strerror(errno));
608                         free(tdb->file->lockrecs);
609                         free(tdb->file);
610                 }
611         }
612
613         free(tdb);
614         errno = saved_errno;
615         return NULL;
616 }
617
618 int tdb_close(struct tdb_context *tdb)
619 {
620         int ret = 0;
621
622         tdb_trace(tdb, "tdb_close");
623
624         if (tdb->transaction) {
625                 tdb_transaction_cancel(tdb);
626         }
627
628         if (tdb->file->map_ptr) {
629                 if (tdb->flags & TDB_INTERNAL)
630                         free(tdb->file->map_ptr);
631                 else
632                         tdb_munmap(tdb->file);
633         }
634         free(cast_const(char *, tdb->name));
635         if (tdb->file) {
636                 struct tdb_file **i;
637
638                 tdb_lock_cleanup(tdb);
639                 if (--tdb->file->refcnt == 0) {
640                         ret = close(tdb->file->fd);
641
642                         /* Remove from files list */
643                         for (i = &files; *i; i = &(*i)->next) {
644                                 if (*i == tdb->file) {
645                                         *i = tdb->file->next;
646                                         break;
647                                 }
648                         }
649                         free(tdb->file->lockrecs);
650                         free(tdb->file);
651                 }
652         }
653
654 #ifdef TDB_TRACE
655         close(tdb->tracefd);
656 #endif
657         free(tdb);
658
659         return ret;
660 }