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 void *_a
, const void *_b
) {
33 const struct vacuum_info
*a
, *b
;
38 if (a
->have_seqnum
&& b
->have_seqnum
&&
39 sd_id128_equal(a
->seqnum_id
, b
->seqnum_id
)) {
40 if (a
->seqnum
< b
->seqnum
)
42 else if (a
->seqnum
> b
->seqnum
)
48 if (a
->realtime
< b
->realtime
)
50 else if (a
->realtime
> b
->realtime
)
52 else if (a
->have_seqnum
&& b
->have_seqnum
)
53 return memcmp(&a
->seqnum_id
, &b
->seqnum_id
, 16);
55 return strcmp(a
->filename
, b
->filename
);
58 static void patch_realtime(
61 const struct stat
*st
,
62 unsigned long long *realtime
) {
66 /* The timestamp was determined by the file name, but let's
67 * see if the file might actually be older than the file name
75 x
= timespec_load(&st
->st_ctim
);
76 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
79 x
= timespec_load(&st
->st_atim
);
80 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
83 x
= timespec_load(&st
->st_mtim
);
84 if (x
> 0 && x
!= USEC_INFINITY
&& x
< *realtime
)
87 /* Let's read the original creation time, if possible. Ideally
88 * we'd just query the creation time the FS might provide, but
89 * unfortunately there's currently no sane API to query
90 * it. Hence let's implement this manually... */
92 if (fd_getcrtime_at(fd
, fn
, &crtime
, 0) >= 0) {
93 if (crtime
< *realtime
)
98 static int journal_file_empty(int dir_fd
, const char *name
) {
99 _cleanup_close_
int fd
;
104 fd
= openat(dir_fd
, name
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NONBLOCK
|O_NOATIME
);
106 /* Maybe failed due to O_NOATIME and lack of privileges? */
107 fd
= openat(dir_fd
, name
, O_RDONLY
|O_CLOEXEC
|O_NOFOLLOW
|O_NONBLOCK
);
112 if (fstat(fd
, &st
) < 0)
115 /* If an offline file doesn't even have a header we consider it empty */
116 if (st
.st_size
< (off_t
) sizeof(Header
))
119 /* If the number of entries is empty, we consider it empty, too */
120 n
= pread(fd
, &n_entries
, sizeof(n_entries
), offsetof(Header
, n_entries
));
123 if (n
!= sizeof(n_entries
))
126 return le64toh(n_entries
) <= 0;
129 int journal_directory_vacuum(
130 const char *directory
,
132 uint64_t n_max_files
,
133 usec_t max_retention_usec
,
137 _cleanup_closedir_
DIR *d
= NULL
;
138 struct vacuum_info
*list
= NULL
;
139 unsigned n_list
= 0, i
, n_active_files
= 0;
140 size_t n_allocated
= 0;
141 uint64_t sum
= 0, freed
= 0;
142 usec_t retention_limit
= 0;
143 char sbytes
[FORMAT_BYTES_MAX
];
149 if (max_use
<= 0 && max_retention_usec
<= 0 && n_max_files
<= 0)
152 if (max_retention_usec
> 0) {
153 retention_limit
= now(CLOCK_REALTIME
);
154 if (retention_limit
> max_retention_usec
)
155 retention_limit
-= max_retention_usec
;
157 max_retention_usec
= retention_limit
= 0;
160 d
= opendir(directory
);
164 FOREACH_DIRENT_ALL(de
, d
, r
= -errno
; goto finish
) {
166 unsigned long long seqnum
= 0, realtime
;
167 _cleanup_free_
char *p
= NULL
;
168 sd_id128_t seqnum_id
;
174 if (fstatat(dirfd(d
), de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0) {
175 log_debug_errno(errno
, "Failed to stat file %s while vacuuming, ignoring: %m", de
->d_name
);
179 if (!S_ISREG(st
.st_mode
))
182 q
= strlen(de
->d_name
);
184 if (endswith(de
->d_name
, ".journal")) {
186 /* Vacuum archived files. Active files are
189 if (q
< 1 + 32 + 1 + 16 + 1 + 16 + 8) {
194 if (de
->d_name
[q
-8-16-1] != '-' ||
195 de
->d_name
[q
-8-16-1-16-1] != '-' ||
196 de
->d_name
[q
-8-16-1-16-1-32-1] != '@') {
201 p
= strdup(de
->d_name
);
207 de
->d_name
[q
-8-16-1-16-1] = 0;
208 if (sd_id128_from_string(de
->d_name
+ q
-8-16-1-16-1-32, &seqnum_id
) < 0) {
213 if (sscanf(de
->d_name
+ q
-8-16-1-16, "%16llx-%16llx.journal", &seqnum
, &realtime
) != 2) {
220 } else if (endswith(de
->d_name
, ".journal~")) {
221 unsigned long long tmp
;
223 /* Vacuum corrupted files */
225 if (q
< 1 + 16 + 1 + 16 + 8 + 1) {
230 if (de
->d_name
[q
-1-8-16-1] != '-' ||
231 de
->d_name
[q
-1-8-16-1-16-1] != '@') {
236 p
= strdup(de
->d_name
);
242 if (sscanf(de
->d_name
+ q
-1-8-16-1-16, "%16llx-%16llx.journal~", &realtime
, &tmp
) != 2) {
249 /* We do not vacuum unknown files! */
250 log_debug("Not vacuuming unknown file %s.", de
->d_name
);
254 size
= 512UL * (uint64_t) st
.st_blocks
;
256 r
= journal_file_empty(dirfd(d
), p
);
258 log_debug_errno(r
, "Failed check if %s is empty, ignoring: %m", p
);
262 /* Always vacuum empty non-online files. */
264 r
= unlinkat_deallocate(dirfd(d
), p
, 0);
267 log_full(verbose
? LOG_INFO
: LOG_DEBUG
,
268 "Deleted empty archived journal %s/%s (%s).", directory
, p
, format_bytes(sbytes
, sizeof(sbytes
), size
));
271 } else if (r
!= -ENOENT
)
272 log_warning_errno(r
, "Failed to delete empty archived journal %s/%s: %m", directory
, p
);
277 patch_realtime(dirfd(d
), p
, &st
, &realtime
);
279 if (!GREEDY_REALLOC(list
, n_allocated
, n_list
+ 1)) {
284 list
[n_list
].filename
= TAKE_PTR(p
);
285 list
[n_list
].usage
= size
;
286 list
[n_list
].seqnum
= seqnum
;
287 list
[n_list
].realtime
= realtime
;
288 list
[n_list
].seqnum_id
= seqnum_id
;
289 list
[n_list
].have_seqnum
= have_seqnum
;
295 qsort_safe(list
, n_list
, sizeof(struct vacuum_info
), vacuum_compare
);
297 for (i
= 0; i
< n_list
; i
++) {
300 left
= n_active_files
+ n_list
- i
;
302 if ((max_retention_usec
<= 0 || list
[i
].realtime
>= retention_limit
) &&
303 (max_use
<= 0 || sum
<= max_use
) &&
304 (n_max_files
<= 0 || left
<= n_max_files
))
307 r
= unlinkat_deallocate(dirfd(d
), list
[i
].filename
, 0);
309 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
));
310 freed
+= list
[i
].usage
;
312 if (list
[i
].usage
< sum
)
313 sum
-= list
[i
].usage
;
317 } else if (r
!= -ENOENT
)
318 log_warning_errno(r
, "Failed to delete archived journal %s/%s: %m", directory
, list
[i
].filename
);
321 if (oldest_usec
&& i
< n_list
&& (*oldest_usec
== 0 || list
[i
].realtime
< *oldest_usec
))
322 *oldest_usec
= list
[i
].realtime
;
327 for (i
= 0; i
< n_list
; i
++)
328 free(list
[i
].filename
);
331 log_full(verbose
? LOG_INFO
: LOG_DEBUG
, "Vacuuming done, freed %s of archived journals from %s.", format_bytes(sbytes
, sizeof(sbytes
), freed
), directory
);