journal-file.c revision 84168d8068bb67dcd5468ab3b376535d81643aef
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering This file is part of systemd.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering Copyright 2011 Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering systemd is free software; you can redistribute it and/or modify it
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering under the terms of the GNU Lesser General Public License as published by
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering (at your option) any later version.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering systemd is distributed in the hope that it will be useful, but
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering Lesser General Public License for more details.
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering You should have received a copy of the GNU Lesser General Public License
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*sizeof(HashItem))
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#define DEFAULT_FIELD_HASH_TABLE_SIZE (333ULL*sizeof(HashItem))
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#define COMPRESSION_SIZE_THRESHOLD (512ULL)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/* This is the minimum journal file size */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#define JOURNAL_FILE_SIZE_MIN (64ULL*1024ULL) /* 64 KiB */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/* These are the lower and upper bounds if we deduce the max_use value
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * from the file system size */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#define DEFAULT_MAX_USE_LOWER (1ULL*1024ULL*1024ULL) /* 1 MiB */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#define DEFAULT_MAX_USE_UPPER (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/* This is the upper bound if we deduce max_size from max_use */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#define DEFAULT_MAX_SIZE_UPPER (128ULL*1024ULL*1024ULL) /* 128 MiB */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/* This is the upper bound if we deduce the keep_free value from the
bb99a35a873c35e80b0b47fe045081022660374dLennart Poettering * file system size */
bb99a35a873c35e80b0b47fe045081022660374dLennart Poettering#define DEFAULT_KEEP_FREE_UPPER (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/* This is the keep_free value when we can't determine the system
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#define DEFAULT_KEEP_FREE (1024ULL*1024ULL) /* 1 MB */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/* n_data was the first entry we added after the initial file format design */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#define HEADER_SIZE_MIN ALIGN64(offsetof(Header, n_data))
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* Write the final tag */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* Sync everything to disk, before we mark the file offline */
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering /* Mark the file offline. Don't override the archived state if it already is set */
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering if (f->writable && f->header->state == STATE_ONLINE)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering munmap(f->header, PAGE_ALIGN(sizeof(Header)));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering munmap(f->fss_file, PAGE_ALIGN(f->fss_file_size));
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringstatic int journal_file_init_header(JournalFile *f, JournalFile *template) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering memcpy(h.signature, HEADER_SIGNATURE, 8);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering h.header_size = htole64(ALIGN64(sizeof(h)));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering htole32(f->compress ? HEADER_INCOMPATIBLE_COMPRESSED : 0);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering htole32(f->seal ? HEADER_COMPATIBLE_SEALED : 0);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering h.seqnum_id = template->header->seqnum_id;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering h.tail_entry_seqnum = template->header->tail_entry_seqnum;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (k != sizeof(h))
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringstatic int journal_file_refresh_header(JournalFile *f) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering r = sd_id128_get_machine(&f->header->machine_id);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (sd_id128_equal(boot_id, f->header->boot_id))
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering /* Sync the online state to disk */
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering msync(f->header, PAGE_ALIGN(sizeof(Header)), MS_SYNC);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic int journal_file_verify_header(JournalFile *f) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (memcmp(f->header->signature, HEADER_SIGNATURE, 8))
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* In both read and write mode we refuse to open files with
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * incompatible flags we don't know */
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering if ((le32toh(f->header->incompatible_flags) & ~HEADER_INCOMPATIBLE_COMPRESSED) != 0)
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering /* When open for writing we refuse to open files with
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering * compatible flags, too */
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering if ((le32toh(f->header->compatible_flags) & ~HEADER_COMPATIBLE_SEALED) != 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* The first addition was n_data, so check that we are at least this large */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (le64toh(f->header->header_size) < HEADER_SIZE_MIN)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if ((le32toh(f->header->compatible_flags) & HEADER_COMPATIBLE_SEALED) &&
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering !JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays))
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if ((le64toh(f->header->header_size) + le64toh(f->header->arena_size)) > (uint64_t) f->last_stat.st_size)
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (le64toh(f->header->tail_object_offset) > (le64toh(f->header->header_size) + le64toh(f->header->arena_size)))
a5344d2c3b0f14e954ce1c0ef905c5b44bc5bf0aLennart Poettering if (!VALID64(f->header->data_hash_table_offset) ||
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering !VALID64(f->header->field_hash_table_offset) ||
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering !VALID64(f->header->tail_object_offset) ||
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (!sd_id128_equal(machine_id, f->header->machine_id))
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_debug("Journal file %s is already online. Assuming unclean closing.", f->path);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_debug("Journal file %s has unknown state %u.", f->path, state);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering f->compress = !!(le32toh(f->header->incompatible_flags) & HEADER_INCOMPATIBLE_COMPRESSED);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering f->seal = !!(le32toh(f->header->compatible_flags) & HEADER_COMPATIBLE_SEALED);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering /* We assume that this file is not sparse, and we know that
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering * for sure, since we always call posix_fallocate()
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering * ourselves */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (new_size < le64toh(f->header->header_size))
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering new_size = le64toh(f->header->header_size);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering /* Note that the glibc fallocate() fallback is very
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering inefficient, hence we try to minimize the allocation area
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering r = posix_fallocate(f->fd, old_size, new_size - old_size);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering mmap_cache_close_fd_range(f->mmap, f->fd, old_size);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering f->header->arena_size = htole64(new_size - le64toh(f->header->header_size));
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poetteringstatic int journal_file_move_to(JournalFile *f, int context, uint64_t offset, uint64_t size, void **ret) {
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering /* Avoid SIGBUS on invalid accesses */
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering if (offset + size > (uint64_t) f->last_stat.st_size) {
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering /* Hmm, out of range? Let's refresh the fstat() data
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering * first, before we trust that check. */
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering offset + size > (uint64_t) f->last_stat.st_size)
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return mmap_cache_get(f->mmap, f->fd, f->prot, context, offset, size, ret);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringstatic uint64_t minimum_header_size(Object *o) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering [OBJECT_DATA_HASH_TABLE] = sizeof(HashTableObject),
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering [OBJECT_FIELD_HASH_TABLE] = sizeof(HashTableObject),
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering [OBJECT_ENTRY_ARRAY] = sizeof(EntryArrayObject),
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering if (o->object.type >= ELEMENTSOF(table) || table[o->object.type] <= 0)
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poetteringint journal_file_move_to_object(JournalFile *f, int type, uint64_t offset, Object **ret) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering /* Objects may only be located at multiple of 64 bit */
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering /* One context for each type, plus one catch-all for the rest */
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering context = type > 0 && type < _OBJECT_TYPE_MAX ? type : 0;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering r = journal_file_move_to(f, context, offset, sizeof(ObjectHeader), &t);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering if (s < sizeof(ObjectHeader))
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering if (s > sizeof(ObjectHeader)) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering r = journal_file_move_to(f, o->object.type, offset, s, &t);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic uint64_t journal_file_entry_seqnum(JournalFile *f, uint64_t *seqnum) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = le64toh(f->header->tail_entry_seqnum) + 1;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering /* If an external seqnum counter was passed, we update
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * both the local and the external one, and set it to
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * the maximum of both */
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering f->header->tail_entry_seqnum = htole64(r);
86b9b8e70d54e79db3ff4f67bbd5280ecfc82537Lennart Poettering f->header->head_entry_seqnum = htole64(r);
bb99a35a873c35e80b0b47fe045081022660374dLennart Poetteringint journal_file_append_object(JournalFile *f, int type, uint64_t size, Object **ret, uint64_t *offset) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(type > 0 && type < _OBJECT_TYPE_MAX);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering p = le64toh(f->header->tail_object_offset);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = journal_file_move_to_object(f, -1, p, &tail);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering p += ALIGN64(le64toh(tail->object.size));
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = journal_file_move_to(f, type, p, size, &t);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering f->header->tail_object_offset = htole64(p);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringstatic int journal_file_setup_data_hash_table(JournalFile *f) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering /* We estimate that we need 1 hash table entry per 768 of
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering journal file and we want to make sure we never get beyond
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering 75% fill level. Calculate the hash table size for the
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering maximum file size based on these metrics. */
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering s = (f->metrics.max_size * 4 / 768 / 3) * sizeof(HashItem);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering log_info("Reserving %llu entries in hash table.", (unsigned long long) (s / sizeof(HashItem)));
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering f->header->data_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering f->header->data_hash_table_size = htole64(s);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringstatic int journal_file_setup_field_hash_table(JournalFile *f) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering f->header->field_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering f->header->field_hash_table_size = htole64(s);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringstatic int journal_file_map_data_hash_table(JournalFile *f) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering p = le64toh(f->header->data_hash_table_offset);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering s = le64toh(f->header->data_hash_table_size);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poetteringstatic int journal_file_map_field_hash_table(JournalFile *f) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering p = le64toh(f->header->field_hash_table_offset);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering s = le64toh(f->header->field_hash_table_size);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringstatic int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering /* This might alter the window we are looking at */
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering o->data.next_hash_offset = o->data.next_field_offset = 0;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering o->data.entry_offset = o->data.entry_array_offset = 0;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering h = hash % (le64toh(f->header->data_hash_table_size) / sizeof(HashItem));
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering p = le64toh(f->data_hash_table[h].tail_hash_offset);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering /* Only entry in the hash table is easy */
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering f->data_hash_table[h].head_hash_offset = htole64(offset);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering /* Move back to the previous data object, to patch in
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
JournalFile *f,
assert(f);
return -EBADMSG;
Object *o;
goto next;
#ifdef HAVE_XZ
return -EBADMSG;
return -EBADMSG;
if (ret)
*ret = o;
if (offset)
*offset = p;
return -EPROTONOSUPPORT;
if (ret)
*ret = o;
if (offset)
*offset = p;
next:
JournalFile *f,
assert(f);
return journal_file_find_data_object_with_hash(f,
static int journal_file_append_data(
JournalFile *f,
Object *o;
bool compressed = false;
assert(f);
if (ret)
*ret = o;
if (offset)
*offset = p;
#ifdef HAVE_XZ
if (f->compress &&
if (compressed) {
if (ret)
*ret = o;
if (offset)
*offset = p;
assert(o);
assert(o);
assert(o);
uint64_t p) {
Object *o;
assert(f);
assert(p > 0);
n = journal_file_entry_array_n_items(o);
ap = a;
if (hidx > n)
if (ap == 0)
uint64_t p) {
assert(f);
assert(p > 0);
if (*idx == 0)
le64_t i;
uint64_t p;
assert(f);
assert(o);
return -EINVAL;
return link_entry_into_array_plus_one(f,
offset);
uint64_t n, i;
assert(f);
assert(o);
r = link_entry_into_array(f,
offset);
/* log_debug("=> %s seqnr=%lu n_entries=%lu", f->path, (unsigned long) o->entry.seqnum, (unsigned long) f->header->n_entries); */
f->tail_entry_monotonic_valid = true;
n = journal_file_entry_n_items(o);
static int journal_file_append_entry_internal(
JournalFile *f,
Object *o;
assert(f);
if (ret)
*ret = o;
if (offset)
assert(f);
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) {
assert(f);
if (!f->writable)
return -EPERM;
if (!ts) {
if (f->tail_entry_monotonic_valid &&
return -EINVAL;
for (i = 0; i < n_iovec; i++) {
uint64_t p;
Object *o;
uint64_t i,
Object *o;
uint64_t p = 0, a;
assert(f);
a = first;
uint64_t n;
n = journal_file_entry_array_n_items(o);
if (ret)
*ret = o;
if (offset)
*offset = p;
uint64_t i,
Object *o;
assert(f);
if (ret)
*ret = o;
if (offset)
uint64_t n,
bool subtract_one = false;
assert(f);
a = first;
if (right <= 0)
return -EBADMSG;
if (r == TEST_FOUND)
if (r == TEST_RIGHT) {
left = 0;
subtract_one = true;
i = left;
goto found;
return -EBADMSG;
if (r == TEST_FOUND)
if (r == TEST_RIGHT)
right = i;
subtract_one = true;
goto found;
if (subtract_one && t == 0 && i == 0)
if (subtract_one && i == 0)
p = last_p;
else if (subtract_one)
if (ret)
*ret = o;
if (offset)
*offset = p;
if (idx)
uint64_t n,
bool step_back = false;
Object *o;
assert(f);
if (r == TEST_FOUND)
if (r == TEST_LEFT)
if (r == TEST_RIGHT) {
goto found;
if (r == 0 && step_back)
goto found;
if (r > 0 && idx)
(*idx) ++;
if (ret)
*ret = o;
if (offset)
if (idx)
*idx = 0;
assert(f);
assert(p > 0);
if (p == needle)
return TEST_FOUND;
else if (p < needle)
return TEST_LEFT;
return TEST_RIGHT;
JournalFile *f,
uint64_t p,
return generic_array_bisect(f,
Object *o;
assert(f);
assert(p > 0);
return TEST_FOUND;
return TEST_LEFT;
return TEST_RIGHT;
JournalFile *f,
return generic_array_bisect(f,
Object *o;
assert(f);
assert(p > 0);
return TEST_FOUND;
return TEST_LEFT;
return TEST_RIGHT;
JournalFile *f,
return generic_array_bisect(f,
Object *o;
assert(f);
assert(p > 0);
return TEST_FOUND;
return TEST_LEFT;
return TEST_RIGHT;
JournalFile *f,
Object *o;
assert(f);
return -ENOENT;
return generic_array_bisect_plus_one(f,
JournalFile *f,
uint64_t i, n;
assert(f);
assert(p > 0 || !o);
return -EINVAL;
r = generic_array_bisect(f,
return generic_array_get(f,
JournalFile *f,
uint64_t i, n;
assert(f);
assert(o);
assert(p > 0);
return -EINVAL;
r = generic_array_bisect(f,
if (skip < 0) {
return -EBADMSG;
return generic_array_get(f,
JournalFile *f,
uint64_t n, i;
Object *d;
assert(f);
assert(p > 0 || !o);
return -EINVAL;
return generic_array_get_plus_one(f,
JournalFile *f,
uint64_t p,
Object *d;
assert(f);
return generic_array_bisect_plus_one(f,
JournalFile *f,
Object *o, *d;
uint64_t b, z;
assert(f);
return -ENOENT;
uint64_t p, q;
if (ret)
if (offset)
*offset = q;
JournalFile *f,
Object *d;
assert(f);
return generic_array_bisect_plus_one(f,
JournalFile *f,
Object *d;
assert(f);
return generic_array_bisect_plus_one(f,
Object *o;
uint64_t p;
assert(f);
goto fail;
case OBJECT_UNUSED:
case OBJECT_DATA:
case OBJECT_ENTRY:
case OBJECT_FIELD_HASH_TABLE:
case OBJECT_DATA_HASH_TABLE:
case OBJECT_ENTRY_ARRAY:
case OBJECT_TAG:
fail:
assert(f);
f->path,
100.0 * (double) le64toh(f->header->n_data) / ((double) (le64toh(f->header->data_hash_table_size) / sizeof(HashItem))));
100.0 * (double) le64toh(f->header->n_fields) / ((double) (le64toh(f->header->field_hash_table_size) / sizeof(HashItem))));
int journal_file_open(
const char *fname,
int flags,
bool compress,
bool seal,
JournalFile *f;
bool newly_created = false;
return -EINVAL;
return -EINVAL;
return -ENOMEM;
if (mmap_cache)
if (!f->mmap) {
r = -ENOMEM;
goto fail;
if (!f->path) {
r = -ENOMEM;
goto fail;
if (f->fd < 0) {
r = -errno;
goto fail;
r = -errno;
goto fail;
newly_created = true;
r = journal_file_fss_load(f);
f->seal = false;
goto fail;
r = -errno;
goto fail;
r = -EIO;
goto fail;
r = -errno;
goto fail;
if (!newly_created) {
r = journal_file_verify_header(f);
goto fail;
r = journal_file_fss_load(f);
goto fail;
if (f->writable) {
if (metrics) {
} else if (template)
r = journal_file_refresh_header(f);
goto fail;
r = journal_file_hmac_setup(f);
goto fail;
if (newly_created) {
goto fail;
goto fail;
r = journal_file_append_first_tag(f);
goto fail;
goto fail;
r = journal_file_map_data_hash_table(f);
goto fail;
if (ret)
*ret = f;
fail:
size_t l;
assert(f);
assert(*f);
old_file = *f;
return -EINVAL;
return -EINVAL;
return -ENOMEM;
free(p);
return -errno;
r = journal_file_open(old_file->path, old_file->flags, old_file->mode, compress, seal, NULL, old_file->mmap, old_file, &new_file);
*f = new_file;
const char *fname,
int flags,
bool compress,
bool seal,
size_t l;
random_ull()) < 0)
return -ENOMEM;
free(p);
return -errno;
int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint64_t p, uint64_t *seqnum, Object **ret, uint64_t *offset) {
uint64_t i, n;
assert(o);
assert(p);
return -EPERM;
return -EINVAL;
n = journal_file_entry_n_items(o);
uint64_t l, h;
size_t t;
void *data;
Object *u;
return -EBADMSG;
t = (size_t) l;
if ((uint64_t) t != l)
return -E2BIG;
#ifdef HAVE_XZ
if (!uncompress_blob(o->data.payload, l, &from->compress_buffer, &from->compress_buffer_size, &rsize))
return -EBADMSG;
l = rsize;
return -EPROTONOSUPPORT;
assert(m);
if (fs_size > 0) {
if (fs_size > 0) {
assert(f);
if (from) {
return -ENOENT;
if (to) {
return -ENOENT;
int journal_file_get_cutoff_monotonic_usec(JournalFile *f, sd_id128_t boot_id, usec_t *from, usec_t *to) {
Object *o;
uint64_t p;
assert(f);
if (from) {
if (to) {
r = generic_array_get_plus_one(f,
&o, NULL);
assert(f);
if (le64toh(f->header->n_data) * 4ULL > (le64toh(f->header->data_hash_table_size) / sizeof(HashItem)) * 3ULL) {
log_debug("Data hash table of %s has a fill level at %.1f (%llu of %llu items, %llu file size, %llu bytes per hash table item), suggesting rotation.",
f->path,
100.0 * (double) le64toh(f->header->n_data) / ((double) (le64toh(f->header->data_hash_table_size) / sizeof(HashItem))),
if (le64toh(f->header->n_fields) * 4ULL > (le64toh(f->header->field_hash_table_size) / sizeof(HashItem)) * 3ULL) {
log_debug("Field hash table of %s has a fill level at %.1f (%llu of %llu items), suggesting rotation.",
f->path,