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