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