]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
bcachefs: Refactor bch2_btree_bset_insert_key()
authorKent Overstreet <kent.overstreet@gmail.com>
Tue, 7 Jan 2020 04:43:04 +0000 (23:43 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:08:34 +0000 (17:08 -0400)
The main thing going on is to separate out the different cases deletion,
overwriting, and inserting a new key.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/btree_update_leaf.c

index 3dc6b35b2e6ac63b9bf59d405794074dfd7aaa67..0c6c2b5b15b39527739b856dd1daa650bebff3e3 100644 (file)
@@ -64,64 +64,63 @@ bool bch2_btree_bset_insert_key(struct btree_iter *iter,
                bkey_cmp(insert->k.p, b->data->max_key) > 0);
 
        k = bch2_btree_node_iter_peek_all(node_iter, b);
-       if (k && !bkey_cmp_packed(b, k, &insert->k)) {
-               BUG_ON(bkey_whiteout(k));
+       if (k && bkey_cmp_packed(b, k, &insert->k))
+               k = NULL;
 
-               if (!bkey_written(b, k) &&
-                   bkey_val_u64s(&insert->k) == bkeyp_val_u64s(f, k) &&
-                   !bkey_whiteout(&insert->k)) {
-                       k->type = insert->k.type;
-                       memcpy_u64s(bkeyp_val(f, k), &insert->v,
-                                   bkey_val_u64s(&insert->k));
-                       return true;
-               }
+       /* @k is the key being overwritten/deleted, if any: */
 
-               btree_account_key_drop(b, k);
+       EBUG_ON(k && bkey_whiteout(k));
 
-               if (bkey_whiteout(&insert->k)) {
-                       unsigned clobber_u64s = k->u64s, new_u64s = k->u64s;
+       if (bkey_whiteout(&insert->k)) {
+               /* Deleting: */
 
-                       k->type = KEY_TYPE_deleted;
+               /* Not found? Nothing to do: */
+               if (!k)
+                       return false;
 
-                       if (k->needs_whiteout) {
-                               push_whiteout(iter->trans->c, b, k);
-                               k->needs_whiteout = false;
-                       }
+               btree_account_key_drop(b, k);
+               k->type = KEY_TYPE_deleted;
 
-                       if (k >= btree_bset_last(b)->start) {
-                               bch2_bset_delete(b, k, clobber_u64s);
-                               new_u64s = 0;
-                       }
+               if (k->needs_whiteout) {
+                       push_whiteout(iter->trans->c, b, k);
+                       k->needs_whiteout = false;
+               }
 
+               if (k >= btree_bset_last(b)->start) {
+                       clobber_u64s = k->u64s;
+
+                       bch2_bset_delete(b, k, clobber_u64s);
                        bch2_btree_node_iter_fix(iter, b, node_iter, k,
-                                                clobber_u64s, new_u64s);
-                       return true;
+                                                clobber_u64s, 0);
+               } else {
+                       bch2_btree_iter_fix_key_modified(iter, b, k);
+               }
 
+               return true;
+       }
+
+       if (k) {
+               /* Overwriting: */
+               if (!bkey_written(b, k) &&
+                   bkey_val_u64s(&insert->k) == bkeyp_val_u64s(f, k)) {
+                       k->type = insert->k.type;
+                       memcpy_u64s(bkeyp_val(f, k), &insert->v,
+                                   bkey_val_u64s(&insert->k));
+                       return true;
                }
 
+               btree_account_key_drop(b, k);
+               k->type = KEY_TYPE_deleted;
+
                insert->k.needs_whiteout = k->needs_whiteout;
                k->needs_whiteout = false;
 
                if (k >= btree_bset_last(b)->start) {
                        clobber_u64s = k->u64s;
                        goto overwrite;
+               } else {
+                       bch2_btree_iter_fix_key_modified(iter, b, k);
                }
-
-               k->type = KEY_TYPE_deleted;
-               /*
-                * XXX: we should be able to do this without two calls to
-                * bch2_btree_node_iter_fix:
-                */
-               bch2_btree_node_iter_fix(iter, b, node_iter, k,
-                                        k->u64s, k->u64s);
-       } else {
-               /*
-                * Deleting, but the key to delete wasn't found - nothing to do:
-                */
-               if (bkey_whiteout(&insert->k))
-                       return false;
-
-               insert->k.needs_whiteout = false;
        }
 
        k = bch2_btree_node_iter_bset_pos(node_iter, b, bset_tree_last(b));