]>
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)) | |
710 | return -EFAULT; | |
711 | ||
7a9dabea | 712 | r = journal_file_move_to(f, type, false, offset, sizeof(ObjectHeader), &t); |
cec736d2 LP |
713 | if (r < 0) |
714 | return r; | |
715 | ||
716 | o = (Object*) t; | |
717 | s = le64toh(o->object.size); | |
718 | ||
719 | if (s < sizeof(ObjectHeader)) | |
720 | return -EBADMSG; | |
721 | ||
16e9f408 LP |
722 | if (o->object.type <= OBJECT_UNUSED) |
723 | return -EBADMSG; | |
724 | ||
725 | if (s < minimum_header_size(o)) | |
726 | return -EBADMSG; | |
727 | ||
d05089d8 | 728 | if (type > OBJECT_UNUSED && o->object.type != type) |
cec736d2 LP |
729 | return -EBADMSG; |
730 | ||
731 | if (s > sizeof(ObjectHeader)) { | |
7a9dabea | 732 | r = journal_file_move_to(f, type, false, offset, s, &t); |
cec736d2 LP |
733 | if (r < 0) |
734 | return r; | |
735 | ||
736 | o = (Object*) t; | |
737 | } | |
738 | ||
cec736d2 LP |
739 | *ret = o; |
740 | return 0; | |
741 | } | |
742 | ||
d98cc1f2 | 743 | static uint64_t journal_file_entry_seqnum(JournalFile *f, uint64_t *seqnum) { |
cec736d2 LP |
744 | uint64_t r; |
745 | ||
746 | assert(f); | |
c88cc6af | 747 | assert(f->header); |
cec736d2 | 748 | |
beec0085 | 749 | r = le64toh(f->header->tail_entry_seqnum) + 1; |
c2373f84 LP |
750 | |
751 | if (seqnum) { | |
de190aef | 752 | /* If an external seqnum counter was passed, we update |
c2373f84 LP |
753 | * both the local and the external one, and set it to |
754 | * the maximum of both */ | |
755 | ||
756 | if (*seqnum + 1 > r) | |
757 | r = *seqnum + 1; | |
758 | ||
759 | *seqnum = r; | |
760 | } | |
761 | ||
beec0085 | 762 | f->header->tail_entry_seqnum = htole64(r); |
cec736d2 | 763 | |
beec0085 LP |
764 | if (f->header->head_entry_seqnum == 0) |
765 | f->header->head_entry_seqnum = htole64(r); | |
de190aef | 766 | |
cec736d2 LP |
767 | return r; |
768 | } | |
769 | ||
78519831 | 770 | int journal_file_append_object(JournalFile *f, ObjectType type, uint64_t size, Object **ret, uint64_t *offset) { |
cec736d2 LP |
771 | int r; |
772 | uint64_t p; | |
773 | Object *tail, *o; | |
774 | void *t; | |
775 | ||
776 | assert(f); | |
c88cc6af | 777 | assert(f->header); |
d05089d8 | 778 | assert(type > OBJECT_UNUSED && type < _OBJECT_TYPE_MAX); |
cec736d2 LP |
779 | assert(size >= sizeof(ObjectHeader)); |
780 | assert(offset); | |
781 | assert(ret); | |
782 | ||
26687bf8 OS |
783 | r = journal_file_set_online(f); |
784 | if (r < 0) | |
785 | return r; | |
786 | ||
cec736d2 | 787 | p = le64toh(f->header->tail_object_offset); |
cec736d2 | 788 | if (p == 0) |
23b0b2b2 | 789 | p = le64toh(f->header->header_size); |
cec736d2 | 790 | else { |
d05089d8 | 791 | r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &tail); |
cec736d2 LP |
792 | if (r < 0) |
793 | return r; | |
794 | ||
795 | p += ALIGN64(le64toh(tail->object.size)); | |
796 | } | |
797 | ||
798 | r = journal_file_allocate(f, p, size); | |
799 | if (r < 0) | |
800 | return r; | |
801 | ||
fcde2389 | 802 | r = journal_file_move_to(f, type, false, p, size, &t); |
cec736d2 LP |
803 | if (r < 0) |
804 | return r; | |
805 | ||
806 | o = (Object*) t; | |
807 | ||
808 | zero(o->object); | |
de190aef | 809 | o->object.type = type; |
cec736d2 LP |
810 | o->object.size = htole64(size); |
811 | ||
812 | f->header->tail_object_offset = htole64(p); | |
cec736d2 LP |
813 | f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1); |
814 | ||
815 | *ret = o; | |
816 | *offset = p; | |
817 | ||
818 | return 0; | |
819 | } | |
820 | ||
de190aef | 821 | static int journal_file_setup_data_hash_table(JournalFile *f) { |
cec736d2 LP |
822 | uint64_t s, p; |
823 | Object *o; | |
824 | int r; | |
825 | ||
826 | assert(f); | |
c88cc6af | 827 | assert(f->header); |
cec736d2 | 828 | |
070052ab LP |
829 | /* We estimate that we need 1 hash table entry per 768 bytes |
830 | of journal file and we want to make sure we never get | |
831 | beyond 75% fill level. Calculate the hash table size for | |
832 | the maximum file size based on these metrics. */ | |
4a92baf3 | 833 | |
dfabe643 | 834 | s = (f->metrics.max_size * 4 / 768 / 3) * sizeof(HashItem); |
4a92baf3 LP |
835 | if (s < DEFAULT_DATA_HASH_TABLE_SIZE) |
836 | s = DEFAULT_DATA_HASH_TABLE_SIZE; | |
837 | ||
507f22bd | 838 | log_debug("Reserving %"PRIu64" entries in hash table.", s / sizeof(HashItem)); |
4a92baf3 | 839 | |
de190aef LP |
840 | r = journal_file_append_object(f, |
841 | OBJECT_DATA_HASH_TABLE, | |
842 | offsetof(Object, hash_table.items) + s, | |
843 | &o, &p); | |
cec736d2 LP |
844 | if (r < 0) |
845 | return r; | |
846 | ||
29804cc1 | 847 | memzero(o->hash_table.items, s); |
cec736d2 | 848 | |
de190aef LP |
849 | f->header->data_hash_table_offset = htole64(p + offsetof(Object, hash_table.items)); |
850 | f->header->data_hash_table_size = htole64(s); | |
cec736d2 LP |
851 | |
852 | return 0; | |
853 | } | |
854 | ||
de190aef | 855 | static int journal_file_setup_field_hash_table(JournalFile *f) { |
cec736d2 LP |
856 | uint64_t s, p; |
857 | Object *o; | |
858 | int r; | |
859 | ||
860 | assert(f); | |
c88cc6af | 861 | assert(f->header); |
cec736d2 | 862 | |
3c1668da LP |
863 | /* We use a fixed size hash table for the fields as this |
864 | * number should grow very slowly only */ | |
865 | ||
de190aef LP |
866 | s = DEFAULT_FIELD_HASH_TABLE_SIZE; |
867 | r = journal_file_append_object(f, | |
868 | OBJECT_FIELD_HASH_TABLE, | |
869 | offsetof(Object, hash_table.items) + s, | |
870 | &o, &p); | |
cec736d2 LP |
871 | if (r < 0) |
872 | return r; | |
873 | ||
29804cc1 | 874 | memzero(o->hash_table.items, s); |
cec736d2 | 875 | |
de190aef LP |
876 | f->header->field_hash_table_offset = htole64(p + offsetof(Object, hash_table.items)); |
877 | f->header->field_hash_table_size = htole64(s); | |
cec736d2 LP |
878 | |
879 | return 0; | |
880 | } | |
881 | ||
dade37d4 | 882 | int journal_file_map_data_hash_table(JournalFile *f) { |
cec736d2 LP |
883 | uint64_t s, p; |
884 | void *t; | |
885 | int r; | |
886 | ||
887 | assert(f); | |
c88cc6af | 888 | assert(f->header); |
cec736d2 | 889 | |
dade37d4 LP |
890 | if (f->data_hash_table) |
891 | return 0; | |
892 | ||
de190aef LP |
893 | p = le64toh(f->header->data_hash_table_offset); |
894 | s = le64toh(f->header->data_hash_table_size); | |
cec736d2 | 895 | |
de190aef | 896 | r = journal_file_move_to(f, |
16e9f408 | 897 | OBJECT_DATA_HASH_TABLE, |
fcde2389 | 898 | true, |
de190aef LP |
899 | p, s, |
900 | &t); | |
cec736d2 LP |
901 | if (r < 0) |
902 | return r; | |
903 | ||
de190aef | 904 | f->data_hash_table = t; |
cec736d2 LP |
905 | return 0; |
906 | } | |
907 | ||
dade37d4 | 908 | int journal_file_map_field_hash_table(JournalFile *f) { |
cec736d2 LP |
909 | uint64_t s, p; |
910 | void *t; | |
911 | int r; | |
912 | ||
913 | assert(f); | |
c88cc6af | 914 | assert(f->header); |
cec736d2 | 915 | |
dade37d4 LP |
916 | if (f->field_hash_table) |
917 | return 0; | |
918 | ||
de190aef LP |
919 | p = le64toh(f->header->field_hash_table_offset); |
920 | s = le64toh(f->header->field_hash_table_size); | |
cec736d2 | 921 | |
de190aef | 922 | r = journal_file_move_to(f, |
16e9f408 | 923 | OBJECT_FIELD_HASH_TABLE, |
fcde2389 | 924 | true, |
de190aef LP |
925 | p, s, |
926 | &t); | |
cec736d2 LP |
927 | if (r < 0) |
928 | return r; | |
929 | ||
de190aef | 930 | f->field_hash_table = t; |
cec736d2 LP |
931 | return 0; |
932 | } | |
933 | ||
3c1668da LP |
934 | static int journal_file_link_field( |
935 | JournalFile *f, | |
936 | Object *o, | |
937 | uint64_t offset, | |
938 | uint64_t hash) { | |
939 | ||
805d1486 | 940 | uint64_t p, h, m; |
3c1668da LP |
941 | int r; |
942 | ||
943 | assert(f); | |
c88cc6af | 944 | assert(f->header); |
90d222c1 | 945 | assert(f->field_hash_table); |
3c1668da LP |
946 | assert(o); |
947 | assert(offset > 0); | |
948 | ||
949 | if (o->object.type != OBJECT_FIELD) | |
950 | return -EINVAL; | |
951 | ||
805d1486 LP |
952 | m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem); |
953 | if (m <= 0) | |
954 | return -EBADMSG; | |
3c1668da | 955 | |
805d1486 | 956 | /* This might alter the window we are looking at */ |
3c1668da LP |
957 | o->field.next_hash_offset = o->field.head_data_offset = 0; |
958 | ||
805d1486 | 959 | h = hash % m; |
3c1668da LP |
960 | p = le64toh(f->field_hash_table[h].tail_hash_offset); |
961 | if (p == 0) | |
962 | f->field_hash_table[h].head_hash_offset = htole64(offset); | |
963 | else { | |
964 | r = journal_file_move_to_object(f, OBJECT_FIELD, p, &o); | |
965 | if (r < 0) | |
966 | return r; | |
967 | ||
968 | o->field.next_hash_offset = htole64(offset); | |
969 | } | |
970 | ||
971 | f->field_hash_table[h].tail_hash_offset = htole64(offset); | |
972 | ||
973 | if (JOURNAL_HEADER_CONTAINS(f->header, n_fields)) | |
974 | f->header->n_fields = htole64(le64toh(f->header->n_fields) + 1); | |
975 | ||
976 | return 0; | |
977 | } | |
978 | ||
979 | static int journal_file_link_data( | |
980 | JournalFile *f, | |
981 | Object *o, | |
982 | uint64_t offset, | |
983 | uint64_t hash) { | |
984 | ||
805d1486 | 985 | uint64_t p, h, m; |
cec736d2 LP |
986 | int r; |
987 | ||
988 | assert(f); | |
c88cc6af | 989 | assert(f->header); |
90d222c1 | 990 | assert(f->data_hash_table); |
cec736d2 LP |
991 | assert(o); |
992 | assert(offset > 0); | |
b588975f LP |
993 | |
994 | if (o->object.type != OBJECT_DATA) | |
995 | return -EINVAL; | |
cec736d2 | 996 | |
805d1486 LP |
997 | m = le64toh(f->header->data_hash_table_size) / sizeof(HashItem); |
998 | if (m <= 0) | |
999 | return -EBADMSG; | |
48496df6 | 1000 | |
805d1486 | 1001 | /* This might alter the window we are looking at */ |
de190aef LP |
1002 | o->data.next_hash_offset = o->data.next_field_offset = 0; |
1003 | o->data.entry_offset = o->data.entry_array_offset = 0; | |
1004 | o->data.n_entries = 0; | |
cec736d2 | 1005 | |
805d1486 | 1006 | h = hash % m; |
8db4213e | 1007 | p = le64toh(f->data_hash_table[h].tail_hash_offset); |
3c1668da | 1008 | if (p == 0) |
cec736d2 | 1009 | /* Only entry in the hash table is easy */ |
de190aef | 1010 | f->data_hash_table[h].head_hash_offset = htole64(offset); |
3c1668da | 1011 | else { |
48496df6 LP |
1012 | /* Move back to the previous data object, to patch in |
1013 | * pointer */ | |
cec736d2 | 1014 | |
de190aef | 1015 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); |
cec736d2 LP |
1016 | if (r < 0) |
1017 | return r; | |
1018 | ||
de190aef | 1019 | o->data.next_hash_offset = htole64(offset); |
cec736d2 LP |
1020 | } |
1021 | ||
de190aef | 1022 | f->data_hash_table[h].tail_hash_offset = htole64(offset); |
cec736d2 | 1023 | |
dca6219e LP |
1024 | if (JOURNAL_HEADER_CONTAINS(f->header, n_data)) |
1025 | f->header->n_data = htole64(le64toh(f->header->n_data) + 1); | |
1026 | ||
cec736d2 LP |
1027 | return 0; |
1028 | } | |
1029 | ||
3c1668da LP |
1030 | int journal_file_find_field_object_with_hash( |
1031 | JournalFile *f, | |
1032 | const void *field, uint64_t size, uint64_t hash, | |
1033 | Object **ret, uint64_t *offset) { | |
1034 | ||
805d1486 | 1035 | uint64_t p, osize, h, m; |
3c1668da LP |
1036 | int r; |
1037 | ||
1038 | assert(f); | |
c88cc6af | 1039 | assert(f->header); |
3c1668da LP |
1040 | assert(field && size > 0); |
1041 | ||
dade37d4 LP |
1042 | /* If the field hash table is empty, we can't find anything */ |
1043 | if (le64toh(f->header->field_hash_table_size) <= 0) | |
1044 | return 0; | |
1045 | ||
1046 | /* Map the field hash table, if it isn't mapped yet. */ | |
1047 | r = journal_file_map_field_hash_table(f); | |
1048 | if (r < 0) | |
1049 | return r; | |
1050 | ||
3c1668da LP |
1051 | osize = offsetof(Object, field.payload) + size; |
1052 | ||
805d1486 | 1053 | m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem); |
805d1486 | 1054 | if (m <= 0) |
3c1668da LP |
1055 | return -EBADMSG; |
1056 | ||
805d1486 | 1057 | h = hash % m; |
3c1668da LP |
1058 | p = le64toh(f->field_hash_table[h].head_hash_offset); |
1059 | ||
1060 | while (p > 0) { | |
1061 | Object *o; | |
1062 | ||
1063 | r = journal_file_move_to_object(f, OBJECT_FIELD, p, &o); | |
1064 | if (r < 0) | |
1065 | return r; | |
1066 | ||
1067 | if (le64toh(o->field.hash) == hash && | |
1068 | le64toh(o->object.size) == osize && | |
1069 | memcmp(o->field.payload, field, size) == 0) { | |
1070 | ||
1071 | if (ret) | |
1072 | *ret = o; | |
1073 | if (offset) | |
1074 | *offset = p; | |
1075 | ||
1076 | return 1; | |
1077 | } | |
1078 | ||
1079 | p = le64toh(o->field.next_hash_offset); | |
1080 | } | |
1081 | ||
1082 | return 0; | |
1083 | } | |
1084 | ||
1085 | int journal_file_find_field_object( | |
1086 | JournalFile *f, | |
1087 | const void *field, uint64_t size, | |
1088 | Object **ret, uint64_t *offset) { | |
1089 | ||
1090 | uint64_t hash; | |
1091 | ||
1092 | assert(f); | |
1093 | assert(field && size > 0); | |
1094 | ||
1095 | hash = hash64(field, size); | |
1096 | ||
1097 | return journal_file_find_field_object_with_hash(f, | |
1098 | field, size, hash, | |
1099 | ret, offset); | |
1100 | } | |
1101 | ||
de190aef LP |
1102 | int journal_file_find_data_object_with_hash( |
1103 | JournalFile *f, | |
1104 | const void *data, uint64_t size, uint64_t hash, | |
1105 | Object **ret, uint64_t *offset) { | |
48496df6 | 1106 | |
805d1486 | 1107 | uint64_t p, osize, h, m; |
cec736d2 LP |
1108 | int r; |
1109 | ||
1110 | assert(f); | |
c88cc6af | 1111 | assert(f->header); |
cec736d2 LP |
1112 | assert(data || size == 0); |
1113 | ||
dade37d4 LP |
1114 | /* If there's no data hash table, then there's no entry. */ |
1115 | if (le64toh(f->header->data_hash_table_size) <= 0) | |
1116 | return 0; | |
1117 | ||
1118 | /* Map the data hash table, if it isn't mapped yet. */ | |
1119 | r = journal_file_map_data_hash_table(f); | |
1120 | if (r < 0) | |
1121 | return r; | |
1122 | ||
cec736d2 LP |
1123 | osize = offsetof(Object, data.payload) + size; |
1124 | ||
805d1486 LP |
1125 | m = le64toh(f->header->data_hash_table_size) / sizeof(HashItem); |
1126 | if (m <= 0) | |
bc85bfee LP |
1127 | return -EBADMSG; |
1128 | ||
805d1486 | 1129 | h = hash % m; |
de190aef | 1130 | p = le64toh(f->data_hash_table[h].head_hash_offset); |
cec736d2 | 1131 | |
de190aef LP |
1132 | while (p > 0) { |
1133 | Object *o; | |
cec736d2 | 1134 | |
de190aef | 1135 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); |
cec736d2 LP |
1136 | if (r < 0) |
1137 | return r; | |
1138 | ||
807e17f0 | 1139 | if (le64toh(o->data.hash) != hash) |
85a131e8 | 1140 | goto next; |
807e17f0 | 1141 | |
d89c8fdf | 1142 | if (o->object.flags & OBJECT_COMPRESSION_MASK) { |
3b1a55e1 | 1143 | #if defined(HAVE_XZ) || defined(HAVE_LZ4) |
fa1c4b51 | 1144 | uint64_t l; |
a7f7d1bd | 1145 | size_t rsize = 0; |
cec736d2 | 1146 | |
807e17f0 LP |
1147 | l = le64toh(o->object.size); |
1148 | if (l <= offsetof(Object, data.payload)) | |
cec736d2 LP |
1149 | return -EBADMSG; |
1150 | ||
807e17f0 LP |
1151 | l -= offsetof(Object, data.payload); |
1152 | ||
d89c8fdf ZJS |
1153 | r = decompress_blob(o->object.flags & OBJECT_COMPRESSION_MASK, |
1154 | o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize, 0); | |
1155 | if (r < 0) | |
1156 | return r; | |
807e17f0 | 1157 | |
b785c858 | 1158 | if (rsize == size && |
807e17f0 LP |
1159 | memcmp(f->compress_buffer, data, size) == 0) { |
1160 | ||
1161 | if (ret) | |
1162 | *ret = o; | |
1163 | ||
1164 | if (offset) | |
1165 | *offset = p; | |
1166 | ||
1167 | return 1; | |
1168 | } | |
3b1a55e1 ZJS |
1169 | #else |
1170 | return -EPROTONOSUPPORT; | |
1171 | #endif | |
807e17f0 LP |
1172 | } else if (le64toh(o->object.size) == osize && |
1173 | memcmp(o->data.payload, data, size) == 0) { | |
1174 | ||
cec736d2 LP |
1175 | if (ret) |
1176 | *ret = o; | |
1177 | ||
1178 | if (offset) | |
1179 | *offset = p; | |
1180 | ||
de190aef | 1181 | return 1; |
cec736d2 LP |
1182 | } |
1183 | ||
85a131e8 | 1184 | next: |
cec736d2 LP |
1185 | p = le64toh(o->data.next_hash_offset); |
1186 | } | |
1187 | ||
de190aef LP |
1188 | return 0; |
1189 | } | |
1190 | ||
1191 | int journal_file_find_data_object( | |
1192 | JournalFile *f, | |
1193 | const void *data, uint64_t size, | |
1194 | Object **ret, uint64_t *offset) { | |
1195 | ||
1196 | uint64_t hash; | |
1197 | ||
1198 | assert(f); | |
1199 | assert(data || size == 0); | |
1200 | ||
1201 | hash = hash64(data, size); | |
1202 | ||
1203 | return journal_file_find_data_object_with_hash(f, | |
1204 | data, size, hash, | |
1205 | ret, offset); | |
1206 | } | |
1207 | ||
3c1668da LP |
1208 | static int journal_file_append_field( |
1209 | JournalFile *f, | |
1210 | const void *field, uint64_t size, | |
1211 | Object **ret, uint64_t *offset) { | |
1212 | ||
1213 | uint64_t hash, p; | |
1214 | uint64_t osize; | |
1215 | Object *o; | |
1216 | int r; | |
1217 | ||
1218 | assert(f); | |
1219 | assert(field && size > 0); | |
1220 | ||
1221 | hash = hash64(field, size); | |
1222 | ||
1223 | r = journal_file_find_field_object_with_hash(f, field, size, hash, &o, &p); | |
1224 | if (r < 0) | |
1225 | return r; | |
1226 | else if (r > 0) { | |
1227 | ||
1228 | if (ret) | |
1229 | *ret = o; | |
1230 | ||
1231 | if (offset) | |
1232 | *offset = p; | |
1233 | ||
1234 | return 0; | |
1235 | } | |
1236 | ||
1237 | osize = offsetof(Object, field.payload) + size; | |
1238 | r = journal_file_append_object(f, OBJECT_FIELD, osize, &o, &p); | |
8c92d4bb LP |
1239 | if (r < 0) |
1240 | return r; | |
3c1668da LP |
1241 | |
1242 | o->field.hash = htole64(hash); | |
1243 | memcpy(o->field.payload, field, size); | |
1244 | ||
1245 | r = journal_file_link_field(f, o, p, hash); | |
1246 | if (r < 0) | |
1247 | return r; | |
1248 | ||
1249 | /* The linking might have altered the window, so let's | |
1250 | * refresh our pointer */ | |
1251 | r = journal_file_move_to_object(f, OBJECT_FIELD, p, &o); | |
1252 | if (r < 0) | |
1253 | return r; | |
1254 | ||
1255 | #ifdef HAVE_GCRYPT | |
1256 | r = journal_file_hmac_put_object(f, OBJECT_FIELD, o, p); | |
1257 | if (r < 0) | |
1258 | return r; | |
1259 | #endif | |
1260 | ||
1261 | if (ret) | |
1262 | *ret = o; | |
1263 | ||
1264 | if (offset) | |
1265 | *offset = p; | |
1266 | ||
1267 | return 0; | |
1268 | } | |
1269 | ||
48496df6 LP |
1270 | static int journal_file_append_data( |
1271 | JournalFile *f, | |
1272 | const void *data, uint64_t size, | |
1273 | Object **ret, uint64_t *offset) { | |
1274 | ||
de190aef LP |
1275 | uint64_t hash, p; |
1276 | uint64_t osize; | |
1277 | Object *o; | |
d89c8fdf | 1278 | int r, compression = 0; |
3c1668da | 1279 | const void *eq; |
de190aef LP |
1280 | |
1281 | assert(f); | |
1282 | assert(data || size == 0); | |
1283 | ||
1284 | hash = hash64(data, size); | |
1285 | ||
1286 | r = journal_file_find_data_object_with_hash(f, data, size, hash, &o, &p); | |
1287 | if (r < 0) | |
1288 | return r; | |
0240c603 | 1289 | if (r > 0) { |
de190aef LP |
1290 | |
1291 | if (ret) | |
1292 | *ret = o; | |
1293 | ||
1294 | if (offset) | |
1295 | *offset = p; | |
1296 | ||
1297 | return 0; | |
1298 | } | |
1299 | ||
1300 | osize = offsetof(Object, data.payload) + size; | |
1301 | r = journal_file_append_object(f, OBJECT_DATA, osize, &o, &p); | |
cec736d2 LP |
1302 | if (r < 0) |
1303 | return r; | |
1304 | ||
cec736d2 | 1305 | o->data.hash = htole64(hash); |
807e17f0 | 1306 | |
d89c8fdf | 1307 | #if defined(HAVE_XZ) || defined(HAVE_LZ4) |
d1afbcd2 | 1308 | if (JOURNAL_FILE_COMPRESS(f) && size >= COMPRESSION_SIZE_THRESHOLD) { |
a7f7d1bd | 1309 | size_t rsize = 0; |
807e17f0 | 1310 | |
5d6f46b6 | 1311 | compression = compress_blob(data, size, o->data.payload, size - 1, &rsize); |
807e17f0 | 1312 | |
d1afbcd2 | 1313 | if (compression >= 0) { |
807e17f0 | 1314 | o->object.size = htole64(offsetof(Object, data.payload) + rsize); |
d89c8fdf | 1315 | o->object.flags |= compression; |
807e17f0 | 1316 | |
fa1c4b51 | 1317 | log_debug("Compressed data object %"PRIu64" -> %zu using %s", |
d89c8fdf | 1318 | size, rsize, object_compressed_to_string(compression)); |
d1afbcd2 LP |
1319 | } else |
1320 | /* Compression didn't work, we don't really care why, let's continue without compression */ | |
1321 | compression = 0; | |
807e17f0 LP |
1322 | } |
1323 | #endif | |
1324 | ||
75f32f04 ZJS |
1325 | if (compression == 0) |
1326 | memcpy_safe(o->data.payload, data, size); | |
cec736d2 | 1327 | |
de190aef | 1328 | r = journal_file_link_data(f, o, p, hash); |
cec736d2 LP |
1329 | if (r < 0) |
1330 | return r; | |
1331 | ||
48496df6 LP |
1332 | /* The linking might have altered the window, so let's |
1333 | * refresh our pointer */ | |
1334 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); | |
1335 | if (r < 0) | |
1336 | return r; | |
1337 | ||
08c6f819 SL |
1338 | if (!data) |
1339 | eq = NULL; | |
1340 | else | |
1341 | eq = memchr(data, '=', size); | |
3c1668da | 1342 | if (eq && eq > data) { |
748db592 | 1343 | Object *fo = NULL; |
3c1668da | 1344 | uint64_t fp; |
3c1668da LP |
1345 | |
1346 | /* Create field object ... */ | |
1347 | r = journal_file_append_field(f, data, (uint8_t*) eq - (uint8_t*) data, &fo, &fp); | |
1348 | if (r < 0) | |
1349 | return r; | |
1350 | ||
1351 | /* ... and link it in. */ | |
1352 | o->data.next_field_offset = fo->field.head_data_offset; | |
1353 | fo->field.head_data_offset = le64toh(p); | |
1354 | } | |
1355 | ||
5996c7c2 LP |
1356 | #ifdef HAVE_GCRYPT |
1357 | r = journal_file_hmac_put_object(f, OBJECT_DATA, o, p); | |
1358 | if (r < 0) | |
1359 | return r; | |
1360 | #endif | |
1361 | ||
cec736d2 LP |
1362 | if (ret) |
1363 | *ret = o; | |
1364 | ||
1365 | if (offset) | |
de190aef | 1366 | *offset = p; |
cec736d2 LP |
1367 | |
1368 | return 0; | |
1369 | } | |
1370 | ||
1371 | uint64_t journal_file_entry_n_items(Object *o) { | |
1372 | assert(o); | |
b588975f LP |
1373 | |
1374 | if (o->object.type != OBJECT_ENTRY) | |
1375 | return 0; | |
cec736d2 LP |
1376 | |
1377 | return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem); | |
1378 | } | |
1379 | ||
0284adc6 | 1380 | uint64_t journal_file_entry_array_n_items(Object *o) { |
de190aef | 1381 | assert(o); |
b588975f LP |
1382 | |
1383 | if (o->object.type != OBJECT_ENTRY_ARRAY) | |
1384 | return 0; | |
de190aef LP |
1385 | |
1386 | return (le64toh(o->object.size) - offsetof(Object, entry_array.items)) / sizeof(uint64_t); | |
1387 | } | |
1388 | ||
fb9a24b6 LP |
1389 | uint64_t journal_file_hash_table_n_items(Object *o) { |
1390 | assert(o); | |
b588975f LP |
1391 | |
1392 | if (o->object.type != OBJECT_DATA_HASH_TABLE && | |
1393 | o->object.type != OBJECT_FIELD_HASH_TABLE) | |
1394 | return 0; | |
fb9a24b6 LP |
1395 | |
1396 | return (le64toh(o->object.size) - offsetof(Object, hash_table.items)) / sizeof(HashItem); | |
1397 | } | |
1398 | ||
de190aef | 1399 | static int link_entry_into_array(JournalFile *f, |
4fd052ae FC |
1400 | le64_t *first, |
1401 | le64_t *idx, | |
de190aef | 1402 | uint64_t p) { |
cec736d2 | 1403 | int r; |
de190aef LP |
1404 | uint64_t n = 0, ap = 0, q, i, a, hidx; |
1405 | Object *o; | |
1406 | ||
cec736d2 | 1407 | assert(f); |
c88cc6af | 1408 | assert(f->header); |
de190aef LP |
1409 | assert(first); |
1410 | assert(idx); | |
1411 | assert(p > 0); | |
cec736d2 | 1412 | |
de190aef LP |
1413 | a = le64toh(*first); |
1414 | i = hidx = le64toh(*idx); | |
1415 | while (a > 0) { | |
1416 | ||
1417 | r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); | |
1418 | if (r < 0) | |
1419 | return r; | |
cec736d2 | 1420 | |
de190aef LP |
1421 | n = journal_file_entry_array_n_items(o); |
1422 | if (i < n) { | |
1423 | o->entry_array.items[i] = htole64(p); | |
1424 | *idx = htole64(hidx + 1); | |
1425 | return 0; | |
1426 | } | |
cec736d2 | 1427 | |
de190aef LP |
1428 | i -= n; |
1429 | ap = a; | |
1430 | a = le64toh(o->entry_array.next_entry_array_offset); | |
1431 | } | |
1432 | ||
1433 | if (hidx > n) | |
1434 | n = (hidx+1) * 2; | |
1435 | else | |
1436 | n = n * 2; | |
1437 | ||
1438 | if (n < 4) | |
1439 | n = 4; | |
1440 | ||
1441 | r = journal_file_append_object(f, OBJECT_ENTRY_ARRAY, | |
1442 | offsetof(Object, entry_array.items) + n * sizeof(uint64_t), | |
1443 | &o, &q); | |
cec736d2 LP |
1444 | if (r < 0) |
1445 | return r; | |
1446 | ||
feb12d3e | 1447 | #ifdef HAVE_GCRYPT |
5996c7c2 | 1448 | r = journal_file_hmac_put_object(f, OBJECT_ENTRY_ARRAY, o, q); |
b0af6f41 LP |
1449 | if (r < 0) |
1450 | return r; | |
feb12d3e | 1451 | #endif |
b0af6f41 | 1452 | |
de190aef | 1453 | o->entry_array.items[i] = htole64(p); |
cec736d2 | 1454 | |
de190aef | 1455 | if (ap == 0) |
7be3aa17 | 1456 | *first = htole64(q); |
cec736d2 | 1457 | else { |
de190aef | 1458 | r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, ap, &o); |
cec736d2 LP |
1459 | if (r < 0) |
1460 | return r; | |
1461 | ||
de190aef LP |
1462 | o->entry_array.next_entry_array_offset = htole64(q); |
1463 | } | |
cec736d2 | 1464 | |
2dee23eb LP |
1465 | if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays)) |
1466 | f->header->n_entry_arrays = htole64(le64toh(f->header->n_entry_arrays) + 1); | |
1467 | ||
de190aef LP |
1468 | *idx = htole64(hidx + 1); |
1469 | ||
1470 | return 0; | |
1471 | } | |
cec736d2 | 1472 | |
de190aef | 1473 | static int link_entry_into_array_plus_one(JournalFile *f, |
4fd052ae FC |
1474 | le64_t *extra, |
1475 | le64_t *first, | |
1476 | le64_t *idx, | |
de190aef LP |
1477 | uint64_t p) { |
1478 | ||
1479 | int r; | |
1480 | ||
1481 | assert(f); | |
1482 | assert(extra); | |
1483 | assert(first); | |
1484 | assert(idx); | |
1485 | assert(p > 0); | |
1486 | ||
1487 | if (*idx == 0) | |
1488 | *extra = htole64(p); | |
1489 | else { | |
4fd052ae | 1490 | le64_t i; |
de190aef | 1491 | |
7be3aa17 | 1492 | i = htole64(le64toh(*idx) - 1); |
de190aef LP |
1493 | r = link_entry_into_array(f, first, &i, p); |
1494 | if (r < 0) | |
1495 | return r; | |
cec736d2 LP |
1496 | } |
1497 | ||
de190aef LP |
1498 | *idx = htole64(le64toh(*idx) + 1); |
1499 | return 0; | |
1500 | } | |
1501 | ||
1502 | static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) { | |
1503 | uint64_t p; | |
1504 | int r; | |
1505 | assert(f); | |
1506 | assert(o); | |
1507 | assert(offset > 0); | |
1508 | ||
1509 | p = le64toh(o->entry.items[i].object_offset); | |
1510 | if (p == 0) | |
1511 | return -EINVAL; | |
1512 | ||
1513 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); | |
cec736d2 LP |
1514 | if (r < 0) |
1515 | return r; | |
1516 | ||
de190aef LP |
1517 | return link_entry_into_array_plus_one(f, |
1518 | &o->data.entry_offset, | |
1519 | &o->data.entry_array_offset, | |
1520 | &o->data.n_entries, | |
1521 | offset); | |
cec736d2 LP |
1522 | } |
1523 | ||
1524 | static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) { | |
de190aef | 1525 | uint64_t n, i; |
cec736d2 LP |
1526 | int r; |
1527 | ||
1528 | assert(f); | |
c88cc6af | 1529 | assert(f->header); |
cec736d2 LP |
1530 | assert(o); |
1531 | assert(offset > 0); | |
b588975f LP |
1532 | |
1533 | if (o->object.type != OBJECT_ENTRY) | |
1534 | return -EINVAL; | |
cec736d2 | 1535 | |
b788cc23 LP |
1536 | __sync_synchronize(); |
1537 | ||
cec736d2 | 1538 | /* Link up the entry itself */ |
de190aef LP |
1539 | r = link_entry_into_array(f, |
1540 | &f->header->entry_array_offset, | |
1541 | &f->header->n_entries, | |
1542 | offset); | |
1543 | if (r < 0) | |
1544 | return r; | |
cec736d2 | 1545 | |
507f22bd | 1546 | /* log_debug("=> %s seqnr=%"PRIu64" n_entries=%"PRIu64, f->path, o->entry.seqnum, f->header->n_entries); */ |
cec736d2 | 1547 | |
de190aef | 1548 | if (f->header->head_entry_realtime == 0) |
0ac38b70 | 1549 | f->header->head_entry_realtime = o->entry.realtime; |
cec736d2 | 1550 | |
0ac38b70 | 1551 | f->header->tail_entry_realtime = o->entry.realtime; |
de190aef LP |
1552 | f->header->tail_entry_monotonic = o->entry.monotonic; |
1553 | ||
1554 | f->tail_entry_monotonic_valid = true; | |
cec736d2 LP |
1555 | |
1556 | /* Link up the items */ | |
1557 | n = journal_file_entry_n_items(o); | |
1558 | for (i = 0; i < n; i++) { | |
1559 | r = journal_file_link_entry_item(f, o, offset, i); | |
1560 | if (r < 0) | |
1561 | return r; | |
1562 | } | |
1563 | ||
cec736d2 LP |
1564 | return 0; |
1565 | } | |
1566 | ||
1567 | static int journal_file_append_entry_internal( | |
1568 | JournalFile *f, | |
1569 | const dual_timestamp *ts, | |
1570 | uint64_t xor_hash, | |
1571 | const EntryItem items[], unsigned n_items, | |
de190aef | 1572 | uint64_t *seqnum, |
cec736d2 LP |
1573 | Object **ret, uint64_t *offset) { |
1574 | uint64_t np; | |
1575 | uint64_t osize; | |
1576 | Object *o; | |
1577 | int r; | |
1578 | ||
1579 | assert(f); | |
c88cc6af | 1580 | assert(f->header); |
cec736d2 | 1581 | assert(items || n_items == 0); |
de190aef | 1582 | assert(ts); |
cec736d2 LP |
1583 | |
1584 | osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem)); | |
1585 | ||
de190aef | 1586 | r = journal_file_append_object(f, OBJECT_ENTRY, osize, &o, &np); |
cec736d2 LP |
1587 | if (r < 0) |
1588 | return r; | |
1589 | ||
d98cc1f2 | 1590 | o->entry.seqnum = htole64(journal_file_entry_seqnum(f, seqnum)); |
75f32f04 | 1591 | memcpy_safe(o->entry.items, items, n_items * sizeof(EntryItem)); |
de190aef LP |
1592 | o->entry.realtime = htole64(ts->realtime); |
1593 | o->entry.monotonic = htole64(ts->monotonic); | |
cec736d2 LP |
1594 | o->entry.xor_hash = htole64(xor_hash); |
1595 | o->entry.boot_id = f->header->boot_id; | |
1596 | ||
feb12d3e | 1597 | #ifdef HAVE_GCRYPT |
5996c7c2 | 1598 | r = journal_file_hmac_put_object(f, OBJECT_ENTRY, o, np); |
b0af6f41 LP |
1599 | if (r < 0) |
1600 | return r; | |
feb12d3e | 1601 | #endif |
b0af6f41 | 1602 | |
cec736d2 LP |
1603 | r = journal_file_link_entry(f, o, np); |
1604 | if (r < 0) | |
1605 | return r; | |
1606 | ||
1607 | if (ret) | |
1608 | *ret = o; | |
1609 | ||
1610 | if (offset) | |
1611 | *offset = np; | |
1612 | ||
1613 | return 0; | |
1614 | } | |
1615 | ||
cf244689 | 1616 | void journal_file_post_change(JournalFile *f) { |
50f20cfd LP |
1617 | assert(f); |
1618 | ||
1619 | /* inotify() does not receive IN_MODIFY events from file | |
1620 | * accesses done via mmap(). After each access we hence | |
1621 | * trigger IN_MODIFY by truncating the journal file to its | |
1622 | * current size which triggers IN_MODIFY. */ | |
1623 | ||
bc85bfee LP |
1624 | __sync_synchronize(); |
1625 | ||
50f20cfd | 1626 | if (ftruncate(f->fd, f->last_stat.st_size) < 0) |
e167d7fd | 1627 | log_debug_errno(errno, "Failed to truncate file to its own size: %m"); |
50f20cfd LP |
1628 | } |
1629 | ||
7a24f3bf VC |
1630 | static int post_change_thunk(sd_event_source *timer, uint64_t usec, void *userdata) { |
1631 | assert(userdata); | |
1632 | ||
1633 | journal_file_post_change(userdata); | |
1634 | ||
1635 | return 1; | |
1636 | } | |
1637 | ||
1638 | static void schedule_post_change(JournalFile *f) { | |
1639 | sd_event_source *timer; | |
1640 | int enabled, r; | |
1641 | uint64_t now; | |
1642 | ||
1643 | assert(f); | |
1644 | assert(f->post_change_timer); | |
1645 | ||
1646 | timer = f->post_change_timer; | |
1647 | ||
1648 | r = sd_event_source_get_enabled(timer, &enabled); | |
1649 | if (r < 0) { | |
e167d7fd LP |
1650 | log_debug_errno(r, "Failed to get ftruncate timer state: %m"); |
1651 | goto fail; | |
7a24f3bf VC |
1652 | } |
1653 | ||
1654 | if (enabled == SD_EVENT_ONESHOT) | |
1655 | return; | |
1656 | ||
1657 | r = sd_event_now(sd_event_source_get_event(timer), CLOCK_MONOTONIC, &now); | |
1658 | if (r < 0) { | |
e167d7fd LP |
1659 | log_debug_errno(r, "Failed to get clock's now for scheduling ftruncate: %m"); |
1660 | goto fail; | |
7a24f3bf VC |
1661 | } |
1662 | ||
1663 | r = sd_event_source_set_time(timer, now+f->post_change_timer_period); | |
1664 | if (r < 0) { | |
e167d7fd LP |
1665 | log_debug_errno(r, "Failed to set time for scheduling ftruncate: %m"); |
1666 | goto fail; | |
7a24f3bf VC |
1667 | } |
1668 | ||
1669 | r = sd_event_source_set_enabled(timer, SD_EVENT_ONESHOT); | |
1670 | if (r < 0) { | |
e167d7fd LP |
1671 | log_debug_errno(r, "Failed to enable scheduled ftruncate: %m"); |
1672 | goto fail; | |
7a24f3bf | 1673 | } |
e167d7fd LP |
1674 | |
1675 | return; | |
1676 | ||
1677 | fail: | |
1678 | /* On failure, let's simply post the change immediately. */ | |
1679 | journal_file_post_change(f); | |
7a24f3bf VC |
1680 | } |
1681 | ||
1682 | /* Enable coalesced change posting in a timer on the provided sd_event instance */ | |
1683 | int journal_file_enable_post_change_timer(JournalFile *f, sd_event *e, usec_t t) { | |
1684 | _cleanup_(sd_event_source_unrefp) sd_event_source *timer = NULL; | |
1685 | int r; | |
1686 | ||
1687 | assert(f); | |
1688 | assert_return(!f->post_change_timer, -EINVAL); | |
1689 | assert(e); | |
1690 | assert(t); | |
1691 | ||
1692 | r = sd_event_add_time(e, &timer, CLOCK_MONOTONIC, 0, 0, post_change_thunk, f); | |
1693 | if (r < 0) | |
1694 | return r; | |
1695 | ||
1696 | r = sd_event_source_set_enabled(timer, SD_EVENT_OFF); | |
1697 | if (r < 0) | |
1698 | return r; | |
1699 | ||
1700 | f->post_change_timer = timer; | |
1701 | timer = NULL; | |
1702 | f->post_change_timer_period = t; | |
1703 | ||
1704 | return r; | |
1705 | } | |
1706 | ||
1f2da9ec LP |
1707 | static int entry_item_cmp(const void *_a, const void *_b) { |
1708 | const EntryItem *a = _a, *b = _b; | |
1709 | ||
1710 | if (le64toh(a->object_offset) < le64toh(b->object_offset)) | |
1711 | return -1; | |
1712 | if (le64toh(a->object_offset) > le64toh(b->object_offset)) | |
1713 | return 1; | |
1714 | return 0; | |
1715 | } | |
1716 | ||
de190aef | 1717 | 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 |
1718 | unsigned i; |
1719 | EntryItem *items; | |
1720 | int r; | |
1721 | uint64_t xor_hash = 0; | |
de190aef | 1722 | struct dual_timestamp _ts; |
cec736d2 LP |
1723 | |
1724 | assert(f); | |
c88cc6af | 1725 | assert(f->header); |
cec736d2 LP |
1726 | assert(iovec || n_iovec == 0); |
1727 | ||
de190aef LP |
1728 | if (!ts) { |
1729 | dual_timestamp_get(&_ts); | |
1730 | ts = &_ts; | |
1731 | } | |
1732 | ||
feb12d3e | 1733 | #ifdef HAVE_GCRYPT |
7560fffc LP |
1734 | r = journal_file_maybe_append_tag(f, ts->realtime); |
1735 | if (r < 0) | |
1736 | return r; | |
feb12d3e | 1737 | #endif |
7560fffc | 1738 | |
64825d3c | 1739 | /* alloca() can't take 0, hence let's allocate at least one */ |
9607d947 | 1740 | items = alloca(sizeof(EntryItem) * MAX(1u, n_iovec)); |
cec736d2 LP |
1741 | |
1742 | for (i = 0; i < n_iovec; i++) { | |
1743 | uint64_t p; | |
1744 | Object *o; | |
1745 | ||
1746 | r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p); | |
1747 | if (r < 0) | |
cf244689 | 1748 | return r; |
cec736d2 LP |
1749 | |
1750 | xor_hash ^= le64toh(o->data.hash); | |
1751 | items[i].object_offset = htole64(p); | |
de7b95cd | 1752 | items[i].hash = o->data.hash; |
cec736d2 LP |
1753 | } |
1754 | ||
1f2da9ec LP |
1755 | /* Order by the position on disk, in order to improve seek |
1756 | * times for rotating media. */ | |
7ff7394d | 1757 | qsort_safe(items, n_iovec, sizeof(EntryItem), entry_item_cmp); |
1f2da9ec | 1758 | |
de190aef | 1759 | r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset); |
cec736d2 | 1760 | |
fa6ac760 LP |
1761 | /* If the memory mapping triggered a SIGBUS then we return an |
1762 | * IO error and ignore the error code passed down to us, since | |
1763 | * it is very likely just an effect of a nullified replacement | |
1764 | * mapping page */ | |
1765 | ||
1766 | if (mmap_cache_got_sigbus(f->mmap, f->fd)) | |
1767 | r = -EIO; | |
1768 | ||
7a24f3bf VC |
1769 | if (f->post_change_timer) |
1770 | schedule_post_change(f); | |
1771 | else | |
1772 | journal_file_post_change(f); | |
50f20cfd | 1773 | |
cec736d2 LP |
1774 | return r; |
1775 | } | |
1776 | ||
a4bcff5b | 1777 | typedef struct ChainCacheItem { |
fb099c8d | 1778 | uint64_t first; /* the array at the beginning of the chain */ |
a4bcff5b LP |
1779 | uint64_t array; /* the cached array */ |
1780 | uint64_t begin; /* the first item in the cached array */ | |
1781 | uint64_t total; /* the total number of items in all arrays before this one in the chain */ | |
f268980d | 1782 | uint64_t last_index; /* the last index we looked at, to optimize locality when bisecting */ |
a4bcff5b LP |
1783 | } ChainCacheItem; |
1784 | ||
1785 | static void chain_cache_put( | |
4743015d | 1786 | OrderedHashmap *h, |
a4bcff5b LP |
1787 | ChainCacheItem *ci, |
1788 | uint64_t first, | |
1789 | uint64_t array, | |
1790 | uint64_t begin, | |
f268980d LP |
1791 | uint64_t total, |
1792 | uint64_t last_index) { | |
a4bcff5b LP |
1793 | |
1794 | if (!ci) { | |
34741aa3 LP |
1795 | /* If the chain item to cache for this chain is the |
1796 | * first one it's not worth caching anything */ | |
1797 | if (array == first) | |
1798 | return; | |
1799 | ||
29433089 | 1800 | if (ordered_hashmap_size(h) >= CHAIN_CACHE_MAX) { |
4743015d | 1801 | ci = ordered_hashmap_steal_first(h); |
29433089 LP |
1802 | assert(ci); |
1803 | } else { | |
a4bcff5b LP |
1804 | ci = new(ChainCacheItem, 1); |
1805 | if (!ci) | |
1806 | return; | |
1807 | } | |
1808 | ||
1809 | ci->first = first; | |
1810 | ||
4743015d | 1811 | if (ordered_hashmap_put(h, &ci->first, ci) < 0) { |
a4bcff5b LP |
1812 | free(ci); |
1813 | return; | |
1814 | } | |
1815 | } else | |
1816 | assert(ci->first == first); | |
1817 | ||
1818 | ci->array = array; | |
1819 | ci->begin = begin; | |
1820 | ci->total = total; | |
f268980d | 1821 | ci->last_index = last_index; |
a4bcff5b LP |
1822 | } |
1823 | ||
f268980d LP |
1824 | static int generic_array_get( |
1825 | JournalFile *f, | |
1826 | uint64_t first, | |
1827 | uint64_t i, | |
1828 | Object **ret, uint64_t *offset) { | |
de190aef | 1829 | |
cec736d2 | 1830 | Object *o; |
a4bcff5b | 1831 | uint64_t p = 0, a, t = 0; |
cec736d2 | 1832 | int r; |
a4bcff5b | 1833 | ChainCacheItem *ci; |
cec736d2 LP |
1834 | |
1835 | assert(f); | |
1836 | ||
de190aef | 1837 | a = first; |
a4bcff5b LP |
1838 | |
1839 | /* Try the chain cache first */ | |
4743015d | 1840 | ci = ordered_hashmap_get(f->chain_cache, &first); |
a4bcff5b LP |
1841 | if (ci && i > ci->total) { |
1842 | a = ci->array; | |
1843 | i -= ci->total; | |
1844 | t = ci->total; | |
1845 | } | |
1846 | ||
de190aef | 1847 | while (a > 0) { |
a4bcff5b | 1848 | uint64_t k; |
cec736d2 | 1849 | |
de190aef LP |
1850 | r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); |
1851 | if (r < 0) | |
1852 | return r; | |
cec736d2 | 1853 | |
a4bcff5b LP |
1854 | k = journal_file_entry_array_n_items(o); |
1855 | if (i < k) { | |
de190aef | 1856 | p = le64toh(o->entry_array.items[i]); |
a4bcff5b | 1857 | goto found; |
cec736d2 LP |
1858 | } |
1859 | ||
a4bcff5b LP |
1860 | i -= k; |
1861 | t += k; | |
de190aef LP |
1862 | a = le64toh(o->entry_array.next_entry_array_offset); |
1863 | } | |
1864 | ||
a4bcff5b LP |
1865 | return 0; |
1866 | ||
1867 | found: | |
1868 | /* Let's cache this item for the next invocation */ | |
af13a6b0 | 1869 | chain_cache_put(f->chain_cache, ci, first, a, le64toh(o->entry_array.items[0]), t, i); |
de190aef LP |
1870 | |
1871 | r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); | |
1872 | if (r < 0) | |
1873 | return r; | |
1874 | ||
1875 | if (ret) | |
1876 | *ret = o; | |
1877 | ||
1878 | if (offset) | |
1879 | *offset = p; | |
1880 | ||
1881 | return 1; | |
1882 | } | |
1883 | ||
f268980d LP |
1884 | static int generic_array_get_plus_one( |
1885 | JournalFile *f, | |
1886 | uint64_t extra, | |
1887 | uint64_t first, | |
1888 | uint64_t i, | |
1889 | Object **ret, uint64_t *offset) { | |
de190aef LP |
1890 | |
1891 | Object *o; | |
1892 | ||
1893 | assert(f); | |
1894 | ||
1895 | if (i == 0) { | |
1896 | int r; | |
1897 | ||
1898 | r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o); | |
cec736d2 LP |
1899 | if (r < 0) |
1900 | return r; | |
1901 | ||
de190aef LP |
1902 | if (ret) |
1903 | *ret = o; | |
cec736d2 | 1904 | |
de190aef LP |
1905 | if (offset) |
1906 | *offset = extra; | |
cec736d2 | 1907 | |
de190aef | 1908 | return 1; |
cec736d2 LP |
1909 | } |
1910 | ||
de190aef LP |
1911 | return generic_array_get(f, first, i-1, ret, offset); |
1912 | } | |
cec736d2 | 1913 | |
de190aef LP |
1914 | enum { |
1915 | TEST_FOUND, | |
1916 | TEST_LEFT, | |
1917 | TEST_RIGHT | |
1918 | }; | |
cec736d2 | 1919 | |
f268980d LP |
1920 | static int generic_array_bisect( |
1921 | JournalFile *f, | |
1922 | uint64_t first, | |
1923 | uint64_t n, | |
1924 | uint64_t needle, | |
1925 | int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle), | |
1926 | direction_t direction, | |
1927 | Object **ret, | |
1928 | uint64_t *offset, | |
1929 | uint64_t *idx) { | |
1930 | ||
1931 | uint64_t a, p, t = 0, i = 0, last_p = 0, last_index = (uint64_t) -1; | |
de190aef LP |
1932 | bool subtract_one = false; |
1933 | Object *o, *array = NULL; | |
1934 | int r; | |
a4bcff5b | 1935 | ChainCacheItem *ci; |
cec736d2 | 1936 | |
de190aef LP |
1937 | assert(f); |
1938 | assert(test_object); | |
cec736d2 | 1939 | |
a4bcff5b | 1940 | /* Start with the first array in the chain */ |
de190aef | 1941 | a = first; |
a4bcff5b | 1942 | |
4743015d | 1943 | ci = ordered_hashmap_get(f->chain_cache, &first); |
a4bcff5b LP |
1944 | if (ci && n > ci->total) { |
1945 | /* Ah, we have iterated this bisection array chain | |
1946 | * previously! Let's see if we can skip ahead in the | |
1947 | * chain, as far as the last time. But we can't jump | |
1948 | * backwards in the chain, so let's check that | |
1949 | * first. */ | |
1950 | ||
1951 | r = test_object(f, ci->begin, needle); | |
1952 | if (r < 0) | |
1953 | return r; | |
1954 | ||
1955 | if (r == TEST_LEFT) { | |
f268980d | 1956 | /* OK, what we are looking for is right of the |
a4bcff5b LP |
1957 | * begin of this EntryArray, so let's jump |
1958 | * straight to previously cached array in the | |
1959 | * chain */ | |
1960 | ||
1961 | a = ci->array; | |
1962 | n -= ci->total; | |
1963 | t = ci->total; | |
f268980d | 1964 | last_index = ci->last_index; |
a4bcff5b LP |
1965 | } |
1966 | } | |
1967 | ||
de190aef LP |
1968 | while (a > 0) { |
1969 | uint64_t left, right, k, lp; | |
1970 | ||
1971 | r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &array); | |
cec736d2 LP |
1972 | if (r < 0) |
1973 | return r; | |
1974 | ||
de190aef LP |
1975 | k = journal_file_entry_array_n_items(array); |
1976 | right = MIN(k, n); | |
1977 | if (right <= 0) | |
1978 | return 0; | |
cec736d2 | 1979 | |
de190aef LP |
1980 | i = right - 1; |
1981 | lp = p = le64toh(array->entry_array.items[i]); | |
1982 | if (p <= 0) | |
1983 | return -EBADMSG; | |
cec736d2 | 1984 | |
de190aef LP |
1985 | r = test_object(f, p, needle); |
1986 | if (r < 0) | |
1987 | return r; | |
cec736d2 | 1988 | |
de190aef LP |
1989 | if (r == TEST_FOUND) |
1990 | r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT; | |
1991 | ||
1992 | if (r == TEST_RIGHT) { | |
1993 | left = 0; | |
1994 | right -= 1; | |
f268980d LP |
1995 | |
1996 | if (last_index != (uint64_t) -1) { | |
1997 | assert(last_index <= right); | |
1998 | ||
1999 | /* If we cached the last index we | |
2000 | * looked at, let's try to not to jump | |
2001 | * too wildly around and see if we can | |
2002 | * limit the range to look at early to | |
2003 | * the immediate neighbors of the last | |
2004 | * index we looked at. */ | |
2005 | ||
2006 | if (last_index > 0) { | |
2007 | uint64_t x = last_index - 1; | |
2008 | ||
2009 | p = le64toh(array->entry_array.items[x]); | |
2010 | if (p <= 0) | |
2011 | return -EBADMSG; | |
2012 | ||
2013 | r = test_object(f, p, needle); | |
2014 | if (r < 0) | |
2015 | return r; | |
2016 | ||
2017 | if (r == TEST_FOUND) | |
2018 | r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT; | |
2019 | ||
2020 | if (r == TEST_RIGHT) | |
2021 | right = x; | |
2022 | else | |
2023 | left = x + 1; | |
2024 | } | |
2025 | ||
2026 | if (last_index < right) { | |
2027 | uint64_t y = last_index + 1; | |
2028 | ||
2029 | p = le64toh(array->entry_array.items[y]); | |
2030 | if (p <= 0) | |
2031 | return -EBADMSG; | |
2032 | ||
2033 | r = test_object(f, p, needle); | |
2034 | if (r < 0) | |
2035 | return r; | |
2036 | ||
2037 | if (r == TEST_FOUND) | |
2038 | r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT; | |
2039 | ||
2040 | if (r == TEST_RIGHT) | |
2041 | right = y; | |
2042 | else | |
2043 | left = y + 1; | |
2044 | } | |
f268980d LP |
2045 | } |
2046 | ||
de190aef LP |
2047 | for (;;) { |
2048 | if (left == right) { | |
2049 | if (direction == DIRECTION_UP) | |
2050 | subtract_one = true; | |
2051 | ||
2052 | i = left; | |
2053 | goto found; | |
2054 | } | |
2055 | ||
2056 | assert(left < right); | |
de190aef | 2057 | i = (left + right) / 2; |
f268980d | 2058 | |
de190aef LP |
2059 | p = le64toh(array->entry_array.items[i]); |
2060 | if (p <= 0) | |
2061 | return -EBADMSG; | |
2062 | ||
2063 | r = test_object(f, p, needle); | |
2064 | if (r < 0) | |
2065 | return r; | |
cec736d2 | 2066 | |
de190aef LP |
2067 | if (r == TEST_FOUND) |
2068 | r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT; | |
2069 | ||
2070 | if (r == TEST_RIGHT) | |
2071 | right = i; | |
2072 | else | |
2073 | left = i + 1; | |
2074 | } | |
2075 | } | |
2076 | ||
2173cbf8 | 2077 | if (k >= n) { |
cbdca852 LP |
2078 | if (direction == DIRECTION_UP) { |
2079 | i = n; | |
2080 | subtract_one = true; | |
2081 | goto found; | |
2082 | } | |
2083 | ||
cec736d2 | 2084 | return 0; |
cbdca852 | 2085 | } |
cec736d2 | 2086 | |
de190aef LP |
2087 | last_p = lp; |
2088 | ||
2089 | n -= k; | |
2090 | t += k; | |
f268980d | 2091 | last_index = (uint64_t) -1; |
de190aef | 2092 | a = le64toh(array->entry_array.next_entry_array_offset); |
cec736d2 LP |
2093 | } |
2094 | ||
2095 | return 0; | |
de190aef LP |
2096 | |
2097 | found: | |
2098 | if (subtract_one && t == 0 && i == 0) | |
2099 | return 0; | |
2100 | ||
a4bcff5b | 2101 | /* Let's cache this item for the next invocation */ |
af13a6b0 | 2102 | 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 | 2103 | |
de190aef LP |
2104 | if (subtract_one && i == 0) |
2105 | p = last_p; | |
2106 | else if (subtract_one) | |
2107 | p = le64toh(array->entry_array.items[i-1]); | |
2108 | else | |
2109 | p = le64toh(array->entry_array.items[i]); | |
2110 | ||
2111 | r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); | |
2112 | if (r < 0) | |
2113 | return r; | |
2114 | ||
2115 | if (ret) | |
2116 | *ret = o; | |
2117 | ||
2118 | if (offset) | |
2119 | *offset = p; | |
2120 | ||
2121 | if (idx) | |
cbdca852 | 2122 | *idx = t + i + (subtract_one ? -1 : 0); |
de190aef LP |
2123 | |
2124 | return 1; | |
cec736d2 LP |
2125 | } |
2126 | ||
f268980d LP |
2127 | static int generic_array_bisect_plus_one( |
2128 | JournalFile *f, | |
2129 | uint64_t extra, | |
2130 | uint64_t first, | |
2131 | uint64_t n, | |
2132 | uint64_t needle, | |
2133 | int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle), | |
2134 | direction_t direction, | |
2135 | Object **ret, | |
2136 | uint64_t *offset, | |
2137 | uint64_t *idx) { | |
de190aef | 2138 | |
cec736d2 | 2139 | int r; |
cbdca852 LP |
2140 | bool step_back = false; |
2141 | Object *o; | |
cec736d2 LP |
2142 | |
2143 | assert(f); | |
de190aef | 2144 | assert(test_object); |
cec736d2 | 2145 | |
de190aef LP |
2146 | if (n <= 0) |
2147 | return 0; | |
cec736d2 | 2148 | |
de190aef LP |
2149 | /* This bisects the array in object 'first', but first checks |
2150 | * an extra */ | |
de190aef LP |
2151 | r = test_object(f, extra, needle); |
2152 | if (r < 0) | |
2153 | return r; | |
a536e261 LP |
2154 | |
2155 | if (r == TEST_FOUND) | |
2156 | r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT; | |
2157 | ||
cbdca852 LP |
2158 | /* if we are looking with DIRECTION_UP then we need to first |
2159 | see if in the actual array there is a matching entry, and | |
2160 | return the last one of that. But if there isn't any we need | |
2161 | to return this one. Hence remember this, and return it | |
2162 | below. */ | |
2163 | if (r == TEST_LEFT) | |
2164 | step_back = direction == DIRECTION_UP; | |
de190aef | 2165 | |
cbdca852 LP |
2166 | if (r == TEST_RIGHT) { |
2167 | if (direction == DIRECTION_DOWN) | |
2168 | goto found; | |
2169 | else | |
2170 | return 0; | |
a536e261 | 2171 | } |
cec736d2 | 2172 | |
de190aef LP |
2173 | r = generic_array_bisect(f, first, n-1, needle, test_object, direction, ret, offset, idx); |
2174 | ||
cbdca852 LP |
2175 | if (r == 0 && step_back) |
2176 | goto found; | |
2177 | ||
ecf68b1d | 2178 | if (r > 0 && idx) |
313cefa1 | 2179 | (*idx)++; |
de190aef LP |
2180 | |
2181 | return r; | |
cbdca852 LP |
2182 | |
2183 | found: | |
2184 | r = journal_file_move_to_object(f, OBJECT_ENTRY, extra, &o); | |
2185 | if (r < 0) | |
2186 | return r; | |
2187 | ||
2188 | if (ret) | |
2189 | *ret = o; | |
2190 | ||
2191 | if (offset) | |
2192 | *offset = extra; | |
2193 | ||
2194 | if (idx) | |
2195 | *idx = 0; | |
2196 | ||
2197 | return 1; | |
2198 | } | |
2199 | ||
44a6b1b6 | 2200 | _pure_ static int test_object_offset(JournalFile *f, uint64_t p, uint64_t needle) { |
cbdca852 LP |
2201 | assert(f); |
2202 | assert(p > 0); | |
2203 | ||
2204 | if (p == needle) | |
2205 | return TEST_FOUND; | |
2206 | else if (p < needle) | |
2207 | return TEST_LEFT; | |
2208 | else | |
2209 | return TEST_RIGHT; | |
2210 | } | |
2211 | ||
de190aef LP |
2212 | static int test_object_seqnum(JournalFile *f, uint64_t p, uint64_t needle) { |
2213 | Object *o; | |
2214 | int r; | |
2215 | ||
2216 | assert(f); | |
2217 | assert(p > 0); | |
2218 | ||
2219 | r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); | |
cec736d2 LP |
2220 | if (r < 0) |
2221 | return r; | |
2222 | ||
de190aef LP |
2223 | if (le64toh(o->entry.seqnum) == needle) |
2224 | return TEST_FOUND; | |
2225 | else if (le64toh(o->entry.seqnum) < needle) | |
2226 | return TEST_LEFT; | |
2227 | else | |
2228 | return TEST_RIGHT; | |
2229 | } | |
cec736d2 | 2230 | |
de190aef LP |
2231 | int journal_file_move_to_entry_by_seqnum( |
2232 | JournalFile *f, | |
2233 | uint64_t seqnum, | |
2234 | direction_t direction, | |
2235 | Object **ret, | |
2236 | uint64_t *offset) { | |
c88cc6af VC |
2237 | assert(f); |
2238 | assert(f->header); | |
de190aef LP |
2239 | |
2240 | return generic_array_bisect(f, | |
2241 | le64toh(f->header->entry_array_offset), | |
2242 | le64toh(f->header->n_entries), | |
2243 | seqnum, | |
2244 | test_object_seqnum, | |
2245 | direction, | |
2246 | ret, offset, NULL); | |
2247 | } | |
cec736d2 | 2248 | |
de190aef LP |
2249 | static int test_object_realtime(JournalFile *f, uint64_t p, uint64_t needle) { |
2250 | Object *o; | |
2251 | int r; | |
2252 | ||
2253 | assert(f); | |
2254 | assert(p > 0); | |
2255 | ||
2256 | r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); | |
2257 | if (r < 0) | |
2258 | return r; | |
2259 | ||
2260 | if (le64toh(o->entry.realtime) == needle) | |
2261 | return TEST_FOUND; | |
2262 | else if (le64toh(o->entry.realtime) < needle) | |
2263 | return TEST_LEFT; | |
2264 | else | |
2265 | return TEST_RIGHT; | |
cec736d2 LP |
2266 | } |
2267 | ||
de190aef LP |
2268 | int journal_file_move_to_entry_by_realtime( |
2269 | JournalFile *f, | |
2270 | uint64_t realtime, | |
2271 | direction_t direction, | |
2272 | Object **ret, | |
2273 | uint64_t *offset) { | |
c88cc6af VC |
2274 | assert(f); |
2275 | assert(f->header); | |
de190aef LP |
2276 | |
2277 | return generic_array_bisect(f, | |
2278 | le64toh(f->header->entry_array_offset), | |
2279 | le64toh(f->header->n_entries), | |
2280 | realtime, | |
2281 | test_object_realtime, | |
2282 | direction, | |
2283 | ret, offset, NULL); | |
2284 | } | |
2285 | ||
2286 | static int test_object_monotonic(JournalFile *f, uint64_t p, uint64_t needle) { | |
2287 | Object *o; | |
2288 | int r; | |
2289 | ||
2290 | assert(f); | |
2291 | assert(p > 0); | |
2292 | ||
2293 | r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); | |
2294 | if (r < 0) | |
2295 | return r; | |
2296 | ||
2297 | if (le64toh(o->entry.monotonic) == needle) | |
2298 | return TEST_FOUND; | |
2299 | else if (le64toh(o->entry.monotonic) < needle) | |
2300 | return TEST_LEFT; | |
2301 | else | |
2302 | return TEST_RIGHT; | |
2303 | } | |
2304 | ||
2a560338 | 2305 | static int find_data_object_by_boot_id( |
47838ab3 ZJS |
2306 | JournalFile *f, |
2307 | sd_id128_t boot_id, | |
2308 | Object **o, | |
2309 | uint64_t *b) { | |
2a560338 | 2310 | |
47838ab3 ZJS |
2311 | char t[sizeof("_BOOT_ID=")-1 + 32 + 1] = "_BOOT_ID="; |
2312 | ||
2313 | sd_id128_to_string(boot_id, t + 9); | |
2314 | return journal_file_find_data_object(f, t, sizeof(t) - 1, o, b); | |
2315 | } | |
2316 | ||
de190aef LP |
2317 | int journal_file_move_to_entry_by_monotonic( |
2318 | JournalFile *f, | |
2319 | sd_id128_t boot_id, | |
2320 | uint64_t monotonic, | |
2321 | direction_t direction, | |
2322 | Object **ret, | |
2323 | uint64_t *offset) { | |
2324 | ||
de190aef LP |
2325 | Object *o; |
2326 | int r; | |
2327 | ||
cbdca852 | 2328 | assert(f); |
de190aef | 2329 | |
47838ab3 | 2330 | r = find_data_object_by_boot_id(f, boot_id, &o, NULL); |
de190aef LP |
2331 | if (r < 0) |
2332 | return r; | |
cbdca852 | 2333 | if (r == 0) |
de190aef LP |
2334 | return -ENOENT; |
2335 | ||
2336 | return generic_array_bisect_plus_one(f, | |
2337 | le64toh(o->data.entry_offset), | |
2338 | le64toh(o->data.entry_array_offset), | |
2339 | le64toh(o->data.n_entries), | |
2340 | monotonic, | |
2341 | test_object_monotonic, | |
2342 | direction, | |
2343 | ret, offset, NULL); | |
2344 | } | |
2345 | ||
1fc605b0 | 2346 | void journal_file_reset_location(JournalFile *f) { |
6573ef05 | 2347 | f->location_type = LOCATION_HEAD; |
1fc605b0 | 2348 | f->current_offset = 0; |
6573ef05 MS |
2349 | f->current_seqnum = 0; |
2350 | f->current_realtime = 0; | |
2351 | f->current_monotonic = 0; | |
2352 | zero(f->current_boot_id); | |
2353 | f->current_xor_hash = 0; | |
2354 | } | |
2355 | ||
950c07d4 | 2356 | void journal_file_save_location(JournalFile *f, Object *o, uint64_t offset) { |
6573ef05 MS |
2357 | f->location_type = LOCATION_SEEK; |
2358 | f->current_offset = offset; | |
2359 | f->current_seqnum = le64toh(o->entry.seqnum); | |
2360 | f->current_realtime = le64toh(o->entry.realtime); | |
2361 | f->current_monotonic = le64toh(o->entry.monotonic); | |
2362 | f->current_boot_id = o->entry.boot_id; | |
2363 | f->current_xor_hash = le64toh(o->entry.xor_hash); | |
1fc605b0 MS |
2364 | } |
2365 | ||
d8ae66d7 MS |
2366 | int journal_file_compare_locations(JournalFile *af, JournalFile *bf) { |
2367 | assert(af); | |
c88cc6af | 2368 | assert(af->header); |
d8ae66d7 | 2369 | assert(bf); |
c88cc6af | 2370 | assert(bf->header); |
d8ae66d7 MS |
2371 | assert(af->location_type == LOCATION_SEEK); |
2372 | assert(bf->location_type == LOCATION_SEEK); | |
2373 | ||
2374 | /* If contents and timestamps match, these entries are | |
2375 | * identical, even if the seqnum does not match */ | |
2376 | if (sd_id128_equal(af->current_boot_id, bf->current_boot_id) && | |
2377 | af->current_monotonic == bf->current_monotonic && | |
2378 | af->current_realtime == bf->current_realtime && | |
2379 | af->current_xor_hash == bf->current_xor_hash) | |
2380 | return 0; | |
2381 | ||
2382 | if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) { | |
2383 | ||
2384 | /* If this is from the same seqnum source, compare | |
2385 | * seqnums */ | |
2386 | if (af->current_seqnum < bf->current_seqnum) | |
2387 | return -1; | |
2388 | if (af->current_seqnum > bf->current_seqnum) | |
2389 | return 1; | |
2390 | ||
2391 | /* Wow! This is weird, different data but the same | |
2392 | * seqnums? Something is borked, but let's make the | |
2393 | * best of it and compare by time. */ | |
2394 | } | |
2395 | ||
2396 | if (sd_id128_equal(af->current_boot_id, bf->current_boot_id)) { | |
2397 | ||
2398 | /* If the boot id matches, compare monotonic time */ | |
2399 | if (af->current_monotonic < bf->current_monotonic) | |
2400 | return -1; | |
2401 | if (af->current_monotonic > bf->current_monotonic) | |
2402 | return 1; | |
2403 | } | |
2404 | ||
2405 | /* Otherwise, compare UTC time */ | |
2406 | if (af->current_realtime < bf->current_realtime) | |
2407 | return -1; | |
2408 | if (af->current_realtime > bf->current_realtime) | |
2409 | return 1; | |
2410 | ||
2411 | /* Finally, compare by contents */ | |
2412 | if (af->current_xor_hash < bf->current_xor_hash) | |
2413 | return -1; | |
2414 | if (af->current_xor_hash > bf->current_xor_hash) | |
2415 | return 1; | |
2416 | ||
2417 | return 0; | |
2418 | } | |
2419 | ||
de190aef LP |
2420 | int journal_file_next_entry( |
2421 | JournalFile *f, | |
f534928a | 2422 | uint64_t p, |
de190aef LP |
2423 | direction_t direction, |
2424 | Object **ret, uint64_t *offset) { | |
2425 | ||
fb099c8d | 2426 | uint64_t i, n, ofs; |
cec736d2 LP |
2427 | int r; |
2428 | ||
2429 | assert(f); | |
c88cc6af | 2430 | assert(f->header); |
de190aef LP |
2431 | |
2432 | n = le64toh(f->header->n_entries); | |
2433 | if (n <= 0) | |
2434 | return 0; | |
cec736d2 | 2435 | |
f534928a | 2436 | if (p == 0) |
de190aef | 2437 | i = direction == DIRECTION_DOWN ? 0 : n - 1; |
cec736d2 | 2438 | else { |
de190aef LP |
2439 | r = generic_array_bisect(f, |
2440 | le64toh(f->header->entry_array_offset), | |
2441 | le64toh(f->header->n_entries), | |
2442 | p, | |
2443 | test_object_offset, | |
2444 | DIRECTION_DOWN, | |
2445 | NULL, NULL, | |
2446 | &i); | |
2447 | if (r <= 0) | |
2448 | return r; | |
2449 | ||
2450 | if (direction == DIRECTION_DOWN) { | |
2451 | if (i >= n - 1) | |
2452 | return 0; | |
2453 | ||
2454 | i++; | |
2455 | } else { | |
2456 | if (i <= 0) | |
2457 | return 0; | |
2458 | ||
2459 | i--; | |
2460 | } | |
cec736d2 LP |
2461 | } |
2462 | ||
de190aef | 2463 | /* And jump to it */ |
fb099c8d ZJS |
2464 | r = generic_array_get(f, |
2465 | le64toh(f->header->entry_array_offset), | |
2466 | i, | |
2467 | ret, &ofs); | |
2468 | if (r <= 0) | |
2469 | return r; | |
2470 | ||
2471 | if (p > 0 && | |
2472 | (direction == DIRECTION_DOWN ? ofs <= p : ofs >= p)) { | |
5d1ce257 | 2473 | log_debug("%s: entry array corrupted at entry %"PRIu64, f->path, i); |
fb099c8d ZJS |
2474 | return -EBADMSG; |
2475 | } | |
2476 | ||
2477 | if (offset) | |
2478 | *offset = ofs; | |
2479 | ||
2480 | return 1; | |
de190aef | 2481 | } |
cec736d2 | 2482 | |
de190aef LP |
2483 | int journal_file_next_entry_for_data( |
2484 | JournalFile *f, | |
2485 | Object *o, uint64_t p, | |
2486 | uint64_t data_offset, | |
2487 | direction_t direction, | |
2488 | Object **ret, uint64_t *offset) { | |
2489 | ||
2490 | uint64_t n, i; | |
cec736d2 | 2491 | int r; |
de190aef | 2492 | Object *d; |
cec736d2 LP |
2493 | |
2494 | assert(f); | |
de190aef | 2495 | assert(p > 0 || !o); |
cec736d2 | 2496 | |
de190aef | 2497 | r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d); |
466ccd92 | 2498 | if (r < 0) |
de190aef | 2499 | return r; |
cec736d2 | 2500 | |
de190aef LP |
2501 | n = le64toh(d->data.n_entries); |
2502 | if (n <= 0) | |
2503 | return n; | |
cec736d2 | 2504 | |
de190aef LP |
2505 | if (!o) |
2506 | i = direction == DIRECTION_DOWN ? 0 : n - 1; | |
2507 | else { | |
2508 | if (o->object.type != OBJECT_ENTRY) | |
2509 | return -EINVAL; | |
cec736d2 | 2510 | |
de190aef LP |
2511 | r = generic_array_bisect_plus_one(f, |
2512 | le64toh(d->data.entry_offset), | |
2513 | le64toh(d->data.entry_array_offset), | |
2514 | le64toh(d->data.n_entries), | |
2515 | p, | |
2516 | test_object_offset, | |
2517 | DIRECTION_DOWN, | |
2518 | NULL, NULL, | |
2519 | &i); | |
2520 | ||
2521 | if (r <= 0) | |
cec736d2 LP |
2522 | return r; |
2523 | ||
de190aef LP |
2524 | if (direction == DIRECTION_DOWN) { |
2525 | if (i >= n - 1) | |
2526 | return 0; | |
cec736d2 | 2527 | |
de190aef LP |
2528 | i++; |
2529 | } else { | |
2530 | if (i <= 0) | |
2531 | return 0; | |
cec736d2 | 2532 | |
de190aef LP |
2533 | i--; |
2534 | } | |
cec736d2 | 2535 | |
de190aef | 2536 | } |
cec736d2 | 2537 | |
de190aef LP |
2538 | return generic_array_get_plus_one(f, |
2539 | le64toh(d->data.entry_offset), | |
2540 | le64toh(d->data.entry_array_offset), | |
2541 | i, | |
2542 | ret, offset); | |
2543 | } | |
cec736d2 | 2544 | |
cbdca852 LP |
2545 | int journal_file_move_to_entry_by_offset_for_data( |
2546 | JournalFile *f, | |
2547 | uint64_t data_offset, | |
2548 | uint64_t p, | |
2549 | direction_t direction, | |
2550 | Object **ret, uint64_t *offset) { | |
2551 | ||
2552 | int r; | |
2553 | Object *d; | |
2554 | ||
2555 | assert(f); | |
2556 | ||
2557 | r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d); | |
2558 | if (r < 0) | |
2559 | return r; | |
2560 | ||
2561 | return generic_array_bisect_plus_one(f, | |
2562 | le64toh(d->data.entry_offset), | |
2563 | le64toh(d->data.entry_array_offset), | |
2564 | le64toh(d->data.n_entries), | |
2565 | p, | |
2566 | test_object_offset, | |
2567 | direction, | |
2568 | ret, offset, NULL); | |
2569 | } | |
2570 | ||
2571 | int journal_file_move_to_entry_by_monotonic_for_data( | |
2572 | JournalFile *f, | |
2573 | uint64_t data_offset, | |
2574 | sd_id128_t boot_id, | |
2575 | uint64_t monotonic, | |
2576 | direction_t direction, | |
2577 | Object **ret, uint64_t *offset) { | |
2578 | ||
cbdca852 LP |
2579 | Object *o, *d; |
2580 | int r; | |
2581 | uint64_t b, z; | |
2582 | ||
2583 | assert(f); | |
2584 | ||
2585 | /* First, seek by time */ | |
47838ab3 | 2586 | r = find_data_object_by_boot_id(f, boot_id, &o, &b); |
cbdca852 LP |
2587 | if (r < 0) |
2588 | return r; | |
2589 | if (r == 0) | |
2590 | return -ENOENT; | |
2591 | ||
2592 | r = generic_array_bisect_plus_one(f, | |
2593 | le64toh(o->data.entry_offset), | |
2594 | le64toh(o->data.entry_array_offset), | |
2595 | le64toh(o->data.n_entries), | |
2596 | monotonic, | |
2597 | test_object_monotonic, | |
2598 | direction, | |
2599 | NULL, &z, NULL); | |
2600 | if (r <= 0) | |
2601 | return r; | |
2602 | ||
2603 | /* And now, continue seeking until we find an entry that | |
2604 | * exists in both bisection arrays */ | |
2605 | ||
2606 | for (;;) { | |
2607 | Object *qo; | |
2608 | uint64_t p, q; | |
2609 | ||
2610 | r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d); | |
2611 | if (r < 0) | |
2612 | return r; | |
2613 | ||
2614 | r = generic_array_bisect_plus_one(f, | |
2615 | le64toh(d->data.entry_offset), | |
2616 | le64toh(d->data.entry_array_offset), | |
2617 | le64toh(d->data.n_entries), | |
2618 | z, | |
2619 | test_object_offset, | |
2620 | direction, | |
2621 | NULL, &p, NULL); | |
2622 | if (r <= 0) | |
2623 | return r; | |
2624 | ||
2625 | r = journal_file_move_to_object(f, OBJECT_DATA, b, &o); | |
2626 | if (r < 0) | |
2627 | return r; | |
2628 | ||
2629 | r = generic_array_bisect_plus_one(f, | |
2630 | le64toh(o->data.entry_offset), | |
2631 | le64toh(o->data.entry_array_offset), | |
2632 | le64toh(o->data.n_entries), | |
2633 | p, | |
2634 | test_object_offset, | |
2635 | direction, | |
2636 | &qo, &q, NULL); | |
2637 | ||
2638 | if (r <= 0) | |
2639 | return r; | |
2640 | ||
2641 | if (p == q) { | |
2642 | if (ret) | |
2643 | *ret = qo; | |
2644 | if (offset) | |
2645 | *offset = q; | |
2646 | ||
2647 | return 1; | |
2648 | } | |
2649 | ||
2650 | z = q; | |
2651 | } | |
cbdca852 LP |
2652 | } |
2653 | ||
de190aef LP |
2654 | int journal_file_move_to_entry_by_seqnum_for_data( |
2655 | JournalFile *f, | |
2656 | uint64_t data_offset, | |
2657 | uint64_t seqnum, | |
2658 | direction_t direction, | |
2659 | Object **ret, uint64_t *offset) { | |
cec736d2 | 2660 | |
de190aef LP |
2661 | Object *d; |
2662 | int r; | |
cec736d2 | 2663 | |
91a31dde LP |
2664 | assert(f); |
2665 | ||
de190aef | 2666 | r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d); |
91a31dde | 2667 | if (r < 0) |
de190aef | 2668 | return r; |
cec736d2 | 2669 | |
de190aef LP |
2670 | return generic_array_bisect_plus_one(f, |
2671 | le64toh(d->data.entry_offset), | |
2672 | le64toh(d->data.entry_array_offset), | |
2673 | le64toh(d->data.n_entries), | |
2674 | seqnum, | |
2675 | test_object_seqnum, | |
2676 | direction, | |
2677 | ret, offset, NULL); | |
2678 | } | |
cec736d2 | 2679 | |
de190aef LP |
2680 | int journal_file_move_to_entry_by_realtime_for_data( |
2681 | JournalFile *f, | |
2682 | uint64_t data_offset, | |
2683 | uint64_t realtime, | |
2684 | direction_t direction, | |
2685 | Object **ret, uint64_t *offset) { | |
2686 | ||
2687 | Object *d; | |
2688 | int r; | |
2689 | ||
91a31dde LP |
2690 | assert(f); |
2691 | ||
de190aef | 2692 | r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &d); |
91a31dde | 2693 | if (r < 0) |
de190aef LP |
2694 | return r; |
2695 | ||
2696 | return generic_array_bisect_plus_one(f, | |
2697 | le64toh(d->data.entry_offset), | |
2698 | le64toh(d->data.entry_array_offset), | |
2699 | le64toh(d->data.n_entries), | |
2700 | realtime, | |
2701 | test_object_realtime, | |
2702 | direction, | |
2703 | ret, offset, NULL); | |
cec736d2 LP |
2704 | } |
2705 | ||
0284adc6 | 2706 | void journal_file_dump(JournalFile *f) { |
7560fffc | 2707 | Object *o; |
7560fffc | 2708 | int r; |
0284adc6 | 2709 | uint64_t p; |
7560fffc LP |
2710 | |
2711 | assert(f); | |
c88cc6af | 2712 | assert(f->header); |
7560fffc | 2713 | |
0284adc6 | 2714 | journal_file_print_header(f); |
7560fffc | 2715 | |
0284adc6 LP |
2716 | p = le64toh(f->header->header_size); |
2717 | while (p != 0) { | |
d05089d8 | 2718 | r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o); |
0284adc6 LP |
2719 | if (r < 0) |
2720 | goto fail; | |
7560fffc | 2721 | |
0284adc6 | 2722 | switch (o->object.type) { |
d98cc1f2 | 2723 | |
0284adc6 LP |
2724 | case OBJECT_UNUSED: |
2725 | printf("Type: OBJECT_UNUSED\n"); | |
2726 | break; | |
d98cc1f2 | 2727 | |
0284adc6 LP |
2728 | case OBJECT_DATA: |
2729 | printf("Type: OBJECT_DATA\n"); | |
2730 | break; | |
7560fffc | 2731 | |
3c1668da LP |
2732 | case OBJECT_FIELD: |
2733 | printf("Type: OBJECT_FIELD\n"); | |
2734 | break; | |
2735 | ||
0284adc6 | 2736 | case OBJECT_ENTRY: |
507f22bd ZJS |
2737 | printf("Type: OBJECT_ENTRY seqnum=%"PRIu64" monotonic=%"PRIu64" realtime=%"PRIu64"\n", |
2738 | le64toh(o->entry.seqnum), | |
2739 | le64toh(o->entry.monotonic), | |
2740 | le64toh(o->entry.realtime)); | |
0284adc6 | 2741 | break; |
7560fffc | 2742 | |
0284adc6 LP |
2743 | case OBJECT_FIELD_HASH_TABLE: |
2744 | printf("Type: OBJECT_FIELD_HASH_TABLE\n"); | |
2745 | break; | |
7560fffc | 2746 | |
0284adc6 LP |
2747 | case OBJECT_DATA_HASH_TABLE: |
2748 | printf("Type: OBJECT_DATA_HASH_TABLE\n"); | |
2749 | break; | |
7560fffc | 2750 | |
0284adc6 LP |
2751 | case OBJECT_ENTRY_ARRAY: |
2752 | printf("Type: OBJECT_ENTRY_ARRAY\n"); | |
2753 | break; | |
7560fffc | 2754 | |
0284adc6 | 2755 | case OBJECT_TAG: |
507f22bd ZJS |
2756 | printf("Type: OBJECT_TAG seqnum=%"PRIu64" epoch=%"PRIu64"\n", |
2757 | le64toh(o->tag.seqnum), | |
2758 | le64toh(o->tag.epoch)); | |
0284adc6 | 2759 | break; |
3c1668da LP |
2760 | |
2761 | default: | |
8facc349 | 2762 | printf("Type: unknown (%i)\n", o->object.type); |
3c1668da | 2763 | break; |
0284adc6 | 2764 | } |
7560fffc | 2765 | |
d89c8fdf ZJS |
2766 | if (o->object.flags & OBJECT_COMPRESSION_MASK) |
2767 | printf("Flags: %s\n", | |
2768 | object_compressed_to_string(o->object.flags & OBJECT_COMPRESSION_MASK)); | |
7560fffc | 2769 | |
0284adc6 LP |
2770 | if (p == le64toh(f->header->tail_object_offset)) |
2771 | p = 0; | |
2772 | else | |
2773 | p = p + ALIGN64(le64toh(o->object.size)); | |
2774 | } | |
7560fffc | 2775 | |
0284adc6 LP |
2776 | return; |
2777 | fail: | |
2778 | log_error("File corrupt"); | |
7560fffc LP |
2779 | } |
2780 | ||
718fe4b1 ZJS |
2781 | static const char* format_timestamp_safe(char *buf, size_t l, usec_t t) { |
2782 | const char *x; | |
2783 | ||
2784 | x = format_timestamp(buf, l, t); | |
2785 | if (x) | |
2786 | return x; | |
2787 | return " --- "; | |
2788 | } | |
2789 | ||
0284adc6 | 2790 | void journal_file_print_header(JournalFile *f) { |
2765b7bb | 2791 | char a[33], b[33], c[33], d[33]; |
ed375beb | 2792 | char x[FORMAT_TIMESTAMP_MAX], y[FORMAT_TIMESTAMP_MAX], z[FORMAT_TIMESTAMP_MAX]; |
a1a03e30 LP |
2793 | struct stat st; |
2794 | char bytes[FORMAT_BYTES_MAX]; | |
7560fffc LP |
2795 | |
2796 | assert(f); | |
c88cc6af | 2797 | assert(f->header); |
7560fffc | 2798 | |
0284adc6 LP |
2799 | printf("File Path: %s\n" |
2800 | "File ID: %s\n" | |
2801 | "Machine ID: %s\n" | |
2802 | "Boot ID: %s\n" | |
2803 | "Sequential Number ID: %s\n" | |
2804 | "State: %s\n" | |
2805 | "Compatible Flags:%s%s\n" | |
d89c8fdf | 2806 | "Incompatible Flags:%s%s%s\n" |
507f22bd ZJS |
2807 | "Header size: %"PRIu64"\n" |
2808 | "Arena size: %"PRIu64"\n" | |
2809 | "Data Hash Table Size: %"PRIu64"\n" | |
2810 | "Field Hash Table Size: %"PRIu64"\n" | |
0284adc6 | 2811 | "Rotate Suggested: %s\n" |
0808b92f LP |
2812 | "Head Sequential Number: %"PRIu64" (%"PRIx64")\n" |
2813 | "Tail Sequential Number: %"PRIu64" (%"PRIx64")\n" | |
2814 | "Head Realtime Timestamp: %s (%"PRIx64")\n" | |
2815 | "Tail Realtime Timestamp: %s (%"PRIx64")\n" | |
2816 | "Tail Monotonic Timestamp: %s (%"PRIx64")\n" | |
507f22bd ZJS |
2817 | "Objects: %"PRIu64"\n" |
2818 | "Entry Objects: %"PRIu64"\n", | |
0284adc6 LP |
2819 | f->path, |
2820 | sd_id128_to_string(f->header->file_id, a), | |
2821 | sd_id128_to_string(f->header->machine_id, b), | |
2822 | sd_id128_to_string(f->header->boot_id, c), | |
2765b7bb | 2823 | sd_id128_to_string(f->header->seqnum_id, d), |
3223f44f LP |
2824 | f->header->state == STATE_OFFLINE ? "OFFLINE" : |
2825 | f->header->state == STATE_ONLINE ? "ONLINE" : | |
2826 | f->header->state == STATE_ARCHIVED ? "ARCHIVED" : "UNKNOWN", | |
8088cbd3 | 2827 | JOURNAL_HEADER_SEALED(f->header) ? " SEALED" : "", |
d89c8fdf ZJS |
2828 | (le32toh(f->header->compatible_flags) & ~HEADER_COMPATIBLE_ANY) ? " ???" : "", |
2829 | JOURNAL_HEADER_COMPRESSED_XZ(f->header) ? " COMPRESSED-XZ" : "", | |
2830 | JOURNAL_HEADER_COMPRESSED_LZ4(f->header) ? " COMPRESSED-LZ4" : "", | |
2831 | (le32toh(f->header->incompatible_flags) & ~HEADER_INCOMPATIBLE_ANY) ? " ???" : "", | |
507f22bd ZJS |
2832 | le64toh(f->header->header_size), |
2833 | le64toh(f->header->arena_size), | |
2834 | le64toh(f->header->data_hash_table_size) / sizeof(HashItem), | |
2835 | le64toh(f->header->field_hash_table_size) / sizeof(HashItem), | |
fb0951b0 | 2836 | yes_no(journal_file_rotate_suggested(f, 0)), |
0808b92f LP |
2837 | le64toh(f->header->head_entry_seqnum), le64toh(f->header->head_entry_seqnum), |
2838 | le64toh(f->header->tail_entry_seqnum), le64toh(f->header->tail_entry_seqnum), | |
2839 | format_timestamp_safe(x, sizeof(x), le64toh(f->header->head_entry_realtime)), le64toh(f->header->head_entry_realtime), | |
2840 | format_timestamp_safe(y, sizeof(y), le64toh(f->header->tail_entry_realtime)), le64toh(f->header->tail_entry_realtime), | |
2841 | format_timespan(z, sizeof(z), le64toh(f->header->tail_entry_monotonic), USEC_PER_MSEC), le64toh(f->header->tail_entry_monotonic), | |
507f22bd ZJS |
2842 | le64toh(f->header->n_objects), |
2843 | le64toh(f->header->n_entries)); | |
7560fffc | 2844 | |
0284adc6 | 2845 | if (JOURNAL_HEADER_CONTAINS(f->header, n_data)) |
507f22bd | 2846 | printf("Data Objects: %"PRIu64"\n" |
0284adc6 | 2847 | "Data Hash Table Fill: %.1f%%\n", |
507f22bd | 2848 | le64toh(f->header->n_data), |
0284adc6 | 2849 | 100.0 * (double) le64toh(f->header->n_data) / ((double) (le64toh(f->header->data_hash_table_size) / sizeof(HashItem)))); |
7560fffc | 2850 | |
0284adc6 | 2851 | if (JOURNAL_HEADER_CONTAINS(f->header, n_fields)) |
507f22bd | 2852 | printf("Field Objects: %"PRIu64"\n" |
0284adc6 | 2853 | "Field Hash Table Fill: %.1f%%\n", |
507f22bd | 2854 | le64toh(f->header->n_fields), |
0284adc6 | 2855 | 100.0 * (double) le64toh(f->header->n_fields) / ((double) (le64toh(f->header->field_hash_table_size) / sizeof(HashItem)))); |
3223f44f LP |
2856 | |
2857 | if (JOURNAL_HEADER_CONTAINS(f->header, n_tags)) | |
507f22bd ZJS |
2858 | printf("Tag Objects: %"PRIu64"\n", |
2859 | le64toh(f->header->n_tags)); | |
3223f44f | 2860 | if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays)) |
507f22bd ZJS |
2861 | printf("Entry Array Objects: %"PRIu64"\n", |
2862 | le64toh(f->header->n_entry_arrays)); | |
a1a03e30 LP |
2863 | |
2864 | if (fstat(f->fd, &st) >= 0) | |
59f448cf | 2865 | printf("Disk usage: %s\n", format_bytes(bytes, sizeof(bytes), (uint64_t) st.st_blocks * 512ULL)); |
7560fffc LP |
2866 | } |
2867 | ||
fc68c929 LP |
2868 | static int journal_file_warn_btrfs(JournalFile *f) { |
2869 | unsigned attrs; | |
2870 | int r; | |
2871 | ||
2872 | assert(f); | |
2873 | ||
2874 | /* Before we write anything, check if the COW logic is turned | |
2875 | * off on btrfs. Given our write pattern that is quite | |
2876 | * unfriendly to COW file systems this should greatly improve | |
2877 | * performance on COW file systems, such as btrfs, at the | |
2878 | * expense of data integrity features (which shouldn't be too | |
2879 | * bad, given that we do our own checksumming). */ | |
2880 | ||
2881 | r = btrfs_is_filesystem(f->fd); | |
2882 | if (r < 0) | |
2883 | return log_warning_errno(r, "Failed to determine if journal is on btrfs: %m"); | |
2884 | if (!r) | |
2885 | return 0; | |
2886 | ||
2887 | r = read_attr_fd(f->fd, &attrs); | |
2888 | if (r < 0) | |
2889 | return log_warning_errno(r, "Failed to read file attributes: %m"); | |
2890 | ||
2891 | if (attrs & FS_NOCOW_FL) { | |
2892 | log_debug("Detected btrfs file system with copy-on-write disabled, all is good."); | |
2893 | return 0; | |
2894 | } | |
2895 | ||
2896 | log_notice("Creating journal file %s on a btrfs file system, and copy-on-write is enabled. " | |
2897 | "This is likely to slow down journal access substantially, please consider turning " | |
2898 | "off the copy-on-write file attribute on the journal directory, using chattr +C.", f->path); | |
2899 | ||
2900 | return 1; | |
2901 | } | |
2902 | ||
0284adc6 | 2903 | int journal_file_open( |
5d1ce257 | 2904 | int fd, |
0284adc6 LP |
2905 | const char *fname, |
2906 | int flags, | |
2907 | mode_t mode, | |
2908 | bool compress, | |
baed47c3 | 2909 | bool seal, |
0284adc6 LP |
2910 | JournalMetrics *metrics, |
2911 | MMapCache *mmap_cache, | |
b58c888f | 2912 | Set *deferred_closes, |
0284adc6 LP |
2913 | JournalFile *template, |
2914 | JournalFile **ret) { | |
7560fffc | 2915 | |
fa6ac760 | 2916 | bool newly_created = false; |
0284adc6 | 2917 | JournalFile *f; |
fa6ac760 | 2918 | void *h; |
0284adc6 | 2919 | int r; |
7560fffc | 2920 | |
0559d3a5 | 2921 | assert(ret); |
5d1ce257 | 2922 | assert(fd >= 0 || fname); |
7560fffc | 2923 | |
0284adc6 LP |
2924 | if ((flags & O_ACCMODE) != O_RDONLY && |
2925 | (flags & O_ACCMODE) != O_RDWR) | |
2926 | return -EINVAL; | |
7560fffc | 2927 | |
5d1ce257 LP |
2928 | if (fname) { |
2929 | if (!endswith(fname, ".journal") && | |
2930 | !endswith(fname, ".journal~")) | |
2931 | return -EINVAL; | |
2932 | } | |
7560fffc | 2933 | |
0284adc6 LP |
2934 | f = new0(JournalFile, 1); |
2935 | if (!f) | |
2936 | return -ENOMEM; | |
7560fffc | 2937 | |
5d1ce257 | 2938 | f->fd = fd; |
0284adc6 | 2939 | f->mode = mode; |
7560fffc | 2940 | |
0284adc6 LP |
2941 | f->flags = flags; |
2942 | f->prot = prot_from_flags(flags); | |
2943 | f->writable = (flags & O_ACCMODE) != O_RDONLY; | |
92261977 | 2944 | #if defined(HAVE_LZ4) |
d89c8fdf ZJS |
2945 | f->compress_lz4 = compress; |
2946 | #elif defined(HAVE_XZ) | |
2947 | f->compress_xz = compress; | |
48b61739 | 2948 | #endif |
49a32d43 | 2949 | #ifdef HAVE_GCRYPT |
baed47c3 | 2950 | f->seal = seal; |
49a32d43 | 2951 | #endif |
7560fffc | 2952 | |
0284adc6 LP |
2953 | if (mmap_cache) |
2954 | f->mmap = mmap_cache_ref(mmap_cache); | |
2955 | else { | |
84168d80 | 2956 | f->mmap = mmap_cache_new(); |
0284adc6 LP |
2957 | if (!f->mmap) { |
2958 | r = -ENOMEM; | |
2959 | goto fail; | |
2960 | } | |
2961 | } | |
7560fffc | 2962 | |
5d1ce257 LP |
2963 | if (fname) |
2964 | f->path = strdup(fname); | |
2965 | else /* If we don't know the path, fill in something explanatory and vaguely useful */ | |
2966 | asprintf(&f->path, "/proc/self/%i", fd); | |
0284adc6 LP |
2967 | if (!f->path) { |
2968 | r = -ENOMEM; | |
2969 | goto fail; | |
2970 | } | |
7560fffc | 2971 | |
4743015d | 2972 | f->chain_cache = ordered_hashmap_new(&uint64_hash_ops); |
a4bcff5b LP |
2973 | if (!f->chain_cache) { |
2974 | r = -ENOMEM; | |
2975 | goto fail; | |
2976 | } | |
2977 | ||
0284adc6 | 2978 | if (f->fd < 0) { |
5d1ce257 LP |
2979 | f->fd = open(f->path, f->flags|O_CLOEXEC, f->mode); |
2980 | if (f->fd < 0) { | |
2981 | r = -errno; | |
2982 | goto fail; | |
2983 | } | |
2984 | ||
2985 | /* fds we opened here by us should also be closed by us. */ | |
2986 | f->close_fd = true; | |
7560fffc | 2987 | } |
7560fffc | 2988 | |
2678031a LP |
2989 | r = journal_file_fstat(f); |
2990 | if (r < 0) | |
0284adc6 | 2991 | goto fail; |
7560fffc | 2992 | |
0284adc6 | 2993 | if (f->last_stat.st_size == 0 && f->writable) { |
11689d2a | 2994 | |
fc68c929 | 2995 | (void) journal_file_warn_btrfs(f); |
11689d2a | 2996 | |
fb0951b0 LP |
2997 | /* Let's attach the creation time to the journal file, |
2998 | * so that the vacuuming code knows the age of this | |
2999 | * file even if the file might end up corrupted one | |
3000 | * day... Ideally we'd just use the creation time many | |
3001 | * file systems maintain for each file, but there is | |
3002 | * currently no usable API to query this, hence let's | |
3003 | * emulate this via extended attributes. If extended | |
3004 | * attributes are not supported we'll just skip this, | |
7517e174 | 3005 | * and rely solely on mtime/atime/ctime of the file. */ |
fb0951b0 | 3006 | |
d61b600d | 3007 | fd_setcrtime(f->fd, 0); |
7560fffc | 3008 | |
feb12d3e | 3009 | #ifdef HAVE_GCRYPT |
0284adc6 | 3010 | /* Try to load the FSPRG state, and if we can't, then |
baed47c3 | 3011 | * just don't do sealing */ |
49a32d43 LP |
3012 | if (f->seal) { |
3013 | r = journal_file_fss_load(f); | |
3014 | if (r < 0) | |
3015 | f->seal = false; | |
3016 | } | |
feb12d3e | 3017 | #endif |
7560fffc | 3018 | |
0284adc6 LP |
3019 | r = journal_file_init_header(f, template); |
3020 | if (r < 0) | |
3021 | goto fail; | |
7560fffc | 3022 | |
2678031a LP |
3023 | r = journal_file_fstat(f); |
3024 | if (r < 0) | |
0284adc6 | 3025 | goto fail; |
fb0951b0 LP |
3026 | |
3027 | newly_created = true; | |
0284adc6 | 3028 | } |
7560fffc | 3029 | |
0284adc6 | 3030 | if (f->last_stat.st_size < (off_t) HEADER_SIZE_MIN) { |
cfb571f3 | 3031 | r = -ENODATA; |
0284adc6 LP |
3032 | goto fail; |
3033 | } | |
7560fffc | 3034 | |
fa6ac760 | 3035 | r = mmap_cache_get(f->mmap, f->fd, f->prot, CONTEXT_HEADER, true, 0, PAGE_ALIGN(sizeof(Header)), &f->last_stat, &h); |
977eaa1e | 3036 | if (r < 0) |
0284adc6 | 3037 | goto fail; |
7560fffc | 3038 | |
fa6ac760 LP |
3039 | f->header = h; |
3040 | ||
0284adc6 | 3041 | if (!newly_created) { |
b58c888f VC |
3042 | if (deferred_closes) |
3043 | journal_file_close_set(deferred_closes); | |
3044 | ||
0284adc6 LP |
3045 | r = journal_file_verify_header(f); |
3046 | if (r < 0) | |
3047 | goto fail; | |
3048 | } | |
7560fffc | 3049 | |
feb12d3e | 3050 | #ifdef HAVE_GCRYPT |
0284adc6 | 3051 | if (!newly_created && f->writable) { |
baed47c3 | 3052 | r = journal_file_fss_load(f); |
0284adc6 LP |
3053 | if (r < 0) |
3054 | goto fail; | |
3055 | } | |
feb12d3e | 3056 | #endif |
cec736d2 LP |
3057 | |
3058 | if (f->writable) { | |
4a92baf3 LP |
3059 | if (metrics) { |
3060 | journal_default_metrics(metrics, f->fd); | |
3061 | f->metrics = *metrics; | |
3062 | } else if (template) | |
3063 | f->metrics = template->metrics; | |
3064 | ||
cec736d2 LP |
3065 | r = journal_file_refresh_header(f); |
3066 | if (r < 0) | |
3067 | goto fail; | |
3068 | } | |
3069 | ||
feb12d3e | 3070 | #ifdef HAVE_GCRYPT |
baed47c3 | 3071 | r = journal_file_hmac_setup(f); |
14d10188 LP |
3072 | if (r < 0) |
3073 | goto fail; | |
feb12d3e | 3074 | #endif |
14d10188 | 3075 | |
cec736d2 | 3076 | if (newly_created) { |
de190aef | 3077 | r = journal_file_setup_field_hash_table(f); |
cec736d2 LP |
3078 | if (r < 0) |
3079 | goto fail; | |
3080 | ||
de190aef | 3081 | r = journal_file_setup_data_hash_table(f); |
cec736d2 LP |
3082 | if (r < 0) |
3083 | goto fail; | |
7560fffc | 3084 | |
feb12d3e | 3085 | #ifdef HAVE_GCRYPT |
7560fffc LP |
3086 | r = journal_file_append_first_tag(f); |
3087 | if (r < 0) | |
3088 | goto fail; | |
feb12d3e | 3089 | #endif |
cec736d2 LP |
3090 | } |
3091 | ||
fa6ac760 LP |
3092 | if (mmap_cache_got_sigbus(f->mmap, f->fd)) { |
3093 | r = -EIO; | |
3094 | goto fail; | |
3095 | } | |
3096 | ||
7a24f3bf | 3097 | if (template && template->post_change_timer) { |
e167d7fd LP |
3098 | r = journal_file_enable_post_change_timer( |
3099 | f, | |
3100 | sd_event_source_get_event(template->post_change_timer), | |
3101 | template->post_change_timer_period); | |
7a24f3bf | 3102 | |
7a24f3bf VC |
3103 | if (r < 0) |
3104 | goto fail; | |
3105 | } | |
3106 | ||
5d1ce257 LP |
3107 | /* The file is opened now successfully, thus we take possesion of any passed in fd. */ |
3108 | f->close_fd = true; | |
3109 | ||
0559d3a5 | 3110 | *ret = f; |
cec736d2 LP |
3111 | return 0; |
3112 | ||
3113 | fail: | |
fa6ac760 LP |
3114 | if (f->fd >= 0 && mmap_cache_got_sigbus(f->mmap, f->fd)) |
3115 | r = -EIO; | |
3116 | ||
69a3a6fd | 3117 | (void) journal_file_close(f); |
cec736d2 LP |
3118 | |
3119 | return r; | |
3120 | } | |
0ac38b70 | 3121 | |
b58c888f | 3122 | int journal_file_rotate(JournalFile **f, bool compress, bool seal, Set *deferred_closes) { |
57535f47 | 3123 | _cleanup_free_ char *p = NULL; |
0ac38b70 LP |
3124 | size_t l; |
3125 | JournalFile *old_file, *new_file = NULL; | |
3126 | int r; | |
3127 | ||
3128 | assert(f); | |
3129 | assert(*f); | |
3130 | ||
3131 | old_file = *f; | |
3132 | ||
3133 | if (!old_file->writable) | |
3134 | return -EINVAL; | |
3135 | ||
5d1ce257 LP |
3136 | /* Is this a journal file that was passed to us as fd? If so, we synthesized a path name for it, and we refuse |
3137 | * rotation, since we don't know the actual path, and couldn't rename the file hence.*/ | |
3138 | if (path_startswith(old_file->path, "/proc/self/fd")) | |
3139 | return -EINVAL; | |
3140 | ||
0ac38b70 LP |
3141 | if (!endswith(old_file->path, ".journal")) |
3142 | return -EINVAL; | |
3143 | ||
3144 | l = strlen(old_file->path); | |
57535f47 ZJS |
3145 | r = asprintf(&p, "%.*s@" SD_ID128_FORMAT_STR "-%016"PRIx64"-%016"PRIx64".journal", |
3146 | (int) l - 8, old_file->path, | |
3147 | SD_ID128_FORMAT_VAL(old_file->header->seqnum_id), | |
3148 | le64toh((*f)->header->head_entry_seqnum), | |
3149 | le64toh((*f)->header->head_entry_realtime)); | |
3150 | if (r < 0) | |
0ac38b70 LP |
3151 | return -ENOMEM; |
3152 | ||
2678031a LP |
3153 | /* Try to rename the file to the archived version. If the file |
3154 | * already was deleted, we'll get ENOENT, let's ignore that | |
3155 | * case. */ | |
0ac38b70 | 3156 | r = rename(old_file->path, p); |
2678031a | 3157 | if (r < 0 && errno != ENOENT) |
0ac38b70 LP |
3158 | return -errno; |
3159 | ||
ccdbaf91 | 3160 | old_file->header->state = STATE_ARCHIVED; |
0ac38b70 | 3161 | |
f27a3864 LP |
3162 | /* Currently, btrfs is not very good with out write patterns |
3163 | * and fragments heavily. Let's defrag our journal files when | |
3164 | * we archive them */ | |
3165 | old_file->defrag_on_close = true; | |
3166 | ||
5d1ce257 | 3167 | 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 |
3168 | |
3169 | if (deferred_closes && | |
3170 | set_put(deferred_closes, old_file) >= 0) | |
3171 | (void) journal_file_set_offline(old_file, false); | |
3172 | else | |
3173 | (void) journal_file_close(old_file); | |
0ac38b70 LP |
3174 | |
3175 | *f = new_file; | |
3176 | return r; | |
3177 | } | |
3178 | ||
9447a7f1 LP |
3179 | int journal_file_open_reliably( |
3180 | const char *fname, | |
3181 | int flags, | |
3182 | mode_t mode, | |
7560fffc | 3183 | bool compress, |
baed47c3 | 3184 | bool seal, |
4a92baf3 | 3185 | JournalMetrics *metrics, |
27370278 | 3186 | MMapCache *mmap_cache, |
b58c888f | 3187 | Set *deferred_closes, |
9447a7f1 LP |
3188 | JournalFile *template, |
3189 | JournalFile **ret) { | |
3190 | ||
3191 | int r; | |
3192 | size_t l; | |
ed375beb | 3193 | _cleanup_free_ char *p = NULL; |
9447a7f1 | 3194 | |
5d1ce257 | 3195 | r = journal_file_open(-1, fname, flags, mode, compress, seal, metrics, mmap_cache, deferred_closes, template, ret); |
288359db ZJS |
3196 | if (!IN_SET(r, |
3197 | -EBADMSG, /* corrupted */ | |
3198 | -ENODATA, /* truncated */ | |
3199 | -EHOSTDOWN, /* other machine */ | |
3200 | -EPROTONOSUPPORT, /* incompatible feature */ | |
3201 | -EBUSY, /* unclean shutdown */ | |
3202 | -ESHUTDOWN, /* already archived */ | |
3203 | -EIO, /* IO error, including SIGBUS on mmap */ | |
3204 | -EIDRM /* File has been deleted */)) | |
9447a7f1 LP |
3205 | return r; |
3206 | ||
3207 | if ((flags & O_ACCMODE) == O_RDONLY) | |
3208 | return r; | |
3209 | ||
3210 | if (!(flags & O_CREAT)) | |
3211 | return r; | |
3212 | ||
7560fffc LP |
3213 | if (!endswith(fname, ".journal")) |
3214 | return r; | |
3215 | ||
5c70eab4 LP |
3216 | /* The file is corrupted. Rotate it away and try it again (but only once) */ |
3217 | ||
9447a7f1 | 3218 | l = strlen(fname); |
d587eca5 | 3219 | if (asprintf(&p, "%.*s@%016"PRIx64 "-%016"PRIx64 ".journal~", |
57535f47 | 3220 | (int) l - 8, fname, |
d587eca5 | 3221 | now(CLOCK_REALTIME), |
9bf3b535 | 3222 | random_u64()) < 0) |
9447a7f1 LP |
3223 | return -ENOMEM; |
3224 | ||
65089b82 | 3225 | if (rename(fname, p) < 0) |
9447a7f1 LP |
3226 | return -errno; |
3227 | ||
f27a3864 LP |
3228 | /* btrfs doesn't cope well with our write pattern and |
3229 | * fragments heavily. Let's defrag all files we rotate */ | |
11689d2a LP |
3230 | |
3231 | (void) chattr_path(p, false, FS_NOCOW_FL); | |
f27a3864 LP |
3232 | (void) btrfs_defrag(p); |
3233 | ||
65089b82 | 3234 | log_warning_errno(r, "File %s corrupted or uncleanly shut down, renaming and replacing.", fname); |
9447a7f1 | 3235 | |
5d1ce257 | 3236 | return journal_file_open(-1, fname, flags, mode, compress, seal, metrics, mmap_cache, deferred_closes, template, ret); |
9447a7f1 LP |
3237 | } |
3238 | ||
cf244689 LP |
3239 | int journal_file_copy_entry(JournalFile *from, JournalFile *to, Object *o, uint64_t p, uint64_t *seqnum, Object **ret, uint64_t *offset) { |
3240 | uint64_t i, n; | |
3241 | uint64_t q, xor_hash = 0; | |
3242 | int r; | |
3243 | EntryItem *items; | |
3244 | dual_timestamp ts; | |
3245 | ||
3246 | assert(from); | |
3247 | assert(to); | |
3248 | assert(o); | |
3249 | assert(p); | |
3250 | ||
3251 | if (!to->writable) | |
3252 | return -EPERM; | |
3253 | ||
3254 | ts.monotonic = le64toh(o->entry.monotonic); | |
3255 | ts.realtime = le64toh(o->entry.realtime); | |
3256 | ||
cf244689 | 3257 | n = journal_file_entry_n_items(o); |
4faa7004 TA |
3258 | /* alloca() can't take 0, hence let's allocate at least one */ |
3259 | items = alloca(sizeof(EntryItem) * MAX(1u, n)); | |
cf244689 LP |
3260 | |
3261 | for (i = 0; i < n; i++) { | |
4fd052ae FC |
3262 | uint64_t l, h; |
3263 | le64_t le_hash; | |
cf244689 LP |
3264 | size_t t; |
3265 | void *data; | |
3266 | Object *u; | |
3267 | ||
3268 | q = le64toh(o->entry.items[i].object_offset); | |
3269 | le_hash = o->entry.items[i].hash; | |
3270 | ||
3271 | r = journal_file_move_to_object(from, OBJECT_DATA, q, &o); | |
3272 | if (r < 0) | |
3273 | return r; | |
3274 | ||
3275 | if (le_hash != o->data.hash) | |
3276 | return -EBADMSG; | |
3277 | ||
3278 | l = le64toh(o->object.size) - offsetof(Object, data.payload); | |
3279 | t = (size_t) l; | |
3280 | ||
3281 | /* We hit the limit on 32bit machines */ | |
3282 | if ((uint64_t) t != l) | |
3283 | return -E2BIG; | |
3284 | ||
d89c8fdf | 3285 | if (o->object.flags & OBJECT_COMPRESSION_MASK) { |
3b1a55e1 | 3286 | #if defined(HAVE_XZ) || defined(HAVE_LZ4) |
a7f7d1bd | 3287 | size_t rsize = 0; |
cf244689 | 3288 | |
d89c8fdf ZJS |
3289 | r = decompress_blob(o->object.flags & OBJECT_COMPRESSION_MASK, |
3290 | o->data.payload, l, &from->compress_buffer, &from->compress_buffer_size, &rsize, 0); | |
3291 | if (r < 0) | |
3292 | return r; | |
cf244689 LP |
3293 | |
3294 | data = from->compress_buffer; | |
3295 | l = rsize; | |
3b1a55e1 ZJS |
3296 | #else |
3297 | return -EPROTONOSUPPORT; | |
3298 | #endif | |
cf244689 LP |
3299 | } else |
3300 | data = o->data.payload; | |
3301 | ||
3302 | r = journal_file_append_data(to, data, l, &u, &h); | |
3303 | if (r < 0) | |
3304 | return r; | |
3305 | ||
3306 | xor_hash ^= le64toh(u->data.hash); | |
3307 | items[i].object_offset = htole64(h); | |
3308 | items[i].hash = u->data.hash; | |
3309 | ||
3310 | r = journal_file_move_to_object(from, OBJECT_ENTRY, p, &o); | |
3311 | if (r < 0) | |
3312 | return r; | |
3313 | } | |
3314 | ||
fa6ac760 LP |
3315 | r = journal_file_append_entry_internal(to, &ts, xor_hash, items, n, seqnum, ret, offset); |
3316 | ||
3317 | if (mmap_cache_got_sigbus(to->mmap, to->fd)) | |
3318 | return -EIO; | |
3319 | ||
3320 | return r; | |
cf244689 | 3321 | } |
babfc091 | 3322 | |
8580d1f7 LP |
3323 | void journal_reset_metrics(JournalMetrics *m) { |
3324 | assert(m); | |
3325 | ||
3326 | /* Set everything to "pick automatic values". */ | |
3327 | ||
3328 | *m = (JournalMetrics) { | |
3329 | .min_use = (uint64_t) -1, | |
3330 | .max_use = (uint64_t) -1, | |
3331 | .min_size = (uint64_t) -1, | |
3332 | .max_size = (uint64_t) -1, | |
3333 | .keep_free = (uint64_t) -1, | |
3334 | .n_max_files = (uint64_t) -1, | |
3335 | }; | |
3336 | } | |
3337 | ||
babfc091 | 3338 | void journal_default_metrics(JournalMetrics *m, int fd) { |
8580d1f7 | 3339 | char a[FORMAT_BYTES_MAX], b[FORMAT_BYTES_MAX], c[FORMAT_BYTES_MAX], d[FORMAT_BYTES_MAX], e[FORMAT_BYTES_MAX]; |
babfc091 | 3340 | struct statvfs ss; |
8580d1f7 | 3341 | uint64_t fs_size; |
babfc091 LP |
3342 | |
3343 | assert(m); | |
3344 | assert(fd >= 0); | |
3345 | ||
3346 | if (fstatvfs(fd, &ss) >= 0) | |
3347 | fs_size = ss.f_frsize * ss.f_blocks; | |
8580d1f7 LP |
3348 | else { |
3349 | log_debug_errno(errno, "Failed to detremine disk size: %m"); | |
3350 | fs_size = 0; | |
3351 | } | |
babfc091 LP |
3352 | |
3353 | if (m->max_use == (uint64_t) -1) { | |
3354 | ||
3355 | if (fs_size > 0) { | |
3356 | m->max_use = PAGE_ALIGN(fs_size / 10); /* 10% of file system size */ | |
3357 | ||
3358 | if (m->max_use > DEFAULT_MAX_USE_UPPER) | |
3359 | m->max_use = DEFAULT_MAX_USE_UPPER; | |
3360 | ||
3361 | if (m->max_use < DEFAULT_MAX_USE_LOWER) | |
3362 | m->max_use = DEFAULT_MAX_USE_LOWER; | |
3363 | } else | |
3364 | m->max_use = DEFAULT_MAX_USE_LOWER; | |
3365 | } else { | |
3366 | m->max_use = PAGE_ALIGN(m->max_use); | |
3367 | ||
8580d1f7 | 3368 | if (m->max_use != 0 && m->max_use < JOURNAL_FILE_SIZE_MIN*2) |
babfc091 LP |
3369 | m->max_use = JOURNAL_FILE_SIZE_MIN*2; |
3370 | } | |
3371 | ||
8580d1f7 LP |
3372 | if (m->min_use == (uint64_t) -1) |
3373 | m->min_use = DEFAULT_MIN_USE; | |
3374 | ||
3375 | if (m->min_use > m->max_use) | |
3376 | m->min_use = m->max_use; | |
3377 | ||
babfc091 LP |
3378 | if (m->max_size == (uint64_t) -1) { |
3379 | m->max_size = PAGE_ALIGN(m->max_use / 8); /* 8 chunks */ | |
3380 | ||
3381 | if (m->max_size > DEFAULT_MAX_SIZE_UPPER) | |
3382 | m->max_size = DEFAULT_MAX_SIZE_UPPER; | |
3383 | } else | |
3384 | m->max_size = PAGE_ALIGN(m->max_size); | |
3385 | ||
8580d1f7 LP |
3386 | if (m->max_size != 0) { |
3387 | if (m->max_size < JOURNAL_FILE_SIZE_MIN) | |
3388 | m->max_size = JOURNAL_FILE_SIZE_MIN; | |
babfc091 | 3389 | |
8580d1f7 LP |
3390 | if (m->max_use != 0 && m->max_size*2 > m->max_use) |
3391 | m->max_use = m->max_size*2; | |
3392 | } | |
babfc091 LP |
3393 | |
3394 | if (m->min_size == (uint64_t) -1) | |
3395 | m->min_size = JOURNAL_FILE_SIZE_MIN; | |
3396 | else { | |
3397 | m->min_size = PAGE_ALIGN(m->min_size); | |
3398 | ||
3399 | if (m->min_size < JOURNAL_FILE_SIZE_MIN) | |
3400 | m->min_size = JOURNAL_FILE_SIZE_MIN; | |
3401 | ||
8580d1f7 | 3402 | if (m->max_size != 0 && m->min_size > m->max_size) |
babfc091 LP |
3403 | m->max_size = m->min_size; |
3404 | } | |
3405 | ||
3406 | if (m->keep_free == (uint64_t) -1) { | |
3407 | ||
3408 | if (fs_size > 0) { | |
8621b110 | 3409 | m->keep_free = PAGE_ALIGN(fs_size * 3 / 20); /* 15% of file system size */ |
babfc091 LP |
3410 | |
3411 | if (m->keep_free > DEFAULT_KEEP_FREE_UPPER) | |
3412 | m->keep_free = DEFAULT_KEEP_FREE_UPPER; | |
3413 | ||
3414 | } else | |
3415 | m->keep_free = DEFAULT_KEEP_FREE; | |
3416 | } | |
3417 | ||
8580d1f7 LP |
3418 | if (m->n_max_files == (uint64_t) -1) |
3419 | m->n_max_files = DEFAULT_N_MAX_FILES; | |
3420 | ||
3421 | log_debug("Fixed min_use=%s max_use=%s max_size=%s min_size=%s keep_free=%s n_max_files=%" PRIu64, | |
3422 | format_bytes(a, sizeof(a), m->min_use), | |
3423 | format_bytes(b, sizeof(b), m->max_use), | |
3424 | format_bytes(c, sizeof(c), m->max_size), | |
3425 | format_bytes(d, sizeof(d), m->min_size), | |
3426 | format_bytes(e, sizeof(e), m->keep_free), | |
3427 | m->n_max_files); | |
babfc091 | 3428 | } |
08984293 LP |
3429 | |
3430 | int journal_file_get_cutoff_realtime_usec(JournalFile *f, usec_t *from, usec_t *to) { | |
08984293 | 3431 | assert(f); |
c88cc6af | 3432 | assert(f->header); |
08984293 LP |
3433 | assert(from || to); |
3434 | ||
3435 | if (from) { | |
162566a4 LP |
3436 | if (f->header->head_entry_realtime == 0) |
3437 | return -ENOENT; | |
08984293 | 3438 | |
162566a4 | 3439 | *from = le64toh(f->header->head_entry_realtime); |
08984293 LP |
3440 | } |
3441 | ||
3442 | if (to) { | |
162566a4 LP |
3443 | if (f->header->tail_entry_realtime == 0) |
3444 | return -ENOENT; | |
08984293 | 3445 | |
162566a4 | 3446 | *to = le64toh(f->header->tail_entry_realtime); |
08984293 LP |
3447 | } |
3448 | ||
3449 | return 1; | |
3450 | } | |
3451 | ||
3452 | int journal_file_get_cutoff_monotonic_usec(JournalFile *f, sd_id128_t boot_id, usec_t *from, usec_t *to) { | |
08984293 LP |
3453 | Object *o; |
3454 | uint64_t p; | |
3455 | int r; | |
3456 | ||
3457 | assert(f); | |
3458 | assert(from || to); | |
3459 | ||
47838ab3 | 3460 | r = find_data_object_by_boot_id(f, boot_id, &o, &p); |
08984293 LP |
3461 | if (r <= 0) |
3462 | return r; | |
3463 | ||
3464 | if (le64toh(o->data.n_entries) <= 0) | |
3465 | return 0; | |
3466 | ||
3467 | if (from) { | |
3468 | r = journal_file_move_to_object(f, OBJECT_ENTRY, le64toh(o->data.entry_offset), &o); | |
3469 | if (r < 0) | |
3470 | return r; | |
3471 | ||
3472 | *from = le64toh(o->entry.monotonic); | |
3473 | } | |
3474 | ||
3475 | if (to) { | |
3476 | r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); | |
3477 | if (r < 0) | |
3478 | return r; | |
3479 | ||
3480 | r = generic_array_get_plus_one(f, | |
3481 | le64toh(o->data.entry_offset), | |
3482 | le64toh(o->data.entry_array_offset), | |
3483 | le64toh(o->data.n_entries)-1, | |
3484 | &o, NULL); | |
3485 | if (r <= 0) | |
3486 | return r; | |
3487 | ||
3488 | *to = le64toh(o->entry.monotonic); | |
3489 | } | |
3490 | ||
3491 | return 1; | |
3492 | } | |
dca6219e | 3493 | |
fb0951b0 | 3494 | bool journal_file_rotate_suggested(JournalFile *f, usec_t max_file_usec) { |
dca6219e | 3495 | assert(f); |
c88cc6af | 3496 | assert(f->header); |
dca6219e LP |
3497 | |
3498 | /* If we gained new header fields we gained new features, | |
3499 | * hence suggest a rotation */ | |
361f9cbc LP |
3500 | if (le64toh(f->header->header_size) < sizeof(Header)) { |
3501 | log_debug("%s uses an outdated header, suggesting rotation.", f->path); | |
dca6219e | 3502 | return true; |
361f9cbc | 3503 | } |
dca6219e LP |
3504 | |
3505 | /* Let's check if the hash tables grew over a certain fill | |
3506 | * level (75%, borrowing this value from Java's hash table | |
3507 | * implementation), and if so suggest a rotation. To calculate | |
3508 | * the fill level we need the n_data field, which only exists | |
3509 | * in newer versions. */ | |
3510 | ||
3511 | if (JOURNAL_HEADER_CONTAINS(f->header, n_data)) | |
361f9cbc | 3512 | if (le64toh(f->header->n_data) * 4ULL > (le64toh(f->header->data_hash_table_size) / sizeof(HashItem)) * 3ULL) { |
507f22bd | 3513 | 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 |
3514 | f->path, |
3515 | 100.0 * (double) le64toh(f->header->n_data) / ((double) (le64toh(f->header->data_hash_table_size) / sizeof(HashItem))), | |
507f22bd ZJS |
3516 | le64toh(f->header->n_data), |
3517 | le64toh(f->header->data_hash_table_size) / sizeof(HashItem), | |
3518 | (unsigned long long) f->last_stat.st_size, | |
3519 | f->last_stat.st_size / le64toh(f->header->n_data)); | |
dca6219e | 3520 | return true; |
361f9cbc | 3521 | } |
dca6219e LP |
3522 | |
3523 | if (JOURNAL_HEADER_CONTAINS(f->header, n_fields)) | |
361f9cbc | 3524 | if (le64toh(f->header->n_fields) * 4ULL > (le64toh(f->header->field_hash_table_size) / sizeof(HashItem)) * 3ULL) { |
507f22bd | 3525 | log_debug("Field hash table of %s has a fill level at %.1f (%"PRIu64" of %"PRIu64" items), suggesting rotation.", |
361f9cbc LP |
3526 | f->path, |
3527 | 100.0 * (double) le64toh(f->header->n_fields) / ((double) (le64toh(f->header->field_hash_table_size) / sizeof(HashItem))), | |
507f22bd ZJS |
3528 | le64toh(f->header->n_fields), |
3529 | le64toh(f->header->field_hash_table_size) / sizeof(HashItem)); | |
dca6219e | 3530 | return true; |
361f9cbc | 3531 | } |
dca6219e | 3532 | |
0598fd4a LP |
3533 | /* Are the data objects properly indexed by field objects? */ |
3534 | if (JOURNAL_HEADER_CONTAINS(f->header, n_data) && | |
3535 | JOURNAL_HEADER_CONTAINS(f->header, n_fields) && | |
3536 | le64toh(f->header->n_data) > 0 && | |
3537 | le64toh(f->header->n_fields) == 0) | |
3538 | return true; | |
3539 | ||
fb0951b0 LP |
3540 | if (max_file_usec > 0) { |
3541 | usec_t t, h; | |
3542 | ||
3543 | h = le64toh(f->header->head_entry_realtime); | |
3544 | t = now(CLOCK_REALTIME); | |
3545 | ||
3546 | if (h > 0 && t > h + max_file_usec) | |
3547 | return true; | |
3548 | } | |
3549 | ||
dca6219e LP |
3550 | return false; |
3551 | } |