bcachefs: bch2_bkey_make_mut() now calls bch2_trans_update()
authorKent Overstreet <kent.overstreet@linux.dev>
Sun, 30 Apr 2023 23:21:06 +0000 (19:21 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:10:01 +0000 (17:10 -0400)
It's safe to call bch2_trans_update with a k/v pair where the value
hasn't been filled out, as long as the key part has been and the value
is filled out by transaction commit time.

This patch folds the bch2_trans_update() call into bch2_bkey_make_mut(),
eliminating a bit of boilerplate.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/alloc_background.c
fs/bcachefs/btree_gc.c
fs/bcachefs/btree_update.h
fs/bcachefs/btree_update_leaf.c
fs/bcachefs/fsck.c
fs/bcachefs/io.c
fs/bcachefs/migrate.c
fs/bcachefs/move.c

index 7b6225fe3443a7c2813c3f05afa11cd2949758ac..dcdef3bcd4c49159fb1962601acb1e5bb9d495c4 100644 (file)
@@ -512,7 +512,7 @@ static inline struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut_inlined(struct btree_
        if (likely(k.k->type == KEY_TYPE_alloc_v4) &&
            ((a = bkey_s_c_to_alloc_v4(k), true) &&
             BCH_ALLOC_V4_NR_BACKPOINTERS(a.v) == 0))
-               return bch2_bkey_make_mut_typed(trans, k, alloc_v4);
+               return bch2_bkey_make_mut_noupdate_typed(trans, k, alloc_v4);
 
        return __bch2_alloc_to_v4_mut(trans, k);
 }
index eedcc09bacffa211825bdb24406a9c569a17c49a..8477e721b63c5ec34ac7c444601e72596e103836 100644 (file)
@@ -1591,7 +1591,7 @@ static int bch2_gc_write_reflink_key(struct btree_trans *trans,
                        "  should be %u",
                        (bch2_bkey_val_to_text(&buf, c, k), buf.buf),
                        r->refcount)) {
-               struct bkey_i *new = bch2_bkey_make_mut(trans, k);
+               struct bkey_i *new = bch2_bkey_make_mut(trans, iter, k, 0);
 
                ret = PTR_ERR_OR_ZERO(new);
                if (ret)
@@ -1601,8 +1601,6 @@ static int bch2_gc_write_reflink_key(struct btree_trans *trans,
                        new->k.type = KEY_TYPE_deleted;
                else
                        *bkey_refcount(new) = cpu_to_le64(r->refcount);
-
-               ret = bch2_trans_update(trans, iter, new, 0);
        }
 fsck_err:
        printbuf_exit(&buf);
@@ -1918,13 +1916,13 @@ static int gc_btree_gens_key(struct btree_trans *trans,
        percpu_up_read(&c->mark_lock);
        return 0;
 update:
-       u = bch2_bkey_make_mut(trans, k);
+       u = bch2_bkey_make_mut(trans, iter, k, 0);
        ret = PTR_ERR_OR_ZERO(u);
        if (ret)
                return ret;
 
        bch2_extent_normalize(c, bkey_i_to_s(u));
-       return bch2_trans_update(trans, iter, u, 0);
+       return 0;
 }
 
 static int bch2_alloc_write_oldest_gen(struct btree_trans *trans, struct btree_iter *iter,
index d823334033f99488ab4a93b828e36c7dd5e1e9a6..34ca2c43a8cac2e107a6b13aa417898505f88ab6 100644 (file)
@@ -183,7 +183,7 @@ static inline void bch2_trans_reset_updates(struct btree_trans *trans)
        }
 }
 
-static inline struct bkey_i *__bch2_bkey_make_mut(struct btree_trans *trans, struct bkey_s_c k,
+static inline struct bkey_i *__bch2_bkey_make_mut_noupdate(struct btree_trans *trans, struct bkey_s_c k,
                                                  unsigned type, unsigned min_bytes)
 {
        unsigned bytes = max_t(unsigned, min_bytes, bkey_bytes(k.k));
@@ -205,13 +205,39 @@ static inline struct bkey_i *__bch2_bkey_make_mut(struct btree_trans *trans, str
        return mut;
 }
 
-static inline struct bkey_i *bch2_bkey_make_mut(struct btree_trans *trans, struct bkey_s_c k)
+static inline struct bkey_i *bch2_bkey_make_mut_noupdate(struct btree_trans *trans, struct bkey_s_c k)
 {
-       return __bch2_bkey_make_mut(trans, k, 0, 0);
+       return __bch2_bkey_make_mut_noupdate(trans, k, 0, 0);
 }
 
-#define bch2_bkey_make_mut_typed(_trans, _k, _type)                    \
-       bkey_i_to_##_type(__bch2_bkey_make_mut(_trans, _k,              \
+#define bch2_bkey_make_mut_noupdate_typed(_trans, _k, _type)           \
+       bkey_i_to_##_type(__bch2_bkey_make_mut_noupdate(_trans, _k,     \
+                               KEY_TYPE_##_type, sizeof(struct bkey_i_##_type)))
+
+static inline struct bkey_i *__bch2_bkey_make_mut(struct btree_trans *trans, struct btree_iter *iter,
+                                       struct bkey_s_c k, unsigned flags,
+                                       unsigned type, unsigned min_bytes)
+{
+       struct bkey_i *mut = __bch2_bkey_make_mut_noupdate(trans, k, type, min_bytes);
+       int ret;
+
+       if (IS_ERR(mut))
+               return mut;
+
+       ret = bch2_trans_update(trans, iter, mut, flags);
+       if (ret)
+               return ERR_PTR(ret);
+       return mut;
+}
+
+static inline struct bkey_i *bch2_bkey_make_mut(struct btree_trans *trans, struct btree_iter *iter,
+                                               struct bkey_s_c k, unsigned flags)
+{
+       return __bch2_bkey_make_mut(trans, iter, k, flags, 0, 0);
+}
+
+#define bch2_bkey_make_mut_typed(_trans, _iter, _k, _flags, _type)     \
+       bkey_i_to_##_type(__bch2_bkey_make_mut(_trans, _iter, _k, _flags,\
                                KEY_TYPE_##_type, sizeof(struct bkey_i_##_type)))
 
 static inline struct bkey_i *__bch2_bkey_get_mut_noupdate(struct btree_trans *trans,
@@ -223,7 +249,7 @@ static inline struct bkey_i *__bch2_bkey_get_mut_noupdate(struct btree_trans *tr
                                btree_id, pos, flags|BTREE_ITER_INTENT, type);
        struct bkey_i *ret = unlikely(IS_ERR(k.k))
                ? ERR_CAST(k.k)
-               : __bch2_bkey_make_mut(trans, k, 0, min_bytes);
+               : __bch2_bkey_make_mut_noupdate(trans, k, 0, min_bytes);
        if (unlikely(IS_ERR(ret)))
                bch2_trans_iter_exit(trans, iter);
        return ret;
index c511541bb5f484c86bd2777e51ba32b18647dfaa..0952885f3caab05459136bbd5e118a1e73f78c7c 100644 (file)
@@ -1268,7 +1268,7 @@ static noinline int extent_front_merge(struct btree_trans *trans,
        struct bkey_i *update;
        int ret;
 
-       update = bch2_bkey_make_mut(trans, k);
+       update = bch2_bkey_make_mut_noupdate(trans, k);
        ret = PTR_ERR_OR_ZERO(update);
        if (ret)
                return ret;
@@ -1390,7 +1390,7 @@ int bch2_trans_update_extent(struct btree_trans *trans,
                        trans->extra_journal_res += compressed_sectors;
 
                if (front_split) {
-                       update = bch2_bkey_make_mut(trans, k);
+                       update = bch2_bkey_make_mut_noupdate(trans, k);
                        if ((ret = PTR_ERR_OR_ZERO(update)))
                                goto err;
 
@@ -1404,7 +1404,7 @@ int bch2_trans_update_extent(struct btree_trans *trans,
 
                if (k.k->p.snapshot != insert->k.p.snapshot &&
                    (front_split || back_split)) {
-                       update = bch2_bkey_make_mut(trans, k);
+                       update = bch2_bkey_make_mut_noupdate(trans, k);
                        if ((ret = PTR_ERR_OR_ZERO(update)))
                                goto err;
 
@@ -1443,7 +1443,7 @@ int bch2_trans_update_extent(struct btree_trans *trans,
                }
 
                if (back_split) {
-                       update = bch2_bkey_make_mut(trans, k);
+                       update = bch2_bkey_make_mut_noupdate(trans, k);
                        if ((ret = PTR_ERR_OR_ZERO(update)))
                                goto err;
 
index 142e64922d8f4f3259761780f7d80e9baa269a9b..4b28fc4f77c60c0a552d63f5601e920c3559b4f3 100644 (file)
@@ -765,7 +765,7 @@ static int hash_redo_key(struct btree_trans *trans,
        if (IS_ERR(delete))
                return PTR_ERR(delete);
 
-       tmp = bch2_bkey_make_mut(trans, k);
+       tmp = bch2_bkey_make_mut_noupdate(trans, k);
        if (IS_ERR(tmp))
                return PTR_ERR(tmp);
 
index 46dc166d23d54d5935a9c218424494881f278242..11ed86453d669a8799e03df303f359691a89d1ce 100644 (file)
@@ -1393,7 +1393,7 @@ static int bch2_nocow_write_convert_one_unwritten(struct btree_trans *trans,
                return 0;
        }
 
-       new = bch2_bkey_make_mut(trans, k);
+       new = bch2_bkey_make_mut_noupdate(trans, k);
        ret = PTR_ERR_OR_ZERO(new);
        if (ret)
                return ret;
index d93db07f0c8781d9ad35afeeeebb10938e94428d..0898fa49b3cd360b04e5a1ee552c954221c23e3a 100644 (file)
@@ -49,7 +49,7 @@ static int bch2_dev_usrdata_drop_key(struct btree_trans *trans,
        if (!bch2_bkey_has_device_c(k, dev_idx))
                return 0;
 
-       n = bch2_bkey_make_mut(trans, k);
+       n = bch2_bkey_make_mut(trans, iter, k, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
        ret = PTR_ERR_OR_ZERO(n);
        if (ret)
                return ret;
@@ -73,8 +73,7 @@ static int bch2_dev_usrdata_drop_key(struct btree_trans *trans,
         */
        if (bkey_deleted(&n->k))
                n->k.size = 0;
-
-       return bch2_trans_update(trans, iter, n, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
+       return 0;
 }
 
 static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
index 642c076216ea1fb5759f3b1df79da80dad389724..7e22176a5c7e5b4351b5b18227e70f1122979355 100644 (file)
@@ -251,7 +251,7 @@ static int bch2_extent_drop_ptrs(struct btree_trans *trans,
        struct bkey_i *n;
        int ret;
 
-       n = bch2_bkey_make_mut(trans, k);
+       n = bch2_bkey_make_mut_noupdate(trans, k);
        ret = PTR_ERR_OR_ZERO(n);
        if (ret)
                return ret;