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