1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2011 Lennart Poettering
12 #include "alloc-util.h"
13 #include "dirent-util.h"
16 #include "journal-def.h"
17 #include "journal-file.h"
18 #include "journal-vacuum.h"
19 #include "parse-util.h"
20 #include "string-util.h"
22 #include "xattr-util.h"
35 static int vacuum_compare(const void *_a
, const void *_b
) {
36 const struct vacuum_info
*a
, *b
;
41 if (a
->have_seqnum
&& b
->have_seqnum
&&
42 sd_id128_equal(a
->seqnum_id
, b
->seqnum_id
)) {
43 if (a
->seqnum
< b
->seqnum
)
45 else if (a
->seqnum
> b
->seqnum
)
51 if (a
->realtime
< b
->realtime
)
53 else if (a
->realtime
> b
->realtime
)
55 else if (a
->have_seqnum
&& b
->have_seqnum
)
56 return memcmp(&a
->seqnum_id
, &b
->seqnum_id
, 16);
58 return strcmp(a
->filename
, b
->filename
);
61 static void patch_realtime(
64 const struct stat
*st
,
65 unsigned long long *realtime
) {
69 /* The timestamp was determined by the file name, but let's
70 * see if the file might actually be older than the file name
78 x
= timespec_load(&st
->st_ctim
);
79 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
82 x
= timespec_load(&st
->st_atim
);
83 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
86 x
= timespec_load(&st
->st_mtim
);
87 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
90 /* Let's read the original creation time, if possible. Ideally
91 * we'd just query the creation time the FS might provide, but
92 * unfortunately there's currently no sane API to query
93 * it. Hence let's implement this manually... */
95 if (fd_getcrtime_at(fd
, fn
, &crtime
, 0) >= 0) {
96 if (crtime
< *realtime
)
101 static int journal_file_empty(int dir_fd
, const char *name
) {
102 _cleanup_close_
int fd
;
107 fd
= openat(dir_fd
, name
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NONBLOCK
|O_NOATIME
);
109 /* Maybe failed due to O_NOATIME and lack of privileges? */
110 fd
= openat(dir_fd
, name
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NONBLOCK
);
115 if (fstat(fd
, &st
) < 0)
118 /* If an offline file doesn't even have a header we consider it empty */
119 if (st
.st_size
< (off_t
) sizeof(Header
))
122 /* If the number of entries is empty, we consider it empty, too */
123 n
= pread(fd
, &n_entries
, sizeof(n_entries
), offsetof(Header
, n_entries
));
126 if (n
!= sizeof(n_entries
))
129 return le64toh(n_entries
) <= 0;
132 int journal_directory_vacuum(
133 const char *directory
,
135 uint64_t n_max_files
,
136 usec_t max_retention_usec
,
140 _cleanup_closedir_
DIR *d
= NULL
;
141 struct vacuum_info
*list
= NULL
;
142 unsigned n_list
= 0, i
, n_active_files
= 0;
143 size_t n_allocated
= 0;
144 uint64_t sum
= 0, freed
= 0;
145 usec_t retention_limit
= 0;
146 char sbytes
[FORMAT_BYTES_MAX
];
152 if (max_use
<= 0 && max_retention_usec
<= 0 && n_max_files
<= 0)
155 if (max_retention_usec
> 0) {
156 retention_limit
= now(CLOCK_REALTIME
);
157 if (retention_limit
> max_retention_usec
)
158 retention_limit
-= max_retention_usec
;
160 max_retention_usec
= retention_limit
= 0;
163 d
= opendir(directory
);
167 FOREACH_DIRENT_ALL(de
, d
, r
= -errno
; goto finish
) {
169 unsigned long long seqnum
= 0, realtime
;
170 _cleanup_free_
char *p
= NULL
;
171 sd_id128_t seqnum_id
;
177 if (fstatat(dirfd(d
), de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0) {
178 log_debug_errno(errno
, "Failed to stat file %s while vacuuming, ignoring: %m", de
->d_name
);
182 if (!S_ISREG(st
.st_mode
))
185 q
= strlen(de
->d_name
);
187 if (endswith(de
->d_name
, ".journal")) {
189 /* Vacuum archived files. Active files are
192 if (q
< 1 + 32 + 1 + 16 + 1 + 16 + 8) {
197 if (de
->d_name
[q
-8-16-1] != '-' ||
198 de
->d_name
[q
-8-16-1-16-1] != '-' ||
199 de
->d_name
[q
-8-16-1-16-1-32-1] != '@') {
204 p
= strdup(de
->d_name
);
210 de
->d_name
[q
-8-16-1-16-1] = 0;
211 if (sd_id128_from_string(de
->d_name
+ q
-8-16-1-16-1-32, &seqnum_id
) < 0) {
216 if (sscanf(de
->d_name
+ q
-8-16-1-16, "%16llx-%16llx.journal", &seqnum
, &realtime
) != 2) {
223 } else if (endswith(de
->d_name
, ".journal~")) {
224 unsigned long long tmp
;
226 /* Vacuum corrupted files */
228 if (q
< 1 + 16 + 1 + 16 + 8 + 1) {
233 if (de
->d_name
[q
-1-8-16-1] != '-' ||
234 de
->d_name
[q
-1-8-16-1-16-1] != '@') {
239 p
= strdup(de
->d_name
);
245 if (sscanf(de
->d_name
+ q
-1-8-16-1-16, "%16llx-%16llx.journal~", &realtime
, &tmp
) != 2) {
252 /* We do not vacuum unknown files! */
253 log_debug("Not vacuuming unknown file %s.", de
->d_name
);
257 size
= 512UL * (uint64_t) st
.st_blocks
;
259 r
= journal_file_empty(dirfd(d
), p
);
261 log_debug_errno(r
, "Failed check if %s is empty, ignoring: %m", p
);
265 /* Always vacuum empty non-online files. */
267 r
= unlinkat_deallocate(dirfd(d
), p
, 0);
270 log_full(verbose
? LOG_INFO
: LOG_DEBUG
,
271 "Deleted empty archived journal %s/%s (%s).", directory
, p
, format_bytes(sbytes
, sizeof(sbytes
), size
));
274 } else if (r
!= -ENOENT
)
275 log_warning_errno(r
, "Failed to delete empty archived journal %s/%s: %m", directory
, p
);
280 patch_realtime(dirfd(d
), p
, &st
, &realtime
);
282 if (!GREEDY_REALLOC(list
, n_allocated
, n_list
+ 1)) {
287 list
[n_list
].filename
= TAKE_PTR(p
);
288 list
[n_list
].usage
= size
;
289 list
[n_list
].seqnum
= seqnum
;
290 list
[n_list
].realtime
= realtime
;
291 list
[n_list
].seqnum_id
= seqnum_id
;
292 list
[n_list
].have_seqnum
= have_seqnum
;
298 qsort_safe(list
, n_list
, sizeof(struct vacuum_info
), vacuum_compare
);
300 for (i
= 0; i
< n_list
; i
++) {
303 left
= n_active_files
+ n_list
- i
;
305 if ((max_retention_usec
<= 0 || list
[i
].realtime
>= retention_limit
) &&
306 (max_use
<= 0 || sum
<= max_use
) &&
307 (n_max_files
<= 0 || left
<= n_max_files
))
310 r
= unlinkat_deallocate(dirfd(d
), list
[i
].filename
, 0);
312 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
));
313 freed
+= list
[i
].usage
;
315 if (list
[i
].usage
< sum
)
316 sum
-= list
[i
].usage
;
320 } else if (r
!= -ENOENT
)
321 log_warning_errno(r
, "Failed to delete archived journal %s/%s: %m", directory
, list
[i
].filename
);
324 if (oldest_usec
&& i
< n_list
&& (*oldest_usec
== 0 || list
[i
].realtime
< *oldest_usec
))
325 *oldest_usec
= list
[i
].realtime
;
330 for (i
= 0; i
< n_list
; i
++)
331 free(list
[i
].filename
);
334 log_full(verbose
? LOG_INFO
: LOG_DEBUG
, "Vacuuming done, freed %s of archived journals from %s.", format_bytes(sbytes
, sizeof(sbytes
), freed
), directory
);