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