|
19 | 19 | #define PAYLOAD_BUFFER_SIZE (16U * 1024U)
|
20 | 20 | #define MINIMUM_HOLE_SIZE (1U * 1024U * 1024U / 2U)
|
21 | 21 |
|
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; |
24 | 24 | int r;
|
25 | 25 |
|
26 |
| - /* truncate excess from the end of archives */ |
27 | 26 | r = journal_file_tail_end_by_pread(f, &p);
|
28 | 27 | if (r < 0)
|
29 | 28 | return log_debug_errno(r, "Failed to determine end of tail object: %m");
|
30 | 29 |
|
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."); |
33 | 40 |
|
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 | + } |
36 | 43 |
|
37 |
| - return journal_file_fstat(f); |
| 44 | + return 0; |
38 | 45 | }
|
39 | 46 |
|
40 | 47 | 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
|
73 | 80 | if (sz < MINIMUM_HOLE_SIZE)
|
74 | 81 | return 0;
|
75 | 82 |
|
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 |
| - |
95 | 83 | if (fallocate(f->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, sz) < 0) {
|
96 | 84 | if (ERRNO_IS_NOT_SUPPORTED(errno))
|
97 | 85 | return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), /* Make recognizable */
|
@@ -192,7 +180,7 @@ static void managed_journal_file_set_offline_internal(ManagedJournalFile *f) {
|
192 | 180 |
|
193 | 181 | case OFFLINE_SYNCING:
|
194 | 182 | if (f->file->archive) {
|
195 |
| - (void) managed_journal_file_truncate(f->file); |
| 183 | + (void) managed_journal_file_end_punch_hole(f->file); |
196 | 184 | (void) managed_journal_file_punch_holes(f->file);
|
197 | 185 | }
|
198 | 186 |
|
|
0 commit comments