Commit 9c4acd19 authored by Kent Overstreet's avatar Kent Overstreet

bcachefs: Replace bucket_valid() asserts in bucket lookup with proper checks

The bucket_gens array and gc_buckets array known their own size; we
should be using those members, and returning an error.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent e0cb5722
...@@ -990,6 +990,8 @@ static int bch2_gc_alloc_start(struct bch_fs *c) ...@@ -990,6 +990,8 @@ static int bch2_gc_alloc_start(struct bch_fs *c)
buckets->first_bucket = ca->mi.first_bucket; buckets->first_bucket = ca->mi.first_bucket;
buckets->nbuckets = ca->mi.nbuckets; buckets->nbuckets = ca->mi.nbuckets;
buckets->nbuckets_minus_first =
buckets->nbuckets - buckets->first_bucket;
rcu_assign_pointer(ca->buckets_gc, buckets); rcu_assign_pointer(ca->buckets_gc, buckets);
} }
......
...@@ -1612,6 +1612,8 @@ int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) ...@@ -1612,6 +1612,8 @@ int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
bucket_gens->first_bucket = ca->mi.first_bucket; bucket_gens->first_bucket = ca->mi.first_bucket;
bucket_gens->nbuckets = nbuckets; bucket_gens->nbuckets = nbuckets;
bucket_gens->nbuckets_minus_first =
bucket_gens->nbuckets - bucket_gens->first_bucket;
if (resize) { if (resize) {
down_write(&c->gc_lock); down_write(&c->gc_lock);
......
...@@ -93,7 +93,8 @@ static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b) ...@@ -93,7 +93,8 @@ static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b)
{ {
struct bucket_array *buckets = gc_bucket_array(ca); struct bucket_array *buckets = gc_bucket_array(ca);
BUG_ON(!bucket_valid(ca, b)); if (b - buckets->first_bucket >= buckets->nbuckets_minus_first)
return NULL;
return buckets->b + b; return buckets->b + b;
} }
...@@ -110,7 +111,8 @@ static inline u8 *bucket_gen(struct bch_dev *ca, size_t b) ...@@ -110,7 +111,8 @@ static inline u8 *bucket_gen(struct bch_dev *ca, size_t b)
{ {
struct bucket_gens *gens = bucket_gens(ca); struct bucket_gens *gens = bucket_gens(ca);
BUG_ON(!bucket_valid(ca, b)); if (b - gens->first_bucket >= gens->nbuckets_minus_first)
return NULL;
return gens->b + b; return gens->b + b;
} }
......
...@@ -22,6 +22,7 @@ struct bucket_array { ...@@ -22,6 +22,7 @@ struct bucket_array {
struct rcu_head rcu; struct rcu_head rcu;
u16 first_bucket; u16 first_bucket;
size_t nbuckets; size_t nbuckets;
size_t nbuckets_minus_first;
struct bucket b[]; struct bucket b[];
}; };
...@@ -29,6 +30,7 @@ struct bucket_gens { ...@@ -29,6 +30,7 @@ struct bucket_gens {
struct rcu_head rcu; struct rcu_head rcu;
u16 first_bucket; u16 first_bucket;
size_t nbuckets; size_t nbuckets;
size_t nbuckets_minus_first;
u8 b[]; u8 b[];
}; };
......
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