Cleanup lo_rw_aio() a bit by refactoring it into three parts: - lo_cmd_nr_bvec(), for calculating how many bvecs in this request - lo_rw_aio_prep(), for preparing loop command, which need to be called once - lo_submit_rw_aio(), for submitting this lo command, which can be called multiple times Prepare for trying to handle loop command by NOWAIT read/write IO first. Signed-off-by: Ming Lei <ming.lei@xxxxxxxxxx> --- drivers/block/loop.c | 83 +++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 6bbbaa4aaf2c..eae38cd38b7b 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -394,24 +394,63 @@ static void lo_rw_aio_complete(struct kiocb *iocb, long ret) lo_rw_aio_do_completion(cmd); } -static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, loff_t pos) +static int lo_submit_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, + loff_t pos, int nr_bvec) { - struct iov_iter iter; - struct req_iterator rq_iter; - struct bio_vec *bvec; struct request *rq = blk_mq_rq_from_pdu(cmd); int dir = (req_op(rq) == REQ_OP_READ) ? ITER_DEST : ITER_SOURCE; - struct bio *bio = rq->bio; struct file *file = lo->lo_backing_file; - struct bio_vec tmp; + struct iov_iter iter; + struct bio_vec *bvec; unsigned int offset; - int nr_bvec = 0; int ret; + if (rq->bio != rq->biotail) { + bvec = cmd->bvec; + offset = 0; + } else { + struct bio *bio = rq->bio; + + offset = bio->bi_iter.bi_bvec_done; + bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); + } + iov_iter_bvec(&iter, dir, bvec, nr_bvec, blk_rq_bytes(rq)); + iter.iov_offset = offset; + cmd->iocb.ki_pos = pos; + + atomic_set(&cmd->ref, 2); + if (dir == ITER_SOURCE) + ret = file->f_op->write_iter(&cmd->iocb, &iter); + else + ret = file->f_op->read_iter(&cmd->iocb, &iter); + lo_rw_aio_do_completion(cmd); + + return ret; +} + +static inline unsigned lo_cmd_nr_bvec(struct loop_cmd *cmd) +{ + struct req_iterator rq_iter; + struct request *rq = blk_mq_rq_from_pdu(cmd); + struct bio_vec tmp; + int nr_bvec = 0; + rq_for_each_bvec(tmp, rq, rq_iter) nr_bvec++; + return nr_bvec; +} + +static int lo_rw_aio_prep(struct loop_device *lo, struct loop_cmd *cmd, + unsigned nr_bvec) +{ + struct request *rq = blk_mq_rq_from_pdu(cmd); + struct file *file = lo->lo_backing_file; + if (rq->bio != rq->biotail) { + struct req_iterator rq_iter; + struct bio_vec *bvec; + struct bio_vec tmp; bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), GFP_NOIO); @@ -429,35 +468,23 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, loff_t pos) *bvec = tmp; bvec++; } - bvec = cmd->bvec; - offset = 0; - } else { - /* - * Same here, this bio may be started from the middle of the - * 'bvec' because of bio splitting, so offset from the bvec - * must be passed to iov iterator - */ - offset = bio->bi_iter.bi_bvec_done; - bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); } - atomic_set(&cmd->ref, 2); - - iov_iter_bvec(&iter, dir, bvec, nr_bvec, blk_rq_bytes(rq)); - iter.iov_offset = offset; - - cmd->iocb.ki_pos = pos; cmd->iocb.ki_filp = file; cmd->iocb.ki_complete = lo_rw_aio_complete; cmd->iocb.ki_flags = IOCB_DIRECT; cmd->iocb.ki_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0); - if (dir == ITER_SOURCE) - ret = file->f_op->write_iter(&cmd->iocb, &iter); - else - ret = file->f_op->read_iter(&cmd->iocb, &iter); + return 0; +} - lo_rw_aio_do_completion(cmd); +static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, loff_t pos) +{ + unsigned int nr_bvec = lo_cmd_nr_bvec(cmd); + int ret = lo_rw_aio_prep(lo, cmd, nr_bvec); + if (ret < 0) + return ret; + ret = lo_submit_rw_aio(lo, cmd, pos, nr_bvec); if (ret != -EIOCBQUEUED) lo_rw_aio_complete(&cmd->iocb, ret); return 0; -- 2.47.0