kern_physio.c revision 1.80.2.7 1 /* $NetBSD: kern_physio.c,v 1.80.2.7 2007/10/09 13:44:27 ad 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.80.2.7 2007/10/09 13:44:27 ad 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 #include <sys/kmem.h>
83
84 #include <uvm/uvm_extern.h>
85
86 ONCE_DECL(physio_initialized);
87 struct workqueue *physio_workqueue;
88
89 /*
90 * The routines implemented in this file are described in:
91 * Leffler, et al.: The Design and Implementation of the 4.3BSD
92 * UNIX Operating System (Addison Welley, 1989)
93 * on pages 231-233.
94 *
95 * The routines "getphysbuf" and "putphysbuf" steal and return a swap
96 * buffer. Leffler, et al., says that swap buffers are used to do the
97 * I/O, so raw I/O requests don't have to be single-threaded. Of course,
98 * NetBSD doesn't use "swap buffers" -- we have our own memory pool for
99 * buffer descriptors.
100 */
101
102 /* #define PHYSIO_DEBUG */
103 #if defined(PHYSIO_DEBUG)
104 #define DPRINTF(a) printf a
105 #else /* defined(PHYSIO_DEBUG) */
106 #define DPRINTF(a) /* nothing */
107 #endif /* defined(PHYSIO_DEBUG) */
108
109 /* abuse these members/flags of struct buf */
110 #define b_running b_freelistindex
111 #define b_endoffset b_lblkno
112 #define BC_DONTFREE BC_AGE
113
114 /*
115 * allocate a buffer structure for use in physical I/O.
116 */
117 static struct buf *
118 getphysbuf(void)
119 {
120 struct buf *bp;
121
122 bp = getiobuf(NULL, true);
123 bp->b_error = 0;
124 bp->b_cflags = BC_BUSY;
125 return(bp);
126 }
127
128 /*
129 * get rid of a swap buffer structure which has been used in physical I/O.
130 */
131 static void
132 putphysbuf(struct buf *bp)
133 {
134
135 if ((bp->b_cflags & BC_DONTFREE) != 0) {
136 return;
137 }
138
139 if (__predict_false((bp->b_cflags & BC_WANTED) != 0))
140 panic("putphysbuf: private buf wanted");
141 putiobuf(bp);
142 }
143
144 static void
145 physio_done(struct work *wk, void *dummy)
146 {
147 struct buf *bp = (void *)wk;
148 size_t todo = bp->b_bufsize;
149 size_t done = bp->b_bcount - bp->b_resid;
150 struct physio_stat *ps = bp->b_private;
151
152 KASSERT(&bp->b_work == wk);
153 KASSERT(bp->b_bcount <= todo);
154 KASSERT(bp->b_resid <= bp->b_bcount);
155 KASSERT((bp->b_flags & B_PHYS) != 0);
156 KASSERT(dummy == NULL);
157
158 vunmapbuf(bp, todo);
159 uvm_vsunlock(bp->b_proc->p_vmspace, bp->b_data, todo);
160
161 mutex_enter(mbp->b_objlock);
162 if (__predict_false(done != todo)) {
163 off_t endoffset = dbtob(bp->b_blkno) + done;
164
165 /*
166 * we got an error or hit EOM.
167 *
168 * we only care about the first one.
169 * ie. the one at the lowest offset.
170 */
171
172 KASSERT(mbp->b_endoffset != endoffset);
173 DPRINTF(("%s: error=%d at %" PRIu64 " - %" PRIu64
174 ", blkno=%" PRIu64 ", bcount=%d, flags=0x%x\n",
175 __func__, bp->b_error, dbtob(bp->b_blkno), endoffset,
176 bp->b_blkno, bp->b_bcount, bp->b_flags));
177
178 mbp->b_flags = EIO;
179 if (mbp->b_endoffset == -1 || endoffset < mbp->b_endoffset) {
180 DPRINTF(("%s: mbp=%p, error %d -> %d, endoff %" PRIu64
181 " -> %" PRIu64 "\n",
182 __func__, mbp,
183 mbp->b_error, bp->b_error,
184 mbp->b_endoffset, endoffset));
185
186 mbp->b_endoffset = endoffset;
187 mbp->b_error = bp->b_error;
188 }
189 } else {
190 KASSERT(bp->b_error == 0);
191 }
192
193 mbp->b_running--;
194 cv_broadcast(&mbp->b_done);
195 mutex_exit(mbp->b_objlock);
196
197 putphysbuf(bp);
198 }
199
200 static void
201 physio_biodone(struct buf *bp)
202 {
203 #if defined(DIAGNOSTIC)
204 struct physio_stat *ps = bp->b_private;
205 size_t todo = bp->b_bufsize;
206
207 KASSERT(ps->ps_running > 0);
208 KASSERT(bp->b_bcount <= todo);
209 KASSERT(bp->b_resid <= bp->b_bcount);
210 #endif /* defined(DIAGNOSTIC) */
211
212 workqueue_enqueue(physio_workqueue, &bp->b_work, NULL);
213 }
214
215 static void
216 physio_wait(struct buf *bp, int n)
217 {
218
219 KASSERT(mutex_owned(bp->b_objlock));
220
221 while ((bp->b_oflags & BO_DONE) == 0 && bp->b_running > n)
222 cv_wait(&bp->b_done, bp->b_objlock);
223 }
224
225 static int
226 physio_init(void)
227 {
228 int error;
229
230 KASSERT(physio_workqueue == NULL);
231
232 error = workqueue_create(&physio_workqueue, "physiod",
233 physio_done, NULL, PRIBIO, IPL_NONE, 0);
234
235 return error;
236 }
237
238 #define PHYSIO_CONCURRENCY 16 /* XXX tune */
239
240 /*
241 * Do "physical I/O" on behalf of a user. "Physical I/O" is I/O directly
242 * from the raw device to user buffers, and bypasses the buffer cache.
243 *
244 * Comments in brackets are from Leffler, et al.'s pseudo-code implementation.
245 */
246 int
247 physio(void (*strategy)(struct buf *), struct buf *obp, dev_t dev, int flags,
248 void (*min_phys)(struct buf *), struct uio *uio)
249 {
250 struct iovec *iovp;
251 struct lwp *l = curlwp;
252 struct proc *p = l->l_proc;
253 int i;
254 int error;
255 struct buf *bp = NULL;
256 struct buf *mbp;
257 int concurrency = PHYSIO_CONCURRENCY - 1;
258
259 error = RUN_ONCE(&physio_initialized, physio_init);
260 if (__predict_false(error != 0)) {
261 return error;
262 }
263
264 DPRINTF(("%s: called: off=%" PRIu64 ", resid=%zu\n",
265 __func__, uio->uio_offset, uio->uio_resid));
266
267 flags &= B_READ | B_WRITE;
268
269 if ((ps = kmem_zalloc(sizeof(*ps), KM_SLEEP)) == NULL)
270 return ENOMEM;
271 /* ps->ps_running = 0; */
272 /* ps->ps_error = 0; */
273 /* ps->ps_failed = 0; */
274 ps->ps_endoffset = -1;
275 mutex_init(&ps->ps_lock, MUTEX_DEFAULT, IPL_NONE);
276 cv_init(&ps->ps_cv, "physio");
277
278 /* Make sure we have a buffer, creating one if necessary. */
279 if (obp != NULL) {
280 mutex_enter(&bufcache_lock);
281 while (bbusy(obp, false, 0) == EPASSTHROUGH)
282 ;
283 /* Mark it busy, so nobody else will use it. */
284 obp->b_cflags = BC_DONTFREE;
285 mutex_exit(&bufcache_lock);
286
287 concurrency = 0; /* see "XXXkludge" comment below */
288 }
289
290 uvm_lwp_hold(l);
291
292 for (i = 0; i < uio->uio_iovcnt; i++) {
293 bool sync = true;
294
295 iovp = &uio->uio_iov[i];
296 while (iovp->iov_len > 0) {
297 size_t todo;
298 vaddr_t endp;
299
300 mutex_enter(mbp->b_objlock);
301 physio_wait(mbp, sync ? 0 : concurrency);
302 if (mbp->b_error != 0) {
303 goto done_locked;
304 }
305 mutex_exit(mbp->b_objlock);
306 if (obp != NULL) {
307 /*
308 * XXXkludge
309 * some drivers use "obp" as an identifier.
310 */
311 bp = obp;
312 } else {
313 bp = getphysbuf();
314 }
315 bp->b_dev = dev;
316 bp->b_proc = p;
317 bp->b_private = ps;
318 bp->b_vp = NULL;
319
320 /*
321 * [mark the buffer busy for physical I/O]
322 * (i.e. set B_PHYS (because it's an I/O to user
323 * memory, and B_RAW, because B_RAW is to be
324 * "Set by physio for raw transfers.", in addition
325 * to the "busy" and read/write flag.)
326 */
327 bp->b_oflags = 0;
328 bp->b_cflags = (bp->b_cflags & BC_DONTFREE) | BC_BUSY;
329 bp->b_flags = flags | B_PHYS | B_RAW;
330 bp->b_iodone = physio_biodone;
331
332 /* [set up the buffer for a maximum-sized transfer] */
333 bp->b_blkno = btodb(uio->uio_offset);
334 if (dbtob(bp->b_blkno) != uio->uio_offset) {
335 error = EINVAL;
336 goto done;
337 }
338 bp->b_bcount = MIN(MAXPHYS, iovp->iov_len);
339 bp->b_data = iovp->iov_base;
340
341 /*
342 * [call minphys to bound the transfer size]
343 * and remember the amount of data to transfer,
344 * for later comparison.
345 */
346 (*min_phys)(bp);
347 todo = bp->b_bufsize = bp->b_bcount;
348 #if defined(DIAGNOSTIC)
349 if (todo > MAXPHYS)
350 panic("todo(%zu) > MAXPHYS; minphys broken",
351 todo);
352 #endif /* defined(DIAGNOSTIC) */
353
354 sync = false;
355 endp = (vaddr_t)bp->b_data + todo;
356 if (trunc_page(endp) != endp) {
357 /*
358 * following requests can overlap.
359 * note that uvm_vslock does round_page.
360 */
361 sync = true;
362 }
363
364 /*
365 * [lock the part of the user address space involved
366 * in the transfer]
367 * Beware vmapbuf(); it clobbers b_data and
368 * saves it in b_saveaddr. However, vunmapbuf()
369 * restores it.
370 */
371 error = uvm_vslock(p->p_vmspace, bp->b_data, todo,
372 (flags & B_READ) ? VM_PROT_WRITE : VM_PROT_READ);
373 if (error) {
374 goto done;
375 }
376 vmapbuf(bp, todo);
377
378 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
379
380 mutex_enter(mbp->b_objlock);
381 mbp->b_running++;
382 mutex_exit(mbp->b_objlock);
383
384 /* [call strategy to start the transfer] */
385 (*strategy)(bp);
386 bp = NULL;
387
388 iovp->iov_len -= todo;
389 iovp->iov_base = (char *)iovp->iov_base + todo;
390 uio->uio_offset += todo;
391 uio->uio_resid -= todo;
392 }
393 }
394
395 done:
396 mutex_enter(mbp->b_objlock);
397 done_locked:
398 physio_wait(mbp, 0);
399 mutex_exit(mbp->b_objlock);
400
401 if (mbp->b_error != 0) {
402 off_t delta;
403
404 delta = uio->uio_offset - mbp->b_endoffset;
405 KASSERT(delta > 0);
406 uio->uio_resid += delta;
407 /* uio->uio_offset = mbp->b_endoffset; */
408 } else {
409 KASSERT(mbp->b_endoffset == -1);
410 }
411 if (bp != NULL) {
412 putphysbuf(bp);
413 }
414 if (error == 0) {
415 error = mbp->b_error;
416 }
417 putphysbuf(mbp);
418
419 /*
420 * [clean up the state of the buffer]
421 * Remember if somebody wants it, so we can wake them up below.
422 * Also, if we had to steal it, give it back.
423 */
424 if (obp != NULL) {
425 KASSERT((obp->b_cflags & BC_BUSY) != 0);
426 KASSERT((obp->b_cflags & BC_DONTFREE) != 0);
427
428 /*
429 * [if another process is waiting for the raw I/O buffer,
430 * wake up processes waiting to do physical I/O;
431 */
432 mutex_enter(&bufcache_lock);
433 obp->b_cflags &= ~(BC_DONTFREE | BC_BUSY);
434 obp->b_flags &= ~(B_PHYS | B_RAW);
435 obp->b_iodone = NULL;
436 cv_broadcast(&obp->b_busy);
437 mutex_exit(&bufcache_lock);
438 }
439 uvm_lwp_rele(l);
440
441 DPRINTF(("%s: done: off=%" PRIu64 ", resid=%zu\n",
442 __func__, uio->uio_offset, uio->uio_resid));
443
444 return error;
445 }
446
447 /*
448 * Leffler, et al., says on p. 231:
449 * "The minphys() routine is called by physio() to adjust the
450 * size of each I/O transfer before the latter is passed to
451 * the strategy routine..."
452 *
453 * so, just adjust the buffer's count accounting to MAXPHYS here,
454 * and return the new count;
455 */
456 void
457 minphys(struct buf *bp)
458 {
459
460 if (bp->b_bcount > MAXPHYS)
461 bp->b_bcount = MAXPHYS;
462 }
463