]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
0284adc6 | 2 | |
0284adc6 LP |
3 | #include <fcntl.h> |
4 | #include <sys/stat.h> | |
0284adc6 | 5 | #include <unistd.h> |
fb0951b0 | 6 | |
07630cea LP |
7 | #include "sd-id128.h" |
8 | ||
b5efdb8a | 9 | #include "alloc-util.h" |
a0956174 | 10 | #include "dirent-util.h" |
3ffd4af2 | 11 | #include "fd-util.h" |
aa892669 | 12 | #include "format-util.h" |
47c073aa | 13 | #include "fs-util.h" |
0284adc6 LP |
14 | #include "journal-def.h" |
15 | #include "journal-file.h" | |
3ffd4af2 | 16 | #include "journal-vacuum.h" |
760877e9 | 17 | #include "sort-util.h" |
07630cea | 18 | #include "string-util.h" |
6761e0a2 | 19 | #include "time-util.h" |
89a5a90c | 20 | #include "xattr-util.h" |
0284adc6 LP |
21 | |
22 | struct vacuum_info { | |
6c142648 | 23 | uint64_t usage; |
0284adc6 LP |
24 | char *filename; |
25 | ||
26 | uint64_t realtime; | |
2e14c544 | 27 | |
0284adc6 LP |
28 | sd_id128_t seqnum_id; |
29 | uint64_t seqnum; | |
0284adc6 LP |
30 | bool have_seqnum; |
31 | }; | |
32 | ||
93bab288 YW |
33 | static int vacuum_compare(const struct vacuum_info *a, const struct vacuum_info *b) { |
34 | int r; | |
0284adc6 LP |
35 | |
36 | if (a->have_seqnum && b->have_seqnum && | |
93bab288 YW |
37 | sd_id128_equal(a->seqnum_id, b->seqnum_id)) |
38 | return CMP(a->seqnum, b->seqnum); | |
0284adc6 | 39 | |
93bab288 YW |
40 | r = CMP(a->realtime, b->realtime); |
41 | if (r != 0) | |
42 | return r; | |
43 | ||
44 | if (a->have_seqnum && b->have_seqnum) | |
0284adc6 | 45 | return memcmp(&a->seqnum_id, &b->seqnum_id, 16); |
93bab288 YW |
46 | |
47 | return strcmp(a->filename, b->filename); | |
0284adc6 LP |
48 | } |
49 | ||
fb0951b0 | 50 | static void patch_realtime( |
2e14c544 | 51 | int fd, |
fb0951b0 LP |
52 | const char *fn, |
53 | const struct stat *st, | |
54 | unsigned long long *realtime) { | |
55 | ||
a7f7d1bd | 56 | usec_t x, crtime = 0; |
fb0951b0 LP |
57 | |
58 | /* The timestamp was determined by the file name, but let's | |
59 | * see if the file might actually be older than the file name | |
60 | * suggested... */ | |
61 | ||
2e14c544 | 62 | assert(fd >= 0); |
fb0951b0 LP |
63 | assert(fn); |
64 | assert(st); | |
65 | assert(realtime); | |
66 | ||
67 | x = timespec_load(&st->st_ctim); | |
3a43da28 | 68 | if (x > 0 && x != USEC_INFINITY && x < *realtime) |
fb0951b0 LP |
69 | *realtime = x; |
70 | ||
71 | x = timespec_load(&st->st_atim); | |
3a43da28 | 72 | if (x > 0 && x != USEC_INFINITY && x < *realtime) |
fb0951b0 LP |
73 | *realtime = x; |
74 | ||
75 | x = timespec_load(&st->st_mtim); | |
3a43da28 | 76 | if (x > 0 && x != USEC_INFINITY && x < *realtime) |
fb0951b0 LP |
77 | *realtime = x; |
78 | ||
fb0951b0 LP |
79 | /* Let's read the original creation time, if possible. Ideally |
80 | * we'd just query the creation time the FS might provide, but | |
81 | * unfortunately there's currently no sane API to query | |
82 | * it. Hence let's implement this manually... */ | |
83 | ||
2e14c544 | 84 | if (fd_getcrtime_at(fd, fn, &crtime, 0) >= 0) { |
4a4d89b6 | 85 | if (crtime < *realtime) |
fb0951b0 LP |
86 | *realtime = crtime; |
87 | } | |
fb0951b0 LP |
88 | } |
89 | ||
9d647740 | 90 | static int journal_file_empty(int dir_fd, const char *name) { |
48979861 | 91 | _cleanup_close_ int fd; |
332076b4 LP |
92 | struct stat st; |
93 | le64_t n_entries; | |
94 | ssize_t n; | |
9d647740 | 95 | |
7b5195e2 LP |
96 | fd = openat(dir_fd, name, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK|O_NOATIME); |
97 | if (fd < 0) { | |
98 | /* Maybe failed due to O_NOATIME and lack of privileges? */ | |
99 | fd = openat(dir_fd, name, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK); | |
100 | if (fd < 0) | |
101 | return -errno; | |
102 | } | |
9d647740 | 103 | |
332076b4 | 104 | if (fstat(fd, &st) < 0) |
9d647740 ZJS |
105 | return -errno; |
106 | ||
332076b4 LP |
107 | /* If an offline file doesn't even have a header we consider it empty */ |
108 | if (st.st_size < (off_t) sizeof(Header)) | |
109 | return 1; | |
110 | ||
111 | /* If the number of entries is empty, we consider it empty, too */ | |
112 | n = pread(fd, &n_entries, sizeof(n_entries), offsetof(Header, n_entries)); | |
113 | if (n < 0) | |
114 | return -errno; | |
115 | if (n != sizeof(n_entries)) | |
116 | return -EIO; | |
9d647740 | 117 | |
332076b4 | 118 | return le64toh(n_entries) <= 0; |
9d647740 ZJS |
119 | } |
120 | ||
fb0951b0 LP |
121 | int journal_directory_vacuum( |
122 | const char *directory, | |
123 | uint64_t max_use, | |
8580d1f7 | 124 | uint64_t n_max_files, |
fb0951b0 | 125 | usec_t max_retention_usec, |
dbd2a83f LP |
126 | usec_t *oldest_usec, |
127 | bool verbose) { | |
fb0951b0 | 128 | |
180e7f4e LP |
129 | uint64_t sum = 0, freed = 0, n_active_files = 0; |
130 | size_t n_list = 0, n_allocated = 0, i; | |
30cb029b | 131 | _cleanup_closedir_ DIR *d = NULL; |
0284adc6 | 132 | struct vacuum_info *list = NULL; |
fb0951b0 | 133 | usec_t retention_limit = 0; |
dbd2a83f | 134 | char sbytes[FORMAT_BYTES_MAX]; |
8580d1f7 LP |
135 | struct dirent *de; |
136 | int r; | |
0284adc6 LP |
137 | |
138 | assert(directory); | |
139 | ||
8580d1f7 | 140 | if (max_use <= 0 && max_retention_usec <= 0 && n_max_files <= 0) |
0284adc6 LP |
141 | return 0; |
142 | ||
6761e0a2 LP |
143 | if (max_retention_usec > 0) |
144 | retention_limit = usec_sub_unsigned(now(CLOCK_REALTIME), max_retention_usec); | |
fb0951b0 | 145 | |
0284adc6 LP |
146 | d = opendir(directory); |
147 | if (!d) | |
148 | return -errno; | |
149 | ||
8580d1f7 LP |
150 | FOREACH_DIRENT_ALL(de, d, r = -errno; goto finish) { |
151 | ||
0284adc6 | 152 | unsigned long long seqnum = 0, realtime; |
8580d1f7 | 153 | _cleanup_free_ char *p = NULL; |
0284adc6 LP |
154 | sd_id128_t seqnum_id; |
155 | bool have_seqnum; | |
8580d1f7 LP |
156 | uint64_t size; |
157 | struct stat st; | |
158 | size_t q; | |
0284adc6 | 159 | |
8580d1f7 LP |
160 | if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { |
161 | log_debug_errno(errno, "Failed to stat file %s while vacuuming, ignoring: %m", de->d_name); | |
0284adc6 | 162 | continue; |
8580d1f7 | 163 | } |
0284adc6 LP |
164 | |
165 | if (!S_ISREG(st.st_mode)) | |
166 | continue; | |
167 | ||
168 | q = strlen(de->d_name); | |
169 | ||
170 | if (endswith(de->d_name, ".journal")) { | |
171 | ||
8580d1f7 LP |
172 | /* Vacuum archived files. Active files are |
173 | * left around */ | |
0284adc6 | 174 | |
8580d1f7 LP |
175 | if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8) { |
176 | n_active_files++; | |
0284adc6 | 177 | continue; |
8580d1f7 | 178 | } |
0284adc6 LP |
179 | |
180 | if (de->d_name[q-8-16-1] != '-' || | |
181 | de->d_name[q-8-16-1-16-1] != '-' || | |
8580d1f7 LP |
182 | de->d_name[q-8-16-1-16-1-32-1] != '@') { |
183 | n_active_files++; | |
0284adc6 | 184 | continue; |
8580d1f7 | 185 | } |
0284adc6 LP |
186 | |
187 | p = strdup(de->d_name); | |
188 | if (!p) { | |
189 | r = -ENOMEM; | |
190 | goto finish; | |
191 | } | |
192 | ||
193 | de->d_name[q-8-16-1-16-1] = 0; | |
194 | if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) { | |
8580d1f7 | 195 | n_active_files++; |
0284adc6 LP |
196 | continue; |
197 | } | |
198 | ||
199 | if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) { | |
8580d1f7 | 200 | n_active_files++; |
0284adc6 LP |
201 | continue; |
202 | } | |
203 | ||
204 | have_seqnum = true; | |
205 | ||
206 | } else if (endswith(de->d_name, ".journal~")) { | |
207 | unsigned long long tmp; | |
208 | ||
209 | /* Vacuum corrupted files */ | |
210 | ||
8580d1f7 | 211 | if (q < 1 + 16 + 1 + 16 + 8 + 1) { |
313cefa1 | 212 | n_active_files++; |
0284adc6 | 213 | continue; |
8580d1f7 | 214 | } |
0284adc6 LP |
215 | |
216 | if (de->d_name[q-1-8-16-1] != '-' || | |
8580d1f7 | 217 | de->d_name[q-1-8-16-1-16-1] != '@') { |
313cefa1 | 218 | n_active_files++; |
0284adc6 | 219 | continue; |
8580d1f7 | 220 | } |
0284adc6 LP |
221 | |
222 | p = strdup(de->d_name); | |
223 | if (!p) { | |
224 | r = -ENOMEM; | |
225 | goto finish; | |
226 | } | |
227 | ||
228 | if (sscanf(de->d_name + q-1-8-16-1-16, "%16llx-%16llx.journal~", &realtime, &tmp) != 2) { | |
313cefa1 | 229 | n_active_files++; |
0284adc6 LP |
230 | continue; |
231 | } | |
232 | ||
233 | have_seqnum = false; | |
8580d1f7 LP |
234 | } else { |
235 | /* We do not vacuum unknown files! */ | |
236 | log_debug("Not vacuuming unknown file %s.", de->d_name); | |
0284adc6 | 237 | continue; |
8580d1f7 | 238 | } |
0284adc6 | 239 | |
8580d1f7 | 240 | size = 512UL * (uint64_t) st.st_blocks; |
9d647740 | 241 | |
8580d1f7 LP |
242 | r = journal_file_empty(dirfd(d), p); |
243 | if (r < 0) { | |
244 | log_debug_errno(r, "Failed check if %s is empty, ignoring: %m", p); | |
245 | continue; | |
246 | } | |
247 | if (r > 0) { | |
248 | /* Always vacuum empty non-online files. */ | |
289f910e | 249 | |
47c073aa LP |
250 | r = unlinkat_deallocate(dirfd(d), p, 0); |
251 | if (r >= 0) { | |
8580d1f7 LP |
252 | |
253 | log_full(verbose ? LOG_INFO : LOG_DEBUG, | |
254 | "Deleted empty archived journal %s/%s (%s).", directory, p, format_bytes(sbytes, sizeof(sbytes), size)); | |
255 | ||
289f910e | 256 | freed += size; |
47c073aa LP |
257 | } else if (r != -ENOENT) |
258 | log_warning_errno(r, "Failed to delete empty archived journal %s/%s: %m", directory, p); | |
289f910e | 259 | |
9d647740 ZJS |
260 | continue; |
261 | } | |
262 | ||
8580d1f7 | 263 | patch_realtime(dirfd(d), p, &st, &realtime); |
fb0951b0 | 264 | |
26d8ff04 | 265 | if (!GREEDY_REALLOC(list, n_allocated, n_list + 1)) { |
26d8ff04 LP |
266 | r = -ENOMEM; |
267 | goto finish; | |
268 | } | |
0284adc6 | 269 | |
ab41da08 LP |
270 | list[n_list++] = (struct vacuum_info) { |
271 | .filename = TAKE_PTR(p), | |
272 | .usage = size, | |
273 | .seqnum = seqnum, | |
274 | .realtime = realtime, | |
275 | .seqnum_id = seqnum_id, | |
276 | .have_seqnum = have_seqnum, | |
277 | }; | |
8580d1f7 | 278 | |
8580d1f7 | 279 | sum += size; |
0284adc6 LP |
280 | } |
281 | ||
93bab288 | 282 | typesafe_qsort(list, n_list, vacuum_compare); |
0284adc6 | 283 | |
fb0951b0 | 284 | for (i = 0; i < n_list; i++) { |
180e7f4e | 285 | uint64_t left; |
8580d1f7 LP |
286 | |
287 | left = n_active_files + n_list - i; | |
288 | ||
fb0951b0 | 289 | if ((max_retention_usec <= 0 || list[i].realtime >= retention_limit) && |
8580d1f7 LP |
290 | (max_use <= 0 || sum <= max_use) && |
291 | (n_max_files <= 0 || left <= n_max_files)) | |
0284adc6 LP |
292 | break; |
293 | ||
47c073aa LP |
294 | r = unlinkat_deallocate(dirfd(d), list[i].filename, 0); |
295 | if (r >= 0) { | |
dbd2a83f | 296 | log_full(verbose ? LOG_INFO : LOG_DEBUG, "Deleted archived journal %s/%s (%s).", directory, list[i].filename, format_bytes(sbytes, sizeof(sbytes), list[i].usage)); |
289f910e | 297 | freed += list[i].usage; |
4fa25d62 | 298 | |
6c142648 | 299 | if (list[i].usage < sum) |
4fa25d62 LP |
300 | sum -= list[i].usage; |
301 | else | |
302 | sum = 0; | |
303 | ||
47c073aa LP |
304 | } else if (r != -ENOENT) |
305 | log_warning_errno(r, "Failed to delete archived journal %s/%s: %m", directory, list[i].filename); | |
0284adc6 LP |
306 | } |
307 | ||
fb0951b0 LP |
308 | if (oldest_usec && i < n_list && (*oldest_usec == 0 || list[i].realtime < *oldest_usec)) |
309 | *oldest_usec = list[i].realtime; | |
310 | ||
8580d1f7 LP |
311 | r = 0; |
312 | ||
0284adc6 LP |
313 | finish: |
314 | for (i = 0; i < n_list; i++) | |
315 | free(list[i].filename); | |
0284adc6 LP |
316 | free(list); |
317 | ||
3cc44bf9 | 318 | log_full(verbose ? LOG_INFO : LOG_DEBUG, "Vacuuming done, freed %s of archived journals from %s.", format_bytes(sbytes, sizeof(sbytes), freed), directory); |
289f910e | 319 | |
0284adc6 LP |
320 | return r; |
321 | } |