naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
923a60
From 283df68dbc1d90cad21beec6563215c26c69ec2c Mon Sep 17 00:00:00 2001
923a60
From: Lennart Poettering <lennart@poettering.net>
923a60
Date: Fri, 24 Jul 2015 01:55:45 +0200
923a60
Subject: [PATCH] journal: avoid mapping empty data and field hash tables
923a60
923a60
When a new journal file is created we write the header first, then sync
923a60
and only then create the data and field hash tables in them. That means
923a60
to other processes it might appear that the files have a valid header
923a60
but not data and field hash tables. Our reader code should be able to
923a60
deal with this.
923a60
923a60
With this change we'll not map the two hash tables right-away after
923a60
opening a file for reading anymore (because that will of course fail if
923a60
the objects are missing), but delay this until the first time we access
923a60
them. On top of that, when we want to look something up in the hash
923a60
tables and we notice they aren't initialized yet, we consider them
923a60
empty.
923a60
923a60
This improves handling of some journal files reported in #487.
923a60
923a60
Cherry-picked from: dade37d403f1b8c1d7bb2efbe2361f2a3e999613
923a60
Related: #1350232
923a60
---
923a60
 src/journal/journal-file.c   | 37 +++++++++++++++++++++++++-----------
923a60
 src/journal/journal-file.h   |  3 +++
923a60
 src/journal/journal-verify.c | 14 ++++++++++++++
923a60
 3 files changed, 43 insertions(+), 11 deletions(-)
923a60
923a60
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
923a60
index 892fe47340..ef18497879 100644
923a60
--- a/src/journal/journal-file.c
923a60
+++ b/src/journal/journal-file.c
923a60
@@ -656,13 +656,16 @@ static int journal_file_setup_field_hash_table(JournalFile *f) {
923a60
         return 0;
923a60
 }
923a60
 
923a60
-static int journal_file_map_data_hash_table(JournalFile *f) {
923a60
+int journal_file_map_data_hash_table(JournalFile *f) {
923a60
         uint64_t s, p;
923a60
         void *t;
923a60
         int r;
923a60
 
923a60
         assert(f);
923a60
 
923a60
+        if (f->data_hash_table)
923a60
+                return 0;
923a60
+
923a60
         p = le64toh(f->header->data_hash_table_offset);
923a60
         s = le64toh(f->header->data_hash_table_size);
923a60
 
923a60
@@ -678,13 +681,16 @@ static int journal_file_map_data_hash_table(JournalFile *f) {
923a60
         return 0;
923a60
 }
923a60
 
923a60
-static int journal_file_map_field_hash_table(JournalFile *f) {
923a60
+int journal_file_map_field_hash_table(JournalFile *f) {
923a60
         uint64_t s, p;
923a60
         void *t;
923a60
         int r;
923a60
 
923a60
         assert(f);
923a60
 
923a60
+        if (f->field_hash_table)
923a60
+                return 0;
923a60
+
923a60
         p = le64toh(f->header->field_hash_table_offset);
923a60
         s = le64toh(f->header->field_hash_table_size);
923a60
 
923a60
@@ -803,10 +809,18 @@ int journal_file_find_field_object_with_hash(
923a60
         assert(f);
923a60
         assert(field && size > 0);
923a60
 
923a60
+        /* If the field hash table is empty, we can't find anything */
923a60
+        if (le64toh(f->header->field_hash_table_size) <= 0)
923a60
+                return 0;
923a60
+
923a60
+        /* Map the field hash table, if it isn't mapped yet. */
923a60
+        r = journal_file_map_field_hash_table(f);
923a60
+        if (r < 0)
923a60
+                return r;
923a60
+
923a60
         osize = offsetof(Object, field.payload) + size;
923a60
 
923a60
         m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
923a60
-
923a60
         if (m <= 0)
923a60
                 return -EBADMSG;
923a60
 
923a60
@@ -866,6 +880,15 @@ int journal_file_find_data_object_with_hash(
923a60
         assert(f);
923a60
         assert(data || size == 0);
923a60
 
923a60
+        /* If there's no data hash table, then there's no entry. */
923a60
+        if (le64toh(f->header->data_hash_table_size) <= 0)
923a60
+                return 0;
923a60
+
923a60
+        /* Map the data hash table, if it isn't mapped yet. */
923a60
+        r = journal_file_map_data_hash_table(f);
923a60
+        if (r < 0)
923a60
+                return r;
923a60
+
923a60
         osize = offsetof(Object, data.payload) + size;
923a60
 
923a60
         m = le64toh(f->header->data_hash_table_size) / sizeof(HashItem);
923a60
@@ -2707,14 +2730,6 @@ int journal_file_open(
923a60
 #endif
923a60
         }
923a60
 
923a60
-        r = journal_file_map_field_hash_table(f);
923a60
-        if (r < 0)
923a60
-                goto fail;
923a60
-
923a60
-        r = journal_file_map_data_hash_table(f);
923a60
-        if (r < 0)
923a60
-                goto fail;
923a60
-
923a60
         if (mmap_cache_got_sigbus(f->mmap, f->fd)) {
923a60
                 r = -EIO;
923a60
                 goto fail;
923a60
diff --git a/src/journal/journal-file.h b/src/journal/journal-file.h
923a60
index 0f29b092b7..c74ad5fc58 100644
923a60
--- a/src/journal/journal-file.h
923a60
+++ b/src/journal/journal-file.h
923a60
@@ -234,3 +234,6 @@ static inline bool JOURNAL_FILE_COMPRESS(JournalFile *f) {
923a60
         assert(f);
923a60
         return f->compress_xz || f->compress_lz4;
923a60
 }
923a60
+
923a60
+int journal_file_map_data_hash_table(JournalFile *f);
923a60
+int journal_file_map_field_hash_table(JournalFile *f);
923a60
diff --git a/src/journal/journal-verify.c b/src/journal/journal-verify.c
923a60
index 983217c1bc..d2d5c400c1 100644
923a60
--- a/src/journal/journal-verify.c
923a60
+++ b/src/journal/journal-verify.c
923a60
@@ -590,6 +590,13 @@ static int verify_hash_table(
923a60
         assert(last_usec);
923a60
 
923a60
         n = le64toh(f->header->data_hash_table_size) / sizeof(HashItem);
923a60
+        if (n <= 0)
923a60
+                return 0;
923a60
+
923a60
+        r = journal_file_map_data_hash_table(f);
923a60
+        if (r < 0)
923a60
+                return log_error_errno(r, "Failed to map data hash table: %m");
923a60
+
923a60
         for (i = 0; i < n; i++) {
923a60
                 uint64_t last = 0, p;
923a60
 
923a60
@@ -647,6 +654,13 @@ static int data_object_in_hash_table(JournalFile *f, uint64_t hash, uint64_t p)
923a60
         assert(f);
923a60
 
923a60
         n = le64toh(f->header->data_hash_table_size) / sizeof(HashItem);
923a60
+        if (n <= 0)
923a60
+                return 0;
923a60
+
923a60
+        r = journal_file_map_data_hash_table(f);
923a60
+        if (r < 0)
923a60
+                return log_error_errno(r, "Failed to map data hash table: %m");
923a60
+
923a60
         h = hash % n;
923a60
 
923a60
         q = le64toh(f->data_hash_table[h].head_hash_offset);