2 #include <ccan/tdb2/tdb2.h>
7 struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
9 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
10 static struct tdb_context *tdbs = NULL;
12 static bool tdb_already_open(dev_t device, ino_t ino)
14 struct tdb_context *i;
16 for (i = tdbs; i; i = i->next) {
17 if (i->device == device && i->inode == ino) {
25 static bool read_all(int fd, void *buf, size_t len)
29 ret = read(fd, buf, len);
37 buf = (char *)buf + ret;
43 static uint64_t random_number(struct tdb_context *tdb)
49 fd = open("/dev/urandom", O_RDONLY);
51 if (read_all(fd, &ret, sizeof(ret))) {
57 /* FIXME: Untested! Based on Wikipedia protocol description! */
58 fd = open("/dev/egd-pool", O_RDWR);
60 /* Command is 1, next byte is size we want to read. */
61 char cmd[2] = { 1, sizeof(uint64_t) };
62 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
63 char reply[1 + sizeof(uint64_t)];
64 int r = read(fd, reply, sizeof(reply));
66 /* Copy at least some bytes. */
67 memcpy(&ret, reply+1, r - 1);
68 if (reply[0] == sizeof(uint64_t)
69 && r == sizeof(reply)) {
78 /* Fallback: pid and time. */
79 gettimeofday(&now, NULL);
80 ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
81 tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
82 "tdb_open: random from getpid and time");
87 struct tdb_header hdr;
88 struct tdb_freetable ftable;
91 /* initialise a new database */
92 static int tdb_new_database(struct tdb_context *tdb,
93 struct tdb_attribute_seed *seed,
94 struct tdb_header *hdr)
96 /* We make it up in memory, then write it out if not internal */
97 struct new_database newdb;
98 unsigned int magic_len;
101 /* Fill in the header */
102 newdb.hdr.version = TDB_VERSION;
104 newdb.hdr.hash_seed = seed->seed;
106 newdb.hdr.hash_seed = random_number(tdb);
107 newdb.hdr.hash_test = TDB_HASH_MAGIC;
108 newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
109 sizeof(newdb.hdr.hash_test),
112 newdb.hdr.recovery = 0;
113 memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
114 /* Initial hashes are empty. */
115 memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
118 newdb.hdr.free_table = offsetof(struct new_database, ftable);
119 memset(&newdb.ftable, 0, sizeof(newdb.ftable));
120 tdb->ecode = set_header(NULL, &newdb.ftable.hdr, TDB_FTABLE_MAGIC, 0,
121 sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
122 sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
124 if (tdb->ecode != TDB_SUCCESS) {
129 memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
130 strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
132 /* This creates an endian-converted database, as if read from disk */
133 magic_len = sizeof(newdb.hdr.magic_food);
135 (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
139 if (tdb->flags & TDB_INTERNAL) {
140 tdb->map_size = sizeof(newdb);
141 tdb->map_ptr = malloc(tdb->map_size);
143 tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
144 "tdb_new_database: failed to allocate");
147 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
150 if (lseek(tdb->fd, 0, SEEK_SET) == -1)
153 if (ftruncate(tdb->fd, 0) == -1)
156 rlen = write(tdb->fd, &newdb, sizeof(newdb));
157 if (rlen != sizeof(newdb)) {
160 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
161 "tdb_new_database: %zi writing header: %s",
162 rlen, strerror(errno));
168 struct tdb_context *tdb_open(const char *name, int tdb_flags,
169 int open_flags, mode_t mode,
170 union tdb_attribute *attr)
172 struct tdb_context *tdb;
178 struct tdb_header hdr;
179 struct tdb_attribute_seed *seed = NULL;
180 enum TDB_ERROR ecode;
182 tdb = malloc(sizeof(*tdb));
190 tdb->direct_access = 0;
192 tdb->map_size = sizeof(struct tdb_header);
193 tdb->ecode = TDB_SUCCESS;
194 tdb->flags = tdb_flags;
196 tdb->transaction = NULL;
204 switch (attr->base.attr) {
205 case TDB_ATTRIBUTE_LOG:
206 tdb->logfn = attr->log.log_fn;
207 tdb->log_private = attr->log.log_private;
209 case TDB_ATTRIBUTE_HASH:
210 tdb->khash = attr->hash.hash_fn;
211 tdb->hash_priv = attr->hash.hash_private;
213 case TDB_ATTRIBUTE_SEED:
216 case TDB_ATTRIBUTE_STATS:
217 tdb->stats = &attr->stats;
218 /* They have stats we don't know about? Tell them. */
219 if (tdb->stats->size > sizeof(attr->stats))
220 tdb->stats->size = sizeof(attr->stats);
223 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
224 "tdb_open: unknown attribute type %u",
228 attr = attr->base.next;
231 if ((open_flags & O_ACCMODE) == O_WRONLY) {
232 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
233 "tdb_open: can't open tdb %s write-only", name);
237 if ((open_flags & O_ACCMODE) == O_RDONLY) {
238 tdb->read_only = true;
239 /* read only databases don't do locking */
240 tdb->flags |= TDB_NOLOCK;
241 tdb->mmap_flags = PROT_READ;
243 tdb->read_only = false;
244 tdb->mmap_flags = PROT_READ | PROT_WRITE;
247 /* internal databases don't need any of the rest. */
248 if (tdb->flags & TDB_INTERNAL) {
249 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
250 if (tdb_new_database(tdb, seed, &hdr) != 0) {
253 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
254 tdb->hash_seed = hdr.hash_seed;
255 tdb_ftable_init(tdb);
259 if ((tdb->fd = open(name, open_flags, mode)) == -1) {
260 /* errno set by open(2) */
262 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
263 "tdb_open: could not open file %s: %s",
264 name, strerror(errno));
268 /* on exec, don't inherit the fd */
269 v = fcntl(tdb->fd, F_GETFD, 0);
270 fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
272 /* ensure there is only one process initialising at once */
273 tdb->ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
274 if (tdb->ecode != TDB_SUCCESS) {
278 /* If they used O_TRUNC, read will return 0. */
279 rlen = read(tdb->fd, &hdr, sizeof(hdr));
280 if (rlen == 0 && (open_flags & O_CREAT)) {
281 if (tdb_new_database(tdb, seed, &hdr) == -1) {
284 } else if (rlen < 0) {
285 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
286 "tdb_open: error %s reading %s",
287 strerror(errno), name);
289 } else if (rlen < sizeof(hdr)
290 || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
291 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
292 "tdb_open: %s is not a tdb file", name);
296 if (hdr.version != TDB_VERSION) {
297 if (hdr.version == bswap_64(TDB_VERSION))
298 tdb->flags |= TDB_CONVERT;
301 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
302 "tdb_open: %s is unknown version 0x%llx",
303 name, (long long)hdr.version);
308 tdb_convert(tdb, &hdr, sizeof(hdr));
309 tdb->hash_seed = hdr.hash_seed;
310 hash_test = TDB_HASH_MAGIC;
311 hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
312 if (hdr.hash_test != hash_test) {
313 /* wrong hash variant */
314 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
315 "tdb_open: %s uses a different hash function",
320 if (fstat(tdb->fd, &st) == -1) {
322 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
323 "tdb_open: could not stat open %s: %s",
324 name, strerror(errno));
328 /* Is it already in the open list? If so, fail. */
329 if (tdb_already_open(st.st_dev, st.st_ino)) {
331 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_ERROR,
332 "tdb_open: %s (%d,%d) is already open in this"
334 name, (int)st.st_dev, (int)st.st_ino);
338 tdb->name = strdup(name);
340 tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
341 "tdb_open: failed to allocate name");
345 tdb->device = st.st_dev;
346 tdb->inode = st.st_ino;
347 tdb_unlock_open(tdb);
349 /* This make sure we have current map_size and mmap. */
350 tdb->methods->oob(tdb, tdb->map_size + 1, true);
352 /* Now it's fully formed, recover if necessary. */
353 if (tdb_needs_recovery(tdb)) {
354 ecode = tdb_lock_and_recover(tdb);
355 if (ecode != TDB_SUCCESS) {
361 tdb->ecode = tdb_ftable_init(tdb);
362 if (tdb->ecode != TDB_SUCCESS) {
371 /* Map ecode to some logical errno. */
373 switch (tdb->ecode) {
374 case TDB_ERR_CORRUPT:
379 saved_errno = EWOULDBLOCK;
382 saved_errno = ENOMEM;
385 saved_errno = EINVAL;
388 saved_errno = EINVAL;
397 if (tdb->flags & TDB_INTERNAL) {
403 free((char *)tdb->name);
405 if (close(tdb->fd) != 0)
406 tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
407 "tdb_open: failed to close tdb->fd"
408 " on error: %s", strerror(errno));
414 static int update_rec_hdr(struct tdb_context *tdb,
418 struct tdb_used_record *rec,
421 uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
422 enum TDB_ERROR ecode;
424 ecode = set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
425 keylen + dataroom, h);
426 if (ecode != TDB_SUCCESS) {
431 ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
432 if (ecode != TDB_SUCCESS) {
439 /* Returns -1 on error, 0 on OK */
440 static int replace_data(struct tdb_context *tdb,
442 struct tdb_data key, struct tdb_data dbuf,
443 tdb_off_t old_off, tdb_len_t old_room,
447 enum TDB_ERROR ecode;
449 /* Allocate a new record. */
450 new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
452 if (TDB_OFF_IS_ERR(new_off)) {
453 tdb->ecode = new_off;
457 /* We didn't like the existing one: remove it. */
459 add_stat(tdb, frees, 1);
460 ecode = add_free_record(tdb, old_off,
461 sizeof(struct tdb_used_record)
462 + key.dsize + old_room);
463 if (ecode == TDB_SUCCESS)
464 ecode = replace_in_hash(tdb, h, new_off);
466 ecode = add_to_hash(tdb, h, new_off);
468 if (ecode != TDB_SUCCESS) {
473 new_off += sizeof(struct tdb_used_record);
474 ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
475 if (ecode != TDB_SUCCESS) {
480 new_off += key.dsize;
481 ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
482 if (ecode != TDB_SUCCESS) {
487 /* FIXME: tdb_increment_seqnum(tdb); */
491 int tdb_store(struct tdb_context *tdb,
492 struct tdb_data key, struct tdb_data dbuf, int flag)
496 tdb_len_t old_room = 0;
497 struct tdb_used_record rec;
499 enum TDB_ERROR ecode;
501 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
502 if (TDB_OFF_IS_ERR(off)) {
507 /* Now we have lock on this hash bucket. */
508 if (flag == TDB_INSERT) {
510 tdb->ecode = TDB_ERR_EXISTS;
515 old_room = rec_data_length(&rec)
516 + rec_extra_padding(&rec);
517 if (old_room >= dbuf.dsize) {
518 /* Can modify in-place. Easy! */
519 if (update_rec_hdr(tdb, off,
520 key.dsize, dbuf.dsize,
523 ecode = tdb->methods->twrite(tdb,
528 if (ecode != TDB_SUCCESS) {
532 tdb_unlock_hashes(tdb, h.hlock_start,
533 h.hlock_range, F_WRLCK);
537 if (flag == TDB_MODIFY) {
538 /* if the record doesn't exist and we
539 are in TDB_MODIFY mode then we should fail
541 tdb->ecode = TDB_ERR_NOEXIST;
547 /* If we didn't use the old record, this implies we're growing. */
548 ret = replace_data(tdb, &h, key, dbuf, off, old_room, off != 0);
549 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
553 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
557 int tdb_append(struct tdb_context *tdb,
558 struct tdb_data key, struct tdb_data dbuf)
562 struct tdb_used_record rec;
563 tdb_len_t old_room = 0, old_dlen;
564 unsigned char *newdata;
565 struct tdb_data new_dbuf;
566 enum TDB_ERROR ecode;
569 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
570 if (TDB_OFF_IS_ERR(off)) {
576 old_dlen = rec_data_length(&rec);
577 old_room = old_dlen + rec_extra_padding(&rec);
579 /* Fast path: can append in place. */
580 if (rec_extra_padding(&rec) >= dbuf.dsize) {
581 if (update_rec_hdr(tdb, off, key.dsize,
582 old_dlen + dbuf.dsize, &rec, h.h))
585 off += sizeof(rec) + key.dsize + old_dlen;
586 ecode = tdb->methods->twrite(tdb, off, dbuf.dptr,
588 if (ecode != TDB_SUCCESS) {
593 /* FIXME: tdb_increment_seqnum(tdb); */
594 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
600 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
602 tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
603 "tdb_append: failed to allocate %zu bytes",
604 (size_t)(key.dsize+old_dlen+dbuf.dsize));
607 ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
609 if (ecode != TDB_SUCCESS) {
614 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
615 new_dbuf.dptr = newdata;
616 new_dbuf.dsize = old_dlen + dbuf.dsize;
622 /* If they're using tdb_append(), it implies they're growing record. */
623 ret = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
624 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
630 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
634 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
637 struct tdb_used_record rec;
641 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
642 if (TDB_OFF_IS_ERR(off)) {
648 tdb->ecode = TDB_ERR_NOEXIST;
651 ret.dsize = rec_data_length(&rec);
652 ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
654 if (TDB_PTR_IS_ERR(ret.dptr)) {
655 tdb->ecode = TDB_PTR_ERR(ret.dptr);
660 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
664 int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
667 struct tdb_used_record rec;
669 enum TDB_ERROR ecode;
671 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
672 if (TDB_OFF_IS_ERR(off)) {
678 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
679 tdb->ecode = TDB_ERR_NOEXIST;
683 ecode = delete_from_hash(tdb, &h);
684 if (ecode != TDB_SUCCESS) {
689 /* Free the deleted entry. */
690 add_stat(tdb, frees, 1);
691 ecode = add_free_record(tdb, off,
692 sizeof(struct tdb_used_record)
693 + rec_key_length(&rec)
694 + rec_data_length(&rec)
695 + rec_extra_padding(&rec));
696 if (ecode != TDB_SUCCESS) {
701 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
705 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
709 int tdb_close(struct tdb_context *tdb)
711 struct tdb_context **i;
714 tdb_trace(tdb, "tdb_close");
716 if (tdb->transaction) {
717 tdb_transaction_cancel(tdb);
721 if (tdb->flags & TDB_INTERNAL)
726 free((char *)tdb->name);
728 ret = close(tdb->fd);
733 /* Remove from contexts list */
734 for (i = &tdbs; *i; i = &(*i)->next) {
749 enum TDB_ERROR tdb_error(const struct tdb_context *tdb)
754 const char *tdb_errorstr(const struct tdb_context *tdb)
756 /* Gcc warns if you miss a case in the switch, so use that. */
757 switch (tdb->ecode) {
758 case TDB_SUCCESS: return "Success";
759 case TDB_ERR_CORRUPT: return "Corrupt database";
760 case TDB_ERR_IO: return "IO Error";
761 case TDB_ERR_LOCK: return "Locking error";
762 case TDB_ERR_OOM: return "Out of memory";
763 case TDB_ERR_EXISTS: return "Record exists";
764 case TDB_ERR_EINVAL: return "Invalid parameter";
765 case TDB_ERR_NOEXIST: return "Record does not exist";
766 case TDB_ERR_RDONLY: return "write not permitted";
768 return "Invalid error code";
771 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
772 enum TDB_ERROR ecode,
773 enum tdb_log_level level,
774 const char *fmt, ...)
779 /* tdb_open paths care about errno, so save it. */
780 int saved_errno = errno;
787 /* FIXME: Doesn't assume asprintf. */
789 len = vsnprintf(NULL, 0, fmt, ap);
792 message = malloc(len + 1);
794 tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
795 "out of memory formatting message:");
796 tdb->logfn(tdb, level, tdb->log_private, fmt);
800 len = vsprintf(message, fmt, ap);
802 tdb->logfn(tdb, level, tdb->log_private, message);