[PATCH 05/12] block: Introduce accessor functions for copy offload bios

Bart Van Assche
2026-04-24T22:42:35Z

Make it easy for block drivers to iterate over the copy offload bios by
providing accessor functions for the copy offloading bios.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
block/blk-copy.c | 47 ++++++++++++++++++++++++++++++++++++++++
include/linux/blk-copy.h | 4 ++++
2 files changed, 51 insertions(+)

diff --git a/block/blk-copy.c b/block/blk-copy.c
index 459ed8581efc..f49a5f835b4a 100644
--- a/block/blk-copy.c
+++ b/block/blk-copy.c
@@ -7,6 +7,53 @@
#include <linux/blk-copy.h>
#include <linux/blk-mq.h>

+static struct bio *__blk_next_copy_bio(struct request *rq, struct bio *prev_bio,
+ enum req_op op)
+{
+ struct bio *bio;
+
+ if (prev_bio) {
+ bio = prev_bio->bi_next;
+ } else {
+ struct bio_copy_offload_ctx *copy_ctx = rq->bio->bi_copy_ctx;
+
+ bio = copy_ctx->bios;
+ }
+
+ for (; bio && bio_op(bio) != op; bio = bio->bi_next)
+ ;
+ return bio;
+}
+
+struct bio *blk_first_copy_bio(struct request *rq, enum req_op op)
+{
+ struct bio *bio = rq->bio;
+
+ if (bio_op(bio) == op)
+ return bio;
+
+ return __blk_next_copy_bio(rq, NULL, op);
+}
+EXPORT_SYMBOL_GPL(blk_first_copy_bio);
+
+struct bio *blk_next_copy_bio(struct bio *bio)
+{
+ return __blk_next_copy_bio(NULL, bio, bio_op(bio));
+}
+EXPORT_SYMBOL_GPL(blk_next_copy_bio);
+
+unsigned int blk_copy_bio_count(struct request *rq, enum req_op op)
+{
+ unsigned int count = 0;
+
+ for (struct bio *bio = blk_first_copy_bio(rq, op); bio;
+ bio = blk_next_copy_bio(bio))
+ count++;
+
+ return count;
+}
+EXPORT_SYMBOL_GPL(blk_copy_bio_count);
+
/**
* Tracks the state of a single onloaded copy operation.
* @params: Data copy parameters.
diff --git a/include/linux/blk-copy.h b/include/linux/blk-copy.h
index 5e38cfc14a71..4c8435312752 100644
--- a/include/linux/blk-copy.h
+++ b/include/linux/blk-copy.h
@@ -43,4 +43,8 @@ struct bio_copy_offload_ctx {
void (*translation_complete)(struct bio_copy_offload_ctx *ctx);
};

+struct bio *blk_first_copy_bio(struct request *rq, enum req_op op);
+struct bio *blk_next_copy_bio(struct bio *bio);
+unsigned int blk_copy_bio_count(struct request *rq, enum req_op op);
+
#endif /* __LINUX_BLK_COPY_H */