1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <linux/magic.h>
10 #include <sys/statvfs.h>
16 #include "alloc-util.h"
17 #include "chattr-util.h"
21 #include "format-util.h"
23 #include "id128-util.h"
24 #include "journal-authenticate.h"
25 #include "journal-def.h"
26 #include "journal-file.h"
27 #include "journal-internal.h"
29 #include "memory-util.h"
30 #include "missing_threads.h"
31 #include "path-util.h"
33 #include "random-util.h"
35 #include "sort-util.h"
36 #include "stat-util.h"
37 #include "string-table.h"
38 #include "string-util.h"
40 #include "sync-util.h"
41 #include "user-util.h"
42 #include "xattr-util.h"
44 #define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*sizeof(HashItem))
45 #define DEFAULT_FIELD_HASH_TABLE_SIZE (333ULL*sizeof(HashItem))
47 #define DEFAULT_COMPRESS_THRESHOLD (512ULL)
48 #define MIN_COMPRESS_THRESHOLD (8ULL)
50 /* This is the minimum journal file size */
51 #define JOURNAL_FILE_SIZE_MIN (512 * 1024ULL) /* 512 KiB */
52 #define JOURNAL_COMPACT_SIZE_MAX UINT32_MAX /* 4 GiB */
54 /* These are the lower and upper bounds if we deduce the max_use value
55 * from the file system size */
56 #define MAX_USE_LOWER (1 * 1024 * 1024ULL) /* 1 MiB */
57 #define MAX_USE_UPPER (4 * 1024 * 1024 * 1024ULL) /* 4 GiB */
59 /* Those are the lower and upper bounds for the minimal use limit,
60 * i.e. how much we'll use even if keep_free suggests otherwise. */
61 #define MIN_USE_LOW (1 * 1024 * 1024ULL) /* 1 MiB */
62 #define MIN_USE_HIGH (16 * 1024 * 1024ULL) /* 16 MiB */
64 /* This is the upper bound if we deduce max_size from max_use */
65 #define MAX_SIZE_UPPER (128 * 1024 * 1024ULL) /* 128 MiB */
67 /* This is the upper bound if we deduce the keep_free value from the
69 #define KEEP_FREE_UPPER (4 * 1024 * 1024 * 1024ULL) /* 4 GiB */
71 /* This is the keep_free value when we can't determine the system
73 #define DEFAULT_KEEP_FREE (1024 * 1024ULL) /* 1 MB */
75 /* This is the default maximum number of journal files to keep around. */
76 #define DEFAULT_N_MAX_FILES 100
78 /* n_data was the first entry we added after the initial file format design */
79 #define HEADER_SIZE_MIN ALIGN64(offsetof(Header, n_data))
81 /* How many entries to keep in the entry array chain cache at max */
82 #define CHAIN_CACHE_MAX 20
84 /* How much to increase the journal file size at once each time we allocate something new. */
85 #define FILE_SIZE_INCREASE (8 * 1024 * 1024ULL) /* 8MB */
87 /* Reread fstat() of the file for detecting deletions at least this often */
88 #define LAST_STAT_REFRESH_USEC (5*USEC_PER_SEC)
90 /* The mmap context to use for the header we pick as one above the last defined typed */
91 #define CONTEXT_HEADER _OBJECT_TYPE_MAX
93 /* Longest hash chain to rotate after */
94 #define HASH_CHAIN_DEPTH_MAX 100
97 # pragma GCC diagnostic ignored "-Waddress-of-packed-member"
100 static int mmap_prot_from_open_flags(int flags
) {
101 switch (flags
& O_ACCMODE
) {
107 return PROT_READ
|PROT_WRITE
;
109 assert_not_reached();
113 int journal_file_tail_end_by_pread(JournalFile
*f
, uint64_t *ret_offset
) {
121 /* Same as journal_file_tail_end_by_mmap() below, but operates with pread() to avoid the mmap cache
122 * (and thus is thread safe) */
124 p
= le64toh(f
->header
->tail_object_offset
);
126 p
= le64toh(f
->header
->header_size
);
131 r
= journal_file_read_object_header(f
, OBJECT_UNUSED
, p
, &tail
);
135 sz
= le64toh(tail
.object
.size
);
136 if (sz
> UINT64_MAX
- sizeof(uint64_t) + 1)
140 if (p
> UINT64_MAX
- sz
)
151 int journal_file_tail_end_by_mmap(JournalFile
*f
, uint64_t *ret_offset
) {
159 /* Same as journal_file_tail_end_by_pread() above, but operates with the usual mmap logic */
161 p
= le64toh(f
->header
->tail_object_offset
);
163 p
= le64toh(f
->header
->header_size
);
168 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, p
, &tail
);
172 sz
= le64toh(READ_NOW(tail
->object
.size
));
173 if (sz
> UINT64_MAX
- sizeof(uint64_t) + 1)
177 if (p
> UINT64_MAX
- sz
)
188 int journal_file_set_offline_thread_join(JournalFile
*f
) {
193 if (f
->offline_state
== OFFLINE_JOINED
)
196 r
= pthread_join(f
->offline_thread
, NULL
);
200 f
->offline_state
= OFFLINE_JOINED
;
202 if (mmap_cache_fd_got_sigbus(f
->cache_fd
))
208 static int journal_file_set_online(JournalFile
*f
) {
213 if (!journal_file_writable(f
))
216 if (f
->fd
< 0 || !f
->header
)
220 switch (f
->offline_state
) {
222 /* No offline thread, no need to wait. */
226 case OFFLINE_SYNCING
: {
227 OfflineState tmp_state
= OFFLINE_SYNCING
;
228 if (!__atomic_compare_exchange_n(&f
->offline_state
, &tmp_state
, OFFLINE_CANCEL
,
229 false, __ATOMIC_SEQ_CST
, __ATOMIC_SEQ_CST
))
232 /* Canceled syncing prior to offlining, no need to wait. */
236 case OFFLINE_AGAIN_FROM_SYNCING
: {
237 OfflineState tmp_state
= OFFLINE_AGAIN_FROM_SYNCING
;
238 if (!__atomic_compare_exchange_n(&f
->offline_state
, &tmp_state
, OFFLINE_CANCEL
,
239 false, __ATOMIC_SEQ_CST
, __ATOMIC_SEQ_CST
))
242 /* Canceled restart from syncing, no need to wait. */
246 case OFFLINE_AGAIN_FROM_OFFLINING
: {
247 OfflineState tmp_state
= OFFLINE_AGAIN_FROM_OFFLINING
;
248 if (!__atomic_compare_exchange_n(&f
->offline_state
, &tmp_state
, OFFLINE_CANCEL
,
249 false, __ATOMIC_SEQ_CST
, __ATOMIC_SEQ_CST
))
252 /* Canceled restart from offlining, must wait for offlining to complete however. */
257 r
= journal_file_set_offline_thread_join(f
);
267 if (mmap_cache_fd_got_sigbus(f
->cache_fd
))
270 switch (f
->header
->state
) {
275 f
->header
->state
= STATE_ONLINE
;
284 JournalFile
* journal_file_close(JournalFile
*f
) {
288 assert(f
->newest_boot_id_prioq_idx
== PRIOQ_IDX_NULL
);
291 mmap_cache_fd_free(f
->cache_fd
);
297 ordered_hashmap_free_free(f
->chain_cache
);
300 free(f
->compress_buffer
);
305 munmap(f
->fss_file
, PAGE_ALIGN(f
->fss_file_size
));
307 free(f
->fsprg_state
);
312 gcry_md_close(f
->hmac
);
318 static bool keyed_hash_requested(void) {
319 static thread_local
int cached
= -1;
323 r
= getenv_bool("SYSTEMD_JOURNAL_KEYED_HASH");
326 log_debug_errno(r
, "Failed to parse $SYSTEMD_JOURNAL_KEYED_HASH environment variable, ignoring: %m");
335 static bool compact_mode_requested(void) {
336 static thread_local
int cached
= -1;
340 r
= getenv_bool("SYSTEMD_JOURNAL_COMPACT");
343 log_debug_errno(r
, "Failed to parse $SYSTEMD_JOURNAL_COMPACT environment variable, ignoring: %m");
353 static Compression
getenv_compression(void) {
358 e
= getenv("SYSTEMD_JOURNAL_COMPRESS");
360 return DEFAULT_COMPRESSION
;
362 r
= parse_boolean(e
);
364 return r
? DEFAULT_COMPRESSION
: COMPRESSION_NONE
;
366 c
= compression_from_string(e
);
368 log_debug_errno(c
, "Failed to parse SYSTEMD_JOURNAL_COMPRESS value, ignoring: %s", e
);
369 return DEFAULT_COMPRESSION
;
372 if (!compression_supported(c
)) {
373 log_debug("Unsupported compression algorithm specified, ignoring: %s", e
);
374 return DEFAULT_COMPRESSION
;
381 static Compression
compression_requested(void) {
383 static thread_local Compression cached
= _COMPRESSION_INVALID
;
386 cached
= getenv_compression();
390 return COMPRESSION_NONE
;
394 static int journal_file_init_header(
396 JournalFileFlags file_flags
,
397 JournalFile
*template) {
406 /* Try to load the FSPRG state, and if we can't, then just don't do sealing */
407 seal
= FLAGS_SET(file_flags
, JOURNAL_SEAL
) && journal_file_fss_load(f
) >= 0;
411 .header_size
= htole64(ALIGN64(sizeof(h
))),
412 .incompatible_flags
= htole32(
413 FLAGS_SET(file_flags
, JOURNAL_COMPRESS
) * COMPRESSION_TO_HEADER_INCOMPATIBLE_FLAG(compression_requested()) |
414 keyed_hash_requested() * HEADER_INCOMPATIBLE_KEYED_HASH
|
415 compact_mode_requested() * HEADER_INCOMPATIBLE_COMPACT
),
416 .compatible_flags
= htole32(
417 (seal
* HEADER_COMPATIBLE_SEALED
) |
418 HEADER_COMPATIBLE_TAIL_ENTRY_BOOT_ID
),
421 assert_cc(sizeof(h
.signature
) == sizeof(HEADER_SIGNATURE
));
422 memcpy(h
.signature
, HEADER_SIGNATURE
, sizeof(HEADER_SIGNATURE
));
424 r
= sd_id128_randomize(&h
.file_id
);
428 r
= sd_id128_get_machine(&h
.machine_id
);
429 if (r
< 0 && !ERRNO_IS_MACHINE_ID_UNSET(r
))
430 return r
; /* If we have no valid machine ID (test environment?), let's simply leave the
431 * machine ID field all zeroes. */
434 h
.seqnum_id
= template->header
->seqnum_id
;
435 h
.tail_entry_seqnum
= template->header
->tail_entry_seqnum
;
437 h
.seqnum_id
= h
.file_id
;
439 k
= pwrite(f
->fd
, &h
, sizeof(h
), 0);
448 static int journal_file_refresh_header(JournalFile
*f
) {
454 /* We used to update the header's boot ID field here, but we don't do that anymore, as per
455 * HEADER_COMPATIBLE_TAIL_ENTRY_BOOT_ID */
457 r
= journal_file_set_online(f
);
459 /* Sync the online state to disk; likely just created a new file, also sync the directory this file
461 (void) fsync_full(f
->fd
);
466 static bool warn_wrong_flags(const JournalFile
*f
, bool compatible
) {
467 const uint32_t any
= compatible
? HEADER_COMPATIBLE_ANY
: HEADER_INCOMPATIBLE_ANY
,
468 supported
= compatible
? HEADER_COMPATIBLE_SUPPORTED
: HEADER_INCOMPATIBLE_SUPPORTED
;
469 const char *type
= compatible
? "compatible" : "incompatible";
475 flags
= le32toh(compatible
? f
->header
->compatible_flags
: f
->header
->incompatible_flags
);
477 if (flags
& ~supported
) {
479 log_debug("Journal file %s has unknown %s flags 0x%"PRIx32
,
480 f
->path
, type
, flags
& ~any
);
481 flags
= (flags
& any
) & ~supported
;
485 _cleanup_free_
char *t
= NULL
;
488 if (flags
& HEADER_COMPATIBLE_SEALED
)
489 strv
[n
++] = "sealed";
491 if (flags
& HEADER_INCOMPATIBLE_COMPRESSED_XZ
)
492 strv
[n
++] = "xz-compressed";
493 if (flags
& HEADER_INCOMPATIBLE_COMPRESSED_LZ4
)
494 strv
[n
++] = "lz4-compressed";
495 if (flags
& HEADER_INCOMPATIBLE_COMPRESSED_ZSTD
)
496 strv
[n
++] = "zstd-compressed";
497 if (flags
& HEADER_INCOMPATIBLE_KEYED_HASH
)
498 strv
[n
++] = "keyed-hash";
499 if (flags
& HEADER_INCOMPATIBLE_COMPACT
)
500 strv
[n
++] = "compact";
503 assert(n
< ELEMENTSOF(strv
));
505 t
= strv_join((char**) strv
, ", ");
506 log_debug("Journal file %s uses %s %s %s disabled at compilation time.",
507 f
->path
, type
, n
> 1 ? "flags" : "flag", strnull(t
));
515 static bool offset_is_valid(uint64_t offset
, uint64_t header_size
, uint64_t tail_object_offset
) {
518 if (!VALID64(offset
))
520 if (offset
< header_size
)
522 if (offset
> tail_object_offset
)
527 static bool hash_table_is_valid(uint64_t offset
, uint64_t size
, uint64_t header_size
, uint64_t arena_size
, uint64_t tail_object_offset
) {
528 if ((offset
== 0) != (size
== 0))
532 if (offset
<= offsetof(Object
, hash_table
.items
))
534 offset
-= offsetof(Object
, hash_table
.items
);
535 if (!offset_is_valid(offset
, header_size
, tail_object_offset
))
537 assert(offset
<= header_size
+ arena_size
);
538 if (size
> header_size
+ arena_size
- offset
)
543 static int journal_file_verify_header(JournalFile
*f
) {
544 uint64_t arena_size
, header_size
;
549 if (memcmp(f
->header
->signature
, HEADER_SIGNATURE
, 8))
552 /* In both read and write mode we refuse to open files with incompatible
553 * flags we don't know. */
554 if (warn_wrong_flags(f
, false))
555 return -EPROTONOSUPPORT
;
557 /* When open for writing we refuse to open files with compatible flags, too. */
558 if (journal_file_writable(f
) && warn_wrong_flags(f
, true))
559 return -EPROTONOSUPPORT
;
561 if (f
->header
->state
>= _STATE_MAX
)
564 header_size
= le64toh(READ_NOW(f
->header
->header_size
));
566 /* The first addition was n_data, so check that we are at least this large */
567 if (header_size
< HEADER_SIZE_MIN
)
570 /* When open for writing we refuse to open files with a mismatch of the header size, i.e. writing to
571 * files implementing older or new header structures. */
572 if (journal_file_writable(f
) && header_size
!= sizeof(Header
))
573 return -EPROTONOSUPPORT
;
575 /* Don't write to journal files without the new boot ID update behavior guarantee. */
576 if (journal_file_writable(f
) && !JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f
->header
))
577 return -EPROTONOSUPPORT
;
579 if (JOURNAL_HEADER_SEALED(f
->header
) && !JOURNAL_HEADER_CONTAINS(f
->header
, n_entry_arrays
))
582 arena_size
= le64toh(READ_NOW(f
->header
->arena_size
));
584 if (UINT64_MAX
- header_size
< arena_size
|| header_size
+ arena_size
> (uint64_t) f
->last_stat
.st_size
)
587 uint64_t tail_object_offset
= le64toh(f
->header
->tail_object_offset
);
588 if (!offset_is_valid(tail_object_offset
, header_size
, UINT64_MAX
))
590 if (header_size
+ arena_size
< tail_object_offset
)
592 if (header_size
+ arena_size
- tail_object_offset
< sizeof(ObjectHeader
))
595 if (!hash_table_is_valid(le64toh(f
->header
->data_hash_table_offset
),
596 le64toh(f
->header
->data_hash_table_size
),
597 header_size
, arena_size
, tail_object_offset
))
600 if (!hash_table_is_valid(le64toh(f
->header
->field_hash_table_offset
),
601 le64toh(f
->header
->field_hash_table_size
),
602 header_size
, arena_size
, tail_object_offset
))
605 uint64_t entry_array_offset
= le64toh(f
->header
->entry_array_offset
);
606 if (!offset_is_valid(entry_array_offset
, header_size
, tail_object_offset
))
609 if (JOURNAL_HEADER_CONTAINS(f
->header
, tail_entry_array_offset
)) {
610 uint32_t offset
= le32toh(f
->header
->tail_entry_array_offset
);
611 uint32_t n
= le32toh(f
->header
->tail_entry_array_n_entries
);
613 if (!offset_is_valid(offset
, header_size
, tail_object_offset
))
615 if (entry_array_offset
> offset
)
617 if (entry_array_offset
== 0 && offset
!= 0)
619 if ((offset
== 0) != (n
== 0))
621 assert(offset
<= header_size
+ arena_size
);
622 if ((uint64_t) n
* journal_file_entry_array_item_size(f
) > header_size
+ arena_size
- offset
)
626 if (JOURNAL_HEADER_CONTAINS(f
->header
, tail_entry_offset
)) {
627 uint64_t offset
= le64toh(f
->header
->tail_entry_offset
);
629 if (!offset_is_valid(offset
, header_size
, tail_object_offset
))
633 /* When there is an entry object, then these fields must be filled. */
634 if (sd_id128_is_null(f
->header
->tail_entry_boot_id
))
636 if (!VALID_REALTIME(le64toh(f
->header
->head_entry_realtime
)))
638 if (!VALID_REALTIME(le64toh(f
->header
->tail_entry_realtime
)))
640 if (!VALID_MONOTONIC(le64toh(f
->header
->tail_entry_realtime
)))
643 /* Otherwise, the fields must be zero. */
644 if (JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f
->header
) &&
645 !sd_id128_is_null(f
->header
->tail_entry_boot_id
))
647 if (f
->header
->head_entry_realtime
!= 0)
649 if (f
->header
->tail_entry_realtime
!= 0)
651 if (f
->header
->tail_entry_realtime
!= 0)
656 /* Verify number of objects */
657 uint64_t n_objects
= le64toh(f
->header
->n_objects
);
658 if (n_objects
> arena_size
/ sizeof(ObjectHeader
))
661 uint64_t n_entries
= le64toh(f
->header
->n_entries
);
662 if (n_entries
> n_objects
)
665 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_data
) &&
666 le64toh(f
->header
->n_data
) > n_objects
)
669 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_fields
) &&
670 le64toh(f
->header
->n_fields
) > n_objects
)
673 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_tags
) &&
674 le64toh(f
->header
->n_tags
) > n_objects
)
677 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_entry_arrays
) &&
678 le64toh(f
->header
->n_entry_arrays
) > n_objects
)
681 if (JOURNAL_HEADER_CONTAINS(f
->header
, tail_entry_array_n_entries
) &&
682 le32toh(f
->header
->tail_entry_array_n_entries
) > n_entries
)
685 if (journal_file_writable(f
)) {
686 sd_id128_t machine_id
;
690 r
= sd_id128_get_machine(&machine_id
);
691 if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r
)) /* Gracefully handle the machine ID not being initialized yet */
692 machine_id
= SD_ID128_NULL
;
696 if (!sd_id128_equal(machine_id
, f
->header
->machine_id
))
697 return log_debug_errno(SYNTHETIC_ERRNO(EHOSTDOWN
),
698 "Trying to open journal file from different host for writing, refusing.");
700 state
= f
->header
->state
;
702 if (state
== STATE_ARCHIVED
)
703 return -ESHUTDOWN
; /* Already archived */
704 if (state
== STATE_ONLINE
)
705 return log_debug_errno(SYNTHETIC_ERRNO(EBUSY
),
706 "Journal file %s is already online. Assuming unclean closing.",
708 if (state
!= STATE_OFFLINE
)
709 return log_debug_errno(SYNTHETIC_ERRNO(EBUSY
),
710 "Journal file %s has unknown state %i.",
713 if (f
->header
->field_hash_table_size
== 0 || f
->header
->data_hash_table_size
== 0)
720 int journal_file_fstat(JournalFile
*f
) {
726 if (fstat(f
->fd
, &f
->last_stat
) < 0)
729 f
->last_stat_usec
= now(CLOCK_MONOTONIC
);
731 /* Refuse dealing with files that aren't regular */
732 r
= stat_verify_regular(&f
->last_stat
);
736 /* Refuse appending to files that are already deleted */
737 if (f
->last_stat
.st_nlink
<= 0)
743 static int journal_file_allocate(JournalFile
*f
, uint64_t offset
, uint64_t size
) {
744 uint64_t old_size
, new_size
, old_header_size
, old_arena_size
;
750 /* We assume that this file is not sparse, and we know that for sure, since we always call
751 * posix_fallocate() ourselves */
753 if (size
> PAGE_ALIGN_DOWN(UINT64_MAX
) - offset
)
756 if (mmap_cache_fd_got_sigbus(f
->cache_fd
))
759 old_header_size
= le64toh(READ_NOW(f
->header
->header_size
));
760 old_arena_size
= le64toh(READ_NOW(f
->header
->arena_size
));
761 if (old_arena_size
> PAGE_ALIGN_DOWN(UINT64_MAX
) - old_header_size
)
764 old_size
= old_header_size
+ old_arena_size
;
766 new_size
= MAX(PAGE_ALIGN(offset
+ size
), old_header_size
);
768 if (new_size
<= old_size
) {
770 /* We already pre-allocated enough space, but before
771 * we write to it, let's check with fstat() if the
772 * file got deleted, in order make sure we don't throw
773 * away the data immediately. Don't check fstat() for
774 * all writes though, but only once ever 10s. */
776 if (f
->last_stat_usec
+ LAST_STAT_REFRESH_USEC
> now(CLOCK_MONOTONIC
))
779 return journal_file_fstat(f
);
782 /* Allocate more space. */
784 if (f
->metrics
.max_size
> 0 && new_size
> f
->metrics
.max_size
)
787 /* Refuse to go over 4G in compact mode so offsets can be stored in 32-bit. */
788 if (JOURNAL_HEADER_COMPACT(f
->header
) && new_size
> UINT32_MAX
)
791 if (new_size
> f
->metrics
.min_size
&& f
->metrics
.keep_free
> 0) {
794 if (fstatvfs(f
->fd
, &svfs
) >= 0) {
797 available
= LESS_BY((uint64_t) svfs
.f_bfree
* (uint64_t) svfs
.f_bsize
, f
->metrics
.keep_free
);
799 if (new_size
- old_size
> available
)
804 /* Increase by larger blocks at once */
805 new_size
= ROUND_UP(new_size
, FILE_SIZE_INCREASE
);
806 if (f
->metrics
.max_size
> 0 && new_size
> f
->metrics
.max_size
)
807 new_size
= f
->metrics
.max_size
;
809 /* Note that the glibc fallocate() fallback is very
810 inefficient, hence we try to minimize the allocation area
812 r
= posix_fallocate_loop(f
->fd
, old_size
, new_size
- old_size
);
816 f
->header
->arena_size
= htole64(new_size
- old_header_size
);
818 return journal_file_fstat(f
);
821 static unsigned type_to_context(ObjectType type
) {
822 /* One context for each type, plus one catch-all for the rest */
823 assert_cc(_OBJECT_TYPE_MAX
<= MMAP_CACHE_MAX_CONTEXTS
);
824 assert_cc(CONTEXT_HEADER
< MMAP_CACHE_MAX_CONTEXTS
);
825 return type
> OBJECT_UNUSED
&& type
< _OBJECT_TYPE_MAX
? type
: 0;
828 static int journal_file_move_to(
841 /* This function may clear, overwrite, or alter previously cached entries with the same type. After
842 * this function has been called, all previously read objects with the same type may be invalidated,
843 * hence must be re-read before use. */
848 if (size
> UINT64_MAX
- offset
)
851 /* Avoid SIGBUS on invalid accesses */
852 if (offset
+ size
> (uint64_t) f
->last_stat
.st_size
) {
853 /* Hmm, out of range? Let's refresh the fstat() data
854 * first, before we trust that check. */
856 r
= journal_file_fstat(f
);
860 if (offset
+ size
> (uint64_t) f
->last_stat
.st_size
)
861 return -EADDRNOTAVAIL
;
864 return mmap_cache_fd_get(f
->cache_fd
, type_to_context(type
), keep_always
, offset
, size
, &f
->last_stat
, ret
);
867 static uint64_t minimum_header_size(JournalFile
*f
, Object
*o
) {
869 static const uint64_t table
[] = {
870 [OBJECT_DATA
] = sizeof(DataObject
),
871 [OBJECT_FIELD
] = sizeof(FieldObject
),
872 [OBJECT_ENTRY
] = sizeof(EntryObject
),
873 [OBJECT_DATA_HASH_TABLE
] = sizeof(HashTableObject
),
874 [OBJECT_FIELD_HASH_TABLE
] = sizeof(HashTableObject
),
875 [OBJECT_ENTRY_ARRAY
] = sizeof(EntryArrayObject
),
876 [OBJECT_TAG
] = sizeof(TagObject
),
882 if (o
->object
.type
== OBJECT_DATA
)
883 return journal_file_data_payload_offset(f
);
885 if (o
->object
.type
>= ELEMENTSOF(table
) || table
[o
->object
.type
] <= 0)
886 return sizeof(ObjectHeader
);
888 return table
[o
->object
.type
];
891 static int check_object_header(JournalFile
*f
, Object
*o
, ObjectType type
, uint64_t offset
) {
897 s
= le64toh(READ_NOW(o
->object
.size
));
899 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
900 "Attempt to move to uninitialized object: %" PRIu64
,
903 if (s
< sizeof(ObjectHeader
))
904 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
905 "Attempt to move to overly short object with size %"PRIu64
": %" PRIu64
,
908 if (o
->object
.type
<= OBJECT_UNUSED
|| o
->object
.type
>= _OBJECT_TYPE_MAX
)
909 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
910 "Attempt to move to object with invalid type (%u): %" PRIu64
,
911 o
->object
.type
, offset
);
913 if (type
> OBJECT_UNUSED
&& o
->object
.type
!= type
)
914 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
915 "Found %s object while expecting %s object: %" PRIu64
,
916 journal_object_type_to_string(o
->object
.type
),
917 journal_object_type_to_string(type
),
920 if (s
< minimum_header_size(f
, o
))
921 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
922 "Size of %s object (%"PRIu64
") is smaller than the minimum object size (%"PRIu64
"): %" PRIu64
,
923 journal_object_type_to_string(o
->object
.type
),
925 minimum_header_size(f
, o
),
931 /* Lightweight object checks. We want this to be fast, so that we won't
932 * slowdown every journal_file_move_to_object() call too much. */
933 static int check_object(JournalFile
*f
, Object
*o
, uint64_t offset
) {
937 switch (o
->object
.type
) {
940 if ((le64toh(o
->data
.entry_offset
) == 0) ^ (le64toh(o
->data
.n_entries
) == 0))
941 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
942 "Bad data n_entries: %" PRIu64
": %" PRIu64
,
943 le64toh(o
->data
.n_entries
),
946 if (le64toh(o
->object
.size
) <= journal_file_data_payload_offset(f
))
947 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
948 "Bad data size (<= %zu): %" PRIu64
": %" PRIu64
,
949 journal_file_data_payload_offset(f
),
950 le64toh(o
->object
.size
),
953 if (!VALID64(le64toh(o
->data
.next_hash_offset
)) ||
954 !VALID64(le64toh(o
->data
.next_field_offset
)) ||
955 !VALID64(le64toh(o
->data
.entry_offset
)) ||
956 !VALID64(le64toh(o
->data
.entry_array_offset
)))
957 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
958 "Invalid offset, next_hash_offset=" OFSfmt
", next_field_offset=" OFSfmt
", entry_offset=" OFSfmt
", entry_array_offset=" OFSfmt
": %" PRIu64
,
959 le64toh(o
->data
.next_hash_offset
),
960 le64toh(o
->data
.next_field_offset
),
961 le64toh(o
->data
.entry_offset
),
962 le64toh(o
->data
.entry_array_offset
),
968 if (le64toh(o
->object
.size
) <= offsetof(Object
, field
.payload
))
969 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
970 "Bad field size (<= %zu): %" PRIu64
": %" PRIu64
,
971 offsetof(Object
, field
.payload
),
972 le64toh(o
->object
.size
),
975 if (!VALID64(le64toh(o
->field
.next_hash_offset
)) ||
976 !VALID64(le64toh(o
->field
.head_data_offset
)))
977 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
978 "Invalid offset, next_hash_offset=" OFSfmt
", head_data_offset=" OFSfmt
": %" PRIu64
,
979 le64toh(o
->field
.next_hash_offset
),
980 le64toh(o
->field
.head_data_offset
),
987 sz
= le64toh(READ_NOW(o
->object
.size
));
988 if (sz
< offsetof(Object
, entry
.items
) ||
989 (sz
- offsetof(Object
, entry
.items
)) % journal_file_entry_item_size(f
) != 0)
990 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
991 "Bad entry size (<= %zu): %" PRIu64
": %" PRIu64
,
992 offsetof(Object
, entry
.items
),
996 if ((sz
- offsetof(Object
, entry
.items
)) / journal_file_entry_item_size(f
) <= 0)
997 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
998 "Invalid number items in entry: %" PRIu64
": %" PRIu64
,
999 (sz
- offsetof(Object
, entry
.items
)) / journal_file_entry_item_size(f
),
1002 if (le64toh(o
->entry
.seqnum
) <= 0)
1003 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1004 "Invalid entry seqnum: %" PRIx64
": %" PRIu64
,
1005 le64toh(o
->entry
.seqnum
),
1008 if (!VALID_REALTIME(le64toh(o
->entry
.realtime
)))
1009 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1010 "Invalid entry realtime timestamp: %" PRIu64
": %" PRIu64
,
1011 le64toh(o
->entry
.realtime
),
1014 if (!VALID_MONOTONIC(le64toh(o
->entry
.monotonic
)))
1015 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1016 "Invalid entry monotonic timestamp: %" PRIu64
": %" PRIu64
,
1017 le64toh(o
->entry
.monotonic
),
1020 if (sd_id128_is_null(o
->entry
.boot_id
))
1021 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1022 "Invalid object entry with an empty boot ID: %" PRIu64
,
1028 case OBJECT_DATA_HASH_TABLE
:
1029 case OBJECT_FIELD_HASH_TABLE
: {
1032 sz
= le64toh(READ_NOW(o
->object
.size
));
1033 if (sz
< offsetof(Object
, hash_table
.items
) ||
1034 (sz
- offsetof(Object
, hash_table
.items
)) % sizeof(HashItem
) != 0 ||
1035 (sz
- offsetof(Object
, hash_table
.items
)) / sizeof(HashItem
) <= 0)
1036 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1037 "Invalid %s hash table size: %" PRIu64
": %" PRIu64
,
1038 journal_object_type_to_string(o
->object
.type
),
1045 case OBJECT_ENTRY_ARRAY
: {
1048 sz
= le64toh(READ_NOW(o
->object
.size
));
1049 if (sz
< offsetof(Object
, entry_array
.items
) ||
1050 (sz
- offsetof(Object
, entry_array
.items
)) % journal_file_entry_array_item_size(f
) != 0 ||
1051 (sz
- offsetof(Object
, entry_array
.items
)) / journal_file_entry_array_item_size(f
) <= 0)
1052 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1053 "Invalid object entry array size: %" PRIu64
": %" PRIu64
,
1056 /* Here, we request that the offset of each entry array object is in strictly increasing order. */
1057 next
= le64toh(o
->entry_array
.next_entry_array_offset
);
1058 if (!VALID64(next
) || (next
> 0 && next
<= offset
))
1059 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1060 "Invalid object entry array next_entry_array_offset: %" PRIu64
": %" PRIu64
,
1068 if (le64toh(o
->object
.size
) != sizeof(TagObject
))
1069 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1070 "Invalid object tag size: %" PRIu64
": %" PRIu64
,
1071 le64toh(o
->object
.size
),
1074 if (!VALID_EPOCH(le64toh(o
->tag
.epoch
)))
1075 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1076 "Invalid object tag epoch: %" PRIu64
": %" PRIu64
,
1077 le64toh(o
->tag
.epoch
), offset
);
1085 int journal_file_move_to_object(JournalFile
*f
, ObjectType type
, uint64_t offset
, Object
**ret
) {
1091 /* Even if this function fails, it may clear, overwrite, or alter previously cached entries with the
1092 * same type. After this function has been called, all previously read objects with the same type may
1093 * be invalidated, hence must be re-read before use. */
1095 /* Objects may only be located at multiple of 64 bit */
1096 if (!VALID64(offset
))
1097 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1098 "Attempt to move to %s object at non-64-bit boundary: %" PRIu64
,
1099 journal_object_type_to_string(type
),
1102 /* Object may not be located in the file header */
1103 if (offset
< le64toh(f
->header
->header_size
))
1104 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1105 "Attempt to move to %s object located in file header: %" PRIu64
,
1106 journal_object_type_to_string(type
),
1109 r
= journal_file_move_to(f
, type
, false, offset
, sizeof(ObjectHeader
), (void**) &o
);
1113 r
= check_object_header(f
, o
, type
, offset
);
1117 r
= journal_file_move_to(f
, type
, false, offset
, le64toh(READ_NOW(o
->object
.size
)), (void**) &o
);
1121 r
= check_object_header(f
, o
, type
, offset
);
1125 r
= check_object(f
, o
, offset
);
1135 int journal_file_read_object_header(JournalFile
*f
, ObjectType type
, uint64_t offset
, Object
*ret
) {
1142 /* Objects may only be located at multiple of 64 bit */
1143 if (!VALID64(offset
))
1144 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1145 "Attempt to read %s object at non-64-bit boundary: %" PRIu64
,
1146 journal_object_type_to_string(type
), offset
);
1148 /* Object may not be located in the file header */
1149 if (offset
< le64toh(f
->header
->header_size
))
1150 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1151 "Attempt to read %s object located in file header: %" PRIu64
,
1152 journal_object_type_to_string(type
), offset
);
1154 /* This will likely read too much data but it avoids having to call pread() twice. */
1155 n
= pread(f
->fd
, &o
, sizeof(o
), offset
);
1157 return log_debug_errno(errno
, "Failed to read journal %s object at offset: %" PRIu64
,
1158 journal_object_type_to_string(type
), offset
);
1160 if ((size_t) n
< sizeof(o
.object
))
1161 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
1162 "Failed to read short %s object at offset: %" PRIu64
,
1163 journal_object_type_to_string(type
), offset
);
1165 r
= check_object_header(f
, &o
, type
, offset
);
1169 if ((size_t) n
< minimum_header_size(f
, &o
))
1170 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
1171 "Short read while reading %s object: %" PRIu64
,
1172 journal_object_type_to_string(type
), offset
);
1174 r
= check_object(f
, &o
, offset
);
1184 static uint64_t inc_seqnum(uint64_t seqnum
) {
1185 if (seqnum
< UINT64_MAX
-1)
1188 return 1; /* skip over UINT64_MAX and 0 when we run out of seqnums and start again */
1191 static uint64_t journal_file_entry_seqnum(
1195 uint64_t next_seqnum
;
1200 /* Picks a new sequence number for the entry we are about to add and returns it. */
1202 next_seqnum
= inc_seqnum(le64toh(f
->header
->tail_entry_seqnum
));
1204 /* If an external seqnum counter was passed, we update both the local and the external one, and set
1205 * it to the maximum of both */
1207 *seqnum
= next_seqnum
= MAX(inc_seqnum(*seqnum
), next_seqnum
);
1209 f
->header
->tail_entry_seqnum
= htole64(next_seqnum
);
1211 if (f
->header
->head_entry_seqnum
== 0)
1212 f
->header
->head_entry_seqnum
= htole64(next_seqnum
);
1217 int journal_file_append_object(
1221 Object
**ret_object
,
1222 uint64_t *ret_offset
) {
1230 assert(type
> OBJECT_UNUSED
&& type
< _OBJECT_TYPE_MAX
);
1231 assert(size
>= sizeof(ObjectHeader
));
1233 r
= journal_file_set_online(f
);
1237 r
= journal_file_tail_end_by_mmap(f
, &p
);
1241 r
= journal_file_allocate(f
, p
, size
);
1245 r
= journal_file_move_to(f
, type
, false, p
, size
, (void**) &o
);
1249 o
->object
= (ObjectHeader
) {
1251 .size
= htole64(size
),
1254 f
->header
->tail_object_offset
= htole64(p
);
1255 f
->header
->n_objects
= htole64(le64toh(f
->header
->n_objects
) + 1);
1266 static int journal_file_setup_data_hash_table(JournalFile
*f
) {
1274 /* We estimate that we need 1 hash table entry per 768 bytes
1275 of journal file and we want to make sure we never get
1276 beyond 75% fill level. Calculate the hash table size for
1277 the maximum file size based on these metrics. */
1279 s
= (f
->metrics
.max_size
* 4 / 768 / 3) * sizeof(HashItem
);
1280 if (s
< DEFAULT_DATA_HASH_TABLE_SIZE
)
1281 s
= DEFAULT_DATA_HASH_TABLE_SIZE
;
1283 log_debug("Reserving %"PRIu64
" entries in data hash table.", s
/ sizeof(HashItem
));
1285 r
= journal_file_append_object(f
,
1286 OBJECT_DATA_HASH_TABLE
,
1287 offsetof(Object
, hash_table
.items
) + s
,
1292 memzero(o
->hash_table
.items
, s
);
1294 f
->header
->data_hash_table_offset
= htole64(p
+ offsetof(Object
, hash_table
.items
));
1295 f
->header
->data_hash_table_size
= htole64(s
);
1300 static int journal_file_setup_field_hash_table(JournalFile
*f
) {
1308 /* We use a fixed size hash table for the fields as this
1309 * number should grow very slowly only */
1311 s
= DEFAULT_FIELD_HASH_TABLE_SIZE
;
1312 log_debug("Reserving %"PRIu64
" entries in field hash table.", s
/ sizeof(HashItem
));
1314 r
= journal_file_append_object(f
,
1315 OBJECT_FIELD_HASH_TABLE
,
1316 offsetof(Object
, hash_table
.items
) + s
,
1321 memzero(o
->hash_table
.items
, s
);
1323 f
->header
->field_hash_table_offset
= htole64(p
+ offsetof(Object
, hash_table
.items
));
1324 f
->header
->field_hash_table_size
= htole64(s
);
1329 int journal_file_map_data_hash_table(JournalFile
*f
) {
1337 if (f
->data_hash_table
)
1340 p
= le64toh(f
->header
->data_hash_table_offset
);
1341 s
= le64toh(f
->header
->data_hash_table_size
);
1343 r
= journal_file_move_to(f
,
1344 OBJECT_DATA_HASH_TABLE
,
1351 f
->data_hash_table
= t
;
1355 int journal_file_map_field_hash_table(JournalFile
*f
) {
1363 if (f
->field_hash_table
)
1366 p
= le64toh(f
->header
->field_hash_table_offset
);
1367 s
= le64toh(f
->header
->field_hash_table_size
);
1369 r
= journal_file_move_to(f
,
1370 OBJECT_FIELD_HASH_TABLE
,
1377 f
->field_hash_table
= t
;
1381 static int journal_file_link_field(
1392 assert(f
->field_hash_table
);
1396 if (o
->object
.type
!= OBJECT_FIELD
)
1399 m
= le64toh(READ_NOW(f
->header
->field_hash_table_size
)) / sizeof(HashItem
);
1403 /* This might alter the window we are looking at */
1404 o
->field
.next_hash_offset
= o
->field
.head_data_offset
= 0;
1407 p
= le64toh(f
->field_hash_table
[h
].tail_hash_offset
);
1409 f
->field_hash_table
[h
].head_hash_offset
= htole64(offset
);
1411 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, p
, &o
);
1415 o
->field
.next_hash_offset
= htole64(offset
);
1418 f
->field_hash_table
[h
].tail_hash_offset
= htole64(offset
);
1420 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_fields
))
1421 f
->header
->n_fields
= htole64(le64toh(f
->header
->n_fields
) + 1);
1426 static int journal_file_link_data(
1437 assert(f
->data_hash_table
);
1441 if (o
->object
.type
!= OBJECT_DATA
)
1444 m
= le64toh(READ_NOW(f
->header
->data_hash_table_size
)) / sizeof(HashItem
);
1448 /* This might alter the window we are looking at */
1449 o
->data
.next_hash_offset
= o
->data
.next_field_offset
= 0;
1450 o
->data
.entry_offset
= o
->data
.entry_array_offset
= 0;
1451 o
->data
.n_entries
= 0;
1454 p
= le64toh(f
->data_hash_table
[h
].tail_hash_offset
);
1456 /* Only entry in the hash table is easy */
1457 f
->data_hash_table
[h
].head_hash_offset
= htole64(offset
);
1459 /* Move back to the previous data object, to patch in
1462 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
1466 o
->data
.next_hash_offset
= htole64(offset
);
1469 f
->data_hash_table
[h
].tail_hash_offset
= htole64(offset
);
1471 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_data
))
1472 f
->header
->n_data
= htole64(le64toh(f
->header
->n_data
) + 1);
1477 static int get_next_hash_offset(
1480 le64_t
*next_hash_offset
,
1482 le64_t
*header_max_depth
) {
1488 assert(next_hash_offset
);
1491 nextp
= le64toh(READ_NOW(*next_hash_offset
));
1493 if (nextp
<= *p
) /* Refuse going in loops */
1494 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
1495 "Detected hash item loop in %s, refusing.", f
->path
);
1499 /* If the depth of this hash chain is larger than all others we have seen so far, record it */
1500 if (header_max_depth
&& journal_file_writable(f
))
1501 *header_max_depth
= htole64(MAX(*depth
, le64toh(*header_max_depth
)));
1508 int journal_file_find_field_object_with_hash(
1513 Object
**ret_object
,
1514 uint64_t *ret_offset
) {
1516 uint64_t p
, osize
, h
, m
, depth
= 0;
1524 /* If the field hash table is empty, we can't find anything */
1525 if (le64toh(f
->header
->field_hash_table_size
) <= 0)
1528 /* Map the field hash table, if it isn't mapped yet. */
1529 r
= journal_file_map_field_hash_table(f
);
1533 osize
= offsetof(Object
, field
.payload
) + size
;
1535 m
= le64toh(READ_NOW(f
->header
->field_hash_table_size
)) / sizeof(HashItem
);
1540 p
= le64toh(f
->field_hash_table
[h
].head_hash_offset
);
1544 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, p
, &o
);
1548 if (le64toh(o
->field
.hash
) == hash
&&
1549 le64toh(o
->object
.size
) == osize
&&
1550 memcmp(o
->field
.payload
, field
, size
) == 0) {
1560 r
= get_next_hash_offset(
1563 &o
->field
.next_hash_offset
,
1565 JOURNAL_HEADER_CONTAINS(f
->header
, field_hash_chain_depth
) ? &f
->header
->field_hash_chain_depth
: NULL
);
1573 uint64_t journal_file_hash_data(
1580 assert(data
|| sz
== 0);
1582 /* We try to unify our codebase on siphash, hence new-styled journal files utilizing the keyed hash
1583 * function use siphash. Old journal files use the Jenkins hash. */
1585 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
1586 return siphash24(data
, sz
, f
->header
->file_id
.bytes
);
1588 return jenkins_hash64(data
, sz
);
1591 int journal_file_find_field_object(
1595 Object
**ret_object
,
1596 uint64_t *ret_offset
) {
1602 return journal_file_find_field_object_with_hash(
1605 journal_file_hash_data(f
, field
, size
),
1606 ret_object
, ret_offset
);
1609 int journal_file_find_data_object_with_hash(
1614 Object
**ret_object
,
1615 uint64_t *ret_offset
) {
1617 uint64_t p
, h
, m
, depth
= 0;
1622 assert(data
|| size
== 0);
1624 /* If there's no data hash table, then there's no entry. */
1625 if (le64toh(f
->header
->data_hash_table_size
) <= 0)
1628 /* Map the data hash table, if it isn't mapped yet. */
1629 r
= journal_file_map_data_hash_table(f
);
1633 m
= le64toh(READ_NOW(f
->header
->data_hash_table_size
)) / sizeof(HashItem
);
1638 p
= le64toh(f
->data_hash_table
[h
].head_hash_offset
);
1645 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
1649 if (le64toh(o
->data
.hash
) != hash
)
1652 r
= journal_file_data_payload(f
, o
, p
, NULL
, 0, 0, &d
, &rsize
);
1655 assert(r
> 0); /* journal_file_data_payload() always returns > 0 if no field is provided. */
1657 if (memcmp_nn(data
, size
, d
, rsize
) == 0) {
1668 r
= get_next_hash_offset(
1671 &o
->data
.next_hash_offset
,
1673 JOURNAL_HEADER_CONTAINS(f
->header
, data_hash_chain_depth
) ? &f
->header
->data_hash_chain_depth
: NULL
);
1681 int journal_file_find_data_object(
1685 Object
**ret_object
,
1686 uint64_t *ret_offset
) {
1689 assert(data
|| size
== 0);
1691 return journal_file_find_data_object_with_hash(
1694 journal_file_hash_data(f
, data
, size
),
1695 ret_object
, ret_offset
);
1698 bool journal_field_valid(const char *p
, size_t l
, bool allow_protected
) {
1699 /* We kinda enforce POSIX syntax recommendations for
1700 environment variables here, but make a couple of additional
1703 http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html */
1710 /* No empty field names */
1714 /* Don't allow names longer than 64 chars */
1718 /* Variables starting with an underscore are protected */
1719 if (!allow_protected
&& p
[0] == '_')
1722 /* Don't allow digits as first character */
1723 if (ascii_isdigit(p
[0]))
1726 /* Only allow A-Z0-9 and '_' */
1727 for (const char *a
= p
; a
< p
+ l
; a
++)
1728 if ((*a
< 'A' || *a
> 'Z') &&
1729 !ascii_isdigit(*a
) &&
1736 static int journal_file_append_field(
1740 Object
**ret_object
,
1741 uint64_t *ret_offset
) {
1752 if (!journal_field_valid(field
, size
, true))
1755 hash
= journal_file_hash_data(f
, field
, size
);
1757 r
= journal_file_find_field_object_with_hash(f
, field
, size
, hash
, ret_object
, ret_offset
);
1763 osize
= offsetof(Object
, field
.payload
) + size
;
1764 r
= journal_file_append_object(f
, OBJECT_FIELD
, osize
, &o
, &p
);
1768 o
->field
.hash
= htole64(hash
);
1769 memcpy(o
->field
.payload
, field
, size
);
1771 r
= journal_file_link_field(f
, o
, p
, hash
);
1775 /* The linking might have altered the window, so let's only pass the offset to hmac which will
1776 * move to the object again if needed. */
1779 r
= journal_file_hmac_put_object(f
, OBJECT_FIELD
, NULL
, p
);
1785 r
= journal_file_move_to_object(f
, OBJECT_FIELD
, p
, ret_object
);
1796 static int maybe_compress_payload(JournalFile
*f
, uint8_t *dst
, const uint8_t *src
, uint64_t size
, size_t *rsize
) {
1800 #if HAVE_COMPRESSION
1804 c
= JOURNAL_FILE_COMPRESSION(f
);
1805 if (c
== COMPRESSION_NONE
|| size
< f
->compress_threshold_bytes
)
1808 r
= compress_blob(c
, src
, size
, dst
, size
- 1, rsize
);
1810 return log_debug_errno(r
, "Failed to compress data object using %s, ignoring: %m", compression_to_string(c
));
1812 log_debug("Compressed data object %"PRIu64
" -> %zu using %s", size
, *rsize
, compression_to_string(c
));
1814 return 1; /* compressed */
1820 static int journal_file_append_data(
1824 Object
**ret_object
,
1825 uint64_t *ret_offset
) {
1827 uint64_t hash
, p
, osize
;
1835 if (!data
|| size
== 0)
1838 hash
= journal_file_hash_data(f
, data
, size
);
1840 r
= journal_file_find_data_object_with_hash(f
, data
, size
, hash
, ret_object
, ret_offset
);
1846 eq
= memchr(data
, '=', size
);
1850 osize
= journal_file_data_payload_offset(f
) + size
;
1851 r
= journal_file_append_object(f
, OBJECT_DATA
, osize
, &o
, &p
);
1855 o
->data
.hash
= htole64(hash
);
1857 r
= maybe_compress_payload(f
, journal_file_data_payload_field(f
, o
), data
, size
, &rsize
);
1859 /* We don't really care failures, let's continue without compression */
1860 memcpy_safe(journal_file_data_payload_field(f
, o
), data
, size
);
1862 Compression c
= JOURNAL_FILE_COMPRESSION(f
);
1864 assert(c
>= 0 && c
< _COMPRESSION_MAX
&& c
!= COMPRESSION_NONE
);
1866 o
->object
.size
= htole64(journal_file_data_payload_offset(f
) + rsize
);
1867 o
->object
.flags
|= COMPRESSION_TO_OBJECT_FLAG(c
);
1870 r
= journal_file_link_data(f
, o
, p
, hash
);
1874 /* The linking might have altered the window, so let's refresh our pointer. */
1875 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
1880 r
= journal_file_hmac_put_object(f
, OBJECT_DATA
, o
, p
);
1885 /* Create field object ... */
1886 r
= journal_file_append_field(f
, data
, (uint8_t*) eq
- (uint8_t*) data
, &fo
, NULL
);
1890 /* ... and link it in. */
1891 o
->data
.next_field_offset
= fo
->field
.head_data_offset
;
1892 fo
->field
.head_data_offset
= le64toh(p
);
1903 static int maybe_decompress_payload(
1907 Compression compression
,
1909 size_t field_length
,
1910 size_t data_threshold
,
1916 /* We can't read objects larger than 4G on a 32-bit machine */
1917 if ((uint64_t) (size_t) size
!= size
)
1920 if (compression
!= COMPRESSION_NONE
) {
1921 #if HAVE_COMPRESSION
1926 r
= decompress_startswith(compression
, payload
, size
, &f
->compress_buffer
, field
,
1929 return log_debug_errno(r
,
1930 "Cannot decompress %s object of length %" PRIu64
": %m",
1931 compression_to_string(compression
),
1942 r
= decompress_blob(compression
, payload
, size
, &f
->compress_buffer
, &rsize
, 0);
1947 *ret_data
= f
->compress_buffer
;
1951 return -EPROTONOSUPPORT
;
1954 if (field
&& (size
< field_length
+ 1 || memcmp(payload
, field
, field_length
) != 0 || payload
[field_length
] != '=')) {
1963 *ret_data
= payload
;
1965 *ret_size
= (size_t) size
;
1971 int journal_file_data_payload(
1976 size_t field_length
,
1977 size_t data_threshold
,
1986 assert(!field
== (field_length
== 0)); /* These must be specified together. */
1989 r
= journal_file_move_to_object(f
, OBJECT_DATA
, offset
, &o
);
1994 size
= le64toh(READ_NOW(o
->object
.size
));
1995 if (size
< journal_file_data_payload_offset(f
))
1998 size
-= journal_file_data_payload_offset(f
);
2000 c
= COMPRESSION_FROM_OBJECT(o
);
2002 return -EPROTONOSUPPORT
;
2004 return maybe_decompress_payload(f
, journal_file_data_payload_field(f
, o
), size
, c
, field
,
2005 field_length
, data_threshold
, ret_data
, ret_size
);
2008 uint64_t journal_file_entry_n_items(JournalFile
*f
, Object
*o
) {
2014 if (o
->object
.type
!= OBJECT_ENTRY
)
2017 sz
= le64toh(READ_NOW(o
->object
.size
));
2018 if (sz
< offsetof(Object
, entry
.items
))
2021 return (sz
- offsetof(Object
, entry
.items
)) / journal_file_entry_item_size(f
);
2024 uint64_t journal_file_entry_array_n_items(JournalFile
*f
, Object
*o
) {
2030 if (o
->object
.type
!= OBJECT_ENTRY_ARRAY
)
2033 sz
= le64toh(READ_NOW(o
->object
.size
));
2034 if (sz
< offsetof(Object
, entry_array
.items
))
2037 return (sz
- offsetof(Object
, entry_array
.items
)) / journal_file_entry_array_item_size(f
);
2040 uint64_t journal_file_hash_table_n_items(Object
*o
) {
2045 if (!IN_SET(o
->object
.type
, OBJECT_DATA_HASH_TABLE
, OBJECT_FIELD_HASH_TABLE
))
2048 sz
= le64toh(READ_NOW(o
->object
.size
));
2049 if (sz
< offsetof(Object
, hash_table
.items
))
2052 return (sz
- offsetof(Object
, hash_table
.items
)) / sizeof(HashItem
);
2055 static void write_entry_array_item(JournalFile
*f
, Object
*o
, uint64_t i
, uint64_t p
) {
2059 if (JOURNAL_HEADER_COMPACT(f
->header
)) {
2060 assert(p
<= UINT32_MAX
);
2061 o
->entry_array
.items
.compact
[i
] = htole32(p
);
2063 o
->entry_array
.items
.regular
[i
] = htole64(p
);
2066 static int link_entry_into_array(
2074 uint64_t n
= 0, ap
= 0, q
, i
, a
, hidx
;
2084 a
= tail
? le32toh(*tail
) : le64toh(*first
);
2085 hidx
= le64toh(READ_NOW(*idx
));
2086 i
= tidx
? le32toh(READ_NOW(*tidx
)) : hidx
;
2089 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
2093 n
= journal_file_entry_array_n_items(f
, o
);
2095 write_entry_array_item(f
, o
, i
, p
);
2096 *idx
= htole64(hidx
+ 1);
2098 *tidx
= htole32(le32toh(*tidx
) + 1);
2104 a
= le64toh(o
->entry_array
.next_entry_array_offset
);
2115 r
= journal_file_append_object(f
, OBJECT_ENTRY_ARRAY
,
2116 offsetof(Object
, entry_array
.items
) + n
* journal_file_entry_array_item_size(f
),
2122 r
= journal_file_hmac_put_object(f
, OBJECT_ENTRY_ARRAY
, o
, q
);
2127 write_entry_array_item(f
, o
, i
, p
);
2130 *first
= htole64(q
);
2132 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, ap
, &o
);
2136 o
->entry_array
.next_entry_array_offset
= htole64(q
);
2142 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_entry_arrays
))
2143 f
->header
->n_entry_arrays
= htole64(le64toh(f
->header
->n_entry_arrays
) + 1);
2145 *idx
= htole64(hidx
+ 1);
2152 static int link_entry_into_array_plus_one(
2170 hidx
= le64toh(READ_NOW(*idx
));
2171 if (hidx
== UINT64_MAX
)
2174 *extra
= htole64(p
);
2178 i
= htole64(hidx
- 1);
2179 r
= link_entry_into_array(f
, first
, &i
, tail
, tidx
, p
);
2184 *idx
= htole64(hidx
+ 1);
2188 static int journal_file_link_entry_item(JournalFile
*f
, uint64_t offset
, uint64_t p
) {
2195 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
2199 return link_entry_into_array_plus_one(f
,
2200 &o
->data
.entry_offset
,
2201 &o
->data
.entry_array_offset
,
2203 JOURNAL_HEADER_COMPACT(f
->header
) ? &o
->data
.compact
.tail_entry_array_offset
: NULL
,
2204 JOURNAL_HEADER_COMPACT(f
->header
) ? &o
->data
.compact
.tail_entry_array_n_entries
: NULL
,
2208 static int journal_file_link_entry(
2212 const EntryItem items
[],
2222 if (o
->object
.type
!= OBJECT_ENTRY
)
2225 __atomic_thread_fence(__ATOMIC_SEQ_CST
);
2227 /* Link up the entry itself */
2228 r
= link_entry_into_array(f
,
2229 &f
->header
->entry_array_offset
,
2230 &f
->header
->n_entries
,
2231 JOURNAL_HEADER_CONTAINS(f
->header
, tail_entry_array_offset
) ? &f
->header
->tail_entry_array_offset
: NULL
,
2232 JOURNAL_HEADER_CONTAINS(f
->header
, tail_entry_array_n_entries
) ? &f
->header
->tail_entry_array_n_entries
: NULL
,
2237 /* log_debug("=> %s seqnr=%"PRIu64" n_entries=%"PRIu64, f->path, o->entry.seqnum, f->header->n_entries); */
2239 if (f
->header
->head_entry_realtime
== 0)
2240 f
->header
->head_entry_realtime
= o
->entry
.realtime
;
2242 f
->header
->tail_entry_realtime
= o
->entry
.realtime
;
2243 f
->header
->tail_entry_monotonic
= o
->entry
.monotonic
;
2244 if (JOURNAL_HEADER_CONTAINS(f
->header
, tail_entry_offset
))
2245 f
->header
->tail_entry_offset
= htole64(offset
);
2246 f
->newest_mtime
= 0; /* we have a new tail entry now, explicitly invalidate newest boot id/timestamp info */
2248 /* Link up the items */
2249 for (uint64_t i
= 0; i
< n_items
; i
++) {
2252 /* If we fail to link an entry item because we can't allocate a new entry array, don't fail
2253 * immediately but try to link the other entry items since it might still be possible to link
2254 * those if they don't require a new entry array to be allocated. */
2256 k
= journal_file_link_entry_item(f
, offset
, items
[i
].object_offset
);
2266 static void write_entry_item(JournalFile
*f
, Object
*o
, uint64_t i
, const EntryItem
*item
) {
2271 if (JOURNAL_HEADER_COMPACT(f
->header
)) {
2272 assert(item
->object_offset
<= UINT32_MAX
);
2273 o
->entry
.items
.compact
[i
].object_offset
= htole32(item
->object_offset
);
2275 o
->entry
.items
.regular
[i
].object_offset
= htole64(item
->object_offset
);
2276 o
->entry
.items
.regular
[i
].hash
= htole64(item
->hash
);
2280 static int journal_file_append_entry_internal(
2282 const dual_timestamp
*ts
,
2283 const sd_id128_t
*boot_id
,
2284 const sd_id128_t
*machine_id
,
2286 const EntryItem items
[],
2289 sd_id128_t
*seqnum_id
,
2290 Object
**ret_object
,
2291 uint64_t *ret_offset
) {
2302 assert(!sd_id128_is_null(*boot_id
));
2303 assert(items
|| n_items
== 0);
2305 if (f
->strict_order
) {
2306 /* If requested be stricter with ordering in this journal file, to make searching via
2307 * bisection fully deterministic. This is an optional feature, so that if desired journal
2308 * files can be written where the ordering is not strictly enforced (in which case bisection
2309 * will yield *a* result, but not the *only* result, when searching for points in
2310 * time). Strict ordering mode is enabled when journald originally writes the files, but
2311 * might not necessarily be if other tools (the remoting tools for example) write journal
2312 * files from combined sources.
2314 * Typically, if any of the errors generated here are seen journald will just rotate the
2315 * journal files and start anew. */
2317 if (ts
->realtime
< le64toh(f
->header
->tail_entry_realtime
))
2318 return log_debug_errno(SYNTHETIC_ERRNO(EREMCHG
),
2319 "Realtime timestamp %" PRIu64
" smaller than previous realtime "
2320 "timestamp %" PRIu64
", refusing entry.",
2321 ts
->realtime
, le64toh(f
->header
->tail_entry_realtime
));
2323 if (sd_id128_equal(*boot_id
, f
->header
->tail_entry_boot_id
) &&
2324 ts
->monotonic
< le64toh(f
->header
->tail_entry_monotonic
))
2325 return log_debug_errno(
2326 SYNTHETIC_ERRNO(ENOTNAM
),
2327 "Monotonic timestamp %" PRIu64
2328 " smaller than previous monotonic timestamp %" PRIu64
2329 " while having the same boot ID, refusing entry.",
2331 le64toh(f
->header
->tail_entry_monotonic
));
2335 /* Settle the passed in sequence number ID */
2337 if (sd_id128_is_null(*seqnum_id
))
2338 *seqnum_id
= f
->header
->seqnum_id
; /* Caller has none assigned, then copy the one from the file */
2339 else if (!sd_id128_equal(*seqnum_id
, f
->header
->seqnum_id
)) {
2340 /* Different seqnum IDs? We can't allow entries from multiple IDs end up in the same journal.*/
2341 if (le64toh(f
->header
->n_entries
) == 0)
2342 f
->header
->seqnum_id
= *seqnum_id
; /* Caller has one, and file so far has no entries, then copy the one from the caller */
2344 return log_debug_errno(SYNTHETIC_ERRNO(EILSEQ
),
2345 "Sequence number IDs don't match, refusing entry.");
2349 if (machine_id
&& sd_id128_is_null(f
->header
->machine_id
))
2350 /* Initialize machine ID when not set yet */
2351 f
->header
->machine_id
= *machine_id
;
2353 osize
= offsetof(Object
, entry
.items
) + (n_items
* journal_file_entry_item_size(f
));
2355 r
= journal_file_append_object(f
, OBJECT_ENTRY
, osize
, &o
, &np
);
2359 o
->entry
.seqnum
= htole64(journal_file_entry_seqnum(f
, seqnum
));
2360 o
->entry
.realtime
= htole64(ts
->realtime
);
2361 o
->entry
.monotonic
= htole64(ts
->monotonic
);
2362 o
->entry
.xor_hash
= htole64(xor_hash
);
2363 o
->entry
.boot_id
= f
->header
->tail_entry_boot_id
= *boot_id
;
2365 for (size_t i
= 0; i
< n_items
; i
++)
2366 write_entry_item(f
, o
, i
, &items
[i
]);
2369 r
= journal_file_hmac_put_object(f
, OBJECT_ENTRY
, o
, np
);
2374 r
= journal_file_link_entry(f
, o
, np
, items
, n_items
);
2387 void journal_file_post_change(JournalFile
*f
) {
2393 /* inotify() does not receive IN_MODIFY events from file
2394 * accesses done via mmap(). After each access we hence
2395 * trigger IN_MODIFY by truncating the journal file to its
2396 * current size which triggers IN_MODIFY. */
2398 __atomic_thread_fence(__ATOMIC_SEQ_CST
);
2400 if (ftruncate(f
->fd
, f
->last_stat
.st_size
) < 0)
2401 log_debug_errno(errno
, "Failed to truncate file to its own size: %m");
2404 static int post_change_thunk(sd_event_source
*timer
, uint64_t usec
, void *userdata
) {
2407 journal_file_post_change(userdata
);
2412 static void schedule_post_change(JournalFile
*f
) {
2417 assert(f
->post_change_timer
);
2419 assert_se(e
= sd_event_source_get_event(f
->post_change_timer
));
2421 /* If we are already going down, post the change immediately. */
2422 if (IN_SET(sd_event_get_state(e
), SD_EVENT_EXITING
, SD_EVENT_FINISHED
))
2425 r
= sd_event_source_get_enabled(f
->post_change_timer
, NULL
);
2427 log_debug_errno(r
, "Failed to get ftruncate timer state: %m");
2433 r
= sd_event_source_set_time_relative(f
->post_change_timer
, f
->post_change_timer_period
);
2435 log_debug_errno(r
, "Failed to set time for scheduling ftruncate: %m");
2439 r
= sd_event_source_set_enabled(f
->post_change_timer
, SD_EVENT_ONESHOT
);
2441 log_debug_errno(r
, "Failed to enable scheduled ftruncate: %m");
2448 /* On failure, let's simply post the change immediately. */
2449 journal_file_post_change(f
);
2452 /* Enable coalesced change posting in a timer on the provided sd_event instance */
2453 int journal_file_enable_post_change_timer(JournalFile
*f
, sd_event
*e
, usec_t t
) {
2454 _cleanup_(sd_event_source_unrefp
) sd_event_source
*timer
= NULL
;
2458 assert_return(!f
->post_change_timer
, -EINVAL
);
2462 r
= sd_event_add_time(e
, &timer
, CLOCK_MONOTONIC
, 0, 0, post_change_thunk
, f
);
2466 r
= sd_event_source_set_enabled(timer
, SD_EVENT_OFF
);
2470 f
->post_change_timer
= TAKE_PTR(timer
);
2471 f
->post_change_timer_period
= t
;
2476 static int entry_item_cmp(const EntryItem
*a
, const EntryItem
*b
) {
2477 return CMP(ASSERT_PTR(a
)->object_offset
, ASSERT_PTR(b
)->object_offset
);
2480 static size_t remove_duplicate_entry_items(EntryItem items
[], size_t n
) {
2483 assert(items
|| n
== 0);
2488 for (size_t i
= 1; i
< n
; i
++)
2489 if (items
[i
].object_offset
!= items
[j
- 1].object_offset
)
2490 items
[j
++] = items
[i
];
2495 int journal_file_append_entry(
2497 const dual_timestamp
*ts
,
2498 const sd_id128_t
*boot_id
,
2499 const struct iovec iovec
[],
2502 sd_id128_t
*seqnum_id
,
2503 Object
**ret_object
,
2504 uint64_t *ret_offset
) {
2506 _cleanup_free_ EntryItem
*items_alloc
= NULL
;
2508 uint64_t xor_hash
= 0;
2509 struct dual_timestamp _ts
;
2510 sd_id128_t _boot_id
, _machine_id
, *machine_id
;
2516 assert(n_iovec
> 0);
2519 if (!VALID_REALTIME(ts
->realtime
))
2520 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2521 "Invalid realtime timestamp %" PRIu64
", refusing entry.",
2523 if (!VALID_MONOTONIC(ts
->monotonic
))
2524 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2525 "Invalid monotomic timestamp %" PRIu64
", refusing entry.",
2528 dual_timestamp_get(&_ts
);
2533 if (sd_id128_is_null(*boot_id
))
2534 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
), "Empty boot ID, refusing entry.");
2536 r
= sd_id128_get_boot(&_boot_id
);
2540 boot_id
= &_boot_id
;
2543 r
= sd_id128_get_machine(&_machine_id
);
2544 if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r
))
2545 /* Gracefully handle the machine ID not being initialized yet */
2550 machine_id
= &_machine_id
;
2553 r
= journal_file_maybe_append_tag(f
, ts
->realtime
);
2558 if (n_iovec
< ALLOCA_MAX
/ sizeof(EntryItem
) / 2)
2559 items
= newa(EntryItem
, n_iovec
);
2561 items_alloc
= new(EntryItem
, n_iovec
);
2565 items
= items_alloc
;
2568 for (size_t i
= 0; i
< n_iovec
; i
++) {
2572 r
= journal_file_append_data(f
, iovec
[i
].iov_base
, iovec
[i
].iov_len
, &o
, &p
);
2576 /* When calculating the XOR hash field, we need to take special care if the "keyed-hash"
2577 * journal file flag is on. We use the XOR hash field to quickly determine the identity of a
2578 * specific record, and give records with otherwise identical position (i.e. match in seqno,
2579 * timestamp, …) a stable ordering. But for that we can't have it that the hash of the
2580 * objects in each file is different since they are keyed. Hence let's calculate the Jenkins
2581 * hash here for that. This also has the benefit that cursors for old and new journal files
2582 * are completely identical (they include the XOR hash after all). For classic Jenkins-hash
2583 * files things are easier, we can just take the value from the stored record directly. */
2585 if (JOURNAL_HEADER_KEYED_HASH(f
->header
))
2586 xor_hash
^= jenkins_hash64(iovec
[i
].iov_base
, iovec
[i
].iov_len
);
2588 xor_hash
^= le64toh(o
->data
.hash
);
2590 items
[i
] = (EntryItem
) {
2592 .hash
= le64toh(o
->data
.hash
),
2596 /* Order by the position on disk, in order to improve seek
2597 * times for rotating media. */
2598 typesafe_qsort(items
, n_iovec
, entry_item_cmp
);
2599 n_iovec
= remove_duplicate_entry_items(items
, n_iovec
);
2601 r
= journal_file_append_entry_internal(
2614 /* If the memory mapping triggered a SIGBUS then we return an
2615 * IO error and ignore the error code passed down to us, since
2616 * it is very likely just an effect of a nullified replacement
2619 if (mmap_cache_fd_got_sigbus(f
->cache_fd
))
2622 if (f
->post_change_timer
)
2623 schedule_post_change(f
);
2625 journal_file_post_change(f
);
2630 typedef struct ChainCacheItem
{
2631 uint64_t first
; /* The offset of the entry array object at the beginning of the chain,
2632 * i.e., le64toh(f->header->entry_array_offset), or le64toh(o->data.entry_offset). */
2633 uint64_t array
; /* The offset of the cached entry array object. */
2634 uint64_t begin
; /* The offset of the first item in the cached array. */
2635 uint64_t total
; /* The total number of items in all arrays before the cached one in the chain. */
2636 uint64_t last_index
; /* The last index we looked at in the cached array, to optimize locality when bisecting. */
2639 static void chain_cache_put(
2646 uint64_t last_index
) {
2651 /* If the chain item to cache for this chain is the
2652 * first one it's not worth caching anything */
2656 if (ordered_hashmap_size(h
) >= CHAIN_CACHE_MAX
) {
2657 ci
= ordered_hashmap_steal_first(h
);
2660 ci
= new(ChainCacheItem
, 1);
2667 if (ordered_hashmap_put(h
, &ci
->first
, ci
) < 0) {
2672 assert(ci
->first
== first
);
2677 ci
->last_index
= last_index
;
2680 static int bump_array_index(uint64_t *i
, direction_t direction
, uint64_t n
) {
2683 /* Increase or decrease the specified index, in the right direction. */
2685 if (direction
== DIRECTION_DOWN
) {
2700 static int bump_entry_array(
2702 Object
*o
, /* the current entry array object. */
2703 uint64_t offset
, /* the offset of the entry array object. */
2704 uint64_t first
, /* The offset of the first entry array object in the chain. */
2705 direction_t direction
,
2713 if (direction
== DIRECTION_DOWN
) {
2715 assert(o
->object
.type
== OBJECT_ENTRY_ARRAY
);
2717 *ret
= le64toh(o
->entry_array
.next_entry_array_offset
);
2720 /* Entry array chains are a singly linked list, so to find the previous array in the chain, we have
2721 * to start iterating from the top. */
2725 uint64_t p
= first
, q
= 0;
2726 while (p
> 0 && p
!= offset
) {
2727 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, p
, &o
);
2732 p
= le64toh(o
->entry_array
.next_entry_array_offset
);
2735 /* If we can't find the previous entry array in the entry array chain, we're likely dealing with a
2736 * corrupted journal file. */
2746 static int generic_array_get(
2748 uint64_t first
, /* The offset of the first entry array object in the chain. */
2749 uint64_t i
, /* The index of the target object counted from the beginning of the entry array chain. */
2750 direction_t direction
,
2751 Object
**ret_object
, /* The found object. */
2752 uint64_t *ret_offset
) { /* The offset of the found object. */
2754 uint64_t a
, t
= 0, k
;
2761 /* FIXME: fix return value assignment on success. */
2765 /* Try the chain cache first */
2766 ci
= ordered_hashmap_get(f
->chain_cache
, &first
);
2767 if (ci
&& i
> ci
->total
) {
2774 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
2775 if (IN_SET(r
, -EBADMSG
, -EADDRNOTAVAIL
)) {
2776 /* If there's corruption and we're going downwards, let's pretend we reached the
2777 * final entry in the entry array chain. */
2779 if (direction
== DIRECTION_DOWN
)
2782 /* If there's corruption and we're going upwards, move back to the previous entry
2783 * array and start iterating entries from there. */
2791 k
= journal_file_entry_array_n_items(f
, o
);
2798 /* The index is larger than the number of elements in the array. Let's move to the next array. */
2801 a
= le64toh(o
->entry_array
.next_entry_array_offset
);
2804 /* If we've found the right location, now look for the first non-corrupt entry object (in the right
2808 if (i
== UINT64_MAX
) {
2809 r
= bump_entry_array(f
, o
, a
, first
, direction
, &a
);
2813 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
2817 k
= journal_file_entry_array_n_items(f
, o
);
2821 if (direction
== DIRECTION_DOWN
)
2824 /* We moved to the previous array. The total must be decreased. */
2826 return -EBADMSG
; /* chain cache is broken ? */
2836 p
= journal_file_entry_array_item(f
, o
, i
);
2838 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, ret_object
);
2840 /* Let's cache this item for the next invocation */
2841 chain_cache_put(f
->chain_cache
, ci
, first
, a
, journal_file_entry_array_item(f
, o
, 0), t
, i
);
2848 if (!IN_SET(r
, -EADDRNOTAVAIL
, -EBADMSG
))
2851 /* OK, so this entry is borked. Most likely some entry didn't get synced to
2852 * disk properly, let's see if the next one might work for us instead. */
2853 log_debug_errno(r
, "Entry item %" PRIu64
" is bad, skipping over it.", i
);
2855 } while (bump_array_index(&i
, direction
, k
) > 0);
2857 /* All entries tried in the above do-while loop are broken. Let's move to the next (or previous) array. */
2859 if (direction
== DIRECTION_DOWN
)
2860 /* We are going to the next array, the total must be incremented. */
2875 static int generic_array_bisect_one(
2877 uint64_t a
, /* offset of entry array object. */
2878 uint64_t i
, /* index of the entry item we will test. */
2880 int (*test_object
)(JournalFile
*f
, uint64_t p
, uint64_t needle
),
2881 direction_t direction
,
2884 uint64_t *ret_offset
) {
2891 assert(test_object
);
2895 assert(i
<= *right
);
2897 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &array
);
2901 p
= journal_file_entry_array_item(f
, array
, i
);
2905 r
= test_object(f
, p
, needle
);
2906 if (IN_SET(r
, -EBADMSG
, -EADDRNOTAVAIL
)) {
2907 log_debug_errno(r
, "Encountered invalid entry while bisecting, cutting algorithm short.");
2909 return -ENOANO
; /* recognizable error */
2914 if (r
== TEST_FOUND
)
2915 r
= direction
== DIRECTION_DOWN
? TEST_RIGHT
: TEST_LEFT
;
2917 if (r
== TEST_RIGHT
)
2928 static int generic_array_bisect(
2933 int (*test_object
)(JournalFile
*f
, uint64_t p
, uint64_t needle
),
2934 direction_t direction
,
2935 Object
**ret_object
,
2936 uint64_t *ret_offset
,
2937 uint64_t *ret_idx
) {
2939 /* Given an entry array chain, this function finds the object "closest" to the given needle in the
2940 * chain, taking into account the provided direction. A function can be provided to determine how
2941 * an object is matched against the given needle.
2943 * Given a journal file, the offset of an object and the needle, the test_object() function should
2944 * return TEST_LEFT if the needle is located earlier in the entry array chain, TEST_LEFT if the
2945 * needle is located later in the entry array chain and TEST_FOUND if the object matches the needle.
2946 * If test_object() returns TEST_FOUND for a specific object, that object's information will be used
2947 * to populate the return values of this function. If test_object() never returns TEST_FOUND, the
2948 * return values are populated with the details of one of the objects closest to the needle. If the
2949 * direction is DIRECTION_UP, the earlier object is used. Otherwise, the later object is used.
2952 uint64_t a
, p
, t
= 0, i
= 0, last_p
= 0, last_index
= UINT64_MAX
;
2953 bool subtract_one
= false;
2959 assert(test_object
);
2961 /* Start with the first array in the chain */
2964 ci
= ordered_hashmap_get(f
->chain_cache
, &first
);
2965 if (ci
&& n
> ci
->total
&& ci
->begin
!= 0) {
2966 /* Ah, we have iterated this bisection array chain previously! Let's see if we can skip ahead
2967 * in the chain, as far as the last time. But we can't jump backwards in the chain, so let's
2968 * check that first. */
2970 r
= test_object(f
, ci
->begin
, needle
);
2974 if (r
== TEST_LEFT
) {
2975 /* OK, what we are looking for is right of the begin of this EntryArray, so let's
2976 * jump straight to previously cached array in the chain */
2981 last_index
= ci
->last_index
;
2986 uint64_t left
= 0, right
, k
, lp
;
2988 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &array
);
2992 k
= journal_file_entry_array_n_items(f
, array
);
2998 r
= generic_array_bisect_one(f
, a
, right
, needle
, test_object
, direction
, &left
, &right
, &lp
);
3006 if (r
== TEST_RIGHT
) {
3007 /* If we cached the last index we looked at, let's try to not to jump too wildly
3008 * around and see if we can limit the range to look at early to the immediate
3009 * neighbors of the last index we looked at. */
3011 if (last_index
> 0 && last_index
- 1 < right
) {
3012 r
= generic_array_bisect_one(f
, a
, last_index
- 1, needle
, test_object
, direction
, &left
, &right
, NULL
);
3013 if (r
< 0 && r
!= -ENOANO
)
3017 if (last_index
< right
) {
3018 r
= generic_array_bisect_one(f
, a
, last_index
+ 1, needle
, test_object
, direction
, &left
, &right
, NULL
);
3019 if (r
< 0 && r
!= -ENOANO
)
3024 if (left
== right
) {
3025 if (direction
== DIRECTION_UP
)
3026 subtract_one
= true;
3032 assert(left
< right
);
3033 i
= (left
+ right
) / 2;
3035 r
= generic_array_bisect_one(f
, a
, i
, needle
, test_object
, direction
, &left
, &right
, NULL
);
3036 if (r
< 0 && r
!= -ENOANO
)
3042 if (direction
== DIRECTION_UP
) {
3044 subtract_one
= true;
3055 last_index
= UINT64_MAX
;
3056 a
= le64toh(array
->entry_array
.next_entry_array_offset
);
3062 if (subtract_one
&& t
== 0 && i
== 0)
3065 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &array
);
3069 p
= journal_file_entry_array_item(f
, array
, 0);
3073 /* Let's cache this item for the next invocation */
3074 chain_cache_put(f
->chain_cache
, ci
, first
, a
, p
, t
, subtract_one
? (i
> 0 ? i
-1 : UINT64_MAX
) : i
);
3076 if (subtract_one
&& i
== 0)
3078 else if (subtract_one
)
3079 p
= journal_file_entry_array_item(f
, array
, i
- 1);
3081 p
= journal_file_entry_array_item(f
, array
, i
);
3084 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, ret_object
);
3093 *ret_idx
= t
+ i
+ (subtract_one
? -1 : 0);
3098 static int generic_array_bisect_plus_one(
3104 int (*test_object
)(JournalFile
*f
, uint64_t p
, uint64_t needle
),
3105 direction_t direction
,
3106 Object
**ret_object
,
3107 uint64_t *ret_offset
) {
3112 assert(test_object
);
3117 /* This bisects the array in object 'first', but first checks an extra. */
3118 r
= test_object(f
, extra
, needle
);
3122 if (direction
== DIRECTION_DOWN
) {
3123 /* If we are going downwards, then we need to return the first object that passes the test.
3124 * When there is no object that passes the test, we need to return the first object that
3125 * test_object() returns TEST_RIGHT for. */
3127 TEST_FOUND
, /* The 'extra' object passes the test. Hence, this is the first
3128 * object that passes the test. */
3129 TEST_RIGHT
)) /* The 'extra' object is the first object that test_object() returns
3130 * TEST_RIGHT for, and no object exists even in the chained arrays
3131 * that passes the test. */
3132 goto use_extra
; /* The 'extra' object is exactly the one we are looking for. It is
3133 * not necessary to bisect the chained arrays. */
3135 /* Otherwise, the 'extra' object is not the one we are looking for. Search in the arrays. */
3138 /* If we are going upwards, then we need to return the last object that passes the test.
3139 * When there is no object that passes the test, we need to return the the last object that
3140 * test_object() returns TEST_LEFT for. */
3141 if (r
== TEST_RIGHT
)
3142 return 0; /* Not only the 'extra' object, but also all objects in the chained arrays
3143 * will never get TEST_FOUND or TEST_LEFT. The object we are looking for
3144 * does not exist. */
3146 /* Even if the 'extra' object passes the test, there may be multiple objects in the arrays
3147 * that also pass the test. Hence, we need to bisect the arrays for finding the last matching
3151 r
= generic_array_bisect(f
, first
, n
-1, needle
, test_object
, direction
, ret_object
, ret_offset
, NULL
);
3153 return r
; /* When > 0, the found object is the first (or last, when DIRECTION_UP) object.
3154 * Hence, return the found object now. */
3156 /* No matching object found in the chained arrays.
3157 * DIRECTION_DOWN : the 'extra' object neither matches the condition. There is no matching object.
3158 * DIRECTION_UP : the 'extra' object matches the condition. So, return it. */
3159 if (direction
== DIRECTION_DOWN
)
3164 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, extra
, ret_object
);
3170 *ret_offset
= extra
;
3175 static int test_object_offset(JournalFile
*f
, uint64_t p
, uint64_t needle
) {
3181 else if (p
< needle
)
3187 int journal_file_move_to_entry_by_offset(
3190 direction_t direction
,
3191 Object
**ret_object
,
3192 uint64_t *ret_offset
) {
3197 return generic_array_bisect(
3199 le64toh(f
->header
->entry_array_offset
),
3200 le64toh(f
->header
->n_entries
),
3204 ret_object
, ret_offset
, NULL
);
3207 static int test_object_seqnum(JournalFile
*f
, uint64_t p
, uint64_t needle
) {
3215 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
3219 sq
= le64toh(READ_NOW(o
->entry
.seqnum
));
3222 else if (sq
< needle
)
3228 int journal_file_move_to_entry_by_seqnum(
3231 direction_t direction
,
3232 Object
**ret_object
,
3233 uint64_t *ret_offset
) {
3238 return generic_array_bisect(
3240 le64toh(f
->header
->entry_array_offset
),
3241 le64toh(f
->header
->n_entries
),
3245 ret_object
, ret_offset
, NULL
);
3248 static int test_object_realtime(JournalFile
*f
, uint64_t p
, uint64_t needle
) {
3256 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
3260 rt
= le64toh(READ_NOW(o
->entry
.realtime
));
3263 else if (rt
< needle
)
3269 int journal_file_move_to_entry_by_realtime(
3272 direction_t direction
,
3273 Object
**ret_object
,
3274 uint64_t *ret_offset
) {
3279 return generic_array_bisect(
3281 le64toh(f
->header
->entry_array_offset
),
3282 le64toh(f
->header
->n_entries
),
3284 test_object_realtime
,
3286 ret_object
, ret_offset
, NULL
);
3289 static int test_object_monotonic(JournalFile
*f
, uint64_t p
, uint64_t needle
) {
3297 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
3301 m
= le64toh(READ_NOW(o
->entry
.monotonic
));
3304 else if (m
< needle
)
3310 static int find_data_object_by_boot_id(
3313 Object
**ret_object
,
3314 uint64_t *ret_offset
) {
3316 char t
[STRLEN("_BOOT_ID=") + 32 + 1] = "_BOOT_ID=";
3320 sd_id128_to_string(boot_id
, t
+ 9);
3321 return journal_file_find_data_object(f
, t
, sizeof(t
) - 1, ret_object
, ret_offset
);
3324 int journal_file_move_to_entry_by_monotonic(
3328 direction_t direction
,
3329 Object
**ret_object
,
3330 uint64_t *ret_offset
) {
3337 r
= find_data_object_by_boot_id(f
, boot_id
, &o
, NULL
);
3341 return generic_array_bisect_plus_one(
3343 le64toh(o
->data
.entry_offset
),
3344 le64toh(o
->data
.entry_array_offset
),
3345 le64toh(o
->data
.n_entries
),
3347 test_object_monotonic
,
3349 ret_object
, ret_offset
);
3352 void journal_file_reset_location(JournalFile
*f
) {
3355 f
->location_type
= LOCATION_HEAD
;
3356 f
->current_offset
= 0;
3357 f
->current_seqnum
= 0;
3358 f
->current_realtime
= 0;
3359 f
->current_monotonic
= 0;
3360 zero(f
->current_boot_id
);
3361 f
->current_xor_hash
= 0;
3363 /* Also reset the previous reading direction. Otherwise, next_beyond_location() may wrongly handle we
3364 * already hit EOF. See issue #29216. */
3365 f
->last_direction
= _DIRECTION_INVALID
;
3368 void journal_file_save_location(JournalFile
*f
, Object
*o
, uint64_t offset
) {
3372 f
->location_type
= LOCATION_SEEK
;
3373 f
->current_offset
= offset
;
3374 f
->current_seqnum
= le64toh(o
->entry
.seqnum
);
3375 f
->current_realtime
= le64toh(o
->entry
.realtime
);
3376 f
->current_monotonic
= le64toh(o
->entry
.monotonic
);
3377 f
->current_boot_id
= o
->entry
.boot_id
;
3378 f
->current_xor_hash
= le64toh(o
->entry
.xor_hash
);
3381 static bool check_properly_ordered(uint64_t new_offset
, uint64_t old_offset
, direction_t direction
) {
3383 /* Consider it an error if any of the two offsets is uninitialized */
3384 if (old_offset
== 0 || new_offset
== 0)
3387 /* If we go down, the new offset must be larger than the old one. */
3388 return direction
== DIRECTION_DOWN
?
3389 new_offset
> old_offset
:
3390 new_offset
< old_offset
;
3393 int journal_file_next_entry(
3396 direction_t direction
,
3397 Object
**ret_object
,
3398 uint64_t *ret_offset
) {
3407 /* FIXME: fix return value assignment. */
3409 n
= le64toh(READ_NOW(f
->header
->n_entries
));
3413 /* When the input offset 'p' is zero, return the first (or last on DIRECTION_UP) entry. */
3415 return generic_array_get(f
,
3416 le64toh(f
->header
->entry_array_offset
),
3417 direction
== DIRECTION_DOWN
? 0 : n
- 1,
3419 ret_object
, ret_offset
);
3421 /* Otherwise, first find the nearest entry object. */
3422 r
= generic_array_bisect(f
,
3423 le64toh(f
->header
->entry_array_offset
),
3424 le64toh(f
->header
->n_entries
),
3428 ret_object
? &o
: NULL
, &q
, &i
);
3432 assert(direction
== DIRECTION_DOWN
? p
<= q
: q
<= p
);
3434 /* If the input offset 'p' points to an entry object, generic_array_bisect() should provides
3435 * the same offset, and the index needs to be shifted. Otherwise, use the found object as is,
3436 * as it is the nearest entry object from the input offset 'p'. */
3441 r
= bump_array_index(&i
, direction
, n
);
3445 /* And jump to it */
3446 r
= generic_array_get(f
, le64toh(f
->header
->entry_array_offset
), i
, direction
, ret_object
? &o
: NULL
, &q
);
3450 /* Ensure our array is properly ordered. */
3451 if (!check_properly_ordered(q
, p
, direction
))
3452 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
3453 "%s: entry array not properly ordered at entry index %" PRIu64
,
3464 int journal_file_move_to_entry_for_data(
3467 direction_t direction
,
3468 Object
**ret_object
,
3469 uint64_t *ret_offset
) {
3471 uint64_t extra
, first
, n
;
3476 assert(d
->object
.type
== OBJECT_DATA
);
3477 assert(IN_SET(direction
, DIRECTION_DOWN
, DIRECTION_UP
));
3479 /* FIXME: fix return value assignment. */
3481 /* This returns the first (when the direction is down, otherwise the last) entry linked to the
3482 * specified data object. */
3484 n
= le64toh(d
->data
.n_entries
);
3487 n
--; /* n_entries is the number of entries linked to the data object, including the 'extra' entry. */
3489 extra
= le64toh(d
->data
.entry_offset
);
3490 first
= le64toh(d
->data
.entry_array_offset
);
3492 if (direction
== DIRECTION_DOWN
&& extra
> 0) {
3493 /* When we are going downwards, first try to read the extra entry. */
3494 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, extra
, ret_object
);
3497 if (!IN_SET(r
, -EADDRNOTAVAIL
, -EBADMSG
))
3502 /* DIRECTION_DOWN : The extra entry is broken, falling back to the entries in the array.
3503 * DIRECTION_UP : Try to find a valid entry in the array from the tail. */
3504 r
= generic_array_get(f
,
3506 direction
== DIRECTION_DOWN
? 0 : n
- 1,
3508 ret_object
, ret_offset
);
3509 if (!IN_SET(r
, 0, -EADDRNOTAVAIL
, -EBADMSG
))
3510 return r
; /* found or critical error. */
3513 if (direction
== DIRECTION_UP
&& extra
> 0) {
3514 /* No valid entry exists in the chained array, falling back to the extra entry. */
3515 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, extra
, ret_object
);
3524 *ret_offset
= extra
;
3529 int journal_file_move_to_entry_by_offset_for_data(
3533 direction_t direction
,
3534 Object
**ret
, uint64_t *ret_offset
) {
3538 assert(d
->object
.type
== OBJECT_DATA
);
3540 return generic_array_bisect_plus_one(
3542 le64toh(d
->data
.entry_offset
),
3543 le64toh(d
->data
.entry_array_offset
),
3544 le64toh(d
->data
.n_entries
),
3551 int journal_file_move_to_entry_by_monotonic_for_data(
3556 direction_t direction
,
3557 Object
**ret_object
,
3558 uint64_t *ret_offset
) {
3560 uint64_t z
, entry_offset
, entry_array_offset
, n_entries
;
3566 assert(d
->object
.type
== OBJECT_DATA
);
3568 /* Save all the required data before the data object gets invalidated. */
3569 entry_offset
= le64toh(READ_NOW(d
->data
.entry_offset
));
3570 entry_array_offset
= le64toh(READ_NOW(d
->data
.entry_array_offset
));
3571 n_entries
= le64toh(READ_NOW(d
->data
.n_entries
));
3573 /* First, seek by time */
3574 r
= find_data_object_by_boot_id(f
, boot_id
, &o
, NULL
);
3578 r
= generic_array_bisect_plus_one(f
,
3579 le64toh(o
->data
.entry_offset
),
3580 le64toh(o
->data
.entry_array_offset
),
3581 le64toh(o
->data
.n_entries
),
3583 test_object_monotonic
,
3589 /* And now, continue seeking until we find an entry that exists in both bisection arrays. */
3593 /* The journal entry found by the above bisect_plus_one() may not have the specified data,
3594 * that is, it may not be linked in the data object. So, we need to check that. */
3596 r
= generic_array_bisect_plus_one(f
,
3603 ret_object
? &entry
: NULL
, &p
);
3607 break; /* The journal entry has the specified data. Yay! */
3609 /* If the entry does not have the data, then move to the next (or previous, depends on the
3610 * 'direction') entry linked to the data object. But, the next entry may be in another boot.
3611 * So, we need to check that the entry has the matching boot ID. */
3613 r
= generic_array_bisect_plus_one(f
,
3614 le64toh(o
->data
.entry_offset
),
3615 le64toh(o
->data
.entry_array_offset
),
3616 le64toh(o
->data
.n_entries
),
3620 ret_object
? &entry
: NULL
, &z
);
3624 break; /* The journal entry has the specified boot ID. Yay! */
3626 /* If not, let's try to the next entry... */
3630 *ret_object
= entry
;
3636 int journal_file_move_to_entry_by_seqnum_for_data(
3640 direction_t direction
,
3641 Object
**ret_object
,
3642 uint64_t *ret_offset
) {
3646 assert(d
->object
.type
== OBJECT_DATA
);
3648 return generic_array_bisect_plus_one(
3650 le64toh(d
->data
.entry_offset
),
3651 le64toh(d
->data
.entry_array_offset
),
3652 le64toh(d
->data
.n_entries
),
3656 ret_object
, ret_offset
);
3659 int journal_file_move_to_entry_by_realtime_for_data(
3663 direction_t direction
,
3664 Object
**ret
, uint64_t *ret_offset
) {
3668 assert(d
->object
.type
== OBJECT_DATA
);
3670 return generic_array_bisect_plus_one(
3672 le64toh(d
->data
.entry_offset
),
3673 le64toh(d
->data
.entry_array_offset
),
3674 le64toh(d
->data
.n_entries
),
3676 test_object_realtime
,
3681 void journal_file_dump(JournalFile
*f
) {
3689 journal_file_print_header(f
);
3691 p
= le64toh(READ_NOW(f
->header
->header_size
));
3696 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, p
, &o
);
3700 s
= journal_object_type_to_string(o
->object
.type
);
3702 switch (o
->object
.type
) {
3707 printf("Type: %s seqnum=%"PRIu64
" monotonic=%"PRIu64
" realtime=%"PRIu64
"\n",
3709 le64toh(o
->entry
.seqnum
),
3710 le64toh(o
->entry
.monotonic
),
3711 le64toh(o
->entry
.realtime
));
3717 printf("Type: %s seqnum=%"PRIu64
" epoch=%"PRIu64
"\n",
3719 le64toh(o
->tag
.seqnum
),
3720 le64toh(o
->tag
.epoch
));
3725 printf("Type: %s \n", s
);
3727 printf("Type: unknown (%i)", o
->object
.type
);
3732 c
= COMPRESSION_FROM_OBJECT(o
);
3733 if (c
> COMPRESSION_NONE
)
3734 printf("Flags: %s\n",
3735 compression_to_string(c
));
3737 if (p
== le64toh(f
->header
->tail_object_offset
))
3740 p
+= ALIGN64(le64toh(o
->object
.size
));
3745 log_error("File corrupt");
3748 /* Note: the lifetime of the compound literal is the immediately surrounding block. */
3749 #define FORMAT_TIMESTAMP_SAFE(t) (FORMAT_TIMESTAMP(t) ?: " --- ")
3751 void journal_file_print_header(JournalFile
*f
) {
3757 printf("File path: %s\n"
3761 "Sequential number ID: %s\n"
3763 "Compatible flags:%s%s%s\n"
3764 "Incompatible flags:%s%s%s%s%s%s\n"
3765 "Header size: %"PRIu64
"\n"
3766 "Arena size: %"PRIu64
"\n"
3767 "Data hash table size: %"PRIu64
"\n"
3768 "Field hash table size: %"PRIu64
"\n"
3769 "Rotate suggested: %s\n"
3770 "Head sequential number: %"PRIu64
" (%"PRIx64
")\n"
3771 "Tail sequential number: %"PRIu64
" (%"PRIx64
")\n"
3772 "Head realtime timestamp: %s (%"PRIx64
")\n"
3773 "Tail realtime timestamp: %s (%"PRIx64
")\n"
3774 "Tail monotonic timestamp: %s (%"PRIx64
")\n"
3775 "Objects: %"PRIu64
"\n"
3776 "Entry objects: %"PRIu64
"\n",
3778 SD_ID128_TO_STRING(f
->header
->file_id
),
3779 SD_ID128_TO_STRING(f
->header
->machine_id
),
3780 SD_ID128_TO_STRING(f
->header
->tail_entry_boot_id
),
3781 SD_ID128_TO_STRING(f
->header
->seqnum_id
),
3782 f
->header
->state
== STATE_OFFLINE
? "OFFLINE" :
3783 f
->header
->state
== STATE_ONLINE
? "ONLINE" :
3784 f
->header
->state
== STATE_ARCHIVED
? "ARCHIVED" : "UNKNOWN",
3785 JOURNAL_HEADER_SEALED(f
->header
) ? " SEALED" : "",
3786 JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f
->header
) ? " TAIL_ENTRY_BOOT_ID" : "",
3787 (le32toh(f
->header
->compatible_flags
) & ~HEADER_COMPATIBLE_ANY
) ? " ???" : "",
3788 JOURNAL_HEADER_COMPRESSED_XZ(f
->header
) ? " COMPRESSED-XZ" : "",
3789 JOURNAL_HEADER_COMPRESSED_LZ4(f
->header
) ? " COMPRESSED-LZ4" : "",
3790 JOURNAL_HEADER_COMPRESSED_ZSTD(f
->header
) ? " COMPRESSED-ZSTD" : "",
3791 JOURNAL_HEADER_KEYED_HASH(f
->header
) ? " KEYED-HASH" : "",
3792 JOURNAL_HEADER_COMPACT(f
->header
) ? " COMPACT" : "",
3793 (le32toh(f
->header
->incompatible_flags
) & ~HEADER_INCOMPATIBLE_ANY
) ? " ???" : "",
3794 le64toh(f
->header
->header_size
),
3795 le64toh(f
->header
->arena_size
),
3796 le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
),
3797 le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
),
3798 yes_no(journal_file_rotate_suggested(f
, 0, LOG_DEBUG
)),
3799 le64toh(f
->header
->head_entry_seqnum
), le64toh(f
->header
->head_entry_seqnum
),
3800 le64toh(f
->header
->tail_entry_seqnum
), le64toh(f
->header
->tail_entry_seqnum
),
3801 FORMAT_TIMESTAMP_SAFE(le64toh(f
->header
->head_entry_realtime
)), le64toh(f
->header
->head_entry_realtime
),
3802 FORMAT_TIMESTAMP_SAFE(le64toh(f
->header
->tail_entry_realtime
)), le64toh(f
->header
->tail_entry_realtime
),
3803 FORMAT_TIMESPAN(le64toh(f
->header
->tail_entry_monotonic
), USEC_PER_MSEC
), le64toh(f
->header
->tail_entry_monotonic
),
3804 le64toh(f
->header
->n_objects
),
3805 le64toh(f
->header
->n_entries
));
3807 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_data
))
3808 printf("Data objects: %"PRIu64
"\n"
3809 "Data hash table fill: %.1f%%\n",
3810 le64toh(f
->header
->n_data
),
3811 100.0 * (double) le64toh(f
->header
->n_data
) / ((double) (le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
))));
3813 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_fields
))
3814 printf("Field objects: %"PRIu64
"\n"
3815 "Field hash table fill: %.1f%%\n",
3816 le64toh(f
->header
->n_fields
),
3817 100.0 * (double) le64toh(f
->header
->n_fields
) / ((double) (le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
))));
3819 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_tags
))
3820 printf("Tag objects: %"PRIu64
"\n",
3821 le64toh(f
->header
->n_tags
));
3822 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_entry_arrays
))
3823 printf("Entry array objects: %"PRIu64
"\n",
3824 le64toh(f
->header
->n_entry_arrays
));
3826 if (JOURNAL_HEADER_CONTAINS(f
->header
, field_hash_chain_depth
))
3827 printf("Deepest field hash chain: %" PRIu64
"\n",
3828 f
->header
->field_hash_chain_depth
);
3830 if (JOURNAL_HEADER_CONTAINS(f
->header
, data_hash_chain_depth
))
3831 printf("Deepest data hash chain: %" PRIu64
"\n",
3832 f
->header
->data_hash_chain_depth
);
3834 if (fstat(f
->fd
, &st
) >= 0)
3835 printf("Disk usage: %s\n", FORMAT_BYTES((uint64_t) st
.st_blocks
* 512ULL));
3838 static int journal_file_warn_btrfs(JournalFile
*f
) {
3844 /* Before we write anything, check if the COW logic is turned
3845 * off on btrfs. Given our write pattern that is quite
3846 * unfriendly to COW file systems this should greatly improve
3847 * performance on COW file systems, such as btrfs, at the
3848 * expense of data integrity features (which shouldn't be too
3849 * bad, given that we do our own checksumming). */
3851 r
= fd_is_fs_type(f
->fd
, BTRFS_SUPER_MAGIC
);
3853 return log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
, "Failed to determine if journal is on btrfs: %m");
3857 r
= read_attr_fd(f
->fd
, &attrs
);
3859 return log_ratelimit_warning_errno(r
, JOURNAL_LOG_RATELIMIT
, "Failed to read file attributes: %m");
3861 if (attrs
& FS_NOCOW_FL
) {
3862 log_debug("Detected btrfs file system with copy-on-write disabled, all is good.");
3866 log_ratelimit_notice(JOURNAL_LOG_RATELIMIT
,
3867 "Creating journal file %s on a btrfs file system, and copy-on-write is enabled. "
3868 "This is likely to slow down journal access substantially, please consider turning "
3869 "off the copy-on-write file attribute on the journal directory, using chattr +C.",
3875 static void journal_default_metrics(JournalMetrics
*m
, int fd
, bool compact
) {
3877 uint64_t fs_size
= 0;
3882 if (fstatvfs(fd
, &ss
) >= 0)
3883 fs_size
= ss
.f_frsize
* ss
.f_blocks
;
3885 log_debug_errno(errno
, "Failed to determine disk size: %m");
3887 if (m
->max_use
== UINT64_MAX
) {
3890 m
->max_use
= CLAMP(PAGE_ALIGN(fs_size
/ 10), /* 10% of file system size */
3891 MAX_USE_LOWER
, MAX_USE_UPPER
);
3893 m
->max_use
= MAX_USE_LOWER
;
3895 m
->max_use
= PAGE_ALIGN(m
->max_use
);
3897 if (m
->max_use
!= 0 && m
->max_use
< JOURNAL_FILE_SIZE_MIN
*2)
3898 m
->max_use
= JOURNAL_FILE_SIZE_MIN
*2;
3901 if (m
->min_use
== UINT64_MAX
) {
3903 m
->min_use
= CLAMP(PAGE_ALIGN(fs_size
/ 50), /* 2% of file system size */
3904 MIN_USE_LOW
, MIN_USE_HIGH
);
3906 m
->min_use
= MIN_USE_LOW
;
3909 if (m
->min_use
> m
->max_use
)
3910 m
->min_use
= m
->max_use
;
3912 if (m
->max_size
== UINT64_MAX
)
3913 m
->max_size
= MIN(PAGE_ALIGN(m
->max_use
/ 8), /* 8 chunks */
3916 m
->max_size
= PAGE_ALIGN(m
->max_size
);
3918 if (compact
&& m
->max_size
> JOURNAL_COMPACT_SIZE_MAX
)
3919 m
->max_size
= JOURNAL_COMPACT_SIZE_MAX
;
3921 if (m
->max_size
!= 0) {
3922 if (m
->max_size
< JOURNAL_FILE_SIZE_MIN
)
3923 m
->max_size
= JOURNAL_FILE_SIZE_MIN
;
3925 if (m
->max_use
!= 0 && m
->max_size
*2 > m
->max_use
)
3926 m
->max_use
= m
->max_size
*2;
3929 if (m
->min_size
== UINT64_MAX
)
3930 m
->min_size
= JOURNAL_FILE_SIZE_MIN
;
3932 m
->min_size
= CLAMP(PAGE_ALIGN(m
->min_size
),
3933 JOURNAL_FILE_SIZE_MIN
,
3934 m
->max_size
?: UINT64_MAX
);
3936 if (m
->keep_free
== UINT64_MAX
) {
3938 m
->keep_free
= MIN(PAGE_ALIGN(fs_size
/ 20), /* 5% of file system size */
3941 m
->keep_free
= DEFAULT_KEEP_FREE
;
3944 if (m
->n_max_files
== UINT64_MAX
)
3945 m
->n_max_files
= DEFAULT_N_MAX_FILES
;
3947 log_debug("Fixed min_use=%s max_use=%s max_size=%s min_size=%s keep_free=%s n_max_files=%" PRIu64
,
3948 FORMAT_BYTES(m
->min_use
),
3949 FORMAT_BYTES(m
->max_use
),
3950 FORMAT_BYTES(m
->max_size
),
3951 FORMAT_BYTES(m
->min_size
),
3952 FORMAT_BYTES(m
->keep_free
),
3956 int journal_file_open(
3960 JournalFileFlags file_flags
,
3962 uint64_t compress_threshold_bytes
,
3963 JournalMetrics
*metrics
,
3964 MMapCache
*mmap_cache
,
3965 JournalFile
*template,
3966 JournalFile
**ret
) {
3968 bool newly_created
= false;
3973 assert(fd
>= 0 || fname
);
3974 assert(file_flags
>= 0);
3975 assert(file_flags
<= _JOURNAL_FILE_FLAGS_MAX
);
3979 if (!IN_SET((open_flags
& O_ACCMODE
), O_RDONLY
, O_RDWR
))
3982 if ((open_flags
& O_ACCMODE
) == O_RDONLY
&& FLAGS_SET(open_flags
, O_CREAT
))
3985 if (fname
&& (open_flags
& O_CREAT
) && !endswith(fname
, ".journal"))
3988 f
= new(JournalFile
, 1);
3992 *f
= (JournalFile
) {
3995 .open_flags
= open_flags
,
3996 .compress_threshold_bytes
= compress_threshold_bytes
== UINT64_MAX
?
3997 DEFAULT_COMPRESS_THRESHOLD
:
3998 MAX(MIN_COMPRESS_THRESHOLD
, compress_threshold_bytes
),
3999 .strict_order
= FLAGS_SET(file_flags
, JOURNAL_STRICT_ORDER
),
4000 .newest_boot_id_prioq_idx
= PRIOQ_IDX_NULL
,
4001 .last_direction
= _DIRECTION_INVALID
,
4005 f
->path
= strdup(fname
);
4013 /* If we don't know the path, fill in something explanatory and vaguely useful */
4014 if (asprintf(&f
->path
, "/proc/self/%i", fd
) < 0) {
4020 f
->chain_cache
= ordered_hashmap_new(&uint64_hash_ops
);
4021 if (!f
->chain_cache
) {
4027 /* We pass O_NONBLOCK here, so that in case somebody pointed us to some character device node or FIFO
4028 * or so, we likely fail quickly than block for long. For regular files O_NONBLOCK has no effect, hence
4029 * it doesn't hurt in that case. */
4031 f
->fd
= openat_report_new(AT_FDCWD
, f
->path
, f
->open_flags
|O_CLOEXEC
|O_NONBLOCK
, f
->mode
, &newly_created
);
4037 /* fds we opened here by us should also be closed by us. */
4040 r
= fd_nonblock(f
->fd
, false);
4044 if (!newly_created
) {
4045 r
= journal_file_fstat(f
);
4050 r
= journal_file_fstat(f
);
4054 /* If we just got the fd passed in, we don't really know if we created the file anew */
4055 newly_created
= f
->last_stat
.st_size
== 0 && journal_file_writable(f
);
4058 r
= mmap_cache_add_fd(mmap_cache
, f
->fd
, mmap_prot_from_open_flags(open_flags
), &f
->cache_fd
);
4062 if (newly_created
) {
4063 (void) journal_file_warn_btrfs(f
);
4065 /* Let's attach the creation time to the journal file, so that the vacuuming code knows the age of this
4066 * file even if the file might end up corrupted one day... Ideally we'd just use the creation time many
4067 * file systems maintain for each file, but the API to query this is very new, hence let's emulate this
4068 * via extended attributes. If extended attributes are not supported we'll just skip this, and rely
4069 * solely on mtime/atime/ctime of the file. */
4070 (void) fd_setcrtime(f
->fd
, 0);
4072 r
= journal_file_init_header(f
, file_flags
, template);
4076 r
= journal_file_fstat(f
);
4081 if (f
->last_stat
.st_size
< (off_t
) HEADER_SIZE_MIN
) {
4086 r
= mmap_cache_fd_get(f
->cache_fd
, CONTEXT_HEADER
, true, 0, PAGE_ALIGN(sizeof(Header
)), &f
->last_stat
, &h
);
4088 /* Some file systems (jffs2 or p9fs) don't support mmap() properly (or only read-only
4089 * mmap()), and return EINVAL in that case. Let's propagate that as a more recognizable error
4099 if (!newly_created
) {
4100 r
= journal_file_verify_header(f
);
4106 if (!newly_created
&& journal_file_writable(f
) && JOURNAL_HEADER_SEALED(f
->header
)) {
4107 r
= journal_file_fss_load(f
);
4113 if (journal_file_writable(f
)) {
4115 journal_default_metrics(metrics
, f
->fd
, JOURNAL_HEADER_COMPACT(f
->header
));
4116 f
->metrics
= *metrics
;
4117 } else if (template)
4118 f
->metrics
= template->metrics
;
4120 r
= journal_file_refresh_header(f
);
4126 r
= journal_file_hmac_setup(f
);
4131 if (newly_created
) {
4132 r
= journal_file_setup_field_hash_table(f
);
4136 r
= journal_file_setup_data_hash_table(f
);
4141 r
= journal_file_append_first_tag(f
);
4147 if (mmap_cache_fd_got_sigbus(f
->cache_fd
)) {
4152 if (template && template->post_change_timer
) {
4153 r
= journal_file_enable_post_change_timer(
4155 sd_event_source_get_event(template->post_change_timer
),
4156 template->post_change_timer_period
);
4162 /* The file is opened now successfully, thus we take possession of any passed in fd. */
4165 if (DEBUG_LOGGING
) {
4166 static int last_seal
= -1, last_keyed_hash
= -1;
4167 static Compression last_compression
= _COMPRESSION_INVALID
;
4168 static uint64_t last_bytes
= UINT64_MAX
;
4170 if (last_seal
!= JOURNAL_HEADER_SEALED(f
->header
) ||
4171 last_keyed_hash
!= JOURNAL_HEADER_KEYED_HASH(f
->header
) ||
4172 last_compression
!= JOURNAL_FILE_COMPRESSION(f
) ||
4173 last_bytes
!= f
->compress_threshold_bytes
) {
4175 log_debug("Journal effective settings seal=%s keyed_hash=%s compress=%s compress_threshold_bytes=%s",
4176 yes_no(JOURNAL_HEADER_SEALED(f
->header
)), yes_no(JOURNAL_HEADER_KEYED_HASH(f
->header
)),
4177 compression_to_string(JOURNAL_FILE_COMPRESSION(f
)), FORMAT_BYTES(f
->compress_threshold_bytes
));
4178 last_seal
= JOURNAL_HEADER_SEALED(f
->header
);
4179 last_keyed_hash
= JOURNAL_HEADER_KEYED_HASH(f
->header
);
4180 last_compression
= JOURNAL_FILE_COMPRESSION(f
);
4181 last_bytes
= f
->compress_threshold_bytes
;
4189 if (f
->cache_fd
&& mmap_cache_fd_got_sigbus(f
->cache_fd
))
4192 (void) journal_file_close(f
);
4194 if (newly_created
&& fd
< 0)
4195 (void) unlink(fname
);
4200 int journal_file_parse_uid_from_filename(const char *path
, uid_t
*ret_uid
) {
4201 _cleanup_free_
char *buf
= NULL
, *p
= NULL
;
4202 const char *a
, *b
, *at
;
4205 /* This helper returns -EREMOTE when the filename doesn't match user online/offline journal
4206 * pattern. Hence it currently doesn't parse archived or disposed user journals. */
4211 r
= path_extract_filename(path
, &p
);
4214 if (r
== O_DIRECTORY
)
4217 a
= startswith(p
, "user-");
4220 b
= endswith(p
, ".journal");
4224 at
= strchr(a
, '@');
4228 buf
= strndup(a
, b
-a
);
4232 return parse_uid(buf
, ret_uid
);
4235 int journal_file_archive(JournalFile
*f
, char **ret_previous_path
) {
4236 _cleanup_free_
char *p
= NULL
;
4240 if (!journal_file_writable(f
))
4243 /* Is this a journal file that was passed to us as fd? If so, we synthesized a path name for it, and we refuse
4244 * rotation, since we don't know the actual path, and couldn't rename the file hence. */
4245 if (path_startswith(f
->path
, "/proc/self/fd"))
4248 if (!endswith(f
->path
, ".journal"))
4251 if (asprintf(&p
, "%.*s@" SD_ID128_FORMAT_STR
"-%016"PRIx64
"-%016"PRIx64
".journal",
4252 (int) strlen(f
->path
) - 8, f
->path
,
4253 SD_ID128_FORMAT_VAL(f
->header
->seqnum_id
),
4254 le64toh(f
->header
->head_entry_seqnum
),
4255 le64toh(f
->header
->head_entry_realtime
)) < 0)
4258 /* Try to rename the file to the archived version. If the file already was deleted, we'll get ENOENT, let's
4259 * ignore that case. */
4260 if (rename(f
->path
, p
) < 0 && errno
!= ENOENT
)
4263 /* Sync the rename to disk */
4264 (void) fsync_directory_of_file(f
->fd
);
4266 if (ret_previous_path
)
4267 *ret_previous_path
= f
->path
;
4271 f
->path
= TAKE_PTR(p
);
4273 /* Set as archive so offlining commits w/state=STATE_ARCHIVED. Previously we would set old_file->header->state
4274 * to STATE_ARCHIVED directly here, but journal_file_set_offline() short-circuits when state != STATE_ONLINE,
4275 * which would result in the rotated journal never getting fsync() called before closing. Now we simply queue
4276 * the archive state by setting an archive bit, leaving the state as STATE_ONLINE so proper offlining
4283 int journal_file_dispose(int dir_fd
, const char *fname
) {
4284 _cleanup_free_
char *p
= NULL
;
4288 /* Renames a journal file to *.journal~, i.e. to mark it as corrupted or otherwise uncleanly shutdown. Note that
4289 * this is done without looking into the file or changing any of its contents. The idea is that this is called
4290 * whenever something is suspicious and we want to move the file away and make clear that it is not accessed
4291 * for writing anymore. */
4293 if (!endswith(fname
, ".journal"))
4296 if (asprintf(&p
, "%.*s@%016" PRIx64
"-%016" PRIx64
".journal~",
4297 (int) strlen(fname
) - 8, fname
,
4298 now(CLOCK_REALTIME
),
4302 if (renameat(dir_fd
, fname
, dir_fd
, p
) < 0)
4308 int journal_file_copy_entry(
4314 sd_id128_t
*seqnum_id
) {
4316 _cleanup_free_ EntryItem
*items_alloc
= NULL
;
4318 uint64_t n
, m
= 0, xor_hash
= 0;
4328 if (!journal_file_writable(to
))
4331 ts
= (dual_timestamp
) {
4332 .monotonic
= le64toh(o
->entry
.monotonic
),
4333 .realtime
= le64toh(o
->entry
.realtime
),
4335 boot_id
= o
->entry
.boot_id
;
4337 n
= journal_file_entry_n_items(from
, o
);
4341 if (n
< ALLOCA_MAX
/ sizeof(EntryItem
) / 2)
4342 items
= newa(EntryItem
, n
);
4344 items_alloc
= new(EntryItem
, n
);
4348 items
= items_alloc
;
4351 for (uint64_t i
= 0; i
< n
; i
++) {
4357 q
= journal_file_entry_item_object_offset(from
, o
, i
);
4358 r
= journal_file_data_payload(from
, NULL
, q
, NULL
, 0, 0, &data
, &l
);
4359 if (IN_SET(r
, -EADDRNOTAVAIL
, -EBADMSG
)) {
4360 log_debug_errno(r
, "Entry item %"PRIu64
" data object is bad, skipping over it: %m", i
);
4370 r
= journal_file_append_data(to
, data
, l
, &u
, &h
);
4374 if (JOURNAL_HEADER_KEYED_HASH(to
->header
))
4375 xor_hash
^= jenkins_hash64(data
, l
);
4377 xor_hash
^= le64toh(u
->data
.hash
);
4379 items
[m
++] = (EntryItem
) {
4381 .hash
= le64toh(u
->data
.hash
),
4388 r
= journal_file_append_entry_internal(
4392 &from
->header
->machine_id
,
4398 /* ret_object= */ NULL
,
4399 /* ret_offset= */ NULL
);
4401 if (mmap_cache_fd_got_sigbus(to
->cache_fd
))
4407 void journal_reset_metrics(JournalMetrics
*m
) {
4410 /* Set everything to "pick automatic values". */
4412 *m
= (JournalMetrics
) {
4413 .min_use
= UINT64_MAX
,
4414 .max_use
= UINT64_MAX
,
4415 .min_size
= UINT64_MAX
,
4416 .max_size
= UINT64_MAX
,
4417 .keep_free
= UINT64_MAX
,
4418 .n_max_files
= UINT64_MAX
,
4422 int journal_file_get_cutoff_realtime_usec(JournalFile
*f
, usec_t
*ret_from
, usec_t
*ret_to
) {
4425 assert(ret_from
|| ret_to
);
4428 if (f
->header
->head_entry_realtime
== 0)
4431 *ret_from
= le64toh(f
->header
->head_entry_realtime
);
4435 if (f
->header
->tail_entry_realtime
== 0)
4438 *ret_to
= le64toh(f
->header
->tail_entry_realtime
);
4444 int journal_file_get_cutoff_monotonic_usec(JournalFile
*f
, sd_id128_t boot_id
, usec_t
*ret_from
, usec_t
*ret_to
) {
4450 assert(ret_from
|| ret_to
);
4452 /* FIXME: fix return value assignment on success with 0. */
4454 r
= find_data_object_by_boot_id(f
, boot_id
, &o
, &p
);
4458 if (le64toh(o
->data
.n_entries
) <= 0)
4462 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, le64toh(o
->data
.entry_offset
), &o
);
4466 *ret_from
= le64toh(o
->entry
.monotonic
);
4470 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
4474 r
= journal_file_move_to_entry_for_data(f
, o
, DIRECTION_UP
, &o
, NULL
);
4478 *ret_to
= le64toh(o
->entry
.monotonic
);
4484 bool journal_file_rotate_suggested(JournalFile
*f
, usec_t max_file_usec
, int log_level
) {
4488 /* If we gained new header fields we gained new features,
4489 * hence suggest a rotation */
4490 if (le64toh(f
->header
->header_size
) < sizeof(Header
)) {
4491 log_ratelimit_full(log_level
, JOURNAL_LOG_RATELIMIT
,
4492 "%s uses an outdated header, suggesting rotation.", f
->path
);
4496 /* Let's check if the hash tables grew over a certain fill level (75%, borrowing this value from
4497 * Java's hash table implementation), and if so suggest a rotation. To calculate the fill level we
4498 * need the n_data field, which only exists in newer versions. */
4500 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_data
))
4501 if (le64toh(f
->header
->n_data
) * 4ULL > (le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
)) * 3ULL) {
4503 log_level
, JOURNAL_LOG_RATELIMIT
,
4504 "Data hash table of %s has a fill level at %.1f (%"PRIu64
" of %"PRIu64
" items, %"PRIu64
" file size, %"PRIu64
" bytes per hash table item), suggesting rotation.",
4506 100.0 * (double) le64toh(f
->header
->n_data
) / ((double) (le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
))),
4507 le64toh(f
->header
->n_data
),
4508 le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
),
4509 (uint64_t) f
->last_stat
.st_size
,
4510 f
->last_stat
.st_size
/ le64toh(f
->header
->n_data
));
4514 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_fields
))
4515 if (le64toh(f
->header
->n_fields
) * 4ULL > (le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
)) * 3ULL) {
4517 log_level
, JOURNAL_LOG_RATELIMIT
,
4518 "Field hash table of %s has a fill level at %.1f (%"PRIu64
" of %"PRIu64
" items), suggesting rotation.",
4520 100.0 * (double) le64toh(f
->header
->n_fields
) / ((double) (le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
))),
4521 le64toh(f
->header
->n_fields
),
4522 le64toh(f
->header
->field_hash_table_size
) / sizeof(HashItem
));
4526 /* If there are too many hash collisions somebody is most likely playing games with us. Hence, if our
4527 * longest chain is longer than some threshold, let's suggest rotation. */
4528 if (JOURNAL_HEADER_CONTAINS(f
->header
, data_hash_chain_depth
) &&
4529 le64toh(f
->header
->data_hash_chain_depth
) > HASH_CHAIN_DEPTH_MAX
) {
4531 log_level
, JOURNAL_LOG_RATELIMIT
,
4532 "Data hash table of %s has deepest hash chain of length %" PRIu64
", suggesting rotation.",
4533 f
->path
, le64toh(f
->header
->data_hash_chain_depth
));
4537 if (JOURNAL_HEADER_CONTAINS(f
->header
, field_hash_chain_depth
) &&
4538 le64toh(f
->header
->field_hash_chain_depth
) > HASH_CHAIN_DEPTH_MAX
) {
4540 log_level
, JOURNAL_LOG_RATELIMIT
,
4541 "Field hash table of %s has deepest hash chain of length at %" PRIu64
", suggesting rotation.",
4542 f
->path
, le64toh(f
->header
->field_hash_chain_depth
));
4546 /* Are the data objects properly indexed by field objects? */
4547 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_data
) &&
4548 JOURNAL_HEADER_CONTAINS(f
->header
, n_fields
) &&
4549 le64toh(f
->header
->n_data
) > 0 &&
4550 le64toh(f
->header
->n_fields
) == 0) {
4552 log_level
, JOURNAL_LOG_RATELIMIT
,
4553 "Data objects of %s are not indexed by field objects, suggesting rotation.",
4558 if (max_file_usec
> 0) {
4561 h
= le64toh(f
->header
->head_entry_realtime
);
4562 t
= now(CLOCK_REALTIME
);
4564 if (h
> 0 && t
> h
+ max_file_usec
) {
4566 log_level
, JOURNAL_LOG_RATELIMIT
,
4567 "Oldest entry in %s is older than the configured file retention duration (%s), suggesting rotation.",
4568 f
->path
, FORMAT_TIMESPAN(max_file_usec
, USEC_PER_SEC
));
4576 static const char * const journal_object_type_table
[] = {
4577 [OBJECT_UNUSED
] = "unused",
4578 [OBJECT_DATA
] = "data",
4579 [OBJECT_FIELD
] = "field",
4580 [OBJECT_ENTRY
] = "entry",
4581 [OBJECT_DATA_HASH_TABLE
] = "data hash table",
4582 [OBJECT_FIELD_HASH_TABLE
] = "field hash table",
4583 [OBJECT_ENTRY_ARRAY
] = "entry array",
4584 [OBJECT_TAG
] = "tag",
4587 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(journal_object_type
, ObjectType
);