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_ITER_H
8 #define __LINUX_BVEC_ITER_H
9
10 #include <linux/kernel.h>
11 #include <linux/bug.h>
12 #include <linux/errno.h>
13 #include <linux/mm.h>
14
15 /*
16 * was unsigned short, but we might as well be ready for > 64kB I/O pages
17 */
18 struct bio_vec {
19 struct page *bv_page;
20 unsigned int bv_len;
21 unsigned int bv_offset;
22 };
23
24 struct bvec_iter {
25 sector_t bi_sector; /* device address in 512 byte
26 sectors */
27 unsigned int bi_size; /* residual I/O count */
28
29 unsigned int bi_idx; /* current index into bvl_vec */
30
31 unsigned int bi_bvec_done; /* number of bytes completed in
32 current bvec */
33 };
34
35 struct bvec_iter_all {
36 struct bio_vec bv;
37 int idx;
38 unsigned done;
39 };
40
41 /*
42 * various member access, note that bio_data should of course not be used
43 * on highmem page vectors
44 */
45 #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
46
47 /* multi-page (mp_bvec) helpers */
48 #define mp_bvec_iter_page(bvec, iter) \
49 (__bvec_iter_bvec((bvec), (iter))->bv_page)
50
51 #define mp_bvec_iter_len(bvec, iter) \
52 min((iter).bi_size, \
53 __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
54
55 #define mp_bvec_iter_offset(bvec, iter) \
56 (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
57
58 #define mp_bvec_iter_page_idx(bvec, iter) \
59 (mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE)
60
61 #define mp_bvec_iter_bvec(bvec, iter) \
62 ((struct bio_vec) { \
63 .bv_page = mp_bvec_iter_page((bvec), (iter)), \
64 .bv_len = mp_bvec_iter_len((bvec), (iter)), \
65 .bv_offset = mp_bvec_iter_offset((bvec), (iter)), \
66 })
67
68 /* For building single-page bvec in flight */
69 #define bvec_iter_offset(bvec, iter) \
70 (mp_bvec_iter_offset((bvec), (iter)) % PAGE_SIZE)
71
72 #define bvec_iter_len(bvec, iter) \
73 min_t(unsigned, mp_bvec_iter_len((bvec), (iter)), \
74 PAGE_SIZE - bvec_iter_offset((bvec), (iter)))
75
76 #define bvec_iter_page(bvec, iter) \
77 (mp_bvec_iter_page((bvec), (iter)) + \
78 mp_bvec_iter_page_idx((bvec), (iter)))
79
80 #define bvec_iter_bvec(bvec, iter) \
81 ((struct bio_vec) { \
82 .bv_page = bvec_iter_page((bvec), (iter)), \
83 .bv_len = bvec_iter_len((bvec), (iter)), \
84 .bv_offset = bvec_iter_offset((bvec), (iter)), \
85 })
86
bvec_iter_advance(const struct bio_vec * bv,struct bvec_iter * iter,unsigned bytes)87 static inline bool bvec_iter_advance(const struct bio_vec *bv,
88 struct bvec_iter *iter, unsigned bytes)
89 {
90 if (WARN_ONCE(bytes > iter->bi_size,
91 "Attempted to advance past end of bvec iter\n")) {
92 iter->bi_size = 0;
93 return false;
94 }
95
96 while (bytes) {
97 const struct bio_vec *cur = bv + iter->bi_idx;
98 unsigned len = min3(bytes, iter->bi_size,
99 cur->bv_len - iter->bi_bvec_done);
100
101 bytes -= len;
102 iter->bi_size -= len;
103 iter->bi_bvec_done += len;
104
105 if (iter->bi_bvec_done == cur->bv_len) {
106 iter->bi_bvec_done = 0;
107 iter->bi_idx++;
108 }
109 }
110 return true;
111 }
112
bvec_iter_skip_zero_bvec(struct bvec_iter * iter)113 static inline void bvec_iter_skip_zero_bvec(struct bvec_iter *iter)
114 {
115 iter->bi_bvec_done = 0;
116 iter->bi_idx++;
117 }
118
119 #define for_each_bvec(bvl, bio_vec, iter, start) \
120 for (iter = (start); \
121 (iter).bi_size && \
122 ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
123 (bvl).bv_len ? (void)bvec_iter_advance((bio_vec), &(iter), \
124 (bvl).bv_len) : bvec_iter_skip_zero_bvec(&(iter)))
125
126 /* for iterating one bio from start to end */
127 #define BVEC_ITER_ALL_INIT (struct bvec_iter) \
128 { \
129 .bi_sector = 0, \
130 .bi_size = UINT_MAX, \
131 .bi_idx = 0, \
132 .bi_bvec_done = 0, \
133 }
134
bvec_init_iter_all(struct bvec_iter_all * iter_all)135 static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all)
136 {
137 iter_all->done = 0;
138 iter_all->idx = 0;
139
140 return &iter_all->bv;
141 }
142
bvec_advance(const struct bio_vec * bvec,struct bvec_iter_all * iter_all)143 static inline void bvec_advance(const struct bio_vec *bvec,
144 struct bvec_iter_all *iter_all)
145 {
146 struct bio_vec *bv = &iter_all->bv;
147
148 if (iter_all->done) {
149 bv->bv_page++;
150 bv->bv_offset = 0;
151 } else {
152 bv->bv_page = bvec->bv_page + (bvec->bv_offset >> PAGE_SHIFT);
153 bv->bv_offset = bvec->bv_offset & ~PAGE_MASK;
154 }
155 bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset,
156 bvec->bv_len - iter_all->done);
157 iter_all->done += bv->bv_len;
158
159 if (iter_all->done == bvec->bv_len) {
160 iter_all->idx++;
161 iter_all->done = 0;
162 }
163 }
164
165 /*
166 * Get the last single-page segment from the multi-page bvec and store it
167 * in @seg
168 */
mp_bvec_last_segment(const struct bio_vec * bvec,struct bio_vec * seg)169 static inline void mp_bvec_last_segment(const struct bio_vec *bvec,
170 struct bio_vec *seg)
171 {
172 unsigned total = bvec->bv_offset + bvec->bv_len;
173 unsigned last_page = (total - 1) / PAGE_SIZE;
174
175 seg->bv_page = bvec->bv_page + last_page;
176
177 /* the whole segment is inside the last page */
178 if (bvec->bv_offset >= last_page * PAGE_SIZE) {
179 seg->bv_offset = bvec->bv_offset % PAGE_SIZE;
180 seg->bv_len = bvec->bv_len;
181 } else {
182 seg->bv_offset = 0;
183 seg->bv_len = total - last_page * PAGE_SIZE;
184 }
185 }
186
187 #endif /* __LINUX_BVEC_ITER_H */
188