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