18c16567dSChristoph Hellwig /* SPDX-License-Identifier: GPL-2.0 */
28fc55455SMing Lei /*
38fc55455SMing Lei * bvec iterator
48fc55455SMing Lei *
58fc55455SMing Lei * Copyright (C) 2001 Ming Lei <[email protected]>
68fc55455SMing Lei */
7e45cef51SChristoph Hellwig #ifndef __LINUX_BVEC_H
8e45cef51SChristoph Hellwig #define __LINUX_BVEC_H
98fc55455SMing Lei
10e6e74717SChristoph Hellwig #include <linux/highmem.h>
110781e79eSMing Lei #include <linux/bug.h>
12b1fb2c52SDmitry Monakhov #include <linux/errno.h>
13b296a6d5SAndy Shevchenko #include <linux/limits.h>
14b296a6d5SAndy Shevchenko #include <linux/minmax.h>
15b296a6d5SAndy Shevchenko #include <linux/types.h>
16b296a6d5SAndy Shevchenko
17b296a6d5SAndy Shevchenko struct page;
180781e79eSMing Lei
19854b5f01SBart Van Assche /**
20854b5f01SBart Van Assche * struct bio_vec - a contiguous range of physical memory addresses
21854b5f01SBart Van Assche * @bv_page: First page associated with the address range.
22854b5f01SBart Van Assche * @bv_len: Number of bytes in the address range.
23854b5f01SBart Van Assche * @bv_offset: Start of the address range relative to the start of @bv_page.
24854b5f01SBart Van Assche *
25854b5f01SBart Van Assche * The following holds for a bvec if n * PAGE_SIZE < bv_offset + bv_len:
26854b5f01SBart Van Assche *
27854b5f01SBart Van Assche * nth_page(@bv_page, n) == @bv_page + n
28854b5f01SBart Van Assche *
29854b5f01SBart Van Assche * This holds because page_is_mergeable() checks the above property.
300781e79eSMing Lei */
310781e79eSMing Lei struct bio_vec {
320781e79eSMing Lei struct page *bv_page;
330781e79eSMing Lei unsigned int bv_len;
340781e79eSMing Lei unsigned int bv_offset;
350781e79eSMing Lei };
360781e79eSMing Lei
37d58cdfaeSChristoph Hellwig /**
38d58cdfaeSChristoph Hellwig * bvec_set_page - initialize a bvec based off a struct page
39d58cdfaeSChristoph Hellwig * @bv: bvec to initialize
40d58cdfaeSChristoph Hellwig * @page: page the bvec should point to
41d58cdfaeSChristoph Hellwig * @len: length of the bvec
42d58cdfaeSChristoph Hellwig * @offset: offset into the page
43d58cdfaeSChristoph Hellwig */
bvec_set_page(struct bio_vec * bv,struct page * page,unsigned int len,unsigned int offset)44d58cdfaeSChristoph Hellwig static inline void bvec_set_page(struct bio_vec *bv, struct page *page,
45d58cdfaeSChristoph Hellwig unsigned int len, unsigned int offset)
46d58cdfaeSChristoph Hellwig {
47d58cdfaeSChristoph Hellwig bv->bv_page = page;
48d58cdfaeSChristoph Hellwig bv->bv_len = len;
49d58cdfaeSChristoph Hellwig bv->bv_offset = offset;
50d58cdfaeSChristoph Hellwig }
51d58cdfaeSChristoph Hellwig
5226db5ee1SChristoph Hellwig /**
5326db5ee1SChristoph Hellwig * bvec_set_folio - initialize a bvec based off a struct folio
5426db5ee1SChristoph Hellwig * @bv: bvec to initialize
5526db5ee1SChristoph Hellwig * @folio: folio the bvec should point to
5626db5ee1SChristoph Hellwig * @len: length of the bvec
5726db5ee1SChristoph Hellwig * @offset: offset into the folio
5826db5ee1SChristoph Hellwig */
bvec_set_folio(struct bio_vec * bv,struct folio * folio,unsigned int len,unsigned int offset)5926db5ee1SChristoph Hellwig static inline void bvec_set_folio(struct bio_vec *bv, struct folio *folio,
6026db5ee1SChristoph Hellwig unsigned int len, unsigned int offset)
6126db5ee1SChristoph Hellwig {
6226db5ee1SChristoph Hellwig bvec_set_page(bv, &folio->page, len, offset);
6326db5ee1SChristoph Hellwig }
6426db5ee1SChristoph Hellwig
65666e6550SChristoph Hellwig /**
66666e6550SChristoph Hellwig * bvec_set_virt - initialize a bvec based on a virtual address
67666e6550SChristoph Hellwig * @bv: bvec to initialize
68666e6550SChristoph Hellwig * @vaddr: virtual address to set the bvec to
69666e6550SChristoph Hellwig * @len: length of the bvec
70666e6550SChristoph Hellwig */
bvec_set_virt(struct bio_vec * bv,void * vaddr,unsigned int len)71666e6550SChristoph Hellwig static inline void bvec_set_virt(struct bio_vec *bv, void *vaddr,
72666e6550SChristoph Hellwig unsigned int len)
73666e6550SChristoph Hellwig {
74666e6550SChristoph Hellwig bvec_set_page(bv, virt_to_page(vaddr), len, offset_in_page(vaddr));
75666e6550SChristoph Hellwig }
76666e6550SChristoph Hellwig
770781e79eSMing Lei struct bvec_iter {
780781e79eSMing Lei sector_t bi_sector; /* device address in 512 byte
790781e79eSMing Lei sectors */
800781e79eSMing Lei unsigned int bi_size; /* residual I/O count */
810781e79eSMing Lei
820781e79eSMing Lei unsigned int bi_idx; /* current index into bvl_vec */
830781e79eSMing Lei
840781e79eSMing Lei unsigned int bi_bvec_done; /* number of bytes completed in
850781e79eSMing Lei current bvec */
867838b465SMing Lei } __packed __aligned(4);
878fc55455SMing Lei
886dc4f100SMing Lei struct bvec_iter_all {
896dc4f100SMing Lei struct bio_vec bv;
906dc4f100SMing Lei int idx;
916dc4f100SMing Lei unsigned done;
926dc4f100SMing Lei };
936dc4f100SMing Lei
948fc55455SMing Lei /*
958fc55455SMing Lei * various member access, note that bio_data should of course not be used
968fc55455SMing Lei * on highmem page vectors
978fc55455SMing Lei */
988fc55455SMing Lei #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
998fc55455SMing Lei
1003d75ca0aSMing Lei /* multi-page (mp_bvec) helpers */
1013d75ca0aSMing Lei #define mp_bvec_iter_page(bvec, iter) \
1028fc55455SMing Lei (__bvec_iter_bvec((bvec), (iter))->bv_page)
1038fc55455SMing Lei
1043d75ca0aSMing Lei #define mp_bvec_iter_len(bvec, iter) \
1058fc55455SMing Lei min((iter).bi_size, \
1068fc55455SMing Lei __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
1078fc55455SMing Lei
1083d75ca0aSMing Lei #define mp_bvec_iter_offset(bvec, iter) \
1098fc55455SMing Lei (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
1108fc55455SMing Lei
1113d75ca0aSMing Lei #define mp_bvec_iter_page_idx(bvec, iter) \
1123d75ca0aSMing Lei (mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE)
1133d75ca0aSMing Lei
1143d75ca0aSMing Lei #define mp_bvec_iter_bvec(bvec, iter) \
1153d75ca0aSMing Lei ((struct bio_vec) { \
1163d75ca0aSMing Lei .bv_page = mp_bvec_iter_page((bvec), (iter)), \
1173d75ca0aSMing Lei .bv_len = mp_bvec_iter_len((bvec), (iter)), \
1183d75ca0aSMing Lei .bv_offset = mp_bvec_iter_offset((bvec), (iter)), \
1193d75ca0aSMing Lei })
1203d75ca0aSMing Lei
1213d75ca0aSMing Lei /* For building single-page bvec in flight */
1223d75ca0aSMing Lei #define bvec_iter_offset(bvec, iter) \
1233d75ca0aSMing Lei (mp_bvec_iter_offset((bvec), (iter)) % PAGE_SIZE)
1243d75ca0aSMing Lei
1253d75ca0aSMing Lei #define bvec_iter_len(bvec, iter) \
1263d75ca0aSMing Lei min_t(unsigned, mp_bvec_iter_len((bvec), (iter)), \
1273d75ca0aSMing Lei PAGE_SIZE - bvec_iter_offset((bvec), (iter)))
1283d75ca0aSMing Lei
1293d75ca0aSMing Lei #define bvec_iter_page(bvec, iter) \
13052d52d1cSChristoph Hellwig (mp_bvec_iter_page((bvec), (iter)) + \
1313d75ca0aSMing Lei mp_bvec_iter_page_idx((bvec), (iter)))
1323d75ca0aSMing Lei
1338fc55455SMing Lei #define bvec_iter_bvec(bvec, iter) \
1348fc55455SMing Lei ((struct bio_vec) { \
1358fc55455SMing Lei .bv_page = bvec_iter_page((bvec), (iter)), \
1368fc55455SMing Lei .bv_len = bvec_iter_len((bvec), (iter)), \
1378fc55455SMing Lei .bv_offset = bvec_iter_offset((bvec), (iter)), \
1388fc55455SMing Lei })
1398fc55455SMing Lei
bvec_iter_advance(const struct bio_vec * bv,struct bvec_iter * iter,unsigned bytes)140b1fb2c52SDmitry Monakhov static inline bool bvec_iter_advance(const struct bio_vec *bv,
141b1fb2c52SDmitry Monakhov struct bvec_iter *iter, unsigned bytes)
1428fc55455SMing Lei {
143795ee49cSPavel Begunkov unsigned int idx = iter->bi_idx;
144795ee49cSPavel Begunkov
145b1fb2c52SDmitry Monakhov if (WARN_ONCE(bytes > iter->bi_size,
146b1fb2c52SDmitry Monakhov "Attempted to advance past end of bvec iter\n")) {
147b1fb2c52SDmitry Monakhov iter->bi_size = 0;
148b1fb2c52SDmitry Monakhov return false;
149b1fb2c52SDmitry Monakhov }
1508fc55455SMing Lei
151795ee49cSPavel Begunkov iter->bi_size -= bytes;
152795ee49cSPavel Begunkov bytes += iter->bi_bvec_done;
1538fc55455SMing Lei
154795ee49cSPavel Begunkov while (bytes && bytes >= bv[idx].bv_len) {
155795ee49cSPavel Begunkov bytes -= bv[idx].bv_len;
156795ee49cSPavel Begunkov idx++;
157795ee49cSPavel Begunkov }
1588fc55455SMing Lei
159795ee49cSPavel Begunkov iter->bi_idx = idx;
160795ee49cSPavel Begunkov iter->bi_bvec_done = bytes;
161b1fb2c52SDmitry Monakhov return true;
1628fc55455SMing Lei }
1638fc55455SMing Lei
1646b6667aaSPavel Begunkov /*
1656b6667aaSPavel Begunkov * A simpler version of bvec_iter_advance(), @bytes should not span
1666b6667aaSPavel Begunkov * across multiple bvec entries, i.e. bytes <= bv[i->bi_idx].bv_len
1676b6667aaSPavel Begunkov */
bvec_iter_advance_single(const struct bio_vec * bv,struct bvec_iter * iter,unsigned int bytes)1686b6667aaSPavel Begunkov static inline void bvec_iter_advance_single(const struct bio_vec *bv,
1696b6667aaSPavel Begunkov struct bvec_iter *iter, unsigned int bytes)
1707e249690SMing Lei {
1716b6667aaSPavel Begunkov unsigned int done = iter->bi_bvec_done + bytes;
1726b6667aaSPavel Begunkov
1736b6667aaSPavel Begunkov if (done == bv[iter->bi_idx].bv_len) {
1746b6667aaSPavel Begunkov done = 0;
1757e249690SMing Lei iter->bi_idx++;
1767e249690SMing Lei }
1776b6667aaSPavel Begunkov iter->bi_bvec_done = done;
1786b6667aaSPavel Begunkov iter->bi_size -= bytes;
1796b6667aaSPavel Begunkov }
1807e249690SMing Lei
1818fc55455SMing Lei #define for_each_bvec(bvl, bio_vec, iter, start) \
1828fc55455SMing Lei for (iter = (start); \
1838fc55455SMing Lei (iter).bi_size && \
1848fc55455SMing Lei ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
1856b6667aaSPavel Begunkov bvec_iter_advance_single((bio_vec), &(iter), (bvl).bv_len))
1868fc55455SMing Lei
187*149974fdSMing Lei #define for_each_mp_bvec(bvl, bio_vec, iter, start) \
188*149974fdSMing Lei for (iter = (start); \
189*149974fdSMing Lei (iter).bi_size && \
190*149974fdSMing Lei ((bvl = mp_bvec_iter_bvec((bio_vec), (iter))), 1); \
191*149974fdSMing Lei bvec_iter_advance_single((bio_vec), &(iter), (bvl).bv_len))
192*149974fdSMing Lei
1933c892a09SMing Lei /* for iterating one bio from start to end */
1943c892a09SMing Lei #define BVEC_ITER_ALL_INIT (struct bvec_iter) \
1953c892a09SMing Lei { \
1963c892a09SMing Lei .bi_sector = 0, \
1973c892a09SMing Lei .bi_size = UINT_MAX, \
1983c892a09SMing Lei .bi_idx = 0, \
1993c892a09SMing Lei .bi_bvec_done = 0, \
2003c892a09SMing Lei }
2013c892a09SMing Lei
bvec_init_iter_all(struct bvec_iter_all * iter_all)2026dc4f100SMing Lei static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all)
2036dc4f100SMing Lei {
2046dc4f100SMing Lei iter_all->done = 0;
2051200e07fSMing Lei iter_all->idx = 0;
2066dc4f100SMing Lei
2076dc4f100SMing Lei return &iter_all->bv;
2086dc4f100SMing Lei }
2096dc4f100SMing Lei
bvec_advance(const struct bio_vec * bvec,struct bvec_iter_all * iter_all)2101200e07fSMing Lei static inline void bvec_advance(const struct bio_vec *bvec,
2116dc4f100SMing Lei struct bvec_iter_all *iter_all)
2126dc4f100SMing Lei {
2136dc4f100SMing Lei struct bio_vec *bv = &iter_all->bv;
2146dc4f100SMing Lei
2151200e07fSMing Lei if (iter_all->done) {
21652d52d1cSChristoph Hellwig bv->bv_page++;
2176dc4f100SMing Lei bv->bv_offset = 0;
2186dc4f100SMing Lei } else {
219b8753433SChristoph Hellwig bv->bv_page = bvec->bv_page + (bvec->bv_offset >> PAGE_SHIFT);
2206bedf00eSMing Lei bv->bv_offset = bvec->bv_offset & ~PAGE_MASK;
2216dc4f100SMing Lei }
2226dc4f100SMing Lei bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset,
2236dc4f100SMing Lei bvec->bv_len - iter_all->done);
2241200e07fSMing Lei iter_all->done += bv->bv_len;
2251200e07fSMing Lei
2261200e07fSMing Lei if (iter_all->done == bvec->bv_len) {
2271200e07fSMing Lei iter_all->idx++;
2281200e07fSMing Lei iter_all->done = 0;
2291200e07fSMing Lei }
2306dc4f100SMing Lei }
2316dc4f100SMing Lei
232e6e74717SChristoph Hellwig /**
233e6e74717SChristoph Hellwig * bvec_kmap_local - map a bvec into the kernel virtual address space
234e6e74717SChristoph Hellwig * @bvec: bvec to map
235e6e74717SChristoph Hellwig *
236e6e74717SChristoph Hellwig * Must be called on single-page bvecs only. Call kunmap_local on the returned
237e6e74717SChristoph Hellwig * address to unmap.
238e6e74717SChristoph Hellwig */
bvec_kmap_local(struct bio_vec * bvec)239e6e74717SChristoph Hellwig static inline void *bvec_kmap_local(struct bio_vec *bvec)
240e6e74717SChristoph Hellwig {
241e6e74717SChristoph Hellwig return kmap_local_page(bvec->bv_page) + bvec->bv_offset;
242e6e74717SChristoph Hellwig }
243e6e74717SChristoph Hellwig
244f93a181aSChristoph Hellwig /**
245f93a181aSChristoph Hellwig * memcpy_from_bvec - copy data from a bvec
246f93a181aSChristoph Hellwig * @bvec: bvec to copy from
247f93a181aSChristoph Hellwig *
248f93a181aSChristoph Hellwig * Must be called on single-page bvecs only.
249f93a181aSChristoph Hellwig */
memcpy_from_bvec(char * to,struct bio_vec * bvec)250f93a181aSChristoph Hellwig static inline void memcpy_from_bvec(char *to, struct bio_vec *bvec)
251f93a181aSChristoph Hellwig {
252f93a181aSChristoph Hellwig memcpy_from_page(to, bvec->bv_page, bvec->bv_offset, bvec->bv_len);
253f93a181aSChristoph Hellwig }
254f93a181aSChristoph Hellwig
255f93a181aSChristoph Hellwig /**
256f93a181aSChristoph Hellwig * memcpy_to_bvec - copy data to a bvec
257f93a181aSChristoph Hellwig * @bvec: bvec to copy to
258f93a181aSChristoph Hellwig *
259f93a181aSChristoph Hellwig * Must be called on single-page bvecs only.
260f93a181aSChristoph Hellwig */
memcpy_to_bvec(struct bio_vec * bvec,const char * from)261f93a181aSChristoph Hellwig static inline void memcpy_to_bvec(struct bio_vec *bvec, const char *from)
262f93a181aSChristoph Hellwig {
263f93a181aSChristoph Hellwig memcpy_to_page(bvec->bv_page, bvec->bv_offset, from, bvec->bv_len);
264f93a181aSChristoph Hellwig }
265f93a181aSChristoph Hellwig
266f93a181aSChristoph Hellwig /**
267f93a181aSChristoph Hellwig * memzero_bvec - zero all data in a bvec
268f93a181aSChristoph Hellwig * @bvec: bvec to zero
269f93a181aSChristoph Hellwig *
270f93a181aSChristoph Hellwig * Must be called on single-page bvecs only.
271f93a181aSChristoph Hellwig */
memzero_bvec(struct bio_vec * bvec)272f93a181aSChristoph Hellwig static inline void memzero_bvec(struct bio_vec *bvec)
273f93a181aSChristoph Hellwig {
274f93a181aSChristoph Hellwig memzero_page(bvec->bv_page, bvec->bv_offset, bvec->bv_len);
275f93a181aSChristoph Hellwig }
276f93a181aSChristoph Hellwig
2771113f0b6SChristoph Hellwig /**
2781113f0b6SChristoph Hellwig * bvec_virt - return the virtual address for a bvec
2791113f0b6SChristoph Hellwig * @bvec: bvec to return the virtual address for
2801113f0b6SChristoph Hellwig *
2811113f0b6SChristoph Hellwig * Note: the caller must ensure that @bvec->bv_page is not a highmem page.
2821113f0b6SChristoph Hellwig */
bvec_virt(struct bio_vec * bvec)2831113f0b6SChristoph Hellwig static inline void *bvec_virt(struct bio_vec *bvec)
2841113f0b6SChristoph Hellwig {
2851113f0b6SChristoph Hellwig WARN_ON_ONCE(PageHighMem(bvec->bv_page));
2861113f0b6SChristoph Hellwig return page_address(bvec->bv_page) + bvec->bv_offset;
2871113f0b6SChristoph Hellwig }
2881113f0b6SChristoph Hellwig
28925f76c3dSChristoph Hellwig /**
29025f76c3dSChristoph Hellwig * bvec_phys - return the physical address for a bvec
29125f76c3dSChristoph Hellwig * @bvec: bvec to return the physical address for
29225f76c3dSChristoph Hellwig */
bvec_phys(const struct bio_vec * bvec)29325f76c3dSChristoph Hellwig static inline phys_addr_t bvec_phys(const struct bio_vec *bvec)
29425f76c3dSChristoph Hellwig {
2952caca8fcSChristoph Hellwig return page_to_phys(bvec->bv_page) + bvec->bv_offset;
29625f76c3dSChristoph Hellwig }
29725f76c3dSChristoph Hellwig
298e45cef51SChristoph Hellwig #endif /* __LINUX_BVEC_H */
299