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