X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fjournal%2Fjournal-file.c;h=ba04d1667b11dbda2f1eac5163252eff15773efb;hb=1f2da9ec5152cbf48c214969e079d9281ef68660;hp=8bd52733c97db551d04f86cd31cee63a18b0cd3c;hpb=48b617399d7d8446c5310b2568b2af6f13331b4c;p=elogind.git diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index 8bd52733c..ba04d1667 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -260,8 +260,7 @@ static int journal_file_verify_header(JournalFile *f) { f->compress = JOURNAL_HEADER_COMPRESSED(f->header); - if (f->writable) - f->seal = JOURNAL_HEADER_SEALED(f->header); + f->seal = JOURNAL_HEADER_SEALED(f->header); return 0; } @@ -590,7 +589,9 @@ static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, ui assert(f); assert(o); assert(offset > 0); - assert(o->object.type == OBJECT_DATA); + + if (o->object.type != OBJECT_DATA) + return -EINVAL; /* This might alter the window we are looking at */ @@ -775,18 +776,18 @@ static int journal_file_append_data( if (r < 0) return r; -#ifdef HAVE_GCRYPT - r = journal_file_hmac_put_object(f, OBJECT_DATA, p); - if (r < 0) - return r; -#endif - /* The linking might have altered the window, so let's * refresh our pointer */ r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); if (r < 0) return r; +#ifdef HAVE_GCRYPT + r = journal_file_hmac_put_object(f, OBJECT_DATA, o, p); + if (r < 0) + return r; +#endif + if (ret) *ret = o; @@ -798,22 +799,28 @@ static int journal_file_append_data( uint64_t journal_file_entry_n_items(Object *o) { assert(o); - assert(o->object.type == OBJECT_ENTRY); + + if (o->object.type != OBJECT_ENTRY) + return 0; return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem); } uint64_t journal_file_entry_array_n_items(Object *o) { assert(o); - assert(o->object.type == OBJECT_ENTRY_ARRAY); + + if (o->object.type != OBJECT_ENTRY_ARRAY) + return 0; return (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / sizeof(uint64_t); } uint64_t journal_file_hash_table_n_items(Object *o) { assert(o); - assert(o->object.type == OBJECT_DATA_HASH_TABLE || - o->object.type == OBJECT_FIELD_HASH_TABLE); + + if (o->object.type != OBJECT_DATA_HASH_TABLE && + o->object.type != OBJECT_FIELD_HASH_TABLE) + return 0; return (le64toh(o->object.size) - offsetof(Object, hash_table.items)) / sizeof(HashItem); } @@ -866,7 +873,7 @@ static int link_entry_into_array(JournalFile *f, return r; #ifdef HAVE_GCRYPT - r = journal_file_hmac_put_object(f, OBJECT_ENTRY_ARRAY, q); + r = journal_file_hmac_put_object(f, OBJECT_ENTRY_ARRAY, o, q); if (r < 0) return r; #endif @@ -949,7 +956,9 @@ static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) { assert(f); assert(o); assert(offset > 0); - assert(o->object.type == OBJECT_ENTRY); + + if (o->object.type != OBJECT_ENTRY) + return -EINVAL; __sync_synchronize(); @@ -1012,7 +1021,7 @@ static int journal_file_append_entry_internal( o->entry.boot_id = f->header->boot_id; #ifdef HAVE_GCRYPT - r = journal_file_hmac_put_object(f, OBJECT_ENTRY, np); + r = journal_file_hmac_put_object(f, OBJECT_ENTRY, o, np); if (r < 0) return r; #endif @@ -1041,7 +1050,17 @@ void journal_file_post_change(JournalFile *f) { __sync_synchronize(); if (ftruncate(f->fd, f->last_stat.st_size) < 0) - log_error("Failed to to truncate file to its own size: %m"); + log_error("Failed to truncate file to its own size: %m"); +} + +static int entry_item_cmp(const void *_a, const void *_b) { + const EntryItem *a = _a, *b = _b; + + if (le64toh(a->object_offset) < le64toh(b->object_offset)) + return -1; + if (le64toh(a->object_offset) > le64toh(b->object_offset)) + return 1; + return 0; } int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqnum, Object **ret, uint64_t *offset) { @@ -1088,6 +1107,10 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st items[i].hash = o->data.hash; } + /* Order by the position on disk, in order to improve seek + * times for rotating media. */ + qsort(items, n_iovec, sizeof(EntryItem), entry_item_cmp); + r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset); journal_file_post_change(f); @@ -2002,6 +2025,7 @@ int journal_file_open( bool newly_created = false; assert(fname); + assert(ret); if ((flags & O_ACCMODE) != O_RDONLY && (flags & O_ACCMODE) != O_RDWR) @@ -2024,7 +2048,9 @@ int journal_file_open( #ifdef HAVE_XZ f->compress = compress; #endif +#ifdef HAVE_GCRYPT f->seal = seal; +#endif if (mmap_cache) f->mmap = mmap_cache_ref(mmap_cache); @@ -2059,9 +2085,11 @@ int journal_file_open( #ifdef HAVE_GCRYPT /* Try to load the FSPRG state, and if we can't, then * just don't do sealing */ - r = journal_file_fss_load(f); - if (r < 0) - f->seal = false; + if (f->seal) { + r = journal_file_fss_load(f); + if (r < 0) + f->seal = false; + } #endif r = journal_file_init_header(f, template); @@ -2142,9 +2170,7 @@ int journal_file_open( if (r < 0) goto fail; - if (ret) - *ret = f; - + *ret = f; return 0; fail: