[PATCH 11/21] bcachefs: bkey_should_have_rb_opts()
Kent Overstreet <[email protected]> Sun, 24 Aug 2025 08:37:48 -0400
| Newsgroups | org.kernel.vger.linux-bcachefs |
|---|---|
| Message-ID | <[email protected]> |
Factor out a small helper. Signed-off-by: Kent Overstreet <[email protected]> --- fs/bcachefs/rebalance.c | 43 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/fs/bcachefs/rebalance.c b/fs/bcachefs/rebalance.c index 932998864ad2..9e22ff0e2d28 100644 --- a/fs/bcachefs/rebalance.c +++ b/fs/bcachefs/rebalance.c @@ -145,20 +145,11 @@ u64 bch2_bkey_sectors_need_rebalance(struct bch_fs *c, struct bkey_s_c k) return sectors; } -static bool bch2_bkey_rebalance_needs_update(struct bch_fs *c, struct bch_inode_opts *opts, - struct bkey_s_c k) +static inline bool bkey_should_have_rb_opts(struct bch_fs *c, + struct bch_inode_opts *opts, + struct bkey_s_c k) { - if (!bkey_extent_is_direct_data(k.k)) - return 0; - - const struct bch_extent_rebalance *old = bch2_bkey_rebalance_opts(k); - - if (k.k->type == KEY_TYPE_reflink_v || bch2_bkey_ptrs_need_rebalance(c, opts, k)) { - struct bch_extent_rebalance new = io_opts_to_rebalance_opts(c, opts); - return old == NULL || memcmp(old, &new, sizeof(new)); - } else { - return old != NULL; - } + return k.k->type == KEY_TYPE_reflink_v || bch2_bkey_ptrs_need_rebalance(c, opts, k); } int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bch_inode_opts *opts, @@ -173,7 +164,7 @@ int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bch_inode_opts *opts, struct bch_extent_rebalance *old = (struct bch_extent_rebalance *) bch2_bkey_rebalance_opts(k.s_c); - if (k.k->type == KEY_TYPE_reflink_v || bch2_bkey_ptrs_need_rebalance(c, opts, k.s_c)) { + if (bkey_should_have_rb_opts(c, opts, k.s_c)) { if (!old) { old = bkey_val_end(k); k.k->u64s += sizeof(*old) / sizeof(u64); @@ -194,9 +185,14 @@ static int bch2_get_update_rebalance_opts(struct btree_trans *trans, struct bkey_s_c k, enum set_needs_rebalance_ctx ctx) { + struct bch_fs *c = trans->c; + BUG_ON(iter->flags & BTREE_ITER_is_extents); BUG_ON(iter->flags & BTREE_ITER_filter_snapshots); + if (!bkey_extent_is_direct_data(k.k)) + return 0; + bool may_update_indirect = ctx == SET_NEEDS_REBALANCE_opt_change_indirect; /* @@ -206,20 +202,23 @@ static int bch2_get_update_rebalance_opts(struct btree_trans *trans, * the inode and may_update_indirect is true (walked from a * REFLINK_P_MAY_UPDATE_OPTIONS pointer). */ - const struct bch_extent_rebalance *r = k.k->type == KEY_TYPE_reflink_v - ? bch2_bkey_rebalance_opts(k) : NULL; - if (r) { + const struct bch_extent_rebalance *old = bch2_bkey_rebalance_opts(k); + if (old && k.k->type == KEY_TYPE_reflink_v) { #define x(_name) \ - if (r->_name##_from_inode && \ + if (old->_name##_from_inode && \ !(may_update_indirect && io_opts->_name##_from_inode)) { \ - io_opts->_name = r->_name; \ + io_opts->_name = old->_name; \ io_opts->_name##_from_inode = true; \ } BCH_REBALANCE_OPTS() #undef x } - if (!bch2_bkey_rebalance_needs_update(trans->c, io_opts, k)) + struct bch_extent_rebalance new = io_opts_to_rebalance_opts(c, io_opts); + + if (bkey_should_have_rb_opts(c, io_opts, k) + ? old && !memcmp(old, &new, sizeof(new)) + : !old) return 0; struct bkey_i *n = bch2_trans_kmalloc(trans, bkey_bytes(k.k) + 8); @@ -231,10 +230,10 @@ static int bch2_get_update_rebalance_opts(struct btree_trans *trans, /* On successfull transaction commit, @k was invalidated: */ - return bch2_bkey_set_needs_rebalance(trans->c, io_opts, n, ctx, 0) ?: + return bch2_bkey_set_needs_rebalance(c, io_opts, n, ctx, 0) ?: bch2_trans_update(trans, iter, n, BTREE_UPDATE_internal_snapshot_node) ?: bch2_trans_commit(trans, NULL, NULL, 0) ?: - bch_err_throw(trans->c, transaction_restart_commit); + bch_err_throw(c, transaction_restart_commit); } static struct bch_inode_opts *bch2_extent_get_io_opts(struct btree_trans *trans, -- 2.50.1