chiark / gitweb /
8bca300f93f4468531b483788d3ca35f47d54c1a
[elogind.git] / src / journal / sd-journal.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <sys/mman.h>
23 #include <errno.h>
24 #include <sys/uio.h>
25 #include <unistd.h>
26 #include <sys/statvfs.h>
27 #include <fcntl.h>
28 #include <stddef.h>
29
30 #include "sd-journal.h"
31 #include "journal-def.h"
32 #include "journal-private.h"
33 #include "lookup3.h"
34 #include "list.h"
35 #include "hashmap.h"
36
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)
40
41 #define DEFAULT_HASH_TABLE_SIZE (2047ULL*16ULL)
42 #define DEFAULT_BISECT_TABLE_SIZE ((DEFAULT_ARENA_MAX_SIZE/(64ULL*1024ULL))*8ULL)
43
44 #define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
45
46 struct JournalFile {
47         int fd;
48         char *path;
49         struct stat last_stat;
50         int prot;
51         bool writable;
52
53         Header *header;
54
55         HashItem *hash_table;
56         void *hash_table_window;
57         uint64_t hash_table_window_size;
58
59         uint64_t *bisect_table;
60         void *bisect_table_window;
61         uint64_t bisect_table_window_size;
62
63         void *window;
64         uint64_t window_offset;
65         uint64_t window_size;
66
67         Object *current;
68         uint64_t current_offset;
69
70         LIST_FIELDS(JournalFile, files);
71 };
72
73 struct sd_journal {
74         Hashmap *files;
75 };
76
77 static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
78
79 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
80
81 void journal_file_close(JournalFile *f) {
82         assert(f);
83
84         if (f->fd >= 0)
85                 close_nointr_nofail(f->fd);
86
87         if (f->header)
88                 munmap(f->header, PAGE_ALIGN(sizeof(Header)));
89
90         if (f->hash_table_window)
91                 munmap(f->hash_table_window, f->hash_table_window_size);
92
93         if (f->bisect_table_window)
94                 munmap(f->bisect_table_window, f->bisect_table_window_size);
95
96         if (f->window)
97                 munmap(f->window, f->window_size);
98
99         free(f->path);
100         free(f);
101 }
102
103 static int journal_file_init_header(JournalFile *f) {
104         Header h;
105         ssize_t k;
106         int r;
107
108         assert(f);
109
110         zero(h);
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);
116
117         r = sd_id128_randomize(&h.file_id);
118         if (r < 0)
119                 return r;
120
121         k = pwrite(f->fd, &h, sizeof(h), 0);
122         if (k < 0)
123                 return -errno;
124
125         if (k != sizeof(h))
126                 return -EIO;
127
128         return 0;
129 }
130
131 static int journal_file_refresh_header(JournalFile *f) {
132         int r;
133
134         assert(f);
135
136         r = sd_id128_get_machine(&f->header->machine_id);
137         if (r < 0)
138                 return r;
139
140         r = sd_id128_get_boot(&f->header->boot_id);
141         if (r < 0)
142                 return r;
143
144         f->header->state = htole32(STATE_ONLINE);
145         return 0;
146 }
147
148 static int journal_file_verify_header(JournalFile *f) {
149         assert(f);
150
151         if (memcmp(f->header, signature, 8))
152                 return -EBADMSG;
153
154         if (f->header->incompatible_flags != 0)
155                 return -EPROTONOSUPPORT;
156
157         if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
158                 return -ENODATA;
159
160         if (f->writable) {
161                 uint32_t state;
162                 sd_id128_t machine_id;
163                 int r;
164
165                 r = sd_id128_get_machine(&machine_id);
166                 if (r < 0)
167                         return r;
168
169                 if (!sd_id128_equal(machine_id, f->header->machine_id))
170                         return -EHOSTDOWN;
171
172                 state = le32toh(f->header->state);
173
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)
177                         return -ESHUTDOWN;
178                 else if (state != STATE_OFFLINE)
179                         log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
180         }
181
182         return 0;
183 }
184
185 static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
186         uint64_t asize;
187         uint64_t old_size, new_size;
188
189         assert(f);
190
191         if (offset < le64toh(f->header->arena_offset))
192                 return -EINVAL;
193
194         new_size = PAGE_ALIGN(offset + size);
195
196         /* We assume that this file is not sparse, and we know that
197          * for sure, since we alway call posix_fallocate()
198          * ourselves */
199
200         old_size =
201                 le64toh(f->header->arena_offset) +
202                 le64toh(f->header->arena_size);
203
204         if (old_size >= new_size)
205                 return 0;
206
207         asize = new_size - le64toh(f->header->arena_offset);
208
209         if (asize > le64toh(f->header->arena_min_size)) {
210                 struct statvfs svfs;
211
212                 if (fstatvfs(f->fd, &svfs) >= 0) {
213                         uint64_t available;
214
215                         available = svfs.f_bfree * svfs.f_bsize;
216
217                         if (available >= f->header->arena_keep_free)
218                                 available -= f->header->arena_keep_free;
219                         else
220                                 available = 0;
221
222                         if (new_size - old_size > available)
223                                 return -E2BIG;
224                 }
225         }
226
227         if (asize > le64toh(f->header->arena_max_size))
228                 return -E2BIG;
229
230         if (posix_fallocate(f->fd, 0, new_size) < 0)
231                 return -errno;
232
233         if (fstat(f->fd, &f->last_stat) < 0)
234                 return -errno;
235
236         f->header->arena_size = htole64(asize);
237
238         return 0;
239 }
240
241 static int journal_file_map(
242                 JournalFile *f,
243                 uint64_t offset,
244                 uint64_t size,
245                 void **_window,
246                 uint64_t *_woffset,
247                 uint64_t *_wsize,
248                 void **ret) {
249
250         uint64_t woffset, wsize;
251         void *window;
252
253         assert(f);
254         assert(size > 0);
255         assert(ret);
256
257         woffset = offset & ~((uint64_t) page_size() - 1ULL);
258         wsize = size + (offset - woffset);
259         wsize = PAGE_ALIGN(wsize);
260
261         window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
262         if (window == MAP_FAILED)
263                 return -errno;
264
265         if (_window)
266                 *_window = window;
267
268         if (_woffset)
269                 *_woffset = woffset;
270
271         if (_wsize)
272                 *_wsize = wsize;
273
274         *ret = (uint8_t*) window + (offset - woffset);
275
276         return 0;
277 }
278
279 static int journal_file_move_to(JournalFile *f, uint64_t offset, uint64_t size, void **ret) {
280         void *p;
281         uint64_t delta;
282         int r;
283
284         assert(f);
285         assert(ret);
286
287         if (_likely_(f->window &&
288                      f->window_offset <= offset &&
289                      f->window_offset+f->window_size >= offset + size)) {
290
291                 *ret = (uint8_t*) f->window + (offset - f->window_offset);
292                 return 0;
293         }
294
295         if (f->window) {
296                 if (munmap(f->window, f->window_size) < 0)
297                         return -errno;
298
299                 f->window = NULL;
300                 f->window_size = f->window_offset = 0;
301         }
302
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 */
309
310                 delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
311
312                 if (offset < delta)
313                         delta = offset;
314
315                 offset -= delta;
316                 size += (DEFAULT_WINDOW_SIZE - delta);
317         } else
318                 delta = 0;
319
320         r = journal_file_map(f,
321                              offset, size,
322                              &f->window, &f->window_offset, &f->window_size,
323                              & p);
324
325         if (r < 0)
326                 return r;
327
328         *ret = (uint8_t*) p + delta;
329         return 0;
330 }
331
332 static bool verify_hash(Object *o) {
333         uint64_t t;
334
335         assert(o);
336
337         t = le64toh(o->object.type);
338         if (t == OBJECT_DATA) {
339                 uint64_t s, h1, h2;
340
341                 s = le64toh(o->object.size);
342
343                 h1 = le64toh(o->data.hash);
344                 h2 = hash64(o->data.payload, s - offsetof(Object, data.payload));
345
346                 return h1 == h2;
347         }
348
349         return true;
350 }
351
352 int journal_file_move_to_object(JournalFile *f, uint64_t offset, Object **ret) {
353         int r;
354         void *t;
355         Object *o;
356         uint64_t s;
357
358         assert(f);
359         assert(ret);
360
361         r = journal_file_move_to(f, offset, sizeof(ObjectHeader), &t);
362         if (r < 0)
363                 return r;
364
365         o = (Object*) t;
366         s = le64toh(o->object.size);
367
368         if (s < sizeof(ObjectHeader))
369                 return -EBADMSG;
370
371         if (s > sizeof(ObjectHeader)) {
372                 r = journal_file_move_to(f, offset, s, &t);
373                 if (r < 0)
374                         return r;
375
376                 o = (Object*) t;
377         }
378
379         if (!verify_hash(o))
380                 return -EBADMSG;
381
382         *ret = o;
383         return 0;
384 }
385
386 static uint64_t journal_file_seqnum(JournalFile *f) {
387         uint64_t r;
388
389         assert(f);
390
391         r = le64toh(f->header->seqnum) + 1;
392         f->header->seqnum = htole64(r);
393
394         return r;
395 }
396
397 static int journal_file_append_object(JournalFile *f, uint64_t size, Object **ret, uint64_t *offset) {
398         int r;
399         uint64_t p;
400         Object *tail, *o;
401         void *t;
402
403         assert(f);
404         assert(size >= sizeof(ObjectHeader));
405         assert(offset);
406         assert(ret);
407
408         p = le64toh(f->header->tail_object_offset);
409
410         if (p == 0)
411                 p = le64toh(f->header->arena_offset);
412         else {
413                 r = journal_file_move_to_object(f, p, &tail);
414                 if (r < 0)
415                         return r;
416
417                 p += ALIGN64(le64toh(tail->object.size));
418         }
419
420         r = journal_file_allocate(f, p, size);
421         if (r < 0)
422                 return r;
423
424         r = journal_file_move_to(f, p, size, &t);
425         if (r < 0)
426                 return r;
427
428         o = (Object*) t;
429
430         zero(o->object);
431         o->object.type = htole64(OBJECT_UNUSED);
432         zero(o->object.reserved);
433         o->object.size = htole64(size);
434
435         f->header->tail_object_offset = htole64(p);
436         if (f->header->head_object_offset == 0)
437                 f->header->head_object_offset = htole64(p);
438
439         f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
440
441         *ret = o;
442         *offset = p;
443
444         return 0;
445 }
446
447 static int journal_file_setup_hash_table(JournalFile *f) {
448         uint64_t s, p;
449         Object *o;
450         int r;
451
452         assert(f);
453
454         s = DEFAULT_HASH_TABLE_SIZE;
455         r = journal_file_append_object(f, offsetof(Object, hash_table.table) + s, &o, &p);
456         if (r < 0)
457                 return r;
458
459         o->object.type = htole64(OBJECT_HASH_TABLE);
460         memset(o->hash_table.table, 0, s);
461
462         f->header->hash_table_offset = htole64(p + offsetof(Object, hash_table.table));
463         f->header->hash_table_size = htole64(s);
464
465         return 0;
466 }
467
468 static int journal_file_setup_bisect_table(JournalFile *f) {
469         uint64_t s, p;
470         Object *o;
471         int r;
472
473         assert(f);
474
475         s = DEFAULT_BISECT_TABLE_SIZE;
476         r = journal_file_append_object(f, offsetof(Object, bisect_table.table) + s, &o, &p);
477         if (r < 0)
478                 return r;
479
480         o->object.type = htole64(OBJECT_BISECT_TABLE);
481         memset(o->bisect_table.table, 0, s);
482
483         f->header->bisect_table_offset = htole64(p + offsetof(Object, bisect_table.table));
484         f->header->bisect_table_size = htole64(s);
485
486         return 0;
487 }
488
489 static int journal_file_map_hash_table(JournalFile *f) {
490         uint64_t s, p;
491         void *t;
492         int r;
493
494         assert(f);
495
496         p = le64toh(f->header->hash_table_offset);
497         s = le64toh(f->header->hash_table_size);
498
499         r = journal_file_map(f,
500                              p, s,
501                              &f->hash_table_window, NULL, &f->hash_table_window_size,
502                              &t);
503         if (r < 0)
504                 return r;
505
506         f->hash_table = t;
507         return 0;
508 }
509
510 static int journal_file_map_bisect_table(JournalFile *f) {
511         uint64_t s, p;
512         void *t;
513         int r;
514
515         assert(f);
516
517         p = le64toh(f->header->bisect_table_offset);
518         s = le64toh(f->header->bisect_table_size);
519
520         r = journal_file_map(f,
521                              p, s,
522                              &f->bisect_table_window, NULL, &f->bisect_table_window_size,
523                              &t);
524
525         if (r < 0)
526                 return r;
527
528         f->bisect_table = t;
529         return 0;
530 }
531
532 static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash_index) {
533         uint64_t p;
534         int r;
535
536         assert(f);
537         assert(o);
538         assert(offset > 0);
539         assert(o->object.type == htole64(OBJECT_DATA));
540
541         o->data.head_entry_offset = o->data.tail_entry_offset = 0;
542         o->data.next_hash_offset = 0;
543
544         p = le64toh(f->hash_table[hash_index].tail_hash_offset);
545         if (p == 0) {
546                 /* Only entry in the hash table is easy */
547
548                 o->data.prev_hash_offset = 0;
549                 f->hash_table[hash_index].head_hash_offset = htole64(offset);
550         } else {
551                 o->data.prev_hash_offset = htole64(p);
552
553                 /* Temporarily move back to the previous data object,
554                  * to patch in pointer */
555
556                 r = journal_file_move_to_object(f, p, &o);
557                 if (r < 0)
558                         return r;
559
560                 o->data.next_hash_offset = offset;
561
562                 r = journal_file_move_to_object(f, offset, &o);
563                 if (r < 0)
564                         return r;
565         }
566
567         f->hash_table[hash_index].tail_hash_offset = htole64(offset);
568
569         return 0;
570 }
571
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;
574         uint64_t osize;
575         Object *o;
576         int r;
577
578         assert(f);
579         assert(data || size == 0);
580
581         osize = offsetof(Object, data.payload) + size;
582
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);
586
587         while (p != 0) {
588                 /* Look for this data object in the hash table */
589
590                 r = journal_file_move_to_object(f, p, &o);
591                 if (r < 0)
592                         return r;
593
594                 if (le64toh(o->object.type) != OBJECT_DATA)
595                         return -EBADMSG;
596
597                 if (le64toh(o->object.size) == osize &&
598                     memcmp(o->data.payload, data, size) == 0) {
599
600                         if (le64toh(o->data.hash) != hash)
601                                 return -EBADMSG;
602
603                         if (ret)
604                                 *ret = o;
605
606                         if (offset)
607                                 *offset = p;
608
609                         return 0;
610                 }
611
612                 p = le64toh(o->data.next_hash_offset);
613         }
614
615         r = journal_file_append_object(f, osize, &o, &np);
616         if (r < 0)
617                 return r;
618
619         o->object.type = htole64(OBJECT_DATA);
620         o->data.hash = htole64(hash);
621         memcpy(o->data.payload, data, size);
622
623         r = journal_file_link_data(f, o, np, h);
624         if (r < 0)
625                 return r;
626
627         if (ret)
628                 *ret = o;
629
630         if (offset)
631                 *offset = np;
632
633         return 0;
634 }
635
636 uint64_t journal_file_entry_n_items(Object *o) {
637         assert(o);
638         assert(o->object.type == htole64(OBJECT_ENTRY));
639
640         return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
641 }
642
643 static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
644         uint64_t p, q;
645         int r;
646         assert(f);
647         assert(o);
648         assert(offset > 0);
649
650         p = le64toh(o->entry.items[i].object_offset);
651         if (p == 0)
652                 return -EINVAL;
653
654         o->entry.items[i].next_entry_offset = 0;
655
656         /* Move to the data object */
657         r = journal_file_move_to_object(f, p, &o);
658         if (r < 0)
659                 return r;
660
661         if (o->object.type != htole64(OBJECT_DATA))
662                 return -EBADMSG;
663
664         q = le64toh(o->data.tail_entry_offset);
665         o->data.tail_entry_offset = htole64(offset);
666
667         if (q == 0)
668                 o->data.head_entry_offset = htole64(offset);
669         else {
670                 uint64_t n, j;
671
672                 /* Move to previous entry */
673                 r = journal_file_move_to_object(f, q, &o);
674                 if (r < 0)
675                         return r;
676
677                 if (o->object.type != htole64(OBJECT_ENTRY))
678                         return -EBADMSG;
679
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)
683                                 break;
684
685                 if (j >= n)
686                         return -EBADMSG;
687
688                 o->entry.items[j].next_entry_offset = offset;
689         }
690
691         /* Move back to original entry */
692         r = journal_file_move_to_object(f, offset, &o);
693         if (r < 0)
694                 return r;
695
696         o->entry.items[i].prev_entry_offset = q;
697         return 0;
698 }
699
700 static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
701         uint64_t p, i, n, k, a, b;
702         int r;
703
704         assert(f);
705         assert(o);
706         assert(offset > 0);
707         assert(o->object.type == htole64(OBJECT_ENTRY));
708
709         /* Link up the entry itself */
710         p = le64toh(f->header->tail_entry_offset);
711
712         o->entry.prev_entry_offset = f->header->tail_entry_offset;
713         o->entry.next_entry_offset = 0;
714
715         if (p == 0)
716                 f->header->head_entry_offset = htole64(offset);
717         else {
718                 /* Temporarily move back to the previous entry, to
719                  * patch in pointer */
720
721                 r = journal_file_move_to_object(f, p, &o);
722                 if (r < 0)
723                         return r;
724
725                 o->entry.next_entry_offset = htole64(offset);
726
727                 r = journal_file_move_to_object(f, offset, &o);
728                 if (r < 0)
729                         return r;
730         }
731
732         f->header->tail_entry_offset = htole64(offset);
733
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);
738                 if (r < 0)
739                         return r;
740         }
741
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;
745
746         a = (le64toh(f->header->last_bisect_offset) + k - 1) / k;
747         b = offset / k;
748
749         for (; a <= b; a++)
750                 f->bisect_table[a] = htole64(offset);
751
752         f->header->last_bisect_offset = htole64(offset + le64toh(o->object.size));
753
754         return 0;
755 }
756
757 static int journal_file_append_entry_internal(
758                 JournalFile *f,
759                 const dual_timestamp *ts,
760                 uint64_t xor_hash,
761                 const EntryItem items[], unsigned n_items,
762                 Object **ret, uint64_t *offset) {
763         uint64_t np;
764         uint64_t osize;
765         Object *o;
766         int r;
767
768         assert(f);
769         assert(items || n_items == 0);
770
771         osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
772
773         r = journal_file_append_object(f, osize, &o, &np);
774         if (r < 0)
775                 return r;
776
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);
783
784         r = journal_file_link_entry(f, o, np);
785         if (r < 0)
786                 return r;
787
788         if (ret)
789                 *ret = o;
790
791         if (offset)
792                 *offset = np;
793
794         return 0;
795 }
796
797 int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset) {
798         unsigned i;
799         EntryItem *items;
800         int r;
801         uint64_t xor_hash = 0;
802
803         assert(f);
804         assert(iovec || n_iovec == 0);
805
806         items = new(EntryItem, n_iovec);
807         if (!items)
808                 return -ENOMEM;
809
810         for (i = 0; i < n_iovec; i++) {
811                 uint64_t p;
812                 Object *o;
813
814                 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
815                 if (r < 0)
816                         goto finish;
817
818                 xor_hash ^= le64toh(o->data.hash);
819                 items[i].object_offset = htole64(p);
820         }
821
822         r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, ret, offset);
823
824 finish:
825         free(items);
826
827         return r;
828 }
829
830 int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset) {
831         Object *o;
832         uint64_t lower, upper, p, n, k;
833         int r;
834
835         assert(f);
836
837         n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
838         k = le64toh(f->header->arena_max_size) / n;
839
840         lower = 0;
841         upper = le64toh(f->header->last_bisect_offset)/k+1;
842
843         while (lower < upper) {
844                 k = (upper + lower) / 2;
845                 p = le64toh(f->bisect_table[k]);
846
847                 if (p == 0) {
848                         upper = k;
849                         continue;
850                 }
851
852                 r = journal_file_move_to_object(f, p, &o);
853                 if (r < 0)
854                         return r;
855
856                 if (o->object.type != htole64(OBJECT_ENTRY))
857                         return -EBADMSG;
858
859                 if (o->entry.seqnum == seqnum) {
860                         if (ret)
861                                 *ret = o;
862
863                         if (offset)
864                                 *offset = p;
865
866                         return 1;
867                 } else if (seqnum < o->entry.seqnum)
868                         upper = k;
869                 else if (seqnum > o->entry.seqnum)
870                         lower = k+1;
871         }
872
873         assert(lower == upper);
874
875         if (lower <= 0)
876                 return 0;
877
878         /* The object we are looking for is between
879          * bisect_table[lower-1] and bisect_table[lower] */
880
881         p = le64toh(f->bisect_table[lower-1]);
882
883         for (;;) {
884                 r = journal_file_move_to_object(f, p, &o);
885                 if (r < 0)
886                         return r;
887
888                 if (o->entry.seqnum == seqnum) {
889                         if (ret)
890                                 *ret = o;
891
892                         if (offset)
893                                 *offset = p;
894
895                         return 1;
896
897                 } if (seqnum < o->entry.seqnum)
898                         return 0;
899
900                 if (o->entry.next_entry_offset == 0)
901                         return 0;
902
903                 p = le64toh(o->entry.next_entry_offset);
904         }
905
906         return 0;
907 }
908
909 int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
910         uint64_t np;
911         int r;
912
913         assert(f);
914
915         if (!o)
916                 np = le64toh(f->header->head_entry_offset);
917         else {
918                 if (le64toh(o->object.type) != OBJECT_ENTRY)
919                         return -EINVAL;
920
921                 np = le64toh(o->entry.next_entry_offset);
922         }
923
924         if (np == 0)
925                 return 0;
926
927         r = journal_file_move_to_object(f, np, &o);
928         if (r < 0)
929                 return r;
930
931         if (le64toh(o->object.type) != OBJECT_ENTRY)
932                 return -EBADMSG;
933
934         if (ret)
935                 *ret = o;
936
937         if (offset)
938                 *offset = np;
939
940         return 1;
941 }
942
943 int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
944         uint64_t np;
945         int r;
946
947         assert(f);
948
949         if (!o)
950                 np = le64toh(f->header->tail_entry_offset);
951         else {
952                 if (le64toh(o->object.type) != OBJECT_ENTRY)
953                         return -EINVAL;
954
955                 np = le64toh(o->entry.prev_entry_offset);
956         }
957
958         if (np == 0)
959                 return 0;
960
961         r = journal_file_move_to_object(f, np, &o);
962         if (r < 0)
963                 return r;
964
965         if (le64toh(o->object.type) != OBJECT_ENTRY)
966                 return -EBADMSG;
967
968         if (ret)
969                 *ret = o;
970
971         if (offset)
972                 *offset = np;
973
974         return 1;
975 }
976
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;
979         int r;
980
981         assert(f);
982         assert(data || size == 0);
983
984         osize = offsetof(Object, data.payload) + size;
985
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);
989
990         while (p != 0) {
991                 Object *o;
992
993                 r = journal_file_move_to_object(f, p, &o);
994                 if (r < 0)
995                         return r;
996
997                 if (le64toh(o->object.type) != OBJECT_DATA)
998                         return -EBADMSG;
999
1000                 if (le64toh(o->object.size) == osize &&
1001                     memcmp(o->data.payload, data, size) == 0) {
1002
1003                         if (le64toh(o->data.hash) != hash)
1004                                 return -EBADMSG;
1005
1006                         if (o->data.head_entry_offset == 0)
1007                                 return 0;
1008
1009                         p = le64toh(o->data.head_entry_offset);
1010                         r = journal_file_move_to_object(f, p, &o);
1011                         if (r < 0)
1012                                 return r;
1013
1014                         if (le64toh(o->object.type) != OBJECT_ENTRY)
1015                                 return -EBADMSG;
1016
1017                         if (ret)
1018                                 *ret = o;
1019
1020                         if (offset)
1021                                 *offset = p;
1022
1023                         return 1;
1024                 }
1025
1026                 p = le64toh(o->data.next_hash_offset);
1027         }
1028
1029         return 0;
1030 }
1031
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;
1034         int r;
1035
1036         assert(f);
1037         assert(data || size == 0);
1038
1039         osize = offsetof(Object, data.payload) + size;
1040
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);
1044
1045         while (p != 0) {
1046                 Object *o;
1047
1048                 r = journal_file_move_to_object(f, p, &o);
1049                 if (r < 0)
1050                         return r;
1051
1052                 if (le64toh(o->object.type) != OBJECT_DATA)
1053                         return -EBADMSG;
1054
1055                 if (le64toh(o->object.size) == osize &&
1056                     memcmp(o->data.payload, data, size) == 0) {
1057
1058                         if (le64toh(o->data.hash) != hash)
1059                                 return -EBADMSG;
1060
1061                         if (o->data.tail_entry_offset == 0)
1062                                 return 0;
1063
1064                         p = le64toh(o->data.tail_entry_offset);
1065                         r = journal_file_move_to_object(f, p, &o);
1066                         if (r < 0)
1067                                 return r;
1068
1069                         if (le64toh(o->object.type) != OBJECT_ENTRY)
1070                                 return -EBADMSG;
1071
1072                         if (ret)
1073                                 *ret = o;
1074
1075                         if (offset)
1076                                 *offset = p;
1077
1078                         return 1;
1079                 }
1080
1081                 p = le64toh(o->data.prev_hash_offset);
1082         }
1083
1084         return 0;
1085 }
1086
1087 void journal_file_dump(JournalFile *f) {
1088         char a[33], b[33], c[33];
1089         Object *o;
1090         int r;
1091         uint64_t p;
1092
1093         assert(f);
1094
1095         printf("File ID: %s\n"
1096                "Machine ID: %s\n"
1097                "Boot 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));
1103
1104         p = le64toh(f->header->head_object_offset);
1105         while (p != 0) {
1106                 r = journal_file_move_to_object(f, p, &o);
1107                 if (r < 0)
1108                         goto fail;
1109
1110                 switch (o->object.type) {
1111
1112                 case OBJECT_UNUSED:
1113                         printf("Type: OBJECT_UNUSED\n");
1114                         break;
1115
1116                 case OBJECT_DATA:
1117                         printf("Type: OBJECT_DATA\n");
1118                         break;
1119
1120                 case OBJECT_ENTRY:
1121                         printf("Type: OBJECT_ENTRY %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
1122                         break;
1123
1124                 case OBJECT_HASH_TABLE:
1125                         printf("Type: OBJECT_HASH_TABLE\n");
1126                         break;
1127
1128                 case OBJECT_BISECT_TABLE:
1129                         printf("Type: OBJECT_BISECT_TABLE\n");
1130                         break;
1131                 }
1132
1133                 if (p == le64toh(f->header->tail_object_offset))
1134                         p = 0;
1135                 else
1136                         p = p + ALIGN64(le64toh(o->object.size));
1137         }
1138
1139         return;
1140 fail:
1141         log_error("File corrupt");
1142 }
1143
1144 int journal_file_open(
1145                 const char *fname,
1146                 int flags,
1147                 mode_t mode,
1148                 JournalFile **ret) {
1149
1150         JournalFile *f;
1151         int r;
1152         bool newly_created = false;
1153
1154         assert(fname);
1155
1156         if ((flags & O_ACCMODE) != O_RDONLY &&
1157             (flags & O_ACCMODE) != O_RDWR)
1158                 return -EINVAL;
1159
1160         f = new0(JournalFile, 1);
1161         if (!f)
1162                 return -ENOMEM;
1163
1164         f->writable = (flags & O_ACCMODE) != O_RDONLY;
1165         f->prot = prot_from_flags(flags);
1166
1167         f->fd = open(fname, flags|O_CLOEXEC, mode);
1168         if (f->fd < 0) {
1169                 r = -errno;
1170                 goto fail;
1171         }
1172
1173         f->path = strdup(fname);
1174         if (!f->path) {
1175                 r = -ENOMEM;
1176                 goto fail;
1177         }
1178
1179         if (fstat(f->fd, &f->last_stat) < 0) {
1180                 r = -errno;
1181                 goto fail;
1182         }
1183
1184         if (f->last_stat.st_size == 0 && f->writable) {
1185                 newly_created = true;
1186
1187                 r = journal_file_init_header(f);
1188                 if (r < 0)
1189                         goto fail;
1190
1191                 if (fstat(f->fd, &f->last_stat) < 0) {
1192                         r = -errno;
1193                         goto fail;
1194                 }
1195         }
1196
1197         if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1198                 r = -EIO;
1199                 goto fail;
1200         }
1201
1202         f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1203         if (f->header == MAP_FAILED) {
1204                 f->header = NULL;
1205                 r = -errno;
1206                 goto fail;
1207         }
1208
1209         if (!newly_created) {
1210                 r = journal_file_verify_header(f);
1211                 if (r < 0)
1212                         goto fail;
1213         }
1214
1215         if (f->writable) {
1216                 r = journal_file_refresh_header(f);
1217                 if (r < 0)
1218                         goto fail;
1219         }
1220
1221         if (newly_created) {
1222
1223                 r = journal_file_setup_hash_table(f);
1224                 if (r < 0)
1225                         goto fail;
1226
1227                 r = journal_file_setup_bisect_table(f);
1228                 if (r < 0)
1229                         goto fail;
1230         }
1231
1232         r = journal_file_map_hash_table(f);
1233         if (r < 0)
1234                 goto fail;
1235
1236         r = journal_file_map_bisect_table(f);
1237         if (r < 0)
1238                 goto fail;
1239
1240         if (ret)
1241                 *ret = f;
1242
1243         return 0;
1244
1245 fail:
1246         journal_file_close(f);
1247
1248         return r;
1249 }
1250
1251 int sd_journal_open(sd_journal **ret) {
1252         sd_journal *j;
1253         char *fn;
1254         const char *p;
1255         int r = 0;
1256         const char search_paths[] =
1257                 "/run/log/journal\0"
1258                 "/var/log/journal\0";
1259
1260         assert(ret);
1261
1262         j = new0(sd_journal, 1);
1263         if (!j)
1264                 return -ENOMEM;
1265
1266         j->files = hashmap_new(string_hash_func, string_compare_func);
1267         if (!j->files)
1268                 goto fail;
1269
1270         NULSTR_FOREACH(p, search_paths) {
1271                 DIR *d;
1272
1273                 d = opendir(p);
1274                 if (!d) {
1275                         if (errno != ENOENT && r == 0)
1276                                 r = -errno;
1277
1278                         continue;
1279                 }
1280
1281                 for (;;) {
1282                         struct dirent buf, *de;
1283                         int k;
1284                         JournalFile *f;
1285
1286                         k = readdir_r(d, &buf, &de);
1287                         if (k != 0) {
1288                                 if (r == 0)
1289                                         r = -k;
1290
1291                                 break;
1292                         }
1293
1294                         if (!de)
1295                                 break;
1296
1297                         if (!dirent_is_file_with_suffix(de, ".journal"))
1298                                 continue;
1299
1300                         fn = join(p, "/", de->d_name, NULL);
1301                         if (!fn) {
1302                                 r = -ENOMEM;
1303                                 closedir(d);
1304                                 goto fail;
1305                         }
1306
1307                         k = journal_file_open(fn, O_RDONLY, 0, &f);
1308                         free(fn);
1309
1310                         if (k < 0) {
1311
1312                                 if (r == 0)
1313                                         r = -k;
1314                         } else {
1315                                 k = hashmap_put(j->files, f->path, f);
1316                                 if (k < 0) {
1317                                         journal_file_close(f);
1318                                         closedir(d);
1319
1320                                         r = k;
1321                                         goto fail;
1322                                 }
1323                         }
1324                 }
1325         }
1326
1327         *ret = j;
1328         return 0;
1329
1330 fail:
1331         sd_journal_close(j);
1332
1333         return r;
1334 };
1335
1336 void sd_journal_close(sd_journal *j) {
1337         assert(j);
1338
1339         if (j->files) {
1340                 JournalFile *f;
1341
1342                 while ((f = hashmap_steal_first(j->files)))
1343                         journal_file_close(f);
1344
1345                 hashmap_free(j->files);
1346         }
1347
1348         free(j);
1349 }