kern_physio.c revision 1.20 1 1.20 cgd /* $NetBSD: kern_physio.c,v 1.20 1994/06/29 06:32:34 cgd Exp $ */
2 1.20 cgd
3 1.20 cgd /*-
4 1.20 cgd * Copyright (c) 1994 Christopher G. Demetriou
5 1.20 cgd * Copyright (c) 1982, 1986, 1990, 1993
6 1.20 cgd * The Regents of the University of California. All rights reserved.
7 1.20 cgd * (c) UNIX System Laboratories, Inc.
8 1.20 cgd * All or some portions of this file are derived from material licensed
9 1.20 cgd * to the University of California by American Telephone and Telegraph
10 1.20 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 1.20 cgd * the permission of UNIX System Laboratories, Inc.
12 1.20 cgd *
13 1.20 cgd * Redistribution and use in source and binary forms, with or without
14 1.20 cgd * modification, are permitted provided that the following conditions
15 1.20 cgd * are met:
16 1.20 cgd * 1. Redistributions of source code must retain the above copyright
17 1.20 cgd * notice, this list of conditions and the following disclaimer.
18 1.20 cgd * 2. Redistributions in binary form must reproduce the above copyright
19 1.20 cgd * notice, this list of conditions and the following disclaimer in the
20 1.20 cgd * documentation and/or other materials provided with the distribution.
21 1.20 cgd * 3. All advertising materials mentioning features or use of this software
22 1.20 cgd * must display the following acknowledgement:
23 1.20 cgd * This product includes software developed by the University of
24 1.20 cgd * California, Berkeley and its contributors.
25 1.20 cgd * 4. Neither the name of the University nor the names of its contributors
26 1.20 cgd * may be used to endorse or promote products derived from this software
27 1.20 cgd * without specific prior written permission.
28 1.20 cgd *
29 1.20 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 1.20 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 1.20 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.20 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 1.20 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 1.20 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 1.20 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 1.20 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 1.20 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 1.20 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 1.20 cgd * SUCH DAMAGE.
40 1.20 cgd *
41 1.20 cgd * @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
42 1.20 cgd */
43 1.20 cgd
44 1.20 cgd #include <sys/param.h>
45 1.20 cgd #include <sys/systm.h>
46 1.20 cgd #include <sys/buf.h>
47 1.20 cgd #include <sys/conf.h>
48 1.20 cgd #include <sys/proc.h>
49 1.20 cgd
50 1.20 cgd /*
51 1.20 cgd * The routines implemented in this file are described in:
52 1.20 cgd * Leffler, et al.: The Design and Implementation of the 4.3BSD
53 1.20 cgd * UNIX Operating System (Addison Welley, 1989)
54 1.20 cgd * on pages 231-233.
55 1.20 cgd *
56 1.20 cgd * The routines "getphysbuf" and "putphysbuf" steal and return a swap
57 1.20 cgd * buffer. Leffler, et al., says that swap buffers are used to do the
58 1.20 cgd * I/O, so raw I/O requests don't have to be single-threaded.
59 1.20 cgd */
60 1.20 cgd
61 1.20 cgd struct buf *getphysbuf __P((void));
62 1.20 cgd void putphysbuf __P((struct buf *bp));
63 1.20 cgd
64 1.20 cgd /*
65 1.20 cgd * Do "physical I/O" on behalf of a user. "Physical I/O" is I/O directly
66 1.20 cgd * from the raw device to user buffers, and bypasses the buffer cache.
67 1.20 cgd *
68 1.20 cgd * Comments in brackets are from Leffler, et al.'s pseudo-code implementation.
69 1.20 cgd */
70 1.20 cgd int
71 1.20 cgd physio(strategy, bp, dev, flags, minphys, uio)
72 1.20 cgd int (*strategy)();
73 1.20 cgd struct buf *bp;
74 1.20 cgd dev_t dev;
75 1.20 cgd int flags;
76 1.20 cgd u_int (*minphys)();
77 1.20 cgd struct uio *uio;
78 1.20 cgd {
79 1.20 cgd struct iovec *iovp;
80 1.20 cgd struct proc *p = curproc;
81 1.20 cgd int error, done, i, nobuf, s, todo;
82 1.20 cgd
83 1.20 cgd error = 0;
84 1.20 cgd flags &= B_READ | B_WRITE;
85 1.20 cgd
86 1.20 cgd /*
87 1.20 cgd * [check user read/write access to the data buffer]
88 1.20 cgd *
89 1.20 cgd * Check each iov one by one. Note that we know if we're reading or
90 1.20 cgd * writing, so we ignore the uio's rw parameter. Also note that if
91 1.20 cgd * we're doing a read, that's a *write* to user-space.
92 1.20 cgd */
93 1.20 cgd for (i = 0; i < uio->uio_iovcnt; i++)
94 1.20 cgd if (!useracc(uio->uio_iov[i].iov_base, uio->uio_iov[i].iov_len,
95 1.20 cgd (flags == B_READ) ? B_WRITE : B_READ))
96 1.20 cgd return (EFAULT);
97 1.20 cgd
98 1.20 cgd /* Make sure we have a buffer, creating one if necessary. */
99 1.20 cgd if (nobuf = (bp == NULL))
100 1.20 cgd bp = getphysbuf();
101 1.20 cgd
102 1.20 cgd /* [raise the processor priority level to splbio;] */
103 1.20 cgd s = splbio();
104 1.20 cgd
105 1.20 cgd /* [while the buffer is marked busy] */
106 1.20 cgd while (bp->b_flags & B_BUSY) {
107 1.20 cgd /* [mark the buffer wanted] */
108 1.20 cgd bp->b_flags |= B_WANTED;
109 1.20 cgd /* [wait until the buffer is available] */
110 1.20 cgd tsleep((caddr_t)bp, PRIBIO+1, "physbuf", 0);
111 1.20 cgd }
112 1.20 cgd
113 1.20 cgd /* Mark it busy, so nobody else will use it. */
114 1.20 cgd bp->b_flags |= B_BUSY;
115 1.20 cgd
116 1.20 cgd /* [lower the priority level] */
117 1.20 cgd splx(s);
118 1.20 cgd
119 1.20 cgd /* [set up the fixed part of the buffer for a transfer] */
120 1.20 cgd bp->b_dev = dev;
121 1.20 cgd bp->b_error = 0;
122 1.20 cgd bp->b_proc = p;
123 1.20 cgd
124 1.20 cgd /*
125 1.20 cgd * [while there are data to transfer and no I/O error]
126 1.20 cgd * Note that I/O errors are handled with a 'goto' at the bottom
127 1.20 cgd * of the 'while' loop.
128 1.20 cgd */
129 1.20 cgd for (i = 0; i < uio->uio_iovcnt; i++) {
130 1.20 cgd iovp = &uio->uio_iov[i];
131 1.20 cgd while (iovp->iov_len > 0) {
132 1.20 cgd /*
133 1.20 cgd * [mark the buffer busy for physical I/O]
134 1.20 cgd * (i.e. set B_PHYS (because it's an I/O to user
135 1.20 cgd * memory, and B_RAW, because B_RAW is to be
136 1.20 cgd * "Set by physio for raw transfers.", in addition
137 1.20 cgd * to the "busy" and read/write flag.)
138 1.20 cgd */
139 1.20 cgd s = splbio();
140 1.20 cgd bp->b_flags = B_BUSY | B_PHYS | B_RAW | flags;
141 1.20 cgd splx(s);
142 1.20 cgd
143 1.20 cgd /* [set up the buffer for a maximum-sized transfer] */
144 1.20 cgd bp->b_blkno = btodb(uio->uio_offset);
145 1.20 cgd bp->b_bcount = iovp->iov_len;
146 1.20 cgd bp->b_data = iovp->iov_base;
147 1.20 cgd
148 1.20 cgd /*
149 1.20 cgd * [call minphys to bound the tranfer size]
150 1.20 cgd * and remember the amount of data to transfer,
151 1.20 cgd * for later comparison.
152 1.20 cgd */
153 1.20 cgd (*minphys)(bp);
154 1.20 cgd todo = bp->b_bcount;
155 1.20 cgd
156 1.20 cgd /*
157 1.20 cgd * [lock the part of the user address space involved
158 1.20 cgd * in the transfer]
159 1.20 cgd * Beware vmapbuf(); it clobbers b_data and
160 1.20 cgd * saves it in b_saveaddr. However, vunmapbuf()
161 1.20 cgd * restores it.
162 1.20 cgd */
163 1.20 cgd p->p_holdcnt++;
164 1.20 cgd vslock(bp->b_data, todo);
165 1.20 cgd vmapbuf(bp, todo);
166 1.20 cgd
167 1.20 cgd /* [call strategy to start the transfer] */
168 1.20 cgd (*strategy)(bp);
169 1.20 cgd
170 1.20 cgd /*
171 1.20 cgd * Note that the raise/wait/lower/get error
172 1.20 cgd * steps below would be done by biowait(), but
173 1.20 cgd * we want to unlock the address space before
174 1.20 cgd * we lower the priority.
175 1.20 cgd *
176 1.20 cgd * [raise the priority level to splbio]
177 1.20 cgd */
178 1.20 cgd s = splbio();
179 1.20 cgd
180 1.20 cgd /* [wait for the transfer to complete] */
181 1.20 cgd while ((bp->b_flags & B_DONE) == 0)
182 1.20 cgd tsleep((caddr_t) bp, PRIBIO + 1, "physio", 0);
183 1.20 cgd
184 1.20 cgd /*
185 1.20 cgd * [unlock the part of the address space previously
186 1.20 cgd * locked]
187 1.20 cgd */
188 1.20 cgd vunmapbuf(bp, todo);
189 1.20 cgd vsunlock(bp->b_data, todo);
190 1.20 cgd p->p_holdcnt--;
191 1.20 cgd
192 1.20 cgd /* remember error value (save a splbio/splx pair) */
193 1.20 cgd if (bp->b_flags & B_ERROR)
194 1.20 cgd error = (bp->b_error ? bp->b_error : EIO);
195 1.20 cgd
196 1.20 cgd /* [lower the priority level] */
197 1.20 cgd splx(s);
198 1.20 cgd
199 1.20 cgd /*
200 1.20 cgd * [deduct the transfer size from the total number
201 1.20 cgd * of data to transfer]
202 1.20 cgd */
203 1.20 cgd done = bp->b_bcount - bp->b_resid;
204 1.20 cgd iovp->iov_len -= done;
205 1.20 cgd iovp->iov_base += done;
206 1.20 cgd uio->uio_offset += done;
207 1.20 cgd uio->uio_resid -= done;
208 1.20 cgd
209 1.20 cgd /*
210 1.20 cgd * Now, check for an error.
211 1.20 cgd * Also, handle weird end-of-disk semantics.
212 1.20 cgd */
213 1.20 cgd if (error || done < todo)
214 1.20 cgd goto done;
215 1.20 cgd }
216 1.20 cgd }
217 1.20 cgd
218 1.20 cgd done:
219 1.20 cgd /*
220 1.20 cgd * [clean up the state of the buffer]
221 1.20 cgd * Remember if somebody wants it, so we can wake them up below.
222 1.20 cgd * Also, if we had to steal it, give it back.
223 1.20 cgd */
224 1.20 cgd s = splbio();
225 1.20 cgd bp->b_flags &= ~(B_BUSY | B_PHYS | B_RAW);
226 1.20 cgd if (nobuf)
227 1.20 cgd putphysbuf(bp);
228 1.20 cgd else {
229 1.20 cgd /*
230 1.20 cgd * [if another process is waiting for the raw I/O buffer,
231 1.20 cgd * wake up processes waiting to do physical I/O;
232 1.20 cgd */
233 1.20 cgd if (bp->b_flags & B_WANTED) {
234 1.20 cgd bp->b_flags &= ~B_WANTED;
235 1.20 cgd wakeup(bp);
236 1.20 cgd }
237 1.20 cgd }
238 1.20 cgd splx(s);
239 1.20 cgd
240 1.20 cgd return (error);
241 1.20 cgd }
242 1.20 cgd
243 1.20 cgd /*
244 1.20 cgd * Get a swap buffer structure, for use in physical I/O.
245 1.20 cgd * Mostly taken from /sys/vm/swap_pager.c, except that it no longer
246 1.20 cgd * records buffer list-empty conditions, and sleeps at PRIBIO + 1,
247 1.20 cgd * rather than PSWP + 1 (and on a different wchan).
248 1.20 cgd */
249 1.20 cgd struct buf *
250 1.20 cgd getphysbuf()
251 1.20 cgd {
252 1.20 cgd struct buf *bp;
253 1.20 cgd int s;
254 1.20 cgd
255 1.20 cgd s = splbio();
256 1.20 cgd while (bswlist.b_actf == NULL) {
257 1.20 cgd bswlist.b_flags |= B_WANTED;
258 1.20 cgd tsleep((caddr_t)&bswlist, PRIBIO + 1, "getphys", 0);
259 1.20 cgd }
260 1.20 cgd bp = bswlist.b_actf;
261 1.20 cgd bswlist.b_actf = bp->b_actf;
262 1.20 cgd splx(s);
263 1.20 cgd return (bp);
264 1.20 cgd }
265 1.20 cgd
266 1.20 cgd /*
267 1.20 cgd * Get rid of a swap buffer structure which has been used in physical I/O.
268 1.20 cgd * Mostly taken from /sys/vm/swap_pager.c, except that it now uses
269 1.20 cgd * wakeup() rather than the VM-internal thread_wakeup(), and that the caller
270 1.20 cgd * must mask disk interrupts, rather than putphysbuf() itself.
271 1.20 cgd */
272 1.20 cgd void
273 1.20 cgd putphysbuf(bp)
274 1.20 cgd struct buf *bp;
275 1.20 cgd {
276 1.20 cgd
277 1.20 cgd bp->b_actf = bswlist.b_actf;
278 1.20 cgd bswlist.b_actf = bp;
279 1.20 cgd if (bp->b_vp)
280 1.20 cgd brelvp(bp);
281 1.20 cgd if (bswlist.b_flags & B_WANTED) {
282 1.20 cgd bswlist.b_flags &= ~B_WANTED;
283 1.20 cgd wakeup(&bswlist);
284 1.20 cgd }
285 1.20 cgd }
286 1.20 cgd
287 1.20 cgd /*
288 1.20 cgd * Leffler, et al., says on p. 231:
289 1.20 cgd * "The minphys() routine is called by physio() to adjust the
290 1.20 cgd * size of each I/O transfer before the latter is passed to
291 1.20 cgd * the strategy routine..."
292 1.20 cgd *
293 1.20 cgd * so, just adjust the buffer's count accounting to MAXPHYS here,
294 1.20 cgd * and return the new count;
295 1.20 cgd */
296 1.20 cgd u_int
297 1.20 cgd minphys(bp)
298 1.20 cgd struct buf *bp;
299 1.20 cgd {
300 1.20 cgd
301 1.20 cgd bp->b_bcount = min(MAXPHYS, bp->b_bcount);
302 1.20 cgd return bp->b_bcount;
303 1.20 cgd }
304 1.20 cgd
305 1.20 cgd /*
306 1.20 cgd * Do a read on a device for a user process.
307 1.20 cgd */
308 1.20 cgd rawread(dev, uio)
309 1.20 cgd dev_t dev;
310 1.20 cgd struct uio *uio;
311 1.20 cgd {
312 1.20 cgd return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
313 1.20 cgd dev, B_READ, minphys, uio));
314 1.20 cgd }
315 1.20 cgd
316 1.20 cgd /*
317 1.20 cgd * Do a write on a device for a user process.
318 1.20 cgd */
319 1.20 cgd rawwrite(dev, uio)
320 1.20 cgd dev_t dev;
321 1.20 cgd struct uio *uio;
322 1.20 cgd {
323 1.20 cgd return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
324 1.20 cgd dev, B_WRITE, minphys, uio));
325 1.20 cgd }
326