From 27616a31241e0625ee063f2cacd8c8e339b2de65 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Sat, 18 Feb 2023 22:11:50 -0500 Subject: [PATCH] bcachefs: Simplify ec stripes heap Now that we have a separate data structure for tracking open stripes, the stripes heap can track all existing stripes, which is a nice simplification. Signed-off-by: Kent Overstreet --- fs/bcachefs/buckets.c | 12 ++++----- fs/bcachefs/ec.c | 57 ++++++++++-------------------------------- fs/bcachefs/ec.h | 2 -- fs/bcachefs/ec_types.h | 5 ---- fs/bcachefs/recovery.c | 2 -- 5 files changed, 18 insertions(+), 60 deletions(-) diff --git a/fs/bcachefs/buckets.c b/fs/bcachefs/buckets.c index ddbf88a759f9..7c4780b3ceb5 100644 --- a/fs/bcachefs/buckets.c +++ b/fs/bcachefs/buckets.c @@ -1031,7 +1031,7 @@ int bch2_mark_stripe(struct btree_trans *trans, if (!gc) { struct stripe *m = genradix_ptr(&c->stripes, idx); - if (!m || (old_s && !m->alive)) { + if (!m) { struct printbuf buf1 = PRINTBUF; struct printbuf buf2 = PRINTBUF; @@ -1047,13 +1047,10 @@ int bch2_mark_stripe(struct btree_trans *trans, } if (!new_s) { - mutex_lock(&c->ec_stripes_heap_lock); bch2_stripes_heap_del(c, m, idx); - mutex_unlock(&c->ec_stripes_heap_lock); memset(m, 0, sizeof(*m)); } else { - m->alive = true; m->sectors = le16_to_cpu(new_s->sectors); m->algorithm = new_s->algorithm; m->nr_blocks = new_s->nr_blocks; @@ -1063,9 +1060,10 @@ int bch2_mark_stripe(struct btree_trans *trans, for (i = 0; i < new_s->nr_blocks; i++) m->blocks_nonempty += !!stripe_blockcount_get(new_s, i); - mutex_lock(&c->ec_stripes_heap_lock); - bch2_stripes_heap_update(c, m, idx); - mutex_unlock(&c->ec_stripes_heap_lock); + if (!old_s) + bch2_stripes_heap_insert(c, m, idx); + else + bch2_stripes_heap_update(c, m, idx); } } else { struct gc_stripe *m = diff --git a/fs/bcachefs/ec.c b/fs/bcachefs/ec.c index eb8ce55e6fd4..924494868102 100644 --- a/fs/bcachefs/ec.c +++ b/fs/bcachefs/ec.c @@ -680,7 +680,6 @@ static void heap_verify_backpointer(struct bch_fs *c, size_t idx) ec_stripes_heap *h = &c->ec_stripes_heap; struct stripe *m = genradix_ptr(&c->stripes, idx); - BUG_ON(!m->alive); BUG_ON(m->heap_idx >= h->used); BUG_ON(h->data[m->heap_idx].idx != idx); } @@ -688,28 +687,21 @@ static void heap_verify_backpointer(struct bch_fs *c, size_t idx) void bch2_stripes_heap_del(struct bch_fs *c, struct stripe *m, size_t idx) { - if (!m->on_heap) - return; - - m->on_heap = false; - + mutex_lock(&c->ec_stripes_heap_lock); heap_verify_backpointer(c, idx); heap_del(&c->ec_stripes_heap, m->heap_idx, ec_stripes_heap_cmp, ec_stripes_heap_set_backpointer); + mutex_unlock(&c->ec_stripes_heap_lock); } void bch2_stripes_heap_insert(struct bch_fs *c, struct stripe *m, size_t idx) { - if (m->on_heap) - return; - + mutex_lock(&c->ec_stripes_heap_lock); BUG_ON(heap_full(&c->ec_stripes_heap)); - m->on_heap = true; - heap_add(&c->ec_stripes_heap, ((struct ec_stripe_heap_entry) { .idx = idx, .blocks_nonempty = m->blocks_nonempty, @@ -718,17 +710,17 @@ void bch2_stripes_heap_insert(struct bch_fs *c, ec_stripes_heap_set_backpointer); heap_verify_backpointer(c, idx); + mutex_unlock(&c->ec_stripes_heap_lock); } void bch2_stripes_heap_update(struct bch_fs *c, struct stripe *m, size_t idx) { ec_stripes_heap *h = &c->ec_stripes_heap; + bool do_deletes; size_t i; - if (!m->on_heap) - return; - + mutex_lock(&c->ec_stripes_heap_lock); heap_verify_backpointer(c, idx); h->data[m->heap_idx].blocks_nonempty = m->blocks_nonempty; @@ -741,7 +733,10 @@ void bch2_stripes_heap_update(struct bch_fs *c, heap_verify_backpointer(c, idx); - if (stripe_idx_to_delete(c)) + do_deletes = stripe_idx_to_delete(c) != 0; + mutex_unlock(&c->ec_stripes_heap_lock); + + if (do_deletes) bch2_do_stripe_deletes(c); } @@ -799,8 +794,6 @@ static void ec_stripe_delete_work(struct work_struct *work) while (1) { mutex_lock(&c->ec_stripes_heap_lock); idx = stripe_idx_to_delete(c); - if (idx) - bch2_stripes_heap_del(c, genradix_ptr(&c->stripes, idx), idx); mutex_unlock(&c->ec_stripes_heap_lock); if (!idx) @@ -1013,7 +1006,6 @@ static void ec_stripe_create(struct ec_stripe_new *s) { struct bch_fs *c = s->c; struct open_bucket *ob; - struct stripe *m; struct bch_stripe *v = &s->new_stripe.key.v; unsigned i, nr_data = v->nr_blocks - v->nr_redundant; int ret; @@ -1076,13 +1068,6 @@ static void ec_stripe_create(struct ec_stripe_new *s) } bch2_stripe_close(c, s); - - mutex_lock(&c->ec_stripes_heap_lock); - m = genradix_ptr(&c->stripes, s->new_stripe.key.k.p.offset); - - BUG_ON(m->on_heap); - bch2_stripes_heap_insert(c, m, s->new_stripe.key.k.p.offset); - mutex_unlock(&c->ec_stripes_heap_lock); err: bch2_disk_reservation_put(c, &s->res); @@ -1491,11 +1476,8 @@ static s64 get_existing_stripe(struct bch_fs *c, if (m->algorithm == head->algo && m->nr_redundant == head->redundancy && m->sectors == head->blocksize && - m->blocks_nonempty < m->nr_blocks - m->nr_redundant) { - if (!bch2_try_open_stripe(c, head->s, stripe_idx)) - continue; - - bch2_stripes_heap_del(c, m, stripe_idx); + m->blocks_nonempty < m->nr_blocks - m->nr_redundant && + bch2_try_open_stripe(c, head->s, stripe_idx)) { ret = stripe_idx; break; } @@ -1696,16 +1678,6 @@ unlock: mutex_unlock(&c->ec_stripe_head_lock); } -void bch2_stripes_heap_start(struct bch_fs *c) -{ - struct genradix_iter iter; - struct stripe *m; - - genradix_for_each(&c->stripes, iter, m) - if (m->alive) - bch2_stripes_heap_insert(c, m, iter.pos); -} - int bch2_stripes_read(struct bch_fs *c) { struct btree_trans trans; @@ -1730,7 +1702,6 @@ int bch2_stripes_read(struct bch_fs *c) s = bkey_s_c_to_stripe(k).v; m = genradix_ptr(&c->stripes, k.k->p.offset); - m->alive = true; m->sectors = le16_to_cpu(s->sectors); m->algorithm = s->algorithm; m->nr_blocks = s->nr_blocks; @@ -1740,9 +1711,7 @@ int bch2_stripes_read(struct bch_fs *c) for (i = 0; i < s->nr_blocks; i++) m->blocks_nonempty += !!stripe_blockcount_get(s, i); - mutex_lock(&c->ec_stripes_heap_lock); - bch2_stripes_heap_update(c, m, k.k->p.offset); - mutex_unlock(&c->ec_stripes_heap_lock); + bch2_stripes_heap_insert(c, m, k.k->p.offset); } bch2_trans_iter_exit(&trans, &iter); diff --git a/fs/bcachefs/ec.h b/fs/bcachefs/ec.h index 0a69114bb160..c9e4fb214649 100644 --- a/fs/bcachefs/ec.h +++ b/fs/bcachefs/ec.h @@ -217,8 +217,6 @@ void bch2_ec_stop_dev(struct bch_fs *, struct bch_dev *); void bch2_ec_flush_new_stripes(struct bch_fs *); -void bch2_stripes_heap_start(struct bch_fs *); - int bch2_stripes_read(struct bch_fs *); void bch2_stripes_heap_to_text(struct printbuf *, struct bch_fs *); diff --git a/fs/bcachefs/ec_types.h b/fs/bcachefs/ec_types.h index 2bf26d254b2c..e2b02a82de32 100644 --- a/fs/bcachefs/ec_types.h +++ b/fs/bcachefs/ec_types.h @@ -11,15 +11,10 @@ struct bch_replicas_padded { struct stripe { size_t heap_idx; - u16 sectors; u8 algorithm; - u8 nr_blocks; u8 nr_redundant; - - unsigned alive:1; /* does a corresponding key exist in stripes btree? */ - unsigned on_heap:1; u8 blocks_nonempty; }; diff --git a/fs/bcachefs/recovery.c b/fs/bcachefs/recovery.c index 1976d5fa3427..f5946b4dbce2 100644 --- a/fs/bcachefs/recovery.c +++ b/fs/bcachefs/recovery.c @@ -1260,8 +1260,6 @@ use_clean: goto err; bch_verbose(c, "stripes_read done"); - bch2_stripes_heap_start(c); - if (c->sb.version < bcachefs_metadata_version_snapshot_2) { err = "error creating root snapshot node"; ret = bch2_fs_initialize_subvolumes(c);