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