2 * Copyright (c) 2000-2007 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>
86 #include <vm/vm_kern.h>
87 #include <vm/vm_map.h>
88 #include <vm/vm_pageout.h>
90 #include <sys/kdebug.h>
95 #define CL_COMMIT 0x08
96 #define CL_PAGEOUT 0x10
98 #define CL_NOZERO 0x40
99 #define CL_PAGEIN 0x80
100 #define CL_DEV_MEMORY 0x100
101 #define CL_PRESERVE 0x200
102 #define CL_THROTTLE 0x400
103 #define CL_KEEPCACHED 0x800
104 #define CL_DIRECT_IO 0x1000
105 #define CL_PASSIVE 0x2000
109 u_int io_completed
; /* amount of io that has currently completed */
110 u_int io_issued
; /* amount of io that was successfully issued */
111 int io_error
; /* error code of first error encountered */
112 int io_wanted
; /* someone is sleeping waiting for a change in state */
115 static lck_grp_t
*cl_mtx_grp
;
116 static lck_attr_t
*cl_mtx_attr
;
117 static lck_grp_attr_t
*cl_mtx_grp_attr
;
118 static lck_mtx_t
*cl_mtxp
;
126 #define PUSH_DELAY 0x01
127 #define PUSH_ALL 0x02
128 #define PUSH_SYNC 0x04
131 static void cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
);
132 static void cluster_wait_IO(buf_t cbp_head
, int async
);
133 static void cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
);
135 static int cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
);
137 static int cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
138 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*)(buf_t
, void *), void *callback_arg
);
139 static int cluster_iodone(buf_t bp
, void *callback_arg
);
140 static int cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
);
141 static int cluster_hard_throttle_on(vnode_t vp
);
143 static void cluster_syncup(vnode_t vp
, off_t newEOF
, int (*)(buf_t
, void *), void *callback_arg
);
145 static void cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int flags
);
146 static int cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
);
148 static int cluster_read_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t filesize
, int flags
,
149 int (*)(buf_t
, void *), void *callback_arg
);
150 static int cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
151 int flags
, int (*)(buf_t
, void *), void *callback_arg
);
152 static int cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
153 int (*)(buf_t
, void *), void *callback_arg
, int flags
);
155 static int cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
,
156 off_t headOff
, off_t tailOff
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
157 static int cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
,
158 int *write_type
, u_int32_t
*write_length
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
159 static int cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
,
160 int *write_type
, u_int32_t
*write_length
, int (*)(buf_t
, void *), void *callback_arg
, int bflag
);
162 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
);
164 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
);
165 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
);
167 static int cluster_push_now(vnode_t vp
, struct cl_extent
*, off_t EOF
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
169 static int cluster_try_push(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int push_flag
, int (*)(buf_t
, void *), void *callback_arg
);
171 static void sparse_cluster_switch(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int (*)(buf_t
, void *), void *callback_arg
);
172 static void sparse_cluster_push(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int push_flag
, int (*)(buf_t
, void *), void *callback_arg
);
173 static void sparse_cluster_add(struct cl_writebehind
*, vnode_t vp
, struct cl_extent
*, off_t EOF
, int (*)(buf_t
, void *), void *callback_arg
);
175 static kern_return_t
vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
);
176 static kern_return_t
vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
);
177 static kern_return_t
vfs_drt_control(void **cmapp
, int op_type
);
179 int is_file_clean(vnode_t
, off_t
);
182 * limit the internal I/O size so that we
183 * can represent it in a 32 bit int
185 #define MAX_IO_REQUEST_SIZE (1024 * 1024 * 256)
186 #define MAX_IO_CONTIG_SIZE (MAX_UPL_SIZE * PAGE_SIZE)
188 #define MIN_DIRECT_WRITE_SIZE (4 * PAGE_SIZE)
191 #define MAX_CLUSTER_SIZE(vp) (cluster_max_io_size(vp->v_mount, CL_WRITE))
192 #define MAX_PREFETCH(vp) (cluster_max_io_size(vp->v_mount, CL_READ) * 3);
195 int speculative_reads_disabled
= 0;
198 * throttle the number of async writes that
199 * can be outstanding on a single vnode
200 * before we issue a synchronous write
202 #define HARD_THROTTLE_MAXCNT 0
203 #define HARD_THROTTLE_MAXSIZE (64 * 1024)
205 int hard_throttle_on_root
= 0;
206 struct timeval priority_IO_timestamp_for_root
;
212 * allocate lock group attribute and group
214 cl_mtx_grp_attr
= lck_grp_attr_alloc_init();
215 cl_mtx_grp
= lck_grp_alloc_init("cluster I/O", cl_mtx_grp_attr
);
218 * allocate the lock attribute
220 cl_mtx_attr
= lck_attr_alloc_init();
223 * allocate and initialize mutex's used to protect updates and waits
224 * on the cluster_io context
226 cl_mtxp
= lck_mtx_alloc_init(cl_mtx_grp
, cl_mtx_attr
);
229 panic("cluster_init: failed to allocate cl_mtxp");
234 cluster_max_io_size(mount_t mp
, int type
)
236 uint32_t max_io_size
;
243 segcnt
= mp
->mnt_segreadcnt
;
244 maxcnt
= mp
->mnt_maxreadcnt
;
247 segcnt
= mp
->mnt_segwritecnt
;
248 maxcnt
= mp
->mnt_maxwritecnt
;
251 segcnt
= min(mp
->mnt_segreadcnt
, mp
->mnt_segwritecnt
);
252 maxcnt
= min(mp
->mnt_maxreadcnt
, mp
->mnt_maxwritecnt
);
255 if (segcnt
> MAX_UPL_SIZE
) {
257 * don't allow a size beyond the max UPL size we can create
259 segcnt
= MAX_UPL_SIZE
;
261 max_io_size
= min((segcnt
* PAGE_SIZE
), maxcnt
);
263 if (max_io_size
< (MAX_UPL_TRANSFER
* PAGE_SIZE
)) {
265 * don't allow a size smaller than the old fixed limit
267 max_io_size
= (MAX_UPL_TRANSFER
* PAGE_SIZE
);
270 * make sure the size specified is a multiple of PAGE_SIZE
272 max_io_size
&= ~PAGE_MASK
;
274 return (max_io_size
);
280 #define CLW_ALLOCATE 0x01
281 #define CLW_RETURNLOCKED 0x02
282 #define CLW_IONOCACHE 0x04
283 #define CLW_IOPASSIVE 0x08
286 * if the read ahead context doesn't yet exist,
287 * allocate and initialize it...
288 * the vnode lock serializes multiple callers
289 * during the actual assignment... first one
290 * to grab the lock wins... the other callers
291 * will release the now unnecessary storage
293 * once the context is present, try to grab (but don't block on)
294 * the lock associated with it... if someone
295 * else currently owns it, than the read
296 * will run without read-ahead. this allows
297 * multiple readers to run in parallel and
298 * since there's only 1 read ahead context,
299 * there's no real loss in only allowing 1
300 * reader to have read-ahead enabled.
302 static struct cl_readahead
*
303 cluster_get_rap(vnode_t vp
)
305 struct ubc_info
*ubc
;
306 struct cl_readahead
*rap
;
310 if ((rap
= ubc
->cl_rahead
) == NULL
) {
311 MALLOC_ZONE(rap
, struct cl_readahead
*, sizeof *rap
, M_CLRDAHEAD
, M_WAITOK
);
313 bzero(rap
, sizeof *rap
);
315 lck_mtx_init(&rap
->cl_lockr
, cl_mtx_grp
, cl_mtx_attr
);
319 if (ubc
->cl_rahead
== NULL
)
320 ubc
->cl_rahead
= rap
;
322 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
323 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
324 rap
= ubc
->cl_rahead
;
328 if (lck_mtx_try_lock(&rap
->cl_lockr
) == TRUE
)
331 return ((struct cl_readahead
*)NULL
);
336 * if the write behind context doesn't yet exist,
337 * and CLW_ALLOCATE is specified, allocate and initialize it...
338 * the vnode lock serializes multiple callers
339 * during the actual assignment... first one
340 * to grab the lock wins... the other callers
341 * will release the now unnecessary storage
343 * if CLW_RETURNLOCKED is set, grab (blocking if necessary)
344 * the lock associated with the write behind context before
348 static struct cl_writebehind
*
349 cluster_get_wbp(vnode_t vp
, int flags
)
351 struct ubc_info
*ubc
;
352 struct cl_writebehind
*wbp
;
356 if ((wbp
= ubc
->cl_wbehind
) == NULL
) {
358 if ( !(flags
& CLW_ALLOCATE
))
359 return ((struct cl_writebehind
*)NULL
);
361 MALLOC_ZONE(wbp
, struct cl_writebehind
*, sizeof *wbp
, M_CLWRBEHIND
, M_WAITOK
);
363 bzero(wbp
, sizeof *wbp
);
364 lck_mtx_init(&wbp
->cl_lockw
, cl_mtx_grp
, cl_mtx_attr
);
368 if (ubc
->cl_wbehind
== NULL
)
369 ubc
->cl_wbehind
= wbp
;
371 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
372 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
373 wbp
= ubc
->cl_wbehind
;
377 if (flags
& CLW_RETURNLOCKED
)
378 lck_mtx_lock(&wbp
->cl_lockw
);
385 cluster_syncup(vnode_t vp
, off_t newEOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
387 struct cl_writebehind
*wbp
;
389 if ((wbp
= cluster_get_wbp(vp
, 0)) != NULL
) {
391 if (wbp
->cl_number
) {
392 lck_mtx_lock(&wbp
->cl_lockw
);
394 cluster_try_push(wbp
, vp
, newEOF
, PUSH_ALL
| PUSH_SYNC
, callback
, callback_arg
);
396 lck_mtx_unlock(&wbp
->cl_lockw
);
403 cluster_hard_throttle_on(vnode_t vp
)
405 static struct timeval hard_throttle_maxelapsed
= { 0, 200000 };
407 if (vp
->v_mount
->mnt_kern_flag
& MNTK_ROOTDEV
) {
408 struct timeval elapsed
;
410 if (hard_throttle_on_root
)
413 microuptime(&elapsed
);
414 timevalsub(&elapsed
, &priority_IO_timestamp_for_root
);
416 if (timevalcmp(&elapsed
, &hard_throttle_maxelapsed
, <))
424 cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
)
426 int upl_abort_code
= 0;
430 if (io_flags
& B_PHYS
)
432 * direct write of any flavor, or a direct read that wasn't aligned
434 ubc_upl_commit_range(upl
, upl_offset
, abort_size
, UPL_COMMIT_FREE_ON_EMPTY
);
436 if (io_flags
& B_PAGEIO
) {
437 if (io_flags
& B_READ
)
442 if (io_flags
& B_CACHE
)
444 * leave pages in the cache unchanged on error
446 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
447 else if (page_out
&& (error
!= ENXIO
))
449 * transient error... leave pages unchanged
451 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
453 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
455 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
457 ubc_upl_abort_range(upl
, upl_offset
, abort_size
, upl_abort_code
);
459 return (upl_abort_code
);
464 cluster_iodone(buf_t bp
, void *callback_arg
)
475 int transaction_size
= 0;
481 struct clios
*iostate
;
482 boolean_t transaction_complete
= FALSE
;
484 cbp_head
= (buf_t
)(bp
->b_trans_head
);
486 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_START
,
487 (int)cbp_head
, bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
489 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
491 * all I/O requests that are part of this transaction
492 * have to complete before we can process it
494 if ( !(cbp
->b_flags
& B_DONE
)) {
496 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
497 (int)cbp_head
, (int)cbp
, cbp
->b_bcount
, cbp
->b_flags
, 0);
501 if (cbp
->b_flags
& B_EOT
)
502 transaction_complete
= TRUE
;
504 if (transaction_complete
== FALSE
) {
505 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
506 (int)cbp_head
, 0, 0, 0, 0);
515 upl_offset
= cbp
->b_uploffset
;
517 b_flags
= cbp
->b_flags
;
518 real_bp
= cbp
->b_real_bp
;
519 zero_offset
= cbp
->b_validend
;
520 iostate
= (struct clios
*)cbp
->b_iostate
;
523 real_bp
->b_dev
= cbp
->b_dev
;
526 if ((cbp
->b_flags
& B_ERROR
) && error
== 0)
527 error
= cbp
->b_error
;
529 total_resid
+= cbp
->b_resid
;
530 total_size
+= cbp
->b_bcount
;
532 cbp_next
= cbp
->b_trans_next
;
534 if (cbp_next
== NULL
)
536 * compute the overall size of the transaction
537 * in case we created one that has 'holes' in it
538 * 'total_size' represents the amount of I/O we
539 * did, not the span of the transaction w/r to the UPL
541 transaction_size
= cbp
->b_uploffset
+ cbp
->b_bcount
- upl_offset
;
548 if (error
== 0 && total_resid
)
552 int (*cliodone_func
)(buf_t
, void *) = (int (*)(buf_t
, void *))(cbp_head
->b_cliodone
);
554 if (cliodone_func
!= NULL
) {
555 cbp_head
->b_bcount
= transaction_size
;
557 error
= (*cliodone_func
)(cbp_head
, callback_arg
);
561 cluster_zero(upl
, zero_offset
, PAGE_SIZE
- (zero_offset
& PAGE_MASK
), real_bp
);
563 free_io_buf(cbp_head
);
569 * someone has issued multiple I/Os asynchrounsly
570 * and is waiting for them to complete (streaming)
572 lck_mtx_lock_spin(cl_mtxp
);
574 if (error
&& iostate
->io_error
== 0)
575 iostate
->io_error
= error
;
577 iostate
->io_completed
+= total_size
;
579 if (iostate
->io_wanted
) {
581 * someone is waiting for the state of
582 * this io stream to change
584 iostate
->io_wanted
= 0;
587 lck_mtx_unlock(cl_mtxp
);
590 wakeup((caddr_t
)&iostate
->io_wanted
);
593 if (b_flags
& B_COMMIT_UPL
) {
595 pg_offset
= upl_offset
& PAGE_MASK
;
596 commit_size
= (pg_offset
+ transaction_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
599 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, commit_size
, error
, b_flags
);
601 upl_flags
= UPL_COMMIT_FREE_ON_EMPTY
;
603 if ((b_flags
& B_PHYS
) && (b_flags
& B_READ
))
604 upl_flags
|= UPL_COMMIT_SET_DIRTY
;
607 upl_flags
|= UPL_COMMIT_INACTIVATE
;
609 ubc_upl_commit_range(upl
, upl_offset
- pg_offset
, commit_size
, upl_flags
);
612 if ((b_flags
& B_NEED_IODONE
) && real_bp
) {
614 real_bp
->b_flags
|= B_ERROR
;
615 real_bp
->b_error
= error
;
617 real_bp
->b_resid
= total_resid
;
619 buf_biodone(real_bp
);
621 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
622 (int)upl
, upl_offset
- pg_offset
, commit_size
, (error
<< 24) | upl_flags
, 0);
629 cluster_zero(upl_t upl
, vm_offset_t upl_offset
, int size
, buf_t bp
)
632 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_START
,
633 upl_offset
, size
, (int)bp
, 0, 0);
635 if (bp
== NULL
|| bp
->b_datap
== 0) {
639 pl
= ubc_upl_pageinfo(upl
);
641 if (upl_device_page(pl
) == TRUE
) {
642 zero_addr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + upl_offset
;
644 bzero_phys_nc(zero_addr
, size
);
651 page_index
= upl_offset
/ PAGE_SIZE
;
652 page_offset
= upl_offset
& PAGE_MASK
;
654 zero_addr
= ((addr64_t
)upl_phys_page(pl
, page_index
) << 12) + page_offset
;
655 zero_cnt
= min(PAGE_SIZE
- page_offset
, size
);
657 bzero_phys(zero_addr
, zero_cnt
);
660 upl_offset
+= zero_cnt
;
664 bzero((caddr_t
)((vm_offset_t
)bp
->b_datap
+ upl_offset
), size
);
666 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_END
,
667 upl_offset
, size
, 0, 0, 0);
672 cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
)
674 cbp_head
->b_validend
= zero_offset
;
675 cbp_tail
->b_flags
|= B_EOT
;
679 cluster_wait_IO(buf_t cbp_head
, int async
)
685 * async callback completion will not normally
686 * generate a wakeup upon I/O completion...
687 * by setting BL_WANTED, we will force a wakeup
688 * to occur as any outstanding I/Os complete...
689 * I/Os already completed will have BL_CALLDONE already
690 * set and we won't block in buf_biowait_callback..
691 * note that we're actually waiting for the bp to have
692 * completed the callback function... only then
693 * can we safely take back ownership of the bp
694 * need the main buf mutex in order to safely
699 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
700 cbp
->b_lflags
|= BL_WANTED
;
704 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
706 buf_biowait_callback(cbp
);
713 cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
)
719 * cluster_complete_transaction will
720 * only be called if we've issued a complete chain in synchronous mode
721 * or, we've already done a cluster_wait_IO on an incomplete chain
724 for (cbp
= *cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
727 error
= cluster_iodone(*cbp_head
, callback_arg
);
729 if ( !(flags
& CL_ASYNC
) && error
&& *retval
== 0) {
730 if (((flags
& (CL_PAGEOUT
| CL_KEEPCACHED
)) != CL_PAGEOUT
) || (error
!= ENXIO
))
733 *cbp_head
= (buf_t
)NULL
;
738 cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
739 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*callback
)(buf_t
, void *), void *callback_arg
)
748 buf_t cbp_head
= NULL
;
749 buf_t cbp_tail
= NULL
;
758 int async_throttle
= 0;
760 vm_offset_t upl_end_offset
;
761 boolean_t need_EOT
= FALSE
;
764 * we currently don't support buffers larger than a page
766 if (real_bp
&& non_rounded_size
> PAGE_SIZE
)
767 panic("%s(): Called with real buffer of size %d bytes which "
768 "is greater than the maximum allowed size of "
769 "%d bytes (the system PAGE_SIZE).\n",
770 __FUNCTION__
, non_rounded_size
, PAGE_SIZE
);
775 * we don't want to do any funny rounding of the size for IO requests
776 * coming through the DIRECT or CONTIGUOUS paths... those pages don't
777 * belong to us... we can't extend (nor do we need to) the I/O to fill
780 if (mp
->mnt_devblocksize
> 1 && !(flags
& (CL_DEV_MEMORY
| CL_DIRECT_IO
))) {
782 * round the requested size up so that this I/O ends on a
783 * page boundary in case this is a 'write'... if the filesystem
784 * has blocks allocated to back the page beyond the EOF, we want to
785 * make sure to write out the zero's that are sitting beyond the EOF
786 * so that in case the filesystem doesn't explicitly zero this area
787 * if a hole is created via a lseek/write beyond the current EOF,
788 * it will return zeros when it's read back from the disk. If the
789 * physical allocation doesn't extend for the whole page, we'll
790 * only write/read from the disk up to the end of this allocation
791 * via the extent info returned from the VNOP_BLOCKMAP call.
793 pg_offset
= upl_offset
& PAGE_MASK
;
795 size
= (((non_rounded_size
+ pg_offset
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - pg_offset
;
798 * anyone advertising a blocksize of 1 byte probably
799 * can't deal with us rounding up the request size
800 * AFP is one such filesystem/device
802 size
= non_rounded_size
;
804 upl_end_offset
= upl_offset
+ size
;
806 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_START
, (int)f_offset
, size
, upl_offset
, flags
, 0);
809 * Set the maximum transaction size to the maximum desired number of
813 if (flags
& CL_DEV_MEMORY
)
814 max_trans_count
= 16;
816 if (flags
& CL_READ
) {
818 bmap_flags
= VNODE_READ
;
820 max_iosize
= mp
->mnt_maxreadcnt
;
821 max_vectors
= mp
->mnt_segreadcnt
;
824 bmap_flags
= VNODE_WRITE
;
826 max_iosize
= mp
->mnt_maxwritecnt
;
827 max_vectors
= mp
->mnt_segwritecnt
;
829 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_NONE
, max_iosize
, max_vectors
, mp
->mnt_devblocksize
, 0, 0);
832 * make sure the maximum iosize is a
833 * multiple of the page size
835 max_iosize
&= ~PAGE_MASK
;
838 * Ensure the maximum iosize is sensible.
841 max_iosize
= PAGE_SIZE
;
843 if (flags
& CL_THROTTLE
) {
844 if ( !(flags
& CL_PAGEOUT
) && cluster_hard_throttle_on(vp
)) {
845 if (max_iosize
> HARD_THROTTLE_MAXSIZE
)
846 max_iosize
= HARD_THROTTLE_MAXSIZE
;
847 async_throttle
= HARD_THROTTLE_MAXCNT
;
849 if ( (flags
& CL_DEV_MEMORY
) )
850 async_throttle
= VNODE_ASYNC_THROTTLE
;
853 u_int max_cluster_size
;
856 max_cluster_size
= MAX_CLUSTER_SIZE(vp
);
857 max_prefetch
= MAX_PREFETCH(vp
);
859 if (max_iosize
> max_cluster_size
)
860 max_cluster
= max_cluster_size
;
862 max_cluster
= max_iosize
;
864 if (size
< max_cluster
)
867 async_throttle
= min(VNODE_ASYNC_THROTTLE
, (max_prefetch
/ max_cluster
) - 1);
873 if (flags
& (CL_PAGEIN
| CL_PAGEOUT
))
874 io_flags
|= B_PAGEIO
;
875 if (flags
& CL_COMMIT
)
876 io_flags
|= B_COMMIT_UPL
;
877 if (flags
& CL_PRESERVE
)
879 if (flags
& CL_KEEPCACHED
)
881 if (flags
& CL_PASSIVE
)
882 io_flags
|= B_PASSIVE
;
883 if (vp
->v_flag
& VSYSTEM
)
886 if ((flags
& CL_READ
) && ((upl_offset
+ non_rounded_size
) & PAGE_MASK
) && (!(flags
& CL_NOZERO
))) {
888 * then we are going to end up
889 * with a page that we can't complete (the file size wasn't a multiple
890 * of PAGE_SIZE and we're trying to read to the end of the file
891 * so we'll go ahead and zero out the portion of the page we can't
892 * read in from the file
894 zero_offset
= upl_offset
+ non_rounded_size
;
899 u_int io_size_wanted
;
901 if (size
> max_iosize
)
902 io_size
= max_iosize
;
906 io_size_wanted
= io_size
;
908 if ((error
= VNOP_BLOCKMAP(vp
, f_offset
, io_size
, &blkno
, (size_t *)&io_size
, NULL
, bmap_flags
, NULL
)))
911 if (io_size
> io_size_wanted
)
912 io_size
= io_size_wanted
;
914 if (real_bp
&& (real_bp
->b_blkno
== real_bp
->b_lblkno
))
915 real_bp
->b_blkno
= blkno
;
917 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 24)) | DBG_FUNC_NONE
,
918 (int)f_offset
, (int)(blkno
>>32), (int)blkno
, io_size
, 0);
922 * vnop_blockmap didn't return an error... however, it did
923 * return an extent size of 0 which means we can't
924 * make forward progress on this I/O... a hole in the
925 * file would be returned as a blkno of -1 with a non-zero io_size
926 * a real extent is returned with a blkno != -1 and a non-zero io_size
931 if ( !(flags
& CL_READ
) && blkno
== -1) {
936 * we're writing into a 'hole'
938 if (flags
& CL_PAGEOUT
) {
940 * if we got here via cluster_pageout
941 * then just error the request and return
942 * the 'hole' should already have been covered
948 * we can get here if the cluster code happens to
949 * pick up a page that was dirtied via mmap vs
950 * a 'write' and the page targets a 'hole'...
951 * i.e. the writes to the cluster were sparse
952 * and the file was being written for the first time
954 * we can also get here if the filesystem supports
955 * 'holes' that are less than PAGE_SIZE.... because
956 * we can't know if the range in the page that covers
957 * the 'hole' has been dirtied via an mmap or not,
958 * we have to assume the worst and try to push the
959 * entire page to storage.
961 * Try paging out the page individually before
962 * giving up entirely and dumping it (the pageout
963 * path will insure that the zero extent accounting
964 * has been taken care of before we get back into cluster_io)
966 * go direct to vnode_pageout so that we don't have to
967 * unbusy the page from the UPL... we used to do this
968 * so that we could call ubc_sync_range, but that results
969 * in a potential deadlock if someone else races us to acquire
970 * that page and wins and in addition needs one of the pages
971 * we're continuing to hold in the UPL
973 pageout_flags
= UPL_MSYNC
| UPL_VNODE_PAGER
| UPL_NESTED_PAGEOUT
;
975 if ( !(flags
& CL_ASYNC
))
976 pageout_flags
|= UPL_IOSYNC
;
977 if ( !(flags
& CL_COMMIT
))
978 pageout_flags
|= UPL_NOCOMMIT
;
984 * first we have to wait for the the current outstanding I/Os
985 * to complete... EOT hasn't been set yet on this transaction
986 * so the pages won't be released just because all of the current
987 * I/O linked to this transaction has completed...
989 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
992 * we've got a transcation that
993 * includes the page we're about to push out through vnode_pageout...
994 * find the last bp in the list which will be the one that
995 * includes the head of this page and round it's iosize down
996 * to a page boundary...
998 for (last_cbp
= cbp
= cbp_head
; cbp
->b_trans_next
; cbp
= cbp
->b_trans_next
)
1001 cbp
->b_bcount
&= ~PAGE_MASK
;
1003 if (cbp
->b_bcount
== 0) {
1005 * this buf no longer has any I/O associated with it
1009 if (cbp
== cbp_head
) {
1011 * the buf we just freed was the only buf in
1012 * this transaction... so there's no I/O to do
1017 * remove the buf we just freed from
1018 * the transaction list
1020 last_cbp
->b_trans_next
= NULL
;
1021 cbp_tail
= last_cbp
;
1026 * there was more to the current transaction
1027 * than just the page we are pushing out via vnode_pageout...
1028 * mark it as finished and complete it... we've already
1029 * waited for the I/Os to complete above in the call to cluster_wait_IO
1031 cluster_EOT(cbp_head
, cbp_tail
, 0);
1033 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1038 if (vnode_pageout(vp
, upl
, trunc_page(upl_offset
), trunc_page_64(f_offset
), PAGE_SIZE
, pageout_flags
, NULL
) != PAGER_SUCCESS
) {
1042 e_offset
= round_page_64(f_offset
+ 1);
1043 io_size
= e_offset
- f_offset
;
1045 f_offset
+= io_size
;
1046 upl_offset
+= io_size
;
1048 if (size
>= io_size
)
1053 * keep track of how much of the original request
1054 * that we've actually completed... non_rounded_size
1055 * may go negative due to us rounding the request
1056 * to a page size multiple (i.e. size > non_rounded_size)
1058 non_rounded_size
-= io_size
;
1060 if (non_rounded_size
<= 0) {
1062 * we've transferred all of the data in the original
1063 * request, but we were unable to complete the tail
1064 * of the last page because the file didn't have
1065 * an allocation to back that portion... this is ok.
1071 lblkno
= (daddr64_t
)(f_offset
/ PAGE_SIZE_64
);
1073 * we have now figured out how much I/O we can do - this is in 'io_size'
1074 * pg_offset is the starting point in the first page for the I/O
1075 * pg_count is the number of full and partial pages that 'io_size' encompasses
1077 pg_offset
= upl_offset
& PAGE_MASK
;
1079 if (flags
& CL_DEV_MEMORY
) {
1081 * treat physical requests as one 'giant' page
1085 pg_count
= (io_size
+ pg_offset
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1087 if ((flags
& CL_READ
) && blkno
== -1) {
1088 vm_offset_t commit_offset
;
1090 int complete_transaction_now
= 0;
1093 * if we're reading and blkno == -1, then we've got a
1094 * 'hole' in the file that we need to deal with by zeroing
1095 * out the affected area in the upl
1097 if (io_size
>= (u_int
)non_rounded_size
) {
1099 * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
1100 * than 'zero_offset' will be non-zero
1101 * if the 'hole' returned by vnop_blockmap extends all the way to the eof
1102 * (indicated by the io_size finishing off the I/O request for this UPL)
1103 * than we're not going to issue an I/O for the
1104 * last page in this upl... we need to zero both the hole and the tail
1105 * of the page beyond the EOF, since the delayed zero-fill won't kick in
1107 bytes_to_zero
= non_rounded_size
;
1108 if (!(flags
& CL_NOZERO
))
1109 bytes_to_zero
= (((upl_offset
+ io_size
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - upl_offset
;
1113 bytes_to_zero
= io_size
;
1117 cluster_zero(upl
, upl_offset
, bytes_to_zero
, real_bp
);
1123 * if there is a current I/O chain pending
1124 * then the first page of the group we just zero'd
1125 * will be handled by the I/O completion if the zero
1126 * fill started in the middle of the page
1128 commit_offset
= (upl_offset
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1130 pg_resid
= commit_offset
- upl_offset
;
1132 if (bytes_to_zero
>= pg_resid
) {
1134 * the last page of the current I/O
1135 * has been completed...
1136 * compute the number of fully zero'd
1137 * pages that are beyond it
1138 * plus the last page if its partial
1139 * and we have no more I/O to issue...
1140 * otherwise a partial page is left
1141 * to begin the next I/O
1143 if ((int)io_size
>= non_rounded_size
)
1144 pg_count
= (bytes_to_zero
- pg_resid
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1146 pg_count
= (bytes_to_zero
- pg_resid
) / PAGE_SIZE
;
1148 complete_transaction_now
= 1;
1152 * no pending I/O to deal with
1153 * so, commit all of the fully zero'd pages
1154 * plus the last page if its partial
1155 * and we have no more I/O to issue...
1156 * otherwise a partial page is left
1157 * to begin the next I/O
1159 if ((int)io_size
>= non_rounded_size
)
1160 pg_count
= (pg_offset
+ bytes_to_zero
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1162 pg_count
= (pg_offset
+ bytes_to_zero
) / PAGE_SIZE
;
1164 commit_offset
= upl_offset
& ~PAGE_MASK
;
1166 if ( (flags
& CL_COMMIT
) && pg_count
) {
1167 ubc_upl_commit_range(upl
, commit_offset
, pg_count
* PAGE_SIZE
,
1168 UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
);
1170 upl_offset
+= io_size
;
1171 f_offset
+= 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.
1191 if (cbp_head
&& (complete_transaction_now
|| size
== 0)) {
1192 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1194 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1196 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1202 if (pg_count
> max_vectors
) {
1203 if (((pg_count
- max_vectors
) * PAGE_SIZE
) > io_size
) {
1204 io_size
= PAGE_SIZE
- pg_offset
;
1207 io_size
-= (pg_count
- max_vectors
) * PAGE_SIZE
;
1208 pg_count
= max_vectors
;
1212 * If the transaction is going to reach the maximum number of
1213 * desired elements, truncate the i/o to the nearest page so
1214 * that the actual i/o is initiated after this buffer is
1215 * created and added to the i/o chain.
1217 * I/O directed to physically contiguous memory
1218 * doesn't have a requirement to make sure we 'fill' a page
1220 if ( !(flags
& CL_DEV_MEMORY
) && trans_count
>= max_trans_count
&&
1221 ((upl_offset
+ io_size
) & PAGE_MASK
)) {
1222 vm_offset_t aligned_ofs
;
1224 aligned_ofs
= (upl_offset
+ io_size
) & ~PAGE_MASK
;
1226 * If the io_size does not actually finish off even a
1227 * single page we have to keep adding buffers to the
1228 * transaction despite having reached the desired limit.
1230 * Eventually we get here with the page being finished
1231 * off (and exceeded) and then we truncate the size of
1232 * this i/o request so that it is page aligned so that
1233 * we can finally issue the i/o on the transaction.
1235 if (aligned_ofs
> upl_offset
) {
1236 io_size
= aligned_ofs
- upl_offset
;
1241 if ( !(mp
->mnt_kern_flag
& MNTK_VIRTUALDEV
))
1243 * if we're not targeting a virtual device i.e. a disk image
1244 * it's safe to dip into the reserve pool since real devices
1245 * can complete this I/O request without requiring additional
1246 * bufs from the alloc_io_buf pool
1249 else if ((flags
& CL_ASYNC
) && !(flags
& CL_PAGEOUT
))
1251 * Throttle the speculative IO
1257 cbp
= alloc_io_buf(vp
, priv
);
1259 if (flags
& CL_PAGEOUT
) {
1262 for (i
= 0; i
< pg_count
; i
++) {
1263 if (buf_invalblkno(vp
, lblkno
+ i
, 0) == EBUSY
)
1264 panic("BUSY bp found in cluster_io");
1267 if (flags
& CL_ASYNC
) {
1268 if (buf_setcallback(cbp
, (void *)cluster_iodone
, callback_arg
))
1269 panic("buf_setcallback failed\n");
1271 cbp
->b_cliodone
= (void *)callback
;
1272 cbp
->b_flags
|= io_flags
;
1274 cbp
->b_lblkno
= lblkno
;
1275 cbp
->b_blkno
= blkno
;
1276 cbp
->b_bcount
= io_size
;
1278 if (buf_setupl(cbp
, upl
, upl_offset
))
1279 panic("buf_setupl failed\n");
1281 cbp
->b_trans_next
= (buf_t
)NULL
;
1283 if ((cbp
->b_iostate
= (void *)iostate
))
1285 * caller wants to track the state of this
1286 * io... bump the amount issued against this stream
1288 iostate
->io_issued
+= io_size
;
1290 if (flags
& CL_READ
) {
1291 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 26)) | DBG_FUNC_NONE
,
1292 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1295 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 27)) | DBG_FUNC_NONE
,
1296 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1300 cbp_tail
->b_trans_next
= cbp
;
1306 if ( (cbp_head
->b_real_bp
= real_bp
) ) {
1307 cbp_head
->b_flags
|= B_NEED_IODONE
;
1308 real_bp
= (buf_t
)NULL
;
1311 *(buf_t
*)(&cbp
->b_trans_head
) = cbp_head
;
1315 upl_offset
+= io_size
;
1316 f_offset
+= io_size
;
1319 * keep track of how much of the original request
1320 * that we've actually completed... non_rounded_size
1321 * may go negative due to us rounding the request
1322 * to a page size multiple (i.e. size > non_rounded_size)
1324 non_rounded_size
-= io_size
;
1326 if (non_rounded_size
<= 0) {
1328 * we've transferred all of the data in the original
1329 * request, but we were unable to complete the tail
1330 * of the last page because the file didn't have
1331 * an allocation to back that portion... this is ok.
1337 * we have no more I/O to issue, so go
1338 * finish the final transaction
1341 } else if ( ((flags
& CL_DEV_MEMORY
) || (upl_offset
& PAGE_MASK
) == 0) &&
1342 ((flags
& CL_ASYNC
) || trans_count
> max_trans_count
) ) {
1344 * I/O directed to physically contiguous memory...
1345 * which doesn't have a requirement to make sure we 'fill' a page
1347 * the current I/O we've prepared fully
1348 * completes the last page in this request
1350 * it's either an ASYNC request or
1351 * we've already accumulated more than 8 I/O's into
1352 * this transaction so mark it as complete so that
1353 * it can finish asynchronously or via the cluster_complete_transaction
1354 * below if the request is synchronous
1358 if (need_EOT
== TRUE
)
1359 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1361 if (flags
& CL_THROTTLE
)
1362 (void)vnode_waitforwrites(vp
, async_throttle
, 0, 0, "cluster_io");
1364 if ( !(io_flags
& B_READ
))
1365 vnode_startwrite(vp
);
1367 (void) VNOP_STRATEGY(cbp
);
1369 if (need_EOT
== TRUE
) {
1370 if ( !(flags
& CL_ASYNC
))
1371 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 1);
1385 * first wait until all of the outstanding I/O
1386 * for this partial transaction has completed
1388 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1391 * Rewind the upl offset to the beginning of the
1394 upl_offset
= cbp_head
->b_uploffset
;
1396 for (cbp
= cbp_head
; cbp
;) {
1399 size
+= cbp
->b_bcount
;
1400 io_size
+= cbp
->b_bcount
;
1402 cbp_next
= cbp
->b_trans_next
;
1408 int need_wakeup
= 0;
1411 * update the error condition for this stream
1412 * since we never really issued the io
1413 * just go ahead and adjust it back
1415 lck_mtx_lock_spin(cl_mtxp
);
1417 if (iostate
->io_error
== 0)
1418 iostate
->io_error
= error
;
1419 iostate
->io_issued
-= io_size
;
1421 if (iostate
->io_wanted
) {
1423 * someone is waiting for the state of
1424 * this io stream to change
1426 iostate
->io_wanted
= 0;
1429 lck_mtx_unlock(cl_mtxp
);
1432 wakeup((caddr_t
)&iostate
->io_wanted
);
1434 if (flags
& CL_COMMIT
) {
1437 pg_offset
= upl_offset
& PAGE_MASK
;
1438 abort_size
= (upl_end_offset
- upl_offset
+ PAGE_MASK
) & ~PAGE_MASK
;
1440 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, abort_size
, error
, io_flags
);
1442 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 28)) | DBG_FUNC_NONE
,
1443 (int)upl
, upl_offset
- pg_offset
, abort_size
, (error
<< 24) | upl_flags
, 0);
1447 } else if (cbp_head
)
1448 panic("%s(): cbp_head is not NULL.\n", __FUNCTION__
);
1452 * can get here if we either encountered an error
1453 * or we completely zero-filled the request and
1457 real_bp
->b_flags
|= B_ERROR
;
1458 real_bp
->b_error
= error
;
1460 buf_biodone(real_bp
);
1462 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_END
, (int)f_offset
, size
, upl_offset
, retval
, 0);
1469 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
)
1471 int pages_in_prefetch
;
1473 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_START
,
1474 (int)f_offset
, size
, (int)filesize
, 0, 0);
1476 if (f_offset
>= filesize
) {
1477 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1478 (int)f_offset
, 0, 0, 0, 0);
1481 if ((off_t
)size
> (filesize
- f_offset
))
1482 size
= filesize
- f_offset
;
1483 pages_in_prefetch
= (size
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1485 advisory_read_ext(vp
, filesize
, f_offset
, size
, callback
, callback_arg
, bflag
);
1487 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1488 (int)f_offset
+ size
, pages_in_prefetch
, 0, 1, 0);
1490 return (pages_in_prefetch
);
1496 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
,
1501 int size_of_prefetch
;
1505 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_START
,
1506 (int)extent
->b_addr
, (int)extent
->e_addr
, (int)rap
->cl_lastr
, 0, 0);
1508 if (extent
->b_addr
== rap
->cl_lastr
&& extent
->b_addr
== extent
->e_addr
) {
1509 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1510 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 0, 0);
1513 if (rap
->cl_lastr
== -1 || (extent
->b_addr
!= rap
->cl_lastr
&& extent
->b_addr
!= (rap
->cl_lastr
+ 1))) {
1517 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1518 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 1, 0);
1522 max_prefetch
= MAX_PREFETCH(vp
);
1524 if (extent
->e_addr
< rap
->cl_maxra
) {
1525 if ((rap
->cl_maxra
- extent
->e_addr
) > ((max_prefetch
/ PAGE_SIZE
) / 4)) {
1527 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1528 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 2, 0);
1532 r_addr
= max(extent
->e_addr
, rap
->cl_maxra
) + 1;
1533 f_offset
= (off_t
)(r_addr
* PAGE_SIZE_64
);
1535 size_of_prefetch
= 0;
1537 ubc_range_op(vp
, f_offset
, f_offset
+ PAGE_SIZE_64
, UPL_ROP_PRESENT
, &size_of_prefetch
);
1539 if (size_of_prefetch
) {
1540 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1541 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 3, 0);
1544 if (f_offset
< filesize
) {
1545 daddr64_t read_size
;
1547 rap
->cl_ralen
= rap
->cl_ralen
? min(max_prefetch
/ PAGE_SIZE
, rap
->cl_ralen
<< 1) : 1;
1549 read_size
= (extent
->e_addr
+ 1) - extent
->b_addr
;
1551 if (read_size
> rap
->cl_ralen
) {
1552 if (read_size
> max_prefetch
/ PAGE_SIZE
)
1553 rap
->cl_ralen
= max_prefetch
/ PAGE_SIZE
;
1555 rap
->cl_ralen
= read_size
;
1557 size_of_prefetch
= cluster_read_prefetch(vp
, f_offset
, rap
->cl_ralen
* PAGE_SIZE
, filesize
, callback
, callback_arg
, bflag
);
1559 if (size_of_prefetch
)
1560 rap
->cl_maxra
= (r_addr
+ size_of_prefetch
) - 1;
1562 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1563 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 4, 0);
1568 cluster_pageout(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
,
1569 int size
, off_t filesize
, int flags
)
1571 return cluster_pageout_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
1577 cluster_pageout_ext(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
,
1578 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1585 if (vp
->v_mount
->mnt_kern_flag
& MNTK_VIRTUALDEV
)
1587 * if we know we're issuing this I/O to a virtual device (i.e. disk image)
1588 * then we don't want to enforce this throttle... if we do, we can
1589 * potentially deadlock since we're stalling the pageout thread at a time
1590 * when the disk image might need additional memory (which won't be available
1591 * if the pageout thread can't run)... instead we'll just depend on the throttle
1592 * that the pageout thread now has in place to deal with external files
1594 local_flags
= CL_PAGEOUT
;
1596 local_flags
= CL_PAGEOUT
| CL_THROTTLE
;
1598 if ((flags
& UPL_IOSYNC
) == 0)
1599 local_flags
|= CL_ASYNC
;
1600 if ((flags
& UPL_NOCOMMIT
) == 0)
1601 local_flags
|= CL_COMMIT
;
1602 if ((flags
& UPL_KEEPCACHED
))
1603 local_flags
|= CL_KEEPCACHED
;
1604 if (flags
& IO_PASSIVE
)
1605 local_flags
|= CL_PASSIVE
;
1608 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 52)) | DBG_FUNC_NONE
,
1609 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
1612 * If they didn't specify any I/O, then we are done...
1613 * we can't issue an abort because we don't know how
1614 * big the upl really is
1619 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
1620 if (local_flags
& CL_COMMIT
)
1621 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
1625 * can't page-in from a negative offset
1626 * or if we're starting beyond the EOF
1627 * or if the file offset isn't page aligned
1628 * or the size requested isn't a multiple of PAGE_SIZE
1630 if (f_offset
< 0 || f_offset
>= filesize
||
1631 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
)) {
1632 if (local_flags
& CL_COMMIT
)
1633 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
1636 max_size
= filesize
- f_offset
;
1638 if (size
< max_size
)
1643 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1645 if (size
> rounded_size
) {
1646 if (local_flags
& CL_COMMIT
)
1647 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
, size
- rounded_size
,
1648 UPL_ABORT_FREE_ON_EMPTY
);
1650 return (cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
1651 local_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
));
1656 cluster_pagein(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
,
1657 int size
, off_t filesize
, int flags
)
1659 return cluster_pagein_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
1664 cluster_pagein_ext(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
,
1665 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1671 int local_flags
= 0;
1673 if (upl
== NULL
|| size
< 0)
1674 panic("cluster_pagein: NULL upl passed in");
1676 if ((flags
& UPL_IOSYNC
) == 0)
1677 local_flags
|= CL_ASYNC
;
1678 if ((flags
& UPL_NOCOMMIT
) == 0)
1679 local_flags
|= CL_COMMIT
;
1680 if (flags
& IO_PASSIVE
)
1681 local_flags
|= CL_PASSIVE
;
1684 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 56)) | DBG_FUNC_NONE
,
1685 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
1688 * can't page-in from a negative offset
1689 * or if we're starting beyond the EOF
1690 * or if the file offset isn't page aligned
1691 * or the size requested isn't a multiple of PAGE_SIZE
1693 if (f_offset
< 0 || f_offset
>= filesize
||
1694 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
) || (upl_offset
& PAGE_MASK
)) {
1695 if (local_flags
& CL_COMMIT
)
1696 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
1699 max_size
= filesize
- f_offset
;
1701 if (size
< max_size
)
1706 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1708 if (size
> rounded_size
&& (local_flags
& CL_COMMIT
))
1709 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
,
1710 size
- rounded_size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
1712 retval
= cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
1713 local_flags
| CL_READ
| CL_PAGEIN
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
1720 cluster_bp(buf_t bp
)
1722 return cluster_bp_ext(bp
, NULL
, NULL
);
1727 cluster_bp_ext(buf_t bp
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1732 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 19)) | DBG_FUNC_START
,
1733 (int)bp
, (int)bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
1735 if (bp
->b_flags
& B_READ
)
1736 flags
= CL_ASYNC
| CL_READ
;
1739 if (bp
->b_flags
& B_PASSIVE
)
1740 flags
|= CL_PASSIVE
;
1742 f_offset
= ubc_blktooff(bp
->b_vp
, bp
->b_lblkno
);
1744 return (cluster_io(bp
->b_vp
, bp
->b_upl
, 0, f_offset
, bp
->b_bcount
, flags
, bp
, (struct clios
*)NULL
, callback
, callback_arg
));
1750 cluster_write(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
, int xflags
)
1752 return cluster_write_ext(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, xflags
, NULL
, NULL
);
1757 cluster_write_ext(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
,
1758 int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1760 user_ssize_t cur_resid
;
1765 int write_type
= IO_COPY
;
1766 u_int32_t write_length
;
1770 if (flags
& IO_PASSIVE
)
1775 if (vp
->v_flag
& VNOCACHE_DATA
)
1776 flags
|= IO_NOCACHE
;
1781 * this call is being made to zero-fill some range in the file
1783 retval
= cluster_write_copy(vp
, NULL
, (u_int32_t
)0, oldEOF
, newEOF
, headOff
, tailOff
, flags
, callback
, callback_arg
);
1788 * do a write through the cache if one of the following is true....
1789 * NOCACHE is not true and
1790 * the uio request doesn't target USERSPACE
1791 * otherwise, find out if we want the direct or contig variant for
1792 * the first vector in the uio request
1794 if ( (flags
& IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) )
1795 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
1797 if ( (flags
& (IO_TAILZEROFILL
| IO_HEADZEROFILL
)) && write_type
== IO_DIRECT
)
1799 * must go through the cached variant in this case
1801 write_type
= IO_COPY
;
1803 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< newEOF
&& retval
== 0) {
1805 switch (write_type
) {
1809 * make sure the uio_resid isn't too big...
1810 * internally, we want to handle all of the I/O in
1811 * chunk sizes that fit in a 32 bit int
1813 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
)) {
1815 * we're going to have to call cluster_write_copy
1818 * only want the last call to cluster_write_copy to
1819 * have the IO_TAILZEROFILL flag set and only the
1820 * first call should have IO_HEADZEROFILL
1822 zflags
= flags
& ~IO_TAILZEROFILL
;
1823 flags
&= ~IO_HEADZEROFILL
;
1825 write_length
= MAX_IO_REQUEST_SIZE
;
1828 * last call to cluster_write_copy
1832 write_length
= (u_int32_t
)cur_resid
;
1834 retval
= cluster_write_copy(vp
, uio
, write_length
, oldEOF
, newEOF
, headOff
, tailOff
, zflags
, callback
, callback_arg
);
1838 zflags
= flags
& ~(IO_TAILZEROFILL
| IO_HEADZEROFILL
);
1840 if (flags
& IO_HEADZEROFILL
) {
1842 * only do this once per request
1844 flags
&= ~IO_HEADZEROFILL
;
1846 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, uio
->uio_offset
,
1847 headOff
, (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
1851 retval
= cluster_write_contig(vp
, uio
, newEOF
, &write_type
, &write_length
, callback
, callback_arg
, bflag
);
1853 if (retval
== 0 && (flags
& IO_TAILZEROFILL
) && uio_resid(uio
) == 0) {
1855 * we're done with the data from the user specified buffer(s)
1856 * and we've been requested to zero fill at the tail
1857 * treat this as an IO_HEADZEROFILL which doesn't require a uio
1858 * by rearranging the args and passing in IO_HEADZEROFILL
1860 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, tailOff
, uio
->uio_offset
,
1861 (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
1867 * cluster_write_direct is never called with IO_TAILZEROFILL || IO_HEADZEROFILL
1869 retval
= cluster_write_direct(vp
, uio
, oldEOF
, newEOF
, &write_type
, &write_length
, flags
, callback
, callback_arg
);
1873 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
1882 cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
1883 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1886 upl_page_info_t
*pl
;
1887 vm_offset_t upl_offset
;
1888 u_int32_t io_req_size
;
1889 u_int32_t offset_in_file
;
1890 u_int32_t offset_in_iovbase
;
1895 vm_size_t upl_needed_size
;
1896 mach_msg_type_number_t pages_in_pl
;
1899 mach_msg_type_number_t i
;
1900 int force_data_sync
;
1903 struct clios iostate
;
1904 user_addr_t iov_base
;
1905 u_int32_t mem_alignment_mask
;
1906 u_int32_t devblocksize
;
1907 u_int32_t max_upl_size
;
1910 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
1912 if (flags
& IO_PASSIVE
)
1918 * When we enter this routine, we know
1919 * -- the resid will not exceed iov_len
1921 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_START
,
1922 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
1924 iostate
.io_completed
= 0;
1925 iostate
.io_issued
= 0;
1926 iostate
.io_error
= 0;
1927 iostate
.io_wanted
= 0;
1929 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
1930 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
1932 if (devblocksize
== 1) {
1934 * the AFP client advertises a devblocksize of 1
1935 * however, its BLOCKMAP routine maps to physical
1936 * blocks that are PAGE_SIZE in size...
1937 * therefore we can't ask for I/Os that aren't page aligned
1938 * or aren't multiples of PAGE_SIZE in size
1939 * by setting devblocksize to PAGE_SIZE, we re-instate
1940 * the old behavior we had before the mem_alignment_mask
1941 * changes went in...
1943 devblocksize
= PAGE_SIZE
;
1947 io_req_size
= *write_length
;
1948 iov_base
= uio_curriovbase(uio
);
1950 offset_in_file
= (u_int32_t
)uio
->uio_offset
& PAGE_MASK
;
1951 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
1953 if (offset_in_file
|| offset_in_iovbase
) {
1955 * one of the 2 important offsets is misaligned
1956 * so fire an I/O through the cache for this entire vector
1958 goto wait_for_dwrites
;
1960 if (iov_base
& (devblocksize
- 1)) {
1962 * the offset in memory must be on a device block boundary
1963 * so that we can guarantee that we can generate an
1964 * I/O that ends on a page boundary in cluster_io
1966 goto wait_for_dwrites
;
1969 while (io_req_size
>= PAGE_SIZE
&& uio
->uio_offset
< newEOF
&& retval
== 0) {
1972 cluster_syncup(vp
, newEOF
, callback
, callback_arg
);
1975 io_size
= io_req_size
& ~PAGE_MASK
;
1976 iov_base
= uio_curriovbase(uio
);
1978 if (io_size
> max_upl_size
)
1979 io_size
= max_upl_size
;
1981 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
1982 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
1984 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_START
,
1985 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
1987 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
1989 upl_size
= upl_needed_size
;
1990 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
1991 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
1993 kret
= vm_map_get_upl(current_map(),
1994 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2002 if (kret
!= KERN_SUCCESS
) {
2003 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2006 * failed to get pagelist
2008 * we may have already spun some portion of this request
2009 * off as async requests... we need to wait for the I/O
2010 * to complete before returning
2012 goto wait_for_dwrites
;
2014 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2015 pages_in_pl
= upl_size
/ PAGE_SIZE
;
2017 for (i
= 0; i
< pages_in_pl
; i
++) {
2018 if (!upl_valid_page(pl
, i
))
2021 if (i
== pages_in_pl
)
2025 * didn't get all the pages back that we
2026 * needed... release this upl and try again
2028 ubc_upl_abort(upl
, 0);
2030 if (force_data_sync
>= 3) {
2031 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2032 i
, pages_in_pl
, upl_size
, kret
, 0);
2034 * for some reason, we couldn't acquire a hold on all
2035 * the pages needed in the user's address space
2037 * we may have already spun some portion of this request
2038 * off as async requests... we need to wait for the I/O
2039 * to complete before returning
2041 goto wait_for_dwrites
;
2045 * Consider the possibility that upl_size wasn't satisfied.
2047 if (upl_size
< upl_needed_size
) {
2048 if (upl_size
&& upl_offset
== 0)
2053 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2054 (int)upl_offset
, upl_size
, (int)iov_base
, io_size
, 0);
2057 ubc_upl_abort(upl
, 0);
2059 * we may have already spun some portion of this request
2060 * off as async requests... we need to wait for the I/O
2061 * to complete before returning
2063 goto wait_for_dwrites
;
2067 * Now look for pages already in the cache
2068 * and throw them away.
2069 * uio->uio_offset is page aligned within the file
2070 * io_size is a multiple of PAGE_SIZE
2072 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ io_size
, UPL_ROP_DUMP
, NULL
);
2075 * we want push out these writes asynchronously so that we can overlap
2076 * the preparation of the next I/O
2077 * if there are already too many outstanding writes
2078 * wait until some complete before issuing the next
2080 lck_mtx_lock(cl_mtxp
);
2082 while ((iostate
.io_issued
- iostate
.io_completed
) > (2 * max_upl_size
)) {
2084 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
2085 iostate
.io_issued
, iostate
.io_completed
, 2 * max_upl_size
, 0, 0);
2087 iostate
.io_wanted
= 1;
2088 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_write_direct", NULL
);
2090 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
2091 iostate
.io_issued
, iostate
.io_completed
, 2 * max_upl_size
, 0, 0);
2093 lck_mtx_unlock(cl_mtxp
);
2095 if (iostate
.io_error
) {
2097 * one of the earlier writes we issued ran into a hard error
2098 * don't issue any more writes, cleanup the UPL
2099 * that was just created but not used, then
2100 * go wait for all writes that are part of this stream
2101 * to complete before returning the error to the caller
2103 ubc_upl_abort(upl
, 0);
2105 goto wait_for_dwrites
;
2107 io_flag
= CL_ASYNC
| CL_PRESERVE
| CL_COMMIT
| CL_THROTTLE
| CL_DIRECT_IO
| bflag
;
2109 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_START
,
2110 (int)upl_offset
, (int)uio
->uio_offset
, io_size
, io_flag
, 0);
2112 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
2113 io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2116 * update the uio structure to
2117 * reflect the I/O that we just issued
2119 uio_update(uio
, (user_size_t
)io_size
);
2121 io_req_size
-= io_size
;
2123 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_END
,
2124 (int)upl_offset
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
2128 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0) {
2130 retval
= cluster_io_type(uio
, write_type
, write_length
, MIN_DIRECT_WRITE_SIZE
);
2132 if (retval
== 0 && *write_type
== IO_DIRECT
) {
2134 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_NONE
,
2135 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
2142 if (iostate
.io_issued
) {
2144 * make sure all async writes issued as part of this stream
2145 * have completed before we return
2147 lck_mtx_lock(cl_mtxp
);
2149 while (iostate
.io_issued
!= iostate
.io_completed
) {
2150 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
2151 iostate
.io_issued
, iostate
.io_completed
, 0, 0, 0);
2153 iostate
.io_wanted
= 1;
2154 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_write_direct", NULL
);
2156 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
2157 iostate
.io_issued
, iostate
.io_completed
, 0, 0, 0);
2159 lck_mtx_unlock(cl_mtxp
);
2161 if (iostate
.io_error
)
2162 retval
= iostate
.io_error
;
2164 if (io_req_size
&& retval
== 0) {
2166 * we couldn't handle the tail of this request in DIRECT mode
2167 * so fire it through the copy path
2169 * note that flags will never have IO_HEADZEROFILL or IO_TAILZEROFILL set
2170 * so we can just pass 0 in for the headOff and tailOff
2172 retval
= cluster_write_copy(vp
, uio
, io_req_size
, oldEOF
, newEOF
, (off_t
)0, (off_t
)0, flags
, callback
, callback_arg
);
2174 *write_type
= IO_UNKNOWN
;
2176 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
2177 (int)uio
->uio_offset
, io_req_size
, retval
, 4, 0);
2184 cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
2185 int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
)
2187 upl_page_info_t
*pl
;
2188 addr64_t src_paddr
= 0;
2189 upl_t upl
[MAX_VECTS
];
2190 vm_offset_t upl_offset
;
2191 u_int32_t tail_size
= 0;
2195 vm_size_t upl_needed_size
;
2196 mach_msg_type_number_t pages_in_pl
;
2199 struct clios iostate
;
2204 user_addr_t iov_base
;
2205 u_int32_t devblocksize
;
2206 u_int32_t mem_alignment_mask
;
2209 * When we enter this routine, we know
2210 * -- the io_req_size will not exceed iov_len
2211 * -- the target address is physically contiguous
2213 cluster_syncup(vp
, newEOF
, callback
, callback_arg
);
2215 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
2216 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
2218 iostate
.io_completed
= 0;
2219 iostate
.io_issued
= 0;
2220 iostate
.io_error
= 0;
2221 iostate
.io_wanted
= 0;
2224 io_size
= *write_length
;
2226 iov_base
= uio_curriovbase(uio
);
2228 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
2229 upl_needed_size
= upl_offset
+ io_size
;
2232 upl_size
= upl_needed_size
;
2233 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
2234 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
2236 kret
= vm_map_get_upl(current_map(),
2237 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2238 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, 0);
2240 if (kret
!= KERN_SUCCESS
) {
2242 * failed to get pagelist
2245 goto wait_for_cwrites
;
2250 * Consider the possibility that upl_size wasn't satisfied.
2252 if (upl_size
< upl_needed_size
) {
2254 * This is a failure in the physical memory case.
2257 goto wait_for_cwrites
;
2259 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
2261 src_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)upl_offset
;
2263 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
2264 u_int32_t head_size
;
2266 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
2268 if (head_size
> io_size
)
2269 head_size
= io_size
;
2271 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, head_size
, 0, callback
, callback_arg
);
2274 goto wait_for_cwrites
;
2276 upl_offset
+= head_size
;
2277 src_paddr
+= head_size
;
2278 io_size
-= head_size
;
2280 iov_base
+= head_size
;
2282 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
2284 * request doesn't set up on a memory boundary
2285 * the underlying DMA engine can handle...
2286 * return an error instead of going through
2287 * the slow copy path since the intent of this
2288 * path is direct I/O from device memory
2291 goto wait_for_cwrites
;
2294 tail_size
= io_size
& (devblocksize
- 1);
2295 io_size
-= tail_size
;
2297 while (io_size
&& error
== 0) {
2299 if (io_size
> MAX_IO_CONTIG_SIZE
)
2300 xsize
= MAX_IO_CONTIG_SIZE
;
2304 * request asynchronously so that we can overlap
2305 * the preparation of the next I/O... we'll do
2306 * the commit after all the I/O has completed
2307 * since its all issued against the same UPL
2308 * if there are already too many outstanding writes
2309 * wait until some have completed before issuing the next
2311 if (iostate
.io_issued
) {
2312 lck_mtx_lock(cl_mtxp
);
2314 while ((iostate
.io_issued
- iostate
.io_completed
) > (2 * MAX_IO_CONTIG_SIZE
)) {
2316 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
2317 iostate
.io_issued
, iostate
.io_completed
, 2 * MAX_IO_CONTIG_SIZE
, 0, 0);
2319 iostate
.io_wanted
= 1;
2320 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_write_contig", NULL
);
2322 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
2323 iostate
.io_issued
, iostate
.io_completed
, 2 * MAX_IO_CONTIG_SIZE
, 0, 0);
2325 lck_mtx_unlock(cl_mtxp
);
2327 if (iostate
.io_error
) {
2329 * one of the earlier writes we issued ran into a hard error
2330 * don't issue any more writes...
2331 * go wait for all writes that are part of this stream
2332 * to complete before returning the error to the caller
2334 goto wait_for_cwrites
;
2337 * issue an asynchronous write to cluster_io
2339 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
,
2340 xsize
, CL_DEV_MEMORY
| CL_ASYNC
| bflag
, (buf_t
)NULL
, (struct clios
*)&iostate
, callback
, callback_arg
);
2344 * The cluster_io write completed successfully,
2345 * update the uio structure
2347 uio_update(uio
, (user_size_t
)xsize
);
2349 upl_offset
+= xsize
;
2354 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
) {
2356 error
= cluster_io_type(uio
, write_type
, write_length
, 0);
2358 if (error
== 0 && *write_type
== IO_CONTIG
) {
2363 *write_type
= IO_UNKNOWN
;
2367 * make sure all async writes that are part of this stream
2368 * have completed before we proceed
2370 lck_mtx_lock(cl_mtxp
);
2372 while (iostate
.io_issued
!= iostate
.io_completed
) {
2373 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
2374 iostate
.io_issued
, iostate
.io_completed
, 0, 0, 0);
2376 iostate
.io_wanted
= 1;
2377 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_write_contig", NULL
);
2379 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
2380 iostate
.io_issued
, iostate
.io_completed
, 0, 0, 0);
2382 lck_mtx_unlock(cl_mtxp
);
2384 if (iostate
.io_error
)
2385 error
= iostate
.io_error
;
2387 if (error
== 0 && tail_size
)
2388 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, tail_size
, 0, callback
, callback_arg
);
2390 for (n
= 0; n
< num_upl
; n
++)
2392 * just release our hold on each physically contiguous
2393 * region without changing any state
2395 ubc_upl_abort(upl
[n
], 0);
2402 cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
, off_t headOff
,
2403 off_t tailOff
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2405 upl_page_info_t
*pl
;
2407 vm_offset_t upl_offset
= 0;
2420 long long total_size
;
2423 long long zero_cnt1
;
2425 struct cl_extent cl
;
2427 struct cl_writebehind
*wbp
;
2429 u_int max_cluster_pgcount
;
2432 if (flags
& IO_PASSIVE
)
2438 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
2439 (int)uio
->uio_offset
, io_req_size
, (int)oldEOF
, (int)newEOF
, 0);
2441 io_resid
= io_req_size
;
2443 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
2444 0, 0, (int)oldEOF
, (int)newEOF
, 0);
2453 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
2454 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
2456 if (flags
& IO_HEADZEROFILL
) {
2458 * some filesystems (HFS is one) don't support unallocated holes within a file...
2459 * so we zero fill the intervening space between the old EOF and the offset
2460 * where the next chunk of real data begins.... ftruncate will also use this
2461 * routine to zero fill to the new EOF when growing a file... in this case, the
2462 * uio structure will not be provided
2465 if (headOff
< uio
->uio_offset
) {
2466 zero_cnt
= uio
->uio_offset
- headOff
;
2469 } else if (headOff
< newEOF
) {
2470 zero_cnt
= newEOF
- headOff
;
2474 if (flags
& IO_TAILZEROFILL
) {
2476 zero_off1
= uio
->uio_offset
+ io_req_size
;
2478 if (zero_off1
< tailOff
)
2479 zero_cnt1
= tailOff
- zero_off1
;
2482 if (zero_cnt
== 0 && uio
== (struct uio
*) 0) {
2483 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
2484 retval
, 0, 0, 0, 0);
2488 while ((total_size
= (io_resid
+ zero_cnt
+ zero_cnt1
)) && retval
== 0) {
2490 * for this iteration of the loop, figure out where our starting point is
2493 start_offset
= (int)(zero_off
& PAGE_MASK_64
);
2494 upl_f_offset
= zero_off
- start_offset
;
2495 } else if (io_resid
) {
2496 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2497 upl_f_offset
= uio
->uio_offset
- start_offset
;
2499 start_offset
= (int)(zero_off1
& PAGE_MASK_64
);
2500 upl_f_offset
= zero_off1
- start_offset
;
2502 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 46)) | DBG_FUNC_NONE
,
2503 (int)zero_off
, (int)zero_cnt
, (int)zero_off1
, (int)zero_cnt1
, 0);
2505 if (total_size
> max_io_size
)
2506 total_size
= max_io_size
;
2508 cl
.b_addr
= (daddr64_t
)(upl_f_offset
/ PAGE_SIZE_64
);
2510 if (uio
&& ((flags
& (IO_SYNC
| IO_HEADZEROFILL
| IO_TAILZEROFILL
)) == 0)) {
2512 * assumption... total_size <= io_resid
2513 * because IO_HEADZEROFILL and IO_TAILZEROFILL not set
2515 if ((start_offset
+ total_size
) > max_io_size
)
2516 total_size
-= start_offset
;
2517 xfer_resid
= total_size
;
2519 retval
= cluster_copy_ubc_data_internal(vp
, uio
, &xfer_resid
, 1, 1);
2524 io_resid
-= (total_size
- xfer_resid
);
2525 total_size
= xfer_resid
;
2526 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2527 upl_f_offset
= uio
->uio_offset
- start_offset
;
2529 if (total_size
== 0) {
2532 * the write did not finish on a page boundary
2533 * which will leave upl_f_offset pointing to the
2534 * beginning of the last page written instead of
2535 * the page beyond it... bump it in this case
2536 * so that the cluster code records the last page
2539 upl_f_offset
+= PAGE_SIZE_64
;
2547 * compute the size of the upl needed to encompass
2548 * the requested write... limit each call to cluster_io
2549 * to the maximum UPL size... cluster_io will clip if
2550 * this exceeds the maximum io_size for the device,
2551 * make sure to account for
2552 * a starting offset that's not page aligned
2554 upl_size
= (start_offset
+ total_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2556 if (upl_size
> max_io_size
)
2557 upl_size
= max_io_size
;
2559 pages_in_upl
= upl_size
/ PAGE_SIZE
;
2560 io_size
= upl_size
- start_offset
;
2562 if ((long long)io_size
> total_size
)
2563 io_size
= total_size
;
2565 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, io_size
, total_size
, 0, 0);
2569 * Gather the pages from the buffer cache.
2570 * The UPL_WILL_MODIFY flag lets the UPL subsystem know
2571 * that we intend to modify these pages.
2573 kret
= ubc_create_upl(vp
,
2578 UPL_SET_LITE
| UPL_WILL_MODIFY
);
2579 if (kret
!= KERN_SUCCESS
)
2580 panic("cluster_write_copy: failed to get pagelist");
2582 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
,
2583 (int)upl
, (int)upl_f_offset
, start_offset
, 0, 0);
2585 if (start_offset
&& !upl_valid_page(pl
, 0)) {
2589 * we're starting in the middle of the first page of the upl
2590 * and the page isn't currently valid, so we're going to have
2591 * to read it in first... this is a synchronous operation
2593 read_size
= PAGE_SIZE
;
2595 if ((upl_f_offset
+ read_size
) > newEOF
)
2596 read_size
= newEOF
- upl_f_offset
;
2598 retval
= cluster_io(vp
, upl
, 0, upl_f_offset
, read_size
,
2599 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
2602 * we had an error during the read which causes us to abort
2603 * the current cluster_write request... before we do, we need
2604 * to release the rest of the pages in the upl without modifying
2605 * there state and mark the failed page in error
2607 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
);
2609 if (upl_size
> PAGE_SIZE
)
2610 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2612 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2613 (int)upl
, 0, 0, retval
, 0);
2617 if ((start_offset
== 0 || upl_size
> PAGE_SIZE
) && ((start_offset
+ io_size
) & PAGE_MASK
)) {
2619 * the last offset we're writing to in this upl does not end on a page
2620 * boundary... if it's not beyond the old EOF, then we'll also need to
2621 * pre-read this page in if it isn't already valid
2623 upl_offset
= upl_size
- PAGE_SIZE
;
2625 if ((upl_f_offset
+ start_offset
+ io_size
) < oldEOF
&&
2626 !upl_valid_page(pl
, upl_offset
/ PAGE_SIZE
)) {
2629 read_size
= PAGE_SIZE
;
2631 if ((upl_f_offset
+ upl_offset
+ read_size
) > newEOF
)
2632 read_size
= newEOF
- (upl_f_offset
+ upl_offset
);
2634 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, read_size
,
2635 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
2638 * we had an error during the read which causes us to abort
2639 * the current cluster_write request... before we do, we
2640 * need to release the rest of the pages in the upl without
2641 * modifying there state and mark the failed page in error
2643 ubc_upl_abort_range(upl
, upl_offset
, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
);
2645 if (upl_size
> PAGE_SIZE
)
2646 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2648 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2649 (int)upl
, 0, 0, retval
, 0);
2654 xfer_resid
= io_size
;
2655 io_offset
= start_offset
;
2657 while (zero_cnt
&& xfer_resid
) {
2659 if (zero_cnt
< (long long)xfer_resid
)
2660 bytes_to_zero
= zero_cnt
;
2662 bytes_to_zero
= xfer_resid
;
2664 if ( !(flags
& (IO_NOZEROVALID
| IO_NOZERODIRTY
))) {
2665 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2669 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off
& PAGE_MASK_64
));
2670 zero_pg_index
= (int)((zero_off
- upl_f_offset
) / PAGE_SIZE_64
);
2672 if ( !upl_valid_page(pl
, zero_pg_index
)) {
2673 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2675 } else if ((flags
& (IO_NOZERODIRTY
| IO_NOZEROVALID
)) == IO_NOZERODIRTY
&&
2676 !upl_dirty_page(pl
, zero_pg_index
)) {
2677 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2680 xfer_resid
-= bytes_to_zero
;
2681 zero_cnt
-= bytes_to_zero
;
2682 zero_off
+= bytes_to_zero
;
2683 io_offset
+= bytes_to_zero
;
2685 if (xfer_resid
&& io_resid
) {
2686 u_int32_t io_requested
;
2688 bytes_to_move
= min(io_resid
, xfer_resid
);
2689 io_requested
= bytes_to_move
;
2691 retval
= cluster_copy_upl_data(uio
, upl
, io_offset
, (int *)&io_requested
);
2695 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
2697 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2698 (int)upl
, 0, 0, retval
, 0);
2700 io_resid
-= bytes_to_move
;
2701 xfer_resid
-= bytes_to_move
;
2702 io_offset
+= bytes_to_move
;
2705 while (xfer_resid
&& zero_cnt1
&& retval
== 0) {
2707 if (zero_cnt1
< (long long)xfer_resid
)
2708 bytes_to_zero
= zero_cnt1
;
2710 bytes_to_zero
= xfer_resid
;
2712 if ( !(flags
& (IO_NOZEROVALID
| IO_NOZERODIRTY
))) {
2713 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2717 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off1
& PAGE_MASK_64
));
2718 zero_pg_index
= (int)((zero_off1
- upl_f_offset
) / PAGE_SIZE_64
);
2720 if ( !upl_valid_page(pl
, zero_pg_index
)) {
2721 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2722 } else if ((flags
& (IO_NOZERODIRTY
| IO_NOZEROVALID
)) == IO_NOZERODIRTY
&&
2723 !upl_dirty_page(pl
, zero_pg_index
)) {
2724 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2727 xfer_resid
-= bytes_to_zero
;
2728 zero_cnt1
-= bytes_to_zero
;
2729 zero_off1
+= bytes_to_zero
;
2730 io_offset
+= bytes_to_zero
;
2735 int ret_cluster_try_push
;
2737 io_size
+= start_offset
;
2739 if ((upl_f_offset
+ io_size
) >= newEOF
&& (u_int
)io_size
< upl_size
) {
2741 * if we're extending the file with this write
2742 * we'll zero fill the rest of the page so that
2743 * if the file gets extended again in such a way as to leave a
2744 * hole starting at this EOF, we'll have zero's in the correct spot
2746 cluster_zero(upl
, io_size
, upl_size
- io_size
, NULL
);
2748 if (flags
& IO_SYNC
)
2750 * if the IO_SYNC flag is set than we need to
2751 * bypass any clusters and immediately issue
2757 * take the lock to protect our accesses
2758 * of the writebehind and sparse cluster state
2760 wbp
= cluster_get_wbp(vp
, CLW_ALLOCATE
| CLW_RETURNLOCKED
);
2763 * calculate the last logical block number
2764 * that this delayed I/O encompassed
2766 cl
.e_addr
= (daddr64_t
)((upl_f_offset
+ (off_t
)upl_size
) / PAGE_SIZE_64
);
2768 if (wbp
->cl_scmap
) {
2770 if ( !(flags
& IO_NOCACHE
)) {
2772 * we've fallen into the sparse
2773 * cluster method of delaying dirty pages
2774 * first, we need to release the upl if we hold one
2775 * since pages in it may be present in the sparse cluster map
2776 * and may span 2 separate buckets there... if they do and
2777 * we happen to have to flush a bucket to make room and it intersects
2778 * this upl, a deadlock may result on page BUSY
2781 ubc_upl_commit_range(upl
, 0, upl_size
,
2782 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2784 sparse_cluster_add(wbp
, vp
, &cl
, newEOF
, callback
, callback_arg
);
2786 lck_mtx_unlock(&wbp
->cl_lockw
);
2791 * must have done cached writes that fell into
2792 * the sparse cluster mechanism... we've switched
2793 * to uncached writes on the file, so go ahead
2794 * and push whatever's in the sparse map
2795 * and switch back to normal clustering
2797 * see the comment above concerning a possible deadlock...
2800 ubc_upl_commit_range(upl
, 0, upl_size
,
2801 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2803 * setting upl_size to 0 keeps us from committing a
2804 * second time in the start_new_cluster path
2808 sparse_cluster_push(wbp
, vp
, newEOF
, PUSH_ALL
, callback
, callback_arg
);
2812 * no clusters of either type present at this point
2813 * so just go directly to start_new_cluster since
2814 * we know we need to delay this I/O since we've
2815 * already released the pages back into the cache
2816 * to avoid the deadlock with sparse_cluster_push
2818 goto start_new_cluster
;
2822 if (wbp
->cl_number
== 0)
2824 * no clusters currently present
2826 goto start_new_cluster
;
2828 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
2830 * check each cluster that we currently hold
2831 * try to merge some or all of this write into
2832 * one or more of the existing clusters... if
2833 * any portion of the write remains, start a
2836 if (cl
.b_addr
>= wbp
->cl_clusters
[cl_index
].b_addr
) {
2838 * the current write starts at or after the current cluster
2840 if (cl
.e_addr
<= (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
2842 * we have a write that fits entirely
2843 * within the existing cluster limits
2845 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
)
2847 * update our idea of where the cluster ends
2849 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
2852 if (cl
.b_addr
< (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
2854 * we have a write that starts in the middle of the current cluster
2855 * but extends beyond the cluster's limit... we know this because
2856 * of the previous checks
2857 * we'll extend the current cluster to the max
2858 * and update the b_addr for the current write to reflect that
2859 * the head of it was absorbed into this cluster...
2860 * note that we'll always have a leftover tail in this case since
2861 * full absorbtion would have occurred in the clause above
2863 wbp
->cl_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
;
2866 daddr64_t start_pg_in_upl
;
2868 start_pg_in_upl
= (daddr64_t
)(upl_f_offset
/ PAGE_SIZE_64
);
2870 if (start_pg_in_upl
< wbp
->cl_clusters
[cl_index
].e_addr
) {
2871 intersection
= (int)((wbp
->cl_clusters
[cl_index
].e_addr
- start_pg_in_upl
) * PAGE_SIZE
);
2873 ubc_upl_commit_range(upl
, upl_offset
, intersection
,
2874 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2875 upl_f_offset
+= intersection
;
2876 upl_offset
+= intersection
;
2877 upl_size
-= intersection
;
2880 cl
.b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
;
2883 * we come here for the case where the current write starts
2884 * beyond the limit of the existing cluster or we have a leftover
2885 * tail after a partial absorbtion
2887 * in either case, we'll check the remaining clusters before
2888 * starting a new one
2892 * the current write starts in front of the cluster we're currently considering
2894 if ((wbp
->cl_clusters
[cl_index
].e_addr
- cl
.b_addr
) <= max_cluster_pgcount
) {
2896 * we can just merge the new request into
2897 * this cluster and leave it in the cache
2898 * since the resulting cluster is still
2899 * less than the maximum allowable size
2901 wbp
->cl_clusters
[cl_index
].b_addr
= cl
.b_addr
;
2903 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
) {
2905 * the current write completely
2906 * envelops the existing cluster and since
2907 * each write is limited to at most max_cluster_pgcount pages
2908 * we can just use the start and last blocknos of the write
2909 * to generate the cluster limits
2911 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
2917 * if we were to combine this write with the current cluster
2918 * we would exceed the cluster size limit.... so,
2919 * let's see if there's any overlap of the new I/O with
2920 * the cluster we're currently considering... in fact, we'll
2921 * stretch the cluster out to it's full limit and see if we
2922 * get an intersection with the current write
2925 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
) {
2927 * the current write extends into the proposed cluster
2928 * clip the length of the current write after first combining it's
2929 * tail with the newly shaped cluster
2931 wbp
->cl_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
;
2934 intersection
= (int)((cl
.e_addr
- wbp
->cl_clusters
[cl_index
].b_addr
) * PAGE_SIZE
);
2936 if ((u_int
)intersection
> upl_size
)
2938 * because the current write may consist of a number of pages found in the cache
2939 * which are not part of the UPL, we may have an intersection that exceeds
2940 * the size of the UPL that is also part of this write
2942 intersection
= upl_size
;
2944 ubc_upl_commit_range(upl
, upl_offset
+ (upl_size
- intersection
), intersection
,
2945 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2946 upl_size
-= intersection
;
2948 cl
.e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
;
2951 * if we get here, there was no way to merge
2952 * any portion of this write with this cluster
2953 * or we could only merge part of it which
2954 * will leave a tail...
2955 * we'll check the remaining clusters before starting a new one
2959 if (cl_index
< wbp
->cl_number
)
2961 * we found an existing cluster(s) that we
2962 * could entirely merge this I/O into
2966 if (wbp
->cl_number
< MAX_CLUSTERS
)
2968 * we didn't find an existing cluster to
2969 * merge into, but there's room to start
2972 goto start_new_cluster
;
2975 * no exisitng cluster to merge with and no
2976 * room to start a new one... we'll try
2977 * pushing one of the existing ones... if none of
2978 * them are able to be pushed, we'll switch
2979 * to the sparse cluster mechanism
2980 * cluster_try_push updates cl_number to the
2981 * number of remaining clusters... and
2982 * returns the number of currently unused clusters
2984 ret_cluster_try_push
= 0;
2987 * if writes are not deferred, call cluster push immediately
2989 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
)) {
2991 ret_cluster_try_push
= cluster_try_push(wbp
, vp
, newEOF
, (flags
& IO_NOCACHE
) ? 0 : PUSH_DELAY
, callback
, callback_arg
);
2995 * execute following regardless of writes being deferred or not
2997 if (ret_cluster_try_push
== 0) {
2999 * no more room in the normal cluster mechanism
3000 * so let's switch to the more expansive but expensive
3001 * sparse mechanism....
3002 * first, we need to release the upl if we hold one
3003 * since pages in it may be present in the sparse cluster map (after the cluster_switch)
3004 * and may span 2 separate buckets there... if they do and
3005 * we happen to have to flush a bucket to make room and it intersects
3006 * this upl, a deadlock may result on page BUSY
3009 ubc_upl_commit_range(upl
, upl_offset
, upl_size
,
3010 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
3012 sparse_cluster_switch(wbp
, vp
, newEOF
, callback
, callback_arg
);
3013 sparse_cluster_add(wbp
, vp
, &cl
, newEOF
, callback
, callback_arg
);
3015 lck_mtx_unlock(&wbp
->cl_lockw
);
3020 * we pushed one cluster successfully, so we must be sequentially writing this file
3021 * otherwise, we would have failed and fallen into the sparse cluster support
3022 * so let's take the opportunity to push out additional clusters...
3023 * this will give us better I/O locality if we're in a copy loop
3024 * (i.e. we won't jump back and forth between the read and write points
3026 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
)) {
3027 while (wbp
->cl_number
)
3028 cluster_try_push(wbp
, vp
, newEOF
, 0, callback
, callback_arg
);
3032 wbp
->cl_clusters
[wbp
->cl_number
].b_addr
= cl
.b_addr
;
3033 wbp
->cl_clusters
[wbp
->cl_number
].e_addr
= cl
.e_addr
;
3035 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
= 0;
3037 if (flags
& IO_NOCACHE
)
3038 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IONOCACHE
;
3040 if (bflag
& CL_PASSIVE
)
3041 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IOPASSIVE
;
3046 ubc_upl_commit_range(upl
, upl_offset
, upl_size
,
3047 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
3049 lck_mtx_unlock(&wbp
->cl_lockw
);
3054 * we don't hold the vnode lock at this point
3056 * because we had to ask for a UPL that provides currenty non-present pages, the
3057 * UPL has been automatically set to clear the dirty flags (both software and hardware)
3058 * upon committing it... this is not the behavior we want since it's possible for
3059 * pages currently present as part of a mapped file to be dirtied while the I/O is in flight.
3060 * in order to maintain some semblance of coherency with mapped writes
3061 * we need to drop the current upl and pick it back up with COPYOUT_FROM set
3062 * so that we correctly deal with a change in state of the hardware modify bit...
3063 * we do this via cluster_push_now... by passing along the IO_SYNC flag, we force
3064 * cluster_push_now to wait until all the I/Os have completed... cluster_push_now is also
3065 * responsible for generating the correct sized I/O(s)
3067 ubc_upl_commit_range(upl
, 0, upl_size
,
3068 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
3070 cl
.e_addr
= (upl_f_offset
+ (off_t
)upl_size
) / PAGE_SIZE_64
;
3072 retval
= cluster_push_now(vp
, &cl
, newEOF
, flags
, callback
, callback_arg
);
3075 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
, retval
, 0, io_resid
, 0, 0);
3083 cluster_read(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
)
3085 return cluster_read_ext(vp
, uio
, filesize
, xflags
, NULL
, NULL
);
3090 cluster_read_ext(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
3094 user_ssize_t cur_resid
;
3096 u_int32_t read_length
= 0;
3097 int read_type
= IO_COPY
;
3101 if (vp
->v_flag
& VNOCACHE_DATA
)
3102 flags
|= IO_NOCACHE
;
3103 if ((vp
->v_flag
& VRAOFF
) || speculative_reads_disabled
)
3107 * do a read through the cache if one of the following is true....
3108 * NOCACHE is not true
3109 * the uio request doesn't target USERSPACE
3110 * otherwise, find out if we want the direct or contig variant for
3111 * the first vector in the uio request
3113 if ( (flags
& IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) )
3114 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3116 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< filesize
&& retval
== 0) {
3118 switch (read_type
) {
3122 * make sure the uio_resid isn't too big...
3123 * internally, we want to handle all of the I/O in
3124 * chunk sizes that fit in a 32 bit int
3126 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
))
3127 io_size
= MAX_IO_REQUEST_SIZE
;
3129 io_size
= (u_int32_t
)cur_resid
;
3131 retval
= cluster_read_copy(vp
, uio
, io_size
, filesize
, flags
, callback
, callback_arg
);
3135 retval
= cluster_read_direct(vp
, uio
, filesize
, &read_type
, &read_length
, flags
, callback
, callback_arg
);
3139 retval
= cluster_read_contig(vp
, uio
, filesize
, &read_type
, &read_length
, callback
, callback_arg
, flags
);
3143 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3153 cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int flags
)
3156 int abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
3158 if ((range
= last_pg
- start_pg
)) {
3159 if ( !(flags
& IO_NOCACHE
))
3160 abort_flags
|= UPL_ABORT_REFERENCE
;
3162 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, range
* PAGE_SIZE
, abort_flags
);
3168 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
)
3170 upl_page_info_t
*pl
;
3172 vm_offset_t upl_offset
;
3181 off_t last_ioread_offset
;
3182 off_t last_request_offset
;
3186 u_int32_t size_of_prefetch
;
3189 u_int32_t max_rd_size
;
3190 u_int32_t max_io_size
;
3191 u_int32_t max_prefetch
;
3192 u_int rd_ahead_enabled
= 1;
3193 u_int prefetch_enabled
= 1;
3194 struct cl_readahead
* rap
;
3195 struct clios iostate
;
3196 struct cl_extent extent
;
3198 int take_reference
= 1;
3200 int policy
= IOPOL_DEFAULT
;
3202 policy
= current_proc()->p_iopol_disk
;
3204 ut
= get_bsdthread_info(current_thread());
3206 if (ut
->uu_iopol_disk
!= IOPOL_DEFAULT
)
3207 policy
= ut
->uu_iopol_disk
;
3209 if (policy
== IOPOL_THROTTLE
)
3212 if (flags
& IO_PASSIVE
)
3217 max_prefetch
= MAX_PREFETCH(vp
);
3218 max_rd_size
= max_prefetch
;
3219 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
3221 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_START
,
3222 (int)uio
->uio_offset
, io_req_size
, (int)filesize
, flags
, 0);
3224 last_request_offset
= uio
->uio_offset
+ io_req_size
;
3226 if ((flags
& (IO_RAOFF
|IO_NOCACHE
)) || ((last_request_offset
& ~PAGE_MASK_64
) == (uio
->uio_offset
& ~PAGE_MASK_64
))) {
3227 rd_ahead_enabled
= 0;
3230 if (cluster_hard_throttle_on(vp
)) {
3231 rd_ahead_enabled
= 0;
3232 prefetch_enabled
= 0;
3234 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
3236 if ((rap
= cluster_get_rap(vp
)) == NULL
)
3237 rd_ahead_enabled
= 0;
3239 if (last_request_offset
> filesize
)
3240 last_request_offset
= filesize
;
3241 extent
.b_addr
= uio
->uio_offset
/ PAGE_SIZE_64
;
3242 extent
.e_addr
= (last_request_offset
- 1) / PAGE_SIZE_64
;
3244 if (rap
!= NULL
&& rap
->cl_ralen
&& (rap
->cl_lastr
== extent
.b_addr
|| (rap
->cl_lastr
+ 1) == extent
.b_addr
)) {
3246 * determine if we already have a read-ahead in the pipe courtesy of the
3247 * last read systemcall that was issued...
3248 * if so, pick up it's extent to determine where we should start
3249 * with respect to any read-ahead that might be necessary to
3250 * garner all the data needed to complete this read systemcall
3252 last_ioread_offset
= (rap
->cl_maxra
* PAGE_SIZE_64
) + PAGE_SIZE_64
;
3254 if (last_ioread_offset
< uio
->uio_offset
)
3255 last_ioread_offset
= (off_t
)0;
3256 else if (last_ioread_offset
> last_request_offset
)
3257 last_ioread_offset
= last_request_offset
;
3259 last_ioread_offset
= (off_t
)0;
3261 while (io_req_size
&& uio
->uio_offset
< filesize
&& retval
== 0) {
3263 * compute the size of the upl needed to encompass
3264 * the requested read... limit each call to cluster_io
3265 * to the maximum UPL size... cluster_io will clip if
3266 * this exceeds the maximum io_size for the device,
3267 * make sure to account for
3268 * a starting offset that's not page aligned
3270 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3271 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
3272 max_size
= filesize
- uio
->uio_offset
;
3274 if ((off_t
)(io_req_size
) < max_size
)
3275 io_size
= io_req_size
;
3279 if (!(flags
& IO_NOCACHE
)) {
3283 u_int32_t io_requested
;
3286 * if we keep finding the pages we need already in the cache, then
3287 * don't bother to call cluster_read_prefetch since it costs CPU cycles
3288 * to determine that we have all the pages we need... once we miss in
3289 * the cache and have issued an I/O, than we'll assume that we're likely
3290 * to continue to miss in the cache and it's to our advantage to try and prefetch
3292 if (last_request_offset
&& last_ioread_offset
&& (size_of_prefetch
= (last_request_offset
- last_ioread_offset
))) {
3293 if ((last_ioread_offset
- uio
->uio_offset
) <= max_rd_size
&& prefetch_enabled
) {
3295 * we've already issued I/O for this request and
3296 * there's still work to do and
3297 * our prefetch stream is running dry, so issue a
3298 * pre-fetch I/O... the I/O latency will overlap
3299 * with the copying of the data
3301 if (size_of_prefetch
> max_rd_size
)
3302 size_of_prefetch
= max_rd_size
;
3304 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
3306 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
3308 if (last_ioread_offset
> last_request_offset
)
3309 last_ioread_offset
= last_request_offset
;
3313 * limit the size of the copy we're about to do so that
3314 * we can notice that our I/O pipe is running dry and
3315 * get the next I/O issued before it does go dry
3317 if (last_ioread_offset
&& io_size
> (max_io_size
/ 4))
3318 io_resid
= (max_io_size
/ 4);
3322 io_requested
= io_resid
;
3324 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_resid
, 0, take_reference
);
3326 xsize
= io_requested
- io_resid
;
3329 io_req_size
-= xsize
;
3331 if (retval
|| io_resid
)
3333 * if we run into a real error or
3334 * a page that is not in the cache
3335 * we need to leave streaming mode
3339 if ((io_size
== 0 || last_ioread_offset
== last_request_offset
) && rd_ahead_enabled
) {
3341 * we're already finished the I/O for this read request
3342 * let's see if we should do a read-ahead
3344 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
3351 if (extent
.e_addr
< rap
->cl_lastr
)
3353 rap
->cl_lastr
= extent
.e_addr
;
3357 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3358 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
3359 max_size
= filesize
- uio
->uio_offset
;
3361 if (io_size
> max_rd_size
)
3362 io_size
= max_rd_size
;
3364 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
3366 if (flags
& IO_NOCACHE
) {
3367 if (upl_size
> max_io_size
)
3368 upl_size
= max_io_size
;
3370 if (upl_size
> max_io_size
/ 4)
3371 upl_size
= max_io_size
/ 4;
3373 pages_in_upl
= upl_size
/ PAGE_SIZE
;
3375 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_START
,
3376 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3378 kret
= ubc_create_upl(vp
,
3383 UPL_FILE_IO
| UPL_SET_LITE
);
3384 if (kret
!= KERN_SUCCESS
)
3385 panic("cluster_read_copy: failed to get pagelist");
3387 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_END
,
3388 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3391 * scan from the beginning of the upl looking for the first
3392 * non-valid page.... this will become the first page in
3393 * the request we're going to make to 'cluster_io'... if all
3394 * of the pages are valid, we won't call through to 'cluster_io'
3396 for (start_pg
= 0; start_pg
< pages_in_upl
; start_pg
++) {
3397 if (!upl_valid_page(pl
, start_pg
))
3402 * scan from the starting invalid page looking for a valid
3403 * page before the end of the upl is reached, if we
3404 * find one, then it will be the last page of the request to
3407 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
3408 if (upl_valid_page(pl
, last_pg
))
3411 iostate
.io_completed
= 0;
3412 iostate
.io_issued
= 0;
3413 iostate
.io_error
= 0;
3414 iostate
.io_wanted
= 0;
3416 if (start_pg
< last_pg
) {
3418 * we found a range of 'invalid' pages that must be filled
3419 * if the last page in this range is the last page of the file
3420 * we may have to clip the size of it to keep from reading past
3421 * the end of the last physical block associated with the file
3423 upl_offset
= start_pg
* PAGE_SIZE
;
3424 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
3426 if ((upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
3427 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
3430 * issue an asynchronous read to cluster_io
3433 error
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
,
3434 io_size
, CL_READ
| CL_ASYNC
| bflag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
3438 * if the read completed successfully, or there was no I/O request
3439 * issued, than copy the data into user land via 'cluster_upl_copy_data'
3440 * we'll first add on any 'valid'
3441 * pages that were present in the upl when we acquired it.
3445 for (uio_last
= last_pg
; uio_last
< pages_in_upl
; uio_last
++) {
3446 if (!upl_valid_page(pl
, uio_last
))
3449 if (uio_last
< pages_in_upl
) {
3451 * there were some invalid pages beyond the valid pages
3452 * that we didn't issue an I/O for, just release them
3453 * unchanged now, so that any prefetch/readahed can
3456 ubc_upl_abort_range(upl
, uio_last
* PAGE_SIZE
,
3457 (pages_in_upl
- uio_last
) * PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
3461 * compute size to transfer this round, if io_req_size is
3462 * still non-zero after this attempt, we'll loop around and
3463 * set up for another I/O.
3465 val_size
= (uio_last
* PAGE_SIZE
) - start_offset
;
3467 if (val_size
> max_size
)
3468 val_size
= max_size
;
3470 if (val_size
> io_req_size
)
3471 val_size
= io_req_size
;
3473 if ((uio
->uio_offset
+ val_size
) > last_ioread_offset
)
3474 last_ioread_offset
= uio
->uio_offset
+ val_size
;
3476 if ((size_of_prefetch
= (last_request_offset
- last_ioread_offset
)) && prefetch_enabled
) {
3478 if ((last_ioread_offset
- (uio
->uio_offset
+ val_size
)) <= upl_size
) {
3480 * if there's still I/O left to do for this request, and...
3481 * we're not in hard throttle mode, and...
3482 * we're close to using up the previous prefetch, then issue a
3483 * new pre-fetch I/O... the I/O latency will overlap
3484 * with the copying of the data
3486 if (size_of_prefetch
> max_rd_size
)
3487 size_of_prefetch
= max_rd_size
;
3489 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
3491 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
3493 if (last_ioread_offset
> last_request_offset
)
3494 last_ioread_offset
= last_request_offset
;
3497 } else if ((uio
->uio_offset
+ val_size
) == last_request_offset
) {
3499 * this transfer will finish this request, so...
3500 * let's try to read ahead if we're in
3501 * a sequential access pattern and we haven't
3502 * explicitly disabled it
3504 if (rd_ahead_enabled
)
3505 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
3508 if (extent
.e_addr
< rap
->cl_lastr
)
3510 rap
->cl_lastr
= extent
.e_addr
;
3513 lck_mtx_lock(cl_mtxp
);
3515 while (iostate
.io_issued
!= iostate
.io_completed
) {
3516 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
3517 iostate
.io_issued
, iostate
.io_completed
, 0, 0, 0);
3519 iostate
.io_wanted
= 1;
3520 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_read_copy", NULL
);
3522 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
3523 iostate
.io_issued
, iostate
.io_completed
, 0, 0, 0);
3525 lck_mtx_unlock(cl_mtxp
);
3527 if (iostate
.io_error
)
3528 error
= iostate
.io_error
;
3530 u_int32_t io_requested
;
3532 io_requested
= val_size
;
3534 retval
= cluster_copy_upl_data(uio
, upl
, start_offset
, (int *)&io_requested
);
3536 io_req_size
-= (val_size
- io_requested
);
3539 if (start_pg
< last_pg
) {
3541 * compute the range of pages that we actually issued an I/O for
3542 * and either commit them as valid if the I/O succeeded
3543 * or abort them if the I/O failed or we're not supposed to
3544 * keep them in the cache
3546 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
3548 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
, (int)upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
3550 if (error
|| (flags
& IO_NOCACHE
))
3551 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
3552 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3554 ubc_upl_commit_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
3555 UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
| UPL_COMMIT_INACTIVATE
);
3557 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, (int)upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
3559 if ((last_pg
- start_pg
) < pages_in_upl
) {
3561 * the set of pages that we issued an I/O for did not encompass
3562 * the entire upl... so just release these without modifying
3566 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3569 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
3570 (int)upl
, -1, pages_in_upl
- (last_pg
- start_pg
), 0, 0);
3573 * handle any valid pages at the beginning of
3574 * the upl... release these appropriately
3576 cluster_read_upl_release(upl
, 0, start_pg
, flags
);
3579 * handle any valid pages immediately after the
3580 * pages we issued I/O for... ... release these appropriately
3582 cluster_read_upl_release(upl
, last_pg
, uio_last
, flags
);
3584 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, (int)upl
, -1, -1, 0, 0);
3591 if (cluster_hard_throttle_on(vp
)) {
3592 rd_ahead_enabled
= 0;
3593 prefetch_enabled
= 0;
3595 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
3597 if (max_rd_size
== HARD_THROTTLE_MAXSIZE
) {
3599 * coming out of throttled state
3602 rd_ahead_enabled
= 1;
3603 prefetch_enabled
= 1;
3605 max_rd_size
= max_prefetch
;
3606 last_ioread_offset
= 0;
3612 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
3613 (int)uio
->uio_offset
, io_req_size
, rap
->cl_lastr
, retval
, 0);
3615 lck_mtx_unlock(&rap
->cl_lockr
);
3617 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
3618 (int)uio
->uio_offset
, io_req_size
, 0, retval
, 0);
3626 cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
3627 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
3630 upl_page_info_t
*pl
;
3632 vm_offset_t upl_offset
;
3634 vm_size_t upl_needed_size
;
3635 unsigned int pages_in_pl
;
3640 int force_data_sync
;
3642 int no_zero_fill
= 0;
3646 struct clios iostate
;
3647 user_addr_t iov_base
;
3648 u_int32_t io_req_size
;
3649 u_int32_t offset_in_file
;
3650 u_int32_t offset_in_iovbase
;
3654 u_int32_t devblocksize
;
3655 u_int32_t mem_alignment_mask
;
3656 u_int32_t max_upl_size
;
3657 u_int32_t max_rd_size
;
3658 u_int32_t max_rd_ahead
;
3661 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
3663 max_rd_size
= max_upl_size
;
3664 max_rd_ahead
= max_rd_size
* 2;
3667 if (flags
& IO_PASSIVE
)
3672 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_START
,
3673 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
3675 iostate
.io_completed
= 0;
3676 iostate
.io_issued
= 0;
3677 iostate
.io_error
= 0;
3678 iostate
.io_wanted
= 0;
3680 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
3681 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
3683 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
3684 (int)devblocksize
, (int)mem_alignment_mask
, 0, 0, 0);
3686 if (devblocksize
== 1) {
3688 * the AFP client advertises a devblocksize of 1
3689 * however, its BLOCKMAP routine maps to physical
3690 * blocks that are PAGE_SIZE in size...
3691 * therefore we can't ask for I/Os that aren't page aligned
3692 * or aren't multiples of PAGE_SIZE in size
3693 * by setting devblocksize to PAGE_SIZE, we re-instate
3694 * the old behavior we had before the mem_alignment_mask
3695 * changes went in...
3697 devblocksize
= PAGE_SIZE
;
3700 io_req_size
= *read_length
;
3701 iov_base
= uio_curriovbase(uio
);
3703 max_io_size
= filesize
- uio
->uio_offset
;
3705 if ((off_t
)io_req_size
> max_io_size
)
3706 io_req_size
= max_io_size
;
3708 offset_in_file
= (u_int32_t
)uio
->uio_offset
& (devblocksize
- 1);
3709 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
3711 if (offset_in_file
|| offset_in_iovbase
) {
3713 * one of the 2 important offsets is misaligned
3714 * so fire an I/O through the cache for this entire vector
3718 if (iov_base
& (devblocksize
- 1)) {
3720 * the offset in memory must be on a device block boundary
3721 * so that we can guarantee that we can generate an
3722 * I/O that ends on a page boundary in cluster_io
3727 * When we get to this point, we know...
3728 * -- the offset into the file is on a devblocksize boundary
3731 while (io_req_size
&& retval
== 0) {
3734 if (cluster_hard_throttle_on(vp
)) {
3735 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
3736 max_rd_ahead
= HARD_THROTTLE_MAXSIZE
- 1;
3738 max_rd_size
= max_upl_size
;
3739 max_rd_ahead
= max_rd_size
* 2;
3741 io_start
= io_size
= io_req_size
;
3744 * First look for pages already in the cache
3745 * and move them to user space.
3747 * cluster_copy_ubc_data returns the resid
3750 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_size
, 0, 0);
3753 * calculate the number of bytes actually copied
3754 * starting size - residual
3756 xsize
= io_start
- io_size
;
3758 io_req_size
-= xsize
;
3761 * check to see if we are finished with this request...
3763 if (io_req_size
== 0 || misaligned
) {
3765 * see if there's another uio vector to
3766 * process that's of type IO_DIRECT
3768 * break out of while loop to get there
3773 * assume the request ends on a device block boundary
3775 io_min
= devblocksize
;
3778 * we can handle I/O's in multiples of the device block size
3779 * however, if io_size isn't a multiple of devblocksize we
3780 * want to clip it back to the nearest page boundary since
3781 * we are going to have to go through cluster_read_copy to
3782 * deal with the 'overhang'... by clipping it to a PAGE_SIZE
3783 * multiple, we avoid asking the drive for the same physical
3784 * blocks twice.. once for the partial page at the end of the
3785 * request and a 2nd time for the page we read into the cache
3786 * (which overlaps the end of the direct read) in order to
3787 * get at the overhang bytes
3789 if (io_size
& (devblocksize
- 1)) {
3791 * request does NOT end on a device block boundary
3792 * so clip it back to a PAGE_SIZE boundary
3794 io_size
&= ~PAGE_MASK
;
3797 if (retval
|| io_size
< io_min
) {
3799 * either an error or we only have the tail left to
3800 * complete via the copy path...
3801 * we may have already spun some portion of this request
3802 * off as async requests... we need to wait for the I/O
3803 * to complete before returning
3805 goto wait_for_dreads
;
3807 if ((xsize
= io_size
) > max_rd_size
)
3808 xsize
= max_rd_size
;
3812 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ xsize
, UPL_ROP_ABSENT
, (int *)&io_size
);
3816 * a page must have just come into the cache
3817 * since the first page in this range is no
3818 * longer absent, go back and re-evaluate
3822 iov_base
= uio_curriovbase(uio
);
3824 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
3825 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
3827 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_START
,
3828 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
3830 if (upl_offset
== 0 && ((io_size
& PAGE_MASK
) == 0)) {
3832 abort_flag
= UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
;
3835 abort_flag
= UPL_ABORT_FREE_ON_EMPTY
;
3837 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
3839 upl_size
= upl_needed_size
;
3840 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
3843 upl_flags
|= UPL_NOZEROFILL
;
3844 if (force_data_sync
)
3845 upl_flags
|= UPL_FORCE_DATA_SYNC
;
3847 kret
= vm_map_create_upl(current_map(),
3848 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
3849 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
);
3851 if (kret
!= KERN_SUCCESS
) {
3852 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
3853 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
3855 * failed to get pagelist
3857 * we may have already spun some portion of this request
3858 * off as async requests... we need to wait for the I/O
3859 * to complete before returning
3861 goto wait_for_dreads
;
3863 pages_in_pl
= upl_size
/ PAGE_SIZE
;
3864 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
3866 for (i
= 0; i
< pages_in_pl
; i
++) {
3867 if (!upl_valid_page(pl
, i
))
3870 if (i
== pages_in_pl
)
3873 ubc_upl_abort(upl
, abort_flag
);
3875 if (force_data_sync
>= 3) {
3876 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
3877 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
3879 goto wait_for_dreads
;
3882 * Consider the possibility that upl_size wasn't satisfied.
3884 if (upl_size
< upl_needed_size
) {
3885 if (upl_size
&& upl_offset
== 0)
3891 ubc_upl_abort(upl
, abort_flag
);
3892 goto wait_for_dreads
;
3894 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
3895 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
3898 * request asynchronously so that we can overlap
3899 * the preparation of the next I/O
3900 * if there are already too many outstanding reads
3901 * wait until some have completed before issuing the next read
3903 lck_mtx_lock(cl_mtxp
);
3905 while ((iostate
.io_issued
- iostate
.io_completed
) > max_rd_ahead
) {
3906 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
3907 iostate
.io_issued
, iostate
.io_completed
, max_rd_ahead
, 0, 0);
3909 iostate
.io_wanted
= 1;
3910 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_read_direct", NULL
);
3912 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
3913 iostate
.io_issued
, iostate
.io_completed
, max_rd_ahead
, 0, 0);
3915 lck_mtx_unlock(cl_mtxp
);
3917 if (iostate
.io_error
) {
3919 * one of the earlier reads we issued ran into a hard error
3920 * don't issue any more reads, cleanup the UPL
3921 * that was just created but not used, then
3922 * go wait for any other reads to complete before
3923 * returning the error to the caller
3925 ubc_upl_abort(upl
, abort_flag
);
3927 goto wait_for_dreads
;
3929 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_START
,
3930 (int)upl
, (int)upl_offset
, (int)uio
->uio_offset
, io_size
, 0);
3933 io_flag
= CL_COMMIT
| CL_READ
| CL_ASYNC
| CL_NOZERO
| CL_DIRECT_IO
| bflag
;
3935 io_flag
= CL_COMMIT
| CL_READ
| CL_ASYNC
| CL_NOZERO
| CL_DIRECT_IO
| CL_PRESERVE
| bflag
;
3937 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
, io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
3940 * update the uio structure
3942 uio_update(uio
, (user_size_t
)io_size
);
3944 io_req_size
-= io_size
;
3946 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_END
,
3947 (int)upl
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
3951 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0 && uio
->uio_offset
< filesize
) {
3953 retval
= cluster_io_type(uio
, read_type
, read_length
, 0);
3955 if (retval
== 0 && *read_type
== IO_DIRECT
) {
3957 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
3958 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
3965 if (iostate
.io_issued
) {
3967 * make sure all async reads that are part of this stream
3968 * have completed before we return
3970 lck_mtx_lock(cl_mtxp
);
3972 while (iostate
.io_issued
!= iostate
.io_completed
) {
3973 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
3974 iostate
.io_issued
, iostate
.io_completed
, 0, 0, 0);
3976 iostate
.io_wanted
= 1;
3977 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_read_direct", NULL
);
3979 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
3980 iostate
.io_issued
, iostate
.io_completed
, 0, 0, 0);
3982 lck_mtx_unlock(cl_mtxp
);
3985 if (iostate
.io_error
)
3986 retval
= iostate
.io_error
;
3988 if (io_req_size
&& retval
== 0) {
3990 * we couldn't handle the tail of this request in DIRECT mode
3991 * so fire it through the copy path
3993 retval
= cluster_read_copy(vp
, uio
, io_req_size
, filesize
, flags
, callback
, callback_arg
);
3995 *read_type
= IO_UNKNOWN
;
3997 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
3998 (int)uio
->uio_offset
, (int)uio_resid(uio
), io_req_size
, retval
, 0);
4005 cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
4006 int (*callback
)(buf_t
, void *), void *callback_arg
, int flags
)
4008 upl_page_info_t
*pl
;
4009 upl_t upl
[MAX_VECTS
];
4010 vm_offset_t upl_offset
;
4011 addr64_t dst_paddr
= 0;
4012 user_addr_t iov_base
;
4015 vm_size_t upl_needed_size
;
4016 mach_msg_type_number_t pages_in_pl
;
4019 struct clios iostate
;
4026 u_int32_t devblocksize
;
4027 u_int32_t mem_alignment_mask
;
4028 u_int32_t tail_size
= 0;
4031 if (flags
& IO_PASSIVE
)
4037 * When we enter this routine, we know
4038 * -- the read_length will not exceed the current iov_len
4039 * -- the target address is physically contiguous for read_length
4041 cluster_syncup(vp
, filesize
, callback
, callback_arg
);
4043 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
4044 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
4046 iostate
.io_completed
= 0;
4047 iostate
.io_issued
= 0;
4048 iostate
.io_error
= 0;
4049 iostate
.io_wanted
= 0;
4052 io_size
= *read_length
;
4054 max_size
= filesize
- uio
->uio_offset
;
4056 if (io_size
> max_size
)
4059 iov_base
= uio_curriovbase(uio
);
4061 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
4062 upl_needed_size
= upl_offset
+ io_size
;
4065 upl_size
= upl_needed_size
;
4066 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
4069 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_START
,
4070 (int)upl_offset
, (int)upl_size
, (int)iov_base
, io_size
, 0);
4072 kret
= vm_map_get_upl(current_map(),
4073 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4074 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, 0);
4076 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_END
,
4077 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4079 if (kret
!= KERN_SUCCESS
) {
4081 * failed to get pagelist
4084 goto wait_for_creads
;
4088 if (upl_size
< upl_needed_size
) {
4090 * The upl_size wasn't satisfied.
4093 goto wait_for_creads
;
4095 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
4097 dst_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)upl_offset
;
4099 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
4100 u_int32_t head_size
;
4102 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
4104 if (head_size
> io_size
)
4105 head_size
= io_size
;
4107 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, head_size
, CL_READ
, callback
, callback_arg
);
4110 goto wait_for_creads
;
4112 upl_offset
+= head_size
;
4113 dst_paddr
+= head_size
;
4114 io_size
-= head_size
;
4116 iov_base
+= head_size
;
4118 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
4120 * request doesn't set up on a memory boundary
4121 * the underlying DMA engine can handle...
4122 * return an error instead of going through
4123 * the slow copy path since the intent of this
4124 * path is direct I/O to device memory
4127 goto wait_for_creads
;
4130 tail_size
= io_size
& (devblocksize
- 1);
4132 io_size
-= tail_size
;
4134 while (io_size
&& error
== 0) {
4136 if (io_size
> MAX_IO_CONTIG_SIZE
)
4137 xsize
= MAX_IO_CONTIG_SIZE
;
4141 * request asynchronously so that we can overlap
4142 * the preparation of the next I/O... we'll do
4143 * the commit after all the I/O has completed
4144 * since its all issued against the same UPL
4145 * if there are already too many outstanding reads
4146 * wait until some have completed before issuing the next
4148 if (iostate
.io_issued
) {
4149 lck_mtx_lock(cl_mtxp
);
4151 while ((iostate
.io_issued
- iostate
.io_completed
) > (2 * MAX_IO_CONTIG_SIZE
)) {
4152 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
4153 iostate
.io_issued
, iostate
.io_completed
, 2 * MAX_IO_CONTIG_SIZE
, 0, 0);
4155 iostate
.io_wanted
= 1;
4156 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_read_contig", NULL
);
4158 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
4159 iostate
.io_issued
, iostate
.io_completed
, 2 * MAX_IO_CONTIG_SIZE
, 0, 0);
4161 lck_mtx_unlock(cl_mtxp
);
4163 if (iostate
.io_error
) {
4165 * one of the earlier reads we issued ran into a hard error
4166 * don't issue any more reads...
4167 * go wait for any other reads to complete before
4168 * returning the error to the caller
4170 goto wait_for_creads
;
4172 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
, xsize
,
4173 CL_READ
| CL_NOZERO
| CL_DEV_MEMORY
| CL_ASYNC
| bflag
,
4174 (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4176 * The cluster_io read was issued successfully,
4177 * update the uio structure
4180 uio_update(uio
, (user_size_t
)xsize
);
4183 upl_offset
+= xsize
;
4187 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
&& uio
->uio_offset
< filesize
) {
4189 error
= cluster_io_type(uio
, read_type
, read_length
, 0);
4191 if (error
== 0 && *read_type
== IO_CONTIG
) {
4196 *read_type
= IO_UNKNOWN
;
4200 * make sure all async reads that are part of this stream
4201 * have completed before we proceed
4203 lck_mtx_lock(cl_mtxp
);
4205 while (iostate
.io_issued
!= iostate
.io_completed
) {
4206 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
4207 iostate
.io_issued
, iostate
.io_completed
, 0, 0, 0);
4209 iostate
.io_wanted
= 1;
4210 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_read_contig", NULL
);
4212 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
4213 iostate
.io_issued
, iostate
.io_completed
, 0, 0, 0);
4215 lck_mtx_unlock(cl_mtxp
);
4217 if (iostate
.io_error
)
4218 error
= iostate
.io_error
;
4220 if (error
== 0 && tail_size
)
4221 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, tail_size
, CL_READ
, callback
, callback_arg
);
4223 for (n
= 0; n
< num_upl
; n
++)
4225 * just release our hold on each physically contiguous
4226 * region without changing any state
4228 ubc_upl_abort(upl
[n
], 0);
4235 cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
)
4237 user_size_t iov_len
;
4238 user_addr_t iov_base
= 0;
4245 * skip over any emtpy vectors
4247 uio_update(uio
, (user_size_t
)0);
4249 iov_len
= uio_curriovlen(uio
);
4251 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_START
, (int)uio
, (int)iov_len
, 0, 0, 0);
4254 iov_base
= uio_curriovbase(uio
);
4256 * make sure the size of the vector isn't too big...
4257 * internally, we want to handle all of the I/O in
4258 * chunk sizes that fit in a 32 bit int
4260 if (iov_len
> (user_size_t
)MAX_IO_REQUEST_SIZE
)
4261 upl_size
= MAX_IO_REQUEST_SIZE
;
4263 upl_size
= (u_int32_t
)iov_len
;
4265 upl_flags
= UPL_QUERY_OBJECT_TYPE
;
4267 if ((vm_map_get_upl(current_map(),
4268 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4269 &upl_size
, &upl
, NULL
, NULL
, &upl_flags
, 0)) != KERN_SUCCESS
) {
4271 * the user app must have passed in an invalid address
4278 *io_length
= upl_size
;
4280 if (upl_flags
& UPL_PHYS_CONTIG
)
4281 *io_type
= IO_CONTIG
;
4282 else if (iov_len
>= min_length
)
4283 *io_type
= IO_DIRECT
;
4288 * nothing left to do for this uio
4291 *io_type
= IO_UNKNOWN
;
4293 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_END
, (int)iov_base
, *io_type
, *io_length
, retval
, 0);
4300 * generate advisory I/O's in the largest chunks possible
4301 * the completed pages will be released into the VM cache
4304 advisory_read(vnode_t vp
, off_t filesize
, off_t f_offset
, int resid
)
4306 return advisory_read_ext(vp
, filesize
, f_offset
, resid
, NULL
, NULL
, CL_PASSIVE
);
4310 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
)
4312 upl_page_info_t
*pl
;
4314 vm_offset_t upl_offset
;
4327 uint32_t max_io_size
;
4330 if ( !UBCINFOEXISTS(vp
))
4333 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
4335 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_START
,
4336 (int)f_offset
, resid
, (int)filesize
, 0, 0);
4338 while (resid
&& f_offset
< filesize
&& retval
== 0) {
4340 * compute the size of the upl needed to encompass
4341 * the requested read... limit each call to cluster_io
4342 * to the maximum UPL size... cluster_io will clip if
4343 * this exceeds the maximum io_size for the device,
4344 * make sure to account for
4345 * a starting offset that's not page aligned
4347 start_offset
= (int)(f_offset
& PAGE_MASK_64
);
4348 upl_f_offset
= f_offset
- (off_t
)start_offset
;
4349 max_size
= filesize
- f_offset
;
4351 if (resid
< max_size
)
4356 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
4357 if ((uint32_t)upl_size
> max_io_size
)
4358 upl_size
= max_io_size
;
4362 * return the number of contiguously present pages in the cache
4363 * starting at upl_f_offset within the file
4365 ubc_range_op(vp
, upl_f_offset
, upl_f_offset
+ upl_size
, UPL_ROP_PRESENT
, &skip_range
);
4369 * skip over pages already present in the cache
4371 io_size
= skip_range
- start_offset
;
4373 f_offset
+= io_size
;
4376 if (skip_range
== upl_size
)
4379 * have to issue some real I/O
4380 * at this point, we know it's starting on a page boundary
4381 * because we've skipped over at least the first page in the request
4384 upl_f_offset
+= skip_range
;
4385 upl_size
-= skip_range
;
4387 pages_in_upl
= upl_size
/ PAGE_SIZE
;
4389 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_START
,
4390 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
4392 kret
= ubc_create_upl(vp
,
4397 UPL_RET_ONLY_ABSENT
| UPL_SET_LITE
);
4398 if (kret
!= KERN_SUCCESS
)
4403 * before we start marching forward, we must make sure we end on
4404 * a present page, otherwise we will be working with a freed
4407 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
4408 if (upl_page_present(pl
, last_pg
))
4411 pages_in_upl
= last_pg
+ 1;
4414 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_END
,
4415 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
4418 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
4420 * scan from the beginning of the upl looking for the first
4421 * page that is present.... this will become the first page in
4422 * the request we're going to make to 'cluster_io'... if all
4423 * of the pages are absent, we won't call through to 'cluster_io'
4425 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
4426 if (upl_page_present(pl
, start_pg
))
4431 * scan from the starting present page looking for an absent
4432 * page before the end of the upl is reached, if we
4433 * find one, then it will terminate the range of pages being
4434 * presented to 'cluster_io'
4436 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
4437 if (!upl_page_present(pl
, last_pg
))
4441 if (last_pg
> start_pg
) {
4443 * we found a range of pages that must be filled
4444 * if the last page in this range is the last page of the file
4445 * we may have to clip the size of it to keep from reading past
4446 * the end of the last physical block associated with the file
4448 upl_offset
= start_pg
* PAGE_SIZE
;
4449 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
4451 if ((upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
4452 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
4455 * issue an asynchronous read to cluster_io
4457 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
4458 CL_ASYNC
| CL_READ
| CL_COMMIT
| CL_AGE
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
4464 ubc_upl_abort(upl
, 0);
4466 io_size
= upl_size
- start_offset
;
4468 if (io_size
> resid
)
4470 f_offset
+= io_size
;
4474 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_END
,
4475 (int)f_offset
, resid
, retval
, 0, 0);
4482 cluster_push(vnode_t vp
, int flags
)
4484 return cluster_push_ext(vp
, flags
, NULL
, NULL
);
4489 cluster_push_ext(vnode_t vp
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
4492 struct cl_writebehind
*wbp
;
4494 if ( !UBCINFOEXISTS(vp
)) {
4495 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, (int)vp
, flags
, 0, -1, 0);
4498 /* return if deferred write is set */
4499 if (((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) && (flags
& IO_DEFWRITE
)) {
4502 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) == NULL
) {
4503 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, (int)vp
, flags
, 0, -2, 0);
4506 if (wbp
->cl_number
== 0 && wbp
->cl_scmap
== NULL
) {
4507 lck_mtx_unlock(&wbp
->cl_lockw
);
4509 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, (int)vp
, flags
, 0, -3, 0);
4512 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_START
,
4513 (int)wbp
->cl_scmap
, wbp
->cl_number
, flags
, 0, 0);
4515 if (wbp
->cl_scmap
) {
4516 sparse_cluster_push(wbp
, vp
, ubc_getsize(vp
), PUSH_ALL
| IO_PASSIVE
, callback
, callback_arg
);
4520 retval
= cluster_try_push(wbp
, vp
, ubc_getsize(vp
), PUSH_ALL
| IO_PASSIVE
, callback
, callback_arg
);
4522 lck_mtx_unlock(&wbp
->cl_lockw
);
4524 if (flags
& IO_SYNC
)
4525 (void)vnode_waitforwrites(vp
, 0, 0, 0, "cluster_push");
4527 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_END
,
4528 (int)wbp
->cl_scmap
, wbp
->cl_number
, retval
, 0, 0);
4534 __private_extern__
void
4535 cluster_release(struct ubc_info
*ubc
)
4537 struct cl_writebehind
*wbp
;
4538 struct cl_readahead
*rap
;
4540 if ((wbp
= ubc
->cl_wbehind
)) {
4542 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, (int)ubc
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
4545 vfs_drt_control(&(wbp
->cl_scmap
), 0);
4547 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, (int)ubc
, 0, 0, 0, 0);
4550 rap
= ubc
->cl_rahead
;
4553 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
4554 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
4556 if ((rap
= ubc
->cl_rahead
)) {
4557 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
4558 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
4560 ubc
->cl_rahead
= NULL
;
4561 ubc
->cl_wbehind
= NULL
;
4563 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_END
, (int)ubc
, (int)rap
, (int)wbp
, 0, 0);
4568 cluster_try_push(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int push_flag
, int (*callback
)(buf_t
, void *), void *callback_arg
)
4575 struct cl_wextent l_clusters
[MAX_CLUSTERS
];
4576 u_int max_cluster_pgcount
;
4579 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
4581 * the write behind context exists and has
4582 * already been locked...
4584 if (wbp
->cl_number
== 0)
4586 * no clusters to push
4587 * return number of empty slots
4589 return (MAX_CLUSTERS
);
4592 * make a local 'sorted' copy of the clusters
4593 * and clear wbp->cl_number so that new clusters can
4596 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
4597 for (min_index
= -1, cl_index1
= 0; cl_index1
< wbp
->cl_number
; cl_index1
++) {
4598 if (wbp
->cl_clusters
[cl_index1
].b_addr
== wbp
->cl_clusters
[cl_index1
].e_addr
)
4600 if (min_index
== -1)
4601 min_index
= cl_index1
;
4602 else if (wbp
->cl_clusters
[cl_index1
].b_addr
< wbp
->cl_clusters
[min_index
].b_addr
)
4603 min_index
= cl_index1
;
4605 if (min_index
== -1)
4607 l_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[min_index
].b_addr
;
4608 l_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
4609 l_clusters
[cl_index
].io_flags
= wbp
->cl_clusters
[min_index
].io_flags
;
4611 wbp
->cl_clusters
[min_index
].b_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
4617 if ( (push_flag
& PUSH_DELAY
) && cl_len
== MAX_CLUSTERS
) {
4621 * determine if we appear to be writing the file sequentially
4622 * if not, by returning without having pushed any clusters
4623 * we will cause this vnode to be pushed into the sparse cluster mechanism
4624 * used for managing more random I/O patterns
4626 * we know that we've got all clusters currently in use and the next write doesn't fit into one of them...
4627 * that's why we're in try_push with PUSH_DELAY...
4629 * check to make sure that all the clusters except the last one are 'full'... and that each cluster
4630 * is adjacent to the next (i.e. we're looking for sequential writes) they were sorted above
4631 * so we can just make a simple pass through, up to, but not including the last one...
4632 * note that e_addr is not inclusive, so it will be equal to the b_addr of the next cluster if they
4635 * we let the last one be partial as long as it was adjacent to the previous one...
4636 * we need to do this to deal with multi-threaded servers that might write an I/O or 2 out
4637 * of order... if this occurs at the tail of the last cluster, we don't want to fall into the sparse cluster world...
4639 for (i
= 0; i
< MAX_CLUSTERS
- 1; i
++) {
4640 if ((l_clusters
[i
].e_addr
- l_clusters
[i
].b_addr
) != max_cluster_pgcount
)
4642 if (l_clusters
[i
].e_addr
!= l_clusters
[i
+1].b_addr
)
4647 * drop the lock while we're firing off the I/Os...
4648 * this is safe since I'm working off of a private sorted copy
4649 * of the clusters, and I'm going to re-evaluate the public
4650 * state after I retake the lock
4652 * we need to drop it to avoid a lock inversion when trying to
4653 * grab pages into the UPL... another thread in 'write' may
4654 * have these pages in its UPL and be blocked trying to
4655 * gain the write-behind lock for this vnode
4657 lck_mtx_unlock(&wbp
->cl_lockw
);
4659 for (cl_index
= 0; cl_index
< cl_len
; cl_index
++) {
4661 struct cl_extent cl
;
4664 * try to push each cluster in turn...
4666 if (l_clusters
[cl_index
].io_flags
& CLW_IONOCACHE
)
4671 if ((l_clusters
[cl_index
].io_flags
& CLW_IOPASSIVE
) || (push_flag
& IO_PASSIVE
))
4672 flags
|= IO_PASSIVE
;
4674 if (push_flag
& PUSH_SYNC
)
4677 cl
.b_addr
= l_clusters
[cl_index
].b_addr
;
4678 cl
.e_addr
= l_clusters
[cl_index
].e_addr
;
4680 cluster_push_now(vp
, &cl
, EOF
, flags
, callback
, callback_arg
);
4682 l_clusters
[cl_index
].b_addr
= 0;
4683 l_clusters
[cl_index
].e_addr
= 0;
4687 if ( !(push_flag
& PUSH_ALL
) )
4690 lck_mtx_lock(&wbp
->cl_lockw
);
4693 if (cl_len
> cl_pushed
) {
4695 * we didn't push all of the clusters, so
4696 * lets try to merge them back in to the vnode
4698 if ((MAX_CLUSTERS
- wbp
->cl_number
) < (cl_len
- cl_pushed
)) {
4700 * we picked up some new clusters while we were trying to
4701 * push the old ones... this can happen because I've dropped
4702 * the vnode lock... the sum of the
4703 * leftovers plus the new cluster count exceeds our ability
4704 * to represent them, so switch to the sparse cluster mechanism
4706 * collect the active public clusters...
4708 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
);
4710 for (cl_index
= 0, cl_index1
= 0; cl_index
< cl_len
; cl_index
++) {
4711 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
4713 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
4714 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
4715 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
4720 * update the cluster count
4722 wbp
->cl_number
= cl_index1
;
4725 * and collect the original clusters that were moved into the
4726 * local storage for sorting purposes
4728 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
);
4732 * we've got room to merge the leftovers back in
4733 * just append them starting at the next 'hole'
4734 * represented by wbp->cl_number
4736 for (cl_index
= 0, cl_index1
= wbp
->cl_number
; cl_index
< cl_len
; cl_index
++) {
4737 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
4740 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
4741 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
4742 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
4747 * update the cluster count
4749 wbp
->cl_number
= cl_index1
;
4752 return (MAX_CLUSTERS
- wbp
->cl_number
);
4758 cluster_push_now(vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
4760 upl_page_info_t
*pl
;
4762 vm_offset_t upl_offset
;
4777 if (flags
& IO_PASSIVE
)
4782 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_START
,
4783 (int)cl
->b_addr
, (int)cl
->e_addr
, (int)EOF
, flags
, 0);
4785 if ((pages_in_upl
= (int)(cl
->e_addr
- cl
->b_addr
)) == 0) {
4786 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 0, 0, 0, 0);
4790 upl_size
= pages_in_upl
* PAGE_SIZE
;
4791 upl_f_offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
4793 if (upl_f_offset
+ upl_size
>= EOF
) {
4795 if (upl_f_offset
>= EOF
) {
4797 * must have truncated the file and missed
4798 * clearing a dangling cluster (i.e. it's completely
4799 * beyond the new EOF
4801 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 1, 0, 0, 0);
4805 size
= EOF
- upl_f_offset
;
4807 upl_size
= (size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
4808 pages_in_upl
= upl_size
/ PAGE_SIZE
;
4812 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, size
, 0, 0, 0);
4815 * by asking for UPL_COPYOUT_FROM and UPL_RET_ONLY_DIRTY, we get the following desirable behavior
4817 * - only pages that are currently dirty are returned... these are the ones we need to clean
4818 * - the hardware dirty bit is cleared when the page is gathered into the UPL... the software dirty bit is set
4819 * - 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
4820 * - when we commit the page, the software dirty bit is cleared... the hardware dirty bit is untouched so that if
4821 * someone dirties this page while the I/O is in progress, we don't lose track of the new state
4823 * when the I/O completes, we no longer ask for an explicit clear of the DIRTY state (either soft or hard)
4826 if ((vp
->v_flag
& VNOCACHE_DATA
) || (flags
& IO_NOCACHE
))
4827 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
| UPL_WILL_BE_DUMPED
;
4829 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
;
4831 kret
= ubc_create_upl(vp
,
4837 if (kret
!= KERN_SUCCESS
)
4838 panic("cluster_push: failed to get pagelist");
4840 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
, (int)upl
, upl_f_offset
, 0, 0, 0);
4843 * since we only asked for the dirty pages back
4844 * it's possible that we may only get a few or even none, so...
4845 * before we start marching forward, we must make sure we know
4846 * where the last present page is in the UPL, otherwise we could
4847 * end up working with a freed upl due to the FREE_ON_EMPTY semantics
4848 * employed by commit_range and abort_range.
4850 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
4851 if (upl_page_present(pl
, last_pg
))
4854 pages_in_upl
= last_pg
+ 1;
4856 if (pages_in_upl
== 0) {
4857 ubc_upl_abort(upl
, 0);
4859 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 2, 0, 0, 0);
4863 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
4865 * find the next dirty page in the UPL
4866 * this will become the first page in the
4867 * next I/O to generate
4869 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
4870 if (upl_dirty_page(pl
, start_pg
))
4872 if (upl_page_present(pl
, start_pg
))
4874 * RET_ONLY_DIRTY will return non-dirty 'precious' pages
4875 * just release these unchanged since we're not going
4876 * to steal them or change their state
4878 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
4880 if (start_pg
>= pages_in_upl
)
4882 * done... no more dirty pages to push
4885 if (start_pg
> last_pg
)
4887 * skipped over some non-dirty pages
4889 size
-= ((start_pg
- last_pg
) * PAGE_SIZE
);
4892 * find a range of dirty pages to write
4894 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
4895 if (!upl_dirty_page(pl
, last_pg
))
4898 upl_offset
= start_pg
* PAGE_SIZE
;
4900 io_size
= min(size
, (last_pg
- start_pg
) * PAGE_SIZE
);
4902 io_flags
= CL_THROTTLE
| CL_COMMIT
| CL_AGE
| bflag
;
4904 if ( !(flags
& IO_SYNC
))
4905 io_flags
|= CL_ASYNC
;
4907 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
4908 io_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
4910 if (error
== 0 && retval
)
4915 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 3, 0, 0, 0);
4922 * sparse_cluster_switch is called with the write behind lock held
4925 sparse_cluster_switch(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
4929 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_START
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
4931 if (wbp
->cl_scmap
== NULL
)
4932 wbp
->cl_scdirty
= 0;
4934 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
4936 struct cl_extent cl
;
4938 for (cl
.b_addr
= wbp
->cl_clusters
[cl_index
].b_addr
; cl
.b_addr
< wbp
->cl_clusters
[cl_index
].e_addr
; cl
.b_addr
++) {
4940 if (ubc_page_op(vp
, (off_t
)(cl
.b_addr
* PAGE_SIZE_64
), 0, NULL
, &flags
) == KERN_SUCCESS
) {
4941 if (flags
& UPL_POP_DIRTY
) {
4942 cl
.e_addr
= cl
.b_addr
+ 1;
4944 sparse_cluster_add(wbp
, vp
, &cl
, EOF
, callback
, callback_arg
);
4951 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_END
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
4956 * sparse_cluster_push is called with the write behind lock held
4959 sparse_cluster_push(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int push_flag
, int (*callback
)(buf_t
, void *), void *callback_arg
)
4961 struct cl_extent cl
;
4965 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_START
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, push_flag
, 0);
4967 if (push_flag
& PUSH_ALL
)
4968 vfs_drt_control(&(wbp
->cl_scmap
), 1);
4971 if (vfs_drt_get_cluster(&(wbp
->cl_scmap
), &offset
, &length
) != KERN_SUCCESS
)
4974 cl
.b_addr
= (daddr64_t
)(offset
/ PAGE_SIZE_64
);
4975 cl
.e_addr
= (daddr64_t
)((offset
+ length
) / PAGE_SIZE_64
);
4977 wbp
->cl_scdirty
-= (int)(cl
.e_addr
- cl
.b_addr
);
4980 * drop the lock while we're firing off the I/Os...
4981 * this is safe since I've already updated the state
4982 * this lock is protecting and I'm going to re-evaluate
4983 * the public state after I retake the lock
4985 * we need to drop it to avoid a lock inversion when trying to
4986 * grab pages into the UPL... another thread in 'write' may
4987 * have these pages in its UPL and be blocked trying to
4988 * gain the write-behind lock for this vnode
4990 lck_mtx_unlock(&wbp
->cl_lockw
);
4992 cluster_push_now(vp
, &cl
, EOF
, push_flag
& IO_PASSIVE
, callback
, callback_arg
);
4994 lck_mtx_lock(&wbp
->cl_lockw
);
4996 if ( !(push_flag
& PUSH_ALL
) )
4999 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_END
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
5004 * sparse_cluster_add is called with the write behind lock held
5007 sparse_cluster_add(struct cl_writebehind
*wbp
, vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5013 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_START
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, (int)cl
->b_addr
, (int)cl
->e_addr
, 0);
5015 offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
5016 length
= ((u_int
)(cl
->e_addr
- cl
->b_addr
)) * PAGE_SIZE
;
5018 while (vfs_drt_mark_pages(&(wbp
->cl_scmap
), offset
, length
, &new_dirty
) != KERN_SUCCESS
) {
5020 * no room left in the map
5021 * only a partial update was done
5022 * push out some pages and try again
5024 wbp
->cl_scdirty
+= new_dirty
;
5026 sparse_cluster_push(wbp
, vp
, EOF
, 0, callback
, callback_arg
);
5028 offset
+= (new_dirty
* PAGE_SIZE_64
);
5029 length
-= (new_dirty
* PAGE_SIZE
);
5031 wbp
->cl_scdirty
+= new_dirty
;
5033 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_END
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
5038 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
)
5040 upl_page_info_t
*pl
;
5050 if (flags
& IO_PASSIVE
)
5055 upl_flags
= UPL_SET_LITE
;
5057 if ( !(flags
& CL_READ
) ) {
5059 * "write" operation: let the UPL subsystem know
5060 * that we intend to modify the buffer cache pages
5063 upl_flags
|= UPL_WILL_MODIFY
;
5066 * indicate that there is no need to pull the
5067 * mapping for this page... we're only going
5068 * to read from it, not modify it.
5070 upl_flags
|= UPL_FILE_IO
;
5072 kret
= ubc_create_upl(vp
,
5073 uio
->uio_offset
& ~PAGE_MASK_64
,
5079 if (kret
!= KERN_SUCCESS
)
5082 if (!upl_valid_page(pl
, 0)) {
5084 * issue a synchronous read to cluster_io
5086 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
5087 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5089 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
5095 ubc_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)(uio
->uio_offset
& PAGE_MASK_64
);
5098 * NOTE: There is no prototype for the following in BSD. It, and the definitions
5099 * of the defines for cppvPsrc, cppvPsnk, cppvFsnk, and cppvFsrc will be found in
5100 * osfmk/ppc/mappings.h. They are not included here because there appears to be no
5101 * way to do so without exporting them to kexts as well.
5103 if (flags
& CL_READ
)
5104 // copypv(ubc_paddr, usr_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsnk); /* Copy physical to physical and flush the destination */
5105 copypv(ubc_paddr
, usr_paddr
, xsize
, 2 | 1 | 4); /* Copy physical to physical and flush the destination */
5107 // copypv(usr_paddr, ubc_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsrc); /* Copy physical to physical and flush the source */
5108 copypv(usr_paddr
, ubc_paddr
, xsize
, 2 | 1 | 8); /* Copy physical to physical and flush the source */
5110 if ( !(flags
& CL_READ
) || (upl_valid_page(pl
, 0) && upl_dirty_page(pl
, 0))) {
5112 * issue a synchronous write to cluster_io
5114 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
5115 bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5118 uio_update(uio
, (user_size_t
)xsize
);
5121 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
5123 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
5125 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, abort_flags
);
5133 cluster_copy_upl_data(struct uio
*uio
, upl_t upl
, int upl_offset
, int *io_resid
)
5141 upl_page_info_t
*pl
;
5145 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
5146 (int)uio
->uio_offset
, upl_offset
, xsize
, 0, 0);
5148 segflg
= uio
->uio_segflg
;
5152 case UIO_USERSPACE32
:
5153 case UIO_USERISPACE32
:
5154 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
5158 case UIO_USERISPACE
:
5159 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
5162 case UIO_USERSPACE64
:
5163 case UIO_USERISPACE64
:
5164 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
5167 case UIO_SYSSPACE32
:
5168 uio
->uio_segflg
= UIO_PHYS_SYSSPACE32
;
5172 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
5175 case UIO_SYSSPACE64
:
5176 uio
->uio_segflg
= UIO_PHYS_SYSSPACE64
;
5179 pl
= ubc_upl_pageinfo(upl
);
5181 pg_index
= upl_offset
/ PAGE_SIZE
;
5182 pg_offset
= upl_offset
& PAGE_MASK
;
5183 csize
= min(PAGE_SIZE
- pg_offset
, xsize
);
5185 while (xsize
&& retval
== 0) {
5188 paddr
= ((addr64_t
)upl_phys_page(pl
, pg_index
) << 12) + pg_offset
;
5190 retval
= uiomove64(paddr
, csize
, uio
);
5195 csize
= min(PAGE_SIZE
, xsize
);
5199 uio
->uio_segflg
= segflg
;
5201 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
5202 (int)uio
->uio_offset
, xsize
, retval
, segflg
, 0);
5209 cluster_copy_ubc_data(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
)
5212 return (cluster_copy_ubc_data_internal(vp
, uio
, io_resid
, mark_dirty
, 1));
5217 cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
)
5224 memory_object_control_t control
;
5226 io_size
= *io_resid
;
5228 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
5229 (int)uio
->uio_offset
, 0, io_size
, 0, 0);
5231 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
5233 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
5234 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
5235 (int)uio
->uio_offset
, io_size
, retval
, 3, 0);
5239 segflg
= uio
->uio_segflg
;
5243 case UIO_USERSPACE32
:
5244 case UIO_USERISPACE32
:
5245 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
5248 case UIO_USERSPACE64
:
5249 case UIO_USERISPACE64
:
5250 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
5253 case UIO_SYSSPACE32
:
5254 uio
->uio_segflg
= UIO_PHYS_SYSSPACE32
;
5257 case UIO_SYSSPACE64
:
5258 uio
->uio_segflg
= UIO_PHYS_SYSSPACE64
;
5262 case UIO_USERISPACE
:
5263 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
5267 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
5271 if ( (io_size
= *io_resid
) ) {
5272 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
5273 xsize
= uio_resid(uio
);
5275 retval
= memory_object_control_uiomove(control
, uio
->uio_offset
- start_offset
, uio
,
5276 start_offset
, io_size
, mark_dirty
, take_reference
);
5277 xsize
-= uio_resid(uio
);
5280 uio
->uio_segflg
= segflg
;
5281 *io_resid
= io_size
;
5283 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
5284 (int)uio
->uio_offset
, io_size
, retval
, 0x80000000 | segflg
, 0);
5291 is_file_clean(vnode_t vp
, off_t filesize
)
5295 int total_dirty
= 0;
5297 for (f_offset
= 0; f_offset
< filesize
; f_offset
+= PAGE_SIZE_64
) {
5298 if (ubc_page_op(vp
, f_offset
, 0, NULL
, &flags
) == KERN_SUCCESS
) {
5299 if (flags
& UPL_POP_DIRTY
) {
5313 * Dirty region tracking/clustering mechanism.
5315 * This code (vfs_drt_*) provides a mechanism for tracking and clustering
5316 * dirty regions within a larger space (file). It is primarily intended to
5317 * support clustering in large files with many dirty areas.
5319 * The implementation assumes that the dirty regions are pages.
5321 * To represent dirty pages within the file, we store bit vectors in a
5322 * variable-size circular hash.
5326 * Bitvector size. This determines the number of pages we group in a
5327 * single hashtable entry. Each hashtable entry is aligned to this
5328 * size within the file.
5330 #define DRT_BITVECTOR_PAGES 256
5333 * File offset handling.
5335 * DRT_ADDRESS_MASK is dependent on DRT_BITVECTOR_PAGES;
5336 * the correct formula is (~(DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1)
5338 #define DRT_ADDRESS_MASK (~((1 << 20) - 1))
5339 #define DRT_ALIGN_ADDRESS(addr) ((addr) & DRT_ADDRESS_MASK)
5342 * Hashtable address field handling.
5344 * The low-order bits of the hashtable address are used to conserve
5347 * DRT_HASH_COUNT_MASK must be large enough to store the range
5348 * 0-DRT_BITVECTOR_PAGES inclusive, as well as have one value
5349 * to indicate that the bucket is actually unoccupied.
5351 #define DRT_HASH_GET_ADDRESS(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_ADDRESS_MASK)
5352 #define DRT_HASH_SET_ADDRESS(scm, i, a) \
5354 (scm)->scm_hashtable[(i)].dhe_control = \
5355 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_ADDRESS_MASK) | DRT_ALIGN_ADDRESS(a); \
5357 #define DRT_HASH_COUNT_MASK 0x1ff
5358 #define DRT_HASH_GET_COUNT(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_HASH_COUNT_MASK)
5359 #define DRT_HASH_SET_COUNT(scm, i, c) \
5361 (scm)->scm_hashtable[(i)].dhe_control = \
5362 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_HASH_COUNT_MASK) | ((c) & DRT_HASH_COUNT_MASK); \
5364 #define DRT_HASH_CLEAR(scm, i) \
5366 (scm)->scm_hashtable[(i)].dhe_control = 0; \
5368 #define DRT_HASH_VACATE(scm, i) DRT_HASH_SET_COUNT((scm), (i), DRT_HASH_COUNT_MASK)
5369 #define DRT_HASH_VACANT(scm, i) (DRT_HASH_GET_COUNT((scm), (i)) == DRT_HASH_COUNT_MASK)
5370 #define DRT_HASH_COPY(oscm, oi, scm, i) \
5372 (scm)->scm_hashtable[(i)].dhe_control = (oscm)->scm_hashtable[(oi)].dhe_control; \
5373 DRT_BITVECTOR_COPY(oscm, oi, scm, i); \
5378 * Hash table moduli.
5380 * Since the hashtable entry's size is dependent on the size of
5381 * the bitvector, and since the hashtable size is constrained to
5382 * both being prime and fitting within the desired allocation
5383 * size, these values need to be manually determined.
5385 * For DRT_BITVECTOR_SIZE = 256, the entry size is 40 bytes.
5387 * The small hashtable allocation is 1024 bytes, so the modulus is 23.
5388 * The large hashtable allocation is 16384 bytes, so the modulus is 401.
5390 #define DRT_HASH_SMALL_MODULUS 23
5391 #define DRT_HASH_LARGE_MODULUS 401
5393 #define DRT_SMALL_ALLOCATION 1024 /* 104 bytes spare */
5394 #define DRT_LARGE_ALLOCATION 16384 /* 344 bytes spare */
5396 /* *** nothing below here has secret dependencies on DRT_BITVECTOR_PAGES *** */
5399 * Hashtable bitvector handling.
5401 * Bitvector fields are 32 bits long.
5404 #define DRT_HASH_SET_BIT(scm, i, bit) \
5405 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] |= (1 << ((bit) % 32))
5407 #define DRT_HASH_CLEAR_BIT(scm, i, bit) \
5408 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] &= ~(1 << ((bit) % 32))
5410 #define DRT_HASH_TEST_BIT(scm, i, bit) \
5411 ((scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] & (1 << ((bit) % 32)))
5413 #define DRT_BITVECTOR_CLEAR(scm, i) \
5414 bzero(&(scm)->scm_hashtable[(i)].dhe_bitvector[0], (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
5416 #define DRT_BITVECTOR_COPY(oscm, oi, scm, i) \
5417 bcopy(&(oscm)->scm_hashtable[(oi)].dhe_bitvector[0], \
5418 &(scm)->scm_hashtable[(i)].dhe_bitvector[0], \
5419 (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
5426 struct vfs_drt_hashentry
{
5427 u_int64_t dhe_control
;
5428 u_int32_t dhe_bitvector
[DRT_BITVECTOR_PAGES
/ 32];
5432 * Dirty Region Tracking structure.
5434 * The hashtable is allocated entirely inside the DRT structure.
5436 * The hash is a simple circular prime modulus arrangement, the structure
5437 * is resized from small to large if it overflows.
5440 struct vfs_drt_clustermap
{
5441 u_int32_t scm_magic
; /* sanity/detection */
5442 #define DRT_SCM_MAGIC 0x12020003
5443 u_int32_t scm_modulus
; /* current ring size */
5444 u_int32_t scm_buckets
; /* number of occupied buckets */
5445 u_int32_t scm_lastclean
; /* last entry we cleaned */
5446 u_int32_t scm_iskips
; /* number of slot skips */
5448 struct vfs_drt_hashentry scm_hashtable
[0];
5452 #define DRT_HASH(scm, addr) ((addr) % (scm)->scm_modulus)
5453 #define DRT_HASH_NEXT(scm, addr) (((addr) + 1) % (scm)->scm_modulus)
5456 * Debugging codes and arguments.
5458 #define DRT_DEBUG_EMPTYFREE (FSDBG_CODE(DBG_FSRW, 82)) /* nil */
5459 #define DRT_DEBUG_RETCLUSTER (FSDBG_CODE(DBG_FSRW, 83)) /* offset, length */
5460 #define DRT_DEBUG_ALLOC (FSDBG_CODE(DBG_FSRW, 84)) /* copycount */
5461 #define DRT_DEBUG_INSERT (FSDBG_CODE(DBG_FSRW, 85)) /* offset, iskip */
5462 #define DRT_DEBUG_MARK (FSDBG_CODE(DBG_FSRW, 86)) /* offset, length,
5465 /* 1 (clean, no map) */
5466 /* 2 (map alloc fail) */
5467 /* 3, resid (partial) */
5468 #define DRT_DEBUG_6 (FSDBG_CODE(DBG_FSRW, 87))
5469 #define DRT_DEBUG_SCMDATA (FSDBG_CODE(DBG_FSRW, 88)) /* modulus, buckets,
5470 * lastclean, iskips */
5473 static kern_return_t
vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
);
5474 static kern_return_t
vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
);
5475 static kern_return_t
vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
,
5476 u_int64_t offset
, int *indexp
);
5477 static kern_return_t
vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
,
5481 static kern_return_t
vfs_drt_do_mark_pages(
5487 static void vfs_drt_trace(
5488 struct vfs_drt_clustermap
*cmap
,
5497 * Allocate and initialise a sparse cluster map.
5499 * Will allocate a new map, resize or compact an existing map.
5501 * XXX we should probably have at least one intermediate map size,
5502 * as the 1:16 ratio seems a bit drastic.
5504 static kern_return_t
5505 vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
)
5507 struct vfs_drt_clustermap
*cmap
, *ocmap
;
5511 int nsize
, active_buckets
, index
, copycount
;
5518 * Decide on the size of the new map.
5520 if (ocmap
== NULL
) {
5521 nsize
= DRT_HASH_SMALL_MODULUS
;
5523 /* count the number of active buckets in the old map */
5525 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
5526 if (!DRT_HASH_VACANT(ocmap
, i
) &&
5527 (DRT_HASH_GET_COUNT(ocmap
, i
) != 0))
5531 * If we're currently using the small allocation, check to
5532 * see whether we should grow to the large one.
5534 if (ocmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) {
5535 /* if the ring is nearly full */
5536 if (active_buckets
> (DRT_HASH_SMALL_MODULUS
- 5)) {
5537 nsize
= DRT_HASH_LARGE_MODULUS
;
5539 nsize
= DRT_HASH_SMALL_MODULUS
;
5542 /* already using the large modulus */
5543 nsize
= DRT_HASH_LARGE_MODULUS
;
5545 * If the ring is completely full, there's
5546 * nothing useful for us to do. Behave as
5547 * though we had compacted into the new
5550 if (active_buckets
>= DRT_HASH_LARGE_MODULUS
)
5551 return(KERN_SUCCESS
);
5556 * Allocate and initialise the new map.
5559 kret
= kmem_alloc(kernel_map
, (vm_offset_t
*)&cmap
,
5560 (nsize
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
5561 if (kret
!= KERN_SUCCESS
)
5563 cmap
->scm_magic
= DRT_SCM_MAGIC
;
5564 cmap
->scm_modulus
= nsize
;
5565 cmap
->scm_buckets
= 0;
5566 cmap
->scm_lastclean
= 0;
5567 cmap
->scm_iskips
= 0;
5568 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
5569 DRT_HASH_CLEAR(cmap
, i
);
5570 DRT_HASH_VACATE(cmap
, i
);
5571 DRT_BITVECTOR_CLEAR(cmap
, i
);
5575 * If there's an old map, re-hash entries from it into the new map.
5578 if (ocmap
!= NULL
) {
5579 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
5580 /* skip empty buckets */
5581 if (DRT_HASH_VACANT(ocmap
, i
) ||
5582 (DRT_HASH_GET_COUNT(ocmap
, i
) == 0))
5585 offset
= DRT_HASH_GET_ADDRESS(ocmap
, i
);
5586 kret
= vfs_drt_get_index(&cmap
, offset
, &index
, 1);
5587 if (kret
!= KERN_SUCCESS
) {
5588 /* XXX need to bail out gracefully here */
5589 panic("vfs_drt: new cluster map mysteriously too small");
5593 DRT_HASH_COPY(ocmap
, i
, cmap
, index
);
5598 /* log what we've done */
5599 vfs_drt_trace(cmap
, DRT_DEBUG_ALLOC
, copycount
, 0, 0, 0);
5602 * It's important to ensure that *cmapp always points to
5603 * a valid map, so we must overwrite it before freeing
5607 if (ocmap
!= NULL
) {
5608 /* emit stats into trace buffer */
5609 vfs_drt_trace(ocmap
, DRT_DEBUG_SCMDATA
,
5612 ocmap
->scm_lastclean
,
5615 vfs_drt_free_map(ocmap
);
5617 return(KERN_SUCCESS
);
5622 * Free a sparse cluster map.
5624 static kern_return_t
5625 vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
)
5627 kmem_free(kernel_map
, (vm_offset_t
)cmap
,
5628 (cmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
5629 return(KERN_SUCCESS
);
5634 * Find the hashtable slot currently occupied by an entry for the supplied offset.
5636 static kern_return_t
5637 vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
, u_int64_t offset
, int *indexp
)
5642 offset
= DRT_ALIGN_ADDRESS(offset
);
5643 index
= DRT_HASH(cmap
, offset
);
5645 /* traverse the hashtable */
5646 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
5649 * If the slot is vacant, we can stop.
5651 if (DRT_HASH_VACANT(cmap
, index
))
5655 * If the address matches our offset, we have success.
5657 if (DRT_HASH_GET_ADDRESS(cmap
, index
) == offset
) {
5659 return(KERN_SUCCESS
);
5663 * Move to the next slot, try again.
5665 index
= DRT_HASH_NEXT(cmap
, index
);
5670 return(KERN_FAILURE
);
5674 * Find the hashtable slot for the supplied offset. If we haven't allocated
5675 * one yet, allocate one and populate the address field. Note that it will
5676 * not have a nonzero page count and thus will still technically be free, so
5677 * in the case where we are called to clean pages, the slot will remain free.
5679 static kern_return_t
5680 vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
, u_int64_t offset
, int *indexp
, int recursed
)
5682 struct vfs_drt_clustermap
*cmap
;
5689 /* look for an existing entry */
5690 kret
= vfs_drt_search_index(cmap
, offset
, indexp
);
5691 if (kret
== KERN_SUCCESS
)
5694 /* need to allocate an entry */
5695 offset
= DRT_ALIGN_ADDRESS(offset
);
5696 index
= DRT_HASH(cmap
, offset
);
5698 /* scan from the index forwards looking for a vacant slot */
5699 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
5701 if (DRT_HASH_VACANT(cmap
, index
) || DRT_HASH_GET_COUNT(cmap
,index
) == 0) {
5702 cmap
->scm_buckets
++;
5703 if (index
< cmap
->scm_lastclean
)
5704 cmap
->scm_lastclean
= index
;
5705 DRT_HASH_SET_ADDRESS(cmap
, index
, offset
);
5706 DRT_HASH_SET_COUNT(cmap
, index
, 0);
5707 DRT_BITVECTOR_CLEAR(cmap
, index
);
5709 vfs_drt_trace(cmap
, DRT_DEBUG_INSERT
, (int)offset
, i
, 0, 0);
5710 return(KERN_SUCCESS
);
5712 cmap
->scm_iskips
+= i
;
5713 index
= DRT_HASH_NEXT(cmap
, index
);
5717 * We haven't found a vacant slot, so the map is full. If we're not
5718 * already recursed, try reallocating/compacting it.
5721 return(KERN_FAILURE
);
5722 kret
= vfs_drt_alloc_map(cmapp
);
5723 if (kret
== KERN_SUCCESS
) {
5724 /* now try to insert again */
5725 kret
= vfs_drt_get_index(cmapp
, offset
, indexp
, 1);
5731 * Implementation of set dirty/clean.
5733 * In the 'clean' case, not finding a map is OK.
5735 static kern_return_t
5736 vfs_drt_do_mark_pages(
5743 struct vfs_drt_clustermap
*cmap
, **cmapp
;
5745 int i
, index
, pgoff
, pgcount
, setcount
, ecount
;
5747 cmapp
= (struct vfs_drt_clustermap
**)private;
5750 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_START
, (int)offset
, (int)length
, dirty
, 0);
5752 if (setcountp
!= NULL
)
5755 /* allocate a cluster map if we don't already have one */
5757 /* no cluster map, nothing to clean */
5759 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 1, 0, 0, 0);
5760 return(KERN_SUCCESS
);
5762 kret
= vfs_drt_alloc_map(cmapp
);
5763 if (kret
!= KERN_SUCCESS
) {
5764 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 2, 0, 0, 0);
5771 * Iterate over the length of the region.
5773 while (length
> 0) {
5775 * Get the hashtable index for this offset.
5777 * XXX this will add blank entries if we are clearing a range
5778 * that hasn't been dirtied.
5780 kret
= vfs_drt_get_index(cmapp
, offset
, &index
, 0);
5781 cmap
= *cmapp
; /* may have changed! */
5782 /* this may be a partial-success return */
5783 if (kret
!= KERN_SUCCESS
) {
5784 if (setcountp
!= NULL
)
5785 *setcountp
= setcount
;
5786 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 3, (int)length
, 0, 0);
5792 * Work out how many pages we're modifying in this
5795 pgoff
= (offset
- DRT_ALIGN_ADDRESS(offset
)) / PAGE_SIZE
;
5796 pgcount
= min((length
/ PAGE_SIZE
), (DRT_BITVECTOR_PAGES
- pgoff
));
5799 * Iterate over pages, dirty/clearing as we go.
5801 ecount
= DRT_HASH_GET_COUNT(cmap
, index
);
5802 for (i
= 0; i
< pgcount
; i
++) {
5804 if (!DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
5805 DRT_HASH_SET_BIT(cmap
, index
, pgoff
+ i
);
5810 if (DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
5811 DRT_HASH_CLEAR_BIT(cmap
, index
, pgoff
+ i
);
5817 DRT_HASH_SET_COUNT(cmap
, index
, ecount
);
5819 offset
+= pgcount
* PAGE_SIZE
;
5820 length
-= pgcount
* PAGE_SIZE
;
5822 if (setcountp
!= NULL
)
5823 *setcountp
= setcount
;
5825 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 0, setcount
, 0, 0);
5827 return(KERN_SUCCESS
);
5831 * Mark a set of pages as dirty/clean.
5833 * This is a public interface.
5836 * Pointer to storage suitable for holding a pointer. Note that
5837 * this must either be NULL or a value set by this function.
5840 * Current file size in bytes.
5843 * Offset of the first page to be marked as dirty, in bytes. Must be
5847 * Length of dirty region, in bytes. Must be a multiple of PAGE_SIZE.
5850 * Number of pages newly marked dirty by this call (optional).
5852 * Returns KERN_SUCCESS if all the pages were successfully marked.
5854 static kern_return_t
5855 vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
)
5857 /* XXX size unused, drop from interface */
5858 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, setcountp
, 1));
5862 static kern_return_t
5863 vfs_drt_unmark_pages(void **cmapp
, off_t offset
, u_int length
)
5865 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0));
5870 * Get a cluster of dirty pages.
5872 * This is a public interface.
5875 * Pointer to storage managed by drt_mark_pages. Note that this must
5876 * be NULL or a value set by drt_mark_pages.
5879 * Returns the byte offset into the file of the first page in the cluster.
5882 * Returns the length in bytes of the cluster of dirty pages.
5884 * Returns success if a cluster was found. If KERN_FAILURE is returned, there
5885 * are no dirty pages meeting the minmum size criteria. Private storage will
5886 * be released if there are no more dirty pages left in the map
5889 static kern_return_t
5890 vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
)
5892 struct vfs_drt_clustermap
*cmap
;
5896 int index
, i
, fs
, ls
;
5899 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
5900 return(KERN_FAILURE
);
5903 /* walk the hashtable */
5904 for (offset
= 0, j
= 0; j
< cmap
->scm_modulus
; offset
+= (DRT_BITVECTOR_PAGES
* PAGE_SIZE
), j
++) {
5905 index
= DRT_HASH(cmap
, offset
);
5907 if (DRT_HASH_VACANT(cmap
, index
) || (DRT_HASH_GET_COUNT(cmap
, index
) == 0))
5910 /* scan the bitfield for a string of bits */
5913 for (i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
5914 if (DRT_HASH_TEST_BIT(cmap
, index
, i
)) {
5920 /* didn't find any bits set */
5921 panic("vfs_drt: entry summary count > 0 but no bits set in map");
5923 for (ls
= 0; i
< DRT_BITVECTOR_PAGES
; i
++, ls
++) {
5924 if (!DRT_HASH_TEST_BIT(cmap
, index
, i
))
5928 /* compute offset and length, mark pages clean */
5929 offset
= DRT_HASH_GET_ADDRESS(cmap
, index
) + (PAGE_SIZE
* fs
);
5930 length
= ls
* PAGE_SIZE
;
5931 vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0);
5932 cmap
->scm_lastclean
= index
;
5934 /* return successful */
5935 *offsetp
= (off_t
)offset
;
5938 vfs_drt_trace(cmap
, DRT_DEBUG_RETCLUSTER
, (int)offset
, (int)length
, 0, 0);
5939 return(KERN_SUCCESS
);
5942 * We didn't find anything... hashtable is empty
5943 * emit stats into trace buffer and
5946 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
5949 cmap
->scm_lastclean
,
5952 vfs_drt_free_map(cmap
);
5955 return(KERN_FAILURE
);
5959 static kern_return_t
5960 vfs_drt_control(void **cmapp
, int op_type
)
5962 struct vfs_drt_clustermap
*cmap
;
5965 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
5966 return(KERN_FAILURE
);
5971 /* emit stats into trace buffer */
5972 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
5975 cmap
->scm_lastclean
,
5978 vfs_drt_free_map(cmap
);
5983 cmap
->scm_lastclean
= 0;
5986 return(KERN_SUCCESS
);
5992 * Emit a summary of the state of the clustermap into the trace buffer
5993 * along with some caller-provided data.
5997 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, int code
, int arg1
, int arg2
, int arg3
, int arg4
)
5999 KERNEL_DEBUG(code
, arg1
, arg2
, arg3
, arg4
, 0);
6003 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, __unused
int code
,
6004 __unused
int arg1
, __unused
int arg2
, __unused
int arg3
,
6012 * Perform basic sanity check on the hash entry summary count
6013 * vs. the actual bits set in the entry.
6016 vfs_drt_sanity(struct vfs_drt_clustermap
*cmap
)
6021 for (index
= 0; index
< cmap
->scm_modulus
; index
++) {
6022 if (DRT_HASH_VACANT(cmap
, index
))
6025 for (bits_on
= 0, i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
6026 if (DRT_HASH_TEST_BIT(cmap
, index
, i
))
6029 if (bits_on
!= DRT_HASH_GET_COUNT(cmap
, index
))
6030 panic("bits_on = %d, index = %d\n", bits_on
, index
);