Skip to content

Commit 9c4acd1

Browse files
author
Kent Overstreet
committed
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: Kent Overstreet <[email protected]>
1 parent e0cb572 commit 9c4acd1

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

fs/bcachefs/btree_gc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,8 @@ static int bch2_gc_alloc_start(struct bch_fs *c)
990990

991991
buckets->first_bucket = ca->mi.first_bucket;
992992
buckets->nbuckets = ca->mi.nbuckets;
993+
buckets->nbuckets_minus_first =
994+
buckets->nbuckets - buckets->first_bucket;
993995
rcu_assign_pointer(ca->buckets_gc, buckets);
994996
}
995997

fs/bcachefs/buckets.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,8 @@ int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
16121612

16131613
bucket_gens->first_bucket = ca->mi.first_bucket;
16141614
bucket_gens->nbuckets = nbuckets;
1615+
bucket_gens->nbuckets_minus_first =
1616+
bucket_gens->nbuckets - bucket_gens->first_bucket;
16151617

16161618
if (resize) {
16171619
down_write(&c->gc_lock);

fs/bcachefs/buckets.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b)
9393
{
9494
struct bucket_array *buckets = gc_bucket_array(ca);
9595

96-
BUG_ON(!bucket_valid(ca, b));
96+
if (b - buckets->first_bucket >= buckets->nbuckets_minus_first)
97+
return NULL;
9798
return buckets->b + b;
9899
}
99100

@@ -110,7 +111,8 @@ static inline u8 *bucket_gen(struct bch_dev *ca, size_t b)
110111
{
111112
struct bucket_gens *gens = bucket_gens(ca);
112113

113-
BUG_ON(!bucket_valid(ca, b));
114+
if (b - gens->first_bucket >= gens->nbuckets_minus_first)
115+
return NULL;
114116
return gens->b + b;
115117
}
116118

fs/bcachefs/buckets_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ struct bucket_array {
2222
struct rcu_head rcu;
2323
u16 first_bucket;
2424
size_t nbuckets;
25+
size_t nbuckets_minus_first;
2526
struct bucket b[];
2627
};
2728

2829
struct bucket_gens {
2930
struct rcu_head rcu;
3031
u16 first_bucket;
3132
size_t nbuckets;
33+
size_t nbuckets_minus_first;
3234
u8 b[];
3335
};
3436

0 commit comments

Comments
 (0)