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) {
372 r = le64toh(f->header->seqnum) + 1;
373 f->header->seqnum = htole64(r);
378 static int journal_file_append_object(JournalFile *f, uint64_t size, Object **ret, uint64_t *offset) {
385 assert(size >= sizeof(ObjectHeader));
389 p = le64toh(f->header->tail_object_offset);
392 p = le64toh(f->header->arena_offset);
394 r = journal_file_move_to_object(f, p, -1, &tail);
398 p += ALIGN64(le64toh(tail->object.size));
401 r = journal_file_allocate(f, p, size);
405 r = journal_file_move_to(f, p, size, &t);
412 o->object.type = htole64(OBJECT_UNUSED);
413 zero(o->object.reserved);
414 o->object.size = htole64(size);
416 f->header->tail_object_offset = htole64(p);
417 if (f->header->head_object_offset == 0)
418 f->header->head_object_offset = htole64(p);
420 f->header->n_objects = htole64(le64toh(f->header->n_objects) + 1);
428 static int journal_file_setup_hash_table(JournalFile *f) {
435 s = DEFAULT_HASH_TABLE_SIZE;
436 r = journal_file_append_object(f, offsetof(Object, hash_table.table) + s, &o, &p);
440 o->object.type = htole64(OBJECT_HASH_TABLE);
441 memset(o->hash_table.table, 0, s);
443 f->header->hash_table_offset = htole64(p + offsetof(Object, hash_table.table));
444 f->header->hash_table_size = htole64(s);
449 static int journal_file_setup_bisect_table(JournalFile *f) {
456 s = DEFAULT_BISECT_TABLE_SIZE;
457 r = journal_file_append_object(f, offsetof(Object, bisect_table.table) + s, &o, &p);
461 o->object.type = htole64(OBJECT_BISECT_TABLE);
462 memset(o->bisect_table.table, 0, s);
464 f->header->bisect_table_offset = htole64(p + offsetof(Object, bisect_table.table));
465 f->header->bisect_table_size = htole64(s);
470 static int journal_file_map_hash_table(JournalFile *f) {
477 p = le64toh(f->header->hash_table_offset);
478 s = le64toh(f->header->hash_table_size);
480 r = journal_file_map(f,
482 &f->hash_table_window, NULL, &f->hash_table_window_size,
491 static int journal_file_map_bisect_table(JournalFile *f) {
498 p = le64toh(f->header->bisect_table_offset);
499 s = le64toh(f->header->bisect_table_size);
501 r = journal_file_map(f,
503 &f->bisect_table_window, NULL, &f->bisect_table_window_size,
513 static int journal_file_link_data(JournalFile *f, Object *o, uint64_t offset, uint64_t hash_index) {
520 assert(o->object.type == htole64(OBJECT_DATA));
522 o->data.head_entry_offset = o->data.tail_entry_offset = 0;
523 o->data.next_hash_offset = 0;
525 p = le64toh(f->hash_table[hash_index].tail_hash_offset);
527 /* Only entry in the hash table is easy */
529 o->data.prev_hash_offset = 0;
530 f->hash_table[hash_index].head_hash_offset = htole64(offset);
532 o->data.prev_hash_offset = htole64(p);
534 /* Temporarily move back to the previous data object,
535 * to patch in pointer */
537 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
541 o->data.next_hash_offset = offset;
543 r = journal_file_move_to_object(f, offset, OBJECT_DATA, &o);
548 f->hash_table[hash_index].tail_hash_offset = htole64(offset);
553 static int journal_file_append_data(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
554 uint64_t hash, h, p, np;
560 assert(data || size == 0);
562 osize = offsetof(Object, data.payload) + size;
564 hash = hash64(data, size);
565 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
566 p = le64toh(f->hash_table[h].head_hash_offset);
569 /* Look for this data object in the hash table */
571 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
575 if (le64toh(o->object.size) == osize &&
576 memcmp(o->data.payload, data, size) == 0) {
578 if (le64toh(o->data.hash) != hash)
590 p = le64toh(o->data.next_hash_offset);
593 r = journal_file_append_object(f, osize, &o, &np);
597 o->object.type = htole64(OBJECT_DATA);
598 o->data.hash = htole64(hash);
599 memcpy(o->data.payload, data, size);
601 r = journal_file_link_data(f, o, np, h);
614 uint64_t journal_file_entry_n_items(Object *o) {
616 assert(o->object.type == htole64(OBJECT_ENTRY));
618 return (le64toh(o->object.size) - offsetof(Object, entry.items)) / sizeof(EntryItem);
621 static int journal_file_link_entry_item(JournalFile *f, Object *o, uint64_t offset, uint64_t i) {
628 p = le64toh(o->entry.items[i].object_offset);
632 o->entry.items[i].next_entry_offset = 0;
634 /* Move to the data object */
635 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
639 q = le64toh(o->data.tail_entry_offset);
640 o->data.tail_entry_offset = htole64(offset);
643 o->data.head_entry_offset = htole64(offset);
647 /* Move to previous entry */
648 r = journal_file_move_to_object(f, q, OBJECT_ENTRY, &o);
652 n = journal_file_entry_n_items(o);
653 for (j = 0; j < n; j++)
654 if (le64toh(o->entry.items[j].object_offset) == p)
660 o->entry.items[j].next_entry_offset = offset;
663 /* Move back to original entry */
664 r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
668 o->entry.items[i].prev_entry_offset = q;
672 static int journal_file_link_entry(JournalFile *f, Object *o, uint64_t offset) {
673 uint64_t p, i, n, k, a, b;
679 assert(o->object.type == htole64(OBJECT_ENTRY));
681 /* Link up the entry itself */
682 p = le64toh(f->header->tail_entry_offset);
684 o->entry.prev_entry_offset = f->header->tail_entry_offset;
685 o->entry.next_entry_offset = 0;
688 f->header->head_entry_offset = htole64(offset);
689 f->header->head_entry_realtime = o->entry.realtime;
691 /* Temporarily move back to the previous entry, to
692 * patch in pointer */
694 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
698 o->entry.next_entry_offset = htole64(offset);
700 r = journal_file_move_to_object(f, offset, OBJECT_ENTRY, &o);
705 f->header->tail_entry_offset = htole64(offset);
706 f->header->tail_entry_realtime = o->entry.realtime;
708 /* Link up the items */
709 n = journal_file_entry_n_items(o);
710 for (i = 0; i < n; i++) {
711 r = journal_file_link_entry_item(f, o, offset, i);
716 /* Link up the entry in the bisect table */
717 n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
718 k = le64toh(f->header->arena_max_size) / n;
720 a = (le64toh(f->header->last_bisect_offset) + k - 1) / k;
724 f->bisect_table[a] = htole64(offset);
726 f->header->last_bisect_offset = htole64(offset + le64toh(o->object.size));
731 static int journal_file_append_entry_internal(
733 const dual_timestamp *ts,
735 const EntryItem items[], unsigned n_items,
736 Object **ret, uint64_t *offset) {
743 assert(items || n_items == 0);
745 osize = offsetof(Object, entry.items) + (n_items * sizeof(EntryItem));
747 r = journal_file_append_object(f, osize, &o, &np);
751 o->object.type = htole64(OBJECT_ENTRY);
752 o->entry.seqnum = htole64(journal_file_seqnum(f));
753 memcpy(o->entry.items, items, n_items * sizeof(EntryItem));
754 o->entry.realtime = ts ? htole64(ts->realtime) : 0;
755 o->entry.monotonic = ts ? htole64(ts->monotonic) : 0;
756 o->entry.xor_hash = htole64(xor_hash);
757 o->entry.boot_id = f->header->boot_id;
759 r = journal_file_link_entry(f, o, np);
772 int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const struct iovec iovec[], unsigned n_iovec, Object **ret, uint64_t *offset) {
776 uint64_t xor_hash = 0;
779 assert(iovec || n_iovec == 0);
781 items = new(EntryItem, n_iovec);
785 for (i = 0; i < n_iovec; i++) {
789 r = journal_file_append_data(f, iovec[i].iov_base, iovec[i].iov_len, &o, &p);
793 xor_hash ^= le64toh(o->data.hash);
794 items[i].object_offset = htole64(p);
797 r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, ret, offset);
805 int journal_file_move_to_entry(JournalFile *f, uint64_t seqnum, Object **ret, uint64_t *offset) {
807 uint64_t lower, upper, p, n, k;
812 n = le64toh(f->header->bisect_table_size) / sizeof(uint64_t);
813 k = le64toh(f->header->arena_max_size) / n;
816 upper = le64toh(f->header->last_bisect_offset)/k+1;
818 while (lower < upper) {
819 k = (upper + lower) / 2;
820 p = le64toh(f->bisect_table[k]);
827 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
831 if (o->entry.seqnum == seqnum) {
839 } else if (seqnum < o->entry.seqnum)
841 else if (seqnum > o->entry.seqnum)
845 assert(lower == upper);
850 /* The object we are looking for is between
851 * bisect_table[lower-1] and bisect_table[lower] */
853 p = le64toh(f->bisect_table[lower-1]);
856 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
860 if (o->entry.seqnum == seqnum) {
869 } if (seqnum < o->entry.seqnum)
872 if (o->entry.next_entry_offset == 0)
875 p = le64toh(o->entry.next_entry_offset);
881 int journal_file_next_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
888 np = le64toh(f->header->head_entry_offset);
890 if (le64toh(o->object.type) != OBJECT_ENTRY)
893 np = le64toh(o->entry.next_entry_offset);
899 r = journal_file_move_to_object(f, np, OBJECT_ENTRY, &o);
912 int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
919 np = le64toh(f->header->tail_entry_offset);
921 if (le64toh(o->object.type) != OBJECT_ENTRY)
924 np = le64toh(o->entry.prev_entry_offset);
930 r = journal_file_move_to_object(f, np, OBJECT_ENTRY, &o);
943 int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
944 uint64_t p, osize, hash, h;
948 assert(data || size == 0);
950 osize = offsetof(Object, data.payload) + size;
952 hash = hash64(data, size);
953 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
954 p = le64toh(f->hash_table[h].head_hash_offset);
959 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
963 if (le64toh(o->object.size) == osize &&
964 memcmp(o->data.payload, data, size) == 0) {
966 if (le64toh(o->data.hash) != hash)
969 if (o->data.head_entry_offset == 0)
972 p = le64toh(o->data.head_entry_offset);
973 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
986 p = le64toh(o->data.next_hash_offset);
992 int journal_file_find_last_entry(JournalFile *f, const void *data, uint64_t size, Object **ret, uint64_t *offset) {
993 uint64_t p, osize, hash, h;
997 assert(data || size == 0);
999 osize = offsetof(Object, data.payload) + size;
1001 hash = hash64(data, size);
1002 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
1003 p = le64toh(f->hash_table[h].tail_hash_offset);
1008 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
1012 if (le64toh(o->object.size) == osize &&
1013 memcmp(o->data.payload, data, size) == 0) {
1015 if (le64toh(o->data.hash) != hash)
1018 if (o->data.tail_entry_offset == 0)
1021 p = le64toh(o->data.tail_entry_offset);
1022 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
1035 p = le64toh(o->data.prev_hash_offset);
1041 void journal_file_dump(JournalFile *f) {
1042 char a[33], b[33], c[33];
1049 printf("File ID: %s\n"
1052 "Arena size: %llu\n",
1053 sd_id128_to_string(f->header->file_id, a),
1054 sd_id128_to_string(f->header->machine_id, b),
1055 sd_id128_to_string(f->header->boot_id, c),
1056 (unsigned long long) le64toh(f->header->arena_size));
1058 p = le64toh(f->header->head_object_offset);
1060 r = journal_file_move_to_object(f, p, -1, &o);
1064 switch (o->object.type) {
1067 printf("Type: OBJECT_UNUSED\n");
1071 printf("Type: OBJECT_DATA\n");
1075 printf("Type: OBJECT_ENTRY %llu\n", (unsigned long long) le64toh(o->entry.seqnum));
1078 case OBJECT_HASH_TABLE:
1079 printf("Type: OBJECT_HASH_TABLE\n");
1082 case OBJECT_BISECT_TABLE:
1083 printf("Type: OBJECT_BISECT_TABLE\n");
1087 if (p == le64toh(f->header->tail_object_offset))
1090 p = p + ALIGN64(le64toh(o->object.size));
1095 log_error("File corrupt");
1098 int journal_file_open(
1102 JournalFile *template,
1103 JournalFile **ret) {
1107 bool newly_created = false;
1111 if ((flags & O_ACCMODE) != O_RDONLY &&
1112 (flags & O_ACCMODE) != O_RDWR)
1115 f = new0(JournalFile, 1);
1122 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1123 f->prot = prot_from_flags(flags);
1125 f->path = strdup(fname);
1131 f->fd = open(f->path, f->flags|O_CLOEXEC, f->mode);
1137 if (fstat(f->fd, &f->last_stat) < 0) {
1142 if (f->last_stat.st_size == 0 && f->writable) {
1143 newly_created = true;
1145 r = journal_file_init_header(f, template);
1149 if (fstat(f->fd, &f->last_stat) < 0) {
1155 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1160 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1161 if (f->header == MAP_FAILED) {
1167 if (!newly_created) {
1168 r = journal_file_verify_header(f);
1174 r = journal_file_refresh_header(f);
1179 if (newly_created) {
1181 r = journal_file_setup_hash_table(f);
1185 r = journal_file_setup_bisect_table(f);
1190 r = journal_file_map_hash_table(f);
1194 r = journal_file_map_bisect_table(f);
1204 journal_file_close(f);
1209 int journal_file_rotate(JournalFile **f) {
1212 JournalFile *old_file, *new_file = NULL;
1220 if (!old_file->writable)
1223 if (!endswith(old_file->path, ".journal"))
1226 l = strlen(old_file->path);
1228 p = new(char, l + 1 + 16 + 1 + 32 + 1 + 16 + 1);
1232 memcpy(p, old_file->path, l - 8);
1234 sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1);
1235 snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
1236 "-%016llx-%016llx.journal",
1237 (unsigned long long) le64toh((*f)->header->seqnum),
1238 (unsigned long long) le64toh((*f)->header->tail_entry_realtime));
1240 r = rename(old_file->path, p);
1246 old_file->header->state = le32toh(STATE_ARCHIVED);
1248 r = journal_file_open(old_file->path, old_file->flags, old_file->mode, old_file, &new_file);
1249 journal_file_close(old_file);
1255 struct vacuum_info {
1260 sd_id128_t seqnum_id;
1264 static int vacuum_compare(const void *_a, const void *_b) {
1265 const struct vacuum_info *a, *b;
1270 if (sd_id128_equal(a->seqnum_id, b->seqnum_id)) {
1271 if (a->seqnum < b->seqnum)
1273 else if (a->seqnum > b->seqnum)
1279 if (a->realtime < b->realtime)
1281 else if (a->realtime > b->realtime)
1284 return memcmp(&a->seqnum_id, &b->seqnum_id, 16);
1287 int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t min_free) {
1290 struct vacuum_info *list = NULL;
1291 unsigned n_list = 0, n_allocated = 0, i;
1297 max_use = DEFAULT_MAX_USE;
1299 d = opendir(directory);
1305 struct dirent buf, *de;
1309 unsigned long long seqnum, realtime;
1310 sd_id128_t seqnum_id;
1312 k = readdir_r(d, &buf, &de);
1321 if (!dirent_is_file_with_suffix(de, ".journal"))
1324 q = strlen(de->d_name);
1326 if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8)
1329 if (de->d_name[q-8-16-1] != '-' ||
1330 de->d_name[q-8-16-1-16-1] != '-' ||
1331 de->d_name[q-8-16-1-16-1-32-1] != '@')
1334 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
1337 if (!S_ISREG(st.st_mode))
1340 p = strdup(de->d_name);
1346 de->d_name[q-8-16-1-16-1] = 0;
1347 if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) {
1352 if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) {
1357 if (n_list >= n_allocated) {
1358 struct vacuum_info *j;
1360 n_allocated = MAX(n_allocated * 2U, 8U);
1361 j = realloc(list, n_allocated * sizeof(struct vacuum_info));
1371 list[n_list].filename = p;
1372 list[n_list].usage = (uint64_t) st.st_blksize * (uint64_t) st.st_blocks;
1373 list[n_list].seqnum = seqnum;
1374 list[n_list].realtime = realtime;
1375 list[n_list].seqnum_id = seqnum_id;
1377 sum += list[n_list].usage;
1382 qsort(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
1384 for(i = 0; i < n_list; i++) {
1387 if (fstatvfs(dirfd(d), &ss) < 0) {
1392 if (sum <= max_use &&
1393 (uint64_t) ss.f_bavail * (uint64_t) ss.f_bsize >= min_free)
1396 if (unlinkat(dirfd(d), list[i].filename, 0) >= 0) {
1397 log_debug("Deleted archived journal %s/%s.", directory, list[i].filename);
1398 sum -= list[i].usage;
1399 } else if (errno != ENOENT)
1400 log_warning("Failed to delete %s/%s: %m", directory, list[i].filename);
1404 for (i = 0; i < n_list; i++)
1405 free(list[i].filename);