journal-file: journal-file: extend journal header to always carry offset of most recent entry

This way we can quickly find the most recent entry, without searching or
traversing entry array chains.

This is relevant later, as it it allows us to quickly determine the most
recent timestamps of each journal file, in a roughly atomic way.
This commit is contained in:
Lennart Poettering
2023-01-31 19:20:27 +01:00
parent 7a67afe331
commit 206f0f397e
3 changed files with 16 additions and 3 deletions

View File

@@ -178,8 +178,10 @@ _packed_ struct Header {
le64_t data_hash_chain_depth;
le64_t field_hash_chain_depth;
/* Added in 252 */
le32_t tail_entry_array_offset; \
le32_t tail_entry_array_n_entries; \
le32_t tail_entry_array_offset;
le32_t tail_entry_array_n_entries;
/* Added in 254 */
le64_t tail_entry_offset;
};
```
@@ -252,6 +254,9 @@ field hash table, minus one.
**tail_entry_array_offset** and **tail_entry_array_n_entries** allow immediate
access to the last entry array in the global entry array chain.
**tail_entry_offset** allow immediate access to the last entry in the journal
file.
## Extensibility
The format is supposed to be extensible in order to enable future additions of

View File

@@ -239,12 +239,14 @@ enum {
/* Added in 252 */ \
le32_t tail_entry_array_offset; \
le32_t tail_entry_array_n_entries; \
/* Added in 254 */ \
le64_t tail_entry_offset; \
}
struct Header struct_Header__contents;
struct Header__packed struct_Header__contents _packed_;
assert_cc(sizeof(struct Header) == sizeof(struct Header__packed));
assert_cc(sizeof(struct Header) == 264);
assert_cc(sizeof(struct Header) == 272);
#define FSS_HEADER_SIGNATURE \
((const char[]) { 'K', 'S', 'H', 'H', 'R', 'H', 'L', 'P' })

View File

@@ -505,6 +505,11 @@ static int journal_file_verify_header(JournalFile *f) {
!VALID64(le64toh(f->header->entry_array_offset)))
return -ENODATA;
if (JOURNAL_HEADER_CONTAINS(f->header, tail_entry_offset) &&
le64toh(f->header->tail_entry_offset) != 0 &&
!VALID64(le64toh(f->header->tail_entry_offset)))
return -ENODATA;
if (journal_file_writable(f)) {
sd_id128_t machine_id;
uint8_t state;
@@ -2053,6 +2058,7 @@ static int journal_file_link_entry(
f->header->tail_entry_realtime = o->entry.realtime;
f->header->tail_entry_monotonic = o->entry.monotonic;
f->header->tail_entry_offset = offset;
/* Link up the items */
for (uint64_t i = 0; i < n_items; i++) {