| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <linux/magic.h> |
| 5 |
#include <pthread.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <sys/mman.h> |
| 8 |
#include <sys/statvfs.h> |
| 9 |
#include <sys/uio.h> |
| 10 |
#include <threads.h> |
| 11 |
#include <unistd.h> |
| 12 |
|
| 13 |
#include "sd-event.h" |
| 14 |
|
| 15 |
#include "alloc-util.h" |
| 16 |
#include "chattr-util.h" |
| 17 |
#include "compress.h" |
| 18 |
#include "env-util.h" |
| 19 |
#include "fd-util.h" |
| 20 |
#include "format-util.h" |
| 21 |
#include "fs-util.h" |
| 22 |
#include "gcrypt-util.h" |
| 23 |
#include "hashmap.h" |
| 24 |
#include "id128-util.h" |
| 25 |
#include "journal-authenticate.h" |
| 26 |
#include "journal-def.h" |
| 27 |
#include "journal-file.h" |
| 28 |
#include "journal-internal.h" |
| 29 |
#include "log.h" |
| 30 |
#include "log-ratelimit.h" |
| 31 |
#include "lookup3.h" |
| 32 |
#include "memory-util.h" |
| 33 |
#include "parse-util.h" |
| 34 |
#include "path-util.h" |
| 35 |
#include "prioq.h" |
| 36 |
#include "random-util.h" |
| 37 |
#include "sort-util.h" |
| 38 |
#include "stat-util.h" |
| 39 |
#include "string-table.h" |
| 40 |
#include "string-util.h" |
| 41 |
#include "strv.h" |
| 42 |
#include "siphash24.h" |
| 43 |
#include "sync-util.h" |
| 44 |
#include "time-util.h" |
| 45 |
#include "user-util.h" |
| 46 |
#include "xattr-util.h" |
| 47 |
|
| 48 |
#define DEFAULT_DATA_HASH_TABLE_SIZE 2047U |
| 49 |
#define DEFAULT_FIELD_HASH_TABLE_SIZE 1023U |
| 50 |
|
| 51 |
#define DEFAULT_COMPRESS_THRESHOLD 512U |
| 52 |
#define MIN_COMPRESS_THRESHOLD 8U |
| 53 |
|
| 54 |
/* This is the minimum journal file size */ |
| 55 |
#define JOURNAL_FILE_SIZE_MIN (512 * U64_KB) /* 512 KiB */ |
| 56 |
#define JOURNAL_COMPACT_SIZE_MAX ((uint64_t) UINT32_MAX) /* 4 GiB */ |
| 57 |
|
| 58 |
/* These are the lower and upper bounds if we deduce the max_use value from the file system size */ |
| 59 |
#define MAX_USE_LOWER (1 * U64_MB) /* 1 MiB */ |
| 60 |
#define MAX_USE_UPPER (4 * U64_GB) /* 4 GiB */ |
| 61 |
|
| 62 |
/* Those are the lower and upper bounds for the minimal use limit, |
| 63 |
* i.e. how much we'll use even if keep_free suggests otherwise. */ |
| 64 |
#define MIN_USE_LOW (1 * U64_MB) /* 1 MiB */ |
| 65 |
#define MIN_USE_HIGH (16 * U64_MB) /* 16 MiB */ |
| 66 |
|
| 67 |
/* This is the upper bound if we deduce max_size from max_use */ |
| 68 |
#define MAX_SIZE_UPPER (128 * U64_MB) /* 128 MiB */ |
| 69 |
|
| 70 |
/* This is the upper bound if we deduce the keep_free value from the file system size */ |
| 71 |
#define KEEP_FREE_UPPER (4 * U64_GB) /* 4 GiB */ |
| 72 |
|
| 73 |
/* This is the keep_free value when we can't determine the system size */ |
| 74 |
#define DEFAULT_KEEP_FREE (1 * U64_MB) /* 1 MB */ |
| 75 |
|
| 76 |
/* This is the default maximum number of journal files to keep around. */ |
| 77 |
#define DEFAULT_N_MAX_FILES 100 |
| 78 |
|
| 79 |
/* n_data was the first entry we added after the initial file format design */ |
| 80 |
#define HEADER_SIZE_MIN ALIGN64(offsetof(Header, n_data)) |
| 81 |
|
| 82 |
/* How many entries to keep in the entry array chain cache at max */ |
| 83 |
#define CHAIN_CACHE_MAX 20 |
| 84 |
|
| 85 |
/* How much to increase the journal file size at once each time we allocate something new. */ |
| 86 |
#define FILE_SIZE_INCREASE (8 * U64_MB) /* 8MB */ |
| 87 |
|
| 88 |
/* Reread fstat() of the file for detecting deletions at least this often */ |
| 89 |
#define LAST_STAT_REFRESH_USEC (5*USEC_PER_SEC) |
| 90 |
|
| 91 |
/* Longest hash chain to rotate after */ |
| 92 |
#define HASH_CHAIN_DEPTH_MAX 100 |
| 93 |
|
| 94 |
#ifdef __clang__ |
| 95 |
# pragma GCC diagnostic ignored "-Waddress-of-packed-member" |
| 96 |
#endif |
| 97 |
|
| 98 |
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 99 |
journal_file_hash_ops_by_path, |
| 100 |
char, path_hash_func, path_compare, |
| 101 |
JournalFile, journal_file_close); |
| 102 |
|
| 103 |
static int mmap_prot_from_open_flags(int flags) { |
| 104 |
switch (flags & O_ACCMODE_STRICT) { |
| 105 |
case O_RDONLY: |
| 106 |
return PROT_READ; |
| 107 |
case O_WRONLY: |
| 108 |
return PROT_WRITE; |
| 109 |
case O_RDWR: |
| 110 |
return PROT_READ|PROT_WRITE; |
| 111 |
default: |
| 112 |
assert_not_reached(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
int journal_file_tail_end_by_pread(JournalFile *f, uint64_t *ret_offset) { |
| 117 |
uint64_t p; |
| 118 |
int r; |
| 119 |
|
| 120 |
assert(f); |
| 121 |
assert(f->header); |
| 122 |
assert(ret_offset); |
| 123 |
|
| 124 |
/* Same as journal_file_tail_end_by_mmap() below, but operates with pread() to avoid the mmap cache |
| 125 |
* (and thus is thread safe) */ |
| 126 |
|
| 127 |
p = le64toh(f->header->tail_object_offset); |
| 128 |
if (p == 0) |
| 129 |
p = le64toh(f->header->header_size); |
| 130 |
else { |
| 131 |
Object tail; |
| 132 |
uint64_t sz; |
| 133 |
|
| 134 |
r = journal_file_read_object_header(f, OBJECT_UNUSED, p, &tail); |
| 135 |
if (r < 0) |
| 136 |
return r; |
| 137 |
|
| 138 |
sz = le64toh(tail.object.size); |
| 139 |
if (sz > UINT64_MAX - sizeof(uint64_t) + 1) |
| 140 |
return -EBADMSG; |
| 141 |
|
| 142 |
sz = ALIGN64(sz); |
| 143 |
if (p > UINT64_MAX - sz) |
| 144 |
return -EBADMSG; |
| 145 |
|
| 146 |
p += sz; |
| 147 |
} |
| 148 |
|
| 149 |
*ret_offset = p; |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
int journal_file_tail_end_by_mmap(JournalFile *f, uint64_t *ret_offset) { |
| 155 |
uint64_t p; |
| 156 |
int r; |
| 157 |
|
| 158 |
assert(f); |
| 159 |
assert(f->header); |
| 160 |
assert(ret_offset); |
| 161 |
|
| 162 |
/* Same as journal_file_tail_end_by_pread() above, but operates with the usual mmap logic */ |
| 163 |
|
| 164 |
p = le64toh(f->header->tail_object_offset); |
| 165 |
if (p == 0) |
| 166 |
p = le64toh(f->header->header_size); |
| 167 |
else { |
| 168 |
Object *tail; |
| 169 |
uint64_t sz; |
| 170 |
|
| 171 |
r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &tail); |
| 172 |
if (r < 0) |
| 173 |
return r; |
| 174 |
|
| 175 |
sz = le64toh(READ_NOW(tail->object.size)); |
| 176 |
if (sz > UINT64_MAX - sizeof(uint64_t) + 1) |
| 177 |
return -EBADMSG; |
| 178 |
|
| 179 |
sz = ALIGN64(sz); |
| 180 |
if (p > UINT64_MAX - sz) |
| 181 |
return -EBADMSG; |
| 182 |
|
| 183 |
p += sz; |
| 184 |
} |
| 185 |
|
| 186 |
*ret_offset = p; |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
int journal_file_set_offline_thread_join(JournalFile *f) { |
| 192 |
int r; |
| 193 |
|
| 194 |
assert(f); |
| 195 |
|
| 196 |
if (f->offline_state == OFFLINE_JOINED) |
| 197 |
return 0; |
| 198 |
|
| 199 |
log_debug("Joining journal offlining thread for %s.", f->path); |
| 200 |
|
| 201 |
r = pthread_join(f->offline_thread, NULL); |
| 202 |
if (r) |
| 203 |
return -r; |
| 204 |
|
| 205 |
log_debug("Journal offlining thread for %s joined.", f->path); |
| 206 |
|
| 207 |
f->offline_state = OFFLINE_JOINED; |
| 208 |
|
| 209 |
if (mmap_cache_fd_got_sigbus(f->cache_fd)) |
| 210 |
return -EIO; |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
static int journal_file_set_online(JournalFile *f) { |
| 216 |
bool wait = true; |
| 217 |
|
| 218 |
assert(f); |
| 219 |
|
| 220 |
if (!journal_file_writable(f)) |
| 221 |
return -EPERM; |
| 222 |
|
| 223 |
if (f->fd < 0 || !f->header) |
| 224 |
return -EINVAL; |
| 225 |
|
| 226 |
while (wait) { |
| 227 |
switch (f->offline_state) { |
| 228 |
case OFFLINE_JOINED: |
| 229 |
/* No offline thread, no need to wait. */ |
| 230 |
wait = false; |
| 231 |
break; |
| 232 |
|
| 233 |
case OFFLINE_SYNCING: { |
| 234 |
OfflineState tmp_state = OFFLINE_SYNCING; |
| 235 |
if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_CANCEL, |
| 236 |
false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) |
| 237 |
continue; |
| 238 |
} |
| 239 |
/* Canceled syncing prior to offlining, no need to wait. */ |
| 240 |
wait = false; |
| 241 |
break; |
| 242 |
|
| 243 |
case OFFLINE_AGAIN_FROM_SYNCING: { |
| 244 |
OfflineState tmp_state = OFFLINE_AGAIN_FROM_SYNCING; |
| 245 |
if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_CANCEL, |
| 246 |
false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) |
| 247 |
continue; |
| 248 |
} |
| 249 |
/* Canceled restart from syncing, no need to wait. */ |
| 250 |
wait = false; |
| 251 |
break; |
| 252 |
|
| 253 |
case OFFLINE_AGAIN_FROM_OFFLINING: { |
| 254 |
OfflineState tmp_state = OFFLINE_AGAIN_FROM_OFFLINING; |
| 255 |
if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_CANCEL, |
| 256 |
false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) |
| 257 |
continue; |
| 258 |
} |
| 259 |
/* Canceled restart from offlining, must wait for offlining to complete however. */ |
| 260 |
_fallthrough_; |
| 261 |
default: { |
| 262 |
int r; |
| 263 |
|
| 264 |
r = journal_file_set_offline_thread_join(f); |
| 265 |
if (r < 0) |
| 266 |
return r; |
| 267 |
|
| 268 |
wait = false; |
| 269 |
}} |
| 270 |
} |
| 271 |
|
| 272 |
if (mmap_cache_fd_got_sigbus(f->cache_fd)) |
| 273 |
return -EIO; |
| 274 |
|
| 275 |
switch (f->header->state) { |
| 276 |
case STATE_ONLINE: |
| 277 |
return 0; |
| 278 |
|
| 279 |
case STATE_OFFLINE: |
| 280 |
f->header->state = STATE_ONLINE; |
| 281 |
(void) fsync(f->fd); |
| 282 |
return 0; |
| 283 |
|
| 284 |
default: |
| 285 |
return -EINVAL; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
JournalFile* journal_file_close(JournalFile *f) { |
| 290 |
if (!f) |
| 291 |
return NULL; |
| 292 |
|
| 293 |
assert(f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL); |
| 294 |
|
| 295 |
sd_event_source_disable_unref(f->post_change_timer); |
| 296 |
|
| 297 |
if (f->cache_fd) |
| 298 |
mmap_cache_fd_free(f->cache_fd); |
| 299 |
|
| 300 |
if (f->close_fd) |
| 301 |
safe_close(f->fd); |
| 302 |
free(f->path); |
| 303 |
|
| 304 |
ordered_hashmap_free(f->chain_cache); |
| 305 |
|
| 306 |
#if HAVE_COMPRESSION |
| 307 |
free(f->compress_buffer); |
| 308 |
#endif |
| 309 |
|
| 310 |
if (f->fss_file) { |
| 311 |
size_t sz = PAGE_ALIGN(f->fss_file_size); |
| 312 |
assert(sz < SIZE_MAX); |
| 313 |
munmap(f->fss_file, sz); |
| 314 |
} else |
| 315 |
free(f->fsprg_state); |
| 316 |
|
| 317 |
free(f->fsprg_seed); |
| 318 |
|
| 319 |
if (f->hmac) |
| 320 |
sym_gcry_md_close(f->hmac); |
| 321 |
|
| 322 |
return mfree(f); |
| 323 |
} |
| 324 |
|
| 325 |
static bool keyed_hash_requested(void) { |
| 326 |
static thread_local int cached = -1; |
| 327 |
int r; |
| 328 |
|
| 329 |
if (cached < 0) { |
| 330 |
r = secure_getenv_bool("SYSTEMD_JOURNAL_KEYED_HASH"); |
| 331 |
if (r < 0) { |
| 332 |
if (r != -ENXIO) |
| 333 |
log_debug_errno(r, "Failed to parse $SYSTEMD_JOURNAL_KEYED_HASH environment variable, ignoring: %m"); |
| 334 |
cached = true; |
| 335 |
} else |
| 336 |
cached = r; |
| 337 |
} |
| 338 |
|
| 339 |
return cached; |
| 340 |
} |
| 341 |
|
| 342 |
static bool compact_mode_requested(void) { |
| 343 |
static thread_local int cached = -1; |
| 344 |
int r; |
| 345 |
|
| 346 |
if (cached < 0) { |
| 347 |
r = secure_getenv_bool("SYSTEMD_JOURNAL_COMPACT"); |
| 348 |
if (r < 0) { |
| 349 |
if (r != -ENXIO) |
| 350 |
log_debug_errno(r, "Failed to parse $SYSTEMD_JOURNAL_COMPACT environment variable, ignoring: %m"); |
| 351 |
cached = true; |
| 352 |
} else |
| 353 |
cached = r; |
| 354 |
} |
| 355 |
|
| 356 |
return cached; |
| 357 |
} |
| 358 |
|
| 359 |
#if HAVE_COMPRESSION |
| 360 |
static Compression getenv_compression(void) { |
| 361 |
Compression c; |
| 362 |
const char *e; |
| 363 |
int r; |
| 364 |
|
| 365 |
e = getenv("SYSTEMD_JOURNAL_COMPRESS"); |
| 366 |
if (!e) |
| 367 |
return DEFAULT_COMPRESSION; |
| 368 |
|
| 369 |
r = parse_boolean(e); |
| 370 |
if (r >= 0) |
| 371 |
return r ? DEFAULT_COMPRESSION : COMPRESSION_NONE; |
| 372 |
|
| 373 |
c = compression_from_string_harder(e); |
| 374 |
if (c < 0) { |
| 375 |
log_debug_errno(c, "Failed to parse SYSTEMD_JOURNAL_COMPRESS value, ignoring: %s", e); |
| 376 |
return DEFAULT_COMPRESSION; |
| 377 |
} |
| 378 |
|
| 379 |
if (!compression_supported(c)) { |
| 380 |
log_debug("Unsupported compression algorithm specified, ignoring: %s", e); |
| 381 |
return DEFAULT_COMPRESSION; |
| 382 |
} |
| 383 |
|
| 384 |
return c; |
| 385 |
} |
| 386 |
#endif |
| 387 |
|
| 388 |
static Compression compression_requested(void) { |
| 389 |
#if HAVE_COMPRESSION |
| 390 |
static thread_local Compression cached = _COMPRESSION_INVALID; |
| 391 |
|
| 392 |
if (cached < 0) |
| 393 |
cached = getenv_compression(); |
| 394 |
|
| 395 |
return cached; |
| 396 |
#else |
| 397 |
return COMPRESSION_NONE; |
| 398 |
#endif |
| 399 |
} |
| 400 |
|
| 401 |
static int journal_file_init_header( |
| 402 |
JournalFile *f, |
| 403 |
JournalFileFlags file_flags, |
| 404 |
JournalFile *template) { |
| 405 |
|
| 406 |
bool seal = false; |
| 407 |
ssize_t k; |
| 408 |
int r; |
| 409 |
|
| 410 |
assert(f); |
| 411 |
|
| 412 |
#if HAVE_GCRYPT |
| 413 |
/* Try to load the FSPRG state, and if we can't, then just don't do sealing */ |
| 414 |
seal = FLAGS_SET(file_flags, JOURNAL_SEAL) && journal_file_fss_load(f) >= 0; |
| 415 |
#endif |
| 416 |
|
| 417 |
Header h = { |
| 418 |
.header_size = htole64(ALIGN64(sizeof(h))), |
| 419 |
.incompatible_flags = htole32( |
| 420 |
FLAGS_SET(file_flags, JOURNAL_COMPRESS) * COMPRESSION_TO_HEADER_INCOMPATIBLE_FLAG(compression_requested()) | |
| 421 |
keyed_hash_requested() * HEADER_INCOMPATIBLE_KEYED_HASH | |
| 422 |
compact_mode_requested() * HEADER_INCOMPATIBLE_COMPACT), |
| 423 |
.compatible_flags = htole32( |
| 424 |
(seal * (HEADER_COMPATIBLE_SEALED | HEADER_COMPATIBLE_SEALED_CONTINUOUS) ) | |
| 425 |
HEADER_COMPATIBLE_TAIL_ENTRY_BOOT_ID), |
| 426 |
}; |
| 427 |
|
| 428 |
assert_cc(sizeof(h.signature) == sizeof(HEADER_SIGNATURE)); |
| 429 |
memcpy(h.signature, HEADER_SIGNATURE, sizeof(HEADER_SIGNATURE)); |
| 430 |
|
| 431 |
r = sd_id128_randomize(&h.file_id); |
| 432 |
if (r < 0) |
| 433 |
return r; |
| 434 |
|
| 435 |
r = sd_id128_get_machine(&h.machine_id); |
| 436 |
if (r < 0 && !ERRNO_IS_MACHINE_ID_UNSET(r)) |
| 437 |
return r; /* If we have no valid machine ID (test environment?), let's simply leave the |
| 438 |
* machine ID field all zeroes. */ |
| 439 |
|
| 440 |
if (template) { |
| 441 |
h.seqnum_id = template->header->seqnum_id; |
| 442 |
h.tail_entry_seqnum = template->header->tail_entry_seqnum; |
| 443 |
} else |
| 444 |
h.seqnum_id = h.file_id; |
| 445 |
|
| 446 |
k = pwrite(f->fd, &h, sizeof(h), 0); |
| 447 |
if (k < 0) |
| 448 |
return -errno; |
| 449 |
if (k != sizeof(h)) |
| 450 |
return -EIO; |
| 451 |
|
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
static int journal_file_refresh_header(JournalFile *f) { |
| 456 |
int r; |
| 457 |
|
| 458 |
assert(f); |
| 459 |
assert(f->header); |
| 460 |
|
| 461 |
/* We used to update the header's boot ID field here, but we don't do that anymore, as per |
| 462 |
* HEADER_COMPATIBLE_TAIL_ENTRY_BOOT_ID */ |
| 463 |
|
| 464 |
r = journal_file_set_online(f); |
| 465 |
|
| 466 |
/* Sync the online state to disk; likely just created a new file, also sync the directory this file |
| 467 |
* is located in. */ |
| 468 |
(void) fsync_full(f->fd); |
| 469 |
|
| 470 |
return r; |
| 471 |
} |
| 472 |
|
| 473 |
static bool warn_wrong_flags(const JournalFile *f, bool compatible) { |
| 474 |
const uint32_t any = compatible ? HEADER_COMPATIBLE_ANY : HEADER_INCOMPATIBLE_ANY, |
| 475 |
supported = compatible ? HEADER_COMPATIBLE_SUPPORTED : HEADER_INCOMPATIBLE_SUPPORTED; |
| 476 |
const char *type = compatible ? "compatible" : "incompatible"; |
| 477 |
uint32_t flags; |
| 478 |
|
| 479 |
assert(f); |
| 480 |
assert(f->header); |
| 481 |
|
| 482 |
flags = le32toh(compatible ? f->header->compatible_flags : f->header->incompatible_flags); |
| 483 |
|
| 484 |
if (flags & ~supported) { |
| 485 |
if (flags & ~any) |
| 486 |
log_debug("Journal file %s has unknown %s flags 0x%"PRIx32, |
| 487 |
f->path, type, flags & ~any); |
| 488 |
flags = (flags & any) & ~supported; |
| 489 |
if (flags) { |
| 490 |
const char* strv[6]; |
| 491 |
size_t n = 0; |
| 492 |
_cleanup_free_ char *t = NULL; |
| 493 |
|
| 494 |
if (compatible) { |
| 495 |
if (flags & HEADER_COMPATIBLE_SEALED) |
| 496 |
strv[n++] = "sealed"; |
| 497 |
if (flags & HEADER_COMPATIBLE_SEALED_CONTINUOUS) |
| 498 |
strv[n++] = "sealed-continuous"; |
| 499 |
} else { |
| 500 |
if (flags & HEADER_INCOMPATIBLE_COMPRESSED_XZ) |
| 501 |
strv[n++] = "xz-compressed"; |
| 502 |
if (flags & HEADER_INCOMPATIBLE_COMPRESSED_LZ4) |
| 503 |
strv[n++] = "lz4-compressed"; |
| 504 |
if (flags & HEADER_INCOMPATIBLE_COMPRESSED_ZSTD) |
| 505 |
strv[n++] = "zstd-compressed"; |
| 506 |
if (flags & HEADER_INCOMPATIBLE_KEYED_HASH) |
| 507 |
strv[n++] = "keyed-hash"; |
| 508 |
if (flags & HEADER_INCOMPATIBLE_COMPACT) |
| 509 |
strv[n++] = "compact"; |
| 510 |
} |
| 511 |
strv[n] = NULL; |
| 512 |
assert(n < ELEMENTSOF(strv)); |
| 513 |
|
| 514 |
t = strv_join((char**) strv, ", "); |
| 515 |
log_debug("Journal file %s uses %s %s %s disabled at compilation time.", |
| 516 |
f->path, type, n > 1 ? "flags" : "flag", strnull(t)); |
| 517 |
} |
| 518 |
return true; |
| 519 |
} |
| 520 |
|
| 521 |
return false; |
| 522 |
} |
| 523 |
|
| 524 |
static bool offset_is_valid(uint64_t offset, uint64_t header_size, uint64_t tail_object_offset) { |
| 525 |
if (offset == 0) |
| 526 |
return true; |
| 527 |
if (!VALID64(offset)) |
| 528 |
return false; |
| 529 |
if (offset < header_size) |
| 530 |
return false; |
| 531 |
if (offset > tail_object_offset) |
| 532 |
return false; |
| 533 |
return true; |
| 534 |
} |
| 535 |
|
| 536 |
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) { |
| 537 |
if ((offset == 0) != (size == 0)) |
| 538 |
return false; |
| 539 |
if (offset == 0) |
| 540 |
return true; |
| 541 |
if (offset <= offsetof(Object, hash_table.items)) |
| 542 |
return false; |
| 543 |
offset -= offsetof(Object, hash_table.items); |
| 544 |
if (!offset_is_valid(offset, header_size, tail_object_offset)) |
| 545 |
return false; |
| 546 |
assert(offset <= header_size + arena_size); |
| 547 |
if (size > header_size + arena_size - offset) |
| 548 |
return false; |
| 549 |
return true; |
| 550 |
} |
| 551 |
|
| 552 |
static int journal_file_verify_header(JournalFile *f) { |
| 553 |
uint64_t arena_size, header_size; |
| 554 |
|
| 555 |
assert(f); |
| 556 |
assert(f->header); |
| 557 |
|
| 558 |
if (memcmp(f->header->signature, HEADER_SIGNATURE, 8) != 0) |
| 559 |
return -EBADMSG; |
| 560 |
|
| 561 |
/* In both read and write mode we refuse to open files with incompatible |
| 562 |
* flags we don't know. */ |
| 563 |
if (warn_wrong_flags(f, false)) |
| 564 |
return -EPROTONOSUPPORT; |
| 565 |
|
| 566 |
/* When open for writing we refuse to open files with compatible flags, too. */ |
| 567 |
if (journal_file_writable(f) && warn_wrong_flags(f, true)) |
| 568 |
return -EPROTONOSUPPORT; |
| 569 |
|
| 570 |
if (f->header->state >= _STATE_MAX) |
| 571 |
return -EBADMSG; |
| 572 |
|
| 573 |
header_size = le64toh(READ_NOW(f->header->header_size)); |
| 574 |
|
| 575 |
/* The first addition was n_data, so check that we are at least this large */ |
| 576 |
if (header_size < HEADER_SIZE_MIN) |
| 577 |
return -EBADMSG; |
| 578 |
|
| 579 |
/* When open for writing we refuse to open files with a mismatch of the header size, i.e. writing to |
| 580 |
* files implementing older or new header structures. */ |
| 581 |
if (journal_file_writable(f) && header_size != sizeof(Header)) |
| 582 |
return -EPROTONOSUPPORT; |
| 583 |
|
| 584 |
/* Don't write to journal files without the new boot ID update behavior guarantee. */ |
| 585 |
if (journal_file_writable(f) && !JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f->header)) |
| 586 |
return -EPROTONOSUPPORT; |
| 587 |
|
| 588 |
if (JOURNAL_HEADER_SEALED(f->header) && !JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays)) |
| 589 |
return -EBADMSG; |
| 590 |
|
| 591 |
arena_size = le64toh(READ_NOW(f->header->arena_size)); |
| 592 |
|
| 593 |
if (UINT64_MAX - header_size < arena_size || header_size + arena_size > (uint64_t) f->last_stat.st_size) |
| 594 |
return -ENODATA; |
| 595 |
|
| 596 |
uint64_t tail_object_offset = le64toh(f->header->tail_object_offset); |
| 597 |
if (!offset_is_valid(tail_object_offset, header_size, UINT64_MAX)) |
| 598 |
return -ENODATA; |
| 599 |
if (header_size + arena_size < tail_object_offset) |
| 600 |
return -ENODATA; |
| 601 |
if (header_size + arena_size - tail_object_offset < offsetof(ObjectHeader, payload)) |
| 602 |
return -ENODATA; |
| 603 |
|
| 604 |
if (!hash_table_is_valid(le64toh(f->header->data_hash_table_offset), |
| 605 |
le64toh(f->header->data_hash_table_size), |
| 606 |
header_size, arena_size, tail_object_offset)) |
| 607 |
return -ENODATA; |
| 608 |
|
| 609 |
if (!hash_table_is_valid(le64toh(f->header->field_hash_table_offset), |
| 610 |
le64toh(f->header->field_hash_table_size), |
| 611 |
header_size, arena_size, tail_object_offset)) |
| 612 |
return -ENODATA; |
| 613 |
|
| 614 |
uint64_t entry_array_offset = le64toh(f->header->entry_array_offset); |
| 615 |
if (!offset_is_valid(entry_array_offset, header_size, tail_object_offset)) |
| 616 |
return -ENODATA; |
| 617 |
|
| 618 |
if (JOURNAL_HEADER_CONTAINS(f->header, tail_entry_array_offset)) { |
| 619 |
uint32_t offset = le32toh(f->header->tail_entry_array_offset); |
| 620 |
uint32_t n = le32toh(f->header->tail_entry_array_n_entries); |
| 621 |
|
| 622 |
if (!offset_is_valid(offset, header_size, tail_object_offset)) |
| 623 |
return -ENODATA; |
| 624 |
if (entry_array_offset > offset) |
| 625 |
return -ENODATA; |
| 626 |
if (entry_array_offset == 0 && offset != 0) |
| 627 |
return -ENODATA; |
| 628 |
if ((offset == 0) != (n == 0)) |
| 629 |
return -ENODATA; |
| 630 |
assert(offset <= header_size + arena_size); |
| 631 |
if ((uint64_t) n * journal_file_entry_array_item_size(f) > header_size + arena_size - offset) |
| 632 |
return -ENODATA; |
| 633 |
} |
| 634 |
|
| 635 |
if (JOURNAL_HEADER_CONTAINS(f->header, tail_entry_offset)) { |
| 636 |
uint64_t offset = le64toh(f->header->tail_entry_offset); |
| 637 |
|
| 638 |
if (!offset_is_valid(offset, header_size, tail_object_offset)) |
| 639 |
return -ENODATA; |
| 640 |
|
| 641 |
if (offset > 0) { |
| 642 |
/* When there is an entry object, then these fields must be filled. */ |
| 643 |
if (sd_id128_is_null(f->header->tail_entry_boot_id)) |
| 644 |
return -ENODATA; |
| 645 |
if (!VALID_REALTIME(le64toh(f->header->head_entry_realtime))) |
| 646 |
return -ENODATA; |
| 647 |
if (!VALID_REALTIME(le64toh(f->header->tail_entry_realtime))) |
| 648 |
return -ENODATA; |
| 649 |
if (!VALID_MONOTONIC(le64toh(f->header->tail_entry_monotonic))) |
| 650 |
return -ENODATA; |
| 651 |
} else { |
| 652 |
/* Otherwise, the fields must be zero. */ |
| 653 |
if (JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f->header) && |
| 654 |
!sd_id128_is_null(f->header->tail_entry_boot_id)) |
| 655 |
return -ENODATA; |
| 656 |
if (f->header->head_entry_realtime != 0) |
| 657 |
return -ENODATA; |
| 658 |
if (f->header->tail_entry_realtime != 0) |
| 659 |
return -ENODATA; |
| 660 |
if (f->header->tail_entry_monotonic != 0) |
| 661 |
return -ENODATA; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
/* Verify number of objects */ |
| 666 |
uint64_t n_objects = le64toh(f->header->n_objects); |
| 667 |
if (n_objects > arena_size / offsetof(ObjectHeader, payload)) |
| 668 |
return -ENODATA; |
| 669 |
|
| 670 |
uint64_t n_entries = le64toh(f->header->n_entries); |
| 671 |
if (n_entries > n_objects) |
| 672 |
return -ENODATA; |
| 673 |
|
| 674 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_data) && |
| 675 |
le64toh(f->header->n_data) > n_objects) |
| 676 |
return -ENODATA; |
| 677 |
|
| 678 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_fields) && |
| 679 |
le64toh(f->header->n_fields) > n_objects) |
| 680 |
return -ENODATA; |
| 681 |
|
| 682 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_tags) && |
| 683 |
le64toh(f->header->n_tags) > n_objects) |
| 684 |
return -ENODATA; |
| 685 |
|
| 686 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays) && |
| 687 |
le64toh(f->header->n_entry_arrays) > n_objects) |
| 688 |
return -ENODATA; |
| 689 |
|
| 690 |
if (JOURNAL_HEADER_CONTAINS(f->header, tail_entry_array_n_entries) && |
| 691 |
le32toh(f->header->tail_entry_array_n_entries) > n_entries) |
| 692 |
return -ENODATA; |
| 693 |
|
| 694 |
if (journal_file_writable(f)) { |
| 695 |
sd_id128_t machine_id; |
| 696 |
uint8_t state; |
| 697 |
int r; |
| 698 |
|
| 699 |
r = sd_id128_get_machine(&machine_id); |
| 700 |
if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r)) /* Gracefully handle the machine ID not being initialized yet */ |
| 701 |
machine_id = SD_ID128_NULL; |
| 702 |
else if (r < 0) |
| 703 |
return r; |
| 704 |
|
| 705 |
if (!sd_id128_equal(machine_id, f->header->machine_id)) |
| 706 |
return log_debug_errno(SYNTHETIC_ERRNO(EHOSTDOWN), |
| 707 |
"Trying to open journal file from different host for writing, refusing."); |
| 708 |
|
| 709 |
state = f->header->state; |
| 710 |
|
| 711 |
if (state == STATE_ARCHIVED) |
| 712 |
return -ESHUTDOWN; /* Already archived */ |
| 713 |
if (state == STATE_ONLINE) |
| 714 |
return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), |
| 715 |
"Journal file %s is already online. Assuming unclean closing.", |
| 716 |
f->path); |
| 717 |
if (state != STATE_OFFLINE) |
| 718 |
return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), |
| 719 |
"Journal file %s has unknown state %i.", |
| 720 |
f->path, state); |
| 721 |
|
| 722 |
if (f->header->field_hash_table_size == 0 || f->header->data_hash_table_size == 0) |
| 723 |
return -EBADMSG; |
| 724 |
} |
| 725 |
|
| 726 |
return 0; |
| 727 |
} |
| 728 |
|
| 729 |
int journal_file_fstat(JournalFile *f) { |
| 730 |
int r; |
| 731 |
|
| 732 |
assert(f); |
| 733 |
assert(f->fd >= 0); |
| 734 |
|
| 735 |
if (fstat(f->fd, &f->last_stat) < 0) |
| 736 |
return -errno; |
| 737 |
|
| 738 |
f->last_stat_usec = now(CLOCK_MONOTONIC); |
| 739 |
|
| 740 |
/* Refuse dealing with files that aren't regular */ |
| 741 |
r = stat_verify_regular(&f->last_stat); |
| 742 |
if (r < 0) |
| 743 |
return r; |
| 744 |
|
| 745 |
/* Refuse appending to files that are already deleted */ |
| 746 |
r = stat_verify_linked(&f->last_stat); |
| 747 |
if (r < 0) |
| 748 |
return r; |
| 749 |
|
| 750 |
return 0; |
| 751 |
} |
| 752 |
|
| 753 |
static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) { |
| 754 |
uint64_t old_size, new_size, old_header_size, old_arena_size; |
| 755 |
int r; |
| 756 |
|
| 757 |
assert(f); |
| 758 |
assert(f->header); |
| 759 |
|
| 760 |
/* We assume that this file is not sparse, and we know that for sure, since we always call |
| 761 |
* posix_fallocate() ourselves */ |
| 762 |
|
| 763 |
if (size > PAGE_ALIGN_DOWN_U64(UINT64_MAX) - offset) |
| 764 |
return -EINVAL; |
| 765 |
|
| 766 |
if (mmap_cache_fd_got_sigbus(f->cache_fd)) |
| 767 |
return -EIO; |
| 768 |
|
| 769 |
old_header_size = le64toh(READ_NOW(f->header->header_size)); |
| 770 |
old_arena_size = le64toh(READ_NOW(f->header->arena_size)); |
| 771 |
if (old_arena_size > PAGE_ALIGN_DOWN_U64(UINT64_MAX) - old_header_size) |
| 772 |
return -EBADMSG; |
| 773 |
|
| 774 |
old_size = old_header_size + old_arena_size; |
| 775 |
|
| 776 |
new_size = MAX(PAGE_ALIGN_U64(offset + size), old_header_size); |
| 777 |
|
| 778 |
if (new_size <= old_size) { |
| 779 |
|
| 780 |
/* We already pre-allocated enough space, but before |
| 781 |
* we write to it, let's check with fstat() if the |
| 782 |
* file got deleted, in order make sure we don't throw |
| 783 |
* away the data immediately. Don't check fstat() for |
| 784 |
* all writes though, but only once ever 10s. */ |
| 785 |
|
| 786 |
if (f->last_stat_usec + LAST_STAT_REFRESH_USEC > now(CLOCK_MONOTONIC)) |
| 787 |
return 0; |
| 788 |
|
| 789 |
return journal_file_fstat(f); |
| 790 |
} |
| 791 |
|
| 792 |
/* Allocate more space. */ |
| 793 |
|
| 794 |
if (f->metrics.max_size > 0 && new_size > f->metrics.max_size) |
| 795 |
return -E2BIG; |
| 796 |
|
| 797 |
/* Refuse to go over 4G in compact mode so offsets can be stored in 32-bit. */ |
| 798 |
if (JOURNAL_HEADER_COMPACT(f->header) && new_size > UINT32_MAX) |
| 799 |
return -E2BIG; |
| 800 |
|
| 801 |
if (new_size > f->metrics.min_size && f->metrics.keep_free > 0) { |
| 802 |
struct statvfs svfs; |
| 803 |
|
| 804 |
if (fstatvfs(f->fd, &svfs) >= 0) { |
| 805 |
uint64_t available; |
| 806 |
|
| 807 |
available = LESS_BY(u64_multiply_safe(svfs.f_bfree, svfs.f_bsize), f->metrics.keep_free); |
| 808 |
|
| 809 |
if (new_size - old_size > available) |
| 810 |
return -E2BIG; |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
/* Increase by larger blocks at once */ |
| 815 |
new_size = ROUND_UP(new_size, FILE_SIZE_INCREASE); |
| 816 |
if (f->metrics.max_size > 0 && new_size > f->metrics.max_size) |
| 817 |
new_size = f->metrics.max_size; |
| 818 |
|
| 819 |
/* Note that the glibc fallocate() fallback is very |
| 820 |
inefficient, hence we try to minimize the allocation area |
| 821 |
as we can. */ |
| 822 |
r = posix_fallocate_loop(f->fd, old_size, new_size - old_size); |
| 823 |
if (r < 0) |
| 824 |
return r; |
| 825 |
|
| 826 |
f->header->arena_size = htole64(new_size - old_header_size); |
| 827 |
|
| 828 |
return journal_file_fstat(f); |
| 829 |
} |
| 830 |
|
| 831 |
static int journal_file_move_to( |
| 832 |
JournalFile *f, |
| 833 |
ObjectType type, |
| 834 |
bool keep_always, |
| 835 |
uint64_t offset, |
| 836 |
uint64_t size, |
| 837 |
void **ret) { |
| 838 |
|
| 839 |
int r; |
| 840 |
|
| 841 |
assert(f); |
| 842 |
assert(ret); |
| 843 |
|
| 844 |
/* This function may clear, overwrite, or alter previously cached entries with the same type. After |
| 845 |
* this function has been called, all previously read objects with the same type may be invalidated, |
| 846 |
* hence must be re-read before use. */ |
| 847 |
|
| 848 |
if (size <= 0) |
| 849 |
return -EINVAL; |
| 850 |
|
| 851 |
if (size > UINT64_MAX - offset) |
| 852 |
return -EBADMSG; |
| 853 |
|
| 854 |
/* Avoid SIGBUS on invalid accesses */ |
| 855 |
if (offset + size > (uint64_t) f->last_stat.st_size) { |
| 856 |
/* Hmm, out of range? Let's refresh the fstat() data |
| 857 |
* first, before we trust that check. */ |
| 858 |
|
| 859 |
r = journal_file_fstat(f); |
| 860 |
if (r < 0) |
| 861 |
return r; |
| 862 |
|
| 863 |
if (offset + size > (uint64_t) f->last_stat.st_size) |
| 864 |
return -EADDRNOTAVAIL; |
| 865 |
} |
| 866 |
|
| 867 |
return mmap_cache_fd_get(f->cache_fd, type_to_category(type), keep_always, offset, size, &f->last_stat, ret); |
| 868 |
} |
| 869 |
|
| 870 |
static uint64_t minimum_header_size(JournalFile *f, Object *o) { |
| 871 |
|
| 872 |
static const uint64_t table[] = { |
| 873 |
[OBJECT_DATA] = sizeof(DataObject), |
| 874 |
[OBJECT_FIELD] = sizeof(FieldObject), |
| 875 |
[OBJECT_ENTRY] = sizeof(EntryObject), |
| 876 |
[OBJECT_DATA_HASH_TABLE] = sizeof(HashTableObject), |
| 877 |
[OBJECT_FIELD_HASH_TABLE] = sizeof(HashTableObject), |
| 878 |
[OBJECT_ENTRY_ARRAY] = sizeof(EntryArrayObject), |
| 879 |
[OBJECT_TAG] = sizeof(TagObject), |
| 880 |
}; |
| 881 |
|
| 882 |
assert(f); |
| 883 |
assert(o); |
| 884 |
|
| 885 |
if (o->object.type == OBJECT_DATA) |
| 886 |
return journal_file_data_payload_offset(f); |
| 887 |
|
| 888 |
if (o->object.type >= ELEMENTSOF(table) || table[o->object.type] <= 0) |
| 889 |
return offsetof(ObjectHeader, payload); |
| 890 |
|
| 891 |
return table[o->object.type]; |
| 892 |
} |
| 893 |
|
| 894 |
static int check_object_header(JournalFile *f, Object *o, ObjectType type, uint64_t offset) { |
| 895 |
uint64_t s; |
| 896 |
|
| 897 |
assert(f); |
| 898 |
assert(o); |
| 899 |
|
| 900 |
s = le64toh(READ_NOW(o->object.size)); |
| 901 |
if (s == 0) |
| 902 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 903 |
"Attempt to move to uninitialized object: %" PRIu64, |
| 904 |
offset); |
| 905 |
|
| 906 |
if (s < offsetof(ObjectHeader, payload)) |
| 907 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 908 |
"Attempt to move to overly short object with size %"PRIu64": %" PRIu64, |
| 909 |
s, offset); |
| 910 |
|
| 911 |
if (o->object.type <= OBJECT_UNUSED || o->object.type >= _OBJECT_TYPE_MAX) |
| 912 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 913 |
"Attempt to move to object with invalid type (%u): %" PRIu64, |
| 914 |
o->object.type, offset); |
| 915 |
|
| 916 |
if (type > OBJECT_UNUSED && o->object.type != type) |
| 917 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 918 |
"Found %s object while expecting %s object: %" PRIu64, |
| 919 |
journal_object_type_to_string(o->object.type), |
| 920 |
journal_object_type_to_string(type), |
| 921 |
offset); |
| 922 |
|
| 923 |
if (s < minimum_header_size(f, o)) |
| 924 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 925 |
"Size of %s object (%"PRIu64") is smaller than the minimum object size (%"PRIu64"): %" PRIu64, |
| 926 |
journal_object_type_to_string(o->object.type), |
| 927 |
s, |
| 928 |
minimum_header_size(f, o), |
| 929 |
offset); |
| 930 |
|
| 931 |
return 0; |
| 932 |
} |
| 933 |
|
| 934 |
/* Lightweight object checks. We want this to be fast, so that we won't |
| 935 |
* slowdown every journal_file_move_to_object() call too much. */ |
| 936 |
static int check_object(JournalFile *f, Object *o, uint64_t offset) { |
| 937 |
assert(f); |
| 938 |
assert(o); |
| 939 |
|
| 940 |
switch (o->object.type) { |
| 941 |
|
| 942 |
case OBJECT_DATA: |
| 943 |
if ((le64toh(o->data.entry_offset) == 0) ^ (le64toh(o->data.n_entries) == 0)) |
| 944 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 945 |
"Bad data n_entries: %" PRIu64 ": %" PRIu64, |
| 946 |
le64toh(o->data.n_entries), |
| 947 |
offset); |
| 948 |
|
| 949 |
if (le64toh(o->object.size) <= journal_file_data_payload_offset(f)) |
| 950 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 951 |
"Bad data size (<= %zu): %" PRIu64 ": %" PRIu64, |
| 952 |
journal_file_data_payload_offset(f), |
| 953 |
le64toh(o->object.size), |
| 954 |
offset); |
| 955 |
|
| 956 |
if (!VALID64(le64toh(o->data.next_hash_offset)) || |
| 957 |
!VALID64(le64toh(o->data.next_field_offset)) || |
| 958 |
!VALID64(le64toh(o->data.entry_offset)) || |
| 959 |
!VALID64(le64toh(o->data.entry_array_offset))) |
| 960 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 961 |
"Invalid offset, next_hash_offset=" OFSfmt ", next_field_offset=" OFSfmt ", entry_offset=" OFSfmt ", entry_array_offset=" OFSfmt ": %" PRIu64, |
| 962 |
le64toh(o->data.next_hash_offset), |
| 963 |
le64toh(o->data.next_field_offset), |
| 964 |
le64toh(o->data.entry_offset), |
| 965 |
le64toh(o->data.entry_array_offset), |
| 966 |
offset); |
| 967 |
|
| 968 |
break; |
| 969 |
|
| 970 |
case OBJECT_FIELD: |
| 971 |
if (le64toh(o->object.size) <= offsetof(Object, field.payload)) |
| 972 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 973 |
"Bad field size (<= %zu): %" PRIu64 ": %" PRIu64, |
| 974 |
offsetof(Object, field.payload), |
| 975 |
le64toh(o->object.size), |
| 976 |
offset); |
| 977 |
|
| 978 |
if (!VALID64(le64toh(o->field.next_hash_offset)) || |
| 979 |
!VALID64(le64toh(o->field.head_data_offset))) |
| 980 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 981 |
"Invalid offset, next_hash_offset=" OFSfmt ", head_data_offset=" OFSfmt ": %" PRIu64, |
| 982 |
le64toh(o->field.next_hash_offset), |
| 983 |
le64toh(o->field.head_data_offset), |
| 984 |
offset); |
| 985 |
break; |
| 986 |
|
| 987 |
case OBJECT_ENTRY: { |
| 988 |
uint64_t sz; |
| 989 |
|
| 990 |
sz = le64toh(READ_NOW(o->object.size)); |
| 991 |
if (sz < offsetof(Object, entry.items) || |
| 992 |
(sz - offsetof(Object, entry.items)) % journal_file_entry_item_size(f) != 0) |
| 993 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 994 |
"Bad entry size (<= %zu): %" PRIu64 ": %" PRIu64, |
| 995 |
offsetof(Object, entry.items), |
| 996 |
sz, |
| 997 |
offset); |
| 998 |
|
| 999 |
if ((sz - offsetof(Object, entry.items)) / journal_file_entry_item_size(f) <= 0) |
| 1000 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1001 |
"Invalid number items in entry: %" PRIu64 ": %" PRIu64, |
| 1002 |
(sz - offsetof(Object, entry.items)) / journal_file_entry_item_size(f), |
| 1003 |
offset); |
| 1004 |
|
| 1005 |
if (le64toh(o->entry.seqnum) <= 0) |
| 1006 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1007 |
"Invalid entry seqnum: %" PRIx64 ": %" PRIu64, |
| 1008 |
le64toh(o->entry.seqnum), |
| 1009 |
offset); |
| 1010 |
|
| 1011 |
if (!VALID_REALTIME(le64toh(o->entry.realtime))) |
| 1012 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1013 |
"Invalid entry realtime timestamp: %" PRIu64 ": %" PRIu64, |
| 1014 |
le64toh(o->entry.realtime), |
| 1015 |
offset); |
| 1016 |
|
| 1017 |
if (!VALID_MONOTONIC(le64toh(o->entry.monotonic))) |
| 1018 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1019 |
"Invalid entry monotonic timestamp: %" PRIu64 ": %" PRIu64, |
| 1020 |
le64toh(o->entry.monotonic), |
| 1021 |
offset); |
| 1022 |
|
| 1023 |
if (sd_id128_is_null(o->entry.boot_id)) |
| 1024 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1025 |
"Invalid object entry with an empty boot ID: %" PRIu64, |
| 1026 |
offset); |
| 1027 |
|
| 1028 |
break; |
| 1029 |
} |
| 1030 |
|
| 1031 |
case OBJECT_DATA_HASH_TABLE: |
| 1032 |
case OBJECT_FIELD_HASH_TABLE: { |
| 1033 |
uint64_t sz; |
| 1034 |
|
| 1035 |
sz = le64toh(READ_NOW(o->object.size)); |
| 1036 |
if (sz < offsetof(Object, hash_table.items) || |
| 1037 |
(sz - offsetof(Object, hash_table.items)) % sizeof(HashItem) != 0 || |
| 1038 |
(sz - offsetof(Object, hash_table.items)) / sizeof(HashItem) <= 0) |
| 1039 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1040 |
"Invalid %s hash table size: %" PRIu64 ": %" PRIu64, |
| 1041 |
journal_object_type_to_string(o->object.type), |
| 1042 |
sz, |
| 1043 |
offset); |
| 1044 |
|
| 1045 |
break; |
| 1046 |
} |
| 1047 |
|
| 1048 |
case OBJECT_ENTRY_ARRAY: { |
| 1049 |
uint64_t sz, next; |
| 1050 |
|
| 1051 |
sz = le64toh(READ_NOW(o->object.size)); |
| 1052 |
if (sz < offsetof(Object, entry_array.items) || |
| 1053 |
(sz - offsetof(Object, entry_array.items)) % journal_file_entry_array_item_size(f) != 0 || |
| 1054 |
(sz - offsetof(Object, entry_array.items)) / journal_file_entry_array_item_size(f) <= 0) |
| 1055 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1056 |
"Invalid object entry array size: %" PRIu64 ": %" PRIu64, |
| 1057 |
sz, |
| 1058 |
offset); |
| 1059 |
/* Here, we request that the offset of each entry array object is in strictly increasing order. */ |
| 1060 |
next = le64toh(o->entry_array.next_entry_array_offset); |
| 1061 |
if (!VALID64(next) || (next > 0 && next <= offset)) |
| 1062 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1063 |
"Invalid object entry array next_entry_array_offset: %" PRIu64 ": %" PRIu64, |
| 1064 |
next, |
| 1065 |
offset); |
| 1066 |
|
| 1067 |
break; |
| 1068 |
} |
| 1069 |
|
| 1070 |
case OBJECT_TAG: |
| 1071 |
if (le64toh(o->object.size) != sizeof(TagObject)) |
| 1072 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1073 |
"Invalid object tag size: %" PRIu64 ": %" PRIu64, |
| 1074 |
le64toh(o->object.size), |
| 1075 |
offset); |
| 1076 |
|
| 1077 |
if (!VALID_EPOCH(le64toh(o->tag.epoch))) |
| 1078 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1079 |
"Invalid object tag epoch: %" PRIu64 ": %" PRIu64, |
| 1080 |
le64toh(o->tag.epoch), offset); |
| 1081 |
|
| 1082 |
break; |
| 1083 |
} |
| 1084 |
|
| 1085 |
return 0; |
| 1086 |
} |
| 1087 |
|
| 1088 |
int journal_file_move_to_object(JournalFile *f, ObjectType type, uint64_t offset, Object **ret) { |
| 1089 |
int r; |
| 1090 |
Object *o; |
| 1091 |
|
| 1092 |
assert(f); |
| 1093 |
|
| 1094 |
/* Even if this function fails, it may clear, overwrite, or alter previously cached entries with the |
| 1095 |
* same type. After this function has been called, all previously read objects with the same type may |
| 1096 |
* be invalidated, hence must be re-read before use. */ |
| 1097 |
|
| 1098 |
/* Objects may only be located at multiple of 64 bit */ |
| 1099 |
if (!VALID64(offset)) |
| 1100 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1101 |
"Attempt to move to %s object at non-64-bit boundary: %" PRIu64, |
| 1102 |
journal_object_type_to_string(type), |
| 1103 |
offset); |
| 1104 |
|
| 1105 |
/* Object may not be located in the file header */ |
| 1106 |
if (offset < le64toh(f->header->header_size)) |
| 1107 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1108 |
"Attempt to move to %s object located in file header: %" PRIu64, |
| 1109 |
journal_object_type_to_string(type), |
| 1110 |
offset); |
| 1111 |
|
| 1112 |
r = journal_file_move_to(f, type, false, offset, offsetof(ObjectHeader, payload), (void**) &o); |
| 1113 |
if (r < 0) |
| 1114 |
return r; |
| 1115 |
|
| 1116 |
r = check_object_header(f, o, type, offset); |
| 1117 |
if (r < 0) |
| 1118 |
return r; |
| 1119 |
|
| 1120 |
r = journal_file_move_to(f, type, false, offset, le64toh(READ_NOW(o->object.size)), (void**) &o); |
| 1121 |
if (r < 0) |
| 1122 |
return r; |
| 1123 |
|
| 1124 |
r = check_object_header(f, o, type, offset); |
| 1125 |
if (r < 0) |
| 1126 |
return r; |
| 1127 |
|
| 1128 |
r = check_object(f, o, offset); |
| 1129 |
if (r < 0) |
| 1130 |
return r; |
| 1131 |
|
| 1132 |
if (ret) |
| 1133 |
*ret = o; |
| 1134 |
|
| 1135 |
return 0; |
| 1136 |
} |
| 1137 |
|
| 1138 |
int journal_file_pin_object(JournalFile *f, Object *o) { |
| 1139 |
assert(f); |
| 1140 |
assert(o); |
| 1141 |
|
| 1142 |
/* This attaches the mmap window that provides the object to the 'pinning' category. So, reading |
| 1143 |
* another object with the same type will not invalidate the object, until this function is called |
| 1144 |
* for another object. */ |
| 1145 |
return mmap_cache_fd_pin(f->cache_fd, type_to_category(o->object.type), o, le64toh(o->object.size)); |
| 1146 |
} |
| 1147 |
|
| 1148 |
int journal_file_read_object_header(JournalFile *f, ObjectType type, uint64_t offset, Object *ret) { |
| 1149 |
ssize_t n; |
| 1150 |
Object o; |
| 1151 |
int r; |
| 1152 |
|
| 1153 |
assert(f); |
| 1154 |
|
| 1155 |
/* Objects may only be located at multiple of 64 bit */ |
| 1156 |
if (!VALID64(offset)) |
| 1157 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1158 |
"Attempt to read %s object at non-64-bit boundary: %" PRIu64, |
| 1159 |
journal_object_type_to_string(type), offset); |
| 1160 |
|
| 1161 |
/* Object may not be located in the file header */ |
| 1162 |
if (offset < le64toh(f->header->header_size)) |
| 1163 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1164 |
"Attempt to read %s object located in file header: %" PRIu64, |
| 1165 |
journal_object_type_to_string(type), offset); |
| 1166 |
|
| 1167 |
/* This will likely read too much data but it avoids having to call pread() twice. */ |
| 1168 |
n = pread(f->fd, &o, sizeof(o), offset); |
| 1169 |
if (n < 0) |
| 1170 |
return log_debug_errno(errno, "Failed to read journal %s object at offset: %" PRIu64, |
| 1171 |
journal_object_type_to_string(type), offset); |
| 1172 |
|
| 1173 |
if ((size_t) n < sizeof(o.object)) |
| 1174 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), |
| 1175 |
"Failed to read short %s object at offset: %" PRIu64, |
| 1176 |
journal_object_type_to_string(type), offset); |
| 1177 |
|
| 1178 |
r = check_object_header(f, &o, type, offset); |
| 1179 |
if (r < 0) |
| 1180 |
return r; |
| 1181 |
|
| 1182 |
if ((size_t) n < minimum_header_size(f, &o)) |
| 1183 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), |
| 1184 |
"Short read while reading %s object: %" PRIu64, |
| 1185 |
journal_object_type_to_string(type), offset); |
| 1186 |
|
| 1187 |
r = check_object(f, &o, offset); |
| 1188 |
if (r < 0) |
| 1189 |
return r; |
| 1190 |
|
| 1191 |
if (ret) |
| 1192 |
*ret = o; |
| 1193 |
|
| 1194 |
return 0; |
| 1195 |
} |
| 1196 |
|
| 1197 |
static uint64_t inc_seqnum(uint64_t seqnum) { |
| 1198 |
if (seqnum < UINT64_MAX-1) |
| 1199 |
return seqnum + 1; |
| 1200 |
|
| 1201 |
return 1; /* skip over UINT64_MAX and 0 when we run out of seqnums and start again */ |
| 1202 |
} |
| 1203 |
|
| 1204 |
static uint64_t journal_file_entry_seqnum( |
| 1205 |
JournalFile *f, |
| 1206 |
uint64_t *seqnum) { |
| 1207 |
|
| 1208 |
uint64_t next_seqnum; |
| 1209 |
|
| 1210 |
assert(f); |
| 1211 |
assert(f->header); |
| 1212 |
|
| 1213 |
/* Picks a new sequence number for the entry we are about to add and returns it. */ |
| 1214 |
|
| 1215 |
next_seqnum = inc_seqnum(le64toh(f->header->tail_entry_seqnum)); |
| 1216 |
|
| 1217 |
/* If an external seqnum counter was passed, we update both the local and the external one, and set |
| 1218 |
* it to the maximum of both */ |
| 1219 |
if (seqnum) |
| 1220 |
*seqnum = next_seqnum = MAX(inc_seqnum(*seqnum), next_seqnum); |
| 1221 |
|
| 1222 |
f->header->tail_entry_seqnum = htole64(next_seqnum); |
| 1223 |
|
| 1224 |
if (f->header->head_entry_seqnum == 0) |
| 1225 |
f->header->head_entry_seqnum = htole64(next_seqnum); |
| 1226 |
|
| 1227 |
return next_seqnum; |
| 1228 |
} |
| 1229 |
|
| 1230 |
int journal_file_append_object( |
| 1231 |
JournalFile *f, |
| 1232 |
ObjectType type, |
| 1233 |
uint64_t size, |
| 1234 |
Object **ret_object, |
| 1235 |
uint64_t *ret_offset) { |
| 1236 |
|
| 1237 |
int r; |
| 1238 |
uint64_t p; |
| 1239 |
Object *o; |
| 1240 |
|
| 1241 |
assert(f); |
| 1242 |
assert(f->header); |
| 1243 |
assert(type > OBJECT_UNUSED && type < _OBJECT_TYPE_MAX); |
| 1244 |
assert(size >= offsetof(ObjectHeader, payload)); |
| 1245 |
|
| 1246 |
r = journal_file_set_online(f); |
| 1247 |
if (r < 0) |
| 1248 |
return r; |
| 1249 |
|
| 1250 |
r = journal_file_tail_end_by_mmap(f, &p); |
| 1251 |
if (r < 0) |
| 1252 |
return r; |
| 1253 |
|
| 1254 |
r = journal_file_allocate(f, p, size); |
| 1255 |
if (r < 0) |
| 1256 |
return r; |
| 1257 |
|
| 1258 |
r = journal_file_move_to(f, type, false, p, size, (void**) &o); |
| 1259 |
if (r < 0) |
| 1260 |
return r; |
| 1261 |
|
| 1262 |
o->object = (ObjectHeader) { |
| 1263 |
.type = type, |
| 1264 |
.size = htole64(size), |
| 1265 |
}; |
| 1266 |
|
| 1267 |
f->header->tail_object_offset = htole64(p); |
| 1268 |
f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1); |
| 1269 |
|
| 1270 |
if (ret_object) |
| 1271 |
*ret_object = o; |
| 1272 |
|
| 1273 |
if (ret_offset) |
| 1274 |
*ret_offset = p; |
| 1275 |
|
| 1276 |
return 0; |
| 1277 |
} |
| 1278 |
|
| 1279 |
static int journal_file_setup_data_hash_table(JournalFile *f) { |
| 1280 |
uint64_t s, p; |
| 1281 |
Object *o; |
| 1282 |
int r; |
| 1283 |
|
| 1284 |
assert(f); |
| 1285 |
assert(f->header); |
| 1286 |
|
| 1287 |
/* We estimate that we need 1 hash table entry per 768 bytes |
| 1288 |
of journal file and we want to make sure we never get |
| 1289 |
beyond 75% fill level. Calculate the hash table size for |
| 1290 |
the maximum file size based on these metrics. */ |
| 1291 |
|
| 1292 |
s = MAX(f->metrics.max_size * 4 / 768 / 3, |
| 1293 |
DEFAULT_DATA_HASH_TABLE_SIZE); |
| 1294 |
|
| 1295 |
log_debug("Reserving %"PRIu64" entries in data hash table.", s); |
| 1296 |
|
| 1297 |
r = journal_file_append_object(f, |
| 1298 |
OBJECT_DATA_HASH_TABLE, |
| 1299 |
offsetof(Object, hash_table.items) + s * sizeof(HashItem), |
| 1300 |
&o, &p); |
| 1301 |
if (r < 0) |
| 1302 |
return r; |
| 1303 |
|
| 1304 |
memzero(o->hash_table.items, s); |
| 1305 |
|
| 1306 |
f->header->data_hash_table_offset = htole64(p + offsetof(Object, hash_table.items)); |
| 1307 |
f->header->data_hash_table_size = htole64(s * sizeof(HashItem)); |
| 1308 |
|
| 1309 |
return 0; |
| 1310 |
} |
| 1311 |
|
| 1312 |
static int journal_file_setup_field_hash_table(JournalFile *f) { |
| 1313 |
uint64_t s, p; |
| 1314 |
Object *o; |
| 1315 |
int r; |
| 1316 |
|
| 1317 |
assert(f); |
| 1318 |
assert(f->header); |
| 1319 |
|
| 1320 |
/* We use a fixed size hash table for the fields as this |
| 1321 |
* number should grow very slowly only */ |
| 1322 |
|
| 1323 |
s = DEFAULT_FIELD_HASH_TABLE_SIZE; |
| 1324 |
log_debug("Reserving %"PRIu64" entries in field hash table.", s); |
| 1325 |
|
| 1326 |
r = journal_file_append_object(f, |
| 1327 |
OBJECT_FIELD_HASH_TABLE, |
| 1328 |
offsetof(Object, hash_table.items) + s * sizeof(HashItem), |
| 1329 |
&o, &p); |
| 1330 |
if (r < 0) |
| 1331 |
return r; |
| 1332 |
|
| 1333 |
memzero(o->hash_table.items, s * sizeof(HashItem)); |
| 1334 |
|
| 1335 |
f->header->field_hash_table_offset = htole64(p + offsetof(Object, hash_table.items)); |
| 1336 |
f->header->field_hash_table_size = htole64(s * sizeof(HashItem)); |
| 1337 |
|
| 1338 |
return 0; |
| 1339 |
} |
| 1340 |
|
| 1341 |
int journal_file_map_data_hash_table(JournalFile *f) { |
| 1342 |
uint64_t s, p; |
| 1343 |
void *t; |
| 1344 |
int r; |
| 1345 |
|
| 1346 |
assert(f); |
| 1347 |
assert(f->header); |
| 1348 |
|
| 1349 |
if (f->data_hash_table) |
| 1350 |
return 0; |
| 1351 |
|
| 1352 |
p = le64toh(f->header->data_hash_table_offset); |
| 1353 |
s = le64toh(f->header->data_hash_table_size); |
| 1354 |
|
| 1355 |
r = journal_file_move_to(f, |
| 1356 |
OBJECT_DATA_HASH_TABLE, |
| 1357 |
true, |
| 1358 |
p, s, |
| 1359 |
&t); |
| 1360 |
if (r < 0) |
| 1361 |
return r; |
| 1362 |
|
| 1363 |
f->data_hash_table = t; |
| 1364 |
return 0; |
| 1365 |
} |
| 1366 |
|
| 1367 |
int journal_file_map_field_hash_table(JournalFile *f) { |
| 1368 |
uint64_t s, p; |
| 1369 |
void *t; |
| 1370 |
int r; |
| 1371 |
|
| 1372 |
assert(f); |
| 1373 |
assert(f->header); |
| 1374 |
|
| 1375 |
if (f->field_hash_table) |
| 1376 |
return 0; |
| 1377 |
|
| 1378 |
p = le64toh(f->header->field_hash_table_offset); |
| 1379 |
s = le64toh(f->header->field_hash_table_size); |
| 1380 |
|
| 1381 |
r = journal_file_move_to(f, |
| 1382 |
OBJECT_FIELD_HASH_TABLE, |
| 1383 |
true, |
| 1384 |
p, s, |
| 1385 |
&t); |
| 1386 |
if (r < 0) |
| 1387 |
return r; |
| 1388 |
|
| 1389 |
f->field_hash_table = t; |
| 1390 |
return 0; |
| 1391 |
} |
| 1392 |
|
| 1393 |
static int journal_file_link_field( |
| 1394 |
JournalFile *f, |
| 1395 |
Object *o, |
| 1396 |
uint64_t offset, |
| 1397 |
uint64_t hash) { |
| 1398 |
|
| 1399 |
uint64_t p, h, m; |
| 1400 |
int r; |
| 1401 |
|
| 1402 |
assert(f); |
| 1403 |
assert(f->header); |
| 1404 |
assert(f->field_hash_table); |
| 1405 |
assert(o); |
| 1406 |
assert(offset > 0); |
| 1407 |
|
| 1408 |
if (o->object.type != OBJECT_FIELD) |
| 1409 |
return -EBADMSG; |
| 1410 |
|
| 1411 |
m = le64toh(READ_NOW(f->header->field_hash_table_size)) / sizeof(HashItem); |
| 1412 |
if (m <= 0) |
| 1413 |
return -EBADMSG; |
| 1414 |
|
| 1415 |
/* This might alter the window we are looking at */ |
| 1416 |
o->field.next_hash_offset = o->field.head_data_offset = 0; |
| 1417 |
|
| 1418 |
h = hash % m; |
| 1419 |
p = le64toh(f->field_hash_table[h].tail_hash_offset); |
| 1420 |
if (p == 0) |
| 1421 |
f->field_hash_table[h].head_hash_offset = htole64(offset); |
| 1422 |
else { |
| 1423 |
r = journal_file_move_to_object(f, OBJECT_FIELD, p, &o); |
| 1424 |
if (r < 0) |
| 1425 |
return r; |
| 1426 |
|
| 1427 |
o->field.next_hash_offset = htole64(offset); |
| 1428 |
} |
| 1429 |
|
| 1430 |
f->field_hash_table[h].tail_hash_offset = htole64(offset); |
| 1431 |
|
| 1432 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_fields)) |
| 1433 |
f->header->n_fields = htole64(le64toh(f->header->n_fields) + 1); |
| 1434 |
|
| 1435 |
return 0; |
| 1436 |
} |
| 1437 |
|
| 1438 |
static int journal_file_link_data( |
| 1439 |
JournalFile *f, |
| 1440 |
Object *o, |
| 1441 |
uint64_t offset, |
| 1442 |
uint64_t hash) { |
| 1443 |
|
| 1444 |
uint64_t p, h, m; |
| 1445 |
int r; |
| 1446 |
|
| 1447 |
assert(f); |
| 1448 |
assert(f->header); |
| 1449 |
assert(f->data_hash_table); |
| 1450 |
assert(o); |
| 1451 |
assert(offset > 0); |
| 1452 |
|
| 1453 |
if (o->object.type != OBJECT_DATA) |
| 1454 |
return -EBADMSG; |
| 1455 |
|
| 1456 |
m = le64toh(READ_NOW(f->header->data_hash_table_size)) / sizeof(HashItem); |
| 1457 |
if (m <= 0) |
| 1458 |
return -EBADMSG; |
| 1459 |
|
| 1460 |
/* This might alter the window we are looking at */ |
| 1461 |
o->data.next_hash_offset = o->data.next_field_offset = 0; |
| 1462 |
o->data.entry_offset = o->data.entry_array_offset = 0; |
| 1463 |
o->data.n_entries = 0; |
| 1464 |
|
| 1465 |
h = hash % m; |
| 1466 |
p = le64toh(f->data_hash_table[h].tail_hash_offset); |
| 1467 |
if (p == 0) |
| 1468 |
/* Only entry in the hash table is easy */ |
| 1469 |
f->data_hash_table[h].head_hash_offset = htole64(offset); |
| 1470 |
else { |
| 1471 |
/* Move back to the previous data object, to patch in |
| 1472 |
* pointer */ |
| 1473 |
|
| 1474 |
r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); |
| 1475 |
if (r < 0) |
| 1476 |
return r; |
| 1477 |
|
| 1478 |
o->data.next_hash_offset = htole64(offset); |
| 1479 |
} |
| 1480 |
|
| 1481 |
f->data_hash_table[h].tail_hash_offset = htole64(offset); |
| 1482 |
|
| 1483 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_data)) |
| 1484 |
f->header->n_data = htole64(le64toh(f->header->n_data) + 1); |
| 1485 |
|
| 1486 |
return 0; |
| 1487 |
} |
| 1488 |
|
| 1489 |
static int get_next_hash_offset( |
| 1490 |
JournalFile *f, |
| 1491 |
uint64_t *p, |
| 1492 |
le64_t *next_hash_offset, |
| 1493 |
uint64_t *depth, |
| 1494 |
le64_t *header_max_depth) { |
| 1495 |
|
| 1496 |
uint64_t nextp; |
| 1497 |
|
| 1498 |
assert(f); |
| 1499 |
assert(p); |
| 1500 |
assert(next_hash_offset); |
| 1501 |
assert(depth); |
| 1502 |
|
| 1503 |
nextp = le64toh(READ_NOW(*next_hash_offset)); |
| 1504 |
if (nextp > 0) { |
| 1505 |
if (nextp <= *p) /* Refuse going in loops */ |
| 1506 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 1507 |
"Detected hash item loop in %s, refusing.", f->path); |
| 1508 |
|
| 1509 |
(*depth)++; |
| 1510 |
|
| 1511 |
/* If the depth of this hash chain is larger than all others we have seen so far, record it */ |
| 1512 |
if (header_max_depth && journal_file_writable(f)) |
| 1513 |
*header_max_depth = htole64(MAX(*depth, le64toh(*header_max_depth))); |
| 1514 |
} |
| 1515 |
|
| 1516 |
*p = nextp; |
| 1517 |
return 0; |
| 1518 |
} |
| 1519 |
|
| 1520 |
int journal_file_find_field_object_with_hash( |
| 1521 |
JournalFile *f, |
| 1522 |
const void *field, |
| 1523 |
uint64_t size, |
| 1524 |
uint64_t hash, |
| 1525 |
Object **ret_object, |
| 1526 |
uint64_t *ret_offset) { |
| 1527 |
|
| 1528 |
uint64_t p, osize, h, m, depth = 0; |
| 1529 |
int r; |
| 1530 |
|
| 1531 |
assert(f); |
| 1532 |
assert(f->header); |
| 1533 |
assert(field); |
| 1534 |
assert(size > 0); |
| 1535 |
|
| 1536 |
/* If the field hash table is empty, we can't find anything */ |
| 1537 |
if (le64toh(f->header->field_hash_table_size) <= 0) |
| 1538 |
return 0; |
| 1539 |
|
| 1540 |
/* Map the field hash table, if it isn't mapped yet. */ |
| 1541 |
r = journal_file_map_field_hash_table(f); |
| 1542 |
if (r < 0) |
| 1543 |
return r; |
| 1544 |
|
| 1545 |
osize = offsetof(Object, field.payload) + size; |
| 1546 |
|
| 1547 |
m = le64toh(READ_NOW(f->header->field_hash_table_size)) / sizeof(HashItem); |
| 1548 |
if (m <= 0) |
| 1549 |
return -EBADMSG; |
| 1550 |
|
| 1551 |
h = hash % m; |
| 1552 |
p = le64toh(f->field_hash_table[h].head_hash_offset); |
| 1553 |
while (p > 0) { |
| 1554 |
Object *o; |
| 1555 |
|
| 1556 |
r = journal_file_move_to_object(f, OBJECT_FIELD, p, &o); |
| 1557 |
if (r < 0) |
| 1558 |
return r; |
| 1559 |
|
| 1560 |
if (le64toh(o->field.hash) == hash && |
| 1561 |
le64toh(o->object.size) == osize && |
| 1562 |
memcmp(o->field.payload, field, size) == 0) { |
| 1563 |
|
| 1564 |
if (ret_object) |
| 1565 |
*ret_object = o; |
| 1566 |
if (ret_offset) |
| 1567 |
*ret_offset = p; |
| 1568 |
|
| 1569 |
return 1; |
| 1570 |
} |
| 1571 |
|
| 1572 |
r = get_next_hash_offset( |
| 1573 |
f, |
| 1574 |
&p, |
| 1575 |
&o->field.next_hash_offset, |
| 1576 |
&depth, |
| 1577 |
JOURNAL_HEADER_CONTAINS(f->header, field_hash_chain_depth) ? &f->header->field_hash_chain_depth : NULL); |
| 1578 |
if (r < 0) |
| 1579 |
return r; |
| 1580 |
} |
| 1581 |
|
| 1582 |
return 0; |
| 1583 |
} |
| 1584 |
|
| 1585 |
uint64_t journal_file_hash_data( |
| 1586 |
JournalFile *f, |
| 1587 |
const void *data, |
| 1588 |
size_t sz) { |
| 1589 |
|
| 1590 |
assert(f); |
| 1591 |
assert(f->header); |
| 1592 |
assert(data || sz == 0); |
| 1593 |
|
| 1594 |
/* We try to unify our codebase on siphash, hence new-styled journal files utilizing the keyed hash |
| 1595 |
* function use siphash. Old journal files use the Jenkins hash. */ |
| 1596 |
|
| 1597 |
if (JOURNAL_HEADER_KEYED_HASH(f->header)) |
| 1598 |
return siphash24(data, sz, f->header->file_id.bytes); |
| 1599 |
|
| 1600 |
return jenkins_hash64(data, sz); |
| 1601 |
} |
| 1602 |
|
| 1603 |
int journal_file_find_field_object( |
| 1604 |
JournalFile *f, |
| 1605 |
const void *field, |
| 1606 |
uint64_t size, |
| 1607 |
Object **ret_object, |
| 1608 |
uint64_t *ret_offset) { |
| 1609 |
|
| 1610 |
assert(f); |
| 1611 |
assert(field); |
| 1612 |
assert(size > 0); |
| 1613 |
|
| 1614 |
return journal_file_find_field_object_with_hash( |
| 1615 |
f, |
| 1616 |
field, size, |
| 1617 |
journal_file_hash_data(f, field, size), |
| 1618 |
ret_object, ret_offset); |
| 1619 |
} |
| 1620 |
|
| 1621 |
int journal_file_find_data_object_with_hash( |
| 1622 |
JournalFile *f, |
| 1623 |
const void *data, |
| 1624 |
uint64_t size, |
| 1625 |
uint64_t hash, |
| 1626 |
Object **ret_object, |
| 1627 |
uint64_t *ret_offset) { |
| 1628 |
|
| 1629 |
uint64_t p, h, m, depth = 0; |
| 1630 |
int r; |
| 1631 |
|
| 1632 |
assert(f); |
| 1633 |
assert(f->header); |
| 1634 |
assert(data || size == 0); |
| 1635 |
|
| 1636 |
/* If there's no data hash table, then there's no entry. */ |
| 1637 |
if (le64toh(f->header->data_hash_table_size) <= 0) |
| 1638 |
return 0; |
| 1639 |
|
| 1640 |
/* Map the data hash table, if it isn't mapped yet. */ |
| 1641 |
r = journal_file_map_data_hash_table(f); |
| 1642 |
if (r < 0) |
| 1643 |
return r; |
| 1644 |
|
| 1645 |
m = le64toh(READ_NOW(f->header->data_hash_table_size)) / sizeof(HashItem); |
| 1646 |
if (m <= 0) |
| 1647 |
return -EBADMSG; |
| 1648 |
|
| 1649 |
h = hash % m; |
| 1650 |
p = le64toh(f->data_hash_table[h].head_hash_offset); |
| 1651 |
|
| 1652 |
while (p > 0) { |
| 1653 |
Object *o; |
| 1654 |
void *d; |
| 1655 |
size_t rsize; |
| 1656 |
|
| 1657 |
r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); |
| 1658 |
if (r < 0) |
| 1659 |
return r; |
| 1660 |
|
| 1661 |
if (le64toh(o->data.hash) != hash) |
| 1662 |
goto next; |
| 1663 |
|
| 1664 |
r = journal_file_data_payload(f, o, p, NULL, 0, 0, &d, &rsize); |
| 1665 |
if (r < 0) |
| 1666 |
return r; |
| 1667 |
assert(r > 0); /* journal_file_data_payload() always returns > 0 if no field is provided. */ |
| 1668 |
|
| 1669 |
if (memcmp_nn(data, size, d, rsize) == 0) { |
| 1670 |
if (ret_object) |
| 1671 |
*ret_object = o; |
| 1672 |
|
| 1673 |
if (ret_offset) |
| 1674 |
*ret_offset = p; |
| 1675 |
|
| 1676 |
return 1; |
| 1677 |
} |
| 1678 |
|
| 1679 |
next: |
| 1680 |
r = get_next_hash_offset( |
| 1681 |
f, |
| 1682 |
&p, |
| 1683 |
&o->data.next_hash_offset, |
| 1684 |
&depth, |
| 1685 |
JOURNAL_HEADER_CONTAINS(f->header, data_hash_chain_depth) ? &f->header->data_hash_chain_depth : NULL); |
| 1686 |
if (r < 0) |
| 1687 |
return r; |
| 1688 |
} |
| 1689 |
|
| 1690 |
return 0; |
| 1691 |
} |
| 1692 |
|
| 1693 |
int journal_file_find_data_object( |
| 1694 |
JournalFile *f, |
| 1695 |
const void *data, |
| 1696 |
uint64_t size, |
| 1697 |
Object **ret_object, |
| 1698 |
uint64_t *ret_offset) { |
| 1699 |
|
| 1700 |
assert(f); |
| 1701 |
assert(data || size == 0); |
| 1702 |
|
| 1703 |
return journal_file_find_data_object_with_hash( |
| 1704 |
f, |
| 1705 |
data, size, |
| 1706 |
journal_file_hash_data(f, data, size), |
| 1707 |
ret_object, ret_offset); |
| 1708 |
} |
| 1709 |
|
| 1710 |
bool journal_field_valid(const char *p, size_t l, bool allow_protected) { |
| 1711 |
/* We kinda enforce POSIX syntax recommendations for |
| 1712 |
environment variables here, but make a couple of additional |
| 1713 |
requirements. |
| 1714 |
|
| 1715 |
http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html */ |
| 1716 |
|
| 1717 |
assert(p); |
| 1718 |
|
| 1719 |
if (l == SIZE_MAX) |
| 1720 |
l = strlen(p); |
| 1721 |
|
| 1722 |
/* No empty field names */ |
| 1723 |
if (l <= 0) |
| 1724 |
return false; |
| 1725 |
|
| 1726 |
/* Don't allow names longer than 64 chars */ |
| 1727 |
if (l > 64) |
| 1728 |
return false; |
| 1729 |
|
| 1730 |
/* Variables starting with an underscore are protected */ |
| 1731 |
if (!allow_protected && p[0] == '_') |
| 1732 |
return false; |
| 1733 |
|
| 1734 |
/* Don't allow digits as first character */ |
| 1735 |
if (ascii_isdigit(p[0])) |
| 1736 |
return false; |
| 1737 |
|
| 1738 |
/* Only allow A-Z0-9 and '_' */ |
| 1739 |
for (const char *a = p; a < p + l; a++) |
| 1740 |
if ((*a < 'A' || *a > 'Z') && |
| 1741 |
!ascii_isdigit(*a) && |
| 1742 |
*a != '_') |
| 1743 |
return false; |
| 1744 |
|
| 1745 |
return true; |
| 1746 |
} |
| 1747 |
|
| 1748 |
static int journal_file_append_field( |
| 1749 |
JournalFile *f, |
| 1750 |
const void *field, |
| 1751 |
uint64_t size, |
| 1752 |
Object **ret_object, |
| 1753 |
uint64_t *ret_offset) { |
| 1754 |
|
| 1755 |
uint64_t hash, p; |
| 1756 |
uint64_t osize; |
| 1757 |
Object *o; |
| 1758 |
int r; |
| 1759 |
|
| 1760 |
assert(f); |
| 1761 |
assert(field); |
| 1762 |
assert(size > 0); |
| 1763 |
|
| 1764 |
if (!journal_field_valid(field, size, true)) |
| 1765 |
return -EBADMSG; |
| 1766 |
|
| 1767 |
hash = journal_file_hash_data(f, field, size); |
| 1768 |
|
| 1769 |
r = journal_file_find_field_object_with_hash(f, field, size, hash, ret_object, ret_offset); |
| 1770 |
if (r < 0) |
| 1771 |
return r; |
| 1772 |
if (r > 0) |
| 1773 |
return 0; |
| 1774 |
|
| 1775 |
osize = offsetof(Object, field.payload) + size; |
| 1776 |
r = journal_file_append_object(f, OBJECT_FIELD, osize, &o, &p); |
| 1777 |
if (r < 0) |
| 1778 |
return r; |
| 1779 |
|
| 1780 |
o->field.hash = htole64(hash); |
| 1781 |
memcpy(o->field.payload, field, size); |
| 1782 |
|
| 1783 |
r = journal_file_link_field(f, o, p, hash); |
| 1784 |
if (r < 0) |
| 1785 |
return r; |
| 1786 |
|
| 1787 |
/* The linking might have altered the window, so let's only pass the offset to hmac which will |
| 1788 |
* move to the object again if needed. */ |
| 1789 |
|
| 1790 |
#if HAVE_GCRYPT |
| 1791 |
r = journal_file_hmac_put_object(f, OBJECT_FIELD, NULL, p); |
| 1792 |
if (r < 0) |
| 1793 |
return r; |
| 1794 |
#endif |
| 1795 |
|
| 1796 |
if (ret_object) { |
| 1797 |
r = journal_file_move_to_object(f, OBJECT_FIELD, p, ret_object); |
| 1798 |
if (r < 0) |
| 1799 |
return r; |
| 1800 |
} |
| 1801 |
|
| 1802 |
if (ret_offset) |
| 1803 |
*ret_offset = p; |
| 1804 |
|
| 1805 |
return 0; |
| 1806 |
} |
| 1807 |
|
| 1808 |
static int maybe_compress_payload( |
| 1809 |
JournalFile *f, |
| 1810 |
uint8_t *dst, |
| 1811 |
const uint8_t *src, |
| 1812 |
uint64_t size, |
| 1813 |
size_t *rsize, |
| 1814 |
Compression *ret_compression) { |
| 1815 |
|
| 1816 |
assert(f); |
| 1817 |
assert(f->header); |
| 1818 |
assert(ret_compression); |
| 1819 |
|
| 1820 |
#if HAVE_COMPRESSION |
| 1821 |
Compression c; |
| 1822 |
int r; |
| 1823 |
|
| 1824 |
c = JOURNAL_FILE_COMPRESSION(f); |
| 1825 |
if (c == COMPRESSION_NONE || size < f->compress_threshold_bytes) { |
| 1826 |
*ret_compression = COMPRESSION_NONE; |
| 1827 |
return 0; |
| 1828 |
} |
| 1829 |
|
| 1830 |
r = compress_blob(c, src, size, dst, size - 1, rsize, /* level= */ -1); |
| 1831 |
if (r < 0) |
| 1832 |
return log_debug_errno(r, "Failed to compress data object using %s, ignoring: %m", compression_to_string(c)); |
| 1833 |
|
| 1834 |
log_debug("Compressed data object %"PRIu64" -> %zu using %s", size, *rsize, compression_to_string(c)); |
| 1835 |
|
| 1836 |
*ret_compression = c; |
| 1837 |
return 1; /* compressed */ |
| 1838 |
#else |
| 1839 |
*ret_compression = COMPRESSION_NONE; |
| 1840 |
return 0; |
| 1841 |
#endif |
| 1842 |
} |
| 1843 |
|
| 1844 |
static int journal_file_append_data( |
| 1845 |
JournalFile *f, |
| 1846 |
const void *data, |
| 1847 |
uint64_t size, |
| 1848 |
Object **ret_object, |
| 1849 |
uint64_t *ret_offset) { |
| 1850 |
|
| 1851 |
uint64_t hash, p, osize; |
| 1852 |
Object *o, *fo; |
| 1853 |
size_t rsize = 0; |
| 1854 |
const void *eq; |
| 1855 |
int r; |
| 1856 |
|
| 1857 |
assert(f); |
| 1858 |
|
| 1859 |
/* Return a recognizable error for data that is submitted for *writing*. Note that we leave EBADMSG |
| 1860 |
* for bad messages we *read*. And EINVAL is too generic an error (which might be triggered by all |
| 1861 |
* kinds of other places). Hence use a separate EUCLEAN here. */ |
| 1862 |
if (!data || size == 0) |
| 1863 |
return -EUCLEAN; |
| 1864 |
|
| 1865 |
hash = journal_file_hash_data(f, data, size); |
| 1866 |
|
| 1867 |
r = journal_file_find_data_object_with_hash(f, data, size, hash, ret_object, ret_offset); |
| 1868 |
if (r < 0) |
| 1869 |
return r; |
| 1870 |
if (r > 0) |
| 1871 |
return 0; |
| 1872 |
|
| 1873 |
eq = memchr(data, '=', size); |
| 1874 |
if (!eq) |
| 1875 |
return -EUCLEAN; |
| 1876 |
|
| 1877 |
osize = journal_file_data_payload_offset(f) + size; |
| 1878 |
r = journal_file_append_object(f, OBJECT_DATA, osize, &o, &p); |
| 1879 |
if (r < 0) |
| 1880 |
return r; |
| 1881 |
|
| 1882 |
o->data.hash = htole64(hash); |
| 1883 |
|
| 1884 |
Compression c; |
| 1885 |
r = maybe_compress_payload(f, journal_file_data_payload_field(f, o), data, size, &rsize, &c); |
| 1886 |
if (r <= 0) |
| 1887 |
/* We don't really care failures, let's continue without compression */ |
| 1888 |
memcpy_safe(journal_file_data_payload_field(f, o), data, size); |
| 1889 |
else { |
| 1890 |
assert(c >= 0 && c < _COMPRESSION_MAX && c != COMPRESSION_NONE); |
| 1891 |
|
| 1892 |
o->object.size = htole64(journal_file_data_payload_offset(f) + rsize); |
| 1893 |
o->object.flags |= COMPRESSION_TO_OBJECT_FLAG(c); |
| 1894 |
} |
| 1895 |
|
| 1896 |
r = journal_file_link_data(f, o, p, hash); |
| 1897 |
if (r < 0) |
| 1898 |
return r; |
| 1899 |
|
| 1900 |
/* The linking might have altered the window, so let's refresh our pointer. */ |
| 1901 |
r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); |
| 1902 |
if (r < 0) |
| 1903 |
return r; |
| 1904 |
|
| 1905 |
#if HAVE_GCRYPT |
| 1906 |
r = journal_file_hmac_put_object(f, OBJECT_DATA, o, p); |
| 1907 |
if (r < 0) |
| 1908 |
return r; |
| 1909 |
#endif |
| 1910 |
|
| 1911 |
/* Create field object ... */ |
| 1912 |
r = journal_file_append_field(f, data, (uint8_t*) eq - (uint8_t*) data, &fo, NULL); |
| 1913 |
if (r < 0) |
| 1914 |
return r; |
| 1915 |
|
| 1916 |
/* ... and link it in. */ |
| 1917 |
o->data.next_field_offset = fo->field.head_data_offset; |
| 1918 |
fo->field.head_data_offset = le64toh(p); |
| 1919 |
|
| 1920 |
if (ret_object) |
| 1921 |
*ret_object = o; |
| 1922 |
|
| 1923 |
if (ret_offset) |
| 1924 |
*ret_offset = p; |
| 1925 |
|
| 1926 |
return 0; |
| 1927 |
} |
| 1928 |
|
| 1929 |
static int maybe_decompress_payload( |
| 1930 |
JournalFile *f, |
| 1931 |
uint8_t *payload, |
| 1932 |
uint64_t size, |
| 1933 |
Compression compression, |
| 1934 |
const char *field, |
| 1935 |
size_t field_length, |
| 1936 |
size_t data_threshold, |
| 1937 |
void **ret_data, |
| 1938 |
size_t *ret_size) { |
| 1939 |
|
| 1940 |
assert(f); |
| 1941 |
|
| 1942 |
/* We can't read objects larger than 4G on a 32-bit machine */ |
| 1943 |
#if __SIZEOF_SIZE_T__ == 4 |
| 1944 |
if (size > UINT32_MAX) |
| 1945 |
return -E2BIG; |
| 1946 |
#endif |
| 1947 |
|
| 1948 |
if (compression != COMPRESSION_NONE) { |
| 1949 |
#if HAVE_COMPRESSION |
| 1950 |
size_t rsize; |
| 1951 |
int r; |
| 1952 |
|
| 1953 |
if (field) { |
| 1954 |
r = decompress_startswith(compression, payload, size, &f->compress_buffer, field, |
| 1955 |
field_length, '='); |
| 1956 |
if (r < 0) |
| 1957 |
return log_debug_errno(r, |
| 1958 |
"Cannot decompress %s object of length %" PRIu64 ": %m", |
| 1959 |
compression_to_string(compression), |
| 1960 |
size); |
| 1961 |
if (r == 0) { |
| 1962 |
if (ret_data) |
| 1963 |
*ret_data = NULL; |
| 1964 |
if (ret_size) |
| 1965 |
*ret_size = 0; |
| 1966 |
return 0; |
| 1967 |
} |
| 1968 |
|
| 1969 |
/* Caller only wants to check field existence, skip full decompression */ |
| 1970 |
if (!ret_data && !ret_size) |
| 1971 |
return 1; |
| 1972 |
} |
| 1973 |
|
| 1974 |
r = decompress_blob(compression, payload, size, &f->compress_buffer, &rsize, DATA_SIZE_MAX); |
| 1975 |
if (r < 0) |
| 1976 |
return r; |
| 1977 |
|
| 1978 |
if (ret_data) |
| 1979 |
*ret_data = f->compress_buffer; |
| 1980 |
if (ret_size) |
| 1981 |
*ret_size = rsize; |
| 1982 |
#else |
| 1983 |
return -EPROTONOSUPPORT; |
| 1984 |
#endif |
| 1985 |
} else { |
| 1986 |
if (field && (size < field_length + 1 || memcmp(payload, field, field_length) != 0 || payload[field_length] != '=')) { |
| 1987 |
if (ret_data) |
| 1988 |
*ret_data = NULL; |
| 1989 |
if (ret_size) |
| 1990 |
*ret_size = 0; |
| 1991 |
return 0; |
| 1992 |
} |
| 1993 |
|
| 1994 |
if (ret_data) |
| 1995 |
*ret_data = payload; |
| 1996 |
if (ret_size) |
| 1997 |
*ret_size = (size_t) size; |
| 1998 |
} |
| 1999 |
|
| 2000 |
return 1; |
| 2001 |
} |
| 2002 |
|
| 2003 |
int journal_file_data_payload( |
| 2004 |
JournalFile *f, |
| 2005 |
Object *o, |
| 2006 |
uint64_t offset, |
| 2007 |
const char *field, |
| 2008 |
size_t field_length, |
| 2009 |
size_t data_threshold, |
| 2010 |
void **ret_data, |
| 2011 |
size_t *ret_size) { |
| 2012 |
|
| 2013 |
uint64_t size; |
| 2014 |
Compression c; |
| 2015 |
int r; |
| 2016 |
|
| 2017 |
assert(f); |
| 2018 |
assert(!field == (field_length == 0)); /* These must be specified together. */ |
| 2019 |
|
| 2020 |
if (!o) { |
| 2021 |
r = journal_file_move_to_object(f, OBJECT_DATA, offset, &o); |
| 2022 |
if (r < 0) |
| 2023 |
return r; |
| 2024 |
} |
| 2025 |
|
| 2026 |
size = le64toh(READ_NOW(o->object.size)); |
| 2027 |
if (size < journal_file_data_payload_offset(f)) |
| 2028 |
return -EBADMSG; |
| 2029 |
|
| 2030 |
size -= journal_file_data_payload_offset(f); |
| 2031 |
|
| 2032 |
c = COMPRESSION_FROM_OBJECT(o); |
| 2033 |
if (c < 0) |
| 2034 |
return -EPROTONOSUPPORT; |
| 2035 |
|
| 2036 |
return maybe_decompress_payload(f, journal_file_data_payload_field(f, o), size, c, field, |
| 2037 |
field_length, data_threshold, ret_data, ret_size); |
| 2038 |
} |
| 2039 |
|
| 2040 |
uint64_t journal_file_entry_n_items(JournalFile *f, Object *o) { |
| 2041 |
uint64_t sz; |
| 2042 |
|
| 2043 |
assert(f); |
| 2044 |
assert(o); |
| 2045 |
|
| 2046 |
if (o->object.type != OBJECT_ENTRY) |
| 2047 |
return 0; |
| 2048 |
|
| 2049 |
sz = le64toh(READ_NOW(o->object.size)); |
| 2050 |
if (sz < offsetof(Object, entry.items)) |
| 2051 |
return 0; |
| 2052 |
|
| 2053 |
return (sz - offsetof(Object, entry.items)) / journal_file_entry_item_size(f); |
| 2054 |
} |
| 2055 |
|
| 2056 |
uint64_t journal_file_entry_array_n_items(JournalFile *f, Object *o) { |
| 2057 |
uint64_t sz; |
| 2058 |
|
| 2059 |
assert(f); |
| 2060 |
assert(o); |
| 2061 |
|
| 2062 |
if (o->object.type != OBJECT_ENTRY_ARRAY) |
| 2063 |
return 0; |
| 2064 |
|
| 2065 |
sz = le64toh(READ_NOW(o->object.size)); |
| 2066 |
if (sz < offsetof(Object, entry_array.items)) |
| 2067 |
return 0; |
| 2068 |
|
| 2069 |
return (sz - offsetof(Object, entry_array.items)) / journal_file_entry_array_item_size(f); |
| 2070 |
} |
| 2071 |
|
| 2072 |
uint64_t journal_file_hash_table_n_items(Object *o) { |
| 2073 |
uint64_t sz; |
| 2074 |
|
| 2075 |
assert(o); |
| 2076 |
|
| 2077 |
if (!IN_SET(o->object.type, OBJECT_DATA_HASH_TABLE, OBJECT_FIELD_HASH_TABLE)) |
| 2078 |
return 0; |
| 2079 |
|
| 2080 |
sz = le64toh(READ_NOW(o->object.size)); |
| 2081 |
if (sz < offsetof(Object, hash_table.items)) |
| 2082 |
return 0; |
| 2083 |
|
| 2084 |
return (sz - offsetof(Object, hash_table.items)) / sizeof(HashItem); |
| 2085 |
} |
| 2086 |
|
| 2087 |
static void write_entry_array_item(JournalFile *f, Object *o, uint64_t i, uint64_t p) { |
| 2088 |
assert(f); |
| 2089 |
assert(o); |
| 2090 |
|
| 2091 |
if (JOURNAL_HEADER_COMPACT(f->header)) { |
| 2092 |
assert(p <= UINT32_MAX); |
| 2093 |
o->entry_array.items.compact[i] = htole32(p); |
| 2094 |
} else |
| 2095 |
o->entry_array.items.regular[i] = htole64(p); |
| 2096 |
} |
| 2097 |
|
| 2098 |
static int link_entry_into_array( |
| 2099 |
JournalFile *f, |
| 2100 |
le64_t *first, |
| 2101 |
le64_t *idx, |
| 2102 |
le32_t *tail, |
| 2103 |
le32_t *tidx, |
| 2104 |
uint64_t p) { |
| 2105 |
|
| 2106 |
uint64_t n = 0, ap = 0, q, i, a, hidx; |
| 2107 |
Object *o; |
| 2108 |
int r; |
| 2109 |
|
| 2110 |
assert(f); |
| 2111 |
assert(f->header); |
| 2112 |
assert(first); |
| 2113 |
assert(idx); |
| 2114 |
POINTER_MAY_BE_NULL(tail); |
| 2115 |
POINTER_MAY_BE_NULL(tidx); |
| 2116 |
assert(p > 0); |
| 2117 |
|
| 2118 |
a = tail ? le32toh(*tail) : le64toh(*first); |
| 2119 |
hidx = le64toh(READ_NOW(*idx)); |
| 2120 |
i = tidx ? le32toh(READ_NOW(*tidx)) : hidx; |
| 2121 |
|
| 2122 |
while (a > 0) { |
| 2123 |
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); |
| 2124 |
if (r < 0) |
| 2125 |
return r; |
| 2126 |
|
| 2127 |
n = journal_file_entry_array_n_items(f, o); |
| 2128 |
if (i < n) { |
| 2129 |
write_entry_array_item(f, o, i, p); |
| 2130 |
*idx = htole64(hidx + 1); |
| 2131 |
if (tidx) |
| 2132 |
*tidx = htole32(le32toh(*tidx) + 1); |
| 2133 |
return 0; |
| 2134 |
} |
| 2135 |
|
| 2136 |
i -= n; |
| 2137 |
ap = a; |
| 2138 |
a = le64toh(o->entry_array.next_entry_array_offset); |
| 2139 |
} |
| 2140 |
|
| 2141 |
if (hidx > n) |
| 2142 |
n = (hidx+1) * 2; |
| 2143 |
else |
| 2144 |
n = n * 2; |
| 2145 |
|
| 2146 |
if (n < 4) |
| 2147 |
n = 4; |
| 2148 |
|
| 2149 |
r = journal_file_append_object(f, OBJECT_ENTRY_ARRAY, |
| 2150 |
offsetof(Object, entry_array.items) + n * journal_file_entry_array_item_size(f), |
| 2151 |
&o, &q); |
| 2152 |
if (r < 0) |
| 2153 |
return r; |
| 2154 |
|
| 2155 |
#if HAVE_GCRYPT |
| 2156 |
r = journal_file_hmac_put_object(f, OBJECT_ENTRY_ARRAY, o, q); |
| 2157 |
if (r < 0) |
| 2158 |
return r; |
| 2159 |
#endif |
| 2160 |
|
| 2161 |
write_entry_array_item(f, o, i, p); |
| 2162 |
|
| 2163 |
if (ap == 0) |
| 2164 |
*first = htole64(q); |
| 2165 |
else { |
| 2166 |
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, ap, &o); |
| 2167 |
if (r < 0) |
| 2168 |
return r; |
| 2169 |
|
| 2170 |
o->entry_array.next_entry_array_offset = htole64(q); |
| 2171 |
} |
| 2172 |
|
| 2173 |
if (tail) |
| 2174 |
*tail = htole32(q); |
| 2175 |
|
| 2176 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays)) |
| 2177 |
f->header->n_entry_arrays = htole64(le64toh(f->header->n_entry_arrays) + 1); |
| 2178 |
|
| 2179 |
*idx = htole64(hidx + 1); |
| 2180 |
if (tidx) |
| 2181 |
*tidx = htole32(1); |
| 2182 |
|
| 2183 |
return 0; |
| 2184 |
} |
| 2185 |
|
| 2186 |
static int link_entry_into_array_plus_one( |
| 2187 |
JournalFile *f, |
| 2188 |
le64_t *extra, |
| 2189 |
le64_t *first, |
| 2190 |
le64_t *idx, |
| 2191 |
le32_t *tail, |
| 2192 |
le32_t *tidx, |
| 2193 |
uint64_t p) { |
| 2194 |
|
| 2195 |
uint64_t hidx; |
| 2196 |
int r; |
| 2197 |
|
| 2198 |
assert(f); |
| 2199 |
assert(extra); |
| 2200 |
assert(first); |
| 2201 |
assert(idx); |
| 2202 |
assert(p > 0); |
| 2203 |
|
| 2204 |
hidx = le64toh(READ_NOW(*idx)); |
| 2205 |
if (hidx == UINT64_MAX) |
| 2206 |
return -EBADMSG; |
| 2207 |
if (hidx == 0) |
| 2208 |
*extra = htole64(p); |
| 2209 |
else { |
| 2210 |
le64_t i; |
| 2211 |
|
| 2212 |
i = htole64(hidx - 1); |
| 2213 |
r = link_entry_into_array(f, first, &i, tail, tidx, p); |
| 2214 |
if (r < 0) |
| 2215 |
return r; |
| 2216 |
} |
| 2217 |
|
| 2218 |
*idx = htole64(hidx + 1); |
| 2219 |
return 0; |
| 2220 |
} |
| 2221 |
|
| 2222 |
static int journal_file_link_entry_item(JournalFile *f, uint64_t offset, uint64_t p) { |
| 2223 |
Object *o; |
| 2224 |
int r; |
| 2225 |
|
| 2226 |
assert(f); |
| 2227 |
assert(offset > 0); |
| 2228 |
|
| 2229 |
r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); |
| 2230 |
if (r < 0) |
| 2231 |
return r; |
| 2232 |
|
| 2233 |
return link_entry_into_array_plus_one(f, |
| 2234 |
&o->data.entry_offset, |
| 2235 |
&o->data.entry_array_offset, |
| 2236 |
&o->data.n_entries, |
| 2237 |
JOURNAL_HEADER_COMPACT(f->header) ? &o->data.compact.tail_entry_array_offset : NULL, |
| 2238 |
JOURNAL_HEADER_COMPACT(f->header) ? &o->data.compact.tail_entry_array_n_entries : NULL, |
| 2239 |
offset); |
| 2240 |
} |
| 2241 |
|
| 2242 |
static int journal_file_link_entry( |
| 2243 |
JournalFile *f, |
| 2244 |
const Object *o, |
| 2245 |
uint64_t offset, |
| 2246 |
const EntryItem items[], |
| 2247 |
size_t n_items) { |
| 2248 |
|
| 2249 |
int r; |
| 2250 |
|
| 2251 |
assert(f); |
| 2252 |
assert(f->header); |
| 2253 |
assert(o); |
| 2254 |
assert(offset > 0); |
| 2255 |
|
| 2256 |
if (o->object.type != OBJECT_ENTRY) |
| 2257 |
return -EBADMSG; |
| 2258 |
|
| 2259 |
__atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 2260 |
|
| 2261 |
/* Link up the entry itself */ |
| 2262 |
r = link_entry_into_array(f, |
| 2263 |
&f->header->entry_array_offset, |
| 2264 |
&f->header->n_entries, |
| 2265 |
JOURNAL_HEADER_CONTAINS(f->header, tail_entry_array_offset) ? &f->header->tail_entry_array_offset : NULL, |
| 2266 |
JOURNAL_HEADER_CONTAINS(f->header, tail_entry_array_n_entries) ? &f->header->tail_entry_array_n_entries : NULL, |
| 2267 |
offset); |
| 2268 |
if (r < 0) |
| 2269 |
return r; |
| 2270 |
|
| 2271 |
/* log_debug("=> %s seqnr=%"PRIu64" n_entries=%"PRIu64, f->path, o->entry.seqnum, f->header->n_entries); */ |
| 2272 |
|
| 2273 |
if (f->header->head_entry_realtime == 0) |
| 2274 |
f->header->head_entry_realtime = o->entry.realtime; |
| 2275 |
|
| 2276 |
f->header->tail_entry_realtime = o->entry.realtime; |
| 2277 |
f->header->tail_entry_monotonic = o->entry.monotonic; |
| 2278 |
if (JOURNAL_HEADER_CONTAINS(f->header, tail_entry_offset)) |
| 2279 |
f->header->tail_entry_offset = htole64(offset); |
| 2280 |
|
| 2281 |
/* Link up the items */ |
| 2282 |
for (uint64_t i = 0; i < n_items; i++) { |
| 2283 |
int k; |
| 2284 |
|
| 2285 |
/* If we fail to link an entry item because we can't allocate a new entry array, don't fail |
| 2286 |
* immediately but try to link the other entry items since it might still be possible to link |
| 2287 |
* those if they don't require a new entry array to be allocated. */ |
| 2288 |
|
| 2289 |
k = journal_file_link_entry_item(f, offset, items[i].object_offset); |
| 2290 |
if (k == -E2BIG) |
| 2291 |
r = k; |
| 2292 |
else if (k < 0) |
| 2293 |
return k; |
| 2294 |
} |
| 2295 |
|
| 2296 |
return r; |
| 2297 |
} |
| 2298 |
|
| 2299 |
static void write_entry_item(JournalFile *f, Object *o, uint64_t i, const EntryItem *item) { |
| 2300 |
assert(f); |
| 2301 |
assert(o); |
| 2302 |
assert(item); |
| 2303 |
|
| 2304 |
if (JOURNAL_HEADER_COMPACT(f->header)) { |
| 2305 |
assert(item->object_offset <= UINT32_MAX); |
| 2306 |
o->entry.items.compact[i].object_offset = htole32(item->object_offset); |
| 2307 |
} else { |
| 2308 |
o->entry.items.regular[i].object_offset = htole64(item->object_offset); |
| 2309 |
o->entry.items.regular[i].hash = htole64(item->hash); |
| 2310 |
} |
| 2311 |
} |
| 2312 |
|
| 2313 |
static int journal_file_append_entry_internal( |
| 2314 |
JournalFile *f, |
| 2315 |
const dual_timestamp *ts, |
| 2316 |
const sd_id128_t *boot_id, |
| 2317 |
const sd_id128_t *machine_id, |
| 2318 |
uint64_t xor_hash, |
| 2319 |
const EntryItem items[], |
| 2320 |
size_t n_items, |
| 2321 |
uint64_t *seqnum, |
| 2322 |
sd_id128_t *seqnum_id, |
| 2323 |
Object **ret_object, |
| 2324 |
uint64_t *ret_offset) { |
| 2325 |
|
| 2326 |
uint64_t np; |
| 2327 |
uint64_t osize; |
| 2328 |
Object *o; |
| 2329 |
int r; |
| 2330 |
|
| 2331 |
assert(f); |
| 2332 |
assert(f->header); |
| 2333 |
assert(ts); |
| 2334 |
assert(boot_id); |
| 2335 |
assert(!sd_id128_is_null(*boot_id)); |
| 2336 |
assert(items || n_items == 0); |
| 2337 |
|
| 2338 |
if (f->strict_order) { |
| 2339 |
/* If requested be stricter with ordering in this journal file, to make searching via |
| 2340 |
* bisection fully deterministic. This is an optional feature, so that if desired journal |
| 2341 |
* files can be written where the ordering is not strictly enforced (in which case bisection |
| 2342 |
* will yield *a* result, but not the *only* result, when searching for points in |
| 2343 |
* time). Strict ordering mode is enabled when journald originally writes the files, but |
| 2344 |
* might not necessarily be if other tools (the remoting tools for example) write journal |
| 2345 |
* files from combined sources. |
| 2346 |
* |
| 2347 |
* Typically, if any of the errors generated here are seen journald will just rotate the |
| 2348 |
* journal files and start anew. */ |
| 2349 |
|
| 2350 |
if (ts->realtime < le64toh(f->header->tail_entry_realtime)) |
| 2351 |
return log_debug_errno(SYNTHETIC_ERRNO(EREMCHG), |
| 2352 |
"Realtime timestamp %" PRIu64 " smaller than previous realtime " |
| 2353 |
"timestamp %" PRIu64 ", refusing entry.", |
| 2354 |
ts->realtime, le64toh(f->header->tail_entry_realtime)); |
| 2355 |
|
| 2356 |
if (sd_id128_equal(*boot_id, f->header->tail_entry_boot_id) && |
| 2357 |
ts->monotonic < le64toh(f->header->tail_entry_monotonic)) |
| 2358 |
return log_debug_errno( |
| 2359 |
SYNTHETIC_ERRNO(ENOTNAM), |
| 2360 |
"Monotonic timestamp %" PRIu64 |
| 2361 |
" smaller than previous monotonic timestamp %" PRIu64 |
| 2362 |
" while having the same boot ID, refusing entry.", |
| 2363 |
ts->monotonic, |
| 2364 |
le64toh(f->header->tail_entry_monotonic)); |
| 2365 |
} |
| 2366 |
|
| 2367 |
if (seqnum_id) { |
| 2368 |
/* Settle the passed in sequence number ID */ |
| 2369 |
|
| 2370 |
if (sd_id128_is_null(*seqnum_id)) |
| 2371 |
*seqnum_id = f->header->seqnum_id; /* Caller has none assigned, then copy the one from the file */ |
| 2372 |
else if (!sd_id128_equal(*seqnum_id, f->header->seqnum_id)) { |
| 2373 |
/* Different seqnum IDs? We can't allow entries from multiple IDs end up in the same journal. */ |
| 2374 |
if (le64toh(f->header->n_entries) == 0) |
| 2375 |
f->header->seqnum_id = *seqnum_id; /* Caller has one, and file so far has no entries, then copy the one from the caller */ |
| 2376 |
else |
| 2377 |
return log_debug_errno(SYNTHETIC_ERRNO(EILSEQ), |
| 2378 |
"Sequence number IDs don't match, refusing entry."); |
| 2379 |
} |
| 2380 |
} |
| 2381 |
|
| 2382 |
if (machine_id && sd_id128_is_null(f->header->machine_id)) |
| 2383 |
/* Initialize machine ID when not set yet */ |
| 2384 |
f->header->machine_id = *machine_id; |
| 2385 |
|
| 2386 |
osize = offsetof(Object, entry.items) + (n_items * journal_file_entry_item_size(f)); |
| 2387 |
|
| 2388 |
r = journal_file_append_object(f, OBJECT_ENTRY, osize, &o, &np); |
| 2389 |
if (r < 0) |
| 2390 |
return r; |
| 2391 |
|
| 2392 |
o->entry.seqnum = htole64(journal_file_entry_seqnum(f, seqnum)); |
| 2393 |
o->entry.realtime = htole64(ts->realtime); |
| 2394 |
o->entry.monotonic = htole64(ts->monotonic); |
| 2395 |
o->entry.xor_hash = htole64(xor_hash); |
| 2396 |
o->entry.boot_id = f->header->tail_entry_boot_id = *boot_id; |
| 2397 |
|
| 2398 |
for (size_t i = 0; i < n_items; i++) |
| 2399 |
write_entry_item(f, o, i, &items[i]); |
| 2400 |
|
| 2401 |
#if HAVE_GCRYPT |
| 2402 |
r = journal_file_hmac_put_object(f, OBJECT_ENTRY, o, np); |
| 2403 |
if (r < 0) |
| 2404 |
return r; |
| 2405 |
#endif |
| 2406 |
|
| 2407 |
r = journal_file_link_entry(f, o, np, items, n_items); |
| 2408 |
if (r < 0) |
| 2409 |
return r; |
| 2410 |
|
| 2411 |
if (ret_object) |
| 2412 |
*ret_object = o; |
| 2413 |
|
| 2414 |
if (ret_offset) |
| 2415 |
*ret_offset = np; |
| 2416 |
|
| 2417 |
return r; |
| 2418 |
} |
| 2419 |
|
| 2420 |
void journal_file_post_change(JournalFile *f) { |
| 2421 |
assert(f); |
| 2422 |
|
| 2423 |
if (f->fd < 0) |
| 2424 |
return; |
| 2425 |
|
| 2426 |
/* inotify() does not receive IN_MODIFY events from file |
| 2427 |
* accesses done via mmap(). After each access we hence |
| 2428 |
* trigger IN_MODIFY by truncating the journal file to its |
| 2429 |
* current size which triggers IN_MODIFY. */ |
| 2430 |
|
| 2431 |
__atomic_thread_fence(__ATOMIC_SEQ_CST); |
| 2432 |
|
| 2433 |
if (ftruncate(f->fd, f->last_stat.st_size) < 0) |
| 2434 |
log_debug_errno(errno, "Failed to truncate file to its own size: %m"); |
| 2435 |
} |
| 2436 |
|
| 2437 |
static int post_change_thunk(sd_event_source *timer, uint64_t usec, void *userdata) { |
| 2438 |
assert(userdata); |
| 2439 |
|
| 2440 |
journal_file_post_change(userdata); |
| 2441 |
|
| 2442 |
return 1; |
| 2443 |
} |
| 2444 |
|
| 2445 |
static void schedule_post_change(JournalFile *f) { |
| 2446 |
sd_event *e; |
| 2447 |
int r; |
| 2448 |
|
| 2449 |
assert(f); |
| 2450 |
assert(f->post_change_timer); |
| 2451 |
|
| 2452 |
assert_se(e = sd_event_source_get_event(f->post_change_timer)); |
| 2453 |
|
| 2454 |
/* If we are already going down, post the change immediately. */ |
| 2455 |
if (IN_SET(sd_event_get_state(e), SD_EVENT_EXITING, SD_EVENT_FINISHED)) |
| 2456 |
goto fail; |
| 2457 |
|
| 2458 |
r = sd_event_source_get_enabled(f->post_change_timer, NULL); |
| 2459 |
if (r < 0) { |
| 2460 |
log_debug_errno(r, "Failed to get ftruncate timer state: %m"); |
| 2461 |
goto fail; |
| 2462 |
} |
| 2463 |
if (r > 0) |
| 2464 |
return; |
| 2465 |
|
| 2466 |
r = sd_event_source_set_time_relative(f->post_change_timer, f->post_change_timer_period); |
| 2467 |
if (r < 0) { |
| 2468 |
log_debug_errno(r, "Failed to set time for scheduling ftruncate: %m"); |
| 2469 |
goto fail; |
| 2470 |
} |
| 2471 |
|
| 2472 |
r = sd_event_source_set_enabled(f->post_change_timer, SD_EVENT_ONESHOT); |
| 2473 |
if (r < 0) { |
| 2474 |
log_debug_errno(r, "Failed to enable scheduled ftruncate: %m"); |
| 2475 |
goto fail; |
| 2476 |
} |
| 2477 |
|
| 2478 |
return; |
| 2479 |
|
| 2480 |
fail: |
| 2481 |
/* On failure, let's simply post the change immediately. */ |
| 2482 |
journal_file_post_change(f); |
| 2483 |
} |
| 2484 |
|
| 2485 |
/* Enable coalesced change posting in a timer on the provided sd_event instance */ |
| 2486 |
int journal_file_enable_post_change_timer(JournalFile *f, sd_event *e, usec_t t) { |
| 2487 |
_cleanup_(sd_event_source_unrefp) sd_event_source *timer = NULL; |
| 2488 |
int r; |
| 2489 |
|
| 2490 |
assert(f); |
| 2491 |
assert_return(!f->post_change_timer, -EINVAL); |
| 2492 |
assert(e); |
| 2493 |
assert(t); |
| 2494 |
|
| 2495 |
/* If we are already going down, we cannot install the timer. |
| 2496 |
* In such case, the caller needs to call journal_file_post_change() explicitly. */ |
| 2497 |
if (IN_SET(sd_event_get_state(e), SD_EVENT_EXITING, SD_EVENT_FINISHED)) |
| 2498 |
return 0; |
| 2499 |
|
| 2500 |
r = sd_event_add_time(e, &timer, CLOCK_MONOTONIC, 0, 0, post_change_thunk, f); |
| 2501 |
if (r < 0) |
| 2502 |
return r; |
| 2503 |
|
| 2504 |
r = sd_event_source_set_enabled(timer, SD_EVENT_OFF); |
| 2505 |
if (r < 0) |
| 2506 |
return r; |
| 2507 |
|
| 2508 |
f->post_change_timer = TAKE_PTR(timer); |
| 2509 |
f->post_change_timer_period = t; |
| 2510 |
|
| 2511 |
return 1; |
| 2512 |
} |
| 2513 |
|
| 2514 |
static int entry_item_cmp(const EntryItem *a, const EntryItem *b) { |
| 2515 |
return CMP(ASSERT_PTR(a)->object_offset, ASSERT_PTR(b)->object_offset); |
| 2516 |
} |
| 2517 |
|
| 2518 |
static size_t remove_duplicate_entry_items(EntryItem items[], size_t n) { |
| 2519 |
size_t j = 1; |
| 2520 |
|
| 2521 |
assert(items || n == 0); |
| 2522 |
|
| 2523 |
if (n <= 1) |
| 2524 |
return n; |
| 2525 |
|
| 2526 |
for (size_t i = 1; i < n; i++) |
| 2527 |
if (items[i].object_offset != items[j - 1].object_offset) |
| 2528 |
items[j++] = items[i]; |
| 2529 |
|
| 2530 |
return j; |
| 2531 |
} |
| 2532 |
|
| 2533 |
int journal_file_append_entry( |
| 2534 |
JournalFile *f, |
| 2535 |
const dual_timestamp *ts, |
| 2536 |
const sd_id128_t *boot_id, |
| 2537 |
const struct iovec iovec[], |
| 2538 |
size_t n_iovec, |
| 2539 |
uint64_t *seqnum, |
| 2540 |
sd_id128_t *seqnum_id, |
| 2541 |
Object **ret_object, |
| 2542 |
uint64_t *ret_offset) { |
| 2543 |
|
| 2544 |
_cleanup_free_ EntryItem *items_alloc = NULL; |
| 2545 |
EntryItem *items; |
| 2546 |
uint64_t xor_hash = 0; |
| 2547 |
struct dual_timestamp _ts; |
| 2548 |
sd_id128_t _boot_id, _machine_id, *machine_id; |
| 2549 |
int r; |
| 2550 |
|
| 2551 |
assert(f); |
| 2552 |
assert(f->header); |
| 2553 |
assert(iovec); |
| 2554 |
assert(n_iovec > 0); |
| 2555 |
|
| 2556 |
if (ts) { |
| 2557 |
if (!VALID_REALTIME(ts->realtime)) |
| 2558 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 2559 |
"Invalid realtime timestamp %" PRIu64 ", refusing entry.", |
| 2560 |
ts->realtime); |
| 2561 |
if (!VALID_MONOTONIC(ts->monotonic)) |
| 2562 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 2563 |
"Invalid monotonic timestamp %" PRIu64 ", refusing entry.", |
| 2564 |
ts->monotonic); |
| 2565 |
} else { |
| 2566 |
dual_timestamp_now(&_ts); |
| 2567 |
ts = &_ts; |
| 2568 |
} |
| 2569 |
|
| 2570 |
if (boot_id) { |
| 2571 |
if (sd_id128_is_null(*boot_id)) |
| 2572 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "Empty boot ID, refusing entry."); |
| 2573 |
} else { |
| 2574 |
r = sd_id128_get_boot(&_boot_id); |
| 2575 |
if (r < 0) |
| 2576 |
return r; |
| 2577 |
|
| 2578 |
boot_id = &_boot_id; |
| 2579 |
} |
| 2580 |
|
| 2581 |
r = sd_id128_get_machine(&_machine_id); |
| 2582 |
if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r)) |
| 2583 |
/* Gracefully handle the machine ID not being initialized yet */ |
| 2584 |
machine_id = NULL; |
| 2585 |
else if (r < 0) |
| 2586 |
return r; |
| 2587 |
else |
| 2588 |
machine_id = &_machine_id; |
| 2589 |
|
| 2590 |
#if HAVE_GCRYPT |
| 2591 |
r = journal_file_maybe_append_tag(f, ts->realtime); |
| 2592 |
if (r < 0) |
| 2593 |
return r; |
| 2594 |
#endif |
| 2595 |
|
| 2596 |
if (n_iovec < ALLOCA_MAX / sizeof(EntryItem) / 2) |
| 2597 |
items = newa(EntryItem, n_iovec); |
| 2598 |
else { |
| 2599 |
items_alloc = new(EntryItem, n_iovec); |
| 2600 |
if (!items_alloc) |
| 2601 |
return -ENOMEM; |
| 2602 |
|
| 2603 |
items = items_alloc; |
| 2604 |
} |
| 2605 |
|
| 2606 |
for (size_t i = 0; i < n_iovec; i++) { |
| 2607 |
uint64_t p; |
| 2608 |
Object *o; |
| 2609 |
|
| 2610 |
r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p); |
| 2611 |
if (r < 0) |
| 2612 |
return r; |
| 2613 |
|
| 2614 |
/* When calculating the XOR hash field, we need to take special care if the "keyed-hash" |
| 2615 |
* journal file flag is on. We use the XOR hash field to quickly determine the identity of a |
| 2616 |
* specific record, and give records with otherwise identical position (i.e. match in seqno, |
| 2617 |
* timestamp, …) a stable ordering. But for that we can't have it that the hash of the |
| 2618 |
* objects in each file is different since they are keyed. Hence let's calculate the Jenkins |
| 2619 |
* hash here for that. This also has the benefit that cursors for old and new journal files |
| 2620 |
* are completely identical (they include the XOR hash after all). For classic Jenkins-hash |
| 2621 |
* files things are easier, we can just take the value from the stored record directly. */ |
| 2622 |
|
| 2623 |
if (JOURNAL_HEADER_KEYED_HASH(f->header)) |
| 2624 |
xor_hash ^= jenkins_hash64(iovec[i].iov_base, iovec[i].iov_len); |
| 2625 |
else |
| 2626 |
xor_hash ^= le64toh(o->data.hash); |
| 2627 |
|
| 2628 |
items[i] = (EntryItem) { |
| 2629 |
.object_offset = p, |
| 2630 |
.hash = le64toh(o->data.hash), |
| 2631 |
}; |
| 2632 |
} |
| 2633 |
|
| 2634 |
/* Order by the position on disk, in order to improve seek |
| 2635 |
* times for rotating media. */ |
| 2636 |
typesafe_qsort(items, n_iovec, entry_item_cmp); |
| 2637 |
n_iovec = remove_duplicate_entry_items(items, n_iovec); |
| 2638 |
|
| 2639 |
r = journal_file_append_entry_internal( |
| 2640 |
f, |
| 2641 |
ts, |
| 2642 |
boot_id, |
| 2643 |
machine_id, |
| 2644 |
xor_hash, |
| 2645 |
items, |
| 2646 |
n_iovec, |
| 2647 |
seqnum, |
| 2648 |
seqnum_id, |
| 2649 |
ret_object, |
| 2650 |
ret_offset); |
| 2651 |
|
| 2652 |
/* If the memory mapping triggered a SIGBUS then we return an |
| 2653 |
* IO error and ignore the error code passed down to us, since |
| 2654 |
* it is very likely just an effect of a nullified replacement |
| 2655 |
* mapping page */ |
| 2656 |
|
| 2657 |
if (mmap_cache_fd_got_sigbus(f->cache_fd)) |
| 2658 |
r = -EIO; |
| 2659 |
|
| 2660 |
if (f->post_change_timer) |
| 2661 |
schedule_post_change(f); |
| 2662 |
else |
| 2663 |
journal_file_post_change(f); |
| 2664 |
|
| 2665 |
return r; |
| 2666 |
} |
| 2667 |
|
| 2668 |
typedef struct ChainCacheItem { |
| 2669 |
uint64_t first; /* The offset of the entry array object at the beginning of the chain, |
| 2670 |
* i.e., le64toh(f->header->entry_array_offset), or le64toh(o->data.entry_offset). */ |
| 2671 |
uint64_t array; /* The offset of the cached entry array object. */ |
| 2672 |
uint64_t begin; /* The offset of the first item in the cached array. */ |
| 2673 |
uint64_t total; /* The total number of items in all arrays before the cached one in the chain. */ |
| 2674 |
uint64_t last_index; /* The last index we looked at in the cached array, to optimize locality when bisecting. */ |
| 2675 |
} ChainCacheItem; |
| 2676 |
|
| 2677 |
static void chain_cache_put( |
| 2678 |
JournalFile *f, |
| 2679 |
ChainCacheItem *ci, |
| 2680 |
uint64_t first, |
| 2681 |
uint64_t array, |
| 2682 |
uint64_t begin, |
| 2683 |
uint64_t total, |
| 2684 |
uint64_t last_index) { |
| 2685 |
|
| 2686 |
assert(f); |
| 2687 |
|
| 2688 |
if (!ci) { |
| 2689 |
/* If the chain item to cache for this chain is the |
| 2690 |
* first one it's not worth caching anything */ |
| 2691 |
if (array == first) |
| 2692 |
return; |
| 2693 |
|
| 2694 |
if (ordered_hashmap_size(f->chain_cache) >= CHAIN_CACHE_MAX) { |
| 2695 |
ci = ordered_hashmap_steal_first(f->chain_cache); |
| 2696 |
assert(ci); |
| 2697 |
} else { |
| 2698 |
ci = new(ChainCacheItem, 1); |
| 2699 |
if (!ci) |
| 2700 |
return; |
| 2701 |
} |
| 2702 |
|
| 2703 |
ci->first = first; |
| 2704 |
|
| 2705 |
if (ordered_hashmap_ensure_put(&f->chain_cache, &uint64_hash_ops_value_free, &ci->first, ci) < 0) { |
| 2706 |
free(ci); |
| 2707 |
return; |
| 2708 |
} |
| 2709 |
} else |
| 2710 |
assert(ci->first == first); |
| 2711 |
|
| 2712 |
ci->array = array; |
| 2713 |
ci->begin = begin; |
| 2714 |
ci->total = total; |
| 2715 |
ci->last_index = last_index; |
| 2716 |
} |
| 2717 |
|
| 2718 |
static int bump_array_index(uint64_t *i, direction_t direction, uint64_t n) { |
| 2719 |
assert(i); |
| 2720 |
|
| 2721 |
/* Increase or decrease the specified index, in the right direction. */ |
| 2722 |
|
| 2723 |
if (direction == DIRECTION_DOWN) { |
| 2724 |
if (*i >= n - 1) |
| 2725 |
return 0; |
| 2726 |
|
| 2727 |
(*i)++; |
| 2728 |
} else { |
| 2729 |
if (*i <= 0) |
| 2730 |
return 0; |
| 2731 |
|
| 2732 |
(*i)--; |
| 2733 |
} |
| 2734 |
|
| 2735 |
return 1; |
| 2736 |
} |
| 2737 |
|
| 2738 |
static int bump_entry_array( |
| 2739 |
JournalFile *f, |
| 2740 |
Object *o, /* the current entry array object. */ |
| 2741 |
uint64_t offset, /* the offset of the entry array object. */ |
| 2742 |
uint64_t first, /* The offset of the first entry array object in the chain. */ |
| 2743 |
direction_t direction, |
| 2744 |
uint64_t *ret) { |
| 2745 |
|
| 2746 |
int r; |
| 2747 |
|
| 2748 |
assert(f); |
| 2749 |
assert(ret); |
| 2750 |
|
| 2751 |
if (direction == DIRECTION_DOWN) { |
| 2752 |
assert(o); |
| 2753 |
|
| 2754 |
if (o->object.type != OBJECT_ENTRY_ARRAY) |
| 2755 |
return -EBADMSG; |
| 2756 |
|
| 2757 |
*ret = le64toh(o->entry_array.next_entry_array_offset); |
| 2758 |
} else { |
| 2759 |
|
| 2760 |
/* Entry array chains are a singly linked list, so to find the previous array in the chain, we have |
| 2761 |
* to start iterating from the top. */ |
| 2762 |
|
| 2763 |
assert(offset > 0); |
| 2764 |
|
| 2765 |
uint64_t p = first, q = 0; |
| 2766 |
while (p > 0 && p != offset) { |
| 2767 |
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, p, &o); |
| 2768 |
if (r < 0) |
| 2769 |
return r; |
| 2770 |
|
| 2771 |
q = p; |
| 2772 |
p = le64toh(o->entry_array.next_entry_array_offset); |
| 2773 |
} |
| 2774 |
|
| 2775 |
/* If we can't find the previous entry array in the entry array chain, we're likely dealing with a |
| 2776 |
* corrupted journal file. */ |
| 2777 |
if (p == 0) |
| 2778 |
return -EBADMSG; |
| 2779 |
|
| 2780 |
*ret = q; |
| 2781 |
} |
| 2782 |
|
| 2783 |
return *ret > 0; |
| 2784 |
} |
| 2785 |
|
| 2786 |
static int generic_array_get( |
| 2787 |
JournalFile *f, |
| 2788 |
uint64_t first, /* The offset of the first entry array object in the chain. */ |
| 2789 |
uint64_t i, /* The index of the target object counted from the beginning of the entry array chain. */ |
| 2790 |
direction_t direction, |
| 2791 |
Object **ret_object, /* The found object. */ |
| 2792 |
uint64_t *ret_offset) { /* The offset of the found object. */ |
| 2793 |
|
| 2794 |
uint64_t a, t = 0, k = 0; /* Explicit initialization of k to appease gcc */ |
| 2795 |
ChainCacheItem *ci; |
| 2796 |
Object *o = NULL; |
| 2797 |
int r; |
| 2798 |
|
| 2799 |
assert(f); |
| 2800 |
|
| 2801 |
/* FIXME: fix return value assignment on success. */ |
| 2802 |
|
| 2803 |
a = first; |
| 2804 |
|
| 2805 |
/* Try the chain cache first */ |
| 2806 |
ci = ordered_hashmap_get(f->chain_cache, &first); |
| 2807 |
if (ci && i > ci->total) { |
| 2808 |
a = ci->array; |
| 2809 |
i -= ci->total; |
| 2810 |
t = ci->total; |
| 2811 |
} |
| 2812 |
|
| 2813 |
while (a > 0) { |
| 2814 |
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); |
| 2815 |
if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) { |
| 2816 |
/* If there's corruption and we're going downwards, let's pretend we reached the |
| 2817 |
* final entry in the entry array chain. */ |
| 2818 |
|
| 2819 |
if (direction == DIRECTION_DOWN) |
| 2820 |
return 0; |
| 2821 |
|
| 2822 |
/* If there's corruption and we're going upwards, move back to the previous entry |
| 2823 |
* array and start iterating entries from there. */ |
| 2824 |
|
| 2825 |
i = UINT64_MAX; |
| 2826 |
break; |
| 2827 |
} |
| 2828 |
if (r < 0) |
| 2829 |
return r; |
| 2830 |
|
| 2831 |
k = journal_file_entry_array_n_items(f, o); |
| 2832 |
if (k == 0) |
| 2833 |
return 0; |
| 2834 |
|
| 2835 |
if (i < k) |
| 2836 |
break; |
| 2837 |
|
| 2838 |
/* The index is larger than the number of elements in the array. Let's move to the next array. */ |
| 2839 |
i -= k; |
| 2840 |
t += k; |
| 2841 |
a = le64toh(o->entry_array.next_entry_array_offset); |
| 2842 |
} |
| 2843 |
|
| 2844 |
/* If we've found the right location, now look for the first non-corrupt entry object (in the right |
| 2845 |
* direction). */ |
| 2846 |
|
| 2847 |
while (a > 0) { |
| 2848 |
if (i == UINT64_MAX) { |
| 2849 |
r = bump_entry_array(f, o, a, first, direction, &a); |
| 2850 |
if (r <= 0) |
| 2851 |
return r; |
| 2852 |
|
| 2853 |
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); |
| 2854 |
if (r < 0) |
| 2855 |
return r; |
| 2856 |
|
| 2857 |
k = journal_file_entry_array_n_items(f, o); |
| 2858 |
if (k == 0) |
| 2859 |
break; |
| 2860 |
|
| 2861 |
if (direction == DIRECTION_DOWN) |
| 2862 |
i = 0; |
| 2863 |
else { |
| 2864 |
/* We moved to the previous array. The total must be decreased. */ |
| 2865 |
if (t < k) |
| 2866 |
return -EBADMSG; /* chain cache is broken ? */ |
| 2867 |
|
| 2868 |
i = k - 1; |
| 2869 |
t -= k; |
| 2870 |
} |
| 2871 |
} |
| 2872 |
|
| 2873 |
do { |
| 2874 |
uint64_t p; |
| 2875 |
|
| 2876 |
p = journal_file_entry_array_item(f, o, i); |
| 2877 |
|
| 2878 |
r = journal_file_move_to_object(f, OBJECT_ENTRY, p, ret_object); |
| 2879 |
if (r >= 0) { |
| 2880 |
/* Let's cache this item for the next invocation */ |
| 2881 |
chain_cache_put(f, ci, first, a, journal_file_entry_array_item(f, o, 0), t, i); |
| 2882 |
|
| 2883 |
if (ret_offset) |
| 2884 |
*ret_offset = p; |
| 2885 |
|
| 2886 |
return 1; |
| 2887 |
} |
| 2888 |
if (!IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) |
| 2889 |
return r; |
| 2890 |
|
| 2891 |
/* OK, so this entry is borked. Most likely some entry didn't get synced to |
| 2892 |
* disk properly, let's see if the next one might work for us instead. */ |
| 2893 |
log_debug_errno(r, "Entry item %" PRIu64 " is bad, skipping over it.", i); |
| 2894 |
|
| 2895 |
} while (bump_array_index(&i, direction, k) > 0); |
| 2896 |
|
| 2897 |
/* All entries tried in the above do-while loop are broken. Let's move to the next (or previous) array. */ |
| 2898 |
|
| 2899 |
if (direction == DIRECTION_DOWN) |
| 2900 |
/* We are going to the next array, the total must be incremented. */ |
| 2901 |
t += k; |
| 2902 |
|
| 2903 |
i = UINT64_MAX; |
| 2904 |
} |
| 2905 |
|
| 2906 |
return 0; |
| 2907 |
} |
| 2908 |
|
| 2909 |
enum { |
| 2910 |
TEST_FOUND, /* The current object passes the test. */ |
| 2911 |
TEST_LEFT, /* The current object is in an earlier position, and the object we are looking |
| 2912 |
* for should exist in a later position. */ |
| 2913 |
TEST_RIGHT, /* The current object is in a later position, and the object we are looking for |
| 2914 |
* should exist in an earlier position. */ |
| 2915 |
TEST_GOTO_NEXT, /* No matching object exists in this array and earlier arrays, go to the next array. */ |
| 2916 |
TEST_GOTO_PREVIOUS, /* No matching object exists in this array and later arrays, go to the previous array. */ |
| 2917 |
}; |
| 2918 |
|
| 2919 |
static int generic_array_bisect_step( |
| 2920 |
JournalFile *f, |
| 2921 |
Object *array, /* entry array object */ |
| 2922 |
uint64_t i, /* index of the entry item in the array we will test. */ |
| 2923 |
uint64_t needle, |
| 2924 |
int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle), |
| 2925 |
direction_t direction, |
| 2926 |
uint64_t *m, /* The maximum number of the entries we will check in the array. */ |
| 2927 |
uint64_t *left, /* The index of the left boundary in the array. */ |
| 2928 |
uint64_t *right) { /* The index of the right boundary in the array. */ |
| 2929 |
|
| 2930 |
uint64_t p; |
| 2931 |
int r; |
| 2932 |
|
| 2933 |
assert(f); |
| 2934 |
assert(array); |
| 2935 |
assert(test_object); |
| 2936 |
assert(m); |
| 2937 |
assert(left); |
| 2938 |
assert(right); |
| 2939 |
assert(*left <= i); |
| 2940 |
assert(i <= *right); |
| 2941 |
assert(*right < *m); |
| 2942 |
|
| 2943 |
p = journal_file_entry_array_item(f, array, i); |
| 2944 |
if (p <= 0) |
| 2945 |
r = -EBADMSG; |
| 2946 |
else |
| 2947 |
r = test_object(f, p, needle); |
| 2948 |
if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) { |
| 2949 |
log_debug_errno(r, "Encountered invalid entry while bisecting, cutting algorithm short."); |
| 2950 |
|
| 2951 |
if (i == *left) { |
| 2952 |
/* This happens on two situations: |
| 2953 |
* |
| 2954 |
* a) i == 0 (hence, *left == 0): |
| 2955 |
* The first entry in the array is corrupted, let's go back to the previous array. |
| 2956 |
* |
| 2957 |
* b) *right == *left or *left + 1, and we are going to downwards: |
| 2958 |
* In that case, the (i-1)-th object has been already tested in the previous call, |
| 2959 |
* which returned TEST_LEFT. See below. So, there is no matching entry in this |
| 2960 |
* array nor in the whole entry array chain. */ |
| 2961 |
assert(i == 0 || (*right - *left <= 1 && direction == DIRECTION_DOWN)); |
| 2962 |
return TEST_GOTO_PREVIOUS; |
| 2963 |
} |
| 2964 |
|
| 2965 |
/* Otherwise, cutting the array short. So, here we limit the number of elements we will see |
| 2966 |
* in this array, and set the right boundary to the last possibly non-corrupted object. */ |
| 2967 |
*m = i; |
| 2968 |
*right = i - 1; |
| 2969 |
return TEST_RIGHT; |
| 2970 |
} |
| 2971 |
if (r < 0) |
| 2972 |
return r; |
| 2973 |
|
| 2974 |
if (r == TEST_FOUND) |
| 2975 |
/* There may be multiple entries that match with the needle. When the direction is down, we |
| 2976 |
* need to find the first matching entry, hence the right boundary can be moved, but the left |
| 2977 |
* one cannot. Similarly, when the direction is up, we need to find the last matching entry, |
| 2978 |
* hence the left boundary can be moved, but the right one cannot. */ |
| 2979 |
r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT; |
| 2980 |
|
| 2981 |
if (r == TEST_RIGHT) { |
| 2982 |
/* Currently, left --- needle --- i --- right, hence we can move the right boundary to i. */ |
| 2983 |
if (direction == DIRECTION_DOWN) |
| 2984 |
*right = i; |
| 2985 |
else { |
| 2986 |
if (i == 0) |
| 2987 |
return TEST_GOTO_PREVIOUS; |
| 2988 |
*right = i - 1; |
| 2989 |
} |
| 2990 |
} else { |
| 2991 |
/* Currently, left --- i --- needle --- right, hence we can move the left boundary to i. */ |
| 2992 |
if (direction == DIRECTION_DOWN) { |
| 2993 |
/* Note, here *m is always positive, as by the assertions at the beginning, we have |
| 2994 |
* 0 <= *left <= i <= *right < m */ |
| 2995 |
if (i == *m - 1) |
| 2996 |
return TEST_GOTO_NEXT; |
| 2997 |
|
| 2998 |
*left = i + 1; |
| 2999 |
} else |
| 3000 |
*left = i; |
| 3001 |
} |
| 3002 |
|
| 3003 |
return r; |
| 3004 |
} |
| 3005 |
|
| 3006 |
static int generic_array_bisect( |
| 3007 |
JournalFile *f, |
| 3008 |
uint64_t first, /* The offset of the first entry array object in the chain. */ |
| 3009 |
uint64_t n, /* The total number of elements in the chain of the entry array. */ |
| 3010 |
uint64_t needle, /* The target value (e.g. seqnum, monotonic, realtime, ...). */ |
| 3011 |
int (*test_object)(JournalFile *f, |
| 3012 |
uint64_t p, /* the offset of the (data or entry) object that will be tested. */ |
| 3013 |
uint64_t needle), |
| 3014 |
direction_t direction, |
| 3015 |
Object **ret_object, /* The found object. */ |
| 3016 |
uint64_t *ret_offset, /* The offset of the found object. */ |
| 3017 |
uint64_t *ret_idx) { /* The index of the found object counted from the beginning of the entry array chain. */ |
| 3018 |
|
| 3019 |
/* Given an entry array chain, this function finds the object "closest" to the given needle in the |
| 3020 |
* chain, taking into account the provided direction. A function can be provided to determine how |
| 3021 |
* an object is matched against the given needle. |
| 3022 |
* |
| 3023 |
* Given a journal file, the offset of an object and the needle, the test_object() function should |
| 3024 |
* return TEST_RIGHT if the needle is located earlier in the entry array chain, TEST_LEFT if the |
| 3025 |
* needle is located later in the entry array chain, and TEST_FOUND if the object matches the needle. |
| 3026 |
* If test_object() returns TEST_FOUND for a specific object, that object's information will be used |
| 3027 |
* to populate the return values of this function. If test_object() never returns TEST_FOUND, the |
| 3028 |
* return values are populated with the details of one of the objects closest to the needle. If the |
| 3029 |
* direction is DIRECTION_UP, the earlier object is used. Otherwise, the later object is used. |
| 3030 |
* If there are multiple objects that test_object() return TEST_FOUND for, then the first matching |
| 3031 |
* object returned when direction is DIRECTION_DOWN. Otherwise the last object is returned. */ |
| 3032 |
|
| 3033 |
uint64_t a, p, t = 0, i, last_index = UINT64_MAX; |
| 3034 |
ChainCacheItem *ci; |
| 3035 |
Object *array; |
| 3036 |
int r; |
| 3037 |
|
| 3038 |
assert(f); |
| 3039 |
assert(test_object); |
| 3040 |
|
| 3041 |
if (n <= 0) |
| 3042 |
return 0; |
| 3043 |
|
| 3044 |
/* Start with the first array in the chain */ |
| 3045 |
a = first; |
| 3046 |
|
| 3047 |
ci = ordered_hashmap_get(f->chain_cache, &first); |
| 3048 |
if (ci && n > ci->total && ci->begin != 0) { |
| 3049 |
/* Ah, we have iterated this bisection array chain previously! Let's see if we can skip ahead |
| 3050 |
* in the chain, as far as the last time. But we can't jump backwards in the chain, so let's |
| 3051 |
* check that first. */ |
| 3052 |
|
| 3053 |
r = test_object(f, ci->begin, needle); |
| 3054 |
if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) |
| 3055 |
log_debug_errno(r, "Cached entry is corrupted, ignoring: %m"); |
| 3056 |
else if (r < 0) |
| 3057 |
return r; |
| 3058 |
else if (r == TEST_LEFT) { |
| 3059 |
/* OK, what we are looking for is right of the begin of this EntryArray, so let's |
| 3060 |
* jump straight to previously cached array in the chain */ |
| 3061 |
|
| 3062 |
a = ci->array; |
| 3063 |
n -= ci->total; |
| 3064 |
t = ci->total; |
| 3065 |
last_index = ci->last_index; |
| 3066 |
} |
| 3067 |
} |
| 3068 |
|
| 3069 |
while (a > 0) { |
| 3070 |
uint64_t left, right, k, m, m_original; |
| 3071 |
|
| 3072 |
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &array); |
| 3073 |
if (r < 0) |
| 3074 |
return r; |
| 3075 |
|
| 3076 |
k = journal_file_entry_array_n_items(f, array); |
| 3077 |
m = m_original = MIN(k, n); |
| 3078 |
if (m <= 0) |
| 3079 |
return 0; |
| 3080 |
|
| 3081 |
left = 0; |
| 3082 |
right = m - 1; |
| 3083 |
|
| 3084 |
if (direction == DIRECTION_UP && left < right) { |
| 3085 |
/* If we're going upwards, the last entry of the previous array may pass the test, |
| 3086 |
* and the first entry of the current array may not pass. In that case, the last |
| 3087 |
* entry of the previous array must be returned. Hence, we need to test the first |
| 3088 |
* entry of the current array. */ |
| 3089 |
r = generic_array_bisect_step(f, array, 0, needle, test_object, direction, &m, &left, &right); |
| 3090 |
if (r < 0) |
| 3091 |
return r; |
| 3092 |
if (r == TEST_GOTO_PREVIOUS) |
| 3093 |
goto previous; |
| 3094 |
} |
| 3095 |
|
| 3096 |
/* Test the last entry of this array, to determine if we should go to the next array. */ |
| 3097 |
r = generic_array_bisect_step(f, array, right, needle, test_object, direction, &m, &left, &right); |
| 3098 |
if (r < 0) |
| 3099 |
return r; |
| 3100 |
if (r == TEST_GOTO_PREVIOUS) |
| 3101 |
goto previous; |
| 3102 |
|
| 3103 |
/* The expected entry should be in this array, (or the last entry of the previous array). */ |
| 3104 |
if (r == TEST_RIGHT) { |
| 3105 |
|
| 3106 |
/* If we cached the last index we looked at, let's try to not to jump too wildly |
| 3107 |
* around and see if we can limit the range to look at early to the immediate |
| 3108 |
* neighbors of the last index we looked at. */ |
| 3109 |
|
| 3110 |
if (last_index > 0 && left < last_index - 1 && last_index - 1 < right) { |
| 3111 |
r = generic_array_bisect_step(f, array, last_index - 1, needle, test_object, direction, &m, &left, &right); |
| 3112 |
if (r < 0) |
| 3113 |
return r; |
| 3114 |
if (r == TEST_GOTO_PREVIOUS) |
| 3115 |
goto previous; |
| 3116 |
} |
| 3117 |
|
| 3118 |
if (last_index < UINT64_MAX && left < last_index + 1 && last_index + 1 < right) { |
| 3119 |
r = generic_array_bisect_step(f, array, last_index + 1, needle, test_object, direction, &m, &left, &right); |
| 3120 |
if (r < 0) |
| 3121 |
return r; |
| 3122 |
if (r == TEST_GOTO_PREVIOUS) |
| 3123 |
goto previous; |
| 3124 |
} |
| 3125 |
|
| 3126 |
for (;;) { |
| 3127 |
if (left == right) { |
| 3128 |
/* We found one or more corrupted entries in generic_array_bisect_step(). |
| 3129 |
* In that case, the entry pointed by 'right' may not be tested. |
| 3130 |
* |
| 3131 |
* When we are going to downwards, the entry object pointed by 'left' |
| 3132 |
* has not been tested yet, Hence, even if left == right, we still |
| 3133 |
* have to check the final entry to see if it actually matches. |
| 3134 |
* |
| 3135 |
* On the other hand, when we are going to upwards, the entry pointed |
| 3136 |
* by 'left' is always tested, So, it is not necessary to test the |
| 3137 |
* final entry again. */ |
| 3138 |
if (m != m_original && direction == DIRECTION_DOWN) { |
| 3139 |
r = generic_array_bisect_step(f, array, left, needle, test_object, direction, &m, &left, &right); |
| 3140 |
if (r < 0) |
| 3141 |
return r; |
| 3142 |
if (IN_SET(r, TEST_GOTO_PREVIOUS, TEST_GOTO_NEXT)) |
| 3143 |
return 0; /* The entry does not pass the test, or is corrupted */ |
| 3144 |
|
| 3145 |
assert(TEST_RIGHT); |
| 3146 |
assert(left == right); |
| 3147 |
} |
| 3148 |
|
| 3149 |
i = left; |
| 3150 |
goto found; |
| 3151 |
} |
| 3152 |
|
| 3153 |
assert(left < right); |
| 3154 |
i = (left + right + (direction == DIRECTION_UP)) / 2; |
| 3155 |
|
| 3156 |
r = generic_array_bisect_step(f, array, i, needle, test_object, direction, &m, &left, &right); |
| 3157 |
if (r < 0) |
| 3158 |
return r; |
| 3159 |
if (r == TEST_GOTO_PREVIOUS) |
| 3160 |
goto previous; |
| 3161 |
if (r == TEST_GOTO_NEXT) |
| 3162 |
return 0; /* Found a corrupt entry, and the array was cut short. */ |
| 3163 |
} |
| 3164 |
} |
| 3165 |
|
| 3166 |
/* Not found in this array (or the last entry of this array should be returned), go to the next array. */ |
| 3167 |
assert(r == (direction == DIRECTION_DOWN ? TEST_GOTO_NEXT : TEST_LEFT)); |
| 3168 |
|
| 3169 |
if (k >= n) { |
| 3170 |
if (direction == DIRECTION_UP) { |
| 3171 |
assert(n > 0); |
| 3172 |
i = n - 1; |
| 3173 |
goto found; |
| 3174 |
} |
| 3175 |
|
| 3176 |
return 0; |
| 3177 |
} |
| 3178 |
|
| 3179 |
n -= k; |
| 3180 |
t += k; |
| 3181 |
last_index = UINT64_MAX; |
| 3182 |
a = le64toh(array->entry_array.next_entry_array_offset); |
| 3183 |
} |
| 3184 |
|
| 3185 |
return 0; |
| 3186 |
|
| 3187 |
previous: |
| 3188 |
/* Not found in the current array, return the last entry of the previous array. */ |
| 3189 |
assert(r == TEST_GOTO_PREVIOUS); |
| 3190 |
|
| 3191 |
/* The current array is the first in the chain. no previous array. */ |
| 3192 |
if (t == 0) |
| 3193 |
return 0; |
| 3194 |
|
| 3195 |
/* When we are going downwards, there is no matching entries in the previous array. */ |
| 3196 |
if (direction == DIRECTION_DOWN) |
| 3197 |
return 0; |
| 3198 |
|
| 3199 |
/* Get the last entry of the previous array. */ |
| 3200 |
r = bump_entry_array(f, NULL, a, first, DIRECTION_UP, &a); |
| 3201 |
if (r <= 0) |
| 3202 |
return r; |
| 3203 |
|
| 3204 |
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &array); |
| 3205 |
if (r < 0) |
| 3206 |
return r; |
| 3207 |
|
| 3208 |
p = journal_file_entry_array_n_items(f, array); |
| 3209 |
if (p == 0 || t < p) |
| 3210 |
return -EBADMSG; |
| 3211 |
|
| 3212 |
t -= p; |
| 3213 |
i = p - 1; |
| 3214 |
|
| 3215 |
found: |
| 3216 |
p = journal_file_entry_array_item(f, array, 0); |
| 3217 |
if (p <= 0) |
| 3218 |
return -EBADMSG; |
| 3219 |
|
| 3220 |
/* Let's cache this item for the next invocation */ |
| 3221 |
chain_cache_put(f, ci, first, a, p, t, i); |
| 3222 |
|
| 3223 |
p = journal_file_entry_array_item(f, array, i); |
| 3224 |
if (p == 0) |
| 3225 |
return -EBADMSG; |
| 3226 |
|
| 3227 |
if (ret_object) { |
| 3228 |
r = journal_file_move_to_object(f, OBJECT_ENTRY, p, ret_object); |
| 3229 |
if (r < 0) |
| 3230 |
return r; |
| 3231 |
} |
| 3232 |
|
| 3233 |
if (ret_offset) |
| 3234 |
*ret_offset = p; |
| 3235 |
|
| 3236 |
if (ret_idx) |
| 3237 |
*ret_idx = t + i; |
| 3238 |
|
| 3239 |
return 1; |
| 3240 |
} |
| 3241 |
|
| 3242 |
static int generic_array_bisect_for_data( |
| 3243 |
JournalFile *f, |
| 3244 |
Object *d, |
| 3245 |
uint64_t needle, |
| 3246 |
int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle), |
| 3247 |
direction_t direction, |
| 3248 |
Object **ret_object, |
| 3249 |
uint64_t *ret_offset) { |
| 3250 |
|
| 3251 |
uint64_t extra, first, n; |
| 3252 |
int r; |
| 3253 |
|
| 3254 |
assert(f); |
| 3255 |
assert(d); |
| 3256 |
assert(test_object); |
| 3257 |
|
| 3258 |
if (d->object.type != OBJECT_DATA) |
| 3259 |
return -EBADMSG; |
| 3260 |
|
| 3261 |
n = le64toh(d->data.n_entries); |
| 3262 |
if (n <= 0) |
| 3263 |
return 0; |
| 3264 |
n--; /* n_entries is the number of entries linked to the data object, including the 'extra' entry. */ |
| 3265 |
|
| 3266 |
extra = le64toh(d->data.entry_offset); |
| 3267 |
first = le64toh(d->data.entry_array_offset); |
| 3268 |
|
| 3269 |
/* This bisects the array in object 'first', but first checks an extra. */ |
| 3270 |
r = test_object(f, extra, needle); |
| 3271 |
if (r < 0) |
| 3272 |
return r; |
| 3273 |
|
| 3274 |
if (direction == DIRECTION_DOWN) { |
| 3275 |
/* If we are going downwards, then we need to return the first object that passes the test. |
| 3276 |
* When there is no object that passes the test, we need to return the first object that |
| 3277 |
* test_object() returns TEST_RIGHT for. */ |
| 3278 |
if (IN_SET(r, |
| 3279 |
TEST_FOUND, /* The 'extra' object passes the test. Hence, this is the first |
| 3280 |
* object that passes the test. */ |
| 3281 |
TEST_RIGHT)) /* The 'extra' object is the first object that test_object() returns |
| 3282 |
* TEST_RIGHT for, and no object exists even in the chained arrays |
| 3283 |
* that passes the test. */ |
| 3284 |
goto use_extra; /* The 'extra' object is exactly the one we are looking for. It is |
| 3285 |
* not necessary to bisect the chained arrays. */ |
| 3286 |
|
| 3287 |
/* Otherwise, the 'extra' object is not the one we are looking for. Search in the arrays. */ |
| 3288 |
|
| 3289 |
} else { |
| 3290 |
/* If we are going upwards, then we need to return the last object that passes the test. |
| 3291 |
* When there is no object that passes the test, we need to return the last object that |
| 3292 |
* test_object() returns TEST_LEFT for. */ |
| 3293 |
if (r == TEST_RIGHT) |
| 3294 |
return 0; /* Not only the 'extra' object, but also all objects in the chained arrays |
| 3295 |
* will never get TEST_FOUND or TEST_LEFT. The object we are looking for |
| 3296 |
* does not exist. */ |
| 3297 |
|
| 3298 |
/* Even if the 'extra' object passes the test, there may be multiple objects in the arrays |
| 3299 |
* that also pass the test. Hence, we need to bisect the arrays for finding the last matching |
| 3300 |
* object. */ |
| 3301 |
} |
| 3302 |
|
| 3303 |
r = generic_array_bisect(f, first, n, needle, test_object, direction, ret_object, ret_offset, NULL); |
| 3304 |
if (r != 0) |
| 3305 |
return r; /* When > 0, the found object is the first (or last, when DIRECTION_UP) object. |
| 3306 |
* Hence, return the found object now. */ |
| 3307 |
|
| 3308 |
/* No matching object found in the chained arrays. |
| 3309 |
* DIRECTION_DOWN : the 'extra' object neither matches the condition. There is no matching object. |
| 3310 |
* DIRECTION_UP : the 'extra' object matches the condition. So, return it. */ |
| 3311 |
if (direction == DIRECTION_DOWN) |
| 3312 |
return 0; |
| 3313 |
|
| 3314 |
use_extra: |
| 3315 |
if (ret_object) { |
| 3316 |
r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, ret_object); |
| 3317 |
if (r < 0) |
| 3318 |
return r; |
| 3319 |
} |
| 3320 |
|
| 3321 |
if (ret_offset) |
| 3322 |
*ret_offset = extra; |
| 3323 |
|
| 3324 |
return 1; |
| 3325 |
} |
| 3326 |
|
| 3327 |
static int test_object_offset(JournalFile *f, uint64_t p, uint64_t needle) { |
| 3328 |
assert(f); |
| 3329 |
|
| 3330 |
if (p <= 0) |
| 3331 |
return -EBADMSG; |
| 3332 |
|
| 3333 |
if (p == needle) |
| 3334 |
return TEST_FOUND; |
| 3335 |
else if (p < needle) |
| 3336 |
return TEST_LEFT; |
| 3337 |
else |
| 3338 |
return TEST_RIGHT; |
| 3339 |
} |
| 3340 |
|
| 3341 |
int journal_file_move_to_entry_by_offset( |
| 3342 |
JournalFile *f, |
| 3343 |
uint64_t p, |
| 3344 |
direction_t direction, |
| 3345 |
Object **ret_object, |
| 3346 |
uint64_t *ret_offset) { |
| 3347 |
|
| 3348 |
assert(f); |
| 3349 |
assert(f->header); |
| 3350 |
|
| 3351 |
return generic_array_bisect( |
| 3352 |
f, |
| 3353 |
le64toh(f->header->entry_array_offset), |
| 3354 |
le64toh(f->header->n_entries), |
| 3355 |
p, |
| 3356 |
test_object_offset, |
| 3357 |
direction, |
| 3358 |
ret_object, ret_offset, NULL); |
| 3359 |
} |
| 3360 |
|
| 3361 |
static int test_object_seqnum(JournalFile *f, uint64_t p, uint64_t needle) { |
| 3362 |
uint64_t sq; |
| 3363 |
Object *o; |
| 3364 |
int r; |
| 3365 |
|
| 3366 |
assert(f); |
| 3367 |
|
| 3368 |
r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); |
| 3369 |
if (r < 0) |
| 3370 |
return r; |
| 3371 |
|
| 3372 |
sq = le64toh(READ_NOW(o->entry.seqnum)); |
| 3373 |
if (sq == needle) |
| 3374 |
return TEST_FOUND; |
| 3375 |
else if (sq < needle) |
| 3376 |
return TEST_LEFT; |
| 3377 |
else |
| 3378 |
return TEST_RIGHT; |
| 3379 |
} |
| 3380 |
|
| 3381 |
int journal_file_move_to_entry_by_seqnum( |
| 3382 |
JournalFile *f, |
| 3383 |
uint64_t seqnum, |
| 3384 |
direction_t direction, |
| 3385 |
Object **ret_object, |
| 3386 |
uint64_t *ret_offset) { |
| 3387 |
|
| 3388 |
assert(f); |
| 3389 |
assert(f->header); |
| 3390 |
|
| 3391 |
return generic_array_bisect( |
| 3392 |
f, |
| 3393 |
le64toh(f->header->entry_array_offset), |
| 3394 |
le64toh(f->header->n_entries), |
| 3395 |
seqnum, |
| 3396 |
test_object_seqnum, |
| 3397 |
direction, |
| 3398 |
ret_object, ret_offset, NULL); |
| 3399 |
} |
| 3400 |
|
| 3401 |
static int test_object_realtime(JournalFile *f, uint64_t p, uint64_t needle) { |
| 3402 |
Object *o; |
| 3403 |
uint64_t rt; |
| 3404 |
int r; |
| 3405 |
|
| 3406 |
assert(f); |
| 3407 |
|
| 3408 |
r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); |
| 3409 |
if (r < 0) |
| 3410 |
return r; |
| 3411 |
|
| 3412 |
rt = le64toh(READ_NOW(o->entry.realtime)); |
| 3413 |
if (rt == needle) |
| 3414 |
return TEST_FOUND; |
| 3415 |
else if (rt < needle) |
| 3416 |
return TEST_LEFT; |
| 3417 |
else |
| 3418 |
return TEST_RIGHT; |
| 3419 |
} |
| 3420 |
|
| 3421 |
int journal_file_move_to_entry_by_realtime( |
| 3422 |
JournalFile *f, |
| 3423 |
uint64_t realtime, |
| 3424 |
direction_t direction, |
| 3425 |
Object **ret_object, |
| 3426 |
uint64_t *ret_offset) { |
| 3427 |
|
| 3428 |
assert(f); |
| 3429 |
assert(f->header); |
| 3430 |
|
| 3431 |
return generic_array_bisect( |
| 3432 |
f, |
| 3433 |
le64toh(f->header->entry_array_offset), |
| 3434 |
le64toh(f->header->n_entries), |
| 3435 |
realtime, |
| 3436 |
test_object_realtime, |
| 3437 |
direction, |
| 3438 |
ret_object, ret_offset, NULL); |
| 3439 |
} |
| 3440 |
|
| 3441 |
static int test_object_monotonic(JournalFile *f, uint64_t p, uint64_t needle) { |
| 3442 |
Object *o; |
| 3443 |
uint64_t m; |
| 3444 |
int r; |
| 3445 |
|
| 3446 |
assert(f); |
| 3447 |
|
| 3448 |
r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); |
| 3449 |
if (r < 0) |
| 3450 |
return r; |
| 3451 |
|
| 3452 |
m = le64toh(READ_NOW(o->entry.monotonic)); |
| 3453 |
if (m == needle) |
| 3454 |
return TEST_FOUND; |
| 3455 |
else if (m < needle) |
| 3456 |
return TEST_LEFT; |
| 3457 |
else |
| 3458 |
return TEST_RIGHT; |
| 3459 |
} |
| 3460 |
|
| 3461 |
static int find_data_object_by_boot_id( |
| 3462 |
JournalFile *f, |
| 3463 |
sd_id128_t boot_id, |
| 3464 |
Object **ret_object, |
| 3465 |
uint64_t *ret_offset) { |
| 3466 |
|
| 3467 |
char t[STRLEN("_BOOT_ID=") + 32 + 1] = "_BOOT_ID="; |
| 3468 |
|
| 3469 |
assert(f); |
| 3470 |
|
| 3471 |
sd_id128_to_string(boot_id, t + 9); |
| 3472 |
return journal_file_find_data_object(f, t, sizeof(t) - 1, ret_object, ret_offset); |
| 3473 |
} |
| 3474 |
|
| 3475 |
int journal_file_move_to_entry_by_monotonic( |
| 3476 |
JournalFile *f, |
| 3477 |
sd_id128_t boot_id, |
| 3478 |
uint64_t monotonic, |
| 3479 |
direction_t direction, |
| 3480 |
Object **ret_object, |
| 3481 |
uint64_t *ret_offset) { |
| 3482 |
|
| 3483 |
Object *o; |
| 3484 |
int r; |
| 3485 |
|
| 3486 |
assert(f); |
| 3487 |
|
| 3488 |
r = find_data_object_by_boot_id(f, boot_id, &o, NULL); |
| 3489 |
if (r <= 0) |
| 3490 |
return r; |
| 3491 |
|
| 3492 |
return generic_array_bisect_for_data( |
| 3493 |
f, |
| 3494 |
o, |
| 3495 |
monotonic, |
| 3496 |
test_object_monotonic, |
| 3497 |
direction, |
| 3498 |
ret_object, ret_offset); |
| 3499 |
} |
| 3500 |
|
| 3501 |
void journal_file_reset_location(JournalFile *f) { |
| 3502 |
assert(f); |
| 3503 |
|
| 3504 |
f->location_type = LOCATION_HEAD; |
| 3505 |
f->current_offset = 0; |
| 3506 |
f->current_seqnum = 0; |
| 3507 |
f->current_realtime = 0; |
| 3508 |
f->current_monotonic = 0; |
| 3509 |
zero(f->current_boot_id); |
| 3510 |
f->current_xor_hash = 0; |
| 3511 |
|
| 3512 |
/* Also reset the previous reading direction. Otherwise, next_beyond_location() may wrongly handle we |
| 3513 |
* already hit EOF. See issue #29216. */ |
| 3514 |
f->last_direction = _DIRECTION_INVALID; |
| 3515 |
} |
| 3516 |
|
| 3517 |
void journal_file_save_location(JournalFile *f, Object *o, uint64_t offset) { |
| 3518 |
assert(f); |
| 3519 |
assert(o); |
| 3520 |
|
| 3521 |
f->location_type = LOCATION_SEEK; |
| 3522 |
f->current_offset = offset; |
| 3523 |
f->current_seqnum = le64toh(o->entry.seqnum); |
| 3524 |
f->current_realtime = le64toh(o->entry.realtime); |
| 3525 |
f->current_monotonic = le64toh(o->entry.monotonic); |
| 3526 |
f->current_boot_id = o->entry.boot_id; |
| 3527 |
f->current_xor_hash = le64toh(o->entry.xor_hash); |
| 3528 |
} |
| 3529 |
|
| 3530 |
static bool check_properly_ordered(uint64_t new_offset, uint64_t old_offset, direction_t direction) { |
| 3531 |
|
| 3532 |
/* Consider it an error if any of the two offsets is uninitialized */ |
| 3533 |
if (old_offset == 0 || new_offset == 0) |
| 3534 |
return false; |
| 3535 |
|
| 3536 |
/* If we go down, the new offset must be larger than the old one. */ |
| 3537 |
return direction == DIRECTION_DOWN ? |
| 3538 |
new_offset > old_offset : |
| 3539 |
new_offset < old_offset; |
| 3540 |
} |
| 3541 |
|
| 3542 |
int journal_file_next_entry( |
| 3543 |
JournalFile *f, |
| 3544 |
uint64_t p, |
| 3545 |
direction_t direction, |
| 3546 |
Object **ret_object, |
| 3547 |
uint64_t *ret_offset) { |
| 3548 |
|
| 3549 |
uint64_t i, n, q; |
| 3550 |
Object *o; |
| 3551 |
int r; |
| 3552 |
|
| 3553 |
assert(f); |
| 3554 |
assert(f->header); |
| 3555 |
|
| 3556 |
/* FIXME: fix return value assignment. */ |
| 3557 |
|
| 3558 |
n = le64toh(READ_NOW(f->header->n_entries)); |
| 3559 |
if (n <= 0) |
| 3560 |
return 0; |
| 3561 |
|
| 3562 |
/* When the input offset 'p' is zero, return the first (or last on DIRECTION_UP) entry. */ |
| 3563 |
if (p == 0) |
| 3564 |
return generic_array_get(f, |
| 3565 |
le64toh(f->header->entry_array_offset), |
| 3566 |
direction == DIRECTION_DOWN ? 0 : n - 1, |
| 3567 |
direction, |
| 3568 |
ret_object, ret_offset); |
| 3569 |
|
| 3570 |
/* Otherwise, first find the nearest entry object. */ |
| 3571 |
r = generic_array_bisect(f, |
| 3572 |
le64toh(f->header->entry_array_offset), |
| 3573 |
le64toh(f->header->n_entries), |
| 3574 |
p, |
| 3575 |
test_object_offset, |
| 3576 |
direction, |
| 3577 |
NULL, &q, &i); /* Here, do not read entry object, as the result object |
| 3578 |
* may not be the one we want, and it may be broken. */ |
| 3579 |
if (r <= 0) |
| 3580 |
return r; |
| 3581 |
|
| 3582 |
assert(direction == DIRECTION_DOWN ? p <= q : q <= p); |
| 3583 |
|
| 3584 |
/* If the input offset 'p' points to an entry object, generic_array_bisect() should provides |
| 3585 |
* the same offset, and the index needs to be shifted. Otherwise, use the found object as is, |
| 3586 |
* as it is the nearest entry object from the input offset 'p'. */ |
| 3587 |
|
| 3588 |
if (p == q) { |
| 3589 |
r = bump_array_index(&i, direction, n); |
| 3590 |
if (r <= 0) |
| 3591 |
return r; |
| 3592 |
} |
| 3593 |
|
| 3594 |
/* And jump to it */ |
| 3595 |
r = generic_array_get(f, le64toh(f->header->entry_array_offset), i, direction, ret_object ? &o : NULL, &q); |
| 3596 |
if (r <= 0) |
| 3597 |
return r; |
| 3598 |
|
| 3599 |
/* Ensure our array is properly ordered. */ |
| 3600 |
if (!check_properly_ordered(q, p, direction)) |
| 3601 |
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
| 3602 |
"%s: entry array not properly ordered at entry index %" PRIu64, |
| 3603 |
f->path, i); |
| 3604 |
|
| 3605 |
if (ret_object) |
| 3606 |
*ret_object = o; |
| 3607 |
if (ret_offset) |
| 3608 |
*ret_offset = q; |
| 3609 |
|
| 3610 |
return 1; |
| 3611 |
} |
| 3612 |
|
| 3613 |
int journal_file_move_to_entry_for_data( |
| 3614 |
JournalFile *f, |
| 3615 |
Object *d, |
| 3616 |
direction_t direction, |
| 3617 |
Object **ret_object, |
| 3618 |
uint64_t *ret_offset) { |
| 3619 |
|
| 3620 |
uint64_t extra, first, n; |
| 3621 |
int r = 0; |
| 3622 |
|
| 3623 |
assert(f); |
| 3624 |
assert(d); |
| 3625 |
assert(IN_SET(direction, DIRECTION_DOWN, DIRECTION_UP)); |
| 3626 |
|
| 3627 |
if (d->object.type != OBJECT_DATA) |
| 3628 |
return -EBADMSG; |
| 3629 |
|
| 3630 |
/* FIXME: fix return value assignment. */ |
| 3631 |
|
| 3632 |
/* This returns the first (when the direction is down, otherwise the last) entry linked to the |
| 3633 |
* specified data object. */ |
| 3634 |
|
| 3635 |
n = le64toh(d->data.n_entries); |
| 3636 |
if (n <= 0) |
| 3637 |
return 0; |
| 3638 |
n--; /* n_entries is the number of entries linked to the data object, including the 'extra' entry. */ |
| 3639 |
|
| 3640 |
extra = le64toh(d->data.entry_offset); |
| 3641 |
first = le64toh(d->data.entry_array_offset); |
| 3642 |
|
| 3643 |
if (direction == DIRECTION_DOWN && extra > 0) { |
| 3644 |
/* When we are going downwards, first try to read the extra entry. */ |
| 3645 |
r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, ret_object); |
| 3646 |
if (r >= 0) |
| 3647 |
goto use_extra; |
| 3648 |
if (!IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) |
| 3649 |
return r; |
| 3650 |
} |
| 3651 |
|
| 3652 |
if (n > 0) { |
| 3653 |
/* DIRECTION_DOWN : The extra entry is broken, falling back to the entries in the array. |
| 3654 |
* DIRECTION_UP : Try to find a valid entry in the array from the tail. */ |
| 3655 |
r = generic_array_get(f, |
| 3656 |
first, |
| 3657 |
direction == DIRECTION_DOWN ? 0 : n - 1, |
| 3658 |
direction, |
| 3659 |
ret_object, ret_offset); |
| 3660 |
if (!IN_SET(r, 0, -EADDRNOTAVAIL, -EBADMSG)) |
| 3661 |
return r; /* found or critical error. */ |
| 3662 |
} |
| 3663 |
|
| 3664 |
if (direction == DIRECTION_UP && extra > 0) { |
| 3665 |
/* No valid entry exists in the chained array, falling back to the extra entry. */ |
| 3666 |
r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, ret_object); |
| 3667 |
if (r >= 0) |
| 3668 |
goto use_extra; |
| 3669 |
} |
| 3670 |
|
| 3671 |
return r; |
| 3672 |
|
| 3673 |
use_extra: |
| 3674 |
if (ret_offset) |
| 3675 |
*ret_offset = extra; |
| 3676 |
|
| 3677 |
return 1; |
| 3678 |
} |
| 3679 |
|
| 3680 |
int journal_file_move_to_entry_by_offset_for_data( |
| 3681 |
JournalFile *f, |
| 3682 |
Object *d, |
| 3683 |
uint64_t p, |
| 3684 |
direction_t direction, |
| 3685 |
Object **ret, uint64_t *ret_offset) { |
| 3686 |
|
| 3687 |
assert(f); |
| 3688 |
assert(d); |
| 3689 |
|
| 3690 |
if (d->object.type != OBJECT_DATA) |
| 3691 |
return -EBADMSG; |
| 3692 |
|
| 3693 |
return generic_array_bisect_for_data( |
| 3694 |
f, |
| 3695 |
d, |
| 3696 |
p, |
| 3697 |
test_object_offset, |
| 3698 |
direction, |
| 3699 |
ret, ret_offset); |
| 3700 |
} |
| 3701 |
|
| 3702 |
int journal_file_move_to_entry_by_monotonic_for_data( |
| 3703 |
JournalFile *f, |
| 3704 |
Object *d, |
| 3705 |
sd_id128_t boot_id, |
| 3706 |
uint64_t monotonic, |
| 3707 |
direction_t direction, |
| 3708 |
Object **ret_object, |
| 3709 |
uint64_t *ret_offset) { |
| 3710 |
|
| 3711 |
Object *o, *entry; |
| 3712 |
uint64_t z; |
| 3713 |
int r; |
| 3714 |
|
| 3715 |
assert(f); |
| 3716 |
assert(d); |
| 3717 |
|
| 3718 |
if (d->object.type != OBJECT_DATA) |
| 3719 |
return -EBADMSG; |
| 3720 |
|
| 3721 |
/* First, pin the given data object, before reading the _BOOT_ID= data object below. */ |
| 3722 |
r = journal_file_pin_object(f, d); |
| 3723 |
if (r < 0) |
| 3724 |
return r; |
| 3725 |
|
| 3726 |
/* Then, read a data object for _BOOT_ID= and seek by time. */ |
| 3727 |
r = find_data_object_by_boot_id(f, boot_id, &o, NULL); |
| 3728 |
if (r <= 0) |
| 3729 |
return r; |
| 3730 |
|
| 3731 |
r = generic_array_bisect_for_data(f, |
| 3732 |
o, |
| 3733 |
monotonic, |
| 3734 |
test_object_monotonic, |
| 3735 |
direction, |
| 3736 |
NULL, &z); |
| 3737 |
if (r <= 0) |
| 3738 |
return r; |
| 3739 |
|
| 3740 |
/* And now, continue seeking until we find an entry that exists in both bisection arrays. */ |
| 3741 |
for (;;) { |
| 3742 |
uint64_t p; |
| 3743 |
|
| 3744 |
/* The journal entry found by the above bisect_plus_one() may not have the specified data, |
| 3745 |
* that is, it may not be linked in the data object. So, we need to check that. */ |
| 3746 |
|
| 3747 |
r = journal_file_move_to_entry_by_offset_for_data( |
| 3748 |
f, d, z, direction, ret_object ? &entry : NULL, &p); |
| 3749 |
if (r <= 0) |
| 3750 |
return r; |
| 3751 |
if (p == z) |
| 3752 |
break; /* The journal entry has the specified data. Yay! */ |
| 3753 |
|
| 3754 |
/* If the entry does not have the data, then move to the next (or previous, depends on the |
| 3755 |
* 'direction') entry linked to the data object. But, the next entry may be in another boot. |
| 3756 |
* So, we need to check that the entry has the matching boot ID. */ |
| 3757 |
|
| 3758 |
r = journal_file_move_to_entry_by_offset_for_data( |
| 3759 |
f, o, p, direction, ret_object ? &entry : NULL, &z); |
| 3760 |
if (r <= 0) |
| 3761 |
return r; |
| 3762 |
if (p == z) |
| 3763 |
break; /* The journal entry has the specified boot ID. Yay! */ |
| 3764 |
|
| 3765 |
/* If not, let's try to the next entry... */ |
| 3766 |
} |
| 3767 |
|
| 3768 |
if (ret_object) |
| 3769 |
*ret_object = entry; |
| 3770 |
if (ret_offset) |
| 3771 |
*ret_offset = z; |
| 3772 |
return 1; |
| 3773 |
} |
| 3774 |
|
| 3775 |
int journal_file_move_to_entry_by_seqnum_for_data( |
| 3776 |
JournalFile *f, |
| 3777 |
Object *d, |
| 3778 |
uint64_t seqnum, |
| 3779 |
direction_t direction, |
| 3780 |
Object **ret_object, |
| 3781 |
uint64_t *ret_offset) { |
| 3782 |
|
| 3783 |
assert(f); |
| 3784 |
assert(d); |
| 3785 |
|
| 3786 |
if (d->object.type != OBJECT_DATA) |
| 3787 |
return -EBADMSG; |
| 3788 |
|
| 3789 |
return generic_array_bisect_for_data( |
| 3790 |
f, |
| 3791 |
d, |
| 3792 |
seqnum, |
| 3793 |
test_object_seqnum, |
| 3794 |
direction, |
| 3795 |
ret_object, ret_offset); |
| 3796 |
} |
| 3797 |
|
| 3798 |
int journal_file_move_to_entry_by_realtime_for_data( |
| 3799 |
JournalFile *f, |
| 3800 |
Object *d, |
| 3801 |
uint64_t realtime, |
| 3802 |
direction_t direction, |
| 3803 |
Object **ret, uint64_t *ret_offset) { |
| 3804 |
|
| 3805 |
assert(f); |
| 3806 |
assert(d); |
| 3807 |
|
| 3808 |
if (d->object.type != OBJECT_DATA) |
| 3809 |
return -EBADMSG; |
| 3810 |
|
| 3811 |
return generic_array_bisect_for_data( |
| 3812 |
f, |
| 3813 |
d, |
| 3814 |
realtime, |
| 3815 |
test_object_realtime, |
| 3816 |
direction, |
| 3817 |
ret, ret_offset); |
| 3818 |
} |
| 3819 |
|
| 3820 |
void journal_file_dump(JournalFile *f) { |
| 3821 |
Object *o; |
| 3822 |
uint64_t p; |
| 3823 |
int r; |
| 3824 |
|
| 3825 |
assert(f); |
| 3826 |
assert(f->header); |
| 3827 |
|
| 3828 |
journal_file_print_header(f); |
| 3829 |
|
| 3830 |
p = le64toh(READ_NOW(f->header->header_size)); |
| 3831 |
while (p != 0) { |
| 3832 |
const char *s; |
| 3833 |
Compression c; |
| 3834 |
|
| 3835 |
r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o); |
| 3836 |
if (r < 0) |
| 3837 |
goto fail; |
| 3838 |
|
| 3839 |
s = journal_object_type_to_string(o->object.type); |
| 3840 |
|
| 3841 |
switch (o->object.type) { |
| 3842 |
|
| 3843 |
case OBJECT_ENTRY: |
| 3844 |
assert(s); |
| 3845 |
|
| 3846 |
log_info("Type: %s seqnum=%"PRIu64" monotonic=%"PRIu64" realtime=%"PRIu64"\n", |
| 3847 |
s, |
| 3848 |
le64toh(o->entry.seqnum), |
| 3849 |
le64toh(o->entry.monotonic), |
| 3850 |
le64toh(o->entry.realtime)); |
| 3851 |
break; |
| 3852 |
|
| 3853 |
case OBJECT_TAG: |
| 3854 |
assert(s); |
| 3855 |
|
| 3856 |
log_info("Type: %s seqnum=%"PRIu64" epoch=%"PRIu64"\n", |
| 3857 |
s, |
| 3858 |
le64toh(o->tag.seqnum), |
| 3859 |
le64toh(o->tag.epoch)); |
| 3860 |
break; |
| 3861 |
|
| 3862 |
default: |
| 3863 |
if (s) |
| 3864 |
log_info("Type: %s \n", s); |
| 3865 |
else |
| 3866 |
log_info("Type: unknown (%i)", o->object.type); |
| 3867 |
} |
| 3868 |
|
| 3869 |
c = COMPRESSION_FROM_OBJECT(o); |
| 3870 |
if (c > COMPRESSION_NONE) |
| 3871 |
log_info("Flags: %s\n", |
| 3872 |
compression_to_string(c)); |
| 3873 |
|
| 3874 |
if (p == le64toh(f->header->tail_object_offset)) |
| 3875 |
p = 0; |
| 3876 |
else |
| 3877 |
p += ALIGN64(le64toh(o->object.size)); |
| 3878 |
} |
| 3879 |
|
| 3880 |
return; |
| 3881 |
fail: |
| 3882 |
log_error("File corrupt"); |
| 3883 |
} |
| 3884 |
|
| 3885 |
/* Note: the lifetime of the compound literal is the immediately surrounding block. */ |
| 3886 |
#define FORMAT_TIMESTAMP_SAFE(t) (FORMAT_TIMESTAMP(t) ?: " --- ") |
| 3887 |
|
| 3888 |
void journal_file_print_header(JournalFile *f) { |
| 3889 |
struct stat st; |
| 3890 |
|
| 3891 |
assert(f); |
| 3892 |
assert(f->header); |
| 3893 |
|
| 3894 |
printf("File path: %s\n" |
| 3895 |
"File ID: %s\n" |
| 3896 |
"Machine ID: %s\n" |
| 3897 |
"Boot ID: %s\n" |
| 3898 |
"Sequential number ID: %s\n" |
| 3899 |
"State: %s\n" |
| 3900 |
"Compatible flags:%s%s%s%s\n" |
| 3901 |
"Incompatible flags:%s%s%s%s%s%s\n" |
| 3902 |
"Header size: %"PRIu64"\n" |
| 3903 |
"Arena size: %"PRIu64"\n" |
| 3904 |
"Data hash table size: %"PRIu64"\n" |
| 3905 |
"Field hash table size: %"PRIu64"\n" |
| 3906 |
"Rotate suggested: %s\n" |
| 3907 |
"Head sequential number: %"PRIu64" (%"PRIx64")\n" |
| 3908 |
"Tail sequential number: %"PRIu64" (%"PRIx64")\n" |
| 3909 |
"Head realtime timestamp: %s (%"PRIx64")\n" |
| 3910 |
"Tail realtime timestamp: %s (%"PRIx64")\n" |
| 3911 |
"Tail monotonic timestamp: %s (%"PRIx64")\n" |
| 3912 |
"Objects: %"PRIu64"\n" |
| 3913 |
"Entry objects: %"PRIu64"\n", |
| 3914 |
f->path, |
| 3915 |
SD_ID128_TO_STRING(f->header->file_id), |
| 3916 |
SD_ID128_TO_STRING(f->header->machine_id), |
| 3917 |
SD_ID128_TO_STRING(f->header->tail_entry_boot_id), |
| 3918 |
SD_ID128_TO_STRING(f->header->seqnum_id), |
| 3919 |
f->header->state == STATE_OFFLINE ? "OFFLINE" : |
| 3920 |
f->header->state == STATE_ONLINE ? "ONLINE" : |
| 3921 |
f->header->state == STATE_ARCHIVED ? "ARCHIVED" : "UNKNOWN", |
| 3922 |
JOURNAL_HEADER_SEALED(f->header) ? " SEALED" : "", |
| 3923 |
JOURNAL_HEADER_SEALED_CONTINUOUS(f->header) ? " SEALED_CONTINUOUS" : "", |
| 3924 |
JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f->header) ? " TAIL_ENTRY_BOOT_ID" : "", |
| 3925 |
(le32toh(f->header->compatible_flags) & ~HEADER_COMPATIBLE_ANY) ? " ???" : "", |
| 3926 |
JOURNAL_HEADER_COMPRESSED_XZ(f->header) ? " COMPRESSED-XZ" : "", |
| 3927 |
JOURNAL_HEADER_COMPRESSED_LZ4(f->header) ? " COMPRESSED-LZ4" : "", |
| 3928 |
JOURNAL_HEADER_COMPRESSED_ZSTD(f->header) ? " COMPRESSED-ZSTD" : "", |
| 3929 |
JOURNAL_HEADER_KEYED_HASH(f->header) ? " KEYED-HASH" : "", |
| 3930 |
JOURNAL_HEADER_COMPACT(f->header) ? " COMPACT" : "", |
| 3931 |
(le32toh(f->header->incompatible_flags) & ~HEADER_INCOMPATIBLE_ANY) ? " ???" : "", |
| 3932 |
le64toh(f->header->header_size), |
| 3933 |
le64toh(f->header->arena_size), |
| 3934 |
le64toh(f->header->data_hash_table_size) / sizeof(HashItem), |
| 3935 |
le64toh(f->header->field_hash_table_size) / sizeof(HashItem), |
| 3936 |
yes_no(journal_file_rotate_suggested(f, 0, LOG_DEBUG)), |
| 3937 |
le64toh(f->header->head_entry_seqnum), le64toh(f->header->head_entry_seqnum), |
| 3938 |
le64toh(f->header->tail_entry_seqnum), le64toh(f->header->tail_entry_seqnum), |
| 3939 |
FORMAT_TIMESTAMP_SAFE(le64toh(f->header->head_entry_realtime)), le64toh(f->header->head_entry_realtime), |
| 3940 |
FORMAT_TIMESTAMP_SAFE(le64toh(f->header->tail_entry_realtime)), le64toh(f->header->tail_entry_realtime), |
| 3941 |
FORMAT_TIMESPAN(le64toh(f->header->tail_entry_monotonic), USEC_PER_MSEC), le64toh(f->header->tail_entry_monotonic), |
| 3942 |
le64toh(f->header->n_objects), |
| 3943 |
le64toh(f->header->n_entries)); |
| 3944 |
|
| 3945 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_data)) { |
| 3946 |
size_t n_data_items = le64toh(f->header->data_hash_table_size) / sizeof(HashItem); |
| 3947 |
printf("Data objects: %"PRIu64"\n" |
| 3948 |
"Data hash table fill: %.1f%%\n", |
| 3949 |
le64toh(f->header->n_data), |
| 3950 |
100.0 * (double) le64toh(f->header->n_data) / (double) n_data_items); |
| 3951 |
} |
| 3952 |
|
| 3953 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_fields)) { |
| 3954 |
size_t n_field_items = le64toh(f->header->field_hash_table_size) / sizeof(HashItem); |
| 3955 |
printf("Field objects: %"PRIu64"\n" |
| 3956 |
"Field hash table fill: %.1f%%\n", |
| 3957 |
le64toh(f->header->n_fields), |
| 3958 |
100.0 * (double) le64toh(f->header->n_fields) / (double) n_field_items); |
| 3959 |
} |
| 3960 |
|
| 3961 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_tags)) |
| 3962 |
printf("Tag objects: %"PRIu64"\n", |
| 3963 |
le64toh(f->header->n_tags)); |
| 3964 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays)) |
| 3965 |
printf("Entry array objects: %"PRIu64"\n", |
| 3966 |
le64toh(f->header->n_entry_arrays)); |
| 3967 |
|
| 3968 |
if (JOURNAL_HEADER_CONTAINS(f->header, field_hash_chain_depth)) |
| 3969 |
printf("Deepest field hash chain: %" PRIu64"\n", |
| 3970 |
f->header->field_hash_chain_depth); |
| 3971 |
|
| 3972 |
if (JOURNAL_HEADER_CONTAINS(f->header, data_hash_chain_depth)) |
| 3973 |
printf("Deepest data hash chain: %" PRIu64"\n", |
| 3974 |
f->header->data_hash_chain_depth); |
| 3975 |
|
| 3976 |
if (fstat(f->fd, &st) >= 0) |
| 3977 |
printf("Disk usage: %s\n", FORMAT_BYTES((uint64_t) st.st_blocks * 512ULL)); |
| 3978 |
} |
| 3979 |
|
| 3980 |
static int journal_file_warn_btrfs(JournalFile *f) { |
| 3981 |
unsigned attrs; |
| 3982 |
int r; |
| 3983 |
|
| 3984 |
assert(f); |
| 3985 |
|
| 3986 |
/* Before we write anything, check if the COW logic is turned |
| 3987 |
* off on btrfs. Given our write pattern that is quite |
| 3988 |
* unfriendly to COW file systems this should greatly improve |
| 3989 |
* performance on COW file systems, such as btrfs, at the |
| 3990 |
* expense of data integrity features (which shouldn't be too |
| 3991 |
* bad, given that we do our own checksumming). */ |
| 3992 |
|
| 3993 |
r = fd_is_fs_type(f->fd, BTRFS_SUPER_MAGIC); |
| 3994 |
if (r < 0) |
| 3995 |
return log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, "Failed to determine if journal is on btrfs: %m"); |
| 3996 |
if (r == 0) |
| 3997 |
return 0; |
| 3998 |
|
| 3999 |
r = read_attr_fd(f->fd, &attrs); |
| 4000 |
if (r < 0) |
| 4001 |
return log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, "Failed to read file attributes: %m"); |
| 4002 |
|
| 4003 |
if (attrs & FS_NOCOW_FL) { |
| 4004 |
log_debug("Detected btrfs file system with copy-on-write disabled, all is good."); |
| 4005 |
return 0; |
| 4006 |
} |
| 4007 |
|
| 4008 |
log_ratelimit_notice(JOURNAL_LOG_RATELIMIT, |
| 4009 |
"Creating journal file %s on a btrfs file system, and copy-on-write is enabled. " |
| 4010 |
"This is likely to slow down journal access substantially, please consider turning " |
| 4011 |
"off the copy-on-write file attribute on the journal directory, using chattr +C.", |
| 4012 |
f->path); |
| 4013 |
|
| 4014 |
return 1; |
| 4015 |
} |
| 4016 |
|
| 4017 |
static void journal_default_metrics(JournalMetrics *m, int fd, bool compact) { |
| 4018 |
struct statvfs ss; |
| 4019 |
uint64_t fs_size = 0; |
| 4020 |
|
| 4021 |
assert(m); |
| 4022 |
assert(fd >= 0); |
| 4023 |
|
| 4024 |
if (fstatvfs(fd, &ss) >= 0) |
| 4025 |
fs_size = u64_multiply_safe(ss.f_frsize, ss.f_blocks); |
| 4026 |
else |
| 4027 |
log_debug_errno(errno, "Failed to determine disk size: %m"); |
| 4028 |
|
| 4029 |
if (m->max_use == UINT64_MAX) { |
| 4030 |
|
| 4031 |
if (fs_size > 0) |
| 4032 |
m->max_use = CLAMP(PAGE_ALIGN_U64(fs_size / 10), /* 10% of file system size */ |
| 4033 |
MAX_USE_LOWER, MAX_USE_UPPER); |
| 4034 |
else |
| 4035 |
m->max_use = MAX_USE_LOWER; |
| 4036 |
} else { |
| 4037 |
m->max_use = PAGE_ALIGN_U64(m->max_use); |
| 4038 |
|
| 4039 |
if (m->max_use != 0 && m->max_use < JOURNAL_FILE_SIZE_MIN*2) |
| 4040 |
m->max_use = JOURNAL_FILE_SIZE_MIN*2; |
| 4041 |
} |
| 4042 |
|
| 4043 |
if (m->min_use == UINT64_MAX) { |
| 4044 |
if (fs_size > 0) |
| 4045 |
m->min_use = CLAMP(PAGE_ALIGN_U64(fs_size / 50), /* 2% of file system size */ |
| 4046 |
MIN_USE_LOW, MIN_USE_HIGH); |
| 4047 |
else |
| 4048 |
m->min_use = MIN_USE_LOW; |
| 4049 |
} |
| 4050 |
|
| 4051 |
if (m->min_use > m->max_use) |
| 4052 |
m->min_use = m->max_use; |
| 4053 |
|
| 4054 |
if (m->max_size == UINT64_MAX) |
| 4055 |
m->max_size = MIN(PAGE_ALIGN_U64(m->max_use / 8), /* 8 chunks */ |
| 4056 |
MAX_SIZE_UPPER); |
| 4057 |
else |
| 4058 |
m->max_size = PAGE_ALIGN_U64(m->max_size); |
| 4059 |
|
| 4060 |
if (compact && m->max_size > JOURNAL_COMPACT_SIZE_MAX) |
| 4061 |
m->max_size = JOURNAL_COMPACT_SIZE_MAX; |
| 4062 |
|
| 4063 |
if (m->max_size != 0) { |
| 4064 |
if (m->max_size < JOURNAL_FILE_SIZE_MIN) |
| 4065 |
m->max_size = JOURNAL_FILE_SIZE_MIN; |
| 4066 |
|
| 4067 |
/* Silence static analyzers */ |
| 4068 |
assert(m->max_size <= UINT64_MAX / 2); |
| 4069 |
if (m->max_use != 0 && m->max_size*2 > m->max_use) |
| 4070 |
m->max_use = m->max_size*2; |
| 4071 |
} |
| 4072 |
|
| 4073 |
if (m->min_size == UINT64_MAX) |
| 4074 |
m->min_size = JOURNAL_FILE_SIZE_MIN; |
| 4075 |
else |
| 4076 |
m->min_size = CLAMP(PAGE_ALIGN_U64(m->min_size), |
| 4077 |
JOURNAL_FILE_SIZE_MIN, |
| 4078 |
m->max_size ?: UINT64_MAX); |
| 4079 |
|
| 4080 |
if (m->keep_free == UINT64_MAX) { |
| 4081 |
if (fs_size > 0) |
| 4082 |
m->keep_free = MIN(PAGE_ALIGN_U64(fs_size / 20), /* 5% of file system size */ |
| 4083 |
KEEP_FREE_UPPER); |
| 4084 |
else |
| 4085 |
m->keep_free = DEFAULT_KEEP_FREE; |
| 4086 |
} |
| 4087 |
|
| 4088 |
if (m->n_max_files == UINT64_MAX) |
| 4089 |
m->n_max_files = DEFAULT_N_MAX_FILES; |
| 4090 |
|
| 4091 |
log_debug("Fixed min_use=%s max_use=%s max_size=%s min_size=%s keep_free=%s n_max_files=%" PRIu64, |
| 4092 |
FORMAT_BYTES(m->min_use), |
| 4093 |
FORMAT_BYTES(m->max_use), |
| 4094 |
FORMAT_BYTES(m->max_size), |
| 4095 |
FORMAT_BYTES(m->min_size), |
| 4096 |
FORMAT_BYTES(m->keep_free), |
| 4097 |
m->n_max_files); |
| 4098 |
} |
| 4099 |
|
| 4100 |
int journal_file_open( |
| 4101 |
int fd, |
| 4102 |
const char *fname, |
| 4103 |
int open_flags, |
| 4104 |
JournalFileFlags file_flags, |
| 4105 |
mode_t mode, |
| 4106 |
uint64_t compress_threshold_bytes, |
| 4107 |
JournalMetrics *metrics, |
| 4108 |
MMapCache *mmap_cache, |
| 4109 |
JournalFile *template, |
| 4110 |
JournalFile **ret) { |
| 4111 |
|
| 4112 |
bool newly_created = false; |
| 4113 |
JournalFile *f; |
| 4114 |
void *h; |
| 4115 |
int r; |
| 4116 |
|
| 4117 |
assert(fd >= 0 || fname); |
| 4118 |
assert((file_flags & ~_JOURNAL_FILE_FLAGS_ALL) == 0); |
| 4119 |
assert(mmap_cache); |
| 4120 |
assert(ret); |
| 4121 |
|
| 4122 |
if (!IN_SET((open_flags & O_ACCMODE_STRICT), O_RDONLY, O_RDWR)) |
| 4123 |
return -EINVAL; |
| 4124 |
|
| 4125 |
if ((open_flags & O_ACCMODE_STRICT) == O_RDONLY && FLAGS_SET(open_flags, O_CREAT)) |
| 4126 |
return -EINVAL; |
| 4127 |
|
| 4128 |
if (fname && (open_flags & O_CREAT) && !endswith(fname, ".journal")) |
| 4129 |
return -EINVAL; |
| 4130 |
|
| 4131 |
f = new(JournalFile, 1); |
| 4132 |
if (!f) |
| 4133 |
return -ENOMEM; |
| 4134 |
|
| 4135 |
*f = (JournalFile) { |
| 4136 |
.fd = fd, |
| 4137 |
.mode = mode, |
| 4138 |
.open_flags = open_flags, |
| 4139 |
.compress_threshold_bytes = compress_threshold_bytes == UINT64_MAX ? |
| 4140 |
DEFAULT_COMPRESS_THRESHOLD : |
| 4141 |
MAX(MIN_COMPRESS_THRESHOLD, compress_threshold_bytes), |
| 4142 |
.strict_order = FLAGS_SET(file_flags, JOURNAL_STRICT_ORDER), |
| 4143 |
.newest_boot_id_prioq_idx = PRIOQ_IDX_NULL, |
| 4144 |
.last_direction = _DIRECTION_INVALID, |
| 4145 |
}; |
| 4146 |
|
| 4147 |
if (f->fd < 0) { |
| 4148 |
/* We pass O_NONBLOCK here, so that in case somebody pointed us to some character device node or FIFO |
| 4149 |
* or so, we likely fail quickly than block for long. For regular files O_NONBLOCK has no effect, hence |
| 4150 |
* it doesn't hurt in that case. */ |
| 4151 |
|
| 4152 |
f->fd = openat_report_new(AT_FDCWD, fname, f->open_flags|O_CLOEXEC|O_NONBLOCK, f->mode, &newly_created); |
| 4153 |
if (f->fd < 0) { |
| 4154 |
r = f->fd; |
| 4155 |
goto fail; |
| 4156 |
} |
| 4157 |
|
| 4158 |
/* fds we opened here by us should also be closed by us. */ |
| 4159 |
f->close_fd = true; |
| 4160 |
|
| 4161 |
r = fd_nonblock(f->fd, false); |
| 4162 |
if (r < 0) |
| 4163 |
goto fail; |
| 4164 |
|
| 4165 |
r = fd_get_path(f->fd, &f->path); |
| 4166 |
if (r < 0) |
| 4167 |
goto fail; |
| 4168 |
|
| 4169 |
if (!newly_created) { |
| 4170 |
r = journal_file_fstat(f); |
| 4171 |
if (r < 0) |
| 4172 |
goto fail; |
| 4173 |
} |
| 4174 |
} else { |
| 4175 |
/* If we don't know the path, fill in something explanatory and vaguely useful */ |
| 4176 |
f->path = strdup(fname ?: FORMAT_PROC_FD_PATH(fd)); |
| 4177 |
if (!f->path) { |
| 4178 |
r = -ENOMEM; |
| 4179 |
goto fail; |
| 4180 |
} |
| 4181 |
|
| 4182 |
r = journal_file_fstat(f); |
| 4183 |
if (r < 0) |
| 4184 |
goto fail; |
| 4185 |
|
| 4186 |
/* If we just got the fd passed in, we don't really know if we created the file anew */ |
| 4187 |
newly_created = f->last_stat.st_size == 0 && journal_file_writable(f); |
| 4188 |
} |
| 4189 |
|
| 4190 |
r = mmap_cache_add_fd(mmap_cache, f->fd, mmap_prot_from_open_flags(open_flags), &f->cache_fd); |
| 4191 |
if (r < 0) |
| 4192 |
goto fail; |
| 4193 |
|
| 4194 |
if (newly_created) { |
| 4195 |
(void) journal_file_warn_btrfs(f); |
| 4196 |
|
| 4197 |
/* Let's attach the creation time to the journal file, so that the vacuuming code knows the age of this |
| 4198 |
* file even if the file might end up corrupted one day... Ideally we'd just use the creation time many |
| 4199 |
* file systems maintain for each file, but the API to query this is very new, hence let's emulate this |
| 4200 |
* via extended attributes. If extended attributes are not supported we'll just skip this, and rely |
| 4201 |
* solely on mtime/atime/ctime of the file. */ |
| 4202 |
(void) fd_setcrtime(f->fd, 0); |
| 4203 |
|
| 4204 |
r = journal_file_init_header(f, file_flags, template); |
| 4205 |
if (r < 0) |
| 4206 |
goto fail; |
| 4207 |
|
| 4208 |
r = journal_file_fstat(f); |
| 4209 |
if (r < 0) |
| 4210 |
goto fail; |
| 4211 |
} |
| 4212 |
|
| 4213 |
if (f->last_stat.st_size < (off_t) HEADER_SIZE_MIN) { |
| 4214 |
r = -ENODATA; |
| 4215 |
goto fail; |
| 4216 |
} |
| 4217 |
|
| 4218 |
r = mmap_cache_fd_get(f->cache_fd, MMAP_CACHE_CATEGORY_HEADER, true, 0, PAGE_ALIGN(sizeof(Header)), &f->last_stat, &h); |
| 4219 |
if (r == -EINVAL) { |
| 4220 |
/* Some file systems (jffs2 or p9fs) don't support mmap() properly (or only read-only |
| 4221 |
* mmap()), and return EINVAL in that case. Let's propagate that as a more recognizable error |
| 4222 |
* code. */ |
| 4223 |
r = -EAFNOSUPPORT; |
| 4224 |
goto fail; |
| 4225 |
} |
| 4226 |
if (r < 0) |
| 4227 |
goto fail; |
| 4228 |
|
| 4229 |
f->header = h; |
| 4230 |
|
| 4231 |
if (!newly_created) { |
| 4232 |
r = journal_file_verify_header(f); |
| 4233 |
if (r < 0) |
| 4234 |
goto fail; |
| 4235 |
} |
| 4236 |
|
| 4237 |
#if HAVE_GCRYPT |
| 4238 |
if (!newly_created && journal_file_writable(f) && JOURNAL_HEADER_SEALED(f->header)) { |
| 4239 |
r = journal_file_fss_load(f); |
| 4240 |
if (r < 0) |
| 4241 |
goto fail; |
| 4242 |
} |
| 4243 |
#endif |
| 4244 |
|
| 4245 |
if (journal_file_writable(f)) { |
| 4246 |
if (metrics) { |
| 4247 |
journal_default_metrics(metrics, f->fd, JOURNAL_HEADER_COMPACT(f->header)); |
| 4248 |
f->metrics = *metrics; |
| 4249 |
} else if (template) |
| 4250 |
f->metrics = template->metrics; |
| 4251 |
|
| 4252 |
r = journal_file_refresh_header(f); |
| 4253 |
if (r < 0) |
| 4254 |
goto fail; |
| 4255 |
} |
| 4256 |
|
| 4257 |
#if HAVE_GCRYPT |
| 4258 |
r = journal_file_hmac_setup(f); |
| 4259 |
if (r < 0) |
| 4260 |
goto fail; |
| 4261 |
#endif |
| 4262 |
|
| 4263 |
if (newly_created) { |
| 4264 |
r = journal_file_setup_field_hash_table(f); |
| 4265 |
if (r < 0) |
| 4266 |
goto fail; |
| 4267 |
|
| 4268 |
r = journal_file_setup_data_hash_table(f); |
| 4269 |
if (r < 0) |
| 4270 |
goto fail; |
| 4271 |
|
| 4272 |
#if HAVE_GCRYPT |
| 4273 |
r = journal_file_append_first_tag(f); |
| 4274 |
if (r < 0) |
| 4275 |
goto fail; |
| 4276 |
#endif |
| 4277 |
} |
| 4278 |
|
| 4279 |
if (mmap_cache_fd_got_sigbus(f->cache_fd)) { |
| 4280 |
r = -EIO; |
| 4281 |
goto fail; |
| 4282 |
} |
| 4283 |
|
| 4284 |
if (template && template->post_change_timer) { |
| 4285 |
r = journal_file_enable_post_change_timer( |
| 4286 |
f, |
| 4287 |
sd_event_source_get_event(template->post_change_timer), |
| 4288 |
template->post_change_timer_period); |
| 4289 |
|
| 4290 |
if (r < 0) |
| 4291 |
goto fail; |
| 4292 |
} |
| 4293 |
|
| 4294 |
/* The file is opened now successfully, thus we take possession of any passed in fd. */ |
| 4295 |
f->close_fd = true; |
| 4296 |
|
| 4297 |
if (DEBUG_LOGGING) { |
| 4298 |
static int last_seal = -1, last_keyed_hash = -1; |
| 4299 |
static Compression last_compression = _COMPRESSION_INVALID; |
| 4300 |
static uint64_t last_bytes = UINT64_MAX; |
| 4301 |
|
| 4302 |
if (last_seal != JOURNAL_HEADER_SEALED(f->header) || |
| 4303 |
last_keyed_hash != JOURNAL_HEADER_KEYED_HASH(f->header) || |
| 4304 |
last_compression != JOURNAL_FILE_COMPRESSION(f) || |
| 4305 |
last_bytes != f->compress_threshold_bytes) { |
| 4306 |
|
| 4307 |
log_debug("Journal effective settings seal=%s keyed_hash=%s compress=%s compress_threshold_bytes=%s", |
| 4308 |
yes_no(JOURNAL_HEADER_SEALED(f->header)), yes_no(JOURNAL_HEADER_KEYED_HASH(f->header)), |
| 4309 |
compression_to_string(JOURNAL_FILE_COMPRESSION(f)), FORMAT_BYTES(f->compress_threshold_bytes)); |
| 4310 |
last_seal = JOURNAL_HEADER_SEALED(f->header); |
| 4311 |
last_keyed_hash = JOURNAL_HEADER_KEYED_HASH(f->header); |
| 4312 |
last_compression = JOURNAL_FILE_COMPRESSION(f); |
| 4313 |
last_bytes = f->compress_threshold_bytes; |
| 4314 |
} |
| 4315 |
} |
| 4316 |
|
| 4317 |
*ret = f; |
| 4318 |
return 0; |
| 4319 |
|
| 4320 |
fail: |
| 4321 |
if (f->cache_fd && mmap_cache_fd_got_sigbus(f->cache_fd)) |
| 4322 |
r = -EIO; |
| 4323 |
|
| 4324 |
(void) journal_file_close(f); |
| 4325 |
|
| 4326 |
if (newly_created && fd < 0) |
| 4327 |
(void) unlink(fname); |
| 4328 |
|
| 4329 |
return r; |
| 4330 |
} |
| 4331 |
|
| 4332 |
int journal_file_parse_uid_from_filename(const char *path, uid_t *ret_uid) { |
| 4333 |
_cleanup_free_ char *buf = NULL, *p = NULL; |
| 4334 |
const char *a, *b, *at; |
| 4335 |
int r; |
| 4336 |
|
| 4337 |
/* This helper returns -EREMOTE when the filename doesn't match user online/offline journal |
| 4338 |
* pattern. Hence it currently doesn't parse archived or disposed user journals. */ |
| 4339 |
|
| 4340 |
assert(path); |
| 4341 |
assert(ret_uid); |
| 4342 |
|
| 4343 |
r = path_extract_filename(path, &p); |
| 4344 |
if (r < 0) |
| 4345 |
return r; |
| 4346 |
if (r == O_DIRECTORY) |
| 4347 |
return -EISDIR; |
| 4348 |
|
| 4349 |
a = startswith(p, "user-"); |
| 4350 |
if (!a) |
| 4351 |
return -EREMOTE; |
| 4352 |
b = endswith(p, ".journal"); |
| 4353 |
if (!b) |
| 4354 |
return -EREMOTE; |
| 4355 |
|
| 4356 |
at = strchr(a, '@'); |
| 4357 |
if (at) |
| 4358 |
return -EREMOTE; |
| 4359 |
|
| 4360 |
buf = strndup(a, b-a); |
| 4361 |
if (!buf) |
| 4362 |
return -ENOMEM; |
| 4363 |
|
| 4364 |
return parse_uid(buf, ret_uid); |
| 4365 |
} |
| 4366 |
|
| 4367 |
int journal_file_archive(JournalFile *f, char **ret_previous_path) { |
| 4368 |
_cleanup_free_ char *p = NULL; |
| 4369 |
|
| 4370 |
assert(f); |
| 4371 |
|
| 4372 |
if (!journal_file_writable(f)) |
| 4373 |
return -EINVAL; |
| 4374 |
|
| 4375 |
/* Is this a journal file that was passed to us as fd? If so, we synthesized a path name for it, and we refuse |
| 4376 |
* rotation, since we don't know the actual path, and couldn't rename the file hence. */ |
| 4377 |
if (path_startswith(f->path, "/proc/self/fd")) |
| 4378 |
return -EINVAL; |
| 4379 |
|
| 4380 |
if (!endswith(f->path, ".journal")) |
| 4381 |
return -EINVAL; |
| 4382 |
|
| 4383 |
if (asprintf(&p, "%.*s@" SD_ID128_FORMAT_STR "-%016"PRIx64"-%016"PRIx64".journal", |
| 4384 |
(int) strlen(f->path) - 8, f->path, |
| 4385 |
SD_ID128_FORMAT_VAL(f->header->seqnum_id), |
| 4386 |
le64toh(f->header->head_entry_seqnum), |
| 4387 |
le64toh(f->header->head_entry_realtime)) < 0) |
| 4388 |
return -ENOMEM; |
| 4389 |
|
| 4390 |
/* Try to rename the file to the archived version. If the file already was deleted, we'll get ENOENT, let's |
| 4391 |
* ignore that case. */ |
| 4392 |
if (rename(f->path, p) < 0 && errno != ENOENT) |
| 4393 |
return -errno; |
| 4394 |
|
| 4395 |
/* Sync the rename to disk */ |
| 4396 |
(void) fsync_directory_of_file(f->fd); |
| 4397 |
|
| 4398 |
if (ret_previous_path) |
| 4399 |
*ret_previous_path = TAKE_PTR(f->path); |
| 4400 |
|
| 4401 |
free_and_replace(f->path, p); |
| 4402 |
|
| 4403 |
/* Set as archive so offlining commits w/state=STATE_ARCHIVED. Previously we would set old_file->header->state |
| 4404 |
* to STATE_ARCHIVED directly here, but journal_file_set_offline() short-circuits when state != STATE_ONLINE, |
| 4405 |
* which would result in the rotated journal never getting fsync() called before closing. Now we simply queue |
| 4406 |
* the archive state by setting an archive bit, leaving the state as STATE_ONLINE so proper offlining |
| 4407 |
* occurs. */ |
| 4408 |
f->archive = true; |
| 4409 |
|
| 4410 |
return 0; |
| 4411 |
} |
| 4412 |
|
| 4413 |
int journal_file_dispose(int dir_fd, const char *fname) { |
| 4414 |
_cleanup_free_ char *p = NULL; |
| 4415 |
|
| 4416 |
assert(fname); |
| 4417 |
|
| 4418 |
/* Renames a journal file to *.journal~, i.e. to mark it as corrupted or otherwise uncleanly shutdown. Note that |
| 4419 |
* this is done without looking into the file or changing any of its contents. The idea is that this is called |
| 4420 |
* whenever something is suspicious and we want to move the file away and make clear that it is not accessed |
| 4421 |
* for writing anymore. */ |
| 4422 |
|
| 4423 |
if (!endswith(fname, ".journal")) |
| 4424 |
return -EINVAL; |
| 4425 |
|
| 4426 |
if (asprintf(&p, "%.*s@%016" PRIx64 "-%016" PRIx64 ".journal~", |
| 4427 |
(int) strlen(fname) - 8, fname, |
| 4428 |
now(CLOCK_REALTIME), |
| 4429 |
random_u64()) < 0) |
| 4430 |
return -ENOMEM; |
| 4431 |
|
| 4432 |
if (renameat(dir_fd, fname, dir_fd, p) < 0) |
| 4433 |
return -errno; |
| 4434 |
|
| 4435 |
return 0; |
| 4436 |
} |
| 4437 |
|
| 4438 |
int journal_file_copy_entry( |
| 4439 |
JournalFile *from, |
| 4440 |
JournalFile *to, |
| 4441 |
Object *o, |
| 4442 |
uint64_t p, |
| 4443 |
uint64_t *seqnum, |
| 4444 |
sd_id128_t *seqnum_id) { |
| 4445 |
|
| 4446 |
_cleanup_free_ EntryItem *items_alloc = NULL; |
| 4447 |
EntryItem *items; |
| 4448 |
uint64_t n, m = 0, xor_hash = 0; |
| 4449 |
sd_id128_t boot_id; |
| 4450 |
dual_timestamp ts; |
| 4451 |
int r; |
| 4452 |
|
| 4453 |
assert(from); |
| 4454 |
assert(to); |
| 4455 |
assert(o); |
| 4456 |
assert(p > 0); |
| 4457 |
|
| 4458 |
if (!journal_file_writable(to)) |
| 4459 |
return -EPERM; |
| 4460 |
|
| 4461 |
ts = (dual_timestamp) { |
| 4462 |
.monotonic = le64toh(o->entry.monotonic), |
| 4463 |
.realtime = le64toh(o->entry.realtime), |
| 4464 |
}; |
| 4465 |
boot_id = o->entry.boot_id; |
| 4466 |
|
| 4467 |
n = journal_file_entry_n_items(from, o); |
| 4468 |
if (n == 0) |
| 4469 |
return 0; |
| 4470 |
|
| 4471 |
if (n < ALLOCA_MAX / sizeof(EntryItem) / 2) |
| 4472 |
items = newa(EntryItem, n); |
| 4473 |
else { |
| 4474 |
items_alloc = new(EntryItem, n); |
| 4475 |
if (!items_alloc) |
| 4476 |
return -ENOMEM; |
| 4477 |
|
| 4478 |
items = items_alloc; |
| 4479 |
} |
| 4480 |
|
| 4481 |
for (uint64_t i = 0; i < n; i++) { |
| 4482 |
uint64_t h, q; |
| 4483 |
void *data; |
| 4484 |
size_t l; |
| 4485 |
Object *u; |
| 4486 |
|
| 4487 |
q = journal_file_entry_item_object_offset(from, o, i); |
| 4488 |
r = journal_file_data_payload(from, NULL, q, NULL, 0, 0, &data, &l); |
| 4489 |
if (IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) { |
| 4490 |
log_debug_errno(r, "Entry item %"PRIu64" data object is bad, skipping over it: %m", i); |
| 4491 |
continue; |
| 4492 |
} |
| 4493 |
if (r < 0) |
| 4494 |
return r; |
| 4495 |
assert(r > 0); |
| 4496 |
|
| 4497 |
r = journal_file_append_data(to, data, l, &u, &h); |
| 4498 |
if (r == -EUCLEAN) { |
| 4499 |
log_debug_errno(r, "Entry item %"PRIu64" invalid, skipping over it: %m", i); |
| 4500 |
continue; |
| 4501 |
} |
| 4502 |
if (r < 0) |
| 4503 |
return r; |
| 4504 |
|
| 4505 |
if (JOURNAL_HEADER_KEYED_HASH(to->header)) |
| 4506 |
xor_hash ^= jenkins_hash64(data, l); |
| 4507 |
else |
| 4508 |
xor_hash ^= le64toh(u->data.hash); |
| 4509 |
|
| 4510 |
items[m++] = (EntryItem) { |
| 4511 |
.object_offset = h, |
| 4512 |
.hash = le64toh(u->data.hash), |
| 4513 |
}; |
| 4514 |
} |
| 4515 |
|
| 4516 |
if (m == 0) |
| 4517 |
return 0; |
| 4518 |
|
| 4519 |
r = journal_file_append_entry_internal( |
| 4520 |
to, |
| 4521 |
&ts, |
| 4522 |
&boot_id, |
| 4523 |
&from->header->machine_id, |
| 4524 |
xor_hash, |
| 4525 |
items, |
| 4526 |
m, |
| 4527 |
seqnum, |
| 4528 |
seqnum_id, |
| 4529 |
/* ret_object= */ NULL, |
| 4530 |
/* ret_offset= */ NULL); |
| 4531 |
|
| 4532 |
if (mmap_cache_fd_got_sigbus(to->cache_fd)) |
| 4533 |
return -EIO; |
| 4534 |
|
| 4535 |
return r; |
| 4536 |
} |
| 4537 |
|
| 4538 |
void journal_reset_metrics(JournalMetrics *m) { |
| 4539 |
assert(m); |
| 4540 |
|
| 4541 |
/* Set everything to "pick automatic values". */ |
| 4542 |
|
| 4543 |
*m = (JournalMetrics) { |
| 4544 |
.min_use = UINT64_MAX, |
| 4545 |
.max_use = UINT64_MAX, |
| 4546 |
.min_size = UINT64_MAX, |
| 4547 |
.max_size = UINT64_MAX, |
| 4548 |
.keep_free = UINT64_MAX, |
| 4549 |
.n_max_files = UINT64_MAX, |
| 4550 |
}; |
| 4551 |
} |
| 4552 |
|
| 4553 |
bool journal_metrics_equal(const JournalMetrics *x, const JournalMetrics *y) { |
| 4554 |
assert(x); |
| 4555 |
assert(y); |
| 4556 |
|
| 4557 |
return |
| 4558 |
x->max_size == y->max_size && |
| 4559 |
x->min_size == y->min_size && |
| 4560 |
x->max_use == y->max_use && |
| 4561 |
x->min_use == y->min_use && |
| 4562 |
x->keep_free == y->keep_free && |
| 4563 |
x->n_max_files == y->n_max_files; |
| 4564 |
} |
| 4565 |
|
| 4566 |
int journal_file_get_cutoff_realtime_usec(JournalFile *f, usec_t *ret_from, usec_t *ret_to) { |
| 4567 |
assert(f); |
| 4568 |
assert(f->header); |
| 4569 |
assert(ret_from || ret_to); |
| 4570 |
|
| 4571 |
if (ret_from) { |
| 4572 |
if (f->header->head_entry_realtime == 0) |
| 4573 |
return -ENOENT; |
| 4574 |
|
| 4575 |
*ret_from = le64toh(f->header->head_entry_realtime); |
| 4576 |
} |
| 4577 |
|
| 4578 |
if (ret_to) { |
| 4579 |
if (f->header->tail_entry_realtime == 0) |
| 4580 |
return -ENOENT; |
| 4581 |
|
| 4582 |
*ret_to = le64toh(f->header->tail_entry_realtime); |
| 4583 |
} |
| 4584 |
|
| 4585 |
return 1; |
| 4586 |
} |
| 4587 |
|
| 4588 |
int journal_file_get_cutoff_monotonic_usec(JournalFile *f, sd_id128_t boot_id, usec_t *ret_from, usec_t *ret_to) { |
| 4589 |
Object *o; |
| 4590 |
uint64_t p; |
| 4591 |
int r; |
| 4592 |
|
| 4593 |
assert(f); |
| 4594 |
assert(ret_from || ret_to); |
| 4595 |
|
| 4596 |
/* FIXME: fix return value assignment on success with 0. */ |
| 4597 |
|
| 4598 |
r = find_data_object_by_boot_id(f, boot_id, &o, &p); |
| 4599 |
if (r <= 0) |
| 4600 |
return r; |
| 4601 |
|
| 4602 |
if (le64toh(o->data.n_entries) <= 0) |
| 4603 |
return 0; |
| 4604 |
|
| 4605 |
if (ret_from) { |
| 4606 |
r = journal_file_move_to_object(f, OBJECT_ENTRY, le64toh(o->data.entry_offset), &o); |
| 4607 |
if (r < 0) |
| 4608 |
return r; |
| 4609 |
|
| 4610 |
*ret_from = le64toh(o->entry.monotonic); |
| 4611 |
} |
| 4612 |
|
| 4613 |
if (ret_to) { |
| 4614 |
r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); |
| 4615 |
if (r < 0) |
| 4616 |
return r; |
| 4617 |
|
| 4618 |
r = journal_file_move_to_entry_for_data(f, o, DIRECTION_UP, &o, NULL); |
| 4619 |
if (r <= 0) |
| 4620 |
return r; |
| 4621 |
|
| 4622 |
*ret_to = le64toh(o->entry.monotonic); |
| 4623 |
} |
| 4624 |
|
| 4625 |
return 1; |
| 4626 |
} |
| 4627 |
|
| 4628 |
bool journal_file_rotate_suggested(JournalFile *f, usec_t max_file_usec, int log_level) { |
| 4629 |
assert(f); |
| 4630 |
assert(f->header); |
| 4631 |
|
| 4632 |
/* If we gained new header fields we gained new features, |
| 4633 |
* hence suggest a rotation */ |
| 4634 |
if (le64toh(f->header->header_size) < sizeof(Header)) { |
| 4635 |
log_ratelimit_full(log_level, JOURNAL_LOG_RATELIMIT, |
| 4636 |
"%s uses an outdated header, suggesting rotation.", f->path); |
| 4637 |
return true; |
| 4638 |
} |
| 4639 |
|
| 4640 |
/* Let's check if the hash tables grew over a certain fill level (75%, borrowing this value from |
| 4641 |
* Java's hash table implementation), and if so suggest a rotation. To calculate the fill level we |
| 4642 |
* need the n_data field, which only exists in newer versions. */ |
| 4643 |
|
| 4644 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_data)) |
| 4645 |
if (le64toh(f->header->n_data) * 4ULL > (le64toh(f->header->data_hash_table_size) / sizeof(HashItem)) * 3ULL) { |
| 4646 |
size_t n_data_items = le64toh(f->header->data_hash_table_size) / sizeof(HashItem); |
| 4647 |
log_ratelimit_full( |
| 4648 |
log_level, JOURNAL_LOG_RATELIMIT, |
| 4649 |
"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.", |
| 4650 |
f->path, |
| 4651 |
100.0 * (double) le64toh(f->header->n_data) / (double) n_data_items, |
| 4652 |
le64toh(f->header->n_data), |
| 4653 |
le64toh(f->header->data_hash_table_size) / sizeof(HashItem), |
| 4654 |
(uint64_t) f->last_stat.st_size, |
| 4655 |
f->last_stat.st_size / le64toh(f->header->n_data)); |
| 4656 |
return true; |
| 4657 |
} |
| 4658 |
|
| 4659 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_fields)) |
| 4660 |
if (le64toh(f->header->n_fields) * 4ULL > (le64toh(f->header->field_hash_table_size) / sizeof(HashItem)) * 3ULL) { |
| 4661 |
size_t n_field_items = le64toh(f->header->field_hash_table_size) / sizeof(HashItem); |
| 4662 |
log_ratelimit_full( |
| 4663 |
log_level, JOURNAL_LOG_RATELIMIT, |
| 4664 |
"Field hash table of %s has a fill level at %.1f (%"PRIu64" of %"PRIu64" items), suggesting rotation.", |
| 4665 |
f->path, |
| 4666 |
100.0 * (double) le64toh(f->header->n_fields) / (double) n_field_items, |
| 4667 |
le64toh(f->header->n_fields), |
| 4668 |
le64toh(f->header->field_hash_table_size) / sizeof(HashItem)); |
| 4669 |
return true; |
| 4670 |
} |
| 4671 |
|
| 4672 |
/* If there are too many hash collisions somebody is most likely playing games with us. Hence, if our |
| 4673 |
* longest chain is longer than some threshold, let's suggest rotation. */ |
| 4674 |
if (JOURNAL_HEADER_CONTAINS(f->header, data_hash_chain_depth) && |
| 4675 |
le64toh(f->header->data_hash_chain_depth) > HASH_CHAIN_DEPTH_MAX) { |
| 4676 |
log_ratelimit_full( |
| 4677 |
log_level, JOURNAL_LOG_RATELIMIT, |
| 4678 |
"Data hash table of %s has deepest hash chain of length %" PRIu64 ", suggesting rotation.", |
| 4679 |
f->path, le64toh(f->header->data_hash_chain_depth)); |
| 4680 |
return true; |
| 4681 |
} |
| 4682 |
|
| 4683 |
if (JOURNAL_HEADER_CONTAINS(f->header, field_hash_chain_depth) && |
| 4684 |
le64toh(f->header->field_hash_chain_depth) > HASH_CHAIN_DEPTH_MAX) { |
| 4685 |
log_ratelimit_full( |
| 4686 |
log_level, JOURNAL_LOG_RATELIMIT, |
| 4687 |
"Field hash table of %s has deepest hash chain of length at %" PRIu64 ", suggesting rotation.", |
| 4688 |
f->path, le64toh(f->header->field_hash_chain_depth)); |
| 4689 |
return true; |
| 4690 |
} |
| 4691 |
|
| 4692 |
/* Are the data objects properly indexed by field objects? */ |
| 4693 |
if (JOURNAL_HEADER_CONTAINS(f->header, n_data) && |
| 4694 |
JOURNAL_HEADER_CONTAINS(f->header, n_fields) && |
| 4695 |
le64toh(f->header->n_data) > 0 && |
| 4696 |
le64toh(f->header->n_fields) == 0) { |
| 4697 |
log_ratelimit_full( |
| 4698 |
log_level, JOURNAL_LOG_RATELIMIT, |
| 4699 |
"Data objects of %s are not indexed by field objects, suggesting rotation.", |
| 4700 |
f->path); |
| 4701 |
return true; |
| 4702 |
} |
| 4703 |
|
| 4704 |
if (max_file_usec > 0) { |
| 4705 |
usec_t t, h; |
| 4706 |
|
| 4707 |
h = le64toh(f->header->head_entry_realtime); |
| 4708 |
t = now(CLOCK_REALTIME); |
| 4709 |
|
| 4710 |
if (h > 0 && t > h + max_file_usec) { |
| 4711 |
log_ratelimit_full( |
| 4712 |
log_level, JOURNAL_LOG_RATELIMIT, |
| 4713 |
"Oldest entry in %s is older than the configured file retention duration (%s), suggesting rotation.", |
| 4714 |
f->path, FORMAT_TIMESPAN(max_file_usec, USEC_PER_SEC)); |
| 4715 |
return true; |
| 4716 |
} |
| 4717 |
} |
| 4718 |
|
| 4719 |
return false; |
| 4720 |
} |
| 4721 |
|
| 4722 |
bool journal_file_writable(const JournalFile *f) { |
| 4723 |
assert(f); |
| 4724 |
|
| 4725 |
return (f->open_flags & O_ACCMODE_STRICT) != O_RDONLY; |
| 4726 |
} |
| 4727 |
|
| 4728 |
static const char * const journal_object_type_table[] = { |
| 4729 |
[OBJECT_UNUSED] = "unused", |
| 4730 |
[OBJECT_DATA] = "data", |
| 4731 |
[OBJECT_FIELD] = "field", |
| 4732 |
[OBJECT_ENTRY] = "entry", |
| 4733 |
[OBJECT_DATA_HASH_TABLE] = "data hash table", |
| 4734 |
[OBJECT_FIELD_HASH_TABLE] = "field hash table", |
| 4735 |
[OBJECT_ENTRY_ARRAY] = "entry array", |
| 4736 |
[OBJECT_TAG] = "tag", |
| 4737 |
}; |
| 4738 |
|
| 4739 |
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(journal_object_type, ObjectType); |