chiark / gitweb /
journal: implement parallel traversal in client
authorLennart Poettering <lennart@poettering.net>
Sat, 8 Oct 2011 00:20:44 +0000 (02:20 +0200)
committerLennart Poettering <lennart@poettering.net>
Sat, 8 Oct 2011 00:21:05 +0000 (02:21 +0200)
Makefile.am
src/journal/journal-def.h
src/journal/journal-file.c [new file with mode: 0644]
src/journal/journal-file.h [moved from src/journal/journal-private.h with 92% similarity]
src/journal/journalctl.c
src/journal/journald.c
src/journal/sd-journal.c
src/journal/sd-journal.h
src/journal/test-journal.c

index d43da3c47bb392137aad0add7b8aee6a17d26c86..892072318fb1751436efc8e518b02ef699431a87 100644 (file)
@@ -965,6 +965,7 @@ test_id128_LDADD = \
 test_journal_SOURCES = \
        src/journal/test-journal.c \
        src/journal/sd-journal.c \
+        src/journal/journal-file.c \
         src/journal/lookup3.c \
         src/sd-id128.c
 
@@ -977,6 +978,7 @@ test_journal_LDADD = \
 systemd_journald_SOURCES = \
        src/journal/journald.c \
        src/journal/sd-journal.c \
+        src/journal/journal-file.c \
         src/journal/lookup3.c \
         src/sd-id128.c \
         src/acl-util.c
@@ -993,6 +995,7 @@ systemd_journald_LDADD = \
 systemd_journalctl_SOURCES = \
        src/journal/journalctl.c \
        src/journal/sd-journal.c \
+        src/journal/journal-file.c \
         src/journal/lookup3.c \
         src/sd-id128.c
 
index 2a519fe0dbf590f609a4336e61d9d162f805f320..b3fa1e524f043e3bfc6d5d12d9de6f39d67cea68 100644 (file)
@@ -48,7 +48,7 @@ enum {
 
 _packed_ struct ObjectHeader {
         uint8_t type;
-        uint8_t reserved[3];
+        uint8_t reserved[7];
         uint64_t size;
         uint8_t payload[];
 };
@@ -74,6 +74,7 @@ _packed_ struct EntryObject {
         uint64_t seqnum;
         uint64_t realtime;
         uint64_t monotonic;
+        sd_id128_t boot_id;
         uint64_t xor_hash;
         uint64_t prev_entry_offset;
         uint64_t next_entry_offset;
@@ -118,6 +119,7 @@ _packed_ struct Header {
         sd_id128_t file_id;
         sd_id128_t machine_id;
         sd_id128_t boot_id;
+        sd_id128_t seqnum_id;
         uint64_t arena_offset;
         uint64_t arena_size;
         uint64_t arena_max_size;
@@ -133,7 +135,6 @@ _packed_ struct Header {
         uint64_t tail_entry_offset;
         uint64_t last_bisect_offset;
         uint64_t n_objects;
-        uint64_t seqnum_base;
         uint64_t seqnum;
 };
 
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
new file mode 100644 (file)
index 0000000..37e2e37
--- /dev/null
@@ -0,0 +1,1191 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2011 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/mman.h>
+#include <errno.h>
+#include <sys/uio.h>
+#include <unistd.h>
+#include <sys/statvfs.h>
+#include <fcntl.h>
+#include <stddef.h>
+
+#include "journal-def.h"
+#include "journal-file.h"
+#include "lookup3.h"
+
+#define DEFAULT_ARENA_MAX_SIZE (16ULL*1024ULL*1024ULL*1024ULL)
+#define DEFAULT_ARENA_MIN_SIZE (256ULL*1024ULL)
+#define DEFAULT_ARENA_KEEP_FREE (1ULL*1024ULL*1024ULL)
+
+#define DEFAULT_HASH_TABLE_SIZE (2047ULL*16ULL)
+#define DEFAULT_BISECT_TABLE_SIZE ((DEFAULT_ARENA_MAX_SIZE/(64ULL*1024ULL))*8ULL)
+
+#define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
+
+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->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;
+
+        h.seqnum_id = h.file_id;
+
+        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, int type, 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 (type >= 0 && le64toh(o->object.type) != type)
+                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, -1, &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, OBJECT_DATA, &o);
+                if (r < 0)
+                        return r;
+
+                o->data.next_hash_offset = offset;
+
+                r = journal_file_move_to_object(f, offset, OBJECT_DATA, &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, OBJECT_DATA, &o);
+                if (r < 0)
+                        return r;
+
+                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, OBJECT_DATA, &o);
+        if (r < 0)
+                return r;
+
+        q = le64toh(o->data.tail_entry_offset);
+        o->data.tail_entry_offset = htole64(offset);
+
+        if (q == 0)
+                o->data.head_entry_offset = htole64(offset);
+        else {
+                uint64_t n, j;
+
+                /* Move to previous entry */
+                r = journal_file_move_to_object(f, q, OBJECT_ENTRY, &o);
+                if (r < 0)
+                        return r;
+
+                n = journal_file_entry_n_items(o);
+                for (j = 0; j < n; j++)
+                        if (le64toh(o->entry.items[j].object_offset) == p)
+                                break;
+
+                if (j >= n)
+                        return -EBADMSG;
+
+                o->entry.items[j].next_entry_offset = offset;
+        }
+
+        /* Move back to original entry */
+        r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
+        if (r < 0)
+                return r;
+
+        o->entry.items[i].prev_entry_offset = q;
+        return 0;
+}
+
+static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
+        uint64_t p, i, n, k, a, b;
+        int r;
+
+        assert(f);
+        assert(o);
+        assert(offset > 0);
+        assert(o->object.type == htole64(OBJECT_ENTRY));
+
+        /* Link up the entry itself */
+        p = le64toh(f->header->tail_entry_offset);
+
+        o->entry.prev_entry_offset = f->header->tail_entry_offset;
+        o->entry.next_entry_offset = 0;
+
+        if (p == 0)
+                f->header->head_entry_offset = htole64(offset);
+        else {
+                /* Temporarily move back to the previous entry, to
+                 * patch in pointer */
+
+                r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
+                if (r < 0)
+                        return r;
+
+                o->entry.next_entry_offset = htole64(offset);
+
+                r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
+                if (r < 0)
+                        return r;
+        }
+
+        f->header->tail_entry_offset = htole64(offset);
+
+        /* Link up the items */
+        n = journal_file_entry_n_items(o);
+        for (i = 0; i < n; i++) {
+                r = journal_file_link_entry_item(f, o, offset, i);
+                if (r < 0)
+                        return r;
+        }
+
+        /* Link up the entry in the bisect table */
+        n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
+        k = le64toh(f->header->arena_max_size) / n;
+
+        a = (le64toh(f->header->last_bisect_offset) + k - 1) / k;
+        b = offset / k;
+
+        for (; a <= b; a++)
+                f->bisect_table[a] = htole64(offset);
+
+        f->header->last_bisect_offset = htole64(offset + le64toh(o->object.size));
+
+        return 0;
+}
+
+static int journal_file_append_entry_internal(
+                JournalFile *f,
+                const dual_timestamp *ts,
+                uint64_t xor_hash,
+                const EntryItem items[], unsigned n_items,
+                Object **ret, uint64_t *offset) {
+        uint64_t np;
+        uint64_t osize;
+        Object *o;
+        int r;
+
+        assert(f);
+        assert(items || n_items == 0);
+
+        osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
+
+        r = journal_file_append_object(f, osize, &o, &np);
+        if (r < 0)
+                return r;
+
+        o->object.type = htole64(OBJECT_ENTRY);
+        o->entry.seqnum = htole64(journal_file_seqnum(f));
+        memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
+        o->entry.realtime = ts ? htole64(ts->realtime) : 0;
+        o->entry.monotonic = ts ? htole64(ts->monotonic) : 0;
+        o->entry.xor_hash = htole64(xor_hash);
+        o->entry.boot_id = f->header->boot_id;
+
+        r = journal_file_link_entry(f, o, np);
+        if (r < 0)
+                return r;
+
+        if (ret)
+                *ret = o;
+
+        if (offset)
+                *offset = np;
+
+        return 0;
+}
+
+int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset) {
+        unsigned i;
+        EntryItem *items;
+        int r;
+        uint64_t xor_hash = 0;
+
+        assert(f);
+        assert(iovec || n_iovec == 0);
+
+        items = new(EntryItem, n_iovec);
+        if (!items)
+                return -ENOMEM;
+
+        for (i = 0; i < n_iovec; i++) {
+                uint64_t p;
+                Object *o;
+
+                r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
+                if (r < 0)
+                        goto finish;
+
+                xor_hash ^= le64toh(o->data.hash);
+                items[i].object_offset = htole64(p);
+        }
+
+        r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, ret, offset);
+
+finish:
+        free(items);
+
+        return r;
+}
+
+int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset) {
+        Object *o;
+        uint64_t lower, upper, p, n, k;
+        int r;
+
+        assert(f);
+
+        n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
+        k = le64toh(f->header->arena_max_size) / n;
+
+        lower = 0;
+        upper = le64toh(f->header->last_bisect_offset)/k+1;
+
+        while (lower < upper) {
+                k = (upper + lower) / 2;
+                p = le64toh(f->bisect_table[k]);
+
+                if (p == 0) {
+                        upper = k;
+                        continue;
+                }
+
+                r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
+                if (r < 0)
+                        return r;
+
+                if (o->entry.seqnum == seqnum) {
+                        if (ret)
+                                *ret = o;
+
+                        if (offset)
+                                *offset = p;
+
+                        return 1;
+                } else if (seqnum < o->entry.seqnum)
+                        upper = k;
+                else if (seqnum > o->entry.seqnum)
+                        lower = k+1;
+        }
+
+        assert(lower == upper);
+
+        if (lower <= 0)
+                return 0;
+
+        /* The object we are looking for is between
+         * bisect_table[lower-1] and bisect_table[lower] */
+
+        p = le64toh(f->bisect_table[lower-1]);
+
+        for (;;) {
+                r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
+                if (r < 0)
+                        return r;
+
+                if (o->entry.seqnum == seqnum) {
+                        if (ret)
+                                *ret = o;
+
+                        if (offset)
+                                *offset = p;
+
+                        return 1;
+
+                } if (seqnum < o->entry.seqnum)
+                        return 0;
+
+                if (o->entry.next_entry_offset == 0)
+                        return 0;
+
+                p = le64toh(o->entry.next_entry_offset);
+        }
+
+        return 0;
+}
+
+int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
+        uint64_t np;
+        int r;
+
+        assert(f);
+
+        if (!o)
+                np = le64toh(f->header->head_entry_offset);
+        else {
+                if (le64toh(o->object.type) != OBJECT_ENTRY)
+                        return -EINVAL;
+
+                np = le64toh(o->entry.next_entry_offset);
+        }
+
+        if (np == 0)
+                return 0;
+
+        r = journal_file_move_to_object(f, np, OBJECT_ENTRY, &o);
+        if (r < 0)
+                return r;
+
+        if (ret)
+                *ret = o;
+
+        if (offset)
+                *offset = np;
+
+        return 1;
+}
+
+int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
+        uint64_t np;
+        int r;
+
+        assert(f);
+
+        if (!o)
+                np = le64toh(f->header->tail_entry_offset);
+        else {
+                if (le64toh(o->object.type) != OBJECT_ENTRY)
+                        return -EINVAL;
+
+                np = le64toh(o->entry.prev_entry_offset);
+        }
+
+        if (np == 0)
+                return 0;
+
+        r = journal_file_move_to_object(f, np, OBJECT_ENTRY, &o);
+        if (r < 0)
+                return r;
+
+        if (ret)
+                *ret = o;
+
+        if (offset)
+                *offset = np;
+
+        return 1;
+}
+
+int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
+        uint64_t p, osize, hash, h;
+        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) {
+                Object *o;
+
+                r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
+                if (r < 0)
+                        return r;
+
+                if (le64toh(o->object.size) == osize &&
+                    memcmp(o->data.payload, data, size) == 0) {
+
+                        if (le64toh(o->data.hash) != hash)
+                                return -EBADMSG;
+
+                        if (o->data.head_entry_offset == 0)
+                                return 0;
+
+                        p = le64toh(o->data.head_entry_offset);
+                        r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
+                        if (r < 0)
+                                return r;
+
+                        if (ret)
+                                *ret = o;
+
+                        if (offset)
+                                *offset = p;
+
+                        return 1;
+                }
+
+                p = le64toh(o->data.next_hash_offset);
+        }
+
+        return 0;
+}
+
+int journal_file_find_last_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
+        uint64_t p, osize, hash, h;
+        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].tail_hash_offset);
+
+        while (p != 0) {
+                Object *o;
+
+                r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
+                if (r < 0)
+                        return r;
+
+                if (le64toh(o->object.size) == osize &&
+                    memcmp(o->data.payload, data, size) == 0) {
+
+                        if (le64toh(o->data.hash) != hash)
+                                return -EBADMSG;
+
+                        if (o->data.tail_entry_offset == 0)
+                                return 0;
+
+                        p = le64toh(o->data.tail_entry_offset);
+                        r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
+                        if (r < 0)
+                                return r;
+
+                        if (ret)
+                                *ret = o;
+
+                        if (offset)
+                                *offset = p;
+
+                        return 1;
+                }
+
+                p = le64toh(o->data.prev_hash_offset);
+        }
+
+        return 0;
+}
+
+void journal_file_dump(JournalFile *f) {
+        char a[33], b[33], c[33];
+        Object *o;
+        int r;
+        uint64_t p;
+
+        assert(f);
+
+        printf("File ID: %s\n"
+               "Machine ID: %s\n"
+               "Boot ID: %s\n"
+               "Arena size: %llu\n",
+               sd_id128_to_string(f->header->file_id, a),
+               sd_id128_to_string(f->header->machine_id, b),
+               sd_id128_to_string(f->header->boot_id, c),
+               (unsigned long long) le64toh(f->header->arena_size));
+
+        p = le64toh(f->header->head_object_offset);
+        while (p != 0) {
+                r = journal_file_move_to_object(f, p, -1, &o);
+                if (r < 0)
+                        goto fail;
+
+                switch (o->object.type) {
+
+                case OBJECT_UNUSED:
+                        printf("Type: OBJECT_UNUSED\n");
+                        break;
+
+                case OBJECT_DATA:
+                        printf("Type: OBJECT_DATA\n");
+                        break;
+
+                case OBJECT_ENTRY:
+                        printf("Type: OBJECT_ENTRY %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
+                        break;
+
+                case OBJECT_HASH_TABLE:
+                        printf("Type: OBJECT_HASH_TABLE\n");
+                        break;
+
+                case OBJECT_BISECT_TABLE:
+                        printf("Type: OBJECT_BISECT_TABLE\n");
+                        break;
+                }
+
+                if (p == le64toh(f->header->tail_object_offset))
+                        p = 0;
+                else
+                        p = p + ALIGN64(le64toh(o->object.size));
+        }
+
+        return;
+fail:
+        log_error("File corrupt");
+}
+
+int journal_file_open(
+                const char *fname,
+                int flags,
+                mode_t mode,
+                JournalFile **ret) {
+
+        JournalFile *f;
+        int r;
+        bool newly_created = false;
+
+        assert(fname);
+
+        if ((flags & O_ACCMODE) != O_RDONLY &&
+            (flags & O_ACCMODE) != O_RDWR)
+                return -EINVAL;
+
+        f = new0(JournalFile, 1);
+        if (!f)
+                return -ENOMEM;
+
+        f->writable = (flags & O_ACCMODE) != O_RDONLY;
+        f->prot = prot_from_flags(flags);
+
+        f->fd = open(fname, flags|O_CLOEXEC, mode);
+        if (f->fd < 0) {
+                r = -errno;
+                goto fail;
+        }
+
+        f->path = strdup(fname);
+        if (!f->path) {
+                r = -ENOMEM;
+                goto fail;
+        }
+
+        if (fstat(f->fd, &f->last_stat) < 0) {
+                r = -errno;
+                goto fail;
+        }
+
+        if (f->last_stat.st_size == 0 && f->writable) {
+                newly_created = true;
+
+                r = journal_file_init_header(f);
+                if (r < 0)
+                        goto fail;
+
+                if (fstat(f->fd, &f->last_stat) < 0) {
+                        r = -errno;
+                        goto fail;
+                }
+        }
+
+        if (f->last_stat.st_size < (off_t) sizeof(Header)) {
+                r = -EIO;
+                goto fail;
+        }
+
+        f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
+        if (f->header == MAP_FAILED) {
+                f->header = NULL;
+                r = -errno;
+                goto fail;
+        }
+
+        if (!newly_created) {
+                r = journal_file_verify_header(f);
+                if (r < 0)
+                        goto fail;
+        }
+
+        if (f->writable) {
+                r = journal_file_refresh_header(f);
+                if (r < 0)
+                        goto fail;
+        }
+
+        if (newly_created) {
+
+                r = journal_file_setup_hash_table(f);
+                if (r < 0)
+                        goto fail;
+
+                r = journal_file_setup_bisect_table(f);
+                if (r < 0)
+                        goto fail;
+        }
+
+        r = journal_file_map_hash_table(f);
+        if (r < 0)
+                goto fail;
+
+        r = journal_file_map_bisect_table(f);
+        if (r < 0)
+                goto fail;
+
+        if (ret)
+                *ret = f;
+
+        return 0;
+
+fail:
+        journal_file_close(f);
+
+        return r;
+}
similarity index 92%
rename from src/journal/journal-private.h
rename to src/journal/journal-file.h
index 3277d2954277626e38e5ae0fe77dc0b13f660037..55cc7153af1f9e50450d08efe8a4f2a42a476860 100644 (file)
@@ -1,7 +1,7 @@
 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
-#ifndef foojournalprivatehfoo
-#define foojournalprivatehfoo
+#ifndef foojournalfilehfoo
+#define foojournalfilehfoo
 
 /***
   This file is part of systemd.
@@ -24,7 +24,6 @@
 
 #include <inttypes.h>
 
-#include "sd-journal.h"
 #include "journal-def.h"
 #include "util.h"
 #include "sd-id128.h"
@@ -50,24 +49,25 @@ typedef struct JournalFile {
         uint64_t window_offset;
         uint64_t window_size;
 
-        Object *current;
         uint64_t current_offset;
 } JournalFile;
 
-typedef struct JournalCoursor {
-        sd_id128_t file_id;
-        sd_id128_t boot_id;
+typedef struct JournalCursor {
+        uint8_t version;
+        uint8_t reserved[7];
         uint64_t seqnum;
+        sd_id128_t seqnum_id;
+        sd_id128_t boot_id;
         uint64_t monotonic;
         uint64_t realtime;
         uint64_t xor_hash;
-} JournalCoursor;
+} JournalCursor;
 
 int journal_file_open(const char *fname, int flags, mode_t mode, JournalFile **ret);
 
 void journal_file_close(JournalFile *j);
 
-int journal_file_move_to_object(JournalFile *f, uint64_t offset, Object **ret);
+int journal_file_move_to_object(JournalFile *f, uint64_t offset, int type, Object **ret);
 
 uint64_t journal_file_entry_n_items(Object *o);
 
index 7bcd842f6db5db8d3a2a1c8c3b19497210a00512..5f17f45cac9e4a96ad490b0f375e66828ac0616f 100644 (file)
@@ -23,7 +23,7 @@
 #include <errno.h>
 #include <stddef.h>
 
-#include "journal-private.h"
+#include "journal-file.h"
 
 int main(int argc, char *argv[]) {
         int r;
@@ -62,21 +62,16 @@ int main(int argc, char *argv[]) {
                         uint64_t p, l;
 
                         p = le64toh(o->entry.items[i].object_offset);
-                        r = journal_file_move_to_object(f, p, &o);
+                        r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
                         if (r < 0) {
                                 log_error("Failed to move to data: %s", strerror(-r));
                                 goto finish;
                         }
 
-                        if (le64toh(o->object.type) != OBJECT_DATA) {
-                                log_error("Invalid file");
-                                goto finish;
-                        }
-
                         l = o->object.size - offsetof(Object, data.payload);
                         printf("\t[%.*s]\n", (int) l, o->data.payload);
 
-                        r = journal_file_move_to_object(f, offset, &o);
+                        r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
                         if (r < 0) {
                                 log_error("Failed to move back to entry: %s", strerror(-r));
                                 goto finish;
index e9ac3a832e7b19d93037c0d74b314d22d2fa14d8..d65451df581913267c44e256986252dca4de7c85 100644 (file)
@@ -29,7 +29,7 @@
 #include <acl/libacl.h>
 
 #include "hashmap.h"
-#include "journal-private.h"
+#include "journal-file.h"
 #include "sd-daemon.h"
 #include "socket-util.h"
 #include "acl-util.h"
@@ -282,7 +282,9 @@ static int process_event(Server *s, struct epoll_event *ev) {
                 log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
                 return 0;
 
-        } else {
+        }
+
+        if (ev->data.fd == s->syslog_fd) {
                 for (;;) {
                         char buf[LINE_MAX+1];
                         struct msghdr msghdr;
@@ -339,9 +341,12 @@ static int process_event(Server *s, struct epoll_event *ev) {
 
                         process_message(s, strstrip(buf), ucred, tv);
                 }
+
+                return 1;
         }
 
-        return 1;
+        log_error("Unknown event.");
+        return 0;
 }
 
 static int server_init(Server *s) {
index 89bf545837ceb811cba68bb95805acd590723bae..8426b3bf9e232fe098f671ad3292bb8d10c13e3e 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <sys/mman.h>
 #include <errno.h>
-#include <sys/uio.h>
-#include <unistd.h>
-#include <sys/statvfs.h>
 #include <fcntl.h>
-#include <stddef.h>
 
 #include "sd-journal.h"
 #include "journal-def.h"
-#include "journal-private.h"
-#include "lookup3.h"
-#include "list.h"
+#include "journal-file.h"
 #include "hashmap.h"
+#include "list.h"
 
-#define DEFAULT_ARENA_MAX_SIZE (16ULL*1024ULL*1024ULL*1024ULL)
-#define DEFAULT_ARENA_MIN_SIZE (256ULL*1024ULL)
-#define DEFAULT_ARENA_KEEP_FREE (1ULL*1024ULL*1024ULL)
+typedef struct Match Match;
 
-#define DEFAULT_HASH_TABLE_SIZE (2047ULL*16ULL)
-#define DEFAULT_BISECT_TABLE_SIZE ((DEFAULT_ARENA_MAX_SIZE/(64ULL*1024ULL))*8ULL)
+struct Match {
+        char *data;
+        size_t size;
+        uint64_t hash;
 
-#define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
+        LIST_FIELDS(Match, matches);
+};
 
 struct sd_journal {
         Hashmap *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->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);
+        JournalFile *current_file;
 
-        r = journal_file_move_to(f, offset, sizeof(ObjectHeader), &t);
-        if (r < 0)
-                return r;
+        LIST_HEAD(Match, matches);
+};
 
-        o = (Object*) t;
-        s = le64toh(o->object.size);
+int sd_journal_add_match(sd_journal *j, const char *field, const void *data, size_t size) {
+        Match *m;
+        char *e;
 
-        if (s < sizeof(ObjectHeader))
-                return -EBADMSG;
+        assert(j);
+        assert(field);
+        assert(data || size == 0);
 
-        if (s > sizeof(ObjectHeader)) {
-                r = journal_file_move_to(f, offset, s, &t);
-                if (r < 0)
-                        return r;
+        m = new0(Match, 1);
+        if (!m)
+                return -ENOMEM;
 
-                o = (Object*) t;
+        m->size = strlen(field) + 1 + size;
+        m->data = malloc(m->size);
+        if (!m->data) {
+                free(m);
+                return -ENOMEM;
         }
 
-        if (!verify_hash(o))
-                return -EBADMSG;
+        e = stpcpy(m->data, field);
+        *(e++) = '=';
+        memcpy(e, data, size);
 
-        *ret = o;
+        LIST_PREPEND(Match, matches, j->matches, m);
         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);
+void sd_journal_flush_matches(sd_journal *j) {
+        assert(j);
 
-        if (p == 0)
-                p = le64toh(f->header->arena_offset);
-        else {
-                r = journal_file_move_to_object(f, p, &tail);
-                if (r < 0)
-                        return r;
+        while (j->matches) {
+                Match *m = j->matches;
 
-                p += ALIGN64(le64toh(tail->object.size));
+                LIST_REMOVE(Match, matches, j->matches, m);
+                free(m->data);
+                free(m);
         }
-
-        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);
+static int compare_order(JournalFile *af, Object *ao, uint64_t ap,
+                            JournalFile *bf, Object *bo, uint64_t bp) {
 
-        r = journal_file_map(f,
-                             p, s,
-                             &f->bisect_table_window, NULL, &f->bisect_table_window_size,
-                             &t);
-
-        if (r < 0)
-                return r;
+        uint64_t a, b;
 
-        f->bisect_table = t;
-        return 0;
-}
+        if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
 
-static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash_index) {
-        uint64_t p;
-        int r;
+                /* If this is from the same seqnum source, compare
+                 * seqnums */
+                a = le64toh(ao->entry.seqnum);
+                b = le64toh(bo->entry.seqnum);
 
-        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;
+        } else if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
 
-        p = le64toh(f->hash_table[hash_index].tail_hash_offset);
-        if (p == 0) {
-                /* Only entry in the hash table is easy */
+                /* If the boot id matches compare monotonic time */
+                a = le64toh(ao->entry.monotonic);
+                b = le64toh(bo->entry.monotonic);
 
-                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;
+                /* Otherwise compare UTC time */
+                a = le64toh(ao->entry.realtime);
+                b = le64toh(ao->entry.realtime);
         }
 
-        f->hash_table[hash_index].tail_hash_offset = htole64(offset);
-
-        return 0;
+        return
+                a < b ? -1 :
+                a > b ? +1 : 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 sd_journal_next(sd_journal *j) {
+        JournalFile *f, *new_current = NULL;
+        Iterator i;
         int r;
+        uint64_t new_offset = 0;
+        Object *new_entry = NULL;
 
-        assert(f);
-        assert(data || size == 0);
-
-        osize = offsetof(Object, data.payload) + size;
+        assert(j);
 
-        hash = hash64(data, size);
-        h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
-        p = le64toh(f->hash_table[h].head_hash_offset);
+        HASHMAP_FOREACH(f, j->files, i) {
+                Object *o;
+                uint64_t p;
 
-        while (p != 0) {
-                /* Look for this data object in the hash table */
+                if (f->current_offset > 0) {
+                        r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
+                        if (r < 0)
+                                return r;
+                } else
+                        o = NULL;
 
-                r = journal_file_move_to_object(f, p, &o);
+                r = journal_file_next_entry(f, o, &o, &p);
                 if (r < 0)
                         return r;
+                else if (r == 0)
+                        continue;
 
-                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;
+                if (!new_current || compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
+                        new_current = f;
+                        new_entry = o;
+                        new_offset = p;
                 }
-
-                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;
-
-        q = le64toh(o->data.tail_entry_offset);
-        o->data.tail_entry_offset = htole64(offset);
-
-        if (q == 0)
-                o->data.head_entry_offset = htole64(offset);
-        else {
-                uint64_t n, j;
-
-                /* Move to previous entry */
-                r = journal_file_move_to_object(f, q, &o);
-                if (r < 0)
-                        return r;
-
-                if (o->object.type != htole64(OBJECT_ENTRY))
-                        return -EBADMSG;
-
-                n = journal_file_entry_n_items(o);
-                for (j = 0; j < n; j++)
-                        if (le64toh(o->entry.items[j].object_offset) == p)
-                                break;
-
-                if (j >= n)
-                        return -EBADMSG;
-
-                o->entry.items[j].next_entry_offset = offset;
-        }
-
-        /* Move back to original entry */
-        r = journal_file_move_to_object(f, offset, &o);
-        if (r < 0)
-                return r;
-
-        o->entry.items[i].prev_entry_offset = q;
-        return 0;
-}
-
-static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
-        uint64_t p, i, n, k, a, b;
-        int r;
-
-        assert(f);
-        assert(o);
-        assert(offset > 0);
-        assert(o->object.type == htole64(OBJECT_ENTRY));
-
-        /* Link up the entry itself */
-        p = le64toh(f->header->tail_entry_offset);
-
-        o->entry.prev_entry_offset = f->header->tail_entry_offset;
-        o->entry.next_entry_offset = 0;
-
-        if (p == 0)
-                f->header->head_entry_offset = htole64(offset);
-        else {
-                /* Temporarily move back to the previous entry, to
-                 * patch in pointer */
-
-                r = journal_file_move_to_object(f, p, &o);
-                if (r < 0)
-                        return r;
-
-                o->entry.next_entry_offset = htole64(offset);
-
-                r = journal_file_move_to_object(f, offset, &o);
-                if (r < 0)
-                        return r;
-        }
-
-        f->header->tail_entry_offset = htole64(offset);
-
-        /* Link up the items */
-        n = journal_file_entry_n_items(o);
-        for (i = 0; i < n; i++) {
-                r = journal_file_link_entry_item(f, o, offset, i);
-                if (r < 0)
-                        return r;
+        if (new_current) {
+                j->current_file = new_current;
+                f->current_offset = new_offset;
+                return 1;
         }
 
-        /* Link up the entry in the bisect table */
-        n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
-        k = le64toh(f->header->arena_max_size) / n;
-
-        a = (le64toh(f->header->last_bisect_offset) + k - 1) / k;
-        b = offset / k;
-
-        for (; a <= b; a++)
-                f->bisect_table[a] = htole64(offset);
-
-        f->header->last_bisect_offset = htole64(offset + le64toh(o->object.size));
-
         return 0;
 }
 
-static int journal_file_append_entry_internal(
-                JournalFile *f,
-                const dual_timestamp *ts,
-                uint64_t xor_hash,
-                const EntryItem items[], unsigned n_items,
-                Object **ret, uint64_t *offset) {
-        uint64_t np;
-        uint64_t osize;
-        Object *o;
+int sd_journal_previous(sd_journal *j) {
+        JournalFile *f, *new_current = NULL;
+        Iterator i;
         int r;
+        uint64_t new_offset = 0;
+        Object *new_entry = NULL;
 
-        assert(f);
-        assert(items || n_items == 0);
-
-        osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
-
-        r = journal_file_append_object(f, osize, &o, &np);
-        if (r < 0)
-                return r;
-
-        o->object.type = htole64(OBJECT_ENTRY);
-        o->entry.seqnum = htole64(journal_file_seqnum(f));
-        memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
-        o->entry.realtime = ts ? htole64(ts->realtime) : 0;
-        o->entry.monotonic = ts ? htole64(ts->monotonic) : 0;
-        o->entry.xor_hash = htole64(xor_hash);
-
-        r = journal_file_link_entry(f, o, np);
-        if (r < 0)
-                return r;
-
-        if (ret)
-                *ret = o;
-
-        if (offset)
-                *offset = np;
-
-        return 0;
-}
-
-int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset) {
-        unsigned i;
-        EntryItem *items;
-        int r;
-        uint64_t xor_hash = 0;
-
-        assert(f);
-        assert(iovec || n_iovec == 0);
-
-        items = new(EntryItem, n_iovec);
-        if (!items)
-                return -ENOMEM;
+        assert(j);
 
-        for (i = 0; i < n_iovec; i++) {
-                uint64_t p;
+        HASHMAP_FOREACH(f, j->files, i) {
                 Object *o;
+                uint64_t p;
 
-                r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
-                if (r < 0)
-                        goto finish;
-
-                xor_hash ^= le64toh(o->data.hash);
-                items[i].object_offset = htole64(p);
-        }
-
-        r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, ret, offset);
-
-finish:
-        free(items);
-
-        return r;
-}
-
-int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset) {
-        Object *o;
-        uint64_t lower, upper, p, n, k;
-        int r;
-
-        assert(f);
-
-        n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
-        k = le64toh(f->header->arena_max_size) / n;
-
-        lower = 0;
-        upper = le64toh(f->header->last_bisect_offset)/k+1;
-
-        while (lower < upper) {
-                k = (upper + lower) / 2;
-                p = le64toh(f->bisect_table[k]);
-
-                if (p == 0) {
-                        upper = k;
-                        continue;
-                }
+                if (f->current_offset > 0) {
+                        r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &o);
+                        if (r < 0)
+                                return r;
+                } else
+                        o = NULL;
 
-                r = journal_file_move_to_object(f, p, &o);
+                r = journal_file_prev_entry(f, o, &o, &p);
                 if (r < 0)
                         return r;
+                else if (r == 0)
+                        continue;
 
-                if (o->object.type != htole64(OBJECT_ENTRY))
-                        return -EBADMSG;
-
-                if (o->entry.seqnum == seqnum) {
-                        if (ret)
-                                *ret = o;
-
-                        if (offset)
-                                *offset = p;
-
-                        return 1;
-                } else if (seqnum < o->entry.seqnum)
-                        upper = k;
-                else if (seqnum > o->entry.seqnum)
-                        lower = k+1;
+                if (!new_current || compare_order(new_current, new_entry, new_offset, f, o, p) > 0) {
+                        new_current = f;
+                        new_entry = o;
+                        new_offset = p;
+                }
         }
 
-        assert(lower == upper);
-
-        if (lower <= 0)
-                return 0;
-
-        /* The object we are looking for is between
-         * bisect_table[lower-1] and bisect_table[lower] */
-
-        p = le64toh(f->bisect_table[lower-1]);
-
-        for (;;) {
-                r = journal_file_move_to_object(f, p, &o);
-                if (r < 0)
-                        return r;
-
-                if (o->entry.seqnum == seqnum) {
-                        if (ret)
-                                *ret = o;
-
-                        if (offset)
-                                *offset = p;
-
-                        return 1;
-
-                } if (seqnum < o->entry.seqnum)
-                        return 0;
-
-                if (o->entry.next_entry_offset == 0)
-                        return 0;
-
-                p = le64toh(o->entry.next_entry_offset);
+        if (new_current) {
+                j->current_file = new_current;
+                f->current_offset = new_offset;
+                return 1;
         }
 
         return 0;
 }
 
-int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
-        uint64_t np;
-        int r;
-
-        assert(f);
-
-        if (!o)
-                np = le64toh(f->header->head_entry_offset);
-        else {
-                if (le64toh(o->object.type) != OBJECT_ENTRY)
-                        return -EINVAL;
-
-                np = le64toh(o->entry.next_entry_offset);
-        }
-
-        if (np == 0)
-                return 0;
-
-        r = journal_file_move_to_object(f, np, &o);
-        if (r < 0)
-                return r;
-
-        if (le64toh(o->object.type) != OBJECT_ENTRY)
-                return -EBADMSG;
-
-        if (ret)
-                *ret = o;
-
-        if (offset)
-                *offset = np;
-
-        return 1;
-}
-
-int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
-        uint64_t np;
+int sd_journal_get_cursor(sd_journal *j, void **cursor, size_t *size) {
+        JournalCursor *c;
+        Object *o;
         int r;
 
-        assert(f);
-
-        if (!o)
-                np = le64toh(f->header->tail_entry_offset);
-        else {
-                if (le64toh(o->object.type) != OBJECT_ENTRY)
-                        return -EINVAL;
-
-                np = le64toh(o->entry.prev_entry_offset);
-        }
+        assert(j);
+        assert(cursor);
+        assert(size);
 
-        if (np == 0)
+        if (!j->current_file || !j->current_file->current_offset <= 0)
                 return 0;
 
-        r = journal_file_move_to_object(f, np, &o);
+        r = journal_file_move_to_object(j->current_file, j->current_file->current_offset, OBJECT_ENTRY, &o);
         if (r < 0)
                 return r;
 
-        if (le64toh(o->object.type) != OBJECT_ENTRY)
-                return -EBADMSG;
+        c = new0(JournalCursor, 1);
+        if (!c)
+                return -ENOMEM;
 
-        if (ret)
-                *ret = o;
+        c->version = 1;
+        c->seqnum = o->entry.seqnum;
+        c->seqnum_id = j->current_file->header->seqnum_id;
+        c->boot_id = o->entry.boot_id;
+        c->monotonic = o->entry.monotonic;
+        c->realtime = o->entry.realtime;
+        c->xor_hash = o->entry.xor_hash;
 
-        if (offset)
-                *offset = np;
+        *cursor = c;
+        *size = sizeof(JournalCursor);
 
         return 1;
 }
 
-int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
-        uint64_t p, osize, hash, h;
-        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) {
-                Object *o;
-
-                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 (o->data.head_entry_offset == 0)
-                                return 0;
-
-                        p = le64toh(o->data.head_entry_offset);
-                        r = journal_file_move_to_object(f, p, &o);
-                        if (r < 0)
-                                return r;
-
-                        if (le64toh(o->object.type) != OBJECT_ENTRY)
-                                return -EBADMSG;
-
-                        if (ret)
-                                *ret = o;
-
-                        if (offset)
-                                *offset = p;
-
-                        return 1;
-                }
-
-                p = le64toh(o->data.next_hash_offset);
-        }
-
-        return 0;
-}
-
-int journal_file_find_last_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
-        uint64_t p, osize, hash, h;
-        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].tail_hash_offset);
-
-        while (p != 0) {
-                Object *o;
-
-                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 (o->data.tail_entry_offset == 0)
-                                return 0;
-
-                        p = le64toh(o->data.tail_entry_offset);
-                        r = journal_file_move_to_object(f, p, &o);
-                        if (r < 0)
-                                return r;
-
-                        if (le64toh(o->object.type) != OBJECT_ENTRY)
-                                return -EBADMSG;
-
-                        if (ret)
-                                *ret = o;
-
-                        if (offset)
-                                *offset = p;
-
-                        return 1;
-                }
-
-                p = le64toh(o->data.prev_hash_offset);
-        }
-
-        return 0;
-}
-
-void journal_file_dump(JournalFile *f) {
-        char a[33], b[33], c[33];
-        Object *o;
-        int r;
-        uint64_t p;
-
-        assert(f);
-
-        printf("File ID: %s\n"
-               "Machine ID: %s\n"
-               "Boot ID: %s\n"
-               "Arena size: %llu\n",
-               sd_id128_to_string(f->header->file_id, a),
-               sd_id128_to_string(f->header->machine_id, b),
-               sd_id128_to_string(f->header->boot_id, c),
-               (unsigned long long) le64toh(f->header->arena_size));
-
-        p = le64toh(f->header->head_object_offset);
-        while (p != 0) {
-                r = journal_file_move_to_object(f, p, &o);
-                if (r < 0)
-                        goto fail;
-
-                switch (o->object.type) {
-
-                case OBJECT_UNUSED:
-                        printf("Type: OBJECT_UNUSED\n");
-                        break;
-
-                case OBJECT_DATA:
-                        printf("Type: OBJECT_DATA\n");
-                        break;
-
-                case OBJECT_ENTRY:
-                        printf("Type: OBJECT_ENTRY %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
-                        break;
-
-                case OBJECT_HASH_TABLE:
-                        printf("Type: OBJECT_HASH_TABLE\n");
-                        break;
-
-                case OBJECT_BISECT_TABLE:
-                        printf("Type: OBJECT_BISECT_TABLE\n");
-                        break;
-                }
-
-                if (p == le64toh(f->header->tail_object_offset))
-                        p = 0;
-                else
-                        p = p + ALIGN64(le64toh(o->object.size));
-        }
-
-        return;
-fail:
-        log_error("File corrupt");
-}
-
-int journal_file_open(
-                const char *fname,
-                int flags,
-                mode_t mode,
-                JournalFile **ret) {
-
-        JournalFile *f;
-        int r;
-        bool newly_created = false;
-
-        assert(fname);
-
-        if ((flags & O_ACCMODE) != O_RDONLY &&
-            (flags & O_ACCMODE) != O_RDWR)
-                return -EINVAL;
-
-        f = new0(JournalFile, 1);
-        if (!f)
-                return -ENOMEM;
-
-        f->writable = (flags & O_ACCMODE) != O_RDONLY;
-        f->prot = prot_from_flags(flags);
-
-        f->fd = open(fname, flags|O_CLOEXEC, mode);
-        if (f->fd < 0) {
-                r = -errno;
-                goto fail;
-        }
-
-        f->path = strdup(fname);
-        if (!f->path) {
-                r = -ENOMEM;
-                goto fail;
-        }
-
-        if (fstat(f->fd, &f->last_stat) < 0) {
-                r = -errno;
-                goto fail;
-        }
-
-        if (f->last_stat.st_size == 0 && f->writable) {
-                newly_created = true;
-
-                r = journal_file_init_header(f);
-                if (r < 0)
-                        goto fail;
-
-                if (fstat(f->fd, &f->last_stat) < 0) {
-                        r = -errno;
-                        goto fail;
-                }
-        }
-
-        if (f->last_stat.st_size < (off_t) sizeof(Header)) {
-                r = -EIO;
-                goto fail;
-        }
-
-        f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
-        if (f->header == MAP_FAILED) {
-                f->header = NULL;
-                r = -errno;
-                goto fail;
-        }
-
-        if (!newly_created) {
-                r = journal_file_verify_header(f);
-                if (r < 0)
-                        goto fail;
-        }
-
-        if (f->writable) {
-                r = journal_file_refresh_header(f);
-                if (r < 0)
-                        goto fail;
-        }
-
-        if (newly_created) {
-
-                r = journal_file_setup_hash_table(f);
-                if (r < 0)
-                        goto fail;
-
-                r = journal_file_setup_bisect_table(f);
-                if (r < 0)
-                        goto fail;
-        }
-
-        r = journal_file_map_hash_table(f);
-        if (r < 0)
-                goto fail;
-
-        r = journal_file_map_bisect_table(f);
-        if (r < 0)
-                goto fail;
-
-        if (ret)
-                *ret = f;
-
-        return 0;
-
-fail:
-        journal_file_close(f);
-
-        return r;
+int sd_journal_set_cursor(sd_journal *j, const void *cursor, size_t size) {
+        return -EINVAL;
 }
 
 int sd_journal_open(sd_journal **ret) {
index 8170dea87c7f02e879e0bf982830afc74762e9f9..55e58601fe9843905a0b83750a4d099580972094 100644 (file)
 #include <inttypes.h>
 #include <sys/types.h>
 
-#include "sd-id128.h"
-
 /* TODO:
  *
  *   - implement rotation
  *   - check LE/BE conversion for 8bit, 16bit, 32bit values
  *   - implement parallel traversal
+ *   - implement inotify usage on client
  *   - implement audit gateway
  *   - implement native gateway
  *   - extend hash table/bisect table as we go
@@ -45,12 +44,13 @@ void sd_journal_close(sd_journal *j);
 int sd_journal_previous(sd_journal *j);
 int sd_journal_next(sd_journal *j);
 
-void* sd_journal_get(sd_journal *j, const char *field, size_t *size);
-uint64_t sd_journal_get_seqnum(sd_journal *j);
-uint64_t sd_journal_get_realtime_usec(sd_journal *j);
-uint64_t sd_journal_get_monotonic_usec(sd_journal *j);
+int sd_journal_get(sd_journal *j, const char *field, const void **data, size_t *size);
+int sd_journal_get_seqnum(sd_journal *j, uint64_t *ret);
+int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret);
+int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret);
 
-int sd_journal_add_match(sd_journal *j, const char *item, size_t *size);
+int sd_journal_add_match(sd_journal *j, const char *field, const void *data, size_t size);
+void sd_journal_flush_matches(sd_journal *j);
 
 int sd_journal_seek_head(sd_journal *j);
 int sd_journal_seek_tail(sd_journal *j);
@@ -59,16 +59,9 @@ int sd_journal_seek_seqnum(sd_journal *j, uint64_t seqnum);
 int sd_journal_seek_monotonic_usec(sd_journal *j, uint64_t usec);
 int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec);
 
-uint64_t sd_journal_get_max_size(sd_journal *j);
-uint64_t sd_journal_get_min_size(sd_journal *j);
-uint64_t sd_journal_get_keep_free(sd_journal *j);
-
-int sd_journal_set_max_size(sd_journal *j, uint64_t size);
-int sd_journal_set_min_size(sd_journal *j, uint64_t size);
-int sd_journal_set_keep_free(sd_journal *j, uint64_t size);
+int sd_journal_get_cursor(sd_journal *j, void **cursor, size_t *size);
+int sd_journal_set_cursor(sd_journal *j, const void *cursor, size_t size);
 
-sd_id128_t sd_journal_get_file_id(sd_journal *j);
-sd_id128_t sd_journal_get_machine_id(sd_journal *j);
-sd_id128_t sd_journal_get_boot_id(sd_journal *j);
+int sd_journal_get_fd(sd_journal *j);
 
 #endif
index e0aedc7b834dcc40b1cfce30631e91587f4ffda9..7028f11f7c283743d89c544c0c5a0e3cb1cf18c2 100644 (file)
@@ -21,7 +21,7 @@
 
 #include <fcntl.h>
 
-#include "journal-private.h"
+#include "journal-file.h"
 #include "log.h"
 
 int main(int argc, char *argv[]) {