2019-04-30 21:42:39 +03:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2016-06-09 19:00:58 +03:00
|
|
|
/*
|
|
|
|
* bvec iterator
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 Ming Lei <ming.lei@canonical.com>
|
|
|
|
*/
|
|
|
|
#ifndef __LINUX_BVEC_ITER_H
|
|
|
|
#define __LINUX_BVEC_ITER_H
|
|
|
|
|
2016-05-30 16:34:30 +03:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/bug.h>
|
2017-06-29 21:31:13 +03:00
|
|
|
#include <linux/errno.h>
|
block: introduce multi-page bvec helpers
This patch introduces helpers of 'mp_bvec_iter_*' for multi-page bvec
support.
The introduced helpers treate one bvec as real multi-page segment,
which may include more than one pages.
The existed helpers of bvec_iter_* are interfaces for supporting current
bvec iterator which is thought as single-page by drivers, fs, dm and
etc. These introduced helpers will build single-page bvec in flight, so
this way won't break current bio/bvec users, which needn't any change.
Follows some multi-page bvec background:
- bvecs stored in bio->bi_io_vec is always multi-page style
- bvec(struct bio_vec) represents one physically contiguous I/O
buffer, now the buffer may include more than one page after
multi-page bvec is supported, and all these pages represented
by one bvec is physically contiguous. Before multi-page bvec
support, at most one page is included in one bvec, we call it
single-page bvec.
- .bv_page of the bvec points to the 1st page in the multi-page bvec
- .bv_offset of the bvec is the offset of the buffer in the bvec
The effect on the current drivers/filesystem/dm/bcache/...:
- almost everyone supposes that one bvec only includes one single
page, so we keep the sp interface not changed, for example,
bio_for_each_segment() still returns single-page bvec
- bio_for_each_segment_all() will return single-page bvec too
- during iterating, iterator variable(struct bvec_iter) is always
updated in multi-page bvec style, and bvec_iter_advance() is kept
not changed
- returned(copied) single-page bvec is built in flight by bvec
helpers from the stored multi-page bvec
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-02-15 14:13:10 +03:00
|
|
|
#include <linux/mm.h>
|
2016-05-30 16:34:30 +03:00
|
|
|
|
2020-05-19 07:07:36 +03:00
|
|
|
/**
|
|
|
|
* struct bio_vec - a contiguous range of physical memory addresses
|
|
|
|
* @bv_page: First page associated with the address range.
|
|
|
|
* @bv_len: Number of bytes in the address range.
|
|
|
|
* @bv_offset: Start of the address range relative to the start of @bv_page.
|
|
|
|
*
|
|
|
|
* The following holds for a bvec if n * PAGE_SIZE < bv_offset + bv_len:
|
|
|
|
*
|
|
|
|
* nth_page(@bv_page, n) == @bv_page + n
|
|
|
|
*
|
|
|
|
* This holds because page_is_mergeable() checks the above property.
|
2016-05-30 16:34:30 +03:00
|
|
|
*/
|
|
|
|
struct bio_vec {
|
|
|
|
struct page *bv_page;
|
|
|
|
unsigned int bv_len;
|
|
|
|
unsigned int bv_offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bvec_iter {
|
|
|
|
sector_t bi_sector; /* device address in 512 byte
|
|
|
|
sectors */
|
|
|
|
unsigned int bi_size; /* residual I/O count */
|
|
|
|
|
|
|
|
unsigned int bi_idx; /* current index into bvl_vec */
|
|
|
|
|
|
|
|
unsigned int bi_bvec_done; /* number of bytes completed in
|
|
|
|
current bvec */
|
|
|
|
};
|
2016-06-09 19:00:58 +03:00
|
|
|
|
2019-02-15 14:13:19 +03:00
|
|
|
struct bvec_iter_all {
|
|
|
|
struct bio_vec bv;
|
|
|
|
int idx;
|
|
|
|
unsigned done;
|
|
|
|
};
|
|
|
|
|
2016-06-09 19:00:58 +03:00
|
|
|
/*
|
|
|
|
* various member access, note that bio_data should of course not be used
|
|
|
|
* on highmem page vectors
|
|
|
|
*/
|
|
|
|
#define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
|
|
|
|
|
block: introduce multi-page bvec helpers
This patch introduces helpers of 'mp_bvec_iter_*' for multi-page bvec
support.
The introduced helpers treate one bvec as real multi-page segment,
which may include more than one pages.
The existed helpers of bvec_iter_* are interfaces for supporting current
bvec iterator which is thought as single-page by drivers, fs, dm and
etc. These introduced helpers will build single-page bvec in flight, so
this way won't break current bio/bvec users, which needn't any change.
Follows some multi-page bvec background:
- bvecs stored in bio->bi_io_vec is always multi-page style
- bvec(struct bio_vec) represents one physically contiguous I/O
buffer, now the buffer may include more than one page after
multi-page bvec is supported, and all these pages represented
by one bvec is physically contiguous. Before multi-page bvec
support, at most one page is included in one bvec, we call it
single-page bvec.
- .bv_page of the bvec points to the 1st page in the multi-page bvec
- .bv_offset of the bvec is the offset of the buffer in the bvec
The effect on the current drivers/filesystem/dm/bcache/...:
- almost everyone supposes that one bvec only includes one single
page, so we keep the sp interface not changed, for example,
bio_for_each_segment() still returns single-page bvec
- bio_for_each_segment_all() will return single-page bvec too
- during iterating, iterator variable(struct bvec_iter) is always
updated in multi-page bvec style, and bvec_iter_advance() is kept
not changed
- returned(copied) single-page bvec is built in flight by bvec
helpers from the stored multi-page bvec
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-02-15 14:13:10 +03:00
|
|
|
/* multi-page (mp_bvec) helpers */
|
|
|
|
#define mp_bvec_iter_page(bvec, iter) \
|
2016-06-09 19:00:58 +03:00
|
|
|
(__bvec_iter_bvec((bvec), (iter))->bv_page)
|
|
|
|
|
block: introduce multi-page bvec helpers
This patch introduces helpers of 'mp_bvec_iter_*' for multi-page bvec
support.
The introduced helpers treate one bvec as real multi-page segment,
which may include more than one pages.
The existed helpers of bvec_iter_* are interfaces for supporting current
bvec iterator which is thought as single-page by drivers, fs, dm and
etc. These introduced helpers will build single-page bvec in flight, so
this way won't break current bio/bvec users, which needn't any change.
Follows some multi-page bvec background:
- bvecs stored in bio->bi_io_vec is always multi-page style
- bvec(struct bio_vec) represents one physically contiguous I/O
buffer, now the buffer may include more than one page after
multi-page bvec is supported, and all these pages represented
by one bvec is physically contiguous. Before multi-page bvec
support, at most one page is included in one bvec, we call it
single-page bvec.
- .bv_page of the bvec points to the 1st page in the multi-page bvec
- .bv_offset of the bvec is the offset of the buffer in the bvec
The effect on the current drivers/filesystem/dm/bcache/...:
- almost everyone supposes that one bvec only includes one single
page, so we keep the sp interface not changed, for example,
bio_for_each_segment() still returns single-page bvec
- bio_for_each_segment_all() will return single-page bvec too
- during iterating, iterator variable(struct bvec_iter) is always
updated in multi-page bvec style, and bvec_iter_advance() is kept
not changed
- returned(copied) single-page bvec is built in flight by bvec
helpers from the stored multi-page bvec
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-02-15 14:13:10 +03:00
|
|
|
#define mp_bvec_iter_len(bvec, iter) \
|
2016-06-09 19:00:58 +03:00
|
|
|
min((iter).bi_size, \
|
|
|
|
__bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
|
|
|
|
|
block: introduce multi-page bvec helpers
This patch introduces helpers of 'mp_bvec_iter_*' for multi-page bvec
support.
The introduced helpers treate one bvec as real multi-page segment,
which may include more than one pages.
The existed helpers of bvec_iter_* are interfaces for supporting current
bvec iterator which is thought as single-page by drivers, fs, dm and
etc. These introduced helpers will build single-page bvec in flight, so
this way won't break current bio/bvec users, which needn't any change.
Follows some multi-page bvec background:
- bvecs stored in bio->bi_io_vec is always multi-page style
- bvec(struct bio_vec) represents one physically contiguous I/O
buffer, now the buffer may include more than one page after
multi-page bvec is supported, and all these pages represented
by one bvec is physically contiguous. Before multi-page bvec
support, at most one page is included in one bvec, we call it
single-page bvec.
- .bv_page of the bvec points to the 1st page in the multi-page bvec
- .bv_offset of the bvec is the offset of the buffer in the bvec
The effect on the current drivers/filesystem/dm/bcache/...:
- almost everyone supposes that one bvec only includes one single
page, so we keep the sp interface not changed, for example,
bio_for_each_segment() still returns single-page bvec
- bio_for_each_segment_all() will return single-page bvec too
- during iterating, iterator variable(struct bvec_iter) is always
updated in multi-page bvec style, and bvec_iter_advance() is kept
not changed
- returned(copied) single-page bvec is built in flight by bvec
helpers from the stored multi-page bvec
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-02-15 14:13:10 +03:00
|
|
|
#define mp_bvec_iter_offset(bvec, iter) \
|
2016-06-09 19:00:58 +03:00
|
|
|
(__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
|
|
|
|
|
block: introduce multi-page bvec helpers
This patch introduces helpers of 'mp_bvec_iter_*' for multi-page bvec
support.
The introduced helpers treate one bvec as real multi-page segment,
which may include more than one pages.
The existed helpers of bvec_iter_* are interfaces for supporting current
bvec iterator which is thought as single-page by drivers, fs, dm and
etc. These introduced helpers will build single-page bvec in flight, so
this way won't break current bio/bvec users, which needn't any change.
Follows some multi-page bvec background:
- bvecs stored in bio->bi_io_vec is always multi-page style
- bvec(struct bio_vec) represents one physically contiguous I/O
buffer, now the buffer may include more than one page after
multi-page bvec is supported, and all these pages represented
by one bvec is physically contiguous. Before multi-page bvec
support, at most one page is included in one bvec, we call it
single-page bvec.
- .bv_page of the bvec points to the 1st page in the multi-page bvec
- .bv_offset of the bvec is the offset of the buffer in the bvec
The effect on the current drivers/filesystem/dm/bcache/...:
- almost everyone supposes that one bvec only includes one single
page, so we keep the sp interface not changed, for example,
bio_for_each_segment() still returns single-page bvec
- bio_for_each_segment_all() will return single-page bvec too
- during iterating, iterator variable(struct bvec_iter) is always
updated in multi-page bvec style, and bvec_iter_advance() is kept
not changed
- returned(copied) single-page bvec is built in flight by bvec
helpers from the stored multi-page bvec
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-02-15 14:13:10 +03:00
|
|
|
#define mp_bvec_iter_page_idx(bvec, iter) \
|
|
|
|
(mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE)
|
|
|
|
|
|
|
|
#define mp_bvec_iter_bvec(bvec, iter) \
|
|
|
|
((struct bio_vec) { \
|
|
|
|
.bv_page = mp_bvec_iter_page((bvec), (iter)), \
|
|
|
|
.bv_len = mp_bvec_iter_len((bvec), (iter)), \
|
|
|
|
.bv_offset = mp_bvec_iter_offset((bvec), (iter)), \
|
|
|
|
})
|
|
|
|
|
|
|
|
/* For building single-page bvec in flight */
|
|
|
|
#define bvec_iter_offset(bvec, iter) \
|
|
|
|
(mp_bvec_iter_offset((bvec), (iter)) % PAGE_SIZE)
|
|
|
|
|
|
|
|
#define bvec_iter_len(bvec, iter) \
|
|
|
|
min_t(unsigned, mp_bvec_iter_len((bvec), (iter)), \
|
|
|
|
PAGE_SIZE - bvec_iter_offset((bvec), (iter)))
|
|
|
|
|
|
|
|
#define bvec_iter_page(bvec, iter) \
|
2019-04-11 09:23:31 +03:00
|
|
|
(mp_bvec_iter_page((bvec), (iter)) + \
|
|
|
|
mp_bvec_iter_page_idx((bvec), (iter)))
|
block: introduce multi-page bvec helpers
This patch introduces helpers of 'mp_bvec_iter_*' for multi-page bvec
support.
The introduced helpers treate one bvec as real multi-page segment,
which may include more than one pages.
The existed helpers of bvec_iter_* are interfaces for supporting current
bvec iterator which is thought as single-page by drivers, fs, dm and
etc. These introduced helpers will build single-page bvec in flight, so
this way won't break current bio/bvec users, which needn't any change.
Follows some multi-page bvec background:
- bvecs stored in bio->bi_io_vec is always multi-page style
- bvec(struct bio_vec) represents one physically contiguous I/O
buffer, now the buffer may include more than one page after
multi-page bvec is supported, and all these pages represented
by one bvec is physically contiguous. Before multi-page bvec
support, at most one page is included in one bvec, we call it
single-page bvec.
- .bv_page of the bvec points to the 1st page in the multi-page bvec
- .bv_offset of the bvec is the offset of the buffer in the bvec
The effect on the current drivers/filesystem/dm/bcache/...:
- almost everyone supposes that one bvec only includes one single
page, so we keep the sp interface not changed, for example,
bio_for_each_segment() still returns single-page bvec
- bio_for_each_segment_all() will return single-page bvec too
- during iterating, iterator variable(struct bvec_iter) is always
updated in multi-page bvec style, and bvec_iter_advance() is kept
not changed
- returned(copied) single-page bvec is built in flight by bvec
helpers from the stored multi-page bvec
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Omar Sandoval <osandov@fb.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2019-02-15 14:13:10 +03:00
|
|
|
|
2016-06-09 19:00:58 +03:00
|
|
|
#define bvec_iter_bvec(bvec, iter) \
|
|
|
|
((struct bio_vec) { \
|
|
|
|
.bv_page = bvec_iter_page((bvec), (iter)), \
|
|
|
|
.bv_len = bvec_iter_len((bvec), (iter)), \
|
|
|
|
.bv_offset = bvec_iter_offset((bvec), (iter)), \
|
|
|
|
})
|
|
|
|
|
2017-06-29 21:31:13 +03:00
|
|
|
static inline bool bvec_iter_advance(const struct bio_vec *bv,
|
|
|
|
struct bvec_iter *iter, unsigned bytes)
|
2016-06-09 19:00:58 +03:00
|
|
|
{
|
2019-11-30 23:23:52 +03:00
|
|
|
unsigned int idx = iter->bi_idx;
|
|
|
|
|
2017-06-29 21:31:13 +03:00
|
|
|
if (WARN_ONCE(bytes > iter->bi_size,
|
|
|
|
"Attempted to advance past end of bvec iter\n")) {
|
|
|
|
iter->bi_size = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-09 19:00:58 +03:00
|
|
|
|
2019-11-30 23:23:52 +03:00
|
|
|
iter->bi_size -= bytes;
|
|
|
|
bytes += iter->bi_bvec_done;
|
2016-06-09 19:00:58 +03:00
|
|
|
|
2019-11-30 23:23:52 +03:00
|
|
|
while (bytes && bytes >= bv[idx].bv_len) {
|
|
|
|
bytes -= bv[idx].bv_len;
|
|
|
|
idx++;
|
2016-06-09 19:00:58 +03:00
|
|
|
}
|
2019-11-30 23:23:52 +03:00
|
|
|
|
|
|
|
iter->bi_idx = idx;
|
|
|
|
iter->bi_bvec_done = bytes;
|
2017-06-29 21:31:13 +03:00
|
|
|
return true;
|
2016-06-09 19:00:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define for_each_bvec(bvl, bio_vec, iter, start) \
|
|
|
|
for (iter = (start); \
|
|
|
|
(iter).bi_size && \
|
|
|
|
((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
|
|
|
|
bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len))
|
|
|
|
|
2017-12-18 15:22:07 +03:00
|
|
|
/* for iterating one bio from start to end */
|
|
|
|
#define BVEC_ITER_ALL_INIT (struct bvec_iter) \
|
|
|
|
{ \
|
|
|
|
.bi_sector = 0, \
|
|
|
|
.bi_size = UINT_MAX, \
|
|
|
|
.bi_idx = 0, \
|
|
|
|
.bi_bvec_done = 0, \
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:13:19 +03:00
|
|
|
static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all)
|
|
|
|
{
|
|
|
|
iter_all->done = 0;
|
2019-04-08 14:02:38 +03:00
|
|
|
iter_all->idx = 0;
|
2019-02-15 14:13:19 +03:00
|
|
|
|
|
|
|
return &iter_all->bv;
|
|
|
|
}
|
|
|
|
|
2019-04-08 14:02:38 +03:00
|
|
|
static inline void bvec_advance(const struct bio_vec *bvec,
|
|
|
|
struct bvec_iter_all *iter_all)
|
2019-02-15 14:13:19 +03:00
|
|
|
{
|
|
|
|
struct bio_vec *bv = &iter_all->bv;
|
|
|
|
|
2019-04-08 14:02:38 +03:00
|
|
|
if (iter_all->done) {
|
2019-04-11 09:23:31 +03:00
|
|
|
bv->bv_page++;
|
2019-02-15 14:13:19 +03:00
|
|
|
bv->bv_offset = 0;
|
|
|
|
} else {
|
2019-05-07 09:53:35 +03:00
|
|
|
bv->bv_page = bvec->bv_page + (bvec->bv_offset >> PAGE_SHIFT);
|
2019-04-17 04:11:26 +03:00
|
|
|
bv->bv_offset = bvec->bv_offset & ~PAGE_MASK;
|
2019-02-15 14:13:19 +03:00
|
|
|
}
|
|
|
|
bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset,
|
|
|
|
bvec->bv_len - iter_all->done);
|
2019-04-08 14:02:38 +03:00
|
|
|
iter_all->done += bv->bv_len;
|
|
|
|
|
|
|
|
if (iter_all->done == bvec->bv_len) {
|
|
|
|
iter_all->idx++;
|
|
|
|
iter_all->done = 0;
|
|
|
|
}
|
2019-02-15 14:13:19 +03:00
|
|
|
}
|
|
|
|
|
2016-06-09 19:00:58 +03:00
|
|
|
#endif /* __LINUX_BVEC_ITER_H */
|