kern_physio.c revision 1.64 1 /* $NetBSD: kern_physio.c,v 1.64 2005/10/30 09:17:02 yamt 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.64 2005/10/30 09:17:02 yamt Exp $");
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/buf.h>
79 #include <sys/proc.h>
80 #include <sys/once.h>
81 #include <sys/workqueue.h>
82
83 #include <uvm/uvm_extern.h>
84
85 ONCE_DECL(physio_initialized);
86 struct workqueue *physio_workqueue;
87
88 /*
89 * The routines implemented in this file are described in:
90 * Leffler, et al.: The Design and Implementation of the 4.3BSD
91 * UNIX Operating System (Addison Welley, 1989)
92 * on pages 231-233.
93 *
94 * The routines "getphysbuf" and "putphysbuf" steal and return a swap
95 * buffer. Leffler, et al., says that swap buffers are used to do the
96 * I/O, so raw I/O requests don't have to be single-threaded. Of course,
97 * NetBSD doesn't use "swap buffers" -- we have our own memory pool for
98 * buffer descriptors.
99 */
100
101 /*
102 * allocate a buffer structure for use in physical I/O.
103 */
104 static struct buf *
105 getphysbuf(void)
106 {
107 struct buf *bp;
108 int s;
109
110 s = splbio();
111 bp = pool_get(&bufpool, PR_WAITOK);
112 splx(s);
113 BUF_INIT(bp);
114 bp->b_error = 0;
115 return(bp);
116 }
117
118 /*
119 * get rid of a swap buffer structure which has been used in physical I/O.
120 */
121 static void
122 putphysbuf(struct buf *bp)
123 {
124 int s;
125
126 if (__predict_false(bp->b_flags & B_WANTED))
127 panic("putphysbuf: private buf B_WANTED");
128 s = splbio();
129 pool_put(&bufpool, bp);
130 splx(s);
131 }
132
133 /* abuse these members of struct buf */
134 #define b_running b_freelistindex
135 #define b_eomoffset b_lblkno
136
137 static void
138 physio_done(struct work *wk, void *dummy)
139 {
140 struct buf *bp = (void *)wk;
141 size_t todo = bp->b_bufsize;
142 struct buf *mbp = bp->b_private;
143
144 KASSERT(&bp->b_work == wk);
145 KASSERT(bp->b_bcount <= todo);
146 KASSERT(bp->b_resid <= bp->b_bcount);
147 KASSERT((bp->b_flags & B_PHYS) != 0);
148 KASSERT(dummy == NULL);
149
150 vunmapbuf(bp, todo);
151 uvm_vsunlock(bp->b_proc, bp->b_data, todo);
152
153 simple_lock(&mbp->b_interlock);
154 if ((mbp->b_flags & B_ERROR) != 0) {
155 goto done;
156 }
157 if ((bp->b_flags & B_ERROR) != 0) {
158 if (bp->b_error == 0) {
159 mbp->b_error = EIO; /* XXX */
160 } else {
161 mbp->b_error = bp->b_error;
162 }
163 mbp->b_flags |= B_ERROR;
164 goto done;
165 }
166 KASSERT(bp->b_resid == 0); /* XXX */
167 if (bp->b_bcount != todo) {
168 #if defined(DIAGNOSTIC)
169 off_t eomoffset = dbtob(bp->b_blkno);
170
171 if ((mbp->b_flags & B_ERROR) != 0 &&
172 mbp->b_eomoffset != eomoffset) {
173 panic("%s: eom mismatch", __func__);
174 }
175 mbp->b_eomoffset = eomoffset;
176 #endif /* defined(DIAGNOSTIC) */
177 mbp->b_flags |= B_ERROR;
178 mbp->b_error = 0;
179 goto done;
180 }
181 done:
182 mbp->b_resid -= bp->b_bcount - bp->b_resid;
183 mbp->b_running--;
184 if ((mbp->b_flags & B_WANTED) != 0) {
185 mbp->b_flags &= ~B_WANTED;
186 wakeup(mbp);
187 }
188 simple_unlock(&mbp->b_interlock);
189
190 putphysbuf(bp);
191 }
192
193 static void
194 physio_biodone(struct buf *bp)
195 {
196 #if defined(DIAGNOSTIC)
197 struct buf *mbp = bp->b_private;
198 size_t todo = bp->b_bufsize;
199
200 KASSERT(mbp->b_running > 0);
201 KASSERT(todo <= mbp->b_resid);
202 KASSERT(bp->b_bcount <= todo);
203 KASSERT(bp->b_resid <= bp->b_bcount);
204 #endif /* defined(DIAGNOSTIC) */
205
206 workqueue_enqueue(physio_workqueue, &bp->b_work);
207 }
208
209 static int
210 physio_wait(struct buf *bp, int n, const char *wchan)
211 {
212 int error = 0;
213
214 LOCK_ASSERT(simple_lock_held(&bp->b_interlock));
215
216 while (bp->b_running > n) {
217 bp->b_flags |= B_WANTED;
218 error = ltsleep(bp, PRIBIO + 1, wchan, 0, &bp->b_interlock);
219 if (error) {
220 break;
221 }
222 }
223
224 return error;
225 }
226
227 static void
228 physio_init(void)
229 {
230
231 KASSERT(physio_workqueue == NULL);
232
233 if (workqueue_create(&physio_workqueue, "physiod",
234 physio_done, NULL, PRIBIO, 0/* IPL_BIO notyet */, 0)) {
235 panic("physiod create");
236 }
237 }
238
239 #define PHYSIO_CONCURRENCY 16 /* XXX tune */
240
241 /*
242 * Do "physical I/O" on behalf of a user. "Physical I/O" is I/O directly
243 * from the raw device to user buffers, and bypasses the buffer cache.
244 *
245 * Comments in brackets are from Leffler, et al.'s pseudo-code implementation.
246 */
247 int
248 physio(void (*strategy)(struct buf *), struct buf *obp, dev_t dev, int flags,
249 void (*min_phys)(struct buf *), struct uio *uio)
250 {
251 struct iovec *iovp;
252 struct lwp *l = curlwp;
253 struct proc *p = l->l_proc;
254 int i, s;
255 int error = 0;
256 int error2;
257 size_t todo;
258 struct buf *bp = NULL;
259 struct buf *mbp;
260
261 RUN_ONCE(&physio_initialized, physio_init);
262
263 flags &= B_READ | B_WRITE;
264
265 /* Make sure we have a buffer, creating one if necessary. */
266 if (obp != NULL) {
267 /* [raise the processor priority level to splbio;] */
268 s = splbio();
269
270 /* [while the buffer is marked busy] */
271 while (obp->b_flags & B_BUSY) {
272 /* [mark the buffer wanted] */
273 obp->b_flags |= B_WANTED;
274 /* [wait until the buffer is available] */
275 tsleep(obp, PRIBIO+1, "physbuf", 0);
276 }
277
278 /* Mark it busy, so nobody else will use it. */
279 obp->b_flags |= B_BUSY;
280
281 /* [lower the priority level] */
282 splx(s);
283 }
284
285 mbp = getphysbuf();
286 mbp->b_resid = uio->uio_resid;
287 mbp->b_running = 0;
288 mbp->b_flags = 0;
289
290 PHOLD(l);
291
292 for (i = 0; i < uio->uio_iovcnt; i++) {
293 iovp = &uio->uio_iov[i];
294 while (iovp->iov_len > 0) {
295 simple_lock(&mbp->b_interlock);
296 if ((mbp->b_flags & B_ERROR) != 0) {
297 error = mbp->b_error;
298 goto done_locked;
299 }
300 error = physio_wait(mbp, PHYSIO_CONCURRENCY - 1,
301 "physio1");
302 if (error) {
303 goto done_locked;
304 }
305 simple_unlock(&mbp->b_interlock);
306 bp = getphysbuf();
307 bp->b_dev = dev;
308 bp->b_proc = p;
309 bp->b_private = mbp;
310 bp->b_vp = NULL;
311
312 /*
313 * [mark the buffer busy for physical I/O]
314 * (i.e. set B_PHYS (because it's an I/O to user
315 * memory, and B_RAW, because B_RAW is to be
316 * "Set by physio for raw transfers.", in addition
317 * to the "busy" and read/write flag.)
318 */
319 bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_CALL | flags;
320 bp->b_iodone = physio_biodone;
321
322 /* [set up the buffer for a maximum-sized transfer] */
323 bp->b_blkno = btodb(uio->uio_offset);
324 if (dbtob(bp->b_blkno) != uio->uio_offset) {
325 error = EINVAL;
326 goto done;
327 }
328 bp->b_bcount = MIN(MAXPHYS, iovp->iov_len);
329 bp->b_data = iovp->iov_base;
330
331 /*
332 * [call minphys to bound the transfer size]
333 * and remember the amount of data to transfer,
334 * for later comparison.
335 */
336 (*min_phys)(bp);
337 todo = bp->b_bufsize = bp->b_bcount;
338 #if defined(DIAGNOSTIC)
339 if (todo > MAXPHYS)
340 panic("todo(%zu) > MAXPHYS; minphys broken",
341 todo);
342 #endif /* defined(DIAGNOSTIC) */
343
344 /*
345 * [lock the part of the user address space involved
346 * in the transfer]
347 * Beware vmapbuf(); it clobbers b_data and
348 * saves it in b_saveaddr. However, vunmapbuf()
349 * restores it.
350 */
351 error = uvm_vslock(p, bp->b_data, todo,
352 (flags & B_READ) ? VM_PROT_WRITE : VM_PROT_READ);
353 if (error) {
354 goto done;
355 }
356 vmapbuf(bp, todo);
357
358 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
359
360 simple_lock(&mbp->b_interlock);
361 mbp->b_running++;
362 simple_unlock(&mbp->b_interlock);
363
364 /* [call strategy to start the transfer] */
365 (*strategy)(bp);
366 bp = NULL;
367
368 iovp->iov_len -= todo;
369 iovp->iov_base = (caddr_t)iovp->iov_base + todo;
370 uio->uio_offset += todo;
371 uio->uio_resid -= todo;
372 }
373 }
374
375 done:
376 simple_lock(&mbp->b_interlock);
377 done_locked:
378 error2 = physio_wait(mbp, 0, "physio2");
379 if (error == 0) {
380 error = error2;
381 }
382 simple_unlock(&mbp->b_interlock);
383 KASSERT((mbp->b_flags & B_ERROR) != 0 ||
384 mbp->b_resid == uio->uio_resid);
385 #if defined(DIAGNOSTIC)
386 if ((mbp->b_flags & B_ERROR) != 0 && mbp->b_error == 0 &&
387 uio->uio_offset - mbp->b_resid != mbp->b_eomoffset) {
388 panic("%s: eom", __func__);
389 }
390 #endif /* defined(DIAGNOSTIC) */
391 uio->uio_resid = mbp->b_resid;
392 if (bp != NULL) {
393 putphysbuf(bp);
394 }
395 if (error == 0) {
396 error = mbp->b_error;
397 }
398 putphysbuf(mbp);
399
400 /*
401 * [clean up the state of the buffer]
402 * Remember if somebody wants it, so we can wake them up below.
403 * Also, if we had to steal it, give it back.
404 */
405 if (obp != NULL) {
406 s = splbio();
407 /*
408 * [if another process is waiting for the raw I/O buffer,
409 * wake up processes waiting to do physical I/O;
410 */
411 if (obp->b_flags & B_WANTED) {
412 obp->b_flags &= ~B_WANTED;
413 wakeup(obp);
414 }
415 splx(s);
416 }
417 PRELE(l);
418
419 return error;
420 }
421
422 /*
423 * Leffler, et al., says on p. 231:
424 * "The minphys() routine is called by physio() to adjust the
425 * size of each I/O transfer before the latter is passed to
426 * the strategy routine..."
427 *
428 * so, just adjust the buffer's count accounting to MAXPHYS here,
429 * and return the new count;
430 */
431 void
432 minphys(struct buf *bp)
433 {
434
435 if (bp->b_bcount > MAXPHYS)
436 bp->b_bcount = MAXPHYS;
437 }
438