1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
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.
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.
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/>.
26 #include <sys/statvfs.h>
30 #include "journal-def.h"
31 #include "journal-file.h"
34 #define DEFAULT_ARENA_MAX_SIZE (16ULL*1024ULL*1024ULL*1024ULL)
35 #define DEFAULT_ARENA_MIN_SIZE (256ULL*1024ULL)
36 #define DEFAULT_ARENA_KEEP_FREE (1ULL*1024ULL*1024ULL)
38 #define DEFAULT_MAX_USE (16ULL*1024ULL*1024ULL*16ULL)
40 #define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*16ULL)
41 #define DEFAULT_FIELD_HASH_TABLE_SIZE (2047ULL*16ULL)
43 #define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
45 static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
47 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
49 void journal_file_close(JournalFile *f) {
54 if (f->header && f->writable)
55 f->header->state = STATE_OFFLINE;
58 for (t = 0; t < _WINDOW_MAX; t++)
59 if (f->windows[t].ptr)
60 munmap(f->windows[t].ptr, f->windows[t].size);
63 close_nointr_nofail(f->fd);
69 static int journal_file_init_header(JournalFile *f, JournalFile *template) {
77 memcpy(h.signature, signature, 8);
78 h.arena_offset = htole64(ALIGN64(sizeof(h)));
79 h.arena_max_size = htole64(DEFAULT_ARENA_MAX_SIZE);
80 h.arena_min_size = htole64(DEFAULT_ARENA_MIN_SIZE);
81 h.arena_keep_free = htole64(DEFAULT_ARENA_KEEP_FREE);
83 r = sd_id128_randomize(&h.file_id);
88 h.seqnum_id = template->header->seqnum_id;
89 h.seqnum = template->header->seqnum;
91 h.seqnum_id = h.file_id;
93 k = pwrite(f->fd, &h, sizeof(h), 0);
103 static int journal_file_refresh_header(JournalFile *f) {
109 r = sd_id128_get_machine(&f->header->machine_id);
113 r = sd_id128_get_boot(&boot_id);
117 if (sd_id128_equal(boot_id, f->header->boot_id))
118 f->tail_entry_monotonic_valid = true;
120 f->header->boot_id = boot_id;
122 f->header->state = STATE_ONLINE;
126 static int journal_file_verify_header(JournalFile *f) {
129 if (memcmp(f->header, signature, 8))
132 if (f->header->incompatible_flags != 0)
133 return -EPROTONOSUPPORT;
135 if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
140 sd_id128_t machine_id;
143 r = sd_id128_get_machine(&machine_id);
147 if (!sd_id128_equal(machine_id, f->header->machine_id))
150 state = f->header->state;
152 if (state == STATE_ONLINE)
153 log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f->path);
154 else if (state == STATE_ARCHIVED)
156 else if (state != STATE_OFFLINE)
157 log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
163 static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
165 uint64_t old_size, new_size;
169 if (offset < le64toh(f->header->arena_offset))
172 new_size = PAGE_ALIGN(offset + size);
174 /* We assume that this file is not sparse, and we know that
175 * for sure, since we always call posix_fallocate()
179 le64toh(f->header->arena_offset) +
180 le64toh(f->header->arena_size);
182 if (old_size >= new_size)
185 asize = new_size - le64toh(f->header->arena_offset);
187 if (asize > le64toh(f->header->arena_min_size)) {
190 if (fstatvfs(f->fd, &svfs) >= 0) {
193 available = svfs.f_bfree * svfs.f_bsize;
195 if (available >= f->header->arena_keep_free)
196 available -= f->header->arena_keep_free;
200 if (new_size - old_size > available)
205 if (asize > le64toh(f->header->arena_max_size))
208 if (posix_fallocate(f->fd, old_size, new_size - old_size) < 0)
211 if (fstat(f->fd, &f->last_stat) < 0)
214 f->header->arena_size = htole64(asize);
219 static int journal_file_map(
228 uint64_t woffset, wsize;
235 woffset = offset & ~((uint64_t) page_size() - 1ULL);
236 wsize = size + (offset - woffset);
237 wsize = PAGE_ALIGN(wsize);
239 window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
240 if (window == MAP_FAILED)
252 *ret = (uint8_t*) window + (offset - woffset);
257 static int journal_file_move_to(JournalFile *f, int wt, uint64_t offset, uint64_t size, void **ret) {
266 assert(wt < _WINDOW_MAX);
270 if (_likely_(w->ptr &&
271 w->offset <= offset &&
272 w->offset + w->size >= offset + size)) {
274 *ret = (uint8_t*) w->ptr + (offset - w->offset);
279 if (munmap(w->ptr, w->size) < 0)
283 w->size = w->offset = 0;
286 if (size < DEFAULT_WINDOW_SIZE) {
287 /* If the default window size is larger then what was
288 * asked for extend the mapping a bit in the hope to
289 * minimize needed remappings later on. We add half
290 * the window space before and half behind the
291 * requested mapping */
293 delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
299 size += (DEFAULT_WINDOW_SIZE - delta);
303 r = journal_file_map(f,
305 &w->ptr, &w->offset, &w->size,
311 *ret = (uint8_t*) p + delta;
315 static bool verify_hash(Object *o) {
320 if (o->object.type == OBJECT_DATA) {
321 h1 = le64toh(o->data.hash);
322 h2 = hash64(o->data.payload, le64toh(o->object.size) - offsetof(Object, data.payload));
323 } else if (o->object.type == OBJECT_FIELD) {
324 h1 = le64toh(o->field.hash);
325 h2 = hash64(o->field.payload, le64toh(o->object.size) - offsetof(Object, field.payload));
332 int journal_file_move_to_object(JournalFile *f, int type, uint64_t offset, Object **ret) {
340 assert(type < _OBJECT_TYPE_MAX);
342 r = journal_file_move_to(f, type >= 0 ? type : WINDOW_UNKNOWN, offset, sizeof(ObjectHeader), &t);
347 s = le64toh(o->object.size);
349 if (s < sizeof(ObjectHeader))
352 if (type >= 0 && o->object.type != type)
355 if (s > sizeof(ObjectHeader)) {
356 r = journal_file_move_to(f, o->object.type, offset, s, &t);
370 static uint64_t journal_file_seqnum(JournalFile *f, uint64_t *seqnum) {
375 r = le64toh(f->header->seqnum) + 1;
378 /* If an external seqnum counter was passed, we update
379 * both the local and the external one, and set it to
380 * the maximum of both */
388 f->header->seqnum = htole64(r);
390 if (f->header->first_seqnum == 0)
391 f->header->first_seqnum = htole64(r);
396 static int journal_file_append_object(JournalFile *f, int type, uint64_t size, Object **ret, uint64_t *offset) {
403 assert(size >= sizeof(ObjectHeader));
407 p = le64toh(f->header->tail_object_offset);
409 p = le64toh(f->header->arena_offset);
411 r = journal_file_move_to_object(f, -1, p, &tail);
415 p += ALIGN64(le64toh(tail->object.size));
418 r = journal_file_allocate(f, p, size);
422 r = journal_file_move_to(f, type, p, size, &t);
429 o->object.type = type;
430 o->object.size = htole64(size);
432 f->header->tail_object_offset = htole64(p);
433 f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
441 static int journal_file_setup_data_hash_table(JournalFile *f) {
448 s = DEFAULT_DATA_HASH_TABLE_SIZE;
449 r = journal_file_append_object(f,
450 OBJECT_DATA_HASH_TABLE,
451 offsetof(Object, hash_table.items) + s,
456 memset(o->hash_table.items, 0, s);
458 f->header->data_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
459 f->header->data_hash_table_size = htole64(s);
464 static int journal_file_setup_field_hash_table(JournalFile *f) {
471 s = DEFAULT_FIELD_HASH_TABLE_SIZE;
472 r = journal_file_append_object(f,
473 OBJECT_FIELD_HASH_TABLE,
474 offsetof(Object, hash_table.items) + s,
479 memset(o->hash_table.items, 0, s);
481 f->header->field_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
482 f->header->field_hash_table_size = htole64(s);
487 static int journal_file_map_data_hash_table(JournalFile *f) {
494 p = le64toh(f->header->data_hash_table_offset);
495 s = le64toh(f->header->data_hash_table_size);
497 r = journal_file_move_to(f,
498 WINDOW_DATA_HASH_TABLE,
504 f->data_hash_table = t;
508 static int journal_file_map_field_hash_table(JournalFile *f) {
515 p = le64toh(f->header->field_hash_table_offset);
516 s = le64toh(f->header->field_hash_table_size);
518 r = journal_file_move_to(f,
519 WINDOW_FIELD_HASH_TABLE,
525 f->field_hash_table = t;
529 static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash) {
536 assert(o->object.type == OBJECT_DATA);
538 o->data.next_hash_offset = o->data.next_field_offset = 0;
539 o->data.entry_offset = o->data.entry_array_offset = 0;
540 o->data.n_entries = 0;
542 h = hash % (le64toh(f->header->data_hash_table_size) / sizeof(HashItem));
543 p = le64toh(f->data_hash_table[h].head_hash_offset);
545 /* Only entry in the hash table is easy */
546 f->data_hash_table[h].head_hash_offset = htole64(offset);
548 /* Temporarily move back to the previous data object,
549 * to patch in pointer */
551 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
555 o->data.next_hash_offset = htole64(offset);
557 r = journal_file_move_to_object(f, OBJECT_DATA, offset, &o);
562 f->data_hash_table[h].tail_hash_offset = htole64(offset);
567 int journal_file_find_data_object_with_hash(
569 const void *data, uint64_t size, uint64_t hash,
570 Object **ret, uint64_t *offset) {
571 uint64_t p, osize, h;
575 assert(data || size == 0);
577 osize = offsetof(Object, data.payload) + size;
579 h = hash % (le64toh(f->header->data_hash_table_size) / sizeof(HashItem));
580 p = le64toh(f->data_hash_table[h].head_hash_offset);
585 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
589 if (le64toh(o->object.size) == osize &&
590 memcmp(o->data.payload, data, size) == 0) {
592 if (le64toh(o->data.hash) != hash)
604 p = le64toh(o->data.next_hash_offset);
610 int journal_file_find_data_object(
612 const void *data, uint64_t size,
613 Object **ret, uint64_t *offset) {
618 assert(data || size == 0);
620 hash = hash64(data, size);
622 return journal_file_find_data_object_with_hash(f,
627 static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
634 assert(data || size == 0);
636 hash = hash64(data, size);
638 r = journal_file_find_data_object_with_hash(f, data, size, hash, &o, &p);
652 osize = offsetof(Object, data.payload) + size;
653 r = journal_file_append_object(f, OBJECT_DATA, osize, &o, &p);
657 o->data.hash = htole64(hash);
658 memcpy(o->data.payload, data, size);
660 r = journal_file_link_data(f, o, p, hash);
673 uint64_t journal_file_entry_n_items(Object *o) {
675 assert(o->object.type == htole64(OBJECT_ENTRY));
677 return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
680 static uint64_t journal_file_entry_array_n_items(Object *o) {
682 assert(o->object.type == htole64(OBJECT_ENTRY_ARRAY));
684 return (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / sizeof(uint64_t);
687 static int link_entry_into_array(JournalFile *f,
692 uint64_t n = 0, ap = 0, q, i, a, hidx;
701 i = hidx = le64toh(*idx);
704 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
708 n = journal_file_entry_array_n_items(o);
710 o->entry_array.items[i] = htole64(p);
711 *idx = htole64(hidx + 1);
717 a = le64toh(o->entry_array.next_entry_array_offset);
728 r = journal_file_append_object(f, OBJECT_ENTRY_ARRAY,
729 offsetof(Object, entry_array.items) + n * sizeof(uint64_t),
734 o->entry_array.items[i] = htole64(p);
739 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, ap, &o);
743 o->entry_array.next_entry_array_offset = htole64(q);
746 *idx = htole64(hidx + 1);
751 static int link_entry_into_array_plus_one(JournalFile *f,
770 i = le64toh(*idx) - 1;
771 r = link_entry_into_array(f, first, &i, p);
776 *idx = htole64(le64toh(*idx) + 1);
780 static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
787 p = le64toh(o->entry.items[i].object_offset);
791 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
795 return link_entry_into_array_plus_one(f,
796 &o->data.entry_offset,
797 &o->data.entry_array_offset,
802 static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
809 assert(o->object.type == OBJECT_ENTRY);
811 /* Link up the entry itself */
812 r = link_entry_into_array(f,
813 &f->header->entry_array_offset,
814 &f->header->n_entries,
819 log_error("%s %lu", f->path, (unsigned long) f->header->n_entries);
821 if (f->header->head_entry_realtime == 0)
822 f->header->head_entry_realtime = o->entry.realtime;
824 f->header->tail_entry_realtime = o->entry.realtime;
825 f->header->tail_entry_monotonic = o->entry.monotonic;
827 f->tail_entry_monotonic_valid = true;
829 /* Link up the items */
830 n = journal_file_entry_n_items(o);
831 for (i = 0; i < n; i++) {
832 r = journal_file_link_entry_item(f, o, offset, i);
840 static int journal_file_append_entry_internal(
842 const dual_timestamp *ts,
844 const EntryItem items[], unsigned n_items,
846 Object **ret, uint64_t *offset) {
853 assert(items || n_items == 0);
856 osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
858 r = journal_file_append_object(f, OBJECT_ENTRY, osize, &o, &np);
862 o->entry.seqnum = htole64(journal_file_seqnum(f, seqnum));
863 memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
864 o->entry.realtime = htole64(ts->realtime);
865 o->entry.monotonic = htole64(ts->monotonic);
866 o->entry.xor_hash = htole64(xor_hash);
867 o->entry.boot_id = f->header->boot_id;
869 r = journal_file_link_entry(f, o, np);
882 int 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) {
886 uint64_t xor_hash = 0;
887 struct dual_timestamp _ts;
890 assert(iovec || n_iovec == 0);
896 dual_timestamp_get(&_ts);
900 if (f->tail_entry_monotonic_valid &&
901 ts->monotonic < le64toh(f->header->tail_entry_monotonic))
904 if (ts->realtime < le64toh(f->header->tail_entry_realtime))
907 items = new(EntryItem, n_iovec);
911 for (i = 0; i < n_iovec; i++) {
915 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
919 xor_hash ^= le64toh(o->data.hash);
920 items[i].object_offset = htole64(p);
921 items[i].hash = o->data.hash;
924 r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset);
932 static int generic_array_get(JournalFile *f,
935 Object **ret, uint64_t *offset) {
947 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
951 n = journal_file_entry_array_n_items(o);
953 p = le64toh(o->entry_array.items[i]);
958 a = le64toh(o->entry_array.next_entry_array_offset);
961 if (a <= 0 || p <= 0)
964 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
977 static int generic_array_get_plus_one(JournalFile *f,
981 Object **ret, uint64_t *offset) {
990 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
1003 return generic_array_get(f, first, i-1, ret, offset);
1012 static int generic_array_bisect(JournalFile *f,
1016 int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1017 direction_t direction,
1022 uint64_t a, p, t = 0, i = 0, last_p = 0;
1023 bool subtract_one = false;
1024 Object *o, *array = NULL;
1028 assert(test_object);
1032 uint64_t left, right, k, lp;
1034 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &array);
1038 k = journal_file_entry_array_n_items(array);
1044 lp = p = le64toh(array->entry_array.items[i]);
1048 r = test_object(f, p, needle);
1052 if (r == TEST_FOUND)
1053 r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1055 if (r == TEST_RIGHT) {
1059 if (left == right) {
1060 if (direction == DIRECTION_UP)
1061 subtract_one = true;
1067 assert(left < right);
1069 i = (left + right) / 2;
1070 p = le64toh(array->entry_array.items[i]);
1074 r = test_object(f, p, needle);
1078 if (r == TEST_FOUND)
1079 r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1081 if (r == TEST_RIGHT)
1095 a = le64toh(array->entry_array.next_entry_array_offset);
1101 if (subtract_one && t == 0 && i == 0)
1104 if (subtract_one && i == 0)
1106 else if (subtract_one)
1107 p = le64toh(array->entry_array.items[i-1]);
1109 p = le64toh(array->entry_array.items[i]);
1111 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1122 *idx = t + i - (subtract_one ? 1 : 0);
1127 static int generic_array_bisect_plus_one(JournalFile *f,
1132 int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1133 direction_t direction,
1141 assert(test_object);
1146 /* This bisects the array in object 'first', but first checks
1149 r = test_object(f, extra, needle);
1152 else if (r == TEST_FOUND) {
1155 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
1164 } else if (r == TEST_RIGHT)
1167 r = generic_array_bisect(f, first, n-1, needle, test_object, direction, ret, offset, idx);
1175 static int test_object_seqnum(JournalFile *f, uint64_t p, uint64_t needle) {
1182 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1186 if (le64toh(o->entry.seqnum) == needle)
1188 else if (le64toh(o->entry.seqnum) < needle)
1194 int journal_file_move_to_entry_by_seqnum(
1197 direction_t direction,
1201 return generic_array_bisect(f,
1202 le64toh(f->header->entry_array_offset),
1203 le64toh(f->header->n_entries),
1210 static int test_object_realtime(JournalFile *f, uint64_t p, uint64_t needle) {
1217 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1221 if (le64toh(o->entry.realtime) == needle)
1223 else if (le64toh(o->entry.realtime) < needle)
1229 int journal_file_move_to_entry_by_realtime(
1232 direction_t direction,
1236 return generic_array_bisect(f,
1237 le64toh(f->header->entry_array_offset),
1238 le64toh(f->header->n_entries),
1240 test_object_realtime,
1245 static int test_object_monotonic(JournalFile *f, uint64_t p, uint64_t needle) {
1252 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1256 if (le64toh(o->entry.monotonic) == needle)
1258 else if (le64toh(o->entry.monotonic) < needle)
1264 int journal_file_move_to_entry_by_monotonic(
1268 direction_t direction,
1272 char t[8+32+1] = "_BOOT_ID=";
1276 sd_id128_to_string(boot_id, t + 8);
1278 r = journal_file_find_data_object(f, t, strlen(t), &o, NULL);
1284 return generic_array_bisect_plus_one(f,
1285 le64toh(o->data.entry_offset),
1286 le64toh(o->data.entry_array_offset),
1287 le64toh(o->data.n_entries),
1289 test_object_monotonic,
1294 static int test_object_offset(JournalFile *f, uint64_t p, uint64_t needle) {
1300 else if (p < needle)
1306 int journal_file_next_entry(
1308 Object *o, uint64_t p,
1309 direction_t direction,
1310 Object **ret, uint64_t *offset) {
1316 assert(p > 0 || !o);
1318 n = le64toh(f->header->n_entries);
1323 i = direction == DIRECTION_DOWN ? 0 : n - 1;
1325 if (o->object.type != OBJECT_ENTRY)
1328 r = generic_array_bisect(f,
1329 le64toh(f->header->entry_array_offset),
1330 le64toh(f->header->n_entries),
1339 if (direction == DIRECTION_DOWN) {
1352 /* And jump to it */
1353 return generic_array_get(f,
1354 le64toh(f->header->entry_array_offset),
1359 int journal_file_skip_entry(
1361 Object *o, uint64_t p,
1363 Object **ret, uint64_t *offset) {
1372 if (o->object.type != OBJECT_ENTRY)
1375 r = generic_array_bisect(f,
1376 le64toh(f->header->entry_array_offset),
1377 le64toh(f->header->n_entries),
1386 /* Calculate new index */
1388 if ((uint64_t) -skip >= i)
1391 i = i - (uint64_t) -skip;
1393 i += (uint64_t) skip;
1395 n = le64toh(f->header->n_entries);
1402 return generic_array_get(f,
1403 le64toh(f->header->entry_array_offset),
1408 int journal_file_next_entry_for_data(
1410 Object *o, uint64_t p,
1411 uint64_t data_offset,
1412 direction_t direction,
1413 Object **ret, uint64_t *offset) {
1420 assert(p > 0 || !o);
1422 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1426 n = le64toh(d->data.n_entries);
1431 i = direction == DIRECTION_DOWN ? 0 : n - 1;
1433 if (o->object.type != OBJECT_ENTRY)
1436 r = generic_array_bisect_plus_one(f,
1437 le64toh(d->data.entry_offset),
1438 le64toh(d->data.entry_array_offset),
1439 le64toh(d->data.n_entries),
1449 if (direction == DIRECTION_DOWN) {
1463 return generic_array_get_plus_one(f,
1464 le64toh(d->data.entry_offset),
1465 le64toh(d->data.entry_array_offset),
1470 int journal_file_move_to_entry_by_seqnum_for_data(
1472 uint64_t data_offset,
1474 direction_t direction,
1475 Object **ret, uint64_t *offset) {
1480 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1484 return generic_array_bisect_plus_one(f,
1485 le64toh(d->data.entry_offset),
1486 le64toh(d->data.entry_array_offset),
1487 le64toh(d->data.n_entries),
1494 int journal_file_move_to_entry_by_realtime_for_data(
1496 uint64_t data_offset,
1498 direction_t direction,
1499 Object **ret, uint64_t *offset) {
1504 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1508 return generic_array_bisect_plus_one(f,
1509 le64toh(d->data.entry_offset),
1510 le64toh(d->data.entry_array_offset),
1511 le64toh(d->data.n_entries),
1513 test_object_realtime,
1518 void journal_file_dump(JournalFile *f) {
1519 char a[33], b[33], c[33];
1526 printf("File Path: %s\n"
1530 "Arena size: %llu\n"
1534 sd_id128_to_string(f->header->file_id, a),
1535 sd_id128_to_string(f->header->machine_id, b),
1536 sd_id128_to_string(f->header->boot_id, c),
1537 (unsigned long long) le64toh(f->header->arena_size),
1538 (unsigned long) le64toh(f->header->n_objects),
1539 (unsigned long) le64toh(f->header->n_entries));
1541 p = le64toh(f->header->arena_offset);
1543 r = journal_file_move_to_object(f, -1, p, &o);
1547 switch (o->object.type) {
1550 printf("Type: OBJECT_UNUSED\n");
1554 printf("Type: OBJECT_DATA\n");
1558 printf("Type: OBJECT_ENTRY %llu %llu %llu\n",
1559 (unsigned long long) le64toh(o->entry.seqnum),
1560 (unsigned long long) le64toh(o->entry.monotonic),
1561 (unsigned long long) le64toh(o->entry.realtime));
1564 case OBJECT_FIELD_HASH_TABLE:
1565 printf("Type: OBJECT_FIELD_HASH_TABLE\n");
1568 case OBJECT_DATA_HASH_TABLE:
1569 printf("Type: OBJECT_DATA_HASH_TABLE\n");
1572 case OBJECT_ENTRY_ARRAY:
1573 printf("Type: OBJECT_ENTRY_ARRAY\n");
1577 if (p == le64toh(f->header->tail_object_offset))
1580 p = p + ALIGN64(le64toh(o->object.size));
1585 log_error("File corrupt");
1588 int journal_file_open(
1592 JournalFile *template,
1593 JournalFile **ret) {
1597 bool newly_created = false;
1601 if ((flags & O_ACCMODE) != O_RDONLY &&
1602 (flags & O_ACCMODE) != O_RDWR)
1605 f = new0(JournalFile, 1);
1612 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1613 f->prot = prot_from_flags(flags);
1615 f->path = strdup(fname);
1621 f->fd = open(f->path, f->flags|O_CLOEXEC, f->mode);
1627 if (fstat(f->fd, &f->last_stat) < 0) {
1632 if (f->last_stat.st_size == 0 && f->writable) {
1633 newly_created = true;
1635 r = journal_file_init_header(f, template);
1639 if (fstat(f->fd, &f->last_stat) < 0) {
1645 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1650 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1651 if (f->header == MAP_FAILED) {
1657 if (!newly_created) {
1658 r = journal_file_verify_header(f);
1664 r = journal_file_refresh_header(f);
1669 if (newly_created) {
1671 r = journal_file_setup_field_hash_table(f);
1675 r = journal_file_setup_data_hash_table(f);
1680 r = journal_file_map_field_hash_table(f);
1684 r = journal_file_map_data_hash_table(f);
1694 journal_file_close(f);
1699 int journal_file_rotate(JournalFile **f) {
1702 JournalFile *old_file, *new_file = NULL;
1710 if (!old_file->writable)
1713 if (!endswith(old_file->path, ".journal"))
1716 l = strlen(old_file->path);
1718 p = new(char, l + 1 + 16 + 1 + 32 + 1 + 16 + 1);
1722 memcpy(p, old_file->path, l - 8);
1724 sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1);
1725 snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
1726 "-%016llx-%016llx.journal",
1727 (unsigned long long) le64toh((*f)->header->seqnum),
1728 (unsigned long long) le64toh((*f)->header->tail_entry_realtime));
1730 r = rename(old_file->path, p);
1736 old_file->header->state = le32toh(STATE_ARCHIVED);
1738 r = journal_file_open(old_file->path, old_file->flags, old_file->mode, old_file, &new_file);
1739 journal_file_close(old_file);
1745 struct vacuum_info {
1750 sd_id128_t seqnum_id;
1754 static int vacuum_compare(const void *_a, const void *_b) {
1755 const struct vacuum_info *a, *b;
1760 if (sd_id128_equal(a->seqnum_id, b->seqnum_id)) {
1761 if (a->seqnum < b->seqnum)
1763 else if (a->seqnum > b->seqnum)
1769 if (a->realtime < b->realtime)
1771 else if (a->realtime > b->realtime)
1774 return memcmp(&a->seqnum_id, &b->seqnum_id, 16);
1777 int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t min_free) {
1780 struct vacuum_info *list = NULL;
1781 unsigned n_list = 0, n_allocated = 0, i;
1787 max_use = DEFAULT_MAX_USE;
1789 d = opendir(directory);
1795 struct dirent buf, *de;
1799 unsigned long long seqnum, realtime;
1800 sd_id128_t seqnum_id;
1802 k = readdir_r(d, &buf, &de);
1811 if (!dirent_is_file_with_suffix(de, ".journal"))
1814 q = strlen(de->d_name);
1816 if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8)
1819 if (de->d_name[q-8-16-1] != '-' ||
1820 de->d_name[q-8-16-1-16-1] != '-' ||
1821 de->d_name[q-8-16-1-16-1-32-1] != '@')
1824 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
1827 if (!S_ISREG(st.st_mode))
1830 p = strdup(de->d_name);
1836 de->d_name[q-8-16-1-16-1] = 0;
1837 if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) {
1842 if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) {
1847 if (n_list >= n_allocated) {
1848 struct vacuum_info *j;
1850 n_allocated = MAX(n_allocated * 2U, 8U);
1851 j = realloc(list, n_allocated * sizeof(struct vacuum_info));
1861 list[n_list].filename = p;
1862 list[n_list].usage = (uint64_t) st.st_blksize * (uint64_t) st.st_blocks;
1863 list[n_list].seqnum = seqnum;
1864 list[n_list].realtime = realtime;
1865 list[n_list].seqnum_id = seqnum_id;
1867 sum += list[n_list].usage;
1872 qsort(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
1874 for(i = 0; i < n_list; i++) {
1877 if (fstatvfs(dirfd(d), &ss) < 0) {
1882 if (sum <= max_use &&
1883 (uint64_t) ss.f_bavail * (uint64_t) ss.f_bsize >= min_free)
1886 if (unlinkat(dirfd(d), list[i].filename, 0) >= 0) {
1887 log_debug("Deleted archived journal %s/%s.", directory, list[i].filename);
1888 sum -= list[i].usage;
1889 } else if (errno != ENOENT)
1890 log_warning("Failed to delete %s/%s: %m", directory, list[i].filename);
1894 for (i = 0; i < n_list; i++)
1895 free(list[i].filename);