-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;