bcachefs: darray_find(), darray_find_p()

New helpers to avoid open coded loops.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2025-05-29 16:56:50 -04:00
parent 9a1accd3a5
commit 5802caf74f
5 changed files with 36 additions and 33 deletions

View File

@ -1792,11 +1792,12 @@ static int discard_in_flight_add(struct bch_dev *ca, u64 bucket, bool in_progres
int ret;
mutex_lock(&ca->discard_buckets_in_flight_lock);
darray_for_each(ca->discard_buckets_in_flight, i)
if (i->bucket == bucket) {
ret = -BCH_ERR_EEXIST_discard_in_flight_add;
goto out;
}
struct discard_in_flight *i =
darray_find_p(ca->discard_buckets_in_flight, i, i->bucket == bucket);
if (i) {
ret = -BCH_ERR_EEXIST_discard_in_flight_add;
goto out;
}
ret = darray_push(&ca->discard_buckets_in_flight, ((struct discard_in_flight) {
.in_progress = in_progress,
@ -1810,14 +1811,11 @@ static int discard_in_flight_add(struct bch_dev *ca, u64 bucket, bool in_progres
static void discard_in_flight_remove(struct bch_dev *ca, u64 bucket)
{
mutex_lock(&ca->discard_buckets_in_flight_lock);
darray_for_each(ca->discard_buckets_in_flight, i)
if (i->bucket == bucket) {
BUG_ON(!i->in_progress);
darray_remove_item(&ca->discard_buckets_in_flight, i);
goto found;
}
BUG();
found:
struct discard_in_flight *i =
darray_find_p(ca->discard_buckets_in_flight, i, i->bucket == bucket);
BUG_ON(!i || !i->in_progress);
darray_remove_item(&ca->discard_buckets_in_flight, i);
mutex_unlock(&ca->discard_buckets_in_flight_lock);
}

View File

@ -87,7 +87,23 @@ int __bch2_darray_resize_noprof(darray_char *, size_t, size_t, gfp_t);
#define darray_remove_item(_d, _pos) \
array_remove_item((_d)->data, (_d)->nr, (_pos) - (_d)->data)
#define __darray_for_each(_d, _i) \
#define darray_find_p(_d, _i, cond) \
({ \
typeof((_d).data) _ret = NULL; \
\
darray_for_each(_d, _i) \
if (cond) { \
_ret = _i; \
break; \
} \
_ret; \
})
#define darray_find(_d, _item) darray_find_p(_d, _i, *_i == _item)
/* Iteration: */
#define __darray_for_each(_d, _i) \
for ((_i) = (_d).data; _i < (_d).data + (_d).nr; _i++)
#define darray_for_each(_d, _i) \

View File

@ -885,14 +885,11 @@ lookup_inode_for_snapshot(struct btree_trans *trans, struct inode_walker *w, str
{
struct bch_fs *c = trans->c;
struct inode_walker_entry *i;
__darray_for_each(w->inodes, i)
if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, i->inode.bi_snapshot))
goto found;
struct inode_walker_entry *i = darray_find_p(w->inodes, i,
bch2_snapshot_is_ancestor(c, k.k->p.snapshot, i->inode.bi_snapshot));
return NULL;
found:
BUG_ON(k.k->p.snapshot > i->inode.bi_snapshot);
if (!i)
return NULL;
struct printbuf buf = PRINTBUF;
int ret = 0;

View File

@ -947,10 +947,7 @@ static inline bool same_snapshot(struct snapshot_tree_reconstruct *r, struct bpo
static inline bool snapshot_id_lists_have_common(snapshot_id_list *l, snapshot_id_list *r)
{
darray_for_each(*l, i)
if (snapshot_list_has_id(r, *i))
return true;
return false;
return darray_find_p(*l, i, snapshot_list_has_id(r, *i)) != NULL;
}
static void snapshot_id_list_to_text(struct printbuf *out, snapshot_id_list *s)
@ -1428,10 +1425,8 @@ int bch2_snapshot_node_create(struct btree_trans *trans, u32 parent,
static inline u32 interior_delete_has_id(interior_delete_list *l, u32 id)
{
darray_for_each(*l, i)
if (i->id == id)
return i->live_child;
return 0;
struct snapshot_interior_delete *i = darray_find_p(*l, i, i->id == id);
return i ? i->live_child : 0;
}
static unsigned __live_child(struct snapshot_table *t, u32 id,

View File

@ -190,10 +190,7 @@ static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
{
darray_for_each(*s, i)
if (*i == id)
return true;
return false;
return darray_find(*s, id) != NULL;
}
static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)