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_DATA_HASH_TABLE_SIZE (2047ULL*16ULL)
35 #define DEFAULT_FIELD_HASH_TABLE_SIZE (2047ULL*16ULL)
37 #define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
39 static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
41 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
43 void journal_file_close(JournalFile *f) {
48 if (f->header && f->writable)
49 f->header->state = STATE_OFFLINE;
52 for (t = 0; t < _WINDOW_MAX; t++)
53 if (f->windows[t].ptr)
54 munmap(f->windows[t].ptr, f->windows[t].size);
57 close_nointr_nofail(f->fd);
63 static int journal_file_init_header(JournalFile *f, JournalFile *template) {
71 memcpy(h.signature, signature, 8);
72 h.arena_offset = htole64(ALIGN64(sizeof(h)));
74 r = sd_id128_randomize(&h.file_id);
79 h.seqnum_id = template->header->seqnum_id;
80 h.seqnum = template->header->seqnum;
82 h.seqnum_id = h.file_id;
84 k = pwrite(f->fd, &h, sizeof(h), 0);
94 static int journal_file_refresh_header(JournalFile *f) {
100 r = sd_id128_get_machine(&f->header->machine_id);
104 r = sd_id128_get_boot(&boot_id);
108 if (sd_id128_equal(boot_id, f->header->boot_id))
109 f->tail_entry_monotonic_valid = true;
111 f->header->boot_id = boot_id;
113 f->header->state = STATE_ONLINE;
117 static int journal_file_verify_header(JournalFile *f) {
120 if (memcmp(f->header, signature, 8))
123 if (f->header->incompatible_flags != 0)
124 return -EPROTONOSUPPORT;
126 if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
131 sd_id128_t machine_id;
134 r = sd_id128_get_machine(&machine_id);
138 if (!sd_id128_equal(machine_id, f->header->machine_id))
141 state = f->header->state;
143 if (state == STATE_ONLINE)
144 log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f->path);
145 else if (state == STATE_ARCHIVED)
147 else if (state != STATE_OFFLINE)
148 log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
154 static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
155 uint64_t old_size, new_size;
159 /* We assume that this file is not sparse, and we know that
160 * for sure, since we always call posix_fallocate()
164 le64toh(f->header->arena_offset) +
165 le64toh(f->header->arena_size);
167 new_size = PAGE_ALIGN(offset + size);
168 if (new_size < le64toh(f->header->arena_offset))
169 new_size = le64toh(f->header->arena_offset);
171 if (new_size <= old_size)
174 if (f->metrics.max_size > 0 &&
175 new_size > f->metrics.max_size)
178 if (new_size > f->metrics.min_size &&
179 f->metrics.keep_free > 0) {
182 if (fstatvfs(f->fd, &svfs) >= 0) {
185 available = svfs.f_bfree * svfs.f_bsize;
187 if (available >= f->metrics.keep_free)
188 available -= f->metrics.keep_free;
192 if (new_size - old_size > available)
197 /* Note that the glibc fallocate() fallback is very
198 inefficient, hence we try to minimize the allocation area
200 if (posix_fallocate(f->fd, old_size, new_size - old_size) < 0)
203 if (fstat(f->fd, &f->last_stat) < 0)
206 f->header->arena_size = new_size - htole64(f->header->arena_offset);
211 static int journal_file_map(
220 uint64_t woffset, wsize;
227 woffset = offset & ~((uint64_t) page_size() - 1ULL);
228 wsize = size + (offset - woffset);
229 wsize = PAGE_ALIGN(wsize);
231 window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
232 if (window == MAP_FAILED)
244 *ret = (uint8_t*) window + (offset - woffset);
249 static int journal_file_move_to(JournalFile *f, int wt, uint64_t offset, uint64_t size, void **ret) {
258 assert(wt < _WINDOW_MAX);
262 if (_likely_(w->ptr &&
263 w->offset <= offset &&
264 w->offset + w->size >= offset + size)) {
266 *ret = (uint8_t*) w->ptr + (offset - w->offset);
271 if (munmap(w->ptr, w->size) < 0)
275 w->size = w->offset = 0;
278 if (size < DEFAULT_WINDOW_SIZE) {
279 /* If the default window size is larger then what was
280 * asked for extend the mapping a bit in the hope to
281 * minimize needed remappings later on. We add half
282 * the window space before and half behind the
283 * requested mapping */
285 delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
291 size += (DEFAULT_WINDOW_SIZE - delta);
295 r = journal_file_map(f,
297 &w->ptr, &w->offset, &w->size,
303 *ret = (uint8_t*) p + delta;
307 static bool verify_hash(Object *o) {
312 if (o->object.type == OBJECT_DATA) {
313 h1 = le64toh(o->data.hash);
314 h2 = hash64(o->data.payload, le64toh(o->object.size) - offsetof(Object, data.payload));
315 } else if (o->object.type == OBJECT_FIELD) {
316 h1 = le64toh(o->field.hash);
317 h2 = hash64(o->field.payload, le64toh(o->object.size) - offsetof(Object, field.payload));
324 int journal_file_move_to_object(JournalFile *f, int type, uint64_t offset, Object **ret) {
332 assert(type < _OBJECT_TYPE_MAX);
334 r = journal_file_move_to(f, type >= 0 ? type : WINDOW_UNKNOWN, offset, sizeof(ObjectHeader), &t);
339 s = le64toh(o->object.size);
341 if (s < sizeof(ObjectHeader))
344 if (type >= 0 && o->object.type != type)
347 if (s > sizeof(ObjectHeader)) {
348 r = journal_file_move_to(f, o->object.type, offset, s, &t);
362 static uint64_t journal_file_seqnum(JournalFile *f, uint64_t *seqnum) {
367 r = le64toh(f->header->seqnum) + 1;
370 /* If an external seqnum counter was passed, we update
371 * both the local and the external one, and set it to
372 * the maximum of both */
380 f->header->seqnum = htole64(r);
382 if (f->header->first_seqnum == 0)
383 f->header->first_seqnum = htole64(r);
388 static int journal_file_append_object(JournalFile *f, int type, uint64_t size, Object **ret, uint64_t *offset) {
395 assert(size >= sizeof(ObjectHeader));
399 p = le64toh(f->header->tail_object_offset);
401 p = le64toh(f->header->arena_offset);
403 r = journal_file_move_to_object(f, -1, p, &tail);
407 p += ALIGN64(le64toh(tail->object.size));
410 r = journal_file_allocate(f, p, size);
414 r = journal_file_move_to(f, type, p, size, &t);
421 o->object.type = type;
422 o->object.size = htole64(size);
424 f->header->tail_object_offset = htole64(p);
425 f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
433 static int journal_file_setup_data_hash_table(JournalFile *f) {
440 s = DEFAULT_DATA_HASH_TABLE_SIZE;
441 r = journal_file_append_object(f,
442 OBJECT_DATA_HASH_TABLE,
443 offsetof(Object, hash_table.items) + s,
448 memset(o->hash_table.items, 0, s);
450 f->header->data_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
451 f->header->data_hash_table_size = htole64(s);
456 static int journal_file_setup_field_hash_table(JournalFile *f) {
463 s = DEFAULT_FIELD_HASH_TABLE_SIZE;
464 r = journal_file_append_object(f,
465 OBJECT_FIELD_HASH_TABLE,
466 offsetof(Object, hash_table.items) + s,
471 memset(o->hash_table.items, 0, s);
473 f->header->field_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
474 f->header->field_hash_table_size = htole64(s);
479 static int journal_file_map_data_hash_table(JournalFile *f) {
486 p = le64toh(f->header->data_hash_table_offset);
487 s = le64toh(f->header->data_hash_table_size);
489 r = journal_file_move_to(f,
490 WINDOW_DATA_HASH_TABLE,
496 f->data_hash_table = t;
500 static int journal_file_map_field_hash_table(JournalFile *f) {
507 p = le64toh(f->header->field_hash_table_offset);
508 s = le64toh(f->header->field_hash_table_size);
510 r = journal_file_move_to(f,
511 WINDOW_FIELD_HASH_TABLE,
517 f->field_hash_table = t;
521 static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash) {
528 assert(o->object.type == OBJECT_DATA);
530 o->data.next_hash_offset = o->data.next_field_offset = 0;
531 o->data.entry_offset = o->data.entry_array_offset = 0;
532 o->data.n_entries = 0;
534 h = hash % (le64toh(f->header->data_hash_table_size) / sizeof(HashItem));
535 p = le64toh(f->data_hash_table[h].head_hash_offset);
537 /* Only entry in the hash table is easy */
538 f->data_hash_table[h].head_hash_offset = htole64(offset);
540 /* Temporarily move back to the previous data object,
541 * to patch in pointer */
543 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
547 o->data.next_hash_offset = htole64(offset);
549 r = journal_file_move_to_object(f, OBJECT_DATA, offset, &o);
554 f->data_hash_table[h].tail_hash_offset = htole64(offset);
559 int journal_file_find_data_object_with_hash(
561 const void *data, uint64_t size, uint64_t hash,
562 Object **ret, uint64_t *offset) {
563 uint64_t p, osize, h;
567 assert(data || size == 0);
569 osize = offsetof(Object, data.payload) + size;
571 if (f->header->data_hash_table_size == 0)
574 h = hash % (le64toh(f->header->data_hash_table_size) / sizeof(HashItem));
575 p = le64toh(f->data_hash_table[h].head_hash_offset);
580 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
584 if (le64toh(o->object.size) == osize &&
585 memcmp(o->data.payload, data, size) == 0) {
587 if (le64toh(o->data.hash) != hash)
599 p = le64toh(o->data.next_hash_offset);
605 int journal_file_find_data_object(
607 const void *data, uint64_t size,
608 Object **ret, uint64_t *offset) {
613 assert(data || size == 0);
615 hash = hash64(data, size);
617 return journal_file_find_data_object_with_hash(f,
622 static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
629 assert(data || size == 0);
631 hash = hash64(data, size);
633 r = journal_file_find_data_object_with_hash(f, data, size, hash, &o, &p);
647 osize = offsetof(Object, data.payload) + size;
648 r = journal_file_append_object(f, OBJECT_DATA, osize, &o, &p);
652 o->data.hash = htole64(hash);
653 memcpy(o->data.payload, data, size);
655 r = journal_file_link_data(f, o, p, hash);
668 uint64_t journal_file_entry_n_items(Object *o) {
670 assert(o->object.type == htole64(OBJECT_ENTRY));
672 return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
675 static uint64_t journal_file_entry_array_n_items(Object *o) {
677 assert(o->object.type == htole64(OBJECT_ENTRY_ARRAY));
679 return (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / sizeof(uint64_t);
682 static int link_entry_into_array(JournalFile *f,
687 uint64_t n = 0, ap = 0, q, i, a, hidx;
696 i = hidx = le64toh(*idx);
699 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
703 n = journal_file_entry_array_n_items(o);
705 o->entry_array.items[i] = htole64(p);
706 *idx = htole64(hidx + 1);
712 a = le64toh(o->entry_array.next_entry_array_offset);
723 r = journal_file_append_object(f, OBJECT_ENTRY_ARRAY,
724 offsetof(Object, entry_array.items) + n * sizeof(uint64_t),
729 o->entry_array.items[i] = htole64(p);
734 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, ap, &o);
738 o->entry_array.next_entry_array_offset = htole64(q);
741 *idx = htole64(hidx + 1);
746 static int link_entry_into_array_plus_one(JournalFile *f,
765 i = le64toh(*idx) - 1;
766 r = link_entry_into_array(f, first, &i, p);
771 *idx = htole64(le64toh(*idx) + 1);
775 static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
782 p = le64toh(o->entry.items[i].object_offset);
786 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
790 return link_entry_into_array_plus_one(f,
791 &o->data.entry_offset,
792 &o->data.entry_array_offset,
797 static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
804 assert(o->object.type == OBJECT_ENTRY);
806 /* Link up the entry itself */
807 r = link_entry_into_array(f,
808 &f->header->entry_array_offset,
809 &f->header->n_entries,
814 log_error("=> %s seqnr=%lu n_entries=%lu", f->path, (unsigned long) o->entry.seqnum, (unsigned long) f->header->n_entries);
816 if (f->header->head_entry_realtime == 0)
817 f->header->head_entry_realtime = o->entry.realtime;
819 f->header->tail_entry_realtime = o->entry.realtime;
820 f->header->tail_entry_monotonic = o->entry.monotonic;
822 f->tail_entry_monotonic_valid = true;
824 /* Link up the items */
825 n = journal_file_entry_n_items(o);
826 for (i = 0; i < n; i++) {
827 r = journal_file_link_entry_item(f, o, offset, i);
835 static int journal_file_append_entry_internal(
837 const dual_timestamp *ts,
839 const EntryItem items[], unsigned n_items,
841 Object **ret, uint64_t *offset) {
848 assert(items || n_items == 0);
851 osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
853 r = journal_file_append_object(f, OBJECT_ENTRY, osize, &o, &np);
857 o->entry.seqnum = htole64(journal_file_seqnum(f, seqnum));
858 memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
859 o->entry.realtime = htole64(ts->realtime);
860 o->entry.monotonic = htole64(ts->monotonic);
861 o->entry.xor_hash = htole64(xor_hash);
862 o->entry.boot_id = f->header->boot_id;
864 r = journal_file_link_entry(f, o, np);
877 static void journal_file_post_change(JournalFile *f) {
880 /* inotify() does not receive IN_MODIFY events from file
881 * accesses done via mmap(). After each access we hence
882 * trigger IN_MODIFY by truncating the journal file to its
883 * current size which triggers IN_MODIFY. */
885 __sync_synchronize();
887 if (ftruncate(f->fd, f->last_stat.st_size) < 0)
888 log_error("Failed to to truncate file to its own size: %m");
891 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) {
895 uint64_t xor_hash = 0;
896 struct dual_timestamp _ts;
899 assert(iovec || n_iovec == 0);
905 dual_timestamp_get(&_ts);
909 if (f->tail_entry_monotonic_valid &&
910 ts->monotonic < le64toh(f->header->tail_entry_monotonic))
913 if (ts->realtime < le64toh(f->header->tail_entry_realtime))
916 items = new(EntryItem, n_iovec);
920 for (i = 0; i < n_iovec; i++) {
924 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
928 xor_hash ^= le64toh(o->data.hash);
929 items[i].object_offset = htole64(p);
930 items[i].hash = o->data.hash;
933 r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset);
935 journal_file_post_change(f);
943 static int generic_array_get(JournalFile *f,
946 Object **ret, uint64_t *offset) {
958 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
962 n = journal_file_entry_array_n_items(o);
964 p = le64toh(o->entry_array.items[i]);
969 a = le64toh(o->entry_array.next_entry_array_offset);
972 if (a <= 0 || p <= 0)
975 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
988 static int generic_array_get_plus_one(JournalFile *f,
992 Object **ret, uint64_t *offset) {
1001 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
1014 return generic_array_get(f, first, i-1, ret, offset);
1023 static int generic_array_bisect(JournalFile *f,
1027 int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1028 direction_t direction,
1033 uint64_t a, p, t = 0, i = 0, last_p = 0;
1034 bool subtract_one = false;
1035 Object *o, *array = NULL;
1039 assert(test_object);
1043 uint64_t left, right, k, lp;
1045 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &array);
1049 k = journal_file_entry_array_n_items(array);
1055 lp = p = le64toh(array->entry_array.items[i]);
1059 r = test_object(f, p, needle);
1063 if (r == TEST_FOUND)
1064 r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1066 if (r == TEST_RIGHT) {
1070 if (left == right) {
1071 if (direction == DIRECTION_UP)
1072 subtract_one = true;
1078 assert(left < right);
1080 i = (left + right) / 2;
1081 p = le64toh(array->entry_array.items[i]);
1085 r = test_object(f, p, needle);
1089 if (r == TEST_FOUND)
1090 r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1092 if (r == TEST_RIGHT)
1106 a = le64toh(array->entry_array.next_entry_array_offset);
1112 if (subtract_one && t == 0 && i == 0)
1115 if (subtract_one && i == 0)
1117 else if (subtract_one)
1118 p = le64toh(array->entry_array.items[i-1]);
1120 p = le64toh(array->entry_array.items[i]);
1122 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1133 *idx = t + i - (subtract_one ? 1 : 0);
1138 static int generic_array_bisect_plus_one(JournalFile *f,
1143 int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1144 direction_t direction,
1152 assert(test_object);
1157 /* This bisects the array in object 'first', but first checks
1160 r = test_object(f, extra, needle);
1163 else if (r == TEST_FOUND) {
1166 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
1175 } else if (r == TEST_RIGHT)
1178 r = generic_array_bisect(f, first, n-1, needle, test_object, direction, ret, offset, idx);
1186 static int test_object_seqnum(JournalFile *f, uint64_t p, uint64_t needle) {
1193 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1197 if (le64toh(o->entry.seqnum) == needle)
1199 else if (le64toh(o->entry.seqnum) < needle)
1205 int journal_file_move_to_entry_by_seqnum(
1208 direction_t direction,
1212 return generic_array_bisect(f,
1213 le64toh(f->header->entry_array_offset),
1214 le64toh(f->header->n_entries),
1221 static int test_object_realtime(JournalFile *f, uint64_t p, uint64_t needle) {
1228 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1232 if (le64toh(o->entry.realtime) == needle)
1234 else if (le64toh(o->entry.realtime) < needle)
1240 int journal_file_move_to_entry_by_realtime(
1243 direction_t direction,
1247 return generic_array_bisect(f,
1248 le64toh(f->header->entry_array_offset),
1249 le64toh(f->header->n_entries),
1251 test_object_realtime,
1256 static int test_object_monotonic(JournalFile *f, uint64_t p, uint64_t needle) {
1263 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1267 if (le64toh(o->entry.monotonic) == needle)
1269 else if (le64toh(o->entry.monotonic) < needle)
1275 int journal_file_move_to_entry_by_monotonic(
1279 direction_t direction,
1283 char t[8+32+1] = "_BOOT_ID=";
1287 sd_id128_to_string(boot_id, t + 8);
1289 r = journal_file_find_data_object(f, t, strlen(t), &o, NULL);
1295 return generic_array_bisect_plus_one(f,
1296 le64toh(o->data.entry_offset),
1297 le64toh(o->data.entry_array_offset),
1298 le64toh(o->data.n_entries),
1300 test_object_monotonic,
1305 static int test_object_offset(JournalFile *f, uint64_t p, uint64_t needle) {
1311 else if (p < needle)
1317 int journal_file_next_entry(
1319 Object *o, uint64_t p,
1320 direction_t direction,
1321 Object **ret, uint64_t *offset) {
1327 assert(p > 0 || !o);
1329 n = le64toh(f->header->n_entries);
1334 i = direction == DIRECTION_DOWN ? 0 : n - 1;
1336 if (o->object.type != OBJECT_ENTRY)
1339 r = generic_array_bisect(f,
1340 le64toh(f->header->entry_array_offset),
1341 le64toh(f->header->n_entries),
1350 if (direction == DIRECTION_DOWN) {
1363 /* And jump to it */
1364 return generic_array_get(f,
1365 le64toh(f->header->entry_array_offset),
1370 int journal_file_skip_entry(
1372 Object *o, uint64_t p,
1374 Object **ret, uint64_t *offset) {
1383 if (o->object.type != OBJECT_ENTRY)
1386 r = generic_array_bisect(f,
1387 le64toh(f->header->entry_array_offset),
1388 le64toh(f->header->n_entries),
1397 /* Calculate new index */
1399 if ((uint64_t) -skip >= i)
1402 i = i - (uint64_t) -skip;
1404 i += (uint64_t) skip;
1406 n = le64toh(f->header->n_entries);
1413 return generic_array_get(f,
1414 le64toh(f->header->entry_array_offset),
1419 int journal_file_next_entry_for_data(
1421 Object *o, uint64_t p,
1422 uint64_t data_offset,
1423 direction_t direction,
1424 Object **ret, uint64_t *offset) {
1431 assert(p > 0 || !o);
1433 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1437 n = le64toh(d->data.n_entries);
1442 i = direction == DIRECTION_DOWN ? 0 : n - 1;
1444 if (o->object.type != OBJECT_ENTRY)
1447 r = generic_array_bisect_plus_one(f,
1448 le64toh(d->data.entry_offset),
1449 le64toh(d->data.entry_array_offset),
1450 le64toh(d->data.n_entries),
1460 if (direction == DIRECTION_DOWN) {
1474 return generic_array_get_plus_one(f,
1475 le64toh(d->data.entry_offset),
1476 le64toh(d->data.entry_array_offset),
1481 int journal_file_move_to_entry_by_seqnum_for_data(
1483 uint64_t data_offset,
1485 direction_t direction,
1486 Object **ret, uint64_t *offset) {
1491 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1495 return generic_array_bisect_plus_one(f,
1496 le64toh(d->data.entry_offset),
1497 le64toh(d->data.entry_array_offset),
1498 le64toh(d->data.n_entries),
1505 int journal_file_move_to_entry_by_realtime_for_data(
1507 uint64_t data_offset,
1509 direction_t direction,
1510 Object **ret, uint64_t *offset) {
1515 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1519 return generic_array_bisect_plus_one(f,
1520 le64toh(d->data.entry_offset),
1521 le64toh(d->data.entry_array_offset),
1522 le64toh(d->data.n_entries),
1524 test_object_realtime,
1529 void journal_file_dump(JournalFile *f) {
1530 char a[33], b[33], c[33];
1537 printf("File Path: %s\n"
1541 "Arena size: %llu\n"
1545 sd_id128_to_string(f->header->file_id, a),
1546 sd_id128_to_string(f->header->machine_id, b),
1547 sd_id128_to_string(f->header->boot_id, c),
1548 (unsigned long long) le64toh(f->header->arena_size),
1549 (unsigned long) le64toh(f->header->n_objects),
1550 (unsigned long) le64toh(f->header->n_entries));
1552 p = le64toh(f->header->arena_offset);
1554 r = journal_file_move_to_object(f, -1, p, &o);
1558 switch (o->object.type) {
1561 printf("Type: OBJECT_UNUSED\n");
1565 printf("Type: OBJECT_DATA\n");
1569 printf("Type: OBJECT_ENTRY %llu %llu %llu\n",
1570 (unsigned long long) le64toh(o->entry.seqnum),
1571 (unsigned long long) le64toh(o->entry.monotonic),
1572 (unsigned long long) le64toh(o->entry.realtime));
1575 case OBJECT_FIELD_HASH_TABLE:
1576 printf("Type: OBJECT_FIELD_HASH_TABLE\n");
1579 case OBJECT_DATA_HASH_TABLE:
1580 printf("Type: OBJECT_DATA_HASH_TABLE\n");
1583 case OBJECT_ENTRY_ARRAY:
1584 printf("Type: OBJECT_ENTRY_ARRAY\n");
1588 if (p == le64toh(f->header->tail_object_offset))
1591 p = p + ALIGN64(le64toh(o->object.size));
1596 log_error("File corrupt");
1599 int journal_file_open(
1603 JournalFile *template,
1604 JournalFile **ret) {
1608 bool newly_created = false;
1612 if ((flags & O_ACCMODE) != O_RDONLY &&
1613 (flags & O_ACCMODE) != O_RDWR)
1616 f = new0(JournalFile, 1);
1623 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1624 f->prot = prot_from_flags(flags);
1626 f->metrics.max_size = DEFAULT_MAX_SIZE;
1627 f->metrics.min_size = DEFAULT_MIN_SIZE;
1628 f->metrics.keep_free = DEFAULT_KEEP_FREE;
1630 f->path = strdup(fname);
1636 f->fd = open(f->path, f->flags|O_CLOEXEC, f->mode);
1642 if (fstat(f->fd, &f->last_stat) < 0) {
1647 if (f->last_stat.st_size == 0 && f->writable) {
1648 newly_created = true;
1650 r = journal_file_init_header(f, template);
1654 if (fstat(f->fd, &f->last_stat) < 0) {
1660 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1665 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1666 if (f->header == MAP_FAILED) {
1672 if (!newly_created) {
1673 r = journal_file_verify_header(f);
1679 r = journal_file_refresh_header(f);
1684 if (newly_created) {
1686 r = journal_file_setup_field_hash_table(f);
1690 r = journal_file_setup_data_hash_table(f);
1695 r = journal_file_map_field_hash_table(f);
1699 r = journal_file_map_data_hash_table(f);
1709 journal_file_close(f);
1714 int journal_file_rotate(JournalFile **f) {
1717 JournalFile *old_file, *new_file = NULL;
1725 if (!old_file->writable)
1728 if (!endswith(old_file->path, ".journal"))
1731 l = strlen(old_file->path);
1733 p = new(char, l + 1 + 16 + 1 + 32 + 1 + 16 + 1);
1737 memcpy(p, old_file->path, l - 8);
1739 sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1);
1740 snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
1741 "-%016llx-%016llx.journal",
1742 (unsigned long long) le64toh((*f)->header->seqnum),
1743 (unsigned long long) le64toh((*f)->header->tail_entry_realtime));
1745 r = rename(old_file->path, p);
1751 old_file->header->state = le32toh(STATE_ARCHIVED);
1753 r = journal_file_open(old_file->path, old_file->flags, old_file->mode, old_file, &new_file);
1754 journal_file_close(old_file);
1760 struct vacuum_info {
1765 sd_id128_t seqnum_id;
1769 static int vacuum_compare(const void *_a, const void *_b) {
1770 const struct vacuum_info *a, *b;
1775 if (sd_id128_equal(a->seqnum_id, b->seqnum_id)) {
1776 if (a->seqnum < b->seqnum)
1778 else if (a->seqnum > b->seqnum)
1784 if (a->realtime < b->realtime)
1786 else if (a->realtime > b->realtime)
1789 return memcmp(&a->seqnum_id, &b->seqnum_id, 16);
1792 int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t min_free) {
1795 struct vacuum_info *list = NULL;
1796 unsigned n_list = 0, n_allocated = 0, i;
1802 max_use = DEFAULT_MAX_USE;
1804 d = opendir(directory);
1810 struct dirent buf, *de;
1814 unsigned long long seqnum, realtime;
1815 sd_id128_t seqnum_id;
1817 k = readdir_r(d, &buf, &de);
1826 if (!dirent_is_file_with_suffix(de, ".journal"))
1829 q = strlen(de->d_name);
1831 if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8)
1834 if (de->d_name[q-8-16-1] != '-' ||
1835 de->d_name[q-8-16-1-16-1] != '-' ||
1836 de->d_name[q-8-16-1-16-1-32-1] != '@')
1839 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
1842 if (!S_ISREG(st.st_mode))
1845 p = strdup(de->d_name);
1851 de->d_name[q-8-16-1-16-1] = 0;
1852 if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) {
1857 if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) {
1862 if (n_list >= n_allocated) {
1863 struct vacuum_info *j;
1865 n_allocated = MAX(n_allocated * 2U, 8U);
1866 j = realloc(list, n_allocated * sizeof(struct vacuum_info));
1876 list[n_list].filename = p;
1877 list[n_list].usage = (uint64_t) st.st_blksize * (uint64_t) st.st_blocks;
1878 list[n_list].seqnum = seqnum;
1879 list[n_list].realtime = realtime;
1880 list[n_list].seqnum_id = seqnum_id;
1882 sum += list[n_list].usage;
1887 qsort(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
1889 for(i = 0; i < n_list; i++) {
1892 if (fstatvfs(dirfd(d), &ss) < 0) {
1897 if (sum <= max_use &&
1898 (uint64_t) ss.f_bavail * (uint64_t) ss.f_bsize >= min_free)
1901 if (unlinkat(dirfd(d), list[i].filename, 0) >= 0) {
1902 log_debug("Deleted archived journal %s/%s.", directory, list[i].filename);
1903 sum -= list[i].usage;
1904 } else if (errno != ENOENT)
1905 log_warning("Failed to delete %s/%s: %m", directory, list[i].filename);
1909 for (i = 0; i < n_list; i++)
1910 free(list[i].filename);