HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/libsystemd/sd-journal/journal-file.c

178,971 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: bed0c812a6957f446f48f2101888a46dca5a6bca

Lines 4001–4786 of 4,786

        if (JOURNAL_HEADER_CONTAINS(f->header, n_fields)) {
                size_t n_field_items = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
                printf("Field objects: %"PRIu64"\n"
                       "Field hash table fill: %.1f%%\n",
                       le64toh(f->header->n_fields),
                       100.0 * (double) le64toh(f->header->n_fields) / (double) n_field_items);
        }
 
        if (JOURNAL_HEADER_CONTAINS(f->header, n_tags))
                printf("Tag objects: %"PRIu64"\n",
                       le64toh(f->header->n_tags));
        if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays))
                printf("Entry array objects: %"PRIu64"\n",
                       le64toh(f->header->n_entry_arrays));
 
        if (JOURNAL_HEADER_CONTAINS(f->header, field_hash_chain_depth))
                printf("Deepest field hash chain: %" PRIu64"\n",
                       f->header->field_hash_chain_depth);
 
        if (JOURNAL_HEADER_CONTAINS(f->header, data_hash_chain_depth))
                printf("Deepest data hash chain: %" PRIu64"\n",
                       f->header->data_hash_chain_depth);
 
        if (fstat(f->fd, &st) >= 0)
                printf("Disk usage: %s\n", FORMAT_BYTES((uint64_t) st.st_blocks * 512ULL));
}
 
static int journal_file_warn_btrfs(JournalFile *f) {
        unsigned attrs;
        int r;
 
        assert(f);
 
        /* Before we write anything, check if the COW logic is turned
         * off on btrfs. Given our write pattern that is quite
         * unfriendly to COW file systems this should greatly improve
         * performance on COW file systems, such as btrfs, at the
         * expense of data integrity features (which shouldn't be too
         * bad, given that we do our own checksumming). */
 
        r = fd_is_fs_type(f->fd, BTRFS_SUPER_MAGIC);
        if (r < 0)
                return log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, "Failed to determine if journal is on btrfs: %m");
        if (r == 0)
                return 0;
 
        r = read_attr_fd(f->fd, &attrs);
        if (r < 0)
                return log_ratelimit_warning_errno(r, JOURNAL_LOG_RATELIMIT, "Failed to read file attributes: %m");
 
        if (attrs & FS_NOCOW_FL) {
                log_debug("Detected btrfs file system with copy-on-write disabled, all is good.");
                return 0;
        }
 
        log_ratelimit_notice(JOURNAL_LOG_RATELIMIT,
                             "Creating journal file %s on a btrfs file system, and copy-on-write is enabled. "
                             "This is likely to slow down journal access substantially, please consider turning "
                             "off the copy-on-write file attribute on the journal directory, using chattr +C.",
                             f->path);
 
        return 1;
}
 
static void journal_default_metrics(JournalMetrics *m, int fd, bool compact) {
        struct statvfs ss;
        uint64_t fs_size = 0;
 
        assert(m);
        assert(fd >= 0);
 
        if (fstatvfs(fd, &ss) >= 0)
                fs_size = u64_multiply_safe(ss.f_frsize, ss.f_blocks);
        else
                log_debug_errno(errno, "Failed to determine disk size: %m");
 
        if (m->max_use == UINT64_MAX) {
 
                if (fs_size > 0)
                        m->max_use = CLAMP(PAGE_ALIGN_U64(fs_size / 10), /* 10% of file system size */
                                           MAX_USE_LOWER, MAX_USE_UPPER);
                else
                        m->max_use = MAX_USE_LOWER;
        } else {
                m->max_use = PAGE_ALIGN_U64(m->max_use);
 
                if (m->max_use != 0 && m->max_use < JOURNAL_FILE_SIZE_MIN*2)
                        m->max_use = JOURNAL_FILE_SIZE_MIN*2;
        }
 
        if (m->min_use == UINT64_MAX) {
                if (fs_size > 0)
                        m->min_use = CLAMP(PAGE_ALIGN_U64(fs_size / 50), /* 2% of file system size */
                                           MIN_USE_LOW, MIN_USE_HIGH);
                else
                        m->min_use = MIN_USE_LOW;
        }
 
        if (m->min_use > m->max_use)
                m->min_use = m->max_use;
 
        if (m->max_size == UINT64_MAX)
                m->max_size = MIN(PAGE_ALIGN_U64(m->max_use / 8), /* 8 chunks */
                                  MAX_SIZE_UPPER);
        else
                m->max_size = PAGE_ALIGN_U64(m->max_size);
 
        if (compact && m->max_size > JOURNAL_COMPACT_SIZE_MAX)
                m->max_size = JOURNAL_COMPACT_SIZE_MAX;
 
        if (m->max_size != 0) {
                if (m->max_size < JOURNAL_FILE_SIZE_MIN)
                        m->max_size = JOURNAL_FILE_SIZE_MIN;
 
                /* Silence static analyzers */
                assert(m->max_size <= UINT64_MAX / 2);
                if (m->max_use != 0 && m->max_size*2 > m->max_use)
                        m->max_use = m->max_size*2;
        }
 
        if (m->min_size == UINT64_MAX)
                m->min_size = JOURNAL_FILE_SIZE_MIN;
        else
                m->min_size = CLAMP(PAGE_ALIGN_U64(m->min_size),
                                    JOURNAL_FILE_SIZE_MIN,
                                    m->max_size ?: UINT64_MAX);
 
        if (m->keep_free == UINT64_MAX) {
                if (fs_size > 0)
                        m->keep_free = MIN(PAGE_ALIGN_U64(fs_size / 20), /* 5% of file system size */
                                           KEEP_FREE_UPPER);
                else
                        m->keep_free = DEFAULT_KEEP_FREE;
        }
 
        if (m->n_max_files == UINT64_MAX)
                m->n_max_files = DEFAULT_N_MAX_FILES;
 
        log_debug("Fixed min_use=%s max_use=%s max_size=%s min_size=%s keep_free=%s n_max_files=%" PRIu64,
                  FORMAT_BYTES(m->min_use),
                  FORMAT_BYTES(m->max_use),
                  FORMAT_BYTES(m->max_size),
                  FORMAT_BYTES(m->min_size),
                  FORMAT_BYTES(m->keep_free),
                  m->n_max_files);
}
 
int journal_file_open(
                int fd,
                const char *fname,
                int open_flags,
                JournalFileFlags file_flags,
                mode_t mode,
                uint64_t compress_threshold_bytes,
                JournalMetrics *metrics,
                MMapCache *mmap_cache,
                JournalFile *template,
                JournalFile **ret) {
 
        bool newly_created = false;
        JournalFile *f;
        void *h;
        int r;
 
        assert(fd >= 0 || fname);
        assert((file_flags & ~_JOURNAL_FILE_FLAGS_ALL) == 0);
        assert(mmap_cache);
        assert(ret);
 
        if (!IN_SET((open_flags & O_ACCMODE_STRICT), O_RDONLY, O_RDWR))
                return -EINVAL;
 
        if ((open_flags & O_ACCMODE_STRICT) == O_RDONLY && FLAGS_SET(open_flags, O_CREAT))
                return -EINVAL;
 
        if (fname && (open_flags & O_CREAT) && !endswith(fname, ".journal"))
                return -EINVAL;
 
        f = new(JournalFile, 1);
        if (!f)
                return -ENOMEM;
 
        *f = (JournalFile) {
                .fd = fd,
                .mode = mode,
                .open_flags = open_flags,
                .compress_threshold_bytes = compress_threshold_bytes == UINT64_MAX ?
                                            DEFAULT_COMPRESS_THRESHOLD :
                                            MAX(MIN_COMPRESS_THRESHOLD, compress_threshold_bytes),
                .strict_order = FLAGS_SET(file_flags, JOURNAL_STRICT_ORDER),
                .newest_boot_id_prioq_idx = PRIOQ_IDX_NULL,
                .last_direction = _DIRECTION_INVALID,
                .tail_timestamp_ratelimit = { .interval = USEC_PER_SEC, .burst = 1 },
        };
 
        if (f->fd < 0) {
                /* We pass O_NONBLOCK here, so that in case somebody pointed us to some character device node or FIFO
                 * or so, we likely fail quickly than block for long. For regular files O_NONBLOCK has no effect, hence
                 * it doesn't hurt in that case. */
 
                f->fd = openat_report_new(AT_FDCWD, fname, f->open_flags|O_CLOEXEC|O_NONBLOCK, f->mode, &newly_created);
                if (f->fd < 0) {
                        r = f->fd;
                        goto fail;
                }
 
                /* fds we opened here by us should also be closed by us. */
                f->close_fd = true;
 
                r = fd_nonblock(f->fd, false);
                if (r < 0)
                        goto fail;
 
                r = fd_get_path(f->fd, &f->path);
                if (r < 0)
                        goto fail;
 
                if (!newly_created) {
                        r = journal_file_fstat(f);
                        if (r < 0)
                                goto fail;
                }
        } else {
                /* If we don't know the path, fill in something explanatory and vaguely useful */
                f->path = strdup(fname ?: FORMAT_PROC_FD_PATH(fd));
                if (!f->path) {
                        r = -ENOMEM;
                        goto fail;
                }
 
                r = journal_file_fstat(f);
                if (r < 0)
                        goto fail;
 
                /* If we just got the fd passed in, we don't really know if we created the file anew */
                newly_created = f->last_stat.st_size == 0 && journal_file_writable(f);
        }
 
        r = mmap_cache_add_fd(mmap_cache, f->fd, mmap_prot_from_open_flags(open_flags), &f->cache_fd);
        if (r < 0)
                goto fail;
 
        if (newly_created) {
                (void) journal_file_warn_btrfs(f);
 
                /* Let's attach the creation time to the journal file, so that the vacuuming code knows the age of this
                 * file even if the file might end up corrupted one day... Ideally we'd just use the creation time many
                 * file systems maintain for each file, but the API to query this is very new, hence let's emulate this
                 * via extended attributes. If extended attributes are not supported we'll just skip this, and rely
                 * solely on mtime/atime/ctime of the file. */
                (void) fd_setcrtime(f->fd, 0);
 
                r = journal_file_init_header(f, file_flags, template);
                if (r < 0)
                        goto fail;
 
                r = journal_file_fstat(f);
                if (r < 0)
                        goto fail;
        }
 
        if (f->last_stat.st_size < (off_t) HEADER_SIZE_MIN) {
                r = -ENODATA;
                goto fail;
        }
 
        r = mmap_cache_fd_get(f->cache_fd, MMAP_CACHE_CATEGORY_HEADER, true, 0, PAGE_ALIGN(sizeof(Header)), &f->last_stat, &h);
        if (r == -EINVAL) {
                /* Some file systems (jffs2 or p9fs) don't support mmap() properly (or only read-only
                 * mmap()), and return EINVAL in that case. Let's propagate that as a more recognizable error
                 * code. */
                r = -EAFNOSUPPORT;
                goto fail;
        }
        if (r < 0)
                goto fail;
 
        f->header = h;
 
        if (!newly_created) {
                r = journal_file_verify_header(f);
                if (r < 0)
                        goto fail;
        }
 
        if (!newly_created && journal_file_writable(f) && JOURNAL_HEADER_SEALED(f->header)) {
                r = journal_file_auth_load(f);
                if (r < 0)
                        goto fail;
        }
 
        if (journal_file_writable(f)) {
                if (metrics) {
                        journal_default_metrics(metrics, f->fd, JOURNAL_HEADER_COMPACT(f->header));
                        f->metrics = *metrics;
                } else if (template)
                        f->metrics = template->metrics;
 
                r = journal_file_refresh_header(f);
                if (r < 0)
                        goto fail;
        }
 
        if (newly_created) {
                r = journal_file_setup_field_hash_table(f);
                if (r < 0)
                        goto fail;
 
                r = journal_file_setup_data_hash_table(f);
                if (r < 0)
                        goto fail;
 
                r = journal_file_auth_append_tag_first(f);
                if (r < 0)
                        goto fail;
        }
 
        if (mmap_cache_fd_got_sigbus(f->cache_fd)) {
                r = -EIO;
                goto fail;
        }
 
        if (template && template->post_change_timer) {
                r = journal_file_enable_post_change_timer(
                                f,
                                sd_event_source_get_event(template->post_change_timer),
                                template->post_change_timer_period);
 
                if (r < 0)
                        goto fail;
        }
 
        /* The file is opened now successfully, thus we take possession of any passed in fd. */
        f->close_fd = true;
 
        if (DEBUG_LOGGING) {
                static int last_seal = -1, last_keyed_hash = -1;
                static Compression last_compression = _COMPRESSION_INVALID;
                static uint64_t last_bytes = UINT64_MAX;
 
                if (last_seal != JOURNAL_HEADER_SEALED(f->header) ||
                    last_keyed_hash != JOURNAL_HEADER_KEYED_HASH(f->header) ||
                    last_compression != JOURNAL_FILE_COMPRESSION(f) ||
                    last_bytes != f->compress_threshold_bytes) {
 
                        log_debug("Journal effective settings seal=%s keyed_hash=%s compress=%s compress_threshold_bytes=%s",
                                  yes_no(JOURNAL_HEADER_SEALED(f->header)), yes_no(JOURNAL_HEADER_KEYED_HASH(f->header)),
                                  compression_to_string(JOURNAL_FILE_COMPRESSION(f)), FORMAT_BYTES(f->compress_threshold_bytes));
                        last_seal = JOURNAL_HEADER_SEALED(f->header);
                        last_keyed_hash = JOURNAL_HEADER_KEYED_HASH(f->header);
                        last_compression = JOURNAL_FILE_COMPRESSION(f);
                        last_bytes = f->compress_threshold_bytes;
                }
        }
 
        *ret = f;
        return 0;
 
fail:
        if (f->cache_fd && mmap_cache_fd_got_sigbus(f->cache_fd))
                r = -EIO;
 
        (void) journal_file_close(f);
 
        if (newly_created && fd < 0)
                (void) unlink(fname);
 
        return r;
}
 
int journal_file_parse_uid_from_filename(const char *path, uid_t *ret_uid) {
        _cleanup_free_ char *buf = NULL, *p = NULL;
        const char *a, *b, *at;
        int r;
 
        /* This helper returns -EREMOTE when the filename doesn't match user online/offline journal
         * pattern. Hence it currently doesn't parse archived or disposed user journals. */
 
        assert(path);
        assert(ret_uid);
 
        r = path_extract_filename(path, &p);
        if (r < 0)
                return r;
        if (r == O_DIRECTORY)
                return -EISDIR;
 
        a = startswith(p, "user-");
        if (!a)
                return -EREMOTE;
        b = endswith(p, ".journal");
        if (!b)
                return -EREMOTE;
 
        at = strchr(a, '@');
        if (at)
                return -EREMOTE;
 
        buf = strndup(a, b-a);
        if (!buf)
                return -ENOMEM;
 
        return parse_uid(buf, ret_uid);
}
 
int journal_file_archive(JournalFile *f, char **ret_previous_path) {
        _cleanup_free_ char *p = NULL;
 
        assert(f);
 
        if (!journal_file_writable(f))
                return -EINVAL;
 
        /* Is this a journal file that was passed to us as fd? If so, we synthesized a path name for it, and we refuse
         * rotation, since we don't know the actual path, and couldn't rename the file hence. */
        if (path_startswith(f->path, "/proc/self/fd"))
                return -EINVAL;
 
        if (!endswith(f->path, ".journal"))
                return -EINVAL;
 
        if (asprintf(&p, "%.*s@" SD_ID128_FORMAT_STR "-%016"PRIx64"-%016"PRIx64".journal",
                     (int) strlen(f->path) - 8, f->path,
                     SD_ID128_FORMAT_VAL(f->header->seqnum_id),
                     le64toh(f->header->head_entry_seqnum),
                     le64toh(f->header->head_entry_realtime)) < 0)
                return -ENOMEM;
 
        /* Try to rename the file to the archived version. If the file already was deleted, we'll get ENOENT, let's
         * ignore that case. */
        if (rename(f->path, p) < 0 && errno != ENOENT)
                return -errno;
 
        /* Sync the rename to disk */
        (void) fsync_directory_of_file(f->fd);
 
        if (ret_previous_path)
                *ret_previous_path = TAKE_PTR(f->path);
 
        free_and_replace(f->path, p);
 
        /* Set as archive so offlining commits w/state=STATE_ARCHIVED. Previously we would set old_file->header->state
         * to STATE_ARCHIVED directly here, but journal_file_set_offline() short-circuits when state != STATE_ONLINE,
         * which would result in the rotated journal never getting fsync() called before closing.  Now we simply queue
         * the archive state by setting an archive bit, leaving the state as STATE_ONLINE so proper offlining
         * occurs. */
        f->archive = true;
 
        return 0;
}
 
int journal_file_dispose(int dir_fd, const char *fname) {
        _cleanup_free_ char *p = NULL;
 
        assert(fname);
 
        /* Renames a journal file to *.journal~, i.e. to mark it as corrupted or otherwise uncleanly shutdown. Note that
         * this is done without looking into the file or changing any of its contents. The idea is that this is called
         * whenever something is suspicious and we want to move the file away and make clear that it is not accessed
         * for writing anymore. */
 
        if (!endswith(fname, ".journal"))
                return -EINVAL;
 
        if (asprintf(&p, "%.*s@%016" PRIx64 "-%016" PRIx64 ".journal~",
                     (int) strlen(fname) - 8, fname,
                     now(CLOCK_REALTIME),
                     random_u64()) < 0)
                return -ENOMEM;
 
        if (renameat(dir_fd, fname, dir_fd, p) < 0)
                return -errno;
 
        return 0;
}
 
int journal_file_copy_entry(
                JournalFile *from,
                JournalFile *to,
                Object *o,
                uint64_t p,
                uint64_t *seqnum,
                sd_id128_t *seqnum_id) {
 
        _cleanup_free_ EntryItem *items_alloc = NULL;
        EntryItem *items;
        uint64_t n, m = 0, xor_hash = 0;
        sd_id128_t boot_id;
        dual_timestamp ts;
        int r;
 
        assert(from);
        assert(to);
        assert(o);
        assert(p > 0);
 
        if (!journal_file_writable(to))
                return -EPERM;
 
        ts = (dual_timestamp) {
                .monotonic = le64toh(o->entry.monotonic),
                .realtime = le64toh(o->entry.realtime),
        };
        boot_id = o->entry.boot_id;
 
        n = journal_file_entry_n_items(from, o);
        if (n == 0)
                return 0;
 
        if (n < ALLOCA_MAX / sizeof(EntryItem) / 2)
                items = newa(EntryItem, n);
        else {
                items_alloc = new(EntryItem, n);
                if (!items_alloc)
                        return -ENOMEM;
 
                items = items_alloc;
        }
 
        for (uint64_t i = 0; i < n; i++) {
                uint64_t h, q;
                const void *data;
                size_t l;
                Object *u;
 
                q = journal_file_entry_item_object_offset(from, o, i);
                r = journal_file_data_payload(from, NULL, q, NULL, 0, 0, &data, &l);
                if (IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) {
                        log_debug_errno(r, "Entry item %"PRIu64" data object is bad, skipping over it: %m", i);
                        continue;
                }
                if (r < 0)
                        return r;
                assert(r > 0);
 
                r = journal_file_append_data(to, data, l, &u, &h);
                if (r == -EUCLEAN) {
                        log_debug_errno(r, "Entry item %"PRIu64" invalid, skipping over it: %m", i);
                        continue;
                }
                if (r < 0)
                        return r;
 
                if (JOURNAL_HEADER_KEYED_HASH(to->header))
                        xor_hash ^= jenkins_hash64(data, l);
                else
                        xor_hash ^= le64toh(u->data.hash);
 
                items[m++] = (EntryItem) {
                        .object_offset = h,
                        .hash = le64toh(u->data.hash),
                };
        }
 
        if (m == 0)
                return 0;
 
        r = journal_file_append_entry_internal(
                        to,
                        &ts,
                        &boot_id,
                        &from->header->machine_id,
                        xor_hash,
                        items,
                        m,
                        seqnum,
                        seqnum_id,
                        /* ret_object= */ NULL,
                        /* ret_offset= */ NULL);
 
        if (mmap_cache_fd_got_sigbus(to->cache_fd))
                return -EIO;
 
        return r;
}
 
void journal_reset_metrics(JournalMetrics *m) {
        assert(m);
 
        /* Set everything to "pick automatic values". */
 
        *m = (JournalMetrics) {
                .min_use = UINT64_MAX,
                .max_use = UINT64_MAX,
                .min_size = UINT64_MAX,
                .max_size = UINT64_MAX,
                .keep_free = UINT64_MAX,
                .n_max_files = UINT64_MAX,
        };
}
 
bool journal_metrics_equal(const JournalMetrics *x, const JournalMetrics *y) {
        assert(x);
        assert(y);
 
        return
                x->max_size == y->max_size &&
                x->min_size == y->min_size &&
                x->max_use == y->max_use &&
                x->min_use == y->min_use &&
                x->keep_free == y->keep_free &&
                x->n_max_files == y->n_max_files;
}
 
int journal_file_get_cutoff_realtime_usec(JournalFile *f, usec_t *ret_from, usec_t *ret_to) {
        assert(f);
        assert(f->header);
        assert(ret_from || ret_to);
 
        if (ret_from) {
                if (f->header->head_entry_realtime == 0)
                        return -ENOENT;
 
                *ret_from = le64toh(f->header->head_entry_realtime);
        }
 
        if (ret_to) {
                Object *o;
                int r;
 
                /* The header may be stale on unclean shutdown, so don't trust it. */
                r = journal_file_next_entry(f, 0, DIRECTION_UP, &o, NULL);
                if (r < 0)
                        return r;
                if (r == 0)
                        return -ENOENT;
 
                *ret_to = le64toh(o->entry.realtime);
        }
 
        return 1;
}
 
int journal_file_get_cutoff_monotonic_usec(JournalFile *f, sd_id128_t boot_id, usec_t *ret_from, usec_t *ret_to) {
        Object *o;
        uint64_t p;
        int r;
 
        assert(f);
        assert(ret_from || ret_to);
 
        /* FIXME: fix return value assignment on success with 0. */
 
        r = find_data_object_by_boot_id(f, boot_id, &o, &p);
        if (r <= 0)
                return r;
 
        if (le64toh(o->data.n_entries) <= 0)
                return 0;
 
        if (ret_from) {
                r = journal_file_move_to_object(f, OBJECT_ENTRY, le64toh(o->data.entry_offset), &o);
                if (r < 0)
                        return r;
 
                *ret_from = le64toh(o->entry.monotonic);
        }
 
        if (ret_to) {
                r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
                if (r < 0)
                        return r;
 
                r = journal_file_move_to_entry_for_data(f, o, DIRECTION_UP, &o, NULL);
                if (r <= 0)
                        return r;
 
                *ret_to = le64toh(o->entry.monotonic);
        }
 
        return 1;
}
 
bool journal_file_rotate_suggested(JournalFile *f, usec_t max_file_usec, int log_level) {
        assert(f);
        assert(f->header);
 
        /* If we gained new header fields we gained new features,
         * hence suggest a rotation */
        if (le64toh(f->header->header_size) < sizeof(Header)) {
                log_ratelimit_full(log_level, JOURNAL_LOG_RATELIMIT,
                                   "%s uses an outdated header, suggesting rotation.", f->path);
                return true;
        }
 
        /* Let's check if the hash tables grew over a certain fill level (75%, borrowing this value from
         * Java's hash table implementation), and if so suggest a rotation. To calculate the fill level we
         * need the n_data field, which only exists in newer versions. */
 
        if (JOURNAL_HEADER_CONTAINS(f->header, n_data))
                if (le64toh(f->header->n_data) * 4ULL > (le64toh(f->header->data_hash_table_size) / sizeof(HashItem)) * 3ULL) {
                        size_t n_data_items = le64toh(f->header->data_hash_table_size) / sizeof(HashItem);
                        log_ratelimit_full(
                                log_level, JOURNAL_LOG_RATELIMIT,
                                "Data hash table of %s has a fill level at %.1f (%"PRIu64" of %"PRIu64" items, %"PRIu64" file size, %"PRIu64" bytes per hash table item), suggesting rotation.",
                                f->path,
                                100.0 * (double) le64toh(f->header->n_data) / (double) n_data_items,
                                le64toh(f->header->n_data),
                                le64toh(f->header->data_hash_table_size) / sizeof(HashItem),
                                (uint64_t) f->last_stat.st_size,
                                f->last_stat.st_size / le64toh(f->header->n_data));
                        return true;
                }
 
        if (JOURNAL_HEADER_CONTAINS(f->header, n_fields))
                if (le64toh(f->header->n_fields) * 4ULL > (le64toh(f->header->field_hash_table_size) / sizeof(HashItem)) * 3ULL) {
                        size_t n_field_items = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
                        log_ratelimit_full(
                                log_level, JOURNAL_LOG_RATELIMIT,
                                "Field hash table of %s has a fill level at %.1f (%"PRIu64" of %"PRIu64" items), suggesting rotation.",
                                f->path,
                                100.0 * (double) le64toh(f->header->n_fields) / (double) n_field_items,
                                le64toh(f->header->n_fields),
                                le64toh(f->header->field_hash_table_size) / sizeof(HashItem));
                        return true;
                }
 
        /* If there are too many hash collisions somebody is most likely playing games with us. Hence, if our
         * longest chain is longer than some threshold, let's suggest rotation. */
        if (JOURNAL_HEADER_CONTAINS(f->header, data_hash_chain_depth) &&
            le64toh(f->header->data_hash_chain_depth) > HASH_CHAIN_DEPTH_MAX) {
                log_ratelimit_full(
                        log_level, JOURNAL_LOG_RATELIMIT,
                        "Data hash table of %s has deepest hash chain of length %" PRIu64 ", suggesting rotation.",
                        f->path, le64toh(f->header->data_hash_chain_depth));
                return true;
        }
 
        if (JOURNAL_HEADER_CONTAINS(f->header, field_hash_chain_depth) &&
            le64toh(f->header->field_hash_chain_depth) > HASH_CHAIN_DEPTH_MAX) {
                log_ratelimit_full(
                        log_level, JOURNAL_LOG_RATELIMIT,
                        "Field hash table of %s has deepest hash chain of length at %" PRIu64 ", suggesting rotation.",
                        f->path, le64toh(f->header->field_hash_chain_depth));
                return true;
        }
 
        /* Are the data objects properly indexed by field objects? */
        if (JOURNAL_HEADER_CONTAINS(f->header, n_data) &&
            JOURNAL_HEADER_CONTAINS(f->header, n_fields) &&
            le64toh(f->header->n_data) > 0 &&
            le64toh(f->header->n_fields) == 0) {
                log_ratelimit_full(
                        log_level, JOURNAL_LOG_RATELIMIT,
                        "Data objects of %s are not indexed by field objects, suggesting rotation.",
                        f->path);
                return true;
        }
 
        if (max_file_usec > 0) {
                usec_t t, h;
 
                h = le64toh(f->header->head_entry_realtime);
                t = now(CLOCK_REALTIME);
 
                if (h > 0 && t > h + max_file_usec) {
                        log_ratelimit_full(
                                log_level, JOURNAL_LOG_RATELIMIT,
                                "Oldest entry in %s is older than the configured file retention duration (%s), suggesting rotation.",
                                f->path, FORMAT_TIMESPAN(max_file_usec, USEC_PER_SEC));
                        return true;
                }
        }
 
        return false;
}
 
bool journal_file_writable(const JournalFile *f) {
        assert(f);
 
        return (f->open_flags & O_ACCMODE_STRICT) != O_RDONLY;
}
 
static const char * const journal_object_type_table[] = {
        [OBJECT_UNUSED]           = "unused",
        [OBJECT_DATA]             = "data",
        [OBJECT_FIELD]            = "field",
        [OBJECT_ENTRY]            = "entry",
        [OBJECT_DATA_HASH_TABLE]  = "data hash table",
        [OBJECT_FIELD_HASH_TABLE] = "field hash table",
        [OBJECT_ENTRY_ARRAY]      = "entry array",
        [OBJECT_TAG]              = "tag",
};
 
DEFINE_STRING_TABLE_LOOKUP_TO_STRING(journal_object_type, ObjectType);