tdb2: allow transaction to nest.
[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_STATS:
207                 return tdb->last_error
208                         = tdb_logerr(tdb, TDB_ERR_EINVAL,
209                                      TDB_LOG_USE_ERROR,
210                                      "tdb_set_attribute:"
211                                      " cannot set TDB_ATTRIBUTE_STATS");
212         case TDB_ATTRIBUTE_FLOCK:
213                 tdb->lock_fn = attr->flock.lock;
214                 tdb->unlock_fn = attr->flock.unlock;
215                 tdb->lock_data = attr->flock.data;
216                 break;
217         default:
218                 return tdb->last_error
219                         = tdb_logerr(tdb, TDB_ERR_EINVAL,
220                                      TDB_LOG_USE_ERROR,
221                                      "tdb_set_attribute:"
222                                      " unknown attribute type %u",
223                                      attr->base.attr);
224         }
225         return TDB_SUCCESS;
226 }
227
228 static uint64_t jenkins_hash(const void *key, size_t length, uint64_t seed,
229                              void *unused)
230 {
231         uint64_t ret;
232         /* hash64_stable assumes lower bits are more important; they are a
233          * slightly better hash.  We use the upper bits first, so swap them. */
234         ret = hash64_stable((const unsigned char *)key, length, seed);
235         return (ret >> 32) | (ret << 32);
236 }
237
238 enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb,
239                                  union tdb_attribute *attr)
240 {
241         switch (attr->base.attr) {
242         case TDB_ATTRIBUTE_LOG:
243                 if (!tdb->log_fn)
244                         return tdb->last_error = TDB_ERR_NOEXIST;
245                 attr->log.fn = tdb->log_fn;
246                 attr->log.data = tdb->log_data;
247                 break;
248         case TDB_ATTRIBUTE_HASH:
249                 attr->hash.fn = tdb->hash_fn;
250                 attr->hash.data = tdb->hash_data;
251                 break;
252         case TDB_ATTRIBUTE_SEED:
253                 attr->seed.seed = tdb->hash_seed;
254                 break;
255         case TDB_ATTRIBUTE_OPENHOOK:
256                 return tdb->last_error
257                         = tdb_logerr(tdb, TDB_ERR_EINVAL,
258                                      TDB_LOG_USE_ERROR,
259                                      "tdb_get_attribute:"
260                                      " cannot get TDB_ATTRIBUTE_OPENHOOK");
261         case TDB_ATTRIBUTE_STATS: {
262                 size_t size = attr->stats.size;
263                 if (size > tdb->stats.size)
264                         size = tdb->stats.size;
265                 memcpy(&attr->stats, &tdb->stats, size);
266                 break;
267         }
268         case TDB_ATTRIBUTE_FLOCK:
269                 attr->flock.lock = tdb->lock_fn;
270                 attr->flock.unlock = tdb->unlock_fn;
271                 attr->flock.data = tdb->lock_data;
272                 break;
273         default:
274                 return tdb->last_error
275                         = tdb_logerr(tdb, TDB_ERR_EINVAL,
276                                      TDB_LOG_USE_ERROR,
277                                      "tdb_get_attribute:"
278                                      " unknown attribute type %u",
279                                      attr->base.attr);
280         }
281         attr->base.next = NULL;
282         return TDB_SUCCESS;
283 }
284
285 void tdb_unset_attribute(struct tdb_context *tdb,
286                          enum tdb_attribute_type type)
287 {
288         switch (type) {
289         case TDB_ATTRIBUTE_LOG:
290                 tdb->log_fn = NULL;
291                 break;
292         case TDB_ATTRIBUTE_HASH:
293         case TDB_ATTRIBUTE_SEED:
294         case TDB_ATTRIBUTE_OPENHOOK:
295                 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
296                            "tdb_unset_attribute: cannot unset %s after opening",
297                            type == TDB_ATTRIBUTE_HASH
298                            ? "TDB_ATTRIBUTE_HASH"
299                            : type == TDB_ATTRIBUTE_SEED
300                            ? "TDB_ATTRIBUTE_SEED"
301                            : "TDB_ATTRIBUTE_OPENHOOK");
302                 break;
303         case TDB_ATTRIBUTE_STATS:
304                 tdb_logerr(tdb, TDB_ERR_EINVAL,
305                            TDB_LOG_USE_ERROR,
306                            "tdb_unset_attribute:"
307                            "cannot unset TDB_ATTRIBUTE_STATS");
308                 break;
309         case TDB_ATTRIBUTE_FLOCK:
310                 tdb->lock_fn = tdb_fcntl_lock;
311                 tdb->unlock_fn = tdb_fcntl_unlock;
312                 break;
313         default:
314                 tdb_logerr(tdb, TDB_ERR_EINVAL,
315                            TDB_LOG_USE_ERROR,
316                            "tdb_unset_attribute: unknown attribute type %u",
317                            type);
318         }
319 }
320
321 struct tdb_context *tdb_open(const char *name, int tdb_flags,
322                              int open_flags, mode_t mode,
323                              union tdb_attribute *attr)
324 {
325         struct tdb_context *tdb;
326         struct stat st;
327         int saved_errno = 0;
328         uint64_t hash_test;
329         unsigned v;
330         ssize_t rlen;
331         struct tdb_header hdr;
332         struct tdb_attribute_seed *seed = NULL;
333         struct tdb_attribute_openhook *openhook = NULL;
334         tdb_bool_err berr;
335         enum TDB_ERROR ecode;
336
337         tdb = malloc(sizeof(*tdb) + (name ? strlen(name) + 1 : 0));
338         if (!tdb) {
339                 /* Can't log this */
340                 errno = ENOMEM;
341                 return NULL;
342         }
343         /* Set name immediately for logging functions. */
344         if (name) {
345                 tdb->name = strcpy((char *)(tdb + 1), name);
346         } else {
347                 tdb->name = NULL;
348         }
349         tdb->direct_access = 0;
350         tdb->flags = tdb_flags;
351         tdb->log_fn = NULL;
352         tdb->transaction = NULL;
353         tdb->access = NULL;
354         tdb->last_error = TDB_SUCCESS;
355         tdb->file = NULL;
356         tdb->lock_fn = tdb_fcntl_lock;
357         tdb->unlock_fn = tdb_fcntl_unlock;
358         tdb->hash_fn = jenkins_hash;
359         memset(&tdb->stats, 0, sizeof(tdb->stats));
360         tdb->stats.base.attr = TDB_ATTRIBUTE_STATS;
361         tdb->stats.size = sizeof(tdb->stats);
362         tdb_io_init(tdb);
363
364         while (attr) {
365                 switch (attr->base.attr) {
366                 case TDB_ATTRIBUTE_HASH:
367                         tdb->hash_fn = attr->hash.fn;
368                         tdb->hash_data = attr->hash.data;
369                         break;
370                 case TDB_ATTRIBUTE_SEED:
371                         seed = &attr->seed;
372                         break;
373                 case TDB_ATTRIBUTE_OPENHOOK:
374                         openhook = &attr->openhook;
375                         break;
376                 default:
377                         /* These are set as normal. */
378                         ecode = tdb_set_attribute(tdb, attr);
379                         if (ecode != TDB_SUCCESS)
380                                 goto fail;
381                 }
382                 attr = attr->base.next;
383         }
384
385         if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
386                           | TDB_NOSYNC | TDB_SEQNUM | TDB_ALLOW_NESTING)) {
387                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
388                                    "tdb_open: unknown flags %u", tdb_flags);
389                 goto fail;
390         }
391
392         if ((open_flags & O_ACCMODE) == O_WRONLY) {
393                 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
394                                    "tdb_open: can't open tdb %s write-only",
395                                    name);
396                 goto fail;
397         }
398
399         if ((open_flags & O_ACCMODE) == O_RDONLY) {
400                 tdb->read_only = true;
401                 tdb->mmap_flags = PROT_READ;
402         } else {
403                 tdb->read_only = false;
404                 tdb->mmap_flags = PROT_READ | PROT_WRITE;
405         }
406
407         /* internal databases don't need any of the rest. */
408         if (tdb->flags & TDB_INTERNAL) {
409                 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
410                 ecode = tdb_new_file(tdb);
411                 if (ecode != TDB_SUCCESS) {
412                         goto fail;
413                 }
414                 tdb->file->fd = -1;
415                 ecode = tdb_new_database(tdb, seed, &hdr);
416                 if (ecode != TDB_SUCCESS) {
417                         goto fail;
418                 }
419                 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
420                 tdb->hash_seed = hdr.hash_seed;
421                 tdb_ftable_init(tdb);
422                 return tdb;
423         }
424
425         if (stat(name, &st) != -1)
426                 tdb->file = find_file(st.st_dev, st.st_ino);
427
428         if (!tdb->file) {
429                 int fd;
430
431                 if ((fd = open(name, open_flags, mode)) == -1) {
432                         /* errno set by open(2) */
433                         saved_errno = errno;
434                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
435                                    "tdb_open: could not open file %s: %s",
436                                    name, strerror(errno));
437                         goto fail_errno;
438                 }
439
440                 /* on exec, don't inherit the fd */
441                 v = fcntl(fd, F_GETFD, 0);
442                 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
443
444                 if (fstat(fd, &st) == -1) {
445                         saved_errno = errno;
446                         tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
447                                    "tdb_open: could not stat open %s: %s",
448                                    name, strerror(errno));
449                         goto fail_errno;
450                 }
451
452                 ecode = tdb_new_file(tdb);
453                 if (ecode != TDB_SUCCESS)
454                         goto fail;
455
456                 tdb->file->next = files;
457                 tdb->file->fd = fd;
458                 tdb->file->device = st.st_dev;
459                 tdb->file->inode = st.st_ino;
460                 tdb->file->map_ptr = NULL;
461                 tdb->file->map_size = sizeof(struct tdb_header);
462         }
463
464         /* ensure there is only one process initialising at once */
465         ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
466         if (ecode != TDB_SUCCESS) {
467                 saved_errno = errno;
468                 goto fail_errno;
469         }
470
471         /* call their open hook if they gave us one. */
472         if (openhook) {
473                 ecode = openhook->fn(tdb->file->fd, openhook->data);
474                 if (ecode != TDB_SUCCESS) {
475                         tdb_logerr(tdb, ecode, TDB_LOG_ERROR,
476                                    "tdb_open: open hook failed");
477                         goto fail;
478                 }
479                 open_flags |= O_CREAT;
480         }
481
482         /* If they used O_TRUNC, read will return 0. */
483         rlen = pread(tdb->file->fd, &hdr, sizeof(hdr), 0);
484         if (rlen == 0 && (open_flags & O_CREAT)) {
485                 ecode = tdb_new_database(tdb, seed, &hdr);
486                 if (ecode != TDB_SUCCESS) {
487                         goto fail;
488                 }
489         } else if (rlen < 0) {
490                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
491                                    "tdb_open: error %s reading %s",
492                                    strerror(errno), name);
493                 goto fail;
494         } else if (rlen < sizeof(hdr)
495                    || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
496                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
497                                    "tdb_open: %s is not a tdb file", name);
498                 goto fail;
499         }
500
501         if (hdr.version != TDB_VERSION) {
502                 if (hdr.version == bswap_64(TDB_VERSION))
503                         tdb->flags |= TDB_CONVERT;
504                 else {
505                         /* wrong version */
506                         ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
507                                            "tdb_open:"
508                                            " %s is unknown version 0x%llx",
509                                            name, (long long)hdr.version);
510                         goto fail;
511                 }
512         }
513
514         tdb_convert(tdb, &hdr, sizeof(hdr));
515         tdb->hash_seed = hdr.hash_seed;
516         hash_test = TDB_HASH_MAGIC;
517         hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
518         if (hdr.hash_test != hash_test) {
519                 /* wrong hash variant */
520                 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
521                                    "tdb_open:"
522                                    " %s uses a different hash function",
523                                    name);
524                 goto fail;
525         }
526
527         /* Clear any features we don't understand. */
528         if ((open_flags & O_ACCMODE) != O_RDONLY) {
529                 hdr.features_used &= TDB_FEATURE_MASK;
530                 if (tdb_write_convert(tdb, offsetof(struct tdb_header,
531                                                     features_used),
532                                       &hdr.features_used,
533                                       sizeof(hdr.features_used)) == -1)
534                         goto fail;
535         }
536
537         tdb_unlock_open(tdb);
538
539         /* This make sure we have current map_size and mmap. */
540         tdb->methods->oob(tdb, tdb->file->map_size + 1, true);
541
542         /* Now it's fully formed, recover if necessary. */
543         berr = tdb_needs_recovery(tdb);
544         if (unlikely(berr != false)) {
545                 if (berr < 0) {
546                         ecode = berr;
547                         goto fail;
548                 }
549                 ecode = tdb_lock_and_recover(tdb);
550                 if (ecode != TDB_SUCCESS) {
551                         goto fail;
552                 }
553         }
554
555         ecode = tdb_ftable_init(tdb);
556         if (ecode != TDB_SUCCESS) {
557                 goto fail;
558         }
559
560         /* Add to linked list if we're new. */
561         if (tdb->file->refcnt == 1)
562                 files = tdb->file;
563         return tdb;
564
565  fail:
566         /* Map ecode to some logical errno. */
567         switch (ecode) {
568         case TDB_ERR_CORRUPT:
569         case TDB_ERR_IO:
570                 saved_errno = EIO;
571                 break;
572         case TDB_ERR_LOCK:
573                 saved_errno = EWOULDBLOCK;
574                 break;
575         case TDB_ERR_OOM:
576                 saved_errno = ENOMEM;
577                 break;
578         case TDB_ERR_EINVAL:
579                 saved_errno = EINVAL;
580                 break;
581         default:
582                 saved_errno = EINVAL;
583                 break;
584         }
585
586 fail_errno:
587 #ifdef TDB_TRACE
588         close(tdb->tracefd);
589 #endif
590         if (tdb->file) {
591                 tdb_lock_cleanup(tdb);
592                 if (--tdb->file->refcnt == 0) {
593                         assert(tdb->file->num_lockrecs == 0);
594                         if (tdb->file->map_ptr) {
595                                 if (tdb->flags & TDB_INTERNAL) {
596                                         free(tdb->file->map_ptr);
597                                 } else
598                                         tdb_munmap(tdb->file);
599                         }
600                         if (close(tdb->file->fd) != 0)
601                                 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
602                                            "tdb_open: failed to close tdb fd"
603                                            " on error: %s", strerror(errno));
604                         free(tdb->file->lockrecs);
605                         free(tdb->file);
606                 }
607         }
608
609         free(tdb);
610         errno = saved_errno;
611         return NULL;
612 }
613
614 int tdb_close(struct tdb_context *tdb)
615 {
616         int ret = 0;
617
618         tdb_trace(tdb, "tdb_close");
619
620         if (tdb->transaction) {
621                 tdb_transaction_cancel(tdb);
622         }
623
624         if (tdb->file->map_ptr) {
625                 if (tdb->flags & TDB_INTERNAL)
626                         free(tdb->file->map_ptr);
627                 else
628                         tdb_munmap(tdb->file);
629         }
630         if (tdb->file) {
631                 struct tdb_file **i;
632
633                 tdb_lock_cleanup(tdb);
634                 if (--tdb->file->refcnt == 0) {
635                         ret = close(tdb->file->fd);
636
637                         /* Remove from files list */
638                         for (i = &files; *i; i = &(*i)->next) {
639                                 if (*i == tdb->file) {
640                                         *i = tdb->file->next;
641                                         break;
642                                 }
643                         }
644                         free(tdb->file->lockrecs);
645                         free(tdb->file);
646                 }
647         }
648
649 #ifdef TDB_TRACE
650         close(tdb->tracefd);
651 #endif
652         free(tdb);
653
654         return ret;
655 }