xref: /openbmc/linux/include/linux/bvec.h (revision 31af04cd)
1 /*
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 
23 #include <linux/kernel.h>
24 #include <linux/bug.h>
25 #include <linux/errno.h>
26 
27 /*
28  * was unsigned short, but we might as well be ready for > 64kB I/O pages
29  */
30 struct bio_vec {
31 	struct page	*bv_page;
32 	unsigned int	bv_len;
33 	unsigned int	bv_offset;
34 };
35 
36 struct 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 
43 	unsigned int            bi_bvec_done;	/* number of bytes completed in
44 						   current bvec */
45 };
46 
47 /*
48  * various member access, note that bio_data should of course not be used
49  * on highmem page vectors
50  */
51 #define __bvec_iter_bvec(bvec, iter)	(&(bvec)[(iter).bi_idx])
52 
53 #define bvec_iter_page(bvec, iter)				\
54 	(__bvec_iter_bvec((bvec), (iter))->bv_page)
55 
56 #define bvec_iter_len(bvec, iter)				\
57 	min((iter).bi_size,					\
58 	    __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
59 
60 #define bvec_iter_offset(bvec, iter)				\
61 	(__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
62 
63 #define bvec_iter_bvec(bvec, iter)				\
64 ((struct bio_vec) {						\
65 	.bv_page	= bvec_iter_page((bvec), (iter)),	\
66 	.bv_len		= bvec_iter_len((bvec), (iter)),	\
67 	.bv_offset	= bvec_iter_offset((bvec), (iter)),	\
68 })
69 
70 static inline bool bvec_iter_advance(const struct bio_vec *bv,
71 		struct bvec_iter *iter, unsigned bytes)
72 {
73 	if (WARN_ONCE(bytes > iter->bi_size,
74 		     "Attempted to advance past end of bvec iter\n")) {
75 		iter->bi_size = 0;
76 		return false;
77 	}
78 
79 	while (bytes) {
80 		unsigned iter_len = bvec_iter_len(bv, *iter);
81 		unsigned len = min(bytes, iter_len);
82 
83 		bytes -= len;
84 		iter->bi_size -= len;
85 		iter->bi_bvec_done += len;
86 
87 		if (iter->bi_bvec_done == __bvec_iter_bvec(bv, *iter)->bv_len) {
88 			iter->bi_bvec_done = 0;
89 			iter->bi_idx++;
90 		}
91 	}
92 	return true;
93 }
94 
95 static inline bool bvec_iter_rewind(const struct bio_vec *bv,
96 				     struct bvec_iter *iter,
97 				     unsigned int bytes)
98 {
99 	while (bytes) {
100 		unsigned len = min(bytes, iter->bi_bvec_done);
101 
102 		if (iter->bi_bvec_done == 0) {
103 			if (WARN_ONCE(iter->bi_idx == 0,
104 				      "Attempted to rewind iter beyond "
105 				      "bvec's boundaries\n")) {
106 				return false;
107 			}
108 			iter->bi_idx--;
109 			iter->bi_bvec_done = __bvec_iter_bvec(bv, *iter)->bv_len;
110 			continue;
111 		}
112 		bytes -= len;
113 		iter->bi_size += len;
114 		iter->bi_bvec_done -= len;
115 	}
116 	return true;
117 }
118 
119 #define for_each_bvec(bvl, bio_vec, iter, start)			\
120 	for (iter = (start);						\
121 	     (iter).bi_size &&						\
122 		((bvl = bvec_iter_bvec((bio_vec), (iter))), 1);	\
123 	     bvec_iter_advance((bio_vec), &(iter), (bvl).bv_len))
124 
125 /* for iterating one bio from start to end */
126 #define BVEC_ITER_ALL_INIT (struct bvec_iter)				\
127 {									\
128 	.bi_sector	= 0,						\
129 	.bi_size	= UINT_MAX,					\
130 	.bi_idx		= 0,						\
131 	.bi_bvec_done	= 0,						\
132 }
133 
134 #endif /* __LINUX_BVEC_ITER_H */
135