2 #include <ccan/asprintf/asprintf.h>
3 #include <ccan/tdb2/tdb2.h>
8 struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
10 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
11 static struct tdb_context *tdbs = NULL;
13 static bool tdb_already_open(dev_t device, ino_t ino)
15 struct tdb_context *i;
17 for (i = tdbs; i; i = i->next) {
18 if (i->device == device && i->inode == ino) {
26 static bool read_all(int fd, void *buf, size_t len)
30 ret = read(fd, buf, len);
38 buf = (char *)buf + ret;
44 static uint64_t random_number(struct tdb_context *tdb)
50 fd = open("/dev/urandom", O_RDONLY);
52 if (read_all(fd, &ret, sizeof(ret))) {
58 /* FIXME: Untested! Based on Wikipedia protocol description! */
59 fd = open("/dev/egd-pool", O_RDWR);
61 /* Command is 1, next byte is size we want to read. */
62 char cmd[2] = { 1, sizeof(uint64_t) };
63 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
64 char reply[1 + sizeof(uint64_t)];
65 int r = read(fd, reply, sizeof(reply));
67 /* Copy at least some bytes. */
68 memcpy(&ret, reply+1, r - 1);
69 if (reply[0] == sizeof(uint64_t)
70 && r == sizeof(reply)) {
79 /* Fallback: pid and time. */
80 gettimeofday(&now, NULL);
81 ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
82 tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
83 "tdb_open: random from getpid and time");
88 struct tdb_header hdr;
89 struct tdb_freetable ftable;
92 /* initialise a new database */
93 static enum TDB_ERROR tdb_new_database(struct tdb_context *tdb,
94 struct tdb_attribute_seed *seed,
95 struct tdb_header *hdr)
97 /* We make it up in memory, then write it out if not internal */
98 struct new_database newdb;
99 unsigned int magic_len;
101 enum TDB_ERROR ecode;
103 /* Fill in the header */
104 newdb.hdr.version = TDB_VERSION;
106 newdb.hdr.hash_seed = seed->seed;
108 newdb.hdr.hash_seed = random_number(tdb);
109 newdb.hdr.hash_test = TDB_HASH_MAGIC;
110 newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
111 sizeof(newdb.hdr.hash_test),
114 newdb.hdr.recovery = 0;
115 newdb.hdr.features_used = newdb.hdr.features_offered = TDB_FEATURE_MASK;
116 memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
117 /* Initial hashes are empty. */
118 memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
121 newdb.hdr.free_table = offsetof(struct new_database, ftable);
122 memset(&newdb.ftable, 0, sizeof(newdb.ftable));
123 ecode = set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
124 sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
125 sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
127 if (ecode != TDB_SUCCESS) {
132 memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
133 strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
135 /* This creates an endian-converted database, as if read from disk */
136 magic_len = sizeof(newdb.hdr.magic_food);
138 (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
142 if (tdb->flags & TDB_INTERNAL) {
143 tdb->map_size = sizeof(newdb);
144 tdb->map_ptr = malloc(tdb->map_size);
146 return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
148 " failed to allocate");
150 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
153 if (lseek(tdb->fd, 0, SEEK_SET) == -1) {
154 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
156 " failed to seek: %s", strerror(errno));
159 if (ftruncate(tdb->fd, 0) == -1) {
160 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
162 " failed to truncate: %s", strerror(errno));
165 rlen = write(tdb->fd, &newdb, sizeof(newdb));
166 if (rlen != sizeof(newdb)) {
169 return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
170 "tdb_new_database: %zi writing header: %s",
171 rlen, strerror(errno));
176 struct tdb_context *tdb_open(const char *name, int tdb_flags,
177 int open_flags, mode_t mode,
178 union tdb_attribute *attr)
180 struct tdb_context *tdb;
186 struct tdb_header hdr;
187 struct tdb_attribute_seed *seed = NULL;
189 enum TDB_ERROR ecode;
191 tdb = malloc(sizeof(*tdb));
199 tdb->direct_access = 0;
201 tdb->map_size = sizeof(struct tdb_header);
202 tdb->flags = tdb_flags;
204 tdb->transaction = NULL;
212 switch (attr->base.attr) {
213 case TDB_ATTRIBUTE_LOG:
214 tdb->logfn = attr->log.log_fn;
215 tdb->log_private = attr->log.log_private;
217 case TDB_ATTRIBUTE_HASH:
218 tdb->khash = attr->hash.hash_fn;
219 tdb->hash_priv = attr->hash.hash_private;
221 case TDB_ATTRIBUTE_SEED:
224 case TDB_ATTRIBUTE_STATS:
225 tdb->stats = &attr->stats;
226 /* They have stats we don't know about? Tell them. */
227 if (tdb->stats->size > sizeof(attr->stats))
228 tdb->stats->size = sizeof(attr->stats);
231 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL,
234 " unknown attribute type %u",
238 attr = attr->base.next;
241 if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
243 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
244 "tdb_open: unknown flags %u", tdb_flags);
248 if ((open_flags & O_ACCMODE) == O_WRONLY) {
249 ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
250 "tdb_open: can't open tdb %s write-only",
255 if ((open_flags & O_ACCMODE) == O_RDONLY) {
256 tdb->read_only = true;
257 tdb->mmap_flags = PROT_READ;
259 tdb->read_only = false;
260 tdb->mmap_flags = PROT_READ | PROT_WRITE;
263 /* internal databases don't need any of the rest. */
264 if (tdb->flags & TDB_INTERNAL) {
265 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
266 ecode = tdb_new_database(tdb, seed, &hdr);
267 if (ecode != TDB_SUCCESS) {
270 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
271 tdb->hash_seed = hdr.hash_seed;
272 tdb_ftable_init(tdb);
276 if ((tdb->fd = open(name, open_flags, mode)) == -1) {
277 /* errno set by open(2) */
279 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
280 "tdb_open: could not open file %s: %s",
281 name, strerror(errno));
285 /* on exec, don't inherit the fd */
286 v = fcntl(tdb->fd, F_GETFD, 0);
287 fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
289 /* ensure there is only one process initialising at once */
290 ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
291 if (ecode != TDB_SUCCESS) {
295 /* If they used O_TRUNC, read will return 0. */
296 rlen = read(tdb->fd, &hdr, sizeof(hdr));
297 if (rlen == 0 && (open_flags & O_CREAT)) {
298 ecode = tdb_new_database(tdb, seed, &hdr);
299 if (ecode != TDB_SUCCESS) {
302 } else if (rlen < 0) {
303 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
304 "tdb_open: error %s reading %s",
305 strerror(errno), name);
307 } else if (rlen < sizeof(hdr)
308 || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
309 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
310 "tdb_open: %s is not a tdb file", name);
314 if (hdr.version != TDB_VERSION) {
315 if (hdr.version == bswap_64(TDB_VERSION))
316 tdb->flags |= TDB_CONVERT;
319 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
321 " %s is unknown version 0x%llx",
322 name, (long long)hdr.version);
327 tdb_convert(tdb, &hdr, sizeof(hdr));
328 tdb->hash_seed = hdr.hash_seed;
329 hash_test = TDB_HASH_MAGIC;
330 hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
331 if (hdr.hash_test != hash_test) {
332 /* wrong hash variant */
333 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
335 " %s uses a different hash function",
340 if (fstat(tdb->fd, &st) == -1) {
342 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
343 "tdb_open: could not stat open %s: %s",
344 name, strerror(errno));
348 /* Is it already in the open list? If so, fail. */
349 if (tdb_already_open(st.st_dev, st.st_ino)) {
351 ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_ERROR,
352 "tdb_open: %s (%d,%d) is already open"
354 name, (int)st.st_dev, (int)st.st_ino);
358 tdb->name = strdup(name);
360 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
361 "tdb_open: failed to allocate name");
365 /* Clear any features we don't understand. */
366 if ((open_flags & O_ACCMODE) != O_RDONLY) {
367 hdr.features_used &= TDB_FEATURE_MASK;
368 if (tdb_write_convert(tdb, offsetof(struct tdb_header,
371 sizeof(hdr.features_used)) == -1)
375 tdb->device = st.st_dev;
376 tdb->inode = st.st_ino;
377 tdb_unlock_open(tdb);
379 /* This make sure we have current map_size and mmap. */
380 tdb->methods->oob(tdb, tdb->map_size + 1, true);
382 /* Now it's fully formed, recover if necessary. */
383 berr = tdb_needs_recovery(tdb);
384 if (unlikely(berr != false)) {
389 ecode = tdb_lock_and_recover(tdb);
390 if (ecode != TDB_SUCCESS) {
395 ecode = tdb_ftable_init(tdb);
396 if (ecode != TDB_SUCCESS) {
405 /* Map ecode to some logical errno. */
408 case TDB_ERR_CORRUPT:
413 saved_errno = EWOULDBLOCK;
416 saved_errno = ENOMEM;
419 saved_errno = EINVAL;
422 saved_errno = EINVAL;
431 if (tdb->flags & TDB_INTERNAL) {
437 free((char *)tdb->name);
439 if (close(tdb->fd) != 0)
440 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
441 "tdb_open: failed to close tdb->fd"
442 " on error: %s", strerror(errno));
448 static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb,
452 struct tdb_used_record *rec,
455 uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
456 enum TDB_ERROR ecode;
458 ecode = set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
459 keylen + dataroom, h);
460 if (ecode == TDB_SUCCESS) {
461 ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
466 static enum TDB_ERROR replace_data(struct tdb_context *tdb,
468 struct tdb_data key, struct tdb_data dbuf,
469 tdb_off_t old_off, tdb_len_t old_room,
473 enum TDB_ERROR ecode;
475 /* Allocate a new record. */
476 new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
478 if (TDB_OFF_IS_ERR(new_off)) {
482 /* We didn't like the existing one: remove it. */
484 add_stat(tdb, frees, 1);
485 ecode = add_free_record(tdb, old_off,
486 sizeof(struct tdb_used_record)
487 + key.dsize + old_room);
488 if (ecode == TDB_SUCCESS)
489 ecode = replace_in_hash(tdb, h, new_off);
491 ecode = add_to_hash(tdb, h, new_off);
493 if (ecode != TDB_SUCCESS) {
497 new_off += sizeof(struct tdb_used_record);
498 ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
499 if (ecode != TDB_SUCCESS) {
503 new_off += key.dsize;
504 ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
505 if (ecode != TDB_SUCCESS) {
509 /* FIXME: tdb_increment_seqnum(tdb); */
513 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
514 struct tdb_data key, struct tdb_data dbuf, int flag)
518 tdb_len_t old_room = 0;
519 struct tdb_used_record rec;
520 enum TDB_ERROR ecode;
522 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
523 if (TDB_OFF_IS_ERR(off)) {
527 /* Now we have lock on this hash bucket. */
528 if (flag == TDB_INSERT) {
530 ecode = TDB_ERR_EXISTS;
535 old_room = rec_data_length(&rec)
536 + rec_extra_padding(&rec);
537 if (old_room >= dbuf.dsize) {
538 /* Can modify in-place. Easy! */
539 ecode = update_rec_hdr(tdb, off,
540 key.dsize, dbuf.dsize,
542 if (ecode != TDB_SUCCESS) {
545 ecode = tdb->methods->twrite(tdb,
550 if (ecode != TDB_SUCCESS) {
553 tdb_unlock_hashes(tdb, h.hlock_start,
554 h.hlock_range, F_WRLCK);
558 if (flag == TDB_MODIFY) {
559 /* if the record doesn't exist and we
560 are in TDB_MODIFY mode then we should fail
562 ecode = TDB_ERR_NOEXIST;
568 /* If we didn't use the old record, this implies we're growing. */
569 ecode = replace_data(tdb, &h, key, dbuf, off, old_room, off);
571 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
575 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
576 struct tdb_data key, struct tdb_data dbuf)
580 struct tdb_used_record rec;
581 tdb_len_t old_room = 0, old_dlen;
582 unsigned char *newdata;
583 struct tdb_data new_dbuf;
584 enum TDB_ERROR ecode;
586 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
587 if (TDB_OFF_IS_ERR(off)) {
592 old_dlen = rec_data_length(&rec);
593 old_room = old_dlen + rec_extra_padding(&rec);
595 /* Fast path: can append in place. */
596 if (rec_extra_padding(&rec) >= dbuf.dsize) {
597 ecode = update_rec_hdr(tdb, off, key.dsize,
598 old_dlen + dbuf.dsize, &rec,
600 if (ecode != TDB_SUCCESS) {
604 off += sizeof(rec) + key.dsize + old_dlen;
605 ecode = tdb->methods->twrite(tdb, off, dbuf.dptr,
611 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
613 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
615 " failed to allocate %zu bytes",
616 (size_t)(key.dsize + old_dlen
620 ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
622 if (ecode != TDB_SUCCESS) {
623 goto out_free_newdata;
625 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
626 new_dbuf.dptr = newdata;
627 new_dbuf.dsize = old_dlen + dbuf.dsize;
633 /* If they're using tdb_append(), it implies they're growing record. */
634 ecode = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
639 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
643 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
644 struct tdb_data *data)
647 struct tdb_used_record rec;
649 enum TDB_ERROR ecode;
651 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
652 if (TDB_OFF_IS_ERR(off)) {
657 ecode = TDB_ERR_NOEXIST;
659 data->dsize = rec_data_length(&rec);
660 data->dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
662 if (TDB_PTR_IS_ERR(data->dptr)) {
663 ecode = TDB_PTR_ERR(data->dptr);
668 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
672 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
675 struct tdb_used_record rec;
677 enum TDB_ERROR ecode;
679 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
680 if (TDB_OFF_IS_ERR(off)) {
685 ecode = TDB_ERR_NOEXIST;
689 ecode = delete_from_hash(tdb, &h);
690 if (ecode != TDB_SUCCESS) {
694 /* Free the deleted entry. */
695 add_stat(tdb, frees, 1);
696 ecode = add_free_record(tdb, off,
697 sizeof(struct tdb_used_record)
698 + rec_key_length(&rec)
699 + rec_data_length(&rec)
700 + rec_extra_padding(&rec));
703 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
707 int tdb_close(struct tdb_context *tdb)
709 struct tdb_context **i;
712 tdb_trace(tdb, "tdb_close");
714 if (tdb->transaction) {
715 tdb_transaction_cancel(tdb);
719 if (tdb->flags & TDB_INTERNAL)
724 free((char *)tdb->name);
726 ret = close(tdb->fd);
731 /* Remove from contexts list */
732 for (i = &tdbs; *i; i = &(*i)->next) {
747 const char *tdb_errorstr(enum TDB_ERROR ecode)
749 /* Gcc warns if you miss a case in the switch, so use that. */
751 case TDB_SUCCESS: return "Success";
752 case TDB_ERR_CORRUPT: return "Corrupt database";
753 case TDB_ERR_IO: return "IO Error";
754 case TDB_ERR_LOCK: return "Locking error";
755 case TDB_ERR_OOM: return "Out of memory";
756 case TDB_ERR_EXISTS: return "Record exists";
757 case TDB_ERR_EINVAL: return "Invalid parameter";
758 case TDB_ERR_NOEXIST: return "Record does not exist";
759 case TDB_ERR_RDONLY: return "write not permitted";
761 return "Invalid error code";
764 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
765 enum TDB_ERROR ecode,
766 enum tdb_log_level level,
767 const char *fmt, ...)
772 /* tdb_open paths care about errno, so save it. */
773 int saved_errno = errno;
779 len = vasprintf(&message, fmt, ap);
783 tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
784 "out of memory formatting message:");
785 tdb->logfn(tdb, level, tdb->log_private, fmt);
787 tdb->logfn(tdb, level, tdb->log_private, message);