1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright (C) 2001 Ming Lei <ming.lei@canonical.com>
10 #include <linux/highmem.h>
11 #include <linux/bug.h>
12 #include <linux/errno.h>
13 #include <linux/limits.h>
14 #include <linux/minmax.h>
16 #include <linux/types.h>
21 * struct bio_vec - a contiguous range of physical memory addresses
22 * @bv_page: First page associated with the address range.
23 * @bv_len: Number of bytes in the address range.
24 * @bv_offset: Start of the address range relative to the start of @bv_page.
26 * The following holds for a bvec if n * PAGE_SIZE < bv_offset + bv_len:
28 * nth_page(@bv_page, n) == @bv_page + n
30 * This holds because page_is_mergeable() checks the above property.
35 unsigned int bv_offset;
39 sector_t bi_sector; /* device address in 512 byte
41 unsigned int bi_size; /* residual I/O count */
43 unsigned int bi_idx; /* current index into bvl_vec */
45 unsigned int bi_bvec_done; /* number of bytes completed in
49 struct bvec_iter_all {
56 * various member access, note that bio_data should of course not be used
57 * on highmem page vectors
59 #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
61 /* multi-page (mp_bvec) helpers */
62 #define mp_bvec_iter_page(bvec, iter) \
63 (__bvec_iter_bvec((bvec), (iter))->bv_page)
65 #define mp_bvec_iter_len(bvec, iter) \
67 __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
69 #define mp_bvec_iter_offset(bvec, iter) \
70 (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
72 #define mp_bvec_iter_page_idx(bvec, iter) \
73 (mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE)
75 #define mp_bvec_iter_bvec(bvec, iter) \
77 .bv_page = mp_bvec_iter_page((bvec), (iter)), \
78 .bv_len = mp_bvec_iter_len((bvec), (iter)), \
79 .bv_offset = mp_bvec_iter_offset((bvec), (iter)), \
82 /* For building single-page bvec in flight */
83 #define bvec_iter_offset(bvec, iter) \
84 (mp_bvec_iter_offset((bvec), (iter)) % PAGE_SIZE)
86 #define bvec_iter_len(bvec, iter) \
87 min_t(unsigned, mp_bvec_iter_len((bvec), (iter)), \
88 PAGE_SIZE - bvec_iter_offset((bvec), (iter)))
90 #define bvec_iter_page(bvec, iter) \
91 (mp_bvec_iter_page((bvec), (iter)) + \
92 mp_bvec_iter_page_idx((bvec), (iter)))
94 #define bvec_iter_bvec(bvec, iter) \
96 .bv_page = bvec_iter_page((bvec), (iter)), \
97 .bv_len = bvec_iter_len((bvec), (iter)), \
98 .bv_offset = bvec_iter_offset((bvec), (iter)), \
101 static inline bool bvec_iter_advance(const struct bio_vec *bv,
102 struct bvec_iter *iter, unsigned bytes)
104 unsigned int idx = iter->bi_idx;
106 if (WARN_ONCE(bytes > iter->bi_size,
107 "Attempted to advance past end of bvec iter\n")) {
112 iter->bi_size -= bytes;
113 bytes += iter->bi_bvec_done;
115 while (bytes && bytes >= bv[idx].bv_len) {
116 bytes -= bv[idx].bv_len;
121 iter->bi_bvec_done = bytes;
126 * A simpler version of bvec_iter_advance(), @bytes should not span
127 * across multiple bvec entries, i.e. bytes <= bv[i->bi_idx].bv_len
129 static inline void bvec_iter_advance_single(const struct bio_vec *bv,
130 struct bvec_iter *iter, unsigned int bytes)
132 unsigned int done = iter->bi_bvec_done + bytes;
134 if (done == bv[iter->bi_idx].bv_len) {
138 iter->bi_bvec_done = done;
139 iter->bi_size -= bytes;
142 #define for_each_bvec(bvl, bio_vec, iter, start) \
143 for (iter = (start); \
145 ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
146 bvec_iter_advance_single((bio_vec), &(iter), (bvl).bv_len))
148 /* for iterating one bio from start to end */
149 #define BVEC_ITER_ALL_INIT (struct bvec_iter) \
152 .bi_size = UINT_MAX, \
157 static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all)
162 return &iter_all->bv;
165 static inline void bvec_advance(const struct bio_vec *bvec,
166 struct bvec_iter_all *iter_all)
168 struct bio_vec *bv = &iter_all->bv;
170 if (iter_all->done) {
174 bv->bv_page = bvec->bv_page + (bvec->bv_offset >> PAGE_SHIFT);
175 bv->bv_offset = bvec->bv_offset & ~PAGE_MASK;
177 bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset,
178 bvec->bv_len - iter_all->done);
179 iter_all->done += bv->bv_len;
181 if (iter_all->done == bvec->bv_len) {
188 * bvec_kmap_local - map a bvec into the kernel virtual address space
191 * Must be called on single-page bvecs only. Call kunmap_local on the returned
194 static inline void *bvec_kmap_local(struct bio_vec *bvec)
196 return kmap_local_page(bvec->bv_page) + bvec->bv_offset;
200 * memcpy_from_bvec - copy data from a bvec
201 * @bvec: bvec to copy from
203 * Must be called on single-page bvecs only.
205 static inline void memcpy_from_bvec(char *to, struct bio_vec *bvec)
207 memcpy_from_page(to, bvec->bv_page, bvec->bv_offset, bvec->bv_len);
211 * memcpy_to_bvec - copy data to a bvec
212 * @bvec: bvec to copy to
214 * Must be called on single-page bvecs only.
216 static inline void memcpy_to_bvec(struct bio_vec *bvec, const char *from)
218 memcpy_to_page(bvec->bv_page, bvec->bv_offset, from, bvec->bv_len);
222 * memzero_bvec - zero all data in a bvec
223 * @bvec: bvec to zero
225 * Must be called on single-page bvecs only.
227 static inline void memzero_bvec(struct bio_vec *bvec)
229 memzero_page(bvec->bv_page, bvec->bv_offset, bvec->bv_len);
233 * bvec_virt - return the virtual address for a bvec
234 * @bvec: bvec to return the virtual address for
236 * Note: the caller must ensure that @bvec->bv_page is not a highmem page.
238 static inline void *bvec_virt(struct bio_vec *bvec)
240 WARN_ON_ONCE(PageHighMem(bvec->bv_page));
241 return page_address(bvec->bv_page) + bvec->bv_offset;
244 #endif /* __LINUX_BVEC_H */