Skip to content

Commit 853960d

Browse files
author
Kent Overstreet
committed
bcachefs: Simplify, fix bch2_backpointer_get_key()
- backpointer_not_found() checks backpointers_no_use_write_buffer, no need to do it inbackpointer_get_key(). - always use backpointer_get_node() for pointers to nodes: backpointer_get_key() was sometimes returning the key from the root node unlocked. Signed-off-by: Kent Overstreet <[email protected]>
1 parent daba90f commit 853960d

File tree

1 file changed

+33
-44
lines changed

1 file changed

+33
-44
lines changed

fs/bcachefs/backpointers.c

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "backpointers.h"
66
#include "btree_cache.h"
77
#include "btree_update.h"
8+
#include "btree_update_interior.h"
89
#include "btree_write_buffer.h"
910
#include "error.h"
1011

@@ -226,6 +227,11 @@ static void backpointer_not_found(struct btree_trans *trans,
226227
struct printbuf buf = PRINTBUF;
227228
struct bpos bucket = bp_pos_to_bucket(c, bp_pos);
228229

230+
/*
231+
* If we're using the btree write buffer, the backpointer we were
232+
* looking at may have already been deleted - failure to find what it
233+
* pointed to is not an error:
234+
*/
229235
if (likely(!bch2_backpointers_no_use_write_buffer))
230236
return;
231237

@@ -256,56 +262,37 @@ struct bkey_s_c bch2_backpointer_get_key(struct btree_trans *trans,
256262
struct bch_backpointer bp,
257263
unsigned iter_flags)
258264
{
259-
struct bch_fs *c = trans->c;
260-
struct btree_root *r = bch2_btree_id_root(c, bp.btree_id);
261-
struct bpos bucket = bp_pos_to_bucket(c, bp_pos);
262-
struct bkey_s_c k;
263-
264-
bch2_trans_node_iter_init(trans, iter,
265-
bp.btree_id,
266-
bp.pos,
267-
0,
268-
min(bp.level, r->level),
269-
iter_flags);
270-
k = bch2_btree_iter_peek_slot(iter);
271-
if (bkey_err(k)) {
272-
bch2_trans_iter_exit(trans, iter);
273-
return k;
274-
}
275-
276-
if (bp.level == r->level + 1)
277-
k = bkey_i_to_s_c(&r->key);
278-
279-
if (k.k && extent_matches_bp(c, bp.btree_id, bp.level, k, bucket, bp))
280-
return k;
281-
282-
bch2_trans_iter_exit(trans, iter);
265+
if (likely(!bp.level)) {
266+
struct bch_fs *c = trans->c;
267+
struct bpos bucket = bp_pos_to_bucket(c, bp_pos);
268+
struct bkey_s_c k;
269+
270+
bch2_trans_node_iter_init(trans, iter,
271+
bp.btree_id,
272+
bp.pos,
273+
0, 0,
274+
iter_flags);
275+
k = bch2_btree_iter_peek_slot(iter);
276+
if (bkey_err(k)) {
277+
bch2_trans_iter_exit(trans, iter);
278+
return k;
279+
}
283280

284-
if (unlikely(bch2_backpointers_no_use_write_buffer)) {
285-
if (bp.level) {
286-
struct btree *b;
281+
if (k.k && extent_matches_bp(c, bp.btree_id, bp.level, k, bucket, bp))
282+
return k;
287283

288-
/*
289-
* If a backpointer for a btree node wasn't found, it may be
290-
* because it was overwritten by a new btree node that hasn't
291-
* been written out yet - backpointer_get_node() checks for
292-
* this:
293-
*/
294-
b = bch2_backpointer_get_node(trans, iter, bp_pos, bp);
295-
if (!IS_ERR_OR_NULL(b))
296-
return bkey_i_to_s_c(&b->key);
284+
bch2_trans_iter_exit(trans, iter);
285+
backpointer_not_found(trans, bp_pos, bp, k);
286+
return bkey_s_c_null;
287+
} else {
288+
struct btree *b = bch2_backpointer_get_node(trans, iter, bp_pos, bp);
297289

290+
if (IS_ERR_OR_NULL(b)) {
298291
bch2_trans_iter_exit(trans, iter);
299-
300-
if (IS_ERR(b))
301-
return bkey_s_c_err(PTR_ERR(b));
302-
return bkey_s_c_null;
292+
return IS_ERR(b) ? bkey_s_c_err(PTR_ERR(b)) : bkey_s_c_null;
303293
}
304-
305-
backpointer_not_found(trans, bp_pos, bp, k);
294+
return bkey_i_to_s_c(&b->key);
306295
}
307-
308-
return bkey_s_c_null;
309296
}
310297

311298
struct btree *bch2_backpointer_get_node(struct btree_trans *trans,
@@ -329,6 +316,8 @@ struct btree *bch2_backpointer_get_node(struct btree_trans *trans,
329316
if (IS_ERR(b))
330317
goto err;
331318

319+
BUG_ON(b->c.level != bp.level - 1);
320+
332321
if (b && extent_matches_bp(c, bp.btree_id, bp.level,
333322
bkey_i_to_s_c(&b->key),
334323
bucket, bp))

0 commit comments

Comments
 (0)