1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "dirent-util.h"
12 #include "format-util.h"
14 #include "journal-def.h"
15 #include "journal-file.h"
16 #include "journal-vacuum.h"
17 #include "sort-util.h"
18 #include "string-util.h"
19 #include "time-util.h"
20 #include "xattr-util.h"
33 static int vacuum_compare(const struct vacuum_info
*a
, const struct vacuum_info
*b
) {
36 if (a
->have_seqnum
&& b
->have_seqnum
&&
37 sd_id128_equal(a
->seqnum_id
, b
->seqnum_id
))
38 return CMP(a
->seqnum
, b
->seqnum
);
40 r
= CMP(a
->realtime
, b
->realtime
);
44 if (a
->have_seqnum
&& b
->have_seqnum
)
45 return memcmp(&a
->seqnum_id
, &b
->seqnum_id
, 16);
47 return strcmp(a
->filename
, b
->filename
);
50 static void patch_realtime(
53 const struct stat
*st
,
54 unsigned long long *realtime
) {
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
67 x
= timespec_load(&st
->st_ctim
);
68 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
71 x
= timespec_load(&st
->st_atim
);
72 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
75 x
= timespec_load(&st
->st_mtim
);
76 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
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... */
84 if (fd_getcrtime_at(fd
, fn
, &crtime
, 0) >= 0) {
85 if (crtime
< *realtime
)
90 static int journal_file_empty(int dir_fd
, const char *name
) {
91 _cleanup_close_
int fd
;
96 fd
= openat(dir_fd
, name
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NONBLOCK
|O_NOATIME
);
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
);
104 if (fstat(fd
, &st
) < 0)
107 /* If an offline file doesn't even have a header we consider it empty */
108 if (st
.st_size
< (off_t
) sizeof(Header
))
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
));
115 if (n
!= sizeof(n_entries
))
118 return le64toh(n_entries
) <= 0;
121 int journal_directory_vacuum(
122 const char *directory
,
124 uint64_t n_max_files
,
125 usec_t max_retention_usec
,
129 uint64_t sum
= 0, freed
= 0, n_active_files
= 0;
130 size_t n_list
= 0, n_allocated
= 0, i
;
131 _cleanup_closedir_
DIR *d
= NULL
;
132 struct vacuum_info
*list
= NULL
;
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
= usec_sub_unsigned(now(CLOCK_REALTIME
), max_retention_usec
);
146 d
= opendir(directory
);
150 FOREACH_DIRENT_ALL(de
, d
, r
= -errno
; goto finish
) {
152 unsigned long long seqnum
= 0, realtime
;
153 _cleanup_free_
char *p
= NULL
;
154 sd_id128_t seqnum_id
;
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
);
165 if (!S_ISREG(st
.st_mode
))
168 q
= strlen(de
->d_name
);
170 if (endswith(de
->d_name
, ".journal")) {
172 /* Vacuum archived files. Active files are
175 if (q
< 1 + 32 + 1 + 16 + 1 + 16 + 8) {
180 if (de
->d_name
[q
-8-16-1] != '-' ||
181 de
->d_name
[q
-8-16-1-16-1] != '-' ||
182 de
->d_name
[q
-8-16-1-16-1-32-1] != '@') {
187 p
= strdup(de
->d_name
);
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) {
199 if (sscanf(de
->d_name
+ q
-8-16-1-16, "%16llx-%16llx.journal", &seqnum
, &realtime
) != 2) {
206 } else if (endswith(de
->d_name
, ".journal~")) {
207 unsigned long long tmp
;
209 /* Vacuum corrupted files */
211 if (q
< 1 + 16 + 1 + 16 + 8 + 1) {
216 if (de
->d_name
[q
-1-8-16-1] != '-' ||
217 de
->d_name
[q
-1-8-16-1-16-1] != '@') {
222 p
= strdup(de
->d_name
);
228 if (sscanf(de
->d_name
+ q
-1-8-16-1-16, "%16llx-%16llx.journal~", &realtime
, &tmp
) != 2) {
235 /* We do not vacuum unknown files! */
236 log_debug("Not vacuuming unknown file %s.", de
->d_name
);
240 size
= 512UL * (uint64_t) st
.st_blocks
;
242 r
= journal_file_empty(dirfd(d
), p
);
244 log_debug_errno(r
, "Failed check if %s is empty, ignoring: %m", p
);
248 /* Always vacuum empty non-online files. */
250 r
= unlinkat_deallocate(dirfd(d
), p
, 0);
253 log_full(verbose
? LOG_INFO
: LOG_DEBUG
,
254 "Deleted empty archived journal %s/%s (%s).", directory
, p
, format_bytes(sbytes
, sizeof(sbytes
), size
));
257 } else if (r
!= -ENOENT
)
258 log_warning_errno(r
, "Failed to delete empty archived journal %s/%s: %m", directory
, p
);
263 patch_realtime(dirfd(d
), p
, &st
, &realtime
);
265 if (!GREEDY_REALLOC(list
, n_allocated
, n_list
+ 1)) {
270 list
[n_list
++] = (struct vacuum_info
) {
271 .filename
= TAKE_PTR(p
),
274 .realtime
= realtime
,
275 .seqnum_id
= seqnum_id
,
276 .have_seqnum
= have_seqnum
,
282 typesafe_qsort(list
, n_list
, vacuum_compare
);
284 for (i
= 0; i
< n_list
; i
++) {
287 left
= n_active_files
+ n_list
- i
;
289 if ((max_retention_usec
<= 0 || list
[i
].realtime
>= retention_limit
) &&
290 (max_use
<= 0 || sum
<= max_use
) &&
291 (n_max_files
<= 0 || left
<= n_max_files
))
294 r
= unlinkat_deallocate(dirfd(d
), list
[i
].filename
, 0);
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
));
297 freed
+= list
[i
].usage
;
299 if (list
[i
].usage
< sum
)
300 sum
-= list
[i
].usage
;
304 } else if (r
!= -ENOENT
)
305 log_warning_errno(r
, "Failed to delete archived journal %s/%s: %m", directory
, list
[i
].filename
);
308 if (oldest_usec
&& i
< n_list
&& (*oldest_usec
== 0 || list
[i
].realtime
< *oldest_usec
))
309 *oldest_usec
= list
[i
].realtime
;
314 for (i
= 0; i
< n_list
; i
++)
315 free(list
[i
].filename
);
318 log_full(verbose
? LOG_INFO
: LOG_DEBUG
, "Vacuuming done, freed %s of archived journals from %s.", format_bytes(sbytes
, sizeof(sbytes
), freed
), directory
);