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_HASH_TABLE_SIZE (2047ULL*16ULL)
39 #define DEFAULT_BISECT_TABLE_SIZE ((DEFAULT_ARENA_MAX_SIZE/(64ULL*1024ULL))*8ULL)
41 #define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
43 static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
45 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
47 void journal_file_close(JournalFile *f) {
51 close_nointr_nofail(f->fd);
54 munmap(f->header, PAGE_ALIGN(sizeof(Header)));
56 if (f->hash_table_window)
57 munmap(f->hash_table_window, f->hash_table_window_size);
59 if (f->bisect_table_window)
60 munmap(f->bisect_table_window, f->bisect_table_window_size);
63 munmap(f->window, f->window_size);
69 static int journal_file_init_header(JournalFile *f) {
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);
87 h.seqnum_id = h.file_id;
89 k = pwrite(f->fd, &h, sizeof(h), 0);
99 static int journal_file_refresh_header(JournalFile *f) {
104 r = sd_id128_get_machine(&f->header->machine_id);
108 r = sd_id128_get_boot(&f->header->boot_id);
112 f->header->state = htole32(STATE_ONLINE);
116 static int journal_file_verify_header(JournalFile *f) {
119 if (memcmp(f->header, signature, 8))
122 if (f->header->incompatible_flags != 0)
123 return -EPROTONOSUPPORT;
125 if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
130 sd_id128_t machine_id;
133 r = sd_id128_get_machine(&machine_id);
137 if (!sd_id128_equal(machine_id, f->header->machine_id))
140 state = le32toh(f->header->state);
142 if (state == STATE_ONLINE)
143 log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f->path);
144 else if (state == STATE_ARCHIVED)
146 else if (state != STATE_OFFLINE)
147 log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
153 static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
155 uint64_t old_size, new_size;
159 if (offset < le64toh(f->header->arena_offset))
162 new_size = PAGE_ALIGN(offset + size);
164 /* We assume that this file is not sparse, and we know that
165 * for sure, since we always call posix_fallocate()
169 le64toh(f->header->arena_offset) +
170 le64toh(f->header->arena_size);
172 if (old_size >= new_size)
175 asize = new_size - le64toh(f->header->arena_offset);
177 if (asize > le64toh(f->header->arena_min_size)) {
180 if (fstatvfs(f->fd, &svfs) >= 0) {
183 available = svfs.f_bfree * svfs.f_bsize;
185 if (available >= f->header->arena_keep_free)
186 available -= f->header->arena_keep_free;
190 if (new_size - old_size > available)
195 if (asize > le64toh(f->header->arena_max_size))
198 if (posix_fallocate(f->fd, old_size, new_size - old_size) < 0)
201 if (fstat(f->fd, &f->last_stat) < 0)
204 f->header->arena_size = htole64(asize);
209 static int journal_file_map(
218 uint64_t woffset, wsize;
225 woffset = offset & ~((uint64_t) page_size() - 1ULL);
226 wsize = size + (offset - woffset);
227 wsize = PAGE_ALIGN(wsize);
229 window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
230 if (window == MAP_FAILED)
242 *ret = (uint8_t*) window + (offset - woffset);
247 static int journal_file_move_to(JournalFile *f, uint64_t offset, uint64_t size, void **ret) {
255 if (_likely_(f->window &&
256 f->window_offset <= offset &&
257 f->window_offset+f->window_size >= offset + size)) {
259 *ret = (uint8_t*) f->window + (offset - f->window_offset);
264 if (munmap(f->window, f->window_size) < 0)
268 f->window_size = f->window_offset = 0;
271 if (size < DEFAULT_WINDOW_SIZE) {
272 /* If the default window size is larger then what was
273 * asked for extend the mapping a bit in the hope to
274 * minimize needed remappings later on. We add half
275 * the window space before and half behind the
276 * requested mapping */
278 delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
284 size += (DEFAULT_WINDOW_SIZE - delta);
288 r = journal_file_map(f,
290 &f->window, &f->window_offset, &f->window_size,
296 *ret = (uint8_t*) p + delta;
300 static bool verify_hash(Object *o) {
305 t = le64toh(o->object.type);
306 if (t == OBJECT_DATA) {
309 s = le64toh(o->object.size);
311 h1 = le64toh(o->data.hash);
312 h2 = hash64(o->data.payload, s - offsetof(Object, data.payload));
320 int journal_file_move_to_object(JournalFile *f, uint64_t offset, int type, Object **ret) {
329 r = journal_file_move_to(f, offset, sizeof(ObjectHeader), &t);
334 s = le64toh(o->object.size);
336 if (s < sizeof(ObjectHeader))
339 if (type >= 0 && le64toh(o->object.type) != type)
342 if (s > sizeof(ObjectHeader)) {
343 r = journal_file_move_to(f, offset, s, &t);
357 static uint64_t journal_file_seqnum(JournalFile *f) {
362 r = le64toh(f->header->seqnum) + 1;
363 f->header->seqnum = htole64(r);
368 static int journal_file_append_object(JournalFile *f, uint64_t size, Object **ret, uint64_t *offset) {
375 assert(size >= sizeof(ObjectHeader));
379 p = le64toh(f->header->tail_object_offset);
382 p = le64toh(f->header->arena_offset);
384 r = journal_file_move_to_object(f, p, -1, &tail);
388 p += ALIGN64(le64toh(tail->object.size));
391 r = journal_file_allocate(f, p, size);
395 r = journal_file_move_to(f, p, size, &t);
402 o->object.type = htole64(OBJECT_UNUSED);
403 zero(o->object.reserved);
404 o->object.size = htole64(size);
406 f->header->tail_object_offset = htole64(p);
407 if (f->header->head_object_offset == 0)
408 f->header->head_object_offset = htole64(p);
410 f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
418 static int journal_file_setup_hash_table(JournalFile *f) {
425 s = DEFAULT_HASH_TABLE_SIZE;
426 r = journal_file_append_object(f, offsetof(Object, hash_table.table) + s, &o, &p);
430 o->object.type = htole64(OBJECT_HASH_TABLE);
431 memset(o->hash_table.table, 0, s);
433 f->header->hash_table_offset = htole64(p + offsetof(Object, hash_table.table));
434 f->header->hash_table_size = htole64(s);
439 static int journal_file_setup_bisect_table(JournalFile *f) {
446 s = DEFAULT_BISECT_TABLE_SIZE;
447 r = journal_file_append_object(f, offsetof(Object, bisect_table.table) + s, &o, &p);
451 o->object.type = htole64(OBJECT_BISECT_TABLE);
452 memset(o->bisect_table.table, 0, s);
454 f->header->bisect_table_offset = htole64(p + offsetof(Object, bisect_table.table));
455 f->header->bisect_table_size = htole64(s);
460 static int journal_file_map_hash_table(JournalFile *f) {
467 p = le64toh(f->header->hash_table_offset);
468 s = le64toh(f->header->hash_table_size);
470 r = journal_file_map(f,
472 &f->hash_table_window, NULL, &f->hash_table_window_size,
481 static int journal_file_map_bisect_table(JournalFile *f) {
488 p = le64toh(f->header->bisect_table_offset);
489 s = le64toh(f->header->bisect_table_size);
491 r = journal_file_map(f,
493 &f->bisect_table_window, NULL, &f->bisect_table_window_size,
503 static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash_index) {
510 assert(o->object.type == htole64(OBJECT_DATA));
512 o->data.head_entry_offset = o->data.tail_entry_offset = 0;
513 o->data.next_hash_offset = 0;
515 p = le64toh(f->hash_table[hash_index].tail_hash_offset);
517 /* Only entry in the hash table is easy */
519 o->data.prev_hash_offset = 0;
520 f->hash_table[hash_index].head_hash_offset = htole64(offset);
522 o->data.prev_hash_offset = htole64(p);
524 /* Temporarily move back to the previous data object,
525 * to patch in pointer */
527 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
531 o->data.next_hash_offset = offset;
533 r = journal_file_move_to_object(f, offset, OBJECT_DATA, &o);
538 f->hash_table[hash_index].tail_hash_offset = htole64(offset);
543 static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
544 uint64_t hash, h, p, np;
550 assert(data || size == 0);
552 osize = offsetof(Object, data.payload) + size;
554 hash = hash64(data, size);
555 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
556 p = le64toh(f->hash_table[h].head_hash_offset);
559 /* Look for this data object in the hash table */
561 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
565 if (le64toh(o->object.size) == osize &&
566 memcmp(o->data.payload, data, size) == 0) {
568 if (le64toh(o->data.hash) != hash)
580 p = le64toh(o->data.next_hash_offset);
583 r = journal_file_append_object(f, osize, &o, &np);
587 o->object.type = htole64(OBJECT_DATA);
588 o->data.hash = htole64(hash);
589 memcpy(o->data.payload, data, size);
591 r = journal_file_link_data(f, o, np, h);
604 uint64_t journal_file_entry_n_items(Object *o) {
606 assert(o->object.type == htole64(OBJECT_ENTRY));
608 return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
611 static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
618 p = le64toh(o->entry.items[i].object_offset);
622 o->entry.items[i].next_entry_offset = 0;
624 /* Move to the data object */
625 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
629 q = le64toh(o->data.tail_entry_offset);
630 o->data.tail_entry_offset = htole64(offset);
633 o->data.head_entry_offset = htole64(offset);
637 /* Move to previous entry */
638 r = journal_file_move_to_object(f, q, OBJECT_ENTRY, &o);
642 n = journal_file_entry_n_items(o);
643 for (j = 0; j < n; j++)
644 if (le64toh(o->entry.items[j].object_offset) == p)
650 o->entry.items[j].next_entry_offset = offset;
653 /* Move back to original entry */
654 r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
658 o->entry.items[i].prev_entry_offset = q;
662 static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
663 uint64_t p, i, n, k, a, b;
669 assert(o->object.type == htole64(OBJECT_ENTRY));
671 /* Link up the entry itself */
672 p = le64toh(f->header->tail_entry_offset);
674 o->entry.prev_entry_offset = f->header->tail_entry_offset;
675 o->entry.next_entry_offset = 0;
678 f->header->head_entry_offset = htole64(offset);
680 /* Temporarily move back to the previous entry, to
681 * patch in pointer */
683 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
687 o->entry.next_entry_offset = htole64(offset);
689 r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
694 f->header->tail_entry_offset = htole64(offset);
696 /* Link up the items */
697 n = journal_file_entry_n_items(o);
698 for (i = 0; i < n; i++) {
699 r = journal_file_link_entry_item(f, o, offset, i);
704 /* Link up the entry in the bisect table */
705 n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
706 k = le64toh(f->header->arena_max_size) / n;
708 a = (le64toh(f->header->last_bisect_offset) + k - 1) / k;
712 f->bisect_table[a] = htole64(offset);
714 f->header->last_bisect_offset = htole64(offset + le64toh(o->object.size));
719 static int journal_file_append_entry_internal(
721 const dual_timestamp *ts,
723 const EntryItem items[], unsigned n_items,
724 Object **ret, uint64_t *offset) {
731 assert(items || n_items == 0);
733 osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
735 r = journal_file_append_object(f, osize, &o, &np);
739 o->object.type = htole64(OBJECT_ENTRY);
740 o->entry.seqnum = htole64(journal_file_seqnum(f));
741 memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
742 o->entry.realtime = ts ? htole64(ts->realtime) : 0;
743 o->entry.monotonic = ts ? htole64(ts->monotonic) : 0;
744 o->entry.xor_hash = htole64(xor_hash);
745 o->entry.boot_id = f->header->boot_id;
747 r = journal_file_link_entry(f, o, np);
760 int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset) {
764 uint64_t xor_hash = 0;
767 assert(iovec || n_iovec == 0);
769 items = new(EntryItem, n_iovec);
773 for (i = 0; i < n_iovec; i++) {
777 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
781 xor_hash ^= le64toh(o->data.hash);
782 items[i].object_offset = htole64(p);
785 r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, ret, offset);
793 int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset) {
795 uint64_t lower, upper, p, n, k;
800 n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
801 k = le64toh(f->header->arena_max_size) / n;
804 upper = le64toh(f->header->last_bisect_offset)/k+1;
806 while (lower < upper) {
807 k = (upper + lower) / 2;
808 p = le64toh(f->bisect_table[k]);
815 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
819 if (o->entry.seqnum == seqnum) {
827 } else if (seqnum < o->entry.seqnum)
829 else if (seqnum > o->entry.seqnum)
833 assert(lower == upper);
838 /* The object we are looking for is between
839 * bisect_table[lower-1] and bisect_table[lower] */
841 p = le64toh(f->bisect_table[lower-1]);
844 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
848 if (o->entry.seqnum == seqnum) {
857 } if (seqnum < o->entry.seqnum)
860 if (o->entry.next_entry_offset == 0)
863 p = le64toh(o->entry.next_entry_offset);
869 int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
876 np = le64toh(f->header->head_entry_offset);
878 if (le64toh(o->object.type) != OBJECT_ENTRY)
881 np = le64toh(o->entry.next_entry_offset);
887 r = journal_file_move_to_object(f, np, OBJECT_ENTRY, &o);
900 int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
907 np = le64toh(f->header->tail_entry_offset);
909 if (le64toh(o->object.type) != OBJECT_ENTRY)
912 np = le64toh(o->entry.prev_entry_offset);
918 r = journal_file_move_to_object(f, np, OBJECT_ENTRY, &o);
931 int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
932 uint64_t p, osize, hash, h;
936 assert(data || size == 0);
938 osize = offsetof(Object, data.payload) + size;
940 hash = hash64(data, size);
941 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
942 p = le64toh(f->hash_table[h].head_hash_offset);
947 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
951 if (le64toh(o->object.size) == osize &&
952 memcmp(o->data.payload, data, size) == 0) {
954 if (le64toh(o->data.hash) != hash)
957 if (o->data.head_entry_offset == 0)
960 p = le64toh(o->data.head_entry_offset);
961 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
974 p = le64toh(o->data.next_hash_offset);
980 int journal_file_find_last_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
981 uint64_t p, osize, hash, h;
985 assert(data || size == 0);
987 osize = offsetof(Object, data.payload) + size;
989 hash = hash64(data, size);
990 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
991 p = le64toh(f->hash_table[h].tail_hash_offset);
996 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
1000 if (le64toh(o->object.size) == osize &&
1001 memcmp(o->data.payload, data, size) == 0) {
1003 if (le64toh(o->data.hash) != hash)
1006 if (o->data.tail_entry_offset == 0)
1009 p = le64toh(o->data.tail_entry_offset);
1010 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
1023 p = le64toh(o->data.prev_hash_offset);
1029 void journal_file_dump(JournalFile *f) {
1030 char a[33], b[33], c[33];
1037 printf("File ID: %s\n"
1040 "Arena size: %llu\n",
1041 sd_id128_to_string(f->header->file_id, a),
1042 sd_id128_to_string(f->header->machine_id, b),
1043 sd_id128_to_string(f->header->boot_id, c),
1044 (unsigned long long) le64toh(f->header->arena_size));
1046 p = le64toh(f->header->head_object_offset);
1048 r = journal_file_move_to_object(f, p, -1, &o);
1052 switch (o->object.type) {
1055 printf("Type: OBJECT_UNUSED\n");
1059 printf("Type: OBJECT_DATA\n");
1063 printf("Type: OBJECT_ENTRY %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
1066 case OBJECT_HASH_TABLE:
1067 printf("Type: OBJECT_HASH_TABLE\n");
1070 case OBJECT_BISECT_TABLE:
1071 printf("Type: OBJECT_BISECT_TABLE\n");
1075 if (p == le64toh(f->header->tail_object_offset))
1078 p = p + ALIGN64(le64toh(o->object.size));
1083 log_error("File corrupt");
1086 int journal_file_open(
1090 JournalFile **ret) {
1094 bool newly_created = false;
1098 if ((flags & O_ACCMODE) != O_RDONLY &&
1099 (flags & O_ACCMODE) != O_RDWR)
1102 f = new0(JournalFile, 1);
1106 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1107 f->prot = prot_from_flags(flags);
1109 f->fd = open(fname, flags|O_CLOEXEC, mode);
1115 f->path = strdup(fname);
1121 if (fstat(f->fd, &f->last_stat) < 0) {
1126 if (f->last_stat.st_size == 0 && f->writable) {
1127 newly_created = true;
1129 r = journal_file_init_header(f);
1133 if (fstat(f->fd, &f->last_stat) < 0) {
1139 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1144 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1145 if (f->header == MAP_FAILED) {
1151 if (!newly_created) {
1152 r = journal_file_verify_header(f);
1158 r = journal_file_refresh_header(f);
1163 if (newly_created) {
1165 r = journal_file_setup_hash_table(f);
1169 r = journal_file_setup_bisect_table(f);
1174 r = journal_file_map_hash_table(f);
1178 r = journal_file_map_bisect_table(f);
1188 journal_file_close(f);