• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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/mm.h>
16 #include <linux/types.h>
17 
18 struct page;
19 
20 /**
21  * struct bio_vec - a contiguous range of physical memory addresses
22  * @bv_page:   First page associated with the address range.
23  * @bv_len:    Number of bytes in the address range.
24  * @bv_offset: Start of the address range relative to the start of @bv_page.
25  *
26  * The following holds for a bvec if n * PAGE_SIZE < bv_offset + bv_len:
27  *
28  *   nth_page(@bv_page, n) == @bv_page + n
29  *
30  * This holds because page_is_mergeable() checks the above property.
31  */
32 struct bio_vec {
33 	struct page	*bv_page;
34 	unsigned int	bv_len;
35 	unsigned int	bv_offset;
36 };
37 
38 /**
39  * bvec_set_page - initialize a bvec based off a struct page
40  * @bv:		bvec to initialize
41  * @page:	page the bvec should point to
42  * @len:	length of the bvec
43  * @offset:	offset into the page
44  */
bvec_set_page(struct bio_vec * bv,struct page * page,unsigned int len,unsigned int offset)45 static inline void bvec_set_page(struct bio_vec *bv, struct page *page,
46 		unsigned int len, unsigned int offset)
47 {
48 	bv->bv_page = page;
49 	bv->bv_len = len;
50 	bv->bv_offset = offset;
51 }
52 
53 struct bvec_iter {
54 	sector_t		bi_sector;	/* device address in 512 byte
55 						   sectors */
56 	unsigned int		bi_size;	/* residual I/O count */
57 
58 	unsigned int		bi_idx;		/* current index into bvl_vec */
59 
60 	unsigned int            bi_bvec_done;	/* number of bytes completed in
61 						   current bvec */
62 } __packed;
63 
64 struct bvec_iter_all {
65 	struct bio_vec	bv;
66 	int		idx;
67 	unsigned	done;
68 };
69 
70 /*
71  * various member access, note that bio_data should of course not be used
72  * on highmem page vectors
73  */
74 #define __bvec_iter_bvec(bvec, iter)	(&(bvec)[(iter).bi_idx])
75 
76 /* multi-page (mp_bvec) helpers */
77 #define mp_bvec_iter_page(bvec, iter)				\
78 	(__bvec_iter_bvec((bvec), (iter))->bv_page)
79 
80 #define mp_bvec_iter_len(bvec, iter)				\
81 	min((iter).bi_size,					\
82 	    __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
83 
84 #define mp_bvec_iter_offset(bvec, iter)				\
85 	(__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
86 
87 #define mp_bvec_iter_page_idx(bvec, iter)			\
88 	(mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE)
89 
90 #define mp_bvec_iter_bvec(bvec, iter)				\
91 ((struct bio_vec) {						\
92 	.bv_page	= mp_bvec_iter_page((bvec), (iter)),	\
93 	.bv_len		= mp_bvec_iter_len((bvec), (iter)),	\
94 	.bv_offset	= mp_bvec_iter_offset((bvec), (iter)),	\
95 })
96 
97 /* For building single-page bvec in flight */
98  #define bvec_iter_offset(bvec, iter)				\
99 	(mp_bvec_iter_offset((bvec), (iter)) % PAGE_SIZE)
100 
101 #define bvec_iter_len(bvec, iter)				\
102 	min_t(unsigned, mp_bvec_iter_len((bvec), (iter)),		\
103 	      PAGE_SIZE - bvec_iter_offset((bvec), (iter)))
104 
105 #define bvec_iter_page(bvec, iter)				\
106 	(mp_bvec_iter_page((bvec), (iter)) +			\
107 	 mp_bvec_iter_page_idx((bvec), (iter)))
108 
109 #define bvec_iter_bvec(bvec, iter)				\
110 ((struct bio_vec) {						\
111 	.bv_page	= bvec_iter_page((bvec), (iter)),	\
112 	.bv_len		= bvec_iter_len((bvec), (iter)),	\
113 	.bv_offset	= bvec_iter_offset((bvec), (iter)),	\
114 })
115 
bvec_iter_advance(const struct bio_vec * bv,struct bvec_iter * iter,unsigned bytes)116 static inline bool bvec_iter_advance(const struct bio_vec *bv,
117 		struct bvec_iter *iter, unsigned bytes)
118 {
119 	unsigned int idx = iter->bi_idx;
120 
121 	if (WARN_ONCE(bytes > iter->bi_size,
122 		     "Attempted to advance past end of bvec iter\n")) {
123 		iter->bi_size = 0;
124 		return false;
125 	}
126 
127 	iter->bi_size -= bytes;
128 	bytes += iter->bi_bvec_done;
129 
130 	while (bytes && bytes >= bv[idx].bv_len) {
131 		bytes -= bv[idx].bv_len;
132 		idx++;
133 	}
134 
135 	iter->bi_idx = idx;
136 	iter->bi_bvec_done = bytes;
137 	return true;
138 }
139 
140 /*
141  * A simpler version of bvec_iter_advance(), @bytes should not span
142  * across multiple bvec entries, i.e. bytes <= bv[i->bi_idx].bv_len
143  */
bvec_iter_advance_single(const struct bio_vec * bv,struct bvec_iter * iter,unsigned int bytes)144 static inline void bvec_iter_advance_single(const struct bio_vec *bv,
145 				struct bvec_iter *iter, unsigned int bytes)
146 {
147 	unsigned int done = iter->bi_bvec_done + bytes;
148 
149 	if (done == bv[iter->bi_idx].bv_len) {
150 		done = 0;
151 		iter->bi_idx++;
152 	}
153 	iter->bi_bvec_done = done;
154 	iter->bi_size -= bytes;
155 }
156 
157 #define for_each_bvec(bvl, bio_vec, iter, start)			\
158 	for (iter = (start);						\
159 	     (iter).bi_size &&						\
160 		((bvl = bvec_iter_bvec((bio_vec), (iter))), 1);	\
161 	     bvec_iter_advance_single((bio_vec), &(iter), (bvl).bv_len))
162 
163 /* for iterating one bio from start to end */
164 #define BVEC_ITER_ALL_INIT (struct bvec_iter)				\
165 {									\
166 	.bi_sector	= 0,						\
167 	.bi_size	= UINT_MAX,					\
168 	.bi_idx		= 0,						\
169 	.bi_bvec_done	= 0,						\
170 }
171 
bvec_init_iter_all(struct bvec_iter_all * iter_all)172 static inline struct bio_vec *bvec_init_iter_all(struct bvec_iter_all *iter_all)
173 {
174 	iter_all->done = 0;
175 	iter_all->idx = 0;
176 
177 	return &iter_all->bv;
178 }
179 
bvec_advance(const struct bio_vec * bvec,struct bvec_iter_all * iter_all)180 static inline void bvec_advance(const struct bio_vec *bvec,
181 				struct bvec_iter_all *iter_all)
182 {
183 	struct bio_vec *bv = &iter_all->bv;
184 
185 	if (iter_all->done) {
186 		bv->bv_page++;
187 		bv->bv_offset = 0;
188 	} else {
189 		bv->bv_page = bvec->bv_page + (bvec->bv_offset >> PAGE_SHIFT);
190 		bv->bv_offset = bvec->bv_offset & ~PAGE_MASK;
191 	}
192 	bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset,
193 			   bvec->bv_len - iter_all->done);
194 	iter_all->done += bv->bv_len;
195 
196 	if (iter_all->done == bvec->bv_len) {
197 		iter_all->idx++;
198 		iter_all->done = 0;
199 	}
200 }
201 
202 /**
203  * bvec_kmap_local - map a bvec into the kernel virtual address space
204  * @bvec: bvec to map
205  *
206  * Must be called on single-page bvecs only.  Call kunmap_local on the returned
207  * address to unmap.
208  */
bvec_kmap_local(struct bio_vec * bvec)209 static inline void *bvec_kmap_local(struct bio_vec *bvec)
210 {
211 	return kmap_local_page(bvec->bv_page) + bvec->bv_offset;
212 }
213 
214 /**
215  * memcpy_from_bvec - copy data from a bvec
216  * @bvec: bvec to copy from
217  *
218  * Must be called on single-page bvecs only.
219  */
memcpy_from_bvec(char * to,struct bio_vec * bvec)220 static inline void memcpy_from_bvec(char *to, struct bio_vec *bvec)
221 {
222 	memcpy_from_page(to, bvec->bv_page, bvec->bv_offset, bvec->bv_len);
223 }
224 
225 /**
226  * memcpy_to_bvec - copy data to a bvec
227  * @bvec: bvec to copy to
228  *
229  * Must be called on single-page bvecs only.
230  */
memcpy_to_bvec(struct bio_vec * bvec,const char * from)231 static inline void memcpy_to_bvec(struct bio_vec *bvec, const char *from)
232 {
233 	memcpy_to_page(bvec->bv_page, bvec->bv_offset, from, bvec->bv_len);
234 }
235 
236 /**
237  * memzero_bvec - zero all data in a bvec
238  * @bvec: bvec to zero
239  *
240  * Must be called on single-page bvecs only.
241  */
memzero_bvec(struct bio_vec * bvec)242 static inline void memzero_bvec(struct bio_vec *bvec)
243 {
244 	memzero_page(bvec->bv_page, bvec->bv_offset, bvec->bv_len);
245 }
246 
247 /**
248  * bvec_virt - return the virtual address for a bvec
249  * @bvec: bvec to return the virtual address for
250  *
251  * Note: the caller must ensure that @bvec->bv_page is not a highmem page.
252  */
bvec_virt(struct bio_vec * bvec)253 static inline void *bvec_virt(struct bio_vec *bvec)
254 {
255 	WARN_ON_ONCE(PageHighMem(bvec->bv_page));
256 	return page_address(bvec->bv_page) + bvec->bv_offset;
257 }
258 
259 #endif /* __LINUX_BVEC_H */
260