| 1 |
|
| 2 | |
| 3 | * bvec iterator
|
| 4 | *
|
| 5 | * Copyright (C) 2001 Ming Lei <[email protected]>
|
| 6 |
|
| 7 | #ifndef __LINUX_BVEC_H
|
| 8 | #define __LINUX_BVEC_H
|
| 9 |
|
| 10 | #include <linux/highmem.h>
|
| 11 | #include <linux/bug.h>
|
| 12 | #include <linux/errno.h>
|
| 13 | #include <linux/limits.h>
|
| 14 | #include <linux/minmax.h>
|
| 15 | #include <linux/types.h>
|
| 16 |
|
| 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.
|
| 30 |
|
| 31 | struct bio_vec {
|
| 32 | struct page *bv_page;
|
| 33 | unsigned int bv_len;
|
| 34 | unsigned int bv_offset;
|
| 35 | };
|
| 36 |
|
| 37 | |
| 38 | * bvec_set_page - initialize a bvec based off a struct page
|
| 39 | * @bv: bvec to initialize
|
| 40 | * @page: page the bvec should point to
|
| 41 | * @len: length of the bvec
|
| 42 | * @offset: offset into the page
|
| 43 |
|
| 44 | static inline void bvec_set_page(struct bio_vec *bv, struct page *page,
|
| 45 | unsigned int len, unsigned int offset)
|
| 46 | {
|
| 47 | bv->bv_page = page;
|
| 48 | bv->bv_len = len;
|
| 49 | bv->bv_offset = offset;
|
| 50 | }
|
| 51 |
|
| 52 | |
| 53 | * bvec_set_folio - initialize a bvec based off a struct folio
|
| 54 | * @bv: bvec to initialize
|
| 55 | * @folio: folio the bvec should point to
|
| 56 | * @len: length of the bvec
|
| 57 | * @offset: offset into the folio
|
| 58 |
|
| 59 | static inline void bvec_set_folio(struct bio_vec *bv, struct folio *folio,
|
| 60 | size_t len, size_t offset)
|
| 61 | {
|
| 62 | unsigned long nr = offset / PAGE_SIZE;
|
| 63 |
|
| 64 | WARN_ON_ONCE(len > UINT_MAX);
|
| 65 | bvec_set_page(bv, folio_page(folio, nr), len, offset % PAGE_SIZE);
|
| 66 | }
|
| 67 |
|
| 68 | |
| 69 | * bvec_set_virt - initialize a bvec based on a virtual address
|
| 70 | * @bv: bvec to initialize
|
| 71 | * @vaddr: virtual address to set the bvec to
|
| 72 | * @len: length of the bvec
|
| 73 |
|
| 74 | static inline void bvec_set_virt(struct bio_vec *bv, void *vaddr,
|
| 75 | unsigned int len)
|
| 76 | {
|
| 77 | bvec_set_page(bv, virt_to_page(vaddr), len, offset_in_page(vaddr));
|
| 78 | }
|
| 79 |
|
| 80 | struct bvec_iter {
|
| 81 | sector_t bi_sector; |
| 82 | sectors
|
| 83 | unsigned int bi_size;
|
| 84 |
|
| 85 | unsigned int bi_idx;
|
| 86 |
|
| 87 | unsigned int bi_bvec_done; |
| 88 | current bvec
|
| 89 | } __packed __aligned(4);
|
| 90 |
|
| 91 | struct bvec_iter_all {
|
| 92 | struct bio_vec bv;
|
| 93 | int idx;
|
| 94 | unsigned done;
|
| 95 | };
|
| 96 |
|
| 97 | |
| 98 | * various member access, note that bio_data should of course not be used
|
| 99 | * on highmem page vectors
|
| 100 |
|
| 101 | #define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
|
| 102 |
|
| 103 |
|
| 104 | #define mp_bvec_iter_page(bvec, iter) \
|
| 105 | (__bvec_iter_bvec((bvec), (iter))->bv_page)
|
| 106 |
|
| 107 | #define mp_bvec_iter_len(bvec, iter) \
|
| 108 | min((iter).bi_size, \
|
| 109 | __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
|
| 110 |
|
| 111 | #define mp_bvec_iter_offset(bvec, iter) \
|
| 112 | (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
|
| 113 |
|
| 114 | #define mp_bvec_iter_page_idx(bvec, iter) \
|
| 115 | (mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE)
|
| 116 |
|
| 117 | #define mp_bvec_iter_bvec(bvec, iter) \
|
| 118 | ((struct bio_vec) { \
|
| 119 | .bv_page = mp_bvec_iter_page((bvec), (iter)), \
|
| 120 | .bv_len = mp_bvec_iter_len((bvec), (iter)), \
|
| 121 | .bv_offset = mp_bvec_iter_offset((bvec), (iter)), \
|
| 122 | })
|
| 123 |
|
| 124 |
|
| 125 | #define bvec_iter_offset(bvec, iter) \
|
| 126 | (mp_bvec_iter_offset((bvec), (iter)) % PAGE_SIZE)
|
| 127 |
|
| 128 | #define bvec_iter_len(bvec, iter) \
|
| 129 | min_t(unsigned, mp_bvec_iter_len((bvec), (iter)), \
|
| 130 | PAGE_SIZE - bvec_iter_offset((bvec), (iter)))
|
| 131 |
|
| 132 | #define bvec_iter_page(bvec, iter) \
|
| 133 | (mp_bvec_iter_page((bvec), (iter)) + \
|
| 134 | mp_bvec_iter_page_idx((bvec), (iter)))
|
| 135 |
|
| 136 | #define bvec_iter_bvec(bvec, iter) \
|
| 137 | ((struct bio_vec) { \
|
| 138 | .bv_page = bvec_iter_page((bvec), (iter)), \
|
| 139 | .bv_len = bvec_iter_len((bvec), (iter)), \
|
| 140 | .bv_offset = bvec_iter_offset((bvec), (iter)), \
|
| 141 | })
|
| 142 |
|
| 143 | static inline bool bvec_iter_advance(const struct bio_vec *bv,
|
| 144 | struct bvec_iter *iter, unsigned bytes)
|
| 145 | {
|
| 146 | unsigned int idx = iter->bi_idx;
|
| 147 |
|
| 148 | if (WARN_ONCE(bytes > iter->bi_size,
|
| 149 | "Attempted to advance past end of bvec iter\n")) {
|
| 150 | iter->bi_size = 0;
|
| 151 | return false;
|
| 152 | }
|
| 153 |
|
| 154 | iter->bi_size -= bytes;
|
| 155 | bytes += iter->bi_bvec_done;
|
| 156 |
|
| 157 | while (bytes && bytes >= bv[idx].bv_len) {
|
| 158 | bytes -= bv[idx].bv_len;
|
| 159 | idx++;
|
| 160 | }
|
| 161 |
|
| 162 | iter->bi_idx = idx;
|
| 163 | iter->bi_bvec_done = bytes;
|
| 164 | return true;
|
| 165 | }
|
| 166 |
|
| 167 | |
| 168 | * A simpler version of bvec_iter_advance(), @bytes should not span
|
| 169 | * across multiple bvec entries, i.e. bytes <= bv[i->bi_idx].bv_len
|
| 170 |
|
| 171 | static inline void bvec_iter_advance_single(const struct bio_vec *bv,
|
| 172 | struct bvec_iter *iter, unsigned int bytes)
|
| 173 | {
|
| 174 | unsigned int done = iter->bi_bvec_done + bytes;
|
| 175 |
|
| 176 | if (done == bv[iter->bi_idx].bv_len) {
|
| 177 | done = 0;
|
| 178 | iter->bi_idx++;
|
| 179 | }
|
| 180 | iter->bi_bvec_done = done;
|
| 181 | iter->bi_size -= bytes;
|
| 182 | }
|
| 183 |
|
| 184 | #define for_each_bvec(bvl, bio_vec, iter, start) \
|
| 185 | for (iter = (start); \
|
| 186 | (iter).bi_size && \
|
| 187 | ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
|
| 188 | bvec_iter_advance_single((bio_vec), &(iter), (bvl).bv_len))
|
| 189 |
|
| 190 | #define for_each_mp_bvec(bvl, bio_vec, iter, start) \
|
| 191 | for (iter = (start); \
|
| 192 | (iter).bi_size && \
|
| 193 | ((bvl = mp_bvec_iter_bvec((bio_vec), (iter))), 1); \
|
| 194 | bvec_iter_advance_single((bio_vec), &(iter), (bvl).bv_len))
|
| 195 |
|
| 196 |
|
| 197 | #define BVEC_ITER_ALL_INIT (struct bvec_iter) \
|
| 198 | { \
|
| 199 | .bi_sector = 0, \
|
| 200 | .bi_size = UINT_MAX, \
|
| 201 | .bi_idx = 0, \
|
| 202 | .bi_bvec_done = 0, \
|
| 203 | }
|
| 204 |
|
| 205 | static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all)
|
| 206 | {
|
| 207 | iter_all->done = 0;
|
| 208 | iter_all->idx = 0;
|
| 209 |
|
| 210 | return &iter_all->bv;
|
| 211 | }
|
| 212 |
|
| 213 | static inline void bvec_advance(const struct bio_vec *bvec,
|
| 214 | struct bvec_iter_all *iter_all)
|
| 215 | {
|
| 216 | struct bio_vec *bv = &iter_all->bv;
|
| 217 |
|
| 218 | if (iter_all->done) {
|
| 219 | bv->bv_page++;
|
| 220 | bv->bv_offset = 0;
|
| 221 | } else {
|
| 222 | bv->bv_page = bvec->bv_page + (bvec->bv_offset >> PAGE_SHIFT);
|
| 223 | bv->bv_offset = bvec->bv_offset & ~PAGE_MASK;
|
| 224 | }
|
| 225 | bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset,
|
| 226 | bvec->bv_len - iter_all->done);
|
| 227 | iter_all->done += bv->bv_len;
|
| 228 |
|
| 229 | if (iter_all->done == bvec->bv_len) {
|
| 230 | iter_all->idx++;
|
| 231 | iter_all->done = 0;
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | |
| 236 | * bvec_kmap_local - map a bvec into the kernel virtual address space
|
| 237 | * @bvec: bvec to map
|
| 238 | *
|
| 239 | * Must be called on single-page bvecs only. Call kunmap_local on the returned
|
| 240 | * address to unmap.
|
| 241 |
|
| 242 | static inline void *bvec_kmap_local(struct bio_vec *bvec)
|
| 243 | {
|
| 244 | return kmap_local_page(bvec->bv_page) + bvec->bv_offset;
|
| 245 | }
|
| 246 |
|
| 247 | |
| 248 | * memcpy_from_bvec - copy data from a bvec
|
| 249 | * @bvec: bvec to copy from
|
| 250 | *
|
| 251 | * Must be called on single-page bvecs only.
|
| 252 |
|
| 253 | static inline void memcpy_from_bvec(char *to, struct bio_vec *bvec)
|
| 254 | {
|
| 255 | memcpy_from_page(to, bvec->bv_page, bvec->bv_offset, bvec->bv_len);
|
| 256 | }
|
| 257 |
|
| 258 | |
| 259 | * memcpy_to_bvec - copy data to a bvec
|
| 260 | * @bvec: bvec to copy to
|
| 261 | *
|
| 262 | * Must be called on single-page bvecs only.
|
| 263 |
|
| 264 | static inline void memcpy_to_bvec(struct bio_vec *bvec, const char *from)
|
| 265 | {
|
| 266 | memcpy_to_page(bvec->bv_page, bvec->bv_offset, from, bvec->bv_len);
|
| 267 | }
|
| 268 |
|
| 269 | |
| 270 | * memzero_bvec - zero all data in a bvec
|
| 271 | * @bvec: bvec to zero
|
| 272 | *
|
| 273 | * Must be called on single-page bvecs only.
|
| 274 |
|
| 275 | static inline void memzero_bvec(struct bio_vec *bvec)
|
| 276 | {
|
| 277 | memzero_page(bvec->bv_page, bvec->bv_offset, bvec->bv_len);
|
| 278 | }
|
| 279 |
|
| 280 | |
| 281 | * bvec_virt - return the virtual address for a bvec
|
| 282 | * @bvec: bvec to return the virtual address for
|
| 283 | *
|
| 284 | * Note: the caller must ensure that @bvec->bv_page is not a highmem page.
|
| 285 |
|
| 286 | static inline void *bvec_virt(struct bio_vec *bvec)
|
| 287 | {
|
| 288 | WARN_ON_ONCE(PageHighMem(bvec->bv_page));
|
| 289 | return page_address(bvec->bv_page) + bvec->bv_offset;
|
| 290 | }
|
| 291 |
|
| 292 | |
| 293 | * bvec_phys - return the physical address for a bvec
|
| 294 | * @bvec: bvec to return the physical address for
|
| 295 |
|
| 296 | static inline phys_addr_t bvec_phys(const struct bio_vec *bvec)
|
| 297 | {
|
| 298 | return page_to_phys(bvec->bv_page) + bvec->bv_offset;
|
| 299 | }
|
| 300 |
|
| 301 | #endif
|