blob: 543bb5fbe7c7bcb15ef4a8db3e5622584b27ee05 [file] [log] [blame]
Ming Lei8fc55452016-06-09 10:00:58 -06001/*
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
Ming Lei0781e792016-05-30 21:34:30 +080023#include <linux/kernel.h>
24#include <linux/bug.h>
Dmitry Monakhovb1fb2c52017-06-29 11:31:13 -070025#include <linux/errno.h>
Ming Lei0781e792016-05-30 21:34:30 +080026
27/*
28 * was unsigned short, but we might as well be ready for > 64kB I/O pages
29 */
30struct bio_vec {
31 struct page *bv_page;
32 unsigned int bv_len;
33 unsigned int bv_offset;
34};
35
36struct 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
Dmitry Monakhovf9df1cd2017-06-29 11:31:14 -070043 unsigned int bi_done; /* number of bytes completed */
44
Ming Lei0781e792016-05-30 21:34:30 +080045 unsigned int bi_bvec_done; /* number of bytes completed in
46 current bvec */
Zhen Kongee7bdc62019-03-14 10:55:19 -070047 u64 bi_dun; /* DUN setting for bio */
Ming Lei0781e792016-05-30 21:34:30 +080048};
Ming Lei8fc55452016-06-09 10:00:58 -060049
50/*
51 * various member access, note that bio_data should of course not be used
52 * on highmem page vectors
53 */
54#define __bvec_iter_bvec(bvec, iter) (&(bvec)[(iter).bi_idx])
55
56#define bvec_iter_page(bvec, iter) \
57 (__bvec_iter_bvec((bvec), (iter))->bv_page)
58
59#define bvec_iter_len(bvec, iter) \
60 min((iter).bi_size, \
61 __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
62
63#define bvec_iter_offset(bvec, iter) \
64 (__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
65
66#define bvec_iter_bvec(bvec, iter) \
67((struct bio_vec) { \
68 .bv_page = bvec_iter_page((bvec), (iter)), \
69 .bv_len = bvec_iter_len((bvec), (iter)), \
70 .bv_offset = bvec_iter_offset((bvec), (iter)), \
71})
72
Dmitry Monakhovb1fb2c52017-06-29 11:31:13 -070073static inline bool bvec_iter_advance(const struct bio_vec *bv,
74 struct bvec_iter *iter, unsigned bytes)
Ming Lei8fc55452016-06-09 10:00:58 -060075{
Dmitry Monakhovb1fb2c52017-06-29 11:31:13 -070076 if (WARN_ONCE(bytes > iter->bi_size,
77 "Attempted to advance past end of bvec iter\n")) {
78 iter->bi_size = 0;
79 return false;
80 }
Ming Lei8fc55452016-06-09 10:00:58 -060081
82 while (bytes) {
Johannes Berg1ea049b2016-08-11 10:15:56 +020083 unsigned iter_len = bvec_iter_len(bv, *iter);
84 unsigned len = min(bytes, iter_len);
Ming Lei8fc55452016-06-09 10:00:58 -060085
86 bytes -= len;
87 iter->bi_size -= len;
88 iter->bi_bvec_done += len;
Dmitry Monakhovf9df1cd2017-06-29 11:31:14 -070089 iter->bi_done += len;
Ming Lei8fc55452016-06-09 10:00:58 -060090
91 if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) {
92 iter->bi_bvec_done = 0;
93 iter->bi_idx++;
94 }
95 }
Dmitry Monakhovb1fb2c52017-06-29 11:31:13 -070096 return true;
Ming Lei8fc55452016-06-09 10:00:58 -060097}
98
Dmitry Monakhovf9df1cd2017-06-29 11:31:14 -070099static inline bool bvec_iter_rewind(const struct bio_vec *bv,
100 struct bvec_iter *iter,
101 unsigned int bytes)
102{
103 while (bytes) {
104 unsigned len = min(bytes, iter->bi_bvec_done);
105
106 if (iter->bi_bvec_done == 0) {
107 if (WARN_ONCE(iter->bi_idx == 0,
108 "Attempted to rewind iter beyond "
109 "bvec's boundaries\n")) {
110 return false;
111 }
112 iter->bi_idx--;
113 iter->bi_bvec_done = __bvec_iter_bvec(bv, *iter)->bv_len;
114 continue;
115 }
116 bytes -= len;
117 iter->bi_size += len;
118 iter->bi_bvec_done -= len;
119 }
120 return true;
121}
122
Ming Lei8fc55452016-06-09 10:00:58 -0600123#define for_each_bvec(bvl, bio_vec, iter, start) \
124 for (iter = (start); \
125 (iter).bi_size && \
126 ((bvl = bvec_iter_bvec((bio_vec), (iter))), 1); \
127 bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len))
128
Ming Lei3c892a02017-12-18 20:22:07 +0800129/* for iterating one bio from start to end */
130#define BVEC_ITER_ALL_INIT (struct bvec_iter) \
131{ \
132 .bi_sector = 0, \
133 .bi_size = UINT_MAX, \
134 .bi_idx = 0, \
135 .bi_bvec_done = 0, \
136}
137
Ming Lei8fc55452016-06-09 10:00:58 -0600138#endif /* __LINUX_BVEC_ITER_H */