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, direction_t direction, Object **ret, uint64_t *offset) {
902 np = le64toh(direction == DIRECTION_DOWN ?
903 f->header->head_entry_offset :
904 f->header->tail_entry_offset);
906 if (le64toh(o->object.type) != OBJECT_ENTRY)
909 np = le64toh(direction == DIRECTION_DOWN ?
910 o->entry.next_entry_offset :
911 o->entry.prev_entry_offset);
917 r = journal_file_move_to_object(f, np, OBJECT_ENTRY, &o);
930 int journal_file_prev_entry(JournalFile *f, Object *o, Object **ret, uint64_t *offset) {
937 np = le64toh(f->header->tail_entry_offset);
939 if (le64toh(o->object.type) != OBJECT_ENTRY)
942 np = le64toh(o->entry.prev_entry_offset);
948 r = journal_file_move_to_object(f, np, OBJECT_ENTRY, &o);
961 int journal_file_find_first_entry(JournalFile *f, const void *data, uint64_t size, direction_t direction, Object **ret, uint64_t *offset) {
962 uint64_t p, osize, hash, h;
966 assert(data || size == 0);
968 osize = offsetof(Object, data.payload) + size;
970 hash = hash64(data, size);
971 h = hash % (le64toh(f->header->hash_table_size) / sizeof(HashItem));
972 p = le64toh(f->hash_table[h].head_hash_offset);
977 r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
981 if (le64toh(o->object.size) == osize &&
982 memcmp(o->data.payload, data, size) == 0) {
984 if (le64toh(o->data.hash) != hash)
987 p = le64toh(direction == DIRECTION_DOWN ?
988 o->data.head_entry_offset :
989 o->data.tail_entry_offset);
994 r = journal_file_move_to_object(f, p, OBJECT_ENTRY, &o);
1007 p = le64toh(o->data.next_hash_offset);
1013 void journal_file_dump(JournalFile *f) {
1014 char a[33], b[33], c[33];
1021 printf("File ID: %s\n"
1024 "Arena size: %llu\n",
1025 sd_id128_to_string(f->header->file_id, a),
1026 sd_id128_to_string(f->header->machine_id, b),
1027 sd_id128_to_string(f->header->boot_id, c),
1028 (unsigned long long) le64toh(f->header->arena_size));
1030 p = le64toh(f->header->head_object_offset);
1032 r = journal_file_move_to_object(f, p, -1, &o);
1036 switch (o->object.type) {
1039 printf("Type: OBJECT_UNUSED\n");
1043 printf("Type: OBJECT_DATA\n");
1047 printf("Type: OBJECT_ENTRY %llu %llu %llu\n",
1048 (unsigned long long) le64toh(o->entry.seqnum),
1049 (unsigned long long) le64toh(o->entry.monotonic),
1050 (unsigned long long) le64toh(o->entry.realtime));
1053 case OBJECT_HASH_TABLE:
1054 printf("Type: OBJECT_HASH_TABLE\n");
1057 case OBJECT_BISECT_TABLE:
1058 printf("Type: OBJECT_BISECT_TABLE\n");
1062 if (p == le64toh(f->header->tail_object_offset))
1065 p = p + ALIGN64(le64toh(o->object.size));
1070 log_error("File corrupt");
1073 int journal_file_open(
1077 JournalFile *template,
1078 JournalFile **ret) {
1082 bool newly_created = false;
1086 if ((flags & O_ACCMODE) != O_RDONLY &&
1087 (flags & O_ACCMODE) != O_RDWR)
1090 f = new0(JournalFile, 1);
1097 f->writable = (flags & O_ACCMODE) != O_RDONLY;
1098 f->prot = prot_from_flags(flags);
1100 f->path = strdup(fname);
1106 f->fd = open(f->path, f->flags|O_CLOEXEC, f->mode);
1112 if (fstat(f->fd, &f->last_stat) < 0) {
1117 if (f->last_stat.st_size == 0 && f->writable) {
1118 newly_created = true;
1120 r = journal_file_init_header(f, template);
1124 if (fstat(f->fd, &f->last_stat) < 0) {
1130 if (f->last_stat.st_size < (off_t) sizeof(Header)) {
1135 f->header = mmap(NULL, PAGE_ALIGN(sizeof(Header)), prot_from_flags(flags), MAP_SHARED, f->fd, 0);
1136 if (f->header == MAP_FAILED) {
1142 if (!newly_created) {
1143 r = journal_file_verify_header(f);
1149 r = journal_file_refresh_header(f);
1154 if (newly_created) {
1156 r = journal_file_setup_hash_table(f);
1160 r = journal_file_setup_bisect_table(f);
1165 r = journal_file_map_hash_table(f);
1169 r = journal_file_map_bisect_table(f);
1179 journal_file_close(f);
1184 int journal_file_rotate(JournalFile **f) {
1187 JournalFile *old_file, *new_file = NULL;
1195 if (!old_file->writable)
1198 if (!endswith(old_file->path, ".journal"))
1201 l = strlen(old_file->path);
1203 p = new(char, l + 1 + 16 + 1 + 32 + 1 + 16 + 1);
1207 memcpy(p, old_file->path, l - 8);
1209 sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1);
1210 snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
1211 "-%016llx-%016llx.journal",
1212 (unsigned long long) le64toh((*f)->header->seqnum),
1213 (unsigned long long) le64toh((*f)->header->tail_entry_realtime));
1215 r = rename(old_file->path, p);
1221 old_file->header->state = le32toh(STATE_ARCHIVED);
1223 r = journal_file_open(old_file->path, old_file->flags, old_file->mode, old_file, &new_file);
1224 journal_file_close(old_file);
1230 struct vacuum_info {
1235 sd_id128_t seqnum_id;
1239 static int vacuum_compare(const void *_a, const void *_b) {
1240 const struct vacuum_info *a, *b;
1245 if (sd_id128_equal(a->seqnum_id, b->seqnum_id)) {
1246 if (a->seqnum < b->seqnum)
1248 else if (a->seqnum > b->seqnum)
1254 if (a->realtime < b->realtime)
1256 else if (a->realtime > b->realtime)
1259 return memcmp(&a->seqnum_id, &b->seqnum_id, 16);
1262 int journal_directory_vacuum(const char *directory, uint64_t max_use, uint64_t min_free) {
1265 struct vacuum_info *list = NULL;
1266 unsigned n_list = 0, n_allocated = 0, i;
1272 max_use = DEFAULT_MAX_USE;
1274 d = opendir(directory);
1280 struct dirent buf, *de;
1284 unsigned long long seqnum, realtime;
1285 sd_id128_t seqnum_id;
1287 k = readdir_r(d, &buf, &de);
1296 if (!dirent_is_file_with_suffix(de, ".journal"))
1299 q = strlen(de->d_name);
1301 if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8)
1304 if (de->d_name[q-8-16-1] != '-' ||
1305 de->d_name[q-8-16-1-16-1] != '-' ||
1306 de->d_name[q-8-16-1-16-1-32-1] != '@')
1309 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
1312 if (!S_ISREG(st.st_mode))
1315 p = strdup(de->d_name);
1321 de->d_name[q-8-16-1-16-1] = 0;
1322 if (sd_id128_from_string(de->d_name + q-8-16-1-16-1-32, &seqnum_id) < 0) {
1327 if (sscanf(de->d_name + q-8-16-1-16, "%16llx-%16llx.journal", &seqnum, &realtime) != 2) {
1332 if (n_list >= n_allocated) {
1333 struct vacuum_info *j;
1335 n_allocated = MAX(n_allocated * 2U, 8U);
1336 j = realloc(list, n_allocated * sizeof(struct vacuum_info));
1346 list[n_list].filename = p;
1347 list[n_list].usage = (uint64_t) st.st_blksize * (uint64_t) st.st_blocks;
1348 list[n_list].seqnum = seqnum;
1349 list[n_list].realtime = realtime;
1350 list[n_list].seqnum_id = seqnum_id;
1352 sum += list[n_list].usage;
1357 qsort(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
1359 for(i = 0; i < n_list; i++) {
1362 if (fstatvfs(dirfd(d), &ss) < 0) {
1367 if (sum <= max_use &&
1368 (uint64_t) ss.f_bavail * (uint64_t) ss.f_bsize >= min_free)
1371 if (unlinkat(dirfd(d), list[i].filename, 0) >= 0) {
1372 log_debug("Deleted archived journal %s/%s.", directory, list[i].filename);
1373 sum -= list[i].usage;
1374 } else if (errno != ENOENT)
1375 log_warning("Failed to delete %s/%s: %m", directory, list[i].filename);
1379 for (i = 0; i < n_list; i++)
1380 free(list[i].filename);