Commit 805ddc20 authored by Kent Overstreet's avatar Kent Overstreet

bcachefs: bch2_dev_rcu_noerror()

bch2_dev_rcu() now properly errors if the device is invalid
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent b99a94fd
...@@ -16,7 +16,7 @@ enum bch_validate_flags; ...@@ -16,7 +16,7 @@ enum bch_validate_flags;
static inline bool bch2_dev_bucket_exists(struct bch_fs *c, struct bpos pos) static inline bool bch2_dev_bucket_exists(struct bch_fs *c, struct bpos pos)
{ {
rcu_read_lock(); rcu_read_lock();
struct bch_dev *ca = bch2_dev_rcu(c, pos.inode); struct bch_dev *ca = bch2_dev_rcu_noerror(c, pos.inode);
bool ret = ca && bucket_valid(ca, pos.offset); bool ret = ca && bucket_valid(ca, pos.offset);
rcu_read_unlock(); rcu_read_unlock();
return ret; return ret;
......
...@@ -55,7 +55,7 @@ int bch2_backpointer_validate(struct bch_fs *c, struct bkey_s_c k, ...@@ -55,7 +55,7 @@ int bch2_backpointer_validate(struct bch_fs *c, struct bkey_s_c k,
struct bkey_s_c_backpointer bp = bkey_s_c_to_backpointer(k); struct bkey_s_c_backpointer bp = bkey_s_c_to_backpointer(k);
rcu_read_lock(); rcu_read_lock();
struct bch_dev *ca = bch2_dev_rcu(c, bp.k->p.inode); struct bch_dev *ca = bch2_dev_rcu_noerror(c, bp.k->p.inode);
if (!ca) { if (!ca) {
/* these will be caught by fsck */ /* these will be caught by fsck */
rcu_read_unlock(); rcu_read_unlock();
...@@ -89,7 +89,7 @@ void bch2_backpointer_to_text(struct printbuf *out, const struct bch_backpointer ...@@ -89,7 +89,7 @@ void bch2_backpointer_to_text(struct printbuf *out, const struct bch_backpointer
void bch2_backpointer_k_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k) void bch2_backpointer_k_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
{ {
rcu_read_lock(); rcu_read_lock();
struct bch_dev *ca = bch2_dev_rcu(c, k.k->p.inode); struct bch_dev *ca = bch2_dev_rcu_noerror(c, k.k->p.inode);
if (ca) { if (ca) {
struct bpos bucket = bp_pos_to_bucket(ca, k.k->p); struct bpos bucket = bp_pos_to_bucket(ca, k.k->p);
rcu_read_unlock(); rcu_read_unlock();
...@@ -673,7 +673,7 @@ static int check_extent_to_backpointers(struct btree_trans *trans, ...@@ -673,7 +673,7 @@ static int check_extent_to_backpointers(struct btree_trans *trans,
continue; continue;
rcu_read_lock(); rcu_read_lock();
struct bch_dev *ca = bch2_dev_rcu(c, p.ptr.dev); struct bch_dev *ca = bch2_dev_rcu_noerror(c, p.ptr.dev);
if (ca) if (ca)
bch2_extent_ptr_to_bp(c, ca, btree, level, k, p, entry, &bucket_pos, &bp); bch2_extent_ptr_to_bp(c, ca, btree, level, k, p, entry, &bucket_pos, &bp);
rcu_read_unlock(); rcu_read_unlock();
......
...@@ -281,7 +281,7 @@ int bch2_check_fix_ptrs(struct btree_trans *trans, ...@@ -281,7 +281,7 @@ int bch2_check_fix_ptrs(struct btree_trans *trans,
goto err; goto err;
rcu_read_lock(); rcu_read_lock();
bch2_bkey_drop_ptrs(bkey_i_to_s(new), ptr, !bch2_dev_rcu(c, ptr->dev)); bch2_bkey_drop_ptrs(bkey_i_to_s(new), ptr, !bch2_dev_exists(c, ptr->dev));
rcu_read_unlock(); rcu_read_unlock();
if (level) { if (level) {
......
...@@ -1021,7 +1021,7 @@ void bch2_extent_ptr_to_text(struct printbuf *out, struct bch_fs *c, const struc ...@@ -1021,7 +1021,7 @@ void bch2_extent_ptr_to_text(struct printbuf *out, struct bch_fs *c, const struc
{ {
out->atomic++; out->atomic++;
rcu_read_lock(); rcu_read_lock();
struct bch_dev *ca = bch2_dev_rcu(c, ptr->dev); struct bch_dev *ca = bch2_dev_rcu_noerror(c, ptr->dev);
if (!ca) { if (!ca) {
prt_printf(out, "ptr: %u:%llu gen %u%s", ptr->dev, prt_printf(out, "ptr: %u:%llu gen %u%s", ptr->dev,
(u64) ptr->offset, ptr->gen, (u64) ptr->offset, ptr->gen,
...@@ -1125,8 +1125,9 @@ static int extent_ptr_validate(struct bch_fs *c, ...@@ -1125,8 +1125,9 @@ static int extent_ptr_validate(struct bch_fs *c,
{ {
int ret = 0; int ret = 0;
/* bad pointers are repaired by check_fix_ptrs(): */
rcu_read_lock(); rcu_read_lock();
struct bch_dev *ca = bch2_dev_rcu(c, ptr->dev); struct bch_dev *ca = bch2_dev_rcu_noerror(c, ptr->dev);
if (!ca) { if (!ca) {
rcu_read_unlock(); rcu_read_unlock();
return 0; return 0;
......
...@@ -795,7 +795,7 @@ bool bch2_have_enough_devs(struct bch_fs *c, struct bch_devs_mask devs, ...@@ -795,7 +795,7 @@ bool bch2_have_enough_devs(struct bch_fs *c, struct bch_devs_mask devs,
for (unsigned i = 0; i < e->nr_devs; i++) { for (unsigned i = 0; i < e->nr_devs; i++) {
nr_online += test_bit(e->devs[i], devs.d); nr_online += test_bit(e->devs[i], devs.d);
struct bch_dev *ca = bch2_dev_rcu(c, e->devs[i]); struct bch_dev *ca = bch2_dev_rcu_noerror(c, e->devs[i]);
nr_failed += !ca || ca->mi.state == BCH_MEMBER_STATE_failed; nr_failed += !ca || ca->mi.state == BCH_MEMBER_STATE_failed;
} }
rcu_read_unlock(); rcu_read_unlock();
......
...@@ -198,29 +198,37 @@ static inline struct bch_dev *bch2_dev_locked(struct bch_fs *c, unsigned dev) ...@@ -198,29 +198,37 @@ static inline struct bch_dev *bch2_dev_locked(struct bch_fs *c, unsigned dev)
lockdep_is_held(&c->state_lock)); lockdep_is_held(&c->state_lock));
} }
static inline struct bch_dev *bch2_dev_rcu(struct bch_fs *c, unsigned dev) static inline struct bch_dev *bch2_dev_rcu_noerror(struct bch_fs *c, unsigned dev)
{ {
return c && dev < c->sb.nr_devices return c && dev < c->sb.nr_devices
? rcu_dereference(c->devs[dev]) ? rcu_dereference(c->devs[dev])
: NULL; : NULL;
} }
void bch2_dev_missing(struct bch_fs *, unsigned);
static inline struct bch_dev *bch2_dev_rcu(struct bch_fs *c, unsigned dev)
{
struct bch_dev *ca = bch2_dev_rcu_noerror(c, dev);
if (unlikely(!ca))
bch2_dev_missing(c, dev);
return ca;
}
static inline struct bch_dev *bch2_dev_tryget_noerror(struct bch_fs *c, unsigned dev) static inline struct bch_dev *bch2_dev_tryget_noerror(struct bch_fs *c, unsigned dev)
{ {
rcu_read_lock(); rcu_read_lock();
struct bch_dev *ca = bch2_dev_rcu(c, dev); struct bch_dev *ca = bch2_dev_rcu_noerror(c, dev);
if (ca) if (ca)
bch2_dev_get(ca); bch2_dev_get(ca);
rcu_read_unlock(); rcu_read_unlock();
return ca; return ca;
} }
void bch2_dev_missing(struct bch_fs *, unsigned);
static inline struct bch_dev *bch2_dev_tryget(struct bch_fs *c, unsigned dev) static inline struct bch_dev *bch2_dev_tryget(struct bch_fs *c, unsigned dev)
{ {
struct bch_dev *ca = bch2_dev_tryget_noerror(c, dev); struct bch_dev *ca = bch2_dev_tryget_noerror(c, dev);
if (!ca) if (unlikely(!ca))
bch2_dev_missing(c, dev); bch2_dev_missing(c, dev);
return ca; return ca;
} }
......
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