Commit f9035b0c authored by Kent Overstreet's avatar Kent Overstreet

bcachefs: Fix refcount leak in check_fix_ptrs()

fsck_err() does a goto fsck_err on error; factor out check_fix_ptr() so
that our error label can drop our device ref.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent bf2b356a
...@@ -465,143 +465,161 @@ int bch2_update_cached_sectors_list(struct btree_trans *trans, unsigned dev, s64 ...@@ -465,143 +465,161 @@ int bch2_update_cached_sectors_list(struct btree_trans *trans, unsigned dev, s64
return bch2_update_replicas_list(trans, &r.e, sectors); return bch2_update_replicas_list(trans, &r.e, sectors);
} }
int bch2_check_fix_ptrs(struct btree_trans *trans, static int bch2_check_fix_ptr(struct btree_trans *trans,
enum btree_id btree, unsigned level, struct bkey_s_c k, struct bkey_s_c k,
enum btree_iter_update_trigger_flags flags) struct extent_ptr_decoded p,
const union bch_extent_entry *entry,
bool *do_update)
{ {
struct bch_fs *c = trans->c; struct bch_fs *c = trans->c;
struct bkey_ptrs_c ptrs_c = bch2_bkey_ptrs_c(k);
const union bch_extent_entry *entry_c;
struct extent_ptr_decoded p = { 0 };
bool do_update = false;
struct printbuf buf = PRINTBUF; struct printbuf buf = PRINTBUF;
int ret = 0; int ret = 0;
percpu_down_read(&c->mark_lock); struct bch_dev *ca = bch2_dev_tryget(c, p.ptr.dev);
if (!ca) {
if (fsck_err(c, ptr_to_invalid_device,
"pointer to missing device %u\n"
"while marking %s",
p.ptr.dev,
(printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
*do_update = true;
return 0;
}
bkey_for_each_ptr_decode(k.k, ptrs_c, p, entry_c) { struct bucket *g = PTR_GC_BUCKET(ca, &p.ptr);
struct bch_dev *ca = bch2_dev_tryget(c, p.ptr.dev); enum bch_data_type data_type = bch2_bkey_ptr_data_type(k, p, entry);
if (!ca) {
if (fsck_err(c, ptr_to_invalid_device,
"pointer to missing device %u\n"
"while marking %s",
p.ptr.dev,
(printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
do_update = true;
continue;
}
struct bucket *g = PTR_GC_BUCKET(ca, &p.ptr); if (fsck_err_on(!g->gen_valid,
enum bch_data_type data_type = bch2_bkey_ptr_data_type(k, p, entry_c); c, ptr_to_missing_alloc_key,
"bucket %u:%zu data type %s ptr gen %u missing in alloc btree\n"
"while marking %s",
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
bch2_data_type_str(ptr_data_type(k.k, &p.ptr)),
p.ptr.gen,
(printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
if (!p.ptr.cached) {
g->gen_valid = true;
g->gen = p.ptr.gen;
} else {
*do_update = true;
}
}
if (fsck_err_on(!g->gen_valid, if (fsck_err_on(gen_cmp(p.ptr.gen, g->gen) > 0,
c, ptr_to_missing_alloc_key, c, ptr_gen_newer_than_bucket_gen,
"bucket %u:%zu data type %s ptr gen %u missing in alloc btree\n" "bucket %u:%zu data type %s ptr gen in the future: %u > %u\n"
"while marking %s", "while marking %s",
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
bch2_data_type_str(ptr_data_type(k.k, &p.ptr)), bch2_data_type_str(ptr_data_type(k.k, &p.ptr)),
p.ptr.gen, p.ptr.gen, g->gen,
(printbuf_reset(&buf), (printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, k), buf.buf))) { bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
if (!p.ptr.cached) { if (!p.ptr.cached &&
g->gen_valid = true; (g->data_type != BCH_DATA_btree ||
g->gen = p.ptr.gen; data_type == BCH_DATA_btree)) {
} else { g->gen_valid = true;
do_update = true; g->gen = p.ptr.gen;
} g->data_type = 0;
g->dirty_sectors = 0;
g->cached_sectors = 0;
} else {
*do_update = true;
} }
}
if (fsck_err_on(gen_cmp(p.ptr.gen, g->gen) > 0, if (fsck_err_on(gen_cmp(g->gen, p.ptr.gen) > BUCKET_GC_GEN_MAX,
c, ptr_gen_newer_than_bucket_gen, c, ptr_gen_newer_than_bucket_gen,
"bucket %u:%zu data type %s ptr gen in the future: %u > %u\n" "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
"while marking %s", "while marking %s",
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), g->gen,
bch2_data_type_str(ptr_data_type(k.k, &p.ptr)), bch2_data_type_str(ptr_data_type(k.k, &p.ptr)),
p.ptr.gen, g->gen, p.ptr.gen,
(printbuf_reset(&buf), (printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, k), buf.buf))) { bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
if (!p.ptr.cached && *do_update = true;
(g->data_type != BCH_DATA_btree ||
data_type == BCH_DATA_btree)) { if (fsck_err_on(!p.ptr.cached && gen_cmp(p.ptr.gen, g->gen) < 0,
g->gen_valid = true; c, stale_dirty_ptr,
g->gen = p.ptr.gen; "bucket %u:%zu data type %s stale dirty ptr: %u < %u\n"
g->data_type = 0; "while marking %s",
g->dirty_sectors = 0; p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
g->cached_sectors = 0; bch2_data_type_str(ptr_data_type(k.k, &p.ptr)),
} else { p.ptr.gen, g->gen,
do_update = true; (printbuf_reset(&buf),
} bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
*do_update = true;
if (data_type != BCH_DATA_btree && p.ptr.gen != g->gen)
goto out;
if (fsck_err_on(bucket_data_type_mismatch(g->data_type, data_type),
c, ptr_bucket_data_type_mismatch,
"bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
"while marking %s",
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), g->gen,
bch2_data_type_str(g->data_type),
bch2_data_type_str(data_type),
(printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
if (data_type == BCH_DATA_btree) {
g->gen_valid = true;
g->gen = p.ptr.gen;
g->data_type = data_type;
g->dirty_sectors = 0;
g->cached_sectors = 0;
} else {
*do_update = true;
} }
}
if (fsck_err_on(gen_cmp(g->gen, p.ptr.gen) > BUCKET_GC_GEN_MAX, if (p.has_ec) {
c, ptr_gen_newer_than_bucket_gen, struct gc_stripe *m = genradix_ptr(&c->gc_stripes, p.ec.idx);
"bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
if (fsck_err_on(!m || !m->alive, c,
ptr_to_missing_stripe,
"pointer to nonexistent stripe %llu\n"
"while marking %s", "while marking %s",
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), g->gen, (u64) p.ec.idx,
bch2_data_type_str(ptr_data_type(k.k, &p.ptr)),
p.ptr.gen,
(printbuf_reset(&buf), (printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, k), buf.buf))) bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
do_update = true; *do_update = true;
if (fsck_err_on(!p.ptr.cached && gen_cmp(p.ptr.gen, g->gen) < 0, if (fsck_err_on(m && m->alive && !bch2_ptr_matches_stripe_m(m, p), c,
c, stale_dirty_ptr, ptr_to_incorrect_stripe,
"bucket %u:%zu data type %s stale dirty ptr: %u < %u\n" "pointer does not match stripe %llu\n"
"while marking %s", "while marking %s",
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), (u64) p.ec.idx,
bch2_data_type_str(ptr_data_type(k.k, &p.ptr)),
p.ptr.gen, g->gen,
(printbuf_reset(&buf), (printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, k), buf.buf))) bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
do_update = true; *do_update = true;
}
out:
fsck_err:
bch2_dev_put(ca);
printbuf_exit(&buf);
return ret;
}
if (data_type != BCH_DATA_btree && p.ptr.gen != g->gen) int bch2_check_fix_ptrs(struct btree_trans *trans,
goto next; enum btree_id btree, unsigned level, struct bkey_s_c k,
enum btree_iter_update_trigger_flags flags)
{
struct bch_fs *c = trans->c;
struct bkey_ptrs_c ptrs_c = bch2_bkey_ptrs_c(k);
const union bch_extent_entry *entry_c;
struct extent_ptr_decoded p = { 0 };
bool do_update = false;
struct printbuf buf = PRINTBUF;
int ret = 0;
if (fsck_err_on(bucket_data_type_mismatch(g->data_type, data_type), percpu_down_read(&c->mark_lock);
c, ptr_bucket_data_type_mismatch,
"bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
"while marking %s",
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), g->gen,
bch2_data_type_str(g->data_type),
bch2_data_type_str(data_type),
(printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
if (data_type == BCH_DATA_btree) {
g->gen_valid = true;
g->gen = p.ptr.gen;
g->data_type = data_type;
g->dirty_sectors = 0;
g->cached_sectors = 0;
} else {
do_update = true;
}
}
if (p.has_ec) { bkey_for_each_ptr_decode(k.k, ptrs_c, p, entry_c) {
struct gc_stripe *m = genradix_ptr(&c->gc_stripes, p.ec.idx); ret = bch2_check_fix_ptr(trans, k, p, entry_c, &do_update);
if (ret)
if (fsck_err_on(!m || !m->alive, c, goto err;
ptr_to_missing_stripe,
"pointer to nonexistent stripe %llu\n"
"while marking %s",
(u64) p.ec.idx,
(printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
do_update = true;
if (fsck_err_on(m && m->alive && !bch2_ptr_matches_stripe_m(m, p), c,
ptr_to_incorrect_stripe,
"pointer does not match stripe %llu\n"
"while marking %s",
(u64) p.ec.idx,
(printbuf_reset(&buf),
bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
do_update = true;
}
next:
bch2_dev_put(ca);
} }
if (do_update) { if (do_update) {
...@@ -716,7 +734,6 @@ int bch2_check_fix_ptrs(struct btree_trans *trans, ...@@ -716,7 +734,6 @@ int bch2_check_fix_ptrs(struct btree_trans *trans,
bch2_btree_node_update_key_early(trans, btree, level - 1, k, new); bch2_btree_node_update_key_early(trans, btree, level - 1, k, new);
} }
err: err:
fsck_err:
percpu_up_read(&c->mark_lock); percpu_up_read(&c->mark_lock);
printbuf_exit(&buf); printbuf_exit(&buf);
return ret; return ret;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment