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
23 #include <linux/kernel.h>
24 #include <linux/bug.h>
25
26 /*
27 * was unsigned short, but we might as well be ready for > 64kB I/O pages
28 */
29 struct bio_vec {
30 struct page *bv_page;
31 unsigned int bv_len;
32 unsigned int bv_offset;
33 };
34
35 struct bvec_iter {
36 sector_t bi_sector; /* device address in 512 byte
37 sectors */
38 unsigned int bi_size; /* residual I/O count */
39
40 unsigned int bi_idx; /* current index into bvl_vec */
41
42 unsigned int bi_bvec_done; /* number of bytes completed in
43 current bvec */
44 };
45
46 /*
47 * various member access, note that bio_data should of course not be used
48 * on highmem page vectors
49 */
50 #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
51
52 #define bvec_iter_page(bvec, iter) \
53 (__bvec_iter_bvec((bvec), (iter))->bv_page)
54
55 #define bvec_iter_len(bvec, iter) \
56 min((iter).bi_size, \
57 __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
58
59 #define bvec_iter_offset(bvec, iter) \
60 (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
61
62 #define bvec_iter_bvec(bvec, iter) \
63 ((struct bio_vec) { \
64 .bv_page = bvec_iter_page((bvec), (iter)), \
65 .bv_len = bvec_iter_len((bvec), (iter)), \
66 .bv_offset = bvec_iter_offset((bvec), (iter)), \
67 })
68
bvec_iter_advance(const struct bio_vec * bv,struct bvec_iter * iter,unsigned bytes)69 static inline void bvec_iter_advance(const struct bio_vec *bv,
70 struct bvec_iter *iter,
71 unsigned bytes)
72 {
73 WARN_ONCE(bytes > iter->bi_size,
74 "Attempted to advance past end of bvec iter\n");
75
76 while (bytes) {
77 unsigned iter_len = bvec_iter_len(bv, *iter);
78 unsigned len = min(bytes, iter_len);
79
80 bytes -= len;
81 iter->bi_size -= len;
82 iter->bi_bvec_done += len;
83
84 if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) {
85 iter->bi_bvec_done = 0;
86 iter->bi_idx++;
87 }
88 }
89 }
90
91 #define for_each_bvec(bvl, bio_vec, iter, start) \
92 for (iter = (start); \
93 (iter).bi_size && \
94 ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
95 bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len))
96
97 #endif /* __LINUX_BVEC_ITER_H */
98