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