Skip to content

Commit 5802caf

Browse files
author
Kent Overstreet
committed
bcachefs: darray_find(), darray_find_p()
New helpers to avoid open coded loops. Signed-off-by: Kent Overstreet <[email protected]>
1 parent 9a1accd commit 5802caf

File tree

5 files changed

+36
-33
lines changed

5 files changed

+36
-33
lines changed

fs/bcachefs/alloc_background.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,11 +1792,12 @@ static int discard_in_flight_add(struct bch_dev *ca, u64 bucket, bool in_progres
17921792
int ret;
17931793

17941794
mutex_lock(&ca->discard_buckets_in_flight_lock);
1795-
darray_for_each(ca->discard_buckets_in_flight, i)
1796-
if (i->bucket == bucket) {
1797-
ret = -BCH_ERR_EEXIST_discard_in_flight_add;
1798-
goto out;
1799-
}
1795+
struct discard_in_flight *i =
1796+
darray_find_p(ca->discard_buckets_in_flight, i, i->bucket == bucket);
1797+
if (i) {
1798+
ret = -BCH_ERR_EEXIST_discard_in_flight_add;
1799+
goto out;
1800+
}
18001801

18011802
ret = darray_push(&ca->discard_buckets_in_flight, ((struct discard_in_flight) {
18021803
.in_progress = in_progress,
@@ -1810,14 +1811,11 @@ static int discard_in_flight_add(struct bch_dev *ca, u64 bucket, bool in_progres
18101811
static void discard_in_flight_remove(struct bch_dev *ca, u64 bucket)
18111812
{
18121813
mutex_lock(&ca->discard_buckets_in_flight_lock);
1813-
darray_for_each(ca->discard_buckets_in_flight, i)
1814-
if (i->bucket == bucket) {
1815-
BUG_ON(!i->in_progress);
1816-
darray_remove_item(&ca->discard_buckets_in_flight, i);
1817-
goto found;
1818-
}
1819-
BUG();
1820-
found:
1814+
struct discard_in_flight *i =
1815+
darray_find_p(ca->discard_buckets_in_flight, i, i->bucket == bucket);
1816+
BUG_ON(!i || !i->in_progress);
1817+
1818+
darray_remove_item(&ca->discard_buckets_in_flight, i);
18211819
mutex_unlock(&ca->discard_buckets_in_flight_lock);
18221820
}
18231821

fs/bcachefs/darray.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,23 @@ int __bch2_darray_resize_noprof(darray_char *, size_t, size_t, gfp_t);
8787
#define darray_remove_item(_d, _pos) \
8888
array_remove_item((_d)->data, (_d)->nr, (_pos) - (_d)->data)
8989

90-
#define __darray_for_each(_d, _i) \
90+
#define darray_find_p(_d, _i, cond) \
91+
({ \
92+
typeof((_d).data) _ret = NULL; \
93+
\
94+
darray_for_each(_d, _i) \
95+
if (cond) { \
96+
_ret = _i; \
97+
break; \
98+
} \
99+
_ret; \
100+
})
101+
102+
#define darray_find(_d, _item) darray_find_p(_d, _i, *_i == _item)
103+
104+
/* Iteration: */
105+
106+
#define __darray_for_each(_d, _i) \
91107
for ((_i) = (_d).data; _i < (_d).data + (_d).nr; _i++)
92108

93109
#define darray_for_each(_d, _i) \

fs/bcachefs/fsck.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -885,14 +885,11 @@ lookup_inode_for_snapshot(struct btree_trans *trans, struct inode_walker *w, str
885885
{
886886
struct bch_fs *c = trans->c;
887887

888-
struct inode_walker_entry *i;
889-
__darray_for_each(w->inodes, i)
890-
if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, i->inode.bi_snapshot))
891-
goto found;
888+
struct inode_walker_entry *i = darray_find_p(w->inodes, i,
889+
bch2_snapshot_is_ancestor(c, k.k->p.snapshot, i->inode.bi_snapshot));
892890

893-
return NULL;
894-
found:
895-
BUG_ON(k.k->p.snapshot > i->inode.bi_snapshot);
891+
if (!i)
892+
return NULL;
896893

897894
struct printbuf buf = PRINTBUF;
898895
int ret = 0;

fs/bcachefs/snapshot.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -947,10 +947,7 @@ static inline bool same_snapshot(struct snapshot_tree_reconstruct *r, struct bpo
947947

948948
static inline bool snapshot_id_lists_have_common(snapshot_id_list *l, snapshot_id_list *r)
949949
{
950-
darray_for_each(*l, i)
951-
if (snapshot_list_has_id(r, *i))
952-
return true;
953-
return false;
950+
return darray_find_p(*l, i, snapshot_list_has_id(r, *i)) != NULL;
954951
}
955952

956953
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,
14281425

14291426
static inline u32 interior_delete_has_id(interior_delete_list *l, u32 id)
14301427
{
1431-
darray_for_each(*l, i)
1432-
if (i->id == id)
1433-
return i->live_child;
1434-
return 0;
1428+
struct snapshot_interior_delete *i = darray_find_p(*l, i, i->id == id);
1429+
return i ? i->live_child : 0;
14351430
}
14361431

14371432
static unsigned __live_child(struct snapshot_table *t, u32 id,

fs/bcachefs/snapshot.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,7 @@ static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
190190

191191
static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
192192
{
193-
darray_for_each(*s, i)
194-
if (*i == id)
195-
return true;
196-
return false;
193+
return darray_find(*s, id) != NULL;
197194
}
198195

199196
static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)

0 commit comments

Comments
 (0)