]>
Commit | Line | Data |
---|---|---|
8c16567d | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
8fc55455 ML |
2 | /* |
3 | * bvec iterator | |
4 | * | |
5 | * Copyright (C) 2001 Ming Lei <ming.lei@canonical.com> | |
8fc55455 ML |
6 | */ |
7 | #ifndef __LINUX_BVEC_ITER_H | |
8 | #define __LINUX_BVEC_ITER_H | |
9 | ||
0781e79e ML |
10 | #include <linux/kernel.h> |
11 | #include <linux/bug.h> | |
b1fb2c52 | 12 | #include <linux/errno.h> |
3d75ca0a | 13 | #include <linux/mm.h> |
0781e79e ML |
14 | |
15 | /* | |
16 | * was unsigned short, but we might as well be ready for > 64kB I/O pages | |
17 | */ | |
18 | struct bio_vec { | |
19 | struct page *bv_page; | |
20 | unsigned int bv_len; | |
21 | unsigned int bv_offset; | |
22 | }; | |
23 | ||
24 | struct bvec_iter { | |
25 | sector_t bi_sector; /* device address in 512 byte | |
26 | sectors */ | |
27 | unsigned int bi_size; /* residual I/O count */ | |
28 | ||
29 | unsigned int bi_idx; /* current index into bvl_vec */ | |
30 | ||
31 | unsigned int bi_bvec_done; /* number of bytes completed in | |
32 | current bvec */ | |
33 | }; | |
8fc55455 | 34 | |
6dc4f100 ML |
35 | struct bvec_iter_all { |
36 | struct bio_vec bv; | |
37 | int idx; | |
38 | unsigned done; | |
39 | }; | |
40 | ||
8fc55455 ML |
41 | /* |
42 | * various member access, note that bio_data should of course not be used | |
43 | * on highmem page vectors | |
44 | */ | |
45 | #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx]) | |
46 | ||
3d75ca0a ML |
47 | /* multi-page (mp_bvec) helpers */ |
48 | #define mp_bvec_iter_page(bvec, iter) \ | |
8fc55455 ML |
49 | (__bvec_iter_bvec((bvec), (iter))->bv_page) |
50 | ||
3d75ca0a | 51 | #define mp_bvec_iter_len(bvec, iter) \ |
8fc55455 ML |
52 | min((iter).bi_size, \ |
53 | __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done) | |
54 | ||
3d75ca0a | 55 | #define mp_bvec_iter_offset(bvec, iter) \ |
8fc55455 ML |
56 | (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done) |
57 | ||
3d75ca0a ML |
58 | #define mp_bvec_iter_page_idx(bvec, iter) \ |
59 | (mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE) | |
60 | ||
61 | #define mp_bvec_iter_bvec(bvec, iter) \ | |
62 | ((struct bio_vec) { \ | |
63 | .bv_page = mp_bvec_iter_page((bvec), (iter)), \ | |
64 | .bv_len = mp_bvec_iter_len((bvec), (iter)), \ | |
65 | .bv_offset = mp_bvec_iter_offset((bvec), (iter)), \ | |
66 | }) | |
67 | ||
68 | /* For building single-page bvec in flight */ | |
69 | #define bvec_iter_offset(bvec, iter) \ | |
70 | (mp_bvec_iter_offset((bvec), (iter)) % PAGE_SIZE) | |
71 | ||
72 | #define bvec_iter_len(bvec, iter) \ | |
73 | min_t(unsigned, mp_bvec_iter_len((bvec), (iter)), \ | |
74 | PAGE_SIZE - bvec_iter_offset((bvec), (iter))) | |
75 | ||
76 | #define bvec_iter_page(bvec, iter) \ | |
52d52d1c CH |
77 | (mp_bvec_iter_page((bvec), (iter)) + \ |
78 | mp_bvec_iter_page_idx((bvec), (iter))) | |
3d75ca0a | 79 | |
8fc55455 ML |
80 | #define bvec_iter_bvec(bvec, iter) \ |
81 | ((struct bio_vec) { \ | |
82 | .bv_page = bvec_iter_page((bvec), (iter)), \ | |
83 | .bv_len = bvec_iter_len((bvec), (iter)), \ | |
84 | .bv_offset = bvec_iter_offset((bvec), (iter)), \ | |
85 | }) | |
86 | ||
b1fb2c52 DM |
87 | static inline bool bvec_iter_advance(const struct bio_vec *bv, |
88 | struct bvec_iter *iter, unsigned bytes) | |
8fc55455 | 89 | { |
b1fb2c52 DM |
90 | if (WARN_ONCE(bytes > iter->bi_size, |
91 | "Attempted to advance past end of bvec iter\n")) { | |
92 | iter->bi_size = 0; | |
93 | return false; | |
94 | } | |
8fc55455 ML |
95 | |
96 | while (bytes) { | |
5b88a17c CH |
97 | const struct bio_vec *cur = bv + iter->bi_idx; |
98 | unsigned len = min3(bytes, iter->bi_size, | |
99 | cur->bv_len - iter->bi_bvec_done); | |
8fc55455 ML |
100 | |
101 | bytes -= len; | |
102 | iter->bi_size -= len; | |
103 | iter->bi_bvec_done += len; | |
104 | ||
5b88a17c | 105 | if (iter->bi_bvec_done == cur->bv_len) { |
8fc55455 ML |
106 | iter->bi_bvec_done = 0; |
107 | iter->bi_idx++; | |
108 | } | |
109 | } | |
b1fb2c52 | 110 | return true; |
8fc55455 ML |
111 | } |
112 | ||
113 | #define for_each_bvec(bvl, bio_vec, iter, start) \ | |
114 | for (iter = (start); \ | |
115 | (iter).bi_size && \ | |
116 | ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \ | |
117 | bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len)) | |
118 | ||
3c892a09 ML |
119 | /* for iterating one bio from start to end */ |
120 | #define BVEC_ITER_ALL_INIT (struct bvec_iter) \ | |
121 | { \ | |
122 | .bi_sector = 0, \ | |
123 | .bi_size = UINT_MAX, \ | |
124 | .bi_idx = 0, \ | |
125 | .bi_bvec_done = 0, \ | |
126 | } | |
127 | ||
6dc4f100 ML |
128 | static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all) |
129 | { | |
6dc4f100 | 130 | iter_all->done = 0; |
1200e07f | 131 | iter_all->idx = 0; |
6dc4f100 ML |
132 | |
133 | return &iter_all->bv; | |
134 | } | |
135 | ||
1200e07f ML |
136 | static inline void bvec_advance(const struct bio_vec *bvec, |
137 | struct bvec_iter_all *iter_all) | |
6dc4f100 ML |
138 | { |
139 | struct bio_vec *bv = &iter_all->bv; | |
140 | ||
1200e07f | 141 | if (iter_all->done) { |
52d52d1c | 142 | bv->bv_page++; |
6dc4f100 ML |
143 | bv->bv_offset = 0; |
144 | } else { | |
b8753433 | 145 | bv->bv_page = bvec->bv_page + (bvec->bv_offset >> PAGE_SHIFT); |
6bedf00e | 146 | bv->bv_offset = bvec->bv_offset & ~PAGE_MASK; |
6dc4f100 ML |
147 | } |
148 | bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset, | |
149 | bvec->bv_len - iter_all->done); | |
1200e07f ML |
150 | iter_all->done += bv->bv_len; |
151 | ||
152 | if (iter_all->done == bvec->bv_len) { | |
153 | iter_all->idx++; | |
154 | iter_all->done = 0; | |
155 | } | |
6dc4f100 ML |
156 | } |
157 | ||
45a3fb95 ML |
158 | /* |
159 | * Get the last single-page segment from the multi-page bvec and store it | |
160 | * in @seg | |
161 | */ | |
162 | static inline void mp_bvec_last_segment(const struct bio_vec *bvec, | |
163 | struct bio_vec *seg) | |
164 | { | |
165 | unsigned total = bvec->bv_offset + bvec->bv_len; | |
166 | unsigned last_page = (total - 1) / PAGE_SIZE; | |
167 | ||
52d52d1c | 168 | seg->bv_page = bvec->bv_page + last_page; |
45a3fb95 ML |
169 | |
170 | /* the whole segment is inside the last page */ | |
171 | if (bvec->bv_offset >= last_page * PAGE_SIZE) { | |
172 | seg->bv_offset = bvec->bv_offset % PAGE_SIZE; | |
173 | seg->bv_len = bvec->bv_len; | |
174 | } else { | |
175 | seg->bv_offset = 0; | |
176 | seg->bv_len = total - last_page * PAGE_SIZE; | |
177 | } | |
178 | } | |
179 | ||
8fc55455 | 180 | #endif /* __LINUX_BVEC_ITER_H */ |