]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/kern/kern_physio.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / bsd / kern / kern_physio.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*-
29 * Copyright (c) 1982, 1986, 1990, 1993
30 * The Regents of the University of California. All rights reserved.
31 * (c) UNIX System Laboratories, Inc.
32 * All or some portions of this file are derived from material licensed
33 * to the University of California by American Telephone and Telegraph
34 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
35 * the permission of UNIX System Laboratories, Inc.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * from: @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
66 */
67/*
68 * HISTORY
69 * 27-July-97 Umesh Vaishampayan (umeshv@apple.com)
70 * Allow physio() to kernel space.
71 */
72
73#include <sys/param.h>
74#include <sys/systm.h>
75#include <sys/buf_internal.h>
76#include <sys/conf.h>
77#include <sys/proc_internal.h>
78#include <sys/uio_internal.h>
79
80int
81physio(strategy, bp, dev, flags, minphys, uio, blocksize)
82 void (*strategy)();
83 buf_t bp;
84 dev_t dev;
85 int flags;
86 u_int (*minphys)();
87 struct uio *uio;
88 int blocksize;
89{
90 struct proc *p = current_proc();
91 int error, i, nobuf, todo, iosize;
92#if LP64KERN
93 int64_t done;
94#else
95 int done;
96#endif
97
98 error = 0;
99 flags &= B_READ | B_WRITE;
100
101 /*
102 * [check user read/write access to the data buffer]
103 *
104 * Check each iov one by one. Note that we know if we're reading or
105 * writing, so we ignore the uio's rw parameter. Also note that if
106 * we're doing a read, that's a *write* to user-space.
107 */
108 for (i = 0; i < uio->uio_iovcnt; i++) {
109 if(UIO_SEG_IS_USER_SPACE(uio->uio_segflg)) {
110 if (!useracc(uio_iov_base_at(uio, i),
111 uio_iov_len_at(uio, i),
112 (flags == B_READ) ? B_WRITE : B_READ))
113 return (EFAULT);
114 }
115 }
116 /* Make sure we have a buffer, creating one if necessary. */
117 if (nobuf = (bp == NULL)) {
118 bp = buf_alloc((vnode_t)0);
119 }
120
121 /* [while the buffer is marked busy] */
122 while (((error = (int)buf_acquire(bp, 0, 0, 0)) == EAGAIN));
123
124 if (error) {
125 if (nobuf)
126 buf_free(bp);
127 return (error);
128 }
129
130 /* [set up the fixed part of the buffer for a transfer] */
131 bp->b_dev = dev;
132 bp->b_proc = p;
133
134 buf_seterror(bp, 0);
135 /*
136 * [while there is data to transfer and no I/O error]
137 * Note that I/O errors are handled with a 'goto' at the bottom
138 * of the 'while' loop.
139 */
140 for (i = 0; i < uio->uio_iovcnt; i++) {
141 while (uio_iov_len_at(uio, i) > 0) {
142 /*
143 * [mark the buffer busy for physical I/O]
144 * (i.e. set B_PHYS (because it's an I/O to user
145 * memory, and B_RAW, because B_RAW is to be
146 * "Set by physio for raw transfers.", in addition
147 * to the read/write flag.)
148 */
149 buf_setflags(bp, B_PHYS | B_RAW | flags);
150
151 if ( (iosize = uio_iov_len_at(uio, i)) > MAXPHYSIO_WIRED)
152 iosize = MAXPHYSIO_WIRED;
153
154 /* [set up the buffer for a maximum-sized transfer] */
155 buf_setblkno(bp, uio->uio_offset / blocksize);
156 buf_setcount(bp, iosize);
157 // LP64todo - fix this!
158 buf_setdataptr(bp, CAST_DOWN(caddr_t, uio_iov_base_at(uio, i)));
159
160 /*
161 * [call minphys to bound the tranfer size]
162 * and remember the amount of data to transfer,
163 * for later comparison.
164 */
165 (*minphys)(bp);
166 todo = buf_count(bp);
167
168 /*
169 * [lock the part of the user address space involved
170 * in the transfer]
171 */
172
173 if(UIO_SEG_IS_USER_SPACE(uio->uio_segflg))
174 vslock(CAST_USER_ADDR_T(buf_dataptr(bp)),
175 (user_size_t)todo);
176
177 /* [call strategy to start the transfer] */
178 (*strategy)(bp);
179
180
181 /* [wait for the transfer to complete] */
182 error = (int)buf_biowait(bp);
183
184 /*
185 * [unlock the part of the address space previously
186 * locked]
187 */
188 if(UIO_SEG_IS_USER_SPACE(uio->uio_segflg))
189 vsunlock(CAST_USER_ADDR_T(buf_dataptr(bp)),
190 (user_size_t)todo,
191 (flags & B_READ));
192
193 /*
194 * [deduct the transfer size from the total number
195 * of data to transfer]
196 */
197 done = buf_count(bp) - buf_resid(bp);
198 uio_iov_len_add_at(uio, -done, i);
199 uio_iov_base_add_at(uio, done, i);
200 uio->uio_offset += done;
201 uio_setresid(uio, (uio_resid(uio) - done));
202
203 /*
204 * Now, check for an error.
205 * Also, handle weird end-of-disk semantics.
206 */
207 if (error || done < todo)
208 goto done;
209 }
210 }
211
212done:
213 /*
214 * [clean up the state of the buffer]
215 * Remember if somebody wants it, so we can wake them up below.
216 * Also, if we had to steal it, give it back.
217 */
218
219 buf_clearflags(bp, B_PHYS | B_RAW);
220 if (nobuf)
221 buf_free(bp);
222 else
223 {
224 buf_drop(bp);
225 }
226
227 return (error);
228}
229
230/*
231 * Leffler, et al., says on p. 231:
232 * "The minphys() routine is called by physio() to adjust the
233 * size of each I/O transfer before the latter is passed to
234 * the strategy routine..."
235 *
236 * so, just adjust the buffer's count accounting to MAXPHYS here,
237 * and return the new count;
238 */
239u_int
240minphys(bp)
241 struct buf *bp;
242{
243
244 buf_setcount(bp, min(MAXPHYS, buf_count(bp)));
245 return buf_count(bp);
246}
247
248/*
249 * Do a read on a device for a user process.
250 */
251rawread(dev, uio)
252 dev_t dev;
253 struct uio *uio;
254{
255 return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
256 dev, B_READ, minphys, uio, DEV_BSIZE));
257}
258
259/*
260 * Do a write on a device for a user process.
261 */
262rawwrite(dev, uio)
263 dev_t dev;
264 struct uio *uio;
265{
266 return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
267 dev, B_WRITE, minphys, uio, DEV_BSIZE));
268}