Debian Codegraph @

src/shared/journal-file-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <pthread.h>
4 #include <unistd.h>
5
6 #include "sd-event.h"
7
8 #include "alloc-util.h"
9 #include "chattr-util.h"
10 #include "copy.h"
11 #include "errno-util.h"
12 #include "fd-util.h"
13 #include "journal-authenticate.h"
14 #include "journal-file-util.h"
15 #include "journal-internal.h"
16 #include "log.h"
17 #include "log-ratelimit.h"
18 #include "set.h"
19 #include "string-util.h"
20
21 #define PAYLOAD_BUFFER_SIZE (16U * 1024U)
22 #define MINIMUM_HOLE_SIZE (1U * 1024U * 1024U / 2U)
23
24 static int journal_file_end_punch_hole(JournalFile *f) {
25 uint64_t p, sz;
26 int r;
27
28 r = journal_file_tail_end_by_pread(f, &p);
29 if (r < 0)
30 return log_debug_errno(r, "Failed to determine end of tail object: %m");
31
32 assert(p <= (uint64_t) f->last_stat.st_size);
33
34 sz = ((uint64_t) f->last_stat.st_size) - p;
35 if (sz < MINIMUM_HOLE_SIZE)
36 return 0;
37
38 if (fallocate(f->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, p, sz) < 0) {
39 if (ERRNO_IS_NOT_SUPPORTED(errno))
40 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), /* Make recognizable */
41 "Hole punching not supported by backing file system, skipping.");
42
43 return log_debug_errno(errno, "Failed to punch hole at end of journal file %s: %m", f->path);
44 }
45
46 return 0;
47 }
48
49 static int journal_file_entry_array_punch_hole(JournalFile *f, uint64_t p, uint64_t n_entries) {
50 Object o;
51 uint64_t offset, sz, n_items = 0, n_unused;
52 int r;
53
54 if (n_entries == 0)
55 return 0;
56
57 for (uint64_t q = p; q != 0; q = le64toh(o.entry_array.next_entry_array_offset)) {
58 r = journal_file_read_object_header(f, OBJECT_ENTRY_ARRAY, q, &o);
59 if (r < 0)
60 return r;
61
62 n_items += journal_file_entry_array_n_items(f, &o);
63 p = q;
64 }
65
66 if (p == 0)
67 return 0;
68
69 if (n_entries > n_items)
70 return -EBADMSG;
71
72 /* Amount of unused items in the final entry array. */
73 n_unused = n_items - n_entries;
74
75 if (n_unused == 0)
76 return 0;
77
78 offset = p + offsetof(Object, entry_array.items) +
79 (journal_file_entry_array_n_items(f, &o) - n_unused) * journal_file_entry_array_item_size(f);
80 sz = p + le64toh(o.object.size) - offset;
81
82 if (sz < MINIMUM_HOLE_SIZE)
83 return 0;
84
85 if (fallocate(f->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, sz) < 0) {
86 if (ERRNO_IS_NOT_SUPPORTED(errno))
87 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), /* Make recognizable */
88 "Hole punching not supported by backing file system, skipping.");
89
90 return log_debug_errno(errno, "Failed to punch hole in entry array of %s: %m", f->path);
91 }
92
93 return 0;
94 }
95
96 static int journal_file_punch_holes(JournalFile *f) {
97 HashItem items[PAYLOAD_BUFFER_SIZE / sizeof(HashItem)];
98 uint64_t p, sz;
99 ssize_t n = SSIZE_MAX;
100 int r;
101
102 r = journal_file_entry_array_punch_hole(
103 f, le64toh(f->header->entry_array_offset), le64toh(f->header->n_entries));
104 if (r < 0)
105 return r;
106
107 p = le64toh(f->header->data_hash_table_offset);
108 sz = le64toh(f->header->data_hash_table_size);
109
110 for (uint64_t i = p; i < p + sz && n > 0; i += n) {
111 size_t m = MIN(sizeof(items), p + sz - i);
112 n = pread(f->fd, items, m, i);
113 if (n < 0)
114 return log_debug_errno(errno, "Failed to read hash table items: %m");
115
116 /* Let's ignore any partial hash items by rounding down to the nearest multiple of HashItem. */
117 n -= n % sizeof(HashItem);
118
119 for (size_t j = 0; j < (size_t) n / sizeof(HashItem); j++) {
120 Object o;
121
122 for (uint64_t q = le64toh(items[j].head_hash_offset); q != 0;
123 q = le64toh(o.data.next_hash_offset)) {
124
125 r = journal_file_read_object_header(f, OBJECT_DATA, q, &o);
126 if (r < 0) {
127 log_debug_errno(r, "Invalid data object: %m, ignoring");
128 break;
129 }
130
131 if (le64toh(o.data.n_entries) == 0)
132 continue;
133
134 r = journal_file_entry_array_punch_hole(
135 f, le64toh(o.data.entry_array_offset), le64toh(o.data.n_entries) - 1);
136 if (r == -EOPNOTSUPP)
137 return -EOPNOTSUPP;
138
139 /* Ignore other errors */
140 }
141 }
142 }
143
144 return 0;
145 }
146
147 /* This may be called from a separate thread to prevent blocking the caller for the duration of fsync().
148 * As a result we use atomic operations on f->offline_state for inter-thread communications with
149 * journal_file_set_offline() and journal_file_set_online(). */
150 static void journal_file_set_offline_internal(JournalFile *f) {
151 int r;
152
153 assert(f);
154 assert(f->fd >= 0);
155 assert(f->header);
156
157 for (;;) {
158 switch (f->offline_state) {
159 case OFFLINE_CANCEL: {
160 OfflineState tmp_state = OFFLINE_CANCEL;
161 if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_DONE,
162 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
163 continue;
164 }
165 return;
166
167 case OFFLINE_AGAIN_FROM_SYNCING: {
168 OfflineState tmp_state = OFFLINE_AGAIN_FROM_SYNCING;
169 if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_SYNCING,
170 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
171 continue;
172 }
173 break;
174
175 case OFFLINE_AGAIN_FROM_OFFLINING: {
176 OfflineState tmp_state = OFFLINE_AGAIN_FROM_OFFLINING;
177 if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_SYNCING,
178 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
179 continue;
180 }
181 break;
182
183 case OFFLINE_SYNCING:
184 if (f->archive) {
185 (void) journal_file_end_punch_hole(f);
186 (void) journal_file_punch_holes(f);
187 }
188
189 (void) fsync(f->fd);
190
191 {
192 OfflineState tmp_state = OFFLINE_SYNCING;
193 if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_OFFLINING,
194 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
195 continue;
196 }
197
198 f->header->state = f->archive ? STATE_ARCHIVED : STATE_OFFLINE;
199 (void) fsync(f->fd);
200
201 /* If we've archived the journal file, first try to re-enable COW on the file. If the
202 * FS_NOCOW_FL flag was never set or we successfully removed it, continue. If we fail
203 * to remove the flag on the archived file, rewrite the file without the NOCOW flag.
204 * We need this fallback because on some filesystems (BTRFS), the NOCOW flag cannot
205 * be removed after data has been written to a file. The only way to remove it is to
206 * copy all data to a new file without the NOCOW flag set. */
207
208 if (f->archive) {
209 r = chattr_fd(f->fd, 0, FS_NOCOW_FL);
210 if (r >= 0)
211 continue;
212
213 log_debug_errno(r, "Failed to re-enable copy-on-write for %s: %m, rewriting file", f->path);
214
215 /* Here, setting COPY_VERIFY_LINKED flag is crucial. Otherwise, a broken
216 * journal file may be created, if journal_directory_vacuum() ->
217 * unlinkat_deallocate() is called in the main thread while this thread is
218 * copying the file. See issue #24150 and #31222. */
219 r = copy_file_atomic_at_full(
220 f->fd, NULL, AT_FDCWD, f->path, f->mode,
221 0,
222 FS_NOCOW_FL,
223 COPY_REPLACE | COPY_FSYNC | COPY_HOLES | COPY_ALL_XATTRS | COPY_VERIFY_LINKED,
224 NULL, NULL);
225 if (r < 0) {
226 log_debug_errno(r, "Failed to rewrite %s: %m", f->path);
227 continue;
228 }
229 }
230
231 break;
232
233 case OFFLINE_OFFLINING: {
234 OfflineState tmp_state = OFFLINE_OFFLINING;
235 if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_DONE,
236 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
237 continue;
238 }
239 _fallthrough_;
240 case OFFLINE_DONE:
241 return;
242
243 case OFFLINE_JOINED:
244 log_debug("OFFLINE_JOINED unexpected offline state for journal_file_set_offline_internal()");
245 return;
246 }
247 }
248 }
249
250 static void * journal_file_set_offline_thread(void *arg) {
251 JournalFile *f = arg;
252
253 (void) pthread_setname_np(pthread_self(), "journal-offline");
254
255 journal_file_set_offline_internal(f);
256
257 return NULL;
258 }
259
260 /* Trigger a restart if the offline thread is mid-flight in a restartable state. */
261 static bool journal_file_set_offline_try_restart(JournalFile *f) {
262 for (;;) {
263 switch (f->offline_state) {
264 case OFFLINE_AGAIN_FROM_SYNCING:
265 case OFFLINE_AGAIN_FROM_OFFLINING:
266 return true;
267
268 case OFFLINE_CANCEL: {
269 OfflineState tmp_state = OFFLINE_CANCEL;
270 if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_AGAIN_FROM_SYNCING,
271 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
272 continue;
273 }
274 return true;
275
276 case OFFLINE_SYNCING: {
277 OfflineState tmp_state = OFFLINE_SYNCING;
278 if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_AGAIN_FROM_SYNCING,
279 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
280 continue;
281 }
282 return true;
283
284 case OFFLINE_OFFLINING: {
285 OfflineState tmp_state = OFFLINE_OFFLINING;
286 if (!__atomic_compare_exchange_n(&f->offline_state, &tmp_state, OFFLINE_AGAIN_FROM_OFFLINING,
287 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
288 continue;
289 }
290 return true;
291
292 default:
293 return false;
294 }
295 }
296 }
297
298 /* Sets a journal offline.
299 *
300 * If wait is false then an offline is dispatched in a separate thread for a
301 * subsequent journal_file_set_offline() or journal_file_set_online() of the
302 * same journal to synchronize with.
303 *
304 * If wait is true, then either an existing offline thread will be restarted
305 * and joined, or if none exists the offline is simply performed in this
306 * context without involving another thread.
307 */
308 int journal_file_set_offline(JournalFile *f, bool wait) {
309 int target_state;
310 bool restarted;
311 int r;
312
313 assert(f);
314
315 if (!journal_file_writable(f))
316 return -EPERM;
317
318 if (f->fd < 0 || !f->header)
319 return -EINVAL;
320
321 target_state = f->archive ? STATE_ARCHIVED : STATE_OFFLINE;
322
323 log_ratelimit_full(LOG_DEBUG,
324 JOURNAL_LOG_RATELIMIT,
325 "Journal file %s is %s transitioning to %s.",
326 f->path,
327 wait ? "synchronously" : "asynchronously",
328 f->archive ? "archived" : "offline");
329
330 /* An offlining journal is implicitly online and may modify f->header->state,
331 * we must also join any potentially lingering offline thread when already in
332 * the desired offline state.
333 */
334 if (!journal_file_is_offlining(f) && f->header->state == target_state) {
335 log_ratelimit_full(LOG_DEBUG,
336 JOURNAL_LOG_RATELIMIT,
337 "Journal file %s is already %s, waiting for offlining thread.",
338 f->path,
339 f->archive ? "archived" : "offline");
340 return journal_file_set_offline_thread_join(f);
341 }
342
343 /* Restart an in-flight offline thread and wait if needed, or join a lingering done one. */
344 restarted = journal_file_set_offline_try_restart(f);
345 if ((restarted && wait) || !restarted) {
346 r = journal_file_set_offline_thread_join(f);
347 if (r < 0)
348 return r;
349 }
350
351 if (restarted)
352 return 0;
353
354 log_ratelimit_full(LOG_DEBUG,
355 JOURNAL_LOG_RATELIMIT,
356 "Starting new %s offlining operation for journal file %s.",
357 wait ? "synchronous" : "asynchronous",
358 f->path);
359
360 /* Initiate a new offline. */
361 f->offline_state = OFFLINE_SYNCING;
362
363 if (wait) {
364 /* Without using a thread if waiting. */
365 journal_file_set_offline_internal(f);
366
367 assert(f->offline_state == OFFLINE_DONE);
368 f->offline_state = OFFLINE_JOINED;
369
370 } else {
371 sigset_t ss, saved_ss;
372 int k;
373
374 assert_se(sigfillset(&ss) >= 0);
375 /* Don't block SIGBUS since the offlining thread accesses a memory mapped file.
376 * Asynchronous SIGBUS signals can safely be handled by either thread. */
377 assert_se(sigdelset(&ss, SIGBUS) >= 0);
378
379 r = pthread_sigmask(SIG_BLOCK, &ss, &saved_ss);
380 if (r > 0)
381 return -r;
382
383 r = pthread_create(&f->offline_thread, NULL, journal_file_set_offline_thread, f);
384
385 k = pthread_sigmask(SIG_SETMASK, &saved_ss, NULL);
386 if (r > 0) {
387 f->offline_state = OFFLINE_JOINED;
388 return -r;
389 }
390 if (k > 0)
391 return -k;
392 }
393
394 return 0;
395 }
396
397 bool journal_file_is_offlining(JournalFile *f) {
398 assert(f);
399
400 __atomic_thread_fence(__ATOMIC_SEQ_CST);
401
402 if (IN_SET(f->offline_state, OFFLINE_DONE, OFFLINE_JOINED))
403 return false;
404
405 return true;
406 }
407
408 void journal_file_write_final_tag(JournalFile *f) {
409 assert(f);
410 #if HAVE_GCRYPT
411 if (!JOURNAL_HEADER_SEALED(f->header) || !journal_file_writable(f))
412 return;
413
414 int r = journal_file_append_tag(f);
415 if (r < 0)
416 log_debug_errno(r, "Failed to append tag when closing journal: %m");
417 #endif
418 }
419
420 JournalFile* journal_file_offline_close(JournalFile *f) {
421 if (!f)
422 return NULL;
423
424 journal_file_write_final_tag(f);
425
426 if (sd_event_source_get_enabled(f->post_change_timer, NULL) > 0)
427 journal_file_post_change(f);
428 f->post_change_timer = sd_event_source_disable_unref(f->post_change_timer);
429
430 journal_file_set_offline(f, true);
431
432 return journal_file_close(f);
433 }
434
435 JournalFile* journal_file_initiate_close(JournalFile *f, Set *deferred_closes) {
436 int r;
437
438 assert(f);
439
440 if (deferred_closes) {
441 r = set_put(deferred_closes, f);
442 if (r < 0)
443 log_debug_errno(r, "Failed to add file to deferred close set, closing immediately.");
444 else {
445 (void) journal_file_set_offline(f, false);
446 return NULL;
447 }
448 }
449
450 return journal_file_offline_close(f);
451 }
452
453 int journal_file_rotate(
454 JournalFile **f,
455 MMapCache *mmap_cache,
456 JournalFileFlags file_flags,
457 uint64_t compress_threshold_bytes,
458 Set *deferred_closes) {
459
460 _cleanup_free_ char *path = NULL;
461 JournalFile *new_file = NULL;
462 int r;
463
464 assert(f);
465 assert(*f);
466
467 journal_file_write_final_tag(*f);
468 r = journal_file_archive(*f, &path);
469 if (r < 0)
470 return r;
471
472 set_clear(deferred_closes);
473
474 r = journal_file_open(
475 /* fd= */ -EBADF,
476 path,
477 (*f)->open_flags,
478 file_flags,
479 (*f)->mode,
480 compress_threshold_bytes,
481 /* metrics= */ NULL,
482 mmap_cache,
483 /* template= */ *f,
484 &new_file);
485
486 journal_file_initiate_close(*f, deferred_closes);
487 *f = new_file;
488
489 return r;
490 }
491
492 int journal_file_open_reliably(
493 const char *fname,
494 int open_flags,
495 JournalFileFlags file_flags,
496 mode_t mode,
497 uint64_t compress_threshold_bytes,
498 JournalMetrics *metrics,
499 MMapCache *mmap_cache,
500 JournalFile **ret) {
501
502 _cleanup_(journal_file_offline_closep) JournalFile *old_file = NULL;
503 int r;
504
505 r = journal_file_open(
506 /* fd= */ -EBADF,
507 fname,
508 open_flags,
509 file_flags,
510 mode,
511 compress_threshold_bytes,
512 metrics,
513 mmap_cache,
514 /* template= */ NULL,
515 ret);
516 if (!IN_SET(r,
517 -EBADMSG, /* Corrupted */
518 -EADDRNOTAVAIL, /* Referenced object offset out of bounds */
519 -ENODATA, /* Truncated */
520 -EHOSTDOWN, /* Other machine */
521 -EPROTONOSUPPORT, /* Incompatible feature */
522 -EBUSY, /* Unclean shutdown */
523 -ESHUTDOWN, /* Already archived */
524 -EIO, /* IO error, including SIGBUS on mmap */
525 -EIDRM)) /* File has been deleted */
526 return r;
527
528 if ((open_flags & O_ACCMODE_STRICT) == O_RDONLY)
529 return r;
530
531 if (!(open_flags & O_CREAT))
532 return r;
533
534 if (!endswith(fname, ".journal"))
535 return r;
536
537 /* The file is corrupted. Rotate it away and try it again (but only once) */
538 log_warning_errno(r, "File %s corrupted or uncleanly shut down, renaming and replacing.", fname);
539
540 /* The file is corrupted. Try opening it read-only as the template before rotating to inherit its
541 * sequence number and ID. */
542 r = journal_file_open(-EBADF, fname,
543 (open_flags & ~(O_ACCMODE_STRICT|O_CREAT|O_EXCL)) | O_RDONLY,
544 file_flags, 0, compress_threshold_bytes, NULL,
545 mmap_cache, /* template= */ NULL, &old_file);
546 if (r < 0)
547 log_debug_errno(r, "Failed to continue sequence from file %s, ignoring: %m", fname);
548
549 r = journal_file_dispose(AT_FDCWD, fname);
550 if (r < 0)
551 return r;
552
553 return journal_file_open(-EBADF, fname, open_flags, file_flags, mode, compress_threshold_bytes, metrics,
554 mmap_cache, /* template= */ old_file, ret);
555 }
556
557 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
558 journal_file_hash_ops_offline_close,
559 void, trivial_hash_func, trivial_compare_func,
560 JournalFile, journal_file_offline_close);