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