]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
cec736d2 | 2 | |
cec736d2 | 3 | #include <errno.h> |
cec736d2 | 4 | #include <fcntl.h> |
11689d2a | 5 | #include <linux/fs.h> |
ac2e41f5 | 6 | #include <pthread.h> |
07630cea LP |
7 | #include <stddef.h> |
8 | #include <sys/mman.h> | |
9 | #include <sys/statvfs.h> | |
10 | #include <sys/uio.h> | |
11 | #include <unistd.h> | |
fb0951b0 | 12 | |
a03d4359 ZJS |
13 | #include "sd-event.h" |
14 | ||
b5efdb8a | 15 | #include "alloc-util.h" |
f27a3864 | 16 | #include "btrfs-util.h" |
c8b3094d | 17 | #include "chattr-util.h" |
07630cea | 18 | #include "compress.h" |
3ffd4af2 | 19 | #include "fd-util.h" |
11b29a96 | 20 | #include "fs-util.h" |
0284adc6 | 21 | #include "journal-authenticate.h" |
cec736d2 LP |
22 | #include "journal-def.h" |
23 | #include "journal-file.h" | |
24 | #include "lookup3.h" | |
0a970718 | 25 | #include "memory-util.h" |
6bedfcbb | 26 | #include "parse-util.h" |
5d1ce257 | 27 | #include "path-util.h" |
3df3e884 | 28 | #include "random-util.h" |
b58c888f | 29 | #include "set.h" |
3cc44114 | 30 | #include "stat-util.h" |
07630cea | 31 | #include "string-util.h" |
4761fd0f | 32 | #include "strv.h" |
89a5a90c | 33 | #include "xattr-util.h" |
cec736d2 | 34 | |
4a92baf3 LP |
35 | #define DEFAULT_DATA_HASH_TABLE_SIZE (2047ULL*sizeof(HashItem)) |
36 | #define DEFAULT_FIELD_HASH_TABLE_SIZE (333ULL*sizeof(HashItem)) | |
cec736d2 | 37 | |
57850536 AG |
38 | #define DEFAULT_COMPRESS_THRESHOLD (512ULL) |
39 | #define MIN_COMPRESS_THRESHOLD (8ULL) | |
807e17f0 | 40 | |
babfc091 | 41 | /* This is the minimum journal file size */ |
16098e93 | 42 | #define JOURNAL_FILE_SIZE_MIN (512ULL*1024ULL) /* 512 KiB */ |
babfc091 LP |
43 | |
44 | /* These are the lower and upper bounds if we deduce the max_use value | |
45 | * from the file system size */ | |
46 | #define DEFAULT_MAX_USE_LOWER (1ULL*1024ULL*1024ULL) /* 1 MiB */ | |
47 | #define DEFAULT_MAX_USE_UPPER (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */ | |
48 | ||
8580d1f7 LP |
49 | /* This is the default minimal use limit, how much we'll use even if keep_free suggests otherwise. */ |
50 | #define DEFAULT_MIN_USE (1ULL*1024ULL*1024ULL) /* 1 MiB */ | |
51 | ||
babfc091 | 52 | /* This is the upper bound if we deduce max_size from max_use */ |
71100051 | 53 | #define DEFAULT_MAX_SIZE_UPPER (128ULL*1024ULL*1024ULL) /* 128 MiB */ |
babfc091 LP |
54 | |
55 | /* This is the upper bound if we deduce the keep_free value from the | |
56 | * file system size */ | |
57 | #define DEFAULT_KEEP_FREE_UPPER (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */ | |
58 | ||
59 | /* This is the keep_free value when we can't determine the system | |
60 | * size */ | |
61 | #define DEFAULT_KEEP_FREE (1024ULL*1024ULL) /* 1 MB */ | |
62 | ||
8580d1f7 LP |
63 | /* This is the default maximum number of journal files to keep around. */ |
64 | #define DEFAULT_N_MAX_FILES (100) | |
65 | ||
dca6219e LP |
66 | /* n_data was the first entry we added after the initial file format design */ |
67 | #define HEADER_SIZE_MIN ALIGN64(offsetof(Header, n_data)) | |
cec736d2 | 68 | |
a4bcff5b LP |
69 | /* How many entries to keep in the entry array chain cache at max */ |
70 | #define CHAIN_CACHE_MAX 20 | |
71 | ||
a676e665 LP |
72 | /* How much to increase the journal file size at once each time we allocate something new. */ |
73 | #define FILE_SIZE_INCREASE (8ULL*1024ULL*1024ULL) /* 8MB */ | |
74 | ||
2678031a LP |
75 | /* Reread fstat() of the file for detecting deletions at least this often */ |
76 | #define LAST_STAT_REFRESH_USEC (5*USEC_PER_SEC) | |
77 | ||
fa6ac760 LP |
78 | /* The mmap context to use for the header we pick as one above the last defined typed */ |
79 | #define CONTEXT_HEADER _OBJECT_TYPE_MAX | |
80 | ||
51804460 ZJS |
81 | #ifdef __clang__ |
82 | # pragma GCC diagnostic ignored "-Waddress-of-packed-member" | |
83 | #endif | |
84 | ||
ac2e41f5 VC |
85 | /* This may be called from a separate thread to prevent blocking the caller for the duration of fsync(). |
86 | * As a result we use atomic operations on f->offline_state for inter-thread communications with | |
87 | * journal_file_set_offline() and journal_file_set_online(). */ | |
88 | static void journal_file_set_offline_internal(JournalFile *f) { | |
26687bf8 | 89 | assert(f); |
ac2e41f5 VC |
90 | assert(f->fd >= 0); |
91 | assert(f->header); | |
92 | ||
93 | for (;;) { | |
94 | switch (f->offline_state) { | |
95 | case OFFLINE_CANCEL: | |
96 | if (!__sync_bool_compare_and_swap(&f->offline_state, OFFLINE_CANCEL, OFFLINE_DONE)) | |
97 | continue; | |
98 | return; | |
99 | ||
100 | case OFFLINE_AGAIN_FROM_SYNCING: | |
101 | if (!__sync_bool_compare_and_swap(&f->offline_state, OFFLINE_AGAIN_FROM_SYNCING, OFFLINE_SYNCING)) | |
102 | continue; | |
103 | break; | |
104 | ||
105 | case OFFLINE_AGAIN_FROM_OFFLINING: | |
106 | if (!__sync_bool_compare_and_swap(&f->offline_state, OFFLINE_AGAIN_FROM_OFFLINING, OFFLINE_SYNCING)) | |
107 | continue; | |
108 | break; | |
109 | ||
110 | case OFFLINE_SYNCING: | |
111 | (void) fsync(f->fd); | |
26687bf8 | 112 | |
ac2e41f5 VC |
113 | if (!__sync_bool_compare_and_swap(&f->offline_state, OFFLINE_SYNCING, OFFLINE_OFFLINING)) |
114 | continue; | |
26687bf8 | 115 | |
8eb85171 | 116 | f->header->state = f->archive ? STATE_ARCHIVED : STATE_OFFLINE; |
ac2e41f5 VC |
117 | (void) fsync(f->fd); |
118 | break; | |
119 | ||
120 | case OFFLINE_OFFLINING: | |
121 | if (!__sync_bool_compare_and_swap(&f->offline_state, OFFLINE_OFFLINING, OFFLINE_DONE)) | |
122 | continue; | |
4831981d | 123 | _fallthrough_; |
ac2e41f5 VC |
124 | case OFFLINE_DONE: |
125 | return; | |
126 | ||
127 | case OFFLINE_JOINED: | |
128 | log_debug("OFFLINE_JOINED unexpected offline state for journal_file_set_offline_internal()"); | |
129 | return; | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | static void * journal_file_set_offline_thread(void *arg) { | |
135 | JournalFile *f = arg; | |
136 | ||
fa7ff4cf LP |
137 | (void) pthread_setname_np(pthread_self(), "journal-offline"); |
138 | ||
ac2e41f5 VC |
139 | journal_file_set_offline_internal(f); |
140 | ||
141 | return NULL; | |
142 | } | |
143 | ||
144 | static int journal_file_set_offline_thread_join(JournalFile *f) { | |
145 | int r; | |
146 | ||
147 | assert(f); | |
148 | ||
149 | if (f->offline_state == OFFLINE_JOINED) | |
150 | return 0; | |
151 | ||
152 | r = pthread_join(f->offline_thread, NULL); | |
153 | if (r) | |
154 | return -r; | |
155 | ||
156 | f->offline_state = OFFLINE_JOINED; | |
26687bf8 | 157 | |
be7cdd8e | 158 | if (mmap_cache_got_sigbus(f->mmap, f->cache_fd)) |
fa6ac760 LP |
159 | return -EIO; |
160 | ||
ac2e41f5 VC |
161 | return 0; |
162 | } | |
26687bf8 | 163 | |
ac2e41f5 VC |
164 | /* Trigger a restart if the offline thread is mid-flight in a restartable state. */ |
165 | static bool journal_file_set_offline_try_restart(JournalFile *f) { | |
166 | for (;;) { | |
167 | switch (f->offline_state) { | |
168 | case OFFLINE_AGAIN_FROM_SYNCING: | |
169 | case OFFLINE_AGAIN_FROM_OFFLINING: | |
170 | return true; | |
171 | ||
172 | case OFFLINE_CANCEL: | |
173 | if (!__sync_bool_compare_and_swap(&f->offline_state, OFFLINE_CANCEL, OFFLINE_AGAIN_FROM_SYNCING)) | |
174 | continue; | |
175 | return true; | |
176 | ||
177 | case OFFLINE_SYNCING: | |
178 | if (!__sync_bool_compare_and_swap(&f->offline_state, OFFLINE_SYNCING, OFFLINE_AGAIN_FROM_SYNCING)) | |
179 | continue; | |
180 | return true; | |
181 | ||
182 | case OFFLINE_OFFLINING: | |
183 | if (!__sync_bool_compare_and_swap(&f->offline_state, OFFLINE_OFFLINING, OFFLINE_AGAIN_FROM_OFFLINING)) | |
184 | continue; | |
185 | return true; | |
26687bf8 OS |
186 | |
187 | default: | |
ac2e41f5 VC |
188 | return false; |
189 | } | |
26687bf8 OS |
190 | } |
191 | } | |
192 | ||
ac2e41f5 VC |
193 | /* Sets a journal offline. |
194 | * | |
195 | * If wait is false then an offline is dispatched in a separate thread for a | |
196 | * subsequent journal_file_set_offline() or journal_file_set_online() of the | |
197 | * same journal to synchronize with. | |
198 | * | |
199 | * If wait is true, then either an existing offline thread will be restarted | |
200 | * and joined, or if none exists the offline is simply performed in this | |
201 | * context without involving another thread. | |
202 | */ | |
203 | int journal_file_set_offline(JournalFile *f, bool wait) { | |
204 | bool restarted; | |
205 | int r; | |
206 | ||
26687bf8 OS |
207 | assert(f); |
208 | ||
209 | if (!f->writable) | |
210 | return -EPERM; | |
211 | ||
846e5418 | 212 | if (f->fd < 0 || !f->header) |
26687bf8 OS |
213 | return -EINVAL; |
214 | ||
b8f99e27 VC |
215 | /* An offlining journal is implicitly online and may modify f->header->state, |
216 | * we must also join any potentially lingering offline thread when not online. */ | |
217 | if (!journal_file_is_offlining(f) && f->header->state != STATE_ONLINE) | |
218 | return journal_file_set_offline_thread_join(f); | |
26687bf8 | 219 | |
ac2e41f5 VC |
220 | /* Restart an in-flight offline thread and wait if needed, or join a lingering done one. */ |
221 | restarted = journal_file_set_offline_try_restart(f); | |
222 | if ((restarted && wait) || !restarted) { | |
223 | r = journal_file_set_offline_thread_join(f); | |
224 | if (r < 0) | |
225 | return r; | |
226 | } | |
26687bf8 | 227 | |
ac2e41f5 VC |
228 | if (restarted) |
229 | return 0; | |
230 | ||
231 | /* Initiate a new offline. */ | |
232 | f->offline_state = OFFLINE_SYNCING; | |
fa6ac760 | 233 | |
ac2e41f5 VC |
234 | if (wait) /* Without using a thread if waiting. */ |
235 | journal_file_set_offline_internal(f); | |
236 | else { | |
5e9f01e8 LP |
237 | sigset_t ss, saved_ss; |
238 | int k; | |
239 | ||
cd2a429e | 240 | assert_se(sigfillset(&ss) >= 0); |
5e9f01e8 LP |
241 | |
242 | r = pthread_sigmask(SIG_BLOCK, &ss, &saved_ss); | |
243 | if (r > 0) | |
244 | return -r; | |
245 | ||
ac2e41f5 | 246 | r = pthread_create(&f->offline_thread, NULL, journal_file_set_offline_thread, f); |
5e9f01e8 LP |
247 | |
248 | k = pthread_sigmask(SIG_SETMASK, &saved_ss, NULL); | |
ec9ffa2c VC |
249 | if (r > 0) { |
250 | f->offline_state = OFFLINE_JOINED; | |
ac2e41f5 | 251 | return -r; |
ec9ffa2c | 252 | } |
5e9f01e8 LP |
253 | if (k > 0) |
254 | return -k; | |
ac2e41f5 VC |
255 | } |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
260 | static int journal_file_set_online(JournalFile *f) { | |
83bf6b67 | 261 | bool wait = true; |
ac2e41f5 VC |
262 | |
263 | assert(f); | |
264 | ||
265 | if (!f->writable) | |
266 | return -EPERM; | |
267 | ||
846e5418 | 268 | if (f->fd < 0 || !f->header) |
ac2e41f5 VC |
269 | return -EINVAL; |
270 | ||
83bf6b67 | 271 | while (wait) { |
ac2e41f5 VC |
272 | switch (f->offline_state) { |
273 | case OFFLINE_JOINED: | |
274 | /* No offline thread, no need to wait. */ | |
83bf6b67 | 275 | wait = false; |
ac2e41f5 VC |
276 | break; |
277 | ||
278 | case OFFLINE_SYNCING: | |
279 | if (!__sync_bool_compare_and_swap(&f->offline_state, OFFLINE_SYNCING, OFFLINE_CANCEL)) | |
280 | continue; | |
281 | /* Canceled syncing prior to offlining, no need to wait. */ | |
83bf6b67 | 282 | wait = false; |
ac2e41f5 VC |
283 | break; |
284 | ||
285 | case OFFLINE_AGAIN_FROM_SYNCING: | |
286 | if (!__sync_bool_compare_and_swap(&f->offline_state, OFFLINE_AGAIN_FROM_SYNCING, OFFLINE_CANCEL)) | |
287 | continue; | |
288 | /* Canceled restart from syncing, no need to wait. */ | |
83bf6b67 | 289 | wait = false; |
ac2e41f5 VC |
290 | break; |
291 | ||
292 | case OFFLINE_AGAIN_FROM_OFFLINING: | |
293 | if (!__sync_bool_compare_and_swap(&f->offline_state, OFFLINE_AGAIN_FROM_OFFLINING, OFFLINE_CANCEL)) | |
294 | continue; | |
295 | /* Canceled restart from offlining, must wait for offlining to complete however. */ | |
4831981d | 296 | _fallthrough_; |
ac2e41f5 VC |
297 | default: { |
298 | int r; | |
299 | ||
300 | r = journal_file_set_offline_thread_join(f); | |
301 | if (r < 0) | |
302 | return r; | |
303 | ||
83bf6b67 | 304 | wait = false; |
ac2e41f5 VC |
305 | break; |
306 | } | |
307 | } | |
308 | } | |
26687bf8 | 309 | |
be7cdd8e | 310 | if (mmap_cache_got_sigbus(f->mmap, f->cache_fd)) |
fa6ac760 LP |
311 | return -EIO; |
312 | ||
ac2e41f5 VC |
313 | switch (f->header->state) { |
314 | case STATE_ONLINE: | |
315 | return 0; | |
26687bf8 | 316 | |
ac2e41f5 VC |
317 | case STATE_OFFLINE: |
318 | f->header->state = STATE_ONLINE; | |
319 | (void) fsync(f->fd); | |
320 | return 0; | |
321 | ||
322 | default: | |
323 | return -EINVAL; | |
324 | } | |
26687bf8 OS |
325 | } |
326 | ||
b58c888f VC |
327 | bool journal_file_is_offlining(JournalFile *f) { |
328 | assert(f); | |
329 | ||
330 | __sync_synchronize(); | |
331 | ||
3742095b | 332 | if (IN_SET(f->offline_state, OFFLINE_DONE, OFFLINE_JOINED)) |
b58c888f VC |
333 | return false; |
334 | ||
335 | return true; | |
336 | } | |
337 | ||
804ae586 | 338 | JournalFile* journal_file_close(JournalFile *f) { |
de190aef | 339 | assert(f); |
cec736d2 | 340 | |
349cc4a5 | 341 | #if HAVE_GCRYPT |
b0af6f41 | 342 | /* Write the final tag */ |
43cd8794 FB |
343 | if (f->seal && f->writable) { |
344 | int r; | |
345 | ||
346 | r = journal_file_append_tag(f); | |
347 | if (r < 0) | |
348 | log_error_errno(r, "Failed to append tag when closing journal: %m"); | |
349 | } | |
feb12d3e | 350 | #endif |
b0af6f41 | 351 | |
7a24f3bf | 352 | if (f->post_change_timer) { |
b6cdfbe5 ZJS |
353 | if (sd_event_source_get_enabled(f->post_change_timer, NULL) > 0) |
354 | journal_file_post_change(f); | |
7a24f3bf | 355 | |
e167d7fd | 356 | (void) sd_event_source_set_enabled(f->post_change_timer, SD_EVENT_OFF); |
7a24f3bf VC |
357 | sd_event_source_unref(f->post_change_timer); |
358 | } | |
359 | ||
ac2e41f5 | 360 | journal_file_set_offline(f, true); |
cec736d2 | 361 | |
be7cdd8e VC |
362 | if (f->mmap && f->cache_fd) |
363 | mmap_cache_free_fd(f->mmap, f->cache_fd); | |
cec736d2 | 364 | |
11689d2a LP |
365 | if (f->fd >= 0 && f->defrag_on_close) { |
366 | ||
367 | /* Be friendly to btrfs: turn COW back on again now, | |
368 | * and defragment the file. We won't write to the file | |
369 | * ever again, hence remove all fragmentation, and | |
370 | * reenable all the good bits COW usually provides | |
371 | * (such as data checksumming). */ | |
372 | ||
db9a4254 | 373 | (void) chattr_fd(f->fd, 0, FS_NOCOW_FL, NULL); |
11689d2a LP |
374 | (void) btrfs_defrag_fd(f->fd); |
375 | } | |
f27a3864 | 376 | |
5d1ce257 LP |
377 | if (f->close_fd) |
378 | safe_close(f->fd); | |
cec736d2 | 379 | free(f->path); |
807e17f0 | 380 | |
f649045c | 381 | mmap_cache_unref(f->mmap); |
16e9f408 | 382 | |
4743015d | 383 | ordered_hashmap_free_free(f->chain_cache); |
a4bcff5b | 384 | |
349cc4a5 | 385 | #if HAVE_XZ || HAVE_LZ4 |
807e17f0 LP |
386 | free(f->compress_buffer); |
387 | #endif | |
388 | ||
349cc4a5 | 389 | #if HAVE_GCRYPT |
baed47c3 LP |
390 | if (f->fss_file) |
391 | munmap(f->fss_file, PAGE_ALIGN(f->fss_file_size)); | |
dc4ebc07 | 392 | else |
b7c9ae91 LP |
393 | free(f->fsprg_state); |
394 | ||
395 | free(f->fsprg_seed); | |
7560fffc LP |
396 | |
397 | if (f->hmac) | |
398 | gcry_md_close(f->hmac); | |
399 | #endif | |
400 | ||
6b430fdb | 401 | return mfree(f); |
cec736d2 LP |
402 | } |
403 | ||
0ac38b70 | 404 | static int journal_file_init_header(JournalFile *f, JournalFile *template) { |
d89c8fdf | 405 | Header h = {}; |
cec736d2 LP |
406 | ssize_t k; |
407 | int r; | |
408 | ||
409 | assert(f); | |
410 | ||
7560fffc | 411 | memcpy(h.signature, HEADER_SIGNATURE, 8); |
23b0b2b2 | 412 | h.header_size = htole64(ALIGN64(sizeof(h))); |
cec736d2 | 413 | |
d89c8fdf ZJS |
414 | h.incompatible_flags |= htole32( |
415 | f->compress_xz * HEADER_INCOMPATIBLE_COMPRESSED_XZ | | |
416 | f->compress_lz4 * HEADER_INCOMPATIBLE_COMPRESSED_LZ4); | |
7560fffc | 417 | |
d89c8fdf ZJS |
418 | h.compatible_flags = htole32( |
419 | f->seal * HEADER_COMPATIBLE_SEALED); | |
7560fffc | 420 | |
cec736d2 LP |
421 | r = sd_id128_randomize(&h.file_id); |
422 | if (r < 0) | |
423 | return r; | |
424 | ||
0ac38b70 LP |
425 | if (template) { |
426 | h.seqnum_id = template->header->seqnum_id; | |
beec0085 | 427 | h.tail_entry_seqnum = template->header->tail_entry_seqnum; |
0ac38b70 LP |
428 | } else |
429 | h.seqnum_id = h.file_id; | |
cec736d2 LP |
430 | |
431 | k = pwrite(f->fd, &h, sizeof(h), 0); | |
432 | if (k < 0) | |
433 | return -errno; | |
434 | ||
435 | if (k != sizeof(h)) | |
436 | return -EIO; | |
437 | ||
438 | return 0; | |
439 | } | |
440 | ||
441 | static int journal_file_refresh_header(JournalFile *f) { | |
de190aef | 442 | sd_id128_t boot_id; |
fa6ac760 | 443 | int r; |
cec736d2 LP |
444 | |
445 | assert(f); | |
c88cc6af | 446 | assert(f->header); |
cec736d2 LP |
447 | |
448 | r = sd_id128_get_machine(&f->header->machine_id); | |
fd4885df ZJS |
449 | if (IN_SET(r, -ENOENT, -ENOMEDIUM)) |
450 | /* We don't have a machine-id, let's continue without */ | |
451 | zero(f->header->machine_id); | |
452 | else if (r < 0) | |
cec736d2 LP |
453 | return r; |
454 | ||
de190aef | 455 | r = sd_id128_get_boot(&boot_id); |
cec736d2 LP |
456 | if (r < 0) |
457 | return r; | |
458 | ||
de190aef LP |
459 | f->header->boot_id = boot_id; |
460 | ||
fa6ac760 | 461 | r = journal_file_set_online(f); |
b788cc23 | 462 | |
7560fffc | 463 | /* Sync the online state to disk */ |
fb426037 | 464 | (void) fsync(f->fd); |
b788cc23 | 465 | |
a0fe2a2d LP |
466 | /* We likely just created a new file, also sync the directory this file is located in. */ |
467 | (void) fsync_directory_of_file(f->fd); | |
468 | ||
fa6ac760 | 469 | return r; |
cec736d2 LP |
470 | } |
471 | ||
4214009f ZJS |
472 | static bool warn_wrong_flags(const JournalFile *f, bool compatible) { |
473 | const uint32_t any = compatible ? HEADER_COMPATIBLE_ANY : HEADER_INCOMPATIBLE_ANY, | |
474 | supported = compatible ? HEADER_COMPATIBLE_SUPPORTED : HEADER_INCOMPATIBLE_SUPPORTED; | |
475 | const char *type = compatible ? "compatible" : "incompatible"; | |
d89c8fdf ZJS |
476 | uint32_t flags; |
477 | ||
4214009f ZJS |
478 | flags = le32toh(compatible ? f->header->compatible_flags : f->header->incompatible_flags); |
479 | ||
480 | if (flags & ~supported) { | |
481 | if (flags & ~any) | |
4761fd0f | 482 | log_debug("Journal file %s has unknown %s flags 0x%"PRIx32, |
4214009f ZJS |
483 | f->path, type, flags & ~any); |
484 | flags = (flags & any) & ~supported; | |
4761fd0f ZJS |
485 | if (flags) { |
486 | const char* strv[3]; | |
487 | unsigned n = 0; | |
488 | _cleanup_free_ char *t = NULL; | |
489 | ||
490 | if (compatible && (flags & HEADER_COMPATIBLE_SEALED)) | |
491 | strv[n++] = "sealed"; | |
492 | if (!compatible && (flags & HEADER_INCOMPATIBLE_COMPRESSED_XZ)) | |
493 | strv[n++] = "xz-compressed"; | |
494 | if (!compatible && (flags & HEADER_INCOMPATIBLE_COMPRESSED_LZ4)) | |
495 | strv[n++] = "lz4-compressed"; | |
496 | strv[n] = NULL; | |
497 | assert(n < ELEMENTSOF(strv)); | |
498 | ||
499 | t = strv_join((char**) strv, ", "); | |
500 | log_debug("Journal file %s uses %s %s %s disabled at compilation time.", | |
501 | f->path, type, n > 1 ? "flags" : "flag", strnull(t)); | |
502 | } | |
4214009f ZJS |
503 | return true; |
504 | } | |
505 | ||
506 | return false; | |
507 | } | |
508 | ||
509 | static int journal_file_verify_header(JournalFile *f) { | |
6f94e420 TS |
510 | uint64_t arena_size, header_size; |
511 | ||
cec736d2 | 512 | assert(f); |
c88cc6af | 513 | assert(f->header); |
cec736d2 | 514 | |
7560fffc | 515 | if (memcmp(f->header->signature, HEADER_SIGNATURE, 8)) |
cec736d2 LP |
516 | return -EBADMSG; |
517 | ||
4214009f ZJS |
518 | /* In both read and write mode we refuse to open files with incompatible |
519 | * flags we don't know. */ | |
520 | if (warn_wrong_flags(f, false)) | |
cec736d2 LP |
521 | return -EPROTONOSUPPORT; |
522 | ||
4214009f ZJS |
523 | /* When open for writing we refuse to open files with compatible flags, too. */ |
524 | if (f->writable && warn_wrong_flags(f, true)) | |
d89c8fdf | 525 | return -EPROTONOSUPPORT; |
7560fffc | 526 | |
db11ac1a LP |
527 | if (f->header->state >= _STATE_MAX) |
528 | return -EBADMSG; | |
529 | ||
6f94e420 TS |
530 | header_size = le64toh(f->header->header_size); |
531 | ||
dca6219e | 532 | /* The first addition was n_data, so check that we are at least this large */ |
6f94e420 | 533 | if (header_size < HEADER_SIZE_MIN) |
23b0b2b2 LP |
534 | return -EBADMSG; |
535 | ||
8088cbd3 | 536 | if (JOURNAL_HEADER_SEALED(f->header) && !JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays)) |
beec0085 LP |
537 | return -EBADMSG; |
538 | ||
6f94e420 TS |
539 | arena_size = le64toh(f->header->arena_size); |
540 | ||
541 | if (UINT64_MAX - header_size < arena_size || header_size + arena_size > (uint64_t) f->last_stat.st_size) | |
db11ac1a LP |
542 | return -ENODATA; |
543 | ||
6f94e420 | 544 | if (le64toh(f->header->tail_object_offset) > header_size + arena_size) |
db11ac1a LP |
545 | return -ENODATA; |
546 | ||
7762e02b LP |
547 | if (!VALID64(le64toh(f->header->data_hash_table_offset)) || |
548 | !VALID64(le64toh(f->header->field_hash_table_offset)) || | |
549 | !VALID64(le64toh(f->header->tail_object_offset)) || | |
550 | !VALID64(le64toh(f->header->entry_array_offset))) | |
551 | return -ENODATA; | |
552 | ||
cec736d2 | 553 | if (f->writable) { |
cec736d2 | 554 | sd_id128_t machine_id; |
ae739cc1 | 555 | uint8_t state; |
cec736d2 LP |
556 | int r; |
557 | ||
558 | r = sd_id128_get_machine(&machine_id); | |
559 | if (r < 0) | |
560 | return r; | |
561 | ||
562 | if (!sd_id128_equal(machine_id, f->header->machine_id)) | |
563 | return -EHOSTDOWN; | |
564 | ||
de190aef | 565 | state = f->header->state; |
cec736d2 | 566 | |
b288cdeb ZJS |
567 | if (state == STATE_ARCHIVED) |
568 | return -ESHUTDOWN; /* Already archived */ | |
baaa35ad ZJS |
569 | else if (state == STATE_ONLINE) |
570 | return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), | |
571 | "Journal file %s is already online. Assuming unclean closing.", | |
572 | f->path); | |
573 | else if (state != STATE_OFFLINE) | |
574 | return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), | |
575 | "Journal file %s has unknown state %i.", | |
576 | f->path, state); | |
ae739cc1 | 577 | |
5b3cc0c8 YN |
578 | if (f->header->field_hash_table_size == 0 || f->header->data_hash_table_size == 0) |
579 | return -EBADMSG; | |
580 | ||
ae739cc1 LP |
581 | /* Don't permit appending to files from the future. Because otherwise the realtime timestamps wouldn't |
582 | * be strictly ordered in the entries in the file anymore, and we can't have that since it breaks | |
583 | * bisection. */ | |
baaa35ad ZJS |
584 | if (le64toh(f->header->tail_entry_realtime) > now(CLOCK_REALTIME)) |
585 | return log_debug_errno(SYNTHETIC_ERRNO(ETXTBSY), | |
586 | "Journal file %s is from the future, refusing to append new data to it that'd be older.", | |
587 | f->path); | |
cec736d2 LP |
588 | } |
589 | ||
d89c8fdf ZJS |
590 | f->compress_xz = JOURNAL_HEADER_COMPRESSED_XZ(f->header); |
591 | f->compress_lz4 = JOURNAL_HEADER_COMPRESSED_LZ4(f->header); | |
c586dbf1 | 592 | |
f1889c91 | 593 | f->seal = JOURNAL_HEADER_SEALED(f->header); |
7560fffc | 594 | |
cec736d2 LP |
595 | return 0; |
596 | } | |
597 | ||
2678031a | 598 | static int journal_file_fstat(JournalFile *f) { |
3cc44114 LP |
599 | int r; |
600 | ||
2678031a LP |
601 | assert(f); |
602 | assert(f->fd >= 0); | |
603 | ||
604 | if (fstat(f->fd, &f->last_stat) < 0) | |
605 | return -errno; | |
606 | ||
607 | f->last_stat_usec = now(CLOCK_MONOTONIC); | |
608 | ||
8d6a4d33 | 609 | /* Refuse dealing with with files that aren't regular */ |
3cc44114 LP |
610 | r = stat_verify_regular(&f->last_stat); |
611 | if (r < 0) | |
612 | return r; | |
8d6a4d33 | 613 | |
2678031a LP |
614 | /* Refuse appending to files that are already deleted */ |
615 | if (f->last_stat.st_nlink <= 0) | |
616 | return -EIDRM; | |
617 | ||
618 | return 0; | |
619 | } | |
620 | ||
cec736d2 | 621 | static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) { |
eda4b58b | 622 | uint64_t old_size, new_size; |
fec2aa2f | 623 | int r; |
cec736d2 LP |
624 | |
625 | assert(f); | |
c88cc6af | 626 | assert(f->header); |
cec736d2 | 627 | |
cec736d2 | 628 | /* We assume that this file is not sparse, and we know that |
38ac38b2 | 629 | * for sure, since we always call posix_fallocate() |
cec736d2 LP |
630 | * ourselves */ |
631 | ||
be7cdd8e | 632 | if (mmap_cache_got_sigbus(f->mmap, f->cache_fd)) |
fa6ac760 LP |
633 | return -EIO; |
634 | ||
cec736d2 | 635 | old_size = |
23b0b2b2 | 636 | le64toh(f->header->header_size) + |
cec736d2 LP |
637 | le64toh(f->header->arena_size); |
638 | ||
bc85bfee | 639 | new_size = PAGE_ALIGN(offset + size); |
23b0b2b2 LP |
640 | if (new_size < le64toh(f->header->header_size)) |
641 | new_size = le64toh(f->header->header_size); | |
bc85bfee | 642 | |
2678031a LP |
643 | if (new_size <= old_size) { |
644 | ||
645 | /* We already pre-allocated enough space, but before | |
646 | * we write to it, let's check with fstat() if the | |
647 | * file got deleted, in order make sure we don't throw | |
648 | * away the data immediately. Don't check fstat() for | |
649 | * all writes though, but only once ever 10s. */ | |
650 | ||
651 | if (f->last_stat_usec + LAST_STAT_REFRESH_USEC > now(CLOCK_MONOTONIC)) | |
652 | return 0; | |
653 | ||
654 | return journal_file_fstat(f); | |
655 | } | |
656 | ||
657 | /* Allocate more space. */ | |
cec736d2 | 658 | |
a676e665 | 659 | if (f->metrics.max_size > 0 && new_size > f->metrics.max_size) |
bc85bfee | 660 | return -E2BIG; |
cec736d2 | 661 | |
a676e665 | 662 | if (new_size > f->metrics.min_size && f->metrics.keep_free > 0) { |
cec736d2 LP |
663 | struct statvfs svfs; |
664 | ||
665 | if (fstatvfs(f->fd, &svfs) >= 0) { | |
666 | uint64_t available; | |
667 | ||
070052ab | 668 | available = LESS_BY((uint64_t) svfs.f_bfree * (uint64_t) svfs.f_bsize, f->metrics.keep_free); |
cec736d2 LP |
669 | |
670 | if (new_size - old_size > available) | |
671 | return -E2BIG; | |
672 | } | |
673 | } | |
674 | ||
eda4b58b | 675 | /* Increase by larger blocks at once */ |
be6b0c21 | 676 | new_size = DIV_ROUND_UP(new_size, FILE_SIZE_INCREASE) * FILE_SIZE_INCREASE; |
eda4b58b LP |
677 | if (f->metrics.max_size > 0 && new_size > f->metrics.max_size) |
678 | new_size = f->metrics.max_size; | |
679 | ||
bc85bfee LP |
680 | /* Note that the glibc fallocate() fallback is very |
681 | inefficient, hence we try to minimize the allocation area | |
682 | as we can. */ | |
fec2aa2f GV |
683 | r = posix_fallocate(f->fd, old_size, new_size - old_size); |
684 | if (r != 0) | |
685 | return -r; | |
cec736d2 | 686 | |
23b0b2b2 | 687 | f->header->arena_size = htole64(new_size - le64toh(f->header->header_size)); |
cec736d2 | 688 | |
2678031a | 689 | return journal_file_fstat(f); |
cec736d2 LP |
690 | } |
691 | ||
78519831 | 692 | static unsigned type_to_context(ObjectType type) { |
d3d3208f | 693 | /* One context for each type, plus one catch-all for the rest */ |
69adae51 | 694 | assert_cc(_OBJECT_TYPE_MAX <= MMAP_CACHE_MAX_CONTEXTS); |
fa6ac760 | 695 | assert_cc(CONTEXT_HEADER < MMAP_CACHE_MAX_CONTEXTS); |
d05089d8 | 696 | return type > OBJECT_UNUSED && type < _OBJECT_TYPE_MAX ? type : 0; |
d3d3208f MS |
697 | } |
698 | ||
b439282e | 699 | static int journal_file_move_to(JournalFile *f, ObjectType type, bool keep_always, uint64_t offset, uint64_t size, void **ret, size_t *ret_size) { |
2678031a LP |
700 | int r; |
701 | ||
cec736d2 | 702 | assert(f); |
cec736d2 LP |
703 | assert(ret); |
704 | ||
7762e02b LP |
705 | if (size <= 0) |
706 | return -EINVAL; | |
707 | ||
2a59ea54 | 708 | /* Avoid SIGBUS on invalid accesses */ |
4bbdcdb3 LP |
709 | if (offset + size > (uint64_t) f->last_stat.st_size) { |
710 | /* Hmm, out of range? Let's refresh the fstat() data | |
711 | * first, before we trust that check. */ | |
712 | ||
2678031a LP |
713 | r = journal_file_fstat(f); |
714 | if (r < 0) | |
715 | return r; | |
716 | ||
717 | if (offset + size > (uint64_t) f->last_stat.st_size) | |
4bbdcdb3 LP |
718 | return -EADDRNOTAVAIL; |
719 | } | |
720 | ||
b439282e | 721 | return mmap_cache_get(f->mmap, f->cache_fd, f->prot, type_to_context(type), keep_always, offset, size, &f->last_stat, ret, ret_size); |
cec736d2 LP |
722 | } |
723 | ||
16e9f408 LP |
724 | static uint64_t minimum_header_size(Object *o) { |
725 | ||
b8e891e6 | 726 | static const uint64_t table[] = { |
16e9f408 LP |
727 | [OBJECT_DATA] = sizeof(DataObject), |
728 | [OBJECT_FIELD] = sizeof(FieldObject), | |
729 | [OBJECT_ENTRY] = sizeof(EntryObject), | |
730 | [OBJECT_DATA_HASH_TABLE] = sizeof(HashTableObject), | |
731 | [OBJECT_FIELD_HASH_TABLE] = sizeof(HashTableObject), | |
732 | [OBJECT_ENTRY_ARRAY] = sizeof(EntryArrayObject), | |
733 | [OBJECT_TAG] = sizeof(TagObject), | |
734 | }; | |
735 | ||
736 | if (o->object.type >= ELEMENTSOF(table) || table[o->object.type] <= 0) | |
737 | return sizeof(ObjectHeader); | |
738 | ||
739 | return table[o->object.type]; | |
740 | } | |
741 | ||
24754f36 TR |
742 | /* Lightweight object checks. We want this to be fast, so that we won't |
743 | * slowdown every journal_file_move_to_object() call too much. */ | |
744 | static int journal_file_check_object(JournalFile *f, uint64_t offset, Object *o) { | |
745 | assert(f); | |
746 | assert(o); | |
747 | ||
748 | switch (o->object.type) { | |
749 | ||
750 | case OBJECT_DATA: { | |
baaa35ad ZJS |
751 | if ((le64toh(o->data.entry_offset) == 0) ^ (le64toh(o->data.n_entries) == 0)) |
752 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
753 | "Bad n_entries: %" PRIu64 ": %" PRIu64, | |
754 | le64toh(o->data.n_entries), | |
755 | offset); | |
756 | ||
757 | if (le64toh(o->object.size) - offsetof(DataObject, payload) <= 0) | |
758 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
759 | "Bad object size (<= %zu): %" PRIu64 ": %" PRIu64, | |
760 | offsetof(DataObject, payload), | |
761 | le64toh(o->object.size), | |
762 | offset); | |
24754f36 | 763 | |
10e8445b TR |
764 | if (!VALID64(le64toh(o->data.next_hash_offset)) || |
765 | !VALID64(le64toh(o->data.next_field_offset)) || | |
766 | !VALID64(le64toh(o->data.entry_offset)) || | |
baaa35ad ZJS |
767 | !VALID64(le64toh(o->data.entry_array_offset))) |
768 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
769 | "Invalid offset, next_hash_offset=" OFSfmt ", next_field_offset=" OFSfmt ", entry_offset=" OFSfmt ", entry_array_offset=" OFSfmt ": %" PRIu64, | |
770 | le64toh(o->data.next_hash_offset), | |
771 | le64toh(o->data.next_field_offset), | |
772 | le64toh(o->data.entry_offset), | |
773 | le64toh(o->data.entry_array_offset), | |
774 | offset); | |
24754f36 TR |
775 | |
776 | break; | |
777 | } | |
778 | ||
779 | case OBJECT_FIELD: | |
baaa35ad ZJS |
780 | if (le64toh(o->object.size) - offsetof(FieldObject, payload) <= 0) |
781 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
782 | "Bad field size (<= %zu): %" PRIu64 ": %" PRIu64, | |
783 | offsetof(FieldObject, payload), | |
784 | le64toh(o->object.size), | |
785 | offset); | |
24754f36 | 786 | |
10e8445b | 787 | if (!VALID64(le64toh(o->field.next_hash_offset)) || |
baaa35ad ZJS |
788 | !VALID64(le64toh(o->field.head_data_offset))) |
789 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
790 | "Invalid offset, next_hash_offset=" OFSfmt ", head_data_offset=" OFSfmt ": %" PRIu64, | |
791 | le64toh(o->field.next_hash_offset), | |
792 | le64toh(o->field.head_data_offset), | |
793 | offset); | |
24754f36 TR |
794 | break; |
795 | ||
796 | case OBJECT_ENTRY: | |
baaa35ad ZJS |
797 | if ((le64toh(o->object.size) - offsetof(EntryObject, items)) % sizeof(EntryItem) != 0) |
798 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
799 | "Bad entry size (<= %zu): %" PRIu64 ": %" PRIu64, | |
800 | offsetof(EntryObject, items), | |
801 | le64toh(o->object.size), | |
802 | offset); | |
803 | ||
804 | if ((le64toh(o->object.size) - offsetof(EntryObject, items)) / sizeof(EntryItem) <= 0) | |
805 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
806 | "Invalid number items in entry: %" PRIu64 ": %" PRIu64, | |
807 | (le64toh(o->object.size) - offsetof(EntryObject, items)) / sizeof(EntryItem), | |
808 | offset); | |
809 | ||
810 | if (le64toh(o->entry.seqnum) <= 0) | |
811 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
812 | "Invalid entry seqnum: %" PRIx64 ": %" PRIu64, | |
813 | le64toh(o->entry.seqnum), | |
814 | offset); | |
815 | ||
816 | if (!VALID_REALTIME(le64toh(o->entry.realtime))) | |
817 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
818 | "Invalid entry realtime timestamp: %" PRIu64 ": %" PRIu64, | |
819 | le64toh(o->entry.realtime), | |
820 | offset); | |
821 | ||
822 | if (!VALID_MONOTONIC(le64toh(o->entry.monotonic))) | |
823 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
824 | "Invalid entry monotonic timestamp: %" PRIu64 ": %" PRIu64, | |
825 | le64toh(o->entry.monotonic), | |
826 | offset); | |
24754f36 TR |
827 | |
828 | break; | |
829 | ||
830 | case OBJECT_DATA_HASH_TABLE: | |
831 | case OBJECT_FIELD_HASH_TABLE: | |
832 | if ((le64toh(o->object.size) - offsetof(HashTableObject, items)) % sizeof(HashItem) != 0 || | |
baaa35ad ZJS |
833 | (le64toh(o->object.size) - offsetof(HashTableObject, items)) / sizeof(HashItem) <= 0) |
834 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
835 | "Invalid %s hash table size: %" PRIu64 ": %" PRIu64, | |
836 | o->object.type == OBJECT_DATA_HASH_TABLE ? "data" : "field", | |
837 | le64toh(o->object.size), | |
838 | offset); | |
24754f36 TR |
839 | |
840 | break; | |
841 | ||
842 | case OBJECT_ENTRY_ARRAY: | |
843 | if ((le64toh(o->object.size) - offsetof(EntryArrayObject, items)) % sizeof(le64_t) != 0 || | |
baaa35ad ZJS |
844 | (le64toh(o->object.size) - offsetof(EntryArrayObject, items)) / sizeof(le64_t) <= 0) |
845 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
846 | "Invalid object entry array size: %" PRIu64 ": %" PRIu64, | |
847 | le64toh(o->object.size), | |
848 | offset); | |
849 | ||
850 | if (!VALID64(le64toh(o->entry_array.next_entry_array_offset))) | |
851 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
852 | "Invalid object entry array next_entry_array_offset: " OFSfmt ": %" PRIu64, | |
853 | le64toh(o->entry_array.next_entry_array_offset), | |
854 | offset); | |
24754f36 TR |
855 | |
856 | break; | |
857 | ||
858 | case OBJECT_TAG: | |
baaa35ad ZJS |
859 | if (le64toh(o->object.size) != sizeof(TagObject)) |
860 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
861 | "Invalid object tag size: %" PRIu64 ": %" PRIu64, | |
862 | le64toh(o->object.size), | |
863 | offset); | |
24754f36 | 864 | |
baaa35ad ZJS |
865 | if (!VALID_EPOCH(le64toh(o->tag.epoch))) |
866 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
867 | "Invalid object tag epoch: %" PRIu64 ": %" PRIu64, | |
868 | le64toh(o->tag.epoch), offset); | |
24754f36 TR |
869 | |
870 | break; | |
871 | } | |
872 | ||
873 | return 0; | |
874 | } | |
875 | ||
78519831 | 876 | int journal_file_move_to_object(JournalFile *f, ObjectType type, uint64_t offset, Object **ret) { |
cec736d2 LP |
877 | int r; |
878 | void *t; | |
b439282e | 879 | size_t tsize; |
cec736d2 LP |
880 | Object *o; |
881 | uint64_t s; | |
882 | ||
883 | assert(f); | |
884 | assert(ret); | |
885 | ||
db11ac1a | 886 | /* Objects may only be located at multiple of 64 bit */ |
baaa35ad ZJS |
887 | if (!VALID64(offset)) |
888 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
889 | "Attempt to move to object at non-64bit boundary: %" PRIu64, | |
890 | offset); | |
db11ac1a | 891 | |
50809d7a | 892 | /* Object may not be located in the file header */ |
baaa35ad ZJS |
893 | if (offset < le64toh(f->header->header_size)) |
894 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
895 | "Attempt to move to object located in file header: %" PRIu64, | |
896 | offset); | |
50809d7a | 897 | |
b439282e | 898 | r = journal_file_move_to(f, type, false, offset, sizeof(ObjectHeader), &t, &tsize); |
cec736d2 LP |
899 | if (r < 0) |
900 | return r; | |
901 | ||
902 | o = (Object*) t; | |
903 | s = le64toh(o->object.size); | |
904 | ||
baaa35ad ZJS |
905 | if (s == 0) |
906 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
907 | "Attempt to move to uninitialized object: %" PRIu64, | |
908 | offset); | |
909 | if (s < sizeof(ObjectHeader)) | |
910 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
911 | "Attempt to move to overly short object: %" PRIu64, | |
912 | offset); | |
913 | ||
914 | if (o->object.type <= OBJECT_UNUSED) | |
915 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
916 | "Attempt to move to object with invalid type: %" PRIu64, | |
917 | offset); | |
918 | ||
919 | if (s < minimum_header_size(o)) | |
920 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
921 | "Attempt to move to truncated object: %" PRIu64, | |
922 | offset); | |
923 | ||
924 | if (type > OBJECT_UNUSED && o->object.type != type) | |
925 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
926 | "Attempt to move to object of unexpected type: %" PRIu64, | |
927 | offset); | |
cec736d2 | 928 | |
b439282e VC |
929 | if (s > tsize) { |
930 | r = journal_file_move_to(f, type, false, offset, s, &t, NULL); | |
cec736d2 LP |
931 | if (r < 0) |
932 | return r; | |
933 | ||
934 | o = (Object*) t; | |
935 | } | |
936 | ||
24754f36 TR |
937 | r = journal_file_check_object(f, offset, o); |
938 | if (r < 0) | |
939 | return r; | |
940 | ||
cec736d2 LP |
941 | *ret = o; |
942 | return 0; | |
943 | } | |
944 | ||
d98cc1f2 | 945 | static uint64_t journal_file_entry_seqnum(JournalFile *f, uint64_t *seqnum) { |
cec736d2 LP |
946 | uint64_t r; |
947 | ||
948 | assert(f); | |
c88cc6af | 949 | assert(f->header); |
cec736d2 | 950 | |
beec0085 | 951 | r = le64toh(f->header->tail_entry_seqnum) + 1; |
c2373f84 LP |
952 | |
953 | if (seqnum) { | |
de190aef | 954 | /* If an external seqnum counter was passed, we update |
c2373f84 LP |
955 | * both the local and the external one, and set it to |
956 | * the maximum of both */ | |
957 | ||
958 | if (*seqnum + 1 > r) | |
959 | r = *seqnum + 1; | |
960 | ||
961 | *seqnum = r; | |
962 | } | |
963 | ||
beec0085 | 964 | f->header->tail_entry_seqnum = htole64(r); |
cec736d2 | 965 | |
beec0085 LP |
966 | if (f->header->head_entry_seqnum == 0) |
967 | f->header->head_entry_seqnum = htole64(r); | |
de190aef | 968 | |
cec736d2 LP |
969 | return r; |
970 | } | |
971 | ||
78519831 | 972 | int journal_file_append_object(JournalFile *f, ObjectType type, uint64_t size, Object **ret, uint64_t *offset) { |
cec736d2 LP |
973 | int r; |
974 | uint64_t p; | |
975 | Object *tail, *o; | |
976 | void *t; | |
977 | ||
978 | assert(f); | |
c88cc6af | 979 | assert(f->header); |
d05089d8 | 980 | assert(type > OBJECT_UNUSED && type < _OBJECT_TYPE_MAX); |
cec736d2 LP |
981 | assert(size >= sizeof(ObjectHeader)); |
982 | assert(offset); | |
983 | assert(ret); | |
984 | ||
26687bf8 OS |
985 | r = journal_file_set_online(f); |
986 | if (r < 0) | |
987 | return r; | |
988 | ||
cec736d2 | 989 | p = le64toh(f->header->tail_object_offset); |
cec736d2 | 990 | if (p == 0) |
23b0b2b2 | 991 | p = le64toh(f->header->header_size); |
cec736d2 | 992 | else { |
d05089d8 | 993 | r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &tail); |
cec736d2 LP |
994 | if (r < 0) |
995 | return r; | |
996 | ||
997 | p += ALIGN64(le64toh(tail->object.size)); | |
998 | } | |
999 | ||
1000 | r = journal_file_allocate(f, p, size); | |
1001 | if (r < 0) | |
1002 | return r; | |
1003 | ||
b439282e | 1004 | r = journal_file_move_to(f, type, false, p, size, &t, NULL); |
cec736d2 LP |
1005 | if (r < 0) |
1006 | return r; | |
1007 | ||
1008 | o = (Object*) t; | |
1009 | ||
1010 | zero(o->object); | |
de190aef | 1011 | o->object.type = type; |
cec736d2 LP |
1012 | o->object.size = htole64(size); |
1013 | ||
1014 | f->header->tail_object_offset = htole64(p); | |
cec736d2 LP |
1015 | f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1); |
1016 | ||
1017 | *ret = o; | |
1018 | *offset = p; | |
1019 | ||
1020 | return 0; | |
1021 | } | |
1022 | ||
de190aef | 1023 | static int journal_file_setup_data_hash_table(JournalFile *f) { |
cec736d2 LP |
1024 | uint64_t s, p; |
1025 | Object *o; | |
1026 | int r; | |
1027 | ||
1028 | assert(f); | |
c88cc6af | 1029 | assert(f->header); |
cec736d2 | 1030 | |
070052ab LP |
1031 | /* We estimate that we need 1 hash table entry per 768 bytes |
1032 | of journal file and we want to make sure we never get | |
1033 | beyond 75% fill level. Calculate the hash table size for | |
1034 | the maximum file size based on these metrics. */ | |
4a92baf3 | 1035 | |
dfabe643 | 1036 | s = (f->metrics.max_size * 4 / 768 / 3) * sizeof(HashItem); |
4a92baf3 LP |
1037 | if (s < DEFAULT_DATA_HASH_TABLE_SIZE) |
1038 | s = DEFAULT_DATA_HASH_TABLE_SIZE; | |
1039 | ||
507f22bd | 1040 | log_debug("Reserving %"PRIu64" entries in hash table.", s / sizeof(HashItem)); |
4a92baf3 | 1041 | |
de190aef LP |
1042 | r = journal_file_append_object(f, |
1043 | OBJECT_DATA_HASH_TABLE, | |
1044 | offsetof(Object, hash_table.items) + s, | |
1045 | &o, &p); | |
cec736d2 LP |
1046 | if (r < 0) |
1047 | return r; | |
1048 | ||
29804cc1 | 1049 | memzero(o->hash_table.items, s); |
cec736d2 | 1050 | |
de190aef LP |
1051 | f->header->data_hash_table_offset = htole64(p + offsetof(Object, hash_table.items)); |
1052 | f->header->data_hash_table_size = htole64(s); | |
cec736d2 LP |
1053 | |
1054 | return 0; | |
1055 | } | |
1056 | ||
de190aef | 1057 | static int journal_file_setup_field_hash_table(JournalFile *f) { |
cec736d2 LP |
1058 | uint64_t s, p; |
1059 | Object *o; | |
1060 | int r; | |
1061 | ||
1062 | assert(f); | |
c88cc6af | 1063 | assert(f->header); |
cec736d2 | 1064 | |
3c1668da LP |
1065 | /* We use a fixed size hash table for the fields as this |
1066 | * number should grow very slowly only */ | |
1067 | ||
de190aef LP |
1068 | s = DEFAULT_FIELD_HASH_TABLE_SIZE; |
1069 | r = journal_file_append_object(f, | |
1070 | OBJECT_FIELD_HASH_TABLE, | |
1071 | offsetof(Object, hash_table.items) + s, | |
1072 | &o, &p); | |
cec736d2 LP |
1073 | if (r < 0) |
1074 | return r; | |
1075 | ||
29804cc1 | 1076 | memzero(o->hash_table.items, s); |
cec736d2 | 1077 | |
de190aef LP |
1078 | f->header->field_hash_table_offset = htole64(p + offsetof(Object, hash_table.items)); |
1079 | f->header->field_hash_table_size = htole64(s); | |
cec736d2 LP |
1080 | |
1081 | return 0; | |
1082 | } | |
1083 | ||
dade37d4 | 1084 | int journal_file_map_data_hash_table(JournalFile *f) { |
cec736d2 LP |
1085 | uint64_t s, p; |
1086 | void *t; | |
1087 | int r; | |
1088 | ||
1089 | assert(f); | |
c88cc6af | 1090 | assert(f->header); |
cec736d2 | 1091 | |
dade37d4 LP |
1092 | if (f->data_hash_table) |
1093 | return 0; | |
1094 | ||
de190aef LP |
1095 | p = le64toh(f->header->data_hash_table_offset); |
1096 | s = le64toh(f->header->data_hash_table_size); | |
cec736d2 | 1097 | |
de190aef | 1098 | r = journal_file_move_to(f, |
16e9f408 | 1099 | OBJECT_DATA_HASH_TABLE, |
fcde2389 | 1100 | true, |
de190aef | 1101 | p, s, |
b42549ad | 1102 | &t, NULL); |
cec736d2 LP |
1103 | if (r < 0) |
1104 | return r; | |
1105 | ||
de190aef | 1106 | f->data_hash_table = t; |
cec736d2 LP |
1107 | return 0; |
1108 | } | |
1109 | ||
dade37d4 | 1110 | int journal_file_map_field_hash_table(JournalFile *f) { |
cec736d2 LP |
1111 | uint64_t s, p; |
1112 | void *t; | |
1113 | int r; | |
1114 | ||
1115 | assert(f); | |
c88cc6af | 1116 | assert(f->header); |
cec736d2 | 1117 | |
dade37d4 LP |
1118 | if (f->field_hash_table) |
1119 | return 0; | |
1120 | ||
de190aef LP |
1121 | p = le64toh(f->header->field_hash_table_offset); |
1122 | s = le64toh(f->header->field_hash_table_size); | |
cec736d2 | 1123 | |
de190aef | 1124 | r = journal_file_move_to(f, |
16e9f408 | 1125 | OBJECT_FIELD_HASH_TABLE, |
fcde2389 | 1126 | true, |
de190aef | 1127 | p, s, |
b42549ad | 1128 | &t, NULL); |
cec736d2 LP |
1129 | if (r < 0) |
1130 | return r; | |
1131 | ||
de190aef | 1132 | f->field_hash_table = t; |
cec736d2 LP |
1133 | return 0; |
1134 | } | |
1135 | ||
3c1668da LP |
1136 | static int journal_file_link_field( |
1137 | JournalFile *f, | |
1138 | Object *o, | |
1139 | uint64_t offset, | |
1140 | uint64_t hash) { | |
1141 | ||
805d1486 | 1142 | uint64_t p, h, m; |
3c1668da LP |
1143 | int r; |
1144 | ||
1145 | assert(f); | |
c88cc6af | 1146 | assert(f->header); |
90d222c1 | 1147 | assert(f->field_hash_table); |
3c1668da LP |
1148 | assert(o); |
1149 | assert(offset > 0); | |
1150 | ||
1151 | if (o->object.type != OBJECT_FIELD) | |
1152 | return -EINVAL; | |
1153 | ||
805d1486 LP |
1154 | m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem); |
1155 | if (m <= 0) | |
1156 | return -EBADMSG; | |
3c1668da | 1157 | |
805d1486 | 1158 | /* This might alter the window we are looking at */ |
3c1668da LP |
1159 | o->field.next_hash_offset = o->field.head_data_offset = 0; |
1160 | ||
805d1486 | 1161 | h = hash % m; |
3c1668da LP |
1162 | p = le64toh(f->field_hash_table[h].tail_hash_offset); |
1163 | if (p == 0) | |
1164 | f->field_hash_table[h].head_hash_offset = htole64(offset); | |
1165 | else { | |
1166 | r = journal_file_move_to_object(f, OBJECT_FIELD, p, &o); | |
1167 | if (r < 0) | |
1168 | return r; | |
1169 | ||
1170 | o->field.next_hash_offset = htole64(offset); | |
1171 | } | |
1172 | ||
1173 | f->field_hash_table[h].tail_hash_offset = htole64(offset); | |
1174 | ||
1175 | if (JOURNAL_HEADER_CONTAINS(f->header, n_fields)) | |
1176 | f->header->n_fields = htole64(le64toh(f->header->n_fields) + 1); | |
1177 | ||
1178 | return 0; | |
1179 | } | |
1180 | ||
1181 | static int journal_file_link_data( | |
1182 | JournalFile *f, | |
1183 | Object *o, | |
1184 | uint64_t offset, | |
1185 | uint64_t hash) { | |
1186 | ||
805d1486 | 1187 | uint64_t p, h, m; |
cec736d2 LP |
1188 | int r; |
1189 | ||
1190 | assert(f); | |
c88cc6af | 1191 | assert(f->header); |
90d222c1 | 1192 | assert(f->data_hash_table); |
cec736d2 LP |
1193 | assert(o); |
1194 | assert(offset > 0); | |
b588975f LP |
1195 | |
1196 | if (o->object.type != OBJECT_DATA) | |
1197 | return -EINVAL; | |
cec736d2 | 1198 | |
805d1486 LP |
1199 | m = le64toh(f->header->data_hash_table_size) / sizeof(HashItem); |
1200 | if (m <= 0) | |
1201 | return -EBADMSG; | |
48496df6 | 1202 | |
805d1486 | 1203 | /* This might alter the window we are looking at */ |
de190aef LP |
1204 | o->data.next_hash_offset = o->data.next_field_offset = 0; |
1205 | o->data.entry_offset = o->data.entry_array_offset = 0; | |
1206 | o->data.n_entries = 0; | |
cec736d2 | 1207 | |
805d1486 | 1208 | h = hash % m; |
8db4213e | 1209 | p = le64toh(f->data_hash_table[h].tail_hash_offset); |
3c1668da | 1210 | if (p == 0) |
cec736d2 | 1211 | /* Only entry in the hash table is easy */ |
de190aef | 1212 | f->data_hash_table[h].head_hash_offset = htole64(offset); |
3c1668da | 1213 | else { |
48496df6 LP |
1214 | /* Move back to the previous data object, to patch in |
1215 | * pointer */ | |
cec736d2 | 1216 | |
de190aef | 1217 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); |
cec736d2 LP |
1218 | if (r < 0) |
1219 | return r; | |
1220 | ||
de190aef | 1221 | o->data.next_hash_offset = htole64(offset); |
cec736d2 LP |
1222 | } |
1223 | ||
de190aef | 1224 | f->data_hash_table[h].tail_hash_offset = htole64(offset); |
cec736d2 | 1225 | |
dca6219e LP |
1226 | if (JOURNAL_HEADER_CONTAINS(f->header, n_data)) |
1227 | f->header->n_data = htole64(le64toh(f->header->n_data) + 1); | |
1228 | ||
cec736d2 LP |
1229 | return 0; |
1230 | } | |
1231 | ||
3c1668da LP |
1232 | int journal_file_find_field_object_with_hash( |
1233 | JournalFile *f, | |
1234 | const void *field, uint64_t size, uint64_t hash, | |
1235 | Object **ret, uint64_t *offset) { | |
1236 | ||
805d1486 | 1237 | uint64_t p, osize, h, m; |
3c1668da LP |
1238 | int r; |
1239 | ||
1240 | assert(f); | |
c88cc6af | 1241 | assert(f->header); |
3c1668da LP |
1242 | assert(field && size > 0); |
1243 | ||
dade37d4 LP |
1244 | /* If the field hash table is empty, we can't find anything */ |
1245 | if (le64toh(f->header->field_hash_table_size) <= 0) | |
1246 | return 0; | |
1247 | ||
1248 | /* Map the field hash table, if it isn't mapped yet. */ | |
1249 | r = journal_file_map_field_hash_table(f); | |
1250 | if (r < 0) | |
1251 | return r; | |
1252 | ||
3c1668da LP |
1253 | osize = offsetof(Object, field.payload) + size; |
1254 | ||
805d1486 | 1255 | m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem); |
805d1486 | 1256 | if (m <= 0) |
3c1668da LP |
1257 | return -EBADMSG; |
1258 | ||
805d1486 | 1259 | h = hash % m; |
3c1668da LP |
1260 | p = le64toh(f->field_hash_table[h].head_hash_offset); |
1261 | ||
1262 | while (p > 0) { | |
1263 | Object *o; | |
1264 | ||
1265 | r = journal_file_move_to_object(f, OBJECT_FIELD, p, &o); | |
1266 | if (r < 0) | |
1267 | return r; | |
1268 | ||
1269 | if (le64toh(o->field.hash) == hash && | |
1270 | le64toh(o->object.size) == osize && | |
1271 | memcmp(o->field.payload, field, size) == 0) { | |
1272 | ||
1273 | if (ret) | |
1274 | *ret = o; | |
1275 | if (offset) | |
1276 | *offset = p; | |
1277 | ||
1278 | return 1; | |
1279 | } | |
1280 | ||
1281 | p = le64toh(o->field.next_hash_offset); | |
1282 | } | |
1283 | ||
1284 | return 0; | |
1285 | } | |
1286 | ||
1287 | int journal_file_find_field_object( | |
1288 | JournalFile *f, | |
1289 | const void *field, uint64_t size, | |
1290 | Object **ret, uint64_t *offset) { | |
1291 | ||
1292 | uint64_t hash; | |
1293 | ||
1294 | assert(f); | |
1295 | assert(field && size > 0); | |
1296 | ||
1297 | hash = hash64(field, size); | |
1298 | ||
1299 | return journal_file_find_field_object_with_hash(f, | |
1300 | field, size, hash, | |
1301 | ret, offset); | |
1302 | } | |
1303 | ||
de190aef LP |
1304 | int journal_file_find_data_object_with_hash( |
1305 | JournalFile *f, | |
1306 | const void *data, uint64_t size, uint64_t hash, | |
1307 | Object **ret, uint64_t *offset) { | |
48496df6 | 1308 | |
805d1486 | 1309 | uint64_t p, osize, h, m; |
cec736d2 LP |
1310 | int r; |
1311 | ||
1312 | assert(f); | |
c88cc6af | 1313 | assert(f->header); |
cec736d2 LP |
1314 | assert(data || size == 0); |
1315 | ||
dade37d4 LP |
1316 | /* If there's no data hash table, then there's no entry. */ |
1317 | if (le64toh(f->header->data_hash_table_size) <= 0) | |
1318 | return 0; | |
1319 | ||
1320 | /* Map the data hash table, if it isn't mapped yet. */ | |
1321 | r = journal_file_map_data_hash_table(f); | |
1322 | if (r < 0) | |
1323 | return r; | |
1324 | ||
cec736d2 LP |
1325 | osize = offsetof(Object, data.payload) + size; |
1326 | ||
805d1486 LP |
1327 | m = le64toh(f->header->data_hash_table_size) / sizeof(HashItem); |
1328 | if (m <= 0) | |
bc85bfee LP |
1329 | return -EBADMSG; |
1330 | ||
805d1486 | 1331 | h = hash % m; |
de190aef | 1332 | p = le64toh(f->data_hash_table[h].head_hash_offset); |
cec736d2 | 1333 | |
de190aef LP |
1334 | while (p > 0) { |
1335 | Object *o; | |
cec736d2 | 1336 | |
de190aef | 1337 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); |
cec736d2 LP |
1338 | if (r < 0) |
1339 | return r; | |
1340 | ||
807e17f0 | 1341 | if (le64toh(o->data.hash) != hash) |
85a131e8 | 1342 | goto next; |
807e17f0 | 1343 | |
d89c8fdf | 1344 | if (o->object.flags & OBJECT_COMPRESSION_MASK) { |
349cc4a5 | 1345 | #if HAVE_XZ || HAVE_LZ4 |
fa1c4b51 | 1346 | uint64_t l; |
a7f7d1bd | 1347 | size_t rsize = 0; |
cec736d2 | 1348 | |
807e17f0 LP |
1349 | l = le64toh(o->object.size); |
1350 | if (l <= offsetof(Object, data.payload)) | |
cec736d2 LP |
1351 | return -EBADMSG; |
1352 | ||
807e17f0 LP |
1353 | l -= offsetof(Object, data.payload); |
1354 | ||
d89c8fdf ZJS |
1355 | r = decompress_blob(o->object.flags & OBJECT_COMPRESSION_MASK, |
1356 | o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize, 0); | |
1357 | if (r < 0) | |
1358 | return r; | |
807e17f0 | 1359 | |
b785c858 | 1360 | if (rsize == size && |
807e17f0 LP |
1361 | memcmp(f->compress_buffer, data, size) == 0) { |
1362 | ||
1363 | if (ret) | |
1364 | *ret = o; | |
1365 | ||
1366 | if (offset) | |
1367 | *offset = p; | |
1368 | ||
1369 | return 1; | |
1370 | } | |
3b1a55e1 ZJS |
1371 | #else |
1372 | return -EPROTONOSUPPORT; | |
1373 | #endif | |
807e17f0 LP |
1374 | } else if (le64toh(o->object.size) == osize && |
1375 | memcmp(o->data.payload, data, size) == 0) { | |
1376 | ||
cec736d2 LP |
1377 | if (ret) |
1378 | *ret = o; | |
1379 | ||
1380 | if (offset) | |
1381 | *offset = p; | |
1382 | ||
de190aef | 1383 | return 1; |
cec736d2 LP |
1384 | } |
1385 | ||
85a131e8 | 1386 | next: |
cec736d2 LP |
1387 | p = le64toh(o->data.next_hash_offset); |
1388 | } | |
1389 | ||
de190aef LP |
1390 | return 0; |
1391 | } | |
1392 | ||
1393 | int journal_file_find_data_object( | |
1394 | JournalFile *f, | |
1395 | const void *data, uint64_t size, | |
1396 | Object **ret, uint64_t *offset) { | |
1397 | ||
1398 | uint64_t hash; | |
1399 | ||
1400 | assert(f); | |
1401 | assert(data || size == 0); | |
1402 | ||
1403 | hash = hash64(data, size); | |
1404 | ||
1405 | return journal_file_find_data_object_with_hash(f, | |
1406 | data, size, hash, | |
1407 | ret, offset); | |
1408 | } | |
1409 | ||
3c1668da LP |
1410 | static int journal_file_append_field( |
1411 | JournalFile *f, | |
1412 | const void *field, uint64_t size, | |
1413 | Object **ret, uint64_t *offset) { | |
1414 | ||
1415 | uint64_t hash, p; | |
1416 | uint64_t osize; | |
1417 | Object *o; | |
1418 | int r; | |
1419 | ||
1420 | assert(f); | |
1421 | assert(field && size > 0); | |
1422 | ||
1423 | hash = hash64(field, size); | |
1424 | ||
1425 | r = journal_file_find_field_object_with_hash(f, field, size, hash, &o, &p); | |
1426 | if (r < 0) | |
1427 | return r; | |
1428 | else if (r > 0) { | |
1429 | ||
1430 | if (ret) | |
1431 | *ret = o; | |
1432 | ||
1433 | if (offset) | |
1434 | *offset = p; | |
1435 | ||
1436 | return 0; | |
1437 | } | |
1438 | ||
1439 | osize = offsetof(Object, field.payload) + size; | |
1440 | r = journal_file_append_object(f, OBJECT_FIELD, osize, &o, &p); | |
8c92d4bb LP |
1441 | if (r < 0) |
1442 | return r; | |
3c1668da LP |
1443 | |
1444 | o->field.hash = htole64(hash); | |
1445 | memcpy(o->field.payload, field, size); | |
1446 | ||
1447 | r = journal_file_link_field(f, o, p, hash); | |
1448 | if (r < 0) | |
1449 | return r; | |
1450 | ||
1451 | /* The linking might have altered the window, so let's | |
1452 | * refresh our pointer */ | |
1453 | r = journal_file_move_to_object(f, OBJECT_FIELD, p, &o); | |
1454 | if (r < 0) | |
1455 | return r; | |
1456 | ||
349cc4a5 | 1457 | #if HAVE_GCRYPT |
3c1668da LP |
1458 | r = journal_file_hmac_put_object(f, OBJECT_FIELD, o, p); |
1459 | if (r < 0) | |
1460 | return r; | |
1461 | #endif | |
1462 | ||
1463 | if (ret) | |
1464 | *ret = o; | |
1465 | ||
1466 | if (offset) | |
1467 | *offset = p; | |
1468 | ||
1469 | return 0; | |
1470 | } | |
1471 | ||
48496df6 LP |
1472 | static int journal_file_append_data( |
1473 | JournalFile *f, | |
1474 | const void *data, uint64_t size, | |
1475 | Object **ret, uint64_t *offset) { | |
1476 | ||
de190aef LP |
1477 | uint64_t hash, p; |
1478 | uint64_t osize; | |
1479 | Object *o; | |
d89c8fdf | 1480 | int r, compression = 0; |
3c1668da | 1481 | const void *eq; |
de190aef LP |
1482 | |
1483 | assert(f); | |
1484 | assert(data || size == 0); | |
1485 | ||
1486 | hash = hash64(data, size); | |
1487 | ||
1488 | r = journal_file_find_data_object_with_hash(f, data, size, hash, &o, &p); | |
1489 | if (r < 0) | |
1490 | return r; | |
0240c603 | 1491 | if (r > 0) { |
de190aef LP |
1492 | |
1493 | if (ret) | |
1494 | *ret = o; | |
1495 | ||
1496 | if (offset) | |
1497 | *offset = p; | |
1498 | ||
1499 | return 0; | |
1500 | } | |
1501 | ||
1502 | osize = offsetof(Object, data.payload) + size; | |
1503 | r = journal_file_append_object(f, OBJECT_DATA, osize, &o, &p); | |
cec736d2 LP |
1504 | if (r < 0) |
1505 | return r; | |
1506 | ||
cec736d2 | 1507 | o->data.hash = htole64(hash); |
807e17f0 | 1508 | |
349cc4a5 | 1509 | #if HAVE_XZ || HAVE_LZ4 |
57850536 | 1510 | if (JOURNAL_FILE_COMPRESS(f) && size >= f->compress_threshold_bytes) { |
a7f7d1bd | 1511 | size_t rsize = 0; |
807e17f0 | 1512 | |
5d6f46b6 | 1513 | compression = compress_blob(data, size, o->data.payload, size - 1, &rsize); |
807e17f0 | 1514 | |
d1afbcd2 | 1515 | if (compression >= 0) { |
807e17f0 | 1516 | o->object.size = htole64(offsetof(Object, data.payload) + rsize); |
d89c8fdf | 1517 | o->object.flags |= compression; |
807e17f0 | 1518 | |
fa1c4b51 | 1519 | log_debug("Compressed data object %"PRIu64" -> %zu using %s", |
d89c8fdf | 1520 | size, rsize, object_compressed_to_string(compression)); |
d1afbcd2 LP |
1521 | } else |
1522 | /* Compression didn't work, we don't really care why, let's continue without compression */ | |
1523 | compression = 0; | |
807e17f0 LP |
1524 | } |
1525 | #endif | |
1526 | ||
75f32f04 ZJS |
1527 | if (compression == 0) |
1528 | memcpy_safe(o->data.payload, data, size); | |
cec736d2 | 1529 | |
de190aef | 1530 | r = journal_file_link_data(f, o, p, hash); |
cec736d2 LP |
1531 | if (r < 0) |
1532 | return r; | |
1533 | ||
349cc4a5 | 1534 | #if HAVE_GCRYPT |
33685a5a FB |
1535 | r = journal_file_hmac_put_object(f, OBJECT_DATA, o, p); |
1536 | if (r < 0) | |
1537 | return r; | |
1538 | #endif | |
1539 | ||
48496df6 LP |
1540 | /* The linking might have altered the window, so let's |
1541 | * refresh our pointer */ | |
1542 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); | |
1543 | if (r < 0) | |
1544 | return r; | |
1545 | ||
08c6f819 SL |
1546 | if (!data) |
1547 | eq = NULL; | |
1548 | else | |
1549 | eq = memchr(data, '=', size); | |
3c1668da | 1550 | if (eq && eq > data) { |
748db592 | 1551 | Object *fo = NULL; |
3c1668da | 1552 | uint64_t fp; |
3c1668da LP |
1553 | |
1554 | /* Create field object ... */ | |
1555 | r = journal_file_append_field(f, data, (uint8_t*) eq - (uint8_t*) data, &fo, &fp); | |
1556 | if (r < 0) | |
1557 | return r; | |
1558 | ||
1559 | /* ... and link it in. */ | |
1560 | o->data.next_field_offset = fo->field.head_data_offset; | |
1561 | fo->field.head_data_offset = le64toh(p); | |
1562 | } | |
1563 | ||
cec736d2 LP |
1564 | if (ret) |
1565 | *ret = o; | |
1566 | ||
1567 | if (offset) | |
de190aef | 1568 | *offset = p; |
cec736d2 LP |
1569 | |
1570 | return 0; | |
1571 | } | |
1572 | ||
1573 | uint64_t journal_file_entry_n_items(Object *o) { | |
1574 | assert(o); | |
b588975f LP |
1575 | |
1576 | if (o->object.type != OBJECT_ENTRY) | |
1577 | return 0; | |
cec736d2 LP |
1578 | |
1579 | return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem); | |
1580 | } | |
1581 | ||
0284adc6 | 1582 | uint64_t journal_file_entry_array_n_items(Object *o) { |
de190aef | 1583 | assert(o); |
b588975f LP |
1584 | |
1585 | if (o->object.type != OBJECT_ENTRY_ARRAY) | |
1586 | return 0; | |
de190aef LP |
1587 | |
1588 | return (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / sizeof(uint64_t); | |
1589 | } | |
1590 | ||
fb9a24b6 LP |
1591 | uint64_t journal_file_hash_table_n_items(Object *o) { |
1592 | assert(o); | |
b588975f | 1593 | |
ec2ce0c5 | 1594 | if (!IN_SET(o->object.type, OBJECT_DATA_HASH_TABLE, OBJECT_FIELD_HASH_TABLE)) |
b588975f | 1595 | return 0; |
fb9a24b6 LP |
1596 | |
1597 | return (le64toh(o->object.size) - offsetof(Object, hash_table.items)) / sizeof(HashItem); | |
1598 | } | |
1599 | ||
de190aef | 1600 | static int link_entry_into_array(JournalFile *f, |
4fd052ae FC |
1601 | le64_t *first, |
1602 | le64_t *idx, | |
de190aef | 1603 | uint64_t p) { |
cec736d2 | 1604 | int r; |
de190aef LP |
1605 | uint64_t n = 0, ap = 0, q, i, a, hidx; |
1606 | Object *o; | |
1607 | ||
cec736d2 | 1608 | assert(f); |
c88cc6af | 1609 | assert(f->header); |
de190aef LP |
1610 | assert(first); |
1611 | assert(idx); | |
1612 | assert(p > 0); | |
cec736d2 | 1613 | |
de190aef LP |
1614 | a = le64toh(*first); |
1615 | i = hidx = le64toh(*idx); | |
1616 | while (a > 0) { | |
1617 | ||
1618 | r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); | |
1619 | if (r < 0) | |
1620 | return r; | |
cec736d2 | 1621 | |
de190aef LP |
1622 | n = journal_file_entry_array_n_items(o); |
1623 | if (i < n) { | |
1624 | o->entry_array.items[i] = htole64(p); | |
1625 | *idx = htole64(hidx + 1); | |
1626 | return 0; | |
1627 | } | |
cec736d2 | 1628 | |
de190aef LP |
1629 | i -= n; |
1630 | ap = a; | |
1631 | a = le64toh(o->entry_array.next_entry_array_offset); | |
1632 | } | |
1633 | ||
1634 | if (hidx > n) | |
1635 | n = (hidx+1) * 2; | |
1636 | else | |
1637 | n = n * 2; | |
1638 | ||
1639 | if (n < 4) | |
1640 | n = 4; | |
1641 | ||
1642 | r = journal_file_append_object(f, OBJECT_ENTRY_ARRAY, | |
1643 | offsetof(Object, entry_array.items) + n * sizeof(uint64_t), | |
1644 | &o, &q); | |
cec736d2 LP |
1645 | if (r < 0) |
1646 | return r; | |
1647 | ||
349cc4a5 | 1648 | #if HAVE_GCRYPT |
5996c7c2 | 1649 | r = journal_file_hmac_put_object(f, OBJECT_ENTRY_ARRAY, o, q); |
b0af6f41 LP |
1650 | if (r < 0) |
1651 | return r; | |
feb12d3e | 1652 | #endif |
b0af6f41 | 1653 | |
de190aef | 1654 | o->entry_array.items[i] = htole64(p); |
cec736d2 | 1655 | |
de190aef | 1656 | if (ap == 0) |
7be3aa17 | 1657 | *first = htole64(q); |
cec736d2 | 1658 | else { |
de190aef | 1659 | r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, ap, &o); |
cec736d2 LP |
1660 | if (r < 0) |
1661 | return r; | |
1662 | ||
de190aef LP |
1663 | o->entry_array.next_entry_array_offset = htole64(q); |
1664 | } | |
cec736d2 | 1665 | |
2dee23eb LP |
1666 | if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays)) |
1667 | f->header->n_entry_arrays = htole64(le64toh(f->header->n_entry_arrays) + 1); | |
1668 | ||
de190aef LP |
1669 | *idx = htole64(hidx + 1); |
1670 | ||
1671 | return 0; | |
1672 | } | |
cec736d2 | 1673 | |
de190aef | 1674 | static int link_entry_into_array_plus_one(JournalFile *f, |
4fd052ae FC |
1675 | le64_t *extra, |
1676 | le64_t *first, | |
1677 | le64_t *idx, | |
de190aef LP |
1678 | uint64_t p) { |
1679 | ||
1680 | int r; | |
1681 | ||
1682 | assert(f); | |
1683 | assert(extra); | |
1684 | assert(first); | |
1685 | assert(idx); | |
1686 | assert(p > 0); | |
1687 | ||
1688 | if (*idx == 0) | |
1689 | *extra = htole64(p); | |
1690 | else { | |
4fd052ae | 1691 | le64_t i; |
de190aef | 1692 | |
7be3aa17 | 1693 | i = htole64(le64toh(*idx) - 1); |
de190aef LP |
1694 | r = link_entry_into_array(f, first, &i, p); |
1695 | if (r < 0) | |
1696 | return r; | |
cec736d2 LP |
1697 | } |
1698 | ||
de190aef LP |
1699 | *idx = htole64(le64toh(*idx) + 1); |
1700 | return 0; | |
1701 | } | |
1702 | ||
1703 | static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) { | |
1704 | uint64_t p; | |
1705 | int r; | |
1706 | assert(f); | |
1707 | assert(o); | |
1708 | assert(offset > 0); | |
1709 | ||
1710 | p = le64toh(o->entry.items[i].object_offset); | |
1711 | if (p == 0) | |
1712 | return -EINVAL; | |
1713 | ||
1714 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); | |
cec736d2 LP |
1715 | if (r < 0) |
1716 | return r; | |
1717 | ||
de190aef LP |
1718 | return link_entry_into_array_plus_one(f, |
1719 | &o->data.entry_offset, | |
1720 | &o->data.entry_array_offset, | |
1721 | &o->data.n_entries, | |
1722 | offset); | |
cec736d2 LP |
1723 | } |
1724 | ||
1725 | static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) { | |
de190aef | 1726 | uint64_t n, i; |
cec736d2 LP |
1727 | int r; |
1728 | ||
1729 | assert(f); | |
c88cc6af | 1730 | assert(f->header); |
cec736d2 LP |
1731 | assert(o); |
1732 | assert(offset > 0); | |
b588975f LP |
1733 | |
1734 | if (o->object.type != OBJECT_ENTRY) | |
1735 | return -EINVAL; | |
cec736d2 | 1736 | |
b788cc23 LP |
1737 | __sync_synchronize(); |
1738 | ||
cec736d2 | 1739 | /* Link up the entry itself */ |
de190aef LP |
1740 | r = link_entry_into_array(f, |
1741 | &f->header->entry_array_offset, | |
1742 | &f->header->n_entries, | |
1743 | offset); | |
1744 | if (r < 0) | |
1745 | return r; | |
cec736d2 | 1746 | |
507f22bd | 1747 | /* log_debug("=> %s seqnr=%"PRIu64" n_entries=%"PRIu64, f->path, o->entry.seqnum, f->header->n_entries); */ |
cec736d2 | 1748 | |
de190aef | 1749 | if (f->header->head_entry_realtime == 0) |
0ac38b70 | 1750 | f->header->head_entry_realtime = o->entry.realtime; |
cec736d2 | 1751 | |
0ac38b70 | 1752 | f->header->tail_entry_realtime = o->entry.realtime; |
de190aef LP |
1753 | f->header->tail_entry_monotonic = o->entry.monotonic; |
1754 | ||
cec736d2 LP |
1755 | /* Link up the items */ |
1756 | n = journal_file_entry_n_items(o); | |
1757 | for (i = 0; i < n; i++) { | |
1758 | r = journal_file_link_entry_item(f, o, offset, i); | |
1759 | if (r < 0) | |
1760 | return r; | |
1761 | } | |
1762 | ||
cec736d2 LP |
1763 | return 0; |
1764 | } | |
1765 | ||
1766 | static int journal_file_append_entry_internal( | |
1767 | JournalFile *f, | |
1768 | const dual_timestamp *ts, | |
d180c349 | 1769 | const sd_id128_t *boot_id, |
cec736d2 LP |
1770 | uint64_t xor_hash, |
1771 | const EntryItem items[], unsigned n_items, | |
de190aef | 1772 | uint64_t *seqnum, |
cec736d2 LP |
1773 | Object **ret, uint64_t *offset) { |
1774 | uint64_t np; | |
1775 | uint64_t osize; | |
1776 | Object *o; | |
1777 | int r; | |
1778 | ||
1779 | assert(f); | |
c88cc6af | 1780 | assert(f->header); |
cec736d2 | 1781 | assert(items || n_items == 0); |
de190aef | 1782 | assert(ts); |
cec736d2 LP |
1783 | |
1784 | osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem)); | |
1785 | ||
de190aef | 1786 | r = journal_file_append_object(f, OBJECT_ENTRY, osize, &o, &np); |
cec736d2 LP |
1787 | if (r < 0) |
1788 | return r; | |
1789 | ||
d98cc1f2 | 1790 | o->entry.seqnum = htole64(journal_file_entry_seqnum(f, seqnum)); |
75f32f04 | 1791 | memcpy_safe(o->entry.items, items, n_items * sizeof(EntryItem)); |
de190aef LP |
1792 | o->entry.realtime = htole64(ts->realtime); |
1793 | o->entry.monotonic = htole64(ts->monotonic); | |
cec736d2 | 1794 | o->entry.xor_hash = htole64(xor_hash); |
d180c349 | 1795 | o->entry.boot_id = boot_id ? *boot_id : f->header->boot_id; |
cec736d2 | 1796 | |
349cc4a5 | 1797 | #if HAVE_GCRYPT |
5996c7c2 | 1798 | r = journal_file_hmac_put_object(f, OBJECT_ENTRY, o, np); |
b0af6f41 LP |
1799 | if (r < 0) |
1800 | return r; | |
feb12d3e | 1801 | #endif |
b0af6f41 | 1802 | |
cec736d2 LP |
1803 | r = journal_file_link_entry(f, o, np); |
1804 | if (r < 0) | |
1805 | return r; | |
1806 | ||
1807 | if (ret) | |
1808 | *ret = o; | |
1809 | ||
1810 | if (offset) | |
1811 | *offset = np; | |
1812 | ||
1813 | return 0; | |
1814 | } | |
1815 | ||
cf244689 | 1816 | void journal_file_post_change(JournalFile *f) { |
50f20cfd LP |
1817 | assert(f); |
1818 | ||
c5236850 DT |
1819 | if (f->fd < 0) |
1820 | return; | |
1821 | ||
50f20cfd LP |
1822 | /* inotify() does not receive IN_MODIFY events from file |
1823 | * accesses done via mmap(). After each access we hence | |
1824 | * trigger IN_MODIFY by truncating the journal file to its | |
1825 | * current size which triggers IN_MODIFY. */ | |
1826 | ||
bc85bfee LP |
1827 | __sync_synchronize(); |
1828 | ||
50f20cfd | 1829 | if (ftruncate(f->fd, f->last_stat.st_size) < 0) |
e167d7fd | 1830 | log_debug_errno(errno, "Failed to truncate file to its own size: %m"); |
50f20cfd LP |
1831 | } |
1832 | ||
7a24f3bf VC |
1833 | static int post_change_thunk(sd_event_source *timer, uint64_t usec, void *userdata) { |
1834 | assert(userdata); | |
1835 | ||
1836 | journal_file_post_change(userdata); | |
1837 | ||
1838 | return 1; | |
1839 | } | |
1840 | ||
1841 | static void schedule_post_change(JournalFile *f) { | |
7a24f3bf | 1842 | uint64_t now; |
b6cdfbe5 | 1843 | int r; |
7a24f3bf VC |
1844 | |
1845 | assert(f); | |
1846 | assert(f->post_change_timer); | |
1847 | ||
b6cdfbe5 | 1848 | r = sd_event_source_get_enabled(f->post_change_timer, NULL); |
7a24f3bf | 1849 | if (r < 0) { |
e167d7fd LP |
1850 | log_debug_errno(r, "Failed to get ftruncate timer state: %m"); |
1851 | goto fail; | |
7a24f3bf | 1852 | } |
b6cdfbe5 | 1853 | if (r > 0) |
7a24f3bf VC |
1854 | return; |
1855 | ||
ca5d90d4 | 1856 | r = sd_event_now(sd_event_source_get_event(f->post_change_timer), CLOCK_MONOTONIC, &now); |
7a24f3bf | 1857 | if (r < 0) { |
e167d7fd LP |
1858 | log_debug_errno(r, "Failed to get clock's now for scheduling ftruncate: %m"); |
1859 | goto fail; | |
7a24f3bf VC |
1860 | } |
1861 | ||
ca5d90d4 | 1862 | r = sd_event_source_set_time(f->post_change_timer, now + f->post_change_timer_period); |
7a24f3bf | 1863 | if (r < 0) { |
e167d7fd LP |
1864 | log_debug_errno(r, "Failed to set time for scheduling ftruncate: %m"); |
1865 | goto fail; | |
7a24f3bf VC |
1866 | } |
1867 | ||
ca5d90d4 | 1868 | r = sd_event_source_set_enabled(f->post_change_timer, SD_EVENT_ONESHOT); |
7a24f3bf | 1869 | if (r < 0) { |
e167d7fd LP |
1870 | log_debug_errno(r, "Failed to enable scheduled ftruncate: %m"); |
1871 | goto fail; | |
7a24f3bf | 1872 | } |
e167d7fd LP |
1873 | |
1874 | return; | |
1875 | ||
1876 | fail: | |
1877 | /* On failure, let's simply post the change immediately. */ | |
1878 | journal_file_post_change(f); | |
7a24f3bf VC |
1879 | } |
1880 | ||
1881 | /* Enable coalesced change posting in a timer on the provided sd_event instance */ | |
1882 | int journal_file_enable_post_change_timer(JournalFile *f, sd_event *e, usec_t t) { | |
1883 | _cleanup_(sd_event_source_unrefp) sd_event_source *timer = NULL; | |
1884 | int r; | |
1885 | ||
1886 | assert(f); | |
1887 | assert_return(!f->post_change_timer, -EINVAL); | |
1888 | assert(e); | |
1889 | assert(t); | |
1890 | ||
1891 | r = sd_event_add_time(e, &timer, CLOCK_MONOTONIC, 0, 0, post_change_thunk, f); | |
1892 | if (r < 0) | |
1893 | return r; | |
1894 | ||
1895 | r = sd_event_source_set_enabled(timer, SD_EVENT_OFF); | |
1896 | if (r < 0) | |
1897 | return r; | |
1898 | ||
1cc6c93a | 1899 | f->post_change_timer = TAKE_PTR(timer); |
7a24f3bf VC |
1900 | f->post_change_timer_period = t; |
1901 | ||
1902 | return r; | |
1903 | } | |
1904 | ||
93bab288 YW |
1905 | static int entry_item_cmp(const EntryItem *a, const EntryItem *b) { |
1906 | return CMP(le64toh(a->object_offset), le64toh(b->object_offset)); | |
1f2da9ec LP |
1907 | } |
1908 | ||
d180c349 ZJS |
1909 | int journal_file_append_entry( |
1910 | JournalFile *f, | |
1911 | const dual_timestamp *ts, | |
1912 | const sd_id128_t *boot_id, | |
1913 | const struct iovec iovec[], unsigned n_iovec, | |
1914 | uint64_t *seqnum, | |
1915 | Object **ret, uint64_t *offset) { | |
1916 | ||
cec736d2 LP |
1917 | unsigned i; |
1918 | EntryItem *items; | |
1919 | int r; | |
1920 | uint64_t xor_hash = 0; | |
de190aef | 1921 | struct dual_timestamp _ts; |
cec736d2 LP |
1922 | |
1923 | assert(f); | |
c88cc6af | 1924 | assert(f->header); |
cec736d2 LP |
1925 | assert(iovec || n_iovec == 0); |
1926 | ||
c6273953 | 1927 | if (ts) { |
baaa35ad ZJS |
1928 | if (!VALID_REALTIME(ts->realtime)) |
1929 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
1930 | "Invalid realtime timestamp %" PRIu64 ", refusing entry.", | |
1931 | ts->realtime); | |
1932 | if (!VALID_MONOTONIC(ts->monotonic)) | |
1933 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
1934 | "Invalid monotomic timestamp %" PRIu64 ", refusing entry.", | |
1935 | ts->monotonic); | |
c6273953 | 1936 | } else { |
de190aef LP |
1937 | dual_timestamp_get(&_ts); |
1938 | ts = &_ts; | |
1939 | } | |
1940 | ||
349cc4a5 | 1941 | #if HAVE_GCRYPT |
7560fffc LP |
1942 | r = journal_file_maybe_append_tag(f, ts->realtime); |
1943 | if (r < 0) | |
1944 | return r; | |
feb12d3e | 1945 | #endif |
7560fffc | 1946 | |
64825d3c | 1947 | /* alloca() can't take 0, hence let's allocate at least one */ |
cf409d15 | 1948 | items = newa(EntryItem, MAX(1u, n_iovec)); |
cec736d2 LP |
1949 | |
1950 | for (i = 0; i < n_iovec; i++) { | |
1951 | uint64_t p; | |
1952 | Object *o; | |
1953 | ||
1954 | r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p); | |
1955 | if (r < 0) | |
cf244689 | 1956 | return r; |
cec736d2 LP |
1957 | |
1958 | xor_hash ^= le64toh(o->data.hash); | |
1959 | items[i].object_offset = htole64(p); | |
de7b95cd | 1960 | items[i].hash = o->data.hash; |
cec736d2 LP |
1961 | } |
1962 | ||
1f2da9ec LP |
1963 | /* Order by the position on disk, in order to improve seek |
1964 | * times for rotating media. */ | |
93bab288 | 1965 | typesafe_qsort(items, n_iovec, entry_item_cmp); |
1f2da9ec | 1966 | |
d180c349 | 1967 | r = journal_file_append_entry_internal(f, ts, boot_id, xor_hash, items, n_iovec, seqnum, ret, offset); |
cec736d2 | 1968 | |
fa6ac760 LP |
1969 | /* If the memory mapping triggered a SIGBUS then we return an |
1970 | * IO error and ignore the error code passed down to us, since | |
1971 | * it is very likely just an effect of a nullified replacement | |
1972 | * mapping page */ | |
1973 | ||
be7cdd8e | 1974 | if (mmap_cache_got_sigbus(f->mmap, f->cache_fd)) |
fa6ac760 LP |
1975 | r = -EIO; |
1976 | ||
7a24f3bf VC |
1977 | if (f->post_change_timer) |
1978 | schedule_post_change(f); | |
1979 | else | |
1980 | journal_file_post_change(f); | |
50f20cfd | 1981 | |
cec736d2 LP |
1982 | return r; |
1983 | } | |
1984 | ||
a4bcff5b | 1985 | typedef struct ChainCacheItem { |
fb099c8d | 1986 | uint64_t first; /* the array at the beginning of the chain */ |
a4bcff5b LP |
1987 | uint64_t array; /* the cached array */ |
1988 | uint64_t begin; /* the first item in the cached array */ | |
1989 | uint64_t total; /* the total number of items in all arrays before this one in the chain */ | |
f268980d | 1990 | uint64_t last_index; /* the last index we looked at, to optimize locality when bisecting */ |
a4bcff5b LP |
1991 | } ChainCacheItem; |
1992 | ||
1993 | static void chain_cache_put( | |
4743015d | 1994 | OrderedHashmap *h, |
a4bcff5b LP |
1995 | ChainCacheItem *ci, |
1996 | uint64_t first, | |
1997 | uint64_t array, | |
1998 | uint64_t begin, | |
f268980d LP |
1999 | uint64_t total, |
2000 | uint64_t last_index) { | |
a4bcff5b LP |
2001 | |
2002 | if (!ci) { | |
34741aa3 LP |
2003 | /* If the chain item to cache for this chain is the |
2004 | * first one it's not worth caching anything */ | |
2005 | if (array == first) | |
2006 | return; | |
2007 | ||
29433089 | 2008 | if (ordered_hashmap_size(h) >= CHAIN_CACHE_MAX) { |
4743015d | 2009 | ci = ordered_hashmap_steal_first(h); |
29433089 LP |
2010 | assert(ci); |
2011 | } else { | |
a4bcff5b LP |
2012 | ci = new(ChainCacheItem, 1); |
2013 | if (!ci) | |
2014 | return; | |
2015 | } | |
2016 | ||
2017 | ci->first = first; | |
2018 | ||
4743015d | 2019 | if (ordered_hashmap_put(h, &ci->first, ci) < 0) { |
a4bcff5b LP |
2020 | free(ci); |
2021 | return; | |
2022 | } | |
2023 | } else | |
2024 | assert(ci->first == first); | |
2025 | ||
2026 | ci->array = array; | |
2027 | ci->begin = begin; | |
2028 | ci->total = total; | |
f268980d | 2029 | ci->last_index = last_index; |
a4bcff5b LP |
2030 | } |
2031 | ||
f268980d LP |
2032 | static int generic_array_get( |
2033 | JournalFile *f, | |
2034 | uint64_t first, | |
2035 | uint64_t i, | |
2036 | Object **ret, uint64_t *offset) { | |
de190aef | 2037 | |
cec736d2 | 2038 | Object *o; |
a4bcff5b | 2039 | uint64_t p = 0, a, t = 0; |
cec736d2 | 2040 | int r; |
a4bcff5b | 2041 | ChainCacheItem *ci; |
cec736d2 LP |
2042 | |
2043 | assert(f); | |
2044 | ||
de190aef | 2045 | a = first; |
a4bcff5b LP |
2046 | |
2047 | /* Try the chain cache first */ | |
4743015d | 2048 | ci = ordered_hashmap_get(f->chain_cache, &first); |
a4bcff5b LP |
2049 | if (ci && i > ci->total) { |
2050 | a = ci->array; | |
2051 | i -= ci->total; | |
2052 | t = ci->total; | |
2053 | } | |
2054 | ||
de190aef | 2055 | while (a > 0) { |
a4bcff5b | 2056 | uint64_t k; |
cec736d2 | 2057 | |
de190aef LP |
2058 | r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); |
2059 | if (r < 0) | |
2060 | return r; | |
cec736d2 | 2061 | |
a4bcff5b LP |
2062 | k = journal_file_entry_array_n_items(o); |
2063 | if (i < k) { | |
de190aef | 2064 | p = le64toh(o->entry_array.items[i]); |
a4bcff5b | 2065 | goto found; |
cec736d2 LP |
2066 | } |
2067 | ||
a4bcff5b LP |
2068 | i -= k; |
2069 | t += k; | |
de190aef LP |
2070 | a = le64toh(o->entry_array.next_entry_array_offset); |
2071 | } | |
2072 | ||
a4bcff5b LP |
2073 | return 0; |
2074 | ||
2075 | found: | |
2076 | /* Let's cache this item for the next invocation */ | |
af13a6b0 | 2077 | chain_cache_put(f->chain_cache, ci, first, a, le64toh(o->entry_array.items[0]), t, i); |
de190aef LP |
2078 | |
2079 | r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); | |
2080 | if (r < 0) | |
2081 | return r; | |
2082 | ||
2083 | if (ret) | |
2084 | *ret = o; | |
2085 | ||
2086 | if (offset) | |
2087 | *offset = p; | |
2088 | ||
2089 | return 1; | |
2090 | } | |
2091 | ||
f268980d LP |
2092 | static int generic_array_get_plus_one( |
2093 | JournalFile *f, | |
2094 | uint64_t extra, | |
2095 | uint64_t first, | |
2096 | uint64_t i, | |
2097 | Object **ret, uint64_t *offset) { | |
de190aef LP |
2098 | |
2099 | Object *o; | |
2100 | ||
2101 | assert(f); | |
2102 | ||
2103 | if (i == 0) { | |
2104 | int r; | |
2105 | ||
2106 | r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o); | |
cec736d2 LP |
2107 | if (r < 0) |
2108 | return r; | |
2109 | ||
de190aef LP |
2110 | if (ret) |
2111 | *ret = o; | |
cec736d2 | 2112 | |
de190aef LP |
2113 | if (offset) |
2114 | *offset = extra; | |
cec736d2 | 2115 | |
de190aef | 2116 | return 1; |
cec736d2 LP |
2117 | } |
2118 | ||
de190aef LP |
2119 | return generic_array_get(f, first, i-1, ret, offset); |
2120 | } | |
cec736d2 | 2121 | |
de190aef LP |
2122 | enum { |
2123 | TEST_FOUND, | |
2124 | TEST_LEFT, | |
2125 | TEST_RIGHT | |
2126 | }; | |
cec736d2 | 2127 | |
f268980d LP |
2128 | static int generic_array_bisect( |
2129 | JournalFile *f, | |
2130 | uint64_t first, | |
2131 | uint64_t n, | |
2132 | uint64_t needle, | |
2133 | int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle), | |
2134 | direction_t direction, | |
2135 | Object **ret, | |
2136 | uint64_t *offset, | |
2137 | uint64_t *idx) { | |
2138 | ||
2139 | uint64_t a, p, t = 0, i = 0, last_p = 0, last_index = (uint64_t) -1; | |
de190aef LP |
2140 | bool subtract_one = false; |
2141 | Object *o, *array = NULL; | |
2142 | int r; | |
a4bcff5b | 2143 | ChainCacheItem *ci; |
cec736d2 | 2144 | |
de190aef LP |
2145 | assert(f); |
2146 | assert(test_object); | |
cec736d2 | 2147 | |
a4bcff5b | 2148 | /* Start with the first array in the chain */ |
de190aef | 2149 | a = first; |
a4bcff5b | 2150 | |
4743015d | 2151 | ci = ordered_hashmap_get(f->chain_cache, &first); |
96d4d024 | 2152 | if (ci && n > ci->total && ci->begin != 0) { |
a4bcff5b LP |
2153 | /* Ah, we have iterated this bisection array chain |
2154 | * previously! Let's see if we can skip ahead in the | |
2155 | * chain, as far as the last time. But we can't jump | |
2156 | * backwards in the chain, so let's check that | |
2157 | * first. */ | |
2158 | ||
2159 | r = test_object(f, ci->begin, needle); | |
2160 | if (r < 0) | |
2161 | return r; | |
2162 | ||
2163 | if (r == TEST_LEFT) { | |
f268980d | 2164 | /* OK, what we are looking for is right of the |
a4bcff5b LP |
2165 | * begin of this EntryArray, so let's jump |
2166 | * straight to previously cached array in the | |
2167 | * chain */ | |
2168 | ||
2169 | a = ci->array; | |
2170 | n -= ci->total; | |
2171 | t = ci->total; | |
f268980d | 2172 | last_index = ci->last_index; |
a4bcff5b LP |
2173 | } |
2174 | } | |
2175 | ||
de190aef LP |
2176 | while (a > 0) { |
2177 | uint64_t left, right, k, lp; | |
2178 | ||
2179 | r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &array); | |
cec736d2 LP |
2180 | if (r < 0) |
2181 | return r; | |
2182 | ||
de190aef LP |
2183 | k = journal_file_entry_array_n_items(array); |
2184 | right = MIN(k, n); | |
2185 | if (right <= 0) | |
2186 | return 0; | |
cec736d2 | 2187 | |
de190aef LP |
2188 | i = right - 1; |
2189 | lp = p = le64toh(array->entry_array.items[i]); | |
2190 | if (p <= 0) | |
bee6a291 LP |
2191 | r = -EBADMSG; |
2192 | else | |
2193 | r = test_object(f, p, needle); | |
2194 | if (r == -EBADMSG) { | |
2195 | log_debug_errno(r, "Encountered invalid entry while bisecting, cutting algorithm short. (1)"); | |
2196 | n = i; | |
2197 | continue; | |
2198 | } | |
de190aef LP |
2199 | if (r < 0) |
2200 | return r; | |
cec736d2 | 2201 | |
de190aef LP |
2202 | if (r == TEST_FOUND) |
2203 | r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT; | |
2204 | ||
2205 | if (r == TEST_RIGHT) { | |
2206 | left = 0; | |
2207 | right -= 1; | |
f268980d LP |
2208 | |
2209 | if (last_index != (uint64_t) -1) { | |
2210 | assert(last_index <= right); | |
2211 | ||
2212 | /* If we cached the last index we | |
2213 | * looked at, let's try to not to jump | |
2214 | * too wildly around and see if we can | |
2215 | * limit the range to look at early to | |
2216 | * the immediate neighbors of the last | |
2217 | * index we looked at. */ | |
2218 | ||
2219 | if (last_index > 0) { | |
2220 | uint64_t x = last_index - 1; | |
2221 | ||
2222 | p = le64toh(array->entry_array.items[x]); | |
2223 | if (p <= 0) | |
2224 | return -EBADMSG; | |
2225 | ||
2226 | r = test_object(f, p, needle); | |
2227 | if (r < 0) | |
2228 | return r; | |
2229 | ||
2230 | if (r == TEST_FOUND) | |
2231 | r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT; | |
2232 | ||
2233 | if (r == TEST_RIGHT) | |
2234 | right = x; | |
2235 | else | |
2236 | left = x + 1; | |
2237 | } | |
2238 | ||
2239 | if (last_index < right) { | |
2240 | uint64_t y = last_index + 1; | |
2241 | ||
2242 | p = le64toh(array->entry_array.items[y]); | |
2243 | if (p <= 0) | |
2244 | return -EBADMSG; | |
2245 | ||
2246 | r = test_object(f, p, needle); | |
2247 | if (r < 0) | |
2248 | return r; | |
2249 | ||
2250 | if (r == TEST_FOUND) | |
2251 | r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT; | |
2252 | ||
2253 | if (r == TEST_RIGHT) | |
2254 | right = y; | |
2255 | else | |
2256 | left = y + 1; | |
2257 | } | |
f268980d LP |
2258 | } |
2259 | ||
de190aef LP |
2260 | for (;;) { |
2261 | if (left == right) { | |
2262 | if (direction == DIRECTION_UP) | |
2263 | subtract_one = true; | |
2264 | ||
2265 | i = left; | |
2266 | goto found; | |
2267 | } | |
2268 | ||
2269 | assert(left < right); | |
de190aef | 2270 | i = (left + right) / 2; |
f268980d | 2271 | |
de190aef LP |
2272 | p = le64toh(array->entry_array.items[i]); |
2273 | if (p <= 0) | |
bee6a291 LP |
2274 | r = -EBADMSG; |
2275 | else | |
2276 | r = test_object(f, p, needle); | |
2277 | if (r == -EBADMSG) { | |
2278 | log_debug_errno(r, "Encountered invalid entry while bisecting, cutting algorithm short. (2)"); | |
2279 | right = n = i; | |
2280 | continue; | |
2281 | } | |
de190aef LP |
2282 | if (r < 0) |
2283 | return r; | |
cec736d2 | 2284 | |
de190aef LP |
2285 | if (r == TEST_FOUND) |
2286 | r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT; | |
2287 | ||
2288 | if (r == TEST_RIGHT) | |
2289 | right = i; | |
2290 | else | |
2291 | left = i + 1; | |
2292 | } | |
2293 | } | |
2294 | ||
2173cbf8 | 2295 | if (k >= n) { |
cbdca852 LP |
2296 | if (direction == DIRECTION_UP) { |
2297 | i = n; | |
2298 | subtract_one = true; | |
2299 | goto found; | |
2300 | } | |
2301 | ||
cec736d2 | 2302 | return 0; |
cbdca852 | 2303 | } |
cec736d2 | 2304 | |
de190aef LP |
2305 | last_p = lp; |
2306 | ||
2307 | n -= k; | |
2308 | t += k; | |
f268980d | 2309 | last_index = (uint64_t) -1; |
de190aef | 2310 | a = le64toh(array->entry_array.next_entry_array_offset); |
cec736d2 LP |
2311 | } |
2312 | ||
2313 | return 0; | |
de190aef LP |
2314 | |
2315 | found: | |
2316 | if (subtract_one && t == 0 && i == 0) | |
2317 | return 0; | |
2318 | ||
a4bcff5b | 2319 | /* Let's cache this item for the next invocation */ |
af13a6b0 | 2320 | chain_cache_put(f->chain_cache, ci, first, a, le64toh(array->entry_array.items[0]), t, subtract_one ? (i > 0 ? i-1 : (uint64_t) -1) : i); |
a4bcff5b | 2321 | |
de190aef LP |
2322 | if (subtract_one && i == 0) |
2323 | p = last_p; | |
2324 | else if (subtract_one) | |
2325 | p = le64toh(array->entry_array.items[i-1]); | |
2326 | else | |
2327 | p = le64toh(array->entry_array.items[i]); | |
2328 | ||
2329 | r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); | |
2330 | if (r < 0) | |
2331 | return r; | |
2332 | ||
2333 | if (ret) | |
2334 | *ret = o; | |
2335 | ||
2336 | if (offset) | |
2337 | *offset = p; | |
2338 | ||
2339 | if (idx) | |
cbdca852 | 2340 | *idx = t + i + (subtract_one ? -1 : 0); |
de190aef LP |
2341 | |
2342 | return 1; | |
cec736d2 LP |
2343 | } |
2344 | ||
f268980d LP |
2345 | static int generic_array_bisect_plus_one( |
2346 | JournalFile *f, | |
2347 | uint64_t extra, | |
2348 | uint64_t first, | |
2349 | uint64_t n, | |
2350 | uint64_t needle, | |
2351 | int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle), | |
2352 | direction_t direction, | |
2353 | Object **ret, | |
2354 | uint64_t *offset, | |
2355 | uint64_t *idx) { | |
de190aef | 2356 | |
cec736d2 | 2357 | int r; |
cbdca852 LP |
2358 | bool step_back = false; |
2359 | Object *o; | |
cec736d2 LP |
2360 | |
2361 | assert(f); | |
de190aef | 2362 | assert(test_object); |
cec736d2 | 2363 | |
de190aef LP |
2364 | if (n <= 0) |
2365 | return 0; | |
cec736d2 | 2366 | |
de190aef LP |
2367 | /* This bisects the array in object 'first', but first checks |
2368 | * an extra */ | |
de190aef LP |
2369 | r = test_object(f, extra, needle); |
2370 | if (r < 0) | |
2371 | return r; | |
a536e261 LP |
2372 | |
2373 | if (r == TEST_FOUND) | |
2374 | r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT; | |
2375 | ||
cbdca852 LP |
2376 | /* if we are looking with DIRECTION_UP then we need to first |
2377 | see if in the actual array there is a matching entry, and | |
2378 | return the last one of that. But if there isn't any we need | |
2379 | to return this one. Hence remember this, and return it | |
2380 | below. */ | |
2381 | if (r == TEST_LEFT) | |
2382 | step_back = direction == DIRECTION_UP; | |
de190aef | 2383 | |
cbdca852 LP |
2384 | if (r == TEST_RIGHT) { |
2385 | if (direction == DIRECTION_DOWN) | |
2386 | goto found; | |
2387 | else | |
2388 | return 0; | |
a536e261 | 2389 | } |
cec736d2 | 2390 | |
de190aef LP |
2391 | r = generic_array_bisect(f, first, n-1, needle, test_object, direction, ret, offset, idx); |
2392 | ||
cbdca852 LP |
2393 | if (r == 0 && step_back) |
2394 | goto found; | |
2395 | ||
ecf68b1d | 2396 | if (r > 0 && idx) |
313cefa1 | 2397 | (*idx)++; |
de190aef LP |
2398 | |
2399 | return r; | |
cbdca852 LP |
2400 | |
2401 | found: | |
2402 | r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o); | |
2403 | if (r < 0) | |
2404 | return r; | |
2405 | ||
2406 | if (ret) | |
2407 | *ret = o; | |
2408 | ||
2409 | if (offset) | |
2410 | *offset = extra; | |
2411 | ||
2412 | if (idx) | |
2413 | *idx = 0; | |
2414 | ||
2415 | return 1; | |
2416 | } | |
2417 | ||
44a6b1b6 | 2418 | _pure_ static int test_object_offset(JournalFile *f, uint64_t p, uint64_t needle) { |
cbdca852 LP |
2419 | assert(f); |
2420 | assert(p > 0); | |
2421 | ||
2422 | if (p == needle) | |
2423 | return TEST_FOUND; | |
2424 | else if (p < needle) | |
2425 | return TEST_LEFT; | |
2426 | else | |
2427 | return TEST_RIGHT; | |
2428 | } | |
2429 | ||
de190aef LP |
2430 | static int test_object_seqnum(JournalFile *f, uint64_t p, uint64_t needle) { |
2431 | Object *o; | |
2432 | int r; | |
2433 | ||
2434 | assert(f); | |
2435 | assert(p > 0); | |
2436 | ||
2437 | r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); | |
cec736d2 LP |
2438 | if (r < 0) |
2439 | return r; | |
2440 | ||
de190aef LP |
2441 | if (le64toh(o->entry.seqnum) == needle) |
2442 | return TEST_FOUND; | |
2443 | else if (le64toh(o->entry.seqnum) < needle) | |
2444 | return TEST_LEFT; | |
2445 | else | |
2446 | return TEST_RIGHT; | |
2447 | } | |
cec736d2 | 2448 | |
de190aef LP |
2449 | int journal_file_move_to_entry_by_seqnum( |
2450 | JournalFile *f, | |
2451 | uint64_t seqnum, | |
2452 | direction_t direction, | |
2453 | Object **ret, | |
2454 | uint64_t *offset) { | |
c88cc6af VC |
2455 | assert(f); |
2456 | assert(f->header); | |
de190aef LP |
2457 | |
2458 | return generic_array_bisect(f, | |
2459 | le64toh(f->header->entry_array_offset), | |
2460 | le64toh(f->header->n_entries), | |
2461 | seqnum, | |
2462 | test_object_seqnum, | |
2463 | direction, | |
2464 | ret, offset, NULL); | |
2465 | } | |
cec736d2 | 2466 | |
de190aef LP |
2467 | static int test_object_realtime(JournalFile *f, uint64_t p, uint64_t needle) { |
2468 | Object *o; | |
2469 | int r; | |
2470 | ||
2471 | assert(f); | |
2472 | assert(p > 0); | |
2473 | ||
2474 | r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); | |
2475 | if (r < 0) | |
2476 | return r; | |
2477 | ||
2478 | if (le64toh(o->entry.realtime) == needle) | |
2479 | return TEST_FOUND; | |
2480 | else if (le64toh(o->entry.realtime) < needle) | |
2481 | return TEST_LEFT; | |
2482 | else | |
2483 | return TEST_RIGHT; | |
cec736d2 LP |
2484 | } |
2485 | ||
de190aef LP |
2486 | int journal_file_move_to_entry_by_realtime( |
2487 | JournalFile *f, | |
2488 | uint64_t realtime, | |
2489 | direction_t direction, | |
2490 | Object **ret, | |
2491 | uint64_t *offset) { | |
c88cc6af VC |
2492 | assert(f); |
2493 | assert(f->header); | |
de190aef LP |
2494 | |
2495 | return generic_array_bisect(f, | |
2496 | le64toh(f->header->entry_array_offset), | |
2497 | le64toh(f->header->n_entries), | |
2498 | realtime, | |
2499 | test_object_realtime, | |
2500 | direction, | |
2501 | ret, offset, NULL); | |
2502 | } | |
2503 | ||
2504 | static int test_object_monotonic(JournalFile *f, uint64_t p, uint64_t needle) { | |
2505 | Object *o; | |
2506 | int r; | |
2507 | ||
2508 | assert(f); | |
2509 | assert(p > 0); | |
2510 | ||
2511 | r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); | |
2512 | if (r < 0) | |
2513 | return r; | |
2514 | ||
2515 | if (le64toh(o->entry.monotonic) == needle) | |
2516 | return TEST_FOUND; | |
2517 | else if (le64toh(o->entry.monotonic) < needle) | |
2518 | return TEST_LEFT; | |
2519 | else | |
2520 | return TEST_RIGHT; | |
2521 | } | |
2522 | ||
2a560338 | 2523 | static int find_data_object_by_boot_id( |
47838ab3 ZJS |
2524 | JournalFile *f, |
2525 | sd_id128_t boot_id, | |
2526 | Object **o, | |
2527 | uint64_t *b) { | |
2a560338 | 2528 | |
fbd0b64f | 2529 | char t[STRLEN("_BOOT_ID=") + 32 + 1] = "_BOOT_ID="; |
47838ab3 ZJS |
2530 | |
2531 | sd_id128_to_string(boot_id, t + 9); | |
2532 | return journal_file_find_data_object(f, t, sizeof(t) - 1, o, b); | |
2533 | } | |
2534 | ||
de190aef LP |
2535 | int journal_file_move_to_entry_by_monotonic( |
2536 | JournalFile *f, | |
2537 | sd_id128_t boot_id, | |
2538 | uint64_t monotonic, | |
2539 | direction_t direction, | |
2540 | Object **ret, | |
2541 | uint64_t *offset) { | |
2542 | ||
de190aef LP |
2543 | Object *o; |
2544 | int r; | |
2545 | ||
cbdca852 | 2546 | assert(f); |
de190aef | 2547 | |
47838ab3 | 2548 | r = find_data_object_by_boot_id(f, boot_id, &o, NULL); |
de190aef LP |
2549 | if (r < 0) |
2550 | return r; | |
cbdca852 | 2551 | if (r == 0) |
de190aef LP |
2552 | return -ENOENT; |
2553 | ||
2554 | return generic_array_bisect_plus_one(f, | |
2555 | le64toh(o->data.entry_offset), | |
2556 | le64toh(o->data.entry_array_offset), | |
2557 | le64toh(o->data.n_entries), | |
2558 | monotonic, | |
2559 | test_object_monotonic, | |
2560 | direction, | |
2561 | ret, offset, NULL); | |
2562 | } | |
2563 | ||
1fc605b0 | 2564 | void journal_file_reset_location(JournalFile *f) { |
6573ef05 | 2565 | f->location_type = LOCATION_HEAD; |
1fc605b0 | 2566 | f->current_offset = 0; |
6573ef05 MS |
2567 | f->current_seqnum = 0; |
2568 | f->current_realtime = 0; | |
2569 | f->current_monotonic = 0; | |
2570 | zero(f->current_boot_id); | |
2571 | f->current_xor_hash = 0; | |
2572 | } | |
2573 | ||
950c07d4 | 2574 | void journal_file_save_location(JournalFile *f, Object *o, uint64_t offset) { |
6573ef05 MS |
2575 | f->location_type = LOCATION_SEEK; |
2576 | f->current_offset = offset; | |
2577 | f->current_seqnum = le64toh(o->entry.seqnum); | |
2578 | f->current_realtime = le64toh(o->entry.realtime); | |
2579 | f->current_monotonic = le64toh(o->entry.monotonic); | |
2580 | f->current_boot_id = o->entry.boot_id; | |
2581 | f->current_xor_hash = le64toh(o->entry.xor_hash); | |
1fc605b0 MS |
2582 | } |
2583 | ||
d8ae66d7 | 2584 | int journal_file_compare_locations(JournalFile *af, JournalFile *bf) { |
90c88092 YW |
2585 | int r; |
2586 | ||
d8ae66d7 | 2587 | assert(af); |
c88cc6af | 2588 | assert(af->header); |
d8ae66d7 | 2589 | assert(bf); |
c88cc6af | 2590 | assert(bf->header); |
d8ae66d7 MS |
2591 | assert(af->location_type == LOCATION_SEEK); |
2592 | assert(bf->location_type == LOCATION_SEEK); | |
2593 | ||
2594 | /* If contents and timestamps match, these entries are | |
2595 | * identical, even if the seqnum does not match */ | |
2596 | if (sd_id128_equal(af->current_boot_id, bf->current_boot_id) && | |
2597 | af->current_monotonic == bf->current_monotonic && | |
2598 | af->current_realtime == bf->current_realtime && | |
2599 | af->current_xor_hash == bf->current_xor_hash) | |
2600 | return 0; | |
2601 | ||
2602 | if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) { | |
2603 | ||
2604 | /* If this is from the same seqnum source, compare | |
2605 | * seqnums */ | |
90c88092 YW |
2606 | r = CMP(af->current_seqnum, bf->current_seqnum); |
2607 | if (r != 0) | |
2608 | return r; | |
d8ae66d7 MS |
2609 | |
2610 | /* Wow! This is weird, different data but the same | |
2611 | * seqnums? Something is borked, but let's make the | |
2612 | * best of it and compare by time. */ | |
2613 | } | |
2614 | ||
2615 | if (sd_id128_equal(af->current_boot_id, bf->current_boot_id)) { | |
2616 | ||
2617 | /* If the boot id matches, compare monotonic time */ | |
90c88092 YW |
2618 | r = CMP(af->current_monotonic, bf->current_monotonic); |
2619 | if (r != 0) | |
2620 | return r; | |
d8ae66d7 MS |
2621 | } |
2622 | ||
2623 | /* Otherwise, compare UTC time */ | |
90c88092 YW |
2624 | r = CMP(af->current_realtime, bf->current_realtime); |
2625 | if (r != 0) | |
2626 | return r; | |
d8ae66d7 MS |
2627 | |
2628 | /* Finally, compare by contents */ | |
6dd91b36 | 2629 | return CMP(af->current_xor_hash, bf->current_xor_hash); |
d8ae66d7 MS |
2630 | } |
2631 | ||
aa598ba5 LP |
2632 | static int bump_array_index(uint64_t *i, direction_t direction, uint64_t n) { |
2633 | ||
2634 | /* Increase or decrease the specified index, in the right direction. */ | |
2635 | ||
2636 | if (direction == DIRECTION_DOWN) { | |
2637 | if (*i >= n - 1) | |
2638 | return 0; | |
2639 | ||
2640 | (*i) ++; | |
2641 | } else { | |
2642 | if (*i <= 0) | |
2643 | return 0; | |
2644 | ||
2645 | (*i) --; | |
2646 | } | |
2647 | ||
2648 | return 1; | |
2649 | } | |
2650 | ||
b6da4ed0 LP |
2651 | static bool check_properly_ordered(uint64_t new_offset, uint64_t old_offset, direction_t direction) { |
2652 | ||
2653 | /* Consider it an error if any of the two offsets is uninitialized */ | |
2654 | if (old_offset == 0 || new_offset == 0) | |
2655 | return false; | |
2656 | ||
2657 | /* If we go down, the new offset must be larger than the old one. */ | |
2658 | return direction == DIRECTION_DOWN ? | |
2659 | new_offset > old_offset : | |
2660 | new_offset < old_offset; | |
2661 | } | |
2662 | ||
de190aef LP |
2663 | int journal_file_next_entry( |
2664 | JournalFile *f, | |
f534928a | 2665 | uint64_t p, |
de190aef LP |
2666 | direction_t direction, |
2667 | Object **ret, uint64_t *offset) { | |
2668 | ||
fb099c8d | 2669 | uint64_t i, n, ofs; |
cec736d2 LP |
2670 | int r; |
2671 | ||
2672 | assert(f); | |
c88cc6af | 2673 | assert(f->header); |
de190aef LP |
2674 | |
2675 | n = le64toh(f->header->n_entries); | |
2676 | if (n <= 0) | |
2677 | return 0; | |
cec736d2 | 2678 | |
f534928a | 2679 | if (p == 0) |
de190aef | 2680 | i = direction == DIRECTION_DOWN ? 0 : n - 1; |
cec736d2 | 2681 | else { |
de190aef LP |
2682 | r = generic_array_bisect(f, |
2683 | le64toh(f->header->entry_array_offset), | |
2684 | le64toh(f->header->n_entries), | |
2685 | p, | |
2686 | test_object_offset, | |
2687 | DIRECTION_DOWN, | |
2688 | NULL, NULL, | |
2689 | &i); | |
2690 | if (r <= 0) | |
2691 | return r; | |
2692 | ||
aa598ba5 LP |
2693 | r = bump_array_index(&i, direction, n); |
2694 | if (r <= 0) | |
2695 | return r; | |
cec736d2 LP |
2696 | } |
2697 | ||
de190aef | 2698 | /* And jump to it */ |
989793d3 LP |
2699 | for (;;) { |
2700 | r = generic_array_get(f, | |
2701 | le64toh(f->header->entry_array_offset), | |
2702 | i, | |
2703 | ret, &ofs); | |
2704 | if (r > 0) | |
2705 | break; | |
2706 | if (r != -EBADMSG) | |
2707 | return r; | |
2708 | ||
2709 | /* OK, so this entry is borked. Most likely some entry didn't get synced to disk properly, let's see if | |
2710 | * the next one might work for us instead. */ | |
2711 | log_debug_errno(r, "Entry item %" PRIu64 " is bad, skipping over it.", i); | |
2712 | ||
2713 | r = bump_array_index(&i, direction, n); | |
2714 | if (r <= 0) | |
2715 | return r; | |
caeab8f6 | 2716 | } |
fb099c8d | 2717 | |
b6da4ed0 | 2718 | /* Ensure our array is properly ordered. */ |
baaa35ad ZJS |
2719 | if (p > 0 && !check_properly_ordered(ofs, p, direction)) |
2720 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
2721 | "%s: entry array not properly ordered at entry %" PRIu64, | |
2722 | f->path, i); | |
fb099c8d ZJS |
2723 | |
2724 | if (offset) | |
2725 | *offset = ofs; | |
2726 | ||
2727 | return 1; | |
de190aef | 2728 | } |
cec736d2 | 2729 | |
de190aef LP |
2730 | int journal_file_next_entry_for_data( |
2731 | JournalFile *f, | |
2732 | Object *o, uint64_t p, | |
2733 | uint64_t data_offset, | |
2734 | direction_t direction, | |
2735 | Object **ret, uint64_t *offset) { | |
2736 | ||
ded5034e | 2737 | uint64_t i, n, ofs; |
de190aef | 2738 | Object *d; |
989793d3 | 2739 | int r; |
cec736d2 LP |
2740 | |
2741 | assert(f); | |
de190aef | 2742 | assert(p > 0 || !o); |
cec736d2 | 2743 | |
de190aef | 2744 | r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d); |
466ccd92 | 2745 | if (r < 0) |
de190aef | 2746 | return r; |
cec736d2 | 2747 | |
de190aef LP |
2748 | n = le64toh(d->data.n_entries); |
2749 | if (n <= 0) | |
2750 | return n; | |
cec736d2 | 2751 | |
de190aef LP |
2752 | if (!o) |
2753 | i = direction == DIRECTION_DOWN ? 0 : n - 1; | |
2754 | else { | |
2755 | if (o->object.type != OBJECT_ENTRY) | |
2756 | return -EINVAL; | |
cec736d2 | 2757 | |
de190aef LP |
2758 | r = generic_array_bisect_plus_one(f, |
2759 | le64toh(d->data.entry_offset), | |
2760 | le64toh(d->data.entry_array_offset), | |
2761 | le64toh(d->data.n_entries), | |
2762 | p, | |
2763 | test_object_offset, | |
2764 | DIRECTION_DOWN, | |
2765 | NULL, NULL, | |
2766 | &i); | |
2767 | ||
2768 | if (r <= 0) | |
cec736d2 LP |
2769 | return r; |
2770 | ||
aa598ba5 LP |
2771 | r = bump_array_index(&i, direction, n); |
2772 | if (r <= 0) | |
2773 | return r; | |
de190aef | 2774 | } |
cec736d2 | 2775 | |
989793d3 LP |
2776 | for (;;) { |
2777 | r = generic_array_get_plus_one(f, | |
2778 | le64toh(d->data.entry_offset), | |
2779 | le64toh(d->data.entry_array_offset), | |
2780 | i, | |
2781 | ret, &ofs); | |
2782 | if (r > 0) | |
2783 | break; | |
2784 | if (r != -EBADMSG) | |
2785 | return r; | |
2786 | ||
2787 | log_debug_errno(r, "Data entry item %" PRIu64 " is bad, skipping over it.", i); | |
2788 | ||
2789 | r = bump_array_index(&i, direction, n); | |
2790 | if (r <= 0) | |
2791 | return r; | |
2792 | } | |
ded5034e LP |
2793 | |
2794 | /* Ensure our array is properly ordered. */ | |
baaa35ad ZJS |
2795 | if (p > 0 && check_properly_ordered(ofs, p, direction)) |
2796 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), | |
2797 | "%s data entry array not properly ordered at entry %" PRIu64, | |
2798 | f->path, i); | |
ded5034e LP |
2799 | |
2800 | if (offset) | |
2801 | *offset = ofs; | |
2802 | ||
2803 | return 1; | |
de190aef | 2804 | } |
cec736d2 | 2805 | |
cbdca852 LP |
2806 | int journal_file_move_to_entry_by_offset_for_data( |
2807 | JournalFile *f, | |
2808 | uint64_t data_offset, | |
2809 | uint64_t p, | |
2810 | direction_t direction, | |
2811 | Object **ret, uint64_t *offset) { | |
2812 | ||
2813 | int r; | |
2814 | Object *d; | |
2815 | ||
2816 | assert(f); | |
2817 | ||
2818 | r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d); | |
2819 | if (r < 0) | |
2820 | return r; | |
2821 | ||
2822 | return generic_array_bisect_plus_one(f, | |
2823 | le64toh(d->data.entry_offset), | |
2824 | le64toh(d->data.entry_array_offset), | |
2825 | le64toh(d->data.n_entries), | |
2826 | p, | |
2827 | test_object_offset, | |
2828 | direction, | |
2829 | ret, offset, NULL); | |
2830 | } | |
2831 | ||
2832 | int journal_file_move_to_entry_by_monotonic_for_data( | |
2833 | JournalFile *f, | |
2834 | uint64_t data_offset, | |
2835 | sd_id128_t boot_id, | |
2836 | uint64_t monotonic, | |
2837 | direction_t direction, | |
2838 | Object **ret, uint64_t *offset) { | |
2839 | ||
cbdca852 LP |
2840 | Object *o, *d; |
2841 | int r; | |
2842 | uint64_t b, z; | |
2843 | ||
2844 | assert(f); | |
2845 | ||
2846 | /* First, seek by time */ | |
47838ab3 | 2847 | r = find_data_object_by_boot_id(f, boot_id, &o, &b); |
cbdca852 LP |
2848 | if (r < 0) |
2849 | return r; | |
2850 | if (r == 0) | |
2851 | return -ENOENT; | |
2852 | ||
2853 | r = generic_array_bisect_plus_one(f, | |
2854 | le64toh(o->data.entry_offset), | |
2855 | le64toh(o->data.entry_array_offset), | |
2856 | le64toh(o->data.n_entries), | |
2857 | monotonic, | |
2858 | test_object_monotonic, | |
2859 | direction, | |
2860 | NULL, &z, NULL); | |
2861 | if (r <= 0) | |
2862 | return r; | |
2863 | ||
2864 | /* And now, continue seeking until we find an entry that | |
2865 | * exists in both bisection arrays */ | |
2866 | ||
2867 | for (;;) { | |
2868 | Object *qo; | |
2869 | uint64_t p, q; | |
2870 | ||
2871 | r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d); | |
2872 | if (r < 0) | |
2873 | return r; | |
2874 | ||
2875 | r = generic_array_bisect_plus_one(f, | |
2876 | le64toh(d->data.entry_offset), | |
2877 | le64toh(d->data.entry_array_offset), | |
2878 | le64toh(d->data.n_entries), | |
2879 | z, | |
2880 | test_object_offset, | |
2881 | direction, | |
2882 | NULL, &p, NULL); | |
2883 | if (r <= 0) | |
2884 | return r; | |
2885 | ||
2886 | r = journal_file_move_to_object(f, OBJECT_DATA, b, &o); | |
2887 | if (r < 0) | |
2888 | return r; | |
2889 | ||
2890 | r = generic_array_bisect_plus_one(f, | |
2891 | le64toh(o->data.entry_offset), | |
2892 | le64toh(o->data.entry_array_offset), | |
2893 | le64toh(o->data.n_entries), | |
2894 | p, | |
2895 | test_object_offset, | |
2896 | direction, | |
2897 | &qo, &q, NULL); | |
2898 | ||
2899 | if (r <= 0) | |
2900 | return r; | |
2901 | ||
2902 | if (p == q) { | |
2903 | if (ret) | |
2904 | *ret = qo; | |
2905 | if (offset) | |
2906 | *offset = q; | |
2907 | ||
2908 | return 1; | |
2909 | } | |
2910 | ||
2911 | z = q; | |
2912 | } | |
cbdca852 LP |
2913 | } |
2914 | ||
de190aef LP |
2915 | int journal_file_move_to_entry_by_seqnum_for_data( |
2916 | JournalFile *f, | |
2917 | uint64_t data_offset, | |
2918 | uint64_t seqnum, | |
2919 | direction_t direction, | |
2920 | Object **ret, uint64_t *offset) { | |
cec736d2 | 2921 | |
de190aef LP |
2922 | Object *d; |
2923 | int r; | |
cec736d2 | 2924 | |
91a31dde LP |
2925 | assert(f); |
2926 | ||
de190aef | 2927 | r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d); |
91a31dde | 2928 | if (r < 0) |
de190aef | 2929 | return r; |
cec736d2 | 2930 | |
de190aef LP |
2931 | return generic_array_bisect_plus_one(f, |
2932 | le64toh(d->data.entry_offset), | |
2933 | le64toh(d->data.entry_array_offset), | |
2934 | le64toh(d->data.n_entries), | |
2935 | seqnum, | |
2936 | test_object_seqnum, | |
2937 | direction, | |
2938 | ret, offset, NULL); | |
2939 | } | |
cec736d2 | 2940 | |
de190aef LP |
2941 | int journal_file_move_to_entry_by_realtime_for_data( |
2942 | JournalFile *f, | |
2943 | uint64_t data_offset, | |
2944 | uint64_t realtime, | |
2945 | direction_t direction, | |
2946 | Object **ret, uint64_t *offset) { | |
2947 | ||
2948 | Object *d; | |
2949 | int r; | |
2950 | ||
91a31dde LP |
2951 | assert(f); |
2952 | ||
de190aef | 2953 | r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d); |
91a31dde | 2954 | if (r < 0) |
de190aef LP |
2955 | return r; |
2956 | ||
2957 | return generic_array_bisect_plus_one(f, | |
2958 | le64toh(d->data.entry_offset), | |
2959 | le64toh(d->data.entry_array_offset), | |
2960 | le64toh(d->data.n_entries), | |
2961 | realtime, | |
2962 | test_object_realtime, | |
2963 | direction, | |
2964 | ret, offset, NULL); | |
cec736d2 LP |
2965 | } |
2966 | ||
0284adc6 | 2967 | void journal_file_dump(JournalFile *f) { |
7560fffc | 2968 | Object *o; |
7560fffc | 2969 | int r; |
0284adc6 | 2970 | uint64_t p; |
7560fffc LP |
2971 | |
2972 | assert(f); | |
c88cc6af | 2973 | assert(f->header); |
7560fffc | 2974 | |
0284adc6 | 2975 | journal_file_print_header(f); |
7560fffc | 2976 | |
0284adc6 LP |
2977 | p = le64toh(f->header->header_size); |
2978 | while (p != 0) { | |
d05089d8 | 2979 | r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o); |
0284adc6 LP |
2980 | if (r < 0) |
2981 | goto fail; | |
7560fffc | 2982 | |
0284adc6 | 2983 | switch (o->object.type) { |
d98cc1f2 | 2984 | |
0284adc6 LP |
2985 | case OBJECT_UNUSED: |
2986 | printf("Type: OBJECT_UNUSED\n"); | |
2987 | break; | |
d98cc1f2 | 2988 | |
0284adc6 LP |
2989 | case OBJECT_DATA: |
2990 | printf("Type: OBJECT_DATA\n"); | |
2991 | break; | |
7560fffc | 2992 | |
3c1668da LP |
2993 | case OBJECT_FIELD: |
2994 | printf("Type: OBJECT_FIELD\n"); | |
2995 | break; | |
2996 | ||
0284adc6 | 2997 | case OBJECT_ENTRY: |
507f22bd ZJS |
2998 | printf("Type: OBJECT_ENTRY seqnum=%"PRIu64" monotonic=%"PRIu64" realtime=%"PRIu64"\n", |
2999 | le64toh(o->entry.seqnum), | |
3000 | le64toh(o->entry.monotonic), | |
3001 | le64toh(o->entry.realtime)); | |
0284adc6 | 3002 | break; |
7560fffc | 3003 | |
0284adc6 LP |
3004 | case OBJECT_FIELD_HASH_TABLE: |
3005 | printf("Type: OBJECT_FIELD_HASH_TABLE\n"); | |
3006 | break; | |
7560fffc | 3007 | |
0284adc6 LP |
3008 | case OBJECT_DATA_HASH_TABLE: |
3009 | printf("Type: OBJECT_DATA_HASH_TABLE\n"); | |
3010 | break; | |
7560fffc | 3011 | |
0284adc6 LP |
3012 | case OBJECT_ENTRY_ARRAY: |
3013 | printf("Type: OBJECT_ENTRY_ARRAY\n"); | |
3014 | break; | |
7560fffc | 3015 | |
0284adc6 | 3016 | case OBJECT_TAG: |
507f22bd ZJS |
3017 | printf("Type: OBJECT_TAG seqnum=%"PRIu64" epoch=%"PRIu64"\n", |
3018 | le64toh(o->tag.seqnum), | |
3019 | le64toh(o->tag.epoch)); | |
0284adc6 | 3020 | break; |
3c1668da LP |
3021 | |
3022 | default: | |
8facc349 | 3023 | printf("Type: unknown (%i)\n", o->object.type); |
3c1668da | 3024 | break; |
0284adc6 | 3025 | } |
7560fffc | 3026 | |
d89c8fdf ZJS |
3027 | if (o->object.flags & OBJECT_COMPRESSION_MASK) |
3028 | printf("Flags: %s\n", | |
3029 | object_compressed_to_string(o->object.flags & OBJECT_COMPRESSION_MASK)); | |
7560fffc | 3030 | |
0284adc6 LP |
3031 | if (p == le64toh(f->header->tail_object_offset)) |
3032 | p = 0; | |
3033 | else | |
3034 | p = p + ALIGN64(le64toh(o->object.size)); | |
3035 | } | |
7560fffc | 3036 | |
0284adc6 LP |
3037 | return; |
3038 | fail: | |
3039 | log_error("File corrupt"); | |
7560fffc LP |
3040 | } |
3041 | ||
718fe4b1 ZJS |
3042 | static const char* format_timestamp_safe(char *buf, size_t l, usec_t t) { |
3043 | const char *x; | |
3044 | ||
3045 | x = format_timestamp(buf, l, t); | |
3046 | if (x) | |
3047 | return x; | |
3048 | return " --- "; | |
3049 | } | |
3050 | ||
0284adc6 | 3051 | void journal_file_print_header(JournalFile *f) { |
2765b7bb | 3052 | char a[33], b[33], c[33], d[33]; |
ed375beb | 3053 | char x[FORMAT_TIMESTAMP_MAX], y[FORMAT_TIMESTAMP_MAX], z[FORMAT_TIMESTAMP_MAX]; |
a1a03e30 LP |
3054 | struct stat st; |
3055 | char bytes[FORMAT_BYTES_MAX]; | |
7560fffc LP |
3056 | |
3057 | assert(f); | |
c88cc6af | 3058 | assert(f->header); |
7560fffc | 3059 | |
0284adc6 LP |
3060 | printf("File Path: %s\n" |
3061 | "File ID: %s\n" | |
3062 | "Machine ID: %s\n" | |
3063 | "Boot ID: %s\n" | |
3064 | "Sequential Number ID: %s\n" | |
3065 | "State: %s\n" | |
3066 | "Compatible Flags:%s%s\n" | |
d89c8fdf | 3067 | "Incompatible Flags:%s%s%s\n" |
507f22bd ZJS |
3068 | "Header size: %"PRIu64"\n" |
3069 | "Arena size: %"PRIu64"\n" | |
3070 | "Data Hash Table Size: %"PRIu64"\n" | |
3071 | "Field Hash Table Size: %"PRIu64"\n" | |
0284adc6 | 3072 | "Rotate Suggested: %s\n" |
0808b92f LP |
3073 | "Head Sequential Number: %"PRIu64" (%"PRIx64")\n" |
3074 | "Tail Sequential Number: %"PRIu64" (%"PRIx64")\n" | |
3075 | "Head Realtime Timestamp: %s (%"PRIx64")\n" | |
3076 | "Tail Realtime Timestamp: %s (%"PRIx64")\n" | |
3077 | "Tail Monotonic Timestamp: %s (%"PRIx64")\n" | |
507f22bd ZJS |
3078 | "Objects: %"PRIu64"\n" |
3079 | "Entry Objects: %"PRIu64"\n", | |
0284adc6 LP |
3080 | f->path, |
3081 | sd_id128_to_string(f->header->file_id, a), | |
3082 | sd_id128_to_string(f->header->machine_id, b), | |
3083 | sd_id128_to_string(f->header->boot_id, c), | |
2765b7bb | 3084 | sd_id128_to_string(f->header->seqnum_id, d), |
3223f44f LP |
3085 | f->header->state == STATE_OFFLINE ? "OFFLINE" : |
3086 | f->header->state == STATE_ONLINE ? "ONLINE" : | |
3087 | f->header->state == STATE_ARCHIVED ? "ARCHIVED" : "UNKNOWN", | |
8088cbd3 | 3088 | JOURNAL_HEADER_SEALED(f->header) ? " SEALED" : "", |
d89c8fdf ZJS |
3089 | (le32toh(f->header->compatible_flags) & ~HEADER_COMPATIBLE_ANY) ? " ???" : "", |
3090 | JOURNAL_HEADER_COMPRESSED_XZ(f->header) ? " COMPRESSED-XZ" : "", | |
3091 | JOURNAL_HEADER_COMPRESSED_LZ4(f->header) ? " COMPRESSED-LZ4" : "", | |
3092 | (le32toh(f->header->incompatible_flags) & ~HEADER_INCOMPATIBLE_ANY) ? " ???" : "", | |
507f22bd ZJS |
3093 | le64toh(f->header->header_size), |
3094 | le64toh(f->header->arena_size), | |
3095 | le64toh(f->header->data_hash_table_size) / sizeof(HashItem), | |
3096 | le64toh(f->header->field_hash_table_size) / sizeof(HashItem), | |
fb0951b0 | 3097 | yes_no(journal_file_rotate_suggested(f, 0)), |
0808b92f LP |
3098 | le64toh(f->header->head_entry_seqnum), le64toh(f->header->head_entry_seqnum), |
3099 | le64toh(f->header->tail_entry_seqnum), le64toh(f->header->tail_entry_seqnum), | |
3100 | format_timestamp_safe(x, sizeof(x), le64toh(f->header->head_entry_realtime)), le64toh(f->header->head_entry_realtime), | |
3101 | format_timestamp_safe(y, sizeof(y), le64toh(f->header->tail_entry_realtime)), le64toh(f->header->tail_entry_realtime), | |
3102 | format_timespan(z, sizeof(z), le64toh(f->header->tail_entry_monotonic), USEC_PER_MSEC), le64toh(f->header->tail_entry_monotonic), | |
507f22bd ZJS |
3103 | le64toh(f->header->n_objects), |
3104 | le64toh(f->header->n_entries)); | |
7560fffc | 3105 | |
0284adc6 | 3106 | if (JOURNAL_HEADER_CONTAINS(f->header, n_data)) |
507f22bd | 3107 | printf("Data Objects: %"PRIu64"\n" |
0284adc6 | 3108 | "Data Hash Table Fill: %.1f%%\n", |
507f22bd | 3109 | le64toh(f->header->n_data), |
0284adc6 | 3110 | 100.0 * (double) le64toh(f->header->n_data) / ((double) (le64toh(f->header->data_hash_table_size) / sizeof(HashItem)))); |
7560fffc | 3111 | |
0284adc6 | 3112 | if (JOURNAL_HEADER_CONTAINS(f->header, n_fields)) |
507f22bd | 3113 | printf("Field Objects: %"PRIu64"\n" |
0284adc6 | 3114 | "Field Hash Table Fill: %.1f%%\n", |
507f22bd | 3115 | le64toh(f->header->n_fields), |
0284adc6 | 3116 | 100.0 * (double) le64toh(f->header->n_fields) / ((double) (le64toh(f->header->field_hash_table_size) / sizeof(HashItem)))); |
3223f44f LP |
3117 | |
3118 | if (JOURNAL_HEADER_CONTAINS(f->header, n_tags)) | |
507f22bd ZJS |
3119 | printf("Tag Objects: %"PRIu64"\n", |
3120 | le64toh(f->header->n_tags)); | |
3223f44f | 3121 | if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays)) |
507f22bd ZJS |
3122 | printf("Entry Array Objects: %"PRIu64"\n", |
3123 | le64toh(f->header->n_entry_arrays)); | |
a1a03e30 LP |
3124 | |
3125 | if (fstat(f->fd, &st) >= 0) | |
59f448cf | 3126 | printf("Disk usage: %s\n", format_bytes(bytes, sizeof(bytes), (uint64_t) st.st_blocks * 512ULL)); |
7560fffc LP |
3127 | } |
3128 | ||
fc68c929 LP |
3129 | static int journal_file_warn_btrfs(JournalFile *f) { |
3130 | unsigned attrs; | |
3131 | int r; | |
3132 | ||
3133 | assert(f); | |
3134 | ||
3135 | /* Before we write anything, check if the COW logic is turned | |
3136 | * off on btrfs. Given our write pattern that is quite | |
3137 | * unfriendly to COW file systems this should greatly improve | |
3138 | * performance on COW file systems, such as btrfs, at the | |
3139 | * expense of data integrity features (which shouldn't be too | |
3140 | * bad, given that we do our own checksumming). */ | |
3141 | ||
3142 | r = btrfs_is_filesystem(f->fd); | |
3143 | if (r < 0) | |
3144 | return log_warning_errno(r, "Failed to determine if journal is on btrfs: %m"); | |
3145 | if (!r) | |
3146 | return 0; | |
3147 | ||
3148 | r = read_attr_fd(f->fd, &attrs); | |
3149 | if (r < 0) | |
3150 | return log_warning_errno(r, "Failed to read file attributes: %m"); | |
3151 | ||
3152 | if (attrs & FS_NOCOW_FL) { | |
3153 | log_debug("Detected btrfs file system with copy-on-write disabled, all is good."); | |
3154 | return 0; | |
3155 | } | |
3156 | ||
3157 | log_notice("Creating journal file %s on a btrfs file system, and copy-on-write is enabled. " | |
3158 | "This is likely to slow down journal access substantially, please consider turning " | |
3159 | "off the copy-on-write file attribute on the journal directory, using chattr +C.", f->path); | |
3160 | ||
3161 | return 1; | |
3162 | } | |
3163 | ||
0284adc6 | 3164 | int journal_file_open( |
5d1ce257 | 3165 | int fd, |
0284adc6 LP |
3166 | const char *fname, |
3167 | int flags, | |
3168 | mode_t mode, | |
3169 | bool compress, | |
57850536 | 3170 | uint64_t compress_threshold_bytes, |
baed47c3 | 3171 | bool seal, |
0284adc6 LP |
3172 | JournalMetrics *metrics, |
3173 | MMapCache *mmap_cache, | |
b58c888f | 3174 | Set *deferred_closes, |
0284adc6 LP |
3175 | JournalFile *template, |
3176 | JournalFile **ret) { | |
7560fffc | 3177 | |
fa6ac760 | 3178 | bool newly_created = false; |
0284adc6 | 3179 | JournalFile *f; |
fa6ac760 | 3180 | void *h; |
0284adc6 | 3181 | int r; |
57850536 | 3182 | char bytes[FORMAT_BYTES_MAX]; |
7560fffc | 3183 | |
0559d3a5 | 3184 | assert(ret); |
5d1ce257 | 3185 | assert(fd >= 0 || fname); |
7560fffc | 3186 | |
ec2ce0c5 | 3187 | if (!IN_SET((flags & O_ACCMODE), O_RDONLY, O_RDWR)) |
0284adc6 | 3188 | return -EINVAL; |
7560fffc | 3189 | |
6eda13d3 LP |
3190 | if (fname && (flags & O_CREAT) && !endswith(fname, ".journal")) |
3191 | return -EINVAL; | |
7560fffc | 3192 | |
971b52c4 | 3193 | f = new(JournalFile, 1); |
0284adc6 LP |
3194 | if (!f) |
3195 | return -ENOMEM; | |
7560fffc | 3196 | |
971b52c4 LP |
3197 | *f = (JournalFile) { |
3198 | .fd = fd, | |
3199 | .mode = mode, | |
3200 | ||
3201 | .flags = flags, | |
3202 | .prot = prot_from_flags(flags), | |
3203 | .writable = (flags & O_ACCMODE) != O_RDONLY, | |
7560fffc | 3204 | |
349cc4a5 | 3205 | #if HAVE_LZ4 |
971b52c4 | 3206 | .compress_lz4 = compress, |
349cc4a5 | 3207 | #elif HAVE_XZ |
971b52c4 | 3208 | .compress_xz = compress, |
48b61739 | 3209 | #endif |
971b52c4 LP |
3210 | .compress_threshold_bytes = compress_threshold_bytes == (uint64_t) -1 ? |
3211 | DEFAULT_COMPRESS_THRESHOLD : | |
3212 | MAX(MIN_COMPRESS_THRESHOLD, compress_threshold_bytes), | |
349cc4a5 | 3213 | #if HAVE_GCRYPT |
971b52c4 | 3214 | .seal = seal, |
49a32d43 | 3215 | #endif |
971b52c4 | 3216 | }; |
7560fffc | 3217 | |
57850536 AG |
3218 | log_debug("Journal effective settings seal=%s compress=%s compress_threshold_bytes=%s", |
3219 | yes_no(f->seal), yes_no(JOURNAL_FILE_COMPRESS(f)), | |
3220 | format_bytes(bytes, sizeof(bytes), f->compress_threshold_bytes)); | |
3221 | ||
0284adc6 LP |
3222 | if (mmap_cache) |
3223 | f->mmap = mmap_cache_ref(mmap_cache); | |
3224 | else { | |
84168d80 | 3225 | f->mmap = mmap_cache_new(); |
0284adc6 LP |
3226 | if (!f->mmap) { |
3227 | r = -ENOMEM; | |
3228 | goto fail; | |
3229 | } | |
3230 | } | |
7560fffc | 3231 | |
7645c77b | 3232 | if (fname) { |
5d1ce257 | 3233 | f->path = strdup(fname); |
7645c77b ZJS |
3234 | if (!f->path) { |
3235 | r = -ENOMEM; | |
3236 | goto fail; | |
3237 | } | |
3238 | } else { | |
817b1c5b LP |
3239 | assert(fd >= 0); |
3240 | ||
7645c77b ZJS |
3241 | /* If we don't know the path, fill in something explanatory and vaguely useful */ |
3242 | if (asprintf(&f->path, "/proc/self/%i", fd) < 0) { | |
3243 | r = -ENOMEM; | |
3244 | goto fail; | |
3245 | } | |
0284adc6 | 3246 | } |
7560fffc | 3247 | |
4743015d | 3248 | f->chain_cache = ordered_hashmap_new(&uint64_hash_ops); |
a4bcff5b LP |
3249 | if (!f->chain_cache) { |
3250 | r = -ENOMEM; | |
3251 | goto fail; | |
3252 | } | |
3253 | ||
0284adc6 | 3254 | if (f->fd < 0) { |
817b1c5b LP |
3255 | /* We pass O_NONBLOCK here, so that in case somebody pointed us to some character device node or FIFO |
3256 | * or so, we likely fail quickly than block for long. For regular files O_NONBLOCK has no effect, hence | |
3257 | * it doesn't hurt in that case. */ | |
3258 | ||
3259 | f->fd = open(f->path, f->flags|O_CLOEXEC|O_NONBLOCK, f->mode); | |
5d1ce257 LP |
3260 | if (f->fd < 0) { |
3261 | r = -errno; | |
3262 | goto fail; | |
3263 | } | |
3264 | ||
3265 | /* fds we opened here by us should also be closed by us. */ | |
3266 | f->close_fd = true; | |
817b1c5b LP |
3267 | |
3268 | r = fd_nonblock(f->fd, false); | |
3269 | if (r < 0) | |
3270 | goto fail; | |
7560fffc | 3271 | } |
7560fffc | 3272 | |
be7cdd8e VC |
3273 | f->cache_fd = mmap_cache_add_fd(f->mmap, f->fd); |
3274 | if (!f->cache_fd) { | |
3275 | r = -ENOMEM; | |
3276 | goto fail; | |
3277 | } | |
3278 | ||
2678031a LP |
3279 | r = journal_file_fstat(f); |
3280 | if (r < 0) | |
0284adc6 | 3281 | goto fail; |
7560fffc | 3282 | |
0284adc6 | 3283 | if (f->last_stat.st_size == 0 && f->writable) { |
11689d2a | 3284 | |
fc68c929 | 3285 | (void) journal_file_warn_btrfs(f); |
11689d2a | 3286 | |
4c2e1b39 LP |
3287 | /* Let's attach the creation time to the journal file, so that the vacuuming code knows the age of this |
3288 | * file even if the file might end up corrupted one day... Ideally we'd just use the creation time many | |
3289 | * file systems maintain for each file, but the API to query this is very new, hence let's emulate this | |
3290 | * via extended attributes. If extended attributes are not supported we'll just skip this, and rely | |
3291 | * solely on mtime/atime/ctime of the file. */ | |
3292 | (void) fd_setcrtime(f->fd, 0); | |
7560fffc | 3293 | |
349cc4a5 | 3294 | #if HAVE_GCRYPT |
0284adc6 | 3295 | /* Try to load the FSPRG state, and if we can't, then |
baed47c3 | 3296 | * just don't do sealing */ |
49a32d43 LP |
3297 | if (f->seal) { |
3298 | r = journal_file_fss_load(f); | |
3299 | if (r < 0) | |
3300 | f->seal = false; | |
3301 | } | |
feb12d3e | 3302 | #endif |
7560fffc | 3303 | |
0284adc6 LP |
3304 | r = journal_file_init_header(f, template); |
3305 | if (r < 0) | |
3306 | goto fail; | |
7560fffc | 3307 | |
2678031a LP |
3308 | r = journal_file_fstat(f); |
3309 | if (r < 0) | |
0284adc6 | 3310 | goto fail; |
fb0951b0 LP |
3311 | |
3312 | newly_created = true; | |
0284adc6 | 3313 | } |
7560fffc | 3314 | |
0284adc6 | 3315 | if (f->last_stat.st_size < (off_t) HEADER_SIZE_MIN) { |
cfb571f3 | 3316 | r = -ENODATA; |
0284adc6 LP |
3317 | goto fail; |
3318 | } | |
7560fffc | 3319 | |
b42549ad | 3320 | r = mmap_cache_get(f->mmap, f->cache_fd, f->prot, CONTEXT_HEADER, true, 0, PAGE_ALIGN(sizeof(Header)), &f->last_stat, &h, NULL); |
977eaa1e | 3321 | if (r < 0) |
0284adc6 | 3322 | goto fail; |
7560fffc | 3323 | |
fa6ac760 LP |
3324 | f->header = h; |
3325 | ||
0284adc6 | 3326 | if (!newly_created) { |
f9168190 | 3327 | set_clear_with_destructor(deferred_closes, journal_file_close); |
b58c888f | 3328 | |
0284adc6 LP |
3329 | r = journal_file_verify_header(f); |
3330 | if (r < 0) | |
3331 | goto fail; | |
3332 | } | |
7560fffc | 3333 | |
349cc4a5 | 3334 | #if HAVE_GCRYPT |
0284adc6 | 3335 | if (!newly_created && f->writable) { |
baed47c3 | 3336 | r = journal_file_fss_load(f); |
0284adc6 LP |
3337 | if (r < 0) |
3338 | goto fail; | |
3339 | } | |
feb12d3e | 3340 | #endif |
cec736d2 LP |
3341 | |
3342 | if (f->writable) { | |
4a92baf3 LP |
3343 | if (metrics) { |
3344 | journal_default_metrics(metrics, f->fd); | |
3345 | f->metrics = *metrics; | |
3346 | } else if (template) | |
3347 | f->metrics = template->metrics; | |
3348 | ||
cec736d2 LP |
3349 | r = journal_file_refresh_header(f); |
3350 | if (r < 0) | |
3351 | goto fail; | |
3352 | } | |
3353 | ||
349cc4a5 | 3354 | #if HAVE_GCRYPT |
baed47c3 | 3355 | r = journal_file_hmac_setup(f); |
14d10188 LP |
3356 | if (r < 0) |
3357 | goto fail; | |
feb12d3e | 3358 | #endif |
14d10188 | 3359 | |
cec736d2 | 3360 | if (newly_created) { |
de190aef | 3361 | r = journal_file_setup_field_hash_table(f); |
cec736d2 LP |
3362 | if (r < 0) |
3363 | goto fail; | |
3364 | ||
de190aef | 3365 | r = journal_file_setup_data_hash_table(f); |
cec736d2 LP |
3366 | if (r < 0) |
3367 | goto fail; | |
7560fffc | 3368 | |
349cc4a5 | 3369 | #if HAVE_GCRYPT |
7560fffc LP |
3370 | r = journal_file_append_first_tag(f); |
3371 | if (r < 0) | |
3372 | goto fail; | |
feb12d3e | 3373 | #endif |
cec736d2 LP |
3374 | } |
3375 | ||
be7cdd8e | 3376 | if (mmap_cache_got_sigbus(f->mmap, f->cache_fd)) { |
fa6ac760 LP |
3377 | r = -EIO; |
3378 | goto fail; | |
3379 | } | |
3380 | ||
7a24f3bf | 3381 | if (template && template->post_change_timer) { |
e167d7fd LP |
3382 | r = journal_file_enable_post_change_timer( |
3383 | f, | |
3384 | sd_event_source_get_event(template->post_change_timer), | |
3385 | template->post_change_timer_period); | |
7a24f3bf | 3386 | |
7a24f3bf VC |
3387 | if (r < 0) |
3388 | goto fail; | |
3389 | } | |
3390 | ||
f8e2f4d6 | 3391 | /* The file is opened now successfully, thus we take possession of any passed in fd. */ |
5d1ce257 LP |
3392 | f->close_fd = true; |
3393 | ||
0559d3a5 | 3394 | *ret = f; |
cec736d2 LP |
3395 | return 0; |
3396 | ||
3397 | fail: | |
be7cdd8e | 3398 | if (f->cache_fd && mmap_cache_got_sigbus(f->mmap, f->cache_fd)) |
fa6ac760 LP |
3399 | r = -EIO; |
3400 | ||
69a3a6fd | 3401 | (void) journal_file_close(f); |
cec736d2 LP |
3402 | |
3403 | return r; | |
3404 | } | |
0ac38b70 | 3405 | |
7a4d21ad | 3406 | int journal_file_archive(JournalFile *f) { |
57535f47 | 3407 | _cleanup_free_ char *p = NULL; |
0ac38b70 LP |
3408 | |
3409 | assert(f); | |
0ac38b70 | 3410 | |
7a4d21ad | 3411 | if (!f->writable) |
0ac38b70 LP |
3412 | return -EINVAL; |
3413 | ||
5d1ce257 | 3414 | /* Is this a journal file that was passed to us as fd? If so, we synthesized a path name for it, and we refuse |
13e785f7 | 3415 | * rotation, since we don't know the actual path, and couldn't rename the file hence. */ |
7a4d21ad | 3416 | if (path_startswith(f->path, "/proc/self/fd")) |
5d1ce257 LP |
3417 | return -EINVAL; |
3418 | ||
7a4d21ad | 3419 | if (!endswith(f->path, ".journal")) |
0ac38b70 LP |
3420 | return -EINVAL; |
3421 | ||
7a4d21ad LP |
3422 | if (asprintf(&p, "%.*s@" SD_ID128_FORMAT_STR "-%016"PRIx64"-%016"PRIx64".journal", |
3423 | (int) strlen(f->path) - 8, f->path, | |
3424 | SD_ID128_FORMAT_VAL(f->header->seqnum_id), | |
3425 | le64toh(f->header->head_entry_seqnum), | |
3426 | le64toh(f->header->head_entry_realtime)) < 0) | |
0ac38b70 LP |
3427 | return -ENOMEM; |
3428 | ||
7a4d21ad LP |
3429 | /* Try to rename the file to the archived version. If the file already was deleted, we'll get ENOENT, let's |
3430 | * ignore that case. */ | |
3431 | if (rename(f->path, p) < 0 && errno != ENOENT) | |
0ac38b70 LP |
3432 | return -errno; |
3433 | ||
1fcefd88 | 3434 | /* Sync the rename to disk */ |
7a4d21ad LP |
3435 | (void) fsync_directory_of_file(f->fd); |
3436 | ||
3437 | /* Set as archive so offlining commits w/state=STATE_ARCHIVED. Previously we would set old_file->header->state | |
3438 | * to STATE_ARCHIVED directly here, but journal_file_set_offline() short-circuits when state != STATE_ONLINE, | |
3439 | * which would result in the rotated journal never getting fsync() called before closing. Now we simply queue | |
3440 | * the archive state by setting an archive bit, leaving the state as STATE_ONLINE so proper offlining | |
3441 | * occurs. */ | |
3442 | f->archive = true; | |
3443 | ||
3444 | /* Currently, btrfs is not very good with out write patterns and fragments heavily. Let's defrag our journal | |
3445 | * files when we archive them */ | |
3446 | f->defrag_on_close = true; | |
3447 | ||
3448 | return 0; | |
3449 | } | |
3450 | ||
3451 | JournalFile* journal_initiate_close( | |
3452 | JournalFile *f, | |
3453 | Set *deferred_closes) { | |
3454 | ||
3455 | int r; | |
3456 | ||
3457 | assert(f); | |
3458 | ||
3459 | if (deferred_closes) { | |
0ac38b70 | 3460 | |
7a4d21ad LP |
3461 | r = set_put(deferred_closes, f); |
3462 | if (r < 0) | |
3463 | log_debug_errno(r, "Failed to add file to deferred close set, closing immediately."); | |
3464 | else { | |
3465 | (void) journal_file_set_offline(f, false); | |
3466 | return NULL; | |
3467 | } | |
3468 | } | |
3469 | ||
3470 | return journal_file_close(f); | |
3471 | } | |
3472 | ||
3473 | int journal_file_rotate( | |
3474 | JournalFile **f, | |
3475 | bool compress, | |
3476 | uint64_t compress_threshold_bytes, | |
3477 | bool seal, | |
3478 | Set *deferred_closes) { | |
3479 | ||
3480 | JournalFile *new_file = NULL; | |
3481 | int r; | |
3482 | ||
3483 | assert(f); | |
3484 | assert(*f); | |
3485 | ||
3486 | r = journal_file_archive(*f); | |
3487 | if (r < 0) | |
3488 | return r; | |
3489 | ||
3490 | r = journal_file_open( | |
3491 | -1, | |
3492 | (*f)->path, | |
3493 | (*f)->flags, | |
3494 | (*f)->mode, | |
3495 | compress, | |
3496 | compress_threshold_bytes, | |
3497 | seal, | |
3498 | NULL, /* metrics */ | |
3499 | (*f)->mmap, | |
3500 | deferred_closes, | |
3501 | *f, /* template */ | |
3502 | &new_file); | |
3503 | ||
3504 | journal_initiate_close(*f, deferred_closes); | |
0ac38b70 | 3505 | *f = new_file; |
7a4d21ad | 3506 | |
0ac38b70 LP |
3507 | return r; |
3508 | } | |
3509 | ||
68127658 LP |
3510 | int journal_file_dispose(int dir_fd, const char *fname) { |
3511 | _cleanup_free_ char *p = NULL; | |
3512 | _cleanup_close_ int fd = -1; | |
3513 | ||
3514 | assert(fname); | |
3515 | ||
3516 | /* Renames a journal file to *.journal~, i.e. to mark it as corruped or otherwise uncleanly shutdown. Note that | |
3517 | * this is done without looking into the file or changing any of its contents. The idea is that this is called | |
3518 | * whenever something is suspicious and we want to move the file away and make clear that it is not accessed | |
3519 | * for writing anymore. */ | |
3520 | ||
3521 | if (!endswith(fname, ".journal")) | |
3522 | return -EINVAL; | |
3523 | ||
3524 | if (asprintf(&p, "%.*s@%016" PRIx64 "-%016" PRIx64 ".journal~", | |
3525 | (int) strlen(fname) - 8, fname, | |
3526 | now(CLOCK_REALTIME), | |
3527 | random_u64()) < 0) | |
3528 | return -ENOMEM; | |
3529 | ||
3530 | if (renameat(dir_fd, fname, dir_fd, p) < 0) | |
3531 | return -errno; | |
3532 | ||
3533 | /* btrfs doesn't cope well with our write pattern and fragments heavily. Let's defrag all files we rotate */ | |
3534 | fd = openat(dir_fd, p, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW); | |
3535 | if (fd < 0) | |
3536 | log_debug_errno(errno, "Failed to open file for defragmentation/FS_NOCOW_FL, ignoring: %m"); | |
3537 | else { | |
3538 | (void) chattr_fd(fd, 0, FS_NOCOW_FL, NULL); | |
3539 | (void) btrfs_defrag_fd(fd); | |
3540 | } | |
3541 | ||
3542 | return 0; | |
3543 | } | |
3544 | ||
9447a7f1 LP |
3545 | int journal_file_open_reliably( |
3546 | const char *fname, | |
3547 | int flags, | |
3548 | mode_t mode, | |
7560fffc | 3549 | bool compress, |
57850536 | 3550 | uint64_t compress_threshold_bytes, |
baed47c3 | 3551 | bool seal, |
4a92baf3 | 3552 | JournalMetrics *metrics, |
27370278 | 3553 | MMapCache *mmap_cache, |
b58c888f | 3554 | Set *deferred_closes, |
9447a7f1 LP |
3555 | JournalFile *template, |
3556 | JournalFile **ret) { | |
3557 | ||
68127658 | 3558 | int r; |
9447a7f1 | 3559 | |
57850536 AG |
3560 | r = journal_file_open(-1, fname, flags, mode, compress, compress_threshold_bytes, seal, metrics, mmap_cache, |
3561 | deferred_closes, template, ret); | |
288359db | 3562 | if (!IN_SET(r, |
b288cdeb ZJS |
3563 | -EBADMSG, /* Corrupted */ |
3564 | -ENODATA, /* Truncated */ | |
3565 | -EHOSTDOWN, /* Other machine */ | |
3566 | -EPROTONOSUPPORT, /* Incompatible feature */ | |
3567 | -EBUSY, /* Unclean shutdown */ | |
3568 | -ESHUTDOWN, /* Already archived */ | |
288359db | 3569 | -EIO, /* IO error, including SIGBUS on mmap */ |
ae739cc1 LP |
3570 | -EIDRM, /* File has been deleted */ |
3571 | -ETXTBSY)) /* File is from the future */ | |
9447a7f1 LP |
3572 | return r; |
3573 | ||
3574 | if ((flags & O_ACCMODE) == O_RDONLY) | |
3575 | return r; | |
3576 | ||
3577 | if (!(flags & O_CREAT)) | |
3578 | return r; | |
3579 | ||
7560fffc LP |
3580 | if (!endswith(fname, ".journal")) |
3581 | return r; | |
3582 | ||
5c70eab4 | 3583 | /* The file is corrupted. Rotate it away and try it again (but only once) */ |
65089b82 | 3584 | log_warning_errno(r, "File %s corrupted or uncleanly shut down, renaming and replacing.", fname); |
9447a7f1 | 3585 | |
68127658 LP |
3586 | r = journal_file_dispose(AT_FDCWD, fname); |
3587 | if (r < 0) | |
3588 | return r; | |
3589 | ||
57850536 AG |
3590 | return journal_file_open(-1, fname, flags, mode, compress, compress_threshold_bytes, seal, metrics, mmap_cache, |
3591 | deferred_closes, template, ret); | |
9447a7f1 LP |
3592 | } |
3593 | ||
5a271b08 | 3594 | int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint64_t p) { |
cf244689 LP |
3595 | uint64_t i, n; |
3596 | uint64_t q, xor_hash = 0; | |
3597 | int r; | |
3598 | EntryItem *items; | |
3599 | dual_timestamp ts; | |
d180c349 | 3600 | const sd_id128_t *boot_id; |
cf244689 LP |
3601 | |
3602 | assert(from); | |
3603 | assert(to); | |
3604 | assert(o); | |
3605 | assert(p); | |
3606 | ||
3607 | if (!to->writable) | |
3608 | return -EPERM; | |
3609 | ||
3610 | ts.monotonic = le64toh(o->entry.monotonic); | |
3611 | ts.realtime = le64toh(o->entry.realtime); | |
d180c349 | 3612 | boot_id = &o->entry.boot_id; |
cf244689 | 3613 | |
cf244689 | 3614 | n = journal_file_entry_n_items(o); |
4faa7004 | 3615 | /* alloca() can't take 0, hence let's allocate at least one */ |
cf409d15 | 3616 | items = newa(EntryItem, MAX(1u, n)); |
cf244689 LP |
3617 | |
3618 | for (i = 0; i < n; i++) { | |
4fd052ae FC |
3619 | uint64_t l, h; |
3620 | le64_t le_hash; | |
cf244689 LP |
3621 | size_t t; |
3622 | void *data; | |
3623 | Object *u; | |
3624 | ||
3625 | q = le64toh(o->entry.items[i].object_offset); | |
3626 | le_hash = o->entry.items[i].hash; | |
3627 | ||
3628 | r = journal_file_move_to_object(from, OBJECT_DATA, q, &o); | |
3629 | if (r < 0) | |
3630 | return r; | |
3631 | ||
3632 | if (le_hash != o->data.hash) | |
3633 | return -EBADMSG; | |
3634 | ||
3635 | l = le64toh(o->object.size) - offsetof(Object, data.payload); | |
3636 | t = (size_t) l; | |
3637 | ||
3638 | /* We hit the limit on 32bit machines */ | |
3639 | if ((uint64_t) t != l) | |
3640 | return -E2BIG; | |
3641 | ||
d89c8fdf | 3642 | if (o->object.flags & OBJECT_COMPRESSION_MASK) { |
349cc4a5 | 3643 | #if HAVE_XZ || HAVE_LZ4 |
a7f7d1bd | 3644 | size_t rsize = 0; |
cf244689 | 3645 | |
d89c8fdf ZJS |
3646 | r = decompress_blob(o->object.flags & OBJECT_COMPRESSION_MASK, |
3647 | o->data.payload, l, &from->compress_buffer, &from->compress_buffer_size, &rsize, 0); | |
3648 | if (r < 0) | |
3649 | return r; | |
cf244689 LP |
3650 | |
3651 | data = from->compress_buffer; | |
3652 | l = rsize; | |
3b1a55e1 ZJS |
3653 | #else |
3654 | return -EPROTONOSUPPORT; | |
3655 | #endif | |
cf244689 LP |
3656 | } else |
3657 | data = o->data.payload; | |
3658 | ||
3659 | r = journal_file_append_data(to, data, l, &u, &h); | |
3660 | if (r < 0) | |
3661 | return r; | |
3662 | ||
3663 | xor_hash ^= le64toh(u->data.hash); | |
3664 | items[i].object_offset = htole64(h); | |
3665 | items[i].hash = u->data.hash; | |
3666 | ||
3667 | r = journal_file_move_to_object(from, OBJECT_ENTRY, p, &o); | |
3668 | if (r < 0) | |
3669 | return r; | |
3670 | } | |
3671 | ||
d180c349 ZJS |
3672 | r = journal_file_append_entry_internal(to, &ts, boot_id, xor_hash, items, n, |
3673 | NULL, NULL, NULL); | |
fa6ac760 | 3674 | |
be7cdd8e | 3675 | if (mmap_cache_got_sigbus(to->mmap, to->cache_fd)) |
fa6ac760 LP |
3676 | return -EIO; |
3677 | ||
3678 | return r; | |
cf244689 | 3679 | } |
babfc091 | 3680 | |
8580d1f7 LP |
3681 | void journal_reset_metrics(JournalMetrics *m) { |
3682 | assert(m); | |
3683 | ||
3684 | /* Set everything to "pick automatic values". */ | |
3685 | ||
3686 | *m = (JournalMetrics) { | |
3687 | .min_use = (uint64_t) -1, | |
3688 | .max_use = (uint64_t) -1, | |
3689 | .min_size = (uint64_t) -1, | |
3690 | .max_size = (uint64_t) -1, | |
3691 | .keep_free = (uint64_t) -1, | |
3692 | .n_max_files = (uint64_t) -1, | |
3693 | }; | |
3694 | } | |
3695 | ||
babfc091 | 3696 | void journal_default_metrics(JournalMetrics *m, int fd) { |
8580d1f7 | 3697 | char a[FORMAT_BYTES_MAX], b[FORMAT_BYTES_MAX], c[FORMAT_BYTES_MAX], d[FORMAT_BYTES_MAX], e[FORMAT_BYTES_MAX]; |
babfc091 | 3698 | struct statvfs ss; |
8580d1f7 | 3699 | uint64_t fs_size; |
babfc091 LP |
3700 | |
3701 | assert(m); | |
3702 | assert(fd >= 0); | |
3703 | ||
3704 | if (fstatvfs(fd, &ss) >= 0) | |
3705 | fs_size = ss.f_frsize * ss.f_blocks; | |
8580d1f7 | 3706 | else { |
8fc58f1a | 3707 | log_debug_errno(errno, "Failed to determine disk size: %m"); |
8580d1f7 LP |
3708 | fs_size = 0; |
3709 | } | |
babfc091 LP |
3710 | |
3711 | if (m->max_use == (uint64_t) -1) { | |
3712 | ||
3713 | if (fs_size > 0) { | |
3714 | m->max_use = PAGE_ALIGN(fs_size / 10); /* 10% of file system size */ | |
3715 | ||
3716 | if (m->max_use > DEFAULT_MAX_USE_UPPER) | |
3717 | m->max_use = DEFAULT_MAX_USE_UPPER; | |
3718 | ||
3719 | if (m->max_use < DEFAULT_MAX_USE_LOWER) | |
3720 | m->max_use = DEFAULT_MAX_USE_LOWER; | |
3721 | } else | |
3722 | m->max_use = DEFAULT_MAX_USE_LOWER; | |
3723 | } else { | |
3724 | m->max_use = PAGE_ALIGN(m->max_use); | |
3725 | ||
8580d1f7 | 3726 | if (m->max_use != 0 && m->max_use < JOURNAL_FILE_SIZE_MIN*2) |
babfc091 LP |
3727 | m->max_use = JOURNAL_FILE_SIZE_MIN*2; |
3728 | } | |
3729 | ||
8580d1f7 LP |
3730 | if (m->min_use == (uint64_t) -1) |
3731 | m->min_use = DEFAULT_MIN_USE; | |
3732 | ||
3733 | if (m->min_use > m->max_use) | |
3734 | m->min_use = m->max_use; | |
3735 | ||
babfc091 LP |
3736 | if (m->max_size == (uint64_t) -1) { |
3737 | m->max_size = PAGE_ALIGN(m->max_use / 8); /* 8 chunks */ | |
3738 | ||
3739 | if (m->max_size > DEFAULT_MAX_SIZE_UPPER) | |
3740 | m->max_size = DEFAULT_MAX_SIZE_UPPER; | |
3741 | } else | |
3742 | m->max_size = PAGE_ALIGN(m->max_size); | |
3743 | ||
8580d1f7 LP |
3744 | if (m->max_size != 0) { |
3745 | if (m->max_size < JOURNAL_FILE_SIZE_MIN) | |
3746 | m->max_size = JOURNAL_FILE_SIZE_MIN; | |
babfc091 | 3747 | |
8580d1f7 LP |
3748 | if (m->max_use != 0 && m->max_size*2 > m->max_use) |
3749 | m->max_use = m->max_size*2; | |
3750 | } | |
babfc091 LP |
3751 | |
3752 | if (m->min_size == (uint64_t) -1) | |
3753 | m->min_size = JOURNAL_FILE_SIZE_MIN; | |
3754 | else { | |
3755 | m->min_size = PAGE_ALIGN(m->min_size); | |
3756 | ||
3757 | if (m->min_size < JOURNAL_FILE_SIZE_MIN) | |
3758 | m->min_size = JOURNAL_FILE_SIZE_MIN; | |
3759 | ||
8580d1f7 | 3760 | if (m->max_size != 0 && m->min_size > m->max_size) |
babfc091 LP |
3761 | m->max_size = m->min_size; |
3762 | } | |
3763 | ||
3764 | if (m->keep_free == (uint64_t) -1) { | |
3765 | ||
3766 | if (fs_size > 0) { | |
8621b110 | 3767 | m->keep_free = PAGE_ALIGN(fs_size * 3 / 20); /* 15% of file system size */ |
babfc091 LP |
3768 | |
3769 | if (m->keep_free > DEFAULT_KEEP_FREE_UPPER) | |
3770 | m->keep_free = DEFAULT_KEEP_FREE_UPPER; | |
3771 | ||
3772 | } else | |
3773 | m->keep_free = DEFAULT_KEEP_FREE; | |
3774 | } | |
3775 | ||
8580d1f7 LP |
3776 | if (m->n_max_files == (uint64_t) -1) |
3777 | m->n_max_files = DEFAULT_N_MAX_FILES; | |
3778 | ||
3779 | log_debug("Fixed min_use=%s max_use=%s max_size=%s min_size=%s keep_free=%s n_max_files=%" PRIu64, | |
3780 | format_bytes(a, sizeof(a), m->min_use), | |
3781 | format_bytes(b, sizeof(b), m->max_use), | |
3782 | format_bytes(c, sizeof(c), m->max_size), | |
3783 | format_bytes(d, sizeof(d), m->min_size), | |
3784 | format_bytes(e, sizeof(e), m->keep_free), | |
3785 | m->n_max_files); | |
babfc091 | 3786 | } |
08984293 LP |
3787 | |
3788 | int journal_file_get_cutoff_realtime_usec(JournalFile *f, usec_t *from, usec_t *to) { | |
08984293 | 3789 | assert(f); |
c88cc6af | 3790 | assert(f->header); |
08984293 LP |
3791 | assert(from || to); |
3792 | ||
3793 | if (from) { | |
162566a4 LP |
3794 | if (f->header->head_entry_realtime == 0) |
3795 | return -ENOENT; | |
08984293 | 3796 | |
162566a4 | 3797 | *from = le64toh(f->header->head_entry_realtime); |
08984293 LP |
3798 | } |
3799 | ||
3800 | if (to) { | |
162566a4 LP |
3801 | if (f->header->tail_entry_realtime == 0) |
3802 | return -ENOENT; | |
08984293 | 3803 | |
162566a4 | 3804 | *to = le64toh(f->header->tail_entry_realtime); |
08984293 LP |
3805 | } |
3806 | ||
3807 | return 1; | |
3808 | } | |
3809 | ||
3810 | int journal_file_get_cutoff_monotonic_usec(JournalFile *f, sd_id128_t boot_id, usec_t *from, usec_t *to) { | |
08984293 LP |
3811 | Object *o; |
3812 | uint64_t p; | |
3813 | int r; | |
3814 | ||
3815 | assert(f); | |
3816 | assert(from || to); | |
3817 | ||
47838ab3 | 3818 | r = find_data_object_by_boot_id(f, boot_id, &o, &p); |
08984293 LP |
3819 | if (r <= 0) |
3820 | return r; | |
3821 | ||
3822 | if (le64toh(o->data.n_entries) <= 0) | |
3823 | return 0; | |
3824 | ||
3825 | if (from) { | |
3826 | r = journal_file_move_to_object(f, OBJECT_ENTRY, le64toh(o->data.entry_offset), &o); | |
3827 | if (r < 0) | |
3828 | return r; | |
3829 | ||
3830 | *from = le64toh(o->entry.monotonic); | |
3831 | } | |
3832 | ||
3833 | if (to) { | |
3834 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); | |
3835 | if (r < 0) | |
3836 | return r; | |
3837 | ||
3838 | r = generic_array_get_plus_one(f, | |
3839 | le64toh(o->data.entry_offset), | |
3840 | le64toh(o->data.entry_array_offset), | |
3841 | le64toh(o->data.n_entries)-1, | |
3842 | &o, NULL); | |
3843 | if (r <= 0) | |
3844 | return r; | |
3845 | ||
3846 | *to = le64toh(o->entry.monotonic); | |
3847 | } | |
3848 | ||
3849 | return 1; | |
3850 | } | |
dca6219e | 3851 | |
fb0951b0 | 3852 | bool journal_file_rotate_suggested(JournalFile *f, usec_t max_file_usec) { |
dca6219e | 3853 | assert(f); |
c88cc6af | 3854 | assert(f->header); |
dca6219e LP |
3855 | |
3856 | /* If we gained new header fields we gained new features, | |
3857 | * hence suggest a rotation */ | |
361f9cbc LP |
3858 | if (le64toh(f->header->header_size) < sizeof(Header)) { |
3859 | log_debug("%s uses an outdated header, suggesting rotation.", f->path); | |
dca6219e | 3860 | return true; |
361f9cbc | 3861 | } |
dca6219e LP |
3862 | |
3863 | /* Let's check if the hash tables grew over a certain fill | |
3864 | * level (75%, borrowing this value from Java's hash table | |
3865 | * implementation), and if so suggest a rotation. To calculate | |
3866 | * the fill level we need the n_data field, which only exists | |
3867 | * in newer versions. */ | |
3868 | ||
3869 | if (JOURNAL_HEADER_CONTAINS(f->header, n_data)) | |
361f9cbc | 3870 | if (le64toh(f->header->n_data) * 4ULL > (le64toh(f->header->data_hash_table_size) / sizeof(HashItem)) * 3ULL) { |
507f22bd | 3871 | log_debug("Data hash table of %s has a fill level at %.1f (%"PRIu64" of %"PRIu64" items, %llu file size, %"PRIu64" bytes per hash table item), suggesting rotation.", |
361f9cbc LP |
3872 | f->path, |
3873 | 100.0 * (double) le64toh(f->header->n_data) / ((double) (le64toh(f->header->data_hash_table_size) / sizeof(HashItem))), | |
507f22bd ZJS |
3874 | le64toh(f->header->n_data), |
3875 | le64toh(f->header->data_hash_table_size) / sizeof(HashItem), | |
3876 | (unsigned long long) f->last_stat.st_size, | |
3877 | f->last_stat.st_size / le64toh(f->header->n_data)); | |
dca6219e | 3878 | return true; |
361f9cbc | 3879 | } |
dca6219e LP |
3880 | |
3881 | if (JOURNAL_HEADER_CONTAINS(f->header, n_fields)) | |
361f9cbc | 3882 | if (le64toh(f->header->n_fields) * 4ULL > (le64toh(f->header->field_hash_table_size) / sizeof(HashItem)) * 3ULL) { |
507f22bd | 3883 | log_debug("Field hash table of %s has a fill level at %.1f (%"PRIu64" of %"PRIu64" items), suggesting rotation.", |
361f9cbc LP |
3884 | f->path, |
3885 | 100.0 * (double) le64toh(f->header->n_fields) / ((double) (le64toh(f->header->field_hash_table_size) / sizeof(HashItem))), | |
507f22bd ZJS |
3886 | le64toh(f->header->n_fields), |
3887 | le64toh(f->header->field_hash_table_size) / sizeof(HashItem)); | |
dca6219e | 3888 | return true; |
361f9cbc | 3889 | } |
dca6219e | 3890 | |
0598fd4a LP |
3891 | /* Are the data objects properly indexed by field objects? */ |
3892 | if (JOURNAL_HEADER_CONTAINS(f->header, n_data) && | |
3893 | JOURNAL_HEADER_CONTAINS(f->header, n_fields) && | |
3894 | le64toh(f->header->n_data) > 0 && | |
3895 | le64toh(f->header->n_fields) == 0) | |
3896 | return true; | |
3897 | ||
fb0951b0 LP |
3898 | if (max_file_usec > 0) { |
3899 | usec_t t, h; | |
3900 | ||
3901 | h = le64toh(f->header->head_entry_realtime); | |
3902 | t = now(CLOCK_REALTIME); | |
3903 | ||
3904 | if (h > 0 && t > h + max_file_usec) | |
3905 | return true; | |
3906 | } | |
3907 | ||
dca6219e LP |
3908 | return false; |
3909 | } |