]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
bcachefs: Pull disk accounting hooks out of trans_commit.c
authorKent Overstreet <kent.overstreet@linux.dev>
Tue, 1 Oct 2024 20:59:08 +0000 (16:59 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sat, 21 Dec 2024 06:36:14 +0000 (01:36 -0500)
Also, fix a minor bug in the revert path, where we weren't checking the
journal entry type correctly.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/btree_trans_commit.c
fs/bcachefs/disk_accounting.h

index 9bf471fa4361405b21fd57faed5a82296fef9374..3d951846a1bedff822b03d5e1732adb982388760 100644 (file)
@@ -609,14 +609,6 @@ static noinline int bch2_trans_commit_run_gc_triggers(struct btree_trans *trans)
        return 0;
 }
 
-static struct bversion journal_pos_to_bversion(struct journal_res *res, unsigned offset)
-{
-       return (struct bversion) {
-               .hi = res->seq >> 32,
-               .lo = (res->seq << 32) | (res->offset + offset),
-       };
-}
-
 static inline int
 bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
                               struct btree_insert_entry **stopped_at,
@@ -701,25 +693,14 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
        struct jset_entry *entry = trans->journal_entries;
 
        percpu_down_read(&c->mark_lock);
-
        for (entry = trans->journal_entries;
             entry != (void *) ((u64 *) trans->journal_entries + trans->journal_entries_u64s);
             entry = vstruct_next(entry))
                if (entry->type == BCH_JSET_ENTRY_write_buffer_keys &&
                    entry->start->k.type == KEY_TYPE_accounting) {
-                       BUG_ON(!trans->journal_res.ref);
-
-                       struct bkey_i_accounting *a = bkey_i_to_accounting(entry->start);
-
-                       a->k.bversion = journal_pos_to_bversion(&trans->journal_res,
-                                                       (u64 *) entry - (u64 *) trans->journal_entries);
-                       BUG_ON(bversion_zero(a->k.bversion));
-
-                       if (likely(!(flags & BCH_TRANS_COMMIT_skip_accounting_apply))) {
-                               ret = bch2_accounting_mem_mod_locked(trans, accounting_i_to_s_c(a), BCH_ACCOUNTING_normal);
-                               if (ret)
-                                       goto revert_fs_usage;
-                       }
+                       ret = bch2_accounting_trans_commit_hook(trans, bkey_i_to_accounting(entry->start), flags);
+                       if (ret)
+                               goto revert_fs_usage;
                }
        percpu_up_read(&c->mark_lock);
 
@@ -833,13 +814,9 @@ revert_fs_usage:
             entry2 != entry;
             entry2 = vstruct_next(entry2))
                if (entry2->type == BCH_JSET_ENTRY_write_buffer_keys &&
-                   entry2->start->k.type == KEY_TYPE_accounting) {
-                       struct bkey_s_accounting a = bkey_i_to_s_accounting(entry2->start);
-
-                       bch2_accounting_neg(a);
-                       bch2_accounting_mem_mod_locked(trans, a.c, BCH_ACCOUNTING_normal);
-                       bch2_accounting_neg(a);
-               }
+                   entry2->start->k.type == KEY_TYPE_accounting)
+                       bch2_accounting_trans_commit_revert(trans,
+                                       bkey_i_to_accounting(entry2->start), flags);
        percpu_up_read(&c->mark_lock);
        return ret;
 }
index 4ea6c8a092bc11d75e77209f7b42ed86aeee1361..6639535dc91c9d408811a21f36f6722de9535517 100644 (file)
@@ -2,6 +2,7 @@
 #ifndef _BCACHEFS_DISK_ACCOUNTING_H
 #define _BCACHEFS_DISK_ACCOUNTING_H
 
+#include "btree_update.h"
 #include "eytzinger.h"
 #include "sb-members.h"
 
@@ -204,6 +205,43 @@ static inline void bch2_accounting_mem_read(struct bch_fs *c, struct bpos p,
        bch2_accounting_mem_read_counters(acc, idx, v, nr, false);
 }
 
+static inline struct bversion journal_pos_to_bversion(struct journal_res *res, unsigned offset)
+{
+       EBUG_ON(!res->ref);
+
+       return (struct bversion) {
+               .hi = res->seq >> 32,
+               .lo = (res->seq << 32) | (res->offset + offset),
+       };
+}
+
+static inline int bch2_accounting_trans_commit_hook(struct btree_trans *trans,
+                                                   struct bkey_i_accounting *a,
+                                                   unsigned commit_flags)
+{
+       a->k.bversion = journal_pos_to_bversion(&trans->journal_res,
+                                               (u64 *) a - (u64 *) trans->journal_entries);
+
+       EBUG_ON(bversion_zero(a->k.bversion));
+
+       return likely(!(commit_flags & BCH_TRANS_COMMIT_skip_accounting_apply))
+               ? bch2_accounting_mem_mod_locked(trans, accounting_i_to_s_c(a), BCH_ACCOUNTING_normal)
+               : 0;
+}
+
+static inline void bch2_accounting_trans_commit_revert(struct btree_trans *trans,
+                                                      struct bkey_i_accounting *a_i,
+                                                      unsigned commit_flags)
+{
+       if (likely(!(commit_flags & BCH_TRANS_COMMIT_skip_accounting_apply))) {
+               struct bkey_s_accounting a = accounting_i_to_s(a_i);
+
+               bch2_accounting_neg(a);
+               bch2_accounting_mem_mod_locked(trans, a.c, BCH_ACCOUNTING_normal);
+               bch2_accounting_neg(a);
+       }
+}
+
 int bch2_fs_replicas_usage_read(struct bch_fs *, darray_char *);
 int bch2_fs_accounting_read(struct bch_fs *, darray_char *, unsigned);
 void bch2_fs_accounting_to_text(struct printbuf *, struct bch_fs *);