bcachefs: bucket_valid()

cut out a branch from doing it the obvious way

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2024-04-11 21:18:35 -04:00
parent 923ed0ae5e
commit d155272b6e
7 changed files with 35 additions and 25 deletions

View File

@ -21,8 +21,7 @@ static inline bool bch2_dev_bucket_exists(struct bch_fs *c, struct bpos pos)
return false;
ca = bch_dev_bkey_exists(c, pos.inode);
return pos.offset >= ca->mi.first_bucket &&
pos.offset < ca->mi.nbuckets;
return bucket_valid(ca, pos.offset);
}
static inline u64 bucket_to_u64(struct bpos bucket)

View File

@ -470,6 +470,7 @@ enum bch_time_stats {
#include "quota_types.h"
#include "rebalance_types.h"
#include "replicas_types.h"
#include "sb-members_types.h"
#include "subvolume_types.h"
#include "super_types.h"
#include "thread_with_file_types.h"

View File

@ -12,7 +12,7 @@
#include "extents.h"
#include "sb-members.h"
static inline size_t sector_to_bucket(const struct bch_dev *ca, sector_t s)
static inline u64 sector_to_bucket(const struct bch_dev *ca, sector_t s)
{
return div_u64(s, ca->mi.bucket_size);
}
@ -30,12 +30,16 @@ static inline sector_t bucket_remainder(const struct bch_dev *ca, sector_t s)
return remainder;
}
static inline size_t sector_to_bucket_and_offset(const struct bch_dev *ca, sector_t s,
u32 *offset)
static inline u64 sector_to_bucket_and_offset(const struct bch_dev *ca, sector_t s, u32 *offset)
{
return div_u64_rem(s, ca->mi.bucket_size, offset);
}
static inline bool bucket_valid(const struct bch_dev *ca, u64 b)
{
return b - ca->mi.first_bucket < ca->mi.nbuckets_minus_first;
}
#define for_each_bucket(_b, _buckets) \
for (_b = (_buckets)->b + (_buckets)->first_bucket; \
_b < (_buckets)->b + (_buckets)->nbuckets; _b++)
@ -94,7 +98,7 @@ static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b)
{
struct bucket_array *buckets = gc_bucket_array(ca);
BUG_ON(b < buckets->first_bucket || b >= buckets->nbuckets);
BUG_ON(!bucket_valid(ca, b));
return buckets->b + b;
}
@ -111,7 +115,7 @@ static inline u8 *bucket_gen(struct bch_dev *ca, size_t b)
{
struct bucket_gens *gens = bucket_gens(ca);
BUG_ON(b < gens->first_bucket || b >= gens->nbuckets);
BUG_ON(!bucket_valid(ca, b));
return gens->b + b;
}

View File

@ -998,9 +998,7 @@ void bch2_extent_ptr_to_text(struct printbuf *out, struct bch_fs *c, const struc
prt_str(out, " cached");
if (ptr->unwritten)
prt_str(out, " unwritten");
if (b >= ca->mi.first_bucket &&
b < ca->mi.nbuckets &&
ptr_stale(ca, ptr))
if (bucket_valid(ca, b) && ptr_stale(ca, ptr))
prt_printf(out, " stale");
}
}

View File

@ -210,6 +210,8 @@ static inline struct bch_member_cpu bch2_mi_to_cpu(struct bch_member *mi)
{
return (struct bch_member_cpu) {
.nbuckets = le64_to_cpu(mi->nbuckets),
.nbuckets_minus_first = le64_to_cpu(mi->nbuckets) -
le16_to_cpu(mi->first_bucket),
.first_bucket = le16_to_cpu(mi->first_bucket),
.bucket_size = le16_to_cpu(mi->bucket_size),
.group = BCH_MEMBER_GROUP(mi),

View File

@ -0,0 +1,21 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _BCACHEFS_SB_MEMBERS_TYPES_H
#define _BCACHEFS_SB_MEMBERS_TYPES_H
struct bch_member_cpu {
u64 nbuckets; /* device size */
u64 nbuckets_minus_first;
u16 first_bucket; /* index of first bucket used */
u16 bucket_size; /* sectors */
u16 group;
u8 state;
u8 discard;
u8 data_allowed;
u8 durability;
u8 freespace_initialized;
u8 valid;
u8 btree_bitmap_shift;
u64 btree_allocated_bitmap;
};
#endif /* _BCACHEFS_SB_MEMBERS_H */

View File

@ -26,19 +26,4 @@ struct bch_devs_list {
u8 data[BCH_BKEY_PTRS_MAX];
};
struct bch_member_cpu {
u64 nbuckets; /* device size */
u16 first_bucket; /* index of first bucket used */
u16 bucket_size; /* sectors */
u16 group;
u8 state;
u8 discard;
u8 data_allowed;
u8 durability;
u8 freespace_initialized;
u8 valid;
u8 btree_bitmap_shift;
u64 btree_allocated_bitmap;
};
#endif /* _BCACHEFS_SUPER_TYPES_H */