]> www.infradead.org Git - nvme.git/commitdiff
bcachefs: bch2_accounting_mem_gc()
authorKent Overstreet <kent.overstreet@linux.dev>
Thu, 6 Jun 2024 17:48:54 +0000 (13:48 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 14 Jul 2024 23:00:15 +0000 (19:00 -0400)
Add a new helper to free zeroed out accounting entries, and use it in
bch2_replicas_gc2(); bch2_replicas_gc2() was killing superblock replicas
entries if their corresponding accounting counters were nonzero, but
that's incorrect - the superblock replicas entry needs to exist if the
accounting entry exists, not if it's nonzero, because we check and
create the replicas entry when creating the new accounting entry - we
don't know when it's becoming nonzero.

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

index 3327d465908d36164fd10f69b101ba2d62008fff..5b1546d1a23d0d93d5ea1d9589a8f6d90fd53b3e 100644 (file)
@@ -273,6 +273,38 @@ int bch2_accounting_mem_insert(struct bch_fs *c, struct bkey_s_c_accounting a, b
        return ret;
 }
 
+static bool accounting_mem_entry_is_zero(struct accounting_mem_entry *e)
+{
+       for (unsigned i = 0; i < e->nr_counters; i++)
+               if (percpu_u64_get(e->v[0] + i) ||
+                   (e->v[1] &&
+                    percpu_u64_get(e->v[1] + i)))
+                       return false;
+       return true;
+}
+
+void bch2_accounting_mem_gc(struct bch_fs *c)
+{
+       struct bch_accounting_mem *acc = &c->accounting;
+
+       percpu_down_write(&c->mark_lock);
+       struct accounting_mem_entry *dst = acc->k.data;
+
+       darray_for_each(acc->k, src) {
+               if (accounting_mem_entry_is_zero(src)) {
+                       free_percpu(src->v[0]);
+                       free_percpu(src->v[1]);
+               } else {
+                       *dst++ = *src;
+               }
+       }
+
+       acc->k.nr = dst - acc->k.data;
+       eytzinger0_sort(acc->k.data, acc->k.nr, sizeof(acc->k.data[0]),
+                       accounting_pos_cmp, NULL);
+       percpu_up_write(&c->mark_lock);
+}
+
 /*
  * Read out accounting keys for replicas entries, as an array of
  * bch_replicas_usage entries.
index 81dab01d1eb8e3774d12049f16cdbc18cd56bca8..3d3f25e08b69614fd4fd817072db53ec8eb85202 100644 (file)
@@ -105,6 +105,7 @@ static inline int accounting_pos_cmp(const void *_l, const void *_r)
 }
 
 int bch2_accounting_mem_insert(struct bch_fs *, struct bkey_s_c_accounting, bool);
+void bch2_accounting_mem_gc(struct bch_fs *);
 
 static inline int __bch2_accounting_mem_mod(struct bch_fs *c, struct bkey_s_c_accounting a, bool gc)
 {
index 06f6d48f74c0ab41e63890a85fe4e09a4e32d8ce..10c96cb2047ae1e71a9d3a36ad9b13e6858f5361 100644 (file)
@@ -420,10 +420,10 @@ int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
 int bch2_replicas_gc2(struct bch_fs *c)
 {
        struct bch_replicas_cpu new = { 0 };
-       unsigned i, nr;
+       unsigned nr;
        int ret = 0;
 
-       bch2_journal_meta(&c->journal);
+       bch2_accounting_mem_gc(c);
 retry:
        nr              = READ_ONCE(c->replicas.nr);
        new.entry_size  = READ_ONCE(c->replicas.entry_size);
@@ -444,7 +444,7 @@ retry:
                goto retry;
        }
 
-       for (i = 0; i < c->replicas.nr; i++) {
+       for (unsigned i = 0; i < c->replicas.nr; i++) {
                struct bch_replicas_entry_v1 *e =
                        cpu_replicas_entry(&c->replicas, i);
 
@@ -454,10 +454,13 @@ retry:
 
                memcpy(&k.replicas, e, replicas_entry_bytes(e));
 
-               u64 v = 0;
-               bch2_accounting_mem_read(c, disk_accounting_pos_to_bpos(&k), &v, 1);
+               struct bpos p = disk_accounting_pos_to_bpos(&k);
 
-               if (e->data_type == BCH_DATA_journal || v)
+               struct bch_accounting_mem *acc = &c->accounting;
+               bool kill = eytzinger0_find(acc->k.data, acc->k.nr, sizeof(acc->k.data[0]),
+                                           accounting_pos_cmp, &p) >= acc->k.nr;
+
+               if (e->data_type == BCH_DATA_journal || !kill)
                        memcpy(cpu_replicas_entry(&new, new.nr++),
                               e, new.entry_size);
        }