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