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