| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "sd-id128.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "dirent-util.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "format-util.h" |
| 13 |
#include "fs-util.h" |
| 14 |
#include "journal-def.h" |
| 15 |
#include "journal-internal.h" |
| 16 |
#include "journal-vacuum.h" |
| 17 |
#include "log.h" |
| 18 |
#include "log-ratelimit.h" |
| 19 |
#include "sort-util.h" |
| 20 |
#include "string-util.h" |
| 21 |
#include "time-util.h" |
| 22 |
#include "xattr-util.h" |
| 23 |
|
| 24 |
typedef struct VacuumInfo { |
| 25 |
uint64_t usage; |
| 26 |
char *filename; |
| 27 |
|
| 28 |
uint64_t realtime; |
| 29 |
|
| 30 |
sd_id128_t seqnum_id; |
| 31 |
uint64_t seqnum; |
| 32 |
bool have_seqnum; |
| 33 |
} VacuumInfo; |
| 34 |
|
| 35 |
static int vacuum_info_compare(const VacuumInfo *a, const VacuumInfo *b) { |
| 36 |
int r; |
| 37 |
|
| 38 |
if (a->have_seqnum && b->have_seqnum && |
| 39 |
sd_id128_equal(a->seqnum_id, b->seqnum_id)) |
| 40 |
return CMP(a->seqnum, b->seqnum); |
| 41 |
|
| 42 |
r = CMP(a->realtime, b->realtime); |
| 43 |
if (r != 0) |
| 44 |
return r; |
| 45 |
|
| 46 |
if (a->have_seqnum && b->have_seqnum) |
| 47 |
return memcmp(&a->seqnum_id, &b->seqnum_id, 16); |
| 48 |
|
| 49 |
return strcmp(a->filename, b->filename); |
| 50 |
} |
| 51 |
|
| 52 |
static void vacuum_info_done(VacuumInfo *info) { |
| 53 |
assert(info); |
| 54 |
info->filename = mfree(info->filename); |
| 55 |
} |
| 56 |
|
| 57 |
static DEFINE_ARRAY_FREE_FUNC(vacuum_info_array_free, VacuumInfo, vacuum_info_done); |
| 58 |
|
| 59 |
static void patch_realtime( |
| 60 |
int fd, |
| 61 |
const char *fn, |
| 62 |
const struct stat *st, |
| 63 |
unsigned long long *realtime) { |
| 64 |
|
| 65 |
usec_t x; |
| 66 |
|
| 67 |
/* The timestamp was determined by the file name, but let's see if the file might actually be older |
| 68 |
* than the file name suggested... */ |
| 69 |
|
| 70 |
assert(fd >= 0); |
| 71 |
assert(fn); |
| 72 |
assert(st); |
| 73 |
assert(realtime); |
| 74 |
|
| 75 |
x = timespec_load(&st->st_ctim); |
| 76 |
if (timestamp_is_set(x) && x < *realtime) |
| 77 |
*realtime = x; |
| 78 |
|
| 79 |
x = timespec_load(&st->st_atim); |
| 80 |
if (timestamp_is_set(x) && x < *realtime) |
| 81 |
*realtime = x; |
| 82 |
|
| 83 |
x = timespec_load(&st->st_mtim); |
| 84 |
if (timestamp_is_set(x) && x < *realtime) |
| 85 |
*realtime = x; |
| 86 |
|
| 87 |
/* Let's read the original creation time, if possible. Ideally we'd just query the creation time the |
| 88 |
* FS might provide, but unfortunately there's currently no sane API to query it. Hence let's |
| 89 |
* implement this manually... */ |
| 90 |
|
| 91 |
if (getcrtime_at(fd, fn, AT_SYMLINK_FOLLOW, &x) >= 0 && x < *realtime) |
| 92 |
*realtime = x; |
| 93 |
} |
| 94 |
|
| 95 |
static int journal_file_empty(int dir_fd, const char *name) { |
| 96 |
_cleanup_close_ int fd = -EBADF; |
| 97 |
struct stat st; |
| 98 |
le64_t n_entries; |
| 99 |
ssize_t n; |
| 100 |
|
| 101 |
fd = openat(dir_fd, name, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK|O_NOATIME); |
| 102 |
if (fd < 0) { |
| 103 |
/* Maybe failed due to O_NOATIME and lack of privileges? */ |
| 104 |
fd = openat(dir_fd, name, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK); |
| 105 |
if (fd < 0) |
| 106 |
return -errno; |
| 107 |
} |
| 108 |
|
| 109 |
if (fstat(fd, &st) < 0) |
| 110 |
return -errno; |
| 111 |
|
| 112 |
/* If an offline file doesn't even have a header we consider it empty */ |
| 113 |
if (st.st_size < (off_t) sizeof(Header)) |
| 114 |
return 1; |
| 115 |
|
| 116 |
/* If the number of entries is empty, we consider it empty, too */ |
| 117 |
n = pread(fd, &n_entries, sizeof(n_entries), offsetof(Header, n_entries)); |
| 118 |
if (n < 0) |
| 119 |
return -errno; |
| 120 |
if (n != sizeof(n_entries)) |
| 121 |
return -EIO; |
| 122 |
|
| 123 |
return le64toh(n_entries) <= 0; |
| 124 |
} |
| 125 |
|
| 126 |
int journal_directory_vacuum( |
| 127 |
const char *directory, |
| 128 |
uint64_t max_use, |
| 129 |
uint64_t n_max_files, |
| 130 |
usec_t max_retention_usec, |
| 131 |
usec_t *oldest_usec, |
| 132 |
bool verbose) { |
| 133 |
|
| 134 |
uint64_t sum = 0, freed = 0, n_active_files = 0; |
| 135 |
size_t n_list = 0, i; |
| 136 |
_cleanup_closedir_ DIR *d = NULL; |
| 137 |
VacuumInfo *list = NULL; |
| 138 |
usec_t retention_limit = 0; |
| 139 |
int r; |
| 140 |
|
| 141 |
CLEANUP_ARRAY(list, n_list, vacuum_info_array_free); |
| 142 |
|
| 143 |
assert(directory); |
| 144 |
|
| 145 |
if (max_use <= 0 && max_retention_usec <= 0 && n_max_files <= 0) |
| 146 |
return 0; |
| 147 |
|
| 148 |
if (max_retention_usec > 0) |
| 149 |
retention_limit = usec_sub_unsigned(now(CLOCK_REALTIME), max_retention_usec); |
| 150 |
|
| 151 |
d = opendir(directory); |
| 152 |
if (!d) |
| 153 |
return -errno; |
| 154 |
|
| 155 |
FOREACH_DIRENT_ALL(de, d, return -errno) { |
| 156 |
unsigned long long seqnum = 0, realtime; |
| 157 |
_cleanup_free_ char *p = NULL; |
| 158 |
sd_id128_t seqnum_id; |
| 159 |
bool have_seqnum; |
| 160 |
uint64_t size; |
| 161 |
struct stat st; |
| 162 |
size_t q; |
| 163 |
|
| 164 |
if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { |
| 165 |
log_debug_errno(errno, "Failed to stat file %s while vacuuming, ignoring: %m", de->d_name); |
| 166 |
continue; |
| 167 |
} |
| 168 |
|
| 169 |
if (!S_ISREG(st.st_mode)) |
| 170 |
continue; |
| 171 |
|
| 172 |
size = 512UL * (uint64_t) st.st_blocks; |
| 173 |
|
| 174 |
q = strlen(de->d_name); |
| 175 |
|
| 176 |
if (endswith(de->d_name, ".journal")) { |
| 177 |
|
| 178 |
/* Vacuum archived files. Active files are |
| 179 |
* left around */ |
| 180 |
|
| 181 |
if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8) { |
| 182 |
n_active_files++; |
| 183 |
sum += size; |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
if (de->d_name[q-8-16-1] != '-' || |
| 188 |
de->d_name[q-8-16-1-16-1] != '-' || |
| 189 |
de->d_name[q-8-16-1-16-1-32-1] != '@') { |
| 190 |
n_active_files++; |
| 191 |
sum += size; |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
p = strdup(de->d_name); |
| 196 |
if (!p) |
| 197 |
return -ENOMEM; |
| 198 |
|
| 199 |
de->d_name[q-8-16-1-16-1] = 0; |
| 200 |
if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) { |
| 201 |
n_active_files++; |
| 202 |
sum += size; |
| 203 |
continue; |
| 204 |
} |
| 205 |
|
| 206 |
if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) { |
| 207 |
n_active_files++; |
| 208 |
sum += size; |
| 209 |
continue; |
| 210 |
} |
| 211 |
|
| 212 |
have_seqnum = true; |
| 213 |
|
| 214 |
} else if (endswith(de->d_name, ".journal~")) { |
| 215 |
unsigned long long tmp; |
| 216 |
|
| 217 |
/* seqnum_id won't be initialised before use below, so set to 0 */ |
| 218 |
seqnum_id = SD_ID128_NULL; |
| 219 |
|
| 220 |
/* Vacuum corrupted files */ |
| 221 |
|
| 222 |
if (q < 1 + 16 + 1 + 16 + 8 + 1) { |
| 223 |
n_active_files++; |
| 224 |
sum += size; |
| 225 |
continue; |
| 226 |
} |
| 227 |
|
| 228 |
if (de->d_name[q-1-8-16-1] != '-' || |
| 229 |
de->d_name[q-1-8-16-1-16-1] != '@') { |
| 230 |
n_active_files++; |
| 231 |
sum += size; |
| 232 |
continue; |
| 233 |
} |
| 234 |
|
| 235 |
p = strdup(de->d_name); |
| 236 |
if (!p) |
| 237 |
return -ENOMEM; |
| 238 |
|
| 239 |
if (sscanf(de->d_name + q-1-8-16-1-16, "%16llx-%16llx.journal~", &realtime, &tmp) != 2) { |
| 240 |
n_active_files++; |
| 241 |
sum += size; |
| 242 |
continue; |
| 243 |
} |
| 244 |
|
| 245 |
have_seqnum = false; |
| 246 |
} else { |
| 247 |
/* We do not vacuum unknown files! */ |
| 248 |
log_debug("Not vacuuming unknown file %s.", de->d_name); |
| 249 |
continue; |
| 250 |
} |
| 251 |
|
| 252 |
r = journal_file_empty(dirfd(d), p); |
| 253 |
if (r < 0) { |
| 254 |
log_debug_errno(r, "Failed to check if %s is empty, ignoring: %m", p); |
| 255 |
continue; |
| 256 |
} |
| 257 |
if (r > 0) { |
| 258 |
/* Always vacuum empty non-online files. */ |
| 259 |
|
| 260 |
r = unlinkat_deallocate(dirfd(d), p, 0); |
| 261 |
if (r >= 0) { |
| 262 |
|
| 263 |
log_full(verbose ? LOG_INFO : LOG_DEBUG, |
| 264 |
"Deleted empty archived journal %s/%s (%s).", directory, p, FORMAT_BYTES(size)); |
| 265 |
|
| 266 |
freed += size; |
| 267 |
} else if (r != -ENOENT) |
| 268 |
log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, |
| 269 |
"Failed to delete empty archived journal %s/%s: %m", |
| 270 |
directory, p); |
| 271 |
|
| 272 |
continue; |
| 273 |
} |
| 274 |
|
| 275 |
patch_realtime(dirfd(d), p, &st, &realtime); |
| 276 |
|
| 277 |
if (!GREEDY_REALLOC(list, n_list + 1)) |
| 278 |
return -ENOMEM; |
| 279 |
|
| 280 |
list[n_list++] = (VacuumInfo) { |
| 281 |
.filename = TAKE_PTR(p), |
| 282 |
.usage = size, |
| 283 |
.seqnum = seqnum, |
| 284 |
.realtime = realtime, |
| 285 |
.seqnum_id = seqnum_id, |
| 286 |
.have_seqnum = have_seqnum, |
| 287 |
}; |
| 288 |
|
| 289 |
sum += size; |
| 290 |
} |
| 291 |
|
| 292 |
typesafe_qsort(list, n_list, vacuum_info_compare); |
| 293 |
|
| 294 |
for (i = 0; i < n_list; i++) { |
| 295 |
uint64_t left; |
| 296 |
|
| 297 |
left = n_active_files + n_list - i; |
| 298 |
|
| 299 |
if ((max_retention_usec <= 0 || list[i].realtime >= retention_limit) && |
| 300 |
(max_use <= 0 || sum <= max_use) && |
| 301 |
(n_max_files <= 0 || left <= n_max_files)) |
| 302 |
break; |
| 303 |
|
| 304 |
r = unlinkat_deallocate(dirfd(d), list[i].filename, 0); |
| 305 |
if (r >= 0) { |
| 306 |
log_full(verbose ? LOG_INFO : LOG_DEBUG, "Deleted archived journal %s/%s (%s).", |
| 307 |
directory, list[i].filename, FORMAT_BYTES(list[i].usage)); |
| 308 |
freed += list[i].usage; |
| 309 |
|
| 310 |
if (list[i].usage < sum) |
| 311 |
sum -= list[i].usage; |
| 312 |
else |
| 313 |
sum = 0; |
| 314 |
|
| 315 |
} else if (r != -ENOENT) |
| 316 |
log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, |
| 317 |
"Failed to delete archived journal %s/%s: %m", |
| 318 |
directory, list[i].filename); |
| 319 |
} |
| 320 |
|
| 321 |
if (oldest_usec && i < n_list && (*oldest_usec == 0 || list[i].realtime < *oldest_usec)) |
| 322 |
*oldest_usec = list[i].realtime; |
| 323 |
|
| 324 |
log_full(verbose ? LOG_INFO : LOG_DEBUG, "Vacuuming done, freed %s of archived journals from %s.", |
| 325 |
FORMAT_BYTES(freed), directory); |
| 326 |
|
| 327 |
return 0; |
| 328 |
} |