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"
35 #define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*16ULL)
36 #define DEFAULT_FIELD_HASH_TABLE_SIZE (2047ULL*16ULL)
38 #define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
40 #define COMPRESSION_SIZE_THRESHOLD (64ULL)
42 /* This is the minimum journal file size */
43 #define JOURNAL_FILE_SIZE_MIN (64ULL*1024ULL)
45 /* These are the lower and upper bounds if we deduce the max_use value
46 * from the file system size */
47 #define DEFAULT_MAX_USE_LOWER (1ULL*1024ULL*1024ULL) /* 1 MiB */
48 #define DEFAULT_MAX_USE_UPPER (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
50 /* This is the upper bound if we deduce max_size from max_use */
51 #define DEFAULT_MAX_SIZE_UPPER (16ULL*1024ULL*1024ULL) /* 16 MiB */
53 /* This is the upper bound if we deduce the keep_free value from the
55 #define DEFAULT_KEEP_FREE_UPPER (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
57 /* This is the keep_free value when we can't determine the system
59 #define DEFAULT_KEEP_FREE (1024ULL*1024ULL) /* 1 MB */
61 static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
63 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
65 void journal_file_close(JournalFile *f) {
70 if (f->header && f->writable)
71 f->header->state = STATE_OFFLINE;
74 for (t = 0; t < _WINDOW_MAX; t++)
75 if (f->windows[t].ptr)
76 munmap(f->windows[t].ptr, f->windows[t].size);
79 close_nointr_nofail(f->fd);
84 free(f->compress_buffer);
90 static int journal_file_init_header(JournalFile *f, JournalFile *template) {
98 memcpy(h.signature, signature, 8);
99 h.arena_offset = htole64(ALIGN64(sizeof(h)));
101 r = sd_id128_randomize(&h.file_id);
106 h.seqnum_id = template->header->seqnum_id;
107 h.seqnum = template->header->seqnum;
109 h.seqnum_id = h.file_id;
111 k = pwrite(f->fd, &h, sizeof(h), 0);
121 static int journal_file_refresh_header(JournalFile *f) {
127 r = sd_id128_get_machine(&f->header->machine_id);
131 r = sd_id128_get_boot(&boot_id);
135 if (sd_id128_equal(boot_id, f->header->boot_id))
136 f->tail_entry_monotonic_valid = true;
138 f->header->boot_id = boot_id;
140 f->header->state = STATE_ONLINE;
144 static int journal_file_verify_header(JournalFile *f) {
147 if (memcmp(f->header, signature, 8))
151 if ((le64toh(f->header->incompatible_flags) & ~HEADER_INCOMPATIBLE_COMPRESSED) != 0)
152 return -EPROTONOSUPPORT;
154 if (f->header->incompatible_flags != 0)
155 return -EPROTONOSUPPORT;
158 if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
163 sd_id128_t machine_id;
166 r = sd_id128_get_machine(&machine_id);
170 if (!sd_id128_equal(machine_id, f->header->machine_id))
173 state = f->header->state;
175 if (state == STATE_ONLINE)
176 log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f->path);
177 else if (state == STATE_ARCHIVED)
179 else if (state != STATE_OFFLINE)
180 log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
186 static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
187 uint64_t old_size, new_size;
191 /* We assume that this file is not sparse, and we know that
192 * for sure, since we always call posix_fallocate()
196 le64toh(f->header->arena_offset) +
197 le64toh(f->header->arena_size);
199 new_size = PAGE_ALIGN(offset + size);
200 if (new_size < le64toh(f->header->arena_offset))
201 new_size = le64toh(f->header->arena_offset);
203 if (new_size <= old_size)
206 if (f->metrics.max_size > 0 &&
207 new_size > f->metrics.max_size)
210 if (new_size > f->metrics.min_size &&
211 f->metrics.keep_free > 0) {
214 if (fstatvfs(f->fd, &svfs) >= 0) {
217 available = svfs.f_bfree * svfs.f_bsize;
219 if (available >= f->metrics.keep_free)
220 available -= f->metrics.keep_free;
224 if (new_size - old_size > available)
229 /* Note that the glibc fallocate() fallback is very
230 inefficient, hence we try to minimize the allocation area
232 if (posix_fallocate(f->fd, old_size, new_size - old_size) < 0)
235 if (fstat(f->fd, &f->last_stat) < 0)
238 f->header->arena_size = new_size - htole64(f->header->arena_offset);
243 static int journal_file_map(
252 uint64_t woffset, wsize;
259 woffset = offset & ~((uint64_t) page_size() - 1ULL);
260 wsize = size + (offset - woffset);
261 wsize = PAGE_ALIGN(wsize);
263 /* Avoid SIGBUS on invalid accesses */
264 if (woffset + wsize > (uint64_t) PAGE_ALIGN(f->last_stat.st_size))
265 return -EADDRNOTAVAIL;
267 window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
268 if (window == MAP_FAILED)
280 *ret = (uint8_t*) window + (offset - woffset);
285 static int journal_file_move_to(JournalFile *f, int wt, uint64_t offset, uint64_t size, void **ret) {
294 assert(wt < _WINDOW_MAX);
298 if (_likely_(w->ptr &&
299 w->offset <= offset &&
300 w->offset + w->size >= offset + size)) {
302 *ret = (uint8_t*) w->ptr + (offset - w->offset);
307 if (munmap(w->ptr, w->size) < 0)
311 w->size = w->offset = 0;
314 if (size < DEFAULT_WINDOW_SIZE) {
315 /* If the default window size is larger then what was
316 * asked for extend the mapping a bit in the hope to
317 * minimize needed remappings later on. We add half
318 * the window space before and half behind the
319 * requested mapping */
321 delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
327 size += (DEFAULT_WINDOW_SIZE - delta);
331 if (offset > (uint64_t) f->last_stat.st_size)
332 return -EADDRNOTAVAIL;
334 if (offset + size > (uint64_t) f->last_stat.st_size)
335 size = PAGE_ALIGN((uint64_t) f->last_stat.st_size - offset);
338 return -EADDRNOTAVAIL;
340 r = journal_file_map(f,
342 &w->ptr, &w->offset, &w->size,
348 *ret = (uint8_t*) p + delta;
352 static bool verify_hash(Object *o) {
357 if (o->object.type == OBJECT_DATA && !(o->object.flags & OBJECT_COMPRESSED)) {
358 h1 = le64toh(o->data.hash);
359 h2 = hash64(o->data.payload, le64toh(o->object.size) - offsetof(Object, data.payload));
360 } else if (o->object.type == OBJECT_FIELD) {
361 h1 = le64toh(o->field.hash);
362 h2 = hash64(o->field.payload, le64toh(o->object.size) - offsetof(Object, field.payload));
369 int journal_file_move_to_object(JournalFile *f, int type, uint64_t offset, Object **ret) {
377 assert(type < _OBJECT_TYPE_MAX);
379 r = journal_file_move_to(f, type >= 0 ? type : WINDOW_UNKNOWN, offset, sizeof(ObjectHeader), &t);
384 s = le64toh(o->object.size);
386 if (s < sizeof(ObjectHeader))
389 if (type >= 0 && o->object.type != type)
392 if (s > sizeof(ObjectHeader)) {
393 r = journal_file_move_to(f, o->object.type, offset, s, &t);
407 static uint64_t journal_file_seqnum(JournalFile *f, uint64_t *seqnum) {
412 r = le64toh(f->header->seqnum) + 1;
415 /* If an external seqnum counter was passed, we update
416 * both the local and the external one, and set it to
417 * the maximum of both */
425 f->header->seqnum = htole64(r);
427 if (f->header->first_seqnum == 0)
428 f->header->first_seqnum = htole64(r);
433 static int journal_file_append_object(JournalFile *f, int type, uint64_t size, Object **ret, uint64_t *offset) {
440 assert(size >= sizeof(ObjectHeader));
444 p = le64toh(f->header->tail_object_offset);
446 p = le64toh(f->header->arena_offset);
448 r = journal_file_move_to_object(f, -1, p, &tail);
452 p += ALIGN64(le64toh(tail->object.size));
455 r = journal_file_allocate(f, p, size);
459 r = journal_file_move_to(f, type, p, size, &t);
466 o->object.type = type;
467 o->object.size = htole64(size);
469 f->header->tail_object_offset = htole64(p);
470 f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
478 static int journal_file_setup_data_hash_table(JournalFile *f) {
485 s = DEFAULT_DATA_HASH_TABLE_SIZE;
486 r = journal_file_append_object(f,
487 OBJECT_DATA_HASH_TABLE,
488 offsetof(Object, hash_table.items) + s,
493 memset(o->hash_table.items, 0, s);
495 f->header->data_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
496 f->header->data_hash_table_size = htole64(s);
501 static int journal_file_setup_field_hash_table(JournalFile *f) {
508 s = DEFAULT_FIELD_HASH_TABLE_SIZE;
509 r = journal_file_append_object(f,
510 OBJECT_FIELD_HASH_TABLE,
511 offsetof(Object, hash_table.items) + s,
516 memset(o->hash_table.items, 0, s);
518 f->header->field_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
519 f->header->field_hash_table_size = htole64(s);
524 static int journal_file_map_data_hash_table(JournalFile *f) {
531 p = le64toh(f->header->data_hash_table_offset);
532 s = le64toh(f->header->data_hash_table_size);
534 r = journal_file_move_to(f,
535 WINDOW_DATA_HASH_TABLE,
541 f->data_hash_table = t;
545 static int journal_file_map_field_hash_table(JournalFile *f) {
552 p = le64toh(f->header->field_hash_table_offset);
553 s = le64toh(f->header->field_hash_table_size);
555 r = journal_file_move_to(f,
556 WINDOW_FIELD_HASH_TABLE,
562 f->field_hash_table = t;
566 static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash) {
573 assert(o->object.type == OBJECT_DATA);
575 o->data.next_hash_offset = o->data.next_field_offset = 0;
576 o->data.entry_offset = o->data.entry_array_offset = 0;
577 o->data.n_entries = 0;
579 h = hash % (le64toh(f->header->data_hash_table_size) / sizeof(HashItem));
580 p = le64toh(f->data_hash_table[h].head_hash_offset);
582 /* Only entry in the hash table is easy */
583 f->data_hash_table[h].head_hash_offset = htole64(offset);
585 /* Temporarily move back to the previous data object,
586 * to patch in pointer */
588 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
592 o->data.next_hash_offset = htole64(offset);
594 r = journal_file_move_to_object(f, OBJECT_DATA, offset, &o);
599 f->data_hash_table[h].tail_hash_offset = htole64(offset);
604 int journal_file_find_data_object_with_hash(
606 const void *data, uint64_t size, uint64_t hash,
607 Object **ret, uint64_t *offset) {
608 uint64_t p, osize, h;
612 assert(data || size == 0);
614 osize = offsetof(Object, data.payload) + size;
616 if (f->header->data_hash_table_size == 0)
619 h = hash % (le64toh(f->header->data_hash_table_size) / sizeof(HashItem));
620 p = le64toh(f->data_hash_table[h].head_hash_offset);
625 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
629 if (le64toh(o->data.hash) != hash)
632 if (o->object.flags & OBJECT_COMPRESSED) {
636 l = le64toh(o->object.size);
637 if (l <= offsetof(Object, data.payload))
640 l -= offsetof(Object, data.payload);
642 if (!uncompress_blob(o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize))
646 memcmp(f->compress_buffer, data, size) == 0) {
657 return -EPROTONOSUPPORT;
660 } else if (le64toh(o->object.size) == osize &&
661 memcmp(o->data.payload, data, size) == 0) {
673 p = le64toh(o->data.next_hash_offset);
679 int journal_file_find_data_object(
681 const void *data, uint64_t size,
682 Object **ret, uint64_t *offset) {
687 assert(data || size == 0);
689 hash = hash64(data, size);
691 return journal_file_find_data_object_with_hash(f,
696 static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
701 bool compressed = false;
704 assert(data || size == 0);
706 hash = hash64(data, size);
708 r = journal_file_find_data_object_with_hash(f, data, size, hash, &o, &p);
722 osize = offsetof(Object, data.payload) + size;
723 r = journal_file_append_object(f, OBJECT_DATA, osize, &o, &p);
727 o->data.hash = htole64(hash);
731 size >= COMPRESSION_SIZE_THRESHOLD) {
734 compressed = compress_blob(data, size, o->data.payload, &rsize);
737 o->object.size = htole64(offsetof(Object, data.payload) + rsize);
738 o->object.flags |= OBJECT_COMPRESSED;
740 f->header->incompatible_flags = htole32(le32toh(f->header->incompatible_flags) | HEADER_INCOMPATIBLE_COMPRESSED);
742 log_debug("Compressed data object %lu -> %lu", (unsigned long) size, (unsigned long) rsize);
748 memcpy(o->data.payload, data, size);
750 r = journal_file_link_data(f, o, p, hash);
763 uint64_t journal_file_entry_n_items(Object *o) {
765 assert(o->object.type == htole64(OBJECT_ENTRY));
767 return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
770 static uint64_t journal_file_entry_array_n_items(Object *o) {
772 assert(o->object.type == htole64(OBJECT_ENTRY_ARRAY));
774 return (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / sizeof(uint64_t);
777 static int link_entry_into_array(JournalFile *f,
782 uint64_t n = 0, ap = 0, q, i, a, hidx;
791 i = hidx = le64toh(*idx);
794 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
798 n = journal_file_entry_array_n_items(o);
800 o->entry_array.items[i] = htole64(p);
801 *idx = htole64(hidx + 1);
807 a = le64toh(o->entry_array.next_entry_array_offset);
818 r = journal_file_append_object(f, OBJECT_ENTRY_ARRAY,
819 offsetof(Object, entry_array.items) + n * sizeof(uint64_t),
824 o->entry_array.items[i] = htole64(p);
829 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, ap, &o);
833 o->entry_array.next_entry_array_offset = htole64(q);
836 *idx = htole64(hidx + 1);
841 static int link_entry_into_array_plus_one(JournalFile *f,
860 i = le64toh(*idx) - 1;
861 r = link_entry_into_array(f, first, &i, p);
866 *idx = htole64(le64toh(*idx) + 1);
870 static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
877 p = le64toh(o->entry.items[i].object_offset);
881 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
885 return link_entry_into_array_plus_one(f,
886 &o->data.entry_offset,
887 &o->data.entry_array_offset,
892 static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
899 assert(o->object.type == OBJECT_ENTRY);
901 /* Link up the entry itself */
902 r = link_entry_into_array(f,
903 &f->header->entry_array_offset,
904 &f->header->n_entries,
909 log_error("=> %s seqnr=%lu n_entries=%lu", f->path, (unsigned long) o->entry.seqnum, (unsigned long) f->header->n_entries);
911 if (f->header->head_entry_realtime == 0)
912 f->header->head_entry_realtime = o->entry.realtime;
914 f->header->tail_entry_realtime = o->entry.realtime;
915 f->header->tail_entry_monotonic = o->entry.monotonic;
917 f->tail_entry_monotonic_valid = true;
919 /* Link up the items */
920 n = journal_file_entry_n_items(o);
921 for (i = 0; i < n; i++) {
922 r = journal_file_link_entry_item(f, o, offset, i);
930 static int journal_file_append_entry_internal(
932 const dual_timestamp *ts,
934 const EntryItem items[], unsigned n_items,
936 Object **ret, uint64_t *offset) {
943 assert(items || n_items == 0);
946 osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
948 r = journal_file_append_object(f, OBJECT_ENTRY, osize, &o, &np);
952 o->entry.seqnum = htole64(journal_file_seqnum(f, seqnum));
953 memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
954 o->entry.realtime = htole64(ts->realtime);
955 o->entry.monotonic = htole64(ts->monotonic);
956 o->entry.xor_hash = htole64(xor_hash);
957 o->entry.boot_id = f->header->boot_id;
959 r = journal_file_link_entry(f, o, np);
972 void journal_file_post_change(JournalFile *f) {
975 /* inotify() does not receive IN_MODIFY events from file
976 * accesses done via mmap(). After each access we hence
977 * trigger IN_MODIFY by truncating the journal file to its
978 * current size which triggers IN_MODIFY. */
980 __sync_synchronize();
982 if (ftruncate(f->fd, f->last_stat.st_size) < 0)
983 log_error("Failed to to truncate file to its own size: %m");
986 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) {
990 uint64_t xor_hash = 0;
991 struct dual_timestamp _ts;
994 assert(iovec || n_iovec == 0);
1000 dual_timestamp_get(&_ts);
1004 if (f->tail_entry_monotonic_valid &&
1005 ts->monotonic < le64toh(f->header->tail_entry_monotonic))
1008 if (ts->realtime < le64toh(f->header->tail_entry_realtime))
1011 items = alloca(sizeof(EntryItem) * n_iovec);
1013 for (i = 0; i < n_iovec; i++) {
1017 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
1021 xor_hash ^= le64toh(o->data.hash);
1022 items[i].object_offset = htole64(p);
1023 items[i].hash = o->data.hash;
1026 r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset);
1028 journal_file_post_change(f);
1033 static int generic_array_get(JournalFile *f,
1036 Object **ret, uint64_t *offset) {
1048 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
1052 n = journal_file_entry_array_n_items(o);
1054 p = le64toh(o->entry_array.items[i]);
1059 a = le64toh(o->entry_array.next_entry_array_offset);
1062 if (a <= 0 || p <= 0)
1065 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1078 static int generic_array_get_plus_one(JournalFile *f,
1082 Object **ret, uint64_t *offset) {
1091 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
1104 return generic_array_get(f, first, i-1, ret, offset);
1113 static int generic_array_bisect(JournalFile *f,
1117 int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1118 direction_t direction,
1123 uint64_t a, p, t = 0, i = 0, last_p = 0;
1124 bool subtract_one = false;
1125 Object *o, *array = NULL;
1129 assert(test_object);
1133 uint64_t left, right, k, lp;
1135 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &array);
1139 k = journal_file_entry_array_n_items(array);
1145 lp = p = le64toh(array->entry_array.items[i]);
1149 r = test_object(f, p, needle);
1153 if (r == TEST_FOUND)
1154 r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1156 if (r == TEST_RIGHT) {
1160 if (left == right) {
1161 if (direction == DIRECTION_UP)
1162 subtract_one = true;
1168 assert(left < right);
1170 i = (left + right) / 2;
1171 p = le64toh(array->entry_array.items[i]);
1175 r = test_object(f, p, needle);
1179 if (r == TEST_FOUND)
1180 r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1182 if (r == TEST_RIGHT)
1196 a = le64toh(array->entry_array.next_entry_array_offset);
1202 if (subtract_one && t == 0 && i == 0)
1205 if (subtract_one && i == 0)
1207 else if (subtract_one)
1208 p = le64toh(array->entry_array.items[i-1]);
1210 p = le64toh(array->entry_array.items[i]);
1212 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1223 *idx = t + i - (subtract_one ? 1 : 0);
1228 static int generic_array_bisect_plus_one(JournalFile *f,
1233 int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1234 direction_t direction,
1242 assert(test_object);
1247 /* This bisects the array in object 'first', but first checks
1249 r = test_object(f, extra, needle);
1252 else if (r == TEST_FOUND) {
1255 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
1269 } else if (r == TEST_RIGHT)
1272 r = generic_array_bisect(f, first, n-1, needle, test_object, direction, ret, offset, idx);
1280 static int test_object_seqnum(JournalFile *f, uint64_t p, uint64_t needle) {
1287 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1291 if (le64toh(o->entry.seqnum) == needle)
1293 else if (le64toh(o->entry.seqnum) < needle)
1299 int journal_file_move_to_entry_by_seqnum(
1302 direction_t direction,
1306 return generic_array_bisect(f,
1307 le64toh(f->header->entry_array_offset),
1308 le64toh(f->header->n_entries),
1315 static int test_object_realtime(JournalFile *f, uint64_t p, uint64_t needle) {
1322 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1326 if (le64toh(o->entry.realtime) == needle)
1328 else if (le64toh(o->entry.realtime) < needle)
1334 int journal_file_move_to_entry_by_realtime(
1337 direction_t direction,
1341 return generic_array_bisect(f,
1342 le64toh(f->header->entry_array_offset),
1343 le64toh(f->header->n_entries),
1345 test_object_realtime,
1350 static int test_object_monotonic(JournalFile *f, uint64_t p, uint64_t needle) {
1357 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1361 if (le64toh(o->entry.monotonic) == needle)
1363 else if (le64toh(o->entry.monotonic) < needle)
1369 int journal_file_move_to_entry_by_monotonic(
1373 direction_t direction,
1377 char t[8+32+1] = "_BOOT_ID=";
1381 sd_id128_to_string(boot_id, t + 8);
1383 r = journal_file_find_data_object(f, t, strlen(t), &o, NULL);
1389 return generic_array_bisect_plus_one(f,
1390 le64toh(o->data.entry_offset),
1391 le64toh(o->data.entry_array_offset),
1392 le64toh(o->data.n_entries),
1394 test_object_monotonic,
1399 static int test_object_offset(JournalFile *f, uint64_t p, uint64_t needle) {
1405 else if (p < needle)
1411 int journal_file_next_entry(
1413 Object *o, uint64_t p,
1414 direction_t direction,
1415 Object **ret, uint64_t *offset) {
1421 assert(p > 0 || !o);
1423 n = le64toh(f->header->n_entries);
1428 i = direction == DIRECTION_DOWN ? 0 : n - 1;
1430 if (o->object.type != OBJECT_ENTRY)
1433 r = generic_array_bisect(f,
1434 le64toh(f->header->entry_array_offset),
1435 le64toh(f->header->n_entries),
1444 if (direction == DIRECTION_DOWN) {
1457 /* And jump to it */
1458 return generic_array_get(f,
1459 le64toh(f->header->entry_array_offset),
1464 int journal_file_skip_entry(
1466 Object *o, uint64_t p,
1468 Object **ret, uint64_t *offset) {
1477 if (o->object.type != OBJECT_ENTRY)
1480 r = generic_array_bisect(f,
1481 le64toh(f->header->entry_array_offset),
1482 le64toh(f->header->n_entries),
1491 /* Calculate new index */
1493 if ((uint64_t) -skip >= i)
1496 i = i - (uint64_t) -skip;
1498 i += (uint64_t) skip;
1500 n = le64toh(f->header->n_entries);
1507 return generic_array_get(f,
1508 le64toh(f->header->entry_array_offset),
1513 int journal_file_next_entry_for_data(
1515 Object *o, uint64_t p,
1516 uint64_t data_offset,
1517 direction_t direction,
1518 Object **ret, uint64_t *offset) {
1525 assert(p > 0 || !o);
1527 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1531 n = le64toh(d->data.n_entries);
1536 i = direction == DIRECTION_DOWN ? 0 : n - 1;
1538 if (o->object.type != OBJECT_ENTRY)
1541 r = generic_array_bisect_plus_one(f,
1542 le64toh(d->data.entry_offset),
1543 le64toh(d->data.entry_array_offset),
1544 le64toh(d->data.n_entries),
1554 if (direction == DIRECTION_DOWN) {
1568 return generic_array_get_plus_one(f,
1569 le64toh(d->data.entry_offset),
1570 le64toh(d->data.entry_array_offset),
1575 int journal_file_move_to_entry_by_seqnum_for_data(
1577 uint64_t data_offset,
1579 direction_t direction,
1580 Object **ret, uint64_t *offset) {
1585 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1589 return generic_array_bisect_plus_one(f,
1590 le64toh(d->data.entry_offset),
1591 le64toh(d->data.entry_array_offset),
1592 le64toh(d->data.n_entries),
1599 int journal_file_move_to_entry_by_realtime_for_data(
1601 uint64_t data_offset,
1603 direction_t direction,
1604 Object **ret, uint64_t *offset) {
1609 r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1613 return generic_array_bisect_plus_one(f,
1614 le64toh(d->data.entry_offset),
1615 le64toh(d->data.entry_array_offset),
1616 le64toh(d->data.n_entries),
1618 test_object_realtime,
1623 void journal_file_dump(JournalFile *f) {
1624 char a[33], b[33], c[33];
1631 printf("File Path: %s\n"
1635 "Arena size: %llu\n"
1639 sd_id128_to_string(f->header->file_id, a),
1640 sd_id128_to_string(f->header->machine_id, b),
1641 sd_id128_to_string(f->header->boot_id, c),
1642 (unsigned long long) le64toh(f->header->arena_size),
1643 (unsigned long) le64toh(f->header->n_objects),
1644 (unsigned long) le64toh(f->header->n_entries));
1646 p = le64toh(f->header->arena_offset);
1648 r = journal_file_move_to_object(f, -1, p, &o);
1652 switch (o->object.type) {
1655 printf("Type: OBJECT_UNUSED\n");
1659 printf("Type: OBJECT_DATA\n");
1663 printf("Type: OBJECT_ENTRY %llu %llu %llu\n",
1664 (unsigned long long) le64toh(o->entry.seqnum),
1665 (unsigned long long) le64toh(o->entry.monotonic),
1666 (unsigned long long) le64toh(o->entry.realtime));
1669 case OBJECT_FIELD_HASH_TABLE:
1670 printf("Type: OBJECT_FIELD_HASH_TABLE\n");
1673 case OBJECT_DATA_HASH_TABLE:
1674 printf("Type: OBJECT_DATA_HASH_TABLE\n");
1677 case OBJECT_ENTRY_ARRAY:
1678 printf("Type: OBJECT_ENTRY_ARRAY\n");
1682 if (o->object.flags & OBJECT_COMPRESSED)
1683 printf("Flags: COMPRESSED\n");
1685 if (p == le64toh(f->header->tail_object_offset))
1688 p = p + ALIGN64(le64toh(o->object.size));
1693 log_error("File corrupt");
1696 int journal_file_open(
1700 JournalFile *template,
1701 JournalFile **ret) {
1705 bool newly_created = false;
1709 if ((flags & O_ACCMODE) != O_RDONLY &&
1710 (flags & O_ACCMODE) != O_RDWR)
1713 f = new0(JournalFile, 1);
1720 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1721 f->prot = prot_from_flags(flags);
1723 f->path = strdup(fname);
1729 f->fd = open(f->path, f->flags|O_CLOEXEC, f->mode);
1735 if (fstat(f->fd, &f->last_stat) < 0) {
1740 if (f->last_stat.st_size == 0 && f->writable) {
1741 newly_created = true;
1743 r = journal_file_init_header(f, template);
1747 if (fstat(f->fd, &f->last_stat) < 0) {
1753 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1758 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1759 if (f->header == MAP_FAILED) {
1765 if (!newly_created) {
1766 r = journal_file_verify_header(f);
1772 r = journal_file_refresh_header(f);
1777 if (newly_created) {
1779 r = journal_file_setup_field_hash_table(f);
1783 r = journal_file_setup_data_hash_table(f);
1788 r = journal_file_map_field_hash_table(f);
1792 r = journal_file_map_data_hash_table(f);
1802 journal_file_close(f);
1807 int journal_file_rotate(JournalFile **f) {
1810 JournalFile *old_file, *new_file = NULL;
1818 if (!old_file->writable)
1821 if (!endswith(old_file->path, ".journal"))
1824 l = strlen(old_file->path);
1826 p = new(char, l + 1 + 16 + 1 + 32 + 1 + 16 + 1);
1830 memcpy(p, old_file->path, l - 8);
1832 sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1);
1833 snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
1834 "-%016llx-%016llx.journal",
1835 (unsigned long long) le64toh((*f)->header->seqnum),
1836 (unsigned long long) le64toh((*f)->header->tail_entry_realtime));
1838 r = rename(old_file->path, p);
1844 old_file->header->state = le32toh(STATE_ARCHIVED);
1846 r = journal_file_open(old_file->path, old_file->flags, old_file->mode, old_file, &new_file);
1847 journal_file_close(old_file);
1853 struct vacuum_info {
1858 sd_id128_t seqnum_id;
1862 static int vacuum_compare(const void *_a, const void *_b) {
1863 const struct vacuum_info *a, *b;
1868 if (sd_id128_equal(a->seqnum_id, b->seqnum_id)) {
1869 if (a->seqnum < b->seqnum)
1871 else if (a->seqnum > b->seqnum)
1877 if (a->realtime < b->realtime)
1879 else if (a->realtime > b->realtime)
1882 return memcmp(&a->seqnum_id, &b->seqnum_id, 16);
1885 int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t min_free) {
1888 struct vacuum_info *list = NULL;
1889 unsigned n_list = 0, n_allocated = 0, i;
1897 d = opendir(directory);
1903 struct dirent buf, *de;
1907 unsigned long long seqnum, realtime;
1908 sd_id128_t seqnum_id;
1910 k = readdir_r(d, &buf, &de);
1919 if (!dirent_is_file_with_suffix(de, ".journal"))
1922 q = strlen(de->d_name);
1924 if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8)
1927 if (de->d_name[q-8-16-1] != '-' ||
1928 de->d_name[q-8-16-1-16-1] != '-' ||
1929 de->d_name[q-8-16-1-16-1-32-1] != '@')
1932 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
1935 if (!S_ISREG(st.st_mode))
1938 p = strdup(de->d_name);
1944 de->d_name[q-8-16-1-16-1] = 0;
1945 if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) {
1950 if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) {
1955 if (n_list >= n_allocated) {
1956 struct vacuum_info *j;
1958 n_allocated = MAX(n_allocated * 2U, 8U);
1959 j = realloc(list, n_allocated * sizeof(struct vacuum_info));
1969 list[n_list].filename = p;
1970 list[n_list].usage = (uint64_t) st.st_blksize * (uint64_t) st.st_blocks;
1971 list[n_list].seqnum = seqnum;
1972 list[n_list].realtime = realtime;
1973 list[n_list].seqnum_id = seqnum_id;
1975 sum += list[n_list].usage;
1980 qsort(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
1982 for(i = 0; i < n_list; i++) {
1985 if (fstatvfs(dirfd(d), &ss) < 0) {
1990 if (sum <= max_use &&
1991 (uint64_t) ss.f_bavail * (uint64_t) ss.f_bsize >= min_free)
1994 if (unlinkat(dirfd(d), list[i].filename, 0) >= 0) {
1995 log_debug("Deleted archived journal %s/%s.", directory, list[i].filename);
1996 sum -= list[i].usage;
1997 } else if (errno != ENOENT)
1998 log_warning("Failed to delete %s/%s: %m", directory, list[i].filename);
2002 for (i = 0; i < n_list; i++)
2003 free(list[i].filename);
2013 int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint64_t p, uint64_t *seqnum, Object **ret, uint64_t *offset) {
2015 uint64_t q, xor_hash = 0;
2028 ts.monotonic = le64toh(o->entry.monotonic);
2029 ts.realtime = le64toh(o->entry.realtime);
2031 if (to->tail_entry_monotonic_valid &&
2032 ts.monotonic < le64toh(to->header->tail_entry_monotonic))
2035 if (ts.realtime < le64toh(to->header->tail_entry_realtime))
2038 n = journal_file_entry_n_items(o);
2039 items = alloca(sizeof(EntryItem) * n);
2041 for (i = 0; i < n; i++) {
2042 uint64_t le_hash, l, h;
2047 q = le64toh(o->entry.items[i].object_offset);
2048 le_hash = o->entry.items[i].hash;
2050 r = journal_file_move_to_object(from, OBJECT_DATA, q, &o);
2054 if (le_hash != o->data.hash)
2057 l = le64toh(o->object.size) - offsetof(Object, data.payload);
2060 /* We hit the limit on 32bit machines */
2061 if ((uint64_t) t != l)
2064 if (o->object.flags & OBJECT_COMPRESSED) {
2068 if (!uncompress_blob(o->data.payload, l, &from->compress_buffer, &from->compress_buffer_size, &rsize))
2071 data = from->compress_buffer;
2074 return -EPROTONOSUPPORT;
2077 data = o->data.payload;
2079 r = journal_file_append_data(to, data, l, &u, &h);
2083 xor_hash ^= le64toh(u->data.hash);
2084 items[i].object_offset = htole64(h);
2085 items[i].hash = u->data.hash;
2087 r = journal_file_move_to_object(from, OBJECT_ENTRY, p, &o);
2092 return journal_file_append_entry_internal(to, &ts, xor_hash, items, n, seqnum, ret, offset);
2095 void journal_default_metrics(JournalMetrics *m, int fd) {
2096 uint64_t fs_size = 0;
2098 char a[64], b[64], c[64], d[64];
2103 if (fstatvfs(fd, &ss) >= 0)
2104 fs_size = ss.f_frsize * ss.f_blocks;
2106 if (m->max_use == (uint64_t) -1) {
2109 m->max_use = PAGE_ALIGN(fs_size / 10); /* 10% of file system size */
2111 if (m->max_use > DEFAULT_MAX_USE_UPPER)
2112 m->max_use = DEFAULT_MAX_USE_UPPER;
2114 if (m->max_use < DEFAULT_MAX_USE_LOWER)
2115 m->max_use = DEFAULT_MAX_USE_LOWER;
2117 m->max_use = DEFAULT_MAX_USE_LOWER;
2119 m->max_use = PAGE_ALIGN(m->max_use);
2121 if (m->max_use < JOURNAL_FILE_SIZE_MIN*2)
2122 m->max_use = JOURNAL_FILE_SIZE_MIN*2;
2125 if (m->max_size == (uint64_t) -1) {
2126 m->max_size = PAGE_ALIGN(m->max_use / 8); /* 8 chunks */
2128 if (m->max_size > DEFAULT_MAX_SIZE_UPPER)
2129 m->max_size = DEFAULT_MAX_SIZE_UPPER;
2131 m->max_size = PAGE_ALIGN(m->max_size);
2133 if (m->max_size < JOURNAL_FILE_SIZE_MIN)
2134 m->max_size = JOURNAL_FILE_SIZE_MIN;
2136 if (m->max_size*2 > m->max_use)
2137 m->max_use = m->max_size*2;
2139 if (m->min_size == (uint64_t) -1)
2140 m->min_size = JOURNAL_FILE_SIZE_MIN;
2142 m->min_size = PAGE_ALIGN(m->min_size);
2144 if (m->min_size < JOURNAL_FILE_SIZE_MIN)
2145 m->min_size = JOURNAL_FILE_SIZE_MIN;
2147 if (m->min_size > m->max_size)
2148 m->max_size = m->min_size;
2151 if (m->keep_free == (uint64_t) -1) {
2154 m->keep_free = PAGE_ALIGN(fs_size / 20); /* 5% of file system size */
2156 if (m->keep_free > DEFAULT_KEEP_FREE_UPPER)
2157 m->keep_free = DEFAULT_KEEP_FREE_UPPER;
2160 m->keep_free = DEFAULT_KEEP_FREE;
2163 log_debug("Fixed max_use=%s max_size=%s min_size=%s keep_free=%s",
2164 format_bytes(a, sizeof(a), m->max_use),
2165 format_bytes(b, sizeof(b), m->max_size),
2166 format_bytes(c, sizeof(c), m->min_size),
2167 format_bytes(d, sizeof(d), m->keep_free));