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