1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <fcntl.h>
5 #include <inttypes.h>
6 #include <sys/uio.h>
7
8 #if HAVE_GCRYPT
9 # include <gcrypt.h>
10 #endif
11
12 #include "sd-event.h"
13 #include "sd-id128.h"
14
15 #include "compress.h"
16 #include "hashmap.h"
17 #include "journal-def.h"
18 #include "mmap-cache.h"
19 #include "sparse-endian.h"
20 #include "time-util.h"
21
22 typedef struct JournalMetrics {
23 /* For all these: UINT64_MAX means "pick automatically", and 0 means "no limit enforced" */
24 uint64_t max_size; /* how large journal files grow at max */
25 uint64_t min_size; /* how large journal files grow at least */
26 uint64_t max_use; /* how much disk space to use in total at max, keep_free permitting */
27 uint64_t min_use; /* how much disk space to use in total at least, even if keep_free says not to */
28 uint64_t keep_free; /* how much to keep free on disk */
29 uint64_t n_max_files; /* how many files to keep around at max */
30 } JournalMetrics;
31
32 typedef enum direction {
33 DIRECTION_UP,
34 DIRECTION_DOWN
35 } direction_t;
36
37 typedef enum LocationType {
38 /* The first and last entries, resp. */
39 LOCATION_HEAD,
40 LOCATION_TAIL,
41
42 /* We already read the entry we currently point to, and the
43 * next one to read should probably not be this one again. */
44 LOCATION_DISCRETE,
45
46 /* We should seek to the precise location specified, and
47 * return it, as we haven't read it yet. */
48 LOCATION_SEEK
49 } LocationType;
50
51 typedef enum OfflineState {
52 OFFLINE_JOINED,
53 OFFLINE_SYNCING,
54 OFFLINE_OFFLINING,
55 OFFLINE_CANCEL,
56 OFFLINE_AGAIN_FROM_SYNCING,
57 OFFLINE_AGAIN_FROM_OFFLINING,
58 OFFLINE_DONE
59 } OfflineState;
60
61 typedef struct JournalFile {
62 int fd;
63 MMapFileDescriptor *cache_fd;
64
65 mode_t mode;
66
67 int open_flags;
68 bool close_fd:1;
69 bool archive:1;
70
71 direction_t last_direction;
72 LocationType location_type;
73 uint64_t last_n_entries;
74
75 char *path;
76 struct stat last_stat;
77 usec_t last_stat_usec;
78
79 Header *header;
80 HashItem *data_hash_table;
81 HashItem *field_hash_table;
82
83 uint64_t current_offset;
84 uint64_t current_seqnum;
85 uint64_t current_realtime;
86 uint64_t current_monotonic;
87 sd_id128_t current_boot_id;
88 uint64_t current_xor_hash;
89
90 JournalMetrics metrics;
91
92 sd_event_source *post_change_timer;
93 usec_t post_change_timer_period;
94
95 OrderedHashmap *chain_cache;
96
97 pthread_t offline_thread;
98 volatile OfflineState offline_state;
99
100 unsigned last_seen_generation;
101
102 uint64_t compress_threshold_bytes;
103 #if HAVE_COMPRESSION
104 void *compress_buffer;
105 #endif
106
107 #if HAVE_GCRYPT
108 gcry_md_hd_t hmac;
109 bool hmac_running;
110
111 FSSHeader *fss_file;
112 size_t fss_file_size;
113
114 uint64_t fss_start_usec;
115 uint64_t fss_interval_usec;
116
117 void *fsprg_state;
118 size_t fsprg_state_size;
119
120 void *fsprg_seed;
121 size_t fsprg_seed_size;
122 #endif
123 } JournalFile;
124
125 typedef enum JournalFileFlags {
126 JOURNAL_COMPRESS = 1 << 0,
127 JOURNAL_SEAL = 1 << 1,
128 } JournalFileFlags;
129
130 int journal_file_open(
131 int fd,
132 const char *fname,
133 int open_flags,
134 JournalFileFlags file_flags,
135 mode_t mode,
136 uint64_t compress_threshold_bytes,
137 JournalMetrics *metrics,
138 MMapCache *mmap_cache,
139 JournalFile *template,
140 JournalFile **ret);
141
142 int journal_file_set_offline_thread_join(JournalFile *f);
143 JournalFile* journal_file_close(JournalFile *j);
144 int journal_file_fstat(JournalFile *f);
145 DEFINE_TRIVIAL_CLEANUP_FUNC(JournalFile*, journal_file_close);
146
147 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
148 #define VALID64(x) (((x) & 7ULL) == 0ULL)
149
150 /* Use six characters to cover the offsets common in smallish journal
151 * files without adding too many zeros. */
152 #define OFSfmt "%06"PRIx64
153
VALID_REALTIME(uint64_t u)154 static inline bool VALID_REALTIME(uint64_t u) {
155 /* This considers timestamps until the year 3112 valid. That should be plenty room... */
156 return u > 0 && u < (1ULL << 55);
157 }
158
VALID_MONOTONIC(uint64_t u)159 static inline bool VALID_MONOTONIC(uint64_t u) {
160 /* This considers timestamps until 1142 years of runtime valid. */
161 return u < (1ULL << 55);
162 }
163
VALID_EPOCH(uint64_t u)164 static inline bool VALID_EPOCH(uint64_t u) {
165 /* This allows changing the key for 1142 years, every usec. */
166 return u < (1ULL << 55);
167 }
168
169 #define JOURNAL_HEADER_CONTAINS(h, field) \
170 (le64toh((h)->header_size) >= offsetof(Header, field) + sizeof((h)->field))
171
172 #define JOURNAL_HEADER_SEALED(h) \
173 FLAGS_SET(le32toh((h)->compatible_flags), HEADER_COMPATIBLE_SEALED)
174
175 #define JOURNAL_HEADER_COMPRESSED_XZ(h) \
176 FLAGS_SET(le32toh((h)->incompatible_flags), HEADER_INCOMPATIBLE_COMPRESSED_XZ)
177
178 #define JOURNAL_HEADER_COMPRESSED_LZ4(h) \
179 FLAGS_SET(le32toh((h)->incompatible_flags), HEADER_INCOMPATIBLE_COMPRESSED_LZ4)
180
181 #define JOURNAL_HEADER_COMPRESSED_ZSTD(h) \
182 FLAGS_SET(le32toh((h)->incompatible_flags), HEADER_INCOMPATIBLE_COMPRESSED_ZSTD)
183
184 #define JOURNAL_HEADER_KEYED_HASH(h) \
185 FLAGS_SET(le32toh((h)->incompatible_flags), HEADER_INCOMPATIBLE_KEYED_HASH)
186
187 int journal_file_move_to_object(JournalFile *f, ObjectType type, uint64_t offset, Object **ret);
188 int journal_file_read_object_header(JournalFile *f, ObjectType type, uint64_t offset, Object *ret);
189
190 int journal_file_tail_end_by_pread(JournalFile *f, uint64_t *ret_offset);
191 int journal_file_tail_end_by_mmap(JournalFile *f, uint64_t *ret_offset);
192
193 uint64_t journal_file_entry_n_items(Object *o) _pure_;
194 uint64_t journal_file_entry_array_n_items(Object *o) _pure_;
195 uint64_t journal_file_hash_table_n_items(Object *o) _pure_;
196
197 int journal_file_append_object(JournalFile *f, ObjectType type, uint64_t size, Object **ret, uint64_t *ret_offset);
198 int journal_file_append_entry(
199 JournalFile *f,
200 const dual_timestamp *ts,
201 const sd_id128_t *boot_id,
202 const struct iovec iovec[], unsigned n_iovec,
203 uint64_t *seqno,
204 Object **ret,
205 uint64_t *ret_offset);
206
207 int journal_file_find_data_object(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *ret_offset);
208 int journal_file_find_data_object_with_hash(JournalFile *f, const void *data, uint64_t size, uint64_t hash, Object **ret, uint64_t *ret_offset);
209
210 int journal_file_find_field_object(JournalFile *f, const void *field, uint64_t size, Object **ret, uint64_t *ret_offset);
211 int journal_file_find_field_object_with_hash(JournalFile *f, const void *field, uint64_t size, uint64_t hash, Object **ret, uint64_t *ret_offset);
212
213 void journal_file_reset_location(JournalFile *f);
214 void journal_file_save_location(JournalFile *f, Object *o, uint64_t offset);
215 int journal_file_compare_locations(JournalFile *af, JournalFile *bf);
216 int journal_file_next_entry(JournalFile *f, uint64_t p, direction_t direction, Object **ret, uint64_t *ret_offset);
217
218 int journal_file_next_entry_for_data(JournalFile *f, Object *d, direction_t direction, Object **ret, uint64_t *ret_offset);
219
220 int journal_file_move_to_entry_by_offset(JournalFile *f, uint64_t p, direction_t direction, Object **ret, uint64_t *ret_offset);
221 int journal_file_move_to_entry_by_seqnum(JournalFile *f, uint64_t seqnum, direction_t direction, Object **ret, uint64_t *ret_offset);
222 int journal_file_move_to_entry_by_realtime(JournalFile *f, uint64_t realtime, direction_t direction, Object **ret, uint64_t *ret_offset);
223 int journal_file_move_to_entry_by_monotonic(JournalFile *f, sd_id128_t boot_id, uint64_t monotonic, direction_t direction, Object **ret, uint64_t *ret_offset);
224
225 int journal_file_move_to_entry_by_offset_for_data(JournalFile *f, Object *d, uint64_t p, direction_t direction, Object **ret, uint64_t *ret_offset);
226 int journal_file_move_to_entry_by_seqnum_for_data(JournalFile *f, Object *d, uint64_t seqnum, direction_t direction, Object **ret, uint64_t *ret_offset);
227 int journal_file_move_to_entry_by_realtime_for_data(JournalFile *f, Object *d, uint64_t realtime, direction_t direction, Object **ret, uint64_t *ret_offset);
228 int journal_file_move_to_entry_by_monotonic_for_data(JournalFile *f, Object *d, sd_id128_t boot_id, uint64_t monotonic, direction_t direction, Object **ret, uint64_t *ret_offset);
229
230 int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint64_t p);
231
232 void journal_file_dump(JournalFile *f);
233 void journal_file_print_header(JournalFile *f);
234
235 int journal_file_archive(JournalFile *f, char **ret_previous_path);
236 JournalFile* journal_initiate_close(JournalFile *f, Set *deferred_closes);
237
238 int journal_file_dispose(int dir_fd, const char *fname);
239
240 void journal_file_post_change(JournalFile *f);
241 int journal_file_enable_post_change_timer(JournalFile *f, sd_event *e, usec_t t);
242
243 void journal_reset_metrics(JournalMetrics *m);
244
245 int journal_file_get_cutoff_realtime_usec(JournalFile *f, usec_t *from, usec_t *to);
246 int journal_file_get_cutoff_monotonic_usec(JournalFile *f, sd_id128_t boot, usec_t *from, usec_t *to);
247
248 bool journal_file_rotate_suggested(JournalFile *f, usec_t max_file_usec, int log_level);
249
250 int journal_file_map_data_hash_table(JournalFile *f);
251 int journal_file_map_field_hash_table(JournalFile *f);
252
JOURNAL_FILE_COMPRESS(JournalFile * f)253 static inline bool JOURNAL_FILE_COMPRESS(JournalFile *f) {
254 assert(f);
255 return JOURNAL_HEADER_COMPRESSED_XZ(f->header) || JOURNAL_HEADER_COMPRESSED_LZ4(f->header) ||
256 JOURNAL_HEADER_COMPRESSED_ZSTD(f->header);
257 }
258
259 uint64_t journal_file_hash_data(JournalFile *f, const void *data, size_t sz);
260
261 bool journal_field_valid(const char *p, size_t l, bool allow_protected);
262
263 const char* journal_object_type_to_string(ObjectType type) _const_;
264
COMPRESSION_FROM_OBJECT(const Object * o)265 static inline Compression COMPRESSION_FROM_OBJECT(const Object *o) {
266 assert(o);
267
268 switch (o->object.flags & _OBJECT_COMPRESSED_MASK) {
269 case 0:
270 return COMPRESSION_NONE;
271 case OBJECT_COMPRESSED_XZ:
272 return COMPRESSION_XZ;
273 case OBJECT_COMPRESSED_LZ4:
274 return COMPRESSION_LZ4;
275 case OBJECT_COMPRESSED_ZSTD:
276 return COMPRESSION_ZSTD;
277 default:
278 return _COMPRESSION_INVALID;
279 }
280 }
281
COMPRESSION_TO_OBJECT_FLAG(Compression c)282 static inline uint8_t COMPRESSION_TO_OBJECT_FLAG(Compression c) {
283 switch (c) {
284 case COMPRESSION_XZ:
285 return OBJECT_COMPRESSED_XZ;
286 case COMPRESSION_LZ4:
287 return OBJECT_COMPRESSED_LZ4;
288 case COMPRESSION_ZSTD:
289 return OBJECT_COMPRESSED_ZSTD;
290 default:
291 return 0;
292 }
293 }
294
COMPRESSION_TO_HEADER_INCOMPATIBLE_FLAG(Compression c)295 static inline uint32_t COMPRESSION_TO_HEADER_INCOMPATIBLE_FLAG(Compression c) {
296 switch (c) {
297 case COMPRESSION_XZ:
298 return HEADER_INCOMPATIBLE_COMPRESSED_XZ;
299 case COMPRESSION_LZ4:
300 return HEADER_INCOMPATIBLE_COMPRESSED_LZ4;
301 case COMPRESSION_ZSTD:
302 return HEADER_INCOMPATIBLE_COMPRESSED_ZSTD;
303 default:
304 return 0;
305 }
306 }
307
journal_file_writable(JournalFile * f)308 static inline bool journal_file_writable(JournalFile *f) {
309 assert(f);
310 return (f->open_flags & O_ACCMODE) != O_RDONLY;
311 }
312