/* Lightweight object checks. We want this to be fast, so that we won't
* slowdown every journal_file_move_to_object() call too much. */
-static int check_object(Object *o, uint64_t offset) {
+static int check_object(JournalFile *f, Object *o, uint64_t offset) {
assert(o);
switch (o->object.type) {
sz = le64toh(READ_NOW(o->object.size));
if (sz < offsetof(Object, entry_array.items) ||
- (sz - offsetof(Object, entry_array.items)) % sizeof(le64_t) != 0 ||
- (sz - offsetof(Object, entry_array.items)) / sizeof(le64_t) <= 0)
+ (sz - offsetof(Object, entry_array.items)) % journal_file_entry_array_item_size(f) != 0 ||
+ (sz - offsetof(Object, entry_array.items)) / journal_file_entry_array_item_size(f) <= 0)
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
"Invalid object entry array size: %" PRIu64 ": %" PRIu64,
sz,
if (r < 0)
return r;
- r = check_object(o, offset);
+ r = check_object(f, o, offset);
if (r < 0)
return r;
"Short read while reading object: %" PRIu64,
offset);
- r = check_object(&o, offset);
+ r = check_object(f, &o, offset);
if (r < 0)
return r;
return (sz - offsetof(Object, entry.items)) / sizeof(EntryItem);
}
-uint64_t journal_file_entry_array_n_items(Object *o) {
+uint64_t journal_file_entry_array_n_items(JournalFile *f, Object *o) {
uint64_t sz;
assert(o);
if (sz < offsetof(Object, entry_array.items))
return 0;
- return (sz - offsetof(Object, entry_array.items)) / sizeof(uint64_t);
+ return (sz - offsetof(Object, entry_array.items)) / journal_file_entry_array_item_size(f);
}
uint64_t journal_file_hash_table_n_items(Object *o) {
return (sz - offsetof(Object, hash_table.items)) / sizeof(HashItem);
}
+static void write_entry_array_item(JournalFile *f, Object *o, uint64_t i, uint64_t p) {
+ assert(f);
+ assert(o);
+
+ if (JOURNAL_HEADER_COMPACT(f->header)) {
+ assert(p <= UINT32_MAX);
+ o->entry_array.items.compact[i] = htole32(p);
+ } else
+ o->entry_array.items.regular[i] = htole64(p);
+}
+
static int link_entry_into_array(JournalFile *f,
le64_t *first,
le64_t *idx,
if (r < 0)
return r;
- n = journal_file_entry_array_n_items(o);
+ n = journal_file_entry_array_n_items(f, o);
if (i < n) {
- o->entry_array.items[i] = htole64(p);
+ write_entry_array_item(f, o, i, p);
*idx = htole64(hidx + 1);
return 0;
}
n = 4;
r = journal_file_append_object(f, OBJECT_ENTRY_ARRAY,
- offsetof(Object, entry_array.items) + n * sizeof(uint64_t),
+ offsetof(Object, entry_array.items) + n * journal_file_entry_array_item_size(f),
&o, &q);
if (r < 0)
return r;
return r;
#endif
- o->entry_array.items[i] = htole64(p);
+ write_entry_array_item(f, o, i, p);
if (ap == 0)
*first = htole64(q);
if (r < 0)
return r;
- k = journal_file_entry_array_n_items(o);
+ k = journal_file_entry_array_n_items(f, o);
if (i < k)
break;
if (r < 0)
return r;
- k = journal_file_entry_array_n_items(o);
+ k = journal_file_entry_array_n_items(f, o);
if (k == 0)
break;
}
do {
- p = le64toh(o->entry_array.items[i]);
+ p = journal_file_entry_array_item(f, o, i);
r = journal_file_move_to_object(f, OBJECT_ENTRY, p, ret);
if (r >= 0) {
/* Let's cache this item for the next invocation */
- chain_cache_put(f->chain_cache, ci, first, a, le64toh(o->entry_array.items[0]), t, i);
+ chain_cache_put(f->chain_cache, ci, first, a, journal_file_entry_array_item(f, o, 0), t, i);
if (ret_offset)
*ret_offset = p;
if (r < 0)
return r;
- k = journal_file_entry_array_n_items(array);
+ k = journal_file_entry_array_n_items(f, array);
right = MIN(k, n);
if (right <= 0)
return 0;
i = right - 1;
- lp = p = le64toh(array->entry_array.items[i]);
+ lp = p = journal_file_entry_array_item(f, array, i);
if (p <= 0)
r = -EBADMSG;
else
if (last_index > 0) {
uint64_t x = last_index - 1;
- p = le64toh(array->entry_array.items[x]);
+ p = journal_file_entry_array_item(f, array, x);
if (p <= 0)
return -EBADMSG;
if (last_index < right) {
uint64_t y = last_index + 1;
- p = le64toh(array->entry_array.items[y]);
+ p = journal_file_entry_array_item(f, array, y);
if (p <= 0)
return -EBADMSG;
assert(left < right);
i = (left + right) / 2;
- p = le64toh(array->entry_array.items[i]);
+ p = journal_file_entry_array_item(f, array, i);
if (p <= 0)
r = -EBADMSG;
else
return 0;
/* Let's cache this item for the next invocation */
- chain_cache_put(f->chain_cache, ci, first, a, le64toh(array->entry_array.items[0]), t, subtract_one ? (i > 0 ? i-1 : UINT64_MAX) : i);
+ chain_cache_put(f->chain_cache, ci, first, a, journal_file_entry_array_item(f, array, 0), t, subtract_one ? (i > 0 ? i-1 : UINT64_MAX) : i);
if (subtract_one && i == 0)
p = last_p;
else if (subtract_one)
- p = le64toh(array->entry_array.items[i-1]);
+ p = journal_file_entry_array_item(f, array, i - 1);
else
- p = le64toh(array->entry_array.items[i]);
+ p = journal_file_entry_array_item(f, array, i);
if (ret) {
r = journal_file_move_to_object(f, OBJECT_ENTRY, p, ret);
break;
case OBJECT_ENTRY_ARRAY:
- if ((le64toh(o->object.size) - offsetof(Object, entry_array.items)) % sizeof(le64_t) != 0 ||
- (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / sizeof(le64_t) <= 0) {
+ if ((le64toh(o->object.size) - offsetof(Object, entry_array.items)) % journal_file_entry_array_item_size(f) != 0 ||
+ (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / journal_file_entry_array_item_size(f) <= 0) {
error(offset,
"Invalid object entry array size: %"PRIu64,
le64toh(o->object.size));
return -EBADMSG;
}
- for (uint64_t i = 0; i < journal_file_entry_array_n_items(o); i++)
- if (le64toh(o->entry_array.items[i]) != 0 &&
- !VALID64(le64toh(o->entry_array.items[i]))) {
+ for (uint64_t i = 0; i < journal_file_entry_array_n_items(f, o); i++) {
+ uint64_t q = journal_file_entry_array_item(f, o, i);
+ if (q != 0 && !VALID64(q)) {
error(offset,
"Invalid object entry array item (%"PRIu64"/%"PRIu64"): "OFSfmt,
- i, journal_file_entry_array_n_items(o),
- le64toh(o->entry_array.items[i]));
+ i, journal_file_entry_array_n_items(f, o), q);
return -EBADMSG;
}
+ }
break;
return -EBADMSG;
}
- m = journal_file_entry_array_n_items(o);
+ m = journal_file_entry_array_n_items(f, o);
for (j = 0; i < n && j < m; i++, j++) {
- q = le64toh(o->entry_array.items[j]);
+ q = journal_file_entry_array_item(f, o, j);
if (q <= last) {
error(p, "Data object's entry array not sorted (%"PRIu64" <= %"PRIu64")", q, last);
return -EBADMSG;
return -EBADMSG;
}
- m = journal_file_entry_array_n_items(o);
+ m = journal_file_entry_array_n_items(f, o);
for (j = 0; i < n && j < m; i++, j++) {
uint64_t p;
- p = le64toh(o->entry_array.items[j]);
+ p = journal_file_entry_array_item(f, o, j);
if (p <= last) {
error(a, "Entry array not sorted at %"PRIu64" of %"PRIu64, i, n);
return -EBADMSG;