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