]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journal-file.c
journal: add inline compression support with XZ
[thirdparty/systemd.git] / src / journal / journal-file.c
CommitLineData
cec736d2
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <sys/mman.h>
23#include <errno.h>
24#include <sys/uio.h>
25#include <unistd.h>
26#include <sys/statvfs.h>
27#include <fcntl.h>
28#include <stddef.h>
29
30#include "journal-def.h"
31#include "journal-file.h"
32#include "lookup3.h"
807e17f0 33#include "compress.h"
cec736d2 34
de190aef
LP
35#define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*16ULL)
36#define DEFAULT_FIELD_HASH_TABLE_SIZE (2047ULL*16ULL)
cec736d2
LP
37
38#define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
39
807e17f0
LP
40#define COMPRESSION_SIZE_THRESHOLD (64ULL)
41
cec736d2
LP
42static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
43
44#define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
45
46void journal_file_close(JournalFile *f) {
de190aef 47 int t;
cec736d2 48
de190aef 49 assert(f);
cec736d2 50
de190aef
LP
51 if (f->header && f->writable)
52 f->header->state = STATE_OFFLINE;
cec736d2 53
cec736d2 54
de190aef
LP
55 for (t = 0; t < _WINDOW_MAX; t++)
56 if (f->windows[t].ptr)
57 munmap(f->windows[t].ptr, f->windows[t].size);
cec736d2 58
0ac38b70
LP
59 if (f->fd >= 0)
60 close_nointr_nofail(f->fd);
61
cec736d2 62 free(f->path);
807e17f0
LP
63
64#ifdef HAVE_XZ
65 free(f->compress_buffer);
66#endif
67
cec736d2
LP
68 free(f);
69}
70
0ac38b70 71static int journal_file_init_header(JournalFile *f, JournalFile *template) {
cec736d2
LP
72 Header h;
73 ssize_t k;
74 int r;
75
76 assert(f);
77
78 zero(h);
79 memcpy(h.signature, signature, 8);
80 h.arena_offset = htole64(ALIGN64(sizeof(h)));
cec736d2
LP
81
82 r = sd_id128_randomize(&h.file_id);
83 if (r < 0)
84 return r;
85
0ac38b70
LP
86 if (template) {
87 h.seqnum_id = template->header->seqnum_id;
88 h.seqnum = template->header->seqnum;
89 } else
90 h.seqnum_id = h.file_id;
cec736d2
LP
91
92 k = pwrite(f->fd, &h, sizeof(h), 0);
93 if (k < 0)
94 return -errno;
95
96 if (k != sizeof(h))
97 return -EIO;
98
99 return 0;
100}
101
102static int journal_file_refresh_header(JournalFile *f) {
103 int r;
de190aef 104 sd_id128_t boot_id;
cec736d2
LP
105
106 assert(f);
107
108 r = sd_id128_get_machine(&f->header->machine_id);
109 if (r < 0)
110 return r;
111
de190aef 112 r = sd_id128_get_boot(&boot_id);
cec736d2
LP
113 if (r < 0)
114 return r;
115
de190aef
LP
116 if (sd_id128_equal(boot_id, f->header->boot_id))
117 f->tail_entry_monotonic_valid = true;
118
119 f->header->boot_id = boot_id;
120
121 f->header->state = STATE_ONLINE;
cec736d2
LP
122 return 0;
123}
124
125static int journal_file_verify_header(JournalFile *f) {
126 assert(f);
127
128 if (memcmp(f->header, signature, 8))
129 return -EBADMSG;
130
807e17f0
LP
131#ifdef HAVE_XZ
132 if ((le64toh(f->header->incompatible_flags) & ~HEADER_INCOMPATIBLE_COMPRESSED) != 0)
133 return -EPROTONOSUPPORT;
134#else
cec736d2
LP
135 if (f->header->incompatible_flags != 0)
136 return -EPROTONOSUPPORT;
807e17f0 137#endif
cec736d2
LP
138
139 if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
140 return -ENODATA;
141
142 if (f->writable) {
143 uint32_t state;
144 sd_id128_t machine_id;
145 int r;
146
147 r = sd_id128_get_machine(&machine_id);
148 if (r < 0)
149 return r;
150
151 if (!sd_id128_equal(machine_id, f->header->machine_id))
152 return -EHOSTDOWN;
153
de190aef 154 state = f->header->state;
cec736d2
LP
155
156 if (state == STATE_ONLINE)
157 log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f->path);
158 else if (state == STATE_ARCHIVED)
159 return -ESHUTDOWN;
160 else if (state != STATE_OFFLINE)
161 log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
162 }
163
164 return 0;
165}
166
167static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
cec736d2
LP
168 uint64_t old_size, new_size;
169
170 assert(f);
171
cec736d2 172 /* We assume that this file is not sparse, and we know that
38ac38b2 173 * for sure, since we always call posix_fallocate()
cec736d2
LP
174 * ourselves */
175
176 old_size =
177 le64toh(f->header->arena_offset) +
178 le64toh(f->header->arena_size);
179
bc85bfee
LP
180 new_size = PAGE_ALIGN(offset + size);
181 if (new_size < le64toh(f->header->arena_offset))
182 new_size = le64toh(f->header->arena_offset);
183
184 if (new_size <= old_size)
cec736d2
LP
185 return 0;
186
bc85bfee
LP
187 if (f->metrics.max_size > 0 &&
188 new_size > f->metrics.max_size)
189 return -E2BIG;
cec736d2 190
bc85bfee
LP
191 if (new_size > f->metrics.min_size &&
192 f->metrics.keep_free > 0) {
cec736d2
LP
193 struct statvfs svfs;
194
195 if (fstatvfs(f->fd, &svfs) >= 0) {
196 uint64_t available;
197
198 available = svfs.f_bfree * svfs.f_bsize;
199
bc85bfee
LP
200 if (available >= f->metrics.keep_free)
201 available -= f->metrics.keep_free;
cec736d2
LP
202 else
203 available = 0;
204
205 if (new_size - old_size > available)
206 return -E2BIG;
207 }
208 }
209
bc85bfee
LP
210 /* Note that the glibc fallocate() fallback is very
211 inefficient, hence we try to minimize the allocation area
212 as we can. */
38ac38b2 213 if (posix_fallocate(f->fd, old_size, new_size - old_size) < 0)
cec736d2
LP
214 return -errno;
215
216 if (fstat(f->fd, &f->last_stat) < 0)
217 return -errno;
218
bc85bfee 219 f->header->arena_size = new_size - htole64(f->header->arena_offset);
cec736d2
LP
220
221 return 0;
222}
223
224static int journal_file_map(
225 JournalFile *f,
226 uint64_t offset,
227 uint64_t size,
228 void **_window,
229 uint64_t *_woffset,
230 uint64_t *_wsize,
231 void **ret) {
232
233 uint64_t woffset, wsize;
234 void *window;
235
236 assert(f);
237 assert(size > 0);
238 assert(ret);
239
240 woffset = offset & ~((uint64_t) page_size() - 1ULL);
241 wsize = size + (offset - woffset);
242 wsize = PAGE_ALIGN(wsize);
243
244 window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
245 if (window == MAP_FAILED)
246 return -errno;
247
248 if (_window)
249 *_window = window;
250
251 if (_woffset)
252 *_woffset = woffset;
253
254 if (_wsize)
255 *_wsize = wsize;
256
257 *ret = (uint8_t*) window + (offset - woffset);
258
259 return 0;
260}
261
de190aef 262static int journal_file_move_to(JournalFile *f, int wt, uint64_t offset, uint64_t size, void **ret) {
cec736d2
LP
263 void *p;
264 uint64_t delta;
265 int r;
de190aef 266 Window *w;
cec736d2
LP
267
268 assert(f);
269 assert(ret);
de190aef
LP
270 assert(wt >= 0);
271 assert(wt < _WINDOW_MAX);
cec736d2 272
de190aef 273 w = f->windows + wt;
cec736d2 274
de190aef
LP
275 if (_likely_(w->ptr &&
276 w->offset <= offset &&
277 w->offset + w->size >= offset + size)) {
278
279 *ret = (uint8_t*) w->ptr + (offset - w->offset);
cec736d2
LP
280 return 0;
281 }
282
de190aef
LP
283 if (w->ptr) {
284 if (munmap(w->ptr, w->size) < 0)
cec736d2
LP
285 return -errno;
286
de190aef
LP
287 w->ptr = NULL;
288 w->size = w->offset = 0;
cec736d2
LP
289 }
290
291 if (size < DEFAULT_WINDOW_SIZE) {
292 /* If the default window size is larger then what was
293 * asked for extend the mapping a bit in the hope to
294 * minimize needed remappings later on. We add half
295 * the window space before and half behind the
296 * requested mapping */
297
298 delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
299
300 if (offset < delta)
301 delta = offset;
302
303 offset -= delta;
304 size += (DEFAULT_WINDOW_SIZE - delta);
305 } else
306 delta = 0;
307
308 r = journal_file_map(f,
309 offset, size,
de190aef
LP
310 &w->ptr, &w->offset, &w->size,
311 &p);
cec736d2
LP
312
313 if (r < 0)
314 return r;
315
316 *ret = (uint8_t*) p + delta;
317 return 0;
318}
319
320static bool verify_hash(Object *o) {
de190aef 321 uint64_t h1, h2;
cec736d2
LP
322
323 assert(o);
324
807e17f0 325 if (o->object.type == OBJECT_DATA && !(o->object.flags & OBJECT_COMPRESSED)) {
cec736d2 326 h1 = le64toh(o->data.hash);
de190aef
LP
327 h2 = hash64(o->data.payload, le64toh(o->object.size) - offsetof(Object, data.payload));
328 } else if (o->object.type == OBJECT_FIELD) {
329 h1 = le64toh(o->field.hash);
330 h2 = hash64(o->field.payload, le64toh(o->object.size) - offsetof(Object, field.payload));
331 } else
332 return true;
cec736d2 333
de190aef 334 return h1 == h2;
cec736d2
LP
335}
336
de190aef 337int journal_file_move_to_object(JournalFile *f, int type, uint64_t offset, Object **ret) {
cec736d2
LP
338 int r;
339 void *t;
340 Object *o;
341 uint64_t s;
342
343 assert(f);
344 assert(ret);
de190aef 345 assert(type < _OBJECT_TYPE_MAX);
cec736d2 346
de190aef 347 r = journal_file_move_to(f, type >= 0 ? type : WINDOW_UNKNOWN, offset, sizeof(ObjectHeader), &t);
cec736d2
LP
348 if (r < 0)
349 return r;
350
351 o = (Object*) t;
352 s = le64toh(o->object.size);
353
354 if (s < sizeof(ObjectHeader))
355 return -EBADMSG;
356
de190aef 357 if (type >= 0 && o->object.type != type)
cec736d2
LP
358 return -EBADMSG;
359
360 if (s > sizeof(ObjectHeader)) {
de190aef 361 r = journal_file_move_to(f, o->object.type, offset, s, &t);
cec736d2
LP
362 if (r < 0)
363 return r;
364
365 o = (Object*) t;
366 }
367
368 if (!verify_hash(o))
369 return -EBADMSG;
370
371 *ret = o;
372 return 0;
373}
374
c2373f84 375static uint64_t journal_file_seqnum(JournalFile *f, uint64_t *seqnum) {
cec736d2
LP
376 uint64_t r;
377
378 assert(f);
379
380 r = le64toh(f->header->seqnum) + 1;
c2373f84
LP
381
382 if (seqnum) {
de190aef 383 /* If an external seqnum counter was passed, we update
c2373f84
LP
384 * both the local and the external one, and set it to
385 * the maximum of both */
386
387 if (*seqnum + 1 > r)
388 r = *seqnum + 1;
389
390 *seqnum = r;
391 }
392
cec736d2
LP
393 f->header->seqnum = htole64(r);
394
de190aef
LP
395 if (f->header->first_seqnum == 0)
396 f->header->first_seqnum = htole64(r);
397
cec736d2
LP
398 return r;
399}
400
de190aef 401static int journal_file_append_object(JournalFile *f, int type, uint64_t size, Object **ret, uint64_t *offset) {
cec736d2
LP
402 int r;
403 uint64_t p;
404 Object *tail, *o;
405 void *t;
406
407 assert(f);
408 assert(size >= sizeof(ObjectHeader));
409 assert(offset);
410 assert(ret);
411
412 p = le64toh(f->header->tail_object_offset);
cec736d2
LP
413 if (p == 0)
414 p = le64toh(f->header->arena_offset);
415 else {
de190aef 416 r = journal_file_move_to_object(f, -1, p, &tail);
cec736d2
LP
417 if (r < 0)
418 return r;
419
420 p += ALIGN64(le64toh(tail->object.size));
421 }
422
423 r = journal_file_allocate(f, p, size);
424 if (r < 0)
425 return r;
426
de190aef 427 r = journal_file_move_to(f, type, p, size, &t);
cec736d2
LP
428 if (r < 0)
429 return r;
430
431 o = (Object*) t;
432
433 zero(o->object);
de190aef 434 o->object.type = type;
cec736d2
LP
435 o->object.size = htole64(size);
436
437 f->header->tail_object_offset = htole64(p);
cec736d2
LP
438 f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
439
440 *ret = o;
441 *offset = p;
442
443 return 0;
444}
445
de190aef 446static int journal_file_setup_data_hash_table(JournalFile *f) {
cec736d2
LP
447 uint64_t s, p;
448 Object *o;
449 int r;
450
451 assert(f);
452
de190aef
LP
453 s = DEFAULT_DATA_HASH_TABLE_SIZE;
454 r = journal_file_append_object(f,
455 OBJECT_DATA_HASH_TABLE,
456 offsetof(Object, hash_table.items) + s,
457 &o, &p);
cec736d2
LP
458 if (r < 0)
459 return r;
460
de190aef 461 memset(o->hash_table.items, 0, s);
cec736d2 462
de190aef
LP
463 f->header->data_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
464 f->header->data_hash_table_size = htole64(s);
cec736d2
LP
465
466 return 0;
467}
468
de190aef 469static int journal_file_setup_field_hash_table(JournalFile *f) {
cec736d2
LP
470 uint64_t s, p;
471 Object *o;
472 int r;
473
474 assert(f);
475
de190aef
LP
476 s = DEFAULT_FIELD_HASH_TABLE_SIZE;
477 r = journal_file_append_object(f,
478 OBJECT_FIELD_HASH_TABLE,
479 offsetof(Object, hash_table.items) + s,
480 &o, &p);
cec736d2
LP
481 if (r < 0)
482 return r;
483
de190aef 484 memset(o->hash_table.items, 0, s);
cec736d2 485
de190aef
LP
486 f->header->field_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
487 f->header->field_hash_table_size = htole64(s);
cec736d2
LP
488
489 return 0;
490}
491
de190aef 492static int journal_file_map_data_hash_table(JournalFile *f) {
cec736d2
LP
493 uint64_t s, p;
494 void *t;
495 int r;
496
497 assert(f);
498
de190aef
LP
499 p = le64toh(f->header->data_hash_table_offset);
500 s = le64toh(f->header->data_hash_table_size);
cec736d2 501
de190aef
LP
502 r = journal_file_move_to(f,
503 WINDOW_DATA_HASH_TABLE,
504 p, s,
505 &t);
cec736d2
LP
506 if (r < 0)
507 return r;
508
de190aef 509 f->data_hash_table = t;
cec736d2
LP
510 return 0;
511}
512
de190aef 513static int journal_file_map_field_hash_table(JournalFile *f) {
cec736d2
LP
514 uint64_t s, p;
515 void *t;
516 int r;
517
518 assert(f);
519
de190aef
LP
520 p = le64toh(f->header->field_hash_table_offset);
521 s = le64toh(f->header->field_hash_table_size);
cec736d2 522
de190aef
LP
523 r = journal_file_move_to(f,
524 WINDOW_FIELD_HASH_TABLE,
525 p, s,
526 &t);
cec736d2
LP
527 if (r < 0)
528 return r;
529
de190aef 530 f->field_hash_table = t;
cec736d2
LP
531 return 0;
532}
533
de190aef
LP
534static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash) {
535 uint64_t p, h;
cec736d2
LP
536 int r;
537
538 assert(f);
539 assert(o);
540 assert(offset > 0);
de190aef 541 assert(o->object.type == OBJECT_DATA);
cec736d2 542
de190aef
LP
543 o->data.next_hash_offset = o->data.next_field_offset = 0;
544 o->data.entry_offset = o->data.entry_array_offset = 0;
545 o->data.n_entries = 0;
cec736d2 546
de190aef
LP
547 h = hash % (le64toh(f->header->data_hash_table_size) / sizeof(HashItem));
548 p = le64toh(f->data_hash_table[h].head_hash_offset);
cec736d2
LP
549 if (p == 0) {
550 /* Only entry in the hash table is easy */
de190aef 551 f->data_hash_table[h].head_hash_offset = htole64(offset);
cec736d2 552 } else {
cec736d2
LP
553 /* Temporarily move back to the previous data object,
554 * to patch in pointer */
555
de190aef 556 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
cec736d2
LP
557 if (r < 0)
558 return r;
559
de190aef 560 o->data.next_hash_offset = htole64(offset);
cec736d2 561
de190aef 562 r = journal_file_move_to_object(f, OBJECT_DATA, offset, &o);
cec736d2
LP
563 if (r < 0)
564 return r;
565 }
566
de190aef 567 f->data_hash_table[h].tail_hash_offset = htole64(offset);
cec736d2
LP
568
569 return 0;
570}
571
de190aef
LP
572int journal_file_find_data_object_with_hash(
573 JournalFile *f,
574 const void *data, uint64_t size, uint64_t hash,
575 Object **ret, uint64_t *offset) {
576 uint64_t p, osize, h;
cec736d2
LP
577 int r;
578
579 assert(f);
580 assert(data || size == 0);
581
582 osize = offsetof(Object, data.payload) + size;
583
bc85bfee
LP
584 if (f->header->data_hash_table_size == 0)
585 return -EBADMSG;
586
de190aef
LP
587 h = hash % (le64toh(f->header->data_hash_table_size) / sizeof(HashItem));
588 p = le64toh(f->data_hash_table[h].head_hash_offset);
cec736d2 589
de190aef
LP
590 while (p > 0) {
591 Object *o;
cec736d2 592
de190aef 593 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
cec736d2
LP
594 if (r < 0)
595 return r;
596
807e17f0
LP
597 if (le64toh(o->data.hash) != hash)
598 return -EBADMSG;
599
600 if (o->object.flags & OBJECT_COMPRESSED) {
601#ifdef HAVE_XZ
602 uint64_t l, rsize;
cec736d2 603
807e17f0
LP
604 l = le64toh(o->object.size);
605 if (l <= offsetof(Object, data.payload))
cec736d2
LP
606 return -EBADMSG;
607
807e17f0
LP
608 l -= offsetof(Object, data.payload);
609
610 if (!uncompress_blob(o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize))
611 return -EBADMSG;
612
613 if (rsize == size &&
614 memcmp(f->compress_buffer, data, size) == 0) {
615
616 if (ret)
617 *ret = o;
618
619 if (offset)
620 *offset = p;
621
622 return 1;
623 }
624#else
625 return -EPROTONOSUPPORT;
626#endif
627
628 } else if (le64toh(o->object.size) == osize &&
629 memcmp(o->data.payload, data, size) == 0) {
630
cec736d2
LP
631 if (ret)
632 *ret = o;
633
634 if (offset)
635 *offset = p;
636
de190aef 637 return 1;
cec736d2
LP
638 }
639
640 p = le64toh(o->data.next_hash_offset);
641 }
642
de190aef
LP
643 return 0;
644}
645
646int journal_file_find_data_object(
647 JournalFile *f,
648 const void *data, uint64_t size,
649 Object **ret, uint64_t *offset) {
650
651 uint64_t hash;
652
653 assert(f);
654 assert(data || size == 0);
655
656 hash = hash64(data, size);
657
658 return journal_file_find_data_object_with_hash(f,
659 data, size, hash,
660 ret, offset);
661}
662
663static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
664 uint64_t hash, p;
665 uint64_t osize;
666 Object *o;
667 int r;
807e17f0 668 bool compressed = false;
de190aef
LP
669
670 assert(f);
671 assert(data || size == 0);
672
673 hash = hash64(data, size);
674
675 r = journal_file_find_data_object_with_hash(f, data, size, hash, &o, &p);
676 if (r < 0)
677 return r;
678 else if (r > 0) {
679
680 if (ret)
681 *ret = o;
682
683 if (offset)
684 *offset = p;
685
686 return 0;
687 }
688
689 osize = offsetof(Object, data.payload) + size;
690 r = journal_file_append_object(f, OBJECT_DATA, osize, &o, &p);
cec736d2
LP
691 if (r < 0)
692 return r;
693
cec736d2 694 o->data.hash = htole64(hash);
807e17f0
LP
695
696#ifdef HAVE_XZ
697 if (f->compress &&
698 size >= COMPRESSION_SIZE_THRESHOLD) {
699 uint64_t rsize;
700
701 compressed = compress_blob(data, size, o->data.payload, &rsize);
702
703 if (compressed) {
704 o->object.size = htole64(offsetof(Object, data.payload) + rsize);
705 o->object.flags |= OBJECT_COMPRESSED;
706
707 f->header->incompatible_flags = htole32(le32toh(f->header->incompatible_flags) | HEADER_INCOMPATIBLE_COMPRESSED);
708
709 log_debug("Compressed data object %lu -> %lu", (unsigned long) size, (unsigned long) rsize);
710 }
711 }
712#endif
713
714 if (!compressed)
715 memcpy(o->data.payload, data, size);
cec736d2 716
de190aef 717 r = journal_file_link_data(f, o, p, hash);
cec736d2
LP
718 if (r < 0)
719 return r;
720
721 if (ret)
722 *ret = o;
723
724 if (offset)
de190aef 725 *offset = p;
cec736d2
LP
726
727 return 0;
728}
729
730uint64_t journal_file_entry_n_items(Object *o) {
731 assert(o);
732 assert(o->object.type == htole64(OBJECT_ENTRY));
733
734 return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
735}
736
de190aef
LP
737static uint64_t journal_file_entry_array_n_items(Object *o) {
738 assert(o);
739 assert(o->object.type == htole64(OBJECT_ENTRY_ARRAY));
740
741 return (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / sizeof(uint64_t);
742}
743
744static int link_entry_into_array(JournalFile *f,
745 uint64_t *first,
746 uint64_t *idx,
747 uint64_t p) {
cec736d2 748 int r;
de190aef
LP
749 uint64_t n = 0, ap = 0, q, i, a, hidx;
750 Object *o;
751
cec736d2 752 assert(f);
de190aef
LP
753 assert(first);
754 assert(idx);
755 assert(p > 0);
cec736d2 756
de190aef
LP
757 a = le64toh(*first);
758 i = hidx = le64toh(*idx);
759 while (a > 0) {
760
761 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
762 if (r < 0)
763 return r;
cec736d2 764
de190aef
LP
765 n = journal_file_entry_array_n_items(o);
766 if (i < n) {
767 o->entry_array.items[i] = htole64(p);
768 *idx = htole64(hidx + 1);
769 return 0;
770 }
cec736d2 771
de190aef
LP
772 i -= n;
773 ap = a;
774 a = le64toh(o->entry_array.next_entry_array_offset);
775 }
776
777 if (hidx > n)
778 n = (hidx+1) * 2;
779 else
780 n = n * 2;
781
782 if (n < 4)
783 n = 4;
784
785 r = journal_file_append_object(f, OBJECT_ENTRY_ARRAY,
786 offsetof(Object, entry_array.items) + n * sizeof(uint64_t),
787 &o, &q);
cec736d2
LP
788 if (r < 0)
789 return r;
790
de190aef 791 o->entry_array.items[i] = htole64(p);
cec736d2 792
de190aef
LP
793 if (ap == 0)
794 *first = q;
cec736d2 795 else {
de190aef 796 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, ap, &o);
cec736d2
LP
797 if (r < 0)
798 return r;
799
de190aef
LP
800 o->entry_array.next_entry_array_offset = htole64(q);
801 }
cec736d2 802
de190aef
LP
803 *idx = htole64(hidx + 1);
804
805 return 0;
806}
cec736d2 807
de190aef
LP
808static int link_entry_into_array_plus_one(JournalFile *f,
809 uint64_t *extra,
810 uint64_t *first,
811 uint64_t *idx,
812 uint64_t p) {
813
814 int r;
815
816 assert(f);
817 assert(extra);
818 assert(first);
819 assert(idx);
820 assert(p > 0);
821
822 if (*idx == 0)
823 *extra = htole64(p);
824 else {
825 uint64_t i;
826
827 i = le64toh(*idx) - 1;
828 r = link_entry_into_array(f, first, &i, p);
829 if (r < 0)
830 return r;
cec736d2
LP
831 }
832
de190aef
LP
833 *idx = htole64(le64toh(*idx) + 1);
834 return 0;
835}
836
837static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
838 uint64_t p;
839 int r;
840 assert(f);
841 assert(o);
842 assert(offset > 0);
843
844 p = le64toh(o->entry.items[i].object_offset);
845 if (p == 0)
846 return -EINVAL;
847
848 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
cec736d2
LP
849 if (r < 0)
850 return r;
851
de190aef
LP
852 return link_entry_into_array_plus_one(f,
853 &o->data.entry_offset,
854 &o->data.entry_array_offset,
855 &o->data.n_entries,
856 offset);
cec736d2
LP
857}
858
859static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
de190aef 860 uint64_t n, i;
cec736d2
LP
861 int r;
862
863 assert(f);
864 assert(o);
865 assert(offset > 0);
de190aef 866 assert(o->object.type == OBJECT_ENTRY);
cec736d2
LP
867
868 /* Link up the entry itself */
de190aef
LP
869 r = link_entry_into_array(f,
870 &f->header->entry_array_offset,
871 &f->header->n_entries,
872 offset);
873 if (r < 0)
874 return r;
cec736d2 875
bc85bfee 876 log_error("=> %s seqnr=%lu n_entries=%lu", f->path, (unsigned long) o->entry.seqnum, (unsigned long) f->header->n_entries);
cec736d2 877
de190aef 878 if (f->header->head_entry_realtime == 0)
0ac38b70 879 f->header->head_entry_realtime = o->entry.realtime;
cec736d2 880
0ac38b70 881 f->header->tail_entry_realtime = o->entry.realtime;
de190aef
LP
882 f->header->tail_entry_monotonic = o->entry.monotonic;
883
884 f->tail_entry_monotonic_valid = true;
cec736d2
LP
885
886 /* Link up the items */
887 n = journal_file_entry_n_items(o);
888 for (i = 0; i < n; i++) {
889 r = journal_file_link_entry_item(f, o, offset, i);
890 if (r < 0)
891 return r;
892 }
893
cec736d2
LP
894 return 0;
895}
896
897static int journal_file_append_entry_internal(
898 JournalFile *f,
899 const dual_timestamp *ts,
900 uint64_t xor_hash,
901 const EntryItem items[], unsigned n_items,
de190aef 902 uint64_t *seqnum,
cec736d2
LP
903 Object **ret, uint64_t *offset) {
904 uint64_t np;
905 uint64_t osize;
906 Object *o;
907 int r;
908
909 assert(f);
910 assert(items || n_items == 0);
de190aef 911 assert(ts);
cec736d2
LP
912
913 osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
914
de190aef 915 r = journal_file_append_object(f, OBJECT_ENTRY, osize, &o, &np);
cec736d2
LP
916 if (r < 0)
917 return r;
918
de190aef 919 o->entry.seqnum = htole64(journal_file_seqnum(f, seqnum));
cec736d2 920 memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
de190aef
LP
921 o->entry.realtime = htole64(ts->realtime);
922 o->entry.monotonic = htole64(ts->monotonic);
cec736d2
LP
923 o->entry.xor_hash = htole64(xor_hash);
924 o->entry.boot_id = f->header->boot_id;
925
926 r = journal_file_link_entry(f, o, np);
927 if (r < 0)
928 return r;
929
930 if (ret)
931 *ret = o;
932
933 if (offset)
934 *offset = np;
935
936 return 0;
937}
938
50f20cfd
LP
939static void journal_file_post_change(JournalFile *f) {
940 assert(f);
941
942 /* inotify() does not receive IN_MODIFY events from file
943 * accesses done via mmap(). After each access we hence
944 * trigger IN_MODIFY by truncating the journal file to its
945 * current size which triggers IN_MODIFY. */
946
bc85bfee
LP
947 __sync_synchronize();
948
50f20cfd
LP
949 if (ftruncate(f->fd, f->last_stat.st_size) < 0)
950 log_error("Failed to to truncate file to its own size: %m");
951}
952
de190aef 953int 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
954 unsigned i;
955 EntryItem *items;
956 int r;
957 uint64_t xor_hash = 0;
de190aef 958 struct dual_timestamp _ts;
cec736d2
LP
959
960 assert(f);
961 assert(iovec || n_iovec == 0);
962
de190aef
LP
963 if (!f->writable)
964 return -EPERM;
965
966 if (!ts) {
967 dual_timestamp_get(&_ts);
968 ts = &_ts;
969 }
970
971 if (f->tail_entry_monotonic_valid &&
972 ts->monotonic < le64toh(f->header->tail_entry_monotonic))
973 return -EINVAL;
974
975 if (ts->realtime < le64toh(f->header->tail_entry_realtime))
976 return -EINVAL;
977
cec736d2
LP
978 items = new(EntryItem, n_iovec);
979 if (!items)
980 return -ENOMEM;
981
982 for (i = 0; i < n_iovec; i++) {
983 uint64_t p;
984 Object *o;
985
986 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
987 if (r < 0)
988 goto finish;
989
990 xor_hash ^= le64toh(o->data.hash);
991 items[i].object_offset = htole64(p);
de7b95cd 992 items[i].hash = o->data.hash;
cec736d2
LP
993 }
994
de190aef 995 r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset);
cec736d2 996
50f20cfd
LP
997 journal_file_post_change(f);
998
cec736d2
LP
999finish:
1000 free(items);
1001
1002 return r;
1003}
1004
de190aef
LP
1005static int generic_array_get(JournalFile *f,
1006 uint64_t first,
1007 uint64_t i,
1008 Object **ret, uint64_t *offset) {
1009
cec736d2 1010 Object *o;
de190aef 1011 uint64_t p, a;
cec736d2
LP
1012 int r;
1013
1014 assert(f);
1015
de190aef
LP
1016 a = first;
1017 while (a > 0) {
1018 uint64_t n;
cec736d2 1019
de190aef
LP
1020 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
1021 if (r < 0)
1022 return r;
cec736d2 1023
de190aef
LP
1024 n = journal_file_entry_array_n_items(o);
1025 if (i < n) {
1026 p = le64toh(o->entry_array.items[i]);
1027 break;
cec736d2
LP
1028 }
1029
de190aef
LP
1030 i -= n;
1031 a = le64toh(o->entry_array.next_entry_array_offset);
1032 }
1033
1034 if (a <= 0 || p <= 0)
1035 return 0;
1036
1037 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1038 if (r < 0)
1039 return r;
1040
1041 if (ret)
1042 *ret = o;
1043
1044 if (offset)
1045 *offset = p;
1046
1047 return 1;
1048}
1049
1050static int generic_array_get_plus_one(JournalFile *f,
1051 uint64_t extra,
1052 uint64_t first,
1053 uint64_t i,
1054 Object **ret, uint64_t *offset) {
1055
1056 Object *o;
1057
1058 assert(f);
1059
1060 if (i == 0) {
1061 int r;
1062
1063 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
cec736d2
LP
1064 if (r < 0)
1065 return r;
1066
de190aef
LP
1067 if (ret)
1068 *ret = o;
cec736d2 1069
de190aef
LP
1070 if (offset)
1071 *offset = extra;
cec736d2 1072
de190aef 1073 return 1;
cec736d2
LP
1074 }
1075
de190aef
LP
1076 return generic_array_get(f, first, i-1, ret, offset);
1077}
cec736d2 1078
de190aef
LP
1079enum {
1080 TEST_FOUND,
1081 TEST_LEFT,
1082 TEST_RIGHT
1083};
cec736d2 1084
de190aef
LP
1085static int generic_array_bisect(JournalFile *f,
1086 uint64_t first,
1087 uint64_t n,
1088 uint64_t needle,
1089 int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1090 direction_t direction,
1091 Object **ret,
1092 uint64_t *offset,
1093 uint64_t *idx) {
1094
1095 uint64_t a, p, t = 0, i = 0, last_p = 0;
1096 bool subtract_one = false;
1097 Object *o, *array = NULL;
1098 int r;
cec736d2 1099
de190aef
LP
1100 assert(f);
1101 assert(test_object);
cec736d2 1102
de190aef
LP
1103 a = first;
1104 while (a > 0) {
1105 uint64_t left, right, k, lp;
1106
1107 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &array);
cec736d2
LP
1108 if (r < 0)
1109 return r;
1110
de190aef
LP
1111 k = journal_file_entry_array_n_items(array);
1112 right = MIN(k, n);
1113 if (right <= 0)
1114 return 0;
cec736d2 1115
de190aef
LP
1116 i = right - 1;
1117 lp = p = le64toh(array->entry_array.items[i]);
1118 if (p <= 0)
1119 return -EBADMSG;
cec736d2 1120
de190aef
LP
1121 r = test_object(f, p, needle);
1122 if (r < 0)
1123 return r;
cec736d2 1124
de190aef
LP
1125 if (r == TEST_FOUND)
1126 r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1127
1128 if (r == TEST_RIGHT) {
1129 left = 0;
1130 right -= 1;
1131 for (;;) {
1132 if (left == right) {
1133 if (direction == DIRECTION_UP)
1134 subtract_one = true;
1135
1136 i = left;
1137 goto found;
1138 }
1139
1140 assert(left < right);
1141
1142 i = (left + right) / 2;
1143 p = le64toh(array->entry_array.items[i]);
1144 if (p <= 0)
1145 return -EBADMSG;
1146
1147 r = test_object(f, p, needle);
1148 if (r < 0)
1149 return r;
cec736d2 1150
de190aef
LP
1151 if (r == TEST_FOUND)
1152 r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1153
1154 if (r == TEST_RIGHT)
1155 right = i;
1156 else
1157 left = i + 1;
1158 }
1159 }
1160
1161 if (k > n)
cec736d2
LP
1162 return 0;
1163
de190aef
LP
1164 last_p = lp;
1165
1166 n -= k;
1167 t += k;
1168 a = le64toh(array->entry_array.next_entry_array_offset);
cec736d2
LP
1169 }
1170
1171 return 0;
de190aef
LP
1172
1173found:
1174 if (subtract_one && t == 0 && i == 0)
1175 return 0;
1176
1177 if (subtract_one && i == 0)
1178 p = last_p;
1179 else if (subtract_one)
1180 p = le64toh(array->entry_array.items[i-1]);
1181 else
1182 p = le64toh(array->entry_array.items[i]);
1183
1184 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1185 if (r < 0)
1186 return r;
1187
1188 if (ret)
1189 *ret = o;
1190
1191 if (offset)
1192 *offset = p;
1193
1194 if (idx)
1195 *idx = t + i - (subtract_one ? 1 : 0);
1196
1197 return 1;
cec736d2
LP
1198}
1199
de190aef
LP
1200static int generic_array_bisect_plus_one(JournalFile *f,
1201 uint64_t extra,
1202 uint64_t first,
1203 uint64_t n,
1204 uint64_t needle,
1205 int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1206 direction_t direction,
1207 Object **ret,
1208 uint64_t *offset,
1209 uint64_t *idx) {
1210
cec736d2
LP
1211 int r;
1212
1213 assert(f);
de190aef 1214 assert(test_object);
cec736d2 1215
de190aef
LP
1216 if (n <= 0)
1217 return 0;
cec736d2 1218
de190aef
LP
1219 /* This bisects the array in object 'first', but first checks
1220 * an extra */
1221
1222 r = test_object(f, extra, needle);
1223 if (r < 0)
1224 return r;
1225 else if (r == TEST_FOUND) {
1226 Object *o;
1227
1228 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
1229 if (r < 0)
1230 return r;
1231
1232 if (ret)
1233 *ret = o;
cec736d2 1234
de190aef
LP
1235 if (offset)
1236 *offset = extra;
1237 } else if (r == TEST_RIGHT)
cec736d2
LP
1238 return 0;
1239
de190aef
LP
1240 r = generic_array_bisect(f, first, n-1, needle, test_object, direction, ret, offset, idx);
1241
1242 if (r > 0)
1243 (*idx) ++;
1244
1245 return r;
1246}
1247
1248static int test_object_seqnum(JournalFile *f, uint64_t p, uint64_t needle) {
1249 Object *o;
1250 int r;
1251
1252 assert(f);
1253 assert(p > 0);
1254
1255 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
cec736d2
LP
1256 if (r < 0)
1257 return r;
1258
de190aef
LP
1259 if (le64toh(o->entry.seqnum) == needle)
1260 return TEST_FOUND;
1261 else if (le64toh(o->entry.seqnum) < needle)
1262 return TEST_LEFT;
1263 else
1264 return TEST_RIGHT;
1265}
cec736d2 1266
de190aef
LP
1267int journal_file_move_to_entry_by_seqnum(
1268 JournalFile *f,
1269 uint64_t seqnum,
1270 direction_t direction,
1271 Object **ret,
1272 uint64_t *offset) {
1273
1274 return generic_array_bisect(f,
1275 le64toh(f->header->entry_array_offset),
1276 le64toh(f->header->n_entries),
1277 seqnum,
1278 test_object_seqnum,
1279 direction,
1280 ret, offset, NULL);
1281}
cec736d2 1282
de190aef
LP
1283static int test_object_realtime(JournalFile *f, uint64_t p, uint64_t needle) {
1284 Object *o;
1285 int r;
1286
1287 assert(f);
1288 assert(p > 0);
1289
1290 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1291 if (r < 0)
1292 return r;
1293
1294 if (le64toh(o->entry.realtime) == needle)
1295 return TEST_FOUND;
1296 else if (le64toh(o->entry.realtime) < needle)
1297 return TEST_LEFT;
1298 else
1299 return TEST_RIGHT;
cec736d2
LP
1300}
1301
de190aef
LP
1302int journal_file_move_to_entry_by_realtime(
1303 JournalFile *f,
1304 uint64_t realtime,
1305 direction_t direction,
1306 Object **ret,
1307 uint64_t *offset) {
1308
1309 return generic_array_bisect(f,
1310 le64toh(f->header->entry_array_offset),
1311 le64toh(f->header->n_entries),
1312 realtime,
1313 test_object_realtime,
1314 direction,
1315 ret, offset, NULL);
1316}
1317
1318static int test_object_monotonic(JournalFile *f, uint64_t p, uint64_t needle) {
1319 Object *o;
1320 int r;
1321
1322 assert(f);
1323 assert(p > 0);
1324
1325 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1326 if (r < 0)
1327 return r;
1328
1329 if (le64toh(o->entry.monotonic) == needle)
1330 return TEST_FOUND;
1331 else if (le64toh(o->entry.monotonic) < needle)
1332 return TEST_LEFT;
1333 else
1334 return TEST_RIGHT;
1335}
1336
1337int journal_file_move_to_entry_by_monotonic(
1338 JournalFile *f,
1339 sd_id128_t boot_id,
1340 uint64_t monotonic,
1341 direction_t direction,
1342 Object **ret,
1343 uint64_t *offset) {
1344
1345 char t[8+32+1] = "_BOOT_ID=";
1346 Object *o;
1347 int r;
1348
1349 sd_id128_to_string(boot_id, t + 8);
1350
1351 r = journal_file_find_data_object(f, t, strlen(t), &o, NULL);
1352 if (r < 0)
1353 return r;
1354 else if (r == 0)
1355 return -ENOENT;
1356
1357 return generic_array_bisect_plus_one(f,
1358 le64toh(o->data.entry_offset),
1359 le64toh(o->data.entry_array_offset),
1360 le64toh(o->data.n_entries),
1361 monotonic,
1362 test_object_monotonic,
1363 direction,
1364 ret, offset, NULL);
1365}
1366
1367static int test_object_offset(JournalFile *f, uint64_t p, uint64_t needle) {
1368 assert(f);
1369 assert(p > 0);
1370
1371 if (p == needle)
1372 return TEST_FOUND;
1373 else if (p < needle)
1374 return TEST_LEFT;
1375 else
1376 return TEST_RIGHT;
1377}
1378
1379int journal_file_next_entry(
1380 JournalFile *f,
1381 Object *o, uint64_t p,
1382 direction_t direction,
1383 Object **ret, uint64_t *offset) {
1384
1385 uint64_t i, n;
cec736d2
LP
1386 int r;
1387
1388 assert(f);
de190aef
LP
1389 assert(p > 0 || !o);
1390
1391 n = le64toh(f->header->n_entries);
1392 if (n <= 0)
1393 return 0;
cec736d2
LP
1394
1395 if (!o)
de190aef 1396 i = direction == DIRECTION_DOWN ? 0 : n - 1;
cec736d2 1397 else {
de190aef 1398 if (o->object.type != OBJECT_ENTRY)
cec736d2
LP
1399 return -EINVAL;
1400
de190aef
LP
1401 r = generic_array_bisect(f,
1402 le64toh(f->header->entry_array_offset),
1403 le64toh(f->header->n_entries),
1404 p,
1405 test_object_offset,
1406 DIRECTION_DOWN,
1407 NULL, NULL,
1408 &i);
1409 if (r <= 0)
1410 return r;
1411
1412 if (direction == DIRECTION_DOWN) {
1413 if (i >= n - 1)
1414 return 0;
1415
1416 i++;
1417 } else {
1418 if (i <= 0)
1419 return 0;
1420
1421 i--;
1422 }
cec736d2
LP
1423 }
1424
de190aef
LP
1425 /* And jump to it */
1426 return generic_array_get(f,
1427 le64toh(f->header->entry_array_offset),
1428 i,
1429 ret, offset);
1430}
cec736d2 1431
de190aef
LP
1432int journal_file_skip_entry(
1433 JournalFile *f,
1434 Object *o, uint64_t p,
1435 int64_t skip,
1436 Object **ret, uint64_t *offset) {
1437
1438 uint64_t i, n;
1439 int r;
1440
1441 assert(f);
1442 assert(o);
1443 assert(p > 0);
1444
1445 if (o->object.type != OBJECT_ENTRY)
1446 return -EINVAL;
1447
1448 r = generic_array_bisect(f,
1449 le64toh(f->header->entry_array_offset),
1450 le64toh(f->header->n_entries),
1451 p,
1452 test_object_offset,
1453 DIRECTION_DOWN,
1454 NULL, NULL,
1455 &i);
1456 if (r <= 0)
cec736d2
LP
1457 return r;
1458
de190aef
LP
1459 /* Calculate new index */
1460 if (skip < 0) {
1461 if ((uint64_t) -skip >= i)
1462 i = 0;
1463 else
1464 i = i - (uint64_t) -skip;
1465 } else
1466 i += (uint64_t) skip;
cec736d2 1467
de190aef
LP
1468 n = le64toh(f->header->n_entries);
1469 if (n <= 0)
1470 return -EBADMSG;
cec736d2 1471
de190aef
LP
1472 if (i >= n)
1473 i = n-1;
1474
1475 return generic_array_get(f,
1476 le64toh(f->header->entry_array_offset),
1477 i,
1478 ret, offset);
cec736d2
LP
1479}
1480
de190aef
LP
1481int journal_file_next_entry_for_data(
1482 JournalFile *f,
1483 Object *o, uint64_t p,
1484 uint64_t data_offset,
1485 direction_t direction,
1486 Object **ret, uint64_t *offset) {
1487
1488 uint64_t n, i;
cec736d2 1489 int r;
de190aef 1490 Object *d;
cec736d2
LP
1491
1492 assert(f);
de190aef 1493 assert(p > 0 || !o);
cec736d2 1494
de190aef 1495 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
466ccd92 1496 if (r < 0)
de190aef 1497 return r;
cec736d2 1498
de190aef
LP
1499 n = le64toh(d->data.n_entries);
1500 if (n <= 0)
1501 return n;
cec736d2 1502
de190aef
LP
1503 if (!o)
1504 i = direction == DIRECTION_DOWN ? 0 : n - 1;
1505 else {
1506 if (o->object.type != OBJECT_ENTRY)
1507 return -EINVAL;
cec736d2 1508
de190aef
LP
1509 r = generic_array_bisect_plus_one(f,
1510 le64toh(d->data.entry_offset),
1511 le64toh(d->data.entry_array_offset),
1512 le64toh(d->data.n_entries),
1513 p,
1514 test_object_offset,
1515 DIRECTION_DOWN,
1516 NULL, NULL,
1517 &i);
1518
1519 if (r <= 0)
cec736d2
LP
1520 return r;
1521
de190aef
LP
1522 if (direction == DIRECTION_DOWN) {
1523 if (i >= n - 1)
1524 return 0;
cec736d2 1525
de190aef
LP
1526 i++;
1527 } else {
1528 if (i <= 0)
1529 return 0;
cec736d2 1530
de190aef
LP
1531 i--;
1532 }
cec736d2 1533
de190aef 1534 }
cec736d2 1535
de190aef
LP
1536 return generic_array_get_plus_one(f,
1537 le64toh(d->data.entry_offset),
1538 le64toh(d->data.entry_array_offset),
1539 i,
1540 ret, offset);
1541}
cec736d2 1542
de190aef
LP
1543int journal_file_move_to_entry_by_seqnum_for_data(
1544 JournalFile *f,
1545 uint64_t data_offset,
1546 uint64_t seqnum,
1547 direction_t direction,
1548 Object **ret, uint64_t *offset) {
cec736d2 1549
de190aef
LP
1550 Object *d;
1551 int r;
cec736d2 1552
de190aef
LP
1553 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1554 if (r <= 0)
1555 return r;
cec736d2 1556
de190aef
LP
1557 return generic_array_bisect_plus_one(f,
1558 le64toh(d->data.entry_offset),
1559 le64toh(d->data.entry_array_offset),
1560 le64toh(d->data.n_entries),
1561 seqnum,
1562 test_object_seqnum,
1563 direction,
1564 ret, offset, NULL);
1565}
cec736d2 1566
de190aef
LP
1567int journal_file_move_to_entry_by_realtime_for_data(
1568 JournalFile *f,
1569 uint64_t data_offset,
1570 uint64_t realtime,
1571 direction_t direction,
1572 Object **ret, uint64_t *offset) {
1573
1574 Object *d;
1575 int r;
1576
1577 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1578 if (r <= 0)
1579 return r;
1580
1581 return generic_array_bisect_plus_one(f,
1582 le64toh(d->data.entry_offset),
1583 le64toh(d->data.entry_array_offset),
1584 le64toh(d->data.n_entries),
1585 realtime,
1586 test_object_realtime,
1587 direction,
1588 ret, offset, NULL);
cec736d2
LP
1589}
1590
1591void journal_file_dump(JournalFile *f) {
1592 char a[33], b[33], c[33];
1593 Object *o;
1594 int r;
1595 uint64_t p;
1596
1597 assert(f);
1598
de190aef
LP
1599 printf("File Path: %s\n"
1600 "File ID: %s\n"
cec736d2
LP
1601 "Machine ID: %s\n"
1602 "Boot ID: %s\n"
de190aef
LP
1603 "Arena size: %llu\n"
1604 "Objects: %lu\n"
1605 "Entries: %lu\n",
1606 f->path,
cec736d2
LP
1607 sd_id128_to_string(f->header->file_id, a),
1608 sd_id128_to_string(f->header->machine_id, b),
1609 sd_id128_to_string(f->header->boot_id, c),
de190aef
LP
1610 (unsigned long long) le64toh(f->header->arena_size),
1611 (unsigned long) le64toh(f->header->n_objects),
1612 (unsigned long) le64toh(f->header->n_entries));
cec736d2 1613
de190aef 1614 p = le64toh(f->header->arena_offset);
cec736d2 1615 while (p != 0) {
de190aef 1616 r = journal_file_move_to_object(f, -1, p, &o);
cec736d2
LP
1617 if (r < 0)
1618 goto fail;
1619
1620 switch (o->object.type) {
1621
1622 case OBJECT_UNUSED:
1623 printf("Type: OBJECT_UNUSED\n");
1624 break;
1625
1626 case OBJECT_DATA:
1627 printf("Type: OBJECT_DATA\n");
1628 break;
1629
1630 case OBJECT_ENTRY:
3fbf9cbb
LP
1631 printf("Type: OBJECT_ENTRY %llu %llu %llu\n",
1632 (unsigned long long) le64toh(o->entry.seqnum),
1633 (unsigned long long) le64toh(o->entry.monotonic),
1634 (unsigned long long) le64toh(o->entry.realtime));
cec736d2
LP
1635 break;
1636
de190aef
LP
1637 case OBJECT_FIELD_HASH_TABLE:
1638 printf("Type: OBJECT_FIELD_HASH_TABLE\n");
cec736d2
LP
1639 break;
1640
de190aef
LP
1641 case OBJECT_DATA_HASH_TABLE:
1642 printf("Type: OBJECT_DATA_HASH_TABLE\n");
1643 break;
1644
1645 case OBJECT_ENTRY_ARRAY:
1646 printf("Type: OBJECT_ENTRY_ARRAY\n");
cec736d2
LP
1647 break;
1648 }
1649
807e17f0
LP
1650 if (o->object.flags & OBJECT_COMPRESSED)
1651 printf("Flags: COMPRESSED\n");
1652
cec736d2
LP
1653 if (p == le64toh(f->header->tail_object_offset))
1654 p = 0;
1655 else
1656 p = p + ALIGN64(le64toh(o->object.size));
1657 }
1658
1659 return;
1660fail:
1661 log_error("File corrupt");
1662}
1663
1664int journal_file_open(
1665 const char *fname,
1666 int flags,
1667 mode_t mode,
0ac38b70 1668 JournalFile *template,
cec736d2
LP
1669 JournalFile **ret) {
1670
1671 JournalFile *f;
1672 int r;
1673 bool newly_created = false;
1674
1675 assert(fname);
1676
1677 if ((flags & O_ACCMODE) != O_RDONLY &&
1678 (flags & O_ACCMODE) != O_RDWR)
1679 return -EINVAL;
1680
1681 f = new0(JournalFile, 1);
1682 if (!f)
1683 return -ENOMEM;
1684
0ac38b70
LP
1685 f->fd = -1;
1686 f->flags = flags;
1687 f->mode = mode;
cec736d2
LP
1688 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1689 f->prot = prot_from_flags(flags);
1690
bc85bfee
LP
1691 f->metrics.max_size = DEFAULT_MAX_SIZE;
1692 f->metrics.min_size = DEFAULT_MIN_SIZE;
1693 f->metrics.keep_free = DEFAULT_KEEP_FREE;
1694
cec736d2
LP
1695 f->path = strdup(fname);
1696 if (!f->path) {
1697 r = -ENOMEM;
1698 goto fail;
1699 }
1700
0ac38b70
LP
1701 f->fd = open(f->path, f->flags|O_CLOEXEC, f->mode);
1702 if (f->fd < 0) {
1703 r = -errno;
1704 goto fail;
1705 }
1706
cec736d2
LP
1707 if (fstat(f->fd, &f->last_stat) < 0) {
1708 r = -errno;
1709 goto fail;
1710 }
1711
1712 if (f->last_stat.st_size == 0 && f->writable) {
1713 newly_created = true;
1714
0ac38b70 1715 r = journal_file_init_header(f, template);
cec736d2
LP
1716 if (r < 0)
1717 goto fail;
1718
1719 if (fstat(f->fd, &f->last_stat) < 0) {
1720 r = -errno;
1721 goto fail;
1722 }
1723 }
1724
1725 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1726 r = -EIO;
1727 goto fail;
1728 }
1729
1730 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1731 if (f->header == MAP_FAILED) {
1732 f->header = NULL;
1733 r = -errno;
1734 goto fail;
1735 }
1736
1737 if (!newly_created) {
1738 r = journal_file_verify_header(f);
1739 if (r < 0)
1740 goto fail;
1741 }
1742
1743 if (f->writable) {
1744 r = journal_file_refresh_header(f);
1745 if (r < 0)
1746 goto fail;
1747 }
1748
1749 if (newly_created) {
1750
de190aef 1751 r = journal_file_setup_field_hash_table(f);
cec736d2
LP
1752 if (r < 0)
1753 goto fail;
1754
de190aef 1755 r = journal_file_setup_data_hash_table(f);
cec736d2
LP
1756 if (r < 0)
1757 goto fail;
1758 }
1759
de190aef 1760 r = journal_file_map_field_hash_table(f);
cec736d2
LP
1761 if (r < 0)
1762 goto fail;
1763
de190aef 1764 r = journal_file_map_data_hash_table(f);
cec736d2
LP
1765 if (r < 0)
1766 goto fail;
1767
1768 if (ret)
1769 *ret = f;
1770
1771 return 0;
1772
1773fail:
1774 journal_file_close(f);
1775
1776 return r;
1777}
0ac38b70
LP
1778
1779int journal_file_rotate(JournalFile **f) {
1780 char *p;
1781 size_t l;
1782 JournalFile *old_file, *new_file = NULL;
1783 int r;
1784
1785 assert(f);
1786 assert(*f);
1787
1788 old_file = *f;
1789
1790 if (!old_file->writable)
1791 return -EINVAL;
1792
1793 if (!endswith(old_file->path, ".journal"))
1794 return -EINVAL;
1795
1796 l = strlen(old_file->path);
1797
1798 p = new(char, l + 1 + 16 + 1 + 32 + 1 + 16 + 1);
1799 if (!p)
1800 return -ENOMEM;
1801
1802 memcpy(p, old_file->path, l - 8);
1803 p[l-8] = '@';
1804 sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1);
1805 snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
1806 "-%016llx-%016llx.journal",
1807 (unsigned long long) le64toh((*f)->header->seqnum),
1808 (unsigned long long) le64toh((*f)->header->tail_entry_realtime));
1809
1810 r = rename(old_file->path, p);
1811 free(p);
1812
1813 if (r < 0)
1814 return -errno;
1815
1816 old_file->header->state = le32toh(STATE_ARCHIVED);
1817
1818 r = journal_file_open(old_file->path, old_file->flags, old_file->mode, old_file, &new_file);
1819 journal_file_close(old_file);
1820
1821 *f = new_file;
1822 return r;
1823}
1824
1825struct vacuum_info {
1826 off_t usage;
1827 char *filename;
1828
1829 uint64_t realtime;
1830 sd_id128_t seqnum_id;
1831 uint64_t seqnum;
1832};
1833
1834static int vacuum_compare(const void *_a, const void *_b) {
1835 const struct vacuum_info *a, *b;
1836
1837 a = _a;
1838 b = _b;
1839
1840 if (sd_id128_equal(a->seqnum_id, b->seqnum_id)) {
1841 if (a->seqnum < b->seqnum)
1842 return -1;
1843 else if (a->seqnum > b->seqnum)
1844 return 1;
1845 else
1846 return 0;
1847 }
1848
1849 if (a->realtime < b->realtime)
1850 return -1;
1851 else if (a->realtime > b->realtime)
1852 return 1;
1853 else
1854 return memcmp(&a->seqnum_id, &b->seqnum_id, 16);
1855}
1856
1857int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t min_free) {
1858 DIR *d;
1859 int r = 0;
1860 struct vacuum_info *list = NULL;
1861 unsigned n_list = 0, n_allocated = 0, i;
1862 uint64_t sum = 0;
1863
1864 assert(directory);
1865
1866 if (max_use <= 0)
1867 max_use = DEFAULT_MAX_USE;
1868
1869 d = opendir(directory);
1870 if (!d)
1871 return -errno;
1872
1873 for (;;) {
1874 int k;
1875 struct dirent buf, *de;
1876 size_t q;
1877 struct stat st;
1878 char *p;
1879 unsigned long long seqnum, realtime;
1880 sd_id128_t seqnum_id;
1881
1882 k = readdir_r(d, &buf, &de);
1883 if (k != 0) {
1884 r = -k;
1885 goto finish;
1886 }
1887
1888 if (!de)
1889 break;
1890
1891 if (!dirent_is_file_with_suffix(de, ".journal"))
1892 continue;
1893
1894 q = strlen(de->d_name);
1895
1896 if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8)
1897 continue;
1898
1899 if (de->d_name[q-8-16-1] != '-' ||
1900 de->d_name[q-8-16-1-16-1] != '-' ||
1901 de->d_name[q-8-16-1-16-1-32-1] != '@')
1902 continue;
1903
1904 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
1905 continue;
1906
1907 if (!S_ISREG(st.st_mode))
1908 continue;
1909
1910 p = strdup(de->d_name);
1911 if (!p) {
1912 r = -ENOMEM;
1913 goto finish;
1914 }
1915
1916 de->d_name[q-8-16-1-16-1] = 0;
1917 if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) {
1918 free(p);
1919 continue;
1920 }
1921
1922 if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) {
1923 free(p);
1924 continue;
1925 }
1926
1927 if (n_list >= n_allocated) {
1928 struct vacuum_info *j;
1929
1930 n_allocated = MAX(n_allocated * 2U, 8U);
1931 j = realloc(list, n_allocated * sizeof(struct vacuum_info));
1932 if (!j) {
1933 free(p);
1934 r = -ENOMEM;
1935 goto finish;
1936 }
1937
1938 list = j;
1939 }
1940
1941 list[n_list].filename = p;
1942 list[n_list].usage = (uint64_t) st.st_blksize * (uint64_t) st.st_blocks;
1943 list[n_list].seqnum = seqnum;
1944 list[n_list].realtime = realtime;
1945 list[n_list].seqnum_id = seqnum_id;
1946
1947 sum += list[n_list].usage;
1948
1949 n_list ++;
1950 }
1951
1952 qsort(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
1953
1954 for(i = 0; i < n_list; i++) {
1955 struct statvfs ss;
1956
1957 if (fstatvfs(dirfd(d), &ss) < 0) {
1958 r = -errno;
1959 goto finish;
1960 }
1961
1962 if (sum <= max_use &&
1963 (uint64_t) ss.f_bavail * (uint64_t) ss.f_bsize >= min_free)
1964 break;
1965
1966 if (unlinkat(dirfd(d), list[i].filename, 0) >= 0) {
1967 log_debug("Deleted archived journal %s/%s.", directory, list[i].filename);
1968 sum -= list[i].usage;
1969 } else if (errno != ENOENT)
1970 log_warning("Failed to delete %s/%s: %m", directory, list[i].filename);
1971 }
1972
1973finish:
1974 for (i = 0; i < n_list; i++)
1975 free(list[i].filename);
1976
1977 free(list);
1978
de190aef
LP
1979 if (d)
1980 closedir(d);
1981
0ac38b70
LP
1982 return r;
1983}