chiark / gitweb /
Merge branch 'master' into journal
[elogind.git] / src / journal / journal-file.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 "journal-def.h"
31 #include "journal-file.h"
32 #include "lookup3.h"
33
34 #define DEFAULT_ARENA_MAX_SIZE (16ULL*1024ULL*1024ULL*1024ULL)
35 #define DEFAULT_ARENA_MIN_SIZE (256ULL*1024ULL)
36 #define DEFAULT_ARENA_KEEP_FREE (1ULL*1024ULL*1024ULL)
37
38 #define DEFAULT_MAX_USE (16ULL*1024ULL*1024ULL*16ULL)
39
40 #define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*16ULL)
41 #define DEFAULT_FIELD_HASH_TABLE_SIZE (2047ULL*16ULL)
42
43 #define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
44
45 static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
46
47 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
48
49 void journal_file_close(JournalFile *f) {
50         int t;
51
52         assert(f);
53
54         if (f->header && f->writable)
55                 f->header->state = STATE_OFFLINE;
56
57
58         for (t = 0; t < _WINDOW_MAX; t++)
59                 if (f->windows[t].ptr)
60                         munmap(f->windows[t].ptr, f->windows[t].size);
61
62         if (f->fd >= 0)
63                 close_nointr_nofail(f->fd);
64
65         free(f->path);
66         free(f);
67 }
68
69 static int journal_file_init_header(JournalFile *f, JournalFile *template) {
70         Header h;
71         ssize_t k;
72         int r;
73
74         assert(f);
75
76         zero(h);
77         memcpy(h.signature, signature, 8);
78         h.arena_offset = htole64(ALIGN64(sizeof(h)));
79         h.arena_max_size = htole64(DEFAULT_ARENA_MAX_SIZE);
80         h.arena_min_size = htole64(DEFAULT_ARENA_MIN_SIZE);
81         h.arena_keep_free = htole64(DEFAULT_ARENA_KEEP_FREE);
82
83         r = sd_id128_randomize(&h.file_id);
84         if (r < 0)
85                 return r;
86
87         if (template) {
88                 h.seqnum_id = template->header->seqnum_id;
89                 h.seqnum = template->header->seqnum;
90         } else
91                 h.seqnum_id = h.file_id;
92
93         k = pwrite(f->fd, &h, sizeof(h), 0);
94         if (k < 0)
95                 return -errno;
96
97         if (k != sizeof(h))
98                 return -EIO;
99
100         return 0;
101 }
102
103 static int journal_file_refresh_header(JournalFile *f) {
104         int r;
105         sd_id128_t boot_id;
106
107         assert(f);
108
109         r = sd_id128_get_machine(&f->header->machine_id);
110         if (r < 0)
111                 return r;
112
113         r = sd_id128_get_boot(&boot_id);
114         if (r < 0)
115                 return r;
116
117         if (sd_id128_equal(boot_id, f->header->boot_id))
118                 f->tail_entry_monotonic_valid = true;
119
120         f->header->boot_id = boot_id;
121
122         f->header->state = STATE_ONLINE;
123         return 0;
124 }
125
126 static int journal_file_verify_header(JournalFile *f) {
127         assert(f);
128
129         if (memcmp(f->header, signature, 8))
130                 return -EBADMSG;
131
132         if (f->header->incompatible_flags != 0)
133                 return -EPROTONOSUPPORT;
134
135         if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
136                 return -ENODATA;
137
138         if (f->writable) {
139                 uint32_t state;
140                 sd_id128_t machine_id;
141                 int r;
142
143                 r = sd_id128_get_machine(&machine_id);
144                 if (r < 0)
145                         return r;
146
147                 if (!sd_id128_equal(machine_id, f->header->machine_id))
148                         return -EHOSTDOWN;
149
150                 state = f->header->state;
151
152                 if (state == STATE_ONLINE)
153                         log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f->path);
154                 else if (state == STATE_ARCHIVED)
155                         return -ESHUTDOWN;
156                 else if (state != STATE_OFFLINE)
157                         log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
158         }
159
160         return 0;
161 }
162
163 static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
164         uint64_t asize;
165         uint64_t old_size, new_size;
166
167         assert(f);
168
169         if (offset < le64toh(f->header->arena_offset))
170                 return -EINVAL;
171
172         new_size = PAGE_ALIGN(offset + size);
173
174         /* We assume that this file is not sparse, and we know that
175          * for sure, since we always call posix_fallocate()
176          * ourselves */
177
178         old_size =
179                 le64toh(f->header->arena_offset) +
180                 le64toh(f->header->arena_size);
181
182         if (old_size >= new_size)
183                 return 0;
184
185         asize = new_size - le64toh(f->header->arena_offset);
186
187         if (asize > le64toh(f->header->arena_min_size)) {
188                 struct statvfs svfs;
189
190                 if (fstatvfs(f->fd, &svfs) >= 0) {
191                         uint64_t available;
192
193                         available = svfs.f_bfree * svfs.f_bsize;
194
195                         if (available >= f->header->arena_keep_free)
196                                 available -= f->header->arena_keep_free;
197                         else
198                                 available = 0;
199
200                         if (new_size - old_size > available)
201                                 return -E2BIG;
202                 }
203         }
204
205         if (asize > le64toh(f->header->arena_max_size))
206                 return -E2BIG;
207
208         if (posix_fallocate(f->fd, old_size, new_size - old_size) < 0)
209                 return -errno;
210
211         if (fstat(f->fd, &f->last_stat) < 0)
212                 return -errno;
213
214         f->header->arena_size = htole64(asize);
215
216         return 0;
217 }
218
219 static int journal_file_map(
220                 JournalFile *f,
221                 uint64_t offset,
222                 uint64_t size,
223                 void **_window,
224                 uint64_t *_woffset,
225                 uint64_t *_wsize,
226                 void **ret) {
227
228         uint64_t woffset, wsize;
229         void *window;
230
231         assert(f);
232         assert(size > 0);
233         assert(ret);
234
235         woffset = offset & ~((uint64_t) page_size() - 1ULL);
236         wsize = size + (offset - woffset);
237         wsize = PAGE_ALIGN(wsize);
238
239         window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
240         if (window == MAP_FAILED)
241                 return -errno;
242
243         if (_window)
244                 *_window = window;
245
246         if (_woffset)
247                 *_woffset = woffset;
248
249         if (_wsize)
250                 *_wsize = wsize;
251
252         *ret = (uint8_t*) window + (offset - woffset);
253
254         return 0;
255 }
256
257 static int journal_file_move_to(JournalFile *f, int wt, uint64_t offset, uint64_t size, void **ret) {
258         void *p;
259         uint64_t delta;
260         int r;
261         Window *w;
262
263         assert(f);
264         assert(ret);
265         assert(wt >= 0);
266         assert(wt < _WINDOW_MAX);
267
268         w = f->windows + wt;
269
270         if (_likely_(w->ptr &&
271                      w->offset <= offset &&
272                      w->offset + w->size >= offset + size)) {
273
274                 *ret = (uint8_t*) w->ptr + (offset - w->offset);
275                 return 0;
276         }
277
278         if (w->ptr) {
279                 if (munmap(w->ptr, w->size) < 0)
280                         return -errno;
281
282                 w->ptr = NULL;
283                 w->size = w->offset = 0;
284         }
285
286         if (size < DEFAULT_WINDOW_SIZE) {
287                 /* If the default window size is larger then what was
288                  * asked for extend the mapping a bit in the hope to
289                  * minimize needed remappings later on. We add half
290                  * the window space before and half behind the
291                  * requested mapping */
292
293                 delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
294
295                 if (offset < delta)
296                         delta = offset;
297
298                 offset -= delta;
299                 size += (DEFAULT_WINDOW_SIZE - delta);
300         } else
301                 delta = 0;
302
303         r = journal_file_map(f,
304                              offset, size,
305                              &w->ptr, &w->offset, &w->size,
306                              &p);
307
308         if (r < 0)
309                 return r;
310
311         *ret = (uint8_t*) p + delta;
312         return 0;
313 }
314
315 static bool verify_hash(Object *o) {
316         uint64_t h1, h2;
317
318         assert(o);
319
320         if (o->object.type == OBJECT_DATA) {
321                 h1 = le64toh(o->data.hash);
322                 h2 = hash64(o->data.payload, le64toh(o->object.size) - offsetof(Object, data.payload));
323         } else if (o->object.type == OBJECT_FIELD) {
324                 h1 = le64toh(o->field.hash);
325                 h2 = hash64(o->field.payload, le64toh(o->object.size) - offsetof(Object, field.payload));
326         } else
327                 return true;
328
329         return h1 == h2;
330 }
331
332 int journal_file_move_to_object(JournalFile *f, int type, uint64_t offset, Object **ret) {
333         int r;
334         void *t;
335         Object *o;
336         uint64_t s;
337
338         assert(f);
339         assert(ret);
340         assert(type < _OBJECT_TYPE_MAX);
341
342         r = journal_file_move_to(f, type >= 0 ? type : WINDOW_UNKNOWN, offset, sizeof(ObjectHeader), &t);
343         if (r < 0)
344                 return r;
345
346         o = (Object*) t;
347         s = le64toh(o->object.size);
348
349         if (s < sizeof(ObjectHeader))
350                 return -EBADMSG;
351
352         if (type >= 0 && o->object.type != type)
353                 return -EBADMSG;
354
355         if (s > sizeof(ObjectHeader)) {
356                 r = journal_file_move_to(f, o->object.type, offset, s, &t);
357                 if (r < 0)
358                         return r;
359
360                 o = (Object*) t;
361         }
362
363         if (!verify_hash(o))
364                 return -EBADMSG;
365
366         *ret = o;
367         return 0;
368 }
369
370 static uint64_t journal_file_seqnum(JournalFile *f, uint64_t *seqnum) {
371         uint64_t r;
372
373         assert(f);
374
375         r = le64toh(f->header->seqnum) + 1;
376
377         if (seqnum) {
378                 /* If an external seqnum counter was passed, we update
379                  * both the local and the external one, and set it to
380                  * the maximum of both */
381
382                 if (*seqnum + 1 > r)
383                         r = *seqnum + 1;
384
385                 *seqnum = r;
386         }
387
388         f->header->seqnum = htole64(r);
389
390         if (f->header->first_seqnum == 0)
391                 f->header->first_seqnum = htole64(r);
392
393         return r;
394 }
395
396 static int journal_file_append_object(JournalFile *f, int type, uint64_t size, Object **ret, uint64_t *offset) {
397         int r;
398         uint64_t p;
399         Object *tail, *o;
400         void *t;
401
402         assert(f);
403         assert(size >= sizeof(ObjectHeader));
404         assert(offset);
405         assert(ret);
406
407         p = le64toh(f->header->tail_object_offset);
408         if (p == 0)
409                 p = le64toh(f->header->arena_offset);
410         else {
411                 r = journal_file_move_to_object(f, -1, p, &tail);
412                 if (r < 0)
413                         return r;
414
415                 p += ALIGN64(le64toh(tail->object.size));
416         }
417
418         r = journal_file_allocate(f, p, size);
419         if (r < 0)
420                 return r;
421
422         r = journal_file_move_to(f, type, p, size, &t);
423         if (r < 0)
424                 return r;
425
426         o = (Object*) t;
427
428         zero(o->object);
429         o->object.type = type;
430         o->object.size = htole64(size);
431
432         f->header->tail_object_offset = htole64(p);
433         f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
434
435         *ret = o;
436         *offset = p;
437
438         return 0;
439 }
440
441 static int journal_file_setup_data_hash_table(JournalFile *f) {
442         uint64_t s, p;
443         Object *o;
444         int r;
445
446         assert(f);
447
448         s = DEFAULT_DATA_HASH_TABLE_SIZE;
449         r = journal_file_append_object(f,
450                                        OBJECT_DATA_HASH_TABLE,
451                                        offsetof(Object, hash_table.items) + s,
452                                        &o, &p);
453         if (r < 0)
454                 return r;
455
456         memset(o->hash_table.items, 0, s);
457
458         f->header->data_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
459         f->header->data_hash_table_size = htole64(s);
460
461         return 0;
462 }
463
464 static int journal_file_setup_field_hash_table(JournalFile *f) {
465         uint64_t s, p;
466         Object *o;
467         int r;
468
469         assert(f);
470
471         s = DEFAULT_FIELD_HASH_TABLE_SIZE;
472         r = journal_file_append_object(f,
473                                        OBJECT_FIELD_HASH_TABLE,
474                                        offsetof(Object, hash_table.items) + s,
475                                        &o, &p);
476         if (r < 0)
477                 return r;
478
479         memset(o->hash_table.items, 0, s);
480
481         f->header->field_hash_table_offset = htole64(p + offsetof(Object, hash_table.items));
482         f->header->field_hash_table_size = htole64(s);
483
484         return 0;
485 }
486
487 static int journal_file_map_data_hash_table(JournalFile *f) {
488         uint64_t s, p;
489         void *t;
490         int r;
491
492         assert(f);
493
494         p = le64toh(f->header->data_hash_table_offset);
495         s = le64toh(f->header->data_hash_table_size);
496
497         r = journal_file_move_to(f,
498                                  WINDOW_DATA_HASH_TABLE,
499                                  p, s,
500                                  &t);
501         if (r < 0)
502                 return r;
503
504         f->data_hash_table = t;
505         return 0;
506 }
507
508 static int journal_file_map_field_hash_table(JournalFile *f) {
509         uint64_t s, p;
510         void *t;
511         int r;
512
513         assert(f);
514
515         p = le64toh(f->header->field_hash_table_offset);
516         s = le64toh(f->header->field_hash_table_size);
517
518         r = journal_file_move_to(f,
519                                  WINDOW_FIELD_HASH_TABLE,
520                                  p, s,
521                                  &t);
522         if (r < 0)
523                 return r;
524
525         f->field_hash_table = t;
526         return 0;
527 }
528
529 static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash) {
530         uint64_t p, h;
531         int r;
532
533         assert(f);
534         assert(o);
535         assert(offset > 0);
536         assert(o->object.type == OBJECT_DATA);
537
538         o->data.next_hash_offset = o->data.next_field_offset = 0;
539         o->data.entry_offset = o->data.entry_array_offset = 0;
540         o->data.n_entries = 0;
541
542         h = hash % (le64toh(f->header->data_hash_table_size) / sizeof(HashItem));
543         p = le64toh(f->data_hash_table[h].head_hash_offset);
544         if (p == 0) {
545                 /* Only entry in the hash table is easy */
546                 f->data_hash_table[h].head_hash_offset = htole64(offset);
547         } else {
548                 /* Temporarily move back to the previous data object,
549                  * to patch in pointer */
550
551                 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
552                 if (r < 0)
553                         return r;
554
555                 o->data.next_hash_offset = htole64(offset);
556
557                 r = journal_file_move_to_object(f, OBJECT_DATA, offset, &o);
558                 if (r < 0)
559                         return r;
560         }
561
562         f->data_hash_table[h].tail_hash_offset = htole64(offset);
563
564         return 0;
565 }
566
567 int journal_file_find_data_object_with_hash(
568                 JournalFile *f,
569                 const void *data, uint64_t size, uint64_t hash,
570                 Object **ret, uint64_t *offset) {
571         uint64_t p, osize, h;
572         int r;
573
574         assert(f);
575         assert(data || size == 0);
576
577         osize = offsetof(Object, data.payload) + size;
578
579         h = hash % (le64toh(f->header->data_hash_table_size) / sizeof(HashItem));
580         p = le64toh(f->data_hash_table[h].head_hash_offset);
581
582         while (p > 0) {
583                 Object *o;
584
585                 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
586                 if (r < 0)
587                         return r;
588
589                 if (le64toh(o->object.size) == osize &&
590                     memcmp(o->data.payload, data, size) == 0) {
591
592                         if (le64toh(o->data.hash) != hash)
593                                 return -EBADMSG;
594
595                         if (ret)
596                                 *ret = o;
597
598                         if (offset)
599                                 *offset = p;
600
601                         return 1;
602                 }
603
604                 p = le64toh(o->data.next_hash_offset);
605         }
606
607         return 0;
608 }
609
610 int journal_file_find_data_object(
611                 JournalFile *f,
612                 const void *data, uint64_t size,
613                 Object **ret, uint64_t *offset) {
614
615         uint64_t hash;
616
617         assert(f);
618         assert(data || size == 0);
619
620         hash = hash64(data, size);
621
622         return journal_file_find_data_object_with_hash(f,
623                                                        data, size, hash,
624                                                        ret, offset);
625 }
626
627 static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
628         uint64_t hash, p;
629         uint64_t osize;
630         Object *o;
631         int r;
632
633         assert(f);
634         assert(data || size == 0);
635
636         hash = hash64(data, size);
637
638         r = journal_file_find_data_object_with_hash(f, data, size, hash, &o, &p);
639         if (r < 0)
640                 return r;
641         else if (r > 0) {
642
643                 if (ret)
644                         *ret = o;
645
646                 if (offset)
647                         *offset = p;
648
649                 return 0;
650         }
651
652         osize = offsetof(Object, data.payload) + size;
653         r = journal_file_append_object(f, OBJECT_DATA, osize, &o, &p);
654         if (r < 0)
655                 return r;
656
657         o->data.hash = htole64(hash);
658         memcpy(o->data.payload, data, size);
659
660         r = journal_file_link_data(f, o, p, hash);
661         if (r < 0)
662                 return r;
663
664         if (ret)
665                 *ret = o;
666
667         if (offset)
668                 *offset = p;
669
670         return 0;
671 }
672
673 uint64_t journal_file_entry_n_items(Object *o) {
674         assert(o);
675         assert(o->object.type == htole64(OBJECT_ENTRY));
676
677         return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
678 }
679
680 static uint64_t journal_file_entry_array_n_items(Object *o) {
681         assert(o);
682         assert(o->object.type == htole64(OBJECT_ENTRY_ARRAY));
683
684         return (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / sizeof(uint64_t);
685 }
686
687 static int link_entry_into_array(JournalFile *f,
688                                  uint64_t *first,
689                                  uint64_t *idx,
690                                  uint64_t p) {
691         int r;
692         uint64_t n = 0, ap = 0, q, i, a, hidx;
693         Object *o;
694
695         assert(f);
696         assert(first);
697         assert(idx);
698         assert(p > 0);
699
700         a = le64toh(*first);
701         i = hidx = le64toh(*idx);
702         while (a > 0) {
703
704                 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
705                 if (r < 0)
706                         return r;
707
708                 n = journal_file_entry_array_n_items(o);
709                 if (i < n) {
710                         o->entry_array.items[i] = htole64(p);
711                         *idx = htole64(hidx + 1);
712                         return 0;
713                 }
714
715                 i -= n;
716                 ap = a;
717                 a = le64toh(o->entry_array.next_entry_array_offset);
718         }
719
720         if (hidx > n)
721                 n = (hidx+1) * 2;
722         else
723                 n = n * 2;
724
725         if (n < 4)
726                 n = 4;
727
728         r = journal_file_append_object(f, OBJECT_ENTRY_ARRAY,
729                                        offsetof(Object, entry_array.items) + n * sizeof(uint64_t),
730                                        &o, &q);
731         if (r < 0)
732                 return r;
733
734         o->entry_array.items[i] = htole64(p);
735
736         if (ap == 0)
737                 *first = q;
738         else {
739                 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, ap, &o);
740                 if (r < 0)
741                         return r;
742
743                 o->entry_array.next_entry_array_offset = htole64(q);
744         }
745
746         *idx = htole64(hidx + 1);
747
748         return 0;
749 }
750
751 static int link_entry_into_array_plus_one(JournalFile *f,
752                                           uint64_t *extra,
753                                           uint64_t *first,
754                                           uint64_t *idx,
755                                           uint64_t p) {
756
757         int r;
758
759         assert(f);
760         assert(extra);
761         assert(first);
762         assert(idx);
763         assert(p > 0);
764
765         if (*idx == 0)
766                 *extra = htole64(p);
767         else {
768                 uint64_t i;
769
770                 i = le64toh(*idx) - 1;
771                 r = link_entry_into_array(f, first, &i, p);
772                 if (r < 0)
773                         return r;
774         }
775
776         *idx = htole64(le64toh(*idx) + 1);
777         return 0;
778 }
779
780 static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
781         uint64_t p;
782         int r;
783         assert(f);
784         assert(o);
785         assert(offset > 0);
786
787         p = le64toh(o->entry.items[i].object_offset);
788         if (p == 0)
789                 return -EINVAL;
790
791         r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
792         if (r < 0)
793                 return r;
794
795         return link_entry_into_array_plus_one(f,
796                                               &o->data.entry_offset,
797                                               &o->data.entry_array_offset,
798                                               &o->data.n_entries,
799                                               offset);
800 }
801
802 static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
803         uint64_t n, i;
804         int r;
805
806         assert(f);
807         assert(o);
808         assert(offset > 0);
809         assert(o->object.type == OBJECT_ENTRY);
810
811         /* Link up the entry itself */
812         r = link_entry_into_array(f,
813                                   &f->header->entry_array_offset,
814                                   &f->header->n_entries,
815                                   offset);
816         if (r < 0)
817                 return r;
818
819         log_error("%s %lu", f->path, (unsigned long) f->header->n_entries);
820
821         if (f->header->head_entry_realtime == 0)
822                 f->header->head_entry_realtime = o->entry.realtime;
823
824         f->header->tail_entry_realtime = o->entry.realtime;
825         f->header->tail_entry_monotonic = o->entry.monotonic;
826
827         f->tail_entry_monotonic_valid = true;
828
829         /* Link up the items */
830         n = journal_file_entry_n_items(o);
831         for (i = 0; i < n; i++) {
832                 r = journal_file_link_entry_item(f, o, offset, i);
833                 if (r < 0)
834                         return r;
835         }
836
837         return 0;
838 }
839
840 static int journal_file_append_entry_internal(
841                 JournalFile *f,
842                 const dual_timestamp *ts,
843                 uint64_t xor_hash,
844                 const EntryItem items[], unsigned n_items,
845                 uint64_t *seqnum,
846                 Object **ret, uint64_t *offset) {
847         uint64_t np;
848         uint64_t osize;
849         Object *o;
850         int r;
851
852         assert(f);
853         assert(items || n_items == 0);
854         assert(ts);
855
856         osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
857
858         r = journal_file_append_object(f, OBJECT_ENTRY, osize, &o, &np);
859         if (r < 0)
860                 return r;
861
862         o->entry.seqnum = htole64(journal_file_seqnum(f, seqnum));
863         memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
864         o->entry.realtime = htole64(ts->realtime);
865         o->entry.monotonic = htole64(ts->monotonic);
866         o->entry.xor_hash = htole64(xor_hash);
867         o->entry.boot_id = f->header->boot_id;
868
869         r = journal_file_link_entry(f, o, np);
870         if (r < 0)
871                 return r;
872
873         if (ret)
874                 *ret = o;
875
876         if (offset)
877                 *offset = np;
878
879         return 0;
880 }
881
882 int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqnum, Object **ret, uint64_t *offset) {
883         unsigned i;
884         EntryItem *items;
885         int r;
886         uint64_t xor_hash = 0;
887         struct dual_timestamp _ts;
888
889         assert(f);
890         assert(iovec || n_iovec == 0);
891
892         if (!f->writable)
893                 return -EPERM;
894
895         if (!ts) {
896                 dual_timestamp_get(&_ts);
897                 ts = &_ts;
898         }
899
900         if (f->tail_entry_monotonic_valid &&
901             ts->monotonic < le64toh(f->header->tail_entry_monotonic))
902                 return -EINVAL;
903
904         if (ts->realtime < le64toh(f->header->tail_entry_realtime))
905                 return -EINVAL;
906
907         items = new(EntryItem, n_iovec);
908         if (!items)
909                 return -ENOMEM;
910
911         for (i = 0; i < n_iovec; i++) {
912                 uint64_t p;
913                 Object *o;
914
915                 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
916                 if (r < 0)
917                         goto finish;
918
919                 xor_hash ^= le64toh(o->data.hash);
920                 items[i].object_offset = htole64(p);
921                 items[i].hash = o->data.hash;
922         }
923
924         r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset);
925
926 finish:
927         free(items);
928
929         return r;
930 }
931
932 static int generic_array_get(JournalFile *f,
933                              uint64_t first,
934                              uint64_t i,
935                              Object **ret, uint64_t *offset) {
936
937         Object *o;
938         uint64_t p, a;
939         int r;
940
941         assert(f);
942
943         a = first;
944         while (a > 0) {
945                 uint64_t n;
946
947                 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
948                 if (r < 0)
949                         return r;
950
951                 n = journal_file_entry_array_n_items(o);
952                 if (i < n) {
953                         p = le64toh(o->entry_array.items[i]);
954                         break;
955                 }
956
957                 i -= n;
958                 a = le64toh(o->entry_array.next_entry_array_offset);
959         }
960
961         if (a <= 0 || p <= 0)
962                 return 0;
963
964         r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
965         if (r < 0)
966                 return r;
967
968         if (ret)
969                 *ret = o;
970
971         if (offset)
972                 *offset = p;
973
974         return 1;
975 }
976
977 static int generic_array_get_plus_one(JournalFile *f,
978                                       uint64_t extra,
979                                       uint64_t first,
980                                       uint64_t i,
981                                       Object **ret, uint64_t *offset) {
982
983         Object *o;
984
985         assert(f);
986
987         if (i == 0) {
988                 int r;
989
990                 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
991                 if (r < 0)
992                         return r;
993
994                 if (ret)
995                         *ret = o;
996
997                 if (offset)
998                         *offset = extra;
999
1000                 return 1;
1001         }
1002
1003         return generic_array_get(f, first, i-1, ret, offset);
1004 }
1005
1006 enum {
1007         TEST_FOUND,
1008         TEST_LEFT,
1009         TEST_RIGHT
1010 };
1011
1012 static int generic_array_bisect(JournalFile *f,
1013                                 uint64_t first,
1014                                 uint64_t n,
1015                                 uint64_t needle,
1016                                 int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1017                                 direction_t direction,
1018                                 Object **ret,
1019                                 uint64_t *offset,
1020                                 uint64_t *idx) {
1021
1022         uint64_t a, p, t = 0, i = 0, last_p = 0;
1023         bool subtract_one = false;
1024         Object *o, *array = NULL;
1025         int r;
1026
1027         assert(f);
1028         assert(test_object);
1029
1030         a = first;
1031         while (a > 0) {
1032                 uint64_t left, right, k, lp;
1033
1034                 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &array);
1035                 if (r < 0)
1036                         return r;
1037
1038                 k = journal_file_entry_array_n_items(array);
1039                 right = MIN(k, n);
1040                 if (right <= 0)
1041                         return 0;
1042
1043                 i = right - 1;
1044                 lp = p = le64toh(array->entry_array.items[i]);
1045                 if (p <= 0)
1046                         return -EBADMSG;
1047
1048                 r = test_object(f, p, needle);
1049                 if (r < 0)
1050                         return r;
1051
1052                 if (r == TEST_FOUND)
1053                         r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1054
1055                 if (r == TEST_RIGHT) {
1056                         left = 0;
1057                         right -= 1;
1058                         for (;;) {
1059                                 if (left == right) {
1060                                         if (direction == DIRECTION_UP)
1061                                                 subtract_one = true;
1062
1063                                         i = left;
1064                                         goto found;
1065                                 }
1066
1067                                 assert(left < right);
1068
1069                                 i = (left + right) / 2;
1070                                 p = le64toh(array->entry_array.items[i]);
1071                                 if (p <= 0)
1072                                         return -EBADMSG;
1073
1074                                 r = test_object(f, p, needle);
1075                                 if (r < 0)
1076                                         return r;
1077
1078                                 if (r == TEST_FOUND)
1079                                         r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
1080
1081                                 if (r == TEST_RIGHT)
1082                                         right = i;
1083                                 else
1084                                         left = i + 1;
1085                         }
1086                 }
1087
1088                 if (k > n)
1089                         return 0;
1090
1091                 last_p = lp;
1092
1093                 n -= k;
1094                 t += k;
1095                 a = le64toh(array->entry_array.next_entry_array_offset);
1096         }
1097
1098         return 0;
1099
1100 found:
1101         if (subtract_one && t == 0 && i == 0)
1102                 return 0;
1103
1104         if (subtract_one && i == 0)
1105                 p = last_p;
1106         else if (subtract_one)
1107                 p = le64toh(array->entry_array.items[i-1]);
1108         else
1109                 p = le64toh(array->entry_array.items[i]);
1110
1111         r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1112         if (r < 0)
1113                 return r;
1114
1115         if (ret)
1116                 *ret = o;
1117
1118         if (offset)
1119                 *offset = p;
1120
1121         if (idx)
1122                 *idx = t + i - (subtract_one ? 1 : 0);
1123
1124         return 1;
1125 }
1126
1127 static int generic_array_bisect_plus_one(JournalFile *f,
1128                                          uint64_t extra,
1129                                          uint64_t first,
1130                                          uint64_t n,
1131                                          uint64_t needle,
1132                                          int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
1133                                          direction_t direction,
1134                                          Object **ret,
1135                                          uint64_t *offset,
1136                                          uint64_t *idx) {
1137
1138         int r;
1139
1140         assert(f);
1141         assert(test_object);
1142
1143         if (n <= 0)
1144                 return 0;
1145
1146         /* This bisects the array in object 'first', but first checks
1147          * an extra  */
1148
1149         r = test_object(f, extra, needle);
1150         if (r < 0)
1151                 return r;
1152         else if (r == TEST_FOUND) {
1153                 Object *o;
1154
1155                 r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o);
1156                 if (r < 0)
1157                         return r;
1158
1159                 if (ret)
1160                         *ret = o;
1161
1162                 if (offset)
1163                         *offset = extra;
1164         } else if (r == TEST_RIGHT)
1165                 return 0;
1166
1167         r = generic_array_bisect(f, first, n-1, needle, test_object, direction, ret, offset, idx);
1168
1169         if (r > 0)
1170                 (*idx) ++;
1171
1172         return r;
1173 }
1174
1175 static int test_object_seqnum(JournalFile *f, uint64_t p, uint64_t needle) {
1176         Object *o;
1177         int r;
1178
1179         assert(f);
1180         assert(p > 0);
1181
1182         r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1183         if (r < 0)
1184                 return r;
1185
1186         if (le64toh(o->entry.seqnum) == needle)
1187                 return TEST_FOUND;
1188         else if (le64toh(o->entry.seqnum) < needle)
1189                 return TEST_LEFT;
1190         else
1191                 return TEST_RIGHT;
1192 }
1193
1194 int journal_file_move_to_entry_by_seqnum(
1195                 JournalFile *f,
1196                 uint64_t seqnum,
1197                 direction_t direction,
1198                 Object **ret,
1199                 uint64_t *offset) {
1200
1201         return generic_array_bisect(f,
1202                                     le64toh(f->header->entry_array_offset),
1203                                     le64toh(f->header->n_entries),
1204                                     seqnum,
1205                                     test_object_seqnum,
1206                                     direction,
1207                                     ret, offset, NULL);
1208 }
1209
1210 static int test_object_realtime(JournalFile *f, uint64_t p, uint64_t needle) {
1211         Object *o;
1212         int r;
1213
1214         assert(f);
1215         assert(p > 0);
1216
1217         r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1218         if (r < 0)
1219                 return r;
1220
1221         if (le64toh(o->entry.realtime) == needle)
1222                 return TEST_FOUND;
1223         else if (le64toh(o->entry.realtime) < needle)
1224                 return TEST_LEFT;
1225         else
1226                 return TEST_RIGHT;
1227 }
1228
1229 int journal_file_move_to_entry_by_realtime(
1230                 JournalFile *f,
1231                 uint64_t realtime,
1232                 direction_t direction,
1233                 Object **ret,
1234                 uint64_t *offset) {
1235
1236         return generic_array_bisect(f,
1237                                     le64toh(f->header->entry_array_offset),
1238                                     le64toh(f->header->n_entries),
1239                                     realtime,
1240                                     test_object_realtime,
1241                                     direction,
1242                                     ret, offset, NULL);
1243 }
1244
1245 static int test_object_monotonic(JournalFile *f, uint64_t p, uint64_t needle) {
1246         Object *o;
1247         int r;
1248
1249         assert(f);
1250         assert(p > 0);
1251
1252         r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
1253         if (r < 0)
1254                 return r;
1255
1256         if (le64toh(o->entry.monotonic) == needle)
1257                 return TEST_FOUND;
1258         else if (le64toh(o->entry.monotonic) < needle)
1259                 return TEST_LEFT;
1260         else
1261                 return TEST_RIGHT;
1262 }
1263
1264 int journal_file_move_to_entry_by_monotonic(
1265                 JournalFile *f,
1266                 sd_id128_t boot_id,
1267                 uint64_t monotonic,
1268                 direction_t direction,
1269                 Object **ret,
1270                 uint64_t *offset) {
1271
1272         char t[8+32+1] = "_BOOT_ID=";
1273         Object *o;
1274         int r;
1275
1276         sd_id128_to_string(boot_id, t + 8);
1277
1278         r = journal_file_find_data_object(f, t, strlen(t), &o, NULL);
1279         if (r < 0)
1280                 return r;
1281         else if (r == 0)
1282                 return -ENOENT;
1283
1284         return generic_array_bisect_plus_one(f,
1285                                              le64toh(o->data.entry_offset),
1286                                              le64toh(o->data.entry_array_offset),
1287                                              le64toh(o->data.n_entries),
1288                                              monotonic,
1289                                              test_object_monotonic,
1290                                              direction,
1291                                              ret, offset, NULL);
1292 }
1293
1294 static int test_object_offset(JournalFile *f, uint64_t p, uint64_t needle) {
1295         assert(f);
1296         assert(p > 0);
1297
1298         if (p == needle)
1299                 return TEST_FOUND;
1300         else if (p < needle)
1301                 return TEST_LEFT;
1302         else
1303                 return TEST_RIGHT;
1304 }
1305
1306 int journal_file_next_entry(
1307                 JournalFile *f,
1308                 Object *o, uint64_t p,
1309                 direction_t direction,
1310                 Object **ret, uint64_t *offset) {
1311
1312         uint64_t i, n;
1313         int r;
1314
1315         assert(f);
1316         assert(p > 0 || !o);
1317
1318         n = le64toh(f->header->n_entries);
1319         if (n <= 0)
1320                 return 0;
1321
1322         if (!o)
1323                 i = direction == DIRECTION_DOWN ? 0 : n - 1;
1324         else {
1325                 if (o->object.type != OBJECT_ENTRY)
1326                         return -EINVAL;
1327
1328                 r = generic_array_bisect(f,
1329                                          le64toh(f->header->entry_array_offset),
1330                                          le64toh(f->header->n_entries),
1331                                          p,
1332                                          test_object_offset,
1333                                          DIRECTION_DOWN,
1334                                          NULL, NULL,
1335                                          &i);
1336                 if (r <= 0)
1337                         return r;
1338
1339                 if (direction == DIRECTION_DOWN) {
1340                         if (i >= n - 1)
1341                                 return 0;
1342
1343                         i++;
1344                 } else {
1345                         if (i <= 0)
1346                                 return 0;
1347
1348                         i--;
1349                 }
1350         }
1351
1352         /* And jump to it */
1353         return generic_array_get(f,
1354                                  le64toh(f->header->entry_array_offset),
1355                                  i,
1356                                  ret, offset);
1357 }
1358
1359 int journal_file_skip_entry(
1360                 JournalFile *f,
1361                 Object *o, uint64_t p,
1362                 int64_t skip,
1363                 Object **ret, uint64_t *offset) {
1364
1365         uint64_t i, n;
1366         int r;
1367
1368         assert(f);
1369         assert(o);
1370         assert(p > 0);
1371
1372         if (o->object.type != OBJECT_ENTRY)
1373                 return -EINVAL;
1374
1375         r = generic_array_bisect(f,
1376                                  le64toh(f->header->entry_array_offset),
1377                                  le64toh(f->header->n_entries),
1378                                  p,
1379                                  test_object_offset,
1380                                  DIRECTION_DOWN,
1381                                  NULL, NULL,
1382                                  &i);
1383         if (r <= 0)
1384                 return r;
1385
1386         /* Calculate new index */
1387         if (skip < 0) {
1388                 if ((uint64_t) -skip >= i)
1389                         i = 0;
1390                 else
1391                         i = i - (uint64_t) -skip;
1392         } else
1393                 i  += (uint64_t) skip;
1394
1395         n = le64toh(f->header->n_entries);
1396         if (n <= 0)
1397                 return -EBADMSG;
1398
1399         if (i >= n)
1400                 i = n-1;
1401
1402         return generic_array_get(f,
1403                                  le64toh(f->header->entry_array_offset),
1404                                  i,
1405                                  ret, offset);
1406 }
1407
1408 int journal_file_next_entry_for_data(
1409                 JournalFile *f,
1410                 Object *o, uint64_t p,
1411                 uint64_t data_offset,
1412                 direction_t direction,
1413                 Object **ret, uint64_t *offset) {
1414
1415         uint64_t n, i;
1416         int r;
1417         Object *d;
1418
1419         assert(f);
1420         assert(p > 0 || !o);
1421
1422         r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1423         if (r <= 0)
1424                 return r;
1425
1426         n = le64toh(d->data.n_entries);
1427         if (n <= 0)
1428                 return n;
1429
1430         if (!o)
1431                 i = direction == DIRECTION_DOWN ? 0 : n - 1;
1432         else {
1433                 if (o->object.type != OBJECT_ENTRY)
1434                         return -EINVAL;
1435
1436                 r = generic_array_bisect_plus_one(f,
1437                                                   le64toh(d->data.entry_offset),
1438                                                   le64toh(d->data.entry_array_offset),
1439                                                   le64toh(d->data.n_entries),
1440                                                   p,
1441                                                   test_object_offset,
1442                                                   DIRECTION_DOWN,
1443                                                   NULL, NULL,
1444                                                   &i);
1445
1446                 if (r <= 0)
1447                         return r;
1448
1449                 if (direction == DIRECTION_DOWN) {
1450                         if (i >= n - 1)
1451                                 return 0;
1452
1453                         i++;
1454                 } else {
1455                         if (i <= 0)
1456                                 return 0;
1457
1458                         i--;
1459                 }
1460
1461         }
1462
1463         return generic_array_get_plus_one(f,
1464                                           le64toh(d->data.entry_offset),
1465                                           le64toh(d->data.entry_array_offset),
1466                                           i,
1467                                           ret, offset);
1468 }
1469
1470 int journal_file_move_to_entry_by_seqnum_for_data(
1471                 JournalFile *f,
1472                 uint64_t data_offset,
1473                 uint64_t seqnum,
1474                 direction_t direction,
1475                 Object **ret, uint64_t *offset) {
1476
1477         Object *d;
1478         int r;
1479
1480         r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1481         if (r <= 0)
1482                 return r;
1483
1484         return generic_array_bisect_plus_one(f,
1485                                              le64toh(d->data.entry_offset),
1486                                              le64toh(d->data.entry_array_offset),
1487                                              le64toh(d->data.n_entries),
1488                                              seqnum,
1489                                              test_object_seqnum,
1490                                              direction,
1491                                              ret, offset, NULL);
1492 }
1493
1494 int journal_file_move_to_entry_by_realtime_for_data(
1495                 JournalFile *f,
1496                 uint64_t data_offset,
1497                 uint64_t realtime,
1498                 direction_t direction,
1499                 Object **ret, uint64_t *offset) {
1500
1501         Object *d;
1502         int r;
1503
1504         r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d);
1505         if (r <= 0)
1506                 return r;
1507
1508         return generic_array_bisect_plus_one(f,
1509                                              le64toh(d->data.entry_offset),
1510                                              le64toh(d->data.entry_array_offset),
1511                                              le64toh(d->data.n_entries),
1512                                              realtime,
1513                                              test_object_realtime,
1514                                              direction,
1515                                              ret, offset, NULL);
1516 }
1517
1518 void journal_file_dump(JournalFile *f) {
1519         char a[33], b[33], c[33];
1520         Object *o;
1521         int r;
1522         uint64_t p;
1523
1524         assert(f);
1525
1526         printf("File Path: %s\n"
1527                "File ID: %s\n"
1528                "Machine ID: %s\n"
1529                "Boot ID: %s\n"
1530                "Arena size: %llu\n"
1531                "Objects: %lu\n"
1532                "Entries: %lu\n",
1533                f->path,
1534                sd_id128_to_string(f->header->file_id, a),
1535                sd_id128_to_string(f->header->machine_id, b),
1536                sd_id128_to_string(f->header->boot_id, c),
1537                (unsigned long long) le64toh(f->header->arena_size),
1538                (unsigned long) le64toh(f->header->n_objects),
1539                (unsigned long) le64toh(f->header->n_entries));
1540
1541         p = le64toh(f->header->arena_offset);
1542         while (p != 0) {
1543                 r = journal_file_move_to_object(f, -1, p, &o);
1544                 if (r < 0)
1545                         goto fail;
1546
1547                 switch (o->object.type) {
1548
1549                 case OBJECT_UNUSED:
1550                         printf("Type: OBJECT_UNUSED\n");
1551                         break;
1552
1553                 case OBJECT_DATA:
1554                         printf("Type: OBJECT_DATA\n");
1555                         break;
1556
1557                 case OBJECT_ENTRY:
1558                         printf("Type: OBJECT_ENTRY %llu %llu %llu\n",
1559                                (unsigned long long) le64toh(o->entry.seqnum),
1560                                (unsigned long long) le64toh(o->entry.monotonic),
1561                                (unsigned long long) le64toh(o->entry.realtime));
1562                         break;
1563
1564                 case OBJECT_FIELD_HASH_TABLE:
1565                         printf("Type: OBJECT_FIELD_HASH_TABLE\n");
1566                         break;
1567
1568                 case OBJECT_DATA_HASH_TABLE:
1569                         printf("Type: OBJECT_DATA_HASH_TABLE\n");
1570                         break;
1571
1572                 case OBJECT_ENTRY_ARRAY:
1573                         printf("Type: OBJECT_ENTRY_ARRAY\n");
1574                         break;
1575                 }
1576
1577                 if (p == le64toh(f->header->tail_object_offset))
1578                         p = 0;
1579                 else
1580                         p = p + ALIGN64(le64toh(o->object.size));
1581         }
1582
1583         return;
1584 fail:
1585         log_error("File corrupt");
1586 }
1587
1588 int journal_file_open(
1589                 const char *fname,
1590                 int flags,
1591                 mode_t mode,
1592                 JournalFile *template,
1593                 JournalFile **ret) {
1594
1595         JournalFile *f;
1596         int r;
1597         bool newly_created = false;
1598
1599         assert(fname);
1600
1601         if ((flags & O_ACCMODE) != O_RDONLY &&
1602             (flags & O_ACCMODE) != O_RDWR)
1603                 return -EINVAL;
1604
1605         f = new0(JournalFile, 1);
1606         if (!f)
1607                 return -ENOMEM;
1608
1609         f->fd = -1;
1610         f->flags = flags;
1611         f->mode = mode;
1612         f->writable = (flags & O_ACCMODE) != O_RDONLY;
1613         f->prot = prot_from_flags(flags);
1614
1615         f->path = strdup(fname);
1616         if (!f->path) {
1617                 r = -ENOMEM;
1618                 goto fail;
1619         }
1620
1621         f->fd = open(f->path, f->flags|O_CLOEXEC, f->mode);
1622         if (f->fd < 0) {
1623                 r = -errno;
1624                 goto fail;
1625         }
1626
1627         if (fstat(f->fd, &f->last_stat) < 0) {
1628                 r = -errno;
1629                 goto fail;
1630         }
1631
1632         if (f->last_stat.st_size == 0 && f->writable) {
1633                 newly_created = true;
1634
1635                 r = journal_file_init_header(f, template);
1636                 if (r < 0)
1637                         goto fail;
1638
1639                 if (fstat(f->fd, &f->last_stat) < 0) {
1640                         r = -errno;
1641                         goto fail;
1642                 }
1643         }
1644
1645         if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1646                 r = -EIO;
1647                 goto fail;
1648         }
1649
1650         f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1651         if (f->header == MAP_FAILED) {
1652                 f->header = NULL;
1653                 r = -errno;
1654                 goto fail;
1655         }
1656
1657         if (!newly_created) {
1658                 r = journal_file_verify_header(f);
1659                 if (r < 0)
1660                         goto fail;
1661         }
1662
1663         if (f->writable) {
1664                 r = journal_file_refresh_header(f);
1665                 if (r < 0)
1666                         goto fail;
1667         }
1668
1669         if (newly_created) {
1670
1671                 r = journal_file_setup_field_hash_table(f);
1672                 if (r < 0)
1673                         goto fail;
1674
1675                 r = journal_file_setup_data_hash_table(f);
1676                 if (r < 0)
1677                         goto fail;
1678         }
1679
1680         r = journal_file_map_field_hash_table(f);
1681         if (r < 0)
1682                 goto fail;
1683
1684         r = journal_file_map_data_hash_table(f);
1685         if (r < 0)
1686                 goto fail;
1687
1688         if (ret)
1689                 *ret = f;
1690
1691         return 0;
1692
1693 fail:
1694         journal_file_close(f);
1695
1696         return r;
1697 }
1698
1699 int journal_file_rotate(JournalFile **f) {
1700         char *p;
1701         size_t l;
1702         JournalFile *old_file, *new_file = NULL;
1703         int r;
1704
1705         assert(f);
1706         assert(*f);
1707
1708         old_file = *f;
1709
1710         if (!old_file->writable)
1711                 return -EINVAL;
1712
1713         if (!endswith(old_file->path, ".journal"))
1714                 return -EINVAL;
1715
1716         l = strlen(old_file->path);
1717
1718         p = new(char, l + 1 + 16 + 1 + 32 + 1 + 16 + 1);
1719         if (!p)
1720                 return -ENOMEM;
1721
1722         memcpy(p, old_file->path, l - 8);
1723         p[l-8] = '@';
1724         sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1);
1725         snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
1726                  "-%016llx-%016llx.journal",
1727                  (unsigned long long) le64toh((*f)->header->seqnum),
1728                  (unsigned long long) le64toh((*f)->header->tail_entry_realtime));
1729
1730         r = rename(old_file->path, p);
1731         free(p);
1732
1733         if (r < 0)
1734                 return -errno;
1735
1736         old_file->header->state = le32toh(STATE_ARCHIVED);
1737
1738         r = journal_file_open(old_file->path, old_file->flags, old_file->mode, old_file, &new_file);
1739         journal_file_close(old_file);
1740
1741         *f = new_file;
1742         return r;
1743 }
1744
1745 struct vacuum_info {
1746         off_t usage;
1747         char *filename;
1748
1749         uint64_t realtime;
1750         sd_id128_t seqnum_id;
1751         uint64_t seqnum;
1752 };
1753
1754 static int vacuum_compare(const void *_a, const void *_b) {
1755         const struct vacuum_info *a, *b;
1756
1757         a = _a;
1758         b = _b;
1759
1760         if (sd_id128_equal(a->seqnum_id, b->seqnum_id)) {
1761                 if (a->seqnum < b->seqnum)
1762                         return -1;
1763                 else if (a->seqnum > b->seqnum)
1764                         return 1;
1765                 else
1766                         return 0;
1767         }
1768
1769         if (a->realtime < b->realtime)
1770                 return -1;
1771         else if (a->realtime > b->realtime)
1772                 return 1;
1773         else
1774                 return memcmp(&a->seqnum_id, &b->seqnum_id, 16);
1775 }
1776
1777 int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t min_free) {
1778         DIR *d;
1779         int r = 0;
1780         struct vacuum_info *list = NULL;
1781         unsigned n_list = 0, n_allocated = 0, i;
1782         uint64_t sum = 0;
1783
1784         assert(directory);
1785
1786         if (max_use <= 0)
1787                 max_use = DEFAULT_MAX_USE;
1788
1789         d = opendir(directory);
1790         if (!d)
1791                 return -errno;
1792
1793         for (;;) {
1794                 int k;
1795                 struct dirent buf, *de;
1796                 size_t q;
1797                 struct stat st;
1798                 char *p;
1799                 unsigned long long seqnum, realtime;
1800                 sd_id128_t seqnum_id;
1801
1802                 k = readdir_r(d, &buf, &de);
1803                 if (k != 0) {
1804                         r = -k;
1805                         goto finish;
1806                 }
1807
1808                 if (!de)
1809                         break;
1810
1811                 if (!dirent_is_file_with_suffix(de, ".journal"))
1812                         continue;
1813
1814                 q = strlen(de->d_name);
1815
1816                 if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8)
1817                         continue;
1818
1819                 if (de->d_name[q-8-16-1] != '-' ||
1820                     de->d_name[q-8-16-1-16-1] != '-' ||
1821                     de->d_name[q-8-16-1-16-1-32-1] != '@')
1822                         continue;
1823
1824                 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
1825                         continue;
1826
1827                 if (!S_ISREG(st.st_mode))
1828                         continue;
1829
1830                 p = strdup(de->d_name);
1831                 if (!p) {
1832                         r = -ENOMEM;
1833                         goto finish;
1834                 }
1835
1836                 de->d_name[q-8-16-1-16-1] = 0;
1837                 if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) {
1838                         free(p);
1839                         continue;
1840                 }
1841
1842                 if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) {
1843                         free(p);
1844                         continue;
1845                 }
1846
1847                 if (n_list >= n_allocated) {
1848                         struct vacuum_info *j;
1849
1850                         n_allocated = MAX(n_allocated * 2U, 8U);
1851                         j = realloc(list, n_allocated * sizeof(struct vacuum_info));
1852                         if (!j) {
1853                                 free(p);
1854                                 r = -ENOMEM;
1855                                 goto finish;
1856                         }
1857
1858                         list = j;
1859                 }
1860
1861                 list[n_list].filename = p;
1862                 list[n_list].usage = (uint64_t) st.st_blksize * (uint64_t) st.st_blocks;
1863                 list[n_list].seqnum = seqnum;
1864                 list[n_list].realtime = realtime;
1865                 list[n_list].seqnum_id = seqnum_id;
1866
1867                 sum += list[n_list].usage;
1868
1869                 n_list ++;
1870         }
1871
1872         qsort(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
1873
1874         for(i = 0; i < n_list; i++) {
1875                 struct statvfs ss;
1876
1877                 if (fstatvfs(dirfd(d), &ss) < 0) {
1878                         r = -errno;
1879                         goto finish;
1880                 }
1881
1882                 if (sum <= max_use &&
1883                     (uint64_t) ss.f_bavail * (uint64_t) ss.f_bsize >= min_free)
1884                         break;
1885
1886                 if (unlinkat(dirfd(d), list[i].filename, 0) >= 0) {
1887                         log_debug("Deleted archived journal %s/%s.", directory, list[i].filename);
1888                         sum -= list[i].usage;
1889                 } else if (errno != ENOENT)
1890                         log_warning("Failed to delete %s/%s: %m", directory, list[i].filename);
1891         }
1892
1893 finish:
1894         for (i = 0; i < n_list; i++)
1895                 free(list[i].filename);
1896
1897         free(list);
1898
1899         if (d)
1900                 closedir(d);
1901
1902         return r;
1903 }