2 Trivial Database 2: opening and closing TDBs
3 Copyright (C) Rusty Russell 2010
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.
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.
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/>.
21 /* all tdbs, to detect double-opens (fcntl file don't nest!) */
22 static struct tdb_context *tdbs = NULL;
24 static struct tdb_file *find_file(dev_t device, ino_t ino)
26 struct tdb_context *i;
28 for (i = tdbs; i; i = i->next) {
29 if (i->file->device == device && i->file->inode == ino) {
37 static bool read_all(int fd, void *buf, size_t len)
41 ret = read(fd, buf, len);
49 buf = (char *)buf + ret;
55 static uint64_t random_number(struct tdb_context *tdb)
61 fd = open("/dev/urandom", O_RDONLY);
63 if (read_all(fd, &ret, sizeof(ret))) {
69 /* FIXME: Untested! Based on Wikipedia protocol description! */
70 fd = open("/dev/egd-pool", O_RDWR);
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));
78 /* Copy at least some bytes. */
79 memcpy(&ret, reply+1, r - 1);
80 if (reply[0] == sizeof(uint64_t)
81 && r == sizeof(reply)) {
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");
99 struct tdb_header hdr;
100 struct tdb_freetable ftable;
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)
108 /* We make it up in memory, then write it out if not internal */
109 struct new_database newdb;
110 unsigned int magic_len;
112 enum TDB_ERROR ecode;
114 /* Fill in the header */
115 newdb.hdr.version = TDB_VERSION;
117 newdb.hdr.hash_seed = seed->seed;
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),
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));
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),
139 if (ecode != TDB_SUCCESS) {
144 memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
145 strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
147 /* This creates an endian-converted database, as if read from disk */
148 magic_len = sizeof(newdb.hdr.magic_food);
150 (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
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,
160 " failed to allocate");
162 memcpy(tdb->file->map_ptr, &newdb, tdb->file->map_size);
165 if (lseek(tdb->file->fd, 0, SEEK_SET) == -1) {
166 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
168 " failed to seek: %s", strerror(errno));
171 if (ftruncate(tdb->file->fd, 0) == -1) {
172 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
174 " failed to truncate: %s", strerror(errno));
177 rlen = write(tdb->file->fd, &newdb, sizeof(newdb));
178 if (rlen != sizeof(newdb)) {
181 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
182 "tdb_new_database: %zi writing header: %s",
183 rlen, strerror(errno));
188 static enum TDB_ERROR tdb_new_file(struct tdb_context *tdb)
190 tdb->file = malloc(sizeof(*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;
201 enum TDB_ERROR tdb_set_attribute(struct tdb_context *tdb,
202 const union tdb_attribute *attr)
204 switch (attr->base.attr) {
205 case TDB_ATTRIBUTE_LOG:
206 tdb->log_fn = attr->log.fn;
207 tdb->log_data = attr->log.data;
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,
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,
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;
237 return tdb->last_error
238 = tdb_logerr(tdb, TDB_ERR_EINVAL,
241 " unknown attribute type %u",
247 enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb,
248 union tdb_attribute *attr)
250 switch (attr->base.attr) {
251 case TDB_ATTRIBUTE_LOG:
253 return tdb->last_error = TDB_ERR_NOEXIST;
254 attr->log.fn = tdb->log_fn;
255 attr->log.data = tdb->log_data;
257 case TDB_ATTRIBUTE_HASH:
258 attr->hash.fn = tdb->hash_fn;
259 attr->hash.data = tdb->hash_data;
261 case TDB_ATTRIBUTE_SEED:
262 attr->seed.seed = tdb->hash_seed;
264 case TDB_ATTRIBUTE_OPENHOOK:
266 return tdb->last_error = TDB_ERR_NOEXIST;
267 attr->openhook.fn = tdb->openhook;
268 attr->openhook.data = tdb->openhook_data;
270 case TDB_ATTRIBUTE_STATS: {
271 size_t size = attr->stats.size;
272 if (size > tdb->stats.size)
273 size = tdb->stats.size;
274 memcpy(&attr->stats, &tdb->stats, size);
277 case TDB_ATTRIBUTE_FLOCK:
278 attr->flock.lock = tdb->lock_fn;
279 attr->flock.unlock = tdb->unlock_fn;
280 attr->flock.data = tdb->lock_data;
282 case TDB_ATTRIBUTE_TDB1_HASHSIZE:
283 if (!(tdb->flags & TDB_VERSION1))
284 return tdb->last_error
285 = tdb_logerr(tdb, TDB_ERR_EINVAL,
288 " cannot get TDB_ATTRIBUTE_TDB1_HASHSIZE"
290 attr->tdb1_hashsize.hsize = tdb->tdb1.header.hash_size;
293 return tdb->last_error
294 = tdb_logerr(tdb, TDB_ERR_EINVAL,
297 " unknown attribute type %u",
300 attr->base.next = NULL;
304 void tdb_unset_attribute(struct tdb_context *tdb,
305 enum tdb_attribute_type type)
308 case TDB_ATTRIBUTE_LOG:
311 case TDB_ATTRIBUTE_OPENHOOK:
312 tdb->openhook = NULL;
314 case TDB_ATTRIBUTE_HASH:
315 case TDB_ATTRIBUTE_SEED:
316 case TDB_ATTRIBUTE_TDB1_HASHSIZE:
317 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
318 "tdb_unset_attribute: cannot unset %s after opening",
319 type == TDB_ATTRIBUTE_HASH
320 ? "TDB_ATTRIBUTE_HASH"
321 : type == TDB_ATTRIBUTE_SEED
322 ? "TDB_ATTRIBUTE_SEED"
323 : "TDB_ATTRIBUTE_TDB1_HASHSIZE");
325 case TDB_ATTRIBUTE_STATS:
326 tdb_logerr(tdb, TDB_ERR_EINVAL,
328 "tdb_unset_attribute:"
329 "cannot unset TDB_ATTRIBUTE_STATS");
331 case TDB_ATTRIBUTE_FLOCK:
332 tdb->lock_fn = tdb_fcntl_lock;
333 tdb->unlock_fn = tdb_fcntl_unlock;
336 tdb_logerr(tdb, TDB_ERR_EINVAL,
338 "tdb_unset_attribute: unknown attribute type %u",
343 struct tdb_context *tdb_open(const char *name, int tdb_flags,
344 int open_flags, mode_t mode,
345 union tdb_attribute *attr)
347 struct tdb_context *tdb;
353 struct tdb_header hdr;
354 struct tdb_attribute_seed *seed = NULL;
355 struct tdb_attribute_tdb1_hashsize *hsize_attr = NULL;
357 enum TDB_ERROR ecode;
360 tdb = malloc(sizeof(*tdb) + (name ? strlen(name) + 1 : 0));
366 /* Set name immediately for logging functions. */
368 tdb->name = strcpy((char *)(tdb + 1), name);
372 tdb->flags = tdb_flags;
374 tdb->open_flags = open_flags;
375 tdb->last_error = TDB_SUCCESS;
377 tdb->openhook = NULL;
378 tdb->lock_fn = tdb_fcntl_lock;
379 tdb->unlock_fn = tdb_fcntl_unlock;
380 tdb->hash_fn = tdb_jenkins_hash;
381 memset(&tdb->stats, 0, sizeof(tdb->stats));
382 tdb->stats.base.attr = TDB_ATTRIBUTE_STATS;
383 tdb->stats.size = sizeof(tdb->stats);
385 tdb->tdb2.direct_access = 0;
386 tdb->tdb2.transaction = NULL;
387 tdb->tdb2.access = NULL;
390 switch (attr->base.attr) {
391 case TDB_ATTRIBUTE_HASH:
392 tdb->hash_fn = attr->hash.fn;
393 tdb->hash_data = attr->hash.data;
395 case TDB_ATTRIBUTE_SEED:
398 case TDB_ATTRIBUTE_OPENHOOK:
399 tdb->openhook = attr->openhook.fn;
400 tdb->openhook_data = attr->openhook.data;
402 case TDB_ATTRIBUTE_TDB1_HASHSIZE:
403 hsize_attr = &attr->tdb1_hashsize;
406 /* These are set as normal. */
407 ecode = tdb_set_attribute(tdb, attr);
408 if (ecode != TDB_SUCCESS)
411 attr = attr->base.next;
414 if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
415 | TDB_NOSYNC | TDB_SEQNUM | TDB_ALLOW_NESTING
417 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
418 "tdb_open: unknown flags %u", tdb_flags);
423 if (!(tdb_flags & TDB_VERSION1) ||
424 (!(tdb_flags & TDB_INTERNAL) && !(open_flags & O_CREAT))) {
425 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
427 "tdb_open: can only use"
428 " TDB_ATTRIBUTE_TDB1_HASHSIZE when"
429 " creating a TDB_VERSION1 tdb");
434 if ((open_flags & O_ACCMODE) == O_WRONLY) {
435 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
436 "tdb_open: can't open tdb %s write-only",
441 if ((open_flags & O_ACCMODE) == O_RDONLY) {
443 tdb->flags |= TDB_RDONLY;
445 if (tdb_flags & TDB_RDONLY) {
446 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
448 "tdb_open: can't use TDB_RDONLY"
449 " without O_RDONLY");
455 /* internal databases don't need any of the rest. */
456 if (tdb->flags & TDB_INTERNAL) {
457 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
458 ecode = tdb_new_file(tdb);
459 if (ecode != TDB_SUCCESS) {
463 ecode = tdb_new_database(tdb, seed, &hdr);
464 if (ecode != TDB_SUCCESS) {
467 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
468 tdb->hash_seed = hdr.hash_seed;
469 tdb_ftable_init(tdb);
473 if (stat(name, &st) != -1)
474 tdb->file = find_file(st.st_dev, st.st_ino);
479 if ((fd = open(name, open_flags, mode)) == -1) {
480 /* errno set by open(2) */
482 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
483 "tdb_open: could not open file %s: %s",
484 name, strerror(errno));
488 /* on exec, don't inherit the fd */
489 v = fcntl(fd, F_GETFD, 0);
490 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
492 if (fstat(fd, &st) == -1) {
494 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
495 "tdb_open: could not stat open %s: %s",
496 name, strerror(errno));
501 ecode = tdb_new_file(tdb);
502 if (ecode != TDB_SUCCESS) {
508 tdb->file->device = st.st_dev;
509 tdb->file->inode = st.st_ino;
510 tdb->file->map_ptr = NULL;
511 tdb->file->map_size = sizeof(struct tdb_header);
514 /* ensure there is only one process initialising at once */
515 ecode = tdb_lock_open(tdb, openlock, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
516 if (ecode != TDB_SUCCESS) {
521 /* call their open hook if they gave us one. */
523 ecode = tdb->openhook(tdb->file->fd, tdb->openhook_data);
524 if (ecode != TDB_SUCCESS) {
525 tdb_logerr(tdb, ecode, TDB_LOG_ERROR,
526 "tdb_open: open hook failed");
529 open_flags |= O_CREAT;
532 /* If they used O_TRUNC, read will return 0. */
533 rlen = pread(tdb->file->fd, &hdr, sizeof(hdr), 0);
534 if (rlen == 0 && (open_flags & O_CREAT)) {
535 ecode = tdb_new_database(tdb, seed, &hdr);
536 if (ecode != TDB_SUCCESS) {
539 } else if (rlen < 0) {
540 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
541 "tdb_open: error %s reading %s",
542 strerror(errno), name);
544 } else if (rlen < sizeof(hdr)
545 || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
546 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
547 "tdb_open: %s is not a tdb file", name);
551 if (hdr.version != TDB_VERSION) {
552 if (hdr.version == bswap_64(TDB_VERSION))
553 tdb->flags |= TDB_CONVERT;
556 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
558 " %s is unknown version 0x%llx",
559 name, (long long)hdr.version);
562 } else if (tdb->flags & TDB_CONVERT) {
563 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
565 " %s does not need TDB_CONVERT",
570 tdb_convert(tdb, &hdr, sizeof(hdr));
571 tdb->hash_seed = hdr.hash_seed;
572 hash_test = TDB_HASH_MAGIC;
573 hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
574 if (hdr.hash_test != hash_test) {
575 /* wrong hash variant */
576 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
578 " %s uses a different hash function",
583 /* Clear any features we don't understand. */
584 if ((open_flags & O_ACCMODE) != O_RDONLY) {
585 hdr.features_used &= TDB_FEATURE_MASK;
586 ecode = tdb_write_convert(tdb, offsetof(struct tdb_header,
589 sizeof(hdr.features_used));
590 if (ecode != TDB_SUCCESS)
594 tdb_unlock_open(tdb, openlock);
596 /* This make sure we have current map_size and mmap. */
597 ecode = tdb->tdb2.io->oob(tdb, tdb->file->map_size + 1, true);
598 if (unlikely(ecode != TDB_SUCCESS))
601 /* Now it's fully formed, recover if necessary. */
602 berr = tdb_needs_recovery(tdb);
603 if (unlikely(berr != false)) {
608 ecode = tdb_lock_and_recover(tdb);
609 if (ecode != TDB_SUCCESS) {
614 ecode = tdb_ftable_init(tdb);
615 if (ecode != TDB_SUCCESS) {
624 /* Map ecode to some logical errno. */
626 case TDB_ERR_CORRUPT:
631 saved_errno = EWOULDBLOCK;
634 saved_errno = ENOMEM;
637 saved_errno = EINVAL;
640 saved_errno = EINVAL;
649 tdb_lock_cleanup(tdb);
650 if (--tdb->file->refcnt == 0) {
651 assert(tdb->file->num_lockrecs == 0);
652 if (tdb->file->map_ptr) {
653 if (tdb->flags & TDB_INTERNAL) {
654 free(tdb->file->map_ptr);
656 tdb_munmap(tdb->file);
658 if (close(tdb->file->fd) != 0)
659 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
660 "tdb_open: failed to close tdb fd"
661 " on error: %s", strerror(errno));
662 free(tdb->file->lockrecs);
672 int tdb_close(struct tdb_context *tdb)
675 struct tdb_context **i;
677 tdb_trace(tdb, "tdb_close");
679 if (tdb->tdb2.transaction) {
680 tdb_transaction_cancel(tdb);
683 if (tdb->file->map_ptr) {
684 if (tdb->flags & TDB_INTERNAL)
685 free(tdb->file->map_ptr);
687 tdb_munmap(tdb->file);
690 tdb_lock_cleanup(tdb);
691 if (--tdb->file->refcnt == 0) {
692 ret = close(tdb->file->fd);
693 free(tdb->file->lockrecs);
698 /* Remove from tdbs list */
699 for (i = &tdbs; *i; i = &(*i)->next) {
714 void tdb_foreach_(int (*fn)(struct tdb_context *, void *), void *p)
716 struct tdb_context *i;
718 for (i = tdbs; i; i = i->next) {