]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_physio.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
25 * (c) UNIX System Laboratories, Inc.
26 * All or some portions of this file are derived from material licensed
27 * to the University of California by American Telephone and Telegraph
28 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
29 * the permission of UNIX System Laboratories, Inc.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * from: @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
63 * 27-July-97 Umesh Vaishampayan (umeshv@apple.com)
64 * Allow physio() to kernel space.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/buf_internal.h>
71 #include <sys/proc_internal.h>
72 #include <sys/uio_internal.h>
75 physio(strategy
, bp
, dev
, flags
, minphys
, uio
, blocksize
)
84 struct proc
*p
= current_proc();
85 int error
, i
, nobuf
, todo
, iosize
;
93 flags
&= B_READ
| B_WRITE
;
96 * [check user read/write access to the data buffer]
98 * Check each iov one by one. Note that we know if we're reading or
99 * writing, so we ignore the uio's rw parameter. Also note that if
100 * we're doing a read, that's a *write* to user-space.
102 for (i
= 0; i
< uio
->uio_iovcnt
; i
++) {
103 if(UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
)) {
104 if (!useracc(uio_iov_base_at(uio
, i
),
105 uio_iov_len_at(uio
, i
),
106 (flags
== B_READ
) ? B_WRITE
: B_READ
))
110 /* Make sure we have a buffer, creating one if necessary. */
111 if (nobuf
= (bp
== NULL
)) {
112 bp
= buf_alloc((vnode_t
)0);
115 /* [while the buffer is marked busy] */
116 while (((error
= (int)buf_acquire(bp
, 0, 0, 0)) == EAGAIN
));
124 /* [set up the fixed part of the buffer for a transfer] */
130 * [while there is data to transfer and no I/O error]
131 * Note that I/O errors are handled with a 'goto' at the bottom
132 * of the 'while' loop.
134 for (i
= 0; i
< uio
->uio_iovcnt
; i
++) {
135 while (uio_iov_len_at(uio
, i
) > 0) {
137 * [mark the buffer busy for physical I/O]
138 * (i.e. set B_PHYS (because it's an I/O to user
139 * memory, and B_RAW, because B_RAW is to be
140 * "Set by physio for raw transfers.", in addition
141 * to the read/write flag.)
143 buf_setflags(bp
, B_PHYS
| B_RAW
| flags
);
145 if ( (iosize
= uio_iov_len_at(uio
, i
)) > MAXPHYSIO_WIRED
)
146 iosize
= MAXPHYSIO_WIRED
;
148 /* [set up the buffer for a maximum-sized transfer] */
149 buf_setblkno(bp
, uio
->uio_offset
/ blocksize
);
150 buf_setcount(bp
, iosize
);
151 // LP64todo - fix this!
152 buf_setdataptr(bp
, CAST_DOWN(caddr_t
, uio_iov_base_at(uio
, i
)));
155 * [call minphys to bound the tranfer size]
156 * and remember the amount of data to transfer,
157 * for later comparison.
160 todo
= buf_count(bp
);
163 * [lock the part of the user address space involved
167 if(UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
168 vslock(CAST_USER_ADDR_T(buf_dataptr(bp
)),
171 /* [call strategy to start the transfer] */
175 /* [wait for the transfer to complete] */
176 error
= (int)buf_biowait(bp
);
179 * [unlock the part of the address space previously
182 if(UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
183 vsunlock(CAST_USER_ADDR_T(buf_dataptr(bp
)),
188 * [deduct the transfer size from the total number
189 * of data to transfer]
191 done
= buf_count(bp
) - buf_resid(bp
);
192 uio_iov_len_add_at(uio
, -done
, i
);
193 uio_iov_base_add_at(uio
, done
, i
);
194 uio
->uio_offset
+= done
;
195 uio_setresid(uio
, (uio_resid(uio
) - done
));
198 * Now, check for an error.
199 * Also, handle weird end-of-disk semantics.
201 if (error
|| done
< todo
)
208 * [clean up the state of the buffer]
209 * Remember if somebody wants it, so we can wake them up below.
210 * Also, if we had to steal it, give it back.
213 buf_clearflags(bp
, B_PHYS
| B_RAW
);
225 * Leffler, et al., says on p. 231:
226 * "The minphys() routine is called by physio() to adjust the
227 * size of each I/O transfer before the latter is passed to
228 * the strategy routine..."
230 * so, just adjust the buffer's count accounting to MAXPHYS here,
231 * and return the new count;
238 buf_setcount(bp
, min(MAXPHYS
, buf_count(bp
)));
239 return buf_count(bp
);
243 * Do a read on a device for a user process.
249 return (physio(cdevsw
[major(dev
)].d_strategy
, (struct buf
*)NULL
,
250 dev
, B_READ
, minphys
, uio
, DEV_BSIZE
));
254 * Do a write on a device for a user process.
260 return (physio(cdevsw
[major(dev
)].d_strategy
, (struct buf
*)NULL
,
261 dev
, B_WRITE
, minphys
, uio
, DEV_BSIZE
));