]>
Commit | Line | Data |
---|---|---|
8fc55455 ML |
1 | /* |
2 | * bvec iterator | |
3 | * | |
4 | * Copyright (C) 2001 Ming Lei <[email protected]> | |
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 | ||
8fc55455 ML |
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 | ||
3d75ca0a ML |
60 | /* multi-page (mp_bvec) helpers */ |
61 | #define mp_bvec_iter_page(bvec, iter) \ | |
8fc55455 ML |
62 | (__bvec_iter_bvec((bvec), (iter))->bv_page) |
63 | ||
3d75ca0a | 64 | #define mp_bvec_iter_len(bvec, iter) \ |
8fc55455 ML |
65 | min((iter).bi_size, \ |
66 | __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done) | |
67 | ||
3d75ca0a | 68 | #define mp_bvec_iter_offset(bvec, iter) \ |
8fc55455 ML |
69 | (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done) |
70 | ||
3d75ca0a ML |
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 | nth_page(mp_bvec_iter_page((bvec), (iter)), \ | |
91 | mp_bvec_iter_page_idx((bvec), (iter))) | |
92 | ||
8fc55455 ML |
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 | ||
b1fb2c52 DM |
100 | static inline bool bvec_iter_advance(const struct bio_vec *bv, |
101 | struct bvec_iter *iter, unsigned bytes) | |
8fc55455 | 102 | { |
b1fb2c52 DM |
103 | if (WARN_ONCE(bytes > iter->bi_size, |
104 | "Attempted to advance past end of bvec iter\n")) { | |
105 | iter->bi_size = 0; | |
106 | return false; | |
107 | } | |
8fc55455 ML |
108 | |
109 | while (bytes) { | |
1ea049b2 JB |
110 | unsigned iter_len = bvec_iter_len(bv, *iter); |
111 | unsigned len = min(bytes, iter_len); | |
8fc55455 ML |
112 | |
113 | bytes -= len; | |
114 | iter->bi_size -= len; | |
115 | iter->bi_bvec_done += len; | |
116 | ||
117 | if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) { | |
118 | iter->bi_bvec_done = 0; | |
119 | iter->bi_idx++; | |
120 | } | |
121 | } | |
b1fb2c52 | 122 | return true; |
8fc55455 ML |
123 | } |
124 | ||
125 | #define for_each_bvec(bvl, bio_vec, iter, start) \ | |
126 | for (iter = (start); \ | |
127 | (iter).bi_size && \ | |
128 | ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \ | |
129 | bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len)) | |
130 | ||
3c892a09 ML |
131 | /* for iterating one bio from start to end */ |
132 | #define BVEC_ITER_ALL_INIT (struct bvec_iter) \ | |
133 | { \ | |
134 | .bi_sector = 0, \ | |
135 | .bi_size = UINT_MAX, \ | |
136 | .bi_idx = 0, \ | |
137 | .bi_bvec_done = 0, \ | |
138 | } | |
139 | ||
6dc4f100 ML |
140 | static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all) |
141 | { | |
142 | iter_all->bv.bv_page = NULL; | |
143 | iter_all->done = 0; | |
144 | ||
145 | return &iter_all->bv; | |
146 | } | |
147 | ||
148 | static inline void mp_bvec_next_segment(const struct bio_vec *bvec, | |
149 | struct bvec_iter_all *iter_all) | |
150 | { | |
151 | struct bio_vec *bv = &iter_all->bv; | |
152 | ||
153 | if (bv->bv_page) { | |
154 | bv->bv_page = nth_page(bv->bv_page, 1); | |
155 | bv->bv_offset = 0; | |
156 | } else { | |
157 | bv->bv_page = bvec->bv_page; | |
158 | bv->bv_offset = bvec->bv_offset; | |
159 | } | |
160 | bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset, | |
161 | bvec->bv_len - iter_all->done); | |
162 | } | |
163 | ||
45a3fb95 ML |
164 | /* |
165 | * Get the last single-page segment from the multi-page bvec and store it | |
166 | * in @seg | |
167 | */ | |
168 | static inline void mp_bvec_last_segment(const struct bio_vec *bvec, | |
169 | struct bio_vec *seg) | |
170 | { | |
171 | unsigned total = bvec->bv_offset + bvec->bv_len; | |
172 | unsigned last_page = (total - 1) / PAGE_SIZE; | |
173 | ||
174 | seg->bv_page = nth_page(bvec->bv_page, last_page); | |
175 | ||
176 | /* the whole segment is inside the last page */ | |
177 | if (bvec->bv_offset >= last_page * PAGE_SIZE) { | |
178 | seg->bv_offset = bvec->bv_offset % PAGE_SIZE; | |
179 | seg->bv_len = bvec->bv_len; | |
180 | } else { | |
181 | seg->bv_offset = 0; | |
182 | seg->bv_len = total - last_page * PAGE_SIZE; | |
183 | } | |
184 | } | |
185 | ||
8fc55455 | 186 | #endif /* __LINUX_BVEC_ITER_H */ |