Commit 3e1a88ec authored by Pavel Begunkov's avatar Pavel Begunkov Committed by Jens Axboe
Browse files

bio: add a helper calculating nr segments to alloc



Add a helper function calculating the number of bvec segments we need to
allocate to construct a bio. It doesn't change anything functionally,
but will be used to not duplicate special cases in the future.

Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarPavel Begunkov <asml.silence@gmail.com>
Reviewed-by: default avatarMing Lei <ming.lei@redhat.com>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 54c8195b
Loading
Loading
Loading
Loading
+4 −3
Original line number Original line Diff line number Diff line
@@ -416,7 +416,7 @@ __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
		dio->size += bio->bi_iter.bi_size;
		dio->size += bio->bi_iter.bi_size;
		pos += bio->bi_iter.bi_size;
		pos += bio->bi_iter.bi_size;


		nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
		nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_PAGES);
		if (!nr_pages) {
		if (!nr_pages) {
			bool polled = false;
			bool polled = false;


@@ -481,9 +481,10 @@ blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
{
{
	int nr_pages;
	int nr_pages;


	nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
	if (!iov_iter_count(iter))
	if (!nr_pages)
		return 0;
		return 0;

	nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_PAGES + 1);
	if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
	if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
		return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
		return __blkdev_direct_IO_simple(iocb, iter, nr_pages);


+4 −5
Original line number Original line Diff line number Diff line
@@ -250,11 +250,8 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
	orig_count = iov_iter_count(dio->submit.iter);
	orig_count = iov_iter_count(dio->submit.iter);
	iov_iter_truncate(dio->submit.iter, length);
	iov_iter_truncate(dio->submit.iter, length);


	nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
	if (!iov_iter_count(dio->submit.iter))
	if (nr_pages <= 0) {
		ret = nr_pages;
		goto out;
		goto out;
	}


	if (need_zeroout) {
	if (need_zeroout) {
		/* zero out from the start of the block to the write offset */
		/* zero out from the start of the block to the write offset */
@@ -263,6 +260,7 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
			iomap_dio_zero(dio, iomap, pos - pad, pad);
			iomap_dio_zero(dio, iomap, pos - pad, pad);
	}
	}


	nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_PAGES);
	do {
	do {
		size_t n;
		size_t n;
		if (dio->error) {
		if (dio->error) {
@@ -308,7 +306,8 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
		dio->size += n;
		dio->size += n;
		copied += n;
		copied += n;


		nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
		nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter,
						 BIO_MAX_PAGES);
		iomap_dio_submit_bio(dio, iomap, bio, pos);
		iomap_dio_submit_bio(dio, iomap, bio, pos);
		pos += n;
		pos += n;
	} while (nr_pages);
	} while (nr_pages);
+10 −0
Original line number Original line Diff line number Diff line
@@ -10,6 +10,7 @@
#include <linux/ioprio.h>
#include <linux/ioprio.h>
/* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
/* struct bio, bio_vec and BIO_* flags are defined in blk_types.h */
#include <linux/blk_types.h>
#include <linux/blk_types.h>
#include <linux/uio.h>


#define BIO_DEBUG
#define BIO_DEBUG


@@ -441,6 +442,15 @@ static inline void bio_wouldblock_error(struct bio *bio)
	bio_endio(bio);
	bio_endio(bio);
}
}


/*
 * Calculate number of bvec segments that should be allocated to fit data
 * pointed by @iter.
 */
static inline int bio_iov_vecs_to_alloc(struct iov_iter *iter, int max_segs)
{
	return iov_iter_npages(iter, max_segs);
}

struct request_queue;
struct request_queue;


extern int submit_bio_wait(struct bio *bio);
extern int submit_bio_wait(struct bio *bio);