]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_physio.c
5e39729998c8965662f3845a3d510ab3f2548711
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
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.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
60 * from: @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
64 * 27-July-97 Umesh Vaishampayan (umeshv@apple.com)
65 * Allow physio() to kernel space.
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/buf_internal.h>
72 #include <sys/proc_internal.h>
73 #include <sys/uio_internal.h>
76 physio(strategy
, bp
, dev
, flags
, minphys
, uio
, blocksize
)
85 struct proc
*p
= current_proc();
86 int error
, i
, nobuf
, todo
, iosize
;
94 flags
&= B_READ
| B_WRITE
;
97 * [check user read/write access to the data buffer]
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.
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
))
111 /* Make sure we have a buffer, creating one if necessary. */
112 if (nobuf
= (bp
== NULL
)) {
113 bp
= buf_alloc((vnode_t
)0);
116 /* [while the buffer is marked busy] */
117 while (((error
= (int)buf_acquire(bp
, 0, 0, 0)) == EAGAIN
));
125 /* [set up the fixed part of the buffer for a transfer] */
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.
135 for (i
= 0; i
< uio
->uio_iovcnt
; i
++) {
136 while (uio_iov_len_at(uio
, i
) > 0) {
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.)
144 buf_setflags(bp
, B_PHYS
| B_RAW
| flags
);
146 if ( (iosize
= uio_iov_len_at(uio
, i
)) > MAXPHYSIO_WIRED
)
147 iosize
= MAXPHYSIO_WIRED
;
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
)));
156 * [call minphys to bound the tranfer size]
157 * and remember the amount of data to transfer,
158 * for later comparison.
161 todo
= buf_count(bp
);
164 * [lock the part of the user address space involved
168 if(UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
169 vslock(CAST_USER_ADDR_T(buf_dataptr(bp
)),
172 /* [call strategy to start the transfer] */
176 /* [wait for the transfer to complete] */
177 error
= (int)buf_biowait(bp
);
180 * [unlock the part of the address space previously
183 if(UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
184 vsunlock(CAST_USER_ADDR_T(buf_dataptr(bp
)),
189 * [deduct the transfer size from the total number
190 * of data to transfer]
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
));
199 * Now, check for an error.
200 * Also, handle weird end-of-disk semantics.
202 if (error
|| done
< todo
)
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.
214 buf_clearflags(bp
, B_PHYS
| B_RAW
);
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..."
231 * so, just adjust the buffer's count accounting to MAXPHYS here,
232 * and return the new count;
239 buf_setcount(bp
, min(MAXPHYS
, buf_count(bp
)));
240 return buf_count(bp
);
244 * Do a read on a device for a user process.
250 return (physio(cdevsw
[major(dev
)].d_strategy
, (struct buf
*)NULL
,
251 dev
, B_READ
, minphys
, uio
, DEV_BSIZE
));
255 * Do a write on a device for a user process.
261 return (physio(cdevsw
[major(dev
)].d_strategy
, (struct buf
*)NULL
,
262 dev
, B_WRITE
, minphys
, uio
, DEV_BSIZE
));