2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)vfs_cluster.c 8.10 (Berkeley) 3/28/95
64 #include <sys/param.h>
65 #include <sys/proc_internal.h>
66 #include <sys/buf_internal.h>
67 #include <sys/mount_internal.h>
68 #include <sys/vnode_internal.h>
69 #include <sys/trace.h>
70 #include <sys/malloc.h>
72 #include <sys/kernel.h>
73 #include <sys/resourcevar.h>
74 #include <sys/uio_internal.h>
75 #include <libkern/libkern.h>
76 #include <machine/machine_routines.h>
78 #include <sys/ubc_internal.h>
79 #include <vm/vnode_pager.h>
81 #include <mach/mach_types.h>
82 #include <mach/memory_object_types.h>
83 #include <mach/vm_map.h>
85 #include <kern/task.h>
87 #include <vm/vm_kern.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_pageout.h>
91 #include <sys/kdebug.h>
92 #include <libkern/OSAtomic.h>
98 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
103 #define CL_WRITE 0x02
104 #define CL_ASYNC 0x04
105 #define CL_COMMIT 0x08
106 #define CL_PAGEOUT 0x10
108 #define CL_NOZERO 0x40
109 #define CL_PAGEIN 0x80
110 #define CL_DEV_MEMORY 0x100
111 #define CL_PRESERVE 0x200
112 #define CL_THROTTLE 0x400
113 #define CL_KEEPCACHED 0x800
114 #define CL_DIRECT_IO 0x1000
115 #define CL_PASSIVE 0x2000
116 #define CL_IOSTREAMING 0x4000
117 #define CL_CLOSE 0x8000
118 #define CL_ENCRYPTED 0x10000
120 #define MAX_VECTOR_UPL_ELEMENTS 8
121 #define MAX_VECTOR_UPL_SIZE (2 * MAX_UPL_SIZE) * PAGE_SIZE
123 extern upl_t
vector_upl_create(vm_offset_t
);
124 extern boolean_t
vector_upl_is_valid(upl_t
);
125 extern boolean_t
vector_upl_set_subupl(upl_t
,upl_t
, u_int32_t
);
126 extern void vector_upl_set_pagelist(upl_t
);
127 extern void vector_upl_set_iostate(upl_t
, upl_t
, vm_offset_t
, u_int32_t
);
131 u_int io_completed
; /* amount of io that has currently completed */
132 u_int io_issued
; /* amount of io that was successfully issued */
133 int io_error
; /* error code of first error encountered */
134 int io_wanted
; /* someone is sleeping waiting for a change in state */
137 static lck_grp_t
*cl_mtx_grp
;
138 static lck_attr_t
*cl_mtx_attr
;
139 static lck_grp_attr_t
*cl_mtx_grp_attr
;
140 static lck_mtx_t
*cl_transaction_mtxp
;
148 #define PUSH_DELAY 0x01
149 #define PUSH_ALL 0x02
150 #define PUSH_SYNC 0x04
153 static void cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
);
154 static void cluster_wait_IO(buf_t cbp_head
, int async
);
155 static void cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
);
157 static int cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
);
159 static int cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
160 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*)(buf_t
, void *), void *callback_arg
);
161 static int cluster_iodone(buf_t bp
, void *callback_arg
);
162 static int cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
);
163 static int cluster_hard_throttle_on(vnode_t vp
, uint32_t);
165 static void cluster_iostate_wait(struct clios
*iostate
, u_int target
, const char *wait_name
);
167 static void cluster_syncup(vnode_t vp
, off_t newEOF
, int (*)(buf_t
, void *), void *callback_arg
);
169 static void cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int take_reference
);
170 static int cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
);
172 static int cluster_read_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t filesize
, int flags
,
173 int (*)(buf_t
, void *), void *callback_arg
);
174 static int cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
175 int flags
, int (*)(buf_t
, void *), void *callback_arg
);
176 static int cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
177 int (*)(buf_t
, void *), void *callback_arg
, int flags
);
179 static int cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
,
180 off_t headOff
, off_t tailOff
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
181 static int cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
,
182 int *write_type
, u_int32_t
*write_length
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
183 static int cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
,
184 int *write_type
, u_int32_t
*write_length
, int (*)(buf_t
, void *), void *callback_arg
, int bflag
);
186 static int cluster_align_phys_io(vnode_t vp
, struct uio
*uio
, addr64_t usr_paddr
, u_int32_t xsize
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
188 static int cluster_read_prefetch(vnode_t vp
, off_t f_offset
, u_int size
, off_t filesize
, int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
);
189 static void cluster_read_ahead(vnode_t vp
, struct cl_extent
*extent
, off_t filesize
, struct cl_readahead
*ra
, int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
);
191 static int cluster_push_now(vnode_t vp
, struct cl_extent
*, off_t EOF
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
193 static int cluster_try_push(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int push_flag
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
195 static void sparse_cluster_switch(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int (*)(buf_t
, void *), void *callback_arg
);
196 static void sparse_cluster_push(void **cmapp
, vnode_t vp
, off_t EOF
, int push_flag
, int io_flags
, int (*)(buf_t
, void *), void *callback_arg
);
197 static void sparse_cluster_add(void **cmapp
, vnode_t vp
, struct cl_extent
*, off_t EOF
, int (*)(buf_t
, void *), void *callback_arg
);
199 static kern_return_t
vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
);
200 static kern_return_t
vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
);
201 static kern_return_t
vfs_drt_control(void **cmapp
, int op_type
);
205 * limit the internal I/O size so that we
206 * can represent it in a 32 bit int
208 #define MAX_IO_REQUEST_SIZE (1024 * 1024 * 512)
209 #define MAX_IO_CONTIG_SIZE (MAX_UPL_SIZE * PAGE_SIZE)
211 #define MIN_DIRECT_WRITE_SIZE (4 * PAGE_SIZE)
213 #define WRITE_THROTTLE 6
214 #define WRITE_THROTTLE_SSD 2
215 #define WRITE_BEHIND 1
216 #define WRITE_BEHIND_SSD 1
218 #define PREFETCH_SSD 2
220 #define IO_SCALE(vp, base) (vp->v_mount->mnt_ioscale * base)
221 #define MAX_CLUSTER_SIZE(vp) (cluster_max_io_size(vp->v_mount, CL_WRITE))
222 #define MAX_PREFETCH(vp, size, is_ssd) (size * IO_SCALE(vp, (is_ssd && !ignore_is_ssd) ? PREFETCH_SSD : PREFETCH))
224 int ignore_is_ssd
= 0;
225 int speculative_reads_disabled
= 0;
226 uint32_t speculative_prefetch_max
= (MAX_UPL_SIZE
* 3);
229 * throttle the number of async writes that
230 * can be outstanding on a single vnode
231 * before we issue a synchronous write
233 #define HARD_THROTTLE_MAXCNT 0
234 #define HARD_THROTTLE_MAXSIZE (32 * 1024)
236 int hard_throttle_on_root
= 0;
237 struct timeval priority_IO_timestamp_for_root
;
243 * allocate lock group attribute and group
245 cl_mtx_grp_attr
= lck_grp_attr_alloc_init();
246 cl_mtx_grp
= lck_grp_alloc_init("cluster I/O", cl_mtx_grp_attr
);
249 * allocate the lock attribute
251 cl_mtx_attr
= lck_attr_alloc_init();
253 cl_transaction_mtxp
= lck_mtx_alloc_init(cl_mtx_grp
, cl_mtx_attr
);
255 if (cl_transaction_mtxp
== NULL
)
256 panic("cluster_init: failed to allocate cl_transaction_mtxp");
261 cluster_max_io_size(mount_t mp
, int type
)
263 uint32_t max_io_size
;
270 segcnt
= mp
->mnt_segreadcnt
;
271 maxcnt
= mp
->mnt_maxreadcnt
;
274 segcnt
= mp
->mnt_segwritecnt
;
275 maxcnt
= mp
->mnt_maxwritecnt
;
278 segcnt
= min(mp
->mnt_segreadcnt
, mp
->mnt_segwritecnt
);
279 maxcnt
= min(mp
->mnt_maxreadcnt
, mp
->mnt_maxwritecnt
);
282 if (segcnt
> MAX_UPL_SIZE
) {
284 * don't allow a size beyond the max UPL size we can create
286 segcnt
= MAX_UPL_SIZE
;
288 max_io_size
= min((segcnt
* PAGE_SIZE
), maxcnt
);
290 if (max_io_size
< (MAX_UPL_TRANSFER
* PAGE_SIZE
)) {
292 * don't allow a size smaller than the old fixed limit
294 max_io_size
= (MAX_UPL_TRANSFER
* PAGE_SIZE
);
297 * make sure the size specified is a multiple of PAGE_SIZE
299 max_io_size
&= ~PAGE_MASK
;
301 return (max_io_size
);
307 #define CLW_ALLOCATE 0x01
308 #define CLW_RETURNLOCKED 0x02
309 #define CLW_IONOCACHE 0x04
310 #define CLW_IOPASSIVE 0x08
313 * if the read ahead context doesn't yet exist,
314 * allocate and initialize it...
315 * the vnode lock serializes multiple callers
316 * during the actual assignment... first one
317 * to grab the lock wins... the other callers
318 * will release the now unnecessary storage
320 * once the context is present, try to grab (but don't block on)
321 * the lock associated with it... if someone
322 * else currently owns it, than the read
323 * will run without read-ahead. this allows
324 * multiple readers to run in parallel and
325 * since there's only 1 read ahead context,
326 * there's no real loss in only allowing 1
327 * reader to have read-ahead enabled.
329 static struct cl_readahead
*
330 cluster_get_rap(vnode_t vp
)
332 struct ubc_info
*ubc
;
333 struct cl_readahead
*rap
;
337 if ((rap
= ubc
->cl_rahead
) == NULL
) {
338 MALLOC_ZONE(rap
, struct cl_readahead
*, sizeof *rap
, M_CLRDAHEAD
, M_WAITOK
);
340 bzero(rap
, sizeof *rap
);
342 lck_mtx_init(&rap
->cl_lockr
, cl_mtx_grp
, cl_mtx_attr
);
346 if (ubc
->cl_rahead
== NULL
)
347 ubc
->cl_rahead
= rap
;
349 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
350 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
351 rap
= ubc
->cl_rahead
;
355 if (lck_mtx_try_lock(&rap
->cl_lockr
) == TRUE
)
358 return ((struct cl_readahead
*)NULL
);
363 * if the write behind context doesn't yet exist,
364 * and CLW_ALLOCATE is specified, allocate and initialize it...
365 * the vnode lock serializes multiple callers
366 * during the actual assignment... first one
367 * to grab the lock wins... the other callers
368 * will release the now unnecessary storage
370 * if CLW_RETURNLOCKED is set, grab (blocking if necessary)
371 * the lock associated with the write behind context before
375 static struct cl_writebehind
*
376 cluster_get_wbp(vnode_t vp
, int flags
)
378 struct ubc_info
*ubc
;
379 struct cl_writebehind
*wbp
;
383 if ((wbp
= ubc
->cl_wbehind
) == NULL
) {
385 if ( !(flags
& CLW_ALLOCATE
))
386 return ((struct cl_writebehind
*)NULL
);
388 MALLOC_ZONE(wbp
, struct cl_writebehind
*, sizeof *wbp
, M_CLWRBEHIND
, M_WAITOK
);
390 bzero(wbp
, sizeof *wbp
);
391 lck_mtx_init(&wbp
->cl_lockw
, cl_mtx_grp
, cl_mtx_attr
);
395 if (ubc
->cl_wbehind
== NULL
)
396 ubc
->cl_wbehind
= wbp
;
398 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
399 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
400 wbp
= ubc
->cl_wbehind
;
404 if (flags
& CLW_RETURNLOCKED
)
405 lck_mtx_lock(&wbp
->cl_lockw
);
412 cluster_syncup(vnode_t vp
, off_t newEOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
414 struct cl_writebehind
*wbp
;
416 if ((wbp
= cluster_get_wbp(vp
, 0)) != NULL
) {
418 if (wbp
->cl_number
) {
419 lck_mtx_lock(&wbp
->cl_lockw
);
421 cluster_try_push(wbp
, vp
, newEOF
, PUSH_ALL
| PUSH_SYNC
, 0, callback
, callback_arg
);
423 lck_mtx_unlock(&wbp
->cl_lockw
);
430 cluster_hard_throttle_on(vnode_t vp
, uint32_t hard_throttle
)
435 static struct timeval hard_throttle_maxelapsed
= { 0, 200000 };
437 if (vp
->v_mount
->mnt_kern_flag
& MNTK_ROOTDEV
) {
438 struct timeval elapsed
;
440 if (hard_throttle_on_root
)
443 microuptime(&elapsed
);
444 timevalsub(&elapsed
, &priority_IO_timestamp_for_root
);
446 if (timevalcmp(&elapsed
, &hard_throttle_maxelapsed
, <))
450 if (throttle_get_io_policy(&ut
) == IOPOL_THROTTLE
) {
451 if (throttle_io_will_be_throttled(-1, vp
->v_mount
)) {
460 cluster_iostate_wait(struct clios
*iostate
, u_int target
, const char *wait_name
)
463 lck_mtx_lock(&iostate
->io_mtxp
);
465 while ((iostate
->io_issued
- iostate
->io_completed
) > target
) {
467 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
468 iostate
->io_issued
, iostate
->io_completed
, target
, 0, 0);
470 iostate
->io_wanted
= 1;
471 msleep((caddr_t
)&iostate
->io_wanted
, &iostate
->io_mtxp
, PRIBIO
+ 1, wait_name
, NULL
);
473 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
474 iostate
->io_issued
, iostate
->io_completed
, target
, 0, 0);
476 lck_mtx_unlock(&iostate
->io_mtxp
);
481 cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
)
483 int upl_abort_code
= 0;
487 if ((io_flags
& (B_PHYS
| B_CACHE
)) == (B_PHYS
| B_CACHE
))
489 * direct write of any flavor, or a direct read that wasn't aligned
491 ubc_upl_commit_range(upl
, upl_offset
, abort_size
, UPL_COMMIT_FREE_ON_EMPTY
);
493 if (io_flags
& B_PAGEIO
) {
494 if (io_flags
& B_READ
)
499 if (io_flags
& B_CACHE
)
501 * leave pages in the cache unchanged on error
503 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
504 else if (page_out
&& (error
!= ENXIO
))
506 * transient error... leave pages unchanged
508 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
510 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
512 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
514 ubc_upl_abort_range(upl
, upl_offset
, abort_size
, upl_abort_code
);
516 return (upl_abort_code
);
521 cluster_iodone(buf_t bp
, void *callback_arg
)
532 int transaction_size
= 0;
538 struct clios
*iostate
;
539 boolean_t transaction_complete
= FALSE
;
541 cbp_head
= (buf_t
)(bp
->b_trans_head
);
543 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_START
,
544 cbp_head
, bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
546 if (cbp_head
->b_trans_next
|| !(cbp_head
->b_flags
& B_EOT
)) {
547 boolean_t need_wakeup
= FALSE
;
549 lck_mtx_lock_spin(cl_transaction_mtxp
);
551 bp
->b_flags
|= B_TDONE
;
553 if (bp
->b_flags
& B_TWANTED
) {
554 CLR(bp
->b_flags
, B_TWANTED
);
557 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
559 * all I/O requests that are part of this transaction
560 * have to complete before we can process it
562 if ( !(cbp
->b_flags
& B_TDONE
)) {
564 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
565 cbp_head
, cbp
, cbp
->b_bcount
, cbp
->b_flags
, 0);
567 lck_mtx_unlock(cl_transaction_mtxp
);
569 if (need_wakeup
== TRUE
)
574 if (cbp
->b_flags
& B_EOT
)
575 transaction_complete
= TRUE
;
577 lck_mtx_unlock(cl_transaction_mtxp
);
579 if (need_wakeup
== TRUE
)
582 if (transaction_complete
== FALSE
) {
583 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
584 cbp_head
, 0, 0, 0, 0);
593 upl_offset
= cbp
->b_uploffset
;
595 b_flags
= cbp
->b_flags
;
596 real_bp
= cbp
->b_real_bp
;
597 zero_offset
= cbp
->b_validend
;
598 iostate
= (struct clios
*)cbp
->b_iostate
;
601 real_bp
->b_dev
= cbp
->b_dev
;
604 if ((cbp
->b_flags
& B_ERROR
) && error
== 0)
605 error
= cbp
->b_error
;
607 total_resid
+= cbp
->b_resid
;
608 total_size
+= cbp
->b_bcount
;
610 cbp_next
= cbp
->b_trans_next
;
612 if (cbp_next
== NULL
)
614 * compute the overall size of the transaction
615 * in case we created one that has 'holes' in it
616 * 'total_size' represents the amount of I/O we
617 * did, not the span of the transaction w/r to the UPL
619 transaction_size
= cbp
->b_uploffset
+ cbp
->b_bcount
- upl_offset
;
626 if (error
== 0 && total_resid
)
630 int (*cliodone_func
)(buf_t
, void *) = (int (*)(buf_t
, void *))(cbp_head
->b_cliodone
);
632 if (cliodone_func
!= NULL
) {
633 cbp_head
->b_bcount
= transaction_size
;
635 error
= (*cliodone_func
)(cbp_head
, callback_arg
);
639 cluster_zero(upl
, zero_offset
, PAGE_SIZE
- (zero_offset
& PAGE_MASK
), real_bp
);
641 free_io_buf(cbp_head
);
647 * someone has issued multiple I/Os asynchrounsly
648 * and is waiting for them to complete (streaming)
650 lck_mtx_lock_spin(&iostate
->io_mtxp
);
652 if (error
&& iostate
->io_error
== 0)
653 iostate
->io_error
= error
;
655 iostate
->io_completed
+= total_size
;
657 if (iostate
->io_wanted
) {
659 * someone is waiting for the state of
660 * this io stream to change
662 iostate
->io_wanted
= 0;
665 lck_mtx_unlock(&iostate
->io_mtxp
);
668 wakeup((caddr_t
)&iostate
->io_wanted
);
671 if (b_flags
& B_COMMIT_UPL
) {
673 pg_offset
= upl_offset
& PAGE_MASK
;
674 commit_size
= (pg_offset
+ transaction_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
677 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, commit_size
, error
, b_flags
);
679 upl_flags
= UPL_COMMIT_FREE_ON_EMPTY
;
681 if ((b_flags
& B_PHYS
) && (b_flags
& B_READ
))
682 upl_flags
|= UPL_COMMIT_SET_DIRTY
;
685 upl_flags
|= UPL_COMMIT_INACTIVATE
;
687 ubc_upl_commit_range(upl
, upl_offset
- pg_offset
, commit_size
, upl_flags
);
692 real_bp
->b_flags
|= B_ERROR
;
693 real_bp
->b_error
= error
;
695 real_bp
->b_resid
= total_resid
;
697 buf_biodone(real_bp
);
699 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
700 upl
, upl_offset
- pg_offset
, commit_size
, (error
<< 24) | upl_flags
, 0);
707 cluster_hard_throttle_limit(vnode_t vp
, uint32_t *limit
, uint32_t hard_throttle
)
709 if (cluster_hard_throttle_on(vp
, hard_throttle
)) {
710 *limit
= HARD_THROTTLE_MAXSIZE
;
718 cluster_zero(upl_t upl
, upl_offset_t upl_offset
, int size
, buf_t bp
)
721 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_START
,
722 upl_offset
, size
, bp
, 0, 0);
724 if (bp
== NULL
|| bp
->b_datap
== 0) {
728 pl
= ubc_upl_pageinfo(upl
);
730 if (upl_device_page(pl
) == TRUE
) {
731 zero_addr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + upl_offset
;
733 bzero_phys_nc(zero_addr
, size
);
740 page_index
= upl_offset
/ PAGE_SIZE
;
741 page_offset
= upl_offset
& PAGE_MASK
;
743 zero_addr
= ((addr64_t
)upl_phys_page(pl
, page_index
) << 12) + page_offset
;
744 zero_cnt
= min(PAGE_SIZE
- page_offset
, size
);
746 bzero_phys(zero_addr
, zero_cnt
);
749 upl_offset
+= zero_cnt
;
753 bzero((caddr_t
)((vm_offset_t
)bp
->b_datap
+ upl_offset
), size
);
755 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_END
,
756 upl_offset
, size
, 0, 0, 0);
761 cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
)
763 cbp_head
->b_validend
= zero_offset
;
764 cbp_tail
->b_flags
|= B_EOT
;
768 cluster_wait_IO(buf_t cbp_head
, int async
)
774 * async callback completion will not normally
775 * generate a wakeup upon I/O completion...
776 * by setting B_TWANTED, we will force a wakeup
777 * to occur as any outstanding I/Os complete...
778 * I/Os already completed will have B_TDONE already
779 * set and we won't cause us to block
780 * note that we're actually waiting for the bp to have
781 * completed the callback function... only then
782 * can we safely take back ownership of the bp
784 lck_mtx_lock_spin(cl_transaction_mtxp
);
786 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
787 cbp
->b_flags
|= B_TWANTED
;
789 lck_mtx_unlock(cl_transaction_mtxp
);
791 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
794 while (!ISSET(cbp
->b_flags
, B_TDONE
)) {
796 lck_mtx_lock_spin(cl_transaction_mtxp
);
798 if (!ISSET(cbp
->b_flags
, B_TDONE
)) {
799 DTRACE_IO1(wait__start
, buf_t
, cbp
);
800 (void) msleep(cbp
, cl_transaction_mtxp
, PDROP
| (PRIBIO
+1), "cluster_wait_IO", NULL
);
801 DTRACE_IO1(wait__done
, buf_t
, cbp
);
803 lck_mtx_unlock(cl_transaction_mtxp
);
811 cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
)
817 * cluster_complete_transaction will
818 * only be called if we've issued a complete chain in synchronous mode
819 * or, we've already done a cluster_wait_IO on an incomplete chain
822 for (cbp
= *cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
826 * we've already waited on all of the I/Os in this transaction,
827 * so mark all of the buf_t's in this transaction as B_TDONE
828 * so that cluster_iodone sees the transaction as completed
830 for (cbp
= *cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
831 cbp
->b_flags
|= B_TDONE
;
833 error
= cluster_iodone(*cbp_head
, callback_arg
);
835 if ( !(flags
& CL_ASYNC
) && error
&& *retval
== 0) {
836 if (((flags
& (CL_PAGEOUT
| CL_KEEPCACHED
)) != CL_PAGEOUT
) || (error
!= ENXIO
))
839 *cbp_head
= (buf_t
)NULL
;
844 cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
845 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*callback
)(buf_t
, void *), void *callback_arg
)
854 buf_t cbp_head
= NULL
;
855 buf_t cbp_tail
= NULL
;
864 int async_throttle
= 0;
866 vm_offset_t upl_end_offset
;
867 boolean_t need_EOT
= FALSE
;
870 * we currently don't support buffers larger than a page
872 if (real_bp
&& non_rounded_size
> PAGE_SIZE
)
873 panic("%s(): Called with real buffer of size %d bytes which "
874 "is greater than the maximum allowed size of "
875 "%d bytes (the system PAGE_SIZE).\n",
876 __FUNCTION__
, non_rounded_size
, PAGE_SIZE
);
881 * we don't want to do any funny rounding of the size for IO requests
882 * coming through the DIRECT or CONTIGUOUS paths... those pages don't
883 * belong to us... we can't extend (nor do we need to) the I/O to fill
886 if (mp
->mnt_devblocksize
> 1 && !(flags
& (CL_DEV_MEMORY
| CL_DIRECT_IO
))) {
888 * round the requested size up so that this I/O ends on a
889 * page boundary in case this is a 'write'... if the filesystem
890 * has blocks allocated to back the page beyond the EOF, we want to
891 * make sure to write out the zero's that are sitting beyond the EOF
892 * so that in case the filesystem doesn't explicitly zero this area
893 * if a hole is created via a lseek/write beyond the current EOF,
894 * it will return zeros when it's read back from the disk. If the
895 * physical allocation doesn't extend for the whole page, we'll
896 * only write/read from the disk up to the end of this allocation
897 * via the extent info returned from the VNOP_BLOCKMAP call.
899 pg_offset
= upl_offset
& PAGE_MASK
;
901 size
= (((non_rounded_size
+ pg_offset
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - pg_offset
;
904 * anyone advertising a blocksize of 1 byte probably
905 * can't deal with us rounding up the request size
906 * AFP is one such filesystem/device
908 size
= non_rounded_size
;
910 upl_end_offset
= upl_offset
+ size
;
912 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_START
, (int)f_offset
, size
, upl_offset
, flags
, 0);
915 * Set the maximum transaction size to the maximum desired number of
919 if (flags
& CL_DEV_MEMORY
)
920 max_trans_count
= 16;
922 if (flags
& CL_READ
) {
924 bmap_flags
= VNODE_READ
;
926 max_iosize
= mp
->mnt_maxreadcnt
;
927 max_vectors
= mp
->mnt_segreadcnt
;
930 bmap_flags
= VNODE_WRITE
;
932 max_iosize
= mp
->mnt_maxwritecnt
;
933 max_vectors
= mp
->mnt_segwritecnt
;
935 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_NONE
, max_iosize
, max_vectors
, mp
->mnt_devblocksize
, 0, 0);
938 * make sure the maximum iosize is a
939 * multiple of the page size
941 max_iosize
&= ~PAGE_MASK
;
944 * Ensure the maximum iosize is sensible.
947 max_iosize
= PAGE_SIZE
;
949 if (flags
& CL_THROTTLE
) {
950 if ( !(flags
& CL_PAGEOUT
) && cluster_hard_throttle_on(vp
, 1)) {
951 if (max_iosize
> HARD_THROTTLE_MAXSIZE
)
952 max_iosize
= HARD_THROTTLE_MAXSIZE
;
953 async_throttle
= HARD_THROTTLE_MAXCNT
;
955 if ( (flags
& CL_DEV_MEMORY
) )
956 async_throttle
= IO_SCALE(vp
, VNODE_ASYNC_THROTTLE
);
959 u_int max_cluster_size
;
962 max_cluster_size
= MAX_CLUSTER_SIZE(vp
);
964 if (max_iosize
> max_cluster_size
)
965 max_cluster
= max_cluster_size
;
967 max_cluster
= max_iosize
;
969 if (size
< max_cluster
)
972 if ((vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
) && !ignore_is_ssd
)
973 scale
= WRITE_THROTTLE_SSD
;
975 scale
= WRITE_THROTTLE
;
977 if (flags
& CL_CLOSE
)
978 scale
+= MAX_CLUSTERS
;
980 async_throttle
= min(IO_SCALE(vp
, VNODE_ASYNC_THROTTLE
), ((scale
* max_cluster_size
) / max_cluster
) - 1);
986 if (flags
& (CL_PAGEIN
| CL_PAGEOUT
))
987 io_flags
|= B_PAGEIO
;
988 if (flags
& (CL_IOSTREAMING
))
989 io_flags
|= B_IOSTREAMING
;
990 if (flags
& CL_COMMIT
)
991 io_flags
|= B_COMMIT_UPL
;
992 if (flags
& CL_DIRECT_IO
)
994 if (flags
& (CL_PRESERVE
| CL_KEEPCACHED
))
996 if (flags
& CL_PASSIVE
)
997 io_flags
|= B_PASSIVE
;
998 if (flags
& CL_ENCRYPTED
)
999 io_flags
|= B_ENCRYPTED_IO
;
1000 if (vp
->v_flag
& VSYSTEM
)
1003 if ((flags
& CL_READ
) && ((upl_offset
+ non_rounded_size
) & PAGE_MASK
) && (!(flags
& CL_NOZERO
))) {
1005 * then we are going to end up
1006 * with a page that we can't complete (the file size wasn't a multiple
1007 * of PAGE_SIZE and we're trying to read to the end of the file
1008 * so we'll go ahead and zero out the portion of the page we can't
1009 * read in from the file
1011 zero_offset
= upl_offset
+ non_rounded_size
;
1016 u_int io_size_wanted
;
1019 if (size
> max_iosize
)
1020 io_size
= max_iosize
;
1024 io_size_wanted
= io_size
;
1025 io_size_tmp
= (size_t)io_size
;
1027 if ((error
= VNOP_BLOCKMAP(vp
, f_offset
, io_size
, &blkno
, &io_size_tmp
, NULL
, bmap_flags
, NULL
)))
1030 if (io_size_tmp
> io_size_wanted
)
1031 io_size
= io_size_wanted
;
1033 io_size
= (u_int
)io_size_tmp
;
1035 if (real_bp
&& (real_bp
->b_blkno
== real_bp
->b_lblkno
))
1036 real_bp
->b_blkno
= blkno
;
1038 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 24)) | DBG_FUNC_NONE
,
1039 (int)f_offset
, (int)(blkno
>>32), (int)blkno
, io_size
, 0);
1043 * vnop_blockmap didn't return an error... however, it did
1044 * return an extent size of 0 which means we can't
1045 * make forward progress on this I/O... a hole in the
1046 * file would be returned as a blkno of -1 with a non-zero io_size
1047 * a real extent is returned with a blkno != -1 and a non-zero io_size
1052 if ( !(flags
& CL_READ
) && blkno
== -1) {
1056 if (upl_get_internal_vectorupl(upl
))
1057 panic("Vector UPLs should not take this code-path\n");
1059 * we're writing into a 'hole'
1061 if (flags
& CL_PAGEOUT
) {
1063 * if we got here via cluster_pageout
1064 * then just error the request and return
1065 * the 'hole' should already have been covered
1071 * we can get here if the cluster code happens to
1072 * pick up a page that was dirtied via mmap vs
1073 * a 'write' and the page targets a 'hole'...
1074 * i.e. the writes to the cluster were sparse
1075 * and the file was being written for the first time
1077 * we can also get here if the filesystem supports
1078 * 'holes' that are less than PAGE_SIZE.... because
1079 * we can't know if the range in the page that covers
1080 * the 'hole' has been dirtied via an mmap or not,
1081 * we have to assume the worst and try to push the
1082 * entire page to storage.
1084 * Try paging out the page individually before
1085 * giving up entirely and dumping it (the pageout
1086 * path will insure that the zero extent accounting
1087 * has been taken care of before we get back into cluster_io)
1089 * go direct to vnode_pageout so that we don't have to
1090 * unbusy the page from the UPL... we used to do this
1091 * so that we could call ubc_sync_range, but that results
1092 * in a potential deadlock if someone else races us to acquire
1093 * that page and wins and in addition needs one of the pages
1094 * we're continuing to hold in the UPL
1096 pageout_flags
= UPL_MSYNC
| UPL_VNODE_PAGER
| UPL_NESTED_PAGEOUT
;
1098 if ( !(flags
& CL_ASYNC
))
1099 pageout_flags
|= UPL_IOSYNC
;
1100 if ( !(flags
& CL_COMMIT
))
1101 pageout_flags
|= UPL_NOCOMMIT
;
1107 * first we have to wait for the the current outstanding I/Os
1108 * to complete... EOT hasn't been set yet on this transaction
1109 * so the pages won't be released just because all of the current
1110 * I/O linked to this transaction has completed...
1112 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1115 * we've got a transcation that
1116 * includes the page we're about to push out through vnode_pageout...
1117 * find the last bp in the list which will be the one that
1118 * includes the head of this page and round it's iosize down
1119 * to a page boundary...
1121 for (last_cbp
= cbp
= cbp_head
; cbp
->b_trans_next
; cbp
= cbp
->b_trans_next
)
1124 cbp
->b_bcount
&= ~PAGE_MASK
;
1126 if (cbp
->b_bcount
== 0) {
1128 * this buf no longer has any I/O associated with it
1132 if (cbp
== cbp_head
) {
1134 * the buf we just freed was the only buf in
1135 * this transaction... so there's no I/O to do
1140 * remove the buf we just freed from
1141 * the transaction list
1143 last_cbp
->b_trans_next
= NULL
;
1144 cbp_tail
= last_cbp
;
1149 * there was more to the current transaction
1150 * than just the page we are pushing out via vnode_pageout...
1151 * mark it as finished and complete it... we've already
1152 * waited for the I/Os to complete above in the call to cluster_wait_IO
1154 cluster_EOT(cbp_head
, cbp_tail
, 0);
1156 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1161 if (vnode_pageout(vp
, upl
, trunc_page(upl_offset
), trunc_page_64(f_offset
), PAGE_SIZE
, pageout_flags
, NULL
) != PAGER_SUCCESS
) {
1164 e_offset
= round_page_64(f_offset
+ 1);
1165 io_size
= e_offset
- f_offset
;
1167 f_offset
+= io_size
;
1168 upl_offset
+= io_size
;
1170 if (size
>= io_size
)
1175 * keep track of how much of the original request
1176 * that we've actually completed... non_rounded_size
1177 * may go negative due to us rounding the request
1178 * to a page size multiple (i.e. size > non_rounded_size)
1180 non_rounded_size
-= io_size
;
1182 if (non_rounded_size
<= 0) {
1184 * we've transferred all of the data in the original
1185 * request, but we were unable to complete the tail
1186 * of the last page because the file didn't have
1187 * an allocation to back that portion... this is ok.
1193 flags
&= ~CL_COMMIT
;
1198 lblkno
= (daddr64_t
)(f_offset
/ PAGE_SIZE_64
);
1200 * we have now figured out how much I/O we can do - this is in 'io_size'
1201 * pg_offset is the starting point in the first page for the I/O
1202 * pg_count is the number of full and partial pages that 'io_size' encompasses
1204 pg_offset
= upl_offset
& PAGE_MASK
;
1206 if (flags
& CL_DEV_MEMORY
) {
1208 * treat physical requests as one 'giant' page
1212 pg_count
= (io_size
+ pg_offset
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1214 if ((flags
& CL_READ
) && blkno
== -1) {
1215 vm_offset_t commit_offset
;
1217 int complete_transaction_now
= 0;
1220 * if we're reading and blkno == -1, then we've got a
1221 * 'hole' in the file that we need to deal with by zeroing
1222 * out the affected area in the upl
1224 if (io_size
>= (u_int
)non_rounded_size
) {
1226 * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
1227 * than 'zero_offset' will be non-zero
1228 * if the 'hole' returned by vnop_blockmap extends all the way to the eof
1229 * (indicated by the io_size finishing off the I/O request for this UPL)
1230 * than we're not going to issue an I/O for the
1231 * last page in this upl... we need to zero both the hole and the tail
1232 * of the page beyond the EOF, since the delayed zero-fill won't kick in
1234 bytes_to_zero
= non_rounded_size
;
1235 if (!(flags
& CL_NOZERO
))
1236 bytes_to_zero
= (((upl_offset
+ io_size
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - upl_offset
;
1240 bytes_to_zero
= io_size
;
1244 cluster_zero(upl
, upl_offset
, bytes_to_zero
, real_bp
);
1250 * if there is a current I/O chain pending
1251 * then the first page of the group we just zero'd
1252 * will be handled by the I/O completion if the zero
1253 * fill started in the middle of the page
1255 commit_offset
= (upl_offset
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1257 pg_resid
= commit_offset
- upl_offset
;
1259 if (bytes_to_zero
>= pg_resid
) {
1261 * the last page of the current I/O
1262 * has been completed...
1263 * compute the number of fully zero'd
1264 * pages that are beyond it
1265 * plus the last page if its partial
1266 * and we have no more I/O to issue...
1267 * otherwise a partial page is left
1268 * to begin the next I/O
1270 if ((int)io_size
>= non_rounded_size
)
1271 pg_count
= (bytes_to_zero
- pg_resid
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1273 pg_count
= (bytes_to_zero
- pg_resid
) / PAGE_SIZE
;
1275 complete_transaction_now
= 1;
1279 * no pending I/O to deal with
1280 * so, commit all of the fully zero'd pages
1281 * plus the last page if its partial
1282 * and we have no more I/O to issue...
1283 * otherwise a partial page is left
1284 * to begin the next I/O
1286 if ((int)io_size
>= non_rounded_size
)
1287 pg_count
= (pg_offset
+ bytes_to_zero
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1289 pg_count
= (pg_offset
+ bytes_to_zero
) / PAGE_SIZE
;
1291 commit_offset
= upl_offset
& ~PAGE_MASK
;
1293 if ( (flags
& CL_COMMIT
) && pg_count
) {
1294 ubc_upl_commit_range(upl
, commit_offset
, pg_count
* PAGE_SIZE
,
1295 UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
);
1297 upl_offset
+= io_size
;
1298 f_offset
+= io_size
;
1302 * keep track of how much of the original request
1303 * that we've actually completed... non_rounded_size
1304 * may go negative due to us rounding the request
1305 * to a page size multiple (i.e. size > non_rounded_size)
1307 non_rounded_size
-= io_size
;
1309 if (non_rounded_size
<= 0) {
1311 * we've transferred all of the data in the original
1312 * request, but we were unable to complete the tail
1313 * of the last page because the file didn't have
1314 * an allocation to back that portion... this is ok.
1318 if (cbp_head
&& (complete_transaction_now
|| size
== 0)) {
1319 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1321 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1323 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1329 if (pg_count
> max_vectors
) {
1330 if (((pg_count
- max_vectors
) * PAGE_SIZE
) > io_size
) {
1331 io_size
= PAGE_SIZE
- pg_offset
;
1334 io_size
-= (pg_count
- max_vectors
) * PAGE_SIZE
;
1335 pg_count
= max_vectors
;
1339 * If the transaction is going to reach the maximum number of
1340 * desired elements, truncate the i/o to the nearest page so
1341 * that the actual i/o is initiated after this buffer is
1342 * created and added to the i/o chain.
1344 * I/O directed to physically contiguous memory
1345 * doesn't have a requirement to make sure we 'fill' a page
1347 if ( !(flags
& CL_DEV_MEMORY
) && trans_count
>= max_trans_count
&&
1348 ((upl_offset
+ io_size
) & PAGE_MASK
)) {
1349 vm_offset_t aligned_ofs
;
1351 aligned_ofs
= (upl_offset
+ io_size
) & ~PAGE_MASK
;
1353 * If the io_size does not actually finish off even a
1354 * single page we have to keep adding buffers to the
1355 * transaction despite having reached the desired limit.
1357 * Eventually we get here with the page being finished
1358 * off (and exceeded) and then we truncate the size of
1359 * this i/o request so that it is page aligned so that
1360 * we can finally issue the i/o on the transaction.
1362 if (aligned_ofs
> upl_offset
) {
1363 io_size
= aligned_ofs
- upl_offset
;
1368 if ( !(mp
->mnt_kern_flag
& MNTK_VIRTUALDEV
))
1370 * if we're not targeting a virtual device i.e. a disk image
1371 * it's safe to dip into the reserve pool since real devices
1372 * can complete this I/O request without requiring additional
1373 * bufs from the alloc_io_buf pool
1376 else if ((flags
& CL_ASYNC
) && !(flags
& CL_PAGEOUT
))
1378 * Throttle the speculative IO
1384 cbp
= alloc_io_buf(vp
, priv
);
1386 if (flags
& CL_PAGEOUT
) {
1389 for (i
= 0; i
< pg_count
; i
++) {
1390 if (buf_invalblkno(vp
, lblkno
+ i
, 0) == EBUSY
)
1391 panic("BUSY bp found in cluster_io");
1394 if (flags
& CL_ASYNC
) {
1395 if (buf_setcallback(cbp
, (void *)cluster_iodone
, callback_arg
))
1396 panic("buf_setcallback failed\n");
1398 cbp
->b_cliodone
= (void *)callback
;
1399 cbp
->b_flags
|= io_flags
;
1401 cbp
->b_lblkno
= lblkno
;
1402 cbp
->b_blkno
= blkno
;
1403 cbp
->b_bcount
= io_size
;
1405 if (buf_setupl(cbp
, upl
, upl_offset
))
1406 panic("buf_setupl failed\n");
1408 cbp
->b_trans_next
= (buf_t
)NULL
;
1410 if ((cbp
->b_iostate
= (void *)iostate
))
1412 * caller wants to track the state of this
1413 * io... bump the amount issued against this stream
1415 iostate
->io_issued
+= io_size
;
1417 if (flags
& CL_READ
) {
1418 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 26)) | DBG_FUNC_NONE
,
1419 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1422 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 27)) | DBG_FUNC_NONE
,
1423 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1427 cbp_tail
->b_trans_next
= cbp
;
1433 if ( (cbp_head
->b_real_bp
= real_bp
) )
1434 real_bp
= (buf_t
)NULL
;
1436 *(buf_t
*)(&cbp
->b_trans_head
) = cbp_head
;
1440 upl_offset
+= io_size
;
1441 f_offset
+= io_size
;
1444 * keep track of how much of the original request
1445 * that we've actually completed... non_rounded_size
1446 * may go negative due to us rounding the request
1447 * to a page size multiple (i.e. size > non_rounded_size)
1449 non_rounded_size
-= io_size
;
1451 if (non_rounded_size
<= 0) {
1453 * we've transferred all of the data in the original
1454 * request, but we were unable to complete the tail
1455 * of the last page because the file didn't have
1456 * an allocation to back that portion... this is ok.
1462 * we have no more I/O to issue, so go
1463 * finish the final transaction
1466 } else if ( ((flags
& CL_DEV_MEMORY
) || (upl_offset
& PAGE_MASK
) == 0) &&
1467 ((flags
& CL_ASYNC
) || trans_count
> max_trans_count
) ) {
1469 * I/O directed to physically contiguous memory...
1470 * which doesn't have a requirement to make sure we 'fill' a page
1472 * the current I/O we've prepared fully
1473 * completes the last page in this request
1475 * it's either an ASYNC request or
1476 * we've already accumulated more than 8 I/O's into
1477 * this transaction so mark it as complete so that
1478 * it can finish asynchronously or via the cluster_complete_transaction
1479 * below if the request is synchronous
1483 if (need_EOT
== TRUE
)
1484 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1486 if (flags
& CL_THROTTLE
)
1487 (void)vnode_waitforwrites(vp
, async_throttle
, 0, 0, "cluster_io");
1489 if ( !(io_flags
& B_READ
))
1490 vnode_startwrite(vp
);
1492 (void) VNOP_STRATEGY(cbp
);
1494 if (need_EOT
== TRUE
) {
1495 if ( !(flags
& CL_ASYNC
))
1496 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 1);
1510 * first wait until all of the outstanding I/O
1511 * for this partial transaction has completed
1513 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1516 * Rewind the upl offset to the beginning of the
1519 upl_offset
= cbp_head
->b_uploffset
;
1521 for (cbp
= cbp_head
; cbp
;) {
1524 size
+= cbp
->b_bcount
;
1525 io_size
+= cbp
->b_bcount
;
1527 cbp_next
= cbp
->b_trans_next
;
1533 int need_wakeup
= 0;
1536 * update the error condition for this stream
1537 * since we never really issued the io
1538 * just go ahead and adjust it back
1540 lck_mtx_lock_spin(&iostate
->io_mtxp
);
1542 if (iostate
->io_error
== 0)
1543 iostate
->io_error
= error
;
1544 iostate
->io_issued
-= io_size
;
1546 if (iostate
->io_wanted
) {
1548 * someone is waiting for the state of
1549 * this io stream to change
1551 iostate
->io_wanted
= 0;
1554 lck_mtx_unlock(&iostate
->io_mtxp
);
1557 wakeup((caddr_t
)&iostate
->io_wanted
);
1559 if (flags
& CL_COMMIT
) {
1562 pg_offset
= upl_offset
& PAGE_MASK
;
1563 abort_size
= (upl_end_offset
- upl_offset
+ PAGE_MASK
) & ~PAGE_MASK
;
1565 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, abort_size
, error
, io_flags
);
1567 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 28)) | DBG_FUNC_NONE
,
1568 upl
, upl_offset
- pg_offset
, abort_size
, (error
<< 24) | upl_flags
, 0);
1572 } else if (cbp_head
)
1573 panic("%s(): cbp_head is not NULL.\n", __FUNCTION__
);
1577 * can get here if we either encountered an error
1578 * or we completely zero-filled the request and
1582 real_bp
->b_flags
|= B_ERROR
;
1583 real_bp
->b_error
= error
;
1585 buf_biodone(real_bp
);
1587 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_END
, (int)f_offset
, size
, upl_offset
, retval
, 0);
1592 #define reset_vector_run_state() \
1593 issueVectorUPL = vector_upl_offset = vector_upl_index = vector_upl_iosize = vector_upl_size = 0;
1596 vector_cluster_io(vnode_t vp
, upl_t vector_upl
, vm_offset_t vector_upl_offset
, off_t v_upl_uio_offset
, int vector_upl_iosize
,
1597 int io_flag
, buf_t real_bp
, struct clios
*iostate
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1599 vector_upl_set_pagelist(vector_upl
);
1601 if(io_flag
& CL_READ
) {
1602 if(vector_upl_offset
== 0 && ((vector_upl_iosize
& PAGE_MASK
)==0))
1603 io_flag
&= ~CL_PRESERVE
; /*don't zero fill*/
1605 io_flag
|= CL_PRESERVE
; /*zero fill*/
1607 return (cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, real_bp
, iostate
, callback
, callback_arg
));
1612 cluster_read_prefetch(vnode_t vp
, off_t f_offset
, u_int size
, off_t filesize
, int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
)
1614 int pages_in_prefetch
;
1616 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_START
,
1617 (int)f_offset
, size
, (int)filesize
, 0, 0);
1619 if (f_offset
>= filesize
) {
1620 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1621 (int)f_offset
, 0, 0, 0, 0);
1624 if ((off_t
)size
> (filesize
- f_offset
))
1625 size
= filesize
- f_offset
;
1626 pages_in_prefetch
= (size
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1628 advisory_read_ext(vp
, filesize
, f_offset
, size
, callback
, callback_arg
, bflag
);
1630 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1631 (int)f_offset
+ size
, pages_in_prefetch
, 0, 1, 0);
1633 return (pages_in_prefetch
);
1639 cluster_read_ahead(vnode_t vp
, struct cl_extent
*extent
, off_t filesize
, struct cl_readahead
*rap
, int (*callback
)(buf_t
, void *), void *callback_arg
,
1644 int size_of_prefetch
;
1648 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_START
,
1649 (int)extent
->b_addr
, (int)extent
->e_addr
, (int)rap
->cl_lastr
, 0, 0);
1651 if (extent
->b_addr
== rap
->cl_lastr
&& extent
->b_addr
== extent
->e_addr
) {
1652 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1653 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 0, 0);
1656 if (rap
->cl_lastr
== -1 || (extent
->b_addr
!= rap
->cl_lastr
&& extent
->b_addr
!= (rap
->cl_lastr
+ 1))) {
1660 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1661 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 1, 0);
1665 max_prefetch
= MAX_PREFETCH(vp
, cluster_max_io_size(vp
->v_mount
, CL_READ
), (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
));
1667 if ((max_prefetch
/ PAGE_SIZE
) > speculative_prefetch_max
)
1668 max_prefetch
= (speculative_prefetch_max
* PAGE_SIZE
);
1670 if (max_prefetch
<= PAGE_SIZE
) {
1671 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1672 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 6, 0);
1675 if (extent
->e_addr
< rap
->cl_maxra
) {
1676 if ((rap
->cl_maxra
- extent
->e_addr
) > ((max_prefetch
/ PAGE_SIZE
) / 4)) {
1678 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1679 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 2, 0);
1683 r_addr
= max(extent
->e_addr
, rap
->cl_maxra
) + 1;
1684 f_offset
= (off_t
)(r_addr
* PAGE_SIZE_64
);
1686 size_of_prefetch
= 0;
1688 ubc_range_op(vp
, f_offset
, f_offset
+ PAGE_SIZE_64
, UPL_ROP_PRESENT
, &size_of_prefetch
);
1690 if (size_of_prefetch
) {
1691 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1692 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 3, 0);
1695 if (f_offset
< filesize
) {
1696 daddr64_t read_size
;
1698 rap
->cl_ralen
= rap
->cl_ralen
? min(max_prefetch
/ PAGE_SIZE
, rap
->cl_ralen
<< 1) : 1;
1700 read_size
= (extent
->e_addr
+ 1) - extent
->b_addr
;
1702 if (read_size
> rap
->cl_ralen
) {
1703 if (read_size
> max_prefetch
/ PAGE_SIZE
)
1704 rap
->cl_ralen
= max_prefetch
/ PAGE_SIZE
;
1706 rap
->cl_ralen
= read_size
;
1708 size_of_prefetch
= cluster_read_prefetch(vp
, f_offset
, rap
->cl_ralen
* PAGE_SIZE
, filesize
, callback
, callback_arg
, bflag
);
1710 if (size_of_prefetch
)
1711 rap
->cl_maxra
= (r_addr
+ size_of_prefetch
) - 1;
1713 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1714 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 4, 0);
1719 cluster_pageout(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
1720 int size
, off_t filesize
, int flags
)
1722 return cluster_pageout_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
1728 cluster_pageout_ext(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
1729 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1736 local_flags
= CL_PAGEOUT
| CL_THROTTLE
;
1738 if ((flags
& UPL_IOSYNC
) == 0)
1739 local_flags
|= CL_ASYNC
;
1740 if ((flags
& UPL_NOCOMMIT
) == 0)
1741 local_flags
|= CL_COMMIT
;
1742 if ((flags
& UPL_KEEPCACHED
))
1743 local_flags
|= CL_KEEPCACHED
;
1744 if (flags
& UPL_PAGING_ENCRYPTED
)
1745 local_flags
|= CL_ENCRYPTED
;
1748 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 52)) | DBG_FUNC_NONE
,
1749 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
1752 * If they didn't specify any I/O, then we are done...
1753 * we can't issue an abort because we don't know how
1754 * big the upl really is
1759 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
1760 if (local_flags
& CL_COMMIT
)
1761 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
1765 * can't page-in from a negative offset
1766 * or if we're starting beyond the EOF
1767 * or if the file offset isn't page aligned
1768 * or the size requested isn't a multiple of PAGE_SIZE
1770 if (f_offset
< 0 || f_offset
>= filesize
||
1771 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
)) {
1772 if (local_flags
& CL_COMMIT
)
1773 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
1776 max_size
= filesize
- f_offset
;
1778 if (size
< max_size
)
1783 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1785 if (size
> rounded_size
) {
1786 if (local_flags
& CL_COMMIT
)
1787 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
, size
- rounded_size
,
1788 UPL_ABORT_FREE_ON_EMPTY
);
1790 return (cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
1791 local_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
));
1796 cluster_pagein(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
1797 int size
, off_t filesize
, int flags
)
1799 return cluster_pagein_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
1804 cluster_pagein_ext(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
1805 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1811 int local_flags
= 0;
1813 if (upl
== NULL
|| size
< 0)
1814 panic("cluster_pagein: NULL upl passed in");
1816 if ((flags
& UPL_IOSYNC
) == 0)
1817 local_flags
|= CL_ASYNC
;
1818 if ((flags
& UPL_NOCOMMIT
) == 0)
1819 local_flags
|= CL_COMMIT
;
1820 if (flags
& UPL_IOSTREAMING
)
1821 local_flags
|= CL_IOSTREAMING
;
1822 if (flags
& UPL_PAGING_ENCRYPTED
)
1823 local_flags
|= CL_ENCRYPTED
;
1826 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 56)) | DBG_FUNC_NONE
,
1827 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
1830 * can't page-in from a negative offset
1831 * or if we're starting beyond the EOF
1832 * or if the file offset isn't page aligned
1833 * or the size requested isn't a multiple of PAGE_SIZE
1835 if (f_offset
< 0 || f_offset
>= filesize
||
1836 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
) || (upl_offset
& PAGE_MASK
)) {
1837 if (local_flags
& CL_COMMIT
)
1838 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
1841 max_size
= filesize
- f_offset
;
1843 if (size
< max_size
)
1848 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1850 if (size
> rounded_size
&& (local_flags
& CL_COMMIT
))
1851 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
,
1852 size
- rounded_size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
1854 retval
= cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
1855 local_flags
| CL_READ
| CL_PAGEIN
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
1862 cluster_bp(buf_t bp
)
1864 return cluster_bp_ext(bp
, NULL
, NULL
);
1869 cluster_bp_ext(buf_t bp
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1874 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 19)) | DBG_FUNC_START
,
1875 bp
, (int)bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
1877 if (bp
->b_flags
& B_READ
)
1878 flags
= CL_ASYNC
| CL_READ
;
1881 if (bp
->b_flags
& B_PASSIVE
)
1882 flags
|= CL_PASSIVE
;
1884 f_offset
= ubc_blktooff(bp
->b_vp
, bp
->b_lblkno
);
1886 return (cluster_io(bp
->b_vp
, bp
->b_upl
, 0, f_offset
, bp
->b_bcount
, flags
, bp
, (struct clios
*)NULL
, callback
, callback_arg
));
1892 cluster_write(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
, int xflags
)
1894 return cluster_write_ext(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, xflags
, NULL
, NULL
);
1899 cluster_write_ext(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
,
1900 int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1902 user_ssize_t cur_resid
;
1907 int write_type
= IO_COPY
;
1908 u_int32_t write_length
;
1912 if (flags
& IO_PASSIVE
)
1917 if (vp
->v_flag
& VNOCACHE_DATA
)
1918 flags
|= IO_NOCACHE
;
1923 * this call is being made to zero-fill some range in the file
1925 retval
= cluster_write_copy(vp
, NULL
, (u_int32_t
)0, oldEOF
, newEOF
, headOff
, tailOff
, flags
, callback
, callback_arg
);
1930 * do a write through the cache if one of the following is true....
1931 * NOCACHE is not true or NODIRECT is true
1932 * the uio request doesn't target USERSPACE
1933 * otherwise, find out if we want the direct or contig variant for
1934 * the first vector in the uio request
1936 if ( ((flags
& (IO_NOCACHE
| IO_NODIRECT
)) == IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) )
1937 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
1939 if ( (flags
& (IO_TAILZEROFILL
| IO_HEADZEROFILL
)) && write_type
== IO_DIRECT
)
1941 * must go through the cached variant in this case
1943 write_type
= IO_COPY
;
1945 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< newEOF
&& retval
== 0) {
1947 switch (write_type
) {
1951 * make sure the uio_resid isn't too big...
1952 * internally, we want to handle all of the I/O in
1953 * chunk sizes that fit in a 32 bit int
1955 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
)) {
1957 * we're going to have to call cluster_write_copy
1960 * only want the last call to cluster_write_copy to
1961 * have the IO_TAILZEROFILL flag set and only the
1962 * first call should have IO_HEADZEROFILL
1964 zflags
= flags
& ~IO_TAILZEROFILL
;
1965 flags
&= ~IO_HEADZEROFILL
;
1967 write_length
= MAX_IO_REQUEST_SIZE
;
1970 * last call to cluster_write_copy
1974 write_length
= (u_int32_t
)cur_resid
;
1976 retval
= cluster_write_copy(vp
, uio
, write_length
, oldEOF
, newEOF
, headOff
, tailOff
, zflags
, callback
, callback_arg
);
1980 zflags
= flags
& ~(IO_TAILZEROFILL
| IO_HEADZEROFILL
);
1982 if (flags
& IO_HEADZEROFILL
) {
1984 * only do this once per request
1986 flags
&= ~IO_HEADZEROFILL
;
1988 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, uio
->uio_offset
,
1989 headOff
, (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
1993 retval
= cluster_write_contig(vp
, uio
, newEOF
, &write_type
, &write_length
, callback
, callback_arg
, bflag
);
1995 if (retval
== 0 && (flags
& IO_TAILZEROFILL
) && uio_resid(uio
) == 0) {
1997 * we're done with the data from the user specified buffer(s)
1998 * and we've been requested to zero fill at the tail
1999 * treat this as an IO_HEADZEROFILL which doesn't require a uio
2000 * by rearranging the args and passing in IO_HEADZEROFILL
2002 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, tailOff
, uio
->uio_offset
,
2003 (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
2009 * cluster_write_direct is never called with IO_TAILZEROFILL || IO_HEADZEROFILL
2011 retval
= cluster_write_direct(vp
, uio
, oldEOF
, newEOF
, &write_type
, &write_length
, flags
, callback
, callback_arg
);
2015 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
2019 * in case we end up calling cluster_write_copy (from cluster_write_direct)
2020 * multiple times to service a multi-vector request that is not aligned properly
2021 * we need to update the oldEOF so that we
2022 * don't zero-fill the head of a page if we've successfully written
2023 * data to that area... 'cluster_write_copy' will zero-fill the head of a
2024 * page that is beyond the oldEOF if the write is unaligned... we only
2025 * want that to happen for the very first page of the cluster_write,
2026 * NOT the first page of each vector making up a multi-vector write.
2028 if (uio
->uio_offset
> oldEOF
)
2029 oldEOF
= uio
->uio_offset
;
2036 cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
2037 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2040 upl_page_info_t
*pl
;
2041 vm_offset_t upl_offset
;
2042 vm_offset_t vector_upl_offset
= 0;
2043 u_int32_t io_req_size
;
2044 u_int32_t offset_in_file
;
2045 u_int32_t offset_in_iovbase
;
2048 upl_size_t upl_size
, vector_upl_size
= 0;
2049 vm_size_t upl_needed_size
;
2050 mach_msg_type_number_t pages_in_pl
;
2053 mach_msg_type_number_t i
;
2054 int force_data_sync
;
2057 struct clios iostate
;
2058 user_addr_t iov_base
;
2059 u_int32_t mem_alignment_mask
;
2060 u_int32_t devblocksize
;
2061 u_int32_t max_upl_size
;
2063 u_int32_t vector_upl_iosize
= 0;
2064 int issueVectorUPL
= 0,useVectorUPL
= (uio
->uio_iovcnt
> 1);
2065 off_t v_upl_uio_offset
= 0;
2066 int vector_upl_index
=0;
2067 upl_t vector_upl
= NULL
;
2071 * When we enter this routine, we know
2072 * -- the resid will not exceed iov_len
2074 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_START
,
2075 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
2077 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
2079 io_flag
= CL_ASYNC
| CL_PRESERVE
| CL_COMMIT
| CL_THROTTLE
| CL_DIRECT_IO
;
2081 if (flags
& IO_PASSIVE
)
2082 io_flag
|= CL_PASSIVE
;
2084 iostate
.io_completed
= 0;
2085 iostate
.io_issued
= 0;
2086 iostate
.io_error
= 0;
2087 iostate
.io_wanted
= 0;
2089 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
2091 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
2092 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
2094 if (devblocksize
== 1) {
2096 * the AFP client advertises a devblocksize of 1
2097 * however, its BLOCKMAP routine maps to physical
2098 * blocks that are PAGE_SIZE in size...
2099 * therefore we can't ask for I/Os that aren't page aligned
2100 * or aren't multiples of PAGE_SIZE in size
2101 * by setting devblocksize to PAGE_SIZE, we re-instate
2102 * the old behavior we had before the mem_alignment_mask
2103 * changes went in...
2105 devblocksize
= PAGE_SIZE
;
2109 io_req_size
= *write_length
;
2110 iov_base
= uio_curriovbase(uio
);
2112 offset_in_file
= (u_int32_t
)uio
->uio_offset
& PAGE_MASK
;
2113 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
2115 if (offset_in_file
|| offset_in_iovbase
) {
2117 * one of the 2 important offsets is misaligned
2118 * so fire an I/O through the cache for this entire vector
2120 goto wait_for_dwrites
;
2122 if (iov_base
& (devblocksize
- 1)) {
2124 * the offset in memory must be on a device block boundary
2125 * so that we can guarantee that we can generate an
2126 * I/O that ends on a page boundary in cluster_io
2128 goto wait_for_dwrites
;
2131 while (io_req_size
>= PAGE_SIZE
&& uio
->uio_offset
< newEOF
&& retval
== 0) {
2134 cluster_syncup(vp
, newEOF
, callback
, callback_arg
);
2137 io_size
= io_req_size
& ~PAGE_MASK
;
2138 iov_base
= uio_curriovbase(uio
);
2140 if (io_size
> max_upl_size
)
2141 io_size
= max_upl_size
;
2143 if(useVectorUPL
&& (iov_base
& PAGE_MASK
)) {
2145 * We have an iov_base that's not page-aligned.
2146 * Issue all I/O's that have been collected within
2147 * this Vectored UPL.
2149 if(vector_upl_index
) {
2150 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2151 reset_vector_run_state();
2155 * After this point, if we are using the Vector UPL path and the base is
2156 * not page-aligned then the UPL with that base will be the first in the vector UPL.
2160 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
2161 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
2163 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_START
,
2164 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
2166 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
2168 upl_size
= upl_needed_size
;
2169 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
2170 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
2172 kret
= vm_map_get_upl(current_map(),
2173 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2181 if (kret
!= KERN_SUCCESS
) {
2182 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2185 * failed to get pagelist
2187 * we may have already spun some portion of this request
2188 * off as async requests... we need to wait for the I/O
2189 * to complete before returning
2191 goto wait_for_dwrites
;
2193 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2194 pages_in_pl
= upl_size
/ PAGE_SIZE
;
2196 for (i
= 0; i
< pages_in_pl
; i
++) {
2197 if (!upl_valid_page(pl
, i
))
2200 if (i
== pages_in_pl
)
2204 * didn't get all the pages back that we
2205 * needed... release this upl and try again
2207 ubc_upl_abort(upl
, 0);
2209 if (force_data_sync
>= 3) {
2210 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2211 i
, pages_in_pl
, upl_size
, kret
, 0);
2213 * for some reason, we couldn't acquire a hold on all
2214 * the pages needed in the user's address space
2216 * we may have already spun some portion of this request
2217 * off as async requests... we need to wait for the I/O
2218 * to complete before returning
2220 goto wait_for_dwrites
;
2224 * Consider the possibility that upl_size wasn't satisfied.
2226 if (upl_size
< upl_needed_size
) {
2227 if (upl_size
&& upl_offset
== 0)
2232 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2233 (int)upl_offset
, upl_size
, (int)iov_base
, io_size
, 0);
2236 ubc_upl_abort(upl
, 0);
2238 * we may have already spun some portion of this request
2239 * off as async requests... we need to wait for the I/O
2240 * to complete before returning
2242 goto wait_for_dwrites
;
2246 vm_offset_t end_off
= ((iov_base
+ io_size
) & PAGE_MASK
);
2250 * After this point, if we are using a vector UPL, then
2251 * either all the UPL elements end on a page boundary OR
2252 * this UPL is the last element because it does not end
2253 * on a page boundary.
2258 * Now look for pages already in the cache
2259 * and throw them away.
2260 * uio->uio_offset is page aligned within the file
2261 * io_size is a multiple of PAGE_SIZE
2263 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ io_size
, UPL_ROP_DUMP
, NULL
);
2266 * we want push out these writes asynchronously so that we can overlap
2267 * the preparation of the next I/O
2268 * if there are already too many outstanding writes
2269 * wait until some complete before issuing the next
2271 if (iostate
.io_issued
> iostate
.io_completed
)
2272 cluster_iostate_wait(&iostate
, max_upl_size
* IO_SCALE(vp
, 2), "cluster_write_direct");
2274 if (iostate
.io_error
) {
2276 * one of the earlier writes we issued ran into a hard error
2277 * don't issue any more writes, cleanup the UPL
2278 * that was just created but not used, then
2279 * go wait for all writes that are part of this stream
2280 * to complete before returning the error to the caller
2282 ubc_upl_abort(upl
, 0);
2284 goto wait_for_dwrites
;
2287 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_START
,
2288 (int)upl_offset
, (int)uio
->uio_offset
, io_size
, io_flag
, 0);
2291 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
2292 io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2295 if(!vector_upl_index
) {
2296 vector_upl
= vector_upl_create(upl_offset
);
2297 v_upl_uio_offset
= uio
->uio_offset
;
2298 vector_upl_offset
= upl_offset
;
2301 vector_upl_set_subupl(vector_upl
,upl
,upl_size
);
2302 vector_upl_set_iostate(vector_upl
, upl
, vector_upl_size
, upl_size
);
2304 vector_upl_iosize
+= io_size
;
2305 vector_upl_size
+= upl_size
;
2307 if(issueVectorUPL
|| vector_upl_index
== MAX_VECTOR_UPL_ELEMENTS
|| vector_upl_size
>= MAX_VECTOR_UPL_SIZE
) {
2308 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2309 reset_vector_run_state();
2314 * update the uio structure to
2315 * reflect the I/O that we just issued
2317 uio_update(uio
, (user_size_t
)io_size
);
2320 * in case we end up calling through to cluster_write_copy to finish
2321 * the tail of this request, we need to update the oldEOF so that we
2322 * don't zero-fill the head of a page if we've successfully written
2323 * data to that area... 'cluster_write_copy' will zero-fill the head of a
2324 * page that is beyond the oldEOF if the write is unaligned... we only
2325 * want that to happen for the very first page of the cluster_write,
2326 * NOT the first page of each vector making up a multi-vector write.
2328 if (uio
->uio_offset
> oldEOF
)
2329 oldEOF
= uio
->uio_offset
;
2331 io_req_size
-= io_size
;
2333 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_END
,
2334 (int)upl_offset
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
2338 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0) {
2340 retval
= cluster_io_type(uio
, write_type
, write_length
, MIN_DIRECT_WRITE_SIZE
);
2342 if (retval
== 0 && *write_type
== IO_DIRECT
) {
2344 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_NONE
,
2345 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
2353 if (retval
== 0 && iostate
.io_error
== 0 && useVectorUPL
&& vector_upl_index
) {
2354 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2355 reset_vector_run_state();
2358 if (iostate
.io_issued
> iostate
.io_completed
) {
2360 * make sure all async writes issued as part of this stream
2361 * have completed before we return
2363 cluster_iostate_wait(&iostate
, 0, "cluster_write_direct");
2365 if (iostate
.io_error
)
2366 retval
= iostate
.io_error
;
2368 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
2370 if (io_req_size
&& retval
== 0) {
2372 * we couldn't handle the tail of this request in DIRECT mode
2373 * so fire it through the copy path
2375 * note that flags will never have IO_HEADZEROFILL or IO_TAILZEROFILL set
2376 * so we can just pass 0 in for the headOff and tailOff
2378 if (uio
->uio_offset
> oldEOF
)
2379 oldEOF
= uio
->uio_offset
;
2381 retval
= cluster_write_copy(vp
, uio
, io_req_size
, oldEOF
, newEOF
, (off_t
)0, (off_t
)0, flags
, callback
, callback_arg
);
2383 *write_type
= IO_UNKNOWN
;
2385 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
2386 (int)uio
->uio_offset
, io_req_size
, retval
, 4, 0);
2393 cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
2394 int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
)
2396 upl_page_info_t
*pl
;
2397 addr64_t src_paddr
= 0;
2398 upl_t upl
[MAX_VECTS
];
2399 vm_offset_t upl_offset
;
2400 u_int32_t tail_size
= 0;
2403 upl_size_t upl_size
;
2404 vm_size_t upl_needed_size
;
2405 mach_msg_type_number_t pages_in_pl
;
2408 struct clios iostate
;
2413 user_addr_t iov_base
;
2414 u_int32_t devblocksize
;
2415 u_int32_t mem_alignment_mask
;
2418 * When we enter this routine, we know
2419 * -- the io_req_size will not exceed iov_len
2420 * -- the target address is physically contiguous
2422 cluster_syncup(vp
, newEOF
, callback
, callback_arg
);
2424 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
2425 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
2427 iostate
.io_completed
= 0;
2428 iostate
.io_issued
= 0;
2429 iostate
.io_error
= 0;
2430 iostate
.io_wanted
= 0;
2432 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
2435 io_size
= *write_length
;
2437 iov_base
= uio_curriovbase(uio
);
2439 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
2440 upl_needed_size
= upl_offset
+ io_size
;
2443 upl_size
= upl_needed_size
;
2444 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
2445 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
2447 kret
= vm_map_get_upl(current_map(),
2448 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2449 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, 0);
2451 if (kret
!= KERN_SUCCESS
) {
2453 * failed to get pagelist
2456 goto wait_for_cwrites
;
2461 * Consider the possibility that upl_size wasn't satisfied.
2463 if (upl_size
< upl_needed_size
) {
2465 * This is a failure in the physical memory case.
2468 goto wait_for_cwrites
;
2470 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
2472 src_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)upl_offset
;
2474 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
2475 u_int32_t head_size
;
2477 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
2479 if (head_size
> io_size
)
2480 head_size
= io_size
;
2482 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, head_size
, 0, callback
, callback_arg
);
2485 goto wait_for_cwrites
;
2487 upl_offset
+= head_size
;
2488 src_paddr
+= head_size
;
2489 io_size
-= head_size
;
2491 iov_base
+= head_size
;
2493 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
2495 * request doesn't set up on a memory boundary
2496 * the underlying DMA engine can handle...
2497 * return an error instead of going through
2498 * the slow copy path since the intent of this
2499 * path is direct I/O from device memory
2502 goto wait_for_cwrites
;
2505 tail_size
= io_size
& (devblocksize
- 1);
2506 io_size
-= tail_size
;
2508 while (io_size
&& error
== 0) {
2510 if (io_size
> MAX_IO_CONTIG_SIZE
)
2511 xsize
= MAX_IO_CONTIG_SIZE
;
2515 * request asynchronously so that we can overlap
2516 * the preparation of the next I/O... we'll do
2517 * the commit after all the I/O has completed
2518 * since its all issued against the same UPL
2519 * if there are already too many outstanding writes
2520 * wait until some have completed before issuing the next
2522 if (iostate
.io_issued
> iostate
.io_completed
)
2523 cluster_iostate_wait(&iostate
, MAX_IO_CONTIG_SIZE
* IO_SCALE(vp
, 2), "cluster_write_contig");
2525 if (iostate
.io_error
) {
2527 * one of the earlier writes we issued ran into a hard error
2528 * don't issue any more writes...
2529 * go wait for all writes that are part of this stream
2530 * to complete before returning the error to the caller
2532 goto wait_for_cwrites
;
2535 * issue an asynchronous write to cluster_io
2537 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
,
2538 xsize
, CL_DEV_MEMORY
| CL_ASYNC
| bflag
, (buf_t
)NULL
, (struct clios
*)&iostate
, callback
, callback_arg
);
2542 * The cluster_io write completed successfully,
2543 * update the uio structure
2545 uio_update(uio
, (user_size_t
)xsize
);
2547 upl_offset
+= xsize
;
2552 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
) {
2554 error
= cluster_io_type(uio
, write_type
, write_length
, 0);
2556 if (error
== 0 && *write_type
== IO_CONTIG
) {
2561 *write_type
= IO_UNKNOWN
;
2565 * make sure all async writes that are part of this stream
2566 * have completed before we proceed
2568 if (iostate
.io_issued
> iostate
.io_completed
)
2569 cluster_iostate_wait(&iostate
, 0, "cluster_write_contig");
2571 if (iostate
.io_error
)
2572 error
= iostate
.io_error
;
2574 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
2576 if (error
== 0 && tail_size
)
2577 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, tail_size
, 0, callback
, callback_arg
);
2579 for (n
= 0; n
< num_upl
; n
++)
2581 * just release our hold on each physically contiguous
2582 * region without changing any state
2584 ubc_upl_abort(upl
[n
], 0);
2591 * need to avoid a race between an msync of a range of pages dirtied via mmap
2592 * vs a filesystem such as HFS deciding to write a 'hole' to disk via cluster_write's
2593 * zerofill mechanism before it has seen the VNOP_PAGEOUTs for the pages being msync'd
2595 * we should never force-zero-fill pages that are already valid in the cache...
2596 * the entire page contains valid data (either from disk, zero-filled or dirtied
2597 * via an mmap) so we can only do damage by trying to zero-fill
2601 cluster_zero_range(upl_t upl
, upl_page_info_t
*pl
, int flags
, int io_offset
, off_t zero_off
, off_t upl_f_offset
, int bytes_to_zero
)
2604 boolean_t need_cluster_zero
= TRUE
;
2606 if ((flags
& (IO_NOZEROVALID
| IO_NOZERODIRTY
))) {
2608 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off
& PAGE_MASK_64
));
2609 zero_pg_index
= (int)((zero_off
- upl_f_offset
) / PAGE_SIZE_64
);
2611 if (upl_valid_page(pl
, zero_pg_index
)) {
2613 * never force zero valid pages - dirty or clean
2614 * we'll leave these in the UPL for cluster_write_copy to deal with
2616 need_cluster_zero
= FALSE
;
2619 if (need_cluster_zero
== TRUE
)
2620 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2622 return (bytes_to_zero
);
2627 cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
, off_t headOff
,
2628 off_t tailOff
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2630 upl_page_info_t
*pl
;
2632 vm_offset_t upl_offset
= 0;
2645 long long total_size
;
2648 long long zero_cnt1
;
2650 off_t write_off
= 0;
2652 boolean_t first_pass
= FALSE
;
2653 struct cl_extent cl
;
2654 struct cl_writebehind
*wbp
;
2656 u_int max_cluster_pgcount
;
2660 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
2661 (int)uio
->uio_offset
, io_req_size
, (int)oldEOF
, (int)newEOF
, 0);
2663 io_resid
= io_req_size
;
2665 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
2666 0, 0, (int)oldEOF
, (int)newEOF
, 0);
2670 if (flags
& IO_PASSIVE
)
2680 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
2681 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
2683 if (flags
& IO_HEADZEROFILL
) {
2685 * some filesystems (HFS is one) don't support unallocated holes within a file...
2686 * so we zero fill the intervening space between the old EOF and the offset
2687 * where the next chunk of real data begins.... ftruncate will also use this
2688 * routine to zero fill to the new EOF when growing a file... in this case, the
2689 * uio structure will not be provided
2692 if (headOff
< uio
->uio_offset
) {
2693 zero_cnt
= uio
->uio_offset
- headOff
;
2696 } else if (headOff
< newEOF
) {
2697 zero_cnt
= newEOF
- headOff
;
2701 if (uio
&& uio
->uio_offset
> oldEOF
) {
2702 zero_off
= uio
->uio_offset
& ~PAGE_MASK_64
;
2704 if (zero_off
>= oldEOF
) {
2705 zero_cnt
= uio
->uio_offset
- zero_off
;
2707 flags
|= IO_HEADZEROFILL
;
2711 if (flags
& IO_TAILZEROFILL
) {
2713 zero_off1
= uio
->uio_offset
+ io_req_size
;
2715 if (zero_off1
< tailOff
)
2716 zero_cnt1
= tailOff
- zero_off1
;
2719 if (uio
&& newEOF
> oldEOF
) {
2720 zero_off1
= uio
->uio_offset
+ io_req_size
;
2722 if (zero_off1
== newEOF
&& (zero_off1
& PAGE_MASK_64
)) {
2723 zero_cnt1
= PAGE_SIZE_64
- (zero_off1
& PAGE_MASK_64
);
2725 flags
|= IO_TAILZEROFILL
;
2729 if (zero_cnt
== 0 && uio
== (struct uio
*) 0) {
2730 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
2731 retval
, 0, 0, 0, 0);
2735 write_off
= uio
->uio_offset
;
2736 write_cnt
= uio_resid(uio
);
2738 * delay updating the sequential write info
2739 * in the control block until we've obtained
2744 while ((total_size
= (io_resid
+ zero_cnt
+ zero_cnt1
)) && retval
== 0) {
2746 * for this iteration of the loop, figure out where our starting point is
2749 start_offset
= (int)(zero_off
& PAGE_MASK_64
);
2750 upl_f_offset
= zero_off
- start_offset
;
2751 } else if (io_resid
) {
2752 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2753 upl_f_offset
= uio
->uio_offset
- start_offset
;
2755 start_offset
= (int)(zero_off1
& PAGE_MASK_64
);
2756 upl_f_offset
= zero_off1
- start_offset
;
2758 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 46)) | DBG_FUNC_NONE
,
2759 (int)zero_off
, (int)zero_cnt
, (int)zero_off1
, (int)zero_cnt1
, 0);
2761 if (total_size
> max_io_size
)
2762 total_size
= max_io_size
;
2764 cl
.b_addr
= (daddr64_t
)(upl_f_offset
/ PAGE_SIZE_64
);
2766 if (uio
&& ((flags
& (IO_SYNC
| IO_HEADZEROFILL
| IO_TAILZEROFILL
)) == 0)) {
2768 * assumption... total_size <= io_resid
2769 * because IO_HEADZEROFILL and IO_TAILZEROFILL not set
2771 if ((start_offset
+ total_size
) > max_io_size
)
2772 total_size
= max_io_size
- start_offset
;
2773 xfer_resid
= total_size
;
2775 retval
= cluster_copy_ubc_data_internal(vp
, uio
, &xfer_resid
, 1, 1);
2780 io_resid
-= (total_size
- xfer_resid
);
2781 total_size
= xfer_resid
;
2782 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2783 upl_f_offset
= uio
->uio_offset
- start_offset
;
2785 if (total_size
== 0) {
2788 * the write did not finish on a page boundary
2789 * which will leave upl_f_offset pointing to the
2790 * beginning of the last page written instead of
2791 * the page beyond it... bump it in this case
2792 * so that the cluster code records the last page
2795 upl_f_offset
+= PAGE_SIZE_64
;
2803 * compute the size of the upl needed to encompass
2804 * the requested write... limit each call to cluster_io
2805 * to the maximum UPL size... cluster_io will clip if
2806 * this exceeds the maximum io_size for the device,
2807 * make sure to account for
2808 * a starting offset that's not page aligned
2810 upl_size
= (start_offset
+ total_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2812 if (upl_size
> max_io_size
)
2813 upl_size
= max_io_size
;
2815 pages_in_upl
= upl_size
/ PAGE_SIZE
;
2816 io_size
= upl_size
- start_offset
;
2818 if ((long long)io_size
> total_size
)
2819 io_size
= total_size
;
2821 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, io_size
, total_size
, 0, 0);
2825 * Gather the pages from the buffer cache.
2826 * The UPL_WILL_MODIFY flag lets the UPL subsystem know
2827 * that we intend to modify these pages.
2829 kret
= ubc_create_upl(vp
,
2834 UPL_SET_LITE
| (( uio
!=NULL
&& (uio
->uio_flags
& UIO_FLAGS_IS_COMPRESSED_FILE
)) ? 0 : UPL_WILL_MODIFY
));
2835 if (kret
!= KERN_SUCCESS
)
2836 panic("cluster_write_copy: failed to get pagelist");
2838 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
,
2839 upl
, (int)upl_f_offset
, start_offset
, 0, 0);
2841 if (start_offset
&& upl_f_offset
< oldEOF
&& !upl_valid_page(pl
, 0)) {
2845 * we're starting in the middle of the first page of the upl
2846 * and the page isn't currently valid, so we're going to have
2847 * to read it in first... this is a synchronous operation
2849 read_size
= PAGE_SIZE
;
2851 if ((upl_f_offset
+ read_size
) > oldEOF
)
2852 read_size
= oldEOF
- upl_f_offset
;
2854 retval
= cluster_io(vp
, upl
, 0, upl_f_offset
, read_size
,
2855 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
2858 * we had an error during the read which causes us to abort
2859 * the current cluster_write request... before we do, we need
2860 * to release the rest of the pages in the upl without modifying
2861 * there state and mark the failed page in error
2863 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
2865 if (upl_size
> PAGE_SIZE
)
2866 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2868 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2869 upl
, 0, 0, retval
, 0);
2873 if ((start_offset
== 0 || upl_size
> PAGE_SIZE
) && ((start_offset
+ io_size
) & PAGE_MASK
)) {
2875 * the last offset we're writing to in this upl does not end on a page
2876 * boundary... if it's not beyond the old EOF, then we'll also need to
2877 * pre-read this page in if it isn't already valid
2879 upl_offset
= upl_size
- PAGE_SIZE
;
2881 if ((upl_f_offset
+ start_offset
+ io_size
) < oldEOF
&&
2882 !upl_valid_page(pl
, upl_offset
/ PAGE_SIZE
)) {
2885 read_size
= PAGE_SIZE
;
2887 if ((off_t
)(upl_f_offset
+ upl_offset
+ read_size
) > oldEOF
)
2888 read_size
= oldEOF
- (upl_f_offset
+ upl_offset
);
2890 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, read_size
,
2891 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
2894 * we had an error during the read which causes us to abort
2895 * the current cluster_write request... before we do, we
2896 * need to release the rest of the pages in the upl without
2897 * modifying there state and mark the failed page in error
2899 ubc_upl_abort_range(upl
, upl_offset
, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
2901 if (upl_size
> PAGE_SIZE
)
2902 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2904 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2905 upl
, 0, 0, retval
, 0);
2910 xfer_resid
= io_size
;
2911 io_offset
= start_offset
;
2913 while (zero_cnt
&& xfer_resid
) {
2915 if (zero_cnt
< (long long)xfer_resid
)
2916 bytes_to_zero
= zero_cnt
;
2918 bytes_to_zero
= xfer_resid
;
2920 bytes_to_zero
= cluster_zero_range(upl
, pl
, flags
, io_offset
, zero_off
, upl_f_offset
, bytes_to_zero
);
2922 xfer_resid
-= bytes_to_zero
;
2923 zero_cnt
-= bytes_to_zero
;
2924 zero_off
+= bytes_to_zero
;
2925 io_offset
+= bytes_to_zero
;
2927 if (xfer_resid
&& io_resid
) {
2928 u_int32_t io_requested
;
2930 bytes_to_move
= min(io_resid
, xfer_resid
);
2931 io_requested
= bytes_to_move
;
2933 retval
= cluster_copy_upl_data(uio
, upl
, io_offset
, (int *)&io_requested
);
2936 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
2938 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2939 upl
, 0, 0, retval
, 0);
2941 io_resid
-= bytes_to_move
;
2942 xfer_resid
-= bytes_to_move
;
2943 io_offset
+= bytes_to_move
;
2946 while (xfer_resid
&& zero_cnt1
&& retval
== 0) {
2948 if (zero_cnt1
< (long long)xfer_resid
)
2949 bytes_to_zero
= zero_cnt1
;
2951 bytes_to_zero
= xfer_resid
;
2953 bytes_to_zero
= cluster_zero_range(upl
, pl
, flags
, io_offset
, zero_off1
, upl_f_offset
, bytes_to_zero
);
2955 xfer_resid
-= bytes_to_zero
;
2956 zero_cnt1
-= bytes_to_zero
;
2957 zero_off1
+= bytes_to_zero
;
2958 io_offset
+= bytes_to_zero
;
2962 int ret_cluster_try_push
;
2964 io_size
+= start_offset
;
2966 if ((upl_f_offset
+ io_size
) >= newEOF
&& (u_int
)io_size
< upl_size
) {
2968 * if we're extending the file with this write
2969 * we'll zero fill the rest of the page so that
2970 * if the file gets extended again in such a way as to leave a
2971 * hole starting at this EOF, we'll have zero's in the correct spot
2973 cluster_zero(upl
, io_size
, upl_size
- io_size
, NULL
);
2976 * release the upl now if we hold one since...
2977 * 1) pages in it may be present in the sparse cluster map
2978 * and may span 2 separate buckets there... if they do and
2979 * we happen to have to flush a bucket to make room and it intersects
2980 * this upl, a deadlock may result on page BUSY
2981 * 2) we're delaying the I/O... from this point forward we're just updating
2982 * the cluster state... no need to hold the pages, so commit them
2983 * 3) IO_SYNC is set...
2984 * because we had to ask for a UPL that provides currenty non-present pages, the
2985 * UPL has been automatically set to clear the dirty flags (both software and hardware)
2986 * upon committing it... this is not the behavior we want since it's possible for
2987 * pages currently present as part of a mapped file to be dirtied while the I/O is in flight.
2988 * we'll pick these pages back up later with the correct behavior specified.
2989 * 4) we don't want to hold pages busy in a UPL and then block on the cluster lock... if a flush
2990 * of this vnode is in progress, we will deadlock if the pages being flushed intersect the pages
2991 * we hold since the flushing context is holding the cluster lock.
2993 ubc_upl_commit_range(upl
, 0, upl_size
,
2994 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2997 * calculate the last logical block number
2998 * that this delayed I/O encompassed
3000 cl
.e_addr
= (daddr64_t
)((upl_f_offset
+ (off_t
)upl_size
) / PAGE_SIZE_64
);
3002 if (flags
& IO_SYNC
) {
3004 * if the IO_SYNC flag is set than we need to
3005 * bypass any clusters and immediately issue
3011 * take the lock to protect our accesses
3012 * of the writebehind and sparse cluster state
3014 wbp
= cluster_get_wbp(vp
, CLW_ALLOCATE
| CLW_RETURNLOCKED
);
3016 if (wbp
->cl_scmap
) {
3018 if ( !(flags
& IO_NOCACHE
)) {
3020 * we've fallen into the sparse
3021 * cluster method of delaying dirty pages
3023 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, newEOF
, callback
, callback_arg
);
3025 lck_mtx_unlock(&wbp
->cl_lockw
);
3030 * must have done cached writes that fell into
3031 * the sparse cluster mechanism... we've switched
3032 * to uncached writes on the file, so go ahead
3033 * and push whatever's in the sparse map
3034 * and switch back to normal clustering
3038 sparse_cluster_push(&(wbp
->cl_scmap
), vp
, newEOF
, PUSH_ALL
, 0, callback
, callback_arg
);
3040 * no clusters of either type present at this point
3041 * so just go directly to start_new_cluster since
3042 * we know we need to delay this I/O since we've
3043 * already released the pages back into the cache
3044 * to avoid the deadlock with sparse_cluster_push
3046 goto start_new_cluster
;
3049 if (write_off
== wbp
->cl_last_write
)
3050 wbp
->cl_seq_written
+= write_cnt
;
3052 wbp
->cl_seq_written
= write_cnt
;
3054 wbp
->cl_last_write
= write_off
+ write_cnt
;
3058 if (wbp
->cl_number
== 0)
3060 * no clusters currently present
3062 goto start_new_cluster
;
3064 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
3066 * check each cluster that we currently hold
3067 * try to merge some or all of this write into
3068 * one or more of the existing clusters... if
3069 * any portion of the write remains, start a
3072 if (cl
.b_addr
>= wbp
->cl_clusters
[cl_index
].b_addr
) {
3074 * the current write starts at or after the current cluster
3076 if (cl
.e_addr
<= (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
3078 * we have a write that fits entirely
3079 * within the existing cluster limits
3081 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
)
3083 * update our idea of where the cluster ends
3085 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
3088 if (cl
.b_addr
< (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
3090 * we have a write that starts in the middle of the current cluster
3091 * but extends beyond the cluster's limit... we know this because
3092 * of the previous checks
3093 * we'll extend the current cluster to the max
3094 * and update the b_addr for the current write to reflect that
3095 * the head of it was absorbed into this cluster...
3096 * note that we'll always have a leftover tail in this case since
3097 * full absorbtion would have occurred in the clause above
3099 wbp
->cl_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
;
3101 cl
.b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
;
3104 * we come here for the case where the current write starts
3105 * beyond the limit of the existing cluster or we have a leftover
3106 * tail after a partial absorbtion
3108 * in either case, we'll check the remaining clusters before
3109 * starting a new one
3113 * the current write starts in front of the cluster we're currently considering
3115 if ((wbp
->cl_clusters
[cl_index
].e_addr
- cl
.b_addr
) <= max_cluster_pgcount
) {
3117 * we can just merge the new request into
3118 * this cluster and leave it in the cache
3119 * since the resulting cluster is still
3120 * less than the maximum allowable size
3122 wbp
->cl_clusters
[cl_index
].b_addr
= cl
.b_addr
;
3124 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
) {
3126 * the current write completely
3127 * envelops the existing cluster and since
3128 * each write is limited to at most max_cluster_pgcount pages
3129 * we can just use the start and last blocknos of the write
3130 * to generate the cluster limits
3132 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
3138 * if we were to combine this write with the current cluster
3139 * we would exceed the cluster size limit.... so,
3140 * let's see if there's any overlap of the new I/O with
3141 * the cluster we're currently considering... in fact, we'll
3142 * stretch the cluster out to it's full limit and see if we
3143 * get an intersection with the current write
3146 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
) {
3148 * the current write extends into the proposed cluster
3149 * clip the length of the current write after first combining it's
3150 * tail with the newly shaped cluster
3152 wbp
->cl_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
;
3154 cl
.e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
;
3157 * if we get here, there was no way to merge
3158 * any portion of this write with this cluster
3159 * or we could only merge part of it which
3160 * will leave a tail...
3161 * we'll check the remaining clusters before starting a new one
3165 if (cl_index
< wbp
->cl_number
)
3167 * we found an existing cluster(s) that we
3168 * could entirely merge this I/O into
3172 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) &&
3173 wbp
->cl_number
== MAX_CLUSTERS
&&
3174 wbp
->cl_seq_written
>= (MAX_CLUSTERS
* (max_cluster_pgcount
* PAGE_SIZE
))) {
3177 if (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
)
3178 n
= WRITE_BEHIND_SSD
;
3183 cluster_try_push(wbp
, vp
, newEOF
, 0, 0, callback
, callback_arg
);
3185 if (wbp
->cl_number
< MAX_CLUSTERS
) {
3187 * we didn't find an existing cluster to
3188 * merge into, but there's room to start
3191 goto start_new_cluster
;
3194 * no exisitng cluster to merge with and no
3195 * room to start a new one... we'll try
3196 * pushing one of the existing ones... if none of
3197 * them are able to be pushed, we'll switch
3198 * to the sparse cluster mechanism
3199 * cluster_try_push updates cl_number to the
3200 * number of remaining clusters... and
3201 * returns the number of currently unused clusters
3203 ret_cluster_try_push
= 0;
3206 * if writes are not deferred, call cluster push immediately
3208 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
)) {
3210 ret_cluster_try_push
= cluster_try_push(wbp
, vp
, newEOF
, (flags
& IO_NOCACHE
) ? 0 : PUSH_DELAY
, 0, callback
, callback_arg
);
3214 * execute following regardless of writes being deferred or not
3216 if (ret_cluster_try_push
== 0) {
3218 * no more room in the normal cluster mechanism
3219 * so let's switch to the more expansive but expensive
3220 * sparse mechanism....
3222 sparse_cluster_switch(wbp
, vp
, newEOF
, callback
, callback_arg
);
3223 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, newEOF
, callback
, callback_arg
);
3225 lck_mtx_unlock(&wbp
->cl_lockw
);
3230 wbp
->cl_clusters
[wbp
->cl_number
].b_addr
= cl
.b_addr
;
3231 wbp
->cl_clusters
[wbp
->cl_number
].e_addr
= cl
.e_addr
;
3233 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
= 0;
3235 if (flags
& IO_NOCACHE
)
3236 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IONOCACHE
;
3238 if (bflag
& CL_PASSIVE
)
3239 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IOPASSIVE
;
3243 lck_mtx_unlock(&wbp
->cl_lockw
);
3248 * we don't hold the lock at this point
3250 * we've already dropped the current upl, so pick it back up with COPYOUT_FROM set
3251 * so that we correctly deal with a change in state of the hardware modify bit...
3252 * we do this via cluster_push_now... by passing along the IO_SYNC flag, we force
3253 * cluster_push_now to wait until all the I/Os have completed... cluster_push_now is also
3254 * responsible for generating the correct sized I/O(s)
3256 retval
= cluster_push_now(vp
, &cl
, newEOF
, flags
, callback
, callback_arg
);
3259 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
, retval
, 0, io_resid
, 0, 0);
3267 cluster_read(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
)
3269 return cluster_read_ext(vp
, uio
, filesize
, xflags
, NULL
, NULL
);
3274 cluster_read_ext(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
3278 user_ssize_t cur_resid
;
3280 u_int32_t read_length
= 0;
3281 int read_type
= IO_COPY
;
3285 if (vp
->v_flag
& VNOCACHE_DATA
)
3286 flags
|= IO_NOCACHE
;
3287 if ((vp
->v_flag
& VRAOFF
) || speculative_reads_disabled
)
3291 * do a read through the cache if one of the following is true....
3292 * NOCACHE is not true
3293 * the uio request doesn't target USERSPACE
3294 * otherwise, find out if we want the direct or contig variant for
3295 * the first vector in the uio request
3297 if ( (flags
& IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) )
3298 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3300 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< filesize
&& retval
== 0) {
3302 switch (read_type
) {
3306 * make sure the uio_resid isn't too big...
3307 * internally, we want to handle all of the I/O in
3308 * chunk sizes that fit in a 32 bit int
3310 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
))
3311 io_size
= MAX_IO_REQUEST_SIZE
;
3313 io_size
= (u_int32_t
)cur_resid
;
3315 retval
= cluster_read_copy(vp
, uio
, io_size
, filesize
, flags
, callback
, callback_arg
);
3319 retval
= cluster_read_direct(vp
, uio
, filesize
, &read_type
, &read_length
, flags
, callback
, callback_arg
);
3323 retval
= cluster_read_contig(vp
, uio
, filesize
, &read_type
, &read_length
, callback
, callback_arg
, flags
);
3327 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3337 cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int take_reference
)
3340 int abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
3342 if ((range
= last_pg
- start_pg
)) {
3344 abort_flags
|= UPL_ABORT_REFERENCE
;
3346 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, range
* PAGE_SIZE
, abort_flags
);
3352 cluster_read_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
3354 upl_page_info_t
*pl
;
3356 vm_offset_t upl_offset
;
3365 off_t last_ioread_offset
;
3366 off_t last_request_offset
;
3370 u_int32_t size_of_prefetch
;
3373 u_int32_t max_rd_size
;
3374 u_int32_t max_io_size
;
3375 u_int32_t max_prefetch
;
3376 u_int rd_ahead_enabled
= 1;
3377 u_int prefetch_enabled
= 1;
3378 struct cl_readahead
* rap
;
3379 struct clios iostate
;
3380 struct cl_extent extent
;
3382 int take_reference
= 1;
3385 #endif /* CONFIG_EMBEDDED */
3386 int policy
= IOPOL_DEFAULT
;
3387 boolean_t iolock_inited
= FALSE
;
3389 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_START
,
3390 (int)uio
->uio_offset
, io_req_size
, (int)filesize
, flags
, 0);
3392 #if !CONFIG_EMBEDDED
3393 policy
= proc_get_task_selfdiskacc();
3394 #else /* !CONFIG_EMBEDDED */
3395 policy
= current_proc()->p_iopol_disk
;
3397 ut
= get_bsdthread_info(current_thread());
3399 if (ut
->uu_iopol_disk
!= IOPOL_DEFAULT
)
3400 policy
= ut
->uu_iopol_disk
;
3401 #endif /* !CONFIG_EMBEDDED */
3403 if (policy
== IOPOL_THROTTLE
|| (flags
& IO_NOCACHE
))
3406 if (flags
& IO_PASSIVE
)
3411 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
3412 max_prefetch
= MAX_PREFETCH(vp
, max_io_size
, (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
));
3413 max_rd_size
= max_prefetch
;
3415 last_request_offset
= uio
->uio_offset
+ io_req_size
;
3417 if (last_request_offset
> filesize
)
3418 last_request_offset
= filesize
;
3420 if ((flags
& (IO_RAOFF
|IO_NOCACHE
)) || ((last_request_offset
& ~PAGE_MASK_64
) == (uio
->uio_offset
& ~PAGE_MASK_64
))) {
3421 rd_ahead_enabled
= 0;
3424 if (cluster_hard_throttle_on(vp
, 1)) {
3425 rd_ahead_enabled
= 0;
3426 prefetch_enabled
= 0;
3428 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
3429 } else if (policy
== IOPOL_THROTTLE
) {
3430 rd_ahead_enabled
= 0;
3431 prefetch_enabled
= 0;
3433 if ((rap
= cluster_get_rap(vp
)) == NULL
)
3434 rd_ahead_enabled
= 0;
3436 extent
.b_addr
= uio
->uio_offset
/ PAGE_SIZE_64
;
3437 extent
.e_addr
= (last_request_offset
- 1) / PAGE_SIZE_64
;
3440 if (rap
!= NULL
&& rap
->cl_ralen
&& (rap
->cl_lastr
== extent
.b_addr
|| (rap
->cl_lastr
+ 1) == extent
.b_addr
)) {
3442 * determine if we already have a read-ahead in the pipe courtesy of the
3443 * last read systemcall that was issued...
3444 * if so, pick up it's extent to determine where we should start
3445 * with respect to any read-ahead that might be necessary to
3446 * garner all the data needed to complete this read systemcall
3448 last_ioread_offset
= (rap
->cl_maxra
* PAGE_SIZE_64
) + PAGE_SIZE_64
;
3450 if (last_ioread_offset
< uio
->uio_offset
)
3451 last_ioread_offset
= (off_t
)0;
3452 else if (last_ioread_offset
> last_request_offset
)
3453 last_ioread_offset
= last_request_offset
;
3455 last_ioread_offset
= (off_t
)0;
3457 while (io_req_size
&& uio
->uio_offset
< filesize
&& retval
== 0) {
3459 max_size
= filesize
- uio
->uio_offset
;
3461 if ((off_t
)(io_req_size
) < max_size
)
3462 io_size
= io_req_size
;
3466 if (!(flags
& IO_NOCACHE
)) {
3470 u_int32_t io_requested
;
3473 * if we keep finding the pages we need already in the cache, then
3474 * don't bother to call cluster_read_prefetch since it costs CPU cycles
3475 * to determine that we have all the pages we need... once we miss in
3476 * the cache and have issued an I/O, than we'll assume that we're likely
3477 * to continue to miss in the cache and it's to our advantage to try and prefetch
3479 if (last_request_offset
&& last_ioread_offset
&& (size_of_prefetch
= (last_request_offset
- last_ioread_offset
))) {
3480 if ((last_ioread_offset
- uio
->uio_offset
) <= max_rd_size
&& prefetch_enabled
) {
3482 * we've already issued I/O for this request and
3483 * there's still work to do and
3484 * our prefetch stream is running dry, so issue a
3485 * pre-fetch I/O... the I/O latency will overlap
3486 * with the copying of the data
3488 if (size_of_prefetch
> max_rd_size
)
3489 size_of_prefetch
= max_rd_size
;
3491 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
3493 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
3495 if (last_ioread_offset
> last_request_offset
)
3496 last_ioread_offset
= last_request_offset
;
3500 * limit the size of the copy we're about to do so that
3501 * we can notice that our I/O pipe is running dry and
3502 * get the next I/O issued before it does go dry
3504 if (last_ioread_offset
&& io_size
> (max_io_size
/ 4))
3505 io_resid
= (max_io_size
/ 4);
3509 io_requested
= io_resid
;
3511 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_resid
, 0, take_reference
);
3513 xsize
= io_requested
- io_resid
;
3516 io_req_size
-= xsize
;
3518 if (retval
|| io_resid
)
3520 * if we run into a real error or
3521 * a page that is not in the cache
3522 * we need to leave streaming mode
3526 if (rd_ahead_enabled
&& (io_size
== 0 || last_ioread_offset
== last_request_offset
)) {
3528 * we're already finished the I/O for this read request
3529 * let's see if we should do a read-ahead
3531 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
3538 if (extent
.e_addr
< rap
->cl_lastr
)
3540 rap
->cl_lastr
= extent
.e_addr
;
3545 * recompute max_size since cluster_copy_ubc_data_internal
3546 * may have advanced uio->uio_offset
3548 max_size
= filesize
- uio
->uio_offset
;
3551 * compute the size of the upl needed to encompass
3552 * the requested read... limit each call to cluster_io
3553 * to the maximum UPL size... cluster_io will clip if
3554 * this exceeds the maximum io_size for the device,
3555 * make sure to account for
3556 * a starting offset that's not page aligned
3558 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3559 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
3561 if (io_size
> max_rd_size
)
3562 io_size
= max_rd_size
;
3564 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
3566 if (flags
& IO_NOCACHE
) {
3567 if (upl_size
> max_io_size
)
3568 upl_size
= max_io_size
;
3570 if (upl_size
> max_io_size
/ 4)
3571 upl_size
= max_io_size
/ 4;
3573 pages_in_upl
= upl_size
/ PAGE_SIZE
;
3575 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_START
,
3576 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3578 kret
= ubc_create_upl(vp
,
3583 UPL_FILE_IO
| UPL_SET_LITE
);
3584 if (kret
!= KERN_SUCCESS
)
3585 panic("cluster_read_copy: failed to get pagelist");
3587 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_END
,
3588 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3591 * scan from the beginning of the upl looking for the first
3592 * non-valid page.... this will become the first page in
3593 * the request we're going to make to 'cluster_io'... if all
3594 * of the pages are valid, we won't call through to 'cluster_io'
3596 for (start_pg
= 0; start_pg
< pages_in_upl
; start_pg
++) {
3597 if (!upl_valid_page(pl
, start_pg
))
3602 * scan from the starting invalid page looking for a valid
3603 * page before the end of the upl is reached, if we
3604 * find one, then it will be the last page of the request to
3607 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
3608 if (upl_valid_page(pl
, last_pg
))
3611 iostate
.io_completed
= 0;
3612 iostate
.io_issued
= 0;
3613 iostate
.io_error
= 0;
3614 iostate
.io_wanted
= 0;
3616 if (start_pg
< last_pg
) {
3618 * we found a range of 'invalid' pages that must be filled
3619 * if the last page in this range is the last page of the file
3620 * we may have to clip the size of it to keep from reading past
3621 * the end of the last physical block associated with the file
3623 if (iolock_inited
== FALSE
) {
3624 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
3626 iolock_inited
= TRUE
;
3628 upl_offset
= start_pg
* PAGE_SIZE
;
3629 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
3631 if ((off_t
)(upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
3632 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
3635 * issue an asynchronous read to cluster_io
3638 error
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
,
3639 io_size
, CL_READ
| CL_ASYNC
| bflag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
3642 if (extent
.e_addr
< rap
->cl_maxra
) {
3644 * we've just issued a read for a block that should have been
3645 * in the cache courtesy of the read-ahead engine... something
3646 * has gone wrong with the pipeline, so reset the read-ahead
3647 * logic which will cause us to restart from scratch
3655 * if the read completed successfully, or there was no I/O request
3656 * issued, than copy the data into user land via 'cluster_upl_copy_data'
3657 * we'll first add on any 'valid'
3658 * pages that were present in the upl when we acquired it.
3662 for (uio_last
= last_pg
; uio_last
< pages_in_upl
; uio_last
++) {
3663 if (!upl_valid_page(pl
, uio_last
))
3666 if (uio_last
< pages_in_upl
) {
3668 * there were some invalid pages beyond the valid pages
3669 * that we didn't issue an I/O for, just release them
3670 * unchanged now, so that any prefetch/readahed can
3673 ubc_upl_abort_range(upl
, uio_last
* PAGE_SIZE
,
3674 (pages_in_upl
- uio_last
) * PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
3678 * compute size to transfer this round, if io_req_size is
3679 * still non-zero after this attempt, we'll loop around and
3680 * set up for another I/O.
3682 val_size
= (uio_last
* PAGE_SIZE
) - start_offset
;
3684 if (val_size
> max_size
)
3685 val_size
= max_size
;
3687 if (val_size
> io_req_size
)
3688 val_size
= io_req_size
;
3690 if ((uio
->uio_offset
+ val_size
) > last_ioread_offset
)
3691 last_ioread_offset
= uio
->uio_offset
+ val_size
;
3693 if ((size_of_prefetch
= (last_request_offset
- last_ioread_offset
)) && prefetch_enabled
) {
3695 if ((last_ioread_offset
- (uio
->uio_offset
+ val_size
)) <= upl_size
) {
3697 * if there's still I/O left to do for this request, and...
3698 * we're not in hard throttle mode, and...
3699 * we're close to using up the previous prefetch, then issue a
3700 * new pre-fetch I/O... the I/O latency will overlap
3701 * with the copying of the data
3703 if (size_of_prefetch
> max_rd_size
)
3704 size_of_prefetch
= max_rd_size
;
3706 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
3708 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
3710 if (last_ioread_offset
> last_request_offset
)
3711 last_ioread_offset
= last_request_offset
;
3714 } else if ((uio
->uio_offset
+ val_size
) == last_request_offset
) {
3716 * this transfer will finish this request, so...
3717 * let's try to read ahead if we're in
3718 * a sequential access pattern and we haven't
3719 * explicitly disabled it
3721 if (rd_ahead_enabled
)
3722 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
3725 if (extent
.e_addr
< rap
->cl_lastr
)
3727 rap
->cl_lastr
= extent
.e_addr
;
3730 if (iostate
.io_issued
> iostate
.io_completed
)
3731 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
3733 if (iostate
.io_error
)
3734 error
= iostate
.io_error
;
3736 u_int32_t io_requested
;
3738 io_requested
= val_size
;
3740 retval
= cluster_copy_upl_data(uio
, upl
, start_offset
, (int *)&io_requested
);
3742 io_req_size
-= (val_size
- io_requested
);
3745 if (iostate
.io_issued
> iostate
.io_completed
)
3746 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
3748 if (start_pg
< last_pg
) {
3750 * compute the range of pages that we actually issued an I/O for
3751 * and either commit them as valid if the I/O succeeded
3752 * or abort them if the I/O failed or we're not supposed to
3753 * keep them in the cache
3755 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
3757 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
, upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
3759 if (error
|| (flags
& IO_NOCACHE
))
3760 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
3761 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3763 int commit_flags
= UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
;
3766 commit_flags
|= UPL_COMMIT_INACTIVATE
;
3768 commit_flags
|= UPL_COMMIT_SPECULATE
;
3770 ubc_upl_commit_range(upl
, start_pg
* PAGE_SIZE
, io_size
, commit_flags
);
3772 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
3774 if ((last_pg
- start_pg
) < pages_in_upl
) {
3776 * the set of pages that we issued an I/O for did not encompass
3777 * the entire upl... so just release these without modifying
3781 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3784 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
3785 upl
, -1, pages_in_upl
- (last_pg
- start_pg
), 0, 0);
3788 * handle any valid pages at the beginning of
3789 * the upl... release these appropriately
3791 cluster_read_upl_release(upl
, 0, start_pg
, take_reference
);
3794 * handle any valid pages immediately after the
3795 * pages we issued I/O for... ... release these appropriately
3797 cluster_read_upl_release(upl
, last_pg
, uio_last
, take_reference
);
3799 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, upl
, -1, -1, 0, 0);
3806 if (cluster_hard_throttle_on(vp
, 1)) {
3807 rd_ahead_enabled
= 0;
3808 prefetch_enabled
= 0;
3810 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
3812 if (max_rd_size
== HARD_THROTTLE_MAXSIZE
) {
3814 * coming out of throttled state
3816 if (policy
!= IOPOL_THROTTLE
) {
3818 rd_ahead_enabled
= 1;
3819 prefetch_enabled
= 1;
3821 max_rd_size
= max_prefetch
;
3822 last_ioread_offset
= 0;
3827 if (iolock_inited
== TRUE
) {
3828 if (iostate
.io_issued
> iostate
.io_completed
) {
3830 * cluster_io returned an error after it
3831 * had already issued some I/O. we need
3832 * to wait for that I/O to complete before
3833 * we can destroy the iostate mutex...
3834 * 'retval' already contains the early error
3835 * so no need to pick it up from iostate.io_error
3837 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
3839 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
3842 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
3843 (int)uio
->uio_offset
, io_req_size
, rap
->cl_lastr
, retval
, 0);
3845 lck_mtx_unlock(&rap
->cl_lockr
);
3847 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
3848 (int)uio
->uio_offset
, io_req_size
, 0, retval
, 0);
3856 cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
3857 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
3860 upl_page_info_t
*pl
;
3862 vm_offset_t upl_offset
, vector_upl_offset
= 0;
3863 upl_size_t upl_size
, vector_upl_size
= 0;
3864 vm_size_t upl_needed_size
;
3865 unsigned int pages_in_pl
;
3869 int force_data_sync
;
3871 int no_zero_fill
= 0;
3874 struct clios iostate
;
3875 user_addr_t iov_base
;
3876 u_int32_t io_req_size
;
3877 u_int32_t offset_in_file
;
3878 u_int32_t offset_in_iovbase
;
3882 u_int32_t devblocksize
;
3883 u_int32_t mem_alignment_mask
;
3884 u_int32_t max_upl_size
;
3885 u_int32_t max_rd_size
;
3886 u_int32_t max_rd_ahead
;
3887 boolean_t strict_uncached_IO
= FALSE
;
3889 u_int32_t vector_upl_iosize
= 0;
3890 int issueVectorUPL
= 0,useVectorUPL
= (uio
->uio_iovcnt
> 1);
3891 off_t v_upl_uio_offset
= 0;
3892 int vector_upl_index
=0;
3893 upl_t vector_upl
= NULL
;
3895 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_START
,
3896 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
3898 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
3900 max_rd_size
= max_upl_size
;
3901 max_rd_ahead
= max_rd_size
* IO_SCALE(vp
, 2);
3903 io_flag
= CL_COMMIT
| CL_READ
| CL_ASYNC
| CL_NOZERO
| CL_DIRECT_IO
;
3905 if (flags
& IO_PASSIVE
)
3906 io_flag
|= CL_PASSIVE
;
3908 iostate
.io_completed
= 0;
3909 iostate
.io_issued
= 0;
3910 iostate
.io_error
= 0;
3911 iostate
.io_wanted
= 0;
3913 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
3915 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
3916 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
3918 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
3919 (int)devblocksize
, (int)mem_alignment_mask
, 0, 0, 0);
3921 if (devblocksize
== 1) {
3923 * the AFP client advertises a devblocksize of 1
3924 * however, its BLOCKMAP routine maps to physical
3925 * blocks that are PAGE_SIZE in size...
3926 * therefore we can't ask for I/Os that aren't page aligned
3927 * or aren't multiples of PAGE_SIZE in size
3928 * by setting devblocksize to PAGE_SIZE, we re-instate
3929 * the old behavior we had before the mem_alignment_mask
3930 * changes went in...
3932 devblocksize
= PAGE_SIZE
;
3935 strict_uncached_IO
= ubc_strict_uncached_IO(vp
);
3938 io_req_size
= *read_length
;
3939 iov_base
= uio_curriovbase(uio
);
3941 max_io_size
= filesize
- uio
->uio_offset
;
3943 if ((off_t
)io_req_size
> max_io_size
)
3944 io_req_size
= max_io_size
;
3946 offset_in_file
= (u_int32_t
)uio
->uio_offset
& (devblocksize
- 1);
3947 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
3949 if (offset_in_file
|| offset_in_iovbase
) {
3951 * one of the 2 important offsets is misaligned
3952 * so fire an I/O through the cache for this entire vector
3956 if (iov_base
& (devblocksize
- 1)) {
3958 * the offset in memory must be on a device block boundary
3959 * so that we can guarantee that we can generate an
3960 * I/O that ends on a page boundary in cluster_io
3965 * When we get to this point, we know...
3966 * -- the offset into the file is on a devblocksize boundary
3969 while (io_req_size
&& retval
== 0) {
3972 if (cluster_hard_throttle_on(vp
, 1)) {
3973 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
3974 max_rd_ahead
= HARD_THROTTLE_MAXSIZE
- 1;
3976 max_rd_size
= max_upl_size
;
3977 max_rd_ahead
= max_rd_size
* IO_SCALE(vp
, 2);
3979 io_start
= io_size
= io_req_size
;
3982 * First look for pages already in the cache
3983 * and move them to user space.
3985 * cluster_copy_ubc_data returns the resid
3988 if (strict_uncached_IO
== FALSE
) {
3989 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_size
, 0, 0);
3992 * calculate the number of bytes actually copied
3993 * starting size - residual
3995 xsize
= io_start
- io_size
;
3997 io_req_size
-= xsize
;
3999 if(useVectorUPL
&& (xsize
|| (iov_base
& PAGE_MASK
))) {
4001 * We found something in the cache or we have an iov_base that's not
4004 * Issue all I/O's that have been collected within this Vectored UPL.
4006 if(vector_upl_index
) {
4007 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4008 reset_vector_run_state();
4015 * After this point, if we are using the Vector UPL path and the base is
4016 * not page-aligned then the UPL with that base will be the first in the vector UPL.
4021 * check to see if we are finished with this request...
4023 if (io_req_size
== 0 || misaligned
) {
4025 * see if there's another uio vector to
4026 * process that's of type IO_DIRECT
4028 * break out of while loop to get there
4033 * assume the request ends on a device block boundary
4035 io_min
= devblocksize
;
4038 * we can handle I/O's in multiples of the device block size
4039 * however, if io_size isn't a multiple of devblocksize we
4040 * want to clip it back to the nearest page boundary since
4041 * we are going to have to go through cluster_read_copy to
4042 * deal with the 'overhang'... by clipping it to a PAGE_SIZE
4043 * multiple, we avoid asking the drive for the same physical
4044 * blocks twice.. once for the partial page at the end of the
4045 * request and a 2nd time for the page we read into the cache
4046 * (which overlaps the end of the direct read) in order to
4047 * get at the overhang bytes
4049 if (io_size
& (devblocksize
- 1)) {
4051 * request does NOT end on a device block boundary
4052 * so clip it back to a PAGE_SIZE boundary
4054 io_size
&= ~PAGE_MASK
;
4057 if (retval
|| io_size
< io_min
) {
4059 * either an error or we only have the tail left to
4060 * complete via the copy path...
4061 * we may have already spun some portion of this request
4062 * off as async requests... we need to wait for the I/O
4063 * to complete before returning
4065 goto wait_for_dreads
;
4068 if (strict_uncached_IO
== FALSE
) {
4070 if ((xsize
= io_size
) > max_rd_size
)
4071 xsize
= max_rd_size
;
4075 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ xsize
, UPL_ROP_ABSENT
, (int *)&io_size
);
4079 * a page must have just come into the cache
4080 * since the first page in this range is no
4081 * longer absent, go back and re-evaluate
4087 iov_base
= uio_curriovbase(uio
);
4089 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
4090 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
4092 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_START
,
4093 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
4095 if (upl_offset
== 0 && ((io_size
& PAGE_MASK
) == 0))
4100 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
4102 upl_size
= upl_needed_size
;
4103 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
4106 upl_flags
|= UPL_NOZEROFILL
;
4107 if (force_data_sync
)
4108 upl_flags
|= UPL_FORCE_DATA_SYNC
;
4110 kret
= vm_map_create_upl(current_map(),
4111 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4112 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
);
4114 if (kret
!= KERN_SUCCESS
) {
4115 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4116 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4118 * failed to get pagelist
4120 * we may have already spun some portion of this request
4121 * off as async requests... we need to wait for the I/O
4122 * to complete before returning
4124 goto wait_for_dreads
;
4126 pages_in_pl
= upl_size
/ PAGE_SIZE
;
4127 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
4129 for (i
= 0; i
< pages_in_pl
; i
++) {
4130 if (!upl_page_present(pl
, i
))
4133 if (i
== pages_in_pl
)
4136 ubc_upl_abort(upl
, 0);
4138 if (force_data_sync
>= 3) {
4139 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4140 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4142 goto wait_for_dreads
;
4145 * Consider the possibility that upl_size wasn't satisfied.
4147 if (upl_size
< upl_needed_size
) {
4148 if (upl_size
&& upl_offset
== 0)
4154 ubc_upl_abort(upl
, 0);
4155 goto wait_for_dreads
;
4157 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4158 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4161 vm_offset_t end_off
= ((iov_base
+ io_size
) & PAGE_MASK
);
4165 * After this point, if we are using a vector UPL, then
4166 * either all the UPL elements end on a page boundary OR
4167 * this UPL is the last element because it does not end
4168 * on a page boundary.
4173 * request asynchronously so that we can overlap
4174 * the preparation of the next I/O
4175 * if there are already too many outstanding reads
4176 * wait until some have completed before issuing the next read
4178 if (iostate
.io_issued
> iostate
.io_completed
)
4179 cluster_iostate_wait(&iostate
, max_rd_ahead
, "cluster_read_direct");
4181 if (iostate
.io_error
) {
4183 * one of the earlier reads we issued ran into a hard error
4184 * don't issue any more reads, cleanup the UPL
4185 * that was just created but not used, then
4186 * go wait for any other reads to complete before
4187 * returning the error to the caller
4189 ubc_upl_abort(upl
, 0);
4191 goto wait_for_dreads
;
4193 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_START
,
4194 upl
, (int)upl_offset
, (int)uio
->uio_offset
, io_size
, 0);
4199 io_flag
&= ~CL_PRESERVE
;
4201 io_flag
|= CL_PRESERVE
;
4203 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
, io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4207 if(!vector_upl_index
) {
4208 vector_upl
= vector_upl_create(upl_offset
);
4209 v_upl_uio_offset
= uio
->uio_offset
;
4210 vector_upl_offset
= upl_offset
;
4213 vector_upl_set_subupl(vector_upl
,upl
, upl_size
);
4214 vector_upl_set_iostate(vector_upl
, upl
, vector_upl_size
, upl_size
);
4216 vector_upl_size
+= upl_size
;
4217 vector_upl_iosize
+= io_size
;
4219 if(issueVectorUPL
|| vector_upl_index
== MAX_VECTOR_UPL_ELEMENTS
|| vector_upl_size
>= MAX_VECTOR_UPL_SIZE
) {
4220 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4221 reset_vector_run_state();
4225 * update the uio structure
4227 uio_update(uio
, (user_size_t
)io_size
);
4229 io_req_size
-= io_size
;
4231 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_END
,
4232 upl
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
4236 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0 && uio
->uio_offset
< filesize
) {
4238 retval
= cluster_io_type(uio
, read_type
, read_length
, 0);
4240 if (retval
== 0 && *read_type
== IO_DIRECT
) {
4242 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
4243 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
4251 if(retval
== 0 && iostate
.io_error
== 0 && useVectorUPL
&& vector_upl_index
) {
4252 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4253 reset_vector_run_state();
4256 * make sure all async reads that are part of this stream
4257 * have completed before we return
4259 if (iostate
.io_issued
> iostate
.io_completed
)
4260 cluster_iostate_wait(&iostate
, 0, "cluster_read_direct");
4262 if (iostate
.io_error
)
4263 retval
= iostate
.io_error
;
4265 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
4267 if (io_req_size
&& retval
== 0) {
4269 * we couldn't handle the tail of this request in DIRECT mode
4270 * so fire it through the copy path
4272 retval
= cluster_read_copy(vp
, uio
, io_req_size
, filesize
, flags
, callback
, callback_arg
);
4274 *read_type
= IO_UNKNOWN
;
4276 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
4277 (int)uio
->uio_offset
, (int)uio_resid(uio
), io_req_size
, retval
, 0);
4284 cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
4285 int (*callback
)(buf_t
, void *), void *callback_arg
, int flags
)
4287 upl_page_info_t
*pl
;
4288 upl_t upl
[MAX_VECTS
];
4289 vm_offset_t upl_offset
;
4290 addr64_t dst_paddr
= 0;
4291 user_addr_t iov_base
;
4293 upl_size_t upl_size
;
4294 vm_size_t upl_needed_size
;
4295 mach_msg_type_number_t pages_in_pl
;
4298 struct clios iostate
;
4305 u_int32_t devblocksize
;
4306 u_int32_t mem_alignment_mask
;
4307 u_int32_t tail_size
= 0;
4310 if (flags
& IO_PASSIVE
)
4316 * When we enter this routine, we know
4317 * -- the read_length will not exceed the current iov_len
4318 * -- the target address is physically contiguous for read_length
4320 cluster_syncup(vp
, filesize
, callback
, callback_arg
);
4322 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
4323 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
4325 iostate
.io_completed
= 0;
4326 iostate
.io_issued
= 0;
4327 iostate
.io_error
= 0;
4328 iostate
.io_wanted
= 0;
4330 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
4333 io_size
= *read_length
;
4335 max_size
= filesize
- uio
->uio_offset
;
4337 if (io_size
> max_size
)
4340 iov_base
= uio_curriovbase(uio
);
4342 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
4343 upl_needed_size
= upl_offset
+ io_size
;
4346 upl_size
= upl_needed_size
;
4347 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
4350 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_START
,
4351 (int)upl_offset
, (int)upl_size
, (int)iov_base
, io_size
, 0);
4353 kret
= vm_map_get_upl(current_map(),
4354 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4355 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, 0);
4357 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_END
,
4358 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4360 if (kret
!= KERN_SUCCESS
) {
4362 * failed to get pagelist
4365 goto wait_for_creads
;
4369 if (upl_size
< upl_needed_size
) {
4371 * The upl_size wasn't satisfied.
4374 goto wait_for_creads
;
4376 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
4378 dst_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)upl_offset
;
4380 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
4381 u_int32_t head_size
;
4383 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
4385 if (head_size
> io_size
)
4386 head_size
= io_size
;
4388 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, head_size
, CL_READ
, callback
, callback_arg
);
4391 goto wait_for_creads
;
4393 upl_offset
+= head_size
;
4394 dst_paddr
+= head_size
;
4395 io_size
-= head_size
;
4397 iov_base
+= head_size
;
4399 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
4401 * request doesn't set up on a memory boundary
4402 * the underlying DMA engine can handle...
4403 * return an error instead of going through
4404 * the slow copy path since the intent of this
4405 * path is direct I/O to device memory
4408 goto wait_for_creads
;
4411 tail_size
= io_size
& (devblocksize
- 1);
4413 io_size
-= tail_size
;
4415 while (io_size
&& error
== 0) {
4417 if (io_size
> MAX_IO_CONTIG_SIZE
)
4418 xsize
= MAX_IO_CONTIG_SIZE
;
4422 * request asynchronously so that we can overlap
4423 * the preparation of the next I/O... we'll do
4424 * the commit after all the I/O has completed
4425 * since its all issued against the same UPL
4426 * if there are already too many outstanding reads
4427 * wait until some have completed before issuing the next
4429 if (iostate
.io_issued
> iostate
.io_completed
)
4430 cluster_iostate_wait(&iostate
, MAX_IO_CONTIG_SIZE
* IO_SCALE(vp
, 2), "cluster_read_contig");
4432 if (iostate
.io_error
) {
4434 * one of the earlier reads we issued ran into a hard error
4435 * don't issue any more reads...
4436 * go wait for any other reads to complete before
4437 * returning the error to the caller
4439 goto wait_for_creads
;
4441 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
, xsize
,
4442 CL_READ
| CL_NOZERO
| CL_DEV_MEMORY
| CL_ASYNC
| bflag
,
4443 (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4445 * The cluster_io read was issued successfully,
4446 * update the uio structure
4449 uio_update(uio
, (user_size_t
)xsize
);
4452 upl_offset
+= xsize
;
4456 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
&& uio
->uio_offset
< filesize
) {
4458 error
= cluster_io_type(uio
, read_type
, read_length
, 0);
4460 if (error
== 0 && *read_type
== IO_CONTIG
) {
4465 *read_type
= IO_UNKNOWN
;
4469 * make sure all async reads that are part of this stream
4470 * have completed before we proceed
4472 if (iostate
.io_issued
> iostate
.io_completed
)
4473 cluster_iostate_wait(&iostate
, 0, "cluster_read_contig");
4475 if (iostate
.io_error
)
4476 error
= iostate
.io_error
;
4478 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
4480 if (error
== 0 && tail_size
)
4481 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, tail_size
, CL_READ
, callback
, callback_arg
);
4483 for (n
= 0; n
< num_upl
; n
++)
4485 * just release our hold on each physically contiguous
4486 * region without changing any state
4488 ubc_upl_abort(upl
[n
], 0);
4495 cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
)
4497 user_size_t iov_len
;
4498 user_addr_t iov_base
= 0;
4500 upl_size_t upl_size
;
4505 * skip over any emtpy vectors
4507 uio_update(uio
, (user_size_t
)0);
4509 iov_len
= uio_curriovlen(uio
);
4511 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_START
, uio
, (int)iov_len
, 0, 0, 0);
4514 iov_base
= uio_curriovbase(uio
);
4516 * make sure the size of the vector isn't too big...
4517 * internally, we want to handle all of the I/O in
4518 * chunk sizes that fit in a 32 bit int
4520 if (iov_len
> (user_size_t
)MAX_IO_REQUEST_SIZE
)
4521 upl_size
= MAX_IO_REQUEST_SIZE
;
4523 upl_size
= (u_int32_t
)iov_len
;
4525 upl_flags
= UPL_QUERY_OBJECT_TYPE
;
4527 if ((vm_map_get_upl(current_map(),
4528 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4529 &upl_size
, &upl
, NULL
, NULL
, &upl_flags
, 0)) != KERN_SUCCESS
) {
4531 * the user app must have passed in an invalid address
4538 *io_length
= upl_size
;
4540 if (upl_flags
& UPL_PHYS_CONTIG
)
4541 *io_type
= IO_CONTIG
;
4542 else if (iov_len
>= min_length
)
4543 *io_type
= IO_DIRECT
;
4548 * nothing left to do for this uio
4551 *io_type
= IO_UNKNOWN
;
4553 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_END
, iov_base
, *io_type
, *io_length
, retval
, 0);
4560 * generate advisory I/O's in the largest chunks possible
4561 * the completed pages will be released into the VM cache
4564 advisory_read(vnode_t vp
, off_t filesize
, off_t f_offset
, int resid
)
4566 return advisory_read_ext(vp
, filesize
, f_offset
, resid
, NULL
, NULL
, CL_PASSIVE
);
4570 advisory_read_ext(vnode_t vp
, off_t filesize
, off_t f_offset
, int resid
, int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
)
4572 upl_page_info_t
*pl
;
4574 vm_offset_t upl_offset
;
4587 uint32_t max_io_size
;
4590 if ( !UBCINFOEXISTS(vp
))
4596 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
4598 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_START
,
4599 (int)f_offset
, resid
, (int)filesize
, 0, 0);
4601 while (resid
&& f_offset
< filesize
&& retval
== 0) {
4603 * compute the size of the upl needed to encompass
4604 * the requested read... limit each call to cluster_io
4605 * to the maximum UPL size... cluster_io will clip if
4606 * this exceeds the maximum io_size for the device,
4607 * make sure to account for
4608 * a starting offset that's not page aligned
4610 start_offset
= (int)(f_offset
& PAGE_MASK_64
);
4611 upl_f_offset
= f_offset
- (off_t
)start_offset
;
4612 max_size
= filesize
- f_offset
;
4614 if (resid
< max_size
)
4619 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
4620 if ((uint32_t)upl_size
> max_io_size
)
4621 upl_size
= max_io_size
;
4625 * return the number of contiguously present pages in the cache
4626 * starting at upl_f_offset within the file
4628 ubc_range_op(vp
, upl_f_offset
, upl_f_offset
+ upl_size
, UPL_ROP_PRESENT
, &skip_range
);
4632 * skip over pages already present in the cache
4634 io_size
= skip_range
- start_offset
;
4636 f_offset
+= io_size
;
4639 if (skip_range
== upl_size
)
4642 * have to issue some real I/O
4643 * at this point, we know it's starting on a page boundary
4644 * because we've skipped over at least the first page in the request
4647 upl_f_offset
+= skip_range
;
4648 upl_size
-= skip_range
;
4650 pages_in_upl
= upl_size
/ PAGE_SIZE
;
4652 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_START
,
4653 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
4655 kret
= ubc_create_upl(vp
,
4660 UPL_RET_ONLY_ABSENT
| UPL_SET_LITE
);
4661 if (kret
!= KERN_SUCCESS
)
4666 * before we start marching forward, we must make sure we end on
4667 * a present page, otherwise we will be working with a freed
4670 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
4671 if (upl_page_present(pl
, last_pg
))
4674 pages_in_upl
= last_pg
+ 1;
4677 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_END
,
4678 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
4681 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
4683 * scan from the beginning of the upl looking for the first
4684 * page that is present.... this will become the first page in
4685 * the request we're going to make to 'cluster_io'... if all
4686 * of the pages are absent, we won't call through to 'cluster_io'
4688 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
4689 if (upl_page_present(pl
, start_pg
))
4694 * scan from the starting present page looking for an absent
4695 * page before the end of the upl is reached, if we
4696 * find one, then it will terminate the range of pages being
4697 * presented to 'cluster_io'
4699 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
4700 if (!upl_page_present(pl
, last_pg
))
4704 if (last_pg
> start_pg
) {
4706 * we found a range of pages that must be filled
4707 * if the last page in this range is the last page of the file
4708 * we may have to clip the size of it to keep from reading past
4709 * the end of the last physical block associated with the file
4711 upl_offset
= start_pg
* PAGE_SIZE
;
4712 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
4714 if ((off_t
)(upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
4715 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
4718 * issue an asynchronous read to cluster_io
4720 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
4721 CL_ASYNC
| CL_READ
| CL_COMMIT
| CL_AGE
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
4727 ubc_upl_abort(upl
, 0);
4729 io_size
= upl_size
- start_offset
;
4731 if (io_size
> resid
)
4733 f_offset
+= io_size
;
4737 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_END
,
4738 (int)f_offset
, resid
, retval
, 0, 0);
4745 cluster_push(vnode_t vp
, int flags
)
4747 return cluster_push_ext(vp
, flags
, NULL
, NULL
);
4752 cluster_push_ext(vnode_t vp
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
4755 int my_sparse_wait
= 0;
4756 struct cl_writebehind
*wbp
;
4758 if ( !UBCINFOEXISTS(vp
)) {
4759 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, vp
, flags
, 0, -1, 0);
4762 /* return if deferred write is set */
4763 if (((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) && (flags
& IO_DEFWRITE
)) {
4766 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) == NULL
) {
4767 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, vp
, flags
, 0, -2, 0);
4770 if (wbp
->cl_number
== 0 && wbp
->cl_scmap
== NULL
) {
4771 lck_mtx_unlock(&wbp
->cl_lockw
);
4773 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, vp
, flags
, 0, -3, 0);
4776 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_START
,
4777 wbp
->cl_scmap
, wbp
->cl_number
, flags
, 0, 0);
4780 * if we have an fsync in progress, we don't want to allow any additional
4781 * sync/fsync/close(s) to occur until it finishes.
4782 * note that its possible for writes to continue to occur to this file
4783 * while we're waiting and also once the fsync starts to clean if we're
4784 * in the sparse map case
4786 while (wbp
->cl_sparse_wait
) {
4787 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_START
, vp
, 0, 0, 0, 0);
4789 msleep((caddr_t
)&wbp
->cl_sparse_wait
, &wbp
->cl_lockw
, PRIBIO
+ 1, "cluster_push_ext", NULL
);
4791 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_END
, vp
, 0, 0, 0, 0);
4793 if (flags
& IO_SYNC
) {
4795 wbp
->cl_sparse_wait
= 1;
4798 * this is an fsync (or equivalent)... we must wait for any existing async
4799 * cleaning operations to complete before we evaulate the current state
4800 * and finish cleaning... this insures that all writes issued before this
4801 * fsync actually get cleaned to the disk before this fsync returns
4803 while (wbp
->cl_sparse_pushes
) {
4804 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 98)) | DBG_FUNC_START
, vp
, 0, 0, 0, 0);
4806 msleep((caddr_t
)&wbp
->cl_sparse_pushes
, &wbp
->cl_lockw
, PRIBIO
+ 1, "cluster_push_ext", NULL
);
4808 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 98)) | DBG_FUNC_END
, vp
, 0, 0, 0, 0);
4811 if (wbp
->cl_scmap
) {
4814 if (wbp
->cl_sparse_pushes
< SPARSE_PUSH_LIMIT
) {
4816 scmap
= wbp
->cl_scmap
;
4817 wbp
->cl_scmap
= NULL
;
4819 wbp
->cl_sparse_pushes
++;
4821 lck_mtx_unlock(&wbp
->cl_lockw
);
4823 sparse_cluster_push(&scmap
, vp
, ubc_getsize(vp
), PUSH_ALL
, flags
| IO_PASSIVE
, callback
, callback_arg
);
4825 lck_mtx_lock(&wbp
->cl_lockw
);
4827 wbp
->cl_sparse_pushes
--;
4829 if (wbp
->cl_sparse_wait
&& wbp
->cl_sparse_pushes
== 0)
4830 wakeup((caddr_t
)&wbp
->cl_sparse_pushes
);
4832 sparse_cluster_push(&(wbp
->cl_scmap
), vp
, ubc_getsize(vp
), PUSH_ALL
, flags
| IO_PASSIVE
, callback
, callback_arg
);
4836 retval
= cluster_try_push(wbp
, vp
, ubc_getsize(vp
), PUSH_ALL
, flags
| IO_PASSIVE
, callback
, callback_arg
);
4838 lck_mtx_unlock(&wbp
->cl_lockw
);
4840 if (flags
& IO_SYNC
)
4841 (void)vnode_waitforwrites(vp
, 0, 0, 0, "cluster_push");
4843 if (my_sparse_wait
) {
4845 * I'm the owner of the serialization token
4846 * clear it and wakeup anyone that is waiting
4849 lck_mtx_lock(&wbp
->cl_lockw
);
4851 wbp
->cl_sparse_wait
= 0;
4852 wakeup((caddr_t
)&wbp
->cl_sparse_wait
);
4854 lck_mtx_unlock(&wbp
->cl_lockw
);
4856 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_END
,
4857 wbp
->cl_scmap
, wbp
->cl_number
, retval
, 0, 0);
4863 __private_extern__
void
4864 cluster_release(struct ubc_info
*ubc
)
4866 struct cl_writebehind
*wbp
;
4867 struct cl_readahead
*rap
;
4869 if ((wbp
= ubc
->cl_wbehind
)) {
4871 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, ubc
, wbp
->cl_scmap
, 0, 0, 0);
4874 vfs_drt_control(&(wbp
->cl_scmap
), 0);
4876 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, ubc
, 0, 0, 0, 0);
4879 rap
= ubc
->cl_rahead
;
4882 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
4883 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
4885 if ((rap
= ubc
->cl_rahead
)) {
4886 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
4887 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
4889 ubc
->cl_rahead
= NULL
;
4890 ubc
->cl_wbehind
= NULL
;
4892 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_END
, ubc
, rap
, wbp
, 0, 0);
4897 cluster_try_push(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int push_flag
, int io_flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
4904 struct cl_wextent l_clusters
[MAX_CLUSTERS
];
4905 u_int max_cluster_pgcount
;
4908 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
4910 * the write behind context exists and has
4911 * already been locked...
4913 if (wbp
->cl_number
== 0)
4915 * no clusters to push
4916 * return number of empty slots
4918 return (MAX_CLUSTERS
);
4921 * make a local 'sorted' copy of the clusters
4922 * and clear wbp->cl_number so that new clusters can
4925 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
4926 for (min_index
= -1, cl_index1
= 0; cl_index1
< wbp
->cl_number
; cl_index1
++) {
4927 if (wbp
->cl_clusters
[cl_index1
].b_addr
== wbp
->cl_clusters
[cl_index1
].e_addr
)
4929 if (min_index
== -1)
4930 min_index
= cl_index1
;
4931 else if (wbp
->cl_clusters
[cl_index1
].b_addr
< wbp
->cl_clusters
[min_index
].b_addr
)
4932 min_index
= cl_index1
;
4934 if (min_index
== -1)
4937 l_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[min_index
].b_addr
;
4938 l_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
4939 l_clusters
[cl_index
].io_flags
= wbp
->cl_clusters
[min_index
].io_flags
;
4941 wbp
->cl_clusters
[min_index
].b_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
4947 if ( (push_flag
& PUSH_DELAY
) && cl_len
== MAX_CLUSTERS
) {
4951 * determine if we appear to be writing the file sequentially
4952 * if not, by returning without having pushed any clusters
4953 * we will cause this vnode to be pushed into the sparse cluster mechanism
4954 * used for managing more random I/O patterns
4956 * we know that we've got all clusters currently in use and the next write doesn't fit into one of them...
4957 * that's why we're in try_push with PUSH_DELAY...
4959 * check to make sure that all the clusters except the last one are 'full'... and that each cluster
4960 * is adjacent to the next (i.e. we're looking for sequential writes) they were sorted above
4961 * so we can just make a simple pass through, up to, but not including the last one...
4962 * note that e_addr is not inclusive, so it will be equal to the b_addr of the next cluster if they
4965 * we let the last one be partial as long as it was adjacent to the previous one...
4966 * we need to do this to deal with multi-threaded servers that might write an I/O or 2 out
4967 * of order... if this occurs at the tail of the last cluster, we don't want to fall into the sparse cluster world...
4969 for (i
= 0; i
< MAX_CLUSTERS
- 1; i
++) {
4970 if ((l_clusters
[i
].e_addr
- l_clusters
[i
].b_addr
) != max_cluster_pgcount
)
4972 if (l_clusters
[i
].e_addr
!= l_clusters
[i
+1].b_addr
)
4976 for (cl_index
= 0; cl_index
< cl_len
; cl_index
++) {
4978 struct cl_extent cl
;
4980 flags
= io_flags
& (IO_PASSIVE
|IO_CLOSE
);
4983 * try to push each cluster in turn...
4985 if (l_clusters
[cl_index
].io_flags
& CLW_IONOCACHE
)
4986 flags
|= IO_NOCACHE
;
4988 if (l_clusters
[cl_index
].io_flags
& CLW_IOPASSIVE
)
4989 flags
|= IO_PASSIVE
;
4991 if (push_flag
& PUSH_SYNC
)
4994 cl
.b_addr
= l_clusters
[cl_index
].b_addr
;
4995 cl
.e_addr
= l_clusters
[cl_index
].e_addr
;
4997 cluster_push_now(vp
, &cl
, EOF
, flags
, callback
, callback_arg
);
4999 l_clusters
[cl_index
].b_addr
= 0;
5000 l_clusters
[cl_index
].e_addr
= 0;
5004 if ( !(push_flag
& PUSH_ALL
) )
5008 if (cl_len
> cl_pushed
) {
5010 * we didn't push all of the clusters, so
5011 * lets try to merge them back in to the vnode
5013 if ((MAX_CLUSTERS
- wbp
->cl_number
) < (cl_len
- cl_pushed
)) {
5015 * we picked up some new clusters while we were trying to
5016 * push the old ones... this can happen because I've dropped
5017 * the vnode lock... the sum of the
5018 * leftovers plus the new cluster count exceeds our ability
5019 * to represent them, so switch to the sparse cluster mechanism
5021 * collect the active public clusters...
5023 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
);
5025 for (cl_index
= 0, cl_index1
= 0; cl_index
< cl_len
; cl_index
++) {
5026 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
5028 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
5029 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
5030 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
5035 * update the cluster count
5037 wbp
->cl_number
= cl_index1
;
5040 * and collect the original clusters that were moved into the
5041 * local storage for sorting purposes
5043 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
);
5047 * we've got room to merge the leftovers back in
5048 * just append them starting at the next 'hole'
5049 * represented by wbp->cl_number
5051 for (cl_index
= 0, cl_index1
= wbp
->cl_number
; cl_index
< cl_len
; cl_index
++) {
5052 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
5055 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
5056 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
5057 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
5062 * update the cluster count
5064 wbp
->cl_number
= cl_index1
;
5067 return (MAX_CLUSTERS
- wbp
->cl_number
);
5073 cluster_push_now(vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5075 upl_page_info_t
*pl
;
5077 vm_offset_t upl_offset
;
5092 if (flags
& IO_PASSIVE
)
5097 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_START
,
5098 (int)cl
->b_addr
, (int)cl
->e_addr
, (int)EOF
, flags
, 0);
5100 if ((pages_in_upl
= (int)(cl
->e_addr
- cl
->b_addr
)) == 0) {
5101 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 0, 0, 0, 0);
5105 upl_size
= pages_in_upl
* PAGE_SIZE
;
5106 upl_f_offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
5108 if (upl_f_offset
+ upl_size
>= EOF
) {
5110 if (upl_f_offset
>= EOF
) {
5112 * must have truncated the file and missed
5113 * clearing a dangling cluster (i.e. it's completely
5114 * beyond the new EOF
5116 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 1, 0, 0, 0);
5120 size
= EOF
- upl_f_offset
;
5122 upl_size
= (size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
5123 pages_in_upl
= upl_size
/ PAGE_SIZE
;
5127 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, size
, 0, 0, 0);
5130 * by asking for UPL_COPYOUT_FROM and UPL_RET_ONLY_DIRTY, we get the following desirable behavior
5132 * - only pages that are currently dirty are returned... these are the ones we need to clean
5133 * - the hardware dirty bit is cleared when the page is gathered into the UPL... the software dirty bit is set
5134 * - if we have to abort the I/O for some reason, the software dirty bit is left set since we didn't clean the page
5135 * - when we commit the page, the software dirty bit is cleared... the hardware dirty bit is untouched so that if
5136 * someone dirties this page while the I/O is in progress, we don't lose track of the new state
5138 * when the I/O completes, we no longer ask for an explicit clear of the DIRTY state (either soft or hard)
5141 if ((vp
->v_flag
& VNOCACHE_DATA
) || (flags
& IO_NOCACHE
))
5142 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
| UPL_WILL_BE_DUMPED
;
5144 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
;
5146 kret
= ubc_create_upl(vp
,
5152 if (kret
!= KERN_SUCCESS
)
5153 panic("cluster_push: failed to get pagelist");
5155 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
, upl
, upl_f_offset
, 0, 0, 0);
5158 * since we only asked for the dirty pages back
5159 * it's possible that we may only get a few or even none, so...
5160 * before we start marching forward, we must make sure we know
5161 * where the last present page is in the UPL, otherwise we could
5162 * end up working with a freed upl due to the FREE_ON_EMPTY semantics
5163 * employed by commit_range and abort_range.
5165 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
5166 if (upl_page_present(pl
, last_pg
))
5169 pages_in_upl
= last_pg
+ 1;
5171 if (pages_in_upl
== 0) {
5172 ubc_upl_abort(upl
, 0);
5174 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 2, 0, 0, 0);
5178 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
5180 * find the next dirty page in the UPL
5181 * this will become the first page in the
5182 * next I/O to generate
5184 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
5185 if (upl_dirty_page(pl
, start_pg
))
5187 if (upl_page_present(pl
, start_pg
))
5189 * RET_ONLY_DIRTY will return non-dirty 'precious' pages
5190 * just release these unchanged since we're not going
5191 * to steal them or change their state
5193 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
5195 if (start_pg
>= pages_in_upl
)
5197 * done... no more dirty pages to push
5200 if (start_pg
> last_pg
)
5202 * skipped over some non-dirty pages
5204 size
-= ((start_pg
- last_pg
) * PAGE_SIZE
);
5207 * find a range of dirty pages to write
5209 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
5210 if (!upl_dirty_page(pl
, last_pg
))
5213 upl_offset
= start_pg
* PAGE_SIZE
;
5215 io_size
= min(size
, (last_pg
- start_pg
) * PAGE_SIZE
);
5217 io_flags
= CL_THROTTLE
| CL_COMMIT
| CL_AGE
| bflag
;
5219 if ( !(flags
& IO_SYNC
))
5220 io_flags
|= CL_ASYNC
;
5222 if (flags
& IO_CLOSE
)
5223 io_flags
|= CL_CLOSE
;
5225 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
5226 io_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5228 if (error
== 0 && retval
)
5233 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 3, 0, 0, 0);
5240 * sparse_cluster_switch is called with the write behind lock held
5243 sparse_cluster_switch(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5247 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_START
, vp
, wbp
->cl_scmap
, 0, 0, 0);
5249 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
5251 struct cl_extent cl
;
5253 for (cl
.b_addr
= wbp
->cl_clusters
[cl_index
].b_addr
; cl
.b_addr
< wbp
->cl_clusters
[cl_index
].e_addr
; cl
.b_addr
++) {
5255 if (ubc_page_op(vp
, (off_t
)(cl
.b_addr
* PAGE_SIZE_64
), 0, NULL
, &flags
) == KERN_SUCCESS
) {
5256 if (flags
& UPL_POP_DIRTY
) {
5257 cl
.e_addr
= cl
.b_addr
+ 1;
5259 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, EOF
, callback
, callback_arg
);
5266 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_END
, vp
, wbp
->cl_scmap
, 0, 0, 0);
5271 * sparse_cluster_push must be called with the write-behind lock held if the scmap is
5272 * still associated with the write-behind context... however, if the scmap has been disassociated
5273 * from the write-behind context (the cluster_push case), the wb lock is not held
5276 sparse_cluster_push(void **scmap
, vnode_t vp
, off_t EOF
, int push_flag
, int io_flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5278 struct cl_extent cl
;
5282 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_START
, vp
, (*scmap
), 0, push_flag
, 0);
5284 if (push_flag
& PUSH_ALL
)
5285 vfs_drt_control(scmap
, 1);
5288 if (vfs_drt_get_cluster(scmap
, &offset
, &length
) != KERN_SUCCESS
)
5291 cl
.b_addr
= (daddr64_t
)(offset
/ PAGE_SIZE_64
);
5292 cl
.e_addr
= (daddr64_t
)((offset
+ length
) / PAGE_SIZE_64
);
5294 cluster_push_now(vp
, &cl
, EOF
, io_flags
& (IO_PASSIVE
|IO_CLOSE
), callback
, callback_arg
);
5296 if ( !(push_flag
& PUSH_ALL
) )
5299 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_END
, vp
, (*scmap
), 0, 0, 0);
5304 * sparse_cluster_add is called with the write behind lock held
5307 sparse_cluster_add(void **scmap
, vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5313 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_START
, (*scmap
), 0, cl
->b_addr
, (int)cl
->e_addr
, 0);
5315 offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
5316 length
= ((u_int
)(cl
->e_addr
- cl
->b_addr
)) * PAGE_SIZE
;
5318 while (vfs_drt_mark_pages(scmap
, offset
, length
, &new_dirty
) != KERN_SUCCESS
) {
5320 * no room left in the map
5321 * only a partial update was done
5322 * push out some pages and try again
5324 sparse_cluster_push(scmap
, vp
, EOF
, 0, 0, callback
, callback_arg
);
5326 offset
+= (new_dirty
* PAGE_SIZE_64
);
5327 length
-= (new_dirty
* PAGE_SIZE
);
5329 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_END
, vp
, (*scmap
), 0, 0, 0);
5334 cluster_align_phys_io(vnode_t vp
, struct uio
*uio
, addr64_t usr_paddr
, u_int32_t xsize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5336 upl_page_info_t
*pl
;
5346 if (flags
& IO_PASSIVE
)
5351 upl_flags
= UPL_SET_LITE
;
5353 if ( !(flags
& CL_READ
) ) {
5355 * "write" operation: let the UPL subsystem know
5356 * that we intend to modify the buffer cache pages
5359 upl_flags
|= UPL_WILL_MODIFY
;
5362 * indicate that there is no need to pull the
5363 * mapping for this page... we're only going
5364 * to read from it, not modify it.
5366 upl_flags
|= UPL_FILE_IO
;
5368 kret
= ubc_create_upl(vp
,
5369 uio
->uio_offset
& ~PAGE_MASK_64
,
5375 if (kret
!= KERN_SUCCESS
)
5378 if (!upl_valid_page(pl
, 0)) {
5380 * issue a synchronous read to cluster_io
5382 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
5383 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5385 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
5391 ubc_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)(uio
->uio_offset
& PAGE_MASK_64
);
5394 * NOTE: There is no prototype for the following in BSD. It, and the definitions
5395 * of the defines for cppvPsrc, cppvPsnk, cppvFsnk, and cppvFsrc will be found in
5396 * osfmk/ppc/mappings.h. They are not included here because there appears to be no
5397 * way to do so without exporting them to kexts as well.
5399 if (flags
& CL_READ
)
5400 // copypv(ubc_paddr, usr_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsnk); /* Copy physical to physical and flush the destination */
5401 copypv(ubc_paddr
, usr_paddr
, xsize
, 2 | 1 | 4); /* Copy physical to physical and flush the destination */
5403 // copypv(usr_paddr, ubc_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsrc); /* Copy physical to physical and flush the source */
5404 copypv(usr_paddr
, ubc_paddr
, xsize
, 2 | 1 | 8); /* Copy physical to physical and flush the source */
5406 if ( !(flags
& CL_READ
) || (upl_valid_page(pl
, 0) && upl_dirty_page(pl
, 0))) {
5408 * issue a synchronous write to cluster_io
5410 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
5411 bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5414 uio_update(uio
, (user_size_t
)xsize
);
5417 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
5419 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
5421 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, abort_flags
);
5429 cluster_copy_upl_data(struct uio
*uio
, upl_t upl
, int upl_offset
, int *io_resid
)
5437 upl_page_info_t
*pl
;
5441 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
5442 (int)uio
->uio_offset
, upl_offset
, xsize
, 0, 0);
5444 segflg
= uio
->uio_segflg
;
5448 case UIO_USERSPACE32
:
5449 case UIO_USERISPACE32
:
5450 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
5454 case UIO_USERISPACE
:
5455 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
5458 case UIO_USERSPACE64
:
5459 case UIO_USERISPACE64
:
5460 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
5464 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
5468 pl
= ubc_upl_pageinfo(upl
);
5470 pg_index
= upl_offset
/ PAGE_SIZE
;
5471 pg_offset
= upl_offset
& PAGE_MASK
;
5472 csize
= min(PAGE_SIZE
- pg_offset
, xsize
);
5474 while (xsize
&& retval
== 0) {
5477 paddr
= ((addr64_t
)upl_phys_page(pl
, pg_index
) << 12) + pg_offset
;
5479 retval
= uiomove64(paddr
, csize
, uio
);
5484 csize
= min(PAGE_SIZE
, xsize
);
5488 uio
->uio_segflg
= segflg
;
5490 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
5491 (int)uio
->uio_offset
, xsize
, retval
, segflg
, 0);
5498 cluster_copy_ubc_data(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
)
5501 return (cluster_copy_ubc_data_internal(vp
, uio
, io_resid
, mark_dirty
, 1));
5506 cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
)
5513 memory_object_control_t control
;
5515 io_size
= *io_resid
;
5517 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
5518 (int)uio
->uio_offset
, io_size
, mark_dirty
, take_reference
, 0);
5520 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
5522 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
5523 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
5524 (int)uio
->uio_offset
, io_size
, retval
, 3, 0);
5528 segflg
= uio
->uio_segflg
;
5532 case UIO_USERSPACE32
:
5533 case UIO_USERISPACE32
:
5534 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
5537 case UIO_USERSPACE64
:
5538 case UIO_USERISPACE64
:
5539 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
5543 case UIO_USERISPACE
:
5544 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
5548 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
5552 if ( (io_size
= *io_resid
) ) {
5553 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
5554 xsize
= uio_resid(uio
);
5556 retval
= memory_object_control_uiomove(control
, uio
->uio_offset
- start_offset
, uio
,
5557 start_offset
, io_size
, mark_dirty
, take_reference
);
5558 xsize
-= uio_resid(uio
);
5561 uio
->uio_segflg
= segflg
;
5562 *io_resid
= io_size
;
5564 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
5565 (int)uio
->uio_offset
, io_size
, retval
, 0x80000000 | segflg
, 0);
5572 is_file_clean(vnode_t vp
, off_t filesize
)
5576 int total_dirty
= 0;
5578 for (f_offset
= 0; f_offset
< filesize
; f_offset
+= PAGE_SIZE_64
) {
5579 if (ubc_page_op(vp
, f_offset
, 0, NULL
, &flags
) == KERN_SUCCESS
) {
5580 if (flags
& UPL_POP_DIRTY
) {
5594 * Dirty region tracking/clustering mechanism.
5596 * This code (vfs_drt_*) provides a mechanism for tracking and clustering
5597 * dirty regions within a larger space (file). It is primarily intended to
5598 * support clustering in large files with many dirty areas.
5600 * The implementation assumes that the dirty regions are pages.
5602 * To represent dirty pages within the file, we store bit vectors in a
5603 * variable-size circular hash.
5607 * Bitvector size. This determines the number of pages we group in a
5608 * single hashtable entry. Each hashtable entry is aligned to this
5609 * size within the file.
5611 #define DRT_BITVECTOR_PAGES 256
5614 * File offset handling.
5616 * DRT_ADDRESS_MASK is dependent on DRT_BITVECTOR_PAGES;
5617 * the correct formula is (~(DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1)
5619 #define DRT_ADDRESS_MASK (~((1 << 20) - 1))
5620 #define DRT_ALIGN_ADDRESS(addr) ((addr) & DRT_ADDRESS_MASK)
5623 * Hashtable address field handling.
5625 * The low-order bits of the hashtable address are used to conserve
5628 * DRT_HASH_COUNT_MASK must be large enough to store the range
5629 * 0-DRT_BITVECTOR_PAGES inclusive, as well as have one value
5630 * to indicate that the bucket is actually unoccupied.
5632 #define DRT_HASH_GET_ADDRESS(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_ADDRESS_MASK)
5633 #define DRT_HASH_SET_ADDRESS(scm, i, a) \
5635 (scm)->scm_hashtable[(i)].dhe_control = \
5636 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_ADDRESS_MASK) | DRT_ALIGN_ADDRESS(a); \
5638 #define DRT_HASH_COUNT_MASK 0x1ff
5639 #define DRT_HASH_GET_COUNT(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_HASH_COUNT_MASK)
5640 #define DRT_HASH_SET_COUNT(scm, i, c) \
5642 (scm)->scm_hashtable[(i)].dhe_control = \
5643 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_HASH_COUNT_MASK) | ((c) & DRT_HASH_COUNT_MASK); \
5645 #define DRT_HASH_CLEAR(scm, i) \
5647 (scm)->scm_hashtable[(i)].dhe_control = 0; \
5649 #define DRT_HASH_VACATE(scm, i) DRT_HASH_SET_COUNT((scm), (i), DRT_HASH_COUNT_MASK)
5650 #define DRT_HASH_VACANT(scm, i) (DRT_HASH_GET_COUNT((scm), (i)) == DRT_HASH_COUNT_MASK)
5651 #define DRT_HASH_COPY(oscm, oi, scm, i) \
5653 (scm)->scm_hashtable[(i)].dhe_control = (oscm)->scm_hashtable[(oi)].dhe_control; \
5654 DRT_BITVECTOR_COPY(oscm, oi, scm, i); \
5659 * Hash table moduli.
5661 * Since the hashtable entry's size is dependent on the size of
5662 * the bitvector, and since the hashtable size is constrained to
5663 * both being prime and fitting within the desired allocation
5664 * size, these values need to be manually determined.
5666 * For DRT_BITVECTOR_SIZE = 256, the entry size is 40 bytes.
5668 * The small hashtable allocation is 1024 bytes, so the modulus is 23.
5669 * The large hashtable allocation is 16384 bytes, so the modulus is 401.
5671 #define DRT_HASH_SMALL_MODULUS 23
5672 #define DRT_HASH_LARGE_MODULUS 401
5675 * Physical memory required before the large hash modulus is permitted.
5677 * On small memory systems, the large hash modulus can lead to phsyical
5678 * memory starvation, so we avoid using it there.
5680 #define DRT_HASH_LARGE_MEMORY_REQUIRED (1024LL * 1024LL * 1024LL) /* 1GiB */
5682 #define DRT_SMALL_ALLOCATION 1024 /* 104 bytes spare */
5683 #define DRT_LARGE_ALLOCATION 16384 /* 344 bytes spare */
5685 /* *** nothing below here has secret dependencies on DRT_BITVECTOR_PAGES *** */
5688 * Hashtable bitvector handling.
5690 * Bitvector fields are 32 bits long.
5693 #define DRT_HASH_SET_BIT(scm, i, bit) \
5694 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] |= (1 << ((bit) % 32))
5696 #define DRT_HASH_CLEAR_BIT(scm, i, bit) \
5697 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] &= ~(1 << ((bit) % 32))
5699 #define DRT_HASH_TEST_BIT(scm, i, bit) \
5700 ((scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] & (1 << ((bit) % 32)))
5702 #define DRT_BITVECTOR_CLEAR(scm, i) \
5703 bzero(&(scm)->scm_hashtable[(i)].dhe_bitvector[0], (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
5705 #define DRT_BITVECTOR_COPY(oscm, oi, scm, i) \
5706 bcopy(&(oscm)->scm_hashtable[(oi)].dhe_bitvector[0], \
5707 &(scm)->scm_hashtable[(i)].dhe_bitvector[0], \
5708 (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
5715 struct vfs_drt_hashentry
{
5716 u_int64_t dhe_control
;
5717 u_int32_t dhe_bitvector
[DRT_BITVECTOR_PAGES
/ 32];
5721 * Dirty Region Tracking structure.
5723 * The hashtable is allocated entirely inside the DRT structure.
5725 * The hash is a simple circular prime modulus arrangement, the structure
5726 * is resized from small to large if it overflows.
5729 struct vfs_drt_clustermap
{
5730 u_int32_t scm_magic
; /* sanity/detection */
5731 #define DRT_SCM_MAGIC 0x12020003
5732 u_int32_t scm_modulus
; /* current ring size */
5733 u_int32_t scm_buckets
; /* number of occupied buckets */
5734 u_int32_t scm_lastclean
; /* last entry we cleaned */
5735 u_int32_t scm_iskips
; /* number of slot skips */
5737 struct vfs_drt_hashentry scm_hashtable
[0];
5741 #define DRT_HASH(scm, addr) ((addr) % (scm)->scm_modulus)
5742 #define DRT_HASH_NEXT(scm, addr) (((addr) + 1) % (scm)->scm_modulus)
5745 * Debugging codes and arguments.
5747 #define DRT_DEBUG_EMPTYFREE (FSDBG_CODE(DBG_FSRW, 82)) /* nil */
5748 #define DRT_DEBUG_RETCLUSTER (FSDBG_CODE(DBG_FSRW, 83)) /* offset, length */
5749 #define DRT_DEBUG_ALLOC (FSDBG_CODE(DBG_FSRW, 84)) /* copycount */
5750 #define DRT_DEBUG_INSERT (FSDBG_CODE(DBG_FSRW, 85)) /* offset, iskip */
5751 #define DRT_DEBUG_MARK (FSDBG_CODE(DBG_FSRW, 86)) /* offset, length,
5754 /* 1 (clean, no map) */
5755 /* 2 (map alloc fail) */
5756 /* 3, resid (partial) */
5757 #define DRT_DEBUG_6 (FSDBG_CODE(DBG_FSRW, 87))
5758 #define DRT_DEBUG_SCMDATA (FSDBG_CODE(DBG_FSRW, 88)) /* modulus, buckets,
5759 * lastclean, iskips */
5762 static kern_return_t
vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
);
5763 static kern_return_t
vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
);
5764 static kern_return_t
vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
,
5765 u_int64_t offset
, int *indexp
);
5766 static kern_return_t
vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
,
5770 static kern_return_t
vfs_drt_do_mark_pages(
5776 static void vfs_drt_trace(
5777 struct vfs_drt_clustermap
*cmap
,
5786 * Allocate and initialise a sparse cluster map.
5788 * Will allocate a new map, resize or compact an existing map.
5790 * XXX we should probably have at least one intermediate map size,
5791 * as the 1:16 ratio seems a bit drastic.
5793 static kern_return_t
5794 vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
)
5796 struct vfs_drt_clustermap
*cmap
, *ocmap
;
5800 int nsize
, active_buckets
, index
, copycount
;
5807 * Decide on the size of the new map.
5809 if (ocmap
== NULL
) {
5810 nsize
= DRT_HASH_SMALL_MODULUS
;
5812 /* count the number of active buckets in the old map */
5814 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
5815 if (!DRT_HASH_VACANT(ocmap
, i
) &&
5816 (DRT_HASH_GET_COUNT(ocmap
, i
) != 0))
5820 * If we're currently using the small allocation, check to
5821 * see whether we should grow to the large one.
5823 if (ocmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) {
5825 * If the ring is nearly full and we are allowed to
5826 * use the large modulus, upgrade.
5828 if ((active_buckets
> (DRT_HASH_SMALL_MODULUS
- 5)) &&
5829 (max_mem
>= DRT_HASH_LARGE_MEMORY_REQUIRED
)) {
5830 nsize
= DRT_HASH_LARGE_MODULUS
;
5832 nsize
= DRT_HASH_SMALL_MODULUS
;
5835 /* already using the large modulus */
5836 nsize
= DRT_HASH_LARGE_MODULUS
;
5838 * If the ring is completely full, there's
5839 * nothing useful for us to do. Behave as
5840 * though we had compacted into the new
5843 if (active_buckets
>= DRT_HASH_LARGE_MODULUS
)
5844 return(KERN_SUCCESS
);
5849 * Allocate and initialise the new map.
5852 kret
= kmem_alloc(kernel_map
, (vm_offset_t
*)&cmap
,
5853 (nsize
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
5854 if (kret
!= KERN_SUCCESS
)
5856 cmap
->scm_magic
= DRT_SCM_MAGIC
;
5857 cmap
->scm_modulus
= nsize
;
5858 cmap
->scm_buckets
= 0;
5859 cmap
->scm_lastclean
= 0;
5860 cmap
->scm_iskips
= 0;
5861 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
5862 DRT_HASH_CLEAR(cmap
, i
);
5863 DRT_HASH_VACATE(cmap
, i
);
5864 DRT_BITVECTOR_CLEAR(cmap
, i
);
5868 * If there's an old map, re-hash entries from it into the new map.
5871 if (ocmap
!= NULL
) {
5872 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
5873 /* skip empty buckets */
5874 if (DRT_HASH_VACANT(ocmap
, i
) ||
5875 (DRT_HASH_GET_COUNT(ocmap
, i
) == 0))
5878 offset
= DRT_HASH_GET_ADDRESS(ocmap
, i
);
5879 kret
= vfs_drt_get_index(&cmap
, offset
, &index
, 1);
5880 if (kret
!= KERN_SUCCESS
) {
5881 /* XXX need to bail out gracefully here */
5882 panic("vfs_drt: new cluster map mysteriously too small");
5886 DRT_HASH_COPY(ocmap
, i
, cmap
, index
);
5891 /* log what we've done */
5892 vfs_drt_trace(cmap
, DRT_DEBUG_ALLOC
, copycount
, 0, 0, 0);
5895 * It's important to ensure that *cmapp always points to
5896 * a valid map, so we must overwrite it before freeing
5900 if (ocmap
!= NULL
) {
5901 /* emit stats into trace buffer */
5902 vfs_drt_trace(ocmap
, DRT_DEBUG_SCMDATA
,
5905 ocmap
->scm_lastclean
,
5908 vfs_drt_free_map(ocmap
);
5910 return(KERN_SUCCESS
);
5915 * Free a sparse cluster map.
5917 static kern_return_t
5918 vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
)
5920 kmem_free(kernel_map
, (vm_offset_t
)cmap
,
5921 (cmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
5922 return(KERN_SUCCESS
);
5927 * Find the hashtable slot currently occupied by an entry for the supplied offset.
5929 static kern_return_t
5930 vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
, u_int64_t offset
, int *indexp
)
5935 offset
= DRT_ALIGN_ADDRESS(offset
);
5936 index
= DRT_HASH(cmap
, offset
);
5938 /* traverse the hashtable */
5939 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
5942 * If the slot is vacant, we can stop.
5944 if (DRT_HASH_VACANT(cmap
, index
))
5948 * If the address matches our offset, we have success.
5950 if (DRT_HASH_GET_ADDRESS(cmap
, index
) == offset
) {
5952 return(KERN_SUCCESS
);
5956 * Move to the next slot, try again.
5958 index
= DRT_HASH_NEXT(cmap
, index
);
5963 return(KERN_FAILURE
);
5967 * Find the hashtable slot for the supplied offset. If we haven't allocated
5968 * one yet, allocate one and populate the address field. Note that it will
5969 * not have a nonzero page count and thus will still technically be free, so
5970 * in the case where we are called to clean pages, the slot will remain free.
5972 static kern_return_t
5973 vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
, u_int64_t offset
, int *indexp
, int recursed
)
5975 struct vfs_drt_clustermap
*cmap
;
5982 /* look for an existing entry */
5983 kret
= vfs_drt_search_index(cmap
, offset
, indexp
);
5984 if (kret
== KERN_SUCCESS
)
5987 /* need to allocate an entry */
5988 offset
= DRT_ALIGN_ADDRESS(offset
);
5989 index
= DRT_HASH(cmap
, offset
);
5991 /* scan from the index forwards looking for a vacant slot */
5992 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
5994 if (DRT_HASH_VACANT(cmap
, index
) || DRT_HASH_GET_COUNT(cmap
,index
) == 0) {
5995 cmap
->scm_buckets
++;
5996 if (index
< cmap
->scm_lastclean
)
5997 cmap
->scm_lastclean
= index
;
5998 DRT_HASH_SET_ADDRESS(cmap
, index
, offset
);
5999 DRT_HASH_SET_COUNT(cmap
, index
, 0);
6000 DRT_BITVECTOR_CLEAR(cmap
, index
);
6002 vfs_drt_trace(cmap
, DRT_DEBUG_INSERT
, (int)offset
, i
, 0, 0);
6003 return(KERN_SUCCESS
);
6005 cmap
->scm_iskips
+= i
;
6006 index
= DRT_HASH_NEXT(cmap
, index
);
6010 * We haven't found a vacant slot, so the map is full. If we're not
6011 * already recursed, try reallocating/compacting it.
6014 return(KERN_FAILURE
);
6015 kret
= vfs_drt_alloc_map(cmapp
);
6016 if (kret
== KERN_SUCCESS
) {
6017 /* now try to insert again */
6018 kret
= vfs_drt_get_index(cmapp
, offset
, indexp
, 1);
6024 * Implementation of set dirty/clean.
6026 * In the 'clean' case, not finding a map is OK.
6028 static kern_return_t
6029 vfs_drt_do_mark_pages(
6036 struct vfs_drt_clustermap
*cmap
, **cmapp
;
6038 int i
, index
, pgoff
, pgcount
, setcount
, ecount
;
6040 cmapp
= (struct vfs_drt_clustermap
**)private;
6043 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_START
, (int)offset
, (int)length
, dirty
, 0);
6045 if (setcountp
!= NULL
)
6048 /* allocate a cluster map if we don't already have one */
6050 /* no cluster map, nothing to clean */
6052 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 1, 0, 0, 0);
6053 return(KERN_SUCCESS
);
6055 kret
= vfs_drt_alloc_map(cmapp
);
6056 if (kret
!= KERN_SUCCESS
) {
6057 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 2, 0, 0, 0);
6064 * Iterate over the length of the region.
6066 while (length
> 0) {
6068 * Get the hashtable index for this offset.
6070 * XXX this will add blank entries if we are clearing a range
6071 * that hasn't been dirtied.
6073 kret
= vfs_drt_get_index(cmapp
, offset
, &index
, 0);
6074 cmap
= *cmapp
; /* may have changed! */
6075 /* this may be a partial-success return */
6076 if (kret
!= KERN_SUCCESS
) {
6077 if (setcountp
!= NULL
)
6078 *setcountp
= setcount
;
6079 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 3, (int)length
, 0, 0);
6085 * Work out how many pages we're modifying in this
6088 pgoff
= (offset
- DRT_ALIGN_ADDRESS(offset
)) / PAGE_SIZE
;
6089 pgcount
= min((length
/ PAGE_SIZE
), (DRT_BITVECTOR_PAGES
- pgoff
));
6092 * Iterate over pages, dirty/clearing as we go.
6094 ecount
= DRT_HASH_GET_COUNT(cmap
, index
);
6095 for (i
= 0; i
< pgcount
; i
++) {
6097 if (!DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
6098 DRT_HASH_SET_BIT(cmap
, index
, pgoff
+ i
);
6103 if (DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
6104 DRT_HASH_CLEAR_BIT(cmap
, index
, pgoff
+ i
);
6110 DRT_HASH_SET_COUNT(cmap
, index
, ecount
);
6112 offset
+= pgcount
* PAGE_SIZE
;
6113 length
-= pgcount
* PAGE_SIZE
;
6115 if (setcountp
!= NULL
)
6116 *setcountp
= setcount
;
6118 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 0, setcount
, 0, 0);
6120 return(KERN_SUCCESS
);
6124 * Mark a set of pages as dirty/clean.
6126 * This is a public interface.
6129 * Pointer to storage suitable for holding a pointer. Note that
6130 * this must either be NULL or a value set by this function.
6133 * Current file size in bytes.
6136 * Offset of the first page to be marked as dirty, in bytes. Must be
6140 * Length of dirty region, in bytes. Must be a multiple of PAGE_SIZE.
6143 * Number of pages newly marked dirty by this call (optional).
6145 * Returns KERN_SUCCESS if all the pages were successfully marked.
6147 static kern_return_t
6148 vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
)
6150 /* XXX size unused, drop from interface */
6151 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, setcountp
, 1));
6155 static kern_return_t
6156 vfs_drt_unmark_pages(void **cmapp
, off_t offset
, u_int length
)
6158 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0));
6163 * Get a cluster of dirty pages.
6165 * This is a public interface.
6168 * Pointer to storage managed by drt_mark_pages. Note that this must
6169 * be NULL or a value set by drt_mark_pages.
6172 * Returns the byte offset into the file of the first page in the cluster.
6175 * Returns the length in bytes of the cluster of dirty pages.
6177 * Returns success if a cluster was found. If KERN_FAILURE is returned, there
6178 * are no dirty pages meeting the minmum size criteria. Private storage will
6179 * be released if there are no more dirty pages left in the map
6182 static kern_return_t
6183 vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
)
6185 struct vfs_drt_clustermap
*cmap
;
6189 int index
, i
, fs
, ls
;
6192 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
6193 return(KERN_FAILURE
);
6196 /* walk the hashtable */
6197 for (offset
= 0, j
= 0; j
< cmap
->scm_modulus
; offset
+= (DRT_BITVECTOR_PAGES
* PAGE_SIZE
), j
++) {
6198 index
= DRT_HASH(cmap
, offset
);
6200 if (DRT_HASH_VACANT(cmap
, index
) || (DRT_HASH_GET_COUNT(cmap
, index
) == 0))
6203 /* scan the bitfield for a string of bits */
6206 for (i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
6207 if (DRT_HASH_TEST_BIT(cmap
, index
, i
)) {
6213 /* didn't find any bits set */
6214 panic("vfs_drt: entry summary count > 0 but no bits set in map");
6216 for (ls
= 0; i
< DRT_BITVECTOR_PAGES
; i
++, ls
++) {
6217 if (!DRT_HASH_TEST_BIT(cmap
, index
, i
))
6221 /* compute offset and length, mark pages clean */
6222 offset
= DRT_HASH_GET_ADDRESS(cmap
, index
) + (PAGE_SIZE
* fs
);
6223 length
= ls
* PAGE_SIZE
;
6224 vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0);
6225 cmap
->scm_lastclean
= index
;
6227 /* return successful */
6228 *offsetp
= (off_t
)offset
;
6231 vfs_drt_trace(cmap
, DRT_DEBUG_RETCLUSTER
, (int)offset
, (int)length
, 0, 0);
6232 return(KERN_SUCCESS
);
6235 * We didn't find anything... hashtable is empty
6236 * emit stats into trace buffer and
6239 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
6242 cmap
->scm_lastclean
,
6245 vfs_drt_free_map(cmap
);
6248 return(KERN_FAILURE
);
6252 static kern_return_t
6253 vfs_drt_control(void **cmapp
, int op_type
)
6255 struct vfs_drt_clustermap
*cmap
;
6258 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
6259 return(KERN_FAILURE
);
6264 /* emit stats into trace buffer */
6265 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
6268 cmap
->scm_lastclean
,
6271 vfs_drt_free_map(cmap
);
6276 cmap
->scm_lastclean
= 0;
6279 return(KERN_SUCCESS
);
6285 * Emit a summary of the state of the clustermap into the trace buffer
6286 * along with some caller-provided data.
6290 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, int code
, int arg1
, int arg2
, int arg3
, int arg4
)
6292 KERNEL_DEBUG(code
, arg1
, arg2
, arg3
, arg4
, 0);
6296 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, __unused
int code
,
6297 __unused
int arg1
, __unused
int arg2
, __unused
int arg3
,
6305 * Perform basic sanity check on the hash entry summary count
6306 * vs. the actual bits set in the entry.
6309 vfs_drt_sanity(struct vfs_drt_clustermap
*cmap
)
6314 for (index
= 0; index
< cmap
->scm_modulus
; index
++) {
6315 if (DRT_HASH_VACANT(cmap
, index
))
6318 for (bits_on
= 0, i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
6319 if (DRT_HASH_TEST_BIT(cmap
, index
, i
))
6322 if (bits_on
!= DRT_HASH_GET_COUNT(cmap
, index
))
6323 panic("bits_on = %d, index = %d\n", bits_on
, index
);