]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
bcachefs: io_opts_to_rebalance_opts()
authorKent Overstreet <kent.overstreet@linux.dev>
Sun, 20 Oct 2024 06:28:51 +0000 (02:28 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sat, 21 Dec 2024 06:36:16 +0000 (01:36 -0500)
New helper to simplify bch2_bkey_set_needs_rebalance()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/data_update.c
fs/bcachefs/extents.c
fs/bcachefs/extents.h
fs/bcachefs/extents_format.h
fs/bcachefs/io_misc.c
fs/bcachefs/io_write.c
fs/bcachefs/opts.h
fs/bcachefs/reflink.c

index 90da57a2696249fe3621c3041be4bef5b86beb50..e4af2ccdf4c8e6dd628282bea0e16121786d9a65 100644 (file)
@@ -357,7 +357,7 @@ restart_drop_extra_replicas:
                                                k.k->p, bkey_start_pos(&insert->k)) ?:
                        bch2_insert_snapshot_whiteouts(trans, m->btree_id,
                                                k.k->p, insert->k.p) ?:
-                       bch2_bkey_set_needs_rebalance(c, insert, &op->opts) ?:
+                       bch2_bkey_set_needs_rebalance(c, &op->opts, insert) ?:
                        bch2_trans_update(trans, &iter, insert,
                                BTREE_UPDATE_internal_snapshot_node) ?:
                        bch2_trans_commit(trans, &op->res,
index ee0e86f0becda346e770861562c3fc5e4f35949b..a134aa5a76bb1b430139b815228a359c050a4e42 100644 (file)
@@ -1478,55 +1478,27 @@ incompressible:
        return sectors;
 }
 
-int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bkey_i *_k,
-                                 struct bch_io_opts *opts)
+int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bch_io_opts *opts,
+                                 struct bkey_i *_k)
 {
-       struct bkey_s k = bkey_i_to_s(_k);
-       struct bch_extent_rebalance *r;
-       unsigned target = opts->background_target;
-       unsigned compression = opts->background_compression;
-       bool needs_rebalance;
-
-       if (!bkey_extent_is_direct_data(k.k))
+       if (!bkey_extent_is_direct_data(&_k->k))
                return 0;
 
-       /* get existing rebalance entry: */
-       r = (struct bch_extent_rebalance *) bch2_bkey_rebalance_opts(k.s_c);
-       if (r) {
-               if (k.k->type == KEY_TYPE_reflink_v) {
-                       /*
-                        * indirect extents: existing options take precedence,
-                        * so that we don't move extents back and forth if
-                        * they're referenced by different inodes with different
-                        * options:
-                        */
-                       if (r->background_target)
-                               target = r->background_target;
-                       if (r->background_compression)
-                               compression = r->background_compression;
+       struct bkey_s k = bkey_i_to_s(_k);
+       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, k.s_c, opts->background_target, opts->background_compression)) {
+               if (!old) {
+                       old = bkey_val_end(k);
+                       k.k->u64s += sizeof(*old) / sizeof(u64);
                }
 
-               r->background_target            = target;
-               r->background_compression       = compression;
-       }
-
-       needs_rebalance = bch2_bkey_ptrs_need_rebalance(c, k.s_c, target, compression);
-
-       if (needs_rebalance && !r) {
-               union bch_extent_entry *new = bkey_val_end(k);
-
-               new->rebalance.type                     = 1U << BCH_EXTENT_ENTRY_rebalance;
-               new->rebalance.background_compression   = compression;
-               new->rebalance.background_target        = target;
-               new->rebalance.unused                   = 0;
-               k.k->u64s += extent_entry_u64s(new);
-       } else if (!needs_rebalance && r && k.k->type != KEY_TYPE_reflink_v) {
-               /*
-                * For indirect extents, don't delete the rebalance entry when
-                * we're finished so that we know we specifically moved it or
-                * compressed it to its current location/compression type
-                */
-               extent_entry_drop(k, (union bch_extent_entry *) r);
+               *old = io_opts_to_rebalance_opts(opts);
+       } else {
+               if (old)
+                       extent_entry_drop(k, (union bch_extent_entry *) old);
        }
 
        return 0;
index 9374599b384d555b81de0ce71e917e5fe64b884e..97af0d6e431911e602b000ddb039f72bd5131e5c 100644 (file)
@@ -715,8 +715,7 @@ unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *, struct bkey_s_c,
                                       unsigned, unsigned);
 u64 bch2_bkey_sectors_need_rebalance(struct bch_fs *, struct bkey_s_c);
 
-int bch2_bkey_set_needs_rebalance(struct bch_fs *, struct bkey_i *,
-                                 struct bch_io_opts *);
+int bch2_bkey_set_needs_rebalance(struct bch_fs *, struct bch_io_opts *, struct bkey_i *);
 
 /* Generic extent code: */
 
index 2cc3e60f3b127233270237c22f405e768da50d43..520697f236c03db8f66e428069d03bf2785e96bd 100644 (file)
@@ -215,6 +215,11 @@ struct bch_extent_rebalance {
 #endif
 };
 
+/* subset of BCH_INODE_OPTS */
+#define BCH_REBALANCE_OPTS()                   \
+       x(background_compression)               \
+       x(background_target)
+
 union bch_extent_entry {
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ||  __BITS_PER_LONG == 64
        unsigned long                   type;
index f283051758d6d5fb1e6ecc9bb4acb9e6bf1a804d..e2acf21ac9b0fb21d486ff6680470c92b7f39509 100644 (file)
@@ -461,7 +461,7 @@ case LOGGED_OP_FINSERT_shift_extents:
 
                op->v.pos = cpu_to_le64(insert ? bkey_start_offset(&delete.k) : delete.k.p.offset);
 
-               ret =   bch2_bkey_set_needs_rebalance(c, copy, &opts) ?:
+               ret =   bch2_bkey_set_needs_rebalance(c, &opts, copy) ?:
                        bch2_btree_insert_trans(trans, BTREE_ID_extents, &delete, 0) ?:
                        bch2_btree_insert_trans(trans, BTREE_ID_extents, copy, 0) ?:
                        bch2_logged_op_update(trans, &op->k_i) ?:
index 96720adcfee033567a0ddd15f6eeb9804d44226c..f2f69e5e0910c7495ee59072746b12df81a5549e 100644 (file)
@@ -369,7 +369,7 @@ static int bch2_write_index_default(struct bch_write_op *op)
                                     bkey_start_pos(&sk.k->k),
                                     BTREE_ITER_slots|BTREE_ITER_intent);
 
-               ret =   bch2_bkey_set_needs_rebalance(c, sk.k, &op->opts) ?:
+               ret =   bch2_bkey_set_needs_rebalance(c, &op->opts, sk.k) ?:
                        bch2_extent_update(trans, inum, &iter, sk.k,
                                        &op->res,
                                        op->new_i_size, &op->i_sectors_delta,
index dd27ef5566117adf51ae507823cfa32949507870..13555bc35f00c16f2140c0d6af74b1f61ad94dab 100644 (file)
@@ -642,4 +642,17 @@ static inline void bch2_io_opts_fixups(struct bch_io_opts *opts)
 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts);
 bool bch2_opt_is_inode_opt(enum bch_opt_id);
 
+/* rebalance opts: */
+
+static inline struct bch_extent_rebalance io_opts_to_rebalance_opts(struct bch_io_opts *opts)
+{
+       return (struct bch_extent_rebalance) {
+               .type = BIT(BCH_EXTENT_ENTRY_rebalance),
+#define x(_name)                                                       \
+               ._name = opts->_name,
+               BCH_REBALANCE_OPTS()
+#undef x
+       };
+};
+
 #endif /* _BCACHEFS_OPTS_H */
index f457925fa362ef4b1728dccfee99d17788c811b5..8a36ebd9dd9cf766b48edd6e83aed987bebfe7dc 100644 (file)
@@ -547,7 +547,7 @@ s64 bch2_remap_range(struct bch_fs *c,
                                min(src_k.k->p.offset - src_want.offset,
                                    dst_end.offset - dst_iter.pos.offset));
 
-               ret =   bch2_bkey_set_needs_rebalance(c, new_dst.k, &opts) ?:
+               ret =   bch2_bkey_set_needs_rebalance(c, &opts, new_dst.k) ?:
                        bch2_extent_update(trans, dst_inum, &dst_iter,
                                        new_dst.k, &disk_res,
                                        new_i_size, i_sectors_delta,