| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <fcntl.h> |
| 5 |
#include <sys/stat.h> |
| 6 |
|
| 7 |
#include "compress.h" |
| 8 |
#include "sd-forward.h" |
| 9 |
#include "gcrypt-util.h" |
| 10 |
#include "journal-def.h" |
| 11 |
#include "mmap-cache.h" |
| 12 |
#include "sparse-endian.h" |
| 13 |
|
| 14 |
typedef struct JournalMetrics { |
| 15 |
/* For all these: UINT64_MAX means "pick automatically", and 0 means "no limit enforced" */ |
| 16 |
uint64_t max_size; /* how large journal files grow at max */ |
| 17 |
uint64_t min_size; /* how large journal files grow at least */ |
| 18 |
uint64_t max_use; /* how much disk space to use in total at max, keep_free permitting */ |
| 19 |
uint64_t min_use; /* how much disk space to use in total at least, even if keep_free says not to */ |
| 20 |
uint64_t keep_free; /* how much to keep free on disk */ |
| 21 |
uint64_t n_max_files; /* how many files to keep around at max */ |
| 22 |
} JournalMetrics; |
| 23 |
|
| 24 |
typedef enum direction { |
| 25 |
DIRECTION_UP, |
| 26 |
DIRECTION_DOWN, |
| 27 |
_DIRECTION_INVALID = -EINVAL, |
| 28 |
} direction_t; |
| 29 |
|
| 30 |
typedef enum LocationType { |
| 31 |
/* The first and last entries, resp. */ |
| 32 |
LOCATION_HEAD, |
| 33 |
LOCATION_TAIL, |
| 34 |
|
| 35 |
/* We already read the entry we currently point to, and the |
| 36 |
* next one to read should probably not be this one again. */ |
| 37 |
LOCATION_DISCRETE, |
| 38 |
|
| 39 |
/* We should seek to the precise location specified, and |
| 40 |
* return it, as we haven't read it yet. */ |
| 41 |
LOCATION_SEEK, |
| 42 |
} LocationType; |
| 43 |
|
| 44 |
typedef enum OfflineState { |
| 45 |
OFFLINE_JOINED, |
| 46 |
OFFLINE_SYNCING, |
| 47 |
OFFLINE_OFFLINING, |
| 48 |
OFFLINE_CANCEL, |
| 49 |
OFFLINE_AGAIN_FROM_SYNCING, |
| 50 |
OFFLINE_AGAIN_FROM_OFFLINING, |
| 51 |
OFFLINE_DONE, |
| 52 |
} OfflineState; |
| 53 |
|
| 54 |
typedef struct JournalFile { |
| 55 |
int fd; |
| 56 |
MMapFileDescriptor *cache_fd; |
| 57 |
|
| 58 |
mode_t mode; |
| 59 |
|
| 60 |
int open_flags; |
| 61 |
bool close_fd:1; |
| 62 |
bool archive:1; |
| 63 |
bool strict_order:1; |
| 64 |
|
| 65 |
direction_t last_direction; |
| 66 |
LocationType location_type; |
| 67 |
uint64_t last_n_entries; |
| 68 |
|
| 69 |
char *path; |
| 70 |
struct stat last_stat; |
| 71 |
usec_t last_stat_usec; |
| 72 |
|
| 73 |
Header *header; |
| 74 |
HashItem *data_hash_table; |
| 75 |
HashItem *field_hash_table; |
| 76 |
|
| 77 |
uint64_t current_offset; |
| 78 |
uint64_t current_seqnum; |
| 79 |
uint64_t current_realtime; |
| 80 |
uint64_t current_monotonic; |
| 81 |
sd_id128_t current_boot_id; |
| 82 |
uint64_t current_xor_hash; |
| 83 |
|
| 84 |
JournalMetrics metrics; |
| 85 |
|
| 86 |
sd_event_source *post_change_timer; |
| 87 |
usec_t post_change_timer_period; |
| 88 |
|
| 89 |
OrderedHashmap *chain_cache; |
| 90 |
|
| 91 |
pthread_t offline_thread; |
| 92 |
volatile OfflineState offline_state; |
| 93 |
|
| 94 |
unsigned last_seen_generation; |
| 95 |
|
| 96 |
uint64_t compress_threshold_bytes; |
| 97 |
#if HAVE_COMPRESSION |
| 98 |
void *compress_buffer; |
| 99 |
#endif |
| 100 |
|
| 101 |
gcry_md_hd_t hmac; |
| 102 |
bool hmac_running; |
| 103 |
|
| 104 |
FSSHeader *fss_file; |
| 105 |
size_t fss_file_size; |
| 106 |
|
| 107 |
uint64_t fss_start_usec; |
| 108 |
uint64_t fss_interval_usec; |
| 109 |
|
| 110 |
void *fsprg_state; |
| 111 |
size_t fsprg_state_size; |
| 112 |
|
| 113 |
void *fsprg_seed; |
| 114 |
size_t fsprg_seed_size; |
| 115 |
|
| 116 |
/* When we insert this file into the per-boot priority queue 'newest_by_boot_id' in sd_journal, then by these keys */ |
| 117 |
sd_id128_t newest_boot_id; |
| 118 |
sd_id128_t newest_machine_id; |
| 119 |
uint64_t newest_monotonic_usec; |
| 120 |
uint64_t newest_realtime_usec; |
| 121 |
unsigned newest_boot_id_prioq_idx; |
| 122 |
uint64_t newest_entry_offset; |
| 123 |
uint8_t newest_state; |
| 124 |
} JournalFile; |
| 125 |
|
| 126 |
typedef enum JournalFileFlags { |
| 127 |
JOURNAL_COMPRESS = 1 << 0, |
| 128 |
JOURNAL_SEAL = 1 << 1, |
| 129 |
JOURNAL_STRICT_ORDER = 1 << 2, |
| 130 |
_JOURNAL_FILE_FLAGS_ALL = JOURNAL_COMPRESS|JOURNAL_SEAL|JOURNAL_STRICT_ORDER, |
| 131 |
} JournalFileFlags; |
| 132 |
|
| 133 |
typedef struct { |
| 134 |
uint64_t object_offset; |
| 135 |
uint64_t hash; |
| 136 |
} EntryItem; |
| 137 |
|
| 138 |
extern const struct hash_ops journal_file_hash_ops_by_path; |
| 139 |
|
| 140 |
int journal_file_open( |
| 141 |
int fd, |
| 142 |
const char *fname, |
| 143 |
int open_flags, |
| 144 |
JournalFileFlags file_flags, |
| 145 |
mode_t mode, |
| 146 |
uint64_t compress_threshold_bytes, |
| 147 |
JournalMetrics *metrics, |
| 148 |
MMapCache *mmap_cache, |
| 149 |
JournalFile *template, |
| 150 |
JournalFile **ret); |
| 151 |
|
| 152 |
int journal_file_set_offline_thread_join(JournalFile *f); |
| 153 |
JournalFile* journal_file_close(JournalFile *f); |
| 154 |
int journal_file_fstat(JournalFile *f); |
| 155 |
DEFINE_TRIVIAL_CLEANUP_FUNC(JournalFile*, journal_file_close); |
| 156 |
|
| 157 |
#define ALIGN64(x) (((x) + 7ULL) & ~7ULL) |
| 158 |
#define VALID64(x) (((x) & 7ULL) == 0ULL) |
| 159 |
|
| 160 |
/* Use six characters to cover the offsets common in smallish journal |
| 161 |
* files without adding too many zeros. */ |
| 162 |
#define OFSfmt "%06"PRIx64 |
| 163 |
|
| 164 |
static inline bool VALID_REALTIME(uint64_t u) { |
| 165 |
/* This considers timestamps until the year 3112 valid. That should be plenty room... */ |
| 166 |
return u > 0 && u < (1ULL << 55); |
| 167 |
} |
| 168 |
|
| 169 |
static inline bool VALID_MONOTONIC(uint64_t u) { |
| 170 |
/* This considers timestamps until 1142 years of runtime valid. */ |
| 171 |
return u < (1ULL << 55); |
| 172 |
} |
| 173 |
|
| 174 |
static inline bool VALID_EPOCH(uint64_t u) { |
| 175 |
/* This allows changing the key for 1142 years, every usec. */ |
| 176 |
return u < (1ULL << 55); |
| 177 |
} |
| 178 |
|
| 179 |
#define JOURNAL_HEADER_CONTAINS(h, field) \ |
| 180 |
(le64toh((h)->header_size) >= offsetof(Header, field) + sizeof((h)->field)) |
| 181 |
|
| 182 |
#define JOURNAL_HEADER_SEALED(h) \ |
| 183 |
FLAGS_SET(le32toh((h)->compatible_flags), HEADER_COMPATIBLE_SEALED) |
| 184 |
|
| 185 |
#define JOURNAL_HEADER_SEALED_CONTINUOUS(h) \ |
| 186 |
FLAGS_SET(le32toh((h)->compatible_flags), HEADER_COMPATIBLE_SEALED_CONTINUOUS) |
| 187 |
|
| 188 |
#define JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(h) \ |
| 189 |
FLAGS_SET(le32toh((h)->compatible_flags), HEADER_COMPATIBLE_TAIL_ENTRY_BOOT_ID) |
| 190 |
|
| 191 |
#define JOURNAL_HEADER_COMPRESSED_XZ(h) \ |
| 192 |
FLAGS_SET(le32toh((h)->incompatible_flags), HEADER_INCOMPATIBLE_COMPRESSED_XZ) |
| 193 |
|
| 194 |
#define JOURNAL_HEADER_COMPRESSED_LZ4(h) \ |
| 195 |
FLAGS_SET(le32toh((h)->incompatible_flags), HEADER_INCOMPATIBLE_COMPRESSED_LZ4) |
| 196 |
|
| 197 |
#define JOURNAL_HEADER_COMPRESSED_ZSTD(h) \ |
| 198 |
FLAGS_SET(le32toh((h)->incompatible_flags), HEADER_INCOMPATIBLE_COMPRESSED_ZSTD) |
| 199 |
|
| 200 |
#define JOURNAL_HEADER_KEYED_HASH(h) \ |
| 201 |
FLAGS_SET(le32toh((h)->incompatible_flags), HEADER_INCOMPATIBLE_KEYED_HASH) |
| 202 |
|
| 203 |
#define JOURNAL_HEADER_COMPACT(h) \ |
| 204 |
FLAGS_SET(le32toh((h)->incompatible_flags), HEADER_INCOMPATIBLE_COMPACT) |
| 205 |
|
| 206 |
int journal_file_move_to_object(JournalFile *f, ObjectType type, uint64_t offset, Object **ret); |
| 207 |
int journal_file_pin_object(JournalFile *f, Object *o); |
| 208 |
int journal_file_read_object_header(JournalFile *f, ObjectType type, uint64_t offset, Object *ret); |
| 209 |
|
| 210 |
int journal_file_tail_end_by_pread(JournalFile *f, uint64_t *ret_offset); |
| 211 |
int journal_file_tail_end_by_mmap(JournalFile *f, uint64_t *ret_offset); |
| 212 |
|
| 213 |
static inline uint64_t journal_file_entry_item_object_offset(JournalFile *f, Object *o, size_t i) { |
| 214 |
assert(f); |
| 215 |
assert(o); |
| 216 |
return JOURNAL_HEADER_COMPACT(f->header) ? le32toh(o->entry.items.compact[i].object_offset) : |
| 217 |
le64toh(o->entry.items.regular[i].object_offset); |
| 218 |
} |
| 219 |
|
| 220 |
static inline size_t journal_file_entry_item_size(JournalFile *f) { |
| 221 |
assert(f); |
| 222 |
return JOURNAL_HEADER_COMPACT(f->header) ? sizeof_field(Object, entry.items.compact[0]) : |
| 223 |
sizeof_field(Object, entry.items.regular[0]); |
| 224 |
} |
| 225 |
|
| 226 |
uint64_t journal_file_entry_n_items(JournalFile *f, Object *o) _pure_; |
| 227 |
|
| 228 |
int journal_file_data_payload( |
| 229 |
JournalFile *f, |
| 230 |
Object *o, |
| 231 |
uint64_t offset, |
| 232 |
const char *field, |
| 233 |
size_t field_length, |
| 234 |
size_t data_threshold, |
| 235 |
void **ret_data, |
| 236 |
size_t *ret_size); |
| 237 |
|
| 238 |
static inline size_t journal_file_data_payload_offset(JournalFile *f) { |
| 239 |
return JOURNAL_HEADER_COMPACT(f->header) |
| 240 |
? offsetof(Object, data.compact.payload) |
| 241 |
: offsetof(Object, data.regular.payload); |
| 242 |
} |
| 243 |
|
| 244 |
static inline uint8_t* journal_file_data_payload_field(JournalFile *f, Object *o) { |
| 245 |
return JOURNAL_HEADER_COMPACT(f->header) ? o->data.compact.payload : o->data.regular.payload; |
| 246 |
} |
| 247 |
|
| 248 |
uint64_t journal_file_entry_array_n_items(JournalFile *f, Object *o) _pure_; |
| 249 |
|
| 250 |
static inline uint64_t journal_file_entry_array_item(JournalFile *f, Object *o, size_t i) { |
| 251 |
assert(f); |
| 252 |
assert(o); |
| 253 |
return JOURNAL_HEADER_COMPACT(f->header) ? le32toh(o->entry_array.items.compact[i]) : |
| 254 |
le64toh(o->entry_array.items.regular[i]); |
| 255 |
} |
| 256 |
|
| 257 |
static inline size_t journal_file_entry_array_item_size(JournalFile *f) { |
| 258 |
assert(f); |
| 259 |
return JOURNAL_HEADER_COMPACT(f->header) ? sizeof(le32_t) : sizeof(le64_t); |
| 260 |
} |
| 261 |
|
| 262 |
uint64_t journal_file_hash_table_n_items(Object *o) _pure_; |
| 263 |
|
| 264 |
int journal_file_append_object(JournalFile *f, ObjectType type, uint64_t size, Object **ret_object, uint64_t *ret_offset); |
| 265 |
int journal_file_append_entry( |
| 266 |
JournalFile *f, |
| 267 |
const dual_timestamp *ts, |
| 268 |
const sd_id128_t *boot_id, |
| 269 |
const struct iovec iovec[], |
| 270 |
size_t n_iovec, |
| 271 |
uint64_t *seqnum, |
| 272 |
sd_id128_t *seqnum_id, |
| 273 |
Object **ret_object, |
| 274 |
uint64_t *ret_offset); |
| 275 |
|
| 276 |
int journal_file_find_data_object(JournalFile *f, const void *data, uint64_t size, Object **ret_object, uint64_t *ret_offset); |
| 277 |
int journal_file_find_data_object_with_hash(JournalFile *f, const void *data, uint64_t size, uint64_t hash, Object **ret_object, uint64_t *ret_offset); |
| 278 |
|
| 279 |
int journal_file_find_field_object(JournalFile *f, const void *field, uint64_t size, Object **ret_object, uint64_t *ret_offset); |
| 280 |
int journal_file_find_field_object_with_hash(JournalFile *f, const void *field, uint64_t size, uint64_t hash, Object **ret_object, uint64_t *ret_offset); |
| 281 |
|
| 282 |
void journal_file_reset_location(JournalFile *f); |
| 283 |
void journal_file_save_location(JournalFile *f, Object *o, uint64_t offset); |
| 284 |
int journal_file_next_entry(JournalFile *f, uint64_t p, direction_t direction, Object **ret_object, uint64_t *ret_offset); |
| 285 |
|
| 286 |
int journal_file_move_to_entry_by_offset(JournalFile *f, uint64_t p, direction_t direction, Object **ret_object, uint64_t *ret_offset); |
| 287 |
int journal_file_move_to_entry_by_seqnum(JournalFile *f, uint64_t seqnum, direction_t direction, Object **ret_object, uint64_t *ret_offset); |
| 288 |
int journal_file_move_to_entry_by_realtime(JournalFile *f, uint64_t realtime, direction_t direction, Object **ret_object, uint64_t *ret_offset); |
| 289 |
int journal_file_move_to_entry_by_monotonic(JournalFile *f, sd_id128_t boot_id, uint64_t monotonic, direction_t direction, Object **ret_object, uint64_t *ret_offset); |
| 290 |
|
| 291 |
int journal_file_move_to_entry_for_data(JournalFile *f, Object *d, direction_t direction, Object **ret_object, uint64_t *ret_offset); |
| 292 |
|
| 293 |
int journal_file_move_to_entry_by_offset_for_data(JournalFile *f, Object *d, uint64_t p, direction_t direction, Object **ret_object, uint64_t *ret_offset); |
| 294 |
int journal_file_move_to_entry_by_seqnum_for_data(JournalFile *f, Object *d, uint64_t seqnum, direction_t direction, Object **ret_object, uint64_t *ret_offset); |
| 295 |
int journal_file_move_to_entry_by_realtime_for_data(JournalFile *f, Object *d, uint64_t realtime, direction_t direction, Object **ret_object, uint64_t *ret_offset); |
| 296 |
int journal_file_move_to_entry_by_monotonic_for_data(JournalFile *f, Object *d, sd_id128_t boot_id, uint64_t monotonic, direction_t direction, Object **ret_object, uint64_t *ret_offset); |
| 297 |
|
| 298 |
int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint64_t p, uint64_t *seqnum, sd_id128_t *seqnum_id); |
| 299 |
|
| 300 |
void journal_file_dump(JournalFile *f); |
| 301 |
void journal_file_print_header(JournalFile *f); |
| 302 |
|
| 303 |
int journal_file_archive(JournalFile *f, char **ret_previous_path); |
| 304 |
int journal_file_parse_uid_from_filename(const char *path, uid_t *uid); |
| 305 |
|
| 306 |
int journal_file_dispose(int dir_fd, const char *fname); |
| 307 |
|
| 308 |
void journal_file_post_change(JournalFile *f); |
| 309 |
int journal_file_enable_post_change_timer(JournalFile *f, sd_event *e, usec_t t); |
| 310 |
|
| 311 |
void journal_reset_metrics(JournalMetrics *m); |
| 312 |
bool journal_metrics_equal(const JournalMetrics *x, const JournalMetrics *y); |
| 313 |
|
| 314 |
int journal_file_get_cutoff_realtime_usec(JournalFile *f, usec_t *ret_from, usec_t *ret_to); |
| 315 |
int journal_file_get_cutoff_monotonic_usec(JournalFile *f, sd_id128_t boot, usec_t *ret_from, usec_t *ret_to); |
| 316 |
|
| 317 |
bool journal_file_rotate_suggested(JournalFile *f, usec_t max_file_usec, int log_level); |
| 318 |
|
| 319 |
int journal_file_map_data_hash_table(JournalFile *f); |
| 320 |
int journal_file_map_field_hash_table(JournalFile *f); |
| 321 |
|
| 322 |
static inline Compression JOURNAL_FILE_COMPRESSION(JournalFile *f) { |
| 323 |
assert(f); |
| 324 |
|
| 325 |
if (JOURNAL_HEADER_COMPRESSED_XZ(f->header)) |
| 326 |
return COMPRESSION_XZ; |
| 327 |
if (JOURNAL_HEADER_COMPRESSED_LZ4(f->header)) |
| 328 |
return COMPRESSION_LZ4; |
| 329 |
if (JOURNAL_HEADER_COMPRESSED_ZSTD(f->header)) |
| 330 |
return COMPRESSION_ZSTD; |
| 331 |
return COMPRESSION_NONE; |
| 332 |
} |
| 333 |
|
| 334 |
uint64_t journal_file_hash_data(JournalFile *f, const void *data, size_t sz); |
| 335 |
|
| 336 |
bool journal_field_valid(const char *p, size_t l, bool allow_protected); |
| 337 |
|
| 338 |
DECLARE_STRING_TABLE_LOOKUP_TO_STRING(journal_object_type, ObjectType); |
| 339 |
|
| 340 |
static inline Compression COMPRESSION_FROM_OBJECT(const Object *o) { |
| 341 |
assert(o); |
| 342 |
|
| 343 |
switch (o->object.flags & _OBJECT_COMPRESSED_MASK) { |
| 344 |
case 0: |
| 345 |
return COMPRESSION_NONE; |
| 346 |
case OBJECT_COMPRESSED_XZ: |
| 347 |
return COMPRESSION_XZ; |
| 348 |
case OBJECT_COMPRESSED_LZ4: |
| 349 |
return COMPRESSION_LZ4; |
| 350 |
case OBJECT_COMPRESSED_ZSTD: |
| 351 |
return COMPRESSION_ZSTD; |
| 352 |
default: |
| 353 |
return _COMPRESSION_INVALID; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
static inline uint8_t COMPRESSION_TO_OBJECT_FLAG(Compression c) { |
| 358 |
switch (c) { |
| 359 |
case COMPRESSION_XZ: |
| 360 |
return OBJECT_COMPRESSED_XZ; |
| 361 |
case COMPRESSION_LZ4: |
| 362 |
return OBJECT_COMPRESSED_LZ4; |
| 363 |
case COMPRESSION_ZSTD: |
| 364 |
return OBJECT_COMPRESSED_ZSTD; |
| 365 |
default: |
| 366 |
return 0; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
static inline uint32_t COMPRESSION_TO_HEADER_INCOMPATIBLE_FLAG(Compression c) { |
| 371 |
switch (c) { |
| 372 |
case COMPRESSION_XZ: |
| 373 |
return HEADER_INCOMPATIBLE_COMPRESSED_XZ; |
| 374 |
case COMPRESSION_LZ4: |
| 375 |
return HEADER_INCOMPATIBLE_COMPRESSED_LZ4; |
| 376 |
case COMPRESSION_ZSTD: |
| 377 |
return HEADER_INCOMPATIBLE_COMPRESSED_ZSTD; |
| 378 |
default: |
| 379 |
return 0; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
bool journal_file_writable(const JournalFile *f) _pure_; |