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