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"
37 #define DEFAULT_ARENA_MAX_SIZE (16ULL*1024ULL*1024ULL*1024ULL)
38 #define DEFAULT_ARENA_MIN_SIZE (256ULL*1024ULL)
39 #define DEFAULT_ARENA_KEEP_FREE (1ULL*1024ULL*1024ULL)
41 #define DEFAULT_HASH_TABLE_SIZE (2047ULL*16ULL)
42 #define DEFAULT_BISECT_TABLE_SIZE ((DEFAULT_ARENA_MAX_SIZE/(64ULL*1024ULL))*8ULL)
44 #define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
49 struct stat last_stat;
56 void *hash_table_window;
57 uint64_t hash_table_window_size;
59 uint64_t *bisect_table;
60 void *bisect_table_window;
61 uint64_t bisect_table_window_size;
64 uint64_t window_offset;
68 uint64_t current_offset;
70 LIST_FIELDS(JournalFile, files);
77 static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
79 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
81 void journal_file_close(JournalFile *f) {
85 close_nointr_nofail(f->fd);
88 munmap(f->header, PAGE_ALIGN(sizeof(Header)));
90 if (f->hash_table_window)
91 munmap(f->hash_table_window, f->hash_table_window_size);
93 if (f->bisect_table_window)
94 munmap(f->bisect_table_window, f->bisect_table_window_size);
97 munmap(f->window, f->window_size);
103 static int journal_file_init_header(JournalFile *f) {
111 memcpy(h.signature, signature, 8);
112 h.arena_offset = htole64(ALIGN64(sizeof(h)));
113 h.arena_max_size = htole64(DEFAULT_ARENA_MAX_SIZE);
114 h.arena_min_size = htole64(DEFAULT_ARENA_MIN_SIZE);
115 h.arena_keep_free = htole64(DEFAULT_ARENA_KEEP_FREE);
117 r = sd_id128_randomize(&h.file_id);
121 k = pwrite(f->fd, &h, sizeof(h), 0);
131 static int journal_file_refresh_header(JournalFile *f) {
136 r = sd_id128_get_machine(&f->header->machine_id);
140 r = sd_id128_get_boot(&f->header->boot_id);
144 f->header->state = htole32(STATE_ONLINE);
148 static int journal_file_verify_header(JournalFile *f) {
151 if (memcmp(f->header, signature, 8))
154 if (f->header->incompatible_flags != 0)
155 return -EPROTONOSUPPORT;
157 if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
162 sd_id128_t machine_id;
165 r = sd_id128_get_machine(&machine_id);
169 if (!sd_id128_equal(machine_id, f->header->machine_id))
172 state = le32toh(f->header->state);
174 if (state == STATE_ONLINE)
175 log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f->path);
176 else if (state == STATE_ARCHIVED)
178 else if (state != STATE_OFFLINE)
179 log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
185 static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
187 uint64_t old_size, new_size;
191 if (offset < le64toh(f->header->arena_offset))
194 new_size = PAGE_ALIGN(offset + size);
196 /* We assume that this file is not sparse, and we know that
197 * for sure, since we alway call posix_fallocate()
201 le64toh(f->header->arena_offset) +
202 le64toh(f->header->arena_size);
204 if (old_size >= new_size)
207 asize = new_size - le64toh(f->header->arena_offset);
209 if (asize > le64toh(f->header->arena_min_size)) {
212 if (fstatvfs(f->fd, &svfs) >= 0) {
215 available = svfs.f_bfree * svfs.f_bsize;
217 if (available >= f->header->arena_keep_free)
218 available -= f->header->arena_keep_free;
222 if (new_size - old_size > available)
227 if (asize > le64toh(f->header->arena_max_size))
230 if (posix_fallocate(f->fd, 0, new_size) < 0)
233 if (fstat(f->fd, &f->last_stat) < 0)
236 f->header->arena_size = htole64(asize);
241 static int journal_file_map(
250 uint64_t woffset, wsize;
257 woffset = offset & ~((uint64_t) page_size() - 1ULL);
258 wsize = size + (offset - woffset);
259 wsize = PAGE_ALIGN(wsize);
261 window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
262 if (window == MAP_FAILED)
274 *ret = (uint8_t*) window + (offset - woffset);
279 static int journal_file_move_to(JournalFile *f, uint64_t offset, uint64_t size, void **ret) {
287 if (_likely_(f->window &&
288 f->window_offset <= offset &&
289 f->window_offset+f->window_size >= offset + size)) {
291 *ret = (uint8_t*) f->window + (offset - f->window_offset);
296 if (munmap(f->window, f->window_size) < 0)
300 f->window_size = f->window_offset = 0;
303 if (size < DEFAULT_WINDOW_SIZE) {
304 /* If the default window size is larger then what was
305 * asked for extend the mapping a bit in the hope to
306 * minimize needed remappings later on. We add half
307 * the window space before and half behind the
308 * requested mapping */
310 delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
316 size += (DEFAULT_WINDOW_SIZE - delta);
320 r = journal_file_map(f,
322 &f->window, &f->window_offset, &f->window_size,
328 *ret = (uint8_t*) p + delta;
332 static bool verify_hash(Object *o) {
337 t = le64toh(o->object.type);
338 if (t == OBJECT_DATA) {
341 s = le64toh(o->object.size);
343 h1 = le64toh(o->data.hash);
344 h2 = hash64(o->data.payload, s - offsetof(Object, data.payload));
352 int journal_file_move_to_object(JournalFile *f, uint64_t offset, Object **ret) {
361 r = journal_file_move_to(f, offset, sizeof(ObjectHeader), &t);
366 s = le64toh(o->object.size);
368 if (s < sizeof(ObjectHeader))
371 if (s > sizeof(ObjectHeader)) {
372 r = journal_file_move_to(f, offset, s, &t);
386 static uint64_t journal_file_seqnum(JournalFile *f) {
391 r = le64toh(f->header->seqnum) + 1;
392 f->header->seqnum = htole64(r);
397 static int journal_file_append_object(JournalFile *f, uint64_t size, Object **ret, uint64_t *offset) {
404 assert(size >= sizeof(ObjectHeader));
408 p = le64toh(f->header->tail_object_offset);
411 p = le64toh(f->header->arena_offset);
413 r = journal_file_move_to_object(f, p, &tail);
417 p += ALIGN64(le64toh(tail->object.size));
420 r = journal_file_allocate(f, p, size);
424 r = journal_file_move_to(f, p, size, &t);
431 o->object.type = htole64(OBJECT_UNUSED);
432 zero(o->object.reserved);
433 o->object.size = htole64(size);
435 f->header->tail_object_offset = htole64(p);
436 if (f->header->head_object_offset == 0)
437 f->header->head_object_offset = htole64(p);
439 f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
447 static int journal_file_setup_hash_table(JournalFile *f) {
454 s = DEFAULT_HASH_TABLE_SIZE;
455 r = journal_file_append_object(f, offsetof(Object, hash_table.table) + s, &o, &p);
459 o->object.type = htole64(OBJECT_HASH_TABLE);
460 memset(o->hash_table.table, 0, s);
462 f->header->hash_table_offset = htole64(p + offsetof(Object, hash_table.table));
463 f->header->hash_table_size = htole64(s);
468 static int journal_file_setup_bisect_table(JournalFile *f) {
475 s = DEFAULT_BISECT_TABLE_SIZE;
476 r = journal_file_append_object(f, offsetof(Object, bisect_table.table) + s, &o, &p);
480 o->object.type = htole64(OBJECT_BISECT_TABLE);
481 memset(o->bisect_table.table, 0, s);
483 f->header->bisect_table_offset = htole64(p + offsetof(Object, bisect_table.table));
484 f->header->bisect_table_size = htole64(s);
489 static int journal_file_map_hash_table(JournalFile *f) {
496 p = le64toh(f->header->hash_table_offset);
497 s = le64toh(f->header->hash_table_size);
499 r = journal_file_map(f,
501 &f->hash_table_window, NULL, &f->hash_table_window_size,
510 static int journal_file_map_bisect_table(JournalFile *f) {
517 p = le64toh(f->header->bisect_table_offset);
518 s = le64toh(f->header->bisect_table_size);
520 r = journal_file_map(f,
522 &f->bisect_table_window, NULL, &f->bisect_table_window_size,
532 static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash_index) {
539 assert(o->object.type == htole64(OBJECT_DATA));
541 o->data.head_entry_offset = o->data.tail_entry_offset = 0;
542 o->data.next_hash_offset = 0;
544 p = le64toh(f->hash_table[hash_index].tail_hash_offset);
546 /* Only entry in the hash table is easy */
548 o->data.prev_hash_offset = 0;
549 f->hash_table[hash_index].head_hash_offset = htole64(offset);
551 o->data.prev_hash_offset = htole64(p);
553 /* Temporarily move back to the previous data object,
554 * to patch in pointer */
556 r = journal_file_move_to_object(f, p, &o);
560 o->data.next_hash_offset = offset;
562 r = journal_file_move_to_object(f, offset, &o);
567 f->hash_table[hash_index].tail_hash_offset = htole64(offset);
572 static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
573 uint64_t hash, h, p, np;
579 assert(data || size == 0);
581 osize = offsetof(Object, data.payload) + size;
583 hash = hash64(data, size);
584 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
585 p = le64toh(f->hash_table[h].head_hash_offset);
588 /* Look for this data object in the hash table */
590 r = journal_file_move_to_object(f, p, &o);
594 if (le64toh(o->object.type) != OBJECT_DATA)
597 if (le64toh(o->object.size) == osize &&
598 memcmp(o->data.payload, data, size) == 0) {
600 if (le64toh(o->data.hash) != hash)
612 p = le64toh(o->data.next_hash_offset);
615 r = journal_file_append_object(f, osize, &o, &np);
619 o->object.type = htole64(OBJECT_DATA);
620 o->data.hash = htole64(hash);
621 memcpy(o->data.payload, data, size);
623 r = journal_file_link_data(f, o, np, h);
636 uint64_t journal_file_entry_n_items(Object *o) {
638 assert(o->object.type == htole64(OBJECT_ENTRY));
640 return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
643 static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
650 p = le64toh(o->entry.items[i].object_offset);
654 o->entry.items[i].next_entry_offset = 0;
656 /* Move to the data object */
657 r = journal_file_move_to_object(f, p, &o);
661 if (o->object.type != htole64(OBJECT_DATA))
664 q = le64toh(o->data.tail_entry_offset);
665 o->data.tail_entry_offset = htole64(offset);
668 o->data.head_entry_offset = htole64(offset);
672 /* Move to previous entry */
673 r = journal_file_move_to_object(f, q, &o);
677 if (o->object.type != htole64(OBJECT_ENTRY))
680 n = journal_file_entry_n_items(o);
681 for (j = 0; j < n; j++)
682 if (le64toh(o->entry.items[j].object_offset) == p)
688 o->entry.items[j].next_entry_offset = offset;
691 /* Move back to original entry */
692 r = journal_file_move_to_object(f, offset, &o);
696 o->entry.items[i].prev_entry_offset = q;
700 static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
701 uint64_t p, i, n, k, a, b;
707 assert(o->object.type == htole64(OBJECT_ENTRY));
709 /* Link up the entry itself */
710 p = le64toh(f->header->tail_entry_offset);
712 o->entry.prev_entry_offset = f->header->tail_entry_offset;
713 o->entry.next_entry_offset = 0;
716 f->header->head_entry_offset = htole64(offset);
718 /* Temporarily move back to the previous entry, to
719 * patch in pointer */
721 r = journal_file_move_to_object(f, p, &o);
725 o->entry.next_entry_offset = htole64(offset);
727 r = journal_file_move_to_object(f, offset, &o);
732 f->header->tail_entry_offset = htole64(offset);
734 /* Link up the items */
735 n = journal_file_entry_n_items(o);
736 for (i = 0; i < n; i++) {
737 r = journal_file_link_entry_item(f, o, offset, i);
742 /* Link up the entry in the bisect table */
743 n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
744 k = le64toh(f->header->arena_max_size) / n;
746 a = (le64toh(f->header->last_bisect_offset) + k - 1) / k;
750 f->bisect_table[a] = htole64(offset);
752 f->header->last_bisect_offset = htole64(offset + le64toh(o->object.size));
757 static int journal_file_append_entry_internal(
759 const dual_timestamp *ts,
761 const EntryItem items[], unsigned n_items,
762 Object **ret, uint64_t *offset) {
769 assert(items || n_items == 0);
771 osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
773 r = journal_file_append_object(f, osize, &o, &np);
777 o->object.type = htole64(OBJECT_ENTRY);
778 o->entry.seqnum = htole64(journal_file_seqnum(f));
779 memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
780 o->entry.realtime = ts ? htole64(ts->realtime) : 0;
781 o->entry.monotonic = ts ? htole64(ts->monotonic) : 0;
782 o->entry.xor_hash = htole64(xor_hash);
784 r = journal_file_link_entry(f, o, np);
797 int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset) {
801 uint64_t xor_hash = 0;
804 assert(iovec || n_iovec == 0);
806 items = new(EntryItem, n_iovec);
810 for (i = 0; i < n_iovec; i++) {
814 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
818 xor_hash ^= le64toh(o->data.hash);
819 items[i].object_offset = htole64(p);
822 r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, ret, offset);
830 int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset) {
832 uint64_t lower, upper, p, n, k;
837 n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
838 k = le64toh(f->header->arena_max_size) / n;
841 upper = le64toh(f->header->last_bisect_offset)/k+1;
843 while (lower < upper) {
844 k = (upper + lower) / 2;
845 p = le64toh(f->bisect_table[k]);
852 r = journal_file_move_to_object(f, p, &o);
856 if (o->object.type != htole64(OBJECT_ENTRY))
859 if (o->entry.seqnum == seqnum) {
867 } else if (seqnum < o->entry.seqnum)
869 else if (seqnum > o->entry.seqnum)
873 assert(lower == upper);
878 /* The object we are looking for is between
879 * bisect_table[lower-1] and bisect_table[lower] */
881 p = le64toh(f->bisect_table[lower-1]);
884 r = journal_file_move_to_object(f, p, &o);
888 if (o->entry.seqnum == seqnum) {
897 } if (seqnum < o->entry.seqnum)
900 if (o->entry.next_entry_offset == 0)
903 p = le64toh(o->entry.next_entry_offset);
909 int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
916 np = le64toh(f->header->head_entry_offset);
918 if (le64toh(o->object.type) != OBJECT_ENTRY)
921 np = le64toh(o->entry.next_entry_offset);
927 r = journal_file_move_to_object(f, np, &o);
931 if (le64toh(o->object.type) != OBJECT_ENTRY)
943 int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
950 np = le64toh(f->header->tail_entry_offset);
952 if (le64toh(o->object.type) != OBJECT_ENTRY)
955 np = le64toh(o->entry.prev_entry_offset);
961 r = journal_file_move_to_object(f, np, &o);
965 if (le64toh(o->object.type) != OBJECT_ENTRY)
977 int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
978 uint64_t p, osize, hash, h;
982 assert(data || size == 0);
984 osize = offsetof(Object, data.payload) + size;
986 hash = hash64(data, size);
987 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
988 p = le64toh(f->hash_table[h].head_hash_offset);
993 r = journal_file_move_to_object(f, p, &o);
997 if (le64toh(o->object.type) != OBJECT_DATA)
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.head_entry_offset == 0)
1009 p = le64toh(o->data.head_entry_offset);
1010 r = journal_file_move_to_object(f, p, &o);
1014 if (le64toh(o->object.type) != OBJECT_ENTRY)
1026 p = le64toh(o->data.next_hash_offset);
1032 int journal_file_find_last_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
1033 uint64_t p, osize, hash, h;
1037 assert(data || size == 0);
1039 osize = offsetof(Object, data.payload) + size;
1041 hash = hash64(data, size);
1042 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
1043 p = le64toh(f->hash_table[h].tail_hash_offset);
1048 r = journal_file_move_to_object(f, p, &o);
1052 if (le64toh(o->object.type) != OBJECT_DATA)
1055 if (le64toh(o->object.size) == osize &&
1056 memcmp(o->data.payload, data, size) == 0) {
1058 if (le64toh(o->data.hash) != hash)
1061 if (o->data.tail_entry_offset == 0)
1064 p = le64toh(o->data.tail_entry_offset);
1065 r = journal_file_move_to_object(f, p, &o);
1069 if (le64toh(o->object.type) != OBJECT_ENTRY)
1081 p = le64toh(o->data.prev_hash_offset);
1087 void journal_file_dump(JournalFile *f) {
1088 char a[33], b[33], c[33];
1095 printf("File ID: %s\n"
1098 "Arena size: %llu\n",
1099 sd_id128_to_string(f->header->file_id, a),
1100 sd_id128_to_string(f->header->machine_id, b),
1101 sd_id128_to_string(f->header->boot_id, c),
1102 (unsigned long long) le64toh(f->header->arena_size));
1104 p = le64toh(f->header->head_object_offset);
1106 r = journal_file_move_to_object(f, p, &o);
1110 switch (o->object.type) {
1113 printf("Type: OBJECT_UNUSED\n");
1117 printf("Type: OBJECT_DATA\n");
1121 printf("Type: OBJECT_ENTRY %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
1124 case OBJECT_HASH_TABLE:
1125 printf("Type: OBJECT_HASH_TABLE\n");
1128 case OBJECT_BISECT_TABLE:
1129 printf("Type: OBJECT_BISECT_TABLE\n");
1133 if (p == le64toh(f->header->tail_object_offset))
1136 p = p + ALIGN64(le64toh(o->object.size));
1141 log_error("File corrupt");
1144 int journal_file_open(
1148 JournalFile **ret) {
1152 bool newly_created = false;
1156 if ((flags & O_ACCMODE) != O_RDONLY &&
1157 (flags & O_ACCMODE) != O_RDWR)
1160 f = new0(JournalFile, 1);
1164 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1165 f->prot = prot_from_flags(flags);
1167 f->fd = open(fname, flags|O_CLOEXEC, mode);
1173 f->path = strdup(fname);
1179 if (fstat(f->fd, &f->last_stat) < 0) {
1184 if (f->last_stat.st_size == 0 && f->writable) {
1185 newly_created = true;
1187 r = journal_file_init_header(f);
1191 if (fstat(f->fd, &f->last_stat) < 0) {
1197 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1202 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1203 if (f->header == MAP_FAILED) {
1209 if (!newly_created) {
1210 r = journal_file_verify_header(f);
1216 r = journal_file_refresh_header(f);
1221 if (newly_created) {
1223 r = journal_file_setup_hash_table(f);
1227 r = journal_file_setup_bisect_table(f);
1232 r = journal_file_map_hash_table(f);
1236 r = journal_file_map_bisect_table(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 j->files = hashmap_new(string_hash_func, string_compare_func);
1270 NULSTR_FOREACH(p, search_paths) {
1275 if (errno != ENOENT && r == 0)
1282 struct dirent buf, *de;
1286 k = readdir_r(d, &buf, &de);
1297 if (!dirent_is_file_with_suffix(de, ".journal"))
1300 fn = join(p, "/", de->d_name, NULL);
1307 k = journal_file_open(fn, O_RDONLY, 0, &f);
1315 k = hashmap_put(j->files, f->path, f);
1317 journal_file_close(f);
1331 sd_journal_close(j);
1336 void sd_journal_close(sd_journal *j) {
1342 while ((f = hashmap_steal_first(j->files)))
1343 journal_file_close(f);
1345 hashmap_free(j->files);