2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)vfs_cluster.c 8.10 (Berkeley) 3/28/95
64 #include <sys/param.h>
65 #include <sys/proc_internal.h>
66 #include <sys/buf_internal.h>
67 #include <sys/mount_internal.h>
68 #include <sys/vnode_internal.h>
69 #include <sys/trace.h>
70 #include <sys/malloc.h>
72 #include <sys/kernel.h>
73 #include <sys/resourcevar.h>
74 #include <miscfs/specfs/specdev.h>
75 #include <sys/uio_internal.h>
76 #include <libkern/libkern.h>
77 #include <machine/machine_routines.h>
79 #include <sys/ubc_internal.h>
80 #include <vm/vnode_pager.h>
82 #include <mach/mach_types.h>
83 #include <mach/memory_object_types.h>
84 #include <mach/vm_map.h>
86 #include <kern/task.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_map.h>
90 #include <vm/vm_pageout.h>
92 #include <sys/kdebug.h>
93 #include <libkern/OSAtomic.h>
99 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
104 #define CL_WRITE 0x02
105 #define CL_ASYNC 0x04
106 #define CL_COMMIT 0x08
107 #define CL_PAGEOUT 0x10
109 #define CL_NOZERO 0x40
110 #define CL_PAGEIN 0x80
111 #define CL_DEV_MEMORY 0x100
112 #define CL_PRESERVE 0x200
113 #define CL_THROTTLE 0x400
114 #define CL_KEEPCACHED 0x800
115 #define CL_DIRECT_IO 0x1000
116 #define CL_PASSIVE 0x2000
117 #define CL_IOSTREAMING 0x4000
118 #define CL_CLOSE 0x8000
119 #define CL_ENCRYPTED 0x10000
120 #define CL_RAW_ENCRYPTED 0x20000
121 #define CL_NOCACHE 0x40000
123 #define MAX_VECTOR_UPL_ELEMENTS 8
124 #define MAX_VECTOR_UPL_SIZE (2 * MAX_UPL_SIZE) * PAGE_SIZE
126 extern upl_t
vector_upl_create(vm_offset_t
);
127 extern boolean_t
vector_upl_is_valid(upl_t
);
128 extern boolean_t
vector_upl_set_subupl(upl_t
,upl_t
, u_int32_t
);
129 extern void vector_upl_set_pagelist(upl_t
);
130 extern void vector_upl_set_iostate(upl_t
, upl_t
, vm_offset_t
, u_int32_t
);
134 u_int io_completed
; /* amount of io that has currently completed */
135 u_int io_issued
; /* amount of io that was successfully issued */
136 int io_error
; /* error code of first error encountered */
137 int io_wanted
; /* someone is sleeping waiting for a change in state */
140 static lck_grp_t
*cl_mtx_grp
;
141 static lck_attr_t
*cl_mtx_attr
;
142 static lck_grp_attr_t
*cl_mtx_grp_attr
;
143 static lck_mtx_t
*cl_transaction_mtxp
;
151 #define PUSH_DELAY 0x01
152 #define PUSH_ALL 0x02
153 #define PUSH_SYNC 0x04
156 static void cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
);
157 static void cluster_wait_IO(buf_t cbp_head
, int async
);
158 static void cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
);
160 static int cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
);
162 static int cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
163 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*)(buf_t
, void *), void *callback_arg
);
164 static int cluster_iodone(buf_t bp
, void *callback_arg
);
165 static int cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
, vnode_t vp
);
166 static int cluster_is_throttled(vnode_t vp
);
168 static void cluster_iostate_wait(struct clios
*iostate
, u_int target
, const char *wait_name
);
170 static void cluster_syncup(vnode_t vp
, off_t newEOF
, int (*)(buf_t
, void *), void *callback_arg
);
172 static void cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int take_reference
);
173 static int cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
);
175 static int cluster_read_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t filesize
, int flags
,
176 int (*)(buf_t
, void *), void *callback_arg
);
177 static int cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
178 int flags
, int (*)(buf_t
, void *), void *callback_arg
);
179 static int cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
180 int (*)(buf_t
, void *), void *callback_arg
, int flags
);
182 static int cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
,
183 off_t headOff
, off_t tailOff
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
184 static int cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
,
185 int *write_type
, u_int32_t
*write_length
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
186 static int cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
,
187 int *write_type
, u_int32_t
*write_length
, int (*)(buf_t
, void *), void *callback_arg
, int bflag
);
189 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
);
191 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
);
192 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
);
194 static int cluster_push_now(vnode_t vp
, struct cl_extent
*, off_t EOF
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
196 static int cluster_try_push(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int push_flag
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
198 static void sparse_cluster_switch(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int (*)(buf_t
, void *), void *callback_arg
);
199 static void sparse_cluster_push(void **cmapp
, vnode_t vp
, off_t EOF
, int push_flag
, int io_flags
, int (*)(buf_t
, void *), void *callback_arg
);
200 static void sparse_cluster_add(void **cmapp
, vnode_t vp
, struct cl_extent
*, off_t EOF
, int (*)(buf_t
, void *), void *callback_arg
);
202 static kern_return_t
vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
);
203 static kern_return_t
vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
);
204 static kern_return_t
vfs_drt_control(void **cmapp
, int op_type
);
208 * For throttled IO to check whether
209 * a block is cached by the boot cache
210 * and thus it can avoid delaying the IO.
212 * bootcache_contains_block is initially
213 * NULL. The BootCache will set it while
214 * the cache is active and clear it when
215 * the cache is jettisoned.
217 * Returns 0 if the block is not
218 * contained in the cache, 1 if it is
221 * The function pointer remains valid
222 * after the cache has been evicted even
223 * if bootcache_contains_block has been
226 * See rdar://9974130 The new throttling mechanism breaks the boot cache for throttled IOs
228 int (*bootcache_contains_block
)(dev_t device
, u_int64_t blkno
) = NULL
;
232 * limit the internal I/O size so that we
233 * can represent it in a 32 bit int
235 #define MAX_IO_REQUEST_SIZE (1024 * 1024 * 512)
236 #define MAX_IO_CONTIG_SIZE (MAX_UPL_SIZE * PAGE_SIZE)
238 #define MIN_DIRECT_WRITE_SIZE (4 * PAGE_SIZE)
240 #define WRITE_THROTTLE 6
241 #define WRITE_THROTTLE_SSD 2
242 #define WRITE_BEHIND 1
243 #define WRITE_BEHIND_SSD 1
246 #define PREFETCH_SSD 1
247 uint32_t speculative_prefetch_max
= (MAX_UPL_SIZE
* 3);
248 uint32_t speculative_prefetch_max_iosize
= (512 * 1024); /* maximum I/O size to use for a specluative read-ahead on SSDs*/
251 #define IO_SCALE(vp, base) (vp->v_mount->mnt_ioscale * (base))
252 #define MAX_CLUSTER_SIZE(vp) (cluster_max_io_size(vp->v_mount, CL_WRITE))
253 #define MAX_PREFETCH(vp, size, is_ssd) (size * IO_SCALE(vp, ((is_ssd && !ignore_is_ssd) ? PREFETCH_SSD : PREFETCH)))
255 int ignore_is_ssd
= 0;
256 int speculative_reads_disabled
= 0;
259 * throttle the number of async writes that
260 * can be outstanding on a single vnode
261 * before we issue a synchronous write
263 #define THROTTLE_MAXCNT 0
265 uint32_t throttle_max_iosize
= (128 * 1024);
267 #define THROTTLE_MAX_IOSIZE (throttle_max_iosize)
269 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_max_iosize
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_max_iosize
, 0, "");
275 * allocate lock group attribute and group
277 cl_mtx_grp_attr
= lck_grp_attr_alloc_init();
278 cl_mtx_grp
= lck_grp_alloc_init("cluster I/O", cl_mtx_grp_attr
);
281 * allocate the lock attribute
283 cl_mtx_attr
= lck_attr_alloc_init();
285 cl_transaction_mtxp
= lck_mtx_alloc_init(cl_mtx_grp
, cl_mtx_attr
);
287 if (cl_transaction_mtxp
== NULL
)
288 panic("cluster_init: failed to allocate cl_transaction_mtxp");
293 cluster_max_io_size(mount_t mp
, int type
)
295 uint32_t max_io_size
;
302 segcnt
= mp
->mnt_segreadcnt
;
303 maxcnt
= mp
->mnt_maxreadcnt
;
306 segcnt
= mp
->mnt_segwritecnt
;
307 maxcnt
= mp
->mnt_maxwritecnt
;
310 segcnt
= min(mp
->mnt_segreadcnt
, mp
->mnt_segwritecnt
);
311 maxcnt
= min(mp
->mnt_maxreadcnt
, mp
->mnt_maxwritecnt
);
314 if (segcnt
> MAX_UPL_SIZE
) {
316 * don't allow a size beyond the max UPL size we can create
318 segcnt
= MAX_UPL_SIZE
;
320 max_io_size
= min((segcnt
* PAGE_SIZE
), maxcnt
);
322 if (max_io_size
< (MAX_UPL_TRANSFER
* PAGE_SIZE
)) {
324 * don't allow a size smaller than the old fixed limit
326 max_io_size
= (MAX_UPL_TRANSFER
* PAGE_SIZE
);
329 * make sure the size specified is a multiple of PAGE_SIZE
331 max_io_size
&= ~PAGE_MASK
;
333 return (max_io_size
);
339 #define CLW_ALLOCATE 0x01
340 #define CLW_RETURNLOCKED 0x02
341 #define CLW_IONOCACHE 0x04
342 #define CLW_IOPASSIVE 0x08
345 * if the read ahead context doesn't yet exist,
346 * allocate and initialize it...
347 * the vnode lock serializes multiple callers
348 * during the actual assignment... first one
349 * to grab the lock wins... the other callers
350 * will release the now unnecessary storage
352 * once the context is present, try to grab (but don't block on)
353 * the lock associated with it... if someone
354 * else currently owns it, than the read
355 * will run without read-ahead. this allows
356 * multiple readers to run in parallel and
357 * since there's only 1 read ahead context,
358 * there's no real loss in only allowing 1
359 * reader to have read-ahead enabled.
361 static struct cl_readahead
*
362 cluster_get_rap(vnode_t vp
)
364 struct ubc_info
*ubc
;
365 struct cl_readahead
*rap
;
369 if ((rap
= ubc
->cl_rahead
) == NULL
) {
370 MALLOC_ZONE(rap
, struct cl_readahead
*, sizeof *rap
, M_CLRDAHEAD
, M_WAITOK
);
372 bzero(rap
, sizeof *rap
);
374 lck_mtx_init(&rap
->cl_lockr
, cl_mtx_grp
, cl_mtx_attr
);
378 if (ubc
->cl_rahead
== NULL
)
379 ubc
->cl_rahead
= rap
;
381 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
382 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
383 rap
= ubc
->cl_rahead
;
387 if (lck_mtx_try_lock(&rap
->cl_lockr
) == TRUE
)
390 return ((struct cl_readahead
*)NULL
);
395 * if the write behind context doesn't yet exist,
396 * and CLW_ALLOCATE is specified, allocate and initialize it...
397 * the vnode lock serializes multiple callers
398 * during the actual assignment... first one
399 * to grab the lock wins... the other callers
400 * will release the now unnecessary storage
402 * if CLW_RETURNLOCKED is set, grab (blocking if necessary)
403 * the lock associated with the write behind context before
407 static struct cl_writebehind
*
408 cluster_get_wbp(vnode_t vp
, int flags
)
410 struct ubc_info
*ubc
;
411 struct cl_writebehind
*wbp
;
415 if ((wbp
= ubc
->cl_wbehind
) == NULL
) {
417 if ( !(flags
& CLW_ALLOCATE
))
418 return ((struct cl_writebehind
*)NULL
);
420 MALLOC_ZONE(wbp
, struct cl_writebehind
*, sizeof *wbp
, M_CLWRBEHIND
, M_WAITOK
);
422 bzero(wbp
, sizeof *wbp
);
423 lck_mtx_init(&wbp
->cl_lockw
, cl_mtx_grp
, cl_mtx_attr
);
427 if (ubc
->cl_wbehind
== NULL
)
428 ubc
->cl_wbehind
= wbp
;
430 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
431 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
432 wbp
= ubc
->cl_wbehind
;
436 if (flags
& CLW_RETURNLOCKED
)
437 lck_mtx_lock(&wbp
->cl_lockw
);
444 cluster_syncup(vnode_t vp
, off_t newEOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
446 struct cl_writebehind
*wbp
;
448 if ((wbp
= cluster_get_wbp(vp
, 0)) != NULL
) {
450 if (wbp
->cl_number
) {
451 lck_mtx_lock(&wbp
->cl_lockw
);
453 cluster_try_push(wbp
, vp
, newEOF
, PUSH_ALL
| PUSH_SYNC
, 0, callback
, callback_arg
);
455 lck_mtx_unlock(&wbp
->cl_lockw
);
462 cluster_io_present_in_BC(vnode_t vp
, off_t f_offset
)
466 int (*bootcache_check_fn
)(dev_t device
, u_int64_t blkno
) = bootcache_contains_block
;
468 if (bootcache_check_fn
) {
469 if (VNOP_BLOCKMAP(vp
, f_offset
, PAGE_SIZE
, &blkno
, &io_size
, NULL
, VNODE_READ
, NULL
))
475 if (bootcache_check_fn(vp
->v_mount
->mnt_devvp
->v_rdev
, blkno
))
483 cluster_is_throttled(vnode_t vp
)
485 return (throttle_io_will_be_throttled(-1, vp
->v_mount
));
490 cluster_iostate_wait(struct clios
*iostate
, u_int target
, const char *wait_name
)
493 lck_mtx_lock(&iostate
->io_mtxp
);
495 while ((iostate
->io_issued
- iostate
->io_completed
) > target
) {
497 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
498 iostate
->io_issued
, iostate
->io_completed
, target
, 0, 0);
500 iostate
->io_wanted
= 1;
501 msleep((caddr_t
)&iostate
->io_wanted
, &iostate
->io_mtxp
, PRIBIO
+ 1, wait_name
, NULL
);
503 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
504 iostate
->io_issued
, iostate
->io_completed
, target
, 0, 0);
506 lck_mtx_unlock(&iostate
->io_mtxp
);
511 cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
, vnode_t vp
)
513 int upl_abort_code
= 0;
517 if ((io_flags
& (B_PHYS
| B_CACHE
)) == (B_PHYS
| B_CACHE
))
519 * direct write of any flavor, or a direct read that wasn't aligned
521 ubc_upl_commit_range(upl
, upl_offset
, abort_size
, UPL_COMMIT_FREE_ON_EMPTY
);
523 if (io_flags
& B_PAGEIO
) {
524 if (io_flags
& B_READ
)
529 if (io_flags
& B_CACHE
)
531 * leave pages in the cache unchanged on error
533 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
534 else if (page_out
&& ((error
!= ENXIO
) || vnode_isswap(vp
)))
536 * transient error... leave pages unchanged
538 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
540 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
542 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
544 ubc_upl_abort_range(upl
, upl_offset
, abort_size
, upl_abort_code
);
546 return (upl_abort_code
);
551 cluster_iodone(buf_t bp
, void *callback_arg
)
562 int transaction_size
= 0;
569 struct clios
*iostate
;
570 boolean_t transaction_complete
= FALSE
;
572 cbp_head
= (buf_t
)(bp
->b_trans_head
);
574 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_START
,
575 cbp_head
, bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
577 if (cbp_head
->b_trans_next
|| !(cbp_head
->b_flags
& B_EOT
)) {
578 boolean_t need_wakeup
= FALSE
;
580 lck_mtx_lock_spin(cl_transaction_mtxp
);
582 bp
->b_flags
|= B_TDONE
;
584 if (bp
->b_flags
& B_TWANTED
) {
585 CLR(bp
->b_flags
, B_TWANTED
);
588 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
590 * all I/O requests that are part of this transaction
591 * have to complete before we can process it
593 if ( !(cbp
->b_flags
& B_TDONE
)) {
595 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
596 cbp_head
, cbp
, cbp
->b_bcount
, cbp
->b_flags
, 0);
598 lck_mtx_unlock(cl_transaction_mtxp
);
600 if (need_wakeup
== TRUE
)
605 if (cbp
->b_flags
& B_EOT
)
606 transaction_complete
= TRUE
;
608 lck_mtx_unlock(cl_transaction_mtxp
);
610 if (need_wakeup
== TRUE
)
613 if (transaction_complete
== FALSE
) {
614 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
615 cbp_head
, 0, 0, 0, 0);
625 upl_offset
= cbp
->b_uploffset
;
627 b_flags
= cbp
->b_flags
;
628 real_bp
= cbp
->b_real_bp
;
629 zero_offset
= cbp
->b_validend
;
630 iostate
= (struct clios
*)cbp
->b_iostate
;
633 real_bp
->b_dev
= cbp
->b_dev
;
636 if ((cbp
->b_flags
& B_ERROR
) && error
== 0)
637 error
= cbp
->b_error
;
639 total_resid
+= cbp
->b_resid
;
640 total_size
+= cbp
->b_bcount
;
642 cbp_next
= cbp
->b_trans_next
;
644 if (cbp_next
== NULL
)
646 * compute the overall size of the transaction
647 * in case we created one that has 'holes' in it
648 * 'total_size' represents the amount of I/O we
649 * did, not the span of the transaction w/r to the UPL
651 transaction_size
= cbp
->b_uploffset
+ cbp
->b_bcount
- upl_offset
;
658 if (error
== 0 && total_resid
)
662 int (*cliodone_func
)(buf_t
, void *) = (int (*)(buf_t
, void *))(cbp_head
->b_cliodone
);
664 if (cliodone_func
!= NULL
) {
665 cbp_head
->b_bcount
= transaction_size
;
667 error
= (*cliodone_func
)(cbp_head
, callback_arg
);
671 cluster_zero(upl
, zero_offset
, PAGE_SIZE
- (zero_offset
& PAGE_MASK
), real_bp
);
673 free_io_buf(cbp_head
);
679 * someone has issued multiple I/Os asynchrounsly
680 * and is waiting for them to complete (streaming)
682 lck_mtx_lock_spin(&iostate
->io_mtxp
);
684 if (error
&& iostate
->io_error
== 0)
685 iostate
->io_error
= error
;
687 iostate
->io_completed
+= total_size
;
689 if (iostate
->io_wanted
) {
691 * someone is waiting for the state of
692 * this io stream to change
694 iostate
->io_wanted
= 0;
697 lck_mtx_unlock(&iostate
->io_mtxp
);
700 wakeup((caddr_t
)&iostate
->io_wanted
);
703 if (b_flags
& B_COMMIT_UPL
) {
705 pg_offset
= upl_offset
& PAGE_MASK
;
706 commit_size
= (pg_offset
+ transaction_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
709 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, commit_size
, error
, b_flags
, vp
);
711 upl_flags
= UPL_COMMIT_FREE_ON_EMPTY
;
713 if ((b_flags
& B_PHYS
) && (b_flags
& B_READ
))
714 upl_flags
|= UPL_COMMIT_SET_DIRTY
;
717 upl_flags
|= UPL_COMMIT_INACTIVATE
;
719 ubc_upl_commit_range(upl
, upl_offset
- pg_offset
, commit_size
, upl_flags
);
724 real_bp
->b_flags
|= B_ERROR
;
725 real_bp
->b_error
= error
;
727 real_bp
->b_resid
= total_resid
;
729 buf_biodone(real_bp
);
731 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
732 upl
, upl_offset
- pg_offset
, commit_size
, (error
<< 24) | upl_flags
, 0);
739 cluster_throttle_io_limit(vnode_t vp
, uint32_t *limit
)
741 if (cluster_is_throttled(vp
)) {
742 *limit
= THROTTLE_MAX_IOSIZE
;
750 cluster_zero(upl_t upl
, upl_offset_t upl_offset
, int size
, buf_t bp
)
753 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_START
,
754 upl_offset
, size
, bp
, 0, 0);
756 if (bp
== NULL
|| bp
->b_datap
== 0) {
760 pl
= ubc_upl_pageinfo(upl
);
762 if (upl_device_page(pl
) == TRUE
) {
763 zero_addr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + upl_offset
;
765 bzero_phys_nc(zero_addr
, size
);
772 page_index
= upl_offset
/ PAGE_SIZE
;
773 page_offset
= upl_offset
& PAGE_MASK
;
775 zero_addr
= ((addr64_t
)upl_phys_page(pl
, page_index
) << 12) + page_offset
;
776 zero_cnt
= min(PAGE_SIZE
- page_offset
, size
);
778 bzero_phys(zero_addr
, zero_cnt
);
781 upl_offset
+= zero_cnt
;
785 bzero((caddr_t
)((vm_offset_t
)bp
->b_datap
+ upl_offset
), size
);
787 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_END
,
788 upl_offset
, size
, 0, 0, 0);
793 cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
)
795 cbp_head
->b_validend
= zero_offset
;
796 cbp_tail
->b_flags
|= B_EOT
;
800 cluster_wait_IO(buf_t cbp_head
, int async
)
806 * async callback completion will not normally
807 * generate a wakeup upon I/O completion...
808 * by setting B_TWANTED, we will force a wakeup
809 * to occur as any outstanding I/Os complete...
810 * I/Os already completed will have B_TDONE already
811 * set and we won't cause us to block
812 * note that we're actually waiting for the bp to have
813 * completed the callback function... only then
814 * can we safely take back ownership of the bp
816 lck_mtx_lock_spin(cl_transaction_mtxp
);
818 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
819 cbp
->b_flags
|= B_TWANTED
;
821 lck_mtx_unlock(cl_transaction_mtxp
);
823 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
826 while (!ISSET(cbp
->b_flags
, B_TDONE
)) {
828 lck_mtx_lock_spin(cl_transaction_mtxp
);
830 if (!ISSET(cbp
->b_flags
, B_TDONE
)) {
831 DTRACE_IO1(wait__start
, buf_t
, cbp
);
832 (void) msleep(cbp
, cl_transaction_mtxp
, PDROP
| (PRIBIO
+1), "cluster_wait_IO", NULL
);
833 DTRACE_IO1(wait__done
, buf_t
, cbp
);
835 lck_mtx_unlock(cl_transaction_mtxp
);
843 cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
)
847 boolean_t isswapout
= FALSE
;
850 * cluster_complete_transaction will
851 * only be called if we've issued a complete chain in synchronous mode
852 * or, we've already done a cluster_wait_IO on an incomplete chain
855 for (cbp
= *cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
859 * we've already waited on all of the I/Os in this transaction,
860 * so mark all of the buf_t's in this transaction as B_TDONE
861 * so that cluster_iodone sees the transaction as completed
863 for (cbp
= *cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
864 cbp
->b_flags
|= B_TDONE
;
867 if ((flags
& (CL_ASYNC
| CL_PAGEOUT
)) == CL_PAGEOUT
&& vnode_isswap(cbp
->b_vp
))
870 error
= cluster_iodone(cbp
, callback_arg
);
872 if ( !(flags
& CL_ASYNC
) && error
&& *retval
== 0) {
873 if (((flags
& (CL_PAGEOUT
| CL_KEEPCACHED
)) != CL_PAGEOUT
) || (error
!= ENXIO
))
875 else if (isswapout
== TRUE
)
878 *cbp_head
= (buf_t
)NULL
;
883 cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
884 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*callback
)(buf_t
, void *), void *callback_arg
)
893 buf_t cbp_head
= NULL
;
894 buf_t cbp_tail
= NULL
;
903 int async_throttle
= 0;
905 vm_offset_t upl_end_offset
;
906 boolean_t need_EOT
= FALSE
;
909 * we currently don't support buffers larger than a page
911 if (real_bp
&& non_rounded_size
> PAGE_SIZE
)
912 panic("%s(): Called with real buffer of size %d bytes which "
913 "is greater than the maximum allowed size of "
914 "%d bytes (the system PAGE_SIZE).\n",
915 __FUNCTION__
, non_rounded_size
, PAGE_SIZE
);
920 * we don't want to do any funny rounding of the size for IO requests
921 * coming through the DIRECT or CONTIGUOUS paths... those pages don't
922 * belong to us... we can't extend (nor do we need to) the I/O to fill
925 if (mp
->mnt_devblocksize
> 1 && !(flags
& (CL_DEV_MEMORY
| CL_DIRECT_IO
))) {
927 * round the requested size up so that this I/O ends on a
928 * page boundary in case this is a 'write'... if the filesystem
929 * has blocks allocated to back the page beyond the EOF, we want to
930 * make sure to write out the zero's that are sitting beyond the EOF
931 * so that in case the filesystem doesn't explicitly zero this area
932 * if a hole is created via a lseek/write beyond the current EOF,
933 * it will return zeros when it's read back from the disk. If the
934 * physical allocation doesn't extend for the whole page, we'll
935 * only write/read from the disk up to the end of this allocation
936 * via the extent info returned from the VNOP_BLOCKMAP call.
938 pg_offset
= upl_offset
& PAGE_MASK
;
940 size
= (((non_rounded_size
+ pg_offset
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - pg_offset
;
943 * anyone advertising a blocksize of 1 byte probably
944 * can't deal with us rounding up the request size
945 * AFP is one such filesystem/device
947 size
= non_rounded_size
;
949 upl_end_offset
= upl_offset
+ size
;
951 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_START
, (int)f_offset
, size
, upl_offset
, flags
, 0);
954 * Set the maximum transaction size to the maximum desired number of
958 if (flags
& CL_DEV_MEMORY
)
959 max_trans_count
= 16;
961 if (flags
& CL_READ
) {
963 bmap_flags
= VNODE_READ
;
965 max_iosize
= mp
->mnt_maxreadcnt
;
966 max_vectors
= mp
->mnt_segreadcnt
;
969 bmap_flags
= VNODE_WRITE
;
971 max_iosize
= mp
->mnt_maxwritecnt
;
972 max_vectors
= mp
->mnt_segwritecnt
;
974 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_NONE
, max_iosize
, max_vectors
, mp
->mnt_devblocksize
, 0, 0);
977 * make sure the maximum iosize is a
978 * multiple of the page size
980 max_iosize
&= ~PAGE_MASK
;
983 * Ensure the maximum iosize is sensible.
986 max_iosize
= PAGE_SIZE
;
988 if (flags
& CL_THROTTLE
) {
989 if ( !(flags
& CL_PAGEOUT
) && cluster_is_throttled(vp
)) {
990 if (max_iosize
> THROTTLE_MAX_IOSIZE
)
991 max_iosize
= THROTTLE_MAX_IOSIZE
;
992 async_throttle
= THROTTLE_MAXCNT
;
994 if ( (flags
& CL_DEV_MEMORY
) )
995 async_throttle
= IO_SCALE(vp
, VNODE_ASYNC_THROTTLE
);
998 u_int max_cluster_size
;
1001 max_cluster_size
= MAX_CLUSTER_SIZE(vp
);
1003 if (max_iosize
> max_cluster_size
)
1004 max_cluster
= max_cluster_size
;
1006 max_cluster
= max_iosize
;
1008 if (size
< max_cluster
)
1011 if ((vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
) && !ignore_is_ssd
)
1012 scale
= WRITE_THROTTLE_SSD
;
1014 scale
= WRITE_THROTTLE
;
1016 if (flags
& CL_CLOSE
)
1017 scale
+= MAX_CLUSTERS
;
1019 async_throttle
= min(IO_SCALE(vp
, VNODE_ASYNC_THROTTLE
), ((scale
* max_cluster_size
) / max_cluster
) - 1);
1025 if (flags
& (CL_PAGEIN
| CL_PAGEOUT
))
1026 io_flags
|= B_PAGEIO
;
1027 if (flags
& (CL_IOSTREAMING
))
1028 io_flags
|= B_IOSTREAMING
;
1029 if (flags
& CL_COMMIT
)
1030 io_flags
|= B_COMMIT_UPL
;
1031 if (flags
& CL_DIRECT_IO
)
1033 if (flags
& (CL_PRESERVE
| CL_KEEPCACHED
))
1034 io_flags
|= B_CACHE
;
1035 if (flags
& CL_PASSIVE
)
1036 io_flags
|= B_PASSIVE
;
1037 if (flags
& CL_ENCRYPTED
)
1038 io_flags
|= B_ENCRYPTED_IO
;
1039 if (vp
->v_flag
& VSYSTEM
)
1042 if ((flags
& CL_READ
) && ((upl_offset
+ non_rounded_size
) & PAGE_MASK
) && (!(flags
& CL_NOZERO
))) {
1044 * then we are going to end up
1045 * with a page that we can't complete (the file size wasn't a multiple
1046 * of PAGE_SIZE and we're trying to read to the end of the file
1047 * so we'll go ahead and zero out the portion of the page we can't
1048 * read in from the file
1050 zero_offset
= upl_offset
+ non_rounded_size
;
1055 u_int io_size_wanted
;
1058 if (size
> max_iosize
)
1059 io_size
= max_iosize
;
1063 io_size_wanted
= io_size
;
1064 io_size_tmp
= (size_t)io_size
;
1066 if ((error
= VNOP_BLOCKMAP(vp
, f_offset
, io_size
, &blkno
, &io_size_tmp
, NULL
, bmap_flags
, NULL
)))
1069 if (io_size_tmp
> io_size_wanted
)
1070 io_size
= io_size_wanted
;
1072 io_size
= (u_int
)io_size_tmp
;
1074 if (real_bp
&& (real_bp
->b_blkno
== real_bp
->b_lblkno
))
1075 real_bp
->b_blkno
= blkno
;
1077 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 24)) | DBG_FUNC_NONE
,
1078 (int)f_offset
, (int)(blkno
>>32), (int)blkno
, io_size
, 0);
1082 * vnop_blockmap didn't return an error... however, it did
1083 * return an extent size of 0 which means we can't
1084 * make forward progress on this I/O... a hole in the
1085 * file would be returned as a blkno of -1 with a non-zero io_size
1086 * a real extent is returned with a blkno != -1 and a non-zero io_size
1091 if ( !(flags
& CL_READ
) && blkno
== -1) {
1095 if (upl_get_internal_vectorupl(upl
))
1096 panic("Vector UPLs should not take this code-path\n");
1098 * we're writing into a 'hole'
1100 if (flags
& CL_PAGEOUT
) {
1102 * if we got here via cluster_pageout
1103 * then just error the request and return
1104 * the 'hole' should already have been covered
1110 * we can get here if the cluster code happens to
1111 * pick up a page that was dirtied via mmap vs
1112 * a 'write' and the page targets a 'hole'...
1113 * i.e. the writes to the cluster were sparse
1114 * and the file was being written for the first time
1116 * we can also get here if the filesystem supports
1117 * 'holes' that are less than PAGE_SIZE.... because
1118 * we can't know if the range in the page that covers
1119 * the 'hole' has been dirtied via an mmap or not,
1120 * we have to assume the worst and try to push the
1121 * entire page to storage.
1123 * Try paging out the page individually before
1124 * giving up entirely and dumping it (the pageout
1125 * path will insure that the zero extent accounting
1126 * has been taken care of before we get back into cluster_io)
1128 * go direct to vnode_pageout so that we don't have to
1129 * unbusy the page from the UPL... we used to do this
1130 * so that we could call ubc_sync_range, but that results
1131 * in a potential deadlock if someone else races us to acquire
1132 * that page and wins and in addition needs one of the pages
1133 * we're continuing to hold in the UPL
1135 pageout_flags
= UPL_MSYNC
| UPL_VNODE_PAGER
| UPL_NESTED_PAGEOUT
;
1137 if ( !(flags
& CL_ASYNC
))
1138 pageout_flags
|= UPL_IOSYNC
;
1139 if ( !(flags
& CL_COMMIT
))
1140 pageout_flags
|= UPL_NOCOMMIT
;
1146 * first we have to wait for the the current outstanding I/Os
1147 * to complete... EOT hasn't been set yet on this transaction
1148 * so the pages won't be released just because all of the current
1149 * I/O linked to this transaction has completed...
1151 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1154 * we've got a transcation that
1155 * includes the page we're about to push out through vnode_pageout...
1156 * find the last bp in the list which will be the one that
1157 * includes the head of this page and round it's iosize down
1158 * to a page boundary...
1160 for (last_cbp
= cbp
= cbp_head
; cbp
->b_trans_next
; cbp
= cbp
->b_trans_next
)
1163 cbp
->b_bcount
&= ~PAGE_MASK
;
1165 if (cbp
->b_bcount
== 0) {
1167 * this buf no longer has any I/O associated with it
1171 if (cbp
== cbp_head
) {
1173 * the buf we just freed was the only buf in
1174 * this transaction... so there's no I/O to do
1179 * remove the buf we just freed from
1180 * the transaction list
1182 last_cbp
->b_trans_next
= NULL
;
1183 cbp_tail
= last_cbp
;
1188 * there was more to the current transaction
1189 * than just the page we are pushing out via vnode_pageout...
1190 * mark it as finished and complete it... we've already
1191 * waited for the I/Os to complete above in the call to cluster_wait_IO
1193 cluster_EOT(cbp_head
, cbp_tail
, 0);
1195 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1200 if (vnode_pageout(vp
, upl
, trunc_page(upl_offset
), trunc_page_64(f_offset
), PAGE_SIZE
, pageout_flags
, NULL
) != PAGER_SUCCESS
) {
1203 e_offset
= round_page_64(f_offset
+ 1);
1204 io_size
= e_offset
- f_offset
;
1206 f_offset
+= io_size
;
1207 upl_offset
+= io_size
;
1209 if (size
>= io_size
)
1214 * keep track of how much of the original request
1215 * that we've actually completed... non_rounded_size
1216 * may go negative due to us rounding the request
1217 * to a page size multiple (i.e. size > non_rounded_size)
1219 non_rounded_size
-= io_size
;
1221 if (non_rounded_size
<= 0) {
1223 * we've transferred all of the data in the original
1224 * request, but we were unable to complete the tail
1225 * of the last page because the file didn't have
1226 * an allocation to back that portion... this is ok.
1232 flags
&= ~CL_COMMIT
;
1237 lblkno
= (daddr64_t
)(f_offset
/ PAGE_SIZE_64
);
1239 * we have now figured out how much I/O we can do - this is in 'io_size'
1240 * pg_offset is the starting point in the first page for the I/O
1241 * pg_count is the number of full and partial pages that 'io_size' encompasses
1243 pg_offset
= upl_offset
& PAGE_MASK
;
1245 if (flags
& CL_DEV_MEMORY
) {
1247 * treat physical requests as one 'giant' page
1251 pg_count
= (io_size
+ pg_offset
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1253 if ((flags
& CL_READ
) && blkno
== -1) {
1254 vm_offset_t commit_offset
;
1256 int complete_transaction_now
= 0;
1259 * if we're reading and blkno == -1, then we've got a
1260 * 'hole' in the file that we need to deal with by zeroing
1261 * out the affected area in the upl
1263 if (io_size
>= (u_int
)non_rounded_size
) {
1265 * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
1266 * than 'zero_offset' will be non-zero
1267 * if the 'hole' returned by vnop_blockmap extends all the way to the eof
1268 * (indicated by the io_size finishing off the I/O request for this UPL)
1269 * than we're not going to issue an I/O for the
1270 * last page in this upl... we need to zero both the hole and the tail
1271 * of the page beyond the EOF, since the delayed zero-fill won't kick in
1273 bytes_to_zero
= non_rounded_size
;
1274 if (!(flags
& CL_NOZERO
))
1275 bytes_to_zero
= (((upl_offset
+ io_size
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - upl_offset
;
1279 bytes_to_zero
= io_size
;
1283 cluster_zero(upl
, upl_offset
, bytes_to_zero
, real_bp
);
1289 * if there is a current I/O chain pending
1290 * then the first page of the group we just zero'd
1291 * will be handled by the I/O completion if the zero
1292 * fill started in the middle of the page
1294 commit_offset
= (upl_offset
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1296 pg_resid
= commit_offset
- upl_offset
;
1298 if (bytes_to_zero
>= pg_resid
) {
1300 * the last page of the current I/O
1301 * has been completed...
1302 * compute the number of fully zero'd
1303 * pages that are beyond it
1304 * plus the last page if its partial
1305 * and we have no more I/O to issue...
1306 * otherwise a partial page is left
1307 * to begin the next I/O
1309 if ((int)io_size
>= non_rounded_size
)
1310 pg_count
= (bytes_to_zero
- pg_resid
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1312 pg_count
= (bytes_to_zero
- pg_resid
) / PAGE_SIZE
;
1314 complete_transaction_now
= 1;
1318 * no pending I/O to deal with
1319 * so, commit all of the fully zero'd pages
1320 * plus the last page if its partial
1321 * and we have no more I/O to issue...
1322 * otherwise a partial page is left
1323 * to begin the next I/O
1325 if ((int)io_size
>= non_rounded_size
)
1326 pg_count
= (pg_offset
+ bytes_to_zero
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1328 pg_count
= (pg_offset
+ bytes_to_zero
) / PAGE_SIZE
;
1330 commit_offset
= upl_offset
& ~PAGE_MASK
;
1332 if ( (flags
& CL_COMMIT
) && pg_count
) {
1333 ubc_upl_commit_range(upl
, commit_offset
, pg_count
* PAGE_SIZE
,
1334 UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
);
1336 upl_offset
+= io_size
;
1337 f_offset
+= io_size
;
1341 * keep track of how much of the original request
1342 * that we've actually completed... non_rounded_size
1343 * may go negative due to us rounding the request
1344 * to a page size multiple (i.e. size > non_rounded_size)
1346 non_rounded_size
-= io_size
;
1348 if (non_rounded_size
<= 0) {
1350 * we've transferred all of the data in the original
1351 * request, but we were unable to complete the tail
1352 * of the last page because the file didn't have
1353 * an allocation to back that portion... this is ok.
1357 if (cbp_head
&& (complete_transaction_now
|| size
== 0)) {
1358 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1360 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1362 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1368 if (pg_count
> max_vectors
) {
1369 if (((pg_count
- max_vectors
) * PAGE_SIZE
) > io_size
) {
1370 io_size
= PAGE_SIZE
- pg_offset
;
1373 io_size
-= (pg_count
- max_vectors
) * PAGE_SIZE
;
1374 pg_count
= max_vectors
;
1378 * If the transaction is going to reach the maximum number of
1379 * desired elements, truncate the i/o to the nearest page so
1380 * that the actual i/o is initiated after this buffer is
1381 * created and added to the i/o chain.
1383 * I/O directed to physically contiguous memory
1384 * doesn't have a requirement to make sure we 'fill' a page
1386 if ( !(flags
& CL_DEV_MEMORY
) && trans_count
>= max_trans_count
&&
1387 ((upl_offset
+ io_size
) & PAGE_MASK
)) {
1388 vm_offset_t aligned_ofs
;
1390 aligned_ofs
= (upl_offset
+ io_size
) & ~PAGE_MASK
;
1392 * If the io_size does not actually finish off even a
1393 * single page we have to keep adding buffers to the
1394 * transaction despite having reached the desired limit.
1396 * Eventually we get here with the page being finished
1397 * off (and exceeded) and then we truncate the size of
1398 * this i/o request so that it is page aligned so that
1399 * we can finally issue the i/o on the transaction.
1401 if (aligned_ofs
> upl_offset
) {
1402 io_size
= aligned_ofs
- upl_offset
;
1407 if ( !(mp
->mnt_kern_flag
& MNTK_VIRTUALDEV
))
1409 * if we're not targeting a virtual device i.e. a disk image
1410 * it's safe to dip into the reserve pool since real devices
1411 * can complete this I/O request without requiring additional
1412 * bufs from the alloc_io_buf pool
1415 else if ((flags
& CL_ASYNC
) && !(flags
& CL_PAGEOUT
))
1417 * Throttle the speculative IO
1423 cbp
= alloc_io_buf(vp
, priv
);
1425 if (flags
& CL_PAGEOUT
) {
1428 for (i
= 0; i
< pg_count
; i
++) {
1429 if (buf_invalblkno(vp
, lblkno
+ i
, 0) == EBUSY
)
1430 panic("BUSY bp found in cluster_io");
1433 if (flags
& CL_ASYNC
) {
1434 if (buf_setcallback(cbp
, (void *)cluster_iodone
, callback_arg
))
1435 panic("buf_setcallback failed\n");
1437 cbp
->b_cliodone
= (void *)callback
;
1438 cbp
->b_flags
|= io_flags
;
1439 if (flags
& CL_NOCACHE
)
1440 cbp
->b_attr
.ba_flags
|= BA_NOCACHE
;
1442 cbp
->b_lblkno
= lblkno
;
1443 cbp
->b_blkno
= blkno
;
1444 cbp
->b_bcount
= io_size
;
1446 if (buf_setupl(cbp
, upl
, upl_offset
))
1447 panic("buf_setupl failed\n");
1449 cbp
->b_trans_next
= (buf_t
)NULL
;
1451 if ((cbp
->b_iostate
= (void *)iostate
))
1453 * caller wants to track the state of this
1454 * io... bump the amount issued against this stream
1456 iostate
->io_issued
+= io_size
;
1458 if (flags
& CL_READ
) {
1459 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 26)) | DBG_FUNC_NONE
,
1460 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1463 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 27)) | DBG_FUNC_NONE
,
1464 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1468 cbp_tail
->b_trans_next
= cbp
;
1474 if ( (cbp_head
->b_real_bp
= real_bp
) )
1475 real_bp
= (buf_t
)NULL
;
1477 *(buf_t
*)(&cbp
->b_trans_head
) = cbp_head
;
1481 upl_offset
+= io_size
;
1482 f_offset
+= io_size
;
1485 * keep track of how much of the original request
1486 * that we've actually completed... non_rounded_size
1487 * may go negative due to us rounding the request
1488 * to a page size multiple (i.e. size > non_rounded_size)
1490 non_rounded_size
-= io_size
;
1492 if (non_rounded_size
<= 0) {
1494 * we've transferred all of the data in the original
1495 * request, but we were unable to complete the tail
1496 * of the last page because the file didn't have
1497 * an allocation to back that portion... this is ok.
1503 * we have no more I/O to issue, so go
1504 * finish the final transaction
1507 } else if ( ((flags
& CL_DEV_MEMORY
) || (upl_offset
& PAGE_MASK
) == 0) &&
1508 ((flags
& CL_ASYNC
) || trans_count
> max_trans_count
) ) {
1510 * I/O directed to physically contiguous memory...
1511 * which doesn't have a requirement to make sure we 'fill' a page
1513 * the current I/O we've prepared fully
1514 * completes the last page in this request
1516 * it's either an ASYNC request or
1517 * we've already accumulated more than 8 I/O's into
1518 * this transaction so mark it as complete so that
1519 * it can finish asynchronously or via the cluster_complete_transaction
1520 * below if the request is synchronous
1524 if (need_EOT
== TRUE
)
1525 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1527 if (flags
& CL_THROTTLE
)
1528 (void)vnode_waitforwrites(vp
, async_throttle
, 0, 0, "cluster_io");
1530 if ( !(io_flags
& B_READ
))
1531 vnode_startwrite(vp
);
1533 if (flags
& CL_RAW_ENCRYPTED
) {
1535 * User requested raw encrypted bytes.
1536 * Twiddle the bit in the ba_flags for the buffer
1538 cbp
->b_attr
.ba_flags
|= BA_RAW_ENCRYPTED_IO
;
1541 (void) VNOP_STRATEGY(cbp
);
1543 if (need_EOT
== TRUE
) {
1544 if ( !(flags
& CL_ASYNC
))
1545 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 1);
1559 * first wait until all of the outstanding I/O
1560 * for this partial transaction has completed
1562 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1565 * Rewind the upl offset to the beginning of the
1568 upl_offset
= cbp_head
->b_uploffset
;
1570 for (cbp
= cbp_head
; cbp
;) {
1573 size
+= cbp
->b_bcount
;
1574 io_size
+= cbp
->b_bcount
;
1576 cbp_next
= cbp
->b_trans_next
;
1582 int need_wakeup
= 0;
1585 * update the error condition for this stream
1586 * since we never really issued the io
1587 * just go ahead and adjust it back
1589 lck_mtx_lock_spin(&iostate
->io_mtxp
);
1591 if (iostate
->io_error
== 0)
1592 iostate
->io_error
= error
;
1593 iostate
->io_issued
-= io_size
;
1595 if (iostate
->io_wanted
) {
1597 * someone is waiting for the state of
1598 * this io stream to change
1600 iostate
->io_wanted
= 0;
1603 lck_mtx_unlock(&iostate
->io_mtxp
);
1606 wakeup((caddr_t
)&iostate
->io_wanted
);
1608 if (flags
& CL_COMMIT
) {
1611 pg_offset
= upl_offset
& PAGE_MASK
;
1612 abort_size
= (upl_end_offset
- upl_offset
+ PAGE_MASK
) & ~PAGE_MASK
;
1614 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, abort_size
, error
, io_flags
, vp
);
1616 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 28)) | DBG_FUNC_NONE
,
1617 upl
, upl_offset
- pg_offset
, abort_size
, (error
<< 24) | upl_flags
, 0);
1621 } else if (cbp_head
)
1622 panic("%s(): cbp_head is not NULL.\n", __FUNCTION__
);
1626 * can get here if we either encountered an error
1627 * or we completely zero-filled the request and
1631 real_bp
->b_flags
|= B_ERROR
;
1632 real_bp
->b_error
= error
;
1634 buf_biodone(real_bp
);
1636 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_END
, (int)f_offset
, size
, upl_offset
, retval
, 0);
1641 #define reset_vector_run_state() \
1642 issueVectorUPL = vector_upl_offset = vector_upl_index = vector_upl_iosize = vector_upl_size = 0;
1645 vector_cluster_io(vnode_t vp
, upl_t vector_upl
, vm_offset_t vector_upl_offset
, off_t v_upl_uio_offset
, int vector_upl_iosize
,
1646 int io_flag
, buf_t real_bp
, struct clios
*iostate
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1648 vector_upl_set_pagelist(vector_upl
);
1650 if(io_flag
& CL_READ
) {
1651 if(vector_upl_offset
== 0 && ((vector_upl_iosize
& PAGE_MASK
)==0))
1652 io_flag
&= ~CL_PRESERVE
; /*don't zero fill*/
1654 io_flag
|= CL_PRESERVE
; /*zero fill*/
1656 return (cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, real_bp
, iostate
, callback
, callback_arg
));
1661 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
)
1663 int pages_in_prefetch
;
1665 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_START
,
1666 (int)f_offset
, size
, (int)filesize
, 0, 0);
1668 if (f_offset
>= filesize
) {
1669 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1670 (int)f_offset
, 0, 0, 0, 0);
1673 if ((off_t
)size
> (filesize
- f_offset
))
1674 size
= filesize
- f_offset
;
1675 pages_in_prefetch
= (size
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1677 advisory_read_ext(vp
, filesize
, f_offset
, size
, callback
, callback_arg
, bflag
);
1679 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1680 (int)f_offset
+ size
, pages_in_prefetch
, 0, 1, 0);
1682 return (pages_in_prefetch
);
1688 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
,
1693 int size_of_prefetch
;
1697 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_START
,
1698 (int)extent
->b_addr
, (int)extent
->e_addr
, (int)rap
->cl_lastr
, 0, 0);
1700 if (extent
->b_addr
== rap
->cl_lastr
&& extent
->b_addr
== extent
->e_addr
) {
1701 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1702 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 0, 0);
1705 if (rap
->cl_lastr
== -1 || (extent
->b_addr
!= rap
->cl_lastr
&& extent
->b_addr
!= (rap
->cl_lastr
+ 1))) {
1709 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1710 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 1, 0);
1714 max_prefetch
= MAX_PREFETCH(vp
, cluster_max_io_size(vp
->v_mount
, CL_READ
), (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
));
1716 if ((max_prefetch
/ PAGE_SIZE
) > speculative_prefetch_max
)
1717 max_prefetch
= (speculative_prefetch_max
* PAGE_SIZE
);
1719 if (max_prefetch
<= PAGE_SIZE
) {
1720 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1721 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 6, 0);
1724 if (extent
->e_addr
< rap
->cl_maxra
) {
1725 if ((rap
->cl_maxra
- extent
->e_addr
) > ((max_prefetch
/ PAGE_SIZE
) / 4)) {
1727 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1728 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 2, 0);
1732 r_addr
= max(extent
->e_addr
, rap
->cl_maxra
) + 1;
1733 f_offset
= (off_t
)(r_addr
* PAGE_SIZE_64
);
1735 size_of_prefetch
= 0;
1737 ubc_range_op(vp
, f_offset
, f_offset
+ PAGE_SIZE_64
, UPL_ROP_PRESENT
, &size_of_prefetch
);
1739 if (size_of_prefetch
) {
1740 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1741 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 3, 0);
1744 if (f_offset
< filesize
) {
1745 daddr64_t read_size
;
1747 rap
->cl_ralen
= rap
->cl_ralen
? min(max_prefetch
/ PAGE_SIZE
, rap
->cl_ralen
<< 1) : 1;
1749 read_size
= (extent
->e_addr
+ 1) - extent
->b_addr
;
1751 if (read_size
> rap
->cl_ralen
) {
1752 if (read_size
> max_prefetch
/ PAGE_SIZE
)
1753 rap
->cl_ralen
= max_prefetch
/ PAGE_SIZE
;
1755 rap
->cl_ralen
= read_size
;
1757 size_of_prefetch
= cluster_read_prefetch(vp
, f_offset
, rap
->cl_ralen
* PAGE_SIZE
, filesize
, callback
, callback_arg
, bflag
);
1759 if (size_of_prefetch
)
1760 rap
->cl_maxra
= (r_addr
+ size_of_prefetch
) - 1;
1762 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1763 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 4, 0);
1768 cluster_pageout(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
1769 int size
, off_t filesize
, int flags
)
1771 return cluster_pageout_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
1777 cluster_pageout_ext(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
1778 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1785 local_flags
= CL_PAGEOUT
| CL_THROTTLE
;
1787 if ((flags
& UPL_IOSYNC
) == 0)
1788 local_flags
|= CL_ASYNC
;
1789 if ((flags
& UPL_NOCOMMIT
) == 0)
1790 local_flags
|= CL_COMMIT
;
1791 if ((flags
& UPL_KEEPCACHED
))
1792 local_flags
|= CL_KEEPCACHED
;
1793 if (flags
& UPL_PAGING_ENCRYPTED
)
1794 local_flags
|= CL_ENCRYPTED
;
1797 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 52)) | DBG_FUNC_NONE
,
1798 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
1801 * If they didn't specify any I/O, then we are done...
1802 * we can't issue an abort because we don't know how
1803 * big the upl really is
1808 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
1809 if (local_flags
& CL_COMMIT
)
1810 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
1814 * can't page-in from a negative offset
1815 * or if we're starting beyond the EOF
1816 * or if the file offset isn't page aligned
1817 * or the size requested isn't a multiple of PAGE_SIZE
1819 if (f_offset
< 0 || f_offset
>= filesize
||
1820 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
)) {
1821 if (local_flags
& CL_COMMIT
)
1822 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
1825 max_size
= filesize
- f_offset
;
1827 if (size
< max_size
)
1832 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1834 if (size
> rounded_size
) {
1835 if (local_flags
& CL_COMMIT
)
1836 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
, size
- rounded_size
,
1837 UPL_ABORT_FREE_ON_EMPTY
);
1839 return (cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
1840 local_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
));
1845 cluster_pagein(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
1846 int size
, off_t filesize
, int flags
)
1848 return cluster_pagein_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
1853 cluster_pagein_ext(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
1854 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1860 int local_flags
= 0;
1862 if (upl
== NULL
|| size
< 0)
1863 panic("cluster_pagein: NULL upl passed in");
1865 if ((flags
& UPL_IOSYNC
) == 0)
1866 local_flags
|= CL_ASYNC
;
1867 if ((flags
& UPL_NOCOMMIT
) == 0)
1868 local_flags
|= CL_COMMIT
;
1869 if (flags
& UPL_IOSTREAMING
)
1870 local_flags
|= CL_IOSTREAMING
;
1871 if (flags
& UPL_PAGING_ENCRYPTED
)
1872 local_flags
|= CL_ENCRYPTED
;
1875 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 56)) | DBG_FUNC_NONE
,
1876 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
1879 * can't page-in from a negative offset
1880 * or if we're starting beyond the EOF
1881 * or if the file offset isn't page aligned
1882 * or the size requested isn't a multiple of PAGE_SIZE
1884 if (f_offset
< 0 || f_offset
>= filesize
||
1885 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
) || (upl_offset
& PAGE_MASK
)) {
1886 if (local_flags
& CL_COMMIT
)
1887 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
1890 max_size
= filesize
- f_offset
;
1892 if (size
< max_size
)
1897 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1899 if (size
> rounded_size
&& (local_flags
& CL_COMMIT
))
1900 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
,
1901 size
- rounded_size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
1903 retval
= cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
1904 local_flags
| CL_READ
| CL_PAGEIN
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
1911 cluster_bp(buf_t bp
)
1913 return cluster_bp_ext(bp
, NULL
, NULL
);
1918 cluster_bp_ext(buf_t bp
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1923 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 19)) | DBG_FUNC_START
,
1924 bp
, (int)bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
1926 if (bp
->b_flags
& B_READ
)
1927 flags
= CL_ASYNC
| CL_READ
;
1930 if (bp
->b_flags
& B_PASSIVE
)
1931 flags
|= CL_PASSIVE
;
1933 f_offset
= ubc_blktooff(bp
->b_vp
, bp
->b_lblkno
);
1935 return (cluster_io(bp
->b_vp
, bp
->b_upl
, 0, f_offset
, bp
->b_bcount
, flags
, bp
, (struct clios
*)NULL
, callback
, callback_arg
));
1941 cluster_write(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
, int xflags
)
1943 return cluster_write_ext(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, xflags
, NULL
, NULL
);
1948 cluster_write_ext(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
,
1949 int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1951 user_ssize_t cur_resid
;
1956 int write_type
= IO_COPY
;
1957 u_int32_t write_length
;
1961 if (flags
& IO_PASSIVE
)
1966 if (vp
->v_flag
& VNOCACHE_DATA
){
1967 flags
|= IO_NOCACHE
;
1968 bflag
|= CL_NOCACHE
;
1973 * this call is being made to zero-fill some range in the file
1975 retval
= cluster_write_copy(vp
, NULL
, (u_int32_t
)0, oldEOF
, newEOF
, headOff
, tailOff
, flags
, callback
, callback_arg
);
1980 * do a write through the cache if one of the following is true....
1981 * NOCACHE is not true or NODIRECT is true
1982 * the uio request doesn't target USERSPACE
1983 * otherwise, find out if we want the direct or contig variant for
1984 * the first vector in the uio request
1986 if ( ((flags
& (IO_NOCACHE
| IO_NODIRECT
)) == IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) )
1987 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
1989 if ( (flags
& (IO_TAILZEROFILL
| IO_HEADZEROFILL
)) && write_type
== IO_DIRECT
)
1991 * must go through the cached variant in this case
1993 write_type
= IO_COPY
;
1995 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< newEOF
&& retval
== 0) {
1997 switch (write_type
) {
2001 * make sure the uio_resid isn't too big...
2002 * internally, we want to handle all of the I/O in
2003 * chunk sizes that fit in a 32 bit int
2005 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
)) {
2007 * we're going to have to call cluster_write_copy
2010 * only want the last call to cluster_write_copy to
2011 * have the IO_TAILZEROFILL flag set and only the
2012 * first call should have IO_HEADZEROFILL
2014 zflags
= flags
& ~IO_TAILZEROFILL
;
2015 flags
&= ~IO_HEADZEROFILL
;
2017 write_length
= MAX_IO_REQUEST_SIZE
;
2020 * last call to cluster_write_copy
2024 write_length
= (u_int32_t
)cur_resid
;
2026 retval
= cluster_write_copy(vp
, uio
, write_length
, oldEOF
, newEOF
, headOff
, tailOff
, zflags
, callback
, callback_arg
);
2030 zflags
= flags
& ~(IO_TAILZEROFILL
| IO_HEADZEROFILL
);
2032 if (flags
& IO_HEADZEROFILL
) {
2034 * only do this once per request
2036 flags
&= ~IO_HEADZEROFILL
;
2038 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, uio
->uio_offset
,
2039 headOff
, (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
2043 retval
= cluster_write_contig(vp
, uio
, newEOF
, &write_type
, &write_length
, callback
, callback_arg
, bflag
);
2045 if (retval
== 0 && (flags
& IO_TAILZEROFILL
) && uio_resid(uio
) == 0) {
2047 * we're done with the data from the user specified buffer(s)
2048 * and we've been requested to zero fill at the tail
2049 * treat this as an IO_HEADZEROFILL which doesn't require a uio
2050 * by rearranging the args and passing in IO_HEADZEROFILL
2052 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, tailOff
, uio
->uio_offset
,
2053 (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
2059 * cluster_write_direct is never called with IO_TAILZEROFILL || IO_HEADZEROFILL
2061 retval
= cluster_write_direct(vp
, uio
, oldEOF
, newEOF
, &write_type
, &write_length
, flags
, callback
, callback_arg
);
2065 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
2069 * in case we end up calling cluster_write_copy (from cluster_write_direct)
2070 * multiple times to service a multi-vector request that is not aligned properly
2071 * we need to update the oldEOF so that we
2072 * don't zero-fill the head of a page if we've successfully written
2073 * data to that area... 'cluster_write_copy' will zero-fill the head of a
2074 * page that is beyond the oldEOF if the write is unaligned... we only
2075 * want that to happen for the very first page of the cluster_write,
2076 * NOT the first page of each vector making up a multi-vector write.
2078 if (uio
->uio_offset
> oldEOF
)
2079 oldEOF
= uio
->uio_offset
;
2086 cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
2087 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2090 upl_page_info_t
*pl
;
2091 vm_offset_t upl_offset
;
2092 vm_offset_t vector_upl_offset
= 0;
2093 u_int32_t io_req_size
;
2094 u_int32_t offset_in_file
;
2095 u_int32_t offset_in_iovbase
;
2098 upl_size_t upl_size
, vector_upl_size
= 0;
2099 vm_size_t upl_needed_size
;
2100 mach_msg_type_number_t pages_in_pl
;
2103 mach_msg_type_number_t i
;
2104 int force_data_sync
;
2107 struct clios iostate
;
2108 user_addr_t iov_base
;
2109 u_int32_t mem_alignment_mask
;
2110 u_int32_t devblocksize
;
2111 u_int32_t max_io_size
;
2112 u_int32_t max_upl_size
;
2113 u_int32_t max_vector_size
;
2114 boolean_t io_throttled
= FALSE
;
2116 u_int32_t vector_upl_iosize
= 0;
2117 int issueVectorUPL
= 0,useVectorUPL
= (uio
->uio_iovcnt
> 1);
2118 off_t v_upl_uio_offset
= 0;
2119 int vector_upl_index
=0;
2120 upl_t vector_upl
= NULL
;
2124 * When we enter this routine, we know
2125 * -- the resid will not exceed iov_len
2127 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_START
,
2128 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
2130 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
2132 io_flag
= CL_ASYNC
| CL_PRESERVE
| CL_COMMIT
| CL_THROTTLE
| CL_DIRECT_IO
;
2134 if (flags
& IO_PASSIVE
)
2135 io_flag
|= CL_PASSIVE
;
2137 if (flags
& IO_NOCACHE
)
2138 io_flag
|= CL_NOCACHE
;
2140 iostate
.io_completed
= 0;
2141 iostate
.io_issued
= 0;
2142 iostate
.io_error
= 0;
2143 iostate
.io_wanted
= 0;
2145 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
2147 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
2148 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
2150 if (devblocksize
== 1) {
2152 * the AFP client advertises a devblocksize of 1
2153 * however, its BLOCKMAP routine maps to physical
2154 * blocks that are PAGE_SIZE in size...
2155 * therefore we can't ask for I/Os that aren't page aligned
2156 * or aren't multiples of PAGE_SIZE in size
2157 * by setting devblocksize to PAGE_SIZE, we re-instate
2158 * the old behavior we had before the mem_alignment_mask
2159 * changes went in...
2161 devblocksize
= PAGE_SIZE
;
2165 io_req_size
= *write_length
;
2166 iov_base
= uio_curriovbase(uio
);
2168 offset_in_file
= (u_int32_t
)uio
->uio_offset
& PAGE_MASK
;
2169 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
2171 if (offset_in_file
|| offset_in_iovbase
) {
2173 * one of the 2 important offsets is misaligned
2174 * so fire an I/O through the cache for this entire vector
2176 goto wait_for_dwrites
;
2178 if (iov_base
& (devblocksize
- 1)) {
2180 * the offset in memory must be on a device block boundary
2181 * so that we can guarantee that we can generate an
2182 * I/O that ends on a page boundary in cluster_io
2184 goto wait_for_dwrites
;
2187 while (io_req_size
>= PAGE_SIZE
&& uio
->uio_offset
< newEOF
&& retval
== 0) {
2190 if ( (throttle_type
= cluster_is_throttled(vp
)) ) {
2192 * we're in the throttle window, at the very least
2193 * we want to limit the size of the I/O we're about
2196 if ( (flags
& IO_RETURN_ON_THROTTLE
) && throttle_type
== THROTTLE_NOW
) {
2198 * we're in the throttle window and at least 1 I/O
2199 * has already been issued by a throttleable thread
2200 * in this window, so return with EAGAIN to indicate
2201 * to the FS issuing the cluster_write call that it
2202 * should now throttle after dropping any locks
2204 throttle_info_update_by_mount(vp
->v_mount
);
2206 io_throttled
= TRUE
;
2207 goto wait_for_dwrites
;
2209 max_vector_size
= THROTTLE_MAX_IOSIZE
;
2210 max_io_size
= THROTTLE_MAX_IOSIZE
;
2212 max_vector_size
= MAX_VECTOR_UPL_SIZE
;
2213 max_io_size
= max_upl_size
;
2217 cluster_syncup(vp
, newEOF
, callback
, callback_arg
);
2220 io_size
= io_req_size
& ~PAGE_MASK
;
2221 iov_base
= uio_curriovbase(uio
);
2223 if (io_size
> max_io_size
)
2224 io_size
= max_io_size
;
2226 if(useVectorUPL
&& (iov_base
& PAGE_MASK
)) {
2228 * We have an iov_base that's not page-aligned.
2229 * Issue all I/O's that have been collected within
2230 * this Vectored UPL.
2232 if(vector_upl_index
) {
2233 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2234 reset_vector_run_state();
2238 * After this point, if we are using the Vector UPL path and the base is
2239 * not page-aligned then the UPL with that base will be the first in the vector UPL.
2243 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
2244 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
2246 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_START
,
2247 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
2249 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
2251 upl_size
= upl_needed_size
;
2252 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
2253 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
2255 kret
= vm_map_get_upl(current_map(),
2256 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2264 if (kret
!= KERN_SUCCESS
) {
2265 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2268 * failed to get pagelist
2270 * we may have already spun some portion of this request
2271 * off as async requests... we need to wait for the I/O
2272 * to complete before returning
2274 goto wait_for_dwrites
;
2276 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2277 pages_in_pl
= upl_size
/ PAGE_SIZE
;
2279 for (i
= 0; i
< pages_in_pl
; i
++) {
2280 if (!upl_valid_page(pl
, i
))
2283 if (i
== pages_in_pl
)
2287 * didn't get all the pages back that we
2288 * needed... release this upl and try again
2290 ubc_upl_abort(upl
, 0);
2292 if (force_data_sync
>= 3) {
2293 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2294 i
, pages_in_pl
, upl_size
, kret
, 0);
2296 * for some reason, we couldn't acquire a hold on all
2297 * the pages needed in the user's address space
2299 * we may have already spun some portion of this request
2300 * off as async requests... we need to wait for the I/O
2301 * to complete before returning
2303 goto wait_for_dwrites
;
2307 * Consider the possibility that upl_size wasn't satisfied.
2309 if (upl_size
< upl_needed_size
) {
2310 if (upl_size
&& upl_offset
== 0)
2315 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2316 (int)upl_offset
, upl_size
, (int)iov_base
, io_size
, 0);
2319 ubc_upl_abort(upl
, 0);
2321 * we may have already spun some portion of this request
2322 * off as async requests... we need to wait for the I/O
2323 * to complete before returning
2325 goto wait_for_dwrites
;
2329 vm_offset_t end_off
= ((iov_base
+ io_size
) & PAGE_MASK
);
2333 * After this point, if we are using a vector UPL, then
2334 * either all the UPL elements end on a page boundary OR
2335 * this UPL is the last element because it does not end
2336 * on a page boundary.
2341 * Now look for pages already in the cache
2342 * and throw them away.
2343 * uio->uio_offset is page aligned within the file
2344 * io_size is a multiple of PAGE_SIZE
2346 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ io_size
, UPL_ROP_DUMP
, NULL
);
2349 * we want push out these writes asynchronously so that we can overlap
2350 * the preparation of the next I/O
2351 * if there are already too many outstanding writes
2352 * wait until some complete before issuing the next
2354 if (iostate
.io_issued
> iostate
.io_completed
)
2355 cluster_iostate_wait(&iostate
, max_upl_size
* IO_SCALE(vp
, 2), "cluster_write_direct");
2357 if (iostate
.io_error
) {
2359 * one of the earlier writes we issued ran into a hard error
2360 * don't issue any more writes, cleanup the UPL
2361 * that was just created but not used, then
2362 * go wait for all writes that are part of this stream
2363 * to complete before returning the error to the caller
2365 ubc_upl_abort(upl
, 0);
2367 goto wait_for_dwrites
;
2370 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_START
,
2371 (int)upl_offset
, (int)uio
->uio_offset
, io_size
, io_flag
, 0);
2374 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
2375 io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2378 if(!vector_upl_index
) {
2379 vector_upl
= vector_upl_create(upl_offset
);
2380 v_upl_uio_offset
= uio
->uio_offset
;
2381 vector_upl_offset
= upl_offset
;
2384 vector_upl_set_subupl(vector_upl
,upl
,upl_size
);
2385 vector_upl_set_iostate(vector_upl
, upl
, vector_upl_size
, upl_size
);
2387 vector_upl_iosize
+= io_size
;
2388 vector_upl_size
+= upl_size
;
2390 if(issueVectorUPL
|| vector_upl_index
== MAX_VECTOR_UPL_ELEMENTS
|| vector_upl_size
>= max_vector_size
) {
2391 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2392 reset_vector_run_state();
2397 * update the uio structure to
2398 * reflect the I/O that we just issued
2400 uio_update(uio
, (user_size_t
)io_size
);
2403 * in case we end up calling through to cluster_write_copy to finish
2404 * the tail of this request, we need to update the oldEOF so that we
2405 * don't zero-fill the head of a page if we've successfully written
2406 * data to that area... 'cluster_write_copy' will zero-fill the head of a
2407 * page that is beyond the oldEOF if the write is unaligned... we only
2408 * want that to happen for the very first page of the cluster_write,
2409 * NOT the first page of each vector making up a multi-vector write.
2411 if (uio
->uio_offset
> oldEOF
)
2412 oldEOF
= uio
->uio_offset
;
2414 io_req_size
-= io_size
;
2416 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_END
,
2417 (int)upl_offset
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
2421 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0) {
2423 retval
= cluster_io_type(uio
, write_type
, write_length
, MIN_DIRECT_WRITE_SIZE
);
2425 if (retval
== 0 && *write_type
== IO_DIRECT
) {
2427 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_NONE
,
2428 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
2436 if (retval
== 0 && iostate
.io_error
== 0 && useVectorUPL
&& vector_upl_index
) {
2437 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2438 reset_vector_run_state();
2441 if (iostate
.io_issued
> iostate
.io_completed
) {
2443 * make sure all async writes issued as part of this stream
2444 * have completed before we return
2446 cluster_iostate_wait(&iostate
, 0, "cluster_write_direct");
2448 if (iostate
.io_error
)
2449 retval
= iostate
.io_error
;
2451 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
2453 if (io_throttled
== TRUE
&& retval
== 0)
2456 if (io_req_size
&& retval
== 0) {
2458 * we couldn't handle the tail of this request in DIRECT mode
2459 * so fire it through the copy path
2461 * note that flags will never have IO_HEADZEROFILL or IO_TAILZEROFILL set
2462 * so we can just pass 0 in for the headOff and tailOff
2464 if (uio
->uio_offset
> oldEOF
)
2465 oldEOF
= uio
->uio_offset
;
2467 retval
= cluster_write_copy(vp
, uio
, io_req_size
, oldEOF
, newEOF
, (off_t
)0, (off_t
)0, flags
, callback
, callback_arg
);
2469 *write_type
= IO_UNKNOWN
;
2471 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
2472 (int)uio
->uio_offset
, io_req_size
, retval
, 4, 0);
2479 cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
2480 int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
)
2482 upl_page_info_t
*pl
;
2483 addr64_t src_paddr
= 0;
2484 upl_t upl
[MAX_VECTS
];
2485 vm_offset_t upl_offset
;
2486 u_int32_t tail_size
= 0;
2489 upl_size_t upl_size
;
2490 vm_size_t upl_needed_size
;
2491 mach_msg_type_number_t pages_in_pl
;
2494 struct clios iostate
;
2499 user_addr_t iov_base
;
2500 u_int32_t devblocksize
;
2501 u_int32_t mem_alignment_mask
;
2504 * When we enter this routine, we know
2505 * -- the io_req_size will not exceed iov_len
2506 * -- the target address is physically contiguous
2508 cluster_syncup(vp
, newEOF
, callback
, callback_arg
);
2510 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
2511 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
2513 iostate
.io_completed
= 0;
2514 iostate
.io_issued
= 0;
2515 iostate
.io_error
= 0;
2516 iostate
.io_wanted
= 0;
2518 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
2521 io_size
= *write_length
;
2523 iov_base
= uio_curriovbase(uio
);
2525 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
2526 upl_needed_size
= upl_offset
+ io_size
;
2529 upl_size
= upl_needed_size
;
2530 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
2531 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
2533 kret
= vm_map_get_upl(current_map(),
2534 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2535 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, 0);
2537 if (kret
!= KERN_SUCCESS
) {
2539 * failed to get pagelist
2542 goto wait_for_cwrites
;
2547 * Consider the possibility that upl_size wasn't satisfied.
2549 if (upl_size
< upl_needed_size
) {
2551 * This is a failure in the physical memory case.
2554 goto wait_for_cwrites
;
2556 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
2558 src_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)upl_offset
;
2560 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
2561 u_int32_t head_size
;
2563 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
2565 if (head_size
> io_size
)
2566 head_size
= io_size
;
2568 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, head_size
, 0, callback
, callback_arg
);
2571 goto wait_for_cwrites
;
2573 upl_offset
+= head_size
;
2574 src_paddr
+= head_size
;
2575 io_size
-= head_size
;
2577 iov_base
+= head_size
;
2579 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
2581 * request doesn't set up on a memory boundary
2582 * the underlying DMA engine can handle...
2583 * return an error instead of going through
2584 * the slow copy path since the intent of this
2585 * path is direct I/O from device memory
2588 goto wait_for_cwrites
;
2591 tail_size
= io_size
& (devblocksize
- 1);
2592 io_size
-= tail_size
;
2594 while (io_size
&& error
== 0) {
2596 if (io_size
> MAX_IO_CONTIG_SIZE
)
2597 xsize
= MAX_IO_CONTIG_SIZE
;
2601 * request asynchronously so that we can overlap
2602 * the preparation of the next I/O... we'll do
2603 * the commit after all the I/O has completed
2604 * since its all issued against the same UPL
2605 * if there are already too many outstanding writes
2606 * wait until some have completed before issuing the next
2608 if (iostate
.io_issued
> iostate
.io_completed
)
2609 cluster_iostate_wait(&iostate
, MAX_IO_CONTIG_SIZE
* IO_SCALE(vp
, 2), "cluster_write_contig");
2611 if (iostate
.io_error
) {
2613 * one of the earlier writes we issued ran into a hard error
2614 * don't issue any more writes...
2615 * go wait for all writes that are part of this stream
2616 * to complete before returning the error to the caller
2618 goto wait_for_cwrites
;
2621 * issue an asynchronous write to cluster_io
2623 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
,
2624 xsize
, CL_DEV_MEMORY
| CL_ASYNC
| bflag
, (buf_t
)NULL
, (struct clios
*)&iostate
, callback
, callback_arg
);
2628 * The cluster_io write completed successfully,
2629 * update the uio structure
2631 uio_update(uio
, (user_size_t
)xsize
);
2633 upl_offset
+= xsize
;
2638 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
) {
2640 error
= cluster_io_type(uio
, write_type
, write_length
, 0);
2642 if (error
== 0 && *write_type
== IO_CONTIG
) {
2647 *write_type
= IO_UNKNOWN
;
2651 * make sure all async writes that are part of this stream
2652 * have completed before we proceed
2654 if (iostate
.io_issued
> iostate
.io_completed
)
2655 cluster_iostate_wait(&iostate
, 0, "cluster_write_contig");
2657 if (iostate
.io_error
)
2658 error
= iostate
.io_error
;
2660 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
2662 if (error
== 0 && tail_size
)
2663 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, tail_size
, 0, callback
, callback_arg
);
2665 for (n
= 0; n
< num_upl
; n
++)
2667 * just release our hold on each physically contiguous
2668 * region without changing any state
2670 ubc_upl_abort(upl
[n
], 0);
2677 * need to avoid a race between an msync of a range of pages dirtied via mmap
2678 * vs a filesystem such as HFS deciding to write a 'hole' to disk via cluster_write's
2679 * zerofill mechanism before it has seen the VNOP_PAGEOUTs for the pages being msync'd
2681 * we should never force-zero-fill pages that are already valid in the cache...
2682 * the entire page contains valid data (either from disk, zero-filled or dirtied
2683 * via an mmap) so we can only do damage by trying to zero-fill
2687 cluster_zero_range(upl_t upl
, upl_page_info_t
*pl
, int flags
, int io_offset
, off_t zero_off
, off_t upl_f_offset
, int bytes_to_zero
)
2690 boolean_t need_cluster_zero
= TRUE
;
2692 if ((flags
& (IO_NOZEROVALID
| IO_NOZERODIRTY
))) {
2694 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off
& PAGE_MASK_64
));
2695 zero_pg_index
= (int)((zero_off
- upl_f_offset
) / PAGE_SIZE_64
);
2697 if (upl_valid_page(pl
, zero_pg_index
)) {
2699 * never force zero valid pages - dirty or clean
2700 * we'll leave these in the UPL for cluster_write_copy to deal with
2702 need_cluster_zero
= FALSE
;
2705 if (need_cluster_zero
== TRUE
)
2706 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2708 return (bytes_to_zero
);
2713 cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
, off_t headOff
,
2714 off_t tailOff
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2716 upl_page_info_t
*pl
;
2718 vm_offset_t upl_offset
= 0;
2731 long long total_size
;
2734 long long zero_cnt1
;
2736 off_t write_off
= 0;
2738 boolean_t first_pass
= FALSE
;
2739 struct cl_extent cl
;
2740 struct cl_writebehind
*wbp
;
2742 u_int max_cluster_pgcount
;
2746 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
2747 (int)uio
->uio_offset
, io_req_size
, (int)oldEOF
, (int)newEOF
, 0);
2749 io_resid
= io_req_size
;
2751 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
2752 0, 0, (int)oldEOF
, (int)newEOF
, 0);
2756 if (flags
& IO_PASSIVE
)
2760 if (flags
& IO_NOCACHE
)
2761 bflag
|= CL_NOCACHE
;
2768 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
2769 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
2771 if (flags
& IO_HEADZEROFILL
) {
2773 * some filesystems (HFS is one) don't support unallocated holes within a file...
2774 * so we zero fill the intervening space between the old EOF and the offset
2775 * where the next chunk of real data begins.... ftruncate will also use this
2776 * routine to zero fill to the new EOF when growing a file... in this case, the
2777 * uio structure will not be provided
2780 if (headOff
< uio
->uio_offset
) {
2781 zero_cnt
= uio
->uio_offset
- headOff
;
2784 } else if (headOff
< newEOF
) {
2785 zero_cnt
= newEOF
- headOff
;
2789 if (uio
&& uio
->uio_offset
> oldEOF
) {
2790 zero_off
= uio
->uio_offset
& ~PAGE_MASK_64
;
2792 if (zero_off
>= oldEOF
) {
2793 zero_cnt
= uio
->uio_offset
- zero_off
;
2795 flags
|= IO_HEADZEROFILL
;
2799 if (flags
& IO_TAILZEROFILL
) {
2801 zero_off1
= uio
->uio_offset
+ io_req_size
;
2803 if (zero_off1
< tailOff
)
2804 zero_cnt1
= tailOff
- zero_off1
;
2807 if (uio
&& newEOF
> oldEOF
) {
2808 zero_off1
= uio
->uio_offset
+ io_req_size
;
2810 if (zero_off1
== newEOF
&& (zero_off1
& PAGE_MASK_64
)) {
2811 zero_cnt1
= PAGE_SIZE_64
- (zero_off1
& PAGE_MASK_64
);
2813 flags
|= IO_TAILZEROFILL
;
2817 if (zero_cnt
== 0 && uio
== (struct uio
*) 0) {
2818 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
2819 retval
, 0, 0, 0, 0);
2823 write_off
= uio
->uio_offset
;
2824 write_cnt
= uio_resid(uio
);
2826 * delay updating the sequential write info
2827 * in the control block until we've obtained
2832 while ((total_size
= (io_resid
+ zero_cnt
+ zero_cnt1
)) && retval
== 0) {
2834 * for this iteration of the loop, figure out where our starting point is
2837 start_offset
= (int)(zero_off
& PAGE_MASK_64
);
2838 upl_f_offset
= zero_off
- start_offset
;
2839 } else if (io_resid
) {
2840 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2841 upl_f_offset
= uio
->uio_offset
- start_offset
;
2843 start_offset
= (int)(zero_off1
& PAGE_MASK_64
);
2844 upl_f_offset
= zero_off1
- start_offset
;
2846 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 46)) | DBG_FUNC_NONE
,
2847 (int)zero_off
, (int)zero_cnt
, (int)zero_off1
, (int)zero_cnt1
, 0);
2849 if (total_size
> max_io_size
)
2850 total_size
= max_io_size
;
2852 cl
.b_addr
= (daddr64_t
)(upl_f_offset
/ PAGE_SIZE_64
);
2854 if (uio
&& ((flags
& (IO_SYNC
| IO_HEADZEROFILL
| IO_TAILZEROFILL
)) == 0)) {
2856 * assumption... total_size <= io_resid
2857 * because IO_HEADZEROFILL and IO_TAILZEROFILL not set
2859 if ((start_offset
+ total_size
) > max_io_size
)
2860 total_size
= max_io_size
- start_offset
;
2861 xfer_resid
= total_size
;
2863 retval
= cluster_copy_ubc_data_internal(vp
, uio
, &xfer_resid
, 1, 1);
2868 io_resid
-= (total_size
- xfer_resid
);
2869 total_size
= xfer_resid
;
2870 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2871 upl_f_offset
= uio
->uio_offset
- start_offset
;
2873 if (total_size
== 0) {
2876 * the write did not finish on a page boundary
2877 * which will leave upl_f_offset pointing to the
2878 * beginning of the last page written instead of
2879 * the page beyond it... bump it in this case
2880 * so that the cluster code records the last page
2883 upl_f_offset
+= PAGE_SIZE_64
;
2891 * compute the size of the upl needed to encompass
2892 * the requested write... limit each call to cluster_io
2893 * to the maximum UPL size... cluster_io will clip if
2894 * this exceeds the maximum io_size for the device,
2895 * make sure to account for
2896 * a starting offset that's not page aligned
2898 upl_size
= (start_offset
+ total_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2900 if (upl_size
> max_io_size
)
2901 upl_size
= max_io_size
;
2903 pages_in_upl
= upl_size
/ PAGE_SIZE
;
2904 io_size
= upl_size
- start_offset
;
2906 if ((long long)io_size
> total_size
)
2907 io_size
= total_size
;
2909 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, io_size
, total_size
, 0, 0);
2913 * Gather the pages from the buffer cache.
2914 * The UPL_WILL_MODIFY flag lets the UPL subsystem know
2915 * that we intend to modify these pages.
2917 kret
= ubc_create_upl(vp
,
2922 UPL_SET_LITE
| (( uio
!=NULL
&& (uio
->uio_flags
& UIO_FLAGS_IS_COMPRESSED_FILE
)) ? 0 : UPL_WILL_MODIFY
));
2923 if (kret
!= KERN_SUCCESS
)
2924 panic("cluster_write_copy: failed to get pagelist");
2926 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
,
2927 upl
, (int)upl_f_offset
, start_offset
, 0, 0);
2929 if (start_offset
&& upl_f_offset
< oldEOF
&& !upl_valid_page(pl
, 0)) {
2933 * we're starting in the middle of the first page of the upl
2934 * and the page isn't currently valid, so we're going to have
2935 * to read it in first... this is a synchronous operation
2937 read_size
= PAGE_SIZE
;
2939 if ((upl_f_offset
+ read_size
) > oldEOF
)
2940 read_size
= oldEOF
- upl_f_offset
;
2942 retval
= cluster_io(vp
, upl
, 0, upl_f_offset
, read_size
,
2943 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
2946 * we had an error during the read which causes us to abort
2947 * the current cluster_write request... before we do, we need
2948 * to release the rest of the pages in the upl without modifying
2949 * there state and mark the failed page in error
2951 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
2953 if (upl_size
> PAGE_SIZE
)
2954 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2956 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2957 upl
, 0, 0, retval
, 0);
2961 if ((start_offset
== 0 || upl_size
> PAGE_SIZE
) && ((start_offset
+ io_size
) & PAGE_MASK
)) {
2963 * the last offset we're writing to in this upl does not end on a page
2964 * boundary... if it's not beyond the old EOF, then we'll also need to
2965 * pre-read this page in if it isn't already valid
2967 upl_offset
= upl_size
- PAGE_SIZE
;
2969 if ((upl_f_offset
+ start_offset
+ io_size
) < oldEOF
&&
2970 !upl_valid_page(pl
, upl_offset
/ PAGE_SIZE
)) {
2973 read_size
= PAGE_SIZE
;
2975 if ((off_t
)(upl_f_offset
+ upl_offset
+ read_size
) > oldEOF
)
2976 read_size
= oldEOF
- (upl_f_offset
+ upl_offset
);
2978 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, read_size
,
2979 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
2982 * we had an error during the read which causes us to abort
2983 * the current cluster_write request... before we do, we
2984 * need to release the rest of the pages in the upl without
2985 * modifying there state and mark the failed page in error
2987 ubc_upl_abort_range(upl
, upl_offset
, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
2989 if (upl_size
> PAGE_SIZE
)
2990 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2992 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2993 upl
, 0, 0, retval
, 0);
2998 xfer_resid
= io_size
;
2999 io_offset
= start_offset
;
3001 while (zero_cnt
&& xfer_resid
) {
3003 if (zero_cnt
< (long long)xfer_resid
)
3004 bytes_to_zero
= zero_cnt
;
3006 bytes_to_zero
= xfer_resid
;
3008 bytes_to_zero
= cluster_zero_range(upl
, pl
, flags
, io_offset
, zero_off
, upl_f_offset
, bytes_to_zero
);
3010 xfer_resid
-= bytes_to_zero
;
3011 zero_cnt
-= bytes_to_zero
;
3012 zero_off
+= bytes_to_zero
;
3013 io_offset
+= bytes_to_zero
;
3015 if (xfer_resid
&& io_resid
) {
3016 u_int32_t io_requested
;
3018 bytes_to_move
= min(io_resid
, xfer_resid
);
3019 io_requested
= bytes_to_move
;
3021 retval
= cluster_copy_upl_data(uio
, upl
, io_offset
, (int *)&io_requested
);
3024 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3026 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
3027 upl
, 0, 0, retval
, 0);
3029 io_resid
-= bytes_to_move
;
3030 xfer_resid
-= bytes_to_move
;
3031 io_offset
+= bytes_to_move
;
3034 while (xfer_resid
&& zero_cnt1
&& retval
== 0) {
3036 if (zero_cnt1
< (long long)xfer_resid
)
3037 bytes_to_zero
= zero_cnt1
;
3039 bytes_to_zero
= xfer_resid
;
3041 bytes_to_zero
= cluster_zero_range(upl
, pl
, flags
, io_offset
, zero_off1
, upl_f_offset
, bytes_to_zero
);
3043 xfer_resid
-= bytes_to_zero
;
3044 zero_cnt1
-= bytes_to_zero
;
3045 zero_off1
+= bytes_to_zero
;
3046 io_offset
+= bytes_to_zero
;
3050 int ret_cluster_try_push
;
3052 io_size
+= start_offset
;
3054 if ((upl_f_offset
+ io_size
) >= newEOF
&& (u_int
)io_size
< upl_size
) {
3056 * if we're extending the file with this write
3057 * we'll zero fill the rest of the page so that
3058 * if the file gets extended again in such a way as to leave a
3059 * hole starting at this EOF, we'll have zero's in the correct spot
3061 cluster_zero(upl
, io_size
, upl_size
- io_size
, NULL
);
3064 * release the upl now if we hold one since...
3065 * 1) pages in it may be present in the sparse cluster map
3066 * and may span 2 separate buckets there... if they do and
3067 * we happen to have to flush a bucket to make room and it intersects
3068 * this upl, a deadlock may result on page BUSY
3069 * 2) we're delaying the I/O... from this point forward we're just updating
3070 * the cluster state... no need to hold the pages, so commit them
3071 * 3) IO_SYNC is set...
3072 * because we had to ask for a UPL that provides currenty non-present pages, the
3073 * UPL has been automatically set to clear the dirty flags (both software and hardware)
3074 * upon committing it... this is not the behavior we want since it's possible for
3075 * pages currently present as part of a mapped file to be dirtied while the I/O is in flight.
3076 * we'll pick these pages back up later with the correct behavior specified.
3077 * 4) we don't want to hold pages busy in a UPL and then block on the cluster lock... if a flush
3078 * of this vnode is in progress, we will deadlock if the pages being flushed intersect the pages
3079 * we hold since the flushing context is holding the cluster lock.
3081 ubc_upl_commit_range(upl
, 0, upl_size
,
3082 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
3085 * calculate the last logical block number
3086 * that this delayed I/O encompassed
3088 cl
.e_addr
= (daddr64_t
)((upl_f_offset
+ (off_t
)upl_size
) / PAGE_SIZE_64
);
3090 if (flags
& IO_SYNC
) {
3092 * if the IO_SYNC flag is set than we need to
3093 * bypass any clusters and immediately issue
3099 * take the lock to protect our accesses
3100 * of the writebehind and sparse cluster state
3102 wbp
= cluster_get_wbp(vp
, CLW_ALLOCATE
| CLW_RETURNLOCKED
);
3104 if (wbp
->cl_scmap
) {
3106 if ( !(flags
& IO_NOCACHE
)) {
3108 * we've fallen into the sparse
3109 * cluster method of delaying dirty pages
3111 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, newEOF
, callback
, callback_arg
);
3113 lck_mtx_unlock(&wbp
->cl_lockw
);
3118 * must have done cached writes that fell into
3119 * the sparse cluster mechanism... we've switched
3120 * to uncached writes on the file, so go ahead
3121 * and push whatever's in the sparse map
3122 * and switch back to normal clustering
3126 sparse_cluster_push(&(wbp
->cl_scmap
), vp
, newEOF
, PUSH_ALL
, 0, callback
, callback_arg
);
3128 * no clusters of either type present at this point
3129 * so just go directly to start_new_cluster since
3130 * we know we need to delay this I/O since we've
3131 * already released the pages back into the cache
3132 * to avoid the deadlock with sparse_cluster_push
3134 goto start_new_cluster
;
3137 if (write_off
== wbp
->cl_last_write
)
3138 wbp
->cl_seq_written
+= write_cnt
;
3140 wbp
->cl_seq_written
= write_cnt
;
3142 wbp
->cl_last_write
= write_off
+ write_cnt
;
3146 if (wbp
->cl_number
== 0)
3148 * no clusters currently present
3150 goto start_new_cluster
;
3152 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
3154 * check each cluster that we currently hold
3155 * try to merge some or all of this write into
3156 * one or more of the existing clusters... if
3157 * any portion of the write remains, start a
3160 if (cl
.b_addr
>= wbp
->cl_clusters
[cl_index
].b_addr
) {
3162 * the current write starts at or after the current cluster
3164 if (cl
.e_addr
<= (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
3166 * we have a write that fits entirely
3167 * within the existing cluster limits
3169 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
)
3171 * update our idea of where the cluster ends
3173 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
3176 if (cl
.b_addr
< (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
3178 * we have a write that starts in the middle of the current cluster
3179 * but extends beyond the cluster's limit... we know this because
3180 * of the previous checks
3181 * we'll extend the current cluster to the max
3182 * and update the b_addr for the current write to reflect that
3183 * the head of it was absorbed into this cluster...
3184 * note that we'll always have a leftover tail in this case since
3185 * full absorbtion would have occurred in the clause above
3187 wbp
->cl_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
;
3189 cl
.b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
;
3192 * we come here for the case where the current write starts
3193 * beyond the limit of the existing cluster or we have a leftover
3194 * tail after a partial absorbtion
3196 * in either case, we'll check the remaining clusters before
3197 * starting a new one
3201 * the current write starts in front of the cluster we're currently considering
3203 if ((wbp
->cl_clusters
[cl_index
].e_addr
- cl
.b_addr
) <= max_cluster_pgcount
) {
3205 * we can just merge the new request into
3206 * this cluster and leave it in the cache
3207 * since the resulting cluster is still
3208 * less than the maximum allowable size
3210 wbp
->cl_clusters
[cl_index
].b_addr
= cl
.b_addr
;
3212 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
) {
3214 * the current write completely
3215 * envelops the existing cluster and since
3216 * each write is limited to at most max_cluster_pgcount pages
3217 * we can just use the start and last blocknos of the write
3218 * to generate the cluster limits
3220 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
3226 * if we were to combine this write with the current cluster
3227 * we would exceed the cluster size limit.... so,
3228 * let's see if there's any overlap of the new I/O with
3229 * the cluster we're currently considering... in fact, we'll
3230 * stretch the cluster out to it's full limit and see if we
3231 * get an intersection with the current write
3234 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
) {
3236 * the current write extends into the proposed cluster
3237 * clip the length of the current write after first combining it's
3238 * tail with the newly shaped cluster
3240 wbp
->cl_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
;
3242 cl
.e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
;
3245 * if we get here, there was no way to merge
3246 * any portion of this write with this cluster
3247 * or we could only merge part of it which
3248 * will leave a tail...
3249 * we'll check the remaining clusters before starting a new one
3253 if (cl_index
< wbp
->cl_number
)
3255 * we found an existing cluster(s) that we
3256 * could entirely merge this I/O into
3260 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) &&
3261 wbp
->cl_number
== MAX_CLUSTERS
&&
3262 wbp
->cl_seq_written
>= (MAX_CLUSTERS
* (max_cluster_pgcount
* PAGE_SIZE
))) {
3265 if (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
)
3266 n
= WRITE_BEHIND_SSD
;
3271 cluster_try_push(wbp
, vp
, newEOF
, 0, 0, callback
, callback_arg
);
3273 if (wbp
->cl_number
< MAX_CLUSTERS
) {
3275 * we didn't find an existing cluster to
3276 * merge into, but there's room to start
3279 goto start_new_cluster
;
3282 * no exisitng cluster to merge with and no
3283 * room to start a new one... we'll try
3284 * pushing one of the existing ones... if none of
3285 * them are able to be pushed, we'll switch
3286 * to the sparse cluster mechanism
3287 * cluster_try_push updates cl_number to the
3288 * number of remaining clusters... and
3289 * returns the number of currently unused clusters
3291 ret_cluster_try_push
= 0;
3294 * if writes are not deferred, call cluster push immediately
3296 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
)) {
3298 ret_cluster_try_push
= cluster_try_push(wbp
, vp
, newEOF
, (flags
& IO_NOCACHE
) ? 0 : PUSH_DELAY
, 0, callback
, callback_arg
);
3302 * execute following regardless of writes being deferred or not
3304 if (ret_cluster_try_push
== 0) {
3306 * no more room in the normal cluster mechanism
3307 * so let's switch to the more expansive but expensive
3308 * sparse mechanism....
3310 sparse_cluster_switch(wbp
, vp
, newEOF
, callback
, callback_arg
);
3311 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, newEOF
, callback
, callback_arg
);
3313 lck_mtx_unlock(&wbp
->cl_lockw
);
3318 wbp
->cl_clusters
[wbp
->cl_number
].b_addr
= cl
.b_addr
;
3319 wbp
->cl_clusters
[wbp
->cl_number
].e_addr
= cl
.e_addr
;
3321 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
= 0;
3323 if (flags
& IO_NOCACHE
)
3324 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IONOCACHE
;
3326 if (bflag
& CL_PASSIVE
)
3327 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IOPASSIVE
;
3331 lck_mtx_unlock(&wbp
->cl_lockw
);
3336 * we don't hold the lock at this point
3338 * we've already dropped the current upl, so pick it back up with COPYOUT_FROM set
3339 * so that we correctly deal with a change in state of the hardware modify bit...
3340 * we do this via cluster_push_now... by passing along the IO_SYNC flag, we force
3341 * cluster_push_now to wait until all the I/Os have completed... cluster_push_now is also
3342 * responsible for generating the correct sized I/O(s)
3344 retval
= cluster_push_now(vp
, &cl
, newEOF
, flags
, callback
, callback_arg
);
3347 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
, retval
, 0, io_resid
, 0, 0);
3355 cluster_read(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
)
3357 return cluster_read_ext(vp
, uio
, filesize
, xflags
, NULL
, NULL
);
3362 cluster_read_ext(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
3366 user_ssize_t cur_resid
;
3368 u_int32_t read_length
= 0;
3369 int read_type
= IO_COPY
;
3373 if (vp
->v_flag
& VNOCACHE_DATA
)
3374 flags
|= IO_NOCACHE
;
3375 if ((vp
->v_flag
& VRAOFF
) || speculative_reads_disabled
)
3379 * If we're doing an encrypted IO, then first check to see
3380 * if the IO requested was page aligned. If not, then bail
3383 if (flags
& IO_ENCRYPTED
) {
3384 if (read_length
& PAGE_MASK
) {
3391 * do a read through the cache if one of the following is true....
3392 * NOCACHE is not true
3393 * the uio request doesn't target USERSPACE
3394 * Alternatively, if IO_ENCRYPTED is set, then we want to bypass the cache as well.
3395 * Reading encrypted data from a CP filesystem should never result in the data touching
3398 * otherwise, find out if we want the direct or contig variant for
3399 * the first vector in the uio request
3401 if (((flags
& IO_NOCACHE
) || (flags
& IO_ENCRYPTED
)) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
)) {
3403 boolean_t check_io_type
= TRUE
;
3406 if (check_io_type
) {
3407 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3411 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< filesize
&& retval
== 0) {
3413 switch (read_type
) {
3417 * make sure the uio_resid isn't too big...
3418 * internally, we want to handle all of the I/O in
3419 * chunk sizes that fit in a 32 bit int
3421 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
))
3422 io_size
= MAX_IO_REQUEST_SIZE
;
3424 io_size
= (u_int32_t
)cur_resid
;
3426 retval
= cluster_read_copy(vp
, uio
, io_size
, filesize
, flags
, callback
, callback_arg
);
3430 retval
= cluster_read_direct(vp
, uio
, filesize
, &read_type
, &read_length
, flags
, callback
, callback_arg
);
3434 retval
= cluster_read_contig(vp
, uio
, filesize
, &read_type
, &read_length
, callback
, callback_arg
, flags
);
3438 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3448 cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int take_reference
)
3451 int abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
3453 if ((range
= last_pg
- start_pg
)) {
3455 abort_flags
|= UPL_ABORT_REFERENCE
;
3457 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, range
* PAGE_SIZE
, abort_flags
);
3463 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
)
3465 upl_page_info_t
*pl
;
3467 vm_offset_t upl_offset
;
3476 off_t last_ioread_offset
;
3477 off_t last_request_offset
;
3481 u_int32_t size_of_prefetch
;
3484 u_int32_t max_rd_size
;
3485 u_int32_t max_io_size
;
3486 u_int32_t max_prefetch
;
3487 u_int rd_ahead_enabled
= 1;
3488 u_int prefetch_enabled
= 1;
3489 struct cl_readahead
* rap
;
3490 struct clios iostate
;
3491 struct cl_extent extent
;
3493 int take_reference
= 1;
3494 int policy
= IOPOL_DEFAULT
;
3495 boolean_t iolock_inited
= FALSE
;
3497 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_START
,
3498 (int)uio
->uio_offset
, io_req_size
, (int)filesize
, flags
, 0);
3500 if (flags
& IO_ENCRYPTED
) {
3501 panic ("encrypted blocks will hit UBC!");
3504 policy
= throttle_get_io_policy(NULL
);
3506 if (policy
== THROTTLE_LEVEL_TIER3
|| policy
== THROTTLE_LEVEL_TIER2
|| (flags
& IO_NOCACHE
))
3509 if (flags
& IO_PASSIVE
)
3514 if (flags
& IO_NOCACHE
)
3515 bflag
|= CL_NOCACHE
;
3517 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
3518 max_prefetch
= MAX_PREFETCH(vp
, max_io_size
, (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
));
3519 max_rd_size
= max_prefetch
;
3521 last_request_offset
= uio
->uio_offset
+ io_req_size
;
3523 if (last_request_offset
> filesize
)
3524 last_request_offset
= filesize
;
3526 if ((flags
& (IO_RAOFF
|IO_NOCACHE
)) || ((last_request_offset
& ~PAGE_MASK_64
) == (uio
->uio_offset
& ~PAGE_MASK_64
))) {
3527 rd_ahead_enabled
= 0;
3530 if (cluster_is_throttled(vp
)) {
3532 * we're in the throttle window, at the very least
3533 * we want to limit the size of the I/O we're about
3536 rd_ahead_enabled
= 0;
3537 prefetch_enabled
= 0;
3539 max_rd_size
= THROTTLE_MAX_IOSIZE
;
3541 if ((rap
= cluster_get_rap(vp
)) == NULL
)
3542 rd_ahead_enabled
= 0;
3544 extent
.b_addr
= uio
->uio_offset
/ PAGE_SIZE_64
;
3545 extent
.e_addr
= (last_request_offset
- 1) / PAGE_SIZE_64
;
3548 if (rap
!= NULL
&& rap
->cl_ralen
&& (rap
->cl_lastr
== extent
.b_addr
|| (rap
->cl_lastr
+ 1) == extent
.b_addr
)) {
3550 * determine if we already have a read-ahead in the pipe courtesy of the
3551 * last read systemcall that was issued...
3552 * if so, pick up it's extent to determine where we should start
3553 * with respect to any read-ahead that might be necessary to
3554 * garner all the data needed to complete this read systemcall
3556 last_ioread_offset
= (rap
->cl_maxra
* PAGE_SIZE_64
) + PAGE_SIZE_64
;
3558 if (last_ioread_offset
< uio
->uio_offset
)
3559 last_ioread_offset
= (off_t
)0;
3560 else if (last_ioread_offset
> last_request_offset
)
3561 last_ioread_offset
= last_request_offset
;
3563 last_ioread_offset
= (off_t
)0;
3565 while (io_req_size
&& uio
->uio_offset
< filesize
&& retval
== 0) {
3567 max_size
= filesize
- uio
->uio_offset
;
3569 if ((off_t
)(io_req_size
) < max_size
)
3570 io_size
= io_req_size
;
3574 if (!(flags
& IO_NOCACHE
)) {
3578 u_int32_t io_requested
;
3581 * if we keep finding the pages we need already in the cache, then
3582 * don't bother to call cluster_read_prefetch since it costs CPU cycles
3583 * to determine that we have all the pages we need... once we miss in
3584 * the cache and have issued an I/O, than we'll assume that we're likely
3585 * to continue to miss in the cache and it's to our advantage to try and prefetch
3587 if (last_request_offset
&& last_ioread_offset
&& (size_of_prefetch
= (last_request_offset
- last_ioread_offset
))) {
3588 if ((last_ioread_offset
- uio
->uio_offset
) <= max_rd_size
&& prefetch_enabled
) {
3590 * we've already issued I/O for this request and
3591 * there's still work to do and
3592 * our prefetch stream is running dry, so issue a
3593 * pre-fetch I/O... the I/O latency will overlap
3594 * with the copying of the data
3596 if (size_of_prefetch
> max_rd_size
)
3597 size_of_prefetch
= max_rd_size
;
3599 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
3601 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
3603 if (last_ioread_offset
> last_request_offset
)
3604 last_ioread_offset
= last_request_offset
;
3608 * limit the size of the copy we're about to do so that
3609 * we can notice that our I/O pipe is running dry and
3610 * get the next I/O issued before it does go dry
3612 if (last_ioread_offset
&& io_size
> (max_io_size
/ 4))
3613 io_resid
= (max_io_size
/ 4);
3617 io_requested
= io_resid
;
3619 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_resid
, 0, take_reference
);
3621 xsize
= io_requested
- io_resid
;
3624 io_req_size
-= xsize
;
3626 if (retval
|| io_resid
)
3628 * if we run into a real error or
3629 * a page that is not in the cache
3630 * we need to leave streaming mode
3634 if (rd_ahead_enabled
&& (io_size
== 0 || last_ioread_offset
== last_request_offset
)) {
3636 * we're already finished the I/O for this read request
3637 * let's see if we should do a read-ahead
3639 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
3646 if (extent
.e_addr
< rap
->cl_lastr
)
3648 rap
->cl_lastr
= extent
.e_addr
;
3653 * recompute max_size since cluster_copy_ubc_data_internal
3654 * may have advanced uio->uio_offset
3656 max_size
= filesize
- uio
->uio_offset
;
3659 iostate
.io_completed
= 0;
3660 iostate
.io_issued
= 0;
3661 iostate
.io_error
= 0;
3662 iostate
.io_wanted
= 0;
3664 if ( (flags
& IO_RETURN_ON_THROTTLE
) ) {
3665 if (cluster_is_throttled(vp
) == THROTTLE_NOW
) {
3666 if ( !cluster_io_present_in_BC(vp
, uio
->uio_offset
)) {
3668 * we're in the throttle window and at least 1 I/O
3669 * has already been issued by a throttleable thread
3670 * in this window, so return with EAGAIN to indicate
3671 * to the FS issuing the cluster_read call that it
3672 * should now throttle after dropping any locks
3674 throttle_info_update_by_mount(vp
->v_mount
);
3683 * compute the size of the upl needed to encompass
3684 * the requested read... limit each call to cluster_io
3685 * to the maximum UPL size... cluster_io will clip if
3686 * this exceeds the maximum io_size for the device,
3687 * make sure to account for
3688 * a starting offset that's not page aligned
3690 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3691 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
3693 if (io_size
> max_rd_size
)
3694 io_size
= max_rd_size
;
3696 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
3698 if (flags
& IO_NOCACHE
) {
3699 if (upl_size
> max_io_size
)
3700 upl_size
= max_io_size
;
3702 if (upl_size
> max_io_size
/ 4)
3703 upl_size
= max_io_size
/ 4;
3705 pages_in_upl
= upl_size
/ PAGE_SIZE
;
3707 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_START
,
3708 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3710 kret
= ubc_create_upl(vp
,
3715 UPL_FILE_IO
| UPL_SET_LITE
);
3716 if (kret
!= KERN_SUCCESS
)
3717 panic("cluster_read_copy: failed to get pagelist");
3719 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_END
,
3720 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3723 * scan from the beginning of the upl looking for the first
3724 * non-valid page.... this will become the first page in
3725 * the request we're going to make to 'cluster_io'... if all
3726 * of the pages are valid, we won't call through to 'cluster_io'
3728 for (start_pg
= 0; start_pg
< pages_in_upl
; start_pg
++) {
3729 if (!upl_valid_page(pl
, start_pg
))
3734 * scan from the starting invalid page looking for a valid
3735 * page before the end of the upl is reached, if we
3736 * find one, then it will be the last page of the request to
3739 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
3740 if (upl_valid_page(pl
, last_pg
))
3744 if (start_pg
< last_pg
) {
3746 * we found a range of 'invalid' pages that must be filled
3747 * if the last page in this range is the last page of the file
3748 * we may have to clip the size of it to keep from reading past
3749 * the end of the last physical block associated with the file
3751 if (iolock_inited
== FALSE
) {
3752 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
3754 iolock_inited
= TRUE
;
3756 upl_offset
= start_pg
* PAGE_SIZE
;
3757 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
3759 if ((off_t
)(upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
3760 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
3763 * issue an asynchronous read to cluster_io
3766 error
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
,
3767 io_size
, CL_READ
| CL_ASYNC
| bflag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
3770 if (extent
.e_addr
< rap
->cl_maxra
) {
3772 * we've just issued a read for a block that should have been
3773 * in the cache courtesy of the read-ahead engine... something
3774 * has gone wrong with the pipeline, so reset the read-ahead
3775 * logic which will cause us to restart from scratch
3783 * if the read completed successfully, or there was no I/O request
3784 * issued, than copy the data into user land via 'cluster_upl_copy_data'
3785 * we'll first add on any 'valid'
3786 * pages that were present in the upl when we acquired it.
3790 for (uio_last
= last_pg
; uio_last
< pages_in_upl
; uio_last
++) {
3791 if (!upl_valid_page(pl
, uio_last
))
3794 if (uio_last
< pages_in_upl
) {
3796 * there were some invalid pages beyond the valid pages
3797 * that we didn't issue an I/O for, just release them
3798 * unchanged now, so that any prefetch/readahed can
3801 ubc_upl_abort_range(upl
, uio_last
* PAGE_SIZE
,
3802 (pages_in_upl
- uio_last
) * PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
3806 * compute size to transfer this round, if io_req_size is
3807 * still non-zero after this attempt, we'll loop around and
3808 * set up for another I/O.
3810 val_size
= (uio_last
* PAGE_SIZE
) - start_offset
;
3812 if (val_size
> max_size
)
3813 val_size
= max_size
;
3815 if (val_size
> io_req_size
)
3816 val_size
= io_req_size
;
3818 if ((uio
->uio_offset
+ val_size
) > last_ioread_offset
)
3819 last_ioread_offset
= uio
->uio_offset
+ val_size
;
3821 if ((size_of_prefetch
= (last_request_offset
- last_ioread_offset
)) && prefetch_enabled
) {
3823 if ((last_ioread_offset
- (uio
->uio_offset
+ val_size
)) <= upl_size
) {
3825 * if there's still I/O left to do for this request, and...
3826 * we're not in hard throttle mode, and...
3827 * we're close to using up the previous prefetch, then issue a
3828 * new pre-fetch I/O... the I/O latency will overlap
3829 * with the copying of the data
3831 if (size_of_prefetch
> max_rd_size
)
3832 size_of_prefetch
= max_rd_size
;
3834 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
3836 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
3838 if (last_ioread_offset
> last_request_offset
)
3839 last_ioread_offset
= last_request_offset
;
3842 } else if ((uio
->uio_offset
+ val_size
) == last_request_offset
) {
3844 * this transfer will finish this request, so...
3845 * let's try to read ahead if we're in
3846 * a sequential access pattern and we haven't
3847 * explicitly disabled it
3849 if (rd_ahead_enabled
)
3850 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
3853 if (extent
.e_addr
< rap
->cl_lastr
)
3855 rap
->cl_lastr
= extent
.e_addr
;
3858 if (iostate
.io_issued
> iostate
.io_completed
)
3859 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
3861 if (iostate
.io_error
)
3862 error
= iostate
.io_error
;
3864 u_int32_t io_requested
;
3866 io_requested
= val_size
;
3868 retval
= cluster_copy_upl_data(uio
, upl
, start_offset
, (int *)&io_requested
);
3870 io_req_size
-= (val_size
- io_requested
);
3873 if (iostate
.io_issued
> iostate
.io_completed
)
3874 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
3876 if (start_pg
< last_pg
) {
3878 * compute the range of pages that we actually issued an I/O for
3879 * and either commit them as valid if the I/O succeeded
3880 * or abort them if the I/O failed or we're not supposed to
3881 * keep them in the cache
3883 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
3885 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
, upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
3887 if (error
|| (flags
& IO_NOCACHE
))
3888 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
3889 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3891 int commit_flags
= UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
;
3894 commit_flags
|= UPL_COMMIT_INACTIVATE
;
3896 commit_flags
|= UPL_COMMIT_SPECULATE
;
3898 ubc_upl_commit_range(upl
, start_pg
* PAGE_SIZE
, io_size
, commit_flags
);
3900 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
3902 if ((last_pg
- start_pg
) < pages_in_upl
) {
3904 * the set of pages that we issued an I/O for did not encompass
3905 * the entire upl... so just release these without modifying
3909 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3912 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
3913 upl
, -1, pages_in_upl
- (last_pg
- start_pg
), 0, 0);
3916 * handle any valid pages at the beginning of
3917 * the upl... release these appropriately
3919 cluster_read_upl_release(upl
, 0, start_pg
, take_reference
);
3922 * handle any valid pages immediately after the
3923 * pages we issued I/O for... ... release these appropriately
3925 cluster_read_upl_release(upl
, last_pg
, uio_last
, take_reference
);
3927 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, upl
, -1, -1, 0, 0);
3934 if (cluster_is_throttled(vp
)) {
3936 * we're in the throttle window, at the very least
3937 * we want to limit the size of the I/O we're about
3940 rd_ahead_enabled
= 0;
3941 prefetch_enabled
= 0;
3942 max_rd_size
= THROTTLE_MAX_IOSIZE
;
3944 if (max_rd_size
== THROTTLE_MAX_IOSIZE
) {
3946 * coming out of throttled state
3948 if (policy
!= THROTTLE_LEVEL_TIER3
&& policy
!= THROTTLE_LEVEL_TIER2
) {
3950 rd_ahead_enabled
= 1;
3951 prefetch_enabled
= 1;
3953 max_rd_size
= max_prefetch
;
3954 last_ioread_offset
= 0;
3959 if (iolock_inited
== TRUE
) {
3960 if (iostate
.io_issued
> iostate
.io_completed
) {
3962 * cluster_io returned an error after it
3963 * had already issued some I/O. we need
3964 * to wait for that I/O to complete before
3965 * we can destroy the iostate mutex...
3966 * 'retval' already contains the early error
3967 * so no need to pick it up from iostate.io_error
3969 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
3971 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
3974 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
3975 (int)uio
->uio_offset
, io_req_size
, rap
->cl_lastr
, retval
, 0);
3977 lck_mtx_unlock(&rap
->cl_lockr
);
3979 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
3980 (int)uio
->uio_offset
, io_req_size
, 0, retval
, 0);
3988 cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
3989 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
3992 upl_page_info_t
*pl
;
3994 vm_offset_t upl_offset
, vector_upl_offset
= 0;
3995 upl_size_t upl_size
, vector_upl_size
= 0;
3996 vm_size_t upl_needed_size
;
3997 unsigned int pages_in_pl
;
4001 int force_data_sync
;
4003 int no_zero_fill
= 0;
4006 struct clios iostate
;
4007 user_addr_t iov_base
;
4008 u_int32_t io_req_size
;
4009 u_int32_t offset_in_file
;
4010 u_int32_t offset_in_iovbase
;
4014 u_int32_t devblocksize
;
4015 u_int32_t mem_alignment_mask
;
4016 u_int32_t max_upl_size
;
4017 u_int32_t max_rd_size
;
4018 u_int32_t max_rd_ahead
;
4019 u_int32_t max_vector_size
;
4020 boolean_t strict_uncached_IO
= FALSE
;
4021 boolean_t io_throttled
= FALSE
;
4023 u_int32_t vector_upl_iosize
= 0;
4024 int issueVectorUPL
= 0,useVectorUPL
= (uio
->uio_iovcnt
> 1);
4025 off_t v_upl_uio_offset
= 0;
4026 int vector_upl_index
=0;
4027 upl_t vector_upl
= NULL
;
4029 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_START
,
4030 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
4032 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
4034 max_rd_size
= max_upl_size
;
4035 max_rd_ahead
= max_rd_size
* IO_SCALE(vp
, 2);
4037 io_flag
= CL_COMMIT
| CL_READ
| CL_ASYNC
| CL_NOZERO
| CL_DIRECT_IO
;
4039 if (flags
& IO_PASSIVE
)
4040 io_flag
|= CL_PASSIVE
;
4042 if (flags
& IO_ENCRYPTED
) {
4043 io_flag
|= CL_RAW_ENCRYPTED
;
4046 if (flags
& IO_NOCACHE
) {
4047 io_flag
|= CL_NOCACHE
;
4050 iostate
.io_completed
= 0;
4051 iostate
.io_issued
= 0;
4052 iostate
.io_error
= 0;
4053 iostate
.io_wanted
= 0;
4055 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
4057 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
4058 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
4060 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
4061 (int)devblocksize
, (int)mem_alignment_mask
, 0, 0, 0);
4063 if (devblocksize
== 1) {
4065 * the AFP client advertises a devblocksize of 1
4066 * however, its BLOCKMAP routine maps to physical
4067 * blocks that are PAGE_SIZE in size...
4068 * therefore we can't ask for I/Os that aren't page aligned
4069 * or aren't multiples of PAGE_SIZE in size
4070 * by setting devblocksize to PAGE_SIZE, we re-instate
4071 * the old behavior we had before the mem_alignment_mask
4072 * changes went in...
4074 devblocksize
= PAGE_SIZE
;
4077 strict_uncached_IO
= ubc_strict_uncached_IO(vp
);
4080 io_req_size
= *read_length
;
4081 iov_base
= uio_curriovbase(uio
);
4083 max_io_size
= filesize
- uio
->uio_offset
;
4085 if ((off_t
)io_req_size
> max_io_size
)
4086 io_req_size
= max_io_size
;
4088 offset_in_file
= (u_int32_t
)uio
->uio_offset
& (devblocksize
- 1);
4089 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
4091 if (offset_in_file
|| offset_in_iovbase
) {
4093 * one of the 2 important offsets is misaligned
4094 * so fire an I/O through the cache for this entire vector
4098 if (iov_base
& (devblocksize
- 1)) {
4100 * the offset in memory must be on a device block boundary
4101 * so that we can guarantee that we can generate an
4102 * I/O that ends on a page boundary in cluster_io
4108 * The user must request IO in aligned chunks. If the
4109 * offset into the file is bad, or the userland pointer
4110 * is non-aligned, then we cannot service the encrypted IO request.
4112 if ((flags
& IO_ENCRYPTED
) && (misaligned
)) {
4117 * When we get to this point, we know...
4118 * -- the offset into the file is on a devblocksize boundary
4121 while (io_req_size
&& retval
== 0) {
4124 if (cluster_is_throttled(vp
)) {
4126 * we're in the throttle window, at the very least
4127 * we want to limit the size of the I/O we're about
4130 max_rd_size
= THROTTLE_MAX_IOSIZE
;
4131 max_rd_ahead
= THROTTLE_MAX_IOSIZE
- 1;
4132 max_vector_size
= THROTTLE_MAX_IOSIZE
;
4134 max_rd_size
= max_upl_size
;
4135 max_rd_ahead
= max_rd_size
* IO_SCALE(vp
, 2);
4136 max_vector_size
= MAX_VECTOR_UPL_SIZE
;
4138 io_start
= io_size
= io_req_size
;
4141 * First look for pages already in the cache
4142 * and move them to user space. But only do this
4143 * check if we are not retrieving encrypted data directly
4144 * from the filesystem; those blocks should never
4147 * cluster_copy_ubc_data returns the resid
4150 if ((strict_uncached_IO
== FALSE
) && ((flags
& IO_ENCRYPTED
) == 0)) {
4151 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_size
, 0, 0);
4154 * calculate the number of bytes actually copied
4155 * starting size - residual
4157 xsize
= io_start
- io_size
;
4159 io_req_size
-= xsize
;
4161 if(useVectorUPL
&& (xsize
|| (iov_base
& PAGE_MASK
))) {
4163 * We found something in the cache or we have an iov_base that's not
4166 * Issue all I/O's that have been collected within this Vectored UPL.
4168 if(vector_upl_index
) {
4169 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4170 reset_vector_run_state();
4177 * After this point, if we are using the Vector UPL path and the base is
4178 * not page-aligned then the UPL with that base will be the first in the vector UPL.
4183 * check to see if we are finished with this request.
4185 * If we satisfied this IO already, then io_req_size will be 0.
4186 * Otherwise, see if the IO was mis-aligned and needs to go through
4187 * the UBC to deal with the 'tail'.
4190 if (io_req_size
== 0 || (misaligned
)) {
4192 * see if there's another uio vector to
4193 * process that's of type IO_DIRECT
4195 * break out of while loop to get there
4200 * assume the request ends on a device block boundary
4202 io_min
= devblocksize
;
4205 * we can handle I/O's in multiples of the device block size
4206 * however, if io_size isn't a multiple of devblocksize we
4207 * want to clip it back to the nearest page boundary since
4208 * we are going to have to go through cluster_read_copy to
4209 * deal with the 'overhang'... by clipping it to a PAGE_SIZE
4210 * multiple, we avoid asking the drive for the same physical
4211 * blocks twice.. once for the partial page at the end of the
4212 * request and a 2nd time for the page we read into the cache
4213 * (which overlaps the end of the direct read) in order to
4214 * get at the overhang bytes
4216 if (io_size
& (devblocksize
- 1)) {
4217 if (flags
& IO_ENCRYPTED
) {
4219 * Normally, we'd round down to the previous page boundary to
4220 * let the UBC manage the zero-filling of the file past the EOF.
4221 * But if we're doing encrypted IO, we can't let any of
4222 * the data hit the UBC. This means we have to do the full
4223 * IO to the upper block boundary of the device block that
4224 * contains the EOF. The user will be responsible for not
4225 * interpreting data PAST the EOF in its buffer.
4227 * So just bump the IO back up to a multiple of devblocksize
4229 io_size
= ((io_size
+ devblocksize
) & ~(devblocksize
- 1));
4234 * Clip the request to the previous page size boundary
4235 * since request does NOT end on a device block boundary
4237 io_size
&= ~PAGE_MASK
;
4242 if (retval
|| io_size
< io_min
) {
4244 * either an error or we only have the tail left to
4245 * complete via the copy path...
4246 * we may have already spun some portion of this request
4247 * off as async requests... we need to wait for the I/O
4248 * to complete before returning
4250 goto wait_for_dreads
;
4254 * Don't re-check the UBC data if we are looking for uncached IO
4255 * or asking for encrypted blocks.
4257 if ((strict_uncached_IO
== FALSE
) && ((flags
& IO_ENCRYPTED
) == 0)) {
4259 if ((xsize
= io_size
) > max_rd_size
)
4260 xsize
= max_rd_size
;
4264 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ xsize
, UPL_ROP_ABSENT
, (int *)&io_size
);
4268 * a page must have just come into the cache
4269 * since the first page in this range is no
4270 * longer absent, go back and re-evaluate
4275 if ( (flags
& IO_RETURN_ON_THROTTLE
) ) {
4276 if (cluster_is_throttled(vp
) == THROTTLE_NOW
) {
4277 if ( !cluster_io_present_in_BC(vp
, uio
->uio_offset
)) {
4279 * we're in the throttle window and at least 1 I/O
4280 * has already been issued by a throttleable thread
4281 * in this window, so return with EAGAIN to indicate
4282 * to the FS issuing the cluster_read call that it
4283 * should now throttle after dropping any locks
4285 throttle_info_update_by_mount(vp
->v_mount
);
4287 io_throttled
= TRUE
;
4288 goto wait_for_dreads
;
4292 if (io_size
> max_rd_size
)
4293 io_size
= max_rd_size
;
4295 iov_base
= uio_curriovbase(uio
);
4297 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
4298 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
4300 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_START
,
4301 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
4303 if (upl_offset
== 0 && ((io_size
& PAGE_MASK
) == 0))
4308 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
4310 upl_size
= upl_needed_size
;
4311 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
4314 upl_flags
|= UPL_NOZEROFILL
;
4315 if (force_data_sync
)
4316 upl_flags
|= UPL_FORCE_DATA_SYNC
;
4318 kret
= vm_map_create_upl(current_map(),
4319 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4320 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
);
4322 if (kret
!= KERN_SUCCESS
) {
4323 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4324 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4326 * failed to get pagelist
4328 * we may have already spun some portion of this request
4329 * off as async requests... we need to wait for the I/O
4330 * to complete before returning
4332 goto wait_for_dreads
;
4334 pages_in_pl
= upl_size
/ PAGE_SIZE
;
4335 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
4337 for (i
= 0; i
< pages_in_pl
; i
++) {
4338 if (!upl_page_present(pl
, i
))
4341 if (i
== pages_in_pl
)
4344 ubc_upl_abort(upl
, 0);
4346 if (force_data_sync
>= 3) {
4347 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4348 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4350 goto wait_for_dreads
;
4353 * Consider the possibility that upl_size wasn't satisfied.
4355 if (upl_size
< upl_needed_size
) {
4356 if (upl_size
&& upl_offset
== 0)
4362 ubc_upl_abort(upl
, 0);
4363 goto wait_for_dreads
;
4365 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4366 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4369 vm_offset_t end_off
= ((iov_base
+ io_size
) & PAGE_MASK
);
4373 * After this point, if we are using a vector UPL, then
4374 * either all the UPL elements end on a page boundary OR
4375 * this UPL is the last element because it does not end
4376 * on a page boundary.
4381 * request asynchronously so that we can overlap
4382 * the preparation of the next I/O
4383 * if there are already too many outstanding reads
4384 * wait until some have completed before issuing the next read
4386 if (iostate
.io_issued
> iostate
.io_completed
)
4387 cluster_iostate_wait(&iostate
, max_rd_ahead
, "cluster_read_direct");
4389 if (iostate
.io_error
) {
4391 * one of the earlier reads we issued ran into a hard error
4392 * don't issue any more reads, cleanup the UPL
4393 * that was just created but not used, then
4394 * go wait for any other reads to complete before
4395 * returning the error to the caller
4397 ubc_upl_abort(upl
, 0);
4399 goto wait_for_dreads
;
4401 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_START
,
4402 upl
, (int)upl_offset
, (int)uio
->uio_offset
, io_size
, 0);
4407 io_flag
&= ~CL_PRESERVE
;
4409 io_flag
|= CL_PRESERVE
;
4411 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
, io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4415 if(!vector_upl_index
) {
4416 vector_upl
= vector_upl_create(upl_offset
);
4417 v_upl_uio_offset
= uio
->uio_offset
;
4418 vector_upl_offset
= upl_offset
;
4421 vector_upl_set_subupl(vector_upl
,upl
, upl_size
);
4422 vector_upl_set_iostate(vector_upl
, upl
, vector_upl_size
, upl_size
);
4424 vector_upl_size
+= upl_size
;
4425 vector_upl_iosize
+= io_size
;
4427 if(issueVectorUPL
|| vector_upl_index
== MAX_VECTOR_UPL_ELEMENTS
|| vector_upl_size
>= max_vector_size
) {
4428 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4429 reset_vector_run_state();
4433 * update the uio structure
4435 if ((flags
& IO_ENCRYPTED
) && (max_io_size
< io_size
)) {
4436 uio_update(uio
, (user_size_t
)max_io_size
);
4439 uio_update(uio
, (user_size_t
)io_size
);
4442 * Under normal circumstances, the io_size should not be
4443 * bigger than the io_req_size, but we may have had to round up
4444 * to the end of the page in the encrypted IO case. In that case only,
4445 * ensure that we only decrement io_req_size to 0.
4447 if ((flags
& IO_ENCRYPTED
) && (io_size
> io_req_size
)) {
4451 io_req_size
-= io_size
;
4454 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_END
,
4455 upl
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
4459 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0 && uio
->uio_offset
< filesize
) {
4461 retval
= cluster_io_type(uio
, read_type
, read_length
, 0);
4463 if (retval
== 0 && *read_type
== IO_DIRECT
) {
4465 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
4466 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
4474 if(retval
== 0 && iostate
.io_error
== 0 && useVectorUPL
&& vector_upl_index
) {
4475 retval
= vector_cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4476 reset_vector_run_state();
4479 * make sure all async reads that are part of this stream
4480 * have completed before we return
4482 if (iostate
.io_issued
> iostate
.io_completed
)
4483 cluster_iostate_wait(&iostate
, 0, "cluster_read_direct");
4485 if (iostate
.io_error
)
4486 retval
= iostate
.io_error
;
4488 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
4490 if (io_throttled
== TRUE
&& retval
== 0)
4493 if (io_req_size
&& retval
== 0) {
4495 * we couldn't handle the tail of this request in DIRECT mode
4496 * so fire it through the copy path
4498 retval
= cluster_read_copy(vp
, uio
, io_req_size
, filesize
, flags
, callback
, callback_arg
);
4500 *read_type
= IO_UNKNOWN
;
4502 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
4503 (int)uio
->uio_offset
, (int)uio_resid(uio
), io_req_size
, retval
, 0);
4510 cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
4511 int (*callback
)(buf_t
, void *), void *callback_arg
, int flags
)
4513 upl_page_info_t
*pl
;
4514 upl_t upl
[MAX_VECTS
];
4515 vm_offset_t upl_offset
;
4516 addr64_t dst_paddr
= 0;
4517 user_addr_t iov_base
;
4519 upl_size_t upl_size
;
4520 vm_size_t upl_needed_size
;
4521 mach_msg_type_number_t pages_in_pl
;
4524 struct clios iostate
;
4531 u_int32_t devblocksize
;
4532 u_int32_t mem_alignment_mask
;
4533 u_int32_t tail_size
= 0;
4536 if (flags
& IO_PASSIVE
)
4541 if (flags
& IO_NOCACHE
)
4542 bflag
|= CL_NOCACHE
;
4545 * When we enter this routine, we know
4546 * -- the read_length will not exceed the current iov_len
4547 * -- the target address is physically contiguous for read_length
4549 cluster_syncup(vp
, filesize
, callback
, callback_arg
);
4551 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
4552 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
4554 iostate
.io_completed
= 0;
4555 iostate
.io_issued
= 0;
4556 iostate
.io_error
= 0;
4557 iostate
.io_wanted
= 0;
4559 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
4562 io_size
= *read_length
;
4564 max_size
= filesize
- uio
->uio_offset
;
4566 if (io_size
> max_size
)
4569 iov_base
= uio_curriovbase(uio
);
4571 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
4572 upl_needed_size
= upl_offset
+ io_size
;
4575 upl_size
= upl_needed_size
;
4576 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
4579 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_START
,
4580 (int)upl_offset
, (int)upl_size
, (int)iov_base
, io_size
, 0);
4582 kret
= vm_map_get_upl(current_map(),
4583 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4584 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, 0);
4586 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_END
,
4587 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4589 if (kret
!= KERN_SUCCESS
) {
4591 * failed to get pagelist
4594 goto wait_for_creads
;
4598 if (upl_size
< upl_needed_size
) {
4600 * The upl_size wasn't satisfied.
4603 goto wait_for_creads
;
4605 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
4607 dst_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)upl_offset
;
4609 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
4610 u_int32_t head_size
;
4612 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
4614 if (head_size
> io_size
)
4615 head_size
= io_size
;
4617 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, head_size
, CL_READ
, callback
, callback_arg
);
4620 goto wait_for_creads
;
4622 upl_offset
+= head_size
;
4623 dst_paddr
+= head_size
;
4624 io_size
-= head_size
;
4626 iov_base
+= head_size
;
4628 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
4630 * request doesn't set up on a memory boundary
4631 * the underlying DMA engine can handle...
4632 * return an error instead of going through
4633 * the slow copy path since the intent of this
4634 * path is direct I/O to device memory
4637 goto wait_for_creads
;
4640 tail_size
= io_size
& (devblocksize
- 1);
4642 io_size
-= tail_size
;
4644 while (io_size
&& error
== 0) {
4646 if (io_size
> MAX_IO_CONTIG_SIZE
)
4647 xsize
= MAX_IO_CONTIG_SIZE
;
4651 * request asynchronously so that we can overlap
4652 * the preparation of the next I/O... we'll do
4653 * the commit after all the I/O has completed
4654 * since its all issued against the same UPL
4655 * if there are already too many outstanding reads
4656 * wait until some have completed before issuing the next
4658 if (iostate
.io_issued
> iostate
.io_completed
)
4659 cluster_iostate_wait(&iostate
, MAX_IO_CONTIG_SIZE
* IO_SCALE(vp
, 2), "cluster_read_contig");
4661 if (iostate
.io_error
) {
4663 * one of the earlier reads we issued ran into a hard error
4664 * don't issue any more reads...
4665 * go wait for any other reads to complete before
4666 * returning the error to the caller
4668 goto wait_for_creads
;
4670 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
, xsize
,
4671 CL_READ
| CL_NOZERO
| CL_DEV_MEMORY
| CL_ASYNC
| bflag
,
4672 (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4674 * The cluster_io read was issued successfully,
4675 * update the uio structure
4678 uio_update(uio
, (user_size_t
)xsize
);
4681 upl_offset
+= xsize
;
4685 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
&& uio
->uio_offset
< filesize
) {
4687 error
= cluster_io_type(uio
, read_type
, read_length
, 0);
4689 if (error
== 0 && *read_type
== IO_CONTIG
) {
4694 *read_type
= IO_UNKNOWN
;
4698 * make sure all async reads that are part of this stream
4699 * have completed before we proceed
4701 if (iostate
.io_issued
> iostate
.io_completed
)
4702 cluster_iostate_wait(&iostate
, 0, "cluster_read_contig");
4704 if (iostate
.io_error
)
4705 error
= iostate
.io_error
;
4707 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
4709 if (error
== 0 && tail_size
)
4710 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, tail_size
, CL_READ
, callback
, callback_arg
);
4712 for (n
= 0; n
< num_upl
; n
++)
4714 * just release our hold on each physically contiguous
4715 * region without changing any state
4717 ubc_upl_abort(upl
[n
], 0);
4724 cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
)
4726 user_size_t iov_len
;
4727 user_addr_t iov_base
= 0;
4729 upl_size_t upl_size
;
4734 * skip over any emtpy vectors
4736 uio_update(uio
, (user_size_t
)0);
4738 iov_len
= uio_curriovlen(uio
);
4740 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_START
, uio
, (int)iov_len
, 0, 0, 0);
4743 iov_base
= uio_curriovbase(uio
);
4745 * make sure the size of the vector isn't too big...
4746 * internally, we want to handle all of the I/O in
4747 * chunk sizes that fit in a 32 bit int
4749 if (iov_len
> (user_size_t
)MAX_IO_REQUEST_SIZE
)
4750 upl_size
= MAX_IO_REQUEST_SIZE
;
4752 upl_size
= (u_int32_t
)iov_len
;
4754 upl_flags
= UPL_QUERY_OBJECT_TYPE
;
4756 if ((vm_map_get_upl(current_map(),
4757 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4758 &upl_size
, &upl
, NULL
, NULL
, &upl_flags
, 0)) != KERN_SUCCESS
) {
4760 * the user app must have passed in an invalid address
4767 *io_length
= upl_size
;
4769 if (upl_flags
& UPL_PHYS_CONTIG
)
4770 *io_type
= IO_CONTIG
;
4771 else if (iov_len
>= min_length
)
4772 *io_type
= IO_DIRECT
;
4777 * nothing left to do for this uio
4780 *io_type
= IO_UNKNOWN
;
4782 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_END
, iov_base
, *io_type
, *io_length
, retval
, 0);
4789 * generate advisory I/O's in the largest chunks possible
4790 * the completed pages will be released into the VM cache
4793 advisory_read(vnode_t vp
, off_t filesize
, off_t f_offset
, int resid
)
4795 return advisory_read_ext(vp
, filesize
, f_offset
, resid
, NULL
, NULL
, CL_PASSIVE
);
4799 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
)
4801 upl_page_info_t
*pl
;
4803 vm_offset_t upl_offset
;
4816 uint32_t max_io_size
;
4819 if ( !UBCINFOEXISTS(vp
))
4825 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
4827 if ((vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
) && !ignore_is_ssd
) {
4828 if (max_io_size
> speculative_prefetch_max_iosize
)
4829 max_io_size
= speculative_prefetch_max_iosize
;
4832 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_START
,
4833 (int)f_offset
, resid
, (int)filesize
, 0, 0);
4835 while (resid
&& f_offset
< filesize
&& retval
== 0) {
4837 * compute the size of the upl needed to encompass
4838 * the requested read... limit each call to cluster_io
4839 * to the maximum UPL size... cluster_io will clip if
4840 * this exceeds the maximum io_size for the device,
4841 * make sure to account for
4842 * a starting offset that's not page aligned
4844 start_offset
= (int)(f_offset
& PAGE_MASK_64
);
4845 upl_f_offset
= f_offset
- (off_t
)start_offset
;
4846 max_size
= filesize
- f_offset
;
4848 if (resid
< max_size
)
4853 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
4854 if ((uint32_t)upl_size
> max_io_size
)
4855 upl_size
= max_io_size
;
4859 * return the number of contiguously present pages in the cache
4860 * starting at upl_f_offset within the file
4862 ubc_range_op(vp
, upl_f_offset
, upl_f_offset
+ upl_size
, UPL_ROP_PRESENT
, &skip_range
);
4866 * skip over pages already present in the cache
4868 io_size
= skip_range
- start_offset
;
4870 f_offset
+= io_size
;
4873 if (skip_range
== upl_size
)
4876 * have to issue some real I/O
4877 * at this point, we know it's starting on a page boundary
4878 * because we've skipped over at least the first page in the request
4881 upl_f_offset
+= skip_range
;
4882 upl_size
-= skip_range
;
4884 pages_in_upl
= upl_size
/ PAGE_SIZE
;
4886 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_START
,
4887 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
4889 kret
= ubc_create_upl(vp
,
4894 UPL_RET_ONLY_ABSENT
| UPL_SET_LITE
);
4895 if (kret
!= KERN_SUCCESS
)
4900 * before we start marching forward, we must make sure we end on
4901 * a present page, otherwise we will be working with a freed
4904 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
4905 if (upl_page_present(pl
, last_pg
))
4908 pages_in_upl
= last_pg
+ 1;
4911 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_END
,
4912 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
4915 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
4917 * scan from the beginning of the upl looking for the first
4918 * page that is present.... this will become the first page in
4919 * the request we're going to make to 'cluster_io'... if all
4920 * of the pages are absent, we won't call through to 'cluster_io'
4922 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
4923 if (upl_page_present(pl
, start_pg
))
4928 * scan from the starting present page looking for an absent
4929 * page before the end of the upl is reached, if we
4930 * find one, then it will terminate the range of pages being
4931 * presented to 'cluster_io'
4933 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
4934 if (!upl_page_present(pl
, last_pg
))
4938 if (last_pg
> start_pg
) {
4940 * we found a range of pages that must be filled
4941 * if the last page in this range is the last page of the file
4942 * we may have to clip the size of it to keep from reading past
4943 * the end of the last physical block associated with the file
4945 upl_offset
= start_pg
* PAGE_SIZE
;
4946 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
4948 if ((off_t
)(upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
4949 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
4952 * issue an asynchronous read to cluster_io
4954 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
4955 CL_ASYNC
| CL_READ
| CL_COMMIT
| CL_AGE
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
4961 ubc_upl_abort(upl
, 0);
4963 io_size
= upl_size
- start_offset
;
4965 if (io_size
> resid
)
4967 f_offset
+= io_size
;
4971 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_END
,
4972 (int)f_offset
, resid
, retval
, 0, 0);
4979 cluster_push(vnode_t vp
, int flags
)
4981 return cluster_push_ext(vp
, flags
, NULL
, NULL
);
4986 cluster_push_ext(vnode_t vp
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
4989 int my_sparse_wait
= 0;
4990 struct cl_writebehind
*wbp
;
4992 if ( !UBCINFOEXISTS(vp
)) {
4993 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, vp
, flags
, 0, -1, 0);
4996 /* return if deferred write is set */
4997 if (((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) && (flags
& IO_DEFWRITE
)) {
5000 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) == NULL
) {
5001 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, vp
, flags
, 0, -2, 0);
5004 if (wbp
->cl_number
== 0 && wbp
->cl_scmap
== NULL
) {
5005 lck_mtx_unlock(&wbp
->cl_lockw
);
5007 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, vp
, flags
, 0, -3, 0);
5010 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_START
,
5011 wbp
->cl_scmap
, wbp
->cl_number
, flags
, 0, 0);
5014 * if we have an fsync in progress, we don't want to allow any additional
5015 * sync/fsync/close(s) to occur until it finishes.
5016 * note that its possible for writes to continue to occur to this file
5017 * while we're waiting and also once the fsync starts to clean if we're
5018 * in the sparse map case
5020 while (wbp
->cl_sparse_wait
) {
5021 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_START
, vp
, 0, 0, 0, 0);
5023 msleep((caddr_t
)&wbp
->cl_sparse_wait
, &wbp
->cl_lockw
, PRIBIO
+ 1, "cluster_push_ext", NULL
);
5025 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_END
, vp
, 0, 0, 0, 0);
5027 if (flags
& IO_SYNC
) {
5029 wbp
->cl_sparse_wait
= 1;
5032 * this is an fsync (or equivalent)... we must wait for any existing async
5033 * cleaning operations to complete before we evaulate the current state
5034 * and finish cleaning... this insures that all writes issued before this
5035 * fsync actually get cleaned to the disk before this fsync returns
5037 while (wbp
->cl_sparse_pushes
) {
5038 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 98)) | DBG_FUNC_START
, vp
, 0, 0, 0, 0);
5040 msleep((caddr_t
)&wbp
->cl_sparse_pushes
, &wbp
->cl_lockw
, PRIBIO
+ 1, "cluster_push_ext", NULL
);
5042 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 98)) | DBG_FUNC_END
, vp
, 0, 0, 0, 0);
5045 if (wbp
->cl_scmap
) {
5048 if (wbp
->cl_sparse_pushes
< SPARSE_PUSH_LIMIT
) {
5050 scmap
= wbp
->cl_scmap
;
5051 wbp
->cl_scmap
= NULL
;
5053 wbp
->cl_sparse_pushes
++;
5055 lck_mtx_unlock(&wbp
->cl_lockw
);
5057 sparse_cluster_push(&scmap
, vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
);
5059 lck_mtx_lock(&wbp
->cl_lockw
);
5061 wbp
->cl_sparse_pushes
--;
5063 if (wbp
->cl_sparse_wait
&& wbp
->cl_sparse_pushes
== 0)
5064 wakeup((caddr_t
)&wbp
->cl_sparse_pushes
);
5066 sparse_cluster_push(&(wbp
->cl_scmap
), vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
);
5070 retval
= cluster_try_push(wbp
, vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
);
5072 lck_mtx_unlock(&wbp
->cl_lockw
);
5074 if (flags
& IO_SYNC
)
5075 (void)vnode_waitforwrites(vp
, 0, 0, 0, "cluster_push");
5077 if (my_sparse_wait
) {
5079 * I'm the owner of the serialization token
5080 * clear it and wakeup anyone that is waiting
5083 lck_mtx_lock(&wbp
->cl_lockw
);
5085 wbp
->cl_sparse_wait
= 0;
5086 wakeup((caddr_t
)&wbp
->cl_sparse_wait
);
5088 lck_mtx_unlock(&wbp
->cl_lockw
);
5090 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_END
,
5091 wbp
->cl_scmap
, wbp
->cl_number
, retval
, 0, 0);
5097 __private_extern__
void
5098 cluster_release(struct ubc_info
*ubc
)
5100 struct cl_writebehind
*wbp
;
5101 struct cl_readahead
*rap
;
5103 if ((wbp
= ubc
->cl_wbehind
)) {
5105 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, ubc
, wbp
->cl_scmap
, 0, 0, 0);
5108 vfs_drt_control(&(wbp
->cl_scmap
), 0);
5110 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, ubc
, 0, 0, 0, 0);
5113 rap
= ubc
->cl_rahead
;
5116 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
5117 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
5119 if ((rap
= ubc
->cl_rahead
)) {
5120 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
5121 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
5123 ubc
->cl_rahead
= NULL
;
5124 ubc
->cl_wbehind
= NULL
;
5126 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_END
, ubc
, rap
, wbp
, 0, 0);
5131 cluster_try_push(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int push_flag
, int io_flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5138 struct cl_wextent l_clusters
[MAX_CLUSTERS
];
5139 u_int max_cluster_pgcount
;
5142 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
5144 * the write behind context exists and has
5145 * already been locked...
5147 if (wbp
->cl_number
== 0)
5149 * no clusters to push
5150 * return number of empty slots
5152 return (MAX_CLUSTERS
);
5155 * make a local 'sorted' copy of the clusters
5156 * and clear wbp->cl_number so that new clusters can
5159 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
5160 for (min_index
= -1, cl_index1
= 0; cl_index1
< wbp
->cl_number
; cl_index1
++) {
5161 if (wbp
->cl_clusters
[cl_index1
].b_addr
== wbp
->cl_clusters
[cl_index1
].e_addr
)
5163 if (min_index
== -1)
5164 min_index
= cl_index1
;
5165 else if (wbp
->cl_clusters
[cl_index1
].b_addr
< wbp
->cl_clusters
[min_index
].b_addr
)
5166 min_index
= cl_index1
;
5168 if (min_index
== -1)
5171 l_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[min_index
].b_addr
;
5172 l_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
5173 l_clusters
[cl_index
].io_flags
= wbp
->cl_clusters
[min_index
].io_flags
;
5175 wbp
->cl_clusters
[min_index
].b_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
5181 if ( (push_flag
& PUSH_DELAY
) && cl_len
== MAX_CLUSTERS
) {
5185 * determine if we appear to be writing the file sequentially
5186 * if not, by returning without having pushed any clusters
5187 * we will cause this vnode to be pushed into the sparse cluster mechanism
5188 * used for managing more random I/O patterns
5190 * we know that we've got all clusters currently in use and the next write doesn't fit into one of them...
5191 * that's why we're in try_push with PUSH_DELAY...
5193 * check to make sure that all the clusters except the last one are 'full'... and that each cluster
5194 * is adjacent to the next (i.e. we're looking for sequential writes) they were sorted above
5195 * so we can just make a simple pass through, up to, but not including the last one...
5196 * note that e_addr is not inclusive, so it will be equal to the b_addr of the next cluster if they
5199 * we let the last one be partial as long as it was adjacent to the previous one...
5200 * we need to do this to deal with multi-threaded servers that might write an I/O or 2 out
5201 * of order... if this occurs at the tail of the last cluster, we don't want to fall into the sparse cluster world...
5203 for (i
= 0; i
< MAX_CLUSTERS
- 1; i
++) {
5204 if ((l_clusters
[i
].e_addr
- l_clusters
[i
].b_addr
) != max_cluster_pgcount
)
5206 if (l_clusters
[i
].e_addr
!= l_clusters
[i
+1].b_addr
)
5210 for (cl_index
= 0; cl_index
< cl_len
; cl_index
++) {
5212 struct cl_extent cl
;
5214 flags
= io_flags
& (IO_PASSIVE
|IO_CLOSE
);
5217 * try to push each cluster in turn...
5219 if (l_clusters
[cl_index
].io_flags
& CLW_IONOCACHE
)
5220 flags
|= IO_NOCACHE
;
5222 if (l_clusters
[cl_index
].io_flags
& CLW_IOPASSIVE
)
5223 flags
|= IO_PASSIVE
;
5225 if (push_flag
& PUSH_SYNC
)
5228 cl
.b_addr
= l_clusters
[cl_index
].b_addr
;
5229 cl
.e_addr
= l_clusters
[cl_index
].e_addr
;
5231 cluster_push_now(vp
, &cl
, EOF
, flags
, callback
, callback_arg
);
5233 l_clusters
[cl_index
].b_addr
= 0;
5234 l_clusters
[cl_index
].e_addr
= 0;
5238 if ( !(push_flag
& PUSH_ALL
) )
5242 if (cl_len
> cl_pushed
) {
5244 * we didn't push all of the clusters, so
5245 * lets try to merge them back in to the vnode
5247 if ((MAX_CLUSTERS
- wbp
->cl_number
) < (cl_len
- cl_pushed
)) {
5249 * we picked up some new clusters while we were trying to
5250 * push the old ones... this can happen because I've dropped
5251 * the vnode lock... the sum of the
5252 * leftovers plus the new cluster count exceeds our ability
5253 * to represent them, so switch to the sparse cluster mechanism
5255 * collect the active public clusters...
5257 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
);
5259 for (cl_index
= 0, cl_index1
= 0; cl_index
< cl_len
; cl_index
++) {
5260 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
5262 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
5263 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
5264 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
5269 * update the cluster count
5271 wbp
->cl_number
= cl_index1
;
5274 * and collect the original clusters that were moved into the
5275 * local storage for sorting purposes
5277 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
);
5281 * we've got room to merge the leftovers back in
5282 * just append them starting at the next 'hole'
5283 * represented by wbp->cl_number
5285 for (cl_index
= 0, cl_index1
= wbp
->cl_number
; cl_index
< cl_len
; cl_index
++) {
5286 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
5289 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
5290 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
5291 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
5296 * update the cluster count
5298 wbp
->cl_number
= cl_index1
;
5301 return (MAX_CLUSTERS
- wbp
->cl_number
);
5307 cluster_push_now(vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5309 upl_page_info_t
*pl
;
5311 vm_offset_t upl_offset
;
5326 if (flags
& IO_PASSIVE
)
5331 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_START
,
5332 (int)cl
->b_addr
, (int)cl
->e_addr
, (int)EOF
, flags
, 0);
5334 if ((pages_in_upl
= (int)(cl
->e_addr
- cl
->b_addr
)) == 0) {
5335 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 0, 0, 0, 0);
5339 upl_size
= pages_in_upl
* PAGE_SIZE
;
5340 upl_f_offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
5342 if (upl_f_offset
+ upl_size
>= EOF
) {
5344 if (upl_f_offset
>= EOF
) {
5346 * must have truncated the file and missed
5347 * clearing a dangling cluster (i.e. it's completely
5348 * beyond the new EOF
5350 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 1, 0, 0, 0);
5354 size
= EOF
- upl_f_offset
;
5356 upl_size
= (size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
5357 pages_in_upl
= upl_size
/ PAGE_SIZE
;
5361 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, size
, 0, 0, 0);
5364 * by asking for UPL_COPYOUT_FROM and UPL_RET_ONLY_DIRTY, we get the following desirable behavior
5366 * - only pages that are currently dirty are returned... these are the ones we need to clean
5367 * - the hardware dirty bit is cleared when the page is gathered into the UPL... the software dirty bit is set
5368 * - 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
5369 * - when we commit the page, the software dirty bit is cleared... the hardware dirty bit is untouched so that if
5370 * someone dirties this page while the I/O is in progress, we don't lose track of the new state
5372 * when the I/O completes, we no longer ask for an explicit clear of the DIRTY state (either soft or hard)
5375 if ((vp
->v_flag
& VNOCACHE_DATA
) || (flags
& IO_NOCACHE
))
5376 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
| UPL_WILL_BE_DUMPED
;
5378 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
;
5380 kret
= ubc_create_upl(vp
,
5386 if (kret
!= KERN_SUCCESS
)
5387 panic("cluster_push: failed to get pagelist");
5389 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
, upl
, upl_f_offset
, 0, 0, 0);
5392 * since we only asked for the dirty pages back
5393 * it's possible that we may only get a few or even none, so...
5394 * before we start marching forward, we must make sure we know
5395 * where the last present page is in the UPL, otherwise we could
5396 * end up working with a freed upl due to the FREE_ON_EMPTY semantics
5397 * employed by commit_range and abort_range.
5399 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
5400 if (upl_page_present(pl
, last_pg
))
5403 pages_in_upl
= last_pg
+ 1;
5405 if (pages_in_upl
== 0) {
5406 ubc_upl_abort(upl
, 0);
5408 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 2, 0, 0, 0);
5412 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
5414 * find the next dirty page in the UPL
5415 * this will become the first page in the
5416 * next I/O to generate
5418 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
5419 if (upl_dirty_page(pl
, start_pg
))
5421 if (upl_page_present(pl
, start_pg
))
5423 * RET_ONLY_DIRTY will return non-dirty 'precious' pages
5424 * just release these unchanged since we're not going
5425 * to steal them or change their state
5427 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
5429 if (start_pg
>= pages_in_upl
)
5431 * done... no more dirty pages to push
5434 if (start_pg
> last_pg
)
5436 * skipped over some non-dirty pages
5438 size
-= ((start_pg
- last_pg
) * PAGE_SIZE
);
5441 * find a range of dirty pages to write
5443 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
5444 if (!upl_dirty_page(pl
, last_pg
))
5447 upl_offset
= start_pg
* PAGE_SIZE
;
5449 io_size
= min(size
, (last_pg
- start_pg
) * PAGE_SIZE
);
5451 io_flags
= CL_THROTTLE
| CL_COMMIT
| CL_AGE
| bflag
;
5453 if ( !(flags
& IO_SYNC
))
5454 io_flags
|= CL_ASYNC
;
5456 if (flags
& IO_CLOSE
)
5457 io_flags
|= CL_CLOSE
;
5459 if (flags
& IO_NOCACHE
)
5460 io_flags
|= CL_NOCACHE
;
5462 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
5463 io_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5465 if (error
== 0 && retval
)
5470 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 3, 0, 0, 0);
5477 * sparse_cluster_switch is called with the write behind lock held
5480 sparse_cluster_switch(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5484 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_START
, vp
, wbp
->cl_scmap
, 0, 0, 0);
5486 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
5488 struct cl_extent cl
;
5490 for (cl
.b_addr
= wbp
->cl_clusters
[cl_index
].b_addr
; cl
.b_addr
< wbp
->cl_clusters
[cl_index
].e_addr
; cl
.b_addr
++) {
5492 if (ubc_page_op(vp
, (off_t
)(cl
.b_addr
* PAGE_SIZE_64
), 0, NULL
, &flags
) == KERN_SUCCESS
) {
5493 if (flags
& UPL_POP_DIRTY
) {
5494 cl
.e_addr
= cl
.b_addr
+ 1;
5496 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, EOF
, callback
, callback_arg
);
5503 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_END
, vp
, wbp
->cl_scmap
, 0, 0, 0);
5508 * sparse_cluster_push must be called with the write-behind lock held if the scmap is
5509 * still associated with the write-behind context... however, if the scmap has been disassociated
5510 * from the write-behind context (the cluster_push case), the wb lock is not held
5513 sparse_cluster_push(void **scmap
, vnode_t vp
, off_t EOF
, int push_flag
, int io_flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5515 struct cl_extent cl
;
5519 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_START
, vp
, (*scmap
), 0, push_flag
, 0);
5521 if (push_flag
& PUSH_ALL
)
5522 vfs_drt_control(scmap
, 1);
5525 if (vfs_drt_get_cluster(scmap
, &offset
, &length
) != KERN_SUCCESS
)
5528 cl
.b_addr
= (daddr64_t
)(offset
/ PAGE_SIZE_64
);
5529 cl
.e_addr
= (daddr64_t
)((offset
+ length
) / PAGE_SIZE_64
);
5531 cluster_push_now(vp
, &cl
, EOF
, io_flags
& (IO_PASSIVE
|IO_CLOSE
), callback
, callback_arg
);
5533 if ( !(push_flag
& PUSH_ALL
) )
5536 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_END
, vp
, (*scmap
), 0, 0, 0);
5541 * sparse_cluster_add is called with the write behind lock held
5544 sparse_cluster_add(void **scmap
, vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5550 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_START
, (*scmap
), 0, cl
->b_addr
, (int)cl
->e_addr
, 0);
5552 offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
5553 length
= ((u_int
)(cl
->e_addr
- cl
->b_addr
)) * PAGE_SIZE
;
5555 while (vfs_drt_mark_pages(scmap
, offset
, length
, &new_dirty
) != KERN_SUCCESS
) {
5557 * no room left in the map
5558 * only a partial update was done
5559 * push out some pages and try again
5561 sparse_cluster_push(scmap
, vp
, EOF
, 0, 0, callback
, callback_arg
);
5563 offset
+= (new_dirty
* PAGE_SIZE_64
);
5564 length
-= (new_dirty
* PAGE_SIZE
);
5566 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_END
, vp
, (*scmap
), 0, 0, 0);
5571 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
)
5573 upl_page_info_t
*pl
;
5583 if (flags
& IO_PASSIVE
)
5588 if (flags
& IO_NOCACHE
)
5589 bflag
|= CL_NOCACHE
;
5591 upl_flags
= UPL_SET_LITE
;
5593 if ( !(flags
& CL_READ
) ) {
5595 * "write" operation: let the UPL subsystem know
5596 * that we intend to modify the buffer cache pages
5599 upl_flags
|= UPL_WILL_MODIFY
;
5602 * indicate that there is no need to pull the
5603 * mapping for this page... we're only going
5604 * to read from it, not modify it.
5606 upl_flags
|= UPL_FILE_IO
;
5608 kret
= ubc_create_upl(vp
,
5609 uio
->uio_offset
& ~PAGE_MASK_64
,
5615 if (kret
!= KERN_SUCCESS
)
5618 if (!upl_valid_page(pl
, 0)) {
5620 * issue a synchronous read to cluster_io
5622 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
5623 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5625 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
5631 ubc_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)(uio
->uio_offset
& PAGE_MASK_64
);
5634 * NOTE: There is no prototype for the following in BSD. It, and the definitions
5635 * of the defines for cppvPsrc, cppvPsnk, cppvFsnk, and cppvFsrc will be found in
5636 * osfmk/ppc/mappings.h. They are not included here because there appears to be no
5637 * way to do so without exporting them to kexts as well.
5639 if (flags
& CL_READ
)
5640 // copypv(ubc_paddr, usr_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsnk); /* Copy physical to physical and flush the destination */
5641 copypv(ubc_paddr
, usr_paddr
, xsize
, 2 | 1 | 4); /* Copy physical to physical and flush the destination */
5643 // copypv(usr_paddr, ubc_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsrc); /* Copy physical to physical and flush the source */
5644 copypv(usr_paddr
, ubc_paddr
, xsize
, 2 | 1 | 8); /* Copy physical to physical and flush the source */
5646 if ( !(flags
& CL_READ
) || (upl_valid_page(pl
, 0) && upl_dirty_page(pl
, 0))) {
5648 * issue a synchronous write to cluster_io
5650 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
5651 bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5654 uio_update(uio
, (user_size_t
)xsize
);
5657 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
5659 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
5661 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, abort_flags
);
5669 cluster_copy_upl_data(struct uio
*uio
, upl_t upl
, int upl_offset
, int *io_resid
)
5677 upl_page_info_t
*pl
;
5681 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
5682 (int)uio
->uio_offset
, upl_offset
, xsize
, 0, 0);
5684 segflg
= uio
->uio_segflg
;
5688 case UIO_USERSPACE32
:
5689 case UIO_USERISPACE32
:
5690 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
5694 case UIO_USERISPACE
:
5695 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
5698 case UIO_USERSPACE64
:
5699 case UIO_USERISPACE64
:
5700 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
5704 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
5708 pl
= ubc_upl_pageinfo(upl
);
5710 pg_index
= upl_offset
/ PAGE_SIZE
;
5711 pg_offset
= upl_offset
& PAGE_MASK
;
5712 csize
= min(PAGE_SIZE
- pg_offset
, xsize
);
5714 while (xsize
&& retval
== 0) {
5717 paddr
= ((addr64_t
)upl_phys_page(pl
, pg_index
) << 12) + pg_offset
;
5719 retval
= uiomove64(paddr
, csize
, uio
);
5724 csize
= min(PAGE_SIZE
, xsize
);
5728 uio
->uio_segflg
= segflg
;
5730 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
5731 (int)uio
->uio_offset
, xsize
, retval
, segflg
, 0);
5738 cluster_copy_ubc_data(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
)
5741 return (cluster_copy_ubc_data_internal(vp
, uio
, io_resid
, mark_dirty
, 1));
5746 cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
)
5753 memory_object_control_t control
;
5755 io_size
= *io_resid
;
5757 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
5758 (int)uio
->uio_offset
, io_size
, mark_dirty
, take_reference
, 0);
5760 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
5762 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
5763 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
5764 (int)uio
->uio_offset
, io_size
, retval
, 3, 0);
5768 segflg
= uio
->uio_segflg
;
5772 case UIO_USERSPACE32
:
5773 case UIO_USERISPACE32
:
5774 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
5777 case UIO_USERSPACE64
:
5778 case UIO_USERISPACE64
:
5779 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
5783 case UIO_USERISPACE
:
5784 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
5788 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
5792 if ( (io_size
= *io_resid
) ) {
5793 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
5794 xsize
= uio_resid(uio
);
5796 retval
= memory_object_control_uiomove(control
, uio
->uio_offset
- start_offset
, uio
,
5797 start_offset
, io_size
, mark_dirty
, take_reference
);
5798 xsize
-= uio_resid(uio
);
5801 uio
->uio_segflg
= segflg
;
5802 *io_resid
= io_size
;
5804 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
5805 (int)uio
->uio_offset
, io_size
, retval
, 0x80000000 | segflg
, 0);
5812 is_file_clean(vnode_t vp
, off_t filesize
)
5816 int total_dirty
= 0;
5818 for (f_offset
= 0; f_offset
< filesize
; f_offset
+= PAGE_SIZE_64
) {
5819 if (ubc_page_op(vp
, f_offset
, 0, NULL
, &flags
) == KERN_SUCCESS
) {
5820 if (flags
& UPL_POP_DIRTY
) {
5834 * Dirty region tracking/clustering mechanism.
5836 * This code (vfs_drt_*) provides a mechanism for tracking and clustering
5837 * dirty regions within a larger space (file). It is primarily intended to
5838 * support clustering in large files with many dirty areas.
5840 * The implementation assumes that the dirty regions are pages.
5842 * To represent dirty pages within the file, we store bit vectors in a
5843 * variable-size circular hash.
5847 * Bitvector size. This determines the number of pages we group in a
5848 * single hashtable entry. Each hashtable entry is aligned to this
5849 * size within the file.
5851 #define DRT_BITVECTOR_PAGES 256
5854 * File offset handling.
5856 * DRT_ADDRESS_MASK is dependent on DRT_BITVECTOR_PAGES;
5857 * the correct formula is (~(DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1)
5859 #define DRT_ADDRESS_MASK (~((1 << 20) - 1))
5860 #define DRT_ALIGN_ADDRESS(addr) ((addr) & DRT_ADDRESS_MASK)
5863 * Hashtable address field handling.
5865 * The low-order bits of the hashtable address are used to conserve
5868 * DRT_HASH_COUNT_MASK must be large enough to store the range
5869 * 0-DRT_BITVECTOR_PAGES inclusive, as well as have one value
5870 * to indicate that the bucket is actually unoccupied.
5872 #define DRT_HASH_GET_ADDRESS(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_ADDRESS_MASK)
5873 #define DRT_HASH_SET_ADDRESS(scm, i, a) \
5875 (scm)->scm_hashtable[(i)].dhe_control = \
5876 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_ADDRESS_MASK) | DRT_ALIGN_ADDRESS(a); \
5878 #define DRT_HASH_COUNT_MASK 0x1ff
5879 #define DRT_HASH_GET_COUNT(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_HASH_COUNT_MASK)
5880 #define DRT_HASH_SET_COUNT(scm, i, c) \
5882 (scm)->scm_hashtable[(i)].dhe_control = \
5883 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_HASH_COUNT_MASK) | ((c) & DRT_HASH_COUNT_MASK); \
5885 #define DRT_HASH_CLEAR(scm, i) \
5887 (scm)->scm_hashtable[(i)].dhe_control = 0; \
5889 #define DRT_HASH_VACATE(scm, i) DRT_HASH_SET_COUNT((scm), (i), DRT_HASH_COUNT_MASK)
5890 #define DRT_HASH_VACANT(scm, i) (DRT_HASH_GET_COUNT((scm), (i)) == DRT_HASH_COUNT_MASK)
5891 #define DRT_HASH_COPY(oscm, oi, scm, i) \
5893 (scm)->scm_hashtable[(i)].dhe_control = (oscm)->scm_hashtable[(oi)].dhe_control; \
5894 DRT_BITVECTOR_COPY(oscm, oi, scm, i); \
5899 * Hash table moduli.
5901 * Since the hashtable entry's size is dependent on the size of
5902 * the bitvector, and since the hashtable size is constrained to
5903 * both being prime and fitting within the desired allocation
5904 * size, these values need to be manually determined.
5906 * For DRT_BITVECTOR_SIZE = 256, the entry size is 40 bytes.
5908 * The small hashtable allocation is 1024 bytes, so the modulus is 23.
5909 * The large hashtable allocation is 16384 bytes, so the modulus is 401.
5911 #define DRT_HASH_SMALL_MODULUS 23
5912 #define DRT_HASH_LARGE_MODULUS 401
5915 * Physical memory required before the large hash modulus is permitted.
5917 * On small memory systems, the large hash modulus can lead to phsyical
5918 * memory starvation, so we avoid using it there.
5920 #define DRT_HASH_LARGE_MEMORY_REQUIRED (1024LL * 1024LL * 1024LL) /* 1GiB */
5922 #define DRT_SMALL_ALLOCATION 1024 /* 104 bytes spare */
5923 #define DRT_LARGE_ALLOCATION 16384 /* 344 bytes spare */
5925 /* *** nothing below here has secret dependencies on DRT_BITVECTOR_PAGES *** */
5928 * Hashtable bitvector handling.
5930 * Bitvector fields are 32 bits long.
5933 #define DRT_HASH_SET_BIT(scm, i, bit) \
5934 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] |= (1 << ((bit) % 32))
5936 #define DRT_HASH_CLEAR_BIT(scm, i, bit) \
5937 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] &= ~(1 << ((bit) % 32))
5939 #define DRT_HASH_TEST_BIT(scm, i, bit) \
5940 ((scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] & (1 << ((bit) % 32)))
5942 #define DRT_BITVECTOR_CLEAR(scm, i) \
5943 bzero(&(scm)->scm_hashtable[(i)].dhe_bitvector[0], (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
5945 #define DRT_BITVECTOR_COPY(oscm, oi, scm, i) \
5946 bcopy(&(oscm)->scm_hashtable[(oi)].dhe_bitvector[0], \
5947 &(scm)->scm_hashtable[(i)].dhe_bitvector[0], \
5948 (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
5955 struct vfs_drt_hashentry
{
5956 u_int64_t dhe_control
;
5957 u_int32_t dhe_bitvector
[DRT_BITVECTOR_PAGES
/ 32];
5961 * Dirty Region Tracking structure.
5963 * The hashtable is allocated entirely inside the DRT structure.
5965 * The hash is a simple circular prime modulus arrangement, the structure
5966 * is resized from small to large if it overflows.
5969 struct vfs_drt_clustermap
{
5970 u_int32_t scm_magic
; /* sanity/detection */
5971 #define DRT_SCM_MAGIC 0x12020003
5972 u_int32_t scm_modulus
; /* current ring size */
5973 u_int32_t scm_buckets
; /* number of occupied buckets */
5974 u_int32_t scm_lastclean
; /* last entry we cleaned */
5975 u_int32_t scm_iskips
; /* number of slot skips */
5977 struct vfs_drt_hashentry scm_hashtable
[0];
5981 #define DRT_HASH(scm, addr) ((addr) % (scm)->scm_modulus)
5982 #define DRT_HASH_NEXT(scm, addr) (((addr) + 1) % (scm)->scm_modulus)
5985 * Debugging codes and arguments.
5987 #define DRT_DEBUG_EMPTYFREE (FSDBG_CODE(DBG_FSRW, 82)) /* nil */
5988 #define DRT_DEBUG_RETCLUSTER (FSDBG_CODE(DBG_FSRW, 83)) /* offset, length */
5989 #define DRT_DEBUG_ALLOC (FSDBG_CODE(DBG_FSRW, 84)) /* copycount */
5990 #define DRT_DEBUG_INSERT (FSDBG_CODE(DBG_FSRW, 85)) /* offset, iskip */
5991 #define DRT_DEBUG_MARK (FSDBG_CODE(DBG_FSRW, 86)) /* offset, length,
5994 /* 1 (clean, no map) */
5995 /* 2 (map alloc fail) */
5996 /* 3, resid (partial) */
5997 #define DRT_DEBUG_6 (FSDBG_CODE(DBG_FSRW, 87))
5998 #define DRT_DEBUG_SCMDATA (FSDBG_CODE(DBG_FSRW, 88)) /* modulus, buckets,
5999 * lastclean, iskips */
6002 static kern_return_t
vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
);
6003 static kern_return_t
vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
);
6004 static kern_return_t
vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
,
6005 u_int64_t offset
, int *indexp
);
6006 static kern_return_t
vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
,
6010 static kern_return_t
vfs_drt_do_mark_pages(
6016 static void vfs_drt_trace(
6017 struct vfs_drt_clustermap
*cmap
,
6026 * Allocate and initialise a sparse cluster map.
6028 * Will allocate a new map, resize or compact an existing map.
6030 * XXX we should probably have at least one intermediate map size,
6031 * as the 1:16 ratio seems a bit drastic.
6033 static kern_return_t
6034 vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
)
6036 struct vfs_drt_clustermap
*cmap
, *ocmap
;
6040 int nsize
, active_buckets
, index
, copycount
;
6047 * Decide on the size of the new map.
6049 if (ocmap
== NULL
) {
6050 nsize
= DRT_HASH_SMALL_MODULUS
;
6052 /* count the number of active buckets in the old map */
6054 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
6055 if (!DRT_HASH_VACANT(ocmap
, i
) &&
6056 (DRT_HASH_GET_COUNT(ocmap
, i
) != 0))
6060 * If we're currently using the small allocation, check to
6061 * see whether we should grow to the large one.
6063 if (ocmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) {
6065 * If the ring is nearly full and we are allowed to
6066 * use the large modulus, upgrade.
6068 if ((active_buckets
> (DRT_HASH_SMALL_MODULUS
- 5)) &&
6069 (max_mem
>= DRT_HASH_LARGE_MEMORY_REQUIRED
)) {
6070 nsize
= DRT_HASH_LARGE_MODULUS
;
6072 nsize
= DRT_HASH_SMALL_MODULUS
;
6075 /* already using the large modulus */
6076 nsize
= DRT_HASH_LARGE_MODULUS
;
6078 * If the ring is completely full, there's
6079 * nothing useful for us to do. Behave as
6080 * though we had compacted into the new
6083 if (active_buckets
>= DRT_HASH_LARGE_MODULUS
)
6084 return(KERN_SUCCESS
);
6089 * Allocate and initialise the new map.
6092 kret
= kmem_alloc(kernel_map
, (vm_offset_t
*)&cmap
,
6093 (nsize
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
6094 if (kret
!= KERN_SUCCESS
)
6096 cmap
->scm_magic
= DRT_SCM_MAGIC
;
6097 cmap
->scm_modulus
= nsize
;
6098 cmap
->scm_buckets
= 0;
6099 cmap
->scm_lastclean
= 0;
6100 cmap
->scm_iskips
= 0;
6101 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
6102 DRT_HASH_CLEAR(cmap
, i
);
6103 DRT_HASH_VACATE(cmap
, i
);
6104 DRT_BITVECTOR_CLEAR(cmap
, i
);
6108 * If there's an old map, re-hash entries from it into the new map.
6111 if (ocmap
!= NULL
) {
6112 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
6113 /* skip empty buckets */
6114 if (DRT_HASH_VACANT(ocmap
, i
) ||
6115 (DRT_HASH_GET_COUNT(ocmap
, i
) == 0))
6118 offset
= DRT_HASH_GET_ADDRESS(ocmap
, i
);
6119 kret
= vfs_drt_get_index(&cmap
, offset
, &index
, 1);
6120 if (kret
!= KERN_SUCCESS
) {
6121 /* XXX need to bail out gracefully here */
6122 panic("vfs_drt: new cluster map mysteriously too small");
6126 DRT_HASH_COPY(ocmap
, i
, cmap
, index
);
6131 /* log what we've done */
6132 vfs_drt_trace(cmap
, DRT_DEBUG_ALLOC
, copycount
, 0, 0, 0);
6135 * It's important to ensure that *cmapp always points to
6136 * a valid map, so we must overwrite it before freeing
6140 if (ocmap
!= NULL
) {
6141 /* emit stats into trace buffer */
6142 vfs_drt_trace(ocmap
, DRT_DEBUG_SCMDATA
,
6145 ocmap
->scm_lastclean
,
6148 vfs_drt_free_map(ocmap
);
6150 return(KERN_SUCCESS
);
6155 * Free a sparse cluster map.
6157 static kern_return_t
6158 vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
)
6160 kmem_free(kernel_map
, (vm_offset_t
)cmap
,
6161 (cmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
6162 return(KERN_SUCCESS
);
6167 * Find the hashtable slot currently occupied by an entry for the supplied offset.
6169 static kern_return_t
6170 vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
, u_int64_t offset
, int *indexp
)
6175 offset
= DRT_ALIGN_ADDRESS(offset
);
6176 index
= DRT_HASH(cmap
, offset
);
6178 /* traverse the hashtable */
6179 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
6182 * If the slot is vacant, we can stop.
6184 if (DRT_HASH_VACANT(cmap
, index
))
6188 * If the address matches our offset, we have success.
6190 if (DRT_HASH_GET_ADDRESS(cmap
, index
) == offset
) {
6192 return(KERN_SUCCESS
);
6196 * Move to the next slot, try again.
6198 index
= DRT_HASH_NEXT(cmap
, index
);
6203 return(KERN_FAILURE
);
6207 * Find the hashtable slot for the supplied offset. If we haven't allocated
6208 * one yet, allocate one and populate the address field. Note that it will
6209 * not have a nonzero page count and thus will still technically be free, so
6210 * in the case where we are called to clean pages, the slot will remain free.
6212 static kern_return_t
6213 vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
, u_int64_t offset
, int *indexp
, int recursed
)
6215 struct vfs_drt_clustermap
*cmap
;
6222 /* look for an existing entry */
6223 kret
= vfs_drt_search_index(cmap
, offset
, indexp
);
6224 if (kret
== KERN_SUCCESS
)
6227 /* need to allocate an entry */
6228 offset
= DRT_ALIGN_ADDRESS(offset
);
6229 index
= DRT_HASH(cmap
, offset
);
6231 /* scan from the index forwards looking for a vacant slot */
6232 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
6234 if (DRT_HASH_VACANT(cmap
, index
) || DRT_HASH_GET_COUNT(cmap
,index
) == 0) {
6235 cmap
->scm_buckets
++;
6236 if (index
< cmap
->scm_lastclean
)
6237 cmap
->scm_lastclean
= index
;
6238 DRT_HASH_SET_ADDRESS(cmap
, index
, offset
);
6239 DRT_HASH_SET_COUNT(cmap
, index
, 0);
6240 DRT_BITVECTOR_CLEAR(cmap
, index
);
6242 vfs_drt_trace(cmap
, DRT_DEBUG_INSERT
, (int)offset
, i
, 0, 0);
6243 return(KERN_SUCCESS
);
6245 cmap
->scm_iskips
+= i
;
6246 index
= DRT_HASH_NEXT(cmap
, index
);
6250 * We haven't found a vacant slot, so the map is full. If we're not
6251 * already recursed, try reallocating/compacting it.
6254 return(KERN_FAILURE
);
6255 kret
= vfs_drt_alloc_map(cmapp
);
6256 if (kret
== KERN_SUCCESS
) {
6257 /* now try to insert again */
6258 kret
= vfs_drt_get_index(cmapp
, offset
, indexp
, 1);
6264 * Implementation of set dirty/clean.
6266 * In the 'clean' case, not finding a map is OK.
6268 static kern_return_t
6269 vfs_drt_do_mark_pages(
6276 struct vfs_drt_clustermap
*cmap
, **cmapp
;
6278 int i
, index
, pgoff
, pgcount
, setcount
, ecount
;
6280 cmapp
= (struct vfs_drt_clustermap
**)private;
6283 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_START
, (int)offset
, (int)length
, dirty
, 0);
6285 if (setcountp
!= NULL
)
6288 /* allocate a cluster map if we don't already have one */
6290 /* no cluster map, nothing to clean */
6292 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 1, 0, 0, 0);
6293 return(KERN_SUCCESS
);
6295 kret
= vfs_drt_alloc_map(cmapp
);
6296 if (kret
!= KERN_SUCCESS
) {
6297 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 2, 0, 0, 0);
6304 * Iterate over the length of the region.
6306 while (length
> 0) {
6308 * Get the hashtable index for this offset.
6310 * XXX this will add blank entries if we are clearing a range
6311 * that hasn't been dirtied.
6313 kret
= vfs_drt_get_index(cmapp
, offset
, &index
, 0);
6314 cmap
= *cmapp
; /* may have changed! */
6315 /* this may be a partial-success return */
6316 if (kret
!= KERN_SUCCESS
) {
6317 if (setcountp
!= NULL
)
6318 *setcountp
= setcount
;
6319 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 3, (int)length
, 0, 0);
6325 * Work out how many pages we're modifying in this
6328 pgoff
= (offset
- DRT_ALIGN_ADDRESS(offset
)) / PAGE_SIZE
;
6329 pgcount
= min((length
/ PAGE_SIZE
), (DRT_BITVECTOR_PAGES
- pgoff
));
6332 * Iterate over pages, dirty/clearing as we go.
6334 ecount
= DRT_HASH_GET_COUNT(cmap
, index
);
6335 for (i
= 0; i
< pgcount
; i
++) {
6337 if (!DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
6338 DRT_HASH_SET_BIT(cmap
, index
, pgoff
+ i
);
6343 if (DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
6344 DRT_HASH_CLEAR_BIT(cmap
, index
, pgoff
+ i
);
6350 DRT_HASH_SET_COUNT(cmap
, index
, ecount
);
6352 offset
+= pgcount
* PAGE_SIZE
;
6353 length
-= pgcount
* PAGE_SIZE
;
6355 if (setcountp
!= NULL
)
6356 *setcountp
= setcount
;
6358 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 0, setcount
, 0, 0);
6360 return(KERN_SUCCESS
);
6364 * Mark a set of pages as dirty/clean.
6366 * This is a public interface.
6369 * Pointer to storage suitable for holding a pointer. Note that
6370 * this must either be NULL or a value set by this function.
6373 * Current file size in bytes.
6376 * Offset of the first page to be marked as dirty, in bytes. Must be
6380 * Length of dirty region, in bytes. Must be a multiple of PAGE_SIZE.
6383 * Number of pages newly marked dirty by this call (optional).
6385 * Returns KERN_SUCCESS if all the pages were successfully marked.
6387 static kern_return_t
6388 vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
)
6390 /* XXX size unused, drop from interface */
6391 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, setcountp
, 1));
6395 static kern_return_t
6396 vfs_drt_unmark_pages(void **cmapp
, off_t offset
, u_int length
)
6398 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0));
6403 * Get a cluster of dirty pages.
6405 * This is a public interface.
6408 * Pointer to storage managed by drt_mark_pages. Note that this must
6409 * be NULL or a value set by drt_mark_pages.
6412 * Returns the byte offset into the file of the first page in the cluster.
6415 * Returns the length in bytes of the cluster of dirty pages.
6417 * Returns success if a cluster was found. If KERN_FAILURE is returned, there
6418 * are no dirty pages meeting the minmum size criteria. Private storage will
6419 * be released if there are no more dirty pages left in the map
6422 static kern_return_t
6423 vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
)
6425 struct vfs_drt_clustermap
*cmap
;
6429 int index
, i
, fs
, ls
;
6432 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
6433 return(KERN_FAILURE
);
6436 /* walk the hashtable */
6437 for (offset
= 0, j
= 0; j
< cmap
->scm_modulus
; offset
+= (DRT_BITVECTOR_PAGES
* PAGE_SIZE
), j
++) {
6438 index
= DRT_HASH(cmap
, offset
);
6440 if (DRT_HASH_VACANT(cmap
, index
) || (DRT_HASH_GET_COUNT(cmap
, index
) == 0))
6443 /* scan the bitfield for a string of bits */
6446 for (i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
6447 if (DRT_HASH_TEST_BIT(cmap
, index
, i
)) {
6453 /* didn't find any bits set */
6454 panic("vfs_drt: entry summary count > 0 but no bits set in map");
6456 for (ls
= 0; i
< DRT_BITVECTOR_PAGES
; i
++, ls
++) {
6457 if (!DRT_HASH_TEST_BIT(cmap
, index
, i
))
6461 /* compute offset and length, mark pages clean */
6462 offset
= DRT_HASH_GET_ADDRESS(cmap
, index
) + (PAGE_SIZE
* fs
);
6463 length
= ls
* PAGE_SIZE
;
6464 vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0);
6465 cmap
->scm_lastclean
= index
;
6467 /* return successful */
6468 *offsetp
= (off_t
)offset
;
6471 vfs_drt_trace(cmap
, DRT_DEBUG_RETCLUSTER
, (int)offset
, (int)length
, 0, 0);
6472 return(KERN_SUCCESS
);
6475 * We didn't find anything... hashtable is empty
6476 * emit stats into trace buffer and
6479 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
6482 cmap
->scm_lastclean
,
6485 vfs_drt_free_map(cmap
);
6488 return(KERN_FAILURE
);
6492 static kern_return_t
6493 vfs_drt_control(void **cmapp
, int op_type
)
6495 struct vfs_drt_clustermap
*cmap
;
6498 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
6499 return(KERN_FAILURE
);
6504 /* emit stats into trace buffer */
6505 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
6508 cmap
->scm_lastclean
,
6511 vfs_drt_free_map(cmap
);
6516 cmap
->scm_lastclean
= 0;
6519 return(KERN_SUCCESS
);
6525 * Emit a summary of the state of the clustermap into the trace buffer
6526 * along with some caller-provided data.
6530 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, int code
, int arg1
, int arg2
, int arg3
, int arg4
)
6532 KERNEL_DEBUG(code
, arg1
, arg2
, arg3
, arg4
, 0);
6536 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, __unused
int code
,
6537 __unused
int arg1
, __unused
int arg2
, __unused
int arg3
,
6545 * Perform basic sanity check on the hash entry summary count
6546 * vs. the actual bits set in the entry.
6549 vfs_drt_sanity(struct vfs_drt_clustermap
*cmap
)
6554 for (index
= 0; index
< cmap
->scm_modulus
; index
++) {
6555 if (DRT_HASH_VACANT(cmap
, index
))
6558 for (bits_on
= 0, i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
6559 if (DRT_HASH_TEST_BIT(cmap
, index
, i
))
6562 if (bits_on
!= DRT_HASH_GET_COUNT(cmap
, index
))
6563 panic("bits_on = %d, index = %d\n", bits_on
, index
);