1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright (C) 2001 Ming Lei <ming.lei@canonical.com>
7 #ifndef __LINUX_BVEC_ITER_H
8 #define __LINUX_BVEC_ITER_H
10 #include <linux/bug.h>
11 #include <linux/errno.h>
12 #include <linux/limits.h>
13 #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 sector_t bi_sector; /* device address in 512 byte
40 unsigned int bi_size; /* residual I/O count */
42 unsigned int bi_idx; /* current index into bvl_vec */
44 unsigned int bi_bvec_done; /* number of bytes completed in
48 struct bvec_iter_all {
55 * various member access, note that bio_data should of course not be used
56 * on highmem page vectors
58 #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
60 /* multi-page (mp_bvec) helpers */
61 #define mp_bvec_iter_page(bvec, iter) \
62 (__bvec_iter_bvec((bvec), (iter))->bv_page)
64 #define mp_bvec_iter_len(bvec, iter) \
66 __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
68 #define mp_bvec_iter_offset(bvec, iter) \
69 (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
71 #define mp_bvec_iter_page_idx(bvec, iter) \
72 (mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE)
74 #define mp_bvec_iter_bvec(bvec, iter) \
76 .bv_page = mp_bvec_iter_page((bvec), (iter)), \
77 .bv_len = mp_bvec_iter_len((bvec), (iter)), \
78 .bv_offset = mp_bvec_iter_offset((bvec), (iter)), \
81 /* For building single-page bvec in flight */
82 #define bvec_iter_offset(bvec, iter) \
83 (mp_bvec_iter_offset((bvec), (iter)) % PAGE_SIZE)
85 #define bvec_iter_len(bvec, iter) \
86 min_t(unsigned, mp_bvec_iter_len((bvec), (iter)), \
87 PAGE_SIZE - bvec_iter_offset((bvec), (iter)))
89 #define bvec_iter_page(bvec, iter) \
90 (mp_bvec_iter_page((bvec), (iter)) + \
91 mp_bvec_iter_page_idx((bvec), (iter)))
93 #define bvec_iter_bvec(bvec, iter) \
95 .bv_page = bvec_iter_page((bvec), (iter)), \
96 .bv_len = bvec_iter_len((bvec), (iter)), \
97 .bv_offset = bvec_iter_offset((bvec), (iter)), \
100 static inline bool bvec_iter_advance(const struct bio_vec *bv,
101 struct bvec_iter *iter, unsigned bytes)
103 unsigned int idx = iter->bi_idx;
105 if (WARN_ONCE(bytes > iter->bi_size,
106 "Attempted to advance past end of bvec iter\n")) {
111 iter->bi_size -= bytes;
112 bytes += iter->bi_bvec_done;
114 while (bytes && bytes >= bv[idx].bv_len) {
115 bytes -= bv[idx].bv_len;
120 iter->bi_bvec_done = bytes;
125 * A simpler version of bvec_iter_advance(), @bytes should not span
126 * across multiple bvec entries, i.e. bytes <= bv[i->bi_idx].bv_len
128 static inline void bvec_iter_advance_single(const struct bio_vec *bv,
129 struct bvec_iter *iter, unsigned int bytes)
131 unsigned int done = iter->bi_bvec_done + bytes;
133 if (done == bv[iter->bi_idx].bv_len) {
137 iter->bi_bvec_done = done;
138 iter->bi_size -= bytes;
141 #define for_each_bvec(bvl, bio_vec, iter, start) \
142 for (iter = (start); \
144 ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
145 bvec_iter_advance_single((bio_vec), &(iter), (bvl).bv_len))
147 /* for iterating one bio from start to end */
148 #define BVEC_ITER_ALL_INIT (struct bvec_iter) \
151 .bi_size = UINT_MAX, \
156 static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all)
161 return &iter_all->bv;
164 static inline void bvec_advance(const struct bio_vec *bvec,
165 struct bvec_iter_all *iter_all)
167 struct bio_vec *bv = &iter_all->bv;
169 if (iter_all->done) {
173 bv->bv_page = bvec->bv_page + (bvec->bv_offset >> PAGE_SHIFT);
174 bv->bv_offset = bvec->bv_offset & ~PAGE_MASK;
176 bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset,
177 bvec->bv_len - iter_all->done);
178 iter_all->done += bv->bv_len;
180 if (iter_all->done == bvec->bv_len) {
186 #endif /* __LINUX_BVEC_ITER_H */