criov.c revision 1.2.4.2 1 1.2.4.2 skrll /* $NetBSD: criov.c,v 1.2.4.2 2004/08/03 10:56:25 skrll Exp $ */
2 1.2.4.2 skrll /* $OpenBSD: criov.c,v 1.11 2002/06/10 19:36:43 espie Exp $ */
3 1.2.4.2 skrll
4 1.2.4.2 skrll /*
5 1.2.4.2 skrll * Copyright (c) 1999 Theo de Raadt
6 1.2.4.2 skrll *
7 1.2.4.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.2.4.2 skrll * modification, are permitted provided that the following conditions
9 1.2.4.2 skrll * are met:
10 1.2.4.2 skrll *
11 1.2.4.2 skrll * 1. Redistributions of source code must retain the above copyright
12 1.2.4.2 skrll * notice, this list of conditions and the following disclaimer.
13 1.2.4.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.4.2 skrll * notice, this list of conditions and the following disclaimer in the
15 1.2.4.2 skrll * documentation and/or other materials provided with the distribution.
16 1.2.4.2 skrll * 3. The name of the author may not be used to endorse or promote products
17 1.2.4.2 skrll * derived from this software without specific prior written permission.
18 1.2.4.2 skrll *
19 1.2.4.2 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.2.4.2 skrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.2.4.2 skrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.2.4.2 skrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.2.4.2 skrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.2.4.2 skrll * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.2.4.2 skrll * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.2.4.2 skrll * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.2.4.2 skrll * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.2.4.2 skrll * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.2.4.2 skrll */
30 1.2.4.2 skrll
31 1.2.4.2 skrll #include <sys/cdefs.h>
32 1.2.4.2 skrll __KERNEL_RCSID(0, "$NetBSD: criov.c,v 1.2.4.2 2004/08/03 10:56:25 skrll Exp $");
33 1.2.4.2 skrll
34 1.2.4.2 skrll #include <sys/param.h>
35 1.2.4.2 skrll #include <sys/systm.h>
36 1.2.4.2 skrll #include <sys/proc.h>
37 1.2.4.2 skrll #include <sys/errno.h>
38 1.2.4.2 skrll #include <sys/malloc.h>
39 1.2.4.2 skrll #include <sys/kernel.h>
40 1.2.4.2 skrll #include <sys/mbuf.h>
41 1.2.4.2 skrll
42 1.2.4.2 skrll #include <uvm/uvm_extern.h>
43 1.2.4.2 skrll
44 1.2.4.2 skrll #include <opencrypto/cryptodev.h>
45 1.2.4.2 skrll int cuio_getindx(struct uio *uio, int loc, int *off);
46 1.2.4.2 skrll
47 1.2.4.2 skrll
48 1.2.4.2 skrll void
49 1.2.4.2 skrll cuio_copydata(uio, off, len, cp)
50 1.2.4.2 skrll struct uio *uio;
51 1.2.4.2 skrll int off, len;
52 1.2.4.2 skrll caddr_t cp;
53 1.2.4.2 skrll {
54 1.2.4.2 skrll struct iovec *iov = uio->uio_iov;
55 1.2.4.2 skrll int iol = uio->uio_iovcnt;
56 1.2.4.2 skrll unsigned count;
57 1.2.4.2 skrll
58 1.2.4.2 skrll if (off < 0)
59 1.2.4.2 skrll panic("cuio_copydata: off %d < 0", off);
60 1.2.4.2 skrll if (len < 0)
61 1.2.4.2 skrll panic("cuio_copydata: len %d < 0", len);
62 1.2.4.2 skrll while (off > 0) {
63 1.2.4.2 skrll if (iol == 0)
64 1.2.4.2 skrll panic("iov_copydata: empty in skip");
65 1.2.4.2 skrll if (off < iov->iov_len)
66 1.2.4.2 skrll break;
67 1.2.4.2 skrll off -= iov->iov_len;
68 1.2.4.2 skrll iol--;
69 1.2.4.2 skrll iov++;
70 1.2.4.2 skrll }
71 1.2.4.2 skrll while (len > 0) {
72 1.2.4.2 skrll if (iol == 0)
73 1.2.4.2 skrll panic("cuio_copydata: empty");
74 1.2.4.2 skrll count = min(iov->iov_len - off, len);
75 1.2.4.2 skrll bcopy(((caddr_t)iov->iov_base) + off, cp, count);
76 1.2.4.2 skrll len -= count;
77 1.2.4.2 skrll cp += count;
78 1.2.4.2 skrll off = 0;
79 1.2.4.2 skrll iol--;
80 1.2.4.2 skrll iov++;
81 1.2.4.2 skrll }
82 1.2.4.2 skrll }
83 1.2.4.2 skrll
84 1.2.4.2 skrll void
85 1.2.4.2 skrll cuio_copyback(uio, off, len, cp)
86 1.2.4.2 skrll struct uio *uio;
87 1.2.4.2 skrll int off, len;
88 1.2.4.2 skrll caddr_t cp;
89 1.2.4.2 skrll {
90 1.2.4.2 skrll struct iovec *iov = uio->uio_iov;
91 1.2.4.2 skrll int iol = uio->uio_iovcnt;
92 1.2.4.2 skrll unsigned count;
93 1.2.4.2 skrll
94 1.2.4.2 skrll if (off < 0)
95 1.2.4.2 skrll panic("cuio_copyback: off %d < 0", off);
96 1.2.4.2 skrll if (len < 0)
97 1.2.4.2 skrll panic("cuio_copyback: len %d < 0", len);
98 1.2.4.2 skrll while (off > 0) {
99 1.2.4.2 skrll if (iol == 0)
100 1.2.4.2 skrll panic("cuio_copyback: empty in skip");
101 1.2.4.2 skrll if (off < iov->iov_len)
102 1.2.4.2 skrll break;
103 1.2.4.2 skrll off -= iov->iov_len;
104 1.2.4.2 skrll iol--;
105 1.2.4.2 skrll iov++;
106 1.2.4.2 skrll }
107 1.2.4.2 skrll while (len > 0) {
108 1.2.4.2 skrll if (iol == 0)
109 1.2.4.2 skrll panic("uio_copyback: empty");
110 1.2.4.2 skrll count = min(iov->iov_len - off, len);
111 1.2.4.2 skrll bcopy(cp, ((caddr_t)iov->iov_base) + off, count);
112 1.2.4.2 skrll len -= count;
113 1.2.4.2 skrll cp += count;
114 1.2.4.2 skrll off = 0;
115 1.2.4.2 skrll iol--;
116 1.2.4.2 skrll iov++;
117 1.2.4.2 skrll }
118 1.2.4.2 skrll }
119 1.2.4.2 skrll
120 1.2.4.2 skrll /*
121 1.2.4.2 skrll * Return a pointer to iov/offset of location in iovec list.
122 1.2.4.2 skrll */
123 1.2.4.2 skrll #ifdef __FreeBSD__
124 1.2.4.2 skrll struct iovec *
125 1.2.4.2 skrll cuio_getptr(struct uio *uio, int loc, int *off)
126 1.2.4.2 skrll {
127 1.2.4.2 skrll struct iovec *iov = uio->uio_iov;
128 1.2.4.2 skrll int iol = uio->uio_iovcnt;
129 1.2.4.2 skrll
130 1.2.4.2 skrll while (loc >= 0) {
131 1.2.4.2 skrll /* Normal end of search */
132 1.2.4.2 skrll if (loc < iov->iov_len) {
133 1.2.4.2 skrll *off = loc;
134 1.2.4.2 skrll return (iov);
135 1.2.4.2 skrll }
136 1.2.4.2 skrll
137 1.2.4.2 skrll loc -= iov->iov_len;
138 1.2.4.2 skrll if (iol == 0) {
139 1.2.4.2 skrll if (loc == 0) {
140 1.2.4.2 skrll /* Point at the end of valid data */
141 1.2.4.2 skrll *off = iov->iov_len;
142 1.2.4.2 skrll return (iov);
143 1.2.4.2 skrll } else
144 1.2.4.2 skrll return (NULL);
145 1.2.4.2 skrll } else {
146 1.2.4.2 skrll iov++, iol--;
147 1.2.4.2 skrll }
148 1.2.4.2 skrll }
149 1.2.4.2 skrll
150 1.2.4.2 skrll return (NULL);
151 1.2.4.2 skrll }
152 1.2.4.2 skrll
153 1.2.4.2 skrll #else
154 1.2.4.2 skrll
155 1.2.4.2 skrll int
156 1.2.4.2 skrll cuio_getptr(struct uio *uio, int loc, int *off)
157 1.2.4.2 skrll {
158 1.2.4.2 skrll int ind, len;
159 1.2.4.2 skrll
160 1.2.4.2 skrll ind = 0;
161 1.2.4.2 skrll while (loc >= 0 && ind < uio->uio_iovcnt) {
162 1.2.4.2 skrll len = uio->uio_iov[ind].iov_len;
163 1.2.4.2 skrll if (len > loc) {
164 1.2.4.2 skrll *off = loc;
165 1.2.4.2 skrll return (ind);
166 1.2.4.2 skrll }
167 1.2.4.2 skrll loc -= len;
168 1.2.4.2 skrll ind++;
169 1.2.4.2 skrll }
170 1.2.4.2 skrll
171 1.2.4.2 skrll if (ind > 0 && loc == 0) {
172 1.2.4.2 skrll ind--;
173 1.2.4.2 skrll *off = uio->uio_iov[ind].iov_len;
174 1.2.4.2 skrll return (ind);
175 1.2.4.2 skrll }
176 1.2.4.2 skrll
177 1.2.4.2 skrll return (-1);
178 1.2.4.2 skrll }
179 1.2.4.2 skrll #endif
180 1.2.4.2 skrll
181 1.2.4.2 skrll int
182 1.2.4.2 skrll cuio_apply(struct uio *uio, int off, int len,
183 1.2.4.2 skrll int (*f)(caddr_t, caddr_t, unsigned int), caddr_t fstate)
184 1.2.4.2 skrll {
185 1.2.4.2 skrll int rval, ind, uiolen;
186 1.2.4.2 skrll unsigned int count;
187 1.2.4.2 skrll
188 1.2.4.2 skrll if (len < 0)
189 1.2.4.2 skrll panic("%s: len %d < 0", __func__, len);
190 1.2.4.2 skrll if (off < 0)
191 1.2.4.2 skrll panic("%s: off %d < 0", __func__, off);
192 1.2.4.2 skrll
193 1.2.4.2 skrll ind = 0;
194 1.2.4.2 skrll while (off > 0) {
195 1.2.4.2 skrll if (ind >= uio->uio_iovcnt)
196 1.2.4.2 skrll panic("cuio_apply: out of ivecs before data in uio");
197 1.2.4.2 skrll uiolen = uio->uio_iov[ind].iov_len;
198 1.2.4.2 skrll if (off < uiolen)
199 1.2.4.2 skrll break;
200 1.2.4.2 skrll off -= uiolen;
201 1.2.4.2 skrll ind++;
202 1.2.4.2 skrll }
203 1.2.4.2 skrll while (len > 0) {
204 1.2.4.2 skrll if (ind >= uio->uio_iovcnt)
205 1.2.4.2 skrll panic("cuio_apply: out of ivecs when processing uio");
206 1.2.4.2 skrll count = min(uio->uio_iov[ind].iov_len - off, len);
207 1.2.4.2 skrll
208 1.2.4.2 skrll rval = f(fstate,
209 1.2.4.2 skrll ((caddr_t)uio->uio_iov[ind].iov_base + off), count);
210 1.2.4.2 skrll if (rval)
211 1.2.4.2 skrll return (rval);
212 1.2.4.2 skrll
213 1.2.4.2 skrll len -= count;
214 1.2.4.2 skrll off = 0;
215 1.2.4.2 skrll ind++;
216 1.2.4.2 skrll }
217 1.2.4.2 skrll
218 1.2.4.2 skrll return (0);
219 1.2.4.2 skrll }
220