1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/statvfs.h>
30 #include "journal-def.h"
31 #include "journal-file.h"
35 #define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*16ULL)
36 #define DEFAULT_FIELD_HASH_TABLE_SIZE (2047ULL*16ULL)
38 #define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
40 #define COMPRESSION_SIZE_THRESHOLD (512ULL)
42 /* This is the minimum journal file size */
43 #define JOURNAL_FILE_SIZE_MIN (64ULL*1024ULL)
45 /* These are the lower and upper bounds if we deduce the max_use value
46 * from the file system size */
47 #define DEFAULT_MAX_USE_LOWER (1ULL*1024ULL*1024ULL) /* 1 MiB */
48 #define DEFAULT_MAX_USE_UPPER (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
50 /* This is the upper bound if we deduce max_size from max_use */
51 #define DEFAULT_MAX_SIZE_UPPER (16ULL*1024ULL*1024ULL) /* 16 MiB */
53 /* This is the upper bound if we deduce the keep_free value from the
55 #define DEFAULT_KEEP_FREE_UPPER (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
57 /* This is the keep_free value when we can't determine the system
59 #define DEFAULT_KEEP_FREE (1024ULL*1024ULL) /* 1 MB */
61 static const char signature
[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
63 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
65 void journal_file_close(JournalFile
*f
) {
70 if (f
->header
&& f
->writable
)
71 f
->header
->state
= STATE_OFFLINE
;
74 for (t
= 0; t
< _WINDOW_MAX
; t
++)
75 if (f
->windows
[t
].ptr
)
76 munmap(f
->windows
[t
].ptr
, f
->windows
[t
].size
);
79 close_nointr_nofail(f
->fd
);
84 free(f
->compress_buffer
);
90 static int journal_file_init_header(JournalFile
*f
, JournalFile
*template) {
98 memcpy(h
.signature
, signature
, 8);
99 h
.arena_offset
= htole64(ALIGN64(sizeof(h
)));
101 r
= sd_id128_randomize(&h
.file_id
);
106 h
.seqnum_id
= template->header
->seqnum_id
;
107 h
.seqnum
= template->header
->seqnum
;
109 h
.seqnum_id
= h
.file_id
;
111 k
= pwrite(f
->fd
, &h
, sizeof(h
), 0);
121 static int journal_file_refresh_header(JournalFile
*f
) {
127 r
= sd_id128_get_machine(&f
->header
->machine_id
);
131 r
= sd_id128_get_boot(&boot_id
);
135 if (sd_id128_equal(boot_id
, f
->header
->boot_id
))
136 f
->tail_entry_monotonic_valid
= true;
138 f
->header
->boot_id
= boot_id
;
140 f
->header
->state
= STATE_ONLINE
;
142 __sync_synchronize();
147 static int journal_file_verify_header(JournalFile
*f
) {
150 if (memcmp(f
->header
, signature
, 8))
154 if ((le64toh(f
->header
->incompatible_flags
) & ~HEADER_INCOMPATIBLE_COMPRESSED
) != 0)
155 return -EPROTONOSUPPORT
;
157 if (f
->header
->incompatible_flags
!= 0)
158 return -EPROTONOSUPPORT
;
161 if ((uint64_t) f
->last_stat
.st_size
< (le64toh(f
->header
->arena_offset
) + le64toh(f
->header
->arena_size
)))
166 sd_id128_t machine_id
;
169 r
= sd_id128_get_machine(&machine_id
);
173 if (!sd_id128_equal(machine_id
, f
->header
->machine_id
))
176 state
= f
->header
->state
;
178 if (state
== STATE_ONLINE
)
179 log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f
->path
);
180 else if (state
== STATE_ARCHIVED
)
182 else if (state
!= STATE_OFFLINE
)
183 log_debug("Journal file %s has unknown state %u. Ignoring.", f
->path
, state
);
189 static int journal_file_allocate(JournalFile
*f
, uint64_t offset
, uint64_t size
) {
190 uint64_t old_size
, new_size
;
194 /* We assume that this file is not sparse, and we know that
195 * for sure, since we always call posix_fallocate()
199 le64toh(f
->header
->arena_offset
) +
200 le64toh(f
->header
->arena_size
);
202 new_size
= PAGE_ALIGN(offset
+ size
);
203 if (new_size
< le64toh(f
->header
->arena_offset
))
204 new_size
= le64toh(f
->header
->arena_offset
);
206 if (new_size
<= old_size
)
209 if (f
->metrics
.max_size
> 0 &&
210 new_size
> f
->metrics
.max_size
)
213 if (new_size
> f
->metrics
.min_size
&&
214 f
->metrics
.keep_free
> 0) {
217 if (fstatvfs(f
->fd
, &svfs
) >= 0) {
220 available
= svfs
.f_bfree
* svfs
.f_bsize
;
222 if (available
>= f
->metrics
.keep_free
)
223 available
-= f
->metrics
.keep_free
;
227 if (new_size
- old_size
> available
)
232 /* Note that the glibc fallocate() fallback is very
233 inefficient, hence we try to minimize the allocation area
235 if (posix_fallocate(f
->fd
, old_size
, new_size
- old_size
) < 0)
238 if (fstat(f
->fd
, &f
->last_stat
) < 0)
241 f
->header
->arena_size
= new_size
- htole64(f
->header
->arena_offset
);
246 static int journal_file_map(
255 uint64_t woffset
, wsize
;
262 woffset
= offset
& ~((uint64_t) page_size() - 1ULL);
263 wsize
= size
+ (offset
- woffset
);
264 wsize
= PAGE_ALIGN(wsize
);
266 /* Avoid SIGBUS on invalid accesses */
267 if (woffset
+ wsize
> (uint64_t) PAGE_ALIGN(f
->last_stat
.st_size
))
268 return -EADDRNOTAVAIL
;
270 window
= mmap(NULL
, wsize
, f
->prot
, MAP_SHARED
, f
->fd
, woffset
);
271 if (window
== MAP_FAILED
)
283 *ret
= (uint8_t*) window
+ (offset
- woffset
);
288 static int journal_file_move_to(JournalFile
*f
, int wt
, uint64_t offset
, uint64_t size
, void **ret
) {
297 assert(wt
< _WINDOW_MAX
);
299 if (offset
+ size
> (uint64_t) f
->last_stat
.st_size
) {
300 /* Hmm, out of range? Let's refresh the fstat() data
301 * first, before we trust that check. */
303 if (fstat(f
->fd
, &f
->last_stat
) < 0 ||
304 offset
+ size
> (uint64_t) f
->last_stat
.st_size
)
305 return -EADDRNOTAVAIL
;
310 if (_likely_(w
->ptr
&&
311 w
->offset
<= offset
&&
312 w
->offset
+ w
->size
>= offset
+ size
)) {
314 *ret
= (uint8_t*) w
->ptr
+ (offset
- w
->offset
);
319 if (munmap(w
->ptr
, w
->size
) < 0)
323 w
->size
= w
->offset
= 0;
326 if (size
< DEFAULT_WINDOW_SIZE
) {
327 /* If the default window size is larger then what was
328 * asked for extend the mapping a bit in the hope to
329 * minimize needed remappings later on. We add half
330 * the window space before and half behind the
331 * requested mapping */
333 delta
= (DEFAULT_WINDOW_SIZE
- size
) / 2;
339 size
= DEFAULT_WINDOW_SIZE
;
343 if (offset
+ size
> (uint64_t) f
->last_stat
.st_size
)
344 size
= (uint64_t) f
->last_stat
.st_size
- offset
;
347 return -EADDRNOTAVAIL
;
349 r
= journal_file_map(f
,
351 &w
->ptr
, &w
->offset
, &w
->size
,
357 *ret
= (uint8_t*) p
+ delta
;
361 static bool verify_hash(Object
*o
) {
366 if (o
->object
.type
== OBJECT_DATA
&& !(o
->object
.flags
& OBJECT_COMPRESSED
)) {
367 h1
= le64toh(o
->data
.hash
);
368 h2
= hash64(o
->data
.payload
, le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
));
369 } else if (o
->object
.type
== OBJECT_FIELD
) {
370 h1
= le64toh(o
->field
.hash
);
371 h2
= hash64(o
->field
.payload
, le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
));
378 int journal_file_move_to_object(JournalFile
*f
, int type
, uint64_t offset
, Object
**ret
) {
386 assert(type
< _OBJECT_TYPE_MAX
);
388 r
= journal_file_move_to(f
, type
>= 0 ? type
: WINDOW_UNKNOWN
, offset
, sizeof(ObjectHeader
), &t
);
393 s
= le64toh(o
->object
.size
);
395 if (s
< sizeof(ObjectHeader
))
398 if (type
>= 0 && o
->object
.type
!= type
)
401 if (s
> sizeof(ObjectHeader
)) {
402 r
= journal_file_move_to(f
, o
->object
.type
, offset
, s
, &t
);
416 static uint64_t journal_file_seqnum(JournalFile
*f
, uint64_t *seqnum
) {
421 r
= le64toh(f
->header
->seqnum
) + 1;
424 /* If an external seqnum counter was passed, we update
425 * both the local and the external one, and set it to
426 * the maximum of both */
434 f
->header
->seqnum
= htole64(r
);
436 if (f
->header
->first_seqnum
== 0)
437 f
->header
->first_seqnum
= htole64(r
);
442 static int journal_file_append_object(JournalFile
*f
, int type
, uint64_t size
, Object
**ret
, uint64_t *offset
) {
449 assert(size
>= sizeof(ObjectHeader
));
453 p
= le64toh(f
->header
->tail_object_offset
);
455 p
= le64toh(f
->header
->arena_offset
);
457 r
= journal_file_move_to_object(f
, -1, p
, &tail
);
461 p
+= ALIGN64(le64toh(tail
->object
.size
));
464 r
= journal_file_allocate(f
, p
, size
);
468 r
= journal_file_move_to(f
, type
, p
, size
, &t
);
475 o
->object
.type
= type
;
476 o
->object
.size
= htole64(size
);
478 f
->header
->tail_object_offset
= htole64(p
);
479 f
->header
->n_objects
= htole64(le64toh(f
->header
->n_objects
) + 1);
487 static int journal_file_setup_data_hash_table(JournalFile
*f
) {
494 s
= DEFAULT_DATA_HASH_TABLE_SIZE
;
495 r
= journal_file_append_object(f
,
496 OBJECT_DATA_HASH_TABLE
,
497 offsetof(Object
, hash_table
.items
) + s
,
502 memset(o
->hash_table
.items
, 0, s
);
504 f
->header
->data_hash_table_offset
= htole64(p
+ offsetof(Object
, hash_table
.items
));
505 f
->header
->data_hash_table_size
= htole64(s
);
510 static int journal_file_setup_field_hash_table(JournalFile
*f
) {
517 s
= DEFAULT_FIELD_HASH_TABLE_SIZE
;
518 r
= journal_file_append_object(f
,
519 OBJECT_FIELD_HASH_TABLE
,
520 offsetof(Object
, hash_table
.items
) + s
,
525 memset(o
->hash_table
.items
, 0, s
);
527 f
->header
->field_hash_table_offset
= htole64(p
+ offsetof(Object
, hash_table
.items
));
528 f
->header
->field_hash_table_size
= htole64(s
);
533 static int journal_file_map_data_hash_table(JournalFile
*f
) {
540 p
= le64toh(f
->header
->data_hash_table_offset
);
541 s
= le64toh(f
->header
->data_hash_table_size
);
543 r
= journal_file_move_to(f
,
544 WINDOW_DATA_HASH_TABLE
,
550 f
->data_hash_table
= t
;
554 static int journal_file_map_field_hash_table(JournalFile
*f
) {
561 p
= le64toh(f
->header
->field_hash_table_offset
);
562 s
= le64toh(f
->header
->field_hash_table_size
);
564 r
= journal_file_move_to(f
,
565 WINDOW_FIELD_HASH_TABLE
,
571 f
->field_hash_table
= t
;
575 static int journal_file_link_data(JournalFile
*f
, Object
*o
, uint64_t offset
, uint64_t hash
) {
582 assert(o
->object
.type
== OBJECT_DATA
);
584 o
->data
.next_hash_offset
= o
->data
.next_field_offset
= 0;
585 o
->data
.entry_offset
= o
->data
.entry_array_offset
= 0;
586 o
->data
.n_entries
= 0;
588 h
= hash
% (le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
));
589 p
= le64toh(f
->data_hash_table
[h
].head_hash_offset
);
591 /* Only entry in the hash table is easy */
592 f
->data_hash_table
[h
].head_hash_offset
= htole64(offset
);
594 /* Temporarily move back to the previous data object,
595 * to patch in pointer */
597 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
601 o
->data
.next_hash_offset
= htole64(offset
);
603 r
= journal_file_move_to_object(f
, OBJECT_DATA
, offset
, &o
);
608 f
->data_hash_table
[h
].tail_hash_offset
= htole64(offset
);
613 int journal_file_find_data_object_with_hash(
615 const void *data
, uint64_t size
, uint64_t hash
,
616 Object
**ret
, uint64_t *offset
) {
617 uint64_t p
, osize
, h
;
621 assert(data
|| size
== 0);
623 osize
= offsetof(Object
, data
.payload
) + size
;
625 if (f
->header
->data_hash_table_size
== 0)
628 h
= hash
% (le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
));
629 p
= le64toh(f
->data_hash_table
[h
].head_hash_offset
);
634 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
638 if (le64toh(o
->data
.hash
) != hash
)
641 if (o
->object
.flags
& OBJECT_COMPRESSED
) {
645 l
= le64toh(o
->object
.size
);
646 if (l
<= offsetof(Object
, data
.payload
))
649 l
-= offsetof(Object
, data
.payload
);
651 if (!uncompress_blob(o
->data
.payload
, l
, &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
))
655 memcmp(f
->compress_buffer
, data
, size
) == 0) {
666 return -EPROTONOSUPPORT
;
669 } else if (le64toh(o
->object
.size
) == osize
&&
670 memcmp(o
->data
.payload
, data
, size
) == 0) {
682 p
= le64toh(o
->data
.next_hash_offset
);
688 int journal_file_find_data_object(
690 const void *data
, uint64_t size
,
691 Object
**ret
, uint64_t *offset
) {
696 assert(data
|| size
== 0);
698 hash
= hash64(data
, size
);
700 return journal_file_find_data_object_with_hash(f
,
705 static int journal_file_append_data(JournalFile
*f
, const void *data
, uint64_t size
, Object
**ret
, uint64_t *offset
) {
710 bool compressed
= false;
713 assert(data
|| size
== 0);
715 hash
= hash64(data
, size
);
717 r
= journal_file_find_data_object_with_hash(f
, data
, size
, hash
, &o
, &p
);
731 osize
= offsetof(Object
, data
.payload
) + size
;
732 r
= journal_file_append_object(f
, OBJECT_DATA
, osize
, &o
, &p
);
736 o
->data
.hash
= htole64(hash
);
740 size
>= COMPRESSION_SIZE_THRESHOLD
) {
743 compressed
= compress_blob(data
, size
, o
->data
.payload
, &rsize
);
746 o
->object
.size
= htole64(offsetof(Object
, data
.payload
) + rsize
);
747 o
->object
.flags
|= OBJECT_COMPRESSED
;
749 f
->header
->incompatible_flags
= htole32(le32toh(f
->header
->incompatible_flags
) | HEADER_INCOMPATIBLE_COMPRESSED
);
751 log_debug("Compressed data object %lu -> %lu", (unsigned long) size
, (unsigned long) rsize
);
757 memcpy(o
->data
.payload
, data
, size
);
759 r
= journal_file_link_data(f
, o
, p
, hash
);
772 uint64_t journal_file_entry_n_items(Object
*o
) {
774 assert(o
->object
.type
== htole64(OBJECT_ENTRY
));
776 return (le64toh(o
->object
.size
) - offsetof(Object
, entry
.items
)) / sizeof(EntryItem
);
779 static uint64_t journal_file_entry_array_n_items(Object
*o
) {
781 assert(o
->object
.type
== htole64(OBJECT_ENTRY_ARRAY
));
783 return (le64toh(o
->object
.size
) - offsetof(Object
, entry_array
.items
)) / sizeof(uint64_t);
786 static int link_entry_into_array(JournalFile
*f
,
791 uint64_t n
= 0, ap
= 0, q
, i
, a
, hidx
;
800 i
= hidx
= le64toh(*idx
);
803 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
807 n
= journal_file_entry_array_n_items(o
);
809 o
->entry_array
.items
[i
] = htole64(p
);
810 *idx
= htole64(hidx
+ 1);
816 a
= le64toh(o
->entry_array
.next_entry_array_offset
);
827 r
= journal_file_append_object(f
, OBJECT_ENTRY_ARRAY
,
828 offsetof(Object
, entry_array
.items
) + n
* sizeof(uint64_t),
833 o
->entry_array
.items
[i
] = htole64(p
);
838 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, ap
, &o
);
842 o
->entry_array
.next_entry_array_offset
= htole64(q
);
845 *idx
= htole64(hidx
+ 1);
850 static int link_entry_into_array_plus_one(JournalFile
*f
,
869 i
= le64toh(*idx
) - 1;
870 r
= link_entry_into_array(f
, first
, &i
, p
);
875 *idx
= htole64(le64toh(*idx
) + 1);
879 static int journal_file_link_entry_item(JournalFile
*f
, Object
*o
, uint64_t offset
, uint64_t i
) {
886 p
= le64toh(o
->entry
.items
[i
].object_offset
);
890 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
894 return link_entry_into_array_plus_one(f
,
895 &o
->data
.entry_offset
,
896 &o
->data
.entry_array_offset
,
901 static int journal_file_link_entry(JournalFile
*f
, Object
*o
, uint64_t offset
) {
908 assert(o
->object
.type
== OBJECT_ENTRY
);
910 __sync_synchronize();
912 /* Link up the entry itself */
913 r
= link_entry_into_array(f
,
914 &f
->header
->entry_array_offset
,
915 &f
->header
->n_entries
,
920 /* log_debug("=> %s seqnr=%lu n_entries=%lu", f->path, (unsigned long) o->entry.seqnum, (unsigned long) f->header->n_entries); */
922 if (f
->header
->head_entry_realtime
== 0)
923 f
->header
->head_entry_realtime
= o
->entry
.realtime
;
925 f
->header
->tail_entry_realtime
= o
->entry
.realtime
;
926 f
->header
->tail_entry_monotonic
= o
->entry
.monotonic
;
928 f
->tail_entry_monotonic_valid
= true;
930 /* Link up the items */
931 n
= journal_file_entry_n_items(o
);
932 for (i
= 0; i
< n
; i
++) {
933 r
= journal_file_link_entry_item(f
, o
, offset
, i
);
941 static int journal_file_append_entry_internal(
943 const dual_timestamp
*ts
,
945 const EntryItem items
[], unsigned n_items
,
947 Object
**ret
, uint64_t *offset
) {
954 assert(items
|| n_items
== 0);
957 osize
= offsetof(Object
, entry
.items
) + (n_items
* sizeof(EntryItem
));
959 r
= journal_file_append_object(f
, OBJECT_ENTRY
, osize
, &o
, &np
);
963 o
->entry
.seqnum
= htole64(journal_file_seqnum(f
, seqnum
));
964 memcpy(o
->entry
.items
, items
, n_items
* sizeof(EntryItem
));
965 o
->entry
.realtime
= htole64(ts
->realtime
);
966 o
->entry
.monotonic
= htole64(ts
->monotonic
);
967 o
->entry
.xor_hash
= htole64(xor_hash
);
968 o
->entry
.boot_id
= f
->header
->boot_id
;
970 r
= journal_file_link_entry(f
, o
, np
);
983 void journal_file_post_change(JournalFile
*f
) {
986 /* inotify() does not receive IN_MODIFY events from file
987 * accesses done via mmap(). After each access we hence
988 * trigger IN_MODIFY by truncating the journal file to its
989 * current size which triggers IN_MODIFY. */
991 __sync_synchronize();
993 if (ftruncate(f
->fd
, f
->last_stat
.st_size
) < 0)
994 log_error("Failed to to truncate file to its own size: %m");
997 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
) {
1001 uint64_t xor_hash
= 0;
1002 struct dual_timestamp _ts
;
1005 assert(iovec
|| n_iovec
== 0);
1011 dual_timestamp_get(&_ts
);
1015 if (f
->tail_entry_monotonic_valid
&&
1016 ts
->monotonic
< le64toh(f
->header
->tail_entry_monotonic
))
1019 items
= alloca(sizeof(EntryItem
) * n_iovec
);
1021 for (i
= 0; i
< n_iovec
; i
++) {
1025 r
= journal_file_append_data(f
, iovec
[i
].iov_base
, iovec
[i
].iov_len
, &o
, &p
);
1029 xor_hash
^= le64toh(o
->data
.hash
);
1030 items
[i
].object_offset
= htole64(p
);
1031 items
[i
].hash
= o
->data
.hash
;
1034 r
= journal_file_append_entry_internal(f
, ts
, xor_hash
, items
, n_iovec
, seqnum
, ret
, offset
);
1036 journal_file_post_change(f
);
1041 static int generic_array_get(JournalFile
*f
,
1044 Object
**ret
, uint64_t *offset
) {
1056 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
1060 n
= journal_file_entry_array_n_items(o
);
1062 p
= le64toh(o
->entry_array
.items
[i
]);
1067 a
= le64toh(o
->entry_array
.next_entry_array_offset
);
1070 if (a
<= 0 || p
<= 0)
1073 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
1086 static int generic_array_get_plus_one(JournalFile
*f
,
1090 Object
**ret
, uint64_t *offset
) {
1099 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, extra
, &o
);
1112 return generic_array_get(f
, first
, i
-1, ret
, offset
);
1121 static int generic_array_bisect(JournalFile
*f
,
1125 int (*test_object
)(JournalFile
*f
, uint64_t p
, uint64_t needle
),
1126 direction_t direction
,
1131 uint64_t a
, p
, t
= 0, i
= 0, last_p
= 0;
1132 bool subtract_one
= false;
1133 Object
*o
, *array
= NULL
;
1137 assert(test_object
);
1141 uint64_t left
, right
, k
, lp
;
1143 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &array
);
1147 k
= journal_file_entry_array_n_items(array
);
1153 lp
= p
= le64toh(array
->entry_array
.items
[i
]);
1157 r
= test_object(f
, p
, needle
);
1161 if (r
== TEST_FOUND
)
1162 r
= direction
== DIRECTION_DOWN
? TEST_RIGHT
: TEST_LEFT
;
1164 if (r
== TEST_RIGHT
) {
1168 if (left
== right
) {
1169 if (direction
== DIRECTION_UP
)
1170 subtract_one
= true;
1176 assert(left
< right
);
1178 i
= (left
+ right
) / 2;
1179 p
= le64toh(array
->entry_array
.items
[i
]);
1183 r
= test_object(f
, p
, needle
);
1187 if (r
== TEST_FOUND
)
1188 r
= direction
== DIRECTION_DOWN
? TEST_RIGHT
: TEST_LEFT
;
1190 if (r
== TEST_RIGHT
)
1204 a
= le64toh(array
->entry_array
.next_entry_array_offset
);
1210 if (subtract_one
&& t
== 0 && i
== 0)
1213 if (subtract_one
&& i
== 0)
1215 else if (subtract_one
)
1216 p
= le64toh(array
->entry_array
.items
[i
-1]);
1218 p
= le64toh(array
->entry_array
.items
[i
]);
1220 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
1231 *idx
= t
+ i
- (subtract_one
? 1 : 0);
1236 static int generic_array_bisect_plus_one(JournalFile
*f
,
1241 int (*test_object
)(JournalFile
*f
, uint64_t p
, uint64_t needle
),
1242 direction_t direction
,
1250 assert(test_object
);
1255 /* This bisects the array in object 'first', but first checks
1257 r
= test_object(f
, extra
, needle
);
1260 else if (r
== TEST_FOUND
) {
1263 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, extra
, &o
);
1277 } else if (r
== TEST_RIGHT
)
1280 r
= generic_array_bisect(f
, first
, n
-1, needle
, test_object
, direction
, ret
, offset
, idx
);
1288 static int test_object_seqnum(JournalFile
*f
, uint64_t p
, uint64_t needle
) {
1295 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
1299 if (le64toh(o
->entry
.seqnum
) == needle
)
1301 else if (le64toh(o
->entry
.seqnum
) < needle
)
1307 int journal_file_move_to_entry_by_seqnum(
1310 direction_t direction
,
1314 return generic_array_bisect(f
,
1315 le64toh(f
->header
->entry_array_offset
),
1316 le64toh(f
->header
->n_entries
),
1323 static int test_object_realtime(JournalFile
*f
, uint64_t p
, uint64_t needle
) {
1330 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
1334 if (le64toh(o
->entry
.realtime
) == needle
)
1336 else if (le64toh(o
->entry
.realtime
) < needle
)
1342 int journal_file_move_to_entry_by_realtime(
1345 direction_t direction
,
1349 return generic_array_bisect(f
,
1350 le64toh(f
->header
->entry_array_offset
),
1351 le64toh(f
->header
->n_entries
),
1353 test_object_realtime
,
1358 static int test_object_monotonic(JournalFile
*f
, uint64_t p
, uint64_t needle
) {
1365 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
1369 if (le64toh(o
->entry
.monotonic
) == needle
)
1371 else if (le64toh(o
->entry
.monotonic
) < needle
)
1377 int journal_file_move_to_entry_by_monotonic(
1381 direction_t direction
,
1385 char t
[8+32+1] = "_BOOT_ID=";
1389 sd_id128_to_string(boot_id
, t
+ 8);
1391 r
= journal_file_find_data_object(f
, t
, strlen(t
), &o
, NULL
);
1397 return generic_array_bisect_plus_one(f
,
1398 le64toh(o
->data
.entry_offset
),
1399 le64toh(o
->data
.entry_array_offset
),
1400 le64toh(o
->data
.n_entries
),
1402 test_object_monotonic
,
1407 static int test_object_offset(JournalFile
*f
, uint64_t p
, uint64_t needle
) {
1413 else if (p
< needle
)
1419 int journal_file_next_entry(
1421 Object
*o
, uint64_t p
,
1422 direction_t direction
,
1423 Object
**ret
, uint64_t *offset
) {
1429 assert(p
> 0 || !o
);
1431 n
= le64toh(f
->header
->n_entries
);
1436 i
= direction
== DIRECTION_DOWN
? 0 : n
- 1;
1438 if (o
->object
.type
!= OBJECT_ENTRY
)
1441 r
= generic_array_bisect(f
,
1442 le64toh(f
->header
->entry_array_offset
),
1443 le64toh(f
->header
->n_entries
),
1452 if (direction
== DIRECTION_DOWN
) {
1465 /* And jump to it */
1466 return generic_array_get(f
,
1467 le64toh(f
->header
->entry_array_offset
),
1472 int journal_file_skip_entry(
1474 Object
*o
, uint64_t p
,
1476 Object
**ret
, uint64_t *offset
) {
1485 if (o
->object
.type
!= OBJECT_ENTRY
)
1488 r
= generic_array_bisect(f
,
1489 le64toh(f
->header
->entry_array_offset
),
1490 le64toh(f
->header
->n_entries
),
1499 /* Calculate new index */
1501 if ((uint64_t) -skip
>= i
)
1504 i
= i
- (uint64_t) -skip
;
1506 i
+= (uint64_t) skip
;
1508 n
= le64toh(f
->header
->n_entries
);
1515 return generic_array_get(f
,
1516 le64toh(f
->header
->entry_array_offset
),
1521 int journal_file_next_entry_for_data(
1523 Object
*o
, uint64_t p
,
1524 uint64_t data_offset
,
1525 direction_t direction
,
1526 Object
**ret
, uint64_t *offset
) {
1533 assert(p
> 0 || !o
);
1535 r
= journal_file_move_to_object(f
, OBJECT_DATA
, data_offset
, &d
);
1539 n
= le64toh(d
->data
.n_entries
);
1544 i
= direction
== DIRECTION_DOWN
? 0 : n
- 1;
1546 if (o
->object
.type
!= OBJECT_ENTRY
)
1549 r
= generic_array_bisect_plus_one(f
,
1550 le64toh(d
->data
.entry_offset
),
1551 le64toh(d
->data
.entry_array_offset
),
1552 le64toh(d
->data
.n_entries
),
1562 if (direction
== DIRECTION_DOWN
) {
1576 return generic_array_get_plus_one(f
,
1577 le64toh(d
->data
.entry_offset
),
1578 le64toh(d
->data
.entry_array_offset
),
1583 int journal_file_move_to_entry_by_seqnum_for_data(
1585 uint64_t data_offset
,
1587 direction_t direction
,
1588 Object
**ret
, uint64_t *offset
) {
1593 r
= journal_file_move_to_object(f
, OBJECT_DATA
, data_offset
, &d
);
1597 return generic_array_bisect_plus_one(f
,
1598 le64toh(d
->data
.entry_offset
),
1599 le64toh(d
->data
.entry_array_offset
),
1600 le64toh(d
->data
.n_entries
),
1607 int journal_file_move_to_entry_by_realtime_for_data(
1609 uint64_t data_offset
,
1611 direction_t direction
,
1612 Object
**ret
, uint64_t *offset
) {
1617 r
= journal_file_move_to_object(f
, OBJECT_DATA
, data_offset
, &d
);
1621 return generic_array_bisect_plus_one(f
,
1622 le64toh(d
->data
.entry_offset
),
1623 le64toh(d
->data
.entry_array_offset
),
1624 le64toh(d
->data
.n_entries
),
1626 test_object_realtime
,
1631 void journal_file_dump(JournalFile
*f
) {
1632 char a
[33], b
[33], c
[33];
1639 printf("File Path: %s\n"
1643 "Arena size: %llu\n"
1647 sd_id128_to_string(f
->header
->file_id
, a
),
1648 sd_id128_to_string(f
->header
->machine_id
, b
),
1649 sd_id128_to_string(f
->header
->boot_id
, c
),
1650 (unsigned long long) le64toh(f
->header
->arena_size
),
1651 (unsigned long) le64toh(f
->header
->n_objects
),
1652 (unsigned long) le64toh(f
->header
->n_entries
));
1654 p
= le64toh(f
->header
->arena_offset
);
1656 r
= journal_file_move_to_object(f
, -1, p
, &o
);
1660 switch (o
->object
.type
) {
1663 printf("Type: OBJECT_UNUSED\n");
1667 printf("Type: OBJECT_DATA\n");
1671 printf("Type: OBJECT_ENTRY %llu %llu %llu\n",
1672 (unsigned long long) le64toh(o
->entry
.seqnum
),
1673 (unsigned long long) le64toh(o
->entry
.monotonic
),
1674 (unsigned long long) le64toh(o
->entry
.realtime
));
1677 case OBJECT_FIELD_HASH_TABLE
:
1678 printf("Type: OBJECT_FIELD_HASH_TABLE\n");
1681 case OBJECT_DATA_HASH_TABLE
:
1682 printf("Type: OBJECT_DATA_HASH_TABLE\n");
1685 case OBJECT_ENTRY_ARRAY
:
1686 printf("Type: OBJECT_ENTRY_ARRAY\n");
1690 if (o
->object
.flags
& OBJECT_COMPRESSED
)
1691 printf("Flags: COMPRESSED\n");
1693 if (p
== le64toh(f
->header
->tail_object_offset
))
1696 p
= p
+ ALIGN64(le64toh(o
->object
.size
));
1701 log_error("File corrupt");
1704 int journal_file_open(
1708 JournalFile
*template,
1709 JournalFile
**ret
) {
1713 bool newly_created
= false;
1717 if ((flags
& O_ACCMODE
) != O_RDONLY
&&
1718 (flags
& O_ACCMODE
) != O_RDWR
)
1721 f
= new0(JournalFile
, 1);
1728 f
->writable
= (flags
& O_ACCMODE
) != O_RDONLY
;
1729 f
->prot
= prot_from_flags(flags
);
1731 f
->path
= strdup(fname
);
1737 f
->fd
= open(f
->path
, f
->flags
|O_CLOEXEC
, f
->mode
);
1743 if (fstat(f
->fd
, &f
->last_stat
) < 0) {
1748 if (f
->last_stat
.st_size
== 0 && f
->writable
) {
1749 newly_created
= true;
1751 r
= journal_file_init_header(f
, template);
1755 if (fstat(f
->fd
, &f
->last_stat
) < 0) {
1761 if (f
->last_stat
.st_size
< (off_t
) sizeof(Header
)) {
1766 f
->header
= mmap(NULL
, PAGE_ALIGN(sizeof(Header
)), prot_from_flags(flags
), MAP_SHARED
, f
->fd
, 0);
1767 if (f
->header
== MAP_FAILED
) {
1773 if (!newly_created
) {
1774 r
= journal_file_verify_header(f
);
1780 r
= journal_file_refresh_header(f
);
1785 if (newly_created
) {
1787 r
= journal_file_setup_field_hash_table(f
);
1791 r
= journal_file_setup_data_hash_table(f
);
1796 r
= journal_file_map_field_hash_table(f
);
1800 r
= journal_file_map_data_hash_table(f
);
1810 journal_file_close(f
);
1815 int journal_file_rotate(JournalFile
**f
) {
1818 JournalFile
*old_file
, *new_file
= NULL
;
1826 if (!old_file
->writable
)
1829 if (!endswith(old_file
->path
, ".journal"))
1832 l
= strlen(old_file
->path
);
1834 p
= new(char, l
+ 1 + 16 + 1 + 32 + 1 + 16 + 1);
1838 memcpy(p
, old_file
->path
, l
- 8);
1840 sd_id128_to_string(old_file
->header
->seqnum_id
, p
+ l
- 8 + 1);
1841 snprintf(p
+ l
- 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
1842 "-%016llx-%016llx.journal",
1843 (unsigned long long) le64toh((*f
)->header
->seqnum
),
1844 (unsigned long long) le64toh((*f
)->header
->tail_entry_realtime
));
1846 r
= rename(old_file
->path
, p
);
1852 old_file
->header
->state
= le32toh(STATE_ARCHIVED
);
1854 r
= journal_file_open(old_file
->path
, old_file
->flags
, old_file
->mode
, old_file
, &new_file
);
1855 journal_file_close(old_file
);
1861 struct vacuum_info
{
1866 sd_id128_t seqnum_id
;
1870 static int vacuum_compare(const void *_a
, const void *_b
) {
1871 const struct vacuum_info
*a
, *b
;
1876 if (sd_id128_equal(a
->seqnum_id
, b
->seqnum_id
)) {
1877 if (a
->seqnum
< b
->seqnum
)
1879 else if (a
->seqnum
> b
->seqnum
)
1885 if (a
->realtime
< b
->realtime
)
1887 else if (a
->realtime
> b
->realtime
)
1890 return memcmp(&a
->seqnum_id
, &b
->seqnum_id
, 16);
1893 int journal_directory_vacuum(const char *directory
, uint64_t max_use
, uint64_t min_free
) {
1896 struct vacuum_info
*list
= NULL
;
1897 unsigned n_list
= 0, n_allocated
= 0, i
;
1905 d
= opendir(directory
);
1911 struct dirent buf
, *de
;
1915 unsigned long long seqnum
, realtime
;
1916 sd_id128_t seqnum_id
;
1918 k
= readdir_r(d
, &buf
, &de
);
1927 if (!dirent_is_file_with_suffix(de
, ".journal"))
1930 q
= strlen(de
->d_name
);
1932 if (q
< 1 + 32 + 1 + 16 + 1 + 16 + 8)
1935 if (de
->d_name
[q
-8-16-1] != '-' ||
1936 de
->d_name
[q
-8-16-1-16-1] != '-' ||
1937 de
->d_name
[q
-8-16-1-16-1-32-1] != '@')
1940 if (fstatat(dirfd(d
), de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0)
1943 if (!S_ISREG(st
.st_mode
))
1946 p
= strdup(de
->d_name
);
1952 de
->d_name
[q
-8-16-1-16-1] = 0;
1953 if (sd_id128_from_string(de
->d_name
+ q
-8-16-1-16-1-32, &seqnum_id
) < 0) {
1958 if (sscanf(de
->d_name
+ q
-8-16-1-16, "%16llx-%16llx.journal", &seqnum
, &realtime
) != 2) {
1963 if (n_list
>= n_allocated
) {
1964 struct vacuum_info
*j
;
1966 n_allocated
= MAX(n_allocated
* 2U, 8U);
1967 j
= realloc(list
, n_allocated
* sizeof(struct vacuum_info
));
1977 list
[n_list
].filename
= p
;
1978 list
[n_list
].usage
= (uint64_t) st
.st_blksize
* (uint64_t) st
.st_blocks
;
1979 list
[n_list
].seqnum
= seqnum
;
1980 list
[n_list
].realtime
= realtime
;
1981 list
[n_list
].seqnum_id
= seqnum_id
;
1983 sum
+= list
[n_list
].usage
;
1988 qsort(list
, n_list
, sizeof(struct vacuum_info
), vacuum_compare
);
1990 for(i
= 0; i
< n_list
; i
++) {
1993 if (fstatvfs(dirfd(d
), &ss
) < 0) {
1998 if (sum
<= max_use
&&
1999 (uint64_t) ss
.f_bavail
* (uint64_t) ss
.f_bsize
>= min_free
)
2002 if (unlinkat(dirfd(d
), list
[i
].filename
, 0) >= 0) {
2003 log_debug("Deleted archived journal %s/%s.", directory
, list
[i
].filename
);
2004 sum
-= list
[i
].usage
;
2005 } else if (errno
!= ENOENT
)
2006 log_warning("Failed to delete %s/%s: %m", directory
, list
[i
].filename
);
2010 for (i
= 0; i
< n_list
; i
++)
2011 free(list
[i
].filename
);
2021 int journal_file_copy_entry(JournalFile
*from
, JournalFile
*to
, Object
*o
, uint64_t p
, uint64_t *seqnum
, Object
**ret
, uint64_t *offset
) {
2023 uint64_t q
, xor_hash
= 0;
2036 ts
.monotonic
= le64toh(o
->entry
.monotonic
);
2037 ts
.realtime
= le64toh(o
->entry
.realtime
);
2039 if (to
->tail_entry_monotonic_valid
&&
2040 ts
.monotonic
< le64toh(to
->header
->tail_entry_monotonic
))
2043 if (ts
.realtime
< le64toh(to
->header
->tail_entry_realtime
))
2046 n
= journal_file_entry_n_items(o
);
2047 items
= alloca(sizeof(EntryItem
) * n
);
2049 for (i
= 0; i
< n
; i
++) {
2050 uint64_t le_hash
, l
, h
;
2055 q
= le64toh(o
->entry
.items
[i
].object_offset
);
2056 le_hash
= o
->entry
.items
[i
].hash
;
2058 r
= journal_file_move_to_object(from
, OBJECT_DATA
, q
, &o
);
2062 if (le_hash
!= o
->data
.hash
)
2065 l
= le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
);
2068 /* We hit the limit on 32bit machines */
2069 if ((uint64_t) t
!= l
)
2072 if (o
->object
.flags
& OBJECT_COMPRESSED
) {
2076 if (!uncompress_blob(o
->data
.payload
, l
, &from
->compress_buffer
, &from
->compress_buffer_size
, &rsize
))
2079 data
= from
->compress_buffer
;
2082 return -EPROTONOSUPPORT
;
2085 data
= o
->data
.payload
;
2087 r
= journal_file_append_data(to
, data
, l
, &u
, &h
);
2091 xor_hash
^= le64toh(u
->data
.hash
);
2092 items
[i
].object_offset
= htole64(h
);
2093 items
[i
].hash
= u
->data
.hash
;
2095 r
= journal_file_move_to_object(from
, OBJECT_ENTRY
, p
, &o
);
2100 return journal_file_append_entry_internal(to
, &ts
, xor_hash
, items
, n
, seqnum
, ret
, offset
);
2103 void journal_default_metrics(JournalMetrics
*m
, int fd
) {
2104 uint64_t fs_size
= 0;
2106 char a
[FORMAT_BYTES_MAX
], b
[FORMAT_BYTES_MAX
], c
[FORMAT_BYTES_MAX
], d
[FORMAT_BYTES_MAX
];
2111 if (fstatvfs(fd
, &ss
) >= 0)
2112 fs_size
= ss
.f_frsize
* ss
.f_blocks
;
2114 if (m
->max_use
== (uint64_t) -1) {
2117 m
->max_use
= PAGE_ALIGN(fs_size
/ 10); /* 10% of file system size */
2119 if (m
->max_use
> DEFAULT_MAX_USE_UPPER
)
2120 m
->max_use
= DEFAULT_MAX_USE_UPPER
;
2122 if (m
->max_use
< DEFAULT_MAX_USE_LOWER
)
2123 m
->max_use
= DEFAULT_MAX_USE_LOWER
;
2125 m
->max_use
= DEFAULT_MAX_USE_LOWER
;
2127 m
->max_use
= PAGE_ALIGN(m
->max_use
);
2129 if (m
->max_use
< JOURNAL_FILE_SIZE_MIN
*2)
2130 m
->max_use
= JOURNAL_FILE_SIZE_MIN
*2;
2133 if (m
->max_size
== (uint64_t) -1) {
2134 m
->max_size
= PAGE_ALIGN(m
->max_use
/ 8); /* 8 chunks */
2136 if (m
->max_size
> DEFAULT_MAX_SIZE_UPPER
)
2137 m
->max_size
= DEFAULT_MAX_SIZE_UPPER
;
2139 m
->max_size
= PAGE_ALIGN(m
->max_size
);
2141 if (m
->max_size
< JOURNAL_FILE_SIZE_MIN
)
2142 m
->max_size
= JOURNAL_FILE_SIZE_MIN
;
2144 if (m
->max_size
*2 > m
->max_use
)
2145 m
->max_use
= m
->max_size
*2;
2147 if (m
->min_size
== (uint64_t) -1)
2148 m
->min_size
= JOURNAL_FILE_SIZE_MIN
;
2150 m
->min_size
= PAGE_ALIGN(m
->min_size
);
2152 if (m
->min_size
< JOURNAL_FILE_SIZE_MIN
)
2153 m
->min_size
= JOURNAL_FILE_SIZE_MIN
;
2155 if (m
->min_size
> m
->max_size
)
2156 m
->max_size
= m
->min_size
;
2159 if (m
->keep_free
== (uint64_t) -1) {
2162 m
->keep_free
= PAGE_ALIGN(fs_size
/ 20); /* 5% of file system size */
2164 if (m
->keep_free
> DEFAULT_KEEP_FREE_UPPER
)
2165 m
->keep_free
= DEFAULT_KEEP_FREE_UPPER
;
2168 m
->keep_free
= DEFAULT_KEEP_FREE
;
2171 log_debug("Fixed max_use=%s max_size=%s min_size=%s keep_free=%s",
2172 format_bytes(a
, sizeof(a
), m
->max_use
),
2173 format_bytes(b
, sizeof(b
), m
->max_size
),
2174 format_bytes(c
, sizeof(c
), m
->min_size
),
2175 format_bytes(d
, sizeof(d
), m
->keep_free
));