-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;
- }
-
- /* 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, 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 = htole64(ts->realtime);
- o->entry.monotonic = htole64(ts->monotonic);
-
- 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;
-
- assert(f);
-
- items = new(EntryItem, n_iovec);
- if (!items)
- return -ENOMEM;
-
- for (i = 0; i < n_iovec; i++) {
- uint64_t p;
-
- r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, NULL, &p);
- if (r < 0)
- goto finish;
-
- items[i].object_offset = htole64(p);
- }
-
- r = journal_file_append_entry_internal(f, ts, 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, &o);
- if (r < 0)
- return r;
-
- 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;
- }
-
- 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);
- }
-
- 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 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, &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_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(
- sd_journal *j,
- 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 (j) {
- LIST_PREPEND(JournalFile, files, j->files, f);
- f->journal = j;
- }
-
- if (ret)
- *ret = f;
-
- return 0;
-
-fail:
- journal_file_close(f);
-
- return r;