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 "sd-journal.h"
31 #include "journal-def.h"
32 #include "journal-private.h"
36 #define DEFAULT_ARENA_MAX_SIZE (16ULL*1024ULL*1024ULL*1024ULL)
37 #define DEFAULT_ARENA_MIN_SIZE (256ULL*1024ULL)
38 #define DEFAULT_ARENA_KEEP_FREE (1ULL*1024ULL*1024ULL)
40 #define DEFAULT_HASH_TABLE_SIZE (2047ULL*16ULL)
41 #define DEFAULT_BISECT_TABLE_SIZE ((DEFAULT_ARENA_MAX_SIZE/(64ULL*1024ULL))*8ULL)
43 #define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
50 struct stat last_stat;
57 void *hash_table_window;
58 uint64_t hash_table_window_size;
60 uint64_t *bisect_table;
61 void *bisect_table_window;
62 uint64_t bisect_table_window_size;
65 uint64_t window_offset;
69 uint64_t current_offset;
71 LIST_FIELDS(JournalFile, files);
75 LIST_HEAD(JournalFile, files);
78 static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
80 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
82 void journal_file_close(JournalFile *f) {
86 LIST_REMOVE(JournalFile, files, f->journal->files, f);
89 close_nointr_nofail(f->fd);
92 munmap(f->header, PAGE_ALIGN(sizeof(Header)));
94 if (f->hash_table_window)
95 munmap(f->hash_table_window, f->hash_table_window_size);
97 if (f->bisect_table_window)
98 munmap(f->bisect_table_window, f->bisect_table_window_size);
101 munmap(f->window, f->window_size);
107 static int journal_file_init_header(JournalFile *f) {
115 memcpy(h.signature, signature, 8);
116 h.arena_offset = htole64(ALIGN64(sizeof(h)));
117 h.arena_max_size = htole64(DEFAULT_ARENA_MAX_SIZE);
118 h.arena_min_size = htole64(DEFAULT_ARENA_MIN_SIZE);
119 h.arena_keep_free = htole64(DEFAULT_ARENA_KEEP_FREE);
121 r = sd_id128_randomize(&h.file_id);
125 k = pwrite(f->fd, &h, sizeof(h), 0);
135 static int journal_file_refresh_header(JournalFile *f) {
140 r = sd_id128_get_machine(&f->header->machine_id);
144 r = sd_id128_get_boot(&f->header->boot_id);
148 f->header->state = htole32(STATE_ONLINE);
152 static int journal_file_verify_header(JournalFile *f) {
155 if (memcmp(f->header, signature, 8))
158 if (f->header->incompatible_flags != 0)
159 return -EPROTONOSUPPORT;
161 if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
166 sd_id128_t machine_id;
169 r = sd_id128_get_machine(&machine_id);
173 if (!sd_id128_equal(machine_id, f->header->machine_id))
176 state = le32toh(f->header->state);
178 if (state == STATE_ONLINE)
179 log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f->path);
180 else if (state == STATE_ARCHIVED)
182 else if (state != STATE_OFFLINE)
183 log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
189 static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
191 uint64_t old_size, new_size;
195 if (offset < le64toh(f->header->arena_offset))
198 new_size = PAGE_ALIGN(offset + size);
200 /* We assume that this file is not sparse, and we know that
201 * for sure, since we alway call posix_fallocate()
205 le64toh(f->header->arena_offset) +
206 le64toh(f->header->arena_size);
208 if (old_size >= new_size)
211 asize = new_size - le64toh(f->header->arena_offset);
213 if (asize > le64toh(f->header->arena_min_size)) {
216 if (fstatvfs(f->fd, &svfs) >= 0) {
219 available = svfs.f_bfree * svfs.f_bsize;
221 if (available >= f->header->arena_keep_free)
222 available -= f->header->arena_keep_free;
226 if (new_size - old_size > available)
231 if (asize > le64toh(f->header->arena_max_size))
234 if (posix_fallocate(f->fd, 0, new_size) < 0)
237 if (fstat(f->fd, &f->last_stat) < 0)
240 f->header->arena_size = htole64(asize);
245 static int journal_file_map(
254 uint64_t woffset, wsize;
261 woffset = offset & ~((uint64_t) page_size() - 1ULL);
262 wsize = size + (offset - woffset);
263 wsize = PAGE_ALIGN(wsize);
265 window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
266 if (window == MAP_FAILED)
278 *ret = (uint8_t*) window + (offset - woffset);
283 static int journal_file_move_to(JournalFile *f, uint64_t offset, uint64_t size, void **ret) {
291 if (_likely_(f->window &&
292 f->window_offset <= offset &&
293 f->window_offset+f->window_size >= offset + size)) {
295 *ret = (uint8_t*) f->window + (offset - f->window_offset);
300 if (munmap(f->window, f->window_size) < 0)
304 f->window_size = f->window_offset = 0;
307 if (size < DEFAULT_WINDOW_SIZE) {
308 /* If the default window size is larger then what was
309 * asked for extend the mapping a bit in the hope to
310 * minimize needed remappings later on. We add half
311 * the window space before and half behind the
312 * requested mapping */
314 delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
320 size += (DEFAULT_WINDOW_SIZE - delta);
324 r = journal_file_map(f,
326 &f->window, &f->window_offset, &f->window_size,
332 *ret = (uint8_t*) p + delta;
336 static bool verify_hash(Object *o) {
341 t = le64toh(o->object.type);
342 if (t == OBJECT_DATA) {
345 s = le64toh(o->object.size);
347 h1 = le64toh(o->data.hash);
348 h2 = hash64(o->data.payload, s - offsetof(Object, data.payload));
356 int journal_file_move_to_object(JournalFile *f, uint64_t offset, Object **ret) {
365 r = journal_file_move_to(f, offset, sizeof(ObjectHeader), &t);
370 s = le64toh(o->object.size);
372 if (s < sizeof(ObjectHeader))
375 if (s > sizeof(ObjectHeader)) {
376 r = journal_file_move_to(f, offset, s, &t);
390 static uint64_t journal_file_seqnum(JournalFile *f) {
395 r = le64toh(f->header->seqnum) + 1;
396 f->header->seqnum = htole64(r);
401 static int journal_file_append_object(JournalFile *f, uint64_t size, Object **ret, uint64_t *offset) {
408 assert(size >= sizeof(ObjectHeader));
412 p = le64toh(f->header->tail_object_offset);
415 p = le64toh(f->header->arena_offset);
417 r = journal_file_move_to_object(f, p, &tail);
421 p += ALIGN64(le64toh(tail->object.size));
424 r = journal_file_allocate(f, p, size);
428 r = journal_file_move_to(f, p, size, &t);
435 o->object.type = htole64(OBJECT_UNUSED);
436 zero(o->object.reserved);
437 o->object.size = htole64(size);
439 f->header->tail_object_offset = htole64(p);
440 if (f->header->head_object_offset == 0)
441 f->header->head_object_offset = htole64(p);
443 f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
451 static int journal_file_setup_hash_table(JournalFile *f) {
458 s = DEFAULT_HASH_TABLE_SIZE;
459 r = journal_file_append_object(f, offsetof(Object, hash_table.table) + s, &o, &p);
463 o->object.type = htole64(OBJECT_HASH_TABLE);
464 memset(o->hash_table.table, 0, s);
466 f->header->hash_table_offset = htole64(p + offsetof(Object, hash_table.table));
467 f->header->hash_table_size = htole64(s);
472 static int journal_file_setup_bisect_table(JournalFile *f) {
479 s = DEFAULT_BISECT_TABLE_SIZE;
480 r = journal_file_append_object(f, offsetof(Object, bisect_table.table) + s, &o, &p);
484 o->object.type = htole64(OBJECT_BISECT_TABLE);
485 memset(o->bisect_table.table, 0, s);
487 f->header->bisect_table_offset = htole64(p + offsetof(Object, bisect_table.table));
488 f->header->bisect_table_size = htole64(s);
493 static int journal_file_map_hash_table(JournalFile *f) {
500 p = le64toh(f->header->hash_table_offset);
501 s = le64toh(f->header->hash_table_size);
503 r = journal_file_map(f,
505 &f->hash_table_window, NULL, &f->hash_table_window_size,
514 static int journal_file_map_bisect_table(JournalFile *f) {
521 p = le64toh(f->header->bisect_table_offset);
522 s = le64toh(f->header->bisect_table_size);
524 r = journal_file_map(f,
526 &f->bisect_table_window, NULL, &f->bisect_table_window_size,
536 static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash_index) {
543 assert(o->object.type == htole64(OBJECT_DATA));
545 o->data.head_entry_offset = o->data.tail_entry_offset = 0;
546 o->data.next_hash_offset = 0;
548 p = le64toh(f->hash_table[hash_index].tail_hash_offset);
550 /* Only entry in the hash table is easy */
552 o->data.prev_hash_offset = 0;
553 f->hash_table[hash_index].head_hash_offset = htole64(offset);
555 o->data.prev_hash_offset = htole64(p);
557 /* Temporarily move back to the previous data object,
558 * to patch in pointer */
560 r = journal_file_move_to_object(f, p, &o);
564 o->data.next_hash_offset = offset;
566 r = journal_file_move_to_object(f, offset, &o);
571 f->hash_table[hash_index].tail_hash_offset = htole64(offset);
576 static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
577 uint64_t hash, h, p, np;
583 assert(data || size == 0);
585 osize = offsetof(Object, data.payload) + size;
587 hash = hash64(data, size);
588 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
589 p = le64toh(f->hash_table[h].head_hash_offset);
592 /* Look for this data object in the hash table */
594 r = journal_file_move_to_object(f, p, &o);
598 if (le64toh(o->object.type) != OBJECT_DATA)
601 if (le64toh(o->object.size) == osize &&
602 memcmp(o->data.payload, data, size) == 0) {
604 if (le64toh(o->data.hash) != hash)
616 p = le64toh(o->data.next_hash_offset);
619 r = journal_file_append_object(f, osize, &o, &np);
623 o->object.type = htole64(OBJECT_DATA);
624 o->data.hash = htole64(hash);
625 memcpy(o->data.payload, data, size);
627 r = journal_file_link_data(f, o, np, h);
640 uint64_t journal_file_entry_n_items(Object *o) {
642 assert(o->object.type == htole64(OBJECT_ENTRY));
644 return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
647 static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
654 p = le64toh(o->entry.items[i].object_offset);
658 o->entry.items[i].next_entry_offset = 0;
660 /* Move to the data object */
661 r = journal_file_move_to_object(f, p, &o);
665 if (o->object.type != htole64(OBJECT_DATA))
668 q = le64toh(o->data.tail_entry_offset);
669 o->data.tail_entry_offset = htole64(offset);
672 o->data.head_entry_offset = htole64(offset);
676 /* Move to previous entry */
677 r = journal_file_move_to_object(f, q, &o);
681 if (o->object.type != htole64(OBJECT_ENTRY))
684 n = journal_file_entry_n_items(o);
685 for (j = 0; j < n; j++)
686 if (le64toh(o->entry.items[j].object_offset) == p)
692 o->entry.items[j].next_entry_offset = offset;
695 /* Move back to original entry */
696 r = journal_file_move_to_object(f, offset, &o);
700 o->entry.items[i].prev_entry_offset = q;
704 static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
705 uint64_t p, i, n, k, a, b;
711 assert(o->object.type == htole64(OBJECT_ENTRY));
713 /* Link up the entry itself */
714 p = le64toh(f->header->tail_entry_offset);
716 o->entry.prev_entry_offset = f->header->tail_entry_offset;
717 o->entry.next_entry_offset = 0;
720 f->header->head_entry_offset = htole64(offset);
722 /* Temporarily move back to the previous entry, to
723 * patch in pointer */
725 r = journal_file_move_to_object(f, p, &o);
729 o->entry.next_entry_offset = htole64(offset);
731 r = journal_file_move_to_object(f, offset, &o);
736 f->header->tail_entry_offset = htole64(offset);
738 /* Link up the items */
739 n = journal_file_entry_n_items(o);
740 for (i = 0; i < n; i++) {
741 r = journal_file_link_entry_item(f, o, offset, i);
746 /* Link up the entry in the bisect table */
747 n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
748 k = le64toh(f->header->arena_max_size) / n;
750 a = (le64toh(f->header->last_bisect_offset) + k - 1) / k;
754 f->bisect_table[a] = htole64(offset);
756 f->header->last_bisect_offset = htole64(offset + le64toh(o->object.size));
761 static int journal_file_append_entry_internal(JournalFile *f, const dual_timestamp *ts, const EntryItem items[], unsigned n_items, Object **ret, uint64_t *offset) {
768 assert(items || n_items == 0);
770 osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
772 r = journal_file_append_object(f, osize, &o, &np);
776 o->object.type = htole64(OBJECT_ENTRY);
777 o->entry.seqnum = htole64(journal_file_seqnum(f));
778 memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
779 o->entry.realtime = htole64(ts->realtime);
780 o->entry.monotonic = htole64(ts->monotonic);
782 r = journal_file_link_entry(f, o, np);
795 int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset) {
802 items = new(EntryItem, n_iovec);
806 for (i = 0; i < n_iovec; i++) {
809 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, NULL, &p);
813 items[i].object_offset = htole64(p);
816 r = journal_file_append_entry_internal(f, ts, items, n_iovec, ret, offset);
824 int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset) {
826 uint64_t lower, upper, p, n, k;
831 n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
832 k = le64toh(f->header->arena_max_size) / n;
835 upper = le64toh(f->header->last_bisect_offset)/k+1;
837 while (lower < upper) {
838 k = (upper + lower) / 2;
839 p = le64toh(f->bisect_table[k]);
846 r = journal_file_move_to_object(f, p, &o);
850 if (o->object.type != htole64(OBJECT_ENTRY))
853 if (o->entry.seqnum == seqnum) {
861 } else if (seqnum < o->entry.seqnum)
863 else if (seqnum > o->entry.seqnum)
867 assert(lower == upper);
872 /* The object we are looking for is between
873 * bisect_table[lower-1] and bisect_table[lower] */
875 p = le64toh(f->bisect_table[lower-1]);
878 r = journal_file_move_to_object(f, p, &o);
882 if (o->entry.seqnum == seqnum) {
891 } if (seqnum < o->entry.seqnum)
894 if (o->entry.next_entry_offset == 0)
897 p = le64toh(o->entry.next_entry_offset);
903 int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
910 np = le64toh(f->header->head_entry_offset);
912 if (le64toh(o->object.type) != OBJECT_ENTRY)
915 np = le64toh(o->entry.next_entry_offset);
921 r = journal_file_move_to_object(f, np, &o);
925 if (le64toh(o->object.type) != OBJECT_ENTRY)
937 int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
944 np = le64toh(f->header->tail_entry_offset);
946 if (le64toh(o->object.type) != OBJECT_ENTRY)
949 np = le64toh(o->entry.prev_entry_offset);
955 r = journal_file_move_to_object(f, np, &o);
959 if (le64toh(o->object.type) != OBJECT_ENTRY)
971 int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
972 uint64_t p, osize, hash, h;
976 assert(data || size == 0);
978 osize = offsetof(Object, data.payload) + size;
980 hash = hash64(data, size);
981 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
982 p = le64toh(f->hash_table[h].head_hash_offset);
987 r = journal_file_move_to_object(f, p, &o);
991 if (le64toh(o->object.type) != OBJECT_DATA)
994 if (le64toh(o->object.size) == osize &&
995 memcmp(o->data.payload, data, size) == 0) {
997 if (le64toh(o->data.hash) != hash)
1000 if (o->data.head_entry_offset == 0)
1003 p = le64toh(o->data.head_entry_offset);
1004 r = journal_file_move_to_object(f, p, &o);
1008 if (le64toh(o->object.type) != OBJECT_ENTRY)
1020 p = le64toh(o->data.next_hash_offset);
1026 int journal_file_find_last_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
1027 uint64_t p, osize, hash, h;
1031 assert(data || size == 0);
1033 osize = offsetof(Object, data.payload) + size;
1035 hash = hash64(data, size);
1036 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
1037 p = le64toh(f->hash_table[h].tail_hash_offset);
1042 r = journal_file_move_to_object(f, p, &o);
1046 if (le64toh(o->object.type) != OBJECT_DATA)
1049 if (le64toh(o->object.size) == osize &&
1050 memcmp(o->data.payload, data, size) == 0) {
1052 if (le64toh(o->data.hash) != hash)
1055 if (o->data.tail_entry_offset == 0)
1058 p = le64toh(o->data.tail_entry_offset);
1059 r = journal_file_move_to_object(f, p, &o);
1063 if (le64toh(o->object.type) != OBJECT_ENTRY)
1075 p = le64toh(o->data.prev_hash_offset);
1081 void journal_file_dump(JournalFile *f) {
1082 char a[33], b[33], c[33];
1089 printf("File ID: %s\n"
1092 "Arena size: %llu\n",
1093 sd_id128_to_string(f->header->file_id, a),
1094 sd_id128_to_string(f->header->machine_id, b),
1095 sd_id128_to_string(f->header->boot_id, c),
1096 (unsigned long long) le64toh(f->header->arena_size));
1098 p = le64toh(f->header->head_object_offset);
1100 r = journal_file_move_to_object(f, p, &o);
1104 switch (o->object.type) {
1107 printf("Type: OBJECT_UNUSED\n");
1111 printf("Type: OBJECT_DATA\n");
1115 printf("Type: OBJECT_ENTRY %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
1118 case OBJECT_HASH_TABLE:
1119 printf("Type: OBJECT_HASH_TABLE\n");
1122 case OBJECT_BISECT_TABLE:
1123 printf("Type: OBJECT_BISECT_TABLE\n");
1127 if (p == le64toh(f->header->tail_object_offset))
1130 p = p + ALIGN64(le64toh(o->object.size));
1135 log_error("File corrupt");
1138 int journal_file_open(
1143 JournalFile **ret) {
1147 bool newly_created = false;
1151 if ((flags & O_ACCMODE) != O_RDONLY &&
1152 (flags & O_ACCMODE) != O_RDWR)
1155 f = new0(JournalFile, 1);
1159 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1160 f->prot = prot_from_flags(flags);
1162 f->fd = open(fname, flags|O_CLOEXEC, mode);
1168 f->path = strdup(fname);
1174 if (fstat(f->fd, &f->last_stat) < 0) {
1179 if (f->last_stat.st_size == 0 && f->writable) {
1180 newly_created = true;
1182 r = journal_file_init_header(f);
1186 if (fstat(f->fd, &f->last_stat) < 0) {
1192 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1197 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1198 if (f->header == MAP_FAILED) {
1204 if (!newly_created) {
1205 r = journal_file_verify_header(f);
1211 r = journal_file_refresh_header(f);
1216 if (newly_created) {
1218 r = journal_file_setup_hash_table(f);
1222 r = journal_file_setup_bisect_table(f);
1227 r = journal_file_map_hash_table(f);
1231 r = journal_file_map_bisect_table(f);
1236 LIST_PREPEND(JournalFile, files, j->files, f);
1246 journal_file_close(f);
1251 int sd_journal_open(sd_journal **ret) {
1256 const char search_paths[] =
1257 "/run/log/journal\0"
1258 "/var/log/journal\0";
1262 j = new0(sd_journal, 1);
1266 NULSTR_FOREACH(p, search_paths) {
1271 if (errno != ENOENT && r == 0)
1278 struct dirent buf, *de;
1281 k = readdir_r(d, &buf, &de);
1292 if (!dirent_is_file_with_suffix(de, ".journal"))
1295 fn = join(p, "/", de->d_name, NULL);
1302 k = journal_file_open(j, fn, O_RDONLY, 0, NULL);
1303 if (k < 0 && r == 0)
1321 sd_journal_close(j);
1326 void sd_journal_close(sd_journal *j) {
1330 journal_file_close(j->files);