2 #include <ccan/tdb2/tdb2.h>
3 #include <ccan/build_assert/build_assert.h>
4 #include <ccan/likely/likely.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 PRINTF_ATTRIBUTE(4, 5) static void
14 null_log_fn(struct tdb_context *tdb,
15 enum tdb_debug_level level, void *priv,
20 static bool tdb_already_open(dev_t device, ino_t ino)
22 struct tdb_context *i;
24 for (i = tdbs; i; i = i->next) {
25 if (i->device == device && i->inode == ino) {
33 static uint64_t random_number(struct tdb_context *tdb)
39 fd = open("/dev/urandom", O_RDONLY);
41 if (tdb_read_all(fd, &ret, sizeof(ret))) {
42 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
43 "tdb_open: random from /dev/urandom\n");
49 /* FIXME: Untested! Based on Wikipedia protocol description! */
50 fd = open("/dev/egd-pool", O_RDWR);
52 /* Command is 1, next byte is size we want to read. */
53 char cmd[2] = { 1, sizeof(uint64_t) };
54 if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
55 char reply[1 + sizeof(uint64_t)];
56 int r = read(fd, reply, sizeof(reply));
58 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
59 "tdb_open: %u random bytes from"
60 " /dev/egd-pool\n", r-1);
61 /* Copy at least some bytes. */
62 memcpy(&ret, reply+1, r - 1);
63 if (reply[0] == sizeof(uint64_t)
64 && r == sizeof(reply)) {
73 /* Fallback: pid and time. */
74 gettimeofday(&now, NULL);
75 ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
76 tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
77 "tdb_open: random from getpid and time\n");
82 struct tdb_header hdr;
83 /* Initial free zone. */
84 struct free_zone_header zhdr;
85 tdb_off_t free[BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS) + 1];
86 struct tdb_free_record frec;
87 /* Rest up to 1 << INITIAL_ZONE_BITS is empty. */
88 char space[(1 << INITIAL_ZONE_BITS)
89 - sizeof(struct free_zone_header)
90 - sizeof(tdb_off_t) * (BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS)+1)
91 - sizeof(struct tdb_free_record)];
93 /* Don't count final padding! */
96 /* initialise a new database */
97 static int tdb_new_database(struct tdb_context *tdb,
98 struct tdb_attribute_seed *seed,
99 struct tdb_header *hdr)
101 /* We make it up in memory, then write it out if not internal */
102 struct new_database newdb;
103 unsigned int bucket, magic_len, dbsize;
105 /* Don't want any extra padding! */
106 dbsize = offsetof(struct new_database, tailer) + sizeof(newdb.tailer);
108 /* Fill in the header */
109 newdb.hdr.version = TDB_VERSION;
111 newdb.hdr.hash_seed = seed->seed;
113 newdb.hdr.hash_seed = random_number(tdb);
114 newdb.hdr.hash_test = TDB_HASH_MAGIC;
115 newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
116 sizeof(newdb.hdr.hash_test),
119 memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
120 /* Initial hashes are empty. */
121 memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
123 /* Free is mostly empty... */
124 newdb.zhdr.zone_bits = INITIAL_ZONE_BITS;
125 memset(newdb.free, 0, sizeof(newdb.free));
127 /* Create the single free entry. */
128 newdb.frec.magic_and_meta = TDB_FREE_MAGIC | INITIAL_ZONE_BITS;
129 newdb.frec.data_len = (sizeof(newdb.frec)
130 - sizeof(struct tdb_used_record)
131 + sizeof(newdb.space));
133 /* Add it to the correct bucket. */
134 bucket = size_to_bucket(INITIAL_ZONE_BITS, newdb.frec.data_len);
135 newdb.free[bucket] = offsetof(struct new_database, frec);
136 newdb.frec.next = newdb.frec.prev = 0;
138 /* Clear free space to keep valgrind happy, and avoid leaking stack. */
139 memset(newdb.space, 0, sizeof(newdb.space));
141 /* Tailer contains maximum number of free_zone bits. */
142 newdb.tailer = INITIAL_ZONE_BITS;
145 memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
146 strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
148 /* This creates an endian-converted database, as if read from disk */
149 magic_len = sizeof(newdb.hdr.magic_food);
151 (char *)&newdb.hdr + magic_len,
152 offsetof(struct new_database, space) - magic_len);
156 if (tdb->flags & TDB_INTERNAL) {
157 tdb->map_size = dbsize;
158 tdb->map_ptr = malloc(tdb->map_size);
160 tdb->ecode = TDB_ERR_OOM;
163 memcpy(tdb->map_ptr, &newdb, tdb->map_size);
166 if (lseek(tdb->fd, 0, SEEK_SET) == -1)
169 if (ftruncate(tdb->fd, 0) == -1)
172 if (!tdb_pwrite_all(tdb->fd, &newdb, dbsize, 0)) {
173 tdb->ecode = TDB_ERR_IO;
179 struct tdb_context *tdb_open(const char *name, int tdb_flags,
180 int open_flags, mode_t mode,
181 union tdb_attribute *attr)
183 struct tdb_context *tdb;
188 struct tdb_header hdr;
189 struct tdb_attribute_seed *seed = NULL;
191 tdb = malloc(sizeof(*tdb));
199 tdb->direct_access = 0;
201 tdb->map_size = sizeof(struct tdb_header);
202 tdb->ecode = TDB_SUCCESS;
203 tdb->flags = tdb_flags;
204 tdb->log = null_log_fn;
205 tdb->log_priv = NULL;
206 tdb->transaction = NULL;
208 /* last_zone will be set below. */
213 switch (attr->base.attr) {
214 case TDB_ATTRIBUTE_LOG:
215 tdb->log = attr->log.log_fn;
216 tdb->log_priv = attr->log.log_private;
218 case TDB_ATTRIBUTE_HASH:
219 tdb->khash = attr->hash.hash_fn;
220 tdb->hash_priv = attr->hash.hash_private;
222 case TDB_ATTRIBUTE_SEED:
226 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
227 "tdb_open: unknown attribute type %u\n",
232 attr = attr->base.next;
235 if ((open_flags & O_ACCMODE) == O_WRONLY) {
236 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
237 "tdb_open: can't open tdb %s write-only\n", name);
242 if ((open_flags & O_ACCMODE) == O_RDONLY) {
243 tdb->read_only = true;
244 /* read only databases don't do locking */
245 tdb->flags |= TDB_NOLOCK;
246 tdb->mmap_flags = PROT_READ;
248 tdb->read_only = false;
249 tdb->mmap_flags = PROT_READ | PROT_WRITE;
252 /* internal databases don't need any of the rest. */
253 if (tdb->flags & TDB_INTERNAL) {
254 tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
255 if (tdb_new_database(tdb, seed, &hdr) != 0) {
256 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
257 "tdb_open: tdb_new_database failed!");
260 tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
261 tdb->hash_seed = hdr.hash_seed;
266 if ((tdb->fd = open(name, open_flags, mode)) == -1) {
267 tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
268 "tdb_open: could not open file %s: %s\n",
269 name, strerror(errno));
270 goto fail; /* errno set by open(2) */
273 /* on exec, don't inherit the fd */
274 v = fcntl(tdb->fd, F_GETFD, 0);
275 fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
277 /* ensure there is only one process initialising at once */
278 if (tdb_lock_open(tdb) == -1) {
279 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
280 "tdb_open: failed to get open lock on %s: %s\n",
281 name, strerror(errno));
282 goto fail; /* errno set by tdb_brlock */
285 if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0)
286 || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
287 if (!(open_flags & O_CREAT)
288 || tdb_new_database(tdb, seed, &hdr) == -1) {
290 errno = EIO; /* ie bad format or something */
294 } else if (hdr.version != TDB_VERSION) {
295 if (hdr.version == bswap_64(TDB_VERSION))
296 tdb->flags |= TDB_CONVERT;
299 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
300 "tdb_open: %s is unknown version 0x%llx\n",
301 name, (long long)hdr.version);
307 tdb_convert(tdb, &hdr, sizeof(hdr));
308 tdb->hash_seed = hdr.hash_seed;
309 hash_test = TDB_HASH_MAGIC;
310 hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
311 if (hdr.hash_test != hash_test) {
312 /* wrong hash variant */
313 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
314 "tdb_open: %s uses a different hash function\n",
320 if (fstat(tdb->fd, &st) == -1)
323 /* Is it already in the open list? If so, fail. */
324 if (tdb_already_open(st.st_dev, st.st_ino)) {
326 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
327 "tdb_open: %s (%d,%d) is already open in this process\n",
328 name, (int)st.st_dev, (int)st.st_ino);
333 tdb->name = strdup(name);
339 tdb->device = st.st_dev;
340 tdb->inode = st.st_ino;
341 tdb_unlock_open(tdb);
343 /* This make sure we have current map_size and mmap. */
344 tdb->methods->oob(tdb, tdb->map_size + 1, true);
346 /* Now we can pick a random free zone to start from. */
347 if (tdb_zone_init(tdb) == -1)
364 if (tdb->flags & TDB_INTERNAL) {
369 free((char *)tdb->name);
371 if (close(tdb->fd) != 0)
372 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
373 "tdb_open: failed to close tdb->fd"
380 /* FIXME: modify, don't rewrite! */
381 static int update_rec_hdr(struct tdb_context *tdb,
385 struct tdb_used_record *rec,
388 uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
390 if (set_header(tdb, rec, keylen, datalen, keylen + dataroom, h,
394 return tdb_write_convert(tdb, off, rec, sizeof(*rec));
397 /* Returns -1 on error, 0 on OK */
398 static int replace_data(struct tdb_context *tdb,
400 struct tdb_data key, struct tdb_data dbuf,
401 tdb_off_t old_off, tdb_len_t old_room,
407 /* Allocate a new record. */
408 new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, growing);
409 if (unlikely(new_off == TDB_OFF_ERR))
412 /* We didn't like the existing one: remove it. */
414 add_free_record(tdb, old_zone, old_off,
415 sizeof(struct tdb_used_record)
416 + key.dsize + old_room);
417 if (replace_in_hash(tdb, h, new_off) == -1)
420 if (add_to_hash(tdb, h, new_off) == -1)
424 new_off += sizeof(struct tdb_used_record);
425 if (tdb->methods->write(tdb, new_off, key.dptr, key.dsize) == -1)
428 new_off += key.dsize;
429 if (tdb->methods->write(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
432 /* FIXME: tdb_increment_seqnum(tdb); */
436 int tdb_store(struct tdb_context *tdb,
437 struct tdb_data key, struct tdb_data dbuf, int flag)
441 tdb_len_t old_room = 0;
442 struct tdb_used_record rec;
445 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
446 if (unlikely(off == TDB_OFF_ERR))
449 /* Now we have lock on this hash bucket. */
450 if (flag == TDB_INSERT) {
452 tdb->ecode = TDB_ERR_EXISTS;
457 old_room = rec_data_length(&rec)
458 + rec_extra_padding(&rec);
459 if (old_room >= dbuf.dsize) {
460 /* Can modify in-place. Easy! */
461 if (update_rec_hdr(tdb, off,
462 key.dsize, dbuf.dsize,
465 if (tdb->methods->write(tdb, off + sizeof(rec)
467 dbuf.dptr, dbuf.dsize))
469 tdb_unlock_hashes(tdb, h.hlock_start,
470 h.hlock_range, F_WRLCK);
473 /* FIXME: See if right record is free? */
475 if (flag == TDB_MODIFY) {
476 /* if the record doesn't exist and we
477 are in TDB_MODIFY mode then we should fail
479 tdb->ecode = TDB_ERR_NOEXIST;
485 /* If we didn't use the old record, this implies we're growing. */
486 ret = replace_data(tdb, &h, key, dbuf, off, old_room,
487 rec_zone_bits(&rec), off != 0);
488 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
492 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
496 int tdb_append(struct tdb_context *tdb,
497 struct tdb_data key, struct tdb_data dbuf)
501 struct tdb_used_record rec;
502 tdb_len_t old_room = 0, old_dlen;
503 unsigned char *newdata;
504 struct tdb_data new_dbuf;
507 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
508 if (unlikely(off == TDB_OFF_ERR))
512 old_dlen = rec_data_length(&rec);
513 old_room = old_dlen + rec_extra_padding(&rec);
515 /* Fast path: can append in place. */
516 if (rec_extra_padding(&rec) >= dbuf.dsize) {
517 if (update_rec_hdr(tdb, off, key.dsize,
518 old_dlen + dbuf.dsize, &rec, h.h))
521 off += sizeof(rec) + key.dsize + old_dlen;
522 if (tdb->methods->write(tdb, off, dbuf.dptr,
526 /* FIXME: tdb_increment_seqnum(tdb); */
527 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
531 /* FIXME: Check right record free? */
534 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
536 tdb->ecode = TDB_ERR_OOM;
537 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
538 "tdb_append: cannot allocate %llu bytes!\n",
539 (long long)key.dsize + old_dlen + dbuf.dsize);
542 if (tdb->methods->read(tdb, off + sizeof(rec) + key.dsize,
543 newdata, old_dlen) != 0) {
547 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
548 new_dbuf.dptr = newdata;
549 new_dbuf.dsize = old_dlen + dbuf.dsize;
555 /* If they're using tdb_append(), it implies they're growing record. */
556 ret = replace_data(tdb, &h, key, new_dbuf, off,
557 old_room, rec_zone_bits(&rec), true);
558 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
564 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
568 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
571 struct tdb_used_record rec;
575 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
576 if (unlikely(off == TDB_OFF_ERR))
580 tdb->ecode = TDB_ERR_NOEXIST;
583 ret.dsize = rec_data_length(&rec);
584 ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
588 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
592 int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
595 struct tdb_used_record rec;
598 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
599 if (unlikely(off == TDB_OFF_ERR))
603 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
604 tdb->ecode = TDB_ERR_NOEXIST;
608 if (delete_from_hash(tdb, &h) == -1)
611 /* Free the deleted entry. */
612 if (add_free_record(tdb, rec_zone_bits(&rec), off,
613 sizeof(struct tdb_used_record)
614 + rec_key_length(&rec)
615 + rec_data_length(&rec)
616 + rec_extra_padding(&rec)) != 0)
619 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
623 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
627 int tdb_close(struct tdb_context *tdb)
629 struct tdb_context **i;
633 if (tdb->transaction) {
634 tdb_transaction_cancel(tdb);
637 tdb_trace(tdb, "tdb_close");
640 if (tdb->flags & TDB_INTERNAL)
645 free((char *)tdb->name);
647 ret = close(tdb->fd);
652 /* Remove from contexts list */
653 for (i = &tdbs; *i; i = &(*i)->next) {
668 enum TDB_ERROR tdb_error(struct tdb_context *tdb)
673 const char *tdb_errorstr(struct tdb_context *tdb)
675 /* Gcc warns if you miss a case in the switch, so use that. */
676 switch (tdb->ecode) {
677 case TDB_SUCCESS: return "Success";
678 case TDB_ERR_CORRUPT: return "Corrupt database";
679 case TDB_ERR_IO: return "IO Error";
680 case TDB_ERR_LOCK: return "Locking error";
681 case TDB_ERR_OOM: return "Out of memory";
682 case TDB_ERR_EXISTS: return "Record exists";
683 case TDB_ERR_NESTING: return "Transaction already started";
684 case TDB_ERR_EINVAL: return "Invalid parameter";
685 case TDB_ERR_NOEXIST: return "Record does not exist";
686 case TDB_ERR_RDONLY: return "write not permitted";
688 return "Invalid error code";