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>
15 #include <linux/types.h>
20 * struct bio_vec - a contiguous range of physical memory addresses
21 * @bv_page: First page associated with the address range.
22 * @bv_len: Number of bytes in the address range.
23 * @bv_offset: Start of the address range relative to the start of @bv_page.
25 * The following holds for a bvec if n * PAGE_SIZE < bv_offset + bv_len:
27 * nth_page(@bv_page, n) == @bv_page + n
29 * This holds because page_is_mergeable() checks the above property.
34 unsigned int bv_offset
;
38 * bvec_set_page - initialize a bvec based off a struct page
39 * @bv: bvec to initialize
40 * @page: page the bvec should point to
41 * @len: length of the bvec
42 * @offset: offset into the page
44 static inline void bvec_set_page(struct bio_vec
*bv
, struct page
*page
,
45 unsigned int len
, unsigned int offset
)
49 bv
->bv_offset
= offset
;
53 * bvec_set_folio - initialize a bvec based off a struct folio
54 * @bv: bvec to initialize
55 * @folio: folio the bvec should point to
56 * @len: length of the bvec
57 * @offset: offset into the folio
59 static inline void bvec_set_folio(struct bio_vec
*bv
, struct folio
*folio
,
60 unsigned int len
, unsigned int offset
)
62 bvec_set_page(bv
, &folio
->page
, len
, offset
);
66 * bvec_set_virt - initialize a bvec based on a virtual address
67 * @bv: bvec to initialize
68 * @vaddr: virtual address to set the bvec to
69 * @len: length of the bvec
71 static inline void bvec_set_virt(struct bio_vec
*bv
, void *vaddr
,
74 bvec_set_page(bv
, virt_to_page(vaddr
), len
, offset_in_page(vaddr
));
78 sector_t bi_sector
; /* device address in 512 byte
80 unsigned int bi_size
; /* residual I/O count */
82 unsigned int bi_idx
; /* current index into bvl_vec */
84 unsigned int bi_bvec_done
; /* number of bytes completed in
86 } __packed
__aligned(4);
88 struct bvec_iter_all
{
95 * various member access, note that bio_data should of course not be used
96 * on highmem page vectors
98 #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
100 /* multi-page (mp_bvec) helpers */
101 #define mp_bvec_iter_page(bvec, iter) \
102 (__bvec_iter_bvec((bvec), (iter))->bv_page)
104 #define mp_bvec_iter_len(bvec, iter) \
105 min((iter).bi_size, \
106 __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
108 #define mp_bvec_iter_offset(bvec, iter) \
109 (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
111 #define mp_bvec_iter_page_idx(bvec, iter) \
112 (mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE)
114 #define mp_bvec_iter_bvec(bvec, iter) \
115 ((struct bio_vec) { \
116 .bv_page = mp_bvec_iter_page((bvec), (iter)), \
117 .bv_len = mp_bvec_iter_len((bvec), (iter)), \
118 .bv_offset = mp_bvec_iter_offset((bvec), (iter)), \
121 /* For building single-page bvec in flight */
122 #define bvec_iter_offset(bvec, iter) \
123 (mp_bvec_iter_offset((bvec), (iter)) % PAGE_SIZE)
125 #define bvec_iter_len(bvec, iter) \
126 min_t(unsigned, mp_bvec_iter_len((bvec), (iter)), \
127 PAGE_SIZE - bvec_iter_offset((bvec), (iter)))
129 #define bvec_iter_page(bvec, iter) \
130 (mp_bvec_iter_page((bvec), (iter)) + \
131 mp_bvec_iter_page_idx((bvec), (iter)))
133 #define bvec_iter_bvec(bvec, iter) \
134 ((struct bio_vec) { \
135 .bv_page = bvec_iter_page((bvec), (iter)), \
136 .bv_len = bvec_iter_len((bvec), (iter)), \
137 .bv_offset = bvec_iter_offset((bvec), (iter)), \
140 static inline bool bvec_iter_advance(const struct bio_vec
*bv
,
141 struct bvec_iter
*iter
, unsigned bytes
)
143 unsigned int idx
= iter
->bi_idx
;
145 if (WARN_ONCE(bytes
> iter
->bi_size
,
146 "Attempted to advance past end of bvec iter\n")) {
151 iter
->bi_size
-= bytes
;
152 bytes
+= iter
->bi_bvec_done
;
154 while (bytes
&& bytes
>= bv
[idx
].bv_len
) {
155 bytes
-= bv
[idx
].bv_len
;
160 iter
->bi_bvec_done
= bytes
;
165 * A simpler version of bvec_iter_advance(), @bytes should not span
166 * across multiple bvec entries, i.e. bytes <= bv[i->bi_idx].bv_len
168 static inline void bvec_iter_advance_single(const struct bio_vec
*bv
,
169 struct bvec_iter
*iter
, unsigned int bytes
)
171 unsigned int done
= iter
->bi_bvec_done
+ bytes
;
173 if (done
== bv
[iter
->bi_idx
].bv_len
) {
177 iter
->bi_bvec_done
= done
;
178 iter
->bi_size
-= bytes
;
181 #define for_each_bvec(bvl, bio_vec, iter, start) \
182 for (iter = (start); \
184 ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
185 bvec_iter_advance_single((bio_vec), &(iter), (bvl).bv_len))
187 /* for iterating one bio from start to end */
188 #define BVEC_ITER_ALL_INIT (struct bvec_iter) \
191 .bi_size = UINT_MAX, \
196 static inline struct bio_vec
*bvec_init_iter_all(struct bvec_iter_all
*iter_all
)
201 return &iter_all
->bv
;
204 static inline void bvec_advance(const struct bio_vec
*bvec
,
205 struct bvec_iter_all
*iter_all
)
207 struct bio_vec
*bv
= &iter_all
->bv
;
209 if (iter_all
->done
) {
213 bv
->bv_page
= bvec
->bv_page
+ (bvec
->bv_offset
>> PAGE_SHIFT
);
214 bv
->bv_offset
= bvec
->bv_offset
& ~PAGE_MASK
;
216 bv
->bv_len
= min_t(unsigned int, PAGE_SIZE
- bv
->bv_offset
,
217 bvec
->bv_len
- iter_all
->done
);
218 iter_all
->done
+= bv
->bv_len
;
220 if (iter_all
->done
== bvec
->bv_len
) {
227 * bvec_kmap_local - map a bvec into the kernel virtual address space
230 * Must be called on single-page bvecs only. Call kunmap_local on the returned
233 static inline void *bvec_kmap_local(struct bio_vec
*bvec
)
235 return kmap_local_page(bvec
->bv_page
) + bvec
->bv_offset
;
239 * memcpy_from_bvec - copy data from a bvec
240 * @bvec: bvec to copy from
242 * Must be called on single-page bvecs only.
244 static inline void memcpy_from_bvec(char *to
, struct bio_vec
*bvec
)
246 memcpy_from_page(to
, bvec
->bv_page
, bvec
->bv_offset
, bvec
->bv_len
);
250 * memcpy_to_bvec - copy data to a bvec
251 * @bvec: bvec to copy to
253 * Must be called on single-page bvecs only.
255 static inline void memcpy_to_bvec(struct bio_vec
*bvec
, const char *from
)
257 memcpy_to_page(bvec
->bv_page
, bvec
->bv_offset
, from
, bvec
->bv_len
);
261 * memzero_bvec - zero all data in a bvec
262 * @bvec: bvec to zero
264 * Must be called on single-page bvecs only.
266 static inline void memzero_bvec(struct bio_vec
*bvec
)
268 memzero_page(bvec
->bv_page
, bvec
->bv_offset
, bvec
->bv_len
);
272 * bvec_virt - return the virtual address for a bvec
273 * @bvec: bvec to return the virtual address for
275 * Note: the caller must ensure that @bvec->bv_page is not a highmem page.
277 static inline void *bvec_virt(struct bio_vec
*bvec
)
279 WARN_ON_ONCE(PageHighMem(bvec
->bv_page
));
280 return page_address(bvec
->bv_page
) + bvec
->bv_offset
;
284 * bvec_phys - return the physical address for a bvec
285 * @bvec: bvec to return the physical address for
287 static inline phys_addr_t
bvec_phys(const struct bio_vec
*bvec
)
290 * Note this open codes page_to_phys because page_to_phys is defined in
291 * <asm/io.h>, which we don't want to pull in here. If it ever moves to
292 * a sensible place we should start using it.
294 return PFN_PHYS(page_to_pfn(bvec
->bv_page
)) + bvec
->bv_offset
;
297 #endif /* __LINUX_BVEC_H */