1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
14 #include "alloc-util.h"
15 #include "dirent-util.h"
18 #include "journal-def.h"
19 #include "journal-file.h"
20 #include "journal-vacuum.h"
21 #include "parse-util.h"
22 #include "string-util.h"
24 #include "xattr-util.h"
37 static int vacuum_compare(const void *_a
, const void *_b
) {
38 const struct vacuum_info
*a
, *b
;
43 if (a
->have_seqnum
&& b
->have_seqnum
&&
44 sd_id128_equal(a
->seqnum_id
, b
->seqnum_id
)) {
45 if (a
->seqnum
< b
->seqnum
)
47 else if (a
->seqnum
> b
->seqnum
)
53 if (a
->realtime
< b
->realtime
)
55 else if (a
->realtime
> b
->realtime
)
57 else if (a
->have_seqnum
&& b
->have_seqnum
)
58 return memcmp(&a
->seqnum_id
, &b
->seqnum_id
, 16);
60 return strcmp(a
->filename
, b
->filename
);
63 static void patch_realtime(
66 const struct stat
*st
,
67 unsigned long long *realtime
) {
71 /* The timestamp was determined by the file name, but let's
72 * see if the file might actually be older than the file name
80 x
= timespec_load(&st
->st_ctim
);
81 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
84 x
= timespec_load(&st
->st_atim
);
85 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
88 x
= timespec_load(&st
->st_mtim
);
89 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
92 /* Let's read the original creation time, if possible. Ideally
93 * we'd just query the creation time the FS might provide, but
94 * unfortunately there's currently no sane API to query
95 * it. Hence let's implement this manually... */
97 if (fd_getcrtime_at(fd
, fn
, &crtime
, 0) >= 0) {
98 if (crtime
< *realtime
)
103 static int journal_file_empty(int dir_fd
, const char *name
) {
104 _cleanup_close_
int fd
;
109 fd
= openat(dir_fd
, name
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NONBLOCK
|O_NOATIME
);
111 /* Maybe failed due to O_NOATIME and lack of privileges? */
112 fd
= openat(dir_fd
, name
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NONBLOCK
);
117 if (fstat(fd
, &st
) < 0)
120 /* If an offline file doesn't even have a header we consider it empty */
121 if (st
.st_size
< (off_t
) sizeof(Header
))
124 /* If the number of entries is empty, we consider it empty, too */
125 n
= pread(fd
, &n_entries
, sizeof(n_entries
), offsetof(Header
, n_entries
));
128 if (n
!= sizeof(n_entries
))
131 return le64toh(n_entries
) <= 0;
134 int journal_directory_vacuum(
135 const char *directory
,
137 uint64_t n_max_files
,
138 usec_t max_retention_usec
,
142 _cleanup_closedir_
DIR *d
= NULL
;
143 struct vacuum_info
*list
= NULL
;
144 unsigned n_list
= 0, i
, n_active_files
= 0;
145 size_t n_allocated
= 0;
146 uint64_t sum
= 0, freed
= 0;
147 usec_t retention_limit
= 0;
148 char sbytes
[FORMAT_BYTES_MAX
];
154 if (max_use
<= 0 && max_retention_usec
<= 0 && n_max_files
<= 0)
157 if (max_retention_usec
> 0) {
158 retention_limit
= now(CLOCK_REALTIME
);
159 if (retention_limit
> max_retention_usec
)
160 retention_limit
-= max_retention_usec
;
162 max_retention_usec
= retention_limit
= 0;
165 d
= opendir(directory
);
169 FOREACH_DIRENT_ALL(de
, d
, r
= -errno
; goto finish
) {
171 unsigned long long seqnum
= 0, realtime
;
172 _cleanup_free_
char *p
= NULL
;
173 sd_id128_t seqnum_id
;
179 if (fstatat(dirfd(d
), de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0) {
180 log_debug_errno(errno
, "Failed to stat file %s while vacuuming, ignoring: %m", de
->d_name
);
184 if (!S_ISREG(st
.st_mode
))
187 q
= strlen(de
->d_name
);
189 if (endswith(de
->d_name
, ".journal")) {
191 /* Vacuum archived files. Active files are
194 if (q
< 1 + 32 + 1 + 16 + 1 + 16 + 8) {
199 if (de
->d_name
[q
-8-16-1] != '-' ||
200 de
->d_name
[q
-8-16-1-16-1] != '-' ||
201 de
->d_name
[q
-8-16-1-16-1-32-1] != '@') {
206 p
= strdup(de
->d_name
);
212 de
->d_name
[q
-8-16-1-16-1] = 0;
213 if (sd_id128_from_string(de
->d_name
+ q
-8-16-1-16-1-32, &seqnum_id
) < 0) {
218 if (sscanf(de
->d_name
+ q
-8-16-1-16, "%16llx-%16llx.journal", &seqnum
, &realtime
) != 2) {
225 } else if (endswith(de
->d_name
, ".journal~")) {
226 unsigned long long tmp
;
228 /* Vacuum corrupted files */
230 if (q
< 1 + 16 + 1 + 16 + 8 + 1) {
235 if (de
->d_name
[q
-1-8-16-1] != '-' ||
236 de
->d_name
[q
-1-8-16-1-16-1] != '@') {
241 p
= strdup(de
->d_name
);
247 if (sscanf(de
->d_name
+ q
-1-8-16-1-16, "%16llx-%16llx.journal~", &realtime
, &tmp
) != 2) {
254 /* We do not vacuum unknown files! */
255 log_debug("Not vacuuming unknown file %s.", de
->d_name
);
259 size
= 512UL * (uint64_t) st
.st_blocks
;
261 r
= journal_file_empty(dirfd(d
), p
);
263 log_debug_errno(r
, "Failed check if %s is empty, ignoring: %m", p
);
267 /* Always vacuum empty non-online files. */
269 r
= unlinkat_deallocate(dirfd(d
), p
, 0);
272 log_full(verbose
? LOG_INFO
: LOG_DEBUG
,
273 "Deleted empty archived journal %s/%s (%s).", directory
, p
, format_bytes(sbytes
, sizeof(sbytes
), size
));
276 } else if (r
!= -ENOENT
)
277 log_warning_errno(r
, "Failed to delete empty archived journal %s/%s: %m", directory
, p
);
282 patch_realtime(dirfd(d
), p
, &st
, &realtime
);
284 if (!GREEDY_REALLOC(list
, n_allocated
, n_list
+ 1)) {
289 list
[n_list
].filename
= TAKE_PTR(p
);
290 list
[n_list
].usage
= size
;
291 list
[n_list
].seqnum
= seqnum
;
292 list
[n_list
].realtime
= realtime
;
293 list
[n_list
].seqnum_id
= seqnum_id
;
294 list
[n_list
].have_seqnum
= have_seqnum
;
300 qsort_safe(list
, n_list
, sizeof(struct vacuum_info
), vacuum_compare
);
302 for (i
= 0; i
< n_list
; i
++) {
305 left
= n_active_files
+ n_list
- i
;
307 if ((max_retention_usec
<= 0 || list
[i
].realtime
>= retention_limit
) &&
308 (max_use
<= 0 || sum
<= max_use
) &&
309 (n_max_files
<= 0 || left
<= n_max_files
))
312 r
= unlinkat_deallocate(dirfd(d
), list
[i
].filename
, 0);
314 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
));
315 freed
+= list
[i
].usage
;
317 if (list
[i
].usage
< sum
)
318 sum
-= list
[i
].usage
;
322 } else if (r
!= -ENOENT
)
323 log_warning_errno(r
, "Failed to delete archived journal %s/%s: %m", directory
, list
[i
].filename
);
326 if (oldest_usec
&& i
< n_list
&& (*oldest_usec
== 0 || list
[i
].realtime
< *oldest_usec
))
327 *oldest_usec
= list
[i
].realtime
;
332 for (i
= 0; i
< n_list
; i
++)
333 free(list
[i
].filename
);
336 log_full(verbose
? LOG_INFO
: LOG_DEBUG
, "Vacuuming done, freed %s of archived journals from %s.", format_bytes(sbytes
, sizeof(sbytes
), freed
), directory
);