bcachefs: Refactor journal entry adding
authorKent Overstreet <kent.overstreet@gmail.com>
Sun, 5 Jun 2022 19:29:00 +0000 (15:29 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:33 +0000 (17:09 -0400)
This takes copying the payload out of bch2_journal_add_entry(), which
means we can use it for journal_transaction_name() - also prep work for
journalling overwrites.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
fs/bcachefs/btree_update_leaf.c
fs/bcachefs/journal.h

index 1a84bdbfda0bb20a2e50fc0eece9de0281549293..df2b21245d00160fb37a3fa99f8e1541396f2e03 100644 (file)
@@ -309,25 +309,15 @@ static inline int bch2_trans_journal_res_get(struct btree_trans *trans,
 static noinline void journal_transaction_name(struct btree_trans *trans)
 {
        struct bch_fs *c = trans->c;
-       struct jset_entry *entry = journal_res_entry(&c->journal, &trans->journal_res);
-       struct jset_entry_log *l = container_of(entry, struct jset_entry_log, entry);
-       unsigned u64s = JSET_ENTRY_LOG_U64s - 1;
-       unsigned b, buflen = u64s * sizeof(u64);
-
-       l->entry.u64s           = cpu_to_le16(u64s);
-       l->entry.btree_id       = 0;
-       l->entry.level          = 0;
-       l->entry.type           = BCH_JSET_ENTRY_log;
-       l->entry.pad[0]         = 0;
-       l->entry.pad[1]         = 0;
-       l->entry.pad[2]         = 0;
-       b = min_t(unsigned, strlen(trans->fn), buflen);
-       memcpy(l->d, trans->fn, b);
-       while (b < buflen)
-               l->d[b++] = '\0';
-
-       trans->journal_res.offset       += JSET_ENTRY_LOG_U64s;
-       trans->journal_res.u64s         -= JSET_ENTRY_LOG_U64s;
+       struct journal *j = &c->journal;
+       struct jset_entry *entry =
+               bch2_journal_add_entry(j, &trans->journal_res,
+                                      BCH_JSET_ENTRY_log, 0, 0,
+                                      JSET_ENTRY_LOG_U64s);
+       struct jset_entry_log *l =
+               container_of(entry, struct jset_entry_log, entry);
+
+       strncpy(l->d, trans->fn, JSET_ENTRY_LOG_U64s * sizeof(u64));
 }
 
 static inline enum btree_insert_ret
@@ -416,10 +406,13 @@ static inline void do_btree_insert_one(struct btree_trans *trans,
 
        if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY)) &&
            !(i->flags & BTREE_UPDATE_NOJOURNAL)) {
-               bch2_journal_add_keys(j, &trans->journal_res,
-                                     i->btree_id,
-                                     i->level,
-                                     i->k);
+               struct jset_entry *entry;
+
+               entry = bch2_journal_add_entry(j, &trans->journal_res,
+                                      BCH_JSET_ENTRY_btree_keys,
+                                      i->btree_id, i->level,
+                                      i->k->k.u64s);
+               bkey_copy(&entry->start[0], i->k);
 
                if (trans->journal_seq)
                        *trans->journal_seq = trans->journal_res.seq;
@@ -1127,7 +1120,7 @@ int __bch2_trans_commit(struct btree_trans *trans)
        trans->journal_transaction_names = READ_ONCE(c->opts.journal_transaction_names);
 
        if (trans->journal_transaction_names)
-               trans->journal_u64s += JSET_ENTRY_LOG_U64s;
+               trans->journal_u64s += jset_u64s(JSET_ENTRY_LOG_U64s);
 
        trans_for_each_update(trans, i) {
                BUG_ON(!i->path->should_be_locked);
index 59453dcfa4e937137404e7be0452a7e30114356c..d3caa7ea7ce9446b2b9994f41af0782abaa79b08 100644 (file)
@@ -199,9 +199,9 @@ journal_res_entry(struct journal *j, struct journal_res *res)
        return vstruct_idx(j->buf[res->idx].data, res->offset);
 }
 
-static inline unsigned journal_entry_set(struct jset_entry *entry, unsigned type,
+static inline unsigned journal_entry_init(struct jset_entry *entry, unsigned type,
                                          enum btree_id id, unsigned level,
-                                         const void *data, unsigned u64s)
+                                         unsigned u64s)
 {
        entry->u64s     = cpu_to_le16(u64s);
        entry->btree_id = id;
@@ -210,32 +210,33 @@ static inline unsigned journal_entry_set(struct jset_entry *entry, unsigned type
        entry->pad[0]   = 0;
        entry->pad[1]   = 0;
        entry->pad[2]   = 0;
-       memcpy_u64s_small(entry->_data, data, u64s);
-
        return jset_u64s(u64s);
 }
 
-static inline void bch2_journal_add_entry(struct journal *j, struct journal_res *res,
-                                         unsigned type, enum btree_id id,
-                                         unsigned level,
+static inline unsigned journal_entry_set(struct jset_entry *entry, unsigned type,
+                                         enum btree_id id, unsigned level,
                                          const void *data, unsigned u64s)
 {
-       unsigned actual = journal_entry_set(journal_res_entry(j, res),
-                              type, id, level, data, u64s);
+       unsigned ret = journal_entry_init(entry, type, id, level, u64s);
+
+       memcpy_u64s_small(entry->_data, data, u64s);
+       return ret;
+}
+
+static inline struct jset_entry *
+bch2_journal_add_entry(struct journal *j, struct journal_res *res,
+                        unsigned type, enum btree_id id,
+                        unsigned level, unsigned u64s)
+{
+       struct jset_entry *entry = journal_res_entry(j, res);
+       unsigned actual = journal_entry_init(entry, type, id, level, u64s);
 
        EBUG_ON(!res->ref);
        EBUG_ON(actual > res->u64s);
 
        res->offset     += actual;
        res->u64s       -= actual;
-}
-
-static inline void bch2_journal_add_keys(struct journal *j, struct journal_res *res,
-                                       enum btree_id id, unsigned level,
-                                       const struct bkey_i *k)
-{
-       bch2_journal_add_entry(j, res, BCH_JSET_ENTRY_btree_keys,
-                              id, level, k, k->k.u64s);
+       return entry;
 }
 
 static inline bool journal_entry_empty(struct jset *j)
@@ -283,7 +284,7 @@ static inline void bch2_journal_res_put(struct journal *j,
        while (res->u64s)
                bch2_journal_add_entry(j, res,
                                       BCH_JSET_ENTRY_btree_keys,
-                                      0, 0, NULL, 0);
+                                      0, 0, 0);
 
        bch2_journal_buf_put(j, res->idx);