-#define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
-
-struct JournalFile {
- sd_journal *journal;
-
- int fd;
- char *path;
- struct stat last_stat;
- int prot;
- bool writable;
-
- Header *header;
-
- HashItem *hash_table;
- void *hash_table_window;
- uint64_t hash_table_window_size;
-
- uint64_t *bisect_table;
- void *bisect_table_window;
- uint64_t bisect_table_window_size;
-
- void *window;
- uint64_t window_offset;
- uint64_t window_size;
-
- Object *current;
- uint64_t current_offset;
-
- LIST_FIELDS(JournalFile, files);
-};
-
-struct sd_journal {
- LIST_HEAD(JournalFile, files);
-};
-
-static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
-
-#define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
-
-void journal_file_close(JournalFile *f) {
- assert(f);
-
- if (f->journal)
- LIST_REMOVE(JournalFile, files, f->journal->files, f);
-
- if (f->fd >= 0)
- close_nointr_nofail(f->fd);
-
- if (f->header)
- munmap(f->header, PAGE_ALIGN(sizeof(Header)));
-
- if (f->hash_table_window)
- munmap(f->hash_table_window, f->hash_table_window_size);
-
- if (f->bisect_table_window)
- munmap(f->bisect_table_window, f->bisect_table_window_size);
-
- if (f->window)
- munmap(f->window, f->window_size);
-
- free(f->path);
- free(f);
-}
-
-static int journal_file_init_header(JournalFile *f) {
- Header h;
- ssize_t k;
- int r;
-
- assert(f);
-
- zero(h);
- memcpy(h.signature, signature, 8);
- h.arena_offset = htole64(ALIGN64(sizeof(h)));
- h.arena_max_size = htole64(DEFAULT_ARENA_MAX_SIZE);
- h.arena_min_size = htole64(DEFAULT_ARENA_MIN_SIZE);
- h.arena_keep_free = htole64(DEFAULT_ARENA_KEEP_FREE);
-
- r = sd_id128_randomize(&h.file_id);
- if (r < 0)
- return r;
-
- k = pwrite(f->fd, &h, sizeof(h), 0);
- if (k < 0)
- return -errno;
-
- if (k != sizeof(h))
- return -EIO;
-
- return 0;
-}
-
-static int journal_file_refresh_header(JournalFile *f) {
- int r;
-
- assert(f);
-
- r = sd_id128_get_machine(&f->header->machine_id);
- if (r < 0)
- return r;
-
- r = sd_id128_get_boot(&f->header->boot_id);
- if (r < 0)
- return r;
-
- f->header->state = htole32(STATE_ONLINE);
- return 0;
-}
-
-static int journal_file_verify_header(JournalFile *f) {
- assert(f);
-
- if (memcmp(f->header, signature, 8))
- return -EBADMSG;
-
- if (f->header->incompatible_flags != 0)
- return -EPROTONOSUPPORT;
-
- if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
- return -ENODATA;
-
- if (f->writable) {
- uint32_t state;
- sd_id128_t machine_id;
- int r;
-
- r = sd_id128_get_machine(&machine_id);
- if (r < 0)
- return r;
-
- if (!sd_id128_equal(machine_id, f->header->machine_id))
- return -EHOSTDOWN;
-
- state = le32toh(f->header->state);
-
- if (state == STATE_ONLINE)
- log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f->path);
- else if (state == STATE_ARCHIVED)
- return -ESHUTDOWN;
- else if (state != STATE_OFFLINE)
- log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
- }
-
- return 0;
-}
-
-static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
- uint64_t asize;
- uint64_t old_size, new_size;
-
- assert(f);
-
- if (offset < le64toh(f->header->arena_offset))
- return -EINVAL;
-
- new_size = PAGE_ALIGN(offset + size);
-
- /* We assume that this file is not sparse, and we know that
- * for sure, since we alway call posix_fallocate()
- * ourselves */
-
- old_size =
- le64toh(f->header->arena_offset) +
- le64toh(f->header->arena_size);
-
- if (old_size >= new_size)
- return 0;
-
- asize = new_size - le64toh(f->header->arena_offset);
-
- if (asize > le64toh(f->header->arena_min_size)) {
- struct statvfs svfs;
-
- if (fstatvfs(f->fd, &svfs) >= 0) {
- uint64_t available;
-
- available = svfs.f_bfree * svfs.f_bsize;
-
- if (available >= f->header->arena_keep_free)
- available -= f->header->arena_keep_free;
- else
- available = 0;
-
- if (new_size - old_size > available)
- return -E2BIG;
- }
- }
-
- if (asize > le64toh(f->header->arena_max_size))
- return -E2BIG;
-
- if (posix_fallocate(f->fd, 0, new_size) < 0)
- return -errno;
-
- if (fstat(f->fd, &f->last_stat) < 0)
- return -errno;
-
- f->header->arena_size = htole64(asize);
-
- return 0;
-}
-
-static int journal_file_map(
- JournalFile *f,
- uint64_t offset,
- uint64_t size,
- void **_window,
- uint64_t *_woffset,
- uint64_t *_wsize,
- void **ret) {
-
- uint64_t woffset, wsize;
- void *window;
-
- assert(f);
- assert(size > 0);
- assert(ret);
-
- woffset = offset & ~((uint64_t) page_size() - 1ULL);
- wsize = size + (offset - woffset);
- wsize = PAGE_ALIGN(wsize);
-
- window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
- if (window == MAP_FAILED)
- return -errno;
-
- if (_window)
- *_window = window;
-
- if (_woffset)
- *_woffset = woffset;
-
- if (_wsize)
- *_wsize = wsize;
-
- *ret = (uint8_t*) window + (offset - woffset);
-
- return 0;
-}
-
-static int journal_file_move_to(JournalFile *f, uint64_t offset, uint64_t size, void **ret) {
- void *p;
- uint64_t delta;
- int r;
-
- assert(f);
- assert(ret);
-
- if (_likely_(f->window &&
- f->window_offset <= offset &&
- f->window_offset+f->window_size >= offset + size)) {
-
- *ret = (uint8_t*) f->window + (offset - f->window_offset);
- return 0;
- }
-
- if (f->window) {
- if (munmap(f->window, f->window_size) < 0)
- return -errno;
-
- f->window = NULL;
- f->window_size = f->window_offset = 0;
- }
-
- if (size < DEFAULT_WINDOW_SIZE) {
- /* If the default window size is larger then what was
- * asked for extend the mapping a bit in the hope to
- * minimize needed remappings later on. We add half
- * the window space before and half behind the
- * requested mapping */
-
- delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
-
- if (offset < delta)
- delta = offset;
-
- offset -= delta;
- size += (DEFAULT_WINDOW_SIZE - delta);
- } else
- delta = 0;
-
- r = journal_file_map(f,
- offset, size,
- &f->window, &f->window_offset, &f->window_size,
- & p);
-
- if (r < 0)
- return r;
-
- *ret = (uint8_t*) p + delta;
- return 0;
-}
-
-static bool verify_hash(Object *o) {
- uint64_t t;
-
- assert(o);
-
- t = le64toh(o->object.type);
- if (t == OBJECT_DATA) {
- uint64_t s, h1, h2;
-
- s = le64toh(o->object.size);
-
- h1 = le64toh(o->data.hash);
- h2 = hash64(o->data.payload, s - offsetof(Object, data.payload));
-
- return h1 == h2;
- }
-
- return true;
-}
-
-int journal_file_move_to_object(JournalFile *f, uint64_t offset, Object **ret) {
- int r;
- void *t;
- Object *o;
- uint64_t s;
-
- assert(f);
- assert(ret);
-
- r = journal_file_move_to(f, offset, sizeof(ObjectHeader), &t);
- if (r < 0)
- return r;
-
- o = (Object*) t;
- s = le64toh(o->object.size);
-
- if (s < sizeof(ObjectHeader))
- return -EBADMSG;
-
- if (s > sizeof(ObjectHeader)) {
- r = journal_file_move_to(f, offset, s, &t);
- if (r < 0)
- return r;
-
- o = (Object*) t;
- }
-
- if (!verify_hash(o))
- return -EBADMSG;
-
- *ret = o;
- return 0;
-}
-
-static uint64_t journal_file_seqnum(JournalFile *f) {
- uint64_t r;
-
- assert(f);
-
- r = le64toh(f->header->seqnum) + 1;
- f->header->seqnum = htole64(r);
-
- return r;
-}
-
-static int journal_file_append_object(JournalFile *f, uint64_t size, Object **ret, uint64_t *offset) {
- int r;
- uint64_t p;
- Object *tail, *o;
- void *t;
-
- assert(f);
- assert(size >= sizeof(ObjectHeader));
- assert(offset);
- assert(ret);
-
- p = le64toh(f->header->tail_object_offset);
-
- if (p == 0)
- p = le64toh(f->header->arena_offset);
- else {
- r = journal_file_move_to_object(f, p, &tail);
- if (r < 0)
- return r;
-
- p += ALIGN64(le64toh(tail->object.size));
- }
-
- r = journal_file_allocate(f, p, size);
- if (r < 0)
- return r;
-
- r = journal_file_move_to(f, p, size, &t);
- if (r < 0)
- return r;
-
- o = (Object*) t;
-
- zero(o->object);
- o->object.type = htole64(OBJECT_UNUSED);
- zero(o->object.reserved);
- o->object.size = htole64(size);
-
- f->header->tail_object_offset = htole64(p);
- if (f->header->head_object_offset == 0)
- f->header->head_object_offset = htole64(p);
-
- f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
-
- *ret = o;
- *offset = p;
-
- return 0;
-}
-
-static int journal_file_setup_hash_table(JournalFile *f) {
- uint64_t s, p;
- Object *o;
- int r;
-
- assert(f);
-
- s = DEFAULT_HASH_TABLE_SIZE;
- r = journal_file_append_object(f, offsetof(Object, hash_table.table) + s, &o, &p);
- if (r < 0)
- return r;
-
- o->object.type = htole64(OBJECT_HASH_TABLE);
- memset(o->hash_table.table, 0, s);
-
- f->header->hash_table_offset = htole64(p + offsetof(Object, hash_table.table));
- f->header->hash_table_size = htole64(s);
-
- return 0;
-}
-
-static int journal_file_setup_bisect_table(JournalFile *f) {
- uint64_t s, p;
- Object *o;
- int r;
-
- assert(f);
-
- s = DEFAULT_BISECT_TABLE_SIZE;
- r = journal_file_append_object(f, offsetof(Object, bisect_table.table) + s, &o, &p);
- if (r < 0)
- return r;
-
- o->object.type = htole64(OBJECT_BISECT_TABLE);
- memset(o->bisect_table.table, 0, s);
-
- f->header->bisect_table_offset = htole64(p + offsetof(Object, bisect_table.table));
- f->header->bisect_table_size = htole64(s);
-
- return 0;
-}
-
-static int journal_file_map_hash_table(JournalFile *f) {
- uint64_t s, p;
- void *t;
- int r;
-
- assert(f);
-
- p = le64toh(f->header->hash_table_offset);
- s = le64toh(f->header->hash_table_size);
-
- r = journal_file_map(f,
- p, s,
- &f->hash_table_window, NULL, &f->hash_table_window_size,
- &t);
- if (r < 0)
- return r;
-
- f->hash_table = t;
- return 0;
-}
-
-static int journal_file_map_bisect_table(JournalFile *f) {
- uint64_t s, p;
- void *t;
- int r;
-
- assert(f);
-
- p = le64toh(f->header->bisect_table_offset);
- s = le64toh(f->header->bisect_table_size);
-
- r = journal_file_map(f,
- p, s,
- &f->bisect_table_window, NULL, &f->bisect_table_window_size,
- &t);
-
- if (r < 0)
- return r;
-
- f->bisect_table = t;
- return 0;
-}
-
-static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash_index) {
- uint64_t p;
- int r;
-
- assert(f);
- assert(o);
- assert(offset > 0);
- assert(o->object.type == htole64(OBJECT_DATA));
-
- o->data.head_entry_offset = o->data.tail_entry_offset = 0;
- o->data.next_hash_offset = 0;
-
- p = le64toh(f->hash_table[hash_index].tail_hash_offset);
- if (p == 0) {
- /* Only entry in the hash table is easy */
-
- o->data.prev_hash_offset = 0;
- f->hash_table[hash_index].head_hash_offset = htole64(offset);
- } else {
- o->data.prev_hash_offset = htole64(p);
-
- /* Temporarily move back to the previous data object,
- * to patch in pointer */
-
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
-
- o->data.next_hash_offset = offset;
-
- r = journal_file_move_to_object(f, offset, &o);
- if (r < 0)
- return r;
- }
-
- f->hash_table[hash_index].tail_hash_offset = htole64(offset);
-
- return 0;
-}
-
-static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
- uint64_t hash, h, p, np;
- uint64_t osize;
- Object *o;
- int r;
-
- assert(f);
- assert(data || size == 0);
-
- osize = offsetof(Object, data.payload) + size;
-
- hash = hash64(data, size);
- h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
- p = le64toh(f->hash_table[h].head_hash_offset);
-
- while (p != 0) {
- /* Look for this data object in the hash table */
-
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
-
- if (le64toh(o->object.type) != OBJECT_DATA)
- return -EBADMSG;
-
- if (le64toh(o->object.size) == osize &&
- memcmp(o->data.payload, data, size) == 0) {
-
- if (le64toh(o->data.hash) != hash)
- return -EBADMSG;
-
- if (ret)
- *ret = o;
-
- if (offset)
- *offset = p;
-
- return 0;
- }
-
- p = le64toh(o->data.next_hash_offset);
- }
-
- r = journal_file_append_object(f, osize, &o, &np);
- if (r < 0)
- return r;
-
- o->object.type = htole64(OBJECT_DATA);
- o->data.hash = htole64(hash);
- memcpy(o->data.payload, data, size);
-
- r = journal_file_link_data(f, o, np, h);
- if (r < 0)
- return r;
-
- if (ret)
- *ret = o;
-
- if (offset)
- *offset = np;
-
- return 0;
-}
-
-uint64_t journal_file_entry_n_items(Object *o) {
- assert(o);
- assert(o->object.type == htole64(OBJECT_ENTRY));
-
- return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
-}
-
-static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
- uint64_t p, q;
- int r;
- assert(f);
- assert(o);
- assert(offset > 0);
-
- p = le64toh(o->entry.items[i].object_offset);
- if (p == 0)
- return -EINVAL;
-
- o->entry.items[i].next_entry_offset = 0;
-
- /* Move to the data object */
- r = journal_file_move_to_object(f, p, &o);
- if (r < 0)
- return r;
-
- if (o->object.type != htole64(OBJECT_DATA))
- return -EBADMSG;