.. | .. |
---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 */ |
---|
1 | 2 | /* |
---|
2 | 3 | * bvec iterator |
---|
3 | 4 | * |
---|
4 | 5 | * 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 | 6 | */ |
---|
20 | 7 | #ifndef __LINUX_BVEC_ITER_H |
---|
21 | 8 | #define __LINUX_BVEC_ITER_H |
---|
22 | 9 | |
---|
23 | | -#include <linux/kernel.h> |
---|
24 | 10 | #include <linux/bug.h> |
---|
25 | 11 | #include <linux/errno.h> |
---|
| 12 | +#include <linux/limits.h> |
---|
| 13 | +#include <linux/minmax.h> |
---|
| 14 | +#include <linux/mm.h> |
---|
| 15 | +#include <linux/types.h> |
---|
26 | 16 | |
---|
27 | | -/* |
---|
28 | | - * was unsigned short, but we might as well be ready for > 64kB I/O pages |
---|
| 17 | +struct page; |
---|
| 18 | + |
---|
| 19 | +/** |
---|
| 20 | + * struct bio_vec - a contiguous range of physical memory addresses |
---|
| 21 | + * @bv_page: First page associated with the address range. |
---|
| 22 | + * @bv_len: Number of bytes in the address range. |
---|
| 23 | + * @bv_offset: Start of the address range relative to the start of @bv_page. |
---|
| 24 | + * |
---|
| 25 | + * The following holds for a bvec if n * PAGE_SIZE < bv_offset + bv_len: |
---|
| 26 | + * |
---|
| 27 | + * nth_page(@bv_page, n) == @bv_page + n |
---|
| 28 | + * |
---|
| 29 | + * This holds because page_is_mergeable() checks the above property. |
---|
29 | 30 | */ |
---|
30 | 31 | struct bio_vec { |
---|
31 | 32 | struct page *bv_page; |
---|
.. | .. |
---|
40 | 41 | |
---|
41 | 42 | unsigned int bi_idx; /* current index into bvl_vec */ |
---|
42 | 43 | |
---|
43 | | - unsigned int bi_done; /* number of bytes completed */ |
---|
44 | | - |
---|
45 | 44 | unsigned int bi_bvec_done; /* number of bytes completed in |
---|
46 | 45 | current bvec */ |
---|
| 46 | +}; |
---|
| 47 | + |
---|
| 48 | +struct bvec_iter_all { |
---|
| 49 | + struct bio_vec bv; |
---|
| 50 | + int idx; |
---|
| 51 | + unsigned done; |
---|
47 | 52 | }; |
---|
48 | 53 | |
---|
49 | 54 | /* |
---|
.. | .. |
---|
52 | 57 | */ |
---|
53 | 58 | #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx]) |
---|
54 | 59 | |
---|
55 | | -#define bvec_iter_page(bvec, iter) \ |
---|
| 60 | +/* multi-page (mp_bvec) helpers */ |
---|
| 61 | +#define mp_bvec_iter_page(bvec, iter) \ |
---|
56 | 62 | (__bvec_iter_bvec((bvec), (iter))->bv_page) |
---|
57 | 63 | |
---|
58 | | -#define bvec_iter_len(bvec, iter) \ |
---|
| 64 | +#define mp_bvec_iter_len(bvec, iter) \ |
---|
59 | 65 | min((iter).bi_size, \ |
---|
60 | 66 | __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done) |
---|
61 | 67 | |
---|
62 | | -#define bvec_iter_offset(bvec, iter) \ |
---|
| 68 | +#define mp_bvec_iter_offset(bvec, iter) \ |
---|
63 | 69 | (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done) |
---|
| 70 | + |
---|
| 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 | + (mp_bvec_iter_page((bvec), (iter)) + \ |
---|
| 91 | + mp_bvec_iter_page_idx((bvec), (iter))) |
---|
64 | 92 | |
---|
65 | 93 | #define bvec_iter_bvec(bvec, iter) \ |
---|
66 | 94 | ((struct bio_vec) { \ |
---|
.. | .. |
---|
72 | 100 | static inline bool bvec_iter_advance(const struct bio_vec *bv, |
---|
73 | 101 | struct bvec_iter *iter, unsigned bytes) |
---|
74 | 102 | { |
---|
| 103 | + unsigned int idx = iter->bi_idx; |
---|
| 104 | + |
---|
75 | 105 | if (WARN_ONCE(bytes > iter->bi_size, |
---|
76 | 106 | "Attempted to advance past end of bvec iter\n")) { |
---|
77 | 107 | iter->bi_size = 0; |
---|
78 | 108 | return false; |
---|
79 | 109 | } |
---|
80 | 110 | |
---|
81 | | - while (bytes) { |
---|
82 | | - unsigned iter_len = bvec_iter_len(bv, *iter); |
---|
83 | | - unsigned len = min(bytes, iter_len); |
---|
| 111 | + iter->bi_size -= bytes; |
---|
| 112 | + bytes += iter->bi_bvec_done; |
---|
84 | 113 | |
---|
85 | | - bytes -= len; |
---|
86 | | - iter->bi_size -= len; |
---|
87 | | - iter->bi_bvec_done += len; |
---|
88 | | - iter->bi_done += len; |
---|
89 | | - |
---|
90 | | - if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) { |
---|
91 | | - iter->bi_bvec_done = 0; |
---|
92 | | - iter->bi_idx++; |
---|
93 | | - } |
---|
| 114 | + while (bytes && bytes >= bv[idx].bv_len) { |
---|
| 115 | + bytes -= bv[idx].bv_len; |
---|
| 116 | + idx++; |
---|
94 | 117 | } |
---|
95 | | - return true; |
---|
96 | | -} |
---|
97 | 118 | |
---|
98 | | -static inline bool bvec_iter_rewind(const struct bio_vec *bv, |
---|
99 | | - struct bvec_iter *iter, |
---|
100 | | - unsigned int bytes) |
---|
101 | | -{ |
---|
102 | | - while (bytes) { |
---|
103 | | - unsigned len = min(bytes, iter->bi_bvec_done); |
---|
104 | | - |
---|
105 | | - if (iter->bi_bvec_done == 0) { |
---|
106 | | - if (WARN_ONCE(iter->bi_idx == 0, |
---|
107 | | - "Attempted to rewind iter beyond " |
---|
108 | | - "bvec's boundaries\n")) { |
---|
109 | | - return false; |
---|
110 | | - } |
---|
111 | | - iter->bi_idx--; |
---|
112 | | - iter->bi_bvec_done = __bvec_iter_bvec(bv, *iter)->bv_len; |
---|
113 | | - continue; |
---|
114 | | - } |
---|
115 | | - bytes -= len; |
---|
116 | | - iter->bi_size += len; |
---|
117 | | - iter->bi_bvec_done -= len; |
---|
118 | | - } |
---|
| 119 | + iter->bi_idx = idx; |
---|
| 120 | + iter->bi_bvec_done = bytes; |
---|
119 | 121 | return true; |
---|
120 | 122 | } |
---|
121 | 123 | |
---|
.. | .. |
---|
141 | 143 | .bi_bvec_done = 0, \ |
---|
142 | 144 | } |
---|
143 | 145 | |
---|
| 146 | +static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all) |
---|
| 147 | +{ |
---|
| 148 | + iter_all->done = 0; |
---|
| 149 | + iter_all->idx = 0; |
---|
| 150 | + |
---|
| 151 | + return &iter_all->bv; |
---|
| 152 | +} |
---|
| 153 | + |
---|
| 154 | +static inline void bvec_advance(const struct bio_vec *bvec, |
---|
| 155 | + struct bvec_iter_all *iter_all) |
---|
| 156 | +{ |
---|
| 157 | + struct bio_vec *bv = &iter_all->bv; |
---|
| 158 | + |
---|
| 159 | + if (iter_all->done) { |
---|
| 160 | + bv->bv_page++; |
---|
| 161 | + bv->bv_offset = 0; |
---|
| 162 | + } else { |
---|
| 163 | + bv->bv_page = bvec->bv_page + (bvec->bv_offset >> PAGE_SHIFT); |
---|
| 164 | + bv->bv_offset = bvec->bv_offset & ~PAGE_MASK; |
---|
| 165 | + } |
---|
| 166 | + bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset, |
---|
| 167 | + bvec->bv_len - iter_all->done); |
---|
| 168 | + iter_all->done += bv->bv_len; |
---|
| 169 | + |
---|
| 170 | + if (iter_all->done == bvec->bv_len) { |
---|
| 171 | + iter_all->idx++; |
---|
| 172 | + iter_all->done = 0; |
---|
| 173 | + } |
---|
| 174 | +} |
---|
| 175 | + |
---|
144 | 176 | #endif /* __LINUX_BVEC_ITER_H */ |
---|