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