]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/journal_io.c
Update bcachefs sources to 8e1519ccb6 bcachefs: Add tracepoint & counter for btree...
[bcachefs-tools-debian] / libbcachefs / journal_io.c
index f6374a2bdc83fe4353be990faab786a2c96ac6c8..641ca36f384f04cd7ee033d64916407600f78259 100644 (file)
@@ -1622,6 +1622,52 @@ static void do_journal_write(struct closure *cl)
        return;
 }
 
+static void bch2_journal_entries_postprocess(struct bch_fs *c, struct jset *jset)
+{
+       struct jset_entry *i, *next, *prev = NULL;
+
+       /*
+        * Simple compaction, dropping empty jset_entries (from journal
+        * reservations that weren't fully used) and merging jset_entries that
+        * can be.
+        *
+        * If we wanted to be really fancy here, we could sort all the keys in
+        * the jset and drop keys that were overwritten - probably not worth it:
+        */
+       vstruct_for_each_safe(jset, i, next) {
+               unsigned u64s = le16_to_cpu(i->u64s);
+
+               /* Empty entry: */
+               if (!u64s)
+                       continue;
+
+               if (i->type == BCH_JSET_ENTRY_btree_root)
+                       bch2_journal_entry_to_btree_root(c, i);
+
+               /* Can we merge with previous entry? */
+               if (prev &&
+                   i->btree_id == prev->btree_id &&
+                   i->level    == prev->level &&
+                   i->type     == prev->type &&
+                   i->type     == BCH_JSET_ENTRY_btree_keys &&
+                   le16_to_cpu(prev->u64s) + u64s <= U16_MAX) {
+                       memmove_u64s_down(vstruct_next(prev),
+                                         i->_data,
+                                         u64s);
+                       le16_add_cpu(&prev->u64s, u64s);
+                       continue;
+               }
+
+               /* Couldn't merge, move i into new position (after prev): */
+               prev = prev ? vstruct_next(prev) : jset->start;
+               if (i != prev)
+                       memmove_u64s_down(prev, i, jset_u64s(u64s));
+       }
+
+       prev = prev ? vstruct_next(prev) : jset->start;
+       jset->u64s = cpu_to_le32((u64 *) prev - jset->_data);
+}
+
 void bch2_journal_write(struct closure *cl)
 {
        struct journal *j = container_of(cl, struct journal, io);
@@ -1693,7 +1739,7 @@ void bch2_journal_write(struct closure *cl)
         * entry:
         */
 
-       bch2_journal_entries_to_btree_roots(c, jset);
+       bch2_journal_entries_postprocess(c, jset);
 
        start = end = vstruct_last(jset);