2021-01-19 06:01:58

by Chaitanya Kulkarni

[permalink] [raw]
Subject: [RFC PATCH 01/37] block: introduce bio_init_fields() helper

There are several places in the file-system, block layer, device drivers
where struct bio members such as bdev, sector, private, end io callback,
io priority, write hints are initialized where we can use a helper
function.

This pach introduces a helper function which we use in the block lyaer
code. Subsequent patches use this function to reduce repeated code.

Signed-off-by: Chaitanya Kulkarni <[email protected]>
---
block/blk-lib.c | 13 +++++--------
include/linux/bio.h | 13 +++++++++++++
2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/block/blk-lib.c b/block/blk-lib.c
index 752f9c722062..5ee488c1bcc6 100644
--- a/block/blk-lib.c
+++ b/block/blk-lib.c
@@ -95,8 +95,7 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
WARN_ON_ONCE((req_sects << 9) > UINT_MAX);

bio = blk_next_bio(bio, 0, gfp_mask);
- bio->bi_iter.bi_sector = sector;
- bio_set_dev(bio, bdev);
+ bio_init_fields(bio, bdev, sector, NULL, NULL, 0, 0);
bio_set_op_attrs(bio, op, 0);

bio->bi_iter.bi_size = req_sects << 9;
@@ -189,8 +188,7 @@ static int __blkdev_issue_write_same(struct block_device *bdev, sector_t sector,

while (nr_sects) {
bio = blk_next_bio(bio, 1, gfp_mask);
- bio->bi_iter.bi_sector = sector;
- bio_set_dev(bio, bdev);
+ bio_init_fields(bio, bdev, sector, NULL, NULL, 0, 0);
bio->bi_vcnt = 1;
bio->bi_io_vec->bv_page = page;
bio->bi_io_vec->bv_offset = 0;
@@ -265,8 +263,7 @@ static int __blkdev_issue_write_zeroes(struct block_device *bdev,

while (nr_sects) {
bio = blk_next_bio(bio, 0, gfp_mask);
- bio->bi_iter.bi_sector = sector;
- bio_set_dev(bio, bdev);
+ bio_init_fields(bio, bdev, sector, NULL, NULL, 0, 0);
bio->bi_opf = REQ_OP_WRITE_ZEROES;
if (flags & BLKDEV_ZERO_NOUNMAP)
bio->bi_opf |= REQ_NOUNMAP;
@@ -317,8 +314,8 @@ static int __blkdev_issue_zero_pages(struct block_device *bdev,
while (nr_sects != 0) {
bio = blk_next_bio(bio, __blkdev_sectors_to_bio_pages(nr_sects),
gfp_mask);
- bio->bi_iter.bi_sector = sector;
- bio_set_dev(bio, bdev);
+ bio_init_fields(bio, bdev, sector, NULL, NULL, 0, 0);
+
bio_set_op_attrs(bio, REQ_OP_WRITE, 0);

while (nr_sects != 0) {
diff --git a/include/linux/bio.h b/include/linux/bio.h
index 1edda614f7ce..fbeaa5e42a5d 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -820,4 +820,17 @@ static inline void bio_set_polled(struct bio *bio, struct kiocb *kiocb)
bio->bi_opf |= REQ_NOWAIT;
}

+static inline void bio_init_fields(struct bio *bio, struct block_device *bdev,
+ sector_t sect, void *priv,
+ bio_end_io_t *end_io,
+ unsigned short prio, unsigned short whint)
+{
+ bio_set_dev(bio, bdev);
+ bio->bi_iter.bi_sector = sect;
+ bio->bi_private = priv;
+ bio->bi_end_io = end_io;
+ bio->bi_ioprio = prio;
+ bio->bi_write_hint = whint;
+}
+
#endif /* __LINUX_BIO_H */
--
2.22.1


2021-01-19 06:33:48

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [RFC PATCH 01/37] block: introduce bio_init_fields() helper

I don't think a helper just to initialize a few fields is very useful.
But there is something in this area I've wanted to do for a while:

> +static inline void bio_init_fields(struct bio *bio, struct block_device *bdev,
> + sector_t sect, void *priv,
> + bio_end_io_t *end_io,
> + unsigned short prio, unsigned short whint)
> +{
> + bio_set_dev(bio, bdev);
> + bio->bi_iter.bi_sector = sect;
> + bio->bi_private = priv;
> + bio->bi_end_io = end_io;
> + bio->bi_ioprio = prio;
> + bio->bi_write_hint = whint;

Ensuring that the device, sector and op are always initialized would
really helper some of the bio mapping helpers, so I'd rather
add a new

struct bio *bio_new(struct block_device *bdev, sector_t sector,
unsigned int op, unsigned int max_bvecs, gfp_t gfp_mask)

helper, where max_bvecs is clamped to BIO_MAX_PAGES.

bi_private, bi_end_io, bi_ioprio and bi_write_hint on the other hand
are purely optional and can be easily set just by the users that care.