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