Skip to content

Commit db14a20

Browse files
DaanDeMeyerorgads
authored andcommitted
journal-file-util: Prefer punching holes instead of truncating
It seems truncating might cause SIGBUS (#24320). Let's play it safe and always prefer punching holes over truncating. (cherry picked from commit f20c07d)
1 parent c5e8029 commit db14a20

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

src/journal/managed-journal-file.c

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,29 @@
1919
#define PAYLOAD_BUFFER_SIZE (16U * 1024U)
2020
#define MINIMUM_HOLE_SIZE (1U * 1024U * 1024U / 2U)
2121

22-
static int managed_journal_file_truncate(JournalFile *f) {
23-
uint64_t p;
22+
static int managed_journal_file_end_punch_hole(JournalFile *f) {
23+
uint64_t p, sz;
2424
int r;
2525

26-
/* truncate excess from the end of archives */
2726
r = journal_file_tail_end_by_pread(f, &p);
2827
if (r < 0)
2928
return log_debug_errno(r, "Failed to determine end of tail object: %m");
3029

31-
/* arena_size can't exceed the file size, ensure it's updated before truncating */
32-
f->header->arena_size = htole64(p - le64toh(f->header->header_size));
30+
assert(p <= (uint64_t) f->last_stat.st_size);
31+
32+
sz = ((uint64_t) f->last_stat.st_size) - p;
33+
if (sz < MINIMUM_HOLE_SIZE)
34+
return 0;
35+
36+
if (fallocate(f->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, p, sz) < 0) {
37+
if (ERRNO_IS_NOT_SUPPORTED(errno))
38+
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), /* Make recognizable */
39+
"Hole punching not supported by backing file system, skipping.");
3340

34-
if (ftruncate(f->fd, p) < 0)
35-
return log_debug_errno(errno, "Failed to truncate %s: %m", f->path);
41+
return log_debug_errno(errno, "Failed to punch hole at end of journal file %s: %m", f->path);
42+
}
3643

37-
return journal_file_fstat(f);
44+
return 0;
3845
}
3946

4047
static int managed_journal_file_entry_array_punch_hole(JournalFile *f, uint64_t p, uint64_t n_entries) {
@@ -73,25 +80,6 @@ static int managed_journal_file_entry_array_punch_hole(JournalFile *f, uint64_t
7380
if (sz < MINIMUM_HOLE_SIZE)
7481
return 0;
7582

76-
if (p == le64toh(f->header->tail_object_offset) && !JOURNAL_HEADER_SEALED(f->header)) {
77-
ssize_t n;
78-
79-
o.object.size = htole64(offset - p);
80-
81-
n = pwrite(f->fd, &o, sizeof(EntryArrayObject), p);
82-
if (n < 0)
83-
return log_debug_errno(errno, "Failed to modify entry array object size: %m");
84-
if ((size_t) n != sizeof(EntryArrayObject))
85-
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Short pwrite() while modifying entry array object size.");
86-
87-
f->header->arena_size = htole64(ALIGN64(offset) - le64toh(f->header->header_size));
88-
89-
if (ftruncate(f->fd, ALIGN64(offset)) < 0)
90-
return log_debug_errno(errno, "Failed to truncate %s: %m", f->path);
91-
92-
return 0;
93-
}
94-
9583
if (fallocate(f->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, sz) < 0) {
9684
if (ERRNO_IS_NOT_SUPPORTED(errno))
9785
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), /* Make recognizable */
@@ -192,7 +180,7 @@ static void managed_journal_file_set_offline_internal(ManagedJournalFile *f) {
192180

193181
case OFFLINE_SYNCING:
194182
if (f->file->archive) {
195-
(void) managed_journal_file_truncate(f->file);
183+
(void) managed_journal_file_end_punch_hole(f->file);
196184
(void) managed_journal_file_punch_holes(f->file);
197185
}
198186

0 commit comments

Comments
 (0)