return 0;
}
-static uint64_t journal_file_seqnum(JournalFile *f) {
+static uint64_t journal_file_seqnum(JournalFile *f, uint64_t *seqnum) {
uint64_t r;
assert(f);
r = le64toh(f->header->seqnum) + 1;
+
+ if (seqnum) {
+ /* If an external seqno counter was passed, we update
+ * both the local and the external one, and set it to
+ * the maximum of both */
+
+ if (*seqnum + 1 > r)
+ r = *seqnum + 1;
+
+ *seqnum = r;
+ }
+
f->header->seqnum = htole64(r);
return r;
const dual_timestamp *ts,
uint64_t xor_hash,
const EntryItem items[], unsigned n_items,
+ uint64_t *seqno,
Object **ret, uint64_t *offset) {
uint64_t np;
uint64_t osize;
return r;
o->object.type = htole64(OBJECT_ENTRY);
- o->entry.seqnum = htole64(journal_file_seqnum(f));
+ o->entry.seqnum = htole64(journal_file_seqnum(f, seqno));
memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
o->entry.realtime = htole64(ts ? ts->realtime : now(CLOCK_REALTIME));
o->entry.monotonic = htole64(ts ? ts->monotonic : now(CLOCK_MONOTONIC));
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) {
+int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqno, Object **ret, uint64_t *offset) {
unsigned i;
EntryItem *items;
int r;
xor_hash ^= le64toh(o->data.hash);
items[i].object_offset = htole64(p);
+ items[i].hash = o->data.hash;
}
- r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, ret, offset);
+ r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqno, ret, offset);
finish:
free(items);
return 0;
}
-int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
+int journal_file_next_entry(JournalFile *f, Object *o, direction_t direction, Object **ret, uint64_t *offset) {
uint64_t np;
int r;
assert(f);
if (!o)
- np = le64toh(f->header->head_entry_offset);
+ np = le64toh(direction == DIRECTION_DOWN ?
+ f->header->head_entry_offset :
+ f->header->tail_entry_offset);
else {
if (le64toh(o->object.type) != OBJECT_ENTRY)
return -EINVAL;
- np = le64toh(o->entry.next_entry_offset);
+ np = le64toh(direction == DIRECTION_DOWN ?
+ o->entry.next_entry_offset :
+ o->entry.prev_entry_offset);
}
if (np == 0)
return 1;
}
-int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
+int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, direction_t direction, Object **ret, uint64_t *offset) {
uint64_t p, osize, hash, h;
int r;
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;
+ p = le64toh(direction == DIRECTION_DOWN ?
+ o->data.head_entry_offset :
+ o->data.tail_entry_offset);
- 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)
+ if (p == 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;
return 1;
}
- p = le64toh(o->data.prev_hash_offset);
+ p = le64toh(o->data.next_hash_offset);
}
return 0;