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(
763 const dual_timestamp *ts,
765 const EntryItem items[], unsigned n_items,
766 Object **ret, uint64_t *offset) {
773 assert(items || n_items == 0);
775 osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
777 r = journal_file_append_object(f, osize, &o, &np);
781 o->object.type = htole64(OBJECT_ENTRY);
782 o->entry.seqnum = htole64(journal_file_seqnum(f));
783 memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
784 o->entry.realtime = ts ? htole64(ts->realtime) : 0;
785 o->entry.monotonic = ts ? htole64(ts->monotonic) : 0;
786 o->entry.xor_hash = htole64(xor_hash);
788 r = journal_file_link_entry(f, o, np);
801 int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset) {
805 uint64_t xor_hash = 0;
808 assert(iovec || n_iovec == 0);
810 items = new(EntryItem, n_iovec);
814 for (i = 0; i < n_iovec; i++) {
818 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
822 xor_hash ^= le64toh(o->data.hash);
823 items[i].object_offset = htole64(p);
826 r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, ret, offset);
834 int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset) {
836 uint64_t lower, upper, p, n, k;
841 n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
842 k = le64toh(f->header->arena_max_size) / n;
845 upper = le64toh(f->header->last_bisect_offset)/k+1;
847 while (lower < upper) {
848 k = (upper + lower) / 2;
849 p = le64toh(f->bisect_table[k]);
856 r = journal_file_move_to_object(f, p, &o);
860 if (o->object.type != htole64(OBJECT_ENTRY))
863 if (o->entry.seqnum == seqnum) {
871 } else if (seqnum < o->entry.seqnum)
873 else if (seqnum > o->entry.seqnum)
877 assert(lower == upper);
882 /* The object we are looking for is between
883 * bisect_table[lower-1] and bisect_table[lower] */
885 p = le64toh(f->bisect_table[lower-1]);
888 r = journal_file_move_to_object(f, p, &o);
892 if (o->entry.seqnum == seqnum) {
901 } if (seqnum < o->entry.seqnum)
904 if (o->entry.next_entry_offset == 0)
907 p = le64toh(o->entry.next_entry_offset);
913 int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
920 np = le64toh(f->header->head_entry_offset);
922 if (le64toh(o->object.type) != OBJECT_ENTRY)
925 np = le64toh(o->entry.next_entry_offset);
931 r = journal_file_move_to_object(f, np, &o);
935 if (le64toh(o->object.type) != OBJECT_ENTRY)
947 int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
954 np = le64toh(f->header->tail_entry_offset);
956 if (le64toh(o->object.type) != OBJECT_ENTRY)
959 np = le64toh(o->entry.prev_entry_offset);
965 r = journal_file_move_to_object(f, np, &o);
969 if (le64toh(o->object.type) != OBJECT_ENTRY)
981 int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
982 uint64_t p, osize, hash, h;
986 assert(data || size == 0);
988 osize = offsetof(Object, data.payload) + size;
990 hash = hash64(data, size);
991 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
992 p = le64toh(f->hash_table[h].head_hash_offset);
997 r = journal_file_move_to_object(f, p, &o);
1001 if (le64toh(o->object.type) != OBJECT_DATA)
1004 if (le64toh(o->object.size) == osize &&
1005 memcmp(o->data.payload, data, size) == 0) {
1007 if (le64toh(o->data.hash) != hash)
1010 if (o->data.head_entry_offset == 0)
1013 p = le64toh(o->data.head_entry_offset);
1014 r = journal_file_move_to_object(f, p, &o);
1018 if (le64toh(o->object.type) != OBJECT_ENTRY)
1030 p = le64toh(o->data.next_hash_offset);
1036 int journal_file_find_last_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
1037 uint64_t p, osize, hash, h;
1041 assert(data || size == 0);
1043 osize = offsetof(Object, data.payload) + size;
1045 hash = hash64(data, size);
1046 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
1047 p = le64toh(f->hash_table[h].tail_hash_offset);
1052 r = journal_file_move_to_object(f, p, &o);
1056 if (le64toh(o->object.type) != OBJECT_DATA)
1059 if (le64toh(o->object.size) == osize &&
1060 memcmp(o->data.payload, data, size) == 0) {
1062 if (le64toh(o->data.hash) != hash)
1065 if (o->data.tail_entry_offset == 0)
1068 p = le64toh(o->data.tail_entry_offset);
1069 r = journal_file_move_to_object(f, p, &o);
1073 if (le64toh(o->object.type) != OBJECT_ENTRY)
1085 p = le64toh(o->data.prev_hash_offset);
1091 void journal_file_dump(JournalFile *f) {
1092 char a[33], b[33], c[33];
1099 printf("File ID: %s\n"
1102 "Arena size: %llu\n",
1103 sd_id128_to_string(f->header->file_id, a),
1104 sd_id128_to_string(f->header->machine_id, b),
1105 sd_id128_to_string(f->header->boot_id, c),
1106 (unsigned long long) le64toh(f->header->arena_size));
1108 p = le64toh(f->header->head_object_offset);
1110 r = journal_file_move_to_object(f, p, &o);
1114 switch (o->object.type) {
1117 printf("Type: OBJECT_UNUSED\n");
1121 printf("Type: OBJECT_DATA\n");
1125 printf("Type: OBJECT_ENTRY %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
1128 case OBJECT_HASH_TABLE:
1129 printf("Type: OBJECT_HASH_TABLE\n");
1132 case OBJECT_BISECT_TABLE:
1133 printf("Type: OBJECT_BISECT_TABLE\n");
1137 if (p == le64toh(f->header->tail_object_offset))
1140 p = p + ALIGN64(le64toh(o->object.size));
1145 log_error("File corrupt");
1148 int journal_file_open(
1153 JournalFile **ret) {
1157 bool newly_created = false;
1161 if ((flags & O_ACCMODE) != O_RDONLY &&
1162 (flags & O_ACCMODE) != O_RDWR)
1165 f = new0(JournalFile, 1);
1169 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1170 f->prot = prot_from_flags(flags);
1172 f->fd = open(fname, flags|O_CLOEXEC, mode);
1178 f->path = strdup(fname);
1184 if (fstat(f->fd, &f->last_stat) < 0) {
1189 if (f->last_stat.st_size == 0 && f->writable) {
1190 newly_created = true;
1192 r = journal_file_init_header(f);
1196 if (fstat(f->fd, &f->last_stat) < 0) {
1202 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1207 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1208 if (f->header == MAP_FAILED) {
1214 if (!newly_created) {
1215 r = journal_file_verify_header(f);
1221 r = journal_file_refresh_header(f);
1226 if (newly_created) {
1228 r = journal_file_setup_hash_table(f);
1232 r = journal_file_setup_bisect_table(f);
1237 r = journal_file_map_hash_table(f);
1241 r = journal_file_map_bisect_table(f);
1246 LIST_PREPEND(JournalFile, files, j->files, f);
1256 journal_file_close(f);
1261 int sd_journal_open(sd_journal **ret) {
1266 const char search_paths[] =
1267 "/run/log/journal\0"
1268 "/var/log/journal\0";
1272 j = new0(sd_journal, 1);
1276 NULSTR_FOREACH(p, search_paths) {
1281 if (errno != ENOENT && r == 0)
1288 struct dirent buf, *de;
1291 k = readdir_r(d, &buf, &de);
1302 if (!dirent_is_file_with_suffix(de, ".journal"))
1305 fn = join(p, "/", de->d_name, NULL);
1312 k = journal_file_open(j, fn, O_RDONLY, 0, NULL);
1313 if (k < 0 && r == 0)
1331 sd_journal_close(j);
1336 void sd_journal_close(sd_journal *j) {
1340 journal_file_close(j->files);