1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "dirent-util.h"
13 #include "journal-def.h"
14 #include "journal-file.h"
15 #include "journal-vacuum.h"
16 #include "parse-util.h"
17 #include "string-util.h"
19 #include "xattr-util.h"
32 static int vacuum_compare(const struct vacuum_info
*a
, const struct vacuum_info
*b
) {
35 if (a
->have_seqnum
&& b
->have_seqnum
&&
36 sd_id128_equal(a
->seqnum_id
, b
->seqnum_id
))
37 return CMP(a
->seqnum
, b
->seqnum
);
39 r
= CMP(a
->realtime
, b
->realtime
);
43 if (a
->have_seqnum
&& b
->have_seqnum
)
44 return memcmp(&a
->seqnum_id
, &b
->seqnum_id
, 16);
46 return strcmp(a
->filename
, b
->filename
);
49 static void patch_realtime(
52 const struct stat
*st
,
53 unsigned long long *realtime
) {
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
66 x
= timespec_load(&st
->st_ctim
);
67 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
70 x
= timespec_load(&st
->st_atim
);
71 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
74 x
= timespec_load(&st
->st_mtim
);
75 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
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... */
83 if (fd_getcrtime_at(fd
, fn
, &crtime
, 0) >= 0) {
84 if (crtime
< *realtime
)
89 static int journal_file_empty(int dir_fd
, const char *name
) {
90 _cleanup_close_
int fd
;
95 fd
= openat(dir_fd
, name
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NONBLOCK
|O_NOATIME
);
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
);
103 if (fstat(fd
, &st
) < 0)
106 /* If an offline file doesn't even have a header we consider it empty */
107 if (st
.st_size
< (off_t
) sizeof(Header
))
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
));
114 if (n
!= sizeof(n_entries
))
117 return le64toh(n_entries
) <= 0;
120 int journal_directory_vacuum(
121 const char *directory
,
123 uint64_t n_max_files
,
124 usec_t max_retention_usec
,
128 _cleanup_closedir_
DIR *d
= NULL
;
129 struct vacuum_info
*list
= NULL
;
130 unsigned n_list
= 0, i
, n_active_files
= 0;
131 size_t n_allocated
= 0;
132 uint64_t sum
= 0, freed
= 0;
133 usec_t retention_limit
= 0;
134 char sbytes
[FORMAT_BYTES_MAX
];
140 if (max_use
<= 0 && max_retention_usec
<= 0 && n_max_files
<= 0)
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
;
148 max_retention_usec
= retention_limit
= 0;
151 d
= opendir(directory
);
155 FOREACH_DIRENT_ALL(de
, d
, r
= -errno
; goto finish
) {
157 unsigned long long seqnum
= 0, realtime
;
158 _cleanup_free_
char *p
= NULL
;
159 sd_id128_t seqnum_id
;
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
);
170 if (!S_ISREG(st
.st_mode
))
173 q
= strlen(de
->d_name
);
175 if (endswith(de
->d_name
, ".journal")) {
177 /* Vacuum archived files. Active files are
180 if (q
< 1 + 32 + 1 + 16 + 1 + 16 + 8) {
185 if (de
->d_name
[q
-8-16-1] != '-' ||
186 de
->d_name
[q
-8-16-1-16-1] != '-' ||
187 de
->d_name
[q
-8-16-1-16-1-32-1] != '@') {
192 p
= strdup(de
->d_name
);
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) {
204 if (sscanf(de
->d_name
+ q
-8-16-1-16, "%16llx-%16llx.journal", &seqnum
, &realtime
) != 2) {
211 } else if (endswith(de
->d_name
, ".journal~")) {
212 unsigned long long tmp
;
214 /* Vacuum corrupted files */
216 if (q
< 1 + 16 + 1 + 16 + 8 + 1) {
221 if (de
->d_name
[q
-1-8-16-1] != '-' ||
222 de
->d_name
[q
-1-8-16-1-16-1] != '@') {
227 p
= strdup(de
->d_name
);
233 if (sscanf(de
->d_name
+ q
-1-8-16-1-16, "%16llx-%16llx.journal~", &realtime
, &tmp
) != 2) {
240 /* We do not vacuum unknown files! */
241 log_debug("Not vacuuming unknown file %s.", de
->d_name
);
245 size
= 512UL * (uint64_t) st
.st_blocks
;
247 r
= journal_file_empty(dirfd(d
), p
);
249 log_debug_errno(r
, "Failed check if %s is empty, ignoring: %m", p
);
253 /* Always vacuum empty non-online files. */
255 r
= unlinkat_deallocate(dirfd(d
), p
, 0);
258 log_full(verbose
? LOG_INFO
: LOG_DEBUG
,
259 "Deleted empty archived journal %s/%s (%s).", directory
, p
, format_bytes(sbytes
, sizeof(sbytes
), size
));
262 } else if (r
!= -ENOENT
)
263 log_warning_errno(r
, "Failed to delete empty archived journal %s/%s: %m", directory
, p
);
268 patch_realtime(dirfd(d
), p
, &st
, &realtime
);
270 if (!GREEDY_REALLOC(list
, n_allocated
, n_list
+ 1)) {
275 list
[n_list
].filename
= TAKE_PTR(p
);
276 list
[n_list
].usage
= size
;
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
;
286 typesafe_qsort(list
, n_list
, vacuum_compare
);
288 for (i
= 0; i
< n_list
; i
++) {
291 left
= n_active_files
+ n_list
- i
;
293 if ((max_retention_usec
<= 0 || list
[i
].realtime
>= retention_limit
) &&
294 (max_use
<= 0 || sum
<= max_use
) &&
295 (n_max_files
<= 0 || left
<= n_max_files
))
298 r
= unlinkat_deallocate(dirfd(d
), list
[i
].filename
, 0);
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
));
301 freed
+= list
[i
].usage
;
303 if (list
[i
].usage
< sum
)
304 sum
-= list
[i
].usage
;
308 } else if (r
!= -ENOENT
)
309 log_warning_errno(r
, "Failed to delete archived journal %s/%s: %m", directory
, list
[i
].filename
);
312 if (oldest_usec
&& i
< n_list
&& (*oldest_usec
== 0 || list
[i
].realtime
< *oldest_usec
))
313 *oldest_usec
= list
[i
].realtime
;
318 for (i
= 0; i
< n_list
; i
++)
319 free(list
[i
].filename
);
322 log_full(verbose
? LOG_INFO
: LOG_DEBUG
, "Vacuuming done, freed %s of archived journals from %s.", format_bytes(sbytes
, sizeof(sbytes
), freed
), directory
);