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 static void journal_file_post_change(JournalFile *f) {
885 /* inotify() does not receive IN_MODIFY events from file
886 * accesses done via mmap(). After each access we hence
887 * trigger IN_MODIFY by truncating the journal file to its
888 * current size which triggers IN_MODIFY. */
890 if (ftruncate(f->fd, f->last_stat.st_size) < 0)
891 log_error("Failed to to truncate file to its own size: %m");
894 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) {
898 uint64_t xor_hash = 0;
899 struct dual_timestamp _ts;
902 assert(iovec || n_iovec == 0);
908 dual_timestamp_get(&_ts);
912 if (f->tail_entry_monotonic_valid &&
913 ts->monotonic < le64toh(f->header->tail_entry_monotonic))
916 if (ts->realtime < le64toh(f->header->tail_entry_realtime))
919 items = new(EntryItem, n_iovec);
923 for (i = 0; i < n_iovec; i++) {
927 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
931 xor_hash ^= le64toh(o->data.hash);
932 items[i].object_offset = htole64(p);
933 items[i].hash = o->data.hash;
936 r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset);
938 journal_file_post_change(f);
946 static int generic_array_get(JournalFile *f,
949 Object **ret, uint64_t *offset) {
961 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
965 n = journal_file_entry_array_n_items(o);
967 p = le64toh(o->entry_array.items[i]);
972 a = le64toh(o->entry_array.next_entry_array_offset);
975 if (a <= 0 || p <= 0)
978 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
991 static int generic_array_get_plus_one(JournalFile *f,
995 Object **ret, uint64_t *offset) {
1004 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
1017 return generic_array_get(f, first, i-1, ret, offset);
1026 static int generic_array_bisect(JournalFile *f,
1030 int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1031 direction_t direction,
1036 uint64_t a, p, t = 0, i = 0, last_p = 0;
1037 bool subtract_one = false;
1038 Object *o, *array = NULL;
1042 assert(test_object);
1046 uint64_t left, right, k, lp;
1048 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &array);
1052 k = journal_file_entry_array_n_items(array);
1058 lp = p = le64toh(array->entry_array.items[i]);
1062 r = test_object(f, p, needle);
1066 if (r == TEST_FOUND)
1067 r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1069 if (r == TEST_RIGHT) {
1073 if (left == right) {
1074 if (direction == DIRECTION_UP)
1075 subtract_one = true;
1081 assert(left < right);
1083 i = (left + right) / 2;
1084 p = le64toh(array->entry_array.items[i]);
1088 r = test_object(f, p, needle);
1092 if (r == TEST_FOUND)
1093 r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1095 if (r == TEST_RIGHT)
1109 a = le64toh(array->entry_array.next_entry_array_offset);
1115 if (subtract_one && t == 0 && i == 0)
1118 if (subtract_one && i == 0)
1120 else if (subtract_one)
1121 p = le64toh(array->entry_array.items[i-1]);
1123 p = le64toh(array->entry_array.items[i]);
1125 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1136 *idx = t + i - (subtract_one ? 1 : 0);
1141 static int generic_array_bisect_plus_one(JournalFile *f,
1146 int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1147 direction_t direction,
1155 assert(test_object);
1160 /* This bisects the array in object 'first', but first checks
1163 r = test_object(f, extra, needle);
1166 else if (r == TEST_FOUND) {
1169 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
1178 } else if (r == TEST_RIGHT)
1181 r = generic_array_bisect(f, first, n-1, needle, test_object, direction, ret, offset, idx);
1189 static int test_object_seqnum(JournalFile *f, uint64_t p, uint64_t needle) {
1196 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1200 if (le64toh(o->entry.seqnum) == needle)
1202 else if (le64toh(o->entry.seqnum) < needle)
1208 int journal_file_move_to_entry_by_seqnum(
1211 direction_t direction,
1215 return generic_array_bisect(f,
1216 le64toh(f->header->entry_array_offset),
1217 le64toh(f->header->n_entries),
1224 static int test_object_realtime(JournalFile *f, uint64_t p, uint64_t needle) {
1231 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1235 if (le64toh(o->entry.realtime) == needle)
1237 else if (le64toh(o->entry.realtime) < needle)
1243 int journal_file_move_to_entry_by_realtime(
1246 direction_t direction,
1250 return generic_array_bisect(f,
1251 le64toh(f->header->entry_array_offset),
1252 le64toh(f->header->n_entries),
1254 test_object_realtime,
1259 static int test_object_monotonic(JournalFile *f, uint64_t p, uint64_t needle) {
1266 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1270 if (le64toh(o->entry.monotonic) == needle)
1272 else if (le64toh(o->entry.monotonic) < needle)
1278 int journal_file_move_to_entry_by_monotonic(
1282 direction_t direction,
1286 char t[8+32+1] = "_BOOT_ID=";
1290 sd_id128_to_string(boot_id, t + 8);
1292 r = journal_file_find_data_object(f, t, strlen(t), &o, NULL);
1298 return generic_array_bisect_plus_one(f,
1299 le64toh(o->data.entry_offset),
1300 le64toh(o->data.entry_array_offset),
1301 le64toh(o->data.n_entries),
1303 test_object_monotonic,
1308 static int test_object_offset(JournalFile *f, uint64_t p, uint64_t needle) {
1314 else if (p < needle)
1320 int journal_file_next_entry(
1322 Object *o, uint64_t p,
1323 direction_t direction,
1324 Object **ret, uint64_t *offset) {
1330 assert(p > 0 || !o);
1332 n = le64toh(f->header->n_entries);
1337 i = direction == DIRECTION_DOWN ? 0 : n - 1;
1339 if (o->object.type != OBJECT_ENTRY)
1342 r = generic_array_bisect(f,
1343 le64toh(f->header->entry_array_offset),
1344 le64toh(f->header->n_entries),
1353 if (direction == DIRECTION_DOWN) {
1366 /* And jump to it */
1367 return generic_array_get(f,
1368 le64toh(f->header->entry_array_offset),
1373 int journal_file_skip_entry(
1375 Object *o, uint64_t p,
1377 Object **ret, uint64_t *offset) {
1386 if (o->object.type != OBJECT_ENTRY)
1389 r = generic_array_bisect(f,
1390 le64toh(f->header->entry_array_offset),
1391 le64toh(f->header->n_entries),
1400 /* Calculate new index */
1402 if ((uint64_t) -skip >= i)
1405 i = i - (uint64_t) -skip;
1407 i += (uint64_t) skip;
1409 n = le64toh(f->header->n_entries);
1416 return generic_array_get(f,
1417 le64toh(f->header->entry_array_offset),
1422 int journal_file_next_entry_for_data(
1424 Object *o, uint64_t p,
1425 uint64_t data_offset,
1426 direction_t direction,
1427 Object **ret, uint64_t *offset) {
1434 assert(p > 0 || !o);
1436 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1440 n = le64toh(d->data.n_entries);
1445 i = direction == DIRECTION_DOWN ? 0 : n - 1;
1447 if (o->object.type != OBJECT_ENTRY)
1450 r = generic_array_bisect_plus_one(f,
1451 le64toh(d->data.entry_offset),
1452 le64toh(d->data.entry_array_offset),
1453 le64toh(d->data.n_entries),
1463 if (direction == DIRECTION_DOWN) {
1477 return generic_array_get_plus_one(f,
1478 le64toh(d->data.entry_offset),
1479 le64toh(d->data.entry_array_offset),
1484 int journal_file_move_to_entry_by_seqnum_for_data(
1486 uint64_t data_offset,
1488 direction_t direction,
1489 Object **ret, uint64_t *offset) {
1494 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1498 return generic_array_bisect_plus_one(f,
1499 le64toh(d->data.entry_offset),
1500 le64toh(d->data.entry_array_offset),
1501 le64toh(d->data.n_entries),
1508 int journal_file_move_to_entry_by_realtime_for_data(
1510 uint64_t data_offset,
1512 direction_t direction,
1513 Object **ret, uint64_t *offset) {
1518 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1522 return generic_array_bisect_plus_one(f,
1523 le64toh(d->data.entry_offset),
1524 le64toh(d->data.entry_array_offset),
1525 le64toh(d->data.n_entries),
1527 test_object_realtime,
1532 void journal_file_dump(JournalFile *f) {
1533 char a[33], b[33], c[33];
1540 printf("File Path: %s\n"
1544 "Arena size: %llu\n"
1548 sd_id128_to_string(f->header->file_id, a),
1549 sd_id128_to_string(f->header->machine_id, b),
1550 sd_id128_to_string(f->header->boot_id, c),
1551 (unsigned long long) le64toh(f->header->arena_size),
1552 (unsigned long) le64toh(f->header->n_objects),
1553 (unsigned long) le64toh(f->header->n_entries));
1555 p = le64toh(f->header->arena_offset);
1557 r = journal_file_move_to_object(f, -1, p, &o);
1561 switch (o->object.type) {
1564 printf("Type: OBJECT_UNUSED\n");
1568 printf("Type: OBJECT_DATA\n");
1572 printf("Type: OBJECT_ENTRY %llu %llu %llu\n",
1573 (unsigned long long) le64toh(o->entry.seqnum),
1574 (unsigned long long) le64toh(o->entry.monotonic),
1575 (unsigned long long) le64toh(o->entry.realtime));
1578 case OBJECT_FIELD_HASH_TABLE:
1579 printf("Type: OBJECT_FIELD_HASH_TABLE\n");
1582 case OBJECT_DATA_HASH_TABLE:
1583 printf("Type: OBJECT_DATA_HASH_TABLE\n");
1586 case OBJECT_ENTRY_ARRAY:
1587 printf("Type: OBJECT_ENTRY_ARRAY\n");
1591 if (p == le64toh(f->header->tail_object_offset))
1594 p = p + ALIGN64(le64toh(o->object.size));
1599 log_error("File corrupt");
1602 int journal_file_open(
1606 JournalFile *template,
1607 JournalFile **ret) {
1611 bool newly_created = false;
1615 if ((flags & O_ACCMODE) != O_RDONLY &&
1616 (flags & O_ACCMODE) != O_RDWR)
1619 f = new0(JournalFile, 1);
1626 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1627 f->prot = prot_from_flags(flags);
1629 f->path = strdup(fname);
1635 f->fd = open(f->path, f->flags|O_CLOEXEC, f->mode);
1641 if (fstat(f->fd, &f->last_stat) < 0) {
1646 if (f->last_stat.st_size == 0 && f->writable) {
1647 newly_created = true;
1649 r = journal_file_init_header(f, template);
1653 if (fstat(f->fd, &f->last_stat) < 0) {
1659 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1664 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1665 if (f->header == MAP_FAILED) {
1671 if (!newly_created) {
1672 r = journal_file_verify_header(f);
1678 r = journal_file_refresh_header(f);
1683 if (newly_created) {
1685 r = journal_file_setup_field_hash_table(f);
1689 r = journal_file_setup_data_hash_table(f);
1694 r = journal_file_map_field_hash_table(f);
1698 r = journal_file_map_data_hash_table(f);
1708 journal_file_close(f);
1713 int journal_file_rotate(JournalFile **f) {
1716 JournalFile *old_file, *new_file = NULL;
1724 if (!old_file->writable)
1727 if (!endswith(old_file->path, ".journal"))
1730 l = strlen(old_file->path);
1732 p = new(char, l + 1 + 16 + 1 + 32 + 1 + 16 + 1);
1736 memcpy(p, old_file->path, l - 8);
1738 sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1);
1739 snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
1740 "-%016llx-%016llx.journal",
1741 (unsigned long long) le64toh((*f)->header->seqnum),
1742 (unsigned long long) le64toh((*f)->header->tail_entry_realtime));
1744 r = rename(old_file->path, p);
1750 old_file->header->state = le32toh(STATE_ARCHIVED);
1752 r = journal_file_open(old_file->path, old_file->flags, old_file->mode, old_file, &new_file);
1753 journal_file_close(old_file);
1759 struct vacuum_info {
1764 sd_id128_t seqnum_id;
1768 static int vacuum_compare(const void *_a, const void *_b) {
1769 const struct vacuum_info *a, *b;
1774 if (sd_id128_equal(a->seqnum_id, b->seqnum_id)) {
1775 if (a->seqnum < b->seqnum)
1777 else if (a->seqnum > b->seqnum)
1783 if (a->realtime < b->realtime)
1785 else if (a->realtime > b->realtime)
1788 return memcmp(&a->seqnum_id, &b->seqnum_id, 16);
1791 int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t min_free) {
1794 struct vacuum_info *list = NULL;
1795 unsigned n_list = 0, n_allocated = 0, i;
1801 max_use = DEFAULT_MAX_USE;
1803 d = opendir(directory);
1809 struct dirent buf, *de;
1813 unsigned long long seqnum, realtime;
1814 sd_id128_t seqnum_id;
1816 k = readdir_r(d, &buf, &de);
1825 if (!dirent_is_file_with_suffix(de, ".journal"))
1828 q = strlen(de->d_name);
1830 if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8)
1833 if (de->d_name[q-8-16-1] != '-' ||
1834 de->d_name[q-8-16-1-16-1] != '-' ||
1835 de->d_name[q-8-16-1-16-1-32-1] != '@')
1838 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
1841 if (!S_ISREG(st.st_mode))
1844 p = strdup(de->d_name);
1850 de->d_name[q-8-16-1-16-1] = 0;
1851 if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) {
1856 if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) {
1861 if (n_list >= n_allocated) {
1862 struct vacuum_info *j;
1864 n_allocated = MAX(n_allocated * 2U, 8U);
1865 j = realloc(list, n_allocated * sizeof(struct vacuum_info));
1875 list[n_list].filename = p;
1876 list[n_list].usage = (uint64_t) st.st_blksize * (uint64_t) st.st_blocks;
1877 list[n_list].seqnum = seqnum;
1878 list[n_list].realtime = realtime;
1879 list[n_list].seqnum_id = seqnum_id;
1881 sum += list[n_list].usage;
1886 qsort(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
1888 for(i = 0; i < n_list; i++) {
1891 if (fstatvfs(dirfd(d), &ss) < 0) {
1896 if (sum <= max_use &&
1897 (uint64_t) ss.f_bavail * (uint64_t) ss.f_bsize >= min_free)
1900 if (unlinkat(dirfd(d), list[i].filename, 0) >= 0) {
1901 log_debug("Deleted archived journal %s/%s.", directory, list[i].filename);
1902 sum -= list[i].usage;
1903 } else if (errno != ENOENT)
1904 log_warning("Failed to delete %s/%s: %m", directory, list[i].filename);
1908 for (i = 0; i < n_list; i++)
1909 free(list[i].filename);