]>
Commit | Line | Data |
---|---|---|
8fc55455 ML |
1 | /* |
2 | * bvec iterator | |
3 | * | |
4 | * Copyright (C) 2001 Ming Lei <ming.lei@canonical.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public Licens | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- | |
19 | */ | |
20 | #ifndef __LINUX_BVEC_ITER_H | |
21 | #define __LINUX_BVEC_ITER_H | |
22 | ||
0781e79e ML |
23 | #include <linux/kernel.h> |
24 | #include <linux/bug.h> | |
b1fb2c52 | 25 | #include <linux/errno.h> |
3d75ca0a | 26 | #include <linux/mm.h> |
0781e79e ML |
27 | |
28 | /* | |
29 | * was unsigned short, but we might as well be ready for > 64kB I/O pages | |
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 | }; | |
8fc55455 | 47 | |
6dc4f100 ML |
48 | struct bvec_iter_all { |
49 | struct bio_vec bv; | |
50 | int idx; | |
51 | unsigned done; | |
52 | }; | |
53 | ||
4d633062 ML |
54 | static inline struct page *bvec_nth_page(struct page *page, int idx) |
55 | { | |
56 | return idx == 0 ? page : nth_page(page, idx); | |
57 | } | |
58 | ||
8fc55455 ML |
59 | /* |
60 | * various member access, note that bio_data should of course not be used | |
61 | * on highmem page vectors | |
62 | */ | |
63 | #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx]) | |
64 | ||
3d75ca0a ML |
65 | /* multi-page (mp_bvec) helpers */ |
66 | #define mp_bvec_iter_page(bvec, iter) \ | |
8fc55455 ML |
67 | (__bvec_iter_bvec((bvec), (iter))->bv_page) |
68 | ||
3d75ca0a | 69 | #define mp_bvec_iter_len(bvec, iter) \ |
8fc55455 ML |
70 | min((iter).bi_size, \ |
71 | __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done) | |
72 | ||
3d75ca0a | 73 | #define mp_bvec_iter_offset(bvec, iter) \ |
8fc55455 ML |
74 | (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done) |
75 | ||
3d75ca0a ML |
76 | #define mp_bvec_iter_page_idx(bvec, iter) \ |
77 | (mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE) | |
78 | ||
79 | #define mp_bvec_iter_bvec(bvec, iter) \ | |
80 | ((struct bio_vec) { \ | |
81 | .bv_page = mp_bvec_iter_page((bvec), (iter)), \ | |
82 | .bv_len = mp_bvec_iter_len((bvec), (iter)), \ | |
83 | .bv_offset = mp_bvec_iter_offset((bvec), (iter)), \ | |
84 | }) | |
85 | ||
86 | /* For building single-page bvec in flight */ | |
87 | #define bvec_iter_offset(bvec, iter) \ | |
88 | (mp_bvec_iter_offset((bvec), (iter)) % PAGE_SIZE) | |
89 | ||
90 | #define bvec_iter_len(bvec, iter) \ | |
91 | min_t(unsigned, mp_bvec_iter_len((bvec), (iter)), \ | |
92 | PAGE_SIZE - bvec_iter_offset((bvec), (iter))) | |
93 | ||
94 | #define bvec_iter_page(bvec, iter) \ | |
4d633062 ML |
95 | bvec_nth_page(mp_bvec_iter_page((bvec), (iter)), \ |
96 | mp_bvec_iter_page_idx((bvec), (iter))) | |
3d75ca0a | 97 | |
8fc55455 ML |
98 | #define bvec_iter_bvec(bvec, iter) \ |
99 | ((struct bio_vec) { \ | |
100 | .bv_page = bvec_iter_page((bvec), (iter)), \ | |
101 | .bv_len = bvec_iter_len((bvec), (iter)), \ | |
102 | .bv_offset = bvec_iter_offset((bvec), (iter)), \ | |
103 | }) | |
104 | ||
b1fb2c52 DM |
105 | static inline bool bvec_iter_advance(const struct bio_vec *bv, |
106 | struct bvec_iter *iter, unsigned bytes) | |
8fc55455 | 107 | { |
b1fb2c52 DM |
108 | if (WARN_ONCE(bytes > iter->bi_size, |
109 | "Attempted to advance past end of bvec iter\n")) { | |
110 | iter->bi_size = 0; | |
111 | return false; | |
112 | } | |
8fc55455 ML |
113 | |
114 | while (bytes) { | |
5b88a17c CH |
115 | const struct bio_vec *cur = bv + iter->bi_idx; |
116 | unsigned len = min3(bytes, iter->bi_size, | |
117 | cur->bv_len - iter->bi_bvec_done); | |
8fc55455 ML |
118 | |
119 | bytes -= len; | |
120 | iter->bi_size -= len; | |
121 | iter->bi_bvec_done += len; | |
122 | ||
5b88a17c | 123 | if (iter->bi_bvec_done == cur->bv_len) { |
8fc55455 ML |
124 | iter->bi_bvec_done = 0; |
125 | iter->bi_idx++; | |
126 | } | |
127 | } | |
b1fb2c52 | 128 | return true; |
8fc55455 ML |
129 | } |
130 | ||
131 | #define for_each_bvec(bvl, bio_vec, iter, start) \ | |
132 | for (iter = (start); \ | |
133 | (iter).bi_size && \ | |
134 | ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \ | |
135 | bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len)) | |
136 | ||
3c892a09 ML |
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 | ||
6dc4f100 ML |
146 | static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all) |
147 | { | |
6dc4f100 | 148 | iter_all->done = 0; |
1200e07f | 149 | iter_all->idx = 0; |
6dc4f100 ML |
150 | |
151 | return &iter_all->bv; | |
152 | } | |
153 | ||
1200e07f ML |
154 | static inline void bvec_advance(const struct bio_vec *bvec, |
155 | struct bvec_iter_all *iter_all) | |
6dc4f100 ML |
156 | { |
157 | struct bio_vec *bv = &iter_all->bv; | |
158 | ||
1200e07f | 159 | if (iter_all->done) { |
6dc4f100 ML |
160 | bv->bv_page = nth_page(bv->bv_page, 1); |
161 | bv->bv_offset = 0; | |
162 | } else { | |
6bedf00e ML |
163 | bv->bv_page = bvec_nth_page(bvec->bv_page, bvec->bv_offset / |
164 | PAGE_SIZE); | |
165 | bv->bv_offset = bvec->bv_offset & ~PAGE_MASK; | |
6dc4f100 ML |
166 | } |
167 | bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset, | |
168 | bvec->bv_len - iter_all->done); | |
1200e07f ML |
169 | iter_all->done += bv->bv_len; |
170 | ||
171 | if (iter_all->done == bvec->bv_len) { | |
172 | iter_all->idx++; | |
173 | iter_all->done = 0; | |
174 | } | |
6dc4f100 ML |
175 | } |
176 | ||
45a3fb95 ML |
177 | /* |
178 | * Get the last single-page segment from the multi-page bvec and store it | |
179 | * in @seg | |
180 | */ | |
181 | static inline void mp_bvec_last_segment(const struct bio_vec *bvec, | |
182 | struct bio_vec *seg) | |
183 | { | |
184 | unsigned total = bvec->bv_offset + bvec->bv_len; | |
185 | unsigned last_page = (total - 1) / PAGE_SIZE; | |
186 | ||
4d633062 | 187 | seg->bv_page = bvec_nth_page(bvec->bv_page, last_page); |
45a3fb95 ML |
188 | |
189 | /* the whole segment is inside the last page */ | |
190 | if (bvec->bv_offset >= last_page * PAGE_SIZE) { | |
191 | seg->bv_offset = bvec->bv_offset % PAGE_SIZE; | |
192 | seg->bv_len = bvec->bv_len; | |
193 | } else { | |
194 | seg->bv_offset = 0; | |
195 | seg->bv_len = total - last_page * PAGE_SIZE; | |
196 | } | |
197 | } | |
198 | ||
594b9a89 ML |
199 | #define mp_bvec_for_each_page(pg, bv, i) \ |
200 | for (i = (bv)->bv_offset / PAGE_SIZE; \ | |
201 | (i <= (((bv)->bv_offset + (bv)->bv_len - 1) / PAGE_SIZE)) && \ | |
202 | (pg = bvec_nth_page((bv)->bv_page, i)); i += 1) | |
203 | ||
8fc55455 | 204 | #endif /* __LINUX_BVEC_ITER_H */ |