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 (64ULL)
42 static const char signature
[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
44 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
46 void journal_file_close(JournalFile
*f
) {
51 if (f
->header
&& f
->writable
)
52 f
->header
->state
= STATE_OFFLINE
;
55 for (t
= 0; t
< _WINDOW_MAX
; t
++)
56 if (f
->windows
[t
].ptr
)
57 munmap(f
->windows
[t
].ptr
, f
->windows
[t
].size
);
60 close_nointr_nofail(f
->fd
);
65 free(f
->compress_buffer
);
71 static int journal_file_init_header(JournalFile
*f
, JournalFile
*template) {
79 memcpy(h
.signature
, signature
, 8);
80 h
.arena_offset
= htole64(ALIGN64(sizeof(h
)));
82 r
= sd_id128_randomize(&h
.file_id
);
87 h
.seqnum_id
= template->header
->seqnum_id
;
88 h
.seqnum
= template->header
->seqnum
;
90 h
.seqnum_id
= h
.file_id
;
92 k
= pwrite(f
->fd
, &h
, sizeof(h
), 0);
102 static int journal_file_refresh_header(JournalFile
*f
) {
108 r
= sd_id128_get_machine(&f
->header
->machine_id
);
112 r
= sd_id128_get_boot(&boot_id
);
116 if (sd_id128_equal(boot_id
, f
->header
->boot_id
))
117 f
->tail_entry_monotonic_valid
= true;
119 f
->header
->boot_id
= boot_id
;
121 f
->header
->state
= STATE_ONLINE
;
125 static int journal_file_verify_header(JournalFile
*f
) {
128 if (memcmp(f
->header
, signature
, 8))
132 if ((le64toh(f
->header
->incompatible_flags
) & ~HEADER_INCOMPATIBLE_COMPRESSED
) != 0)
133 return -EPROTONOSUPPORT
;
135 if (f
->header
->incompatible_flags
!= 0)
136 return -EPROTONOSUPPORT
;
139 if ((uint64_t) f
->last_stat
.st_size
< (le64toh(f
->header
->arena_offset
) + le64toh(f
->header
->arena_size
)))
144 sd_id128_t machine_id
;
147 r
= sd_id128_get_machine(&machine_id
);
151 if (!sd_id128_equal(machine_id
, f
->header
->machine_id
))
154 state
= f
->header
->state
;
156 if (state
== STATE_ONLINE
)
157 log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f
->path
);
158 else if (state
== STATE_ARCHIVED
)
160 else if (state
!= STATE_OFFLINE
)
161 log_debug("Journal file %s has unknown state %u. Ignoring.", f
->path
, state
);
167 static int journal_file_allocate(JournalFile
*f
, uint64_t offset
, uint64_t size
) {
168 uint64_t old_size
, new_size
;
172 /* We assume that this file is not sparse, and we know that
173 * for sure, since we always call posix_fallocate()
177 le64toh(f
->header
->arena_offset
) +
178 le64toh(f
->header
->arena_size
);
180 new_size
= PAGE_ALIGN(offset
+ size
);
181 if (new_size
< le64toh(f
->header
->arena_offset
))
182 new_size
= le64toh(f
->header
->arena_offset
);
184 if (new_size
<= old_size
)
187 if (f
->metrics
.max_size
> 0 &&
188 new_size
> f
->metrics
.max_size
)
191 if (new_size
> f
->metrics
.min_size
&&
192 f
->metrics
.keep_free
> 0) {
195 if (fstatvfs(f
->fd
, &svfs
) >= 0) {
198 available
= svfs
.f_bfree
* svfs
.f_bsize
;
200 if (available
>= f
->metrics
.keep_free
)
201 available
-= f
->metrics
.keep_free
;
205 if (new_size
- old_size
> available
)
210 /* Note that the glibc fallocate() fallback is very
211 inefficient, hence we try to minimize the allocation area
213 if (posix_fallocate(f
->fd
, old_size
, new_size
- old_size
) < 0)
216 if (fstat(f
->fd
, &f
->last_stat
) < 0)
219 f
->header
->arena_size
= new_size
- htole64(f
->header
->arena_offset
);
224 static int journal_file_map(
233 uint64_t woffset
, wsize
;
240 woffset
= offset
& ~((uint64_t) page_size() - 1ULL);
241 wsize
= size
+ (offset
- woffset
);
242 wsize
= PAGE_ALIGN(wsize
);
244 window
= mmap(NULL
, wsize
, f
->prot
, MAP_SHARED
, f
->fd
, woffset
);
245 if (window
== MAP_FAILED
)
257 *ret
= (uint8_t*) window
+ (offset
- woffset
);
262 static int journal_file_move_to(JournalFile
*f
, int wt
, uint64_t offset
, uint64_t size
, void **ret
) {
271 assert(wt
< _WINDOW_MAX
);
275 if (_likely_(w
->ptr
&&
276 w
->offset
<= offset
&&
277 w
->offset
+ w
->size
>= offset
+ size
)) {
279 *ret
= (uint8_t*) w
->ptr
+ (offset
- w
->offset
);
284 if (munmap(w
->ptr
, w
->size
) < 0)
288 w
->size
= w
->offset
= 0;
291 if (size
< DEFAULT_WINDOW_SIZE
) {
292 /* If the default window size is larger then what was
293 * asked for extend the mapping a bit in the hope to
294 * minimize needed remappings later on. We add half
295 * the window space before and half behind the
296 * requested mapping */
298 delta
= PAGE_ALIGN((DEFAULT_WINDOW_SIZE
- size
) / 2);
304 size
+= (DEFAULT_WINDOW_SIZE
- delta
);
308 r
= journal_file_map(f
,
310 &w
->ptr
, &w
->offset
, &w
->size
,
316 *ret
= (uint8_t*) p
+ delta
;
320 static bool verify_hash(Object
*o
) {
325 if (o
->object
.type
== OBJECT_DATA
&& !(o
->object
.flags
& OBJECT_COMPRESSED
)) {
326 h1
= le64toh(o
->data
.hash
);
327 h2
= hash64(o
->data
.payload
, le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
));
328 } else if (o
->object
.type
== OBJECT_FIELD
) {
329 h1
= le64toh(o
->field
.hash
);
330 h2
= hash64(o
->field
.payload
, le64toh(o
->object
.size
) - offsetof(Object
, field
.payload
));
337 int journal_file_move_to_object(JournalFile
*f
, int type
, uint64_t offset
, Object
**ret
) {
345 assert(type
< _OBJECT_TYPE_MAX
);
347 r
= journal_file_move_to(f
, type
>= 0 ? type
: WINDOW_UNKNOWN
, offset
, sizeof(ObjectHeader
), &t
);
352 s
= le64toh(o
->object
.size
);
354 if (s
< sizeof(ObjectHeader
))
357 if (type
>= 0 && o
->object
.type
!= type
)
360 if (s
> sizeof(ObjectHeader
)) {
361 r
= journal_file_move_to(f
, o
->object
.type
, offset
, s
, &t
);
375 static uint64_t journal_file_seqnum(JournalFile
*f
, uint64_t *seqnum
) {
380 r
= le64toh(f
->header
->seqnum
) + 1;
383 /* If an external seqnum counter was passed, we update
384 * both the local and the external one, and set it to
385 * the maximum of both */
393 f
->header
->seqnum
= htole64(r
);
395 if (f
->header
->first_seqnum
== 0)
396 f
->header
->first_seqnum
= htole64(r
);
401 static int journal_file_append_object(JournalFile
*f
, int type
, uint64_t size
, Object
**ret
, uint64_t *offset
) {
408 assert(size
>= sizeof(ObjectHeader
));
412 p
= le64toh(f
->header
->tail_object_offset
);
414 p
= le64toh(f
->header
->arena_offset
);
416 r
= journal_file_move_to_object(f
, -1, p
, &tail
);
420 p
+= ALIGN64(le64toh(tail
->object
.size
));
423 r
= journal_file_allocate(f
, p
, size
);
427 r
= journal_file_move_to(f
, type
, p
, size
, &t
);
434 o
->object
.type
= type
;
435 o
->object
.size
= htole64(size
);
437 f
->header
->tail_object_offset
= htole64(p
);
438 f
->header
->n_objects
= htole64(le64toh(f
->header
->n_objects
) + 1);
446 static int journal_file_setup_data_hash_table(JournalFile
*f
) {
453 s
= DEFAULT_DATA_HASH_TABLE_SIZE
;
454 r
= journal_file_append_object(f
,
455 OBJECT_DATA_HASH_TABLE
,
456 offsetof(Object
, hash_table
.items
) + s
,
461 memset(o
->hash_table
.items
, 0, s
);
463 f
->header
->data_hash_table_offset
= htole64(p
+ offsetof(Object
, hash_table
.items
));
464 f
->header
->data_hash_table_size
= htole64(s
);
469 static int journal_file_setup_field_hash_table(JournalFile
*f
) {
476 s
= DEFAULT_FIELD_HASH_TABLE_SIZE
;
477 r
= journal_file_append_object(f
,
478 OBJECT_FIELD_HASH_TABLE
,
479 offsetof(Object
, hash_table
.items
) + s
,
484 memset(o
->hash_table
.items
, 0, s
);
486 f
->header
->field_hash_table_offset
= htole64(p
+ offsetof(Object
, hash_table
.items
));
487 f
->header
->field_hash_table_size
= htole64(s
);
492 static int journal_file_map_data_hash_table(JournalFile
*f
) {
499 p
= le64toh(f
->header
->data_hash_table_offset
);
500 s
= le64toh(f
->header
->data_hash_table_size
);
502 r
= journal_file_move_to(f
,
503 WINDOW_DATA_HASH_TABLE
,
509 f
->data_hash_table
= t
;
513 static int journal_file_map_field_hash_table(JournalFile
*f
) {
520 p
= le64toh(f
->header
->field_hash_table_offset
);
521 s
= le64toh(f
->header
->field_hash_table_size
);
523 r
= journal_file_move_to(f
,
524 WINDOW_FIELD_HASH_TABLE
,
530 f
->field_hash_table
= t
;
534 static int journal_file_link_data(JournalFile
*f
, Object
*o
, uint64_t offset
, uint64_t hash
) {
541 assert(o
->object
.type
== OBJECT_DATA
);
543 o
->data
.next_hash_offset
= o
->data
.next_field_offset
= 0;
544 o
->data
.entry_offset
= o
->data
.entry_array_offset
= 0;
545 o
->data
.n_entries
= 0;
547 h
= hash
% (le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
));
548 p
= le64toh(f
->data_hash_table
[h
].head_hash_offset
);
550 /* Only entry in the hash table is easy */
551 f
->data_hash_table
[h
].head_hash_offset
= htole64(offset
);
553 /* Temporarily move back to the previous data object,
554 * to patch in pointer */
556 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
560 o
->data
.next_hash_offset
= htole64(offset
);
562 r
= journal_file_move_to_object(f
, OBJECT_DATA
, offset
, &o
);
567 f
->data_hash_table
[h
].tail_hash_offset
= htole64(offset
);
572 int journal_file_find_data_object_with_hash(
574 const void *data
, uint64_t size
, uint64_t hash
,
575 Object
**ret
, uint64_t *offset
) {
576 uint64_t p
, osize
, h
;
580 assert(data
|| size
== 0);
582 osize
= offsetof(Object
, data
.payload
) + size
;
584 if (f
->header
->data_hash_table_size
== 0)
587 h
= hash
% (le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
));
588 p
= le64toh(f
->data_hash_table
[h
].head_hash_offset
);
593 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
597 if (le64toh(o
->data
.hash
) != hash
)
600 if (o
->object
.flags
& OBJECT_COMPRESSED
) {
604 l
= le64toh(o
->object
.size
);
605 if (l
<= offsetof(Object
, data
.payload
))
608 l
-= offsetof(Object
, data
.payload
);
610 if (!uncompress_blob(o
->data
.payload
, l
, &f
->compress_buffer
, &f
->compress_buffer_size
, &rsize
))
614 memcmp(f
->compress_buffer
, data
, size
) == 0) {
625 return -EPROTONOSUPPORT
;
628 } else if (le64toh(o
->object
.size
) == osize
&&
629 memcmp(o
->data
.payload
, data
, size
) == 0) {
641 p
= le64toh(o
->data
.next_hash_offset
);
647 int journal_file_find_data_object(
649 const void *data
, uint64_t size
,
650 Object
**ret
, uint64_t *offset
) {
655 assert(data
|| size
== 0);
657 hash
= hash64(data
, size
);
659 return journal_file_find_data_object_with_hash(f
,
664 static int journal_file_append_data(JournalFile
*f
, const void *data
, uint64_t size
, Object
**ret
, uint64_t *offset
) {
669 bool compressed
= false;
672 assert(data
|| size
== 0);
674 hash
= hash64(data
, size
);
676 r
= journal_file_find_data_object_with_hash(f
, data
, size
, hash
, &o
, &p
);
690 osize
= offsetof(Object
, data
.payload
) + size
;
691 r
= journal_file_append_object(f
, OBJECT_DATA
, osize
, &o
, &p
);
695 o
->data
.hash
= htole64(hash
);
699 size
>= COMPRESSION_SIZE_THRESHOLD
) {
702 compressed
= compress_blob(data
, size
, o
->data
.payload
, &rsize
);
705 o
->object
.size
= htole64(offsetof(Object
, data
.payload
) + rsize
);
706 o
->object
.flags
|= OBJECT_COMPRESSED
;
708 f
->header
->incompatible_flags
= htole32(le32toh(f
->header
->incompatible_flags
) | HEADER_INCOMPATIBLE_COMPRESSED
);
710 log_debug("Compressed data object %lu -> %lu", (unsigned long) size
, (unsigned long) rsize
);
716 memcpy(o
->data
.payload
, data
, size
);
718 r
= journal_file_link_data(f
, o
, p
, hash
);
731 uint64_t journal_file_entry_n_items(Object
*o
) {
733 assert(o
->object
.type
== htole64(OBJECT_ENTRY
));
735 return (le64toh(o
->object
.size
) - offsetof(Object
, entry
.items
)) / sizeof(EntryItem
);
738 static uint64_t journal_file_entry_array_n_items(Object
*o
) {
740 assert(o
->object
.type
== htole64(OBJECT_ENTRY_ARRAY
));
742 return (le64toh(o
->object
.size
) - offsetof(Object
, entry_array
.items
)) / sizeof(uint64_t);
745 static int link_entry_into_array(JournalFile
*f
,
750 uint64_t n
= 0, ap
= 0, q
, i
, a
, hidx
;
759 i
= hidx
= le64toh(*idx
);
762 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
766 n
= journal_file_entry_array_n_items(o
);
768 o
->entry_array
.items
[i
] = htole64(p
);
769 *idx
= htole64(hidx
+ 1);
775 a
= le64toh(o
->entry_array
.next_entry_array_offset
);
786 r
= journal_file_append_object(f
, OBJECT_ENTRY_ARRAY
,
787 offsetof(Object
, entry_array
.items
) + n
* sizeof(uint64_t),
792 o
->entry_array
.items
[i
] = htole64(p
);
797 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, ap
, &o
);
801 o
->entry_array
.next_entry_array_offset
= htole64(q
);
804 *idx
= htole64(hidx
+ 1);
809 static int link_entry_into_array_plus_one(JournalFile
*f
,
828 i
= le64toh(*idx
) - 1;
829 r
= link_entry_into_array(f
, first
, &i
, p
);
834 *idx
= htole64(le64toh(*idx
) + 1);
838 static int journal_file_link_entry_item(JournalFile
*f
, Object
*o
, uint64_t offset
, uint64_t i
) {
845 p
= le64toh(o
->entry
.items
[i
].object_offset
);
849 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
853 return link_entry_into_array_plus_one(f
,
854 &o
->data
.entry_offset
,
855 &o
->data
.entry_array_offset
,
860 static int journal_file_link_entry(JournalFile
*f
, Object
*o
, uint64_t offset
) {
867 assert(o
->object
.type
== OBJECT_ENTRY
);
869 /* Link up the entry itself */
870 r
= link_entry_into_array(f
,
871 &f
->header
->entry_array_offset
,
872 &f
->header
->n_entries
,
877 log_error("=> %s seqnr=%lu n_entries=%lu", f
->path
, (unsigned long) o
->entry
.seqnum
, (unsigned long) f
->header
->n_entries
);
879 if (f
->header
->head_entry_realtime
== 0)
880 f
->header
->head_entry_realtime
= o
->entry
.realtime
;
882 f
->header
->tail_entry_realtime
= o
->entry
.realtime
;
883 f
->header
->tail_entry_monotonic
= o
->entry
.monotonic
;
885 f
->tail_entry_monotonic_valid
= true;
887 /* Link up the items */
888 n
= journal_file_entry_n_items(o
);
889 for (i
= 0; i
< n
; i
++) {
890 r
= journal_file_link_entry_item(f
, o
, offset
, i
);
898 static int journal_file_append_entry_internal(
900 const dual_timestamp
*ts
,
902 const EntryItem items
[], unsigned n_items
,
904 Object
**ret
, uint64_t *offset
) {
911 assert(items
|| n_items
== 0);
914 osize
= offsetof(Object
, entry
.items
) + (n_items
* sizeof(EntryItem
));
916 r
= journal_file_append_object(f
, OBJECT_ENTRY
, osize
, &o
, &np
);
920 o
->entry
.seqnum
= htole64(journal_file_seqnum(f
, seqnum
));
921 memcpy(o
->entry
.items
, items
, n_items
* sizeof(EntryItem
));
922 o
->entry
.realtime
= htole64(ts
->realtime
);
923 o
->entry
.monotonic
= htole64(ts
->monotonic
);
924 o
->entry
.xor_hash
= htole64(xor_hash
);
925 o
->entry
.boot_id
= f
->header
->boot_id
;
927 r
= journal_file_link_entry(f
, o
, np
);
940 static void journal_file_post_change(JournalFile
*f
) {
943 /* inotify() does not receive IN_MODIFY events from file
944 * accesses done via mmap(). After each access we hence
945 * trigger IN_MODIFY by truncating the journal file to its
946 * current size which triggers IN_MODIFY. */
948 __sync_synchronize();
950 if (ftruncate(f
->fd
, f
->last_stat
.st_size
) < 0)
951 log_error("Failed to to truncate file to its own size: %m");
954 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
) {
958 uint64_t xor_hash
= 0;
959 struct dual_timestamp _ts
;
962 assert(iovec
|| n_iovec
== 0);
968 dual_timestamp_get(&_ts
);
972 if (f
->tail_entry_monotonic_valid
&&
973 ts
->monotonic
< le64toh(f
->header
->tail_entry_monotonic
))
976 if (ts
->realtime
< le64toh(f
->header
->tail_entry_realtime
))
979 items
= new(EntryItem
, n_iovec
);
983 for (i
= 0; i
< n_iovec
; i
++) {
987 r
= journal_file_append_data(f
, iovec
[i
].iov_base
, iovec
[i
].iov_len
, &o
, &p
);
991 xor_hash
^= le64toh(o
->data
.hash
);
992 items
[i
].object_offset
= htole64(p
);
993 items
[i
].hash
= o
->data
.hash
;
996 r
= journal_file_append_entry_internal(f
, ts
, xor_hash
, items
, n_iovec
, seqnum
, ret
, offset
);
998 journal_file_post_change(f
);
1006 static int generic_array_get(JournalFile
*f
,
1009 Object
**ret
, uint64_t *offset
) {
1021 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
1025 n
= journal_file_entry_array_n_items(o
);
1027 p
= le64toh(o
->entry_array
.items
[i
]);
1032 a
= le64toh(o
->entry_array
.next_entry_array_offset
);
1035 if (a
<= 0 || p
<= 0)
1038 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
1051 static int generic_array_get_plus_one(JournalFile
*f
,
1055 Object
**ret
, uint64_t *offset
) {
1064 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, extra
, &o
);
1077 return generic_array_get(f
, first
, i
-1, ret
, offset
);
1086 static int generic_array_bisect(JournalFile
*f
,
1090 int (*test_object
)(JournalFile
*f
, uint64_t p
, uint64_t needle
),
1091 direction_t direction
,
1096 uint64_t a
, p
, t
= 0, i
= 0, last_p
= 0;
1097 bool subtract_one
= false;
1098 Object
*o
, *array
= NULL
;
1102 assert(test_object
);
1106 uint64_t left
, right
, k
, lp
;
1108 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &array
);
1112 k
= journal_file_entry_array_n_items(array
);
1118 lp
= p
= le64toh(array
->entry_array
.items
[i
]);
1122 r
= test_object(f
, p
, needle
);
1126 if (r
== TEST_FOUND
)
1127 r
= direction
== DIRECTION_DOWN
? TEST_RIGHT
: TEST_LEFT
;
1129 if (r
== TEST_RIGHT
) {
1133 if (left
== right
) {
1134 if (direction
== DIRECTION_UP
)
1135 subtract_one
= true;
1141 assert(left
< right
);
1143 i
= (left
+ right
) / 2;
1144 p
= le64toh(array
->entry_array
.items
[i
]);
1148 r
= test_object(f
, p
, needle
);
1152 if (r
== TEST_FOUND
)
1153 r
= direction
== DIRECTION_DOWN
? TEST_RIGHT
: TEST_LEFT
;
1155 if (r
== TEST_RIGHT
)
1169 a
= le64toh(array
->entry_array
.next_entry_array_offset
);
1175 if (subtract_one
&& t
== 0 && i
== 0)
1178 if (subtract_one
&& i
== 0)
1180 else if (subtract_one
)
1181 p
= le64toh(array
->entry_array
.items
[i
-1]);
1183 p
= le64toh(array
->entry_array
.items
[i
]);
1185 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
1196 *idx
= t
+ i
- (subtract_one
? 1 : 0);
1201 static int generic_array_bisect_plus_one(JournalFile
*f
,
1206 int (*test_object
)(JournalFile
*f
, uint64_t p
, uint64_t needle
),
1207 direction_t direction
,
1215 assert(test_object
);
1220 /* This bisects the array in object 'first', but first checks
1222 r
= test_object(f
, extra
, needle
);
1225 else if (r
== TEST_FOUND
) {
1228 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, extra
, &o
);
1242 } else if (r
== TEST_RIGHT
)
1245 r
= generic_array_bisect(f
, first
, n
-1, needle
, test_object
, direction
, ret
, offset
, idx
);
1253 static int test_object_seqnum(JournalFile
*f
, uint64_t p
, uint64_t needle
) {
1260 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
1264 if (le64toh(o
->entry
.seqnum
) == needle
)
1266 else if (le64toh(o
->entry
.seqnum
) < needle
)
1272 int journal_file_move_to_entry_by_seqnum(
1275 direction_t direction
,
1279 return generic_array_bisect(f
,
1280 le64toh(f
->header
->entry_array_offset
),
1281 le64toh(f
->header
->n_entries
),
1288 static int test_object_realtime(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
.realtime
) == needle
)
1301 else if (le64toh(o
->entry
.realtime
) < needle
)
1307 int journal_file_move_to_entry_by_realtime(
1310 direction_t direction
,
1314 return generic_array_bisect(f
,
1315 le64toh(f
->header
->entry_array_offset
),
1316 le64toh(f
->header
->n_entries
),
1318 test_object_realtime
,
1323 static int test_object_monotonic(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
.monotonic
) == needle
)
1336 else if (le64toh(o
->entry
.monotonic
) < needle
)
1342 int journal_file_move_to_entry_by_monotonic(
1346 direction_t direction
,
1350 char t
[8+32+1] = "_BOOT_ID=";
1354 sd_id128_to_string(boot_id
, t
+ 8);
1356 r
= journal_file_find_data_object(f
, t
, strlen(t
), &o
, NULL
);
1362 return generic_array_bisect_plus_one(f
,
1363 le64toh(o
->data
.entry_offset
),
1364 le64toh(o
->data
.entry_array_offset
),
1365 le64toh(o
->data
.n_entries
),
1367 test_object_monotonic
,
1372 static int test_object_offset(JournalFile
*f
, uint64_t p
, uint64_t needle
) {
1378 else if (p
< needle
)
1384 int journal_file_next_entry(
1386 Object
*o
, uint64_t p
,
1387 direction_t direction
,
1388 Object
**ret
, uint64_t *offset
) {
1394 assert(p
> 0 || !o
);
1396 n
= le64toh(f
->header
->n_entries
);
1401 i
= direction
== DIRECTION_DOWN
? 0 : n
- 1;
1403 if (o
->object
.type
!= OBJECT_ENTRY
)
1406 r
= generic_array_bisect(f
,
1407 le64toh(f
->header
->entry_array_offset
),
1408 le64toh(f
->header
->n_entries
),
1417 if (direction
== DIRECTION_DOWN
) {
1430 /* And jump to it */
1431 return generic_array_get(f
,
1432 le64toh(f
->header
->entry_array_offset
),
1437 int journal_file_skip_entry(
1439 Object
*o
, uint64_t p
,
1441 Object
**ret
, uint64_t *offset
) {
1450 if (o
->object
.type
!= OBJECT_ENTRY
)
1453 r
= generic_array_bisect(f
,
1454 le64toh(f
->header
->entry_array_offset
),
1455 le64toh(f
->header
->n_entries
),
1464 /* Calculate new index */
1466 if ((uint64_t) -skip
>= i
)
1469 i
= i
- (uint64_t) -skip
;
1471 i
+= (uint64_t) skip
;
1473 n
= le64toh(f
->header
->n_entries
);
1480 return generic_array_get(f
,
1481 le64toh(f
->header
->entry_array_offset
),
1486 int journal_file_next_entry_for_data(
1488 Object
*o
, uint64_t p
,
1489 uint64_t data_offset
,
1490 direction_t direction
,
1491 Object
**ret
, uint64_t *offset
) {
1498 assert(p
> 0 || !o
);
1500 r
= journal_file_move_to_object(f
, OBJECT_DATA
, data_offset
, &d
);
1504 n
= le64toh(d
->data
.n_entries
);
1509 i
= direction
== DIRECTION_DOWN
? 0 : n
- 1;
1511 if (o
->object
.type
!= OBJECT_ENTRY
)
1514 r
= generic_array_bisect_plus_one(f
,
1515 le64toh(d
->data
.entry_offset
),
1516 le64toh(d
->data
.entry_array_offset
),
1517 le64toh(d
->data
.n_entries
),
1527 if (direction
== DIRECTION_DOWN
) {
1541 return generic_array_get_plus_one(f
,
1542 le64toh(d
->data
.entry_offset
),
1543 le64toh(d
->data
.entry_array_offset
),
1548 int journal_file_move_to_entry_by_seqnum_for_data(
1550 uint64_t data_offset
,
1552 direction_t direction
,
1553 Object
**ret
, uint64_t *offset
) {
1558 r
= journal_file_move_to_object(f
, OBJECT_DATA
, data_offset
, &d
);
1562 return generic_array_bisect_plus_one(f
,
1563 le64toh(d
->data
.entry_offset
),
1564 le64toh(d
->data
.entry_array_offset
),
1565 le64toh(d
->data
.n_entries
),
1572 int journal_file_move_to_entry_by_realtime_for_data(
1574 uint64_t data_offset
,
1576 direction_t direction
,
1577 Object
**ret
, uint64_t *offset
) {
1582 r
= journal_file_move_to_object(f
, OBJECT_DATA
, data_offset
, &d
);
1586 return generic_array_bisect_plus_one(f
,
1587 le64toh(d
->data
.entry_offset
),
1588 le64toh(d
->data
.entry_array_offset
),
1589 le64toh(d
->data
.n_entries
),
1591 test_object_realtime
,
1596 void journal_file_dump(JournalFile
*f
) {
1597 char a
[33], b
[33], c
[33];
1604 printf("File Path: %s\n"
1608 "Arena size: %llu\n"
1612 sd_id128_to_string(f
->header
->file_id
, a
),
1613 sd_id128_to_string(f
->header
->machine_id
, b
),
1614 sd_id128_to_string(f
->header
->boot_id
, c
),
1615 (unsigned long long) le64toh(f
->header
->arena_size
),
1616 (unsigned long) le64toh(f
->header
->n_objects
),
1617 (unsigned long) le64toh(f
->header
->n_entries
));
1619 p
= le64toh(f
->header
->arena_offset
);
1621 r
= journal_file_move_to_object(f
, -1, p
, &o
);
1625 switch (o
->object
.type
) {
1628 printf("Type: OBJECT_UNUSED\n");
1632 printf("Type: OBJECT_DATA\n");
1636 printf("Type: OBJECT_ENTRY %llu %llu %llu\n",
1637 (unsigned long long) le64toh(o
->entry
.seqnum
),
1638 (unsigned long long) le64toh(o
->entry
.monotonic
),
1639 (unsigned long long) le64toh(o
->entry
.realtime
));
1642 case OBJECT_FIELD_HASH_TABLE
:
1643 printf("Type: OBJECT_FIELD_HASH_TABLE\n");
1646 case OBJECT_DATA_HASH_TABLE
:
1647 printf("Type: OBJECT_DATA_HASH_TABLE\n");
1650 case OBJECT_ENTRY_ARRAY
:
1651 printf("Type: OBJECT_ENTRY_ARRAY\n");
1655 if (o
->object
.flags
& OBJECT_COMPRESSED
)
1656 printf("Flags: COMPRESSED\n");
1658 if (p
== le64toh(f
->header
->tail_object_offset
))
1661 p
= p
+ ALIGN64(le64toh(o
->object
.size
));
1666 log_error("File corrupt");
1669 int journal_file_open(
1673 JournalFile
*template,
1674 JournalFile
**ret
) {
1678 bool newly_created
= false;
1682 if ((flags
& O_ACCMODE
) != O_RDONLY
&&
1683 (flags
& O_ACCMODE
) != O_RDWR
)
1686 f
= new0(JournalFile
, 1);
1693 f
->writable
= (flags
& O_ACCMODE
) != O_RDONLY
;
1694 f
->prot
= prot_from_flags(flags
);
1696 f
->metrics
.max_size
= DEFAULT_MAX_SIZE
;
1697 f
->metrics
.min_size
= DEFAULT_MIN_SIZE
;
1698 f
->metrics
.keep_free
= DEFAULT_KEEP_FREE
;
1700 f
->path
= strdup(fname
);
1706 f
->fd
= open(f
->path
, f
->flags
|O_CLOEXEC
, f
->mode
);
1712 if (fstat(f
->fd
, &f
->last_stat
) < 0) {
1717 if (f
->last_stat
.st_size
== 0 && f
->writable
) {
1718 newly_created
= true;
1720 r
= journal_file_init_header(f
, template);
1724 if (fstat(f
->fd
, &f
->last_stat
) < 0) {
1730 if (f
->last_stat
.st_size
< (off_t
) sizeof(Header
)) {
1735 f
->header
= mmap(NULL
, PAGE_ALIGN(sizeof(Header
)), prot_from_flags(flags
), MAP_SHARED
, f
->fd
, 0);
1736 if (f
->header
== MAP_FAILED
) {
1742 if (!newly_created
) {
1743 r
= journal_file_verify_header(f
);
1749 r
= journal_file_refresh_header(f
);
1754 if (newly_created
) {
1756 r
= journal_file_setup_field_hash_table(f
);
1760 r
= journal_file_setup_data_hash_table(f
);
1765 r
= journal_file_map_field_hash_table(f
);
1769 r
= journal_file_map_data_hash_table(f
);
1779 journal_file_close(f
);
1784 int journal_file_rotate(JournalFile
**f
) {
1787 JournalFile
*old_file
, *new_file
= NULL
;
1795 if (!old_file
->writable
)
1798 if (!endswith(old_file
->path
, ".journal"))
1801 l
= strlen(old_file
->path
);
1803 p
= new(char, l
+ 1 + 16 + 1 + 32 + 1 + 16 + 1);
1807 memcpy(p
, old_file
->path
, l
- 8);
1809 sd_id128_to_string(old_file
->header
->seqnum_id
, p
+ l
- 8 + 1);
1810 snprintf(p
+ l
- 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
1811 "-%016llx-%016llx.journal",
1812 (unsigned long long) le64toh((*f
)->header
->seqnum
),
1813 (unsigned long long) le64toh((*f
)->header
->tail_entry_realtime
));
1815 r
= rename(old_file
->path
, p
);
1821 old_file
->header
->state
= le32toh(STATE_ARCHIVED
);
1823 r
= journal_file_open(old_file
->path
, old_file
->flags
, old_file
->mode
, old_file
, &new_file
);
1824 journal_file_close(old_file
);
1830 struct vacuum_info
{
1835 sd_id128_t seqnum_id
;
1839 static int vacuum_compare(const void *_a
, const void *_b
) {
1840 const struct vacuum_info
*a
, *b
;
1845 if (sd_id128_equal(a
->seqnum_id
, b
->seqnum_id
)) {
1846 if (a
->seqnum
< b
->seqnum
)
1848 else if (a
->seqnum
> b
->seqnum
)
1854 if (a
->realtime
< b
->realtime
)
1856 else if (a
->realtime
> b
->realtime
)
1859 return memcmp(&a
->seqnum_id
, &b
->seqnum_id
, 16);
1862 int journal_directory_vacuum(const char *directory
, uint64_t max_use
, uint64_t min_free
) {
1865 struct vacuum_info
*list
= NULL
;
1866 unsigned n_list
= 0, n_allocated
= 0, i
;
1872 max_use
= DEFAULT_MAX_USE
;
1874 d
= opendir(directory
);
1880 struct dirent buf
, *de
;
1884 unsigned long long seqnum
, realtime
;
1885 sd_id128_t seqnum_id
;
1887 k
= readdir_r(d
, &buf
, &de
);
1896 if (!dirent_is_file_with_suffix(de
, ".journal"))
1899 q
= strlen(de
->d_name
);
1901 if (q
< 1 + 32 + 1 + 16 + 1 + 16 + 8)
1904 if (de
->d_name
[q
-8-16-1] != '-' ||
1905 de
->d_name
[q
-8-16-1-16-1] != '-' ||
1906 de
->d_name
[q
-8-16-1-16-1-32-1] != '@')
1909 if (fstatat(dirfd(d
), de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0)
1912 if (!S_ISREG(st
.st_mode
))
1915 p
= strdup(de
->d_name
);
1921 de
->d_name
[q
-8-16-1-16-1] = 0;
1922 if (sd_id128_from_string(de
->d_name
+ q
-8-16-1-16-1-32, &seqnum_id
) < 0) {
1927 if (sscanf(de
->d_name
+ q
-8-16-1-16, "%16llx-%16llx.journal", &seqnum
, &realtime
) != 2) {
1932 if (n_list
>= n_allocated
) {
1933 struct vacuum_info
*j
;
1935 n_allocated
= MAX(n_allocated
* 2U, 8U);
1936 j
= realloc(list
, n_allocated
* sizeof(struct vacuum_info
));
1946 list
[n_list
].filename
= p
;
1947 list
[n_list
].usage
= (uint64_t) st
.st_blksize
* (uint64_t) st
.st_blocks
;
1948 list
[n_list
].seqnum
= seqnum
;
1949 list
[n_list
].realtime
= realtime
;
1950 list
[n_list
].seqnum_id
= seqnum_id
;
1952 sum
+= list
[n_list
].usage
;
1957 qsort(list
, n_list
, sizeof(struct vacuum_info
), vacuum_compare
);
1959 for(i
= 0; i
< n_list
; i
++) {
1962 if (fstatvfs(dirfd(d
), &ss
) < 0) {
1967 if (sum
<= max_use
&&
1968 (uint64_t) ss
.f_bavail
* (uint64_t) ss
.f_bsize
>= min_free
)
1971 if (unlinkat(dirfd(d
), list
[i
].filename
, 0) >= 0) {
1972 log_debug("Deleted archived journal %s/%s.", directory
, list
[i
].filename
);
1973 sum
-= list
[i
].usage
;
1974 } else if (errno
!= ENOENT
)
1975 log_warning("Failed to delete %s/%s: %m", directory
, list
[i
].filename
);
1979 for (i
= 0; i
< n_list
; i
++)
1980 free(list
[i
].filename
);