572a44
From eb38fb593b201a97620555c0aca83ff487a50748 Mon Sep 17 00:00:00 2001
572a44
From: Lennart Poettering <lennart@poettering.net>
572a44
Date: Tue, 26 Nov 2013 18:39:42 +0100
572a44
Subject: [PATCH] journal: when appending to journal file, allocate larger
572a44
 blocks at once
572a44
572a44
Conflicts:
572a44
	src/journal/journal-file.c
572a44
---
572a44
 src/journal/journal-file.c | 51 +++++++++++++++++++++++-----------------------
572a44
 1 file changed, 26 insertions(+), 25 deletions(-)
572a44
572a44
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
572a44
index 81c344f..4062a83 100644
572a44
--- a/src/journal/journal-file.c
572a44
+++ b/src/journal/journal-file.c
572a44
@@ -68,6 +68,9 @@
572a44
 /* How many entries to keep in the entry array chain cache at max */
572a44
 #define CHAIN_CACHE_MAX 20
572a44
 
572a44
+/* How much to increase the journal file size at once each time we allocate something new. */
572a44
+#define FILE_SIZE_INCREASE (8ULL*1024ULL*1024ULL)              /* 8MB */
572a44
+
572a44
 int journal_file_set_online(JournalFile *f) {
572a44
         assert(f);
572a44
 
572a44
@@ -218,8 +221,7 @@ static int journal_file_refresh_header(JournalFile *f) {
572a44
         journal_file_set_online(f);
572a44
 
572a44
         /* Sync the online state to disk */
572a44
-        msync(f->header, PAGE_ALIGN(sizeof(Header)), MS_SYNC);
572a44
-        fdatasync(f->fd);
572a44
+        fsync(f->fd);
572a44
 
572a44
         return 0;
572a44
 }
572a44
@@ -313,7 +315,7 @@ static int journal_file_verify_header(JournalFile *f) {
572a44
 }
572a44
 
572a44
 static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
572a44
-        uint64_t old_size, new_size;
572a44
+        uint64_t old_size, new_size, file_size;
572a44
         int r;
572a44
 
572a44
         assert(f);
572a44
@@ -333,12 +335,10 @@ static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size)
572a44
         if (new_size <= old_size)
572a44
                 return 0;
572a44
 
572a44
-        if (f->metrics.max_size > 0 &&
572a44
-            new_size > f->metrics.max_size)
572a44
+        if (f->metrics.max_size > 0 && new_size > f->metrics.max_size)
572a44
                 return -E2BIG;
572a44
 
572a44
-        if (new_size > f->metrics.min_size &&
572a44
-            f->metrics.keep_free > 0) {
572a44
+        if (new_size > f->metrics.min_size && f->metrics.keep_free > 0) {
572a44
                 struct statvfs svfs;
572a44
 
572a44
                 if (fstatvfs(f->fd, &svfs) >= 0) {
572a44
@@ -363,8 +363,16 @@ static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size)
572a44
         if (r != 0)
572a44
                 return -r;
572a44
 
572a44
-        if (fstat(f->fd, &f->last_stat) < 0)
572a44
-                return -errno;
572a44
+        /* Increase the file size a bit further than this, so that we
572a44
+         * we can create larger memory maps to cache */
572a44
+        file_size = ((new_size+FILE_SIZE_INCREASE-1) / FILE_SIZE_INCREASE) * FILE_SIZE_INCREASE;
572a44
+        if (file_size > (uint64_t) f->last_stat.st_size) {
572a44
+                if (file_size > new_size)
572a44
+                        ftruncate(f->fd, file_size);
572a44
+
572a44
+                if (fstat(f->fd, &f->last_stat) < 0)
572a44
+                        return -errno;
572a44
+        }
572a44
 
572a44
         f->header->arena_size = htole64(new_size - le64toh(f->header->header_size));
572a44
 
572a44
@@ -1344,7 +1352,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
572a44
 
572a44
         /* Order by the position on disk, in order to improve seek
572a44
          * times for rotating media. */
572a44
-        qsort(items, n_iovec, sizeof(EntryItem), entry_item_cmp);
572a44
+        qsort_safe(items, n_iovec, sizeof(EntryItem), entry_item_cmp);
572a44
 
572a44
         r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset);
572a44
 
572a44
@@ -2551,7 +2559,7 @@ fail:
572a44
 }
572a44
 
572a44
 int journal_file_rotate(JournalFile **f, bool compress, bool seal) {
572a44
-        char *p;
572a44
+        _cleanup_free_ char *p = NULL;
572a44
         size_t l;
572a44
         JournalFile *old_file, *new_file = NULL;
572a44
         int r;
572a44
@@ -2568,22 +2576,15 @@ int journal_file_rotate(JournalFile **f, bool compress, bool seal) {
572a44
                 return -EINVAL;
572a44
 
572a44
         l = strlen(old_file->path);
572a44
-
572a44
-        p = new(char, l + 1 + 32 + 1 + 16 + 1 + 16 + 1);
572a44
-        if (!p)
572a44
+        r = asprintf(&p, "%.*s@" SD_ID128_FORMAT_STR "-%016"PRIx64"-%016"PRIx64".journal",
572a44
+                     (int) l - 8, old_file->path,
572a44
+                     SD_ID128_FORMAT_VAL(old_file->header->seqnum_id),
572a44
+                     le64toh((*f)->header->head_entry_seqnum),
572a44
+                     le64toh((*f)->header->head_entry_realtime));
572a44
+        if (r < 0)
572a44
                 return -ENOMEM;
572a44
 
572a44
-        memcpy(p, old_file->path, l - 8);
572a44
-        p[l-8] = '@';
572a44
-        sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1);
572a44
-        snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
572a44
-                 "-%016"PRIx64"-%016"PRIx64".journal",
572a44
-                 le64toh((*f)->header->head_entry_seqnum),
572a44
-                 le64toh((*f)->header->head_entry_realtime));
572a44
-
572a44
         r = rename(old_file->path, p);
572a44
-        free(p);
572a44
-
572a44
         if (r < 0)
572a44
                 return -errno;
572a44
 
572a44
@@ -2634,7 +2635,7 @@ int journal_file_open_reliably(
572a44
 
572a44
         l = strlen(fname);
572a44
         if (asprintf(&p, "%.*s@%016llx-%016llx.journal~",
572a44
-                     (int) (l-8), fname,
572a44
+                     (int) l - 8, fname,
572a44
                      (unsigned long long) now(CLOCK_REALTIME),
572a44
                      random_ull()) < 0)
572a44
                 return -ENOMEM;