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