1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/statvfs.h>
30 #include "journal-def.h"
31 #include "journal-file.h"
34 #define DEFAULT_ARENA_MAX_SIZE (16ULL*1024ULL*1024ULL*1024ULL)
35 #define DEFAULT_ARENA_MIN_SIZE (256ULL*1024ULL)
36 #define DEFAULT_ARENA_KEEP_FREE (1ULL*1024ULL*1024ULL)
38 #define DEFAULT_MAX_USE (16ULL*1024ULL*1024ULL*16ULL)
40 #define DEFAULT_HASH_TABLE_SIZE (2047ULL*16ULL)
41 #define DEFAULT_BISECT_TABLE_SIZE ((DEFAULT_ARENA_MAX_SIZE/(64ULL*1024ULL))*8ULL)
43 #define DEFAULT_WINDOW_SIZE (128ULL*1024ULL*1024ULL)
45 static const char signature[] = { 'L', 'P', 'K', 'S', 'H', 'H', 'R', 'H' };
47 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
49 void journal_file_close(JournalFile *f) {
53 if (f->writable && f->header->state == htole32(STATE_ONLINE))
54 f->header->state = htole32(STATE_OFFLINE);
56 munmap(f->header, PAGE_ALIGN(sizeof(Header)));
59 if (f->hash_table_window)
60 munmap(f->hash_table_window, f->hash_table_window_size);
62 if (f->bisect_table_window)
63 munmap(f->bisect_table_window, f->bisect_table_window_size);
66 munmap(f->window, f->window_size);
69 close_nointr_nofail(f->fd);
75 static int journal_file_init_header(JournalFile *f, JournalFile *template) {
83 memcpy(h.signature, signature, 8);
84 h.arena_offset = htole64(ALIGN64(sizeof(h)));
85 h.arena_max_size = htole64(DEFAULT_ARENA_MAX_SIZE);
86 h.arena_min_size = htole64(DEFAULT_ARENA_MIN_SIZE);
87 h.arena_keep_free = htole64(DEFAULT_ARENA_KEEP_FREE);
89 r = sd_id128_randomize(&h.file_id);
94 h.seqnum_id = template->header->seqnum_id;
95 h.seqnum = template->header->seqnum;
97 h.seqnum_id = h.file_id;
99 k = pwrite(f->fd, &h, sizeof(h), 0);
109 static int journal_file_refresh_header(JournalFile *f) {
114 r = sd_id128_get_machine(&f->header->machine_id);
118 r = sd_id128_get_boot(&f->header->boot_id);
122 f->header->state = htole32(STATE_ONLINE);
126 static int journal_file_verify_header(JournalFile *f) {
129 if (memcmp(f->header, signature, 8))
132 if (f->header->incompatible_flags != 0)
133 return -EPROTONOSUPPORT;
135 if ((uint64_t) f->last_stat.st_size < (le64toh(f->header->arena_offset) + le64toh(f->header->arena_size)))
140 sd_id128_t machine_id;
143 r = sd_id128_get_machine(&machine_id);
147 if (!sd_id128_equal(machine_id, f->header->machine_id))
150 state = le32toh(f->header->state);
152 if (state == STATE_ONLINE)
153 log_debug("Journal file %s is already online. Assuming unclean closing. Ignoring.", f->path);
154 else if (state == STATE_ARCHIVED)
156 else if (state != STATE_OFFLINE)
157 log_debug("Journal file %s has unknown state %u. Ignoring.", f->path, state);
163 static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
165 uint64_t old_size, new_size;
169 if (offset < le64toh(f->header->arena_offset))
172 new_size = PAGE_ALIGN(offset + size);
174 /* We assume that this file is not sparse, and we know that
175 * for sure, since we always call posix_fallocate()
179 le64toh(f->header->arena_offset) +
180 le64toh(f->header->arena_size);
182 if (old_size >= new_size)
185 asize = new_size - le64toh(f->header->arena_offset);
187 if (asize > le64toh(f->header->arena_min_size)) {
190 if (fstatvfs(f->fd, &svfs) >= 0) {
193 available = svfs.f_bfree * svfs.f_bsize;
195 if (available >= f->header->arena_keep_free)
196 available -= f->header->arena_keep_free;
200 if (new_size - old_size > available)
205 if (asize > le64toh(f->header->arena_max_size))
208 if (posix_fallocate(f->fd, old_size, new_size - old_size) < 0)
211 if (fstat(f->fd, &f->last_stat) < 0)
214 f->header->arena_size = htole64(asize);
219 static int journal_file_map(
228 uint64_t woffset, wsize;
235 woffset = offset & ~((uint64_t) page_size() - 1ULL);
236 wsize = size + (offset - woffset);
237 wsize = PAGE_ALIGN(wsize);
239 window = mmap(NULL, wsize, f->prot, MAP_SHARED, f->fd, woffset);
240 if (window == MAP_FAILED)
252 *ret = (uint8_t*) window + (offset - woffset);
257 static int journal_file_move_to(JournalFile *f, uint64_t offset, uint64_t size, void **ret) {
265 if (_likely_(f->window &&
266 f->window_offset <= offset &&
267 f->window_offset+f->window_size >= offset + size)) {
269 *ret = (uint8_t*) f->window + (offset - f->window_offset);
274 if (munmap(f->window, f->window_size) < 0)
278 f->window_size = f->window_offset = 0;
281 if (size < DEFAULT_WINDOW_SIZE) {
282 /* If the default window size is larger then what was
283 * asked for extend the mapping a bit in the hope to
284 * minimize needed remappings later on. We add half
285 * the window space before and half behind the
286 * requested mapping */
288 delta = PAGE_ALIGN((DEFAULT_WINDOW_SIZE - size) / 2);
294 size += (DEFAULT_WINDOW_SIZE - delta);
298 r = journal_file_map(f,
300 &f->window, &f->window_offset, &f->window_size,
306 *ret = (uint8_t*) p + delta;
310 static bool verify_hash(Object *o) {
315 t = le64toh(o->object.type);
316 if (t == OBJECT_DATA) {
319 s = le64toh(o->object.size);
321 h1 = le64toh(o->data.hash);
322 h2 = hash64(o->data.payload, s - offsetof(Object, data.payload));
330 int journal_file_move_to_object(JournalFile *f, uint64_t offset, int type, Object **ret) {
339 r = journal_file_move_to(f, offset, sizeof(ObjectHeader), &t);
344 s = le64toh(o->object.size);
346 if (s < sizeof(ObjectHeader))
349 if (type >= 0 && le64toh(o->object.type) != type)
352 if (s > sizeof(ObjectHeader)) {
353 r = journal_file_move_to(f, offset, s, &t);
367 static uint64_t journal_file_seqnum(JournalFile *f, uint64_t *seqnum) {
372 r = le64toh(f->header->seqnum) + 1;
375 /* If an external seqno counter was passed, we update
376 * both the local and the external one, and set it to
377 * the maximum of both */
385 f->header->seqnum = htole64(r);
390 static int journal_file_append_object(JournalFile *f, uint64_t size, Object **ret, uint64_t *offset) {
397 assert(size >= sizeof(ObjectHeader));
401 p = le64toh(f->header->tail_object_offset);
404 p = le64toh(f->header->arena_offset);
406 r = journal_file_move_to_object(f, p, -1, &tail);
410 p += ALIGN64(le64toh(tail->object.size));
413 r = journal_file_allocate(f, p, size);
417 r = journal_file_move_to(f, p, size, &t);
424 o->object.type = htole64(OBJECT_UNUSED);
425 zero(o->object.reserved);
426 o->object.size = htole64(size);
428 f->header->tail_object_offset = htole64(p);
429 if (f->header->head_object_offset == 0)
430 f->header->head_object_offset = htole64(p);
432 f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
440 static int journal_file_setup_hash_table(JournalFile *f) {
447 s = DEFAULT_HASH_TABLE_SIZE;
448 r = journal_file_append_object(f, offsetof(Object, hash_table.table) + s, &o, &p);
452 o->object.type = htole64(OBJECT_HASH_TABLE);
453 memset(o->hash_table.table, 0, s);
455 f->header->hash_table_offset = htole64(p + offsetof(Object, hash_table.table));
456 f->header->hash_table_size = htole64(s);
461 static int journal_file_setup_bisect_table(JournalFile *f) {
468 s = DEFAULT_BISECT_TABLE_SIZE;
469 r = journal_file_append_object(f, offsetof(Object, bisect_table.table) + s, &o, &p);
473 o->object.type = htole64(OBJECT_BISECT_TABLE);
474 memset(o->bisect_table.table, 0, s);
476 f->header->bisect_table_offset = htole64(p + offsetof(Object, bisect_table.table));
477 f->header->bisect_table_size = htole64(s);
482 static int journal_file_map_hash_table(JournalFile *f) {
489 p = le64toh(f->header->hash_table_offset);
490 s = le64toh(f->header->hash_table_size);
492 r = journal_file_map(f,
494 &f->hash_table_window, NULL, &f->hash_table_window_size,
503 static int journal_file_map_bisect_table(JournalFile *f) {
510 p = le64toh(f->header->bisect_table_offset);
511 s = le64toh(f->header->bisect_table_size);
513 r = journal_file_map(f,
515 &f->bisect_table_window, NULL, &f->bisect_table_window_size,
525 static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash_index) {
532 assert(o->object.type == htole64(OBJECT_DATA));
534 o->data.head_entry_offset = o->data.tail_entry_offset = 0;
535 o->data.next_hash_offset = 0;
537 p = le64toh(f->hash_table[hash_index].tail_hash_offset);
539 /* Only entry in the hash table is easy */
541 o->data.prev_hash_offset = 0;
542 f->hash_table[hash_index].head_hash_offset = htole64(offset);
544 o->data.prev_hash_offset = htole64(p);
546 /* Temporarily move back to the previous data object,
547 * to patch in pointer */
549 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
553 o->data.next_hash_offset = offset;
555 r = journal_file_move_to_object(f, offset, OBJECT_DATA, &o);
560 f->hash_table[hash_index].tail_hash_offset = htole64(offset);
565 static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
566 uint64_t hash, h, p, np;
572 assert(data || size == 0);
574 osize = offsetof(Object, data.payload) + size;
576 hash = hash64(data, size);
577 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
578 p = le64toh(f->hash_table[h].head_hash_offset);
581 /* Look for this data object in the hash table */
583 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
587 if (le64toh(o->object.size) == osize &&
588 memcmp(o->data.payload, data, size) == 0) {
590 if (le64toh(o->data.hash) != hash)
602 p = le64toh(o->data.next_hash_offset);
605 r = journal_file_append_object(f, osize, &o, &np);
609 o->object.type = htole64(OBJECT_DATA);
610 o->data.hash = htole64(hash);
611 memcpy(o->data.payload, data, size);
613 r = journal_file_link_data(f, o, np, h);
626 uint64_t journal_file_entry_n_items(Object *o) {
628 assert(o->object.type == htole64(OBJECT_ENTRY));
630 return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
633 static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
640 p = le64toh(o->entry.items[i].object_offset);
644 o->entry.items[i].next_entry_offset = 0;
646 /* Move to the data object */
647 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
651 q = le64toh(o->data.tail_entry_offset);
652 o->data.tail_entry_offset = htole64(offset);
655 o->data.head_entry_offset = htole64(offset);
659 /* Move to previous entry */
660 r = journal_file_move_to_object(f, q, OBJECT_ENTRY, &o);
664 n = journal_file_entry_n_items(o);
665 for (j = 0; j < n; j++)
666 if (le64toh(o->entry.items[j].object_offset) == p)
672 o->entry.items[j].next_entry_offset = offset;
675 /* Move back to original entry */
676 r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
680 o->entry.items[i].prev_entry_offset = q;
684 static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
685 uint64_t p, i, n, k, a, b;
691 assert(o->object.type == htole64(OBJECT_ENTRY));
693 /* Link up the entry itself */
694 p = le64toh(f->header->tail_entry_offset);
696 o->entry.prev_entry_offset = f->header->tail_entry_offset;
697 o->entry.next_entry_offset = 0;
700 f->header->head_entry_offset = htole64(offset);
701 f->header->head_entry_realtime = o->entry.realtime;
703 /* Temporarily move back to the previous entry, to
704 * patch in pointer */
706 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
710 o->entry.next_entry_offset = htole64(offset);
712 r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
717 f->header->tail_entry_offset = htole64(offset);
718 f->header->tail_entry_realtime = o->entry.realtime;
720 /* Link up the items */
721 n = journal_file_entry_n_items(o);
722 for (i = 0; i < n; i++) {
723 r = journal_file_link_entry_item(f, o, offset, i);
728 /* Link up the entry in the bisect table */
729 n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
730 k = le64toh(f->header->arena_max_size) / n;
732 a = (le64toh(f->header->last_bisect_offset) + k - 1) / k;
736 f->bisect_table[a] = htole64(offset);
738 f->header->last_bisect_offset = htole64(offset + le64toh(o->object.size));
743 static int journal_file_append_entry_internal(
745 const dual_timestamp *ts,
747 const EntryItem items[], unsigned n_items,
749 Object **ret, uint64_t *offset) {
756 assert(items || n_items == 0);
758 osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
760 r = journal_file_append_object(f, osize, &o, &np);
764 o->object.type = htole64(OBJECT_ENTRY);
765 o->entry.seqnum = htole64(journal_file_seqnum(f, seqno));
766 memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
767 o->entry.realtime = htole64(ts ? ts->realtime : now(CLOCK_REALTIME));
768 o->entry.monotonic = htole64(ts ? ts->monotonic : now(CLOCK_MONOTONIC));
769 o->entry.xor_hash = htole64(xor_hash);
770 o->entry.boot_id = f->header->boot_id;
772 r = journal_file_link_entry(f, o, np);
785 int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, uint64_t *seqno, Object **ret, uint64_t *offset) {
789 uint64_t xor_hash = 0;
792 assert(iovec || n_iovec == 0);
794 items = new(EntryItem, n_iovec);
798 for (i = 0; i < n_iovec; i++) {
802 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
806 xor_hash ^= le64toh(o->data.hash);
807 items[i].object_offset = htole64(p);
808 items[i].hash = o->data.hash;
811 r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqno, ret, offset);
819 int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset) {
821 uint64_t lower, upper, p, n, k;
826 n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
827 k = le64toh(f->header->arena_max_size) / n;
830 upper = le64toh(f->header->last_bisect_offset)/k+1;
832 while (lower < upper) {
833 k = (upper + lower) / 2;
834 p = le64toh(f->bisect_table[k]);
841 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
845 if (o->entry.seqnum == seqnum) {
853 } else if (seqnum < o->entry.seqnum)
855 else if (seqnum > o->entry.seqnum)
859 assert(lower == upper);
864 /* The object we are looking for is between
865 * bisect_table[lower-1] and bisect_table[lower] */
867 p = le64toh(f->bisect_table[lower-1]);
870 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
874 if (o->entry.seqnum == seqnum) {
883 } if (seqnum < o->entry.seqnum)
886 if (o->entry.next_entry_offset == 0)
889 p = le64toh(o->entry.next_entry_offset);
895 int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
902 np = le64toh(f->header->head_entry_offset);
904 if (le64toh(o->object.type) != OBJECT_ENTRY)
907 np = le64toh(o->entry.next_entry_offset);
913 r = journal_file_move_to_object(f, np, OBJECT_ENTRY, &o);
926 int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
933 np = le64toh(f->header->tail_entry_offset);
935 if (le64toh(o->object.type) != OBJECT_ENTRY)
938 np = le64toh(o->entry.prev_entry_offset);
944 r = journal_file_move_to_object(f, np, OBJECT_ENTRY, &o);
957 int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
958 uint64_t p, osize, hash, h;
962 assert(data || size == 0);
964 osize = offsetof(Object, data.payload) + size;
966 hash = hash64(data, size);
967 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
968 p = le64toh(f->hash_table[h].head_hash_offset);
973 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
977 if (le64toh(o->object.size) == osize &&
978 memcmp(o->data.payload, data, size) == 0) {
980 if (le64toh(o->data.hash) != hash)
983 if (o->data.head_entry_offset == 0)
986 p = le64toh(o->data.head_entry_offset);
987 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
1000 p = le64toh(o->data.next_hash_offset);
1006 int journal_file_find_last_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
1007 uint64_t p, osize, hash, h;
1011 assert(data || size == 0);
1013 osize = offsetof(Object, data.payload) + size;
1015 hash = hash64(data, size);
1016 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
1017 p = le64toh(f->hash_table[h].tail_hash_offset);
1022 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
1026 if (le64toh(o->object.size) == osize &&
1027 memcmp(o->data.payload, data, size) == 0) {
1029 if (le64toh(o->data.hash) != hash)
1032 if (o->data.tail_entry_offset == 0)
1035 p = le64toh(o->data.tail_entry_offset);
1036 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
1049 p = le64toh(o->data.prev_hash_offset);
1055 void journal_file_dump(JournalFile *f) {
1056 char a[33], b[33], c[33];
1063 printf("File ID: %s\n"
1066 "Arena size: %llu\n",
1067 sd_id128_to_string(f->header->file_id, a),
1068 sd_id128_to_string(f->header->machine_id, b),
1069 sd_id128_to_string(f->header->boot_id, c),
1070 (unsigned long long) le64toh(f->header->arena_size));
1072 p = le64toh(f->header->head_object_offset);
1074 r = journal_file_move_to_object(f, p, -1, &o);
1078 switch (o->object.type) {
1081 printf("Type: OBJECT_UNUSED\n");
1085 printf("Type: OBJECT_DATA\n");
1089 printf("Type: OBJECT_ENTRY %llu %llu %llu\n",
1090 (unsigned long long) le64toh(o->entry.seqnum),
1091 (unsigned long long) le64toh(o->entry.monotonic),
1092 (unsigned long long) le64toh(o->entry.realtime));
1095 case OBJECT_HASH_TABLE:
1096 printf("Type: OBJECT_HASH_TABLE\n");
1099 case OBJECT_BISECT_TABLE:
1100 printf("Type: OBJECT_BISECT_TABLE\n");
1104 if (p == le64toh(f->header->tail_object_offset))
1107 p = p + ALIGN64(le64toh(o->object.size));
1112 log_error("File corrupt");
1115 int journal_file_open(
1119 JournalFile *template,
1120 JournalFile **ret) {
1124 bool newly_created = false;
1128 if ((flags & O_ACCMODE) != O_RDONLY &&
1129 (flags & O_ACCMODE) != O_RDWR)
1132 f = new0(JournalFile, 1);
1139 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1140 f->prot = prot_from_flags(flags);
1142 f->path = strdup(fname);
1148 f->fd = open(f->path, f->flags|O_CLOEXEC, f->mode);
1154 if (fstat(f->fd, &f->last_stat) < 0) {
1159 if (f->last_stat.st_size == 0 && f->writable) {
1160 newly_created = true;
1162 r = journal_file_init_header(f, template);
1166 if (fstat(f->fd, &f->last_stat) < 0) {
1172 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1177 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1178 if (f->header == MAP_FAILED) {
1184 if (!newly_created) {
1185 r = journal_file_verify_header(f);
1191 r = journal_file_refresh_header(f);
1196 if (newly_created) {
1198 r = journal_file_setup_hash_table(f);
1202 r = journal_file_setup_bisect_table(f);
1207 r = journal_file_map_hash_table(f);
1211 r = journal_file_map_bisect_table(f);
1221 journal_file_close(f);
1226 int journal_file_rotate(JournalFile **f) {
1229 JournalFile *old_file, *new_file = NULL;
1237 if (!old_file->writable)
1240 if (!endswith(old_file->path, ".journal"))
1243 l = strlen(old_file->path);
1245 p = new(char, l + 1 + 16 + 1 + 32 + 1 + 16 + 1);
1249 memcpy(p, old_file->path, l - 8);
1251 sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1);
1252 snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
1253 "-%016llx-%016llx.journal",
1254 (unsigned long long) le64toh((*f)->header->seqnum),
1255 (unsigned long long) le64toh((*f)->header->tail_entry_realtime));
1257 r = rename(old_file->path, p);
1263 old_file->header->state = le32toh(STATE_ARCHIVED);
1265 r = journal_file_open(old_file->path, old_file->flags, old_file->mode, old_file, &new_file);
1266 journal_file_close(old_file);
1272 struct vacuum_info {
1277 sd_id128_t seqnum_id;
1281 static int vacuum_compare(const void *_a, const void *_b) {
1282 const struct vacuum_info *a, *b;
1287 if (sd_id128_equal(a->seqnum_id, b->seqnum_id)) {
1288 if (a->seqnum < b->seqnum)
1290 else if (a->seqnum > b->seqnum)
1296 if (a->realtime < b->realtime)
1298 else if (a->realtime > b->realtime)
1301 return memcmp(&a->seqnum_id, &b->seqnum_id, 16);
1304 int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t min_free) {
1307 struct vacuum_info *list = NULL;
1308 unsigned n_list = 0, n_allocated = 0, i;
1314 max_use = DEFAULT_MAX_USE;
1316 d = opendir(directory);
1322 struct dirent buf, *de;
1326 unsigned long long seqnum, realtime;
1327 sd_id128_t seqnum_id;
1329 k = readdir_r(d, &buf, &de);
1338 if (!dirent_is_file_with_suffix(de, ".journal"))
1341 q = strlen(de->d_name);
1343 if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8)
1346 if (de->d_name[q-8-16-1] != '-' ||
1347 de->d_name[q-8-16-1-16-1] != '-' ||
1348 de->d_name[q-8-16-1-16-1-32-1] != '@')
1351 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
1354 if (!S_ISREG(st.st_mode))
1357 p = strdup(de->d_name);
1363 de->d_name[q-8-16-1-16-1] = 0;
1364 if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) {
1369 if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) {
1374 if (n_list >= n_allocated) {
1375 struct vacuum_info *j;
1377 n_allocated = MAX(n_allocated * 2U, 8U);
1378 j = realloc(list, n_allocated * sizeof(struct vacuum_info));
1388 list[n_list].filename = p;
1389 list[n_list].usage = (uint64_t) st.st_blksize * (uint64_t) st.st_blocks;
1390 list[n_list].seqnum = seqnum;
1391 list[n_list].realtime = realtime;
1392 list[n_list].seqnum_id = seqnum_id;
1394 sum += list[n_list].usage;
1399 qsort(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
1401 for(i = 0; i < n_list; i++) {
1404 if (fstatvfs(dirfd(d), &ss) < 0) {
1409 if (sum <= max_use &&
1410 (uint64_t) ss.f_bavail * (uint64_t) ss.f_bsize >= min_free)
1413 if (unlinkat(dirfd(d), list[i].filename, 0) >= 0) {
1414 log_debug("Deleted archived journal %s/%s.", directory, list[i].filename);
1415 sum -= list[i].usage;
1416 } else if (errno != ENOENT)
1417 log_warning("Failed to delete %s/%s: %m", directory, list[i].filename);
1421 for (i = 0; i < n_list; i++)
1422 free(list[i].filename);