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