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> |
0781e79e ML |
26 | |
27 | /* | |
28 | * was unsigned short, but we might as well be ready for > 64kB I/O pages | |
29 | */ | |
30 | struct bio_vec { | |
31 | struct page *bv_page; | |
32 | unsigned int bv_len; | |
33 | unsigned int bv_offset; | |
34 | }; | |
35 | ||
36 | struct bvec_iter { | |
37 | sector_t bi_sector; /* device address in 512 byte | |
38 | sectors */ | |
39 | unsigned int bi_size; /* residual I/O count */ | |
40 | ||
41 | unsigned int bi_idx; /* current index into bvl_vec */ | |
42 | ||
f9df1cd9 DM |
43 | unsigned int bi_done; /* number of bytes completed */ |
44 | ||
0781e79e ML |
45 | unsigned int bi_bvec_done; /* number of bytes completed in |
46 | current bvec */ | |
49e3bbe3 BK |
47 | #ifdef CONFIG_CRYPTO_DISKCIPHER_DUN |
48 | u64 bi_dun; | |
49 | #endif | |
0781e79e | 50 | }; |
8fc55455 ML |
51 | |
52 | /* | |
53 | * various member access, note that bio_data should of course not be used | |
54 | * on highmem page vectors | |
55 | */ | |
56 | #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx]) | |
57 | ||
58 | #define bvec_iter_page(bvec, iter) \ | |
59 | (__bvec_iter_bvec((bvec), (iter))->bv_page) | |
60 | ||
61 | #define bvec_iter_len(bvec, iter) \ | |
62 | min((iter).bi_size, \ | |
63 | __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done) | |
64 | ||
65 | #define bvec_iter_offset(bvec, iter) \ | |
66 | (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done) | |
67 | ||
68 | #define bvec_iter_bvec(bvec, iter) \ | |
69 | ((struct bio_vec) { \ | |
70 | .bv_page = bvec_iter_page((bvec), (iter)), \ | |
71 | .bv_len = bvec_iter_len((bvec), (iter)), \ | |
72 | .bv_offset = bvec_iter_offset((bvec), (iter)), \ | |
73 | }) | |
74 | ||
b1fb2c52 DM |
75 | static inline bool bvec_iter_advance(const struct bio_vec *bv, |
76 | struct bvec_iter *iter, unsigned bytes) | |
8fc55455 | 77 | { |
b1fb2c52 DM |
78 | if (WARN_ONCE(bytes > iter->bi_size, |
79 | "Attempted to advance past end of bvec iter\n")) { | |
80 | iter->bi_size = 0; | |
81 | return false; | |
82 | } | |
8fc55455 ML |
83 | |
84 | while (bytes) { | |
1ea049b2 JB |
85 | unsigned iter_len = bvec_iter_len(bv, *iter); |
86 | unsigned len = min(bytes, iter_len); | |
8fc55455 ML |
87 | |
88 | bytes -= len; | |
89 | iter->bi_size -= len; | |
90 | iter->bi_bvec_done += len; | |
f9df1cd9 | 91 | iter->bi_done += len; |
8fc55455 ML |
92 | |
93 | if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) { | |
94 | iter->bi_bvec_done = 0; | |
95 | iter->bi_idx++; | |
96 | } | |
97 | } | |
b1fb2c52 | 98 | return true; |
8fc55455 ML |
99 | } |
100 | ||
f9df1cd9 DM |
101 | static inline bool bvec_iter_rewind(const struct bio_vec *bv, |
102 | struct bvec_iter *iter, | |
103 | unsigned int bytes) | |
104 | { | |
105 | while (bytes) { | |
106 | unsigned len = min(bytes, iter->bi_bvec_done); | |
107 | ||
108 | if (iter->bi_bvec_done == 0) { | |
109 | if (WARN_ONCE(iter->bi_idx == 0, | |
110 | "Attempted to rewind iter beyond " | |
111 | "bvec's boundaries\n")) { | |
112 | return false; | |
113 | } | |
114 | iter->bi_idx--; | |
115 | iter->bi_bvec_done = __bvec_iter_bvec(bv, *iter)->bv_len; | |
116 | continue; | |
117 | } | |
118 | bytes -= len; | |
119 | iter->bi_size += len; | |
120 | iter->bi_bvec_done -= len; | |
121 | } | |
122 | return true; | |
123 | } | |
124 | ||
8fc55455 ML |
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 | ||
131 | #endif /* __LINUX_BVEC_ITER_H */ |