]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
bcachefs: darray_find(), darray_find_p()
authorKent Overstreet <kent.overstreet@linux.dev>
Thu, 29 May 2025 20:56:50 +0000 (16:56 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 1 Jun 2025 02:03:17 +0000 (22:03 -0400)
New helpers to avoid open coded loops.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/alloc_background.c
fs/bcachefs/darray.h
fs/bcachefs/fsck.c
fs/bcachefs/snapshot.c
fs/bcachefs/snapshot.h

index f284b4a2b535ab533f6fb3d41fad460f830c4879..2325a2699a8900fe5f1b5e42e8a43a8adc5d9133 100644 (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 @@ out:
 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);
 }
 
index 50ec3decfe8c32e8e70211cc1ad539b2ee150018..d08d39c1b93d84277ef456d9c1fcb77281457fbc 100644 (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)                                                \
index 950fa9685d3e84c8a6a0faa8d5851591cfe3653c..631ee2af8585f19f8b2d656f594a2b3bb2a68ad9 100644 (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;
index f553fe095f61f45b4722e9763206cf281c8843b8..cf9a65e858f64e7953dbbd344fac3780d657b6bf 100644 (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,
index be7b71c0662124d47c6ec98a01c420c7a42ee157..ee79f81f175c020111f01b4cdf2d5438064081bb 100644 (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)