1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * bvec iterator
4  *
5  * Copyright (C) 2001 Ming Lei <ming.lei@canonical.com>
6  */
7 #ifndef __LINUX_BVEC_ITER_H
8 #define __LINUX_BVEC_ITER_H
9 
10 #include <linux/kernel.h>
11 #include <linux/bug.h>
12 #include <linux/errno.h>
13 #include <linux/mm.h>
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 };
34 
35 struct bvec_iter_all {
36 	struct bio_vec	bv;
37 	int		idx;
38 	unsigned	done;
39 };
40 
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 
47 /* multi-page (mp_bvec) helpers */
48 #define mp_bvec_iter_page(bvec, iter)				\
49 	(__bvec_iter_bvec((bvec), (iter))->bv_page)
50 
51 #define mp_bvec_iter_len(bvec, iter)				\
52 	min((iter).bi_size,					\
53 	    __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
54 
55 #define mp_bvec_iter_offset(bvec, iter)				\
56 	(__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
57 
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)				\
77 	(mp_bvec_iter_page((bvec), (iter)) +			\
78 	 mp_bvec_iter_page_idx((bvec), (iter)))
79 
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 
bvec_iter_advance(const struct bio_vec * bv,struct bvec_iter * iter,unsigned bytes)87 static inline bool bvec_iter_advance(const struct bio_vec *bv,
88 		struct bvec_iter *iter, unsigned bytes)
89 {
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 	}
95 
96 	while (bytes) {
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);
100 
101 		bytes -= len;
102 		iter->bi_size -= len;
103 		iter->bi_bvec_done += len;
104 
105 		if (iter->bi_bvec_done == cur->bv_len) {
106 			iter->bi_bvec_done = 0;
107 			iter->bi_idx++;
108 		}
109 	}
110 	return true;
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 
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 
bvec_init_iter_all(struct bvec_iter_all * iter_all)128 static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all)
129 {
130 	iter_all->done = 0;
131 	iter_all->idx = 0;
132 
133 	return &iter_all->bv;
134 }
135 
bvec_advance(const struct bio_vec * bvec,struct bvec_iter_all * iter_all)136 static inline void bvec_advance(const struct bio_vec *bvec,
137 				struct bvec_iter_all *iter_all)
138 {
139 	struct bio_vec *bv = &iter_all->bv;
140 
141 	if (iter_all->done) {
142 		bv->bv_page++;
143 		bv->bv_offset = 0;
144 	} else {
145 		bv->bv_page = bvec->bv_page + (bvec->bv_offset >> PAGE_SHIFT);
146 		bv->bv_offset = bvec->bv_offset & ~PAGE_MASK;
147 	}
148 	bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset,
149 			   bvec->bv_len - iter_all->done);
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 	}
156 }
157 
158 /*
159  * Get the last single-page segment from the multi-page bvec and store it
160  * in @seg
161  */
mp_bvec_last_segment(const struct bio_vec * bvec,struct bio_vec * seg)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 
168 	seg->bv_page = bvec->bv_page + last_page;
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 
180 #endif /* __LINUX_BVEC_ITER_H */
181