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