xref: /netbsd-src/sys/kern/kern_physio.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: kern_physio.c,v 1.51 2001/11/12 15:25:13 lukem 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/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: kern_physio.c,v 1.51 2001/11/12 15:25:13 lukem Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/buf.h>
50 #include <sys/malloc.h>
51 #include <sys/proc.h>
52 
53 #include <uvm/uvm_extern.h>
54 
55 /*
56  * The routines implemented in this file are described in:
57  *	Leffler, et al.: The Design and Implementation of the 4.3BSD
58  *	    UNIX Operating System (Addison Welley, 1989)
59  * on pages 231-233.
60  *
61  * The routines "getphysbuf" and "putphysbuf" steal and return a swap
62  * buffer.  Leffler, et al., says that swap buffers are used to do the
63  * I/O, so raw I/O requests don't have to be single-threaded.
64  */
65 
66 struct buf *getphysbuf __P((void));
67 void putphysbuf __P((struct buf *bp));
68 
69 /*
70  * Do "physical I/O" on behalf of a user.  "Physical I/O" is I/O directly
71  * from the raw device to user buffers, and bypasses the buffer cache.
72  *
73  * Comments in brackets are from Leffler, et al.'s pseudo-code implementation.
74  */
75 int
76 physio(strategy, bp, dev, flags, minphys, uio)
77 	void (*strategy) __P((struct buf *));
78 	struct buf *bp;
79 	dev_t dev;
80 	int flags;
81 	void (*minphys) __P((struct buf *));
82 	struct uio *uio;
83 {
84 	struct iovec *iovp;
85 	struct proc *p = curproc;
86 	int error, done, i, nobuf, s;
87 	long todo;
88 
89 	error = 0;
90 	flags &= B_READ | B_WRITE | B_ORDERED;
91 
92 	/* Make sure we have a buffer, creating one if necessary. */
93 	if ((nobuf = (bp == NULL)) != 0) {
94 
95 		bp = getphysbuf();
96 		/* bp was just malloc'd so can't already be busy */
97 		bp->b_flags |= B_BUSY;
98 
99 	} else {
100 
101 		/* [raise the processor priority level to splbio;] */
102 		s = splbio();
103 
104 		/* [while the buffer is marked busy] */
105 		while (bp->b_flags & B_BUSY) {
106 			/* [mark the buffer wanted] */
107 			bp->b_flags |= B_WANTED;
108 			/* [wait until the buffer is available] */
109 			tsleep((caddr_t)bp, PRIBIO+1, "physbuf", 0);
110 		}
111 
112 		/* Mark it busy, so nobody else will use it. */
113 		bp->b_flags |= B_BUSY;
114 
115 		/* [lower the priority level] */
116 		splx(s);
117 	}
118 
119 	/* [set up the fixed part of the buffer for a transfer] */
120 	bp->b_dev = dev;
121 	bp->b_error = 0;
122 	bp->b_proc = p;
123 	LIST_INIT(&bp->b_dep);
124 
125 	/*
126 	 * [while there are data to transfer and no I/O error]
127 	 * Note that I/O errors are handled with a 'goto' at the bottom
128 	 * of the 'while' loop.
129 	 */
130 	for (i = 0; i < uio->uio_iovcnt; i++) {
131 		iovp = &uio->uio_iov[i];
132 		while (iovp->iov_len > 0) {
133 
134 			/*
135 			 * [mark the buffer busy for physical I/O]
136 			 * (i.e. set B_PHYS (because it's an I/O to user
137 			 * memory, and B_RAW, because B_RAW is to be
138 			 * "Set by physio for raw transfers.", in addition
139 			 * to the "busy" and read/write flag.)
140 			 */
141 			bp->b_flags = B_BUSY | B_PHYS | B_RAW | flags;
142 
143 			/* [set up the buffer for a maximum-sized transfer] */
144 			bp->b_blkno = btodb(uio->uio_offset);
145 			bp->b_bcount = iovp->iov_len;
146 			bp->b_data = iovp->iov_base;
147 
148 			/*
149 			 * [call minphys to bound the transfer size]
150 			 * and remember the amount of data to transfer,
151 			 * for later comparison.
152 			 */
153 			(*minphys)(bp);
154 			todo = bp->b_bcount;
155 #ifdef DIAGNOSTIC
156 			if (todo <= 0)
157 				panic("todo(%ld) <= 0; minphys broken", todo);
158 			if (todo > MAXPHYS)
159 				panic("todo(%ld) > MAXPHYS; minphys broken",
160 				      todo);
161 #endif
162 
163 			/*
164 			 * [lock the part of the user address space involved
165 			 *    in the transfer]
166 			 * Beware vmapbuf(); it clobbers b_data and
167 			 * saves it in b_saveaddr.  However, vunmapbuf()
168 			 * restores it.
169 			 */
170 			PHOLD(p);
171 			error = uvm_vslock(p, bp->b_data, todo,
172 					   (flags & B_READ) ?
173 					   VM_PROT_READ | VM_PROT_WRITE :
174 					   VM_PROT_READ);
175 			if (error) {
176 				bp->b_flags |= B_ERROR;
177 				bp->b_error = error;
178 				goto after_vsunlock;
179 			}
180 			vmapbuf(bp, todo);
181 
182 			/* [call strategy to start the transfer] */
183 			(*strategy)(bp);
184 
185 			/*
186 			 * Note that the raise/wait/lower/get error
187 			 * steps below would be done by biowait(), but
188 			 * we want to unlock the address space before
189 			 * we lower the priority.
190 			 *
191 			 * [raise the priority level to splbio]
192 			 */
193 			s = splbio();
194 
195 			/* [wait for the transfer to complete] */
196 			while ((bp->b_flags & B_DONE) == 0)
197 				tsleep((caddr_t) bp, PRIBIO + 1, "physio", 0);
198 
199 			/* Mark it busy again, so nobody else will use it. */
200 			bp->b_flags |= B_BUSY;
201 
202 			/* [lower the priority level] */
203 			splx(s);
204 
205 			/*
206 			 * [unlock the part of the address space previously
207 			 *    locked]
208 			 */
209 			vunmapbuf(bp, todo);
210 			uvm_vsunlock(p, bp->b_data, todo);
211  after_vsunlock:
212 			PRELE(p);
213 
214 			/* remember error value (save a splbio/splx pair) */
215 			if (bp->b_flags & B_ERROR)
216 				error = (bp->b_error ? bp->b_error : EIO);
217 
218 			/*
219 			 * [deduct the transfer size from the total number
220 			 *    of data to transfer]
221 			 */
222 			done = bp->b_bcount - bp->b_resid;
223 			KASSERT(done >= 0);
224 			KASSERT(done <= todo);
225 
226 			iovp->iov_len -= done;
227 			iovp->iov_base = (caddr_t)iovp->iov_base + done;
228 			uio->uio_offset += done;
229 			uio->uio_resid -= done;
230 
231 			/*
232 			 * Now, check for an error.
233 			 * Also, handle weird end-of-disk semantics.
234 			 */
235 			if (error || done < todo)
236 				goto done;
237 		}
238 	}
239 
240 done:
241 	/*
242 	 * [clean up the state of the buffer]
243 	 * Remember if somebody wants it, so we can wake them up below.
244 	 * Also, if we had to steal it, give it back.
245 	 */
246 	s = splbio();
247 	bp->b_flags &= ~(B_BUSY | B_PHYS | B_RAW);
248 	if (nobuf)
249 		putphysbuf(bp);
250 	else {
251 		/*
252 		 * [if another process is waiting for the raw I/O buffer,
253 		 *    wake up processes waiting to do physical I/O;
254 		 */
255 		if (bp->b_flags & B_WANTED) {
256 			bp->b_flags &= ~B_WANTED;
257 			wakeup(bp);
258 		}
259 	}
260 	splx(s);
261 
262 	return (error);
263 }
264 
265 /*
266  * allocate a buffer structure for use in physical I/O.
267  */
268 struct buf *
269 getphysbuf()
270 {
271 	struct buf *bp;
272 	int s;
273 
274 	s = splbio();
275 	bp = pool_get(&bufpool, PR_WAITOK);
276 	splx(s);
277 	memset(bp, 0, sizeof(*bp));
278 	return(bp);
279 }
280 
281 /*
282  * get rid of a swap buffer structure which has been used in physical I/O.
283  */
284 void
285 putphysbuf(bp)
286         struct buf *bp;
287 {
288 	int s;
289 
290 	if (__predict_false(bp->b_flags & B_WANTED))
291 		panic("putphysbuf: private buf B_WANTED");
292 	s = splbio();
293 	pool_put(&bufpool, bp);
294 	splx(s);
295 }
296 
297 /*
298  * Leffler, et al., says on p. 231:
299  * "The minphys() routine is called by physio() to adjust the
300  * size of each I/O transfer before the latter is passed to
301  * the strategy routine..."
302  *
303  * so, just adjust the buffer's count accounting to MAXPHYS here,
304  * and return the new count;
305  */
306 void
307 minphys(bp)
308 	struct buf *bp;
309 {
310 
311 	if (bp->b_bcount > MAXPHYS)
312 		bp->b_bcount = MAXPHYS;
313 }
314