]>
git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_cluster.c
763ecc5330cd79ad08f0ea67ece86c646c41c67a
2 * Copyright (c) 2000 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@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)vfs_cluster.c 8.10 (Berkeley) 3/28/95
58 #include <sys/param.h>
61 #include <sys/vnode.h>
62 #include <sys/mount.h>
63 #include <sys/trace.h>
64 #include <sys/malloc.h>
65 #include <sys/resourcevar.h>
66 #include <libkern/libkern.h>
69 #include <vm/vm_pageout.h>
71 #include <sys/kdebug.h>
75 #define CL_COMMIT 0x04
77 #define CL_PAGEOUT 0x10
80 #define CL_NOZERO 0x80
81 #define CL_PAGEIN 0x100
82 #define CL_DEV_MEMORY 0x200
85 * throttle the number of async writes that
86 * can be outstanding on a single vnode
87 * before we issue a synchronous write
89 #define ASYNC_THROTTLE 6
102 struct buf
*cbp_head
;
103 struct buf
*cbp_next
;
110 cbp_head
= (struct buf
*)(bp
->b_trans_head
);
112 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_START
,
113 cbp_head
, bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
115 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
117 * all I/O requests that are part of this transaction
118 * have to complete before we can process it
120 if ( !(cbp
->b_flags
& B_DONE
)) {
122 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
123 cbp_head
, cbp
, cbp
->b_bcount
, cbp
->b_flags
, 0);
133 upl_offset
= cbp
->b_uploffset
;
134 upl
= cbp
->b_pagelist
;
135 b_flags
= cbp
->b_flags
;
136 real_bp
= cbp
->b_real_bp
;
140 if (cbp
->b_vectorcount
> 1)
141 _FREE(cbp
->b_vectorlist
, M_SEGMENT
);
143 if ((cbp
->b_flags
& B_ERROR
) && error
== 0)
144 error
= cbp
->b_error
;
146 total_resid
+= cbp
->b_resid
;
147 total_size
+= cbp
->b_bcount
;
149 cbp_next
= cbp
->b_trans_next
;
155 if ((vp
->v_flag
& VTHROTTLED
) && (vp
->v_numoutput
<= (ASYNC_THROTTLE
/ 3))) {
156 vp
->v_flag
&= ~VTHROTTLED
;
157 wakeup((caddr_t
)&vp
->v_numoutput
);
159 if ((b_flags
& B_NEED_IODONE
) && real_bp
) {
161 real_bp
->b_flags
|= B_ERROR
;
162 real_bp
->b_error
= error
;
164 real_bp
->b_resid
= total_resid
;
168 if (error
== 0 && total_resid
)
171 if (b_flags
& B_COMMIT_UPL
) {
172 pg_offset
= upl_offset
& PAGE_MASK
;
173 commit_size
= (((pg_offset
+ total_size
) + (PAGE_SIZE
- 1)) / PAGE_SIZE
) * PAGE_SIZE
;
175 if (error
|| (b_flags
& B_NOCACHE
)) {
178 if (b_flags
& B_PAGEOUT
)
179 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
180 else if (b_flags
& B_PGIN
)
181 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
183 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
185 ubc_upl_abort_range(upl
, upl_offset
- pg_offset
, commit_size
,
188 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
189 upl
, upl_offset
- pg_offset
, commit_size
,
190 0x80000000|upl_abort_code
, 0);
193 int upl_commit_flags
= UPL_COMMIT_FREE_ON_EMPTY
;
195 if ( !(b_flags
& B_PAGEOUT
))
196 upl_commit_flags
|= UPL_COMMIT_CLEAR_DIRTY
;
198 upl_commit_flags
|= UPL_COMMIT_INACTIVATE
;
200 ubc_upl_commit_range(upl
, upl_offset
- pg_offset
, commit_size
,
203 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
204 upl
, upl_offset
- pg_offset
, commit_size
,
205 upl_commit_flags
, 0);
208 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
209 upl
, upl_offset
, 0, error
, 0);
216 cluster_zero(upl
, upl_offset
, size
, flags
, bp
)
218 vm_offset_t upl_offset
;
223 vm_offset_t io_addr
= 0;
226 if ( !(flags
& CL_NOMAP
)) {
227 kret
= ubc_upl_map(upl
, &io_addr
);
229 if (kret
!= KERN_SUCCESS
)
230 panic("cluster_zero: ubc_upl_map() failed with (%d)", kret
);
232 panic("cluster_zero: ubc_upl_map() mapped 0");
234 io_addr
= (vm_offset_t
)bp
->b_data
;
235 bzero((caddr_t
)(io_addr
+ upl_offset
), size
);
237 if ( !(flags
& CL_NOMAP
)) {
238 kret
= ubc_upl_unmap(upl
);
240 if (kret
!= KERN_SUCCESS
)
241 panic("cluster_zero: kernel_upl_unmap failed");
246 cluster_io(vp
, upl
, upl_offset
, f_offset
, size
, flags
, real_bp
)
249 vm_offset_t upl_offset
;
260 struct buf
*cbp_head
= 0;
261 struct buf
*cbp_tail
= 0;
269 if (flags
& CL_READ
) {
270 io_flags
= (B_VECTORLIST
| B_READ
);
272 vfs_io_attributes(vp
, B_READ
, &max_iosize
, &max_vectors
);
274 io_flags
= (B_VECTORLIST
| B_WRITEINPROG
);
276 vfs_io_attributes(vp
, B_WRITE
, &max_iosize
, &max_vectors
);
278 pl
= ubc_upl_pageinfo(upl
);
280 if (flags
& CL_ASYNC
)
281 io_flags
|= (B_CALL
| B_ASYNC
);
285 io_flags
|= B_NOCACHE
;
286 if (flags
& CL_PAGEIN
)
290 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_START
,
291 (int)f_offset
, size
, upl_offset
, flags
, 0);
293 if ((flags
& CL_READ
) && ((upl_offset
+ size
) & PAGE_MASK
) && (!(flags
& CL_NOZERO
))) {
295 * then we are going to end up
296 * with a page that we can't complete (the file size wasn't a multiple
297 * of PAGE_SIZE and we're trying to read to the end of the file
298 * so we'll go ahead and zero out the portion of the page we can't
299 * read in from the file
301 cluster_zero(upl
, upl_offset
+ size
, PAGE_SIZE
- ((upl_offset
+ size
) & PAGE_MASK
), flags
, real_bp
);
303 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_NONE
,
304 upl_offset
+ size
, PAGE_SIZE
- ((upl_offset
+ size
) & PAGE_MASK
),
317 if (size
> max_iosize
)
318 io_size
= max_iosize
;
322 if (error
= VOP_CMAP(vp
, f_offset
, io_size
, &blkno
, &io_size
, NULL
)) {
323 if (error
== EOPNOTSUPP
)
324 panic("VOP_CMAP Unimplemented");
328 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 24)) | DBG_FUNC_NONE
,
329 (int)f_offset
, (int)blkno
, io_size
, 0, 0);
331 if ( (!(flags
& CL_READ
) && (long)blkno
== -1) || io_size
== 0) {
332 if (flags
& CL_PAGEOUT
) {
337 /* Try paging out the page individually before
338 giving up entirely and dumping it (it could
339 be mapped in a "hole" and require allocation
342 ubc_upl_abort_range(upl
, upl_offset
, PAGE_SIZE_64
, UPL_ABORT_FREE_ON_EMPTY
);
343 if (ubc_pushdirty_range(vp
, f_offset
, PAGE_SIZE_64
) == 0) {
348 upl_offset
+= PAGE_SIZE_64
;
349 f_offset
+= PAGE_SIZE_64
;
350 size
-= PAGE_SIZE_64
;
353 lblkno
= (daddr_t
)(f_offset
/ PAGE_SIZE_64
);
355 * we have now figured out how much I/O we can do - this is in 'io_size'
356 * pl_index represents the first page in the 'upl' that the I/O will occur for
357 * pg_offset is the starting point in the first page for the I/O
358 * pg_count is the number of full and partial pages that 'io_size' encompasses
360 pl_index
= upl_offset
/ PAGE_SIZE
;
361 pg_offset
= upl_offset
& PAGE_MASK
;
362 pg_count
= (io_size
+ pg_offset
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
364 if (flags
& CL_DEV_MEMORY
) {
366 * currently, can't deal with reading 'holes' in file
368 if ((long)blkno
== -1) {
373 * treat physical requests as one 'giant' page
377 if ((flags
& CL_READ
) && (long)blkno
== -1) {
379 * if we're reading and blkno == -1, then we've got a
380 * 'hole' in the file that we need to deal with by zeroing
381 * out the affected area in the upl
383 cluster_zero(upl
, upl_offset
, io_size
, flags
, real_bp
);
385 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_NONE
,
386 upl_offset
, io_size
, flags
, real_bp
, 0);
388 pg_count
= (io_size
- pg_offset
) / PAGE_SIZE
;
390 if (io_size
== size
&& ((upl_offset
+ io_size
) & PAGE_MASK
))
395 pg_resid
= PAGE_SIZE
- pg_offset
;
398 if (flags
& CL_COMMIT
)
399 ubc_upl_commit_range(upl
,
400 upl_offset
+ pg_resid
,
401 pg_count
* PAGE_SIZE
,
402 UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
);
404 upl_offset
+= io_size
;
408 if (cbp_head
&& pg_count
)
411 } else if (real_bp
&& (real_bp
->b_blkno
== real_bp
->b_lblkno
)) {
412 real_bp
->b_blkno
= blkno
;
416 if (pg_count
> max_vectors
) {
417 io_size
-= (pg_count
- max_vectors
) * PAGE_SIZE
;
420 io_size
= PAGE_SIZE
- pg_offset
;
423 pg_count
= max_vectors
;
426 * we need to allocate space for the vector list
429 iovp
= (struct iovec
*)_MALLOC(sizeof(struct iovec
) * pg_count
,
430 M_SEGMENT
, M_NOWAIT
);
432 if (iovp
== (struct iovec
*) 0) {
434 * if the allocation fails, then throttle down to a single page
436 io_size
= PAGE_SIZE
- pg_offset
;
442 /* Throttle the speculative IO */
443 if ((flags
& CL_ASYNC
) && !(flags
& CL_PAGEOUT
))
448 cbp
= alloc_io_buf(vp
, priv
);
452 * we use the io vector that's reserved in the buffer header
453 * this insures we can always issue an I/O even in a low memory
454 * condition that prevents the _MALLOC from succeeding... this
455 * is necessary to prevent deadlocks with the pager
457 iovp
= (struct iovec
*)(&cbp
->b_vects
[0]);
459 cbp
->b_vectorlist
= (void *)iovp
;
460 cbp
->b_vectorcount
= pg_count
;
462 if (flags
& CL_DEV_MEMORY
) {
464 iovp
->iov_len
= io_size
;
465 iovp
->iov_base
= (caddr_t
)upl_phys_page(pl
, 0);
467 if (iovp
->iov_base
== (caddr_t
) 0) {
471 iovp
->iov_base
+= upl_offset
;
474 for (i
= 0, vsize
= io_size
; i
< pg_count
; i
++, iovp
++) {
477 psize
= PAGE_SIZE
- pg_offset
;
482 iovp
->iov_len
= psize
;
483 iovp
->iov_base
= (caddr_t
)upl_phys_page(pl
, pl_index
+ i
);
485 if (iovp
->iov_base
== (caddr_t
) 0) {
487 _FREE(cbp
->b_vectorlist
, M_SEGMENT
);
493 iovp
->iov_base
+= pg_offset
;
496 if (flags
& CL_PAGEOUT
) {
501 if (bp
= incore(vp
, lblkno
+ i
)) {
502 if (!ISSET(bp
->b_flags
, B_BUSY
)) {
504 SET(bp
->b_flags
, (B_BUSY
| B_INVAL
));
508 panic("BUSY bp found in cluster_io");
518 if (flags
& CL_ASYNC
)
519 cbp
->b_iodone
= (void *)cluster_iodone
;
520 cbp
->b_flags
|= io_flags
;
522 cbp
->b_lblkno
= lblkno
;
523 cbp
->b_blkno
= blkno
;
524 cbp
->b_bcount
= io_size
;
525 cbp
->b_pagelist
= upl
;
526 cbp
->b_uploffset
= upl_offset
;
527 cbp
->b_trans_next
= (struct buf
*)0;
530 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 26)) | DBG_FUNC_NONE
,
531 cbp
->b_lblkno
, cbp
->b_blkno
, upl_offset
, io_size
, 0);
533 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 27)) | DBG_FUNC_NONE
,
534 cbp
->b_lblkno
, cbp
->b_blkno
, upl_offset
, io_size
, 0);
537 cbp_tail
->b_trans_next
= cbp
;
543 (struct buf
*)(cbp
->b_trans_head
) = cbp_head
;
545 upl_offset
+= io_size
;
549 if ( (!(upl_offset
& PAGE_MASK
) && !(flags
& CL_DEV_MEMORY
)) || size
== 0) {
551 * if we have no more I/O to issue or
552 * the current I/O we've prepared fully
553 * completes the last page in this request
554 * or it's been completed via a zero-fill
555 * due to a 'hole' in the file
556 * then go ahead and issue the I/O
559 if (flags
& CL_COMMIT
)
560 cbp_head
->b_flags
|= B_COMMIT_UPL
;
561 if (flags
& CL_PAGEOUT
)
562 cbp_head
->b_flags
|= B_PAGEOUT
;
563 if (flags
& CL_PAGEIN
)
564 cbp_head
->b_flags
|= B_PGIN
;
567 cbp_head
->b_flags
|= B_NEED_IODONE
;
568 cbp_head
->b_real_bp
= real_bp
;
571 for (cbp
= cbp_head
; cbp
;) {
572 struct buf
* cbp_next
;
574 if (io_flags
& B_WRITEINPROG
)
575 cbp
->b_vp
->v_numoutput
++;
577 cbp_next
= cbp
->b_trans_next
;
579 (void) VOP_STRATEGY(cbp
);
582 if ( !(flags
& CL_ASYNC
)) {
583 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
586 if (error
= cluster_iodone(cbp_head
)) {
591 cbp_head
= (struct buf
*)0;
592 cbp_tail
= (struct buf
*)0;
598 for (cbp
= cbp_head
; cbp
;) {
599 struct buf
* cbp_next
;
601 if (cbp
->b_vectorcount
> 1)
602 _FREE(cbp
->b_vectorlist
, M_SEGMENT
);
603 upl_offset
-= cbp
->b_bcount
;
604 size
+= cbp
->b_bcount
;
606 cbp_next
= cbp
->b_trans_next
;
610 pg_offset
= upl_offset
& PAGE_MASK
;
611 abort_size
= ((size
+ pg_offset
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
) * PAGE_SIZE
;
613 if (flags
& CL_COMMIT
) {
616 if (flags
& CL_PAGEOUT
)
617 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
618 else if (flags
& CL_PAGEIN
)
619 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
621 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
623 ubc_upl_abort_range(upl
, upl_offset
- pg_offset
, abort_size
,
626 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 28)) | DBG_FUNC_NONE
,
627 upl
, upl_offset
- pg_offset
, abort_size
, error
, 0);
630 real_bp
->b_flags
|= B_ERROR
;
631 real_bp
->b_error
= error
;
638 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_END
,
639 (int)f_offset
, size
, upl_offset
, retval
, 0);
646 cluster_rd_prefetch(vp
, f_offset
, size
, filesize
, devblocksize
)
662 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_START
,
663 (int)f_offset
, size
, (int)filesize
, 0, 0);
665 if (f_offset
>= filesize
) {
666 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
667 (int)f_offset
, 0, 0, 0, 0);
670 if (ubc_page_op(vp
, f_offset
, 0, 0, 0) == KERN_SUCCESS
) {
671 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
672 (int)f_offset
, 0, 0, 0, 0);
675 if (size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
676 size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
678 size
= (size
+ (PAGE_SIZE
- 1)) & ~(PAGE_SIZE
- 1);
680 if ((off_t
)size
> (filesize
- f_offset
))
681 size
= ((filesize
- f_offset
) + (devblocksize
- 1)) & ~(devblocksize
- 1);
683 pages_in_upl
= (size
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
687 pages_in_upl
* PAGE_SIZE
,
692 if (upl
== (upl_t
) 0)
696 * scan from the beginning of the upl looking for the first
697 * non-valid page.... this will become the first page in
698 * the request we're going to make to 'cluster_io'... if all
699 * of the pages are valid, we won't call through to 'cluster_io'
701 for (start_pg
= 0; start_pg
< pages_in_upl
; start_pg
++) {
702 if (!upl_valid_page(pl
, start_pg
))
707 * scan from the starting invalid page looking for a valid
708 * page before the end of the upl is reached, if we
709 * find one, then it will be the last page of the request to
712 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
713 if (upl_valid_page(pl
, last_pg
))
718 * if we find any more free valid pages at the tail of the upl
719 * than update maxra accordingly....
721 for (last_valid
= last_pg
; last_valid
< pages_in_upl
; last_valid
++) {
722 if (!upl_valid_page(pl
, last_valid
))
725 if (start_pg
< last_pg
) {
726 vm_offset_t upl_offset
;
729 * we found a range of 'invalid' pages that must be filled
730 * 'size' has already been clipped to the LEOF
731 * make sure it's at least a multiple of the device block size
733 upl_offset
= start_pg
* PAGE_SIZE
;
734 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
736 if ((upl_offset
+ io_size
) > size
) {
737 io_size
= size
- upl_offset
;
739 KERNEL_DEBUG(0xd001000, upl_offset
, size
, io_size
, 0, 0);
741 cluster_io(vp
, upl
, upl_offset
, f_offset
+ upl_offset
, io_size
,
742 CL_READ
| CL_COMMIT
| CL_ASYNC
| CL_AGE
, (struct buf
*)0);
746 * start_pg of non-zero indicates we found some already valid pages
747 * at the beginning of the upl.... we need to release these without
748 * modifying there state
750 ubc_upl_abort_range(upl
, 0, start_pg
* PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
752 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 50)) | DBG_FUNC_NONE
,
753 upl
, 0, start_pg
* PAGE_SIZE
, 0, 0);
755 if (last_pg
< pages_in_upl
) {
757 * the set of pages that we issued an I/O for did not extend all the
758 * way to the end of the upl... so just release them without modifying
761 ubc_upl_abort_range(upl
, last_pg
* PAGE_SIZE
, (pages_in_upl
- last_pg
) * PAGE_SIZE
,
762 UPL_ABORT_FREE_ON_EMPTY
);
764 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 50)) | DBG_FUNC_NONE
,
765 upl
, last_pg
* PAGE_SIZE
, (pages_in_upl
- last_pg
) * PAGE_SIZE
, 0, 0);
768 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
769 (int)f_offset
+ (last_valid
* PAGE_SIZE
), 0, 0, 0, 0);
777 cluster_rd_ahead(vp
, b_lblkno
, e_lblkno
, filesize
, devblocksize
)
786 int size_of_prefetch
;
790 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_START
,
791 b_lblkno
, e_lblkno
, vp
->v_lastr
, 0, 0);
793 if (b_lblkno
== vp
->v_lastr
&& b_lblkno
== e_lblkno
) {
794 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
795 vp
->v_ralen
, vp
->v_maxra
, vp
->v_lastr
, 0, 0);
799 if (vp
->v_lastr
== -1 || (b_lblkno
!= vp
->v_lastr
&& b_lblkno
!= (vp
->v_lastr
+ 1) && b_lblkno
!= (vp
->v_maxra
+ 1))) {
803 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
804 vp
->v_ralen
, vp
->v_maxra
, vp
->v_lastr
, 1, 0);
808 vfs_io_attributes(vp
, B_READ
, &max_iosize
, &max_pages
);
810 if ((max_iosize
/ PAGE_SIZE
) < max_pages
)
811 max_pages
= max_iosize
/ PAGE_SIZE
;
812 if (max_pages
> MAX_UPL_TRANSFER
)
813 max_pages
= MAX_UPL_TRANSFER
;
815 vp
->v_ralen
= vp
->v_ralen
? min(max_pages
, vp
->v_ralen
<< 1) : 1;
817 if (((e_lblkno
+ 1) - b_lblkno
) > vp
->v_ralen
)
818 vp
->v_ralen
= min(max_pages
, (e_lblkno
+ 1) - b_lblkno
);
820 if (e_lblkno
< vp
->v_maxra
) {
821 if ((vp
->v_maxra
- e_lblkno
) > (max_pages
/ 4)) {
823 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
824 vp
->v_ralen
, vp
->v_maxra
, vp
->v_lastr
, 2, 0);
828 r_lblkno
= max(e_lblkno
, vp
->v_maxra
) + 1;
829 f_offset
= (off_t
)r_lblkno
* PAGE_SIZE_64
;
831 size_of_prefetch
= cluster_rd_prefetch(vp
, f_offset
, vp
->v_ralen
* PAGE_SIZE
, filesize
, devblocksize
);
833 if (size_of_prefetch
)
834 vp
->v_maxra
= r_lblkno
+ (size_of_prefetch
- 1);
836 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
837 vp
->v_ralen
, vp
->v_maxra
, vp
->v_lastr
, 3, 0);
841 cluster_pageout(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, devblocksize
, flags
)
844 vm_offset_t upl_offset
;
854 int local_flags
= CL_PAGEOUT
;
856 if ((flags
& UPL_IOSYNC
) == 0)
857 local_flags
|= CL_ASYNC
;
858 if ((flags
& UPL_NOCOMMIT
) == 0)
859 local_flags
|= CL_COMMIT
;
861 if (upl
== (upl_t
) 0)
862 panic("cluster_pageout: can't handle NULL upl yet\n");
865 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 52)) | DBG_FUNC_NONE
,
866 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
869 * If they didn't specify any I/O, then we are done...
870 * we can't issue an abort because we don't know how
871 * big the upl really is
876 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
877 if (local_flags
& CL_COMMIT
)
878 ubc_upl_abort_range(upl
, upl_offset
, size
,
879 UPL_ABORT_FREE_ON_EMPTY
);
883 * can't page-in from a negative offset
884 * or if we're starting beyond the EOF
885 * or if the file offset isn't page aligned
886 * or the size requested isn't a multiple of PAGE_SIZE
888 if (f_offset
< 0 || f_offset
>= filesize
||
889 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
)) {
890 if (local_flags
& CL_COMMIT
)
891 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
894 max_size
= filesize
- f_offset
;
899 io_size
= (max_size
+ (devblocksize
- 1)) & ~(devblocksize
- 1);
901 pg_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
903 if (size
> pg_size
) {
904 if (local_flags
& CL_COMMIT
)
905 ubc_upl_abort_range(upl
, upl_offset
+ pg_size
, size
- pg_size
,
906 UPL_ABORT_FREE_ON_EMPTY
);
908 while (vp
->v_numoutput
>= ASYNC_THROTTLE
) {
909 vp
->v_flag
|= VTHROTTLED
;
910 tsleep((caddr_t
)&vp
->v_numoutput
, PRIBIO
+ 1, "cluster_pageout", 0);
913 return (cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
914 local_flags
, (struct buf
*)0));
918 cluster_pagein(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, devblocksize
, flags
)
921 vm_offset_t upl_offset
;
936 * If they didn't ask for any data, then we are done...
937 * we can't issue an abort because we don't know how
938 * big the upl really is
943 if ((flags
& UPL_NOCOMMIT
) == 0)
944 local_flags
= CL_COMMIT
;
946 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 56)) | DBG_FUNC_NONE
,
947 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
950 * can't page-in from a negative offset
951 * or if we're starting beyond the EOF
952 * or if the file offset isn't page aligned
953 * or the size requested isn't a multiple of PAGE_SIZE
955 if (f_offset
< 0 || f_offset
>= filesize
||
956 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
)) {
957 if (local_flags
& CL_COMMIT
)
958 ubc_upl_abort_range(upl
, upl_offset
, size
,
959 UPL_ABORT_ERROR
| UPL_ABORT_FREE_ON_EMPTY
);
962 max_size
= filesize
- f_offset
;
967 io_size
= (max_size
+ (devblocksize
- 1)) & ~(devblocksize
- 1);
969 pg_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
971 if (upl
== (upl_t
) 0) {
979 if (upl
== (upl_t
) 0)
982 upl_offset
= (vm_offset_t
)0;
985 if (size
> pg_size
) {
986 if (local_flags
& CL_COMMIT
)
987 ubc_upl_abort_range(upl
, upl_offset
+ pg_size
, size
- pg_size
,
988 UPL_ABORT_FREE_ON_EMPTY
);
991 retval
= cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
992 local_flags
| CL_READ
| CL_PAGEIN
, (struct buf
*)0);
998 b_lblkno
= (int)(f_offset
/ PAGE_SIZE_64
);
1000 ((f_offset
+ ((off_t
)io_size
- 1)) / PAGE_SIZE_64
);
1002 if (!(flags
& UPL_NORDAHEAD
) && !(vp
->v_flag
& VRAOFF
)) {
1004 * we haven't read the last page in of the file yet
1005 * so let's try to read ahead if we're in
1006 * a sequential access pattern
1008 cluster_rd_ahead(vp
, b_lblkno
, e_lblkno
, filesize
, devblocksize
);
1010 vp
->v_lastr
= e_lblkno
;
1022 if (bp
->b_pagelist
== (upl_t
) 0)
1023 panic("cluster_bp: can't handle NULL upl yet\n");
1024 if (bp
->b_flags
& B_READ
)
1025 flags
= CL_ASYNC
| CL_NOMAP
| CL_READ
;
1027 flags
= CL_ASYNC
| CL_NOMAP
;
1029 f_offset
= ubc_blktooff(bp
->b_vp
, bp
->b_lblkno
);
1031 return (cluster_io(bp
->b_vp
, bp
->b_pagelist
, 0, f_offset
, bp
->b_bcount
, flags
, bp
));
1035 cluster_write(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, devblocksize
, flags
)
1049 vm_offset_t upl_offset
;
1052 upl_page_info_t
*pl
;
1058 if ((!uio
) || (uio
->uio_segflg
!= UIO_USERSPACE
) || (!(vp
->v_flag
& VNOCACHE_DATA
)))
1060 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, devblocksize
, flags
);
1064 while (uio
->uio_resid
&& uio
->uio_offset
< newEOF
&& retval
== 0)
1066 /* we know we have a resid, so this is safe */
1068 while (iov
->iov_len
== 0) {
1075 * We check every vector target and if it is physically
1076 * contiguous space, we skip the sanity checks.
1079 upl_offset
= (vm_offset_t
)iov
->iov_base
& ~PAGE_MASK
;
1080 upl_size
= (upl_offset
+ PAGE_SIZE
+(PAGE_SIZE
-1)) & ~PAGE_MASK
;
1082 upl_flags
= UPL_QUERY_OBJECT_TYPE
;
1083 if ((vm_map_get_upl(current_map(),
1084 (vm_offset_t
)iov
->iov_base
& ~PAGE_MASK
,
1085 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
, 0)) != KERN_SUCCESS
)
1088 * the user app must have passed in an invalid address
1093 if (upl_flags
& UPL_PHYS_CONTIG
)
1096 * since the interface to the IOKit below us uses physical block #'s and
1097 * block counts to specify the I/O, we can't handle anything that isn't
1098 * devblocksize aligned
1100 if ((uio
->uio_offset
& (devblocksize
- 1)) || (uio
->uio_resid
& (devblocksize
- 1)))
1103 if (flags
& IO_HEADZEROFILL
)
1105 flags
&= ~IO_HEADZEROFILL
;
1107 if (retval
= cluster_write_x(vp
, (struct uio
*)0, 0, uio
->uio_offset
, headOff
, 0, devblocksize
, IO_HEADZEROFILL
))
1111 retval
= cluster_phys_write(vp
, uio
);
1113 if (uio
->uio_resid
== 0 && (flags
& IO_TAILZEROFILL
))
1115 retval
= cluster_write_x(vp
, (struct uio
*)0, 0, tailOff
, uio
->uio_offset
, 0, devblocksize
, IO_HEADZEROFILL
);
1119 else if ((uio
->uio_resid
< 4 * PAGE_SIZE
) || (flags
& (IO_TAILZEROFILL
| IO_HEADZEROFILL
)))
1122 * We set a threshhold of 4 pages to decide if the nocopy
1123 * write loop is worth the trouble...
1124 * we also come here if we're trying to zero the head and/or tail
1125 * of a partially written page, and the user source is not a physically contiguous region
1127 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, devblocksize
, flags
);
1130 else if (uio
->uio_offset
& PAGE_MASK_64
)
1132 /* Bring the file offset write up to a pagesize boundary */
1133 clip_size
= (PAGE_SIZE
- (uio
->uio_offset
& PAGE_MASK_64
));
1134 if (uio
->uio_resid
< clip_size
)
1135 clip_size
= uio
->uio_resid
;
1137 * Fake the resid going into the cluster_write_x call
1138 * and restore it on the way out.
1140 prev_resid
= uio
->uio_resid
;
1141 uio
->uio_resid
= clip_size
;
1142 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, devblocksize
, flags
);
1143 uio
->uio_resid
= prev_resid
- (clip_size
- uio
->uio_resid
);
1145 else if ((int)iov
->iov_base
& PAGE_MASK_64
)
1147 clip_size
= iov
->iov_len
;
1148 prev_resid
= uio
->uio_resid
;
1149 uio
->uio_resid
= clip_size
;
1150 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, devblocksize
, flags
);
1151 uio
->uio_resid
= prev_resid
- (clip_size
- uio
->uio_resid
);
1156 * If we come in here, we know the offset into
1157 * the file is on a pagesize boundary
1160 max_io_size
= newEOF
- uio
->uio_offset
;
1161 clip_size
= uio
->uio_resid
;
1162 if (iov
->iov_len
< clip_size
)
1163 clip_size
= iov
->iov_len
;
1164 if (max_io_size
< clip_size
)
1165 clip_size
= max_io_size
;
1167 if (clip_size
< PAGE_SIZE
)
1170 * Take care of tail end of write in this vector
1172 prev_resid
= uio
->uio_resid
;
1173 uio
->uio_resid
= clip_size
;
1174 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, devblocksize
, flags
);
1175 uio
->uio_resid
= prev_resid
- (clip_size
- uio
->uio_resid
);
1179 /* round clip_size down to a multiple of pagesize */
1180 clip_size
= clip_size
& ~(PAGE_MASK
);
1181 prev_resid
= uio
->uio_resid
;
1182 uio
->uio_resid
= clip_size
;
1183 retval
= cluster_nocopy_write(vp
, uio
, newEOF
, devblocksize
, flags
);
1184 if ((retval
== 0) && uio
->uio_resid
)
1185 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, devblocksize
, flags
);
1186 uio
->uio_resid
= prev_resid
- (clip_size
- uio
->uio_resid
);
1194 cluster_nocopy_write(vp
, uio
, newEOF
, devblocksize
, flags
)
1202 upl_page_info_t
*pl
;
1204 vm_offset_t upl_offset
;
1208 int upl_needed_size
;
1214 int force_data_sync
;
1217 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_START
,
1218 (int)uio
->uio_offset
, (int)uio
->uio_resid
,
1219 (int)newEOF
, devblocksize
, 0);
1222 * When we enter this routine, we know
1223 * -- the offset into the file is on a pagesize boundary
1224 * -- the resid is a page multiple
1225 * -- the resid will not exceed iov_len
1230 while (uio
->uio_resid
&& uio
->uio_offset
< newEOF
&& error
== 0) {
1231 io_size
= uio
->uio_resid
;
1233 if (io_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
1234 io_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
1236 upl_offset
= (vm_offset_t
)iov
->iov_base
& PAGE_MASK_64
;
1237 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
1239 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_START
,
1240 (int)upl_offset
, upl_needed_size
, iov
->iov_base
, io_size
, 0);
1242 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++)
1245 upl_size
= upl_needed_size
;
1246 upl_flags
= UPL_COPYOUT_FROM
| UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
;
1248 kret
= vm_map_get_upl(current_map(),
1249 (vm_offset_t
)iov
->iov_base
& ~PAGE_MASK
,
1257 if (kret
!= KERN_SUCCESS
)
1259 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
1262 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
1263 (int)uio
->uio_offset
, (int)uio
->uio_resid
, kret
, 1, 0);
1265 /* cluster_nocopy_write: failed to get pagelist */
1266 /* do not return kret here */
1270 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
1271 pages_in_pl
= upl_size
/ PAGE_SIZE
;
1273 for(i
=0; i
< pages_in_pl
; i
++)
1275 if (!upl_valid_page(pl
, i
))
1279 if (i
== pages_in_pl
)
1282 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
1283 UPL_ABORT_FREE_ON_EMPTY
);
1286 if (force_data_sync
>= 3)
1288 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
1289 i
, pages_in_pl
, upl_size
, kret
, 0);
1291 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
1292 (int)uio
->uio_offset
, (int)uio
->uio_resid
, kret
, 2, 0);
1297 * Consider the possibility that upl_size wasn't satisfied.
1299 if (upl_size
!= upl_needed_size
)
1300 io_size
= (upl_size
- (int)upl_offset
) & ~PAGE_MASK
;
1302 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
1303 (int)upl_offset
, upl_size
, iov
->iov_base
, io_size
, 0);
1307 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
1308 UPL_ABORT_FREE_ON_EMPTY
);
1309 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
1310 (int)uio
->uio_offset
, uio
->uio_resid
, 0, 3, 0);
1316 * Now look for pages already in the cache
1317 * and throw them away.
1320 upl_f_offset
= uio
->uio_offset
; /* this is page aligned in the file */
1321 max_io_size
= io_size
;
1323 while (max_io_size
) {
1326 * Flag UPL_POP_DUMP says if the page is found
1327 * in the page cache it must be thrown away.
1331 UPL_POP_SET
| UPL_POP_BUSY
| UPL_POP_DUMP
,
1333 max_io_size
-= PAGE_SIZE
;
1334 upl_f_offset
+= PAGE_SIZE
;
1338 * issue a synchronous write to cluster_io
1341 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_START
,
1342 (int)upl_offset
, (int)uio
->uio_offset
, io_size
, 0, 0);
1344 error
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
1345 io_size
, 0, (struct buf
*)0);
1349 * The cluster_io write completed successfully,
1350 * update the uio structure and commit.
1353 ubc_upl_commit_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
1354 UPL_COMMIT_FREE_ON_EMPTY
);
1356 iov
->iov_base
+= io_size
;
1357 iov
->iov_len
-= io_size
;
1358 uio
->uio_resid
-= io_size
;
1359 uio
->uio_offset
+= io_size
;
1362 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
1363 UPL_ABORT_FREE_ON_EMPTY
);
1366 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_END
,
1367 (int)upl_offset
, (int)uio
->uio_offset
, (int)uio
->uio_resid
, error
, 0);
1372 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
1373 (int)uio
->uio_offset
, (int)uio
->uio_resid
, error
, 4, 0);
1379 cluster_phys_write(vp
, uio
)
1384 vm_offset_t upl_offset
;
1387 int upl_needed_size
;
1395 * When we enter this routine, we know
1396 * -- the resid will not exceed iov_len
1397 * -- the vector target address is physcially contiguous
1401 io_size
= iov
->iov_len
;
1402 upl_offset
= (vm_offset_t
)iov
->iov_base
& PAGE_MASK_64
;
1403 upl_needed_size
= upl_offset
+ io_size
;
1406 upl_size
= upl_needed_size
;
1407 upl_flags
= UPL_COPYOUT_FROM
| UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
;
1409 kret
= vm_map_get_upl(current_map(),
1410 (vm_offset_t
)iov
->iov_base
& ~PAGE_MASK
,
1411 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
, 0);
1413 if (kret
!= KERN_SUCCESS
)
1415 /* cluster_phys_write: failed to get pagelist */
1416 /* note: return kret here */
1421 * Consider the possibility that upl_size wasn't satisfied.
1422 * This is a failure in the physical memory case.
1424 if (upl_size
< upl_needed_size
)
1426 kernel_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
1431 * issue a synchronous write to cluster_io
1434 error
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
1435 io_size
, CL_DEV_MEMORY
, (struct buf
*)0);
1439 * The cluster_io write completed successfully,
1440 * update the uio structure and commit.
1443 ubc_upl_commit_range(upl
, 0, upl_size
, UPL_COMMIT_FREE_ON_EMPTY
);
1445 iov
->iov_base
+= io_size
;
1446 iov
->iov_len
-= io_size
;
1447 uio
->uio_resid
-= io_size
;
1448 uio
->uio_offset
+= io_size
;
1451 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
1457 cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, devblocksize
, flags
)
1467 upl_page_info_t
*pl
;
1469 vm_offset_t upl_offset
;
1476 int io_size_before_rounding
;
1478 vm_offset_t io_address
;
1485 long long total_size
;
1488 long long zero_cnt1
;
1490 daddr_t start_blkno
;
1494 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
1495 (int)uio
->uio_offset
, uio
->uio_resid
, (int)oldEOF
, (int)newEOF
, 0);
1497 uio_resid
= uio
->uio_resid
;
1499 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
1500 0, 0, (int)oldEOF
, (int)newEOF
, 0);
1507 if (flags
& IO_HEADZEROFILL
) {
1509 * some filesystems (HFS is one) don't support unallocated holes within a file...
1510 * so we zero fill the intervening space between the old EOF and the offset
1511 * where the next chunk of real data begins.... ftruncate will also use this
1512 * routine to zero fill to the new EOF when growing a file... in this case, the
1513 * uio structure will not be provided
1516 if (headOff
< uio
->uio_offset
) {
1517 zero_cnt
= uio
->uio_offset
- headOff
;
1520 } else if (headOff
< newEOF
) {
1521 zero_cnt
= newEOF
- headOff
;
1525 if (flags
& IO_TAILZEROFILL
) {
1527 zero_off1
= uio
->uio_offset
+ uio
->uio_resid
;
1529 if (zero_off1
< tailOff
)
1530 zero_cnt1
= tailOff
- zero_off1
;
1533 if (zero_cnt
== 0 && uio
== (struct uio
*) 0)
1535 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
1536 retval
, 0, 0, 0, 0);
1540 while ((total_size
= (uio_resid
+ zero_cnt
+ zero_cnt1
)) && retval
== 0) {
1542 * for this iteration of the loop, figure out where our starting point is
1545 start_offset
= (int)(zero_off
& PAGE_MASK_64
);
1546 upl_f_offset
= zero_off
- start_offset
;
1547 } else if (uio_resid
) {
1548 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
1549 upl_f_offset
= uio
->uio_offset
- start_offset
;
1551 start_offset
= (int)(zero_off1
& PAGE_MASK_64
);
1552 upl_f_offset
= zero_off1
- start_offset
;
1554 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 46)) | DBG_FUNC_NONE
,
1555 (int)zero_off
, (int)zero_cnt
, (int)zero_off1
, (int)zero_cnt1
, 0);
1557 if (total_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
1558 total_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
1561 * compute the size of the upl needed to encompass
1562 * the requested write... limit each call to cluster_io
1563 * to the maximum UPL size... cluster_io will clip if
1564 * this exceeds the maximum io_size for the device,
1565 * make sure to account for
1566 * a starting offset that's not page aligned
1568 upl_size
= (start_offset
+ total_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1570 if (upl_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
1571 upl_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
1573 pages_in_upl
= upl_size
/ PAGE_SIZE
;
1574 io_size
= upl_size
- start_offset
;
1576 if ((long long)io_size
> total_size
)
1577 io_size
= total_size
;
1579 start_blkno
= (daddr_t
)(upl_f_offset
/ PAGE_SIZE_64
);
1580 last_blkno
= start_blkno
+ pages_in_upl
;
1582 kret
= ubc_create_upl(vp
,
1588 if (kret
!= KERN_SUCCESS
)
1589 panic("cluster_write: failed to get pagelist");
1591 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_NONE
,
1592 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
1594 if (start_offset
&& !upl_valid_page(pl
, 0)) {
1598 * we're starting in the middle of the first page of the upl
1599 * and the page isn't currently valid, so we're going to have
1600 * to read it in first... this is a synchronous operation
1602 read_size
= PAGE_SIZE
;
1604 if ((upl_f_offset
+ read_size
) > newEOF
) {
1605 read_size
= newEOF
- upl_f_offset
;
1606 read_size
= (read_size
+ (devblocksize
- 1)) & ~(devblocksize
- 1);
1608 retval
= cluster_io(vp
, upl
, 0, upl_f_offset
, read_size
,
1609 CL_READ
, (struct buf
*)0);
1612 * we had an error during the read which causes us to abort
1613 * the current cluster_write request... before we do, we need
1614 * to release the rest of the pages in the upl without modifying
1615 * there state and mark the failed page in error
1617 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
);
1618 ubc_upl_abort(upl
, 0);
1620 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
1621 upl
, 0, 0, retval
, 0);
1625 if ((start_offset
== 0 || upl_size
> PAGE_SIZE
) && ((start_offset
+ io_size
) & PAGE_MASK
)) {
1627 * the last offset we're writing to in this upl does not end on a page
1628 * boundary... if it's not beyond the old EOF, then we'll also need to
1629 * pre-read this page in if it isn't already valid
1631 upl_offset
= upl_size
- PAGE_SIZE
;
1633 if ((upl_f_offset
+ start_offset
+ io_size
) < oldEOF
&&
1634 !upl_valid_page(pl
, upl_offset
/ PAGE_SIZE
)) {
1637 read_size
= PAGE_SIZE
;
1639 if ((upl_f_offset
+ upl_offset
+ read_size
) > newEOF
) {
1640 read_size
= newEOF
- (upl_f_offset
+ upl_offset
);
1641 read_size
= (read_size
+ (devblocksize
- 1)) & ~(devblocksize
- 1);
1643 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, read_size
,
1644 CL_READ
, (struct buf
*)0);
1647 * we had an error during the read which causes us to abort
1648 * the current cluster_write request... before we do, we
1649 * need to release the rest of the pages in the upl without
1650 * modifying there state and mark the failed page in error
1652 ubc_upl_abort_range(upl
, upl_offset
, PAGE_SIZE
,
1653 UPL_ABORT_DUMP_PAGES
);
1654 ubc_upl_abort(upl
, 0);
1656 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
1657 upl
, 0, 0, retval
, 0);
1662 if ((kret
= ubc_upl_map(upl
, &io_address
)) != KERN_SUCCESS
)
1663 panic("cluster_write: ubc_upl_map failed\n");
1664 xfer_resid
= io_size
;
1665 io_offset
= start_offset
;
1667 while (zero_cnt
&& xfer_resid
) {
1669 if (zero_cnt
< (long long)xfer_resid
)
1670 bytes_to_zero
= zero_cnt
;
1672 bytes_to_zero
= xfer_resid
;
1674 if ( !(flags
& IO_NOZEROVALID
)) {
1675 bzero((caddr_t
)(io_address
+ io_offset
), bytes_to_zero
);
1677 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 43)) | DBG_FUNC_NONE
,
1678 (int)upl_f_offset
+ io_offset
, bytes_to_zero
,
1679 (int)zero_cnt
, xfer_resid
, 0);
1681 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off
& PAGE_MASK_64
));
1683 if ( !upl_valid_page(pl
, (int)(zero_off
/ PAGE_SIZE_64
))) {
1684 bzero((caddr_t
)(io_address
+ io_offset
), bytes_to_zero
);
1686 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 43)) | DBG_FUNC_NONE
,
1687 (int)upl_f_offset
+ io_offset
, bytes_to_zero
,
1688 (int)zero_cnt
, xfer_resid
, 0);
1691 xfer_resid
-= bytes_to_zero
;
1692 zero_cnt
-= bytes_to_zero
;
1693 zero_off
+= bytes_to_zero
;
1694 io_offset
+= bytes_to_zero
;
1696 if (xfer_resid
&& uio_resid
) {
1697 bytes_to_move
= min(uio_resid
, xfer_resid
);
1699 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 42)) | DBG_FUNC_NONE
,
1700 (int)uio
->uio_offset
, bytes_to_move
, uio_resid
, xfer_resid
, 0);
1702 retval
= uiomove((caddr_t
)(io_address
+ io_offset
), bytes_to_move
, uio
);
1705 if ((kret
= ubc_upl_unmap(upl
)) != KERN_SUCCESS
)
1706 panic("cluster_write: kernel_upl_unmap failed\n");
1707 ubc_upl_abort(upl
, UPL_ABORT_DUMP_PAGES
);
1709 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
1710 upl
, 0, 0, retval
, 0);
1712 uio_resid
-= bytes_to_move
;
1713 xfer_resid
-= bytes_to_move
;
1714 io_offset
+= bytes_to_move
;
1717 while (xfer_resid
&& zero_cnt1
&& retval
== 0) {
1719 if (zero_cnt1
< (long long)xfer_resid
)
1720 bytes_to_zero
= zero_cnt1
;
1722 bytes_to_zero
= xfer_resid
;
1724 if ( !(flags
& IO_NOZEROVALID
)) {
1725 bzero((caddr_t
)(io_address
+ io_offset
), bytes_to_zero
);
1727 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 43)) | DBG_FUNC_NONE
,
1728 (int)upl_f_offset
+ io_offset
,
1729 bytes_to_zero
, (int)zero_cnt1
, xfer_resid
, 0);
1731 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off1
& PAGE_MASK_64
));
1732 if ( !upl_valid_page(pl
, (int)(zero_off1
/ PAGE_SIZE_64
))) {
1733 bzero((caddr_t
)(io_address
+ io_offset
), bytes_to_zero
);
1735 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 43)) | DBG_FUNC_NONE
,
1736 (int)upl_f_offset
+ io_offset
,
1737 bytes_to_zero
, (int)zero_cnt1
, xfer_resid
, 0);
1740 xfer_resid
-= bytes_to_zero
;
1741 zero_cnt1
-= bytes_to_zero
;
1742 zero_off1
+= bytes_to_zero
;
1743 io_offset
+= bytes_to_zero
;
1750 io_size
+= start_offset
;
1752 if ((upl_f_offset
+ io_size
) == newEOF
&& io_size
< upl_size
) {
1754 * if we're extending the file with this write
1755 * we'll zero fill the rest of the page so that
1756 * if the file gets extended again in such a way as to leave a
1757 * hole starting at this EOF, we'll have zero's in the correct spot
1759 bzero((caddr_t
)(io_address
+ io_size
), upl_size
- io_size
);
1761 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 43)) | DBG_FUNC_NONE
,
1762 (int)upl_f_offset
+ io_size
,
1763 upl_size
- io_size
, 0, 0, 0);
1765 if ((kret
= ubc_upl_unmap(upl
)) != KERN_SUCCESS
)
1766 panic("cluster_write: kernel_upl_unmap failed\n");
1768 io_size_before_rounding
= io_size
;
1770 if (io_size
& (devblocksize
- 1))
1771 io_size
= (io_size
+ (devblocksize
- 1)) & ~(devblocksize
- 1);
1780 * we have an existing cluster... see if this write will extend it nicely
1782 if (start_blkno
>= vp
->v_cstart
) {
1783 if (last_blkno
<= (vp
->v_cstart
+ vp
->v_clen
)) {
1785 * we have a write that fits entirely
1786 * within the existing cluster limits
1788 if (last_blkno
>= vp
->v_lastw
) {
1790 * if we're extending the dirty region within the cluster
1791 * we need to update the cluster info... we check for blkno
1792 * equality because we may be extending the file with a
1793 * partial write.... this in turn changes our idea of how
1794 * much data to write out (v_ciosiz) for the last page
1796 vp
->v_lastw
= last_blkno
;
1797 newsize
= io_size
+ ((start_blkno
- vp
->v_cstart
) * PAGE_SIZE
);
1799 if (newsize
> vp
->v_ciosiz
)
1800 vp
->v_ciosiz
= newsize
;
1805 if (start_blkno
< (vp
->v_cstart
+ vp
->v_clen
)) {
1807 * we have a write that starts in the middle of the current cluster
1808 * but extends beyond the cluster's limit
1809 * we'll clip the current cluster if we actually
1810 * overlap with the new write and then push it out
1811 * and start a new cluster with the current write
1813 if (vp
->v_lastw
> start_blkno
) {
1814 vp
->v_lastw
= start_blkno
;
1815 vp
->v_ciosiz
= (vp
->v_lastw
- vp
->v_cstart
) * PAGE_SIZE
;
1819 * we also get here for the case where the current write starts
1820 * beyond the limit of the existing cluster
1826 * the current write starts in front of the current cluster
1828 if (last_blkno
> vp
->v_cstart
) {
1830 * the current write extends into the existing cluster
1832 if ((vp
->v_lastw
- start_blkno
) > vp
->v_clen
) {
1834 * if we were to combine this write with the current cluster
1835 * we would exceed the cluster size limit....
1836 * clip the current cluster by moving the start position
1837 * to where the current write ends, and then push it
1839 vp
->v_ciosiz
-= (last_blkno
- vp
->v_cstart
) * PAGE_SIZE
;
1840 vp
->v_cstart
= last_blkno
;
1843 * round up the io_size to the nearest page size
1844 * since we've coalesced with at least 1 pre-existing
1845 * page in the current cluster... this write may have ended in the
1846 * middle of the page which would cause io_size to give us an
1847 * inaccurate view of how much I/O we actually need to do
1849 io_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1855 * we can coalesce the current write with the existing cluster
1856 * adjust the cluster info to reflect this
1858 if (last_blkno
> vp
->v_lastw
) {
1860 * the current write completey overlaps
1861 * the existing cluster
1863 vp
->v_lastw
= last_blkno
;
1864 vp
->v_ciosiz
= io_size
;
1866 vp
->v_ciosiz
+= (vp
->v_cstart
- start_blkno
) * PAGE_SIZE
;
1868 if (io_size
> vp
->v_ciosiz
)
1869 vp
->v_ciosiz
= io_size
;
1871 vp
->v_cstart
= start_blkno
;
1876 * this I/O range is entirely in front of the current cluster
1877 * so we need to push the current cluster out before beginning
1886 if (io_size_before_rounding
< (MAX_UPL_TRANSFER
* PAGE_SIZE
) && !(flags
& IO_SYNC
)) {
1887 vp
->v_clen
= MAX_UPL_TRANSFER
;
1888 vp
->v_cstart
= start_blkno
;
1889 vp
->v_lastw
= last_blkno
;
1890 vp
->v_ciosiz
= io_size
;
1896 ubc_upl_commit_range(upl
, 0, upl_size
,
1897 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
);
1900 if (flags
& IO_SYNC
)
1901 io_flags
= CL_COMMIT
| CL_AGE
;
1903 io_flags
= CL_COMMIT
| CL_AGE
| CL_ASYNC
;
1905 if (vp
->v_flag
& VNOCACHE_DATA
)
1906 io_flags
|= CL_DUMP
;
1908 while (vp
->v_numoutput
>= ASYNC_THROTTLE
) {
1909 vp
->v_flag
|= VTHROTTLED
;
1910 tsleep((caddr_t
)&vp
->v_numoutput
, PRIBIO
+ 1, "cluster_write", 0);
1912 retval
= cluster_io(vp
, upl
, 0, upl_f_offset
, io_size
,
1913 io_flags
, (struct buf
*)0);
1916 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
1917 retval
, 0, 0, 0, 0);
1922 cluster_read(vp
, uio
, filesize
, devblocksize
, flags
)
1933 vm_offset_t upl_offset
;
1936 upl_page_info_t
*pl
;
1941 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_START
,
1942 (int)uio
->uio_offset
, uio
->uio_resid
, (int)filesize
, devblocksize
, 0);
1945 * We set a threshhold of 4 pages to decide if the nocopy
1946 * read loop is worth the trouble...
1949 if (!((vp
->v_flag
& VNOCACHE_DATA
) && (uio
->uio_segflg
== UIO_USERSPACE
)))
1951 retval
= cluster_read_x(vp
, uio
, filesize
, devblocksize
, flags
);
1952 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
1953 (int)uio
->uio_offset
, uio
->uio_resid
, vp
->v_lastr
, retval
, 0);
1957 while (uio
->uio_resid
&& uio
->uio_offset
< filesize
&& retval
== 0)
1959 /* we know we have a resid, so this is safe */
1961 while (iov
->iov_len
== 0) {
1968 * We check every vector target and if it is physically
1969 * contiguous space, we skip the sanity checks.
1972 upl_offset
= (vm_offset_t
)iov
->iov_base
& ~PAGE_MASK
;
1973 upl_size
= (upl_offset
+ PAGE_SIZE
+(PAGE_SIZE
-1)) & ~PAGE_MASK
;
1975 upl_flags
= UPL_QUERY_OBJECT_TYPE
;
1976 if((vm_map_get_upl(current_map(),
1977 (vm_offset_t
)iov
->iov_base
& ~PAGE_MASK
,
1978 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
, 0)) != KERN_SUCCESS
)
1981 * the user app must have passed in an invalid address
1986 if (upl_flags
& UPL_PHYS_CONTIG
)
1988 retval
= cluster_phys_read(vp
, uio
, filesize
);
1990 else if (uio
->uio_resid
< 4 * PAGE_SIZE
)
1993 * We set a threshhold of 4 pages to decide if the nocopy
1994 * read loop is worth the trouble...
1996 retval
= cluster_read_x(vp
, uio
, filesize
, devblocksize
, flags
);
1997 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
1998 (int)uio
->uio_offset
, uio
->uio_resid
, vp
->v_lastr
, retval
, 0);
2001 else if (uio
->uio_offset
& PAGE_MASK_64
)
2003 /* Bring the file offset read up to a pagesize boundary */
2004 clip_size
= (PAGE_SIZE
- (int)(uio
->uio_offset
& PAGE_MASK_64
));
2005 if (uio
->uio_resid
< clip_size
)
2006 clip_size
= uio
->uio_resid
;
2008 * Fake the resid going into the cluster_read_x call
2009 * and restore it on the way out.
2011 prev_resid
= uio
->uio_resid
;
2012 uio
->uio_resid
= clip_size
;
2013 retval
= cluster_read_x(vp
, uio
, filesize
, devblocksize
, flags
);
2014 uio
->uio_resid
= prev_resid
- (clip_size
- uio
->uio_resid
);
2016 else if ((int)iov
->iov_base
& PAGE_MASK_64
)
2018 clip_size
= iov
->iov_len
;
2019 prev_resid
= uio
->uio_resid
;
2020 uio
->uio_resid
= clip_size
;
2021 retval
= cluster_read_x(vp
, uio
, filesize
, devblocksize
, flags
);
2022 uio
->uio_resid
= prev_resid
- (clip_size
- uio
->uio_resid
);
2027 * If we come in here, we know the offset into
2028 * the file is on a pagesize boundary
2031 max_io_size
= filesize
- uio
->uio_offset
;
2032 clip_size
= uio
->uio_resid
;
2033 if (iov
->iov_len
< clip_size
)
2034 clip_size
= iov
->iov_len
;
2035 if (max_io_size
< clip_size
)
2036 clip_size
= (int)max_io_size
;
2038 if (clip_size
< PAGE_SIZE
)
2041 * Take care of the tail end of the read in this vector.
2043 prev_resid
= uio
->uio_resid
;
2044 uio
->uio_resid
= clip_size
;
2045 retval
= cluster_read_x(vp
, uio
, filesize
, devblocksize
, flags
);
2046 uio
->uio_resid
= prev_resid
- (clip_size
- uio
->uio_resid
);
2050 /* round clip_size down to a multiple of pagesize */
2051 clip_size
= clip_size
& ~(PAGE_MASK
);
2052 prev_resid
= uio
->uio_resid
;
2053 uio
->uio_resid
= clip_size
;
2054 retval
= cluster_nocopy_read(vp
, uio
, filesize
, devblocksize
, flags
);
2055 if ((retval
==0) && uio
->uio_resid
)
2056 retval
= cluster_read_x(vp
, uio
, filesize
, devblocksize
, flags
);
2057 uio
->uio_resid
= prev_resid
- (clip_size
- uio
->uio_resid
);
2062 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
2063 (int)uio
->uio_offset
, uio
->uio_resid
, vp
->v_lastr
, retval
, 0);
2069 cluster_read_x(vp
, uio
, filesize
, devblocksize
, flags
)
2076 upl_page_info_t
*pl
;
2078 vm_offset_t upl_offset
;
2088 vm_offset_t io_address
;
2096 b_lblkno
= (int)(uio
->uio_offset
/ PAGE_SIZE_64
);
2098 while (uio
->uio_resid
&& uio
->uio_offset
< filesize
&& retval
== 0) {
2100 * compute the size of the upl needed to encompass
2101 * the requested read... limit each call to cluster_io
2102 * to the maximum UPL size... cluster_io will clip if
2103 * this exceeds the maximum io_size for the device,
2104 * make sure to account for
2105 * a starting offset that's not page aligned
2107 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2108 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
2109 max_size
= filesize
- uio
->uio_offset
;
2111 if ((off_t
)((unsigned int)uio
->uio_resid
) < max_size
)
2112 io_size
= uio
->uio_resid
;
2116 if (uio
->uio_segflg
== UIO_USERSPACE
&& !(vp
->v_flag
& VNOCACHE_DATA
)) {
2117 segflg
= uio
->uio_segflg
;
2119 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
2121 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
2122 (int)uio
->uio_offset
, io_size
, uio
->uio_resid
, 0, 0);
2124 while (io_size
&& retval
== 0) {
2130 UPL_POP_SET
| UPL_POP_BUSY
,
2131 &paddr
, 0) != KERN_SUCCESS
)
2134 xsize
= PAGE_SIZE
- start_offset
;
2136 if (xsize
> io_size
)
2139 retval
= uiomove((caddr_t
)(paddr
+ start_offset
), xsize
, uio
);
2141 ubc_page_op(vp
, upl_f_offset
,
2142 UPL_POP_CLR
| UPL_POP_BUSY
, 0, 0);
2145 start_offset
= (int)
2146 (uio
->uio_offset
& PAGE_MASK_64
);
2147 upl_f_offset
= uio
->uio_offset
- start_offset
;
2149 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
2150 (int)uio
->uio_offset
, io_size
, uio
->uio_resid
, 0, 0);
2152 uio
->uio_segflg
= segflg
;
2159 * we're already finished with this read request
2160 * let's see if we should do a read-ahead
2163 ((uio
->uio_offset
- 1) / PAGE_SIZE_64
);
2165 if (!(vp
->v_flag
& VRAOFF
))
2167 * let's try to read ahead if we're in
2168 * a sequential access pattern
2170 cluster_rd_ahead(vp
, b_lblkno
, e_lblkno
, filesize
, devblocksize
);
2171 vp
->v_lastr
= e_lblkno
;
2175 max_size
= filesize
- uio
->uio_offset
;
2178 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2179 if (upl_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
2180 upl_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
2181 pages_in_upl
= upl_size
/ PAGE_SIZE
;
2183 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_START
,
2184 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
2186 kret
= ubc_create_upl(vp
,
2192 if (kret
!= KERN_SUCCESS
)
2193 panic("cluster_read: failed to get pagelist");
2195 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_END
,
2196 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
2199 * scan from the beginning of the upl looking for the first
2200 * non-valid page.... this will become the first page in
2201 * the request we're going to make to 'cluster_io'... if all
2202 * of the pages are valid, we won't call through to 'cluster_io'
2204 for (start_pg
= 0; start_pg
< pages_in_upl
; start_pg
++) {
2205 if (!upl_valid_page(pl
, start_pg
))
2210 * scan from the starting invalid page looking for a valid
2211 * page before the end of the upl is reached, if we
2212 * find one, then it will be the last page of the request to
2215 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
2216 if (upl_valid_page(pl
, last_pg
))
2220 if (start_pg
< last_pg
) {
2222 * we found a range of 'invalid' pages that must be filled
2223 * if the last page in this range is the last page of the file
2224 * we may have to clip the size of it to keep from reading past
2225 * the end of the last physical block associated with the file
2227 upl_offset
= start_pg
* PAGE_SIZE
;
2228 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
2230 if ((upl_f_offset
+ upl_offset
+ io_size
) > filesize
) {
2231 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
2232 io_size
= (io_size
+ (devblocksize
- 1)) & ~(devblocksize
- 1);
2235 * issue a synchronous read to cluster_io
2238 error
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
,
2239 io_size
, CL_READ
, (struct buf
*)0);
2243 * if the read completed successfully, or there was no I/O request
2244 * issued, than map the upl into kernel address space and
2245 * move the data into user land.... we'll first add on any 'valid'
2246 * pages that were present in the upl when we acquired it.
2249 u_int size_of_prefetch
;
2251 for (uio_last
= last_pg
; uio_last
< pages_in_upl
; uio_last
++) {
2252 if (!upl_valid_page(pl
, uio_last
))
2256 * compute size to transfer this round, if uio->uio_resid is
2257 * still non-zero after this uiomove, we'll loop around and
2258 * set up for another I/O.
2260 val_size
= (uio_last
* PAGE_SIZE
) - start_offset
;
2262 if (max_size
< val_size
)
2263 val_size
= max_size
;
2265 if (uio
->uio_resid
< val_size
)
2266 val_size
= uio
->uio_resid
;
2268 e_lblkno
= (int)((uio
->uio_offset
+ ((off_t
)val_size
- 1)) / PAGE_SIZE_64
);
2270 if (size_of_prefetch
= (uio
->uio_resid
- val_size
)) {
2272 * if there's still I/O left to do for this request, then issue a
2273 * pre-fetch I/O... the I/O wait time will overlap
2274 * with the copying of the data
2276 cluster_rd_prefetch(vp
, uio
->uio_offset
+ val_size
, size_of_prefetch
, filesize
, devblocksize
);
2278 if (!(vp
->v_flag
& VRAOFF
) && !(vp
->v_flag
& VNOCACHE_DATA
))
2280 * let's try to read ahead if we're in
2281 * a sequential access pattern
2283 cluster_rd_ahead(vp
, b_lblkno
, e_lblkno
, filesize
, devblocksize
);
2284 vp
->v_lastr
= e_lblkno
;
2287 if (uio
->uio_segflg
== UIO_USERSPACE
) {
2290 segflg
= uio
->uio_segflg
;
2292 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
2295 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
2296 (int)uio
->uio_offset
, val_size
, uio
->uio_resid
, 0, 0);
2298 offset
= start_offset
;
2300 while (val_size
&& retval
== 0) {
2305 i
= offset
/ PAGE_SIZE
;
2306 csize
= min(PAGE_SIZE
- start_offset
, val_size
);
2308 paddr
= (caddr_t
)upl_phys_page(pl
, i
) + start_offset
;
2310 retval
= uiomove(paddr
, csize
, uio
);
2314 start_offset
= offset
& PAGE_MASK
;
2316 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
2317 (int)uio
->uio_offset
, val_size
, uio
->uio_resid
, 0, 0);
2319 uio
->uio_segflg
= segflg
;
2323 if ((kret
= ubc_upl_map(upl
, &io_address
)) != KERN_SUCCESS
)
2324 panic("cluster_read: ubc_upl_map() failed\n");
2326 retval
= uiomove((caddr_t
)(io_address
+ start_offset
), val_size
, uio
);
2328 if ((kret
= ubc_upl_unmap(upl
)) != KERN_SUCCESS
)
2329 panic("cluster_read: ubc_upl_unmap() failed\n");
2332 if (start_pg
< last_pg
) {
2334 * compute the range of pages that we actually issued an I/O for
2335 * and either commit them as valid if the I/O succeeded
2336 * or abort them if the I/O failed
2338 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
2340 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
2341 upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
2343 if (error
|| (vp
->v_flag
& VNOCACHE_DATA
))
2344 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
2345 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
2347 ubc_upl_commit_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
2348 UPL_COMMIT_CLEAR_DIRTY
2349 | UPL_COMMIT_FREE_ON_EMPTY
2350 | UPL_COMMIT_INACTIVATE
);
2352 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
,
2353 upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
2355 if ((last_pg
- start_pg
) < pages_in_upl
) {
2360 * the set of pages that we issued an I/O for did not encompass
2361 * the entire upl... so just release these without modifying
2365 ubc_upl_abort(upl
, 0);
2367 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
2368 upl
, -1, pages_in_upl
- (last_pg
- start_pg
), 0, 0);
2372 * we found some already valid pages at the beginning of
2373 * the upl commit these back to the inactive list with
2376 for (cur_pg
= 0; cur_pg
< start_pg
; cur_pg
++) {
2377 commit_flags
= UPL_COMMIT_FREE_ON_EMPTY
2378 | UPL_COMMIT_INACTIVATE
;
2380 if (upl_dirty_page(pl
, cur_pg
))
2381 commit_flags
|= UPL_COMMIT_SET_DIRTY
;
2383 if ( !(commit_flags
& UPL_COMMIT_SET_DIRTY
) && (vp
->v_flag
& VNOCACHE_DATA
))
2384 ubc_upl_abort_range(upl
, cur_pg
* PAGE_SIZE
, PAGE_SIZE
,
2385 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
2387 ubc_upl_commit_range(upl
, cur_pg
* PAGE_SIZE
,
2388 PAGE_SIZE
, commit_flags
);
2391 if (last_pg
< uio_last
) {
2393 * we found some already valid pages immediately after the
2394 * pages we issued I/O for, commit these back to the
2395 * inactive list with reference cleared
2397 for (cur_pg
= last_pg
; cur_pg
< uio_last
; cur_pg
++) {
2398 commit_flags
= UPL_COMMIT_FREE_ON_EMPTY
2399 | UPL_COMMIT_INACTIVATE
;
2401 if (upl_dirty_page(pl
, cur_pg
))
2402 commit_flags
|= UPL_COMMIT_SET_DIRTY
;
2404 if ( !(commit_flags
& UPL_COMMIT_SET_DIRTY
) && (vp
->v_flag
& VNOCACHE_DATA
))
2405 ubc_upl_abort_range(upl
, cur_pg
* PAGE_SIZE
, PAGE_SIZE
,
2406 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
2408 ubc_upl_commit_range(upl
, cur_pg
* PAGE_SIZE
,
2409 PAGE_SIZE
, commit_flags
);
2412 if (uio_last
< pages_in_upl
) {
2414 * there were some invalid pages beyond the valid pages
2415 * that we didn't issue an I/O for, just release them
2418 ubc_upl_abort(upl
, 0);
2421 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
,
2433 cluster_nocopy_read(vp
, uio
, filesize
, devblocksize
, flags
)
2441 upl_page_info_t
*pl
;
2443 vm_offset_t upl_offset
;
2444 off_t start_upl_f_offset
;
2448 int upl_needed_size
;
2456 int force_data_sync
;
2460 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_START
,
2461 (int)uio
->uio_offset
, uio
->uio_resid
, (int)filesize
, devblocksize
, 0);
2464 * When we enter this routine, we know
2465 * -- the offset into the file is on a pagesize boundary
2466 * -- the resid is a page multiple
2467 * -- the resid will not exceed iov_len
2471 while (uio
->uio_resid
&& uio
->uio_offset
< filesize
&& retval
== 0) {
2473 max_io_size
= filesize
- uio
->uio_offset
;
2475 if (max_io_size
< (off_t
)((unsigned int)uio
->uio_resid
))
2476 io_size
= max_io_size
;
2478 io_size
= uio
->uio_resid
;
2481 * We don't come into this routine unless
2482 * UIO_USERSPACE is set.
2484 segflg
= uio
->uio_segflg
;
2486 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
2489 * First look for pages already in the cache
2490 * and move them to user space.
2492 while (io_size
&& (retval
== 0)) {
2493 upl_f_offset
= uio
->uio_offset
;
2496 * If this call fails, it means the page is not
2497 * in the page cache.
2499 if (ubc_page_op(vp
, upl_f_offset
,
2500 UPL_POP_SET
| UPL_POP_BUSY
, &paddr
, 0) != KERN_SUCCESS
)
2503 retval
= uiomove((caddr_t
)(paddr
), PAGE_SIZE
, uio
);
2505 ubc_page_op(vp
, upl_f_offset
,
2506 UPL_POP_CLR
| UPL_POP_BUSY
, 0, 0);
2508 io_size
-= PAGE_SIZE
;
2509 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 71)) | DBG_FUNC_NONE
,
2510 (int)uio
->uio_offset
, io_size
, uio
->uio_resid
, 0, 0);
2513 uio
->uio_segflg
= segflg
;
2517 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
2518 (int)uio
->uio_offset
, uio
->uio_resid
, 2, retval
, 0);
2522 /* If we are already finished with this read, then return */
2526 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
2527 (int)uio
->uio_offset
, uio
->uio_resid
, 3, io_size
, 0);
2531 max_io_size
= io_size
;
2532 if (max_io_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
2533 max_io_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
2535 start_upl_f_offset
= uio
->uio_offset
; /* this is page aligned in the file */
2536 upl_f_offset
= start_upl_f_offset
;
2539 while(io_size
< max_io_size
)
2542 if(ubc_page_op(vp
, upl_f_offset
,
2543 UPL_POP_SET
| UPL_POP_BUSY
, &paddr
, 0) == KERN_SUCCESS
)
2545 ubc_page_op(vp
, upl_f_offset
,
2546 UPL_POP_CLR
| UPL_POP_BUSY
, 0, 0);
2551 * Build up the io request parameters.
2554 io_size
+= PAGE_SIZE
;
2555 upl_f_offset
+= PAGE_SIZE
;
2561 upl_offset
= (vm_offset_t
)iov
->iov_base
& PAGE_MASK_64
;
2562 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
2564 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_START
,
2565 (int)upl_offset
, upl_needed_size
, iov
->iov_base
, io_size
, 0);
2567 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++)
2570 upl_size
= upl_needed_size
;
2571 upl_flags
= UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
;
2573 kret
= vm_map_get_upl(current_map(),
2574 (vm_offset_t
)iov
->iov_base
& ~PAGE_MASK
,
2575 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
, force_data_sync
);
2577 if (kret
!= KERN_SUCCESS
)
2579 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
2580 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
2582 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
2583 (int)uio
->uio_offset
, uio
->uio_resid
, 4, retval
, 0);
2585 /* cluster_nocopy_read: failed to get pagelist */
2586 /* do not return kret here */
2590 pages_in_pl
= upl_size
/ PAGE_SIZE
;
2591 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2593 for(i
=0; i
< pages_in_pl
; i
++)
2595 if (!upl_valid_page(pl
, i
))
2598 if (i
== pages_in_pl
)
2601 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
2602 UPL_ABORT_FREE_ON_EMPTY
);
2605 if (force_data_sync
>= 3)
2607 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
2608 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
2610 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
2611 (int)uio
->uio_offset
, uio
->uio_resid
, 5, retval
, 0);
2615 * Consider the possibility that upl_size wasn't satisfied.
2617 if (upl_size
!= upl_needed_size
)
2618 io_size
= (upl_size
- (int)upl_offset
) & ~PAGE_MASK
;
2622 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
2623 UPL_ABORT_FREE_ON_EMPTY
);
2627 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
2628 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
2631 * issue a synchronous read to cluster_io
2634 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_START
,
2635 upl
, (int)upl_offset
, (int)start_upl_f_offset
, io_size
, 0);
2637 error
= cluster_io(vp
, upl
, upl_offset
, start_upl_f_offset
,
2638 io_size
, CL_READ
| CL_NOZERO
, (struct buf
*)0);
2642 * The cluster_io read completed successfully,
2643 * update the uio structure and commit.
2646 ubc_upl_commit_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
2647 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
);
2649 iov
->iov_base
+= io_size
;
2650 iov
->iov_len
-= io_size
;
2651 uio
->uio_resid
-= io_size
;
2652 uio
->uio_offset
+= io_size
;
2655 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
2656 UPL_ABORT_FREE_ON_EMPTY
);
2659 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_END
,
2660 upl
, (int)uio
->uio_offset
, (int)uio
->uio_resid
, error
, 0);
2668 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
2669 (int)uio
->uio_offset
, (int)uio
->uio_resid
, 6, retval
, 0);
2676 cluster_phys_read(vp
, uio
, filesize
)
2682 vm_offset_t upl_offset
;
2686 int upl_needed_size
;
2694 * When we enter this routine, we know
2695 * -- the resid will not exceed iov_len
2696 * -- the target address is physically contiguous
2701 max_size
= filesize
- uio
->uio_offset
;
2703 if (max_size
< (off_t
)((unsigned int)iov
->iov_len
))
2706 io_size
= iov
->iov_len
;
2708 upl_offset
= (vm_offset_t
)iov
->iov_base
& PAGE_MASK_64
;
2709 upl_needed_size
= upl_offset
+ io_size
;
2712 upl_size
= upl_needed_size
;
2713 upl_flags
= UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
;
2715 kret
= vm_map_get_upl(current_map(),
2716 (vm_offset_t
)iov
->iov_base
& ~PAGE_MASK
,
2717 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
, 0);
2719 if (kret
!= KERN_SUCCESS
)
2721 /* cluster_phys_read: failed to get pagelist */
2726 * Consider the possibility that upl_size wasn't satisfied.
2728 if (upl_size
< upl_needed_size
)
2730 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2735 * issue a synchronous read to cluster_io
2738 error
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
2739 io_size
, CL_READ
| CL_NOZERO
| CL_DEV_MEMORY
, (struct buf
*)0);
2744 * The cluster_io read completed successfully,
2745 * update the uio structure and commit.
2748 ubc_upl_commit_range(upl
, 0, upl_size
, UPL_COMMIT_FREE_ON_EMPTY
);
2750 iov
->iov_base
+= io_size
;
2751 iov
->iov_len
-= io_size
;
2752 uio
->uio_resid
-= io_size
;
2753 uio
->uio_offset
+= io_size
;
2756 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2762 * generate advisory I/O's in the largest chunks possible
2763 * the completed pages will be released into the VM cache
2765 advisory_read(vp
, filesize
, f_offset
, resid
, devblocksize
)
2772 upl_page_info_t
*pl
;
2774 vm_offset_t upl_offset
;
2787 if (!UBCINFOEXISTS(vp
))
2790 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_START
,
2791 (int)f_offset
, resid
, (int)filesize
, devblocksize
, 0);
2793 while (resid
&& f_offset
< filesize
&& retval
== 0) {
2795 * compute the size of the upl needed to encompass
2796 * the requested read... limit each call to cluster_io
2797 * to the maximum UPL size... cluster_io will clip if
2798 * this exceeds the maximum io_size for the device,
2799 * make sure to account for
2800 * a starting offset that's not page aligned
2802 start_offset
= (int)(f_offset
& PAGE_MASK_64
);
2803 upl_f_offset
= f_offset
- (off_t
)start_offset
;
2804 max_size
= filesize
- f_offset
;
2806 if (resid
< max_size
)
2811 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2812 if (upl_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
2813 upl_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
2814 pages_in_upl
= upl_size
/ PAGE_SIZE
;
2816 kret
= ubc_create_upl(vp
,
2822 if (kret
!= KERN_SUCCESS
)
2823 panic("advisory_read: failed to get pagelist");
2826 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_NONE
,
2827 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
2830 * scan from the beginning of the upl looking for the first
2831 * non-valid page.... this will become the first page in
2832 * the request we're going to make to 'cluster_io'... if all
2833 * of the pages are valid, we won't call through to 'cluster_io'
2835 for (start_pg
= 0; start_pg
< pages_in_upl
; start_pg
++) {
2836 if (!upl_valid_page(pl
, start_pg
))
2841 * scan from the starting invalid page looking for a valid
2842 * page before the end of the upl is reached, if we
2843 * find one, then it will be the last page of the request to
2846 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
2847 if (upl_valid_page(pl
, last_pg
))
2851 if (start_pg
< last_pg
) {
2853 * we found a range of 'invalid' pages that must be filled
2854 * if the last page in this range is the last page of the file
2855 * we may have to clip the size of it to keep from reading past
2856 * the end of the last physical block associated with the file
2858 upl_offset
= start_pg
* PAGE_SIZE
;
2859 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
2861 if ((upl_f_offset
+ upl_offset
+ io_size
) > filesize
) {
2862 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
2863 io_size
= (io_size
+ (devblocksize
- 1)) & ~(devblocksize
- 1);
2866 * issue an asynchronous read to cluster_io
2868 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
2869 CL_ASYNC
| CL_READ
| CL_COMMIT
| CL_AGE
, (struct buf
*)0);
2873 * start_pg of non-zero indicates we found some already valid pages
2874 * at the beginning of the upl.... we need to release these without
2875 * modifying there state
2877 ubc_upl_abort_range(upl
, 0, start_pg
* PAGE_SIZE
,
2878 UPL_ABORT_FREE_ON_EMPTY
);
2880 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 62)) | DBG_FUNC_NONE
,
2881 upl
, 0, start_pg
* PAGE_SIZE
, 0, 0);
2883 if (last_pg
< pages_in_upl
) {
2885 * the set of pages that we issued an I/O for did not extend all the
2886 * way to the end of the upl..so just release them without modifying
2889 ubc_upl_abort_range(upl
, last_pg
* PAGE_SIZE
, (pages_in_upl
- last_pg
) * PAGE_SIZE
,
2890 UPL_ABORT_FREE_ON_EMPTY
);
2892 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 63)) | DBG_FUNC_NONE
,
2893 upl
, last_pg
* PAGE_SIZE
,
2894 (pages_in_upl
- last_pg
) * PAGE_SIZE
, 0, 0);
2896 io_size
= (last_pg
* PAGE_SIZE
) - start_offset
;
2898 if (io_size
> resid
)
2900 f_offset
+= io_size
;
2903 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_END
,
2904 (int)f_offset
, resid
, retval
, 0, 0);
2913 upl_page_info_t
*pl
;
2915 vm_offset_t upl_offset
;
2927 if (!UBCINFOEXISTS(vp
))
2930 if (vp
->v_clen
== 0 || (pages_in_upl
= vp
->v_lastw
- vp
->v_cstart
) == 0)
2932 upl_size
= pages_in_upl
* PAGE_SIZE
;
2933 upl_f_offset
= ((off_t
)vp
->v_cstart
) * PAGE_SIZE_64
;
2934 size
= vp
->v_ciosiz
;
2937 if (size
> upl_size
|| (upl_size
- size
) > PAGE_SIZE
)
2938 panic("cluster_push: v_ciosiz doesn't match size of cluster\n");
2940 kret
= ubc_create_upl(vp
,
2946 if (kret
!= KERN_SUCCESS
)
2947 panic("cluster_push: failed to get pagelist");
2953 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
2954 if (upl_valid_page(pl
, start_pg
) && upl_dirty_page(pl
, start_pg
))
2957 if (start_pg
> last_pg
) {
2958 io_size
= (start_pg
- last_pg
) * PAGE_SIZE
;
2960 ubc_upl_abort_range(upl
, last_pg
* PAGE_SIZE
, io_size
,
2961 UPL_ABORT_FREE_ON_EMPTY
);
2968 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
2969 if (!upl_valid_page(pl
, last_pg
) || !upl_dirty_page(pl
, last_pg
))
2972 upl_offset
= start_pg
* PAGE_SIZE
;
2974 io_size
= min(size
, (last_pg
- start_pg
) * PAGE_SIZE
);
2976 if (vp
->v_flag
& VNOCACHE_DATA
)
2977 io_flags
= CL_COMMIT
| CL_AGE
| CL_ASYNC
| CL_DUMP
;
2979 io_flags
= CL_COMMIT
| CL_AGE
| CL_ASYNC
;
2981 while (vp
->v_numoutput
>= ASYNC_THROTTLE
) {
2982 vp
->v_flag
|= VTHROTTLED
;
2983 tsleep((caddr_t
)&vp
->v_numoutput
, PRIBIO
+ 1, "cluster_push", 0);
2985 cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
, io_flags
, (struct buf
*)0);