]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-file: goodbye JournalFile.mmap
authorVito Caputo <vcaputo@pengaru.com>
Thu, 25 Nov 2021 23:32:07 +0000 (15:32 -0800)
committerVito Caputo <vcaputo@pengaru.com>
Tue, 7 Dec 2021 21:03:34 +0000 (13:03 -0800)
This gets rid of the manual MMapCache ref/unref goop in
journal_file_{open,close}(), in favor of just letting the
JournalFile.cache_fd MMapFileDescriptor carry the baton.

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

index 11b9da1cb54b52a29db0d8cc76642f13e6c0736b..4302a99f80d8b6afe6676765751495c63233c996 100644 (file)
@@ -217,15 +217,13 @@ JournalFile* journal_file_close(JournalFile *f) {
         if (!f)
                 return NULL;
 
-        if (f->mmap && f->cache_fd)
+        if (f->cache_fd)
                 mmap_cache_fd_free(f->cache_fd);
 
         if (f->close_fd)
                 safe_close(f->fd);
         free(f->path);
 
-        mmap_cache_unref(f->mmap);
-
         ordered_hashmap_free_free(f->chain_cache);
 
 #if HAVE_COMPRESSION
@@ -3252,6 +3250,7 @@ int journal_file_open(
                 JournalFile **ret) {
 
         bool newly_created = false;
+        MMapCache *m = mmap_cache;
         JournalFile *f;
         void *h;
         int r;
@@ -3320,11 +3319,9 @@ int journal_file_open(
                 }
         }
 
-        if (mmap_cache)
-                f->mmap = mmap_cache_ref(mmap_cache);
-        else {
-                f->mmap = mmap_cache_new();
-                if (!f->mmap) {
+        if (!m) {
+                m = mmap_cache_new();
+                if (!m) {
                         r = -ENOMEM;
                         goto fail;
                 }
@@ -3371,12 +3368,17 @@ int journal_file_open(
                         goto fail;
         }
 
-        f->cache_fd = mmap_cache_add_fd(f->mmap, f->fd, prot_from_flags(flags));
+        /* On success this incs refcnt on *m, which mmap_cache_fd_free() will dec. */
+        f->cache_fd = mmap_cache_add_fd(m, f->fd, prot_from_flags(flags));
         if (!f->cache_fd) {
                 r = -ENOMEM;
                 goto fail;
         }
 
+        /* If we created *m just for this file, unref *m so only f->cache_fd's ref remains */
+        if (!mmap_cache)
+                (void) mmap_cache_unref(m);
+
         r = journal_file_fstat(f);
         if (r < 0)
                 goto fail;
index 90c9dbe6374fdd29f2dd5b37bf034a64eee591c3..b90e3a608accf0b759ee47e2682979abd6d88a20 100644 (file)
@@ -92,7 +92,6 @@ typedef struct JournalFile {
         uint64_t current_xor_hash;
 
         JournalMetrics metrics;
-        MMapCache *mmap;
 
         sd_event_source *post_change_timer;
         usec_t post_change_timer_period;