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