1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * bvec iterator
4 *
5 * Copyright (C) 2001 Ming Lei <ming.lei@canonical.com>
6 */
7 #ifndef __LINUX_BVEC_H
8 #define __LINUX_BVEC_H
9
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>
16
17 struct page;
18
19 /**
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.
24 *
25 * All pages within a bio_vec starting from @bv_page are contiguous and
26 * can simply be iterated (see bvec_advance()).
27 */
28 struct bio_vec {
29 struct page *bv_page;
30 unsigned int bv_len;
31 unsigned int bv_offset;
32 };
33
34 /**
35 * bvec_set_page - initialize a bvec based off a struct page
36 * @bv: bvec to initialize
37 * @page: page the bvec should point to
38 * @len: length of the bvec
39 * @offset: offset into the page
40 */
bvec_set_page(struct bio_vec * bv,struct page * page,unsigned int len,unsigned int offset)41 static inline void bvec_set_page(struct bio_vec *bv, struct page *page,
42 unsigned int len, unsigned int offset)
43 {
44 bv->bv_page = page;
45 bv->bv_len = len;
46 bv->bv_offset = offset;
47 }
48
49 /**
50 * bvec_set_folio - initialize a bvec based off a struct folio
51 * @bv: bvec to initialize
52 * @folio: folio the bvec should point to
53 * @len: length of the bvec
54 * @offset: offset into the folio
55 */
bvec_set_folio(struct bio_vec * bv,struct folio * folio,size_t len,size_t offset)56 static inline void bvec_set_folio(struct bio_vec *bv, struct folio *folio,
57 size_t len, size_t offset)
58 {
59 unsigned long nr = offset / PAGE_SIZE;
60
61 WARN_ON_ONCE(len > UINT_MAX);
62 bvec_set_page(bv, folio_page(folio, nr), len, offset % PAGE_SIZE);
63 }
64
65 /**
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
70 */
bvec_set_virt(struct bio_vec * bv,void * vaddr,unsigned int len)71 static inline void bvec_set_virt(struct bio_vec *bv, void *vaddr,
72 unsigned int len)
73 {
74 bvec_set_page(bv, virt_to_page(vaddr), len, offset_in_page(vaddr));
75 }
76
77 /**
78 * bvec_folio - Return the first folio referenced by this bvec
79 * @bv: bvec to access
80 *
81 * A bvec can contain non-folio memory, so this should only be called by
82 * the creator of the bvec; drivers have no business looking at the owner
83 * of the memory. It may not even be the right interface for the caller
84 * to use as a bvec can span multiple folios. You may be better off using
85 * something like bio_for_each_folio_all() which iterates over all folios.
86 */
bvec_folio(const struct bio_vec * bv)87 static inline struct folio *bvec_folio(const struct bio_vec *bv)
88 {
89 return page_folio(bv->bv_page);
90 }
91
92 struct bvec_iter {
93 /*
94 * Current device address in 512 byte sectors. Only updated by the bio
95 * iter wrappers and not the bvec iterator helpers themselves.
96 */
97 sector_t bi_sector;
98
99 /*
100 * Remaining size in bytes.
101 */
102 unsigned int bi_size;
103
104 /*
105 * Current index into the bvec array. This indexes into `bi_io_vec` when
106 * iterating a bvec array that is part of a `bio`.
107 */
108 unsigned int bi_idx;
109
110 /*
111 * Current offset in the bvec entry pointed to by `bi_idx`.
112 */
113 unsigned int bi_offset;
114 } __packed __aligned(4);
115
116 struct bvec_iter_all {
117 struct bio_vec bv;
118 int idx;
119 unsigned done;
120 };
121
122 static __always_inline const struct bio_vec *
__bvec_iter_bvec(const struct bio_vec * bvecs,const struct bvec_iter iter)123 __bvec_iter_bvec(const struct bio_vec *bvecs, const struct bvec_iter iter)
124 {
125 return bvecs + iter.bi_idx;
126 }
127
128 /* multi-page (mp_bvec) helpers */
129 static __always_inline struct page *
mp_bvec_iter_page(const struct bio_vec * bvecs,const struct bvec_iter iter)130 mp_bvec_iter_page(const struct bio_vec *bvecs, const struct bvec_iter iter)
131 {
132 return __bvec_iter_bvec(bvecs, iter)->bv_page;
133 }
134
135 static __always_inline unsigned int
mp_bvec_iter_len(const struct bio_vec * bvecs,const struct bvec_iter iter)136 mp_bvec_iter_len(const struct bio_vec *bvecs, const struct bvec_iter iter)
137 {
138 return min(__bvec_iter_bvec(bvecs, iter)->bv_len - iter.bi_offset,
139 iter.bi_size);
140 }
141
142 static __always_inline unsigned int
mp_bvec_iter_offset(const struct bio_vec * bvecs,const struct bvec_iter iter)143 mp_bvec_iter_offset(const struct bio_vec *bvecs, const struct bvec_iter iter)
144 {
145 return __bvec_iter_bvec(bvecs, iter)->bv_offset + iter.bi_offset;
146 }
147
148 static __always_inline unsigned int
mp_bvec_iter_page_idx(const struct bio_vec * bvecs,const struct bvec_iter iter)149 mp_bvec_iter_page_idx(const struct bio_vec *bvecs, const struct bvec_iter iter)
150 {
151 return mp_bvec_iter_offset(bvecs, iter) / PAGE_SIZE;
152 }
153
154 static __always_inline struct bio_vec
mp_bvec_iter_bvec(const struct bio_vec * bvecs,const struct bvec_iter iter)155 mp_bvec_iter_bvec(const struct bio_vec *bvecs, const struct bvec_iter iter)
156 {
157 return (struct bio_vec) {
158 .bv_page = mp_bvec_iter_page(bvecs, iter),
159 .bv_len = mp_bvec_iter_len(bvecs, iter),
160 .bv_offset = mp_bvec_iter_offset(bvecs, iter),
161 };
162 }
163
164 /* For building single-page bvec in flight */
165 static __always_inline unsigned int
bvec_iter_offset(const struct bio_vec * bvecs,const struct bvec_iter iter)166 bvec_iter_offset(const struct bio_vec *bvecs, const struct bvec_iter iter)
167 {
168 return mp_bvec_iter_offset(bvecs, iter) % PAGE_SIZE;
169 }
170
171 static __always_inline unsigned int
bvec_iter_len(const struct bio_vec * bvecs,const struct bvec_iter iter)172 bvec_iter_len(const struct bio_vec *bvecs, const struct bvec_iter iter)
173 {
174 return min(mp_bvec_iter_len(bvecs, iter),
175 PAGE_SIZE - bvec_iter_offset(bvecs, iter));
176 }
177
178 static __always_inline struct page *
bvec_iter_page(const struct bio_vec * bvecs,const struct bvec_iter iter)179 bvec_iter_page(const struct bio_vec *bvecs, const struct bvec_iter iter)
180 {
181 return mp_bvec_iter_page(bvecs, iter) +
182 mp_bvec_iter_page_idx(bvecs, iter);
183 }
184
185 static __always_inline struct bio_vec
bvec_iter_bvec(const struct bio_vec * bvecs,const struct bvec_iter iter)186 bvec_iter_bvec(const struct bio_vec *bvecs, const struct bvec_iter iter)
187 {
188 return (struct bio_vec) {
189 .bv_page = bvec_iter_page(bvecs, iter),
190 .bv_len = bvec_iter_len(bvecs, iter),
191 .bv_offset = bvec_iter_offset(bvecs, iter),
192 };
193 }
194
bvec_iter_advance(const struct bio_vec * bv,struct bvec_iter * iter,unsigned bytes)195 static inline bool bvec_iter_advance(const struct bio_vec *bv,
196 struct bvec_iter *iter, unsigned bytes)
197 {
198 unsigned int idx = iter->bi_idx;
199
200 if (WARN_ONCE(bytes > iter->bi_size,
201 "Attempted to advance past end of bvec iter\n")) {
202 iter->bi_size = 0;
203 return false;
204 }
205
206 iter->bi_size -= bytes;
207 bytes += iter->bi_offset;
208
209 while (bytes && bytes >= bv[idx].bv_len) {
210 bytes -= bv[idx].bv_len;
211 idx++;
212 }
213
214 iter->bi_idx = idx;
215 iter->bi_offset = bytes;
216 return true;
217 }
218
219 /*
220 * A simpler version of bvec_iter_advance(), @bytes should not span
221 * across multiple bvec entries, i.e. bytes <= bv[i->bi_idx].bv_len
222 */
bvec_iter_advance_single(const struct bio_vec * bv,struct bvec_iter * iter,unsigned int bytes)223 static inline void bvec_iter_advance_single(const struct bio_vec *bv,
224 struct bvec_iter *iter, unsigned int bytes)
225 {
226 unsigned int done = iter->bi_offset + bytes;
227
228 if (done == bv[iter->bi_idx].bv_len) {
229 done = 0;
230 iter->bi_idx++;
231 }
232 iter->bi_offset = done;
233 iter->bi_size -= bytes;
234 }
235
236 #define for_each_bvec(bvl, bio_vec, iter, start) \
237 for (iter = (start); \
238 (iter).bi_size && \
239 ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
240 bvec_iter_advance_single((bio_vec), &(iter), (bvl).bv_len))
241
242 #define for_each_mp_bvec(bvl, bio_vec, iter, start) \
243 for (iter = (start); \
244 (iter).bi_size && \
245 ((bvl = mp_bvec_iter_bvec((bio_vec), (iter))), 1); \
246 bvec_iter_advance_single((bio_vec), &(iter), (bvl).bv_len))
247
bvec_init_iter_all(struct bvec_iter_all * iter_all)248 static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all)
249 {
250 iter_all->done = 0;
251 iter_all->idx = 0;
252
253 return &iter_all->bv;
254 }
255
bvec_advance(const struct bio_vec * bvec,struct bvec_iter_all * iter_all)256 static inline void bvec_advance(const struct bio_vec *bvec,
257 struct bvec_iter_all *iter_all)
258 {
259 struct bio_vec *bv = &iter_all->bv;
260
261 if (iter_all->done) {
262 bv->bv_page++;
263 bv->bv_offset = 0;
264 } else {
265 bv->bv_page = bvec->bv_page + (bvec->bv_offset >> PAGE_SHIFT);
266 bv->bv_offset = bvec->bv_offset & ~PAGE_MASK;
267 }
268 bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset,
269 bvec->bv_len - iter_all->done);
270 iter_all->done += bv->bv_len;
271
272 if (iter_all->done == bvec->bv_len) {
273 iter_all->idx++;
274 iter_all->done = 0;
275 }
276 }
277
278 /**
279 * bvec_kmap_local - map a bvec into the kernel virtual address space
280 * @bvec: bvec to map
281 *
282 * Must be called on single-page bvecs only. Call kunmap_local on the returned
283 * address to unmap.
284 */
bvec_kmap_local(struct bio_vec * bvec)285 static inline void *bvec_kmap_local(struct bio_vec *bvec)
286 {
287 return kmap_local_page(bvec->bv_page) + bvec->bv_offset;
288 }
289
290 /**
291 * memcpy_from_bvec - copy data from a bvec
292 * @to: Kernel virtual address to copy to.
293 * @bvec: bvec to copy from
294 *
295 * Must be called on single-page bvecs only.
296 */
memcpy_from_bvec(char * to,struct bio_vec * bvec)297 static inline void memcpy_from_bvec(char *to, struct bio_vec *bvec)
298 {
299 memcpy_from_page(to, bvec->bv_page, bvec->bv_offset, bvec->bv_len);
300 }
301
302 /**
303 * memcpy_to_bvec - copy data to a bvec
304 * @bvec: bvec to copy to
305 * @from: Kernel virtual address to copy from.
306 *
307 * Must be called on single-page bvecs only.
308 */
memcpy_to_bvec(struct bio_vec * bvec,const char * from)309 static inline void memcpy_to_bvec(struct bio_vec *bvec, const char *from)
310 {
311 memcpy_to_page(bvec->bv_page, bvec->bv_offset, from, bvec->bv_len);
312 }
313
314 /**
315 * memzero_bvec - zero all data in a bvec
316 * @bvec: bvec to zero
317 *
318 * Must be called on single-page bvecs only.
319 */
memzero_bvec(struct bio_vec * bvec)320 static inline void memzero_bvec(struct bio_vec *bvec)
321 {
322 memzero_page(bvec->bv_page, bvec->bv_offset, bvec->bv_len);
323 }
324
325 /**
326 * bvec_virt - return the virtual address for a bvec
327 * @bvec: bvec to return the virtual address for
328 *
329 * Note: the caller must ensure that @bvec->bv_page is not a highmem page.
330 */
bvec_virt(struct bio_vec * bvec)331 static inline void *bvec_virt(struct bio_vec *bvec)
332 {
333 WARN_ON_ONCE(PageHighMem(bvec->bv_page));
334 return page_address(bvec->bv_page) + bvec->bv_offset;
335 }
336
337 /**
338 * bvec_phys - return the physical address for a bvec
339 * @bvec: bvec to return the physical address for
340 */
bvec_phys(const struct bio_vec * bvec)341 static inline phys_addr_t bvec_phys(const struct bio_vec *bvec)
342 {
343 return page_to_phys(bvec->bv_page) + bvec->bv_offset;
344 }
345
346 #endif /* __LINUX_BVEC_H */
347