]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_physio.c
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. 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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
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.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
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.
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
67 * from: @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
71 * 27-July-97 Umesh Vaishampayan (umeshv@apple.com)
72 * Allow physio() to kernel space.
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/buf_internal.h>
79 #include <sys/proc_internal.h>
80 #include <sys/uio_internal.h>
83 physio(strategy
, bp
, dev
, flags
, minphys
, uio
, blocksize
)
92 struct proc
*p
= current_proc();
93 int error
, i
, nobuf
, todo
, iosize
;
101 flags
&= B_READ
| B_WRITE
;
104 * [check user read/write access to the data buffer]
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.
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
))
118 /* Make sure we have a buffer, creating one if necessary. */
119 if (nobuf
= (bp
== NULL
)) {
120 bp
= buf_alloc((vnode_t
)0);
123 /* [while the buffer is marked busy] */
124 while (((error
= (int)buf_acquire(bp
, 0, 0, 0)) == EAGAIN
));
132 /* [set up the fixed part of the buffer for a transfer] */
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.
142 for (i
= 0; i
< uio
->uio_iovcnt
; i
++) {
143 while (uio_iov_len_at(uio
, i
) > 0) {
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.)
151 buf_setflags(bp
, B_PHYS
| B_RAW
| flags
);
153 if ( (iosize
= uio_iov_len_at(uio
, i
)) > MAXPHYSIO_WIRED
)
154 iosize
= MAXPHYSIO_WIRED
;
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
)));
163 * [call minphys to bound the tranfer size]
164 * and remember the amount of data to transfer,
165 * for later comparison.
168 todo
= buf_count(bp
);
171 * [lock the part of the user address space involved
175 if(UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
176 vslock(CAST_USER_ADDR_T(buf_dataptr(bp
)),
179 /* [call strategy to start the transfer] */
183 /* [wait for the transfer to complete] */
184 error
= (int)buf_biowait(bp
);
187 * [unlock the part of the address space previously
190 if(UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
191 vsunlock(CAST_USER_ADDR_T(buf_dataptr(bp
)),
196 * [deduct the transfer size from the total number
197 * of data to transfer]
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
));
206 * Now, check for an error.
207 * Also, handle weird end-of-disk semantics.
209 if (error
|| done
< todo
)
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.
221 buf_clearflags(bp
, B_PHYS
| B_RAW
);
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..."
238 * so, just adjust the buffer's count accounting to MAXPHYS here,
239 * and return the new count;
246 buf_setcount(bp
, min(MAXPHYS
, buf_count(bp
)));
247 return buf_count(bp
);
251 * Do a read on a device for a user process.
257 return (physio(cdevsw
[major(dev
)].d_strategy
, (struct buf
*)NULL
,
258 dev
, B_READ
, minphys
, uio
, DEV_BSIZE
));
262 * Do a write on a device for a user process.
268 return (physio(cdevsw
[major(dev
)].d_strategy
, (struct buf
*)NULL
,
269 dev
, B_WRITE
, minphys
, uio
, DEV_BSIZE
));