2 * Copyright (c) 2000-2014 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>
87 #include <kern/policy_internal.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_pageout.h>
92 #include <vm/vm_fault.h>
94 #include <sys/kdebug.h>
95 #include <libkern/OSAtomic.h>
101 #include <vfs/vfs_disk_conditioner.h>
105 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
110 #define CL_WRITE 0x02
111 #define CL_ASYNC 0x04
112 #define CL_COMMIT 0x08
113 #define CL_PAGEOUT 0x10
115 #define CL_NOZERO 0x40
116 #define CL_PAGEIN 0x80
117 #define CL_DEV_MEMORY 0x100
118 #define CL_PRESERVE 0x200
119 #define CL_THROTTLE 0x400
120 #define CL_KEEPCACHED 0x800
121 #define CL_DIRECT_IO 0x1000
122 #define CL_PASSIVE 0x2000
123 #define CL_IOSTREAMING 0x4000
124 #define CL_CLOSE 0x8000
125 #define CL_ENCRYPTED 0x10000
126 #define CL_RAW_ENCRYPTED 0x20000
127 #define CL_NOCACHE 0x40000
129 #define MAX_VECTOR_UPL_ELEMENTS 8
130 #define MAX_VECTOR_UPL_SIZE (2 * MAX_UPL_SIZE_BYTES)
132 #define CLUSTER_IO_WAITING ((buf_t)1)
134 extern upl_t
vector_upl_create(vm_offset_t
);
135 extern boolean_t
vector_upl_is_valid(upl_t
);
136 extern boolean_t
vector_upl_set_subupl(upl_t
, upl_t
, u_int32_t
);
137 extern void vector_upl_set_pagelist(upl_t
);
138 extern void vector_upl_set_iostate(upl_t
, upl_t
, vm_offset_t
, u_int32_t
);
142 u_int io_completed
; /* amount of io that has currently completed */
143 u_int io_issued
; /* amount of io that was successfully issued */
144 int io_error
; /* error code of first error encountered */
145 int io_wanted
; /* someone is sleeping waiting for a change in state */
148 struct cl_direct_read_lock
{
149 LIST_ENTRY(cl_direct_read_lock
) chain
;
155 #define CL_DIRECT_READ_LOCK_BUCKETS 61
157 static LIST_HEAD(cl_direct_read_locks
, cl_direct_read_lock
)
158 cl_direct_read_locks
[CL_DIRECT_READ_LOCK_BUCKETS
];
160 static lck_spin_t cl_direct_read_spin_lock
;
162 static lck_grp_t
*cl_mtx_grp
;
163 static lck_attr_t
*cl_mtx_attr
;
164 static lck_grp_attr_t
*cl_mtx_grp_attr
;
165 static lck_mtx_t
*cl_transaction_mtxp
;
172 #define PUSH_DELAY 0x01
173 #define PUSH_ALL 0x02
174 #define PUSH_SYNC 0x04
177 static void cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
);
178 static void cluster_wait_IO(buf_t cbp_head
, int async
);
179 static void cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
);
181 static int cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
);
183 static int cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
184 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*)(buf_t
, void *), void *callback_arg
);
185 static int cluster_iodone(buf_t bp
, void *callback_arg
);
186 static int cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
, vnode_t vp
);
187 static int cluster_is_throttled(vnode_t vp
);
189 static void cluster_iostate_wait(struct clios
*iostate
, u_int target
, const char *wait_name
);
191 static void cluster_syncup(vnode_t vp
, off_t newEOF
, int (*)(buf_t
, void *), void *callback_arg
, int flags
);
193 static void cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int take_reference
);
194 static int cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
);
196 static int cluster_read_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t filesize
, int flags
,
197 int (*)(buf_t
, void *), void *callback_arg
);
198 static int cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
199 int flags
, int (*)(buf_t
, void *), void *callback_arg
);
200 static int cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
201 int (*)(buf_t
, void *), void *callback_arg
, int flags
);
203 static int cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
,
204 off_t headOff
, off_t tailOff
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
205 static int cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
,
206 int *write_type
, u_int32_t
*write_length
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
207 static int cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
,
208 int *write_type
, u_int32_t
*write_length
, int (*)(buf_t
, void *), void *callback_arg
, int bflag
);
210 static void cluster_update_state_internal(vnode_t vp
, struct cl_extent
*cl
, int flags
, boolean_t defer_writes
, boolean_t
*first_pass
,
211 off_t write_off
, int write_cnt
, off_t newEOF
, int (*callback
)(buf_t
, void *), void *callback_arg
, boolean_t vm_initiated
);
213 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
);
215 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
);
216 static void cluster_read_ahead(vnode_t vp
, struct cl_extent
*extent
, off_t filesize
, struct cl_readahead
*ra
,
217 int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
);
219 static int cluster_push_now(vnode_t vp
, struct cl_extent
*, off_t EOF
, int flags
, int (*)(buf_t
, void *), void *callback_arg
, boolean_t vm_ioitiated
);
221 static int cluster_try_push(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int push_flag
, int flags
, int (*)(buf_t
, void *),
222 void *callback_arg
, int *err
, boolean_t vm_initiated
);
224 static int sparse_cluster_switch(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int (*)(buf_t
, void *), void *callback_arg
, boolean_t vm_initiated
);
225 static int sparse_cluster_push(struct cl_writebehind
*, void **cmapp
, vnode_t vp
, off_t EOF
, int push_flag
,
226 int io_flags
, int (*)(buf_t
, void *), void *callback_arg
, boolean_t vm_initiated
);
227 static int sparse_cluster_add(struct cl_writebehind
*, void **cmapp
, vnode_t vp
, struct cl_extent
*, off_t EOF
,
228 int (*)(buf_t
, void *), void *callback_arg
, boolean_t vm_initiated
);
230 static kern_return_t
vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
);
231 static kern_return_t
vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
);
232 static kern_return_t
vfs_drt_control(void **cmapp
, int op_type
);
233 static kern_return_t
vfs_get_scmap_push_behavior_internal(void **cmapp
, int *push_flag
);
237 * For throttled IO to check whether
238 * a block is cached by the boot cache
239 * and thus it can avoid delaying the IO.
241 * bootcache_contains_block is initially
242 * NULL. The BootCache will set it while
243 * the cache is active and clear it when
244 * the cache is jettisoned.
246 * Returns 0 if the block is not
247 * contained in the cache, 1 if it is
250 * The function pointer remains valid
251 * after the cache has been evicted even
252 * if bootcache_contains_block has been
255 * See rdar://9974130 The new throttling mechanism breaks the boot cache for throttled IOs
257 int (*bootcache_contains_block
)(dev_t device
, u_int64_t blkno
) = NULL
;
261 * limit the internal I/O size so that we
262 * can represent it in a 32 bit int
264 #define MAX_IO_REQUEST_SIZE (1024 * 1024 * 512)
265 #define MAX_IO_CONTIG_SIZE MAX_UPL_SIZE_BYTES
268 * The MIN_DIRECT_WRITE_SIZE governs how much I/O should be issued before we consider
269 * allowing the caller to bypass the buffer cache. For small I/Os (less than 16k),
270 * we have not historically allowed the write to bypass the UBC.
272 #define MIN_DIRECT_WRITE_SIZE (16384)
274 #define WRITE_THROTTLE 6
275 #define WRITE_THROTTLE_SSD 2
276 #define WRITE_BEHIND 1
277 #define WRITE_BEHIND_SSD 1
281 #define PREFETCH_SSD 1
282 uint32_t speculative_prefetch_max
= (2048 * 1024); /* maximum bytes in a specluative read-ahead */
283 uint32_t speculative_prefetch_max_iosize
= (512 * 1024); /* maximum I/O size to use in a specluative read-ahead */
286 #define PREFETCH_SSD 2
287 uint32_t speculative_prefetch_max
= (MAX_UPL_SIZE_BYTES
* 3); /* maximum bytes in a specluative read-ahead */
288 uint32_t speculative_prefetch_max_iosize
= (512 * 1024); /* maximum I/O size to use in a specluative read-ahead on SSDs*/
292 #define IO_SCALE(vp, base) (vp->v_mount->mnt_ioscale * (base))
293 #define MAX_CLUSTER_SIZE(vp) (cluster_max_io_size(vp->v_mount, CL_WRITE))
294 #define MAX_PREFETCH(vp, size, is_ssd) (size * IO_SCALE(vp, ((is_ssd) ? PREFETCH_SSD : PREFETCH)))
296 int speculative_reads_disabled
= 0;
299 * throttle the number of async writes that
300 * can be outstanding on a single vnode
301 * before we issue a synchronous write
303 #define THROTTLE_MAXCNT 0
305 uint32_t throttle_max_iosize
= (128 * 1024);
307 #define THROTTLE_MAX_IOSIZE (throttle_max_iosize)
309 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_max_iosize
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_max_iosize
, 0, "");
316 * allocate lock group attribute and group
318 cl_mtx_grp_attr
= lck_grp_attr_alloc_init();
319 cl_mtx_grp
= lck_grp_alloc_init("cluster I/O", cl_mtx_grp_attr
);
322 * allocate the lock attribute
324 cl_mtx_attr
= lck_attr_alloc_init();
326 cl_transaction_mtxp
= lck_mtx_alloc_init(cl_mtx_grp
, cl_mtx_attr
);
328 if (cl_transaction_mtxp
== NULL
) {
329 panic("cluster_init: failed to allocate cl_transaction_mtxp");
332 lck_spin_init(&cl_direct_read_spin_lock
, cl_mtx_grp
, cl_mtx_attr
);
334 for (int i
= 0; i
< CL_DIRECT_READ_LOCK_BUCKETS
; ++i
) {
335 LIST_INIT(&cl_direct_read_locks
[i
]);
341 cluster_max_io_size(mount_t mp
, int type
)
343 uint32_t max_io_size
;
349 segcnt
= mp
->mnt_segreadcnt
;
350 maxcnt
= mp
->mnt_maxreadcnt
;
353 segcnt
= mp
->mnt_segwritecnt
;
354 maxcnt
= mp
->mnt_maxwritecnt
;
357 segcnt
= min(mp
->mnt_segreadcnt
, mp
->mnt_segwritecnt
);
358 maxcnt
= min(mp
->mnt_maxreadcnt
, mp
->mnt_maxwritecnt
);
361 if (segcnt
> (MAX_UPL_SIZE_BYTES
>> PAGE_SHIFT
)) {
363 * don't allow a size beyond the max UPL size we can create
365 segcnt
= MAX_UPL_SIZE_BYTES
>> PAGE_SHIFT
;
367 max_io_size
= min((segcnt
* PAGE_SIZE
), maxcnt
);
369 if (max_io_size
< MAX_UPL_TRANSFER_BYTES
) {
371 * don't allow a size smaller than the old fixed limit
373 max_io_size
= MAX_UPL_TRANSFER_BYTES
;
376 * make sure the size specified is a multiple of PAGE_SIZE
378 max_io_size
&= ~PAGE_MASK
;
386 #define CLW_ALLOCATE 0x01
387 #define CLW_RETURNLOCKED 0x02
388 #define CLW_IONOCACHE 0x04
389 #define CLW_IOPASSIVE 0x08
392 * if the read ahead context doesn't yet exist,
393 * allocate and initialize it...
394 * the vnode lock serializes multiple callers
395 * during the actual assignment... first one
396 * to grab the lock wins... the other callers
397 * will release the now unnecessary storage
399 * once the context is present, try to grab (but don't block on)
400 * the lock associated with it... if someone
401 * else currently owns it, than the read
402 * will run without read-ahead. this allows
403 * multiple readers to run in parallel and
404 * since there's only 1 read ahead context,
405 * there's no real loss in only allowing 1
406 * reader to have read-ahead enabled.
408 static struct cl_readahead
*
409 cluster_get_rap(vnode_t vp
)
411 struct ubc_info
*ubc
;
412 struct cl_readahead
*rap
;
416 if ((rap
= ubc
->cl_rahead
) == NULL
) {
417 MALLOC_ZONE(rap
, struct cl_readahead
*, sizeof *rap
, M_CLRDAHEAD
, M_WAITOK
);
419 bzero(rap
, sizeof *rap
);
421 lck_mtx_init(&rap
->cl_lockr
, cl_mtx_grp
, cl_mtx_attr
);
425 if (ubc
->cl_rahead
== NULL
) {
426 ubc
->cl_rahead
= rap
;
428 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
429 FREE_ZONE(rap
, sizeof *rap
, M_CLRDAHEAD
);
430 rap
= ubc
->cl_rahead
;
434 if (lck_mtx_try_lock(&rap
->cl_lockr
) == TRUE
) {
438 return (struct cl_readahead
*)NULL
;
443 * if the write behind context doesn't yet exist,
444 * and CLW_ALLOCATE is specified, allocate and initialize it...
445 * the vnode lock serializes multiple callers
446 * during the actual assignment... first one
447 * to grab the lock wins... the other callers
448 * will release the now unnecessary storage
450 * if CLW_RETURNLOCKED is set, grab (blocking if necessary)
451 * the lock associated with the write behind context before
455 static struct cl_writebehind
*
456 cluster_get_wbp(vnode_t vp
, int flags
)
458 struct ubc_info
*ubc
;
459 struct cl_writebehind
*wbp
;
463 if ((wbp
= ubc
->cl_wbehind
) == NULL
) {
464 if (!(flags
& CLW_ALLOCATE
)) {
465 return (struct cl_writebehind
*)NULL
;
468 MALLOC_ZONE(wbp
, struct cl_writebehind
*, sizeof *wbp
, M_CLWRBEHIND
, M_WAITOK
);
470 bzero(wbp
, sizeof *wbp
);
471 lck_mtx_init(&wbp
->cl_lockw
, cl_mtx_grp
, cl_mtx_attr
);
475 if (ubc
->cl_wbehind
== NULL
) {
476 ubc
->cl_wbehind
= wbp
;
478 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
479 FREE_ZONE(wbp
, sizeof *wbp
, M_CLWRBEHIND
);
480 wbp
= ubc
->cl_wbehind
;
484 if (flags
& CLW_RETURNLOCKED
) {
485 lck_mtx_lock(&wbp
->cl_lockw
);
493 cluster_syncup(vnode_t vp
, off_t newEOF
, int (*callback
)(buf_t
, void *), void *callback_arg
, int flags
)
495 struct cl_writebehind
*wbp
;
497 if ((wbp
= cluster_get_wbp(vp
, 0)) != NULL
) {
498 if (wbp
->cl_number
) {
499 lck_mtx_lock(&wbp
->cl_lockw
);
501 cluster_try_push(wbp
, vp
, newEOF
, PUSH_ALL
| flags
, 0, callback
, callback_arg
, NULL
, FALSE
);
503 lck_mtx_unlock(&wbp
->cl_lockw
);
510 cluster_io_present_in_BC(vnode_t vp
, off_t f_offset
)
514 int (*bootcache_check_fn
)(dev_t device
, u_int64_t blkno
) = bootcache_contains_block
;
516 if (bootcache_check_fn
&& vp
->v_mount
&& vp
->v_mount
->mnt_devvp
) {
517 if (VNOP_BLOCKMAP(vp
, f_offset
, PAGE_SIZE
, &blkno
, &io_size
, NULL
, VNODE_READ
| VNODE_BLOCKMAP_NO_TRACK
, NULL
)) {
525 if (bootcache_check_fn(vp
->v_mount
->mnt_devvp
->v_rdev
, blkno
)) {
534 cluster_is_throttled(vnode_t vp
)
536 return throttle_io_will_be_throttled(-1, vp
->v_mount
);
541 cluster_iostate_wait(struct clios
*iostate
, u_int target
, const char *wait_name
)
543 lck_mtx_lock(&iostate
->io_mtxp
);
545 while ((iostate
->io_issued
- iostate
->io_completed
) > target
) {
546 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
547 iostate
->io_issued
, iostate
->io_completed
, target
, 0, 0);
549 iostate
->io_wanted
= 1;
550 msleep((caddr_t
)&iostate
->io_wanted
, &iostate
->io_mtxp
, PRIBIO
+ 1, wait_name
, NULL
);
552 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
553 iostate
->io_issued
, iostate
->io_completed
, target
, 0, 0);
555 lck_mtx_unlock(&iostate
->io_mtxp
);
559 cluster_handle_associated_upl(struct clios
*iostate
, upl_t upl
,
560 upl_offset_t upl_offset
, upl_size_t size
)
566 upl_t associated_upl
= upl_associated_upl(upl
);
568 if (!associated_upl
) {
573 printf("1: %d %d\n", upl_offset
, upl_offset
+ size
);
577 * The associated UPL is page aligned to file offsets whereas the
578 * UPL it's attached to has different alignment requirements. The
579 * upl_offset that we have refers to @upl. The code that follows
580 * has to deal with the first and last pages in this transaction
581 * which might straddle pages in the associated UPL. To keep
582 * track of these pages, we use the mark bits: if the mark bit is
583 * set, we know another transaction has completed its part of that
584 * page and so we can unlock that page here.
586 * The following illustrates what we have to deal with:
588 * MEM u <------------ 1 PAGE ------------> e
589 * +-------------+----------------------+-----------------
590 * | |######################|#################
591 * +-------------+----------------------+-----------------
592 * FILE | <--- a ---> o <------------ 1 PAGE ------------>
594 * So here we show a write to offset @o. The data that is to be
595 * written is in a buffer that is not page aligned; it has offset
596 * @a in the page. The upl that carries the data starts in memory
597 * at @u. The associated upl starts in the file at offset @o. A
598 * transaction will always end on a page boundary (like @e above)
599 * except for the very last transaction in the group. We cannot
600 * unlock the page at @o in the associated upl until both the
601 * transaction ending at @e and the following transaction (that
602 * starts at @e) has completed.
606 * We record whether or not the two UPLs are aligned as the mark
607 * bit in the first page of @upl.
609 upl_page_info_t
*pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
610 bool is_unaligned
= upl_page_get_mark(pl
, 0);
613 upl_page_info_t
*assoc_pl
= UPL_GET_INTERNAL_PAGE_LIST(associated_upl
);
615 upl_offset_t upl_end
= upl_offset
+ size
;
616 assert(upl_end
>= PAGE_SIZE
);
618 upl_size_t assoc_upl_size
= upl_get_size(associated_upl
);
621 * In the very first transaction in the group, upl_offset will
622 * not be page aligned, but after that it will be and in that
623 * case we want the preceding page in the associated UPL hence
628 upl_offset
= trunc_page_32(upl_offset
- 1);
631 lck_mtx_lock_spin(&iostate
->io_mtxp
);
633 // Look at the first page...
635 && !upl_page_get_mark(assoc_pl
, upl_offset
>> PAGE_SHIFT
)) {
637 * The first page isn't marked so let another transaction
638 * completion handle it.
640 upl_page_set_mark(assoc_pl
, upl_offset
>> PAGE_SHIFT
, true);
641 upl_offset
+= PAGE_SIZE
;
644 // And now the last page...
647 * This needs to be > rather than >= because if it's equal, it
648 * means there's another transaction that is sharing the last
651 if (upl_end
> assoc_upl_size
) {
652 upl_end
= assoc_upl_size
;
654 upl_end
= trunc_page_32(upl_end
);
655 const int last_pg
= (upl_end
>> PAGE_SHIFT
) - 1;
657 if (!upl_page_get_mark(assoc_pl
, last_pg
)) {
659 * The last page isn't marked so mark the page and let another
660 * transaction completion handle it.
662 upl_page_set_mark(assoc_pl
, last_pg
, true);
663 upl_end
-= PAGE_SIZE
;
667 lck_mtx_unlock(&iostate
->io_mtxp
);
670 printf("2: %d %d\n", upl_offset
, upl_end
);
673 if (upl_end
<= upl_offset
) {
677 size
= upl_end
- upl_offset
;
679 assert(!(upl_offset
& PAGE_MASK
));
680 assert(!(size
& PAGE_MASK
));
686 * We can unlock these pages now and as this is for a
687 * direct/uncached write, we want to dump the pages too.
689 kern_return_t kr
= upl_abort_range(associated_upl
, upl_offset
, size
,
690 UPL_ABORT_DUMP_PAGES
, &empty
);
695 upl_set_associated_upl(upl
, NULL
);
696 upl_deallocate(associated_upl
);
701 cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
, vnode_t vp
)
703 int upl_abort_code
= 0;
707 if ((io_flags
& (B_PHYS
| B_CACHE
)) == (B_PHYS
| B_CACHE
)) {
709 * direct write of any flavor, or a direct read that wasn't aligned
711 ubc_upl_commit_range(upl
, upl_offset
, abort_size
, UPL_COMMIT_FREE_ON_EMPTY
);
713 if (io_flags
& B_PAGEIO
) {
714 if (io_flags
& B_READ
) {
720 if (io_flags
& B_CACHE
) {
722 * leave pages in the cache unchanged on error
724 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
725 } else if (((io_flags
& B_READ
) == 0) && ((error
!= ENXIO
) || vnode_isswap(vp
))) {
727 * transient error on pageout/write path... leave pages unchanged
729 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
730 } else if (page_in
) {
731 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
733 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
736 ubc_upl_abort_range(upl
, upl_offset
, abort_size
, upl_abort_code
);
738 return upl_abort_code
;
743 cluster_iodone(buf_t bp
, void *callback_arg
)
754 int transaction_size
= 0;
761 struct clios
*iostate
;
762 boolean_t transaction_complete
= FALSE
;
764 __IGNORE_WCASTALIGN(cbp_head
= (buf_t
)(bp
->b_trans_head
));
766 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_START
,
767 cbp_head
, bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
769 if (cbp_head
->b_trans_next
|| !(cbp_head
->b_flags
& B_EOT
)) {
770 lck_mtx_lock_spin(cl_transaction_mtxp
);
772 bp
->b_flags
|= B_TDONE
;
774 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
776 * all I/O requests that are part of this transaction
777 * have to complete before we can process it
779 if (!(cbp
->b_flags
& B_TDONE
)) {
780 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
781 cbp_head
, cbp
, cbp
->b_bcount
, cbp
->b_flags
, 0);
783 lck_mtx_unlock(cl_transaction_mtxp
);
788 if (cbp
->b_trans_next
== CLUSTER_IO_WAITING
) {
789 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
790 cbp_head
, cbp
, cbp
->b_bcount
, cbp
->b_flags
, 0);
792 lck_mtx_unlock(cl_transaction_mtxp
);
798 if (cbp
->b_flags
& B_EOT
) {
799 transaction_complete
= TRUE
;
802 lck_mtx_unlock(cl_transaction_mtxp
);
804 if (transaction_complete
== FALSE
) {
805 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
806 cbp_head
, 0, 0, 0, 0);
816 upl_offset
= cbp
->b_uploffset
;
818 b_flags
= cbp
->b_flags
;
819 real_bp
= cbp
->b_real_bp
;
820 zero_offset
= cbp
->b_validend
;
821 iostate
= (struct clios
*)cbp
->b_iostate
;
824 real_bp
->b_dev
= cbp
->b_dev
;
828 if ((cbp
->b_flags
& B_ERROR
) && error
== 0) {
829 error
= cbp
->b_error
;
832 total_resid
+= cbp
->b_resid
;
833 total_size
+= cbp
->b_bcount
;
835 cbp_next
= cbp
->b_trans_next
;
837 if (cbp_next
== NULL
) {
839 * compute the overall size of the transaction
840 * in case we created one that has 'holes' in it
841 * 'total_size' represents the amount of I/O we
842 * did, not the span of the transaction w/r to the UPL
844 transaction_size
= cbp
->b_uploffset
+ cbp
->b_bcount
- upl_offset
;
847 if (cbp
!= cbp_head
) {
854 if (ISSET(b_flags
, B_COMMIT_UPL
)) {
855 cluster_handle_associated_upl(iostate
,
861 if (error
== 0 && total_resid
) {
866 int (*cliodone_func
)(buf_t
, void *) = (int (*)(buf_t
, void *))(cbp_head
->b_cliodone
);
868 if (cliodone_func
!= NULL
) {
869 cbp_head
->b_bcount
= transaction_size
;
871 error
= (*cliodone_func
)(cbp_head
, callback_arg
);
875 cluster_zero(upl
, zero_offset
, PAGE_SIZE
- (zero_offset
& PAGE_MASK
), real_bp
);
878 free_io_buf(cbp_head
);
884 * someone has issued multiple I/Os asynchrounsly
885 * and is waiting for them to complete (streaming)
887 lck_mtx_lock_spin(&iostate
->io_mtxp
);
889 if (error
&& iostate
->io_error
== 0) {
890 iostate
->io_error
= error
;
893 iostate
->io_completed
+= total_size
;
895 if (iostate
->io_wanted
) {
897 * someone is waiting for the state of
898 * this io stream to change
900 iostate
->io_wanted
= 0;
903 lck_mtx_unlock(&iostate
->io_mtxp
);
906 wakeup((caddr_t
)&iostate
->io_wanted
);
910 if (b_flags
& B_COMMIT_UPL
) {
911 pg_offset
= upl_offset
& PAGE_MASK
;
912 commit_size
= (pg_offset
+ transaction_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
915 upl_set_iodone_error(upl
, error
);
917 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, commit_size
, error
, b_flags
, vp
);
919 upl_flags
= UPL_COMMIT_FREE_ON_EMPTY
;
921 if ((b_flags
& B_PHYS
) && (b_flags
& B_READ
)) {
922 upl_flags
|= UPL_COMMIT_SET_DIRTY
;
925 if (b_flags
& B_AGE
) {
926 upl_flags
|= UPL_COMMIT_INACTIVATE
;
929 ubc_upl_commit_range(upl
, upl_offset
- pg_offset
, commit_size
, upl_flags
);
934 real_bp
->b_flags
|= B_ERROR
;
935 real_bp
->b_error
= error
;
937 real_bp
->b_resid
= total_resid
;
939 buf_biodone(real_bp
);
941 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
942 upl
, upl_offset
- pg_offset
, commit_size
, (error
<< 24) | upl_flags
, 0);
949 cluster_throttle_io_limit(vnode_t vp
, uint32_t *limit
)
951 if (cluster_is_throttled(vp
)) {
952 *limit
= THROTTLE_MAX_IOSIZE
;
960 cluster_zero(upl_t upl
, upl_offset_t upl_offset
, int size
, buf_t bp
)
962 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_START
,
963 upl_offset
, size
, bp
, 0, 0);
965 if (bp
== NULL
|| bp
->b_datap
== 0) {
969 pl
= ubc_upl_pageinfo(upl
);
971 if (upl_device_page(pl
) == TRUE
) {
972 zero_addr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + upl_offset
;
974 bzero_phys_nc(zero_addr
, size
);
981 page_index
= upl_offset
/ PAGE_SIZE
;
982 page_offset
= upl_offset
& PAGE_MASK
;
984 zero_addr
= ((addr64_t
)upl_phys_page(pl
, page_index
) << PAGE_SHIFT
) + page_offset
;
985 zero_cnt
= min(PAGE_SIZE
- page_offset
, size
);
987 bzero_phys(zero_addr
, zero_cnt
);
990 upl_offset
+= zero_cnt
;
994 bzero((caddr_t
)((vm_offset_t
)bp
->b_datap
+ upl_offset
), size
);
997 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_END
,
998 upl_offset
, size
, 0, 0, 0);
1003 cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
)
1005 cbp_head
->b_validend
= zero_offset
;
1006 cbp_tail
->b_flags
|= B_EOT
;
1010 cluster_wait_IO(buf_t cbp_head
, int async
)
1016 * Async callback completion will not normally generate a
1017 * wakeup upon I/O completion. To get woken up, we set
1018 * b_trans_next (which is safe for us to modify) on the last
1019 * buffer to CLUSTER_IO_WAITING so that cluster_iodone knows
1020 * to wake us up when all buffers as part of this transaction
1021 * are completed. This is done under the umbrella of
1022 * cl_transaction_mtxp which is also taken in cluster_iodone.
1027 lck_mtx_lock_spin(cl_transaction_mtxp
);
1029 for (cbp
= cbp_head
; cbp
; last
= cbp
, cbp
= cbp
->b_trans_next
) {
1030 if (!ISSET(cbp
->b_flags
, B_TDONE
)) {
1036 last
->b_trans_next
= CLUSTER_IO_WAITING
;
1038 DTRACE_IO1(wait__start
, buf_t
, last
);
1040 msleep(last
, cl_transaction_mtxp
, PSPIN
| (PRIBIO
+ 1), "cluster_wait_IO", NULL
);
1043 * We should only have been woken up if all the
1044 * buffers are completed, but just in case...
1047 for (cbp
= cbp_head
; cbp
!= CLUSTER_IO_WAITING
; cbp
= cbp
->b_trans_next
) {
1048 if (!ISSET(cbp
->b_flags
, B_TDONE
)) {
1054 DTRACE_IO1(wait__done
, buf_t
, last
);
1056 last
->b_trans_next
= NULL
;
1059 lck_mtx_unlock(cl_transaction_mtxp
);
1061 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
1068 cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
)
1072 boolean_t isswapout
= FALSE
;
1075 * cluster_complete_transaction will
1076 * only be called if we've issued a complete chain in synchronous mode
1077 * or, we've already done a cluster_wait_IO on an incomplete chain
1080 for (cbp
= *cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
1085 * we've already waited on all of the I/Os in this transaction,
1086 * so mark all of the buf_t's in this transaction as B_TDONE
1087 * so that cluster_iodone sees the transaction as completed
1089 for (cbp
= *cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
1090 cbp
->b_flags
|= B_TDONE
;
1094 if ((flags
& (CL_ASYNC
| CL_PAGEOUT
)) == CL_PAGEOUT
&& vnode_isswap(cbp
->b_vp
)) {
1098 error
= cluster_iodone(cbp
, callback_arg
);
1100 if (!(flags
& CL_ASYNC
) && error
&& *retval
== 0) {
1101 if (((flags
& (CL_PAGEOUT
| CL_KEEPCACHED
)) != CL_PAGEOUT
) || (error
!= ENXIO
)) {
1103 } else if (isswapout
== TRUE
) {
1107 *cbp_head
= (buf_t
)NULL
;
1112 cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
1113 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1122 buf_t cbp_head
= NULL
;
1123 buf_t cbp_tail
= NULL
;
1124 int trans_count
= 0;
1125 int max_trans_count
;
1131 int zero_offset
= 0;
1132 int async_throttle
= 0;
1134 vm_offset_t upl_end_offset
;
1135 boolean_t need_EOT
= FALSE
;
1138 * we currently don't support buffers larger than a page
1140 if (real_bp
&& non_rounded_size
> PAGE_SIZE
) {
1141 panic("%s(): Called with real buffer of size %d bytes which "
1142 "is greater than the maximum allowed size of "
1143 "%d bytes (the system PAGE_SIZE).\n",
1144 __FUNCTION__
, non_rounded_size
, PAGE_SIZE
);
1150 * we don't want to do any funny rounding of the size for IO requests
1151 * coming through the DIRECT or CONTIGUOUS paths... those pages don't
1152 * belong to us... we can't extend (nor do we need to) the I/O to fill
1155 if (mp
->mnt_devblocksize
> 1 && !(flags
& (CL_DEV_MEMORY
| CL_DIRECT_IO
))) {
1157 * round the requested size up so that this I/O ends on a
1158 * page boundary in case this is a 'write'... if the filesystem
1159 * has blocks allocated to back the page beyond the EOF, we want to
1160 * make sure to write out the zero's that are sitting beyond the EOF
1161 * so that in case the filesystem doesn't explicitly zero this area
1162 * if a hole is created via a lseek/write beyond the current EOF,
1163 * it will return zeros when it's read back from the disk. If the
1164 * physical allocation doesn't extend for the whole page, we'll
1165 * only write/read from the disk up to the end of this allocation
1166 * via the extent info returned from the VNOP_BLOCKMAP call.
1168 pg_offset
= upl_offset
& PAGE_MASK
;
1170 size
= (((non_rounded_size
+ pg_offset
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - pg_offset
;
1173 * anyone advertising a blocksize of 1 byte probably
1174 * can't deal with us rounding up the request size
1175 * AFP is one such filesystem/device
1177 size
= non_rounded_size
;
1179 upl_end_offset
= upl_offset
+ size
;
1181 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_START
, (int)f_offset
, size
, upl_offset
, flags
, 0);
1184 * Set the maximum transaction size to the maximum desired number of
1187 max_trans_count
= 8;
1188 if (flags
& CL_DEV_MEMORY
) {
1189 max_trans_count
= 16;
1192 if (flags
& CL_READ
) {
1194 bmap_flags
= VNODE_READ
;
1196 max_iosize
= mp
->mnt_maxreadcnt
;
1197 max_vectors
= mp
->mnt_segreadcnt
;
1200 bmap_flags
= VNODE_WRITE
;
1202 max_iosize
= mp
->mnt_maxwritecnt
;
1203 max_vectors
= mp
->mnt_segwritecnt
;
1205 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_NONE
, max_iosize
, max_vectors
, mp
->mnt_devblocksize
, 0, 0);
1208 * make sure the maximum iosize is a
1209 * multiple of the page size
1211 max_iosize
&= ~PAGE_MASK
;
1214 * Ensure the maximum iosize is sensible.
1217 max_iosize
= PAGE_SIZE
;
1220 if (flags
& CL_THROTTLE
) {
1221 if (!(flags
& CL_PAGEOUT
) && cluster_is_throttled(vp
)) {
1222 if (max_iosize
> THROTTLE_MAX_IOSIZE
) {
1223 max_iosize
= THROTTLE_MAX_IOSIZE
;
1225 async_throttle
= THROTTLE_MAXCNT
;
1227 if ((flags
& CL_DEV_MEMORY
)) {
1228 async_throttle
= IO_SCALE(vp
, VNODE_ASYNC_THROTTLE
);
1231 u_int max_cluster_size
;
1234 if (vp
->v_mount
->mnt_minsaturationbytecount
) {
1235 max_cluster_size
= vp
->v_mount
->mnt_minsaturationbytecount
;
1239 max_cluster_size
= MAX_CLUSTER_SIZE(vp
);
1241 if (disk_conditioner_mount_is_ssd(vp
->v_mount
)) {
1242 scale
= WRITE_THROTTLE_SSD
;
1244 scale
= WRITE_THROTTLE
;
1247 if (max_iosize
> max_cluster_size
) {
1248 max_cluster
= max_cluster_size
;
1250 max_cluster
= max_iosize
;
1253 if (size
< max_cluster
) {
1257 if (flags
& CL_CLOSE
) {
1258 scale
+= MAX_CLUSTERS
;
1261 async_throttle
= min(IO_SCALE(vp
, VNODE_ASYNC_THROTTLE
), ((scale
* max_cluster_size
) / max_cluster
) - 1);
1265 if (flags
& CL_AGE
) {
1268 if (flags
& (CL_PAGEIN
| CL_PAGEOUT
)) {
1269 io_flags
|= B_PAGEIO
;
1271 if (flags
& (CL_IOSTREAMING
)) {
1272 io_flags
|= B_IOSTREAMING
;
1274 if (flags
& CL_COMMIT
) {
1275 io_flags
|= B_COMMIT_UPL
;
1277 if (flags
& CL_DIRECT_IO
) {
1280 if (flags
& (CL_PRESERVE
| CL_KEEPCACHED
)) {
1281 io_flags
|= B_CACHE
;
1283 if (flags
& CL_PASSIVE
) {
1284 io_flags
|= B_PASSIVE
;
1286 if (flags
& CL_ENCRYPTED
) {
1287 io_flags
|= B_ENCRYPTED_IO
;
1290 if (vp
->v_flag
& VSYSTEM
) {
1294 if ((flags
& CL_READ
) && ((upl_offset
+ non_rounded_size
) & PAGE_MASK
) && (!(flags
& CL_NOZERO
))) {
1296 * then we are going to end up
1297 * with a page that we can't complete (the file size wasn't a multiple
1298 * of PAGE_SIZE and we're trying to read to the end of the file
1299 * so we'll go ahead and zero out the portion of the page we can't
1300 * read in from the file
1302 zero_offset
= upl_offset
+ non_rounded_size
;
1303 } else if (!ISSET(flags
, CL_READ
) && ISSET(flags
, CL_DIRECT_IO
)) {
1304 assert(ISSET(flags
, CL_COMMIT
));
1306 // For a direct/uncached write, we need to lock pages...
1311 * Create a UPL to lock the pages in the cache whilst the
1312 * write is in progress.
1314 ubc_create_upl_kernel(vp
, f_offset
, non_rounded_size
, &cached_upl
,
1315 NULL
, UPL_SET_LITE
, VM_KERN_MEMORY_FILE
);
1318 * Attach this UPL to the other UPL so that we can find it
1321 upl_set_associated_upl(upl
, cached_upl
);
1323 if (upl_offset
& PAGE_MASK
) {
1325 * The two UPLs are not aligned, so mark the first page in
1326 * @upl so that cluster_handle_associated_upl can handle
1329 upl_page_info_t
*pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
1330 upl_page_set_mark(pl
, 0, true);
1337 u_int io_size_wanted
;
1340 if (size
> max_iosize
) {
1341 io_size
= max_iosize
;
1346 io_size_wanted
= io_size
;
1347 io_size_tmp
= (size_t)io_size
;
1349 if ((error
= VNOP_BLOCKMAP(vp
, f_offset
, io_size
, &blkno
, &io_size_tmp
, NULL
, bmap_flags
, NULL
))) {
1353 if (io_size_tmp
> io_size_wanted
) {
1354 io_size
= io_size_wanted
;
1356 io_size
= (u_int
)io_size_tmp
;
1359 if (real_bp
&& (real_bp
->b_blkno
== real_bp
->b_lblkno
)) {
1360 real_bp
->b_blkno
= blkno
;
1363 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 24)) | DBG_FUNC_NONE
,
1364 (int)f_offset
, (int)(blkno
>> 32), (int)blkno
, io_size
, 0);
1368 * vnop_blockmap didn't return an error... however, it did
1369 * return an extent size of 0 which means we can't
1370 * make forward progress on this I/O... a hole in the
1371 * file would be returned as a blkno of -1 with a non-zero io_size
1372 * a real extent is returned with a blkno != -1 and a non-zero io_size
1377 if (!(flags
& CL_READ
) && blkno
== -1) {
1381 if (upl_get_internal_vectorupl(upl
)) {
1382 panic("Vector UPLs should not take this code-path\n");
1385 * we're writing into a 'hole'
1387 if (flags
& CL_PAGEOUT
) {
1389 * if we got here via cluster_pageout
1390 * then just error the request and return
1391 * the 'hole' should already have been covered
1397 * we can get here if the cluster code happens to
1398 * pick up a page that was dirtied via mmap vs
1399 * a 'write' and the page targets a 'hole'...
1400 * i.e. the writes to the cluster were sparse
1401 * and the file was being written for the first time
1403 * we can also get here if the filesystem supports
1404 * 'holes' that are less than PAGE_SIZE.... because
1405 * we can't know if the range in the page that covers
1406 * the 'hole' has been dirtied via an mmap or not,
1407 * we have to assume the worst and try to push the
1408 * entire page to storage.
1410 * Try paging out the page individually before
1411 * giving up entirely and dumping it (the pageout
1412 * path will insure that the zero extent accounting
1413 * has been taken care of before we get back into cluster_io)
1415 * go direct to vnode_pageout so that we don't have to
1416 * unbusy the page from the UPL... we used to do this
1417 * so that we could call ubc_msync, but that results
1418 * in a potential deadlock if someone else races us to acquire
1419 * that page and wins and in addition needs one of the pages
1420 * we're continuing to hold in the UPL
1422 pageout_flags
= UPL_MSYNC
| UPL_VNODE_PAGER
| UPL_NESTED_PAGEOUT
;
1424 if (!(flags
& CL_ASYNC
)) {
1425 pageout_flags
|= UPL_IOSYNC
;
1427 if (!(flags
& CL_COMMIT
)) {
1428 pageout_flags
|= UPL_NOCOMMIT
;
1433 int bytes_in_last_page
;
1436 * first we have to wait for the the current outstanding I/Os
1437 * to complete... EOT hasn't been set yet on this transaction
1438 * so the pages won't be released
1440 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1442 bytes_in_last_page
= cbp_head
->b_uploffset
& PAGE_MASK
;
1443 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
1444 bytes_in_last_page
+= cbp
->b_bcount
;
1446 bytes_in_last_page
&= PAGE_MASK
;
1448 while (bytes_in_last_page
) {
1450 * we've got a transcation that
1451 * includes the page we're about to push out through vnode_pageout...
1452 * find the bp's in the list which intersect this page and either
1453 * remove them entirely from the transaction (there could be multiple bp's), or
1454 * round it's iosize down to the page boundary (there can only be one)...
1456 * find the last bp in the list and act on it
1458 for (prev_cbp
= cbp
= cbp_head
; cbp
->b_trans_next
; cbp
= cbp
->b_trans_next
) {
1462 if (bytes_in_last_page
>= cbp
->b_bcount
) {
1464 * this buf no longer has any I/O associated with it
1466 bytes_in_last_page
-= cbp
->b_bcount
;
1471 if (cbp
== cbp_head
) {
1472 assert(bytes_in_last_page
== 0);
1474 * the buf we just freed was the only buf in
1475 * this transaction... so there's no I/O to do
1481 * remove the buf we just freed from
1482 * the transaction list
1484 prev_cbp
->b_trans_next
= NULL
;
1485 cbp_tail
= prev_cbp
;
1489 * this is the last bp that has I/O
1490 * intersecting the page of interest
1491 * only some of the I/O is in the intersection
1492 * so clip the size but keep it in the transaction list
1494 cbp
->b_bcount
-= bytes_in_last_page
;
1496 bytes_in_last_page
= 0;
1501 * there was more to the current transaction
1502 * than just the page we are pushing out via vnode_pageout...
1503 * mark it as finished and complete it... we've already
1504 * waited for the I/Os to complete above in the call to cluster_wait_IO
1506 cluster_EOT(cbp_head
, cbp_tail
, 0);
1508 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1513 if (vnode_pageout(vp
, upl
, trunc_page(upl_offset
), trunc_page_64(f_offset
), PAGE_SIZE
, pageout_flags
, NULL
) != PAGER_SUCCESS
) {
1516 e_offset
= round_page_64(f_offset
+ 1);
1517 io_size
= e_offset
- f_offset
;
1519 f_offset
+= io_size
;
1520 upl_offset
+= io_size
;
1522 if (size
>= io_size
) {
1528 * keep track of how much of the original request
1529 * that we've actually completed... non_rounded_size
1530 * may go negative due to us rounding the request
1531 * to a page size multiple (i.e. size > non_rounded_size)
1533 non_rounded_size
-= io_size
;
1535 if (non_rounded_size
<= 0) {
1537 * we've transferred all of the data in the original
1538 * request, but we were unable to complete the tail
1539 * of the last page because the file didn't have
1540 * an allocation to back that portion... this is ok.
1546 flags
&= ~CL_COMMIT
;
1552 lblkno
= (daddr64_t
)(f_offset
/ 0x1000);
1554 * we have now figured out how much I/O we can do - this is in 'io_size'
1555 * pg_offset is the starting point in the first page for the I/O
1556 * pg_count is the number of full and partial pages that 'io_size' encompasses
1558 pg_offset
= upl_offset
& PAGE_MASK
;
1560 if (flags
& CL_DEV_MEMORY
) {
1562 * treat physical requests as one 'giant' page
1566 pg_count
= (io_size
+ pg_offset
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1569 if ((flags
& CL_READ
) && blkno
== -1) {
1570 vm_offset_t commit_offset
;
1572 int complete_transaction_now
= 0;
1575 * if we're reading and blkno == -1, then we've got a
1576 * 'hole' in the file that we need to deal with by zeroing
1577 * out the affected area in the upl
1579 if (io_size
>= (u_int
)non_rounded_size
) {
1581 * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
1582 * than 'zero_offset' will be non-zero
1583 * if the 'hole' returned by vnop_blockmap extends all the way to the eof
1584 * (indicated by the io_size finishing off the I/O request for this UPL)
1585 * than we're not going to issue an I/O for the
1586 * last page in this upl... we need to zero both the hole and the tail
1587 * of the page beyond the EOF, since the delayed zero-fill won't kick in
1589 bytes_to_zero
= non_rounded_size
;
1590 if (!(flags
& CL_NOZERO
)) {
1591 bytes_to_zero
= (((upl_offset
+ io_size
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - upl_offset
;
1596 bytes_to_zero
= io_size
;
1601 cluster_zero(upl
, upl_offset
, bytes_to_zero
, real_bp
);
1607 * if there is a current I/O chain pending
1608 * then the first page of the group we just zero'd
1609 * will be handled by the I/O completion if the zero
1610 * fill started in the middle of the page
1612 commit_offset
= (upl_offset
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1614 pg_resid
= commit_offset
- upl_offset
;
1616 if (bytes_to_zero
>= pg_resid
) {
1618 * the last page of the current I/O
1619 * has been completed...
1620 * compute the number of fully zero'd
1621 * pages that are beyond it
1622 * plus the last page if its partial
1623 * and we have no more I/O to issue...
1624 * otherwise a partial page is left
1625 * to begin the next I/O
1627 if ((int)io_size
>= non_rounded_size
) {
1628 pg_count
= (bytes_to_zero
- pg_resid
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1630 pg_count
= (bytes_to_zero
- pg_resid
) / PAGE_SIZE
;
1633 complete_transaction_now
= 1;
1637 * no pending I/O to deal with
1638 * so, commit all of the fully zero'd pages
1639 * plus the last page if its partial
1640 * and we have no more I/O to issue...
1641 * otherwise a partial page is left
1642 * to begin the next I/O
1644 if ((int)io_size
>= non_rounded_size
) {
1645 pg_count
= (pg_offset
+ bytes_to_zero
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1647 pg_count
= (pg_offset
+ bytes_to_zero
) / PAGE_SIZE
;
1650 commit_offset
= upl_offset
& ~PAGE_MASK
;
1653 // Associated UPL is currently only used in the direct write path
1654 assert(!upl_associated_upl(upl
));
1656 if ((flags
& CL_COMMIT
) && pg_count
) {
1657 ubc_upl_commit_range(upl
, commit_offset
, pg_count
* PAGE_SIZE
,
1658 UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
);
1660 upl_offset
+= io_size
;
1661 f_offset
+= io_size
;
1665 * keep track of how much of the original request
1666 * that we've actually completed... non_rounded_size
1667 * may go negative due to us rounding the request
1668 * to a page size multiple (i.e. size > non_rounded_size)
1670 non_rounded_size
-= io_size
;
1672 if (non_rounded_size
<= 0) {
1674 * we've transferred all of the data in the original
1675 * request, but we were unable to complete the tail
1676 * of the last page because the file didn't have
1677 * an allocation to back that portion... this is ok.
1681 if (cbp_head
&& (complete_transaction_now
|| size
== 0)) {
1682 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1684 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1686 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1692 if (pg_count
> max_vectors
) {
1693 if (((pg_count
- max_vectors
) * PAGE_SIZE
) > io_size
) {
1694 io_size
= PAGE_SIZE
- pg_offset
;
1697 io_size
-= (pg_count
- max_vectors
) * PAGE_SIZE
;
1698 pg_count
= max_vectors
;
1702 * If the transaction is going to reach the maximum number of
1703 * desired elements, truncate the i/o to the nearest page so
1704 * that the actual i/o is initiated after this buffer is
1705 * created and added to the i/o chain.
1707 * I/O directed to physically contiguous memory
1708 * doesn't have a requirement to make sure we 'fill' a page
1710 if (!(flags
& CL_DEV_MEMORY
) && trans_count
>= max_trans_count
&&
1711 ((upl_offset
+ io_size
) & PAGE_MASK
)) {
1712 vm_offset_t aligned_ofs
;
1714 aligned_ofs
= (upl_offset
+ io_size
) & ~PAGE_MASK
;
1716 * If the io_size does not actually finish off even a
1717 * single page we have to keep adding buffers to the
1718 * transaction despite having reached the desired limit.
1720 * Eventually we get here with the page being finished
1721 * off (and exceeded) and then we truncate the size of
1722 * this i/o request so that it is page aligned so that
1723 * we can finally issue the i/o on the transaction.
1725 if (aligned_ofs
> upl_offset
) {
1726 io_size
= aligned_ofs
- upl_offset
;
1731 if (!(mp
->mnt_kern_flag
& MNTK_VIRTUALDEV
)) {
1733 * if we're not targeting a virtual device i.e. a disk image
1734 * it's safe to dip into the reserve pool since real devices
1735 * can complete this I/O request without requiring additional
1736 * bufs from the alloc_io_buf pool
1739 } else if ((flags
& CL_ASYNC
) && !(flags
& CL_PAGEOUT
)) {
1741 * Throttle the speculative IO
1748 cbp
= alloc_io_buf(vp
, priv
);
1750 if (flags
& CL_PAGEOUT
) {
1754 * since blocks are in offsets of 0x1000, scale
1755 * iteration to (PAGE_SIZE * pg_count) of blks.
1757 for (i
= 0; i
< (PAGE_SIZE
* pg_count
) / 0x1000; i
++) {
1758 if (buf_invalblkno(vp
, lblkno
+ i
, 0) == EBUSY
) {
1759 panic("BUSY bp found in cluster_io");
1763 if (flags
& CL_ASYNC
) {
1764 if (buf_setcallback(cbp
, (void *)cluster_iodone
, callback_arg
)) {
1765 panic("buf_setcallback failed\n");
1768 cbp
->b_cliodone
= (void *)callback
;
1769 cbp
->b_flags
|= io_flags
;
1770 if (flags
& CL_NOCACHE
) {
1771 cbp
->b_attr
.ba_flags
|= BA_NOCACHE
;
1774 cbp
->b_lblkno
= lblkno
;
1775 cbp
->b_blkno
= blkno
;
1776 cbp
->b_bcount
= io_size
;
1778 if (buf_setupl(cbp
, upl
, upl_offset
)) {
1779 panic("buf_setupl failed\n");
1782 upl_set_blkno(upl
, upl_offset
, io_size
, blkno
);
1784 cbp
->b_trans_next
= (buf_t
)NULL
;
1786 if ((cbp
->b_iostate
= (void *)iostate
)) {
1788 * caller wants to track the state of this
1789 * io... bump the amount issued against this stream
1791 iostate
->io_issued
+= io_size
;
1794 if (flags
& CL_READ
) {
1795 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 26)) | DBG_FUNC_NONE
,
1796 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1798 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 27)) | DBG_FUNC_NONE
,
1799 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1803 cbp_tail
->b_trans_next
= cbp
;
1809 if ((cbp_head
->b_real_bp
= real_bp
)) {
1810 real_bp
= (buf_t
)NULL
;
1813 *(buf_t
*)(&cbp
->b_trans_head
) = cbp_head
;
1817 upl_offset
+= io_size
;
1818 f_offset
+= io_size
;
1821 * keep track of how much of the original request
1822 * that we've actually completed... non_rounded_size
1823 * may go negative due to us rounding the request
1824 * to a page size multiple (i.e. size > non_rounded_size)
1826 non_rounded_size
-= io_size
;
1828 if (non_rounded_size
<= 0) {
1830 * we've transferred all of the data in the original
1831 * request, but we were unable to complete the tail
1832 * of the last page because the file didn't have
1833 * an allocation to back that portion... this is ok.
1839 * we have no more I/O to issue, so go
1840 * finish the final transaction
1843 } else if (((flags
& CL_DEV_MEMORY
) || (upl_offset
& PAGE_MASK
) == 0) &&
1844 ((flags
& CL_ASYNC
) || trans_count
> max_trans_count
)) {
1846 * I/O directed to physically contiguous memory...
1847 * which doesn't have a requirement to make sure we 'fill' a page
1849 * the current I/O we've prepared fully
1850 * completes the last page in this request
1852 * it's either an ASYNC request or
1853 * we've already accumulated more than 8 I/O's into
1854 * this transaction so mark it as complete so that
1855 * it can finish asynchronously or via the cluster_complete_transaction
1856 * below if the request is synchronous
1860 if (need_EOT
== TRUE
) {
1861 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1864 if (flags
& CL_THROTTLE
) {
1865 (void)vnode_waitforwrites(vp
, async_throttle
, 0, 0, "cluster_io");
1868 if (!(io_flags
& B_READ
)) {
1869 vnode_startwrite(vp
);
1872 if (flags
& CL_RAW_ENCRYPTED
) {
1874 * User requested raw encrypted bytes.
1875 * Twiddle the bit in the ba_flags for the buffer
1877 cbp
->b_attr
.ba_flags
|= BA_RAW_ENCRYPTED_IO
;
1880 (void) VNOP_STRATEGY(cbp
);
1882 if (need_EOT
== TRUE
) {
1883 if (!(flags
& CL_ASYNC
)) {
1884 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 1);
1899 * Wait until all of the outstanding I/O
1900 * for this partial transaction has completed
1902 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1905 * Rewind the upl offset to the beginning of the
1908 upl_offset
= cbp_head
->b_uploffset
;
1911 if (ISSET(flags
, CL_COMMIT
)) {
1912 cluster_handle_associated_upl(iostate
, upl
, upl_offset
,
1913 upl_end_offset
- upl_offset
);
1916 // Free all the IO buffers in this transaction
1917 for (cbp
= cbp_head
; cbp
;) {
1920 size
+= cbp
->b_bcount
;
1921 io_size
+= cbp
->b_bcount
;
1923 cbp_next
= cbp
->b_trans_next
;
1929 int need_wakeup
= 0;
1932 * update the error condition for this stream
1933 * since we never really issued the io
1934 * just go ahead and adjust it back
1936 lck_mtx_lock_spin(&iostate
->io_mtxp
);
1938 if (iostate
->io_error
== 0) {
1939 iostate
->io_error
= error
;
1941 iostate
->io_issued
-= io_size
;
1943 if (iostate
->io_wanted
) {
1945 * someone is waiting for the state of
1946 * this io stream to change
1948 iostate
->io_wanted
= 0;
1951 lck_mtx_unlock(&iostate
->io_mtxp
);
1954 wakeup((caddr_t
)&iostate
->io_wanted
);
1958 if (flags
& CL_COMMIT
) {
1961 pg_offset
= upl_offset
& PAGE_MASK
;
1962 abort_size
= (upl_end_offset
- upl_offset
+ PAGE_MASK
) & ~PAGE_MASK
;
1964 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, abort_size
, error
, io_flags
, vp
);
1966 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 28)) | DBG_FUNC_NONE
,
1967 upl
, upl_offset
- pg_offset
, abort_size
, (error
<< 24) | upl_flags
, 0);
1972 } else if (cbp_head
) {
1973 panic("%s(): cbp_head is not NULL.\n", __FUNCTION__
);
1978 * can get here if we either encountered an error
1979 * or we completely zero-filled the request and
1983 real_bp
->b_flags
|= B_ERROR
;
1984 real_bp
->b_error
= error
;
1986 buf_biodone(real_bp
);
1988 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_END
, (int)f_offset
, size
, upl_offset
, retval
, 0);
1993 #define reset_vector_run_state() \
1994 issueVectorUPL = vector_upl_offset = vector_upl_index = vector_upl_iosize = vector_upl_size = 0;
1997 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
,
1998 int io_flag
, buf_t real_bp
, struct clios
*iostate
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2000 vector_upl_set_pagelist(vector_upl
);
2002 if (io_flag
& CL_READ
) {
2003 if (vector_upl_offset
== 0 && ((vector_upl_iosize
& PAGE_MASK
) == 0)) {
2004 io_flag
&= ~CL_PRESERVE
; /*don't zero fill*/
2006 io_flag
|= CL_PRESERVE
; /*zero fill*/
2009 return cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, real_bp
, iostate
, callback
, callback_arg
);
2013 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
)
2015 int pages_in_prefetch
;
2017 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_START
,
2018 (int)f_offset
, size
, (int)filesize
, 0, 0);
2020 if (f_offset
>= filesize
) {
2021 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
2022 (int)f_offset
, 0, 0, 0, 0);
2025 if ((off_t
)size
> (filesize
- f_offset
)) {
2026 size
= filesize
- f_offset
;
2028 pages_in_prefetch
= (size
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
2030 advisory_read_ext(vp
, filesize
, f_offset
, size
, callback
, callback_arg
, bflag
);
2032 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
2033 (int)f_offset
+ size
, pages_in_prefetch
, 0, 1, 0);
2035 return pages_in_prefetch
;
2041 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
,
2046 int size_of_prefetch
;
2050 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_START
,
2051 (int)extent
->b_addr
, (int)extent
->e_addr
, (int)rap
->cl_lastr
, 0, 0);
2053 if (extent
->b_addr
== rap
->cl_lastr
&& extent
->b_addr
== extent
->e_addr
) {
2054 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
2055 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 0, 0);
2058 if (rap
->cl_lastr
== -1 || (extent
->b_addr
!= rap
->cl_lastr
&& extent
->b_addr
!= (rap
->cl_lastr
+ 1))) {
2062 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
2063 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 1, 0);
2067 max_prefetch
= MAX_PREFETCH(vp
, cluster_max_io_size(vp
->v_mount
, CL_READ
), disk_conditioner_mount_is_ssd(vp
->v_mount
));
2069 if (max_prefetch
> speculative_prefetch_max
) {
2070 max_prefetch
= speculative_prefetch_max
;
2073 if (max_prefetch
<= PAGE_SIZE
) {
2074 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
2075 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 6, 0);
2078 if (extent
->e_addr
< rap
->cl_maxra
&& rap
->cl_ralen
>= 4) {
2079 if ((rap
->cl_maxra
- extent
->e_addr
) > (rap
->cl_ralen
/ 4)) {
2080 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
2081 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 2, 0);
2085 r_addr
= max(extent
->e_addr
, rap
->cl_maxra
) + 1;
2086 f_offset
= (off_t
)(r_addr
* PAGE_SIZE_64
);
2088 size_of_prefetch
= 0;
2090 ubc_range_op(vp
, f_offset
, f_offset
+ PAGE_SIZE_64
, UPL_ROP_PRESENT
, &size_of_prefetch
);
2092 if (size_of_prefetch
) {
2093 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
2094 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 3, 0);
2097 if (f_offset
< filesize
) {
2098 daddr64_t read_size
;
2100 rap
->cl_ralen
= rap
->cl_ralen
? min(max_prefetch
/ PAGE_SIZE
, rap
->cl_ralen
<< 1) : 1;
2102 read_size
= (extent
->e_addr
+ 1) - extent
->b_addr
;
2104 if (read_size
> rap
->cl_ralen
) {
2105 if (read_size
> max_prefetch
/ PAGE_SIZE
) {
2106 rap
->cl_ralen
= max_prefetch
/ PAGE_SIZE
;
2108 rap
->cl_ralen
= read_size
;
2111 size_of_prefetch
= cluster_read_prefetch(vp
, f_offset
, rap
->cl_ralen
* PAGE_SIZE
, filesize
, callback
, callback_arg
, bflag
);
2113 if (size_of_prefetch
) {
2114 rap
->cl_maxra
= (r_addr
+ size_of_prefetch
) - 1;
2117 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
2118 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 4, 0);
2123 cluster_pageout(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
2124 int size
, off_t filesize
, int flags
)
2126 return cluster_pageout_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
2131 cluster_pageout_ext(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
2132 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2139 local_flags
= CL_PAGEOUT
| CL_THROTTLE
;
2141 if ((flags
& UPL_IOSYNC
) == 0) {
2142 local_flags
|= CL_ASYNC
;
2144 if ((flags
& UPL_NOCOMMIT
) == 0) {
2145 local_flags
|= CL_COMMIT
;
2147 if ((flags
& UPL_KEEPCACHED
)) {
2148 local_flags
|= CL_KEEPCACHED
;
2150 if (flags
& UPL_PAGING_ENCRYPTED
) {
2151 local_flags
|= CL_ENCRYPTED
;
2155 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 52)) | DBG_FUNC_NONE
,
2156 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
2159 * If they didn't specify any I/O, then we are done...
2160 * we can't issue an abort because we don't know how
2161 * big the upl really is
2167 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
2168 if (local_flags
& CL_COMMIT
) {
2169 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
2174 * can't page-in from a negative offset
2175 * or if we're starting beyond the EOF
2176 * or if the file offset isn't page aligned
2177 * or the size requested isn't a multiple of PAGE_SIZE
2179 if (f_offset
< 0 || f_offset
>= filesize
||
2180 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
)) {
2181 if (local_flags
& CL_COMMIT
) {
2182 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
2186 max_size
= filesize
- f_offset
;
2188 if (size
< max_size
) {
2194 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2196 if (size
> rounded_size
) {
2197 if (local_flags
& CL_COMMIT
) {
2198 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
, size
- rounded_size
,
2199 UPL_ABORT_FREE_ON_EMPTY
);
2202 return cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
2203 local_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
2208 cluster_pagein(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
2209 int size
, off_t filesize
, int flags
)
2211 return cluster_pagein_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
2216 cluster_pagein_ext(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
2217 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2223 int local_flags
= 0;
2225 if (upl
== NULL
|| size
< 0) {
2226 panic("cluster_pagein: NULL upl passed in");
2229 if ((flags
& UPL_IOSYNC
) == 0) {
2230 local_flags
|= CL_ASYNC
;
2232 if ((flags
& UPL_NOCOMMIT
) == 0) {
2233 local_flags
|= CL_COMMIT
;
2235 if (flags
& UPL_IOSTREAMING
) {
2236 local_flags
|= CL_IOSTREAMING
;
2238 if (flags
& UPL_PAGING_ENCRYPTED
) {
2239 local_flags
|= CL_ENCRYPTED
;
2243 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 56)) | DBG_FUNC_NONE
,
2244 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
2247 * can't page-in from a negative offset
2248 * or if we're starting beyond the EOF
2249 * or if the file offset isn't page aligned
2250 * or the size requested isn't a multiple of PAGE_SIZE
2252 if (f_offset
< 0 || f_offset
>= filesize
||
2253 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
) || (upl_offset
& PAGE_MASK
)) {
2254 if (local_flags
& CL_COMMIT
) {
2255 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
2259 max_size
= filesize
- f_offset
;
2261 if (size
< max_size
) {
2267 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2269 if (size
> rounded_size
&& (local_flags
& CL_COMMIT
)) {
2270 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
,
2271 size
- rounded_size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
2274 retval
= cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
2275 local_flags
| CL_READ
| CL_PAGEIN
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
2282 cluster_bp(buf_t bp
)
2284 return cluster_bp_ext(bp
, NULL
, NULL
);
2289 cluster_bp_ext(buf_t bp
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2294 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 19)) | DBG_FUNC_START
,
2295 bp
, (int)bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
2297 if (bp
->b_flags
& B_READ
) {
2298 flags
= CL_ASYNC
| CL_READ
;
2302 if (bp
->b_flags
& B_PASSIVE
) {
2303 flags
|= CL_PASSIVE
;
2306 f_offset
= ubc_blktooff(bp
->b_vp
, bp
->b_lblkno
);
2308 return cluster_io(bp
->b_vp
, bp
->b_upl
, 0, f_offset
, bp
->b_bcount
, flags
, bp
, (struct clios
*)NULL
, callback
, callback_arg
);
2314 cluster_write(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
, int xflags
)
2316 return cluster_write_ext(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, xflags
, NULL
, NULL
);
2321 cluster_write_ext(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
,
2322 int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2324 user_ssize_t cur_resid
;
2329 int write_type
= IO_COPY
;
2330 u_int32_t write_length
;
2334 if (flags
& IO_PASSIVE
) {
2340 if (vp
->v_flag
& VNOCACHE_DATA
) {
2341 flags
|= IO_NOCACHE
;
2342 bflag
|= CL_NOCACHE
;
2347 * this call is being made to zero-fill some range in the file
2349 retval
= cluster_write_copy(vp
, NULL
, (u_int32_t
)0, oldEOF
, newEOF
, headOff
, tailOff
, flags
, callback
, callback_arg
);
2354 * do a write through the cache if one of the following is true....
2355 * NOCACHE is not true or NODIRECT is true
2356 * the uio request doesn't target USERSPACE
2357 * otherwise, find out if we want the direct or contig variant for
2358 * the first vector in the uio request
2360 if (((flags
& (IO_NOCACHE
| IO_NODIRECT
)) == IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
)) {
2361 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
2364 if ((flags
& (IO_TAILZEROFILL
| IO_HEADZEROFILL
)) && write_type
== IO_DIRECT
) {
2366 * must go through the cached variant in this case
2368 write_type
= IO_COPY
;
2371 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< newEOF
&& retval
== 0) {
2372 switch (write_type
) {
2375 * make sure the uio_resid isn't too big...
2376 * internally, we want to handle all of the I/O in
2377 * chunk sizes that fit in a 32 bit int
2379 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
)) {
2381 * we're going to have to call cluster_write_copy
2384 * only want the last call to cluster_write_copy to
2385 * have the IO_TAILZEROFILL flag set and only the
2386 * first call should have IO_HEADZEROFILL
2388 zflags
= flags
& ~IO_TAILZEROFILL
;
2389 flags
&= ~IO_HEADZEROFILL
;
2391 write_length
= MAX_IO_REQUEST_SIZE
;
2394 * last call to cluster_write_copy
2398 write_length
= (u_int32_t
)cur_resid
;
2400 retval
= cluster_write_copy(vp
, uio
, write_length
, oldEOF
, newEOF
, headOff
, tailOff
, zflags
, callback
, callback_arg
);
2404 zflags
= flags
& ~(IO_TAILZEROFILL
| IO_HEADZEROFILL
);
2406 if (flags
& IO_HEADZEROFILL
) {
2408 * only do this once per request
2410 flags
&= ~IO_HEADZEROFILL
;
2412 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, uio
->uio_offset
,
2413 headOff
, (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
2418 retval
= cluster_write_contig(vp
, uio
, newEOF
, &write_type
, &write_length
, callback
, callback_arg
, bflag
);
2420 if (retval
== 0 && (flags
& IO_TAILZEROFILL
) && uio_resid(uio
) == 0) {
2422 * we're done with the data from the user specified buffer(s)
2423 * and we've been requested to zero fill at the tail
2424 * treat this as an IO_HEADZEROFILL which doesn't require a uio
2425 * by rearranging the args and passing in IO_HEADZEROFILL
2427 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, tailOff
, uio
->uio_offset
,
2428 (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
2434 * cluster_write_direct is never called with IO_TAILZEROFILL || IO_HEADZEROFILL
2436 retval
= cluster_write_direct(vp
, uio
, oldEOF
, newEOF
, &write_type
, &write_length
, flags
, callback
, callback_arg
);
2440 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
2444 * in case we end up calling cluster_write_copy (from cluster_write_direct)
2445 * multiple times to service a multi-vector request that is not aligned properly
2446 * we need to update the oldEOF so that we
2447 * don't zero-fill the head of a page if we've successfully written
2448 * data to that area... 'cluster_write_copy' will zero-fill the head of a
2449 * page that is beyond the oldEOF if the write is unaligned... we only
2450 * want that to happen for the very first page of the cluster_write,
2451 * NOT the first page of each vector making up a multi-vector write.
2453 if (uio
->uio_offset
> oldEOF
) {
2454 oldEOF
= uio
->uio_offset
;
2462 cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
2463 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2466 upl_page_info_t
*pl
;
2467 vm_offset_t upl_offset
;
2468 vm_offset_t vector_upl_offset
= 0;
2469 u_int32_t io_req_size
;
2470 u_int32_t offset_in_file
;
2471 u_int32_t offset_in_iovbase
;
2474 upl_size_t upl_size
, vector_upl_size
= 0;
2475 vm_size_t upl_needed_size
;
2476 mach_msg_type_number_t pages_in_pl
;
2477 upl_control_flags_t upl_flags
;
2479 mach_msg_type_number_t i
;
2480 int force_data_sync
;
2483 struct clios iostate
;
2484 user_addr_t iov_base
;
2485 u_int32_t mem_alignment_mask
;
2486 u_int32_t devblocksize
;
2487 u_int32_t max_io_size
;
2488 u_int32_t max_upl_size
;
2489 u_int32_t max_vector_size
;
2490 u_int32_t bytes_outstanding_limit
;
2491 boolean_t io_throttled
= FALSE
;
2493 u_int32_t vector_upl_iosize
= 0;
2494 int issueVectorUPL
= 0, useVectorUPL
= (uio
->uio_iovcnt
> 1);
2495 off_t v_upl_uio_offset
= 0;
2496 int vector_upl_index
= 0;
2497 upl_t vector_upl
= NULL
;
2501 * When we enter this routine, we know
2502 * -- the resid will not exceed iov_len
2504 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_START
,
2505 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
2507 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
2509 io_flag
= CL_ASYNC
| CL_PRESERVE
| CL_COMMIT
| CL_THROTTLE
| CL_DIRECT_IO
;
2511 if (flags
& IO_PASSIVE
) {
2512 io_flag
|= CL_PASSIVE
;
2515 if (flags
& IO_NOCACHE
) {
2516 io_flag
|= CL_NOCACHE
;
2519 if (flags
& IO_SKIP_ENCRYPTION
) {
2520 io_flag
|= CL_ENCRYPTED
;
2523 iostate
.io_completed
= 0;
2524 iostate
.io_issued
= 0;
2525 iostate
.io_error
= 0;
2526 iostate
.io_wanted
= 0;
2528 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
2530 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
2531 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
2533 if (devblocksize
== 1) {
2535 * the AFP client advertises a devblocksize of 1
2536 * however, its BLOCKMAP routine maps to physical
2537 * blocks that are PAGE_SIZE in size...
2538 * therefore we can't ask for I/Os that aren't page aligned
2539 * or aren't multiples of PAGE_SIZE in size
2540 * by setting devblocksize to PAGE_SIZE, we re-instate
2541 * the old behavior we had before the mem_alignment_mask
2542 * changes went in...
2544 devblocksize
= PAGE_SIZE
;
2548 io_req_size
= *write_length
;
2549 iov_base
= uio_curriovbase(uio
);
2551 offset_in_file
= (u_int32_t
)uio
->uio_offset
& PAGE_MASK
;
2552 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
2554 if (offset_in_file
|| offset_in_iovbase
) {
2556 * one of the 2 important offsets is misaligned
2557 * so fire an I/O through the cache for this entire vector
2559 goto wait_for_dwrites
;
2561 if (iov_base
& (devblocksize
- 1)) {
2563 * the offset in memory must be on a device block boundary
2564 * so that we can guarantee that we can generate an
2565 * I/O that ends on a page boundary in cluster_io
2567 goto wait_for_dwrites
;
2570 task_update_logical_writes(current_task(), (io_req_size
& ~PAGE_MASK
), TASK_WRITE_IMMEDIATE
, vp
);
2571 while (io_req_size
>= PAGE_SIZE
&& uio
->uio_offset
< newEOF
&& retval
== 0) {
2574 if ((throttle_type
= cluster_is_throttled(vp
))) {
2576 * we're in the throttle window, at the very least
2577 * we want to limit the size of the I/O we're about
2580 if ((flags
& IO_RETURN_ON_THROTTLE
) && throttle_type
== THROTTLE_NOW
) {
2582 * we're in the throttle window and at least 1 I/O
2583 * has already been issued by a throttleable thread
2584 * in this window, so return with EAGAIN to indicate
2585 * to the FS issuing the cluster_write call that it
2586 * should now throttle after dropping any locks
2588 throttle_info_update_by_mount(vp
->v_mount
);
2590 io_throttled
= TRUE
;
2591 goto wait_for_dwrites
;
2593 max_vector_size
= THROTTLE_MAX_IOSIZE
;
2594 max_io_size
= THROTTLE_MAX_IOSIZE
;
2596 max_vector_size
= MAX_VECTOR_UPL_SIZE
;
2597 max_io_size
= max_upl_size
;
2601 cluster_syncup(vp
, newEOF
, callback
, callback_arg
, callback
? PUSH_SYNC
: 0);
2604 io_size
= io_req_size
& ~PAGE_MASK
;
2605 iov_base
= uio_curriovbase(uio
);
2607 if (io_size
> max_io_size
) {
2608 io_size
= max_io_size
;
2611 if (useVectorUPL
&& (iov_base
& PAGE_MASK
)) {
2613 * We have an iov_base that's not page-aligned.
2614 * Issue all I/O's that have been collected within
2615 * this Vectored UPL.
2617 if (vector_upl_index
) {
2618 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
);
2619 reset_vector_run_state();
2623 * After this point, if we are using the Vector UPL path and the base is
2624 * not page-aligned then the UPL with that base will be the first in the vector UPL.
2628 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
2629 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2631 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_START
,
2632 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
2634 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
2635 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
2637 upl_size
= upl_needed_size
;
2638 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
2639 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
2641 kret
= vm_map_get_upl(map
,
2642 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2648 VM_KERN_MEMORY_FILE
,
2651 if (kret
!= KERN_SUCCESS
) {
2652 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2655 * failed to get pagelist
2657 * we may have already spun some portion of this request
2658 * off as async requests... we need to wait for the I/O
2659 * to complete before returning
2661 goto wait_for_dwrites
;
2663 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2664 pages_in_pl
= upl_size
/ PAGE_SIZE
;
2666 for (i
= 0; i
< pages_in_pl
; i
++) {
2667 if (!upl_valid_page(pl
, i
)) {
2671 if (i
== pages_in_pl
) {
2676 * didn't get all the pages back that we
2677 * needed... release this upl and try again
2679 ubc_upl_abort(upl
, 0);
2681 if (force_data_sync
>= 3) {
2682 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2683 i
, pages_in_pl
, upl_size
, kret
, 0);
2685 * for some reason, we couldn't acquire a hold on all
2686 * the pages needed in the user's address space
2688 * we may have already spun some portion of this request
2689 * off as async requests... we need to wait for the I/O
2690 * to complete before returning
2692 goto wait_for_dwrites
;
2696 * Consider the possibility that upl_size wasn't satisfied.
2698 if (upl_size
< upl_needed_size
) {
2699 if (upl_size
&& upl_offset
== 0) {
2705 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2706 (int)upl_offset
, upl_size
, (int)iov_base
, io_size
, 0);
2709 ubc_upl_abort(upl
, 0);
2711 * we may have already spun some portion of this request
2712 * off as async requests... we need to wait for the I/O
2713 * to complete before returning
2715 goto wait_for_dwrites
;
2719 vm_offset_t end_off
= ((iov_base
+ io_size
) & PAGE_MASK
);
2724 * After this point, if we are using a vector UPL, then
2725 * either all the UPL elements end on a page boundary OR
2726 * this UPL is the last element because it does not end
2727 * on a page boundary.
2732 * we want push out these writes asynchronously so that we can overlap
2733 * the preparation of the next I/O
2734 * if there are already too many outstanding writes
2735 * wait until some complete before issuing the next
2737 if (vp
->v_mount
->mnt_minsaturationbytecount
) {
2738 bytes_outstanding_limit
= vp
->v_mount
->mnt_minsaturationbytecount
;
2740 bytes_outstanding_limit
= max_upl_size
* IO_SCALE(vp
, 2);
2743 cluster_iostate_wait(&iostate
, bytes_outstanding_limit
, "cluster_write_direct");
2745 if (iostate
.io_error
) {
2747 * one of the earlier writes we issued ran into a hard error
2748 * don't issue any more writes, cleanup the UPL
2749 * that was just created but not used, then
2750 * go wait for all writes that are part of this stream
2751 * to complete before returning the error to the caller
2753 ubc_upl_abort(upl
, 0);
2755 goto wait_for_dwrites
;
2758 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_START
,
2759 (int)upl_offset
, (int)uio
->uio_offset
, io_size
, io_flag
, 0);
2761 if (!useVectorUPL
) {
2762 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
2763 io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2765 if (!vector_upl_index
) {
2766 vector_upl
= vector_upl_create(upl_offset
);
2767 v_upl_uio_offset
= uio
->uio_offset
;
2768 vector_upl_offset
= upl_offset
;
2771 vector_upl_set_subupl(vector_upl
, upl
, upl_size
);
2772 vector_upl_set_iostate(vector_upl
, upl
, vector_upl_size
, upl_size
);
2774 vector_upl_iosize
+= io_size
;
2775 vector_upl_size
+= upl_size
;
2777 if (issueVectorUPL
|| vector_upl_index
== MAX_VECTOR_UPL_ELEMENTS
|| vector_upl_size
>= max_vector_size
) {
2778 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
);
2779 reset_vector_run_state();
2784 * update the uio structure to
2785 * reflect the I/O that we just issued
2787 uio_update(uio
, (user_size_t
)io_size
);
2790 * in case we end up calling through to cluster_write_copy to finish
2791 * the tail of this request, we need to update the oldEOF so that we
2792 * don't zero-fill the head of a page if we've successfully written
2793 * data to that area... 'cluster_write_copy' will zero-fill the head of a
2794 * page that is beyond the oldEOF if the write is unaligned... we only
2795 * want that to happen for the very first page of the cluster_write,
2796 * NOT the first page of each vector making up a multi-vector write.
2798 if (uio
->uio_offset
> oldEOF
) {
2799 oldEOF
= uio
->uio_offset
;
2802 io_req_size
-= io_size
;
2804 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_END
,
2805 (int)upl_offset
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
2808 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0) {
2809 retval
= cluster_io_type(uio
, write_type
, write_length
, MIN_DIRECT_WRITE_SIZE
);
2811 if (retval
== 0 && *write_type
== IO_DIRECT
) {
2812 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_NONE
,
2813 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
2821 if (retval
== 0 && iostate
.io_error
== 0 && useVectorUPL
&& vector_upl_index
) {
2822 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
);
2823 reset_vector_run_state();
2826 * make sure all async writes issued as part of this stream
2827 * have completed before we return
2829 cluster_iostate_wait(&iostate
, 0, "cluster_write_direct");
2831 if (iostate
.io_error
) {
2832 retval
= iostate
.io_error
;
2835 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
2837 if (io_throttled
== TRUE
&& retval
== 0) {
2841 if (io_req_size
&& retval
== 0) {
2843 * we couldn't handle the tail of this request in DIRECT mode
2844 * so fire it through the copy path
2846 * note that flags will never have IO_HEADZEROFILL or IO_TAILZEROFILL set
2847 * so we can just pass 0 in for the headOff and tailOff
2849 if (uio
->uio_offset
> oldEOF
) {
2850 oldEOF
= uio
->uio_offset
;
2853 retval
= cluster_write_copy(vp
, uio
, io_req_size
, oldEOF
, newEOF
, (off_t
)0, (off_t
)0, flags
, callback
, callback_arg
);
2855 *write_type
= IO_UNKNOWN
;
2857 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
2858 (int)uio
->uio_offset
, io_req_size
, retval
, 4, 0);
2865 cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
2866 int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
)
2868 upl_page_info_t
*pl
;
2869 addr64_t src_paddr
= 0;
2870 upl_t upl
[MAX_VECTS
];
2871 vm_offset_t upl_offset
;
2872 u_int32_t tail_size
= 0;
2875 upl_size_t upl_size
;
2876 vm_size_t upl_needed_size
;
2877 mach_msg_type_number_t pages_in_pl
;
2878 upl_control_flags_t upl_flags
;
2880 struct clios iostate
;
2885 user_addr_t iov_base
;
2886 u_int32_t devblocksize
;
2887 u_int32_t mem_alignment_mask
;
2890 * When we enter this routine, we know
2891 * -- the io_req_size will not exceed iov_len
2892 * -- the target address is physically contiguous
2894 cluster_syncup(vp
, newEOF
, callback
, callback_arg
, callback
? PUSH_SYNC
: 0);
2896 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
2897 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
2899 iostate
.io_completed
= 0;
2900 iostate
.io_issued
= 0;
2901 iostate
.io_error
= 0;
2902 iostate
.io_wanted
= 0;
2904 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
2907 io_size
= *write_length
;
2909 iov_base
= uio_curriovbase(uio
);
2911 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
2912 upl_needed_size
= upl_offset
+ io_size
;
2915 upl_size
= upl_needed_size
;
2916 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
2917 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
2919 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
2920 kret
= vm_map_get_upl(map
,
2921 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2922 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, VM_KERN_MEMORY_FILE
, 0);
2924 if (kret
!= KERN_SUCCESS
) {
2926 * failed to get pagelist
2929 goto wait_for_cwrites
;
2934 * Consider the possibility that upl_size wasn't satisfied.
2936 if (upl_size
< upl_needed_size
) {
2938 * This is a failure in the physical memory case.
2941 goto wait_for_cwrites
;
2943 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
2945 src_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + (addr64_t
)upl_offset
;
2947 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
2948 u_int32_t head_size
;
2950 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
2952 if (head_size
> io_size
) {
2953 head_size
= io_size
;
2956 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, head_size
, 0, callback
, callback_arg
);
2959 goto wait_for_cwrites
;
2962 upl_offset
+= head_size
;
2963 src_paddr
+= head_size
;
2964 io_size
-= head_size
;
2966 iov_base
+= head_size
;
2968 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
2970 * request doesn't set up on a memory boundary
2971 * the underlying DMA engine can handle...
2972 * return an error instead of going through
2973 * the slow copy path since the intent of this
2974 * path is direct I/O from device memory
2977 goto wait_for_cwrites
;
2980 tail_size
= io_size
& (devblocksize
- 1);
2981 io_size
-= tail_size
;
2983 while (io_size
&& error
== 0) {
2984 if (io_size
> MAX_IO_CONTIG_SIZE
) {
2985 xsize
= MAX_IO_CONTIG_SIZE
;
2990 * request asynchronously so that we can overlap
2991 * the preparation of the next I/O... we'll do
2992 * the commit after all the I/O has completed
2993 * since its all issued against the same UPL
2994 * if there are already too many outstanding writes
2995 * wait until some have completed before issuing the next
2997 cluster_iostate_wait(&iostate
, MAX_IO_CONTIG_SIZE
* IO_SCALE(vp
, 2), "cluster_write_contig");
2999 if (iostate
.io_error
) {
3001 * one of the earlier writes we issued ran into a hard error
3002 * don't issue any more writes...
3003 * go wait for all writes that are part of this stream
3004 * to complete before returning the error to the caller
3006 goto wait_for_cwrites
;
3009 * issue an asynchronous write to cluster_io
3011 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
,
3012 xsize
, CL_DEV_MEMORY
| CL_ASYNC
| bflag
, (buf_t
)NULL
, (struct clios
*)&iostate
, callback
, callback_arg
);
3016 * The cluster_io write completed successfully,
3017 * update the uio structure
3019 uio_update(uio
, (user_size_t
)xsize
);
3021 upl_offset
+= xsize
;
3026 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
) {
3027 error
= cluster_io_type(uio
, write_type
, write_length
, 0);
3029 if (error
== 0 && *write_type
== IO_CONTIG
) {
3034 *write_type
= IO_UNKNOWN
;
3039 * make sure all async writes that are part of this stream
3040 * have completed before we proceed
3042 cluster_iostate_wait(&iostate
, 0, "cluster_write_contig");
3044 if (iostate
.io_error
) {
3045 error
= iostate
.io_error
;
3048 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
3050 if (error
== 0 && tail_size
) {
3051 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, tail_size
, 0, callback
, callback_arg
);
3054 for (n
= 0; n
< num_upl
; n
++) {
3056 * just release our hold on each physically contiguous
3057 * region without changing any state
3059 ubc_upl_abort(upl
[n
], 0);
3067 * need to avoid a race between an msync of a range of pages dirtied via mmap
3068 * vs a filesystem such as HFS deciding to write a 'hole' to disk via cluster_write's
3069 * zerofill mechanism before it has seen the VNOP_PAGEOUTs for the pages being msync'd
3071 * we should never force-zero-fill pages that are already valid in the cache...
3072 * the entire page contains valid data (either from disk, zero-filled or dirtied
3073 * via an mmap) so we can only do damage by trying to zero-fill
3077 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
)
3080 boolean_t need_cluster_zero
= TRUE
;
3082 if ((flags
& (IO_NOZEROVALID
| IO_NOZERODIRTY
))) {
3083 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off
& PAGE_MASK_64
));
3084 zero_pg_index
= (int)((zero_off
- upl_f_offset
) / PAGE_SIZE_64
);
3086 if (upl_valid_page(pl
, zero_pg_index
)) {
3088 * never force zero valid pages - dirty or clean
3089 * we'll leave these in the UPL for cluster_write_copy to deal with
3091 need_cluster_zero
= FALSE
;
3094 if (need_cluster_zero
== TRUE
) {
3095 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
3098 return bytes_to_zero
;
3103 cluster_update_state(vnode_t vp
, vm_object_offset_t s_offset
, vm_object_offset_t e_offset
, boolean_t vm_initiated
)
3105 struct cl_extent cl
;
3106 boolean_t first_pass
= TRUE
;
3108 assert(s_offset
< e_offset
);
3109 assert((s_offset
& PAGE_MASK_64
) == 0);
3110 assert((e_offset
& PAGE_MASK_64
) == 0);
3112 cl
.b_addr
= (daddr64_t
)(s_offset
/ PAGE_SIZE_64
);
3113 cl
.e_addr
= (daddr64_t
)(e_offset
/ PAGE_SIZE_64
);
3115 cluster_update_state_internal(vp
, &cl
, 0, TRUE
, &first_pass
, s_offset
, (int)(e_offset
- s_offset
),
3116 vp
->v_un
.vu_ubcinfo
->ui_size
, NULL
, NULL
, vm_initiated
);
3121 cluster_update_state_internal(vnode_t vp
, struct cl_extent
*cl
, int flags
, boolean_t defer_writes
,
3122 boolean_t
*first_pass
, off_t write_off
, int write_cnt
, off_t newEOF
,
3123 int (*callback
)(buf_t
, void *), void *callback_arg
, boolean_t vm_initiated
)
3125 struct cl_writebehind
*wbp
;
3127 int ret_cluster_try_push
;
3128 u_int max_cluster_pgcount
;
3131 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
3134 * take the lock to protect our accesses
3135 * of the writebehind and sparse cluster state
3137 wbp
= cluster_get_wbp(vp
, CLW_ALLOCATE
| CLW_RETURNLOCKED
);
3139 if (wbp
->cl_scmap
) {
3140 if (!(flags
& IO_NOCACHE
)) {
3142 * we've fallen into the sparse
3143 * cluster method of delaying dirty pages
3145 sparse_cluster_add(wbp
, &(wbp
->cl_scmap
), vp
, cl
, newEOF
, callback
, callback_arg
, vm_initiated
);
3147 lck_mtx_unlock(&wbp
->cl_lockw
);
3151 * must have done cached writes that fell into
3152 * the sparse cluster mechanism... we've switched
3153 * to uncached writes on the file, so go ahead
3154 * and push whatever's in the sparse map
3155 * and switch back to normal clustering
3159 sparse_cluster_push(wbp
, &(wbp
->cl_scmap
), vp
, newEOF
, PUSH_ALL
, 0, callback
, callback_arg
, vm_initiated
);
3161 * no clusters of either type present at this point
3162 * so just go directly to start_new_cluster since
3163 * we know we need to delay this I/O since we've
3164 * already released the pages back into the cache
3165 * to avoid the deadlock with sparse_cluster_push
3167 goto start_new_cluster
;
3169 if (*first_pass
== TRUE
) {
3170 if (write_off
== wbp
->cl_last_write
) {
3171 wbp
->cl_seq_written
+= write_cnt
;
3173 wbp
->cl_seq_written
= write_cnt
;
3176 wbp
->cl_last_write
= write_off
+ write_cnt
;
3178 *first_pass
= FALSE
;
3180 if (wbp
->cl_number
== 0) {
3182 * no clusters currently present
3184 goto start_new_cluster
;
3187 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
3189 * check each cluster that we currently hold
3190 * try to merge some or all of this write into
3191 * one or more of the existing clusters... if
3192 * any portion of the write remains, start a
3195 if (cl
->b_addr
>= wbp
->cl_clusters
[cl_index
].b_addr
) {
3197 * the current write starts at or after the current cluster
3199 if (cl
->e_addr
<= (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
3201 * we have a write that fits entirely
3202 * within the existing cluster limits
3204 if (cl
->e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
) {
3206 * update our idea of where the cluster ends
3208 wbp
->cl_clusters
[cl_index
].e_addr
= cl
->e_addr
;
3212 if (cl
->b_addr
< (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
3214 * we have a write that starts in the middle of the current cluster
3215 * but extends beyond the cluster's limit... we know this because
3216 * of the previous checks
3217 * we'll extend the current cluster to the max
3218 * and update the b_addr for the current write to reflect that
3219 * the head of it was absorbed into this cluster...
3220 * note that we'll always have a leftover tail in this case since
3221 * full absorbtion would have occurred in the clause above
3223 wbp
->cl_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
;
3225 cl
->b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
;
3228 * we come here for the case where the current write starts
3229 * beyond the limit of the existing cluster or we have a leftover
3230 * tail after a partial absorbtion
3232 * in either case, we'll check the remaining clusters before
3233 * starting a new one
3237 * the current write starts in front of the cluster we're currently considering
3239 if ((wbp
->cl_clusters
[cl_index
].e_addr
- cl
->b_addr
) <= max_cluster_pgcount
) {
3241 * we can just merge the new request into
3242 * this cluster and leave it in the cache
3243 * since the resulting cluster is still
3244 * less than the maximum allowable size
3246 wbp
->cl_clusters
[cl_index
].b_addr
= cl
->b_addr
;
3248 if (cl
->e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
) {
3250 * the current write completely
3251 * envelops the existing cluster and since
3252 * each write is limited to at most max_cluster_pgcount pages
3253 * we can just use the start and last blocknos of the write
3254 * to generate the cluster limits
3256 wbp
->cl_clusters
[cl_index
].e_addr
= cl
->e_addr
;
3261 * if we were to combine this write with the current cluster
3262 * we would exceed the cluster size limit.... so,
3263 * let's see if there's any overlap of the new I/O with
3264 * the cluster we're currently considering... in fact, we'll
3265 * stretch the cluster out to it's full limit and see if we
3266 * get an intersection with the current write
3269 if (cl
->e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
) {
3271 * the current write extends into the proposed cluster
3272 * clip the length of the current write after first combining it's
3273 * tail with the newly shaped cluster
3275 wbp
->cl_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
;
3277 cl
->e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
;
3280 * if we get here, there was no way to merge
3281 * any portion of this write with this cluster
3282 * or we could only merge part of it which
3283 * will leave a tail...
3284 * we'll check the remaining clusters before starting a new one
3288 if (cl_index
< wbp
->cl_number
) {
3290 * we found an existing cluster(s) that we
3291 * could entirely merge this I/O into
3296 if (defer_writes
== FALSE
&&
3297 wbp
->cl_number
== MAX_CLUSTERS
&&
3298 wbp
->cl_seq_written
>= (MAX_CLUSTERS
* (max_cluster_pgcount
* PAGE_SIZE
))) {
3301 if (vp
->v_mount
->mnt_minsaturationbytecount
) {
3302 n
= vp
->v_mount
->mnt_minsaturationbytecount
/ MAX_CLUSTER_SIZE(vp
);
3304 if (n
> MAX_CLUSTERS
) {
3312 if (disk_conditioner_mount_is_ssd(vp
->v_mount
)) {
3313 n
= WRITE_BEHIND_SSD
;
3319 cluster_try_push(wbp
, vp
, newEOF
, 0, 0, callback
, callback_arg
, NULL
, vm_initiated
);
3322 if (wbp
->cl_number
< MAX_CLUSTERS
) {
3324 * we didn't find an existing cluster to
3325 * merge into, but there's room to start
3328 goto start_new_cluster
;
3331 * no exisitng cluster to merge with and no
3332 * room to start a new one... we'll try
3333 * pushing one of the existing ones... if none of
3334 * them are able to be pushed, we'll switch
3335 * to the sparse cluster mechanism
3336 * cluster_try_push updates cl_number to the
3337 * number of remaining clusters... and
3338 * returns the number of currently unused clusters
3340 ret_cluster_try_push
= 0;
3343 * if writes are not deferred, call cluster push immediately
3345 if (defer_writes
== FALSE
) {
3346 ret_cluster_try_push
= cluster_try_push(wbp
, vp
, newEOF
, (flags
& IO_NOCACHE
) ? 0 : PUSH_DELAY
, 0, callback
, callback_arg
, NULL
, vm_initiated
);
3349 * execute following regardless of writes being deferred or not
3351 if (ret_cluster_try_push
== 0) {
3353 * no more room in the normal cluster mechanism
3354 * so let's switch to the more expansive but expensive
3355 * sparse mechanism....
3357 sparse_cluster_switch(wbp
, vp
, newEOF
, callback
, callback_arg
, vm_initiated
);
3358 sparse_cluster_add(wbp
, &(wbp
->cl_scmap
), vp
, cl
, newEOF
, callback
, callback_arg
, vm_initiated
);
3360 lck_mtx_unlock(&wbp
->cl_lockw
);
3364 wbp
->cl_clusters
[wbp
->cl_number
].b_addr
= cl
->b_addr
;
3365 wbp
->cl_clusters
[wbp
->cl_number
].e_addr
= cl
->e_addr
;
3367 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
= 0;
3369 if (flags
& IO_NOCACHE
) {
3370 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IONOCACHE
;
3373 if (flags
& IO_PASSIVE
) {
3374 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IOPASSIVE
;
3379 lck_mtx_unlock(&wbp
->cl_lockw
);
3385 cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
, off_t headOff
,
3386 off_t tailOff
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
3388 upl_page_info_t
*pl
;
3390 vm_offset_t upl_offset
= 0;
3403 long long total_size
;
3406 long long zero_cnt1
;
3408 off_t write_off
= 0;
3410 boolean_t first_pass
= FALSE
;
3411 struct cl_extent cl
;
3416 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
3417 (int)uio
->uio_offset
, io_req_size
, (int)oldEOF
, (int)newEOF
, 0);
3419 io_resid
= io_req_size
;
3421 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
3422 0, 0, (int)oldEOF
, (int)newEOF
, 0);
3426 if (flags
& IO_PASSIVE
) {
3431 if (flags
& IO_NOCACHE
) {
3432 bflag
|= CL_NOCACHE
;
3435 if (flags
& IO_SKIP_ENCRYPTION
) {
3436 bflag
|= CL_ENCRYPTED
;
3444 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
3446 if (flags
& IO_HEADZEROFILL
) {
3448 * some filesystems (HFS is one) don't support unallocated holes within a file...
3449 * so we zero fill the intervening space between the old EOF and the offset
3450 * where the next chunk of real data begins.... ftruncate will also use this
3451 * routine to zero fill to the new EOF when growing a file... in this case, the
3452 * uio structure will not be provided
3455 if (headOff
< uio
->uio_offset
) {
3456 zero_cnt
= uio
->uio_offset
- headOff
;
3459 } else if (headOff
< newEOF
) {
3460 zero_cnt
= newEOF
- headOff
;
3464 if (uio
&& uio
->uio_offset
> oldEOF
) {
3465 zero_off
= uio
->uio_offset
& ~PAGE_MASK_64
;
3467 if (zero_off
>= oldEOF
) {
3468 zero_cnt
= uio
->uio_offset
- zero_off
;
3470 flags
|= IO_HEADZEROFILL
;
3474 if (flags
& IO_TAILZEROFILL
) {
3476 zero_off1
= uio
->uio_offset
+ io_req_size
;
3478 if (zero_off1
< tailOff
) {
3479 zero_cnt1
= tailOff
- zero_off1
;
3483 if (uio
&& newEOF
> oldEOF
) {
3484 zero_off1
= uio
->uio_offset
+ io_req_size
;
3486 if (zero_off1
== newEOF
&& (zero_off1
& PAGE_MASK_64
)) {
3487 zero_cnt1
= PAGE_SIZE_64
- (zero_off1
& PAGE_MASK_64
);
3489 flags
|= IO_TAILZEROFILL
;
3493 if (zero_cnt
== 0 && uio
== (struct uio
*) 0) {
3494 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
3495 retval
, 0, 0, 0, 0);
3499 write_off
= uio
->uio_offset
;
3500 write_cnt
= uio_resid(uio
);
3502 * delay updating the sequential write info
3503 * in the control block until we've obtained
3508 while ((total_size
= (io_resid
+ zero_cnt
+ zero_cnt1
)) && retval
== 0) {
3510 * for this iteration of the loop, figure out where our starting point is
3513 start_offset
= (int)(zero_off
& PAGE_MASK_64
);
3514 upl_f_offset
= zero_off
- start_offset
;
3515 } else if (io_resid
) {
3516 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3517 upl_f_offset
= uio
->uio_offset
- start_offset
;
3519 start_offset
= (int)(zero_off1
& PAGE_MASK_64
);
3520 upl_f_offset
= zero_off1
- start_offset
;
3522 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 46)) | DBG_FUNC_NONE
,
3523 (int)zero_off
, (int)zero_cnt
, (int)zero_off1
, (int)zero_cnt1
, 0);
3525 if (total_size
> max_io_size
) {
3526 total_size
= max_io_size
;
3529 cl
.b_addr
= (daddr64_t
)(upl_f_offset
/ PAGE_SIZE_64
);
3531 if (uio
&& ((flags
& (IO_SYNC
| IO_HEADZEROFILL
| IO_TAILZEROFILL
)) == 0)) {
3533 * assumption... total_size <= io_resid
3534 * because IO_HEADZEROFILL and IO_TAILZEROFILL not set
3536 if ((start_offset
+ total_size
) > max_io_size
) {
3537 total_size
= max_io_size
- start_offset
;
3539 xfer_resid
= total_size
;
3541 retval
= cluster_copy_ubc_data_internal(vp
, uio
, &xfer_resid
, 1, 1);
3547 io_resid
-= (total_size
- xfer_resid
);
3548 total_size
= xfer_resid
;
3549 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3550 upl_f_offset
= uio
->uio_offset
- start_offset
;
3552 if (total_size
== 0) {
3555 * the write did not finish on a page boundary
3556 * which will leave upl_f_offset pointing to the
3557 * beginning of the last page written instead of
3558 * the page beyond it... bump it in this case
3559 * so that the cluster code records the last page
3562 upl_f_offset
+= PAGE_SIZE_64
;
3570 * compute the size of the upl needed to encompass
3571 * the requested write... limit each call to cluster_io
3572 * to the maximum UPL size... cluster_io will clip if
3573 * this exceeds the maximum io_size for the device,
3574 * make sure to account for
3575 * a starting offset that's not page aligned
3577 upl_size
= (start_offset
+ total_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
3579 if (upl_size
> max_io_size
) {
3580 upl_size
= max_io_size
;
3583 pages_in_upl
= upl_size
/ PAGE_SIZE
;
3584 io_size
= upl_size
- start_offset
;
3586 if ((long long)io_size
> total_size
) {
3587 io_size
= total_size
;
3590 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, io_size
, total_size
, 0, 0);
3594 * Gather the pages from the buffer cache.
3595 * The UPL_WILL_MODIFY flag lets the UPL subsystem know
3596 * that we intend to modify these pages.
3598 kret
= ubc_create_upl_kernel(vp
,
3603 UPL_SET_LITE
| ((uio
!= NULL
&& (uio
->uio_flags
& UIO_FLAGS_IS_COMPRESSED_FILE
)) ? 0 : UPL_WILL_MODIFY
),
3604 VM_KERN_MEMORY_FILE
);
3605 if (kret
!= KERN_SUCCESS
) {
3606 panic("cluster_write_copy: failed to get pagelist");
3609 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
,
3610 upl
, (int)upl_f_offset
, start_offset
, 0, 0);
3612 if (start_offset
&& upl_f_offset
< oldEOF
&& !upl_valid_page(pl
, 0)) {
3616 * we're starting in the middle of the first page of the upl
3617 * and the page isn't currently valid, so we're going to have
3618 * to read it in first... this is a synchronous operation
3620 read_size
= PAGE_SIZE
;
3622 if ((upl_f_offset
+ read_size
) > oldEOF
) {
3623 read_size
= oldEOF
- upl_f_offset
;
3626 retval
= cluster_io(vp
, upl
, 0, upl_f_offset
, read_size
,
3627 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
3630 * we had an error during the read which causes us to abort
3631 * the current cluster_write request... before we do, we need
3632 * to release the rest of the pages in the upl without modifying
3633 * there state and mark the failed page in error
3635 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3637 if (upl_size
> PAGE_SIZE
) {
3638 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3641 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
3642 upl
, 0, 0, retval
, 0);
3646 if ((start_offset
== 0 || upl_size
> PAGE_SIZE
) && ((start_offset
+ io_size
) & PAGE_MASK
)) {
3648 * the last offset we're writing to in this upl does not end on a page
3649 * boundary... if it's not beyond the old EOF, then we'll also need to
3650 * pre-read this page in if it isn't already valid
3652 upl_offset
= upl_size
- PAGE_SIZE
;
3654 if ((upl_f_offset
+ start_offset
+ io_size
) < oldEOF
&&
3655 !upl_valid_page(pl
, upl_offset
/ PAGE_SIZE
)) {
3658 read_size
= PAGE_SIZE
;
3660 if ((off_t
)(upl_f_offset
+ upl_offset
+ read_size
) > oldEOF
) {
3661 read_size
= oldEOF
- (upl_f_offset
+ upl_offset
);
3664 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, read_size
,
3665 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
3668 * we had an error during the read which causes us to abort
3669 * the current cluster_write request... before we do, we
3670 * need to release the rest of the pages in the upl without
3671 * modifying there state and mark the failed page in error
3673 ubc_upl_abort_range(upl
, upl_offset
, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3675 if (upl_size
> PAGE_SIZE
) {
3676 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3679 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
3680 upl
, 0, 0, retval
, 0);
3685 xfer_resid
= io_size
;
3686 io_offset
= start_offset
;
3688 while (zero_cnt
&& xfer_resid
) {
3689 if (zero_cnt
< (long long)xfer_resid
) {
3690 bytes_to_zero
= zero_cnt
;
3692 bytes_to_zero
= xfer_resid
;
3695 bytes_to_zero
= cluster_zero_range(upl
, pl
, flags
, io_offset
, zero_off
, upl_f_offset
, bytes_to_zero
);
3697 xfer_resid
-= bytes_to_zero
;
3698 zero_cnt
-= bytes_to_zero
;
3699 zero_off
+= bytes_to_zero
;
3700 io_offset
+= bytes_to_zero
;
3702 if (xfer_resid
&& io_resid
) {
3703 u_int32_t io_requested
;
3705 bytes_to_move
= min(io_resid
, xfer_resid
);
3706 io_requested
= bytes_to_move
;
3708 retval
= cluster_copy_upl_data(uio
, upl
, io_offset
, (int *)&io_requested
);
3711 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3713 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
3714 upl
, 0, 0, retval
, 0);
3716 io_resid
-= bytes_to_move
;
3717 xfer_resid
-= bytes_to_move
;
3718 io_offset
+= bytes_to_move
;
3721 while (xfer_resid
&& zero_cnt1
&& retval
== 0) {
3722 if (zero_cnt1
< (long long)xfer_resid
) {
3723 bytes_to_zero
= zero_cnt1
;
3725 bytes_to_zero
= xfer_resid
;
3728 bytes_to_zero
= cluster_zero_range(upl
, pl
, flags
, io_offset
, zero_off1
, upl_f_offset
, bytes_to_zero
);
3730 xfer_resid
-= bytes_to_zero
;
3731 zero_cnt1
-= bytes_to_zero
;
3732 zero_off1
+= bytes_to_zero
;
3733 io_offset
+= bytes_to_zero
;
3738 io_size
+= start_offset
;
3740 /* Force more restrictive zeroing behavior only on APFS */
3741 if ((vnode_tag(vp
) == VT_APFS
) && (newEOF
< oldEOF
)) {
3745 if (do_zeroing
&& (upl_f_offset
+ io_size
) >= newEOF
&& (u_int
)io_size
< upl_size
) {
3747 * if we're extending the file with this write
3748 * we'll zero fill the rest of the page so that
3749 * if the file gets extended again in such a way as to leave a
3750 * hole starting at this EOF, we'll have zero's in the correct spot
3752 cluster_zero(upl
, io_size
, upl_size
- io_size
, NULL
);
3755 * release the upl now if we hold one since...
3756 * 1) pages in it may be present in the sparse cluster map
3757 * and may span 2 separate buckets there... if they do and
3758 * we happen to have to flush a bucket to make room and it intersects
3759 * this upl, a deadlock may result on page BUSY
3760 * 2) we're delaying the I/O... from this point forward we're just updating
3761 * the cluster state... no need to hold the pages, so commit them
3762 * 3) IO_SYNC is set...
3763 * because we had to ask for a UPL that provides currenty non-present pages, the
3764 * UPL has been automatically set to clear the dirty flags (both software and hardware)
3765 * upon committing it... this is not the behavior we want since it's possible for
3766 * pages currently present as part of a mapped file to be dirtied while the I/O is in flight.
3767 * we'll pick these pages back up later with the correct behavior specified.
3768 * 4) we don't want to hold pages busy in a UPL and then block on the cluster lock... if a flush
3769 * of this vnode is in progress, we will deadlock if the pages being flushed intersect the pages
3770 * we hold since the flushing context is holding the cluster lock.
3772 ubc_upl_commit_range(upl
, 0, upl_size
,
3773 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
3776 * calculate the last logical block number
3777 * that this delayed I/O encompassed
3779 cl
.e_addr
= (daddr64_t
)((upl_f_offset
+ (off_t
)upl_size
) / PAGE_SIZE_64
);
3781 if (flags
& IO_SYNC
) {
3783 * if the IO_SYNC flag is set than we need to bypass
3784 * any clustering and immediately issue the I/O
3786 * we don't hold the lock at this point
3788 * we've already dropped the current upl, so pick it back up with COPYOUT_FROM set
3789 * so that we correctly deal with a change in state of the hardware modify bit...
3790 * we do this via cluster_push_now... by passing along the IO_SYNC flag, we force
3791 * cluster_push_now to wait until all the I/Os have completed... cluster_push_now is also
3792 * responsible for generating the correct sized I/O(s)
3794 retval
= cluster_push_now(vp
, &cl
, newEOF
, flags
, callback
, callback_arg
, FALSE
);
3796 boolean_t defer_writes
= FALSE
;
3798 if (vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) {
3799 defer_writes
= TRUE
;
3802 cluster_update_state_internal(vp
, &cl
, flags
, defer_writes
, &first_pass
,
3803 write_off
, write_cnt
, newEOF
, callback
, callback_arg
, FALSE
);
3807 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
, retval
, 0, io_resid
, 0, 0);
3815 cluster_read(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
)
3817 return cluster_read_ext(vp
, uio
, filesize
, xflags
, NULL
, NULL
);
3822 cluster_read_ext(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
3826 user_ssize_t cur_resid
;
3828 u_int32_t read_length
= 0;
3829 int read_type
= IO_COPY
;
3833 if (vp
->v_flag
& VNOCACHE_DATA
) {
3834 flags
|= IO_NOCACHE
;
3836 if ((vp
->v_flag
& VRAOFF
) || speculative_reads_disabled
) {
3840 if (flags
& IO_SKIP_ENCRYPTION
) {
3841 flags
|= IO_ENCRYPTED
;
3845 * do a read through the cache if one of the following is true....
3846 * NOCACHE is not true
3847 * the uio request doesn't target USERSPACE
3848 * Alternatively, if IO_ENCRYPTED is set, then we want to bypass the cache as well.
3849 * Reading encrypted data from a CP filesystem should never result in the data touching
3852 * otherwise, find out if we want the direct or contig variant for
3853 * the first vector in the uio request
3855 if (((flags
& IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
)) || (flags
& IO_ENCRYPTED
)) {
3856 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3859 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< filesize
&& retval
== 0) {
3860 switch (read_type
) {
3863 * make sure the uio_resid isn't too big...
3864 * internally, we want to handle all of the I/O in
3865 * chunk sizes that fit in a 32 bit int
3867 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
)) {
3868 io_size
= MAX_IO_REQUEST_SIZE
;
3870 io_size
= (u_int32_t
)cur_resid
;
3873 retval
= cluster_read_copy(vp
, uio
, io_size
, filesize
, flags
, callback
, callback_arg
);
3877 retval
= cluster_read_direct(vp
, uio
, filesize
, &read_type
, &read_length
, flags
, callback
, callback_arg
);
3881 retval
= cluster_read_contig(vp
, uio
, filesize
, &read_type
, &read_length
, callback
, callback_arg
, flags
);
3885 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3895 cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int take_reference
)
3898 int abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
3900 if ((range
= last_pg
- start_pg
)) {
3901 if (take_reference
) {
3902 abort_flags
|= UPL_ABORT_REFERENCE
;
3905 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, range
* PAGE_SIZE
, abort_flags
);
3911 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
)
3913 upl_page_info_t
*pl
;
3915 vm_offset_t upl_offset
;
3924 off_t last_ioread_offset
;
3925 off_t last_request_offset
;
3929 u_int32_t size_of_prefetch
;
3932 u_int32_t max_rd_size
;
3933 u_int32_t max_io_size
;
3934 u_int32_t max_prefetch
;
3935 u_int rd_ahead_enabled
= 1;
3936 u_int prefetch_enabled
= 1;
3937 struct cl_readahead
* rap
;
3938 struct clios iostate
;
3939 struct cl_extent extent
;
3941 int take_reference
= 1;
3942 int policy
= IOPOL_DEFAULT
;
3943 boolean_t iolock_inited
= FALSE
;
3945 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_START
,
3946 (int)uio
->uio_offset
, io_req_size
, (int)filesize
, flags
, 0);
3948 if (flags
& IO_ENCRYPTED
) {
3949 panic("encrypted blocks will hit UBC!");
3952 policy
= throttle_get_io_policy(NULL
);
3954 if (policy
== THROTTLE_LEVEL_TIER3
|| policy
== THROTTLE_LEVEL_TIER2
|| (flags
& IO_NOCACHE
)) {
3958 if (flags
& IO_PASSIVE
) {
3964 if (flags
& IO_NOCACHE
) {
3965 bflag
|= CL_NOCACHE
;
3968 if (flags
& IO_SKIP_ENCRYPTION
) {
3969 bflag
|= CL_ENCRYPTED
;
3972 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
3973 max_prefetch
= MAX_PREFETCH(vp
, max_io_size
, disk_conditioner_mount_is_ssd(vp
->v_mount
));
3974 max_rd_size
= max_prefetch
;
3976 last_request_offset
= uio
->uio_offset
+ io_req_size
;
3978 if (last_request_offset
> filesize
) {
3979 last_request_offset
= filesize
;
3982 if ((flags
& (IO_RAOFF
| IO_NOCACHE
)) || ((last_request_offset
& ~PAGE_MASK_64
) == (uio
->uio_offset
& ~PAGE_MASK_64
))) {
3983 rd_ahead_enabled
= 0;
3986 if (cluster_is_throttled(vp
)) {
3988 * we're in the throttle window, at the very least
3989 * we want to limit the size of the I/O we're about
3992 rd_ahead_enabled
= 0;
3993 prefetch_enabled
= 0;
3995 max_rd_size
= THROTTLE_MAX_IOSIZE
;
3997 if ((rap
= cluster_get_rap(vp
)) == NULL
) {
3998 rd_ahead_enabled
= 0;
4000 extent
.b_addr
= uio
->uio_offset
/ PAGE_SIZE_64
;
4001 extent
.e_addr
= (last_request_offset
- 1) / PAGE_SIZE_64
;
4004 if (rap
!= NULL
&& rap
->cl_ralen
&& (rap
->cl_lastr
== extent
.b_addr
|| (rap
->cl_lastr
+ 1) == extent
.b_addr
)) {
4006 * determine if we already have a read-ahead in the pipe courtesy of the
4007 * last read systemcall that was issued...
4008 * if so, pick up it's extent to determine where we should start
4009 * with respect to any read-ahead that might be necessary to
4010 * garner all the data needed to complete this read systemcall
4012 last_ioread_offset
= (rap
->cl_maxra
* PAGE_SIZE_64
) + PAGE_SIZE_64
;
4014 if (last_ioread_offset
< uio
->uio_offset
) {
4015 last_ioread_offset
= (off_t
)0;
4016 } else if (last_ioread_offset
> last_request_offset
) {
4017 last_ioread_offset
= last_request_offset
;
4020 last_ioread_offset
= (off_t
)0;
4023 while (io_req_size
&& uio
->uio_offset
< filesize
&& retval
== 0) {
4024 max_size
= filesize
- uio
->uio_offset
;
4026 if ((off_t
)(io_req_size
) < max_size
) {
4027 io_size
= io_req_size
;
4032 if (!(flags
& IO_NOCACHE
)) {
4035 u_int32_t io_requested
;
4038 * if we keep finding the pages we need already in the cache, then
4039 * don't bother to call cluster_read_prefetch since it costs CPU cycles
4040 * to determine that we have all the pages we need... once we miss in
4041 * the cache and have issued an I/O, than we'll assume that we're likely
4042 * to continue to miss in the cache and it's to our advantage to try and prefetch
4044 if (last_request_offset
&& last_ioread_offset
&& (size_of_prefetch
= (last_request_offset
- last_ioread_offset
))) {
4045 if ((last_ioread_offset
- uio
->uio_offset
) <= max_rd_size
&& prefetch_enabled
) {
4047 * we've already issued I/O for this request and
4048 * there's still work to do and
4049 * our prefetch stream is running dry, so issue a
4050 * pre-fetch I/O... the I/O latency will overlap
4051 * with the copying of the data
4053 if (size_of_prefetch
> max_rd_size
) {
4054 size_of_prefetch
= max_rd_size
;
4057 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
4059 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
4061 if (last_ioread_offset
> last_request_offset
) {
4062 last_ioread_offset
= last_request_offset
;
4067 * limit the size of the copy we're about to do so that
4068 * we can notice that our I/O pipe is running dry and
4069 * get the next I/O issued before it does go dry
4071 if (last_ioread_offset
&& io_size
> (max_io_size
/ 4)) {
4072 io_resid
= (max_io_size
/ 4);
4077 io_requested
= io_resid
;
4079 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_resid
, 0, take_reference
);
4081 xsize
= io_requested
- io_resid
;
4084 io_req_size
-= xsize
;
4086 if (retval
|| io_resid
) {
4088 * if we run into a real error or
4089 * a page that is not in the cache
4090 * we need to leave streaming mode
4095 if (rd_ahead_enabled
&& (io_size
== 0 || last_ioread_offset
== last_request_offset
)) {
4097 * we're already finished the I/O for this read request
4098 * let's see if we should do a read-ahead
4100 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
4108 if (extent
.e_addr
< rap
->cl_lastr
) {
4111 rap
->cl_lastr
= extent
.e_addr
;
4116 * recompute max_size since cluster_copy_ubc_data_internal
4117 * may have advanced uio->uio_offset
4119 max_size
= filesize
- uio
->uio_offset
;
4122 iostate
.io_completed
= 0;
4123 iostate
.io_issued
= 0;
4124 iostate
.io_error
= 0;
4125 iostate
.io_wanted
= 0;
4127 if ((flags
& IO_RETURN_ON_THROTTLE
)) {
4128 if (cluster_is_throttled(vp
) == THROTTLE_NOW
) {
4129 if (!cluster_io_present_in_BC(vp
, uio
->uio_offset
)) {
4131 * we're in the throttle window and at least 1 I/O
4132 * has already been issued by a throttleable thread
4133 * in this window, so return with EAGAIN to indicate
4134 * to the FS issuing the cluster_read call that it
4135 * should now throttle after dropping any locks
4137 throttle_info_update_by_mount(vp
->v_mount
);
4146 * compute the size of the upl needed to encompass
4147 * the requested read... limit each call to cluster_io
4148 * to the maximum UPL size... cluster_io will clip if
4149 * this exceeds the maximum io_size for the device,
4150 * make sure to account for
4151 * a starting offset that's not page aligned
4153 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
4154 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
4156 if (io_size
> max_rd_size
) {
4157 io_size
= max_rd_size
;
4160 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
4162 if (flags
& IO_NOCACHE
) {
4163 if (upl_size
> max_io_size
) {
4164 upl_size
= max_io_size
;
4167 if (upl_size
> max_io_size
/ 4) {
4168 upl_size
= max_io_size
/ 4;
4169 upl_size
&= ~PAGE_MASK
;
4171 if (upl_size
== 0) {
4172 upl_size
= PAGE_SIZE
;
4176 pages_in_upl
= upl_size
/ PAGE_SIZE
;
4178 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_START
,
4179 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
4181 kret
= ubc_create_upl_kernel(vp
,
4186 UPL_FILE_IO
| UPL_SET_LITE
,
4187 VM_KERN_MEMORY_FILE
);
4188 if (kret
!= KERN_SUCCESS
) {
4189 panic("cluster_read_copy: failed to get pagelist");
4192 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_END
,
4193 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
4196 * scan from the beginning of the upl looking for the first
4197 * non-valid page.... this will become the first page in
4198 * the request we're going to make to 'cluster_io'... if all
4199 * of the pages are valid, we won't call through to 'cluster_io'
4201 for (start_pg
= 0; start_pg
< pages_in_upl
; start_pg
++) {
4202 if (!upl_valid_page(pl
, start_pg
)) {
4208 * scan from the starting invalid page looking for a valid
4209 * page before the end of the upl is reached, if we
4210 * find one, then it will be the last page of the request to
4213 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
4214 if (upl_valid_page(pl
, last_pg
)) {
4219 if (start_pg
< last_pg
) {
4221 * we found a range of 'invalid' pages that must be filled
4222 * if the last page in this range is the last page of the file
4223 * we may have to clip the size of it to keep from reading past
4224 * the end of the last physical block associated with the file
4226 if (iolock_inited
== FALSE
) {
4227 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
4229 iolock_inited
= TRUE
;
4231 upl_offset
= start_pg
* PAGE_SIZE
;
4232 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
4234 if ((off_t
)(upl_f_offset
+ upl_offset
+ io_size
) > filesize
) {
4235 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
4239 * issue an asynchronous read to cluster_io
4242 error
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
,
4243 io_size
, CL_READ
| CL_ASYNC
| bflag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4246 if (extent
.e_addr
< rap
->cl_maxra
) {
4248 * we've just issued a read for a block that should have been
4249 * in the cache courtesy of the read-ahead engine... something
4250 * has gone wrong with the pipeline, so reset the read-ahead
4251 * logic which will cause us to restart from scratch
4259 * if the read completed successfully, or there was no I/O request
4260 * issued, than copy the data into user land via 'cluster_upl_copy_data'
4261 * we'll first add on any 'valid'
4262 * pages that were present in the upl when we acquired it.
4266 for (uio_last
= last_pg
; uio_last
< pages_in_upl
; uio_last
++) {
4267 if (!upl_valid_page(pl
, uio_last
)) {
4271 if (uio_last
< pages_in_upl
) {
4273 * there were some invalid pages beyond the valid pages
4274 * that we didn't issue an I/O for, just release them
4275 * unchanged now, so that any prefetch/readahed can
4278 ubc_upl_abort_range(upl
, uio_last
* PAGE_SIZE
,
4279 (pages_in_upl
- uio_last
) * PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
4283 * compute size to transfer this round, if io_req_size is
4284 * still non-zero after this attempt, we'll loop around and
4285 * set up for another I/O.
4287 val_size
= (uio_last
* PAGE_SIZE
) - start_offset
;
4289 if (val_size
> max_size
) {
4290 val_size
= max_size
;
4293 if (val_size
> io_req_size
) {
4294 val_size
= io_req_size
;
4297 if ((uio
->uio_offset
+ val_size
) > last_ioread_offset
) {
4298 last_ioread_offset
= uio
->uio_offset
+ val_size
;
4301 if ((size_of_prefetch
= (last_request_offset
- last_ioread_offset
)) && prefetch_enabled
) {
4302 if ((last_ioread_offset
- (uio
->uio_offset
+ val_size
)) <= upl_size
) {
4304 * if there's still I/O left to do for this request, and...
4305 * we're not in hard throttle mode, and...
4306 * we're close to using up the previous prefetch, then issue a
4307 * new pre-fetch I/O... the I/O latency will overlap
4308 * with the copying of the data
4310 if (size_of_prefetch
> max_rd_size
) {
4311 size_of_prefetch
= max_rd_size
;
4314 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
4316 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
4318 if (last_ioread_offset
> last_request_offset
) {
4319 last_ioread_offset
= last_request_offset
;
4322 } else if ((uio
->uio_offset
+ val_size
) == last_request_offset
) {
4324 * this transfer will finish this request, so...
4325 * let's try to read ahead if we're in
4326 * a sequential access pattern and we haven't
4327 * explicitly disabled it
4329 if (rd_ahead_enabled
) {
4330 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
4334 if (extent
.e_addr
< rap
->cl_lastr
) {
4337 rap
->cl_lastr
= extent
.e_addr
;
4340 if (iolock_inited
== TRUE
) {
4341 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
4344 if (iostate
.io_error
) {
4345 error
= iostate
.io_error
;
4347 u_int32_t io_requested
;
4349 io_requested
= val_size
;
4351 retval
= cluster_copy_upl_data(uio
, upl
, start_offset
, (int *)&io_requested
);
4353 io_req_size
-= (val_size
- io_requested
);
4356 if (iolock_inited
== TRUE
) {
4357 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
4360 if (start_pg
< last_pg
) {
4362 * compute the range of pages that we actually issued an I/O for
4363 * and either commit them as valid if the I/O succeeded
4364 * or abort them if the I/O failed or we're not supposed to
4365 * keep them in the cache
4367 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
4369 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
, upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
4371 if (error
|| (flags
& IO_NOCACHE
)) {
4372 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
4373 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
4375 int commit_flags
= UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
;
4377 if (take_reference
) {
4378 commit_flags
|= UPL_COMMIT_INACTIVATE
;
4380 commit_flags
|= UPL_COMMIT_SPECULATE
;
4383 ubc_upl_commit_range(upl
, start_pg
* PAGE_SIZE
, io_size
, commit_flags
);
4385 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
4387 if ((last_pg
- start_pg
) < pages_in_upl
) {
4389 * the set of pages that we issued an I/O for did not encompass
4390 * the entire upl... so just release these without modifying
4394 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
4396 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
4397 upl
, -1, pages_in_upl
- (last_pg
- start_pg
), 0, 0);
4400 * handle any valid pages at the beginning of
4401 * the upl... release these appropriately
4403 cluster_read_upl_release(upl
, 0, start_pg
, take_reference
);
4406 * handle any valid pages immediately after the
4407 * pages we issued I/O for... ... release these appropriately
4409 cluster_read_upl_release(upl
, last_pg
, uio_last
, take_reference
);
4411 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, upl
, -1, -1, 0, 0);
4419 if (cluster_is_throttled(vp
)) {
4421 * we're in the throttle window, at the very least
4422 * we want to limit the size of the I/O we're about
4425 rd_ahead_enabled
= 0;
4426 prefetch_enabled
= 0;
4427 max_rd_size
= THROTTLE_MAX_IOSIZE
;
4429 if (max_rd_size
== THROTTLE_MAX_IOSIZE
) {
4431 * coming out of throttled state
4433 if (policy
!= THROTTLE_LEVEL_TIER3
&& policy
!= THROTTLE_LEVEL_TIER2
) {
4435 rd_ahead_enabled
= 1;
4437 prefetch_enabled
= 1;
4439 max_rd_size
= max_prefetch
;
4440 last_ioread_offset
= 0;
4445 if (iolock_inited
== TRUE
) {
4447 * cluster_io returned an error after it
4448 * had already issued some I/O. we need
4449 * to wait for that I/O to complete before
4450 * we can destroy the iostate mutex...
4451 * 'retval' already contains the early error
4452 * so no need to pick it up from iostate.io_error
4454 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
4456 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
4459 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
4460 (int)uio
->uio_offset
, io_req_size
, rap
->cl_lastr
, retval
, 0);
4462 lck_mtx_unlock(&rap
->cl_lockr
);
4464 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
4465 (int)uio
->uio_offset
, io_req_size
, 0, retval
, 0);
4472 * We don't want another read/write lock for every vnode in the system
4473 * so we keep a hash of them here. There should never be very many of
4474 * these around at any point in time.
4476 cl_direct_read_lock_t
*
4477 cluster_lock_direct_read(vnode_t vp
, lck_rw_type_t type
)
4479 struct cl_direct_read_locks
*head
4480 = &cl_direct_read_locks
[(uintptr_t)vp
/ sizeof(*vp
)
4481 % CL_DIRECT_READ_LOCK_BUCKETS
];
4483 struct cl_direct_read_lock
*lck
, *new_lck
= NULL
;
4486 lck_spin_lock(&cl_direct_read_spin_lock
);
4488 LIST_FOREACH(lck
, head
, chain
) {
4489 if (lck
->vp
== vp
) {
4491 lck_spin_unlock(&cl_direct_read_spin_lock
);
4493 // Someone beat us to it, ditch the allocation
4494 lck_rw_destroy(&new_lck
->rw_lock
, cl_mtx_grp
);
4495 FREE(new_lck
, M_TEMP
);
4497 lck_rw_lock(&lck
->rw_lock
, type
);
4503 // Use the lock we allocated
4504 LIST_INSERT_HEAD(head
, new_lck
, chain
);
4505 lck_spin_unlock(&cl_direct_read_spin_lock
);
4506 lck_rw_lock(&new_lck
->rw_lock
, type
);
4510 lck_spin_unlock(&cl_direct_read_spin_lock
);
4512 // Allocate a new lock
4513 MALLOC(new_lck
, cl_direct_read_lock_t
*, sizeof(*new_lck
),
4515 lck_rw_init(&new_lck
->rw_lock
, cl_mtx_grp
, cl_mtx_attr
);
4517 new_lck
->ref_count
= 1;
4519 // Got to go round again
4524 cluster_unlock_direct_read(cl_direct_read_lock_t
*lck
)
4526 lck_rw_done(&lck
->rw_lock
);
4528 lck_spin_lock(&cl_direct_read_spin_lock
);
4529 if (lck
->ref_count
== 1) {
4530 LIST_REMOVE(lck
, chain
);
4531 lck_spin_unlock(&cl_direct_read_spin_lock
);
4532 lck_rw_destroy(&lck
->rw_lock
, cl_mtx_grp
);
4536 lck_spin_unlock(&cl_direct_read_spin_lock
);
4541 cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
4542 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
4545 upl_page_info_t
*pl
;
4547 vm_offset_t upl_offset
, vector_upl_offset
= 0;
4548 upl_size_t upl_size
, vector_upl_size
= 0;
4549 vm_size_t upl_needed_size
;
4550 unsigned int pages_in_pl
;
4551 upl_control_flags_t upl_flags
;
4554 int force_data_sync
;
4556 int no_zero_fill
= 0;
4559 struct clios iostate
;
4560 user_addr_t iov_base
;
4561 u_int32_t io_req_size
;
4562 u_int32_t offset_in_file
;
4563 u_int32_t offset_in_iovbase
;
4567 u_int32_t devblocksize
;
4568 u_int32_t mem_alignment_mask
;
4569 u_int32_t max_upl_size
;
4570 u_int32_t max_rd_size
;
4571 u_int32_t max_rd_ahead
;
4572 u_int32_t max_vector_size
;
4573 boolean_t io_throttled
= FALSE
;
4575 u_int32_t vector_upl_iosize
= 0;
4576 int issueVectorUPL
= 0, useVectorUPL
= (uio
->uio_iovcnt
> 1);
4577 off_t v_upl_uio_offset
= 0;
4578 int vector_upl_index
= 0;
4579 upl_t vector_upl
= NULL
;
4580 cl_direct_read_lock_t
*lock
= NULL
;
4582 user_addr_t orig_iov_base
= 0;
4583 user_addr_t last_iov_base
= 0;
4584 user_addr_t next_iov_base
= 0;
4586 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_START
,
4587 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
4589 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
4591 max_rd_size
= max_upl_size
;
4592 max_rd_ahead
= max_rd_size
* IO_SCALE(vp
, 2);
4594 io_flag
= CL_COMMIT
| CL_READ
| CL_ASYNC
| CL_NOZERO
| CL_DIRECT_IO
;
4596 if (flags
& IO_PASSIVE
) {
4597 io_flag
|= CL_PASSIVE
;
4600 if (flags
& IO_ENCRYPTED
) {
4601 io_flag
|= CL_RAW_ENCRYPTED
;
4604 if (flags
& IO_NOCACHE
) {
4605 io_flag
|= CL_NOCACHE
;
4608 if (flags
& IO_SKIP_ENCRYPTION
) {
4609 io_flag
|= CL_ENCRYPTED
;
4612 iostate
.io_completed
= 0;
4613 iostate
.io_issued
= 0;
4614 iostate
.io_error
= 0;
4615 iostate
.io_wanted
= 0;
4617 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
4619 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
4620 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
4622 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
4623 (int)devblocksize
, (int)mem_alignment_mask
, 0, 0, 0);
4625 if (devblocksize
== 1) {
4627 * the AFP client advertises a devblocksize of 1
4628 * however, its BLOCKMAP routine maps to physical
4629 * blocks that are PAGE_SIZE in size...
4630 * therefore we can't ask for I/Os that aren't page aligned
4631 * or aren't multiples of PAGE_SIZE in size
4632 * by setting devblocksize to PAGE_SIZE, we re-instate
4633 * the old behavior we had before the mem_alignment_mask
4634 * changes went in...
4636 devblocksize
= PAGE_SIZE
;
4639 orig_iov_base
= uio_curriovbase(uio
);
4640 last_iov_base
= orig_iov_base
;
4643 io_req_size
= *read_length
;
4644 iov_base
= uio_curriovbase(uio
);
4646 offset_in_file
= (u_int32_t
)uio
->uio_offset
& (devblocksize
- 1);
4647 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
4649 if (offset_in_file
|| offset_in_iovbase
) {
4651 * one of the 2 important offsets is misaligned
4652 * so fire an I/O through the cache for this entire vector
4656 if (iov_base
& (devblocksize
- 1)) {
4658 * the offset in memory must be on a device block boundary
4659 * so that we can guarantee that we can generate an
4660 * I/O that ends on a page boundary in cluster_io
4665 max_io_size
= filesize
- uio
->uio_offset
;
4668 * The user must request IO in aligned chunks. If the
4669 * offset into the file is bad, or the userland pointer
4670 * is non-aligned, then we cannot service the encrypted IO request.
4672 if (flags
& IO_ENCRYPTED
) {
4673 if (misaligned
|| (io_req_size
& (devblocksize
- 1))) {
4677 max_io_size
= roundup(max_io_size
, devblocksize
);
4680 if ((off_t
)io_req_size
> max_io_size
) {
4681 io_req_size
= max_io_size
;
4685 * When we get to this point, we know...
4686 * -- the offset into the file is on a devblocksize boundary
4689 while (io_req_size
&& retval
== 0) {
4692 if (cluster_is_throttled(vp
)) {
4694 * we're in the throttle window, at the very least
4695 * we want to limit the size of the I/O we're about
4698 max_rd_size
= THROTTLE_MAX_IOSIZE
;
4699 max_rd_ahead
= THROTTLE_MAX_IOSIZE
- 1;
4700 max_vector_size
= THROTTLE_MAX_IOSIZE
;
4702 max_rd_size
= max_upl_size
;
4703 max_rd_ahead
= max_rd_size
* IO_SCALE(vp
, 2);
4704 max_vector_size
= MAX_VECTOR_UPL_SIZE
;
4706 io_start
= io_size
= io_req_size
;
4709 * First look for pages already in the cache
4710 * and move them to user space. But only do this
4711 * check if we are not retrieving encrypted data directly
4712 * from the filesystem; those blocks should never
4715 * cluster_copy_ubc_data returns the resid
4718 if ((flags
& IO_ENCRYPTED
) == 0) {
4719 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_size
, 0, 0);
4722 * calculate the number of bytes actually copied
4723 * starting size - residual
4725 xsize
= io_start
- io_size
;
4727 io_req_size
-= xsize
;
4729 if (useVectorUPL
&& (xsize
|| (iov_base
& PAGE_MASK
))) {
4731 * We found something in the cache or we have an iov_base that's not
4734 * Issue all I/O's that have been collected within this Vectored UPL.
4736 if (vector_upl_index
) {
4737 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
);
4738 reset_vector_run_state();
4746 * After this point, if we are using the Vector UPL path and the base is
4747 * not page-aligned then the UPL with that base will be the first in the vector UPL.
4752 * check to see if we are finished with this request.
4754 * If we satisfied this IO already, then io_req_size will be 0.
4755 * Otherwise, see if the IO was mis-aligned and needs to go through
4756 * the UBC to deal with the 'tail'.
4759 if (io_req_size
== 0 || (misaligned
)) {
4761 * see if there's another uio vector to
4762 * process that's of type IO_DIRECT
4764 * break out of while loop to get there
4769 * assume the request ends on a device block boundary
4771 io_min
= devblocksize
;
4774 * we can handle I/O's in multiples of the device block size
4775 * however, if io_size isn't a multiple of devblocksize we
4776 * want to clip it back to the nearest page boundary since
4777 * we are going to have to go through cluster_read_copy to
4778 * deal with the 'overhang'... by clipping it to a PAGE_SIZE
4779 * multiple, we avoid asking the drive for the same physical
4780 * blocks twice.. once for the partial page at the end of the
4781 * request and a 2nd time for the page we read into the cache
4782 * (which overlaps the end of the direct read) in order to
4783 * get at the overhang bytes
4785 if (io_size
& (devblocksize
- 1)) {
4786 assert(!(flags
& IO_ENCRYPTED
));
4788 * Clip the request to the previous page size boundary
4789 * since request does NOT end on a device block boundary
4791 io_size
&= ~PAGE_MASK
;
4794 if (retval
|| io_size
< io_min
) {
4796 * either an error or we only have the tail left to
4797 * complete via the copy path...
4798 * we may have already spun some portion of this request
4799 * off as async requests... we need to wait for the I/O
4800 * to complete before returning
4802 goto wait_for_dreads
;
4806 * Don't re-check the UBC data if we are looking for uncached IO
4807 * or asking for encrypted blocks.
4809 if ((flags
& IO_ENCRYPTED
) == 0) {
4810 if ((xsize
= io_size
) > max_rd_size
) {
4811 xsize
= max_rd_size
;
4818 * We hold a lock here between the time we check the
4819 * cache and the time we issue I/O. This saves us
4820 * from having to lock the pages in the cache. Not
4821 * all clients will care about this lock but some
4822 * clients may want to guarantee stability between
4823 * here and when the I/O is issued in which case they
4824 * will take the lock exclusively.
4826 lock
= cluster_lock_direct_read(vp
, LCK_RW_TYPE_SHARED
);
4829 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ xsize
, UPL_ROP_ABSENT
, (int *)&io_size
);
4833 * a page must have just come into the cache
4834 * since the first page in this range is no
4835 * longer absent, go back and re-evaluate
4840 if ((flags
& IO_RETURN_ON_THROTTLE
)) {
4841 if (cluster_is_throttled(vp
) == THROTTLE_NOW
) {
4842 if (!cluster_io_present_in_BC(vp
, uio
->uio_offset
)) {
4844 * we're in the throttle window and at least 1 I/O
4845 * has already been issued by a throttleable thread
4846 * in this window, so return with EAGAIN to indicate
4847 * to the FS issuing the cluster_read call that it
4848 * should now throttle after dropping any locks
4850 throttle_info_update_by_mount(vp
->v_mount
);
4852 io_throttled
= TRUE
;
4853 goto wait_for_dreads
;
4857 if (io_size
> max_rd_size
) {
4858 io_size
= max_rd_size
;
4861 iov_base
= uio_curriovbase(uio
);
4863 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
4864 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
4866 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_START
,
4867 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
4869 if (upl_offset
== 0 && ((io_size
& PAGE_MASK
) == 0)) {
4875 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
4876 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
4878 upl_size
= upl_needed_size
;
4879 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
4881 upl_flags
|= UPL_NOZEROFILL
;
4883 if (force_data_sync
) {
4884 upl_flags
|= UPL_FORCE_DATA_SYNC
;
4887 kret
= vm_map_create_upl(map
,
4888 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4889 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
, VM_KERN_MEMORY_FILE
);
4891 if (kret
!= KERN_SUCCESS
) {
4892 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4893 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4895 * failed to get pagelist
4897 * we may have already spun some portion of this request
4898 * off as async requests... we need to wait for the I/O
4899 * to complete before returning
4901 goto wait_for_dreads
;
4903 pages_in_pl
= upl_size
/ PAGE_SIZE
;
4904 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
4906 for (i
= 0; i
< pages_in_pl
; i
++) {
4907 if (!upl_page_present(pl
, i
)) {
4911 if (i
== pages_in_pl
) {
4915 ubc_upl_abort(upl
, 0);
4917 if (force_data_sync
>= 3) {
4918 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4919 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4921 goto wait_for_dreads
;
4924 * Consider the possibility that upl_size wasn't satisfied.
4926 if (upl_size
< upl_needed_size
) {
4927 if (upl_size
&& upl_offset
== 0) {
4934 ubc_upl_abort(upl
, 0);
4935 goto wait_for_dreads
;
4937 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4938 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4941 vm_offset_t end_off
= ((iov_base
+ io_size
) & PAGE_MASK
);
4946 * After this point, if we are using a vector UPL, then
4947 * either all the UPL elements end on a page boundary OR
4948 * this UPL is the last element because it does not end
4949 * on a page boundary.
4954 * request asynchronously so that we can overlap
4955 * the preparation of the next I/O
4956 * if there are already too many outstanding reads
4957 * wait until some have completed before issuing the next read
4959 cluster_iostate_wait(&iostate
, max_rd_ahead
, "cluster_read_direct");
4961 if (iostate
.io_error
) {
4963 * one of the earlier reads we issued ran into a hard error
4964 * don't issue any more reads, cleanup the UPL
4965 * that was just created but not used, then
4966 * go wait for any other reads to complete before
4967 * returning the error to the caller
4969 ubc_upl_abort(upl
, 0);
4971 goto wait_for_dreads
;
4973 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_START
,
4974 upl
, (int)upl_offset
, (int)uio
->uio_offset
, io_size
, 0);
4976 if (!useVectorUPL
) {
4978 io_flag
&= ~CL_PRESERVE
;
4980 io_flag
|= CL_PRESERVE
;
4983 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
, io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4985 if (!vector_upl_index
) {
4986 vector_upl
= vector_upl_create(upl_offset
);
4987 v_upl_uio_offset
= uio
->uio_offset
;
4988 vector_upl_offset
= upl_offset
;
4991 vector_upl_set_subupl(vector_upl
, upl
, upl_size
);
4992 vector_upl_set_iostate(vector_upl
, upl
, vector_upl_size
, upl_size
);
4994 vector_upl_size
+= upl_size
;
4995 vector_upl_iosize
+= io_size
;
4997 if (issueVectorUPL
|| vector_upl_index
== MAX_VECTOR_UPL_ELEMENTS
|| vector_upl_size
>= max_vector_size
) {
4998 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
);
4999 reset_vector_run_state();
5002 last_iov_base
= iov_base
+ io_size
;
5005 // We don't need to wait for the I/O to complete
5006 cluster_unlock_direct_read(lock
);
5011 * update the uio structure
5013 if ((flags
& IO_ENCRYPTED
) && (max_io_size
< io_size
)) {
5014 uio_update(uio
, (user_size_t
)max_io_size
);
5016 uio_update(uio
, (user_size_t
)io_size
);
5019 io_req_size
-= io_size
;
5021 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_END
,
5022 upl
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
5025 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0 && uio
->uio_offset
< filesize
) {
5026 retval
= cluster_io_type(uio
, read_type
, read_length
, 0);
5028 if (retval
== 0 && *read_type
== IO_DIRECT
) {
5029 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
5030 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
5038 if (retval
== 0 && iostate
.io_error
== 0 && useVectorUPL
&& vector_upl_index
) {
5039 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
);
5040 reset_vector_run_state();
5043 // We don't need to wait for the I/O to complete
5045 cluster_unlock_direct_read(lock
);
5049 * make sure all async reads that are part of this stream
5050 * have completed before we return
5052 cluster_iostate_wait(&iostate
, 0, "cluster_read_direct");
5054 if (iostate
.io_error
) {
5055 retval
= iostate
.io_error
;
5058 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
5060 if (io_throttled
== TRUE
&& retval
== 0) {
5064 for (next_iov_base
= orig_iov_base
; next_iov_base
< last_iov_base
; next_iov_base
+= PAGE_SIZE
) {
5066 * This is specifically done for pmap accounting purposes.
5067 * vm_pre_fault() will call vm_fault() to enter the page into
5068 * the pmap if there isn't _a_ physical page for that VA already.
5070 vm_pre_fault(vm_map_trunc_page(next_iov_base
, PAGE_MASK
));
5073 if (io_req_size
&& retval
== 0) {
5075 * we couldn't handle the tail of this request in DIRECT mode
5076 * so fire it through the copy path
5078 if (flags
& IO_ENCRYPTED
) {
5080 * We cannot fall back to the copy path for encrypted I/O. If this
5081 * happens, there is something wrong with the user buffer passed
5086 retval
= cluster_read_copy(vp
, uio
, io_req_size
, filesize
, flags
, callback
, callback_arg
);
5089 *read_type
= IO_UNKNOWN
;
5091 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
5092 (int)uio
->uio_offset
, (int)uio_resid(uio
), io_req_size
, retval
, 0);
5099 cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
5100 int (*callback
)(buf_t
, void *), void *callback_arg
, int flags
)
5102 upl_page_info_t
*pl
;
5103 upl_t upl
[MAX_VECTS
];
5104 vm_offset_t upl_offset
;
5105 addr64_t dst_paddr
= 0;
5106 user_addr_t iov_base
;
5108 upl_size_t upl_size
;
5109 vm_size_t upl_needed_size
;
5110 mach_msg_type_number_t pages_in_pl
;
5111 upl_control_flags_t upl_flags
;
5113 struct clios iostate
;
5120 u_int32_t devblocksize
;
5121 u_int32_t mem_alignment_mask
;
5122 u_int32_t tail_size
= 0;
5125 if (flags
& IO_PASSIVE
) {
5131 if (flags
& IO_NOCACHE
) {
5132 bflag
|= CL_NOCACHE
;
5136 * When we enter this routine, we know
5137 * -- the read_length will not exceed the current iov_len
5138 * -- the target address is physically contiguous for read_length
5140 cluster_syncup(vp
, filesize
, callback
, callback_arg
, PUSH_SYNC
);
5142 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
5143 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
5145 iostate
.io_completed
= 0;
5146 iostate
.io_issued
= 0;
5147 iostate
.io_error
= 0;
5148 iostate
.io_wanted
= 0;
5150 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
5153 io_size
= *read_length
;
5155 max_size
= filesize
- uio
->uio_offset
;
5157 if (io_size
> max_size
) {
5161 iov_base
= uio_curriovbase(uio
);
5163 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
5164 upl_needed_size
= upl_offset
+ io_size
;
5167 upl_size
= upl_needed_size
;
5168 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
5171 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_START
,
5172 (int)upl_offset
, (int)upl_size
, (int)iov_base
, io_size
, 0);
5174 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
5175 kret
= vm_map_get_upl(map
,
5176 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
5177 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, VM_KERN_MEMORY_FILE
, 0);
5179 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_END
,
5180 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
5182 if (kret
!= KERN_SUCCESS
) {
5184 * failed to get pagelist
5187 goto wait_for_creads
;
5191 if (upl_size
< upl_needed_size
) {
5193 * The upl_size wasn't satisfied.
5196 goto wait_for_creads
;
5198 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
5200 dst_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + (addr64_t
)upl_offset
;
5202 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
5203 u_int32_t head_size
;
5205 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
5207 if (head_size
> io_size
) {
5208 head_size
= io_size
;
5211 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, head_size
, CL_READ
, callback
, callback_arg
);
5214 goto wait_for_creads
;
5217 upl_offset
+= head_size
;
5218 dst_paddr
+= head_size
;
5219 io_size
-= head_size
;
5221 iov_base
+= head_size
;
5223 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
5225 * request doesn't set up on a memory boundary
5226 * the underlying DMA engine can handle...
5227 * return an error instead of going through
5228 * the slow copy path since the intent of this
5229 * path is direct I/O to device memory
5232 goto wait_for_creads
;
5235 tail_size
= io_size
& (devblocksize
- 1);
5237 io_size
-= tail_size
;
5239 while (io_size
&& error
== 0) {
5240 if (io_size
> MAX_IO_CONTIG_SIZE
) {
5241 xsize
= MAX_IO_CONTIG_SIZE
;
5246 * request asynchronously so that we can overlap
5247 * the preparation of the next I/O... we'll do
5248 * the commit after all the I/O has completed
5249 * since its all issued against the same UPL
5250 * if there are already too many outstanding reads
5251 * wait until some have completed before issuing the next
5253 cluster_iostate_wait(&iostate
, MAX_IO_CONTIG_SIZE
* IO_SCALE(vp
, 2), "cluster_read_contig");
5255 if (iostate
.io_error
) {
5257 * one of the earlier reads we issued ran into a hard error
5258 * don't issue any more reads...
5259 * go wait for any other reads to complete before
5260 * returning the error to the caller
5262 goto wait_for_creads
;
5264 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
, xsize
,
5265 CL_READ
| CL_NOZERO
| CL_DEV_MEMORY
| CL_ASYNC
| bflag
,
5266 (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
5268 * The cluster_io read was issued successfully,
5269 * update the uio structure
5272 uio_update(uio
, (user_size_t
)xsize
);
5275 upl_offset
+= xsize
;
5279 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
&& uio
->uio_offset
< filesize
) {
5280 error
= cluster_io_type(uio
, read_type
, read_length
, 0);
5282 if (error
== 0 && *read_type
== IO_CONTIG
) {
5287 *read_type
= IO_UNKNOWN
;
5292 * make sure all async reads that are part of this stream
5293 * have completed before we proceed
5295 cluster_iostate_wait(&iostate
, 0, "cluster_read_contig");
5297 if (iostate
.io_error
) {
5298 error
= iostate
.io_error
;
5301 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
5303 if (error
== 0 && tail_size
) {
5304 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, tail_size
, CL_READ
, callback
, callback_arg
);
5307 for (n
= 0; n
< num_upl
; n
++) {
5309 * just release our hold on each physically contiguous
5310 * region without changing any state
5312 ubc_upl_abort(upl
[n
], 0);
5320 cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
)
5322 user_size_t iov_len
;
5323 user_addr_t iov_base
= 0;
5325 upl_size_t upl_size
;
5326 upl_control_flags_t upl_flags
;
5330 * skip over any emtpy vectors
5332 uio_update(uio
, (user_size_t
)0);
5334 iov_len
= uio_curriovlen(uio
);
5336 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_START
, uio
, (int)iov_len
, 0, 0, 0);
5339 iov_base
= uio_curriovbase(uio
);
5341 * make sure the size of the vector isn't too big...
5342 * internally, we want to handle all of the I/O in
5343 * chunk sizes that fit in a 32 bit int
5345 if (iov_len
> (user_size_t
)MAX_IO_REQUEST_SIZE
) {
5346 upl_size
= MAX_IO_REQUEST_SIZE
;
5348 upl_size
= (u_int32_t
)iov_len
;
5351 upl_flags
= UPL_QUERY_OBJECT_TYPE
;
5353 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
5354 if ((vm_map_get_upl(map
,
5355 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
5356 &upl_size
, &upl
, NULL
, NULL
, &upl_flags
, VM_KERN_MEMORY_FILE
, 0)) != KERN_SUCCESS
) {
5358 * the user app must have passed in an invalid address
5362 if (upl_size
== 0) {
5366 *io_length
= upl_size
;
5368 if (upl_flags
& UPL_PHYS_CONTIG
) {
5369 *io_type
= IO_CONTIG
;
5370 } else if (iov_len
>= min_length
) {
5371 *io_type
= IO_DIRECT
;
5377 * nothing left to do for this uio
5380 *io_type
= IO_UNKNOWN
;
5382 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_END
, iov_base
, *io_type
, *io_length
, retval
, 0);
5389 * generate advisory I/O's in the largest chunks possible
5390 * the completed pages will be released into the VM cache
5393 advisory_read(vnode_t vp
, off_t filesize
, off_t f_offset
, int resid
)
5395 return advisory_read_ext(vp
, filesize
, f_offset
, resid
, NULL
, NULL
, CL_PASSIVE
);
5399 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
)
5401 upl_page_info_t
*pl
;
5403 vm_offset_t upl_offset
;
5416 uint32_t max_io_size
;
5419 if (!UBCINFOEXISTS(vp
)) {
5427 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
5430 if (max_io_size
> speculative_prefetch_max_iosize
) {
5431 max_io_size
= speculative_prefetch_max_iosize
;
5434 if (disk_conditioner_mount_is_ssd(vp
->v_mount
)) {
5435 if (max_io_size
> speculative_prefetch_max_iosize
) {
5436 max_io_size
= speculative_prefetch_max_iosize
;
5441 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_START
,
5442 (int)f_offset
, resid
, (int)filesize
, 0, 0);
5444 while (resid
&& f_offset
< filesize
&& retval
== 0) {
5446 * compute the size of the upl needed to encompass
5447 * the requested read... limit each call to cluster_io
5448 * to the maximum UPL size... cluster_io will clip if
5449 * this exceeds the maximum io_size for the device,
5450 * make sure to account for
5451 * a starting offset that's not page aligned
5453 start_offset
= (int)(f_offset
& PAGE_MASK_64
);
5454 upl_f_offset
= f_offset
- (off_t
)start_offset
;
5455 max_size
= filesize
- f_offset
;
5457 if (resid
< max_size
) {
5463 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
5464 if ((uint32_t)upl_size
> max_io_size
) {
5465 upl_size
= max_io_size
;
5470 * return the number of contiguously present pages in the cache
5471 * starting at upl_f_offset within the file
5473 ubc_range_op(vp
, upl_f_offset
, upl_f_offset
+ upl_size
, UPL_ROP_PRESENT
, &skip_range
);
5477 * skip over pages already present in the cache
5479 io_size
= skip_range
- start_offset
;
5481 f_offset
+= io_size
;
5484 if (skip_range
== upl_size
) {
5488 * have to issue some real I/O
5489 * at this point, we know it's starting on a page boundary
5490 * because we've skipped over at least the first page in the request
5493 upl_f_offset
+= skip_range
;
5494 upl_size
-= skip_range
;
5496 pages_in_upl
= upl_size
/ PAGE_SIZE
;
5498 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_START
,
5499 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
5501 kret
= ubc_create_upl_kernel(vp
,
5506 UPL_RET_ONLY_ABSENT
| UPL_SET_LITE
,
5507 VM_KERN_MEMORY_FILE
);
5508 if (kret
!= KERN_SUCCESS
) {
5514 * before we start marching forward, we must make sure we end on
5515 * a present page, otherwise we will be working with a freed
5518 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
5519 if (upl_page_present(pl
, last_pg
)) {
5523 pages_in_upl
= last_pg
+ 1;
5526 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_END
,
5527 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
5530 for (last_pg
= 0; last_pg
< pages_in_upl
;) {
5532 * scan from the beginning of the upl looking for the first
5533 * page that is present.... this will become the first page in
5534 * the request we're going to make to 'cluster_io'... if all
5535 * of the pages are absent, we won't call through to 'cluster_io'
5537 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
5538 if (upl_page_present(pl
, start_pg
)) {
5544 * scan from the starting present page looking for an absent
5545 * page before the end of the upl is reached, if we
5546 * find one, then it will terminate the range of pages being
5547 * presented to 'cluster_io'
5549 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
5550 if (!upl_page_present(pl
, last_pg
)) {
5555 if (last_pg
> start_pg
) {
5557 * we found a range of pages that must be filled
5558 * if the last page in this range is the last page of the file
5559 * we may have to clip the size of it to keep from reading past
5560 * the end of the last physical block associated with the file
5562 upl_offset
= start_pg
* PAGE_SIZE
;
5563 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
5565 if ((off_t
)(upl_f_offset
+ upl_offset
+ io_size
) > filesize
) {
5566 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
5570 * issue an asynchronous read to cluster_io
5572 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
5573 CL_ASYNC
| CL_READ
| CL_COMMIT
| CL_AGE
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5578 if (issued_io
== 0) {
5579 ubc_upl_abort(upl
, 0);
5582 io_size
= upl_size
- start_offset
;
5584 if (io_size
> resid
) {
5587 f_offset
+= io_size
;
5591 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_END
,
5592 (int)f_offset
, resid
, retval
, 0, 0);
5599 cluster_push(vnode_t vp
, int flags
)
5601 return cluster_push_ext(vp
, flags
, NULL
, NULL
);
5606 cluster_push_ext(vnode_t vp
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5608 return cluster_push_err(vp
, flags
, callback
, callback_arg
, NULL
);
5611 /* write errors via err, but return the number of clusters written */
5613 cluster_push_err(vnode_t vp
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
, int *err
)
5616 int my_sparse_wait
= 0;
5617 struct cl_writebehind
*wbp
;
5624 if (!UBCINFOEXISTS(vp
)) {
5625 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, kdebug_vnode(vp
), flags
, 0, -1, 0);
5628 /* return if deferred write is set */
5629 if (((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) && (flags
& IO_DEFWRITE
)) {
5632 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) == NULL
) {
5633 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, kdebug_vnode(vp
), flags
, 0, -2, 0);
5636 if (!ISSET(flags
, IO_SYNC
) && wbp
->cl_number
== 0 && wbp
->cl_scmap
== NULL
) {
5637 lck_mtx_unlock(&wbp
->cl_lockw
);
5639 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, kdebug_vnode(vp
), flags
, 0, -3, 0);
5642 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_START
,
5643 wbp
->cl_scmap
, wbp
->cl_number
, flags
, 0, 0);
5646 * if we have an fsync in progress, we don't want to allow any additional
5647 * sync/fsync/close(s) to occur until it finishes.
5648 * note that its possible for writes to continue to occur to this file
5649 * while we're waiting and also once the fsync starts to clean if we're
5650 * in the sparse map case
5652 while (wbp
->cl_sparse_wait
) {
5653 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_START
, kdebug_vnode(vp
), 0, 0, 0, 0);
5655 msleep((caddr_t
)&wbp
->cl_sparse_wait
, &wbp
->cl_lockw
, PRIBIO
+ 1, "cluster_push_ext", NULL
);
5657 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_END
, kdebug_vnode(vp
), 0, 0, 0, 0);
5659 if (flags
& IO_SYNC
) {
5661 wbp
->cl_sparse_wait
= 1;
5664 * this is an fsync (or equivalent)... we must wait for any existing async
5665 * cleaning operations to complete before we evaulate the current state
5666 * and finish cleaning... this insures that all writes issued before this
5667 * fsync actually get cleaned to the disk before this fsync returns
5669 while (wbp
->cl_sparse_pushes
) {
5670 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 98)) | DBG_FUNC_START
, kdebug_vnode(vp
), 0, 0, 0, 0);
5672 msleep((caddr_t
)&wbp
->cl_sparse_pushes
, &wbp
->cl_lockw
, PRIBIO
+ 1, "cluster_push_ext", NULL
);
5674 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 98)) | DBG_FUNC_END
, kdebug_vnode(vp
), 0, 0, 0, 0);
5677 if (wbp
->cl_scmap
) {
5680 if (wbp
->cl_sparse_pushes
< SPARSE_PUSH_LIMIT
) {
5681 scmap
= wbp
->cl_scmap
;
5682 wbp
->cl_scmap
= NULL
;
5684 wbp
->cl_sparse_pushes
++;
5686 lck_mtx_unlock(&wbp
->cl_lockw
);
5688 retval
= sparse_cluster_push(wbp
, &scmap
, vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
, FALSE
);
5690 lck_mtx_lock(&wbp
->cl_lockw
);
5692 wbp
->cl_sparse_pushes
--;
5695 if (wbp
->cl_scmap
!= NULL
) {
5696 panic("cluster_push_err: Expected NULL cl_scmap\n");
5699 wbp
->cl_scmap
= scmap
;
5702 if (wbp
->cl_sparse_wait
&& wbp
->cl_sparse_pushes
== 0) {
5703 wakeup((caddr_t
)&wbp
->cl_sparse_pushes
);
5706 retval
= sparse_cluster_push(wbp
, &(wbp
->cl_scmap
), vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
, FALSE
);
5716 retval
= cluster_try_push(wbp
, vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
, &local_err
, FALSE
);
5721 lck_mtx_unlock(&wbp
->cl_lockw
);
5723 if (flags
& IO_SYNC
) {
5724 (void)vnode_waitforwrites(vp
, 0, 0, 0, "cluster_push");
5727 if (my_sparse_wait
) {
5729 * I'm the owner of the serialization token
5730 * clear it and wakeup anyone that is waiting
5733 lck_mtx_lock(&wbp
->cl_lockw
);
5735 wbp
->cl_sparse_wait
= 0;
5736 wakeup((caddr_t
)&wbp
->cl_sparse_wait
);
5738 lck_mtx_unlock(&wbp
->cl_lockw
);
5740 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_END
,
5741 wbp
->cl_scmap
, wbp
->cl_number
, retval
, local_err
, 0);
5747 __private_extern__
void
5748 cluster_release(struct ubc_info
*ubc
)
5750 struct cl_writebehind
*wbp
;
5751 struct cl_readahead
*rap
;
5753 if ((wbp
= ubc
->cl_wbehind
)) {
5754 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, ubc
, wbp
->cl_scmap
, 0, 0, 0);
5756 if (wbp
->cl_scmap
) {
5757 vfs_drt_control(&(wbp
->cl_scmap
), 0);
5760 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, ubc
, 0, 0, 0, 0);
5763 rap
= ubc
->cl_rahead
;
5766 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
5767 FREE_ZONE(wbp
, sizeof *wbp
, M_CLWRBEHIND
);
5769 if ((rap
= ubc
->cl_rahead
)) {
5770 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
5771 FREE_ZONE(rap
, sizeof *rap
, M_CLRDAHEAD
);
5773 ubc
->cl_rahead
= NULL
;
5774 ubc
->cl_wbehind
= NULL
;
5776 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_END
, ubc
, rap
, wbp
, 0, 0);
5781 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
, int *err
, boolean_t vm_initiated
)
5788 struct cl_wextent l_clusters
[MAX_CLUSTERS
];
5789 u_int max_cluster_pgcount
;
5792 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
5794 * the write behind context exists and has
5795 * already been locked...
5797 if (wbp
->cl_number
== 0) {
5799 * no clusters to push
5800 * return number of empty slots
5802 return MAX_CLUSTERS
;
5806 * make a local 'sorted' copy of the clusters
5807 * and clear wbp->cl_number so that new clusters can
5810 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
5811 for (min_index
= -1, cl_index1
= 0; cl_index1
< wbp
->cl_number
; cl_index1
++) {
5812 if (wbp
->cl_clusters
[cl_index1
].b_addr
== wbp
->cl_clusters
[cl_index1
].e_addr
) {
5815 if (min_index
== -1) {
5816 min_index
= cl_index1
;
5817 } else if (wbp
->cl_clusters
[cl_index1
].b_addr
< wbp
->cl_clusters
[min_index
].b_addr
) {
5818 min_index
= cl_index1
;
5821 if (min_index
== -1) {
5825 l_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[min_index
].b_addr
;
5826 l_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
5827 l_clusters
[cl_index
].io_flags
= wbp
->cl_clusters
[min_index
].io_flags
;
5829 wbp
->cl_clusters
[min_index
].b_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
5835 /* skip switching to the sparse cluster mechanism if on diskimage */
5836 if (((push_flag
& PUSH_DELAY
) && cl_len
== MAX_CLUSTERS
) &&
5837 !(vp
->v_mount
->mnt_kern_flag
& MNTK_VIRTUALDEV
)) {
5841 * determine if we appear to be writing the file sequentially
5842 * if not, by returning without having pushed any clusters
5843 * we will cause this vnode to be pushed into the sparse cluster mechanism
5844 * used for managing more random I/O patterns
5846 * we know that we've got all clusters currently in use and the next write doesn't fit into one of them...
5847 * that's why we're in try_push with PUSH_DELAY...
5849 * check to make sure that all the clusters except the last one are 'full'... and that each cluster
5850 * is adjacent to the next (i.e. we're looking for sequential writes) they were sorted above
5851 * so we can just make a simple pass through, up to, but not including the last one...
5852 * note that e_addr is not inclusive, so it will be equal to the b_addr of the next cluster if they
5855 * we let the last one be partial as long as it was adjacent to the previous one...
5856 * we need to do this to deal with multi-threaded servers that might write an I/O or 2 out
5857 * of order... if this occurs at the tail of the last cluster, we don't want to fall into the sparse cluster world...
5859 for (i
= 0; i
< MAX_CLUSTERS
- 1; i
++) {
5860 if ((l_clusters
[i
].e_addr
- l_clusters
[i
].b_addr
) != max_cluster_pgcount
) {
5863 if (l_clusters
[i
].e_addr
!= l_clusters
[i
+ 1].b_addr
) {
5868 if (vm_initiated
== TRUE
) {
5869 lck_mtx_unlock(&wbp
->cl_lockw
);
5872 for (cl_index
= 0; cl_index
< cl_len
; cl_index
++) {
5874 struct cl_extent cl
;
5877 flags
= io_flags
& (IO_PASSIVE
| IO_CLOSE
);
5880 * try to push each cluster in turn...
5882 if (l_clusters
[cl_index
].io_flags
& CLW_IONOCACHE
) {
5883 flags
|= IO_NOCACHE
;
5886 if (l_clusters
[cl_index
].io_flags
& CLW_IOPASSIVE
) {
5887 flags
|= IO_PASSIVE
;
5890 if (push_flag
& PUSH_SYNC
) {
5894 cl
.b_addr
= l_clusters
[cl_index
].b_addr
;
5895 cl
.e_addr
= l_clusters
[cl_index
].e_addr
;
5897 retval
= cluster_push_now(vp
, &cl
, EOF
, flags
, callback
, callback_arg
, vm_initiated
);
5902 l_clusters
[cl_index
].b_addr
= 0;
5903 l_clusters
[cl_index
].e_addr
= 0;
5904 } else if (error
== 0) {
5908 if (!(push_flag
& PUSH_ALL
)) {
5912 if (vm_initiated
== TRUE
) {
5913 lck_mtx_lock(&wbp
->cl_lockw
);
5921 if (cl_len
> cl_pushed
) {
5923 * we didn't push all of the clusters, so
5924 * lets try to merge them back in to the vnode
5926 if ((MAX_CLUSTERS
- wbp
->cl_number
) < (cl_len
- cl_pushed
)) {
5928 * we picked up some new clusters while we were trying to
5929 * push the old ones... this can happen because I've dropped
5930 * the vnode lock... the sum of the
5931 * leftovers plus the new cluster count exceeds our ability
5932 * to represent them, so switch to the sparse cluster mechanism
5934 * collect the active public clusters...
5936 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
, vm_initiated
);
5938 for (cl_index
= 0, cl_index1
= 0; cl_index
< cl_len
; cl_index
++) {
5939 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
) {
5942 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
5943 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
5944 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
5949 * update the cluster count
5951 wbp
->cl_number
= cl_index1
;
5954 * and collect the original clusters that were moved into the
5955 * local storage for sorting purposes
5957 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
, vm_initiated
);
5960 * we've got room to merge the leftovers back in
5961 * just append them starting at the next 'hole'
5962 * represented by wbp->cl_number
5964 for (cl_index
= 0, cl_index1
= wbp
->cl_number
; cl_index
< cl_len
; cl_index
++) {
5965 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
) {
5969 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
5970 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
5971 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
5976 * update the cluster count
5978 wbp
->cl_number
= cl_index1
;
5981 return MAX_CLUSTERS
- wbp
->cl_number
;
5987 cluster_push_now(vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int flags
,
5988 int (*callback
)(buf_t
, void *), void *callback_arg
, boolean_t vm_initiated
)
5990 upl_page_info_t
*pl
;
5992 vm_offset_t upl_offset
;
6007 if (flags
& IO_PASSIVE
) {
6013 if (flags
& IO_SKIP_ENCRYPTION
) {
6014 bflag
|= CL_ENCRYPTED
;
6017 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_START
,
6018 (int)cl
->b_addr
, (int)cl
->e_addr
, (int)EOF
, flags
, 0);
6020 if ((pages_in_upl
= (int)(cl
->e_addr
- cl
->b_addr
)) == 0) {
6021 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 0, 0, 0, 0);
6025 upl_size
= pages_in_upl
* PAGE_SIZE
;
6026 upl_f_offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
6028 if (upl_f_offset
+ upl_size
>= EOF
) {
6029 if (upl_f_offset
>= EOF
) {
6031 * must have truncated the file and missed
6032 * clearing a dangling cluster (i.e. it's completely
6033 * beyond the new EOF
6035 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 1, 0, 0, 0);
6039 size
= EOF
- upl_f_offset
;
6041 upl_size
= (size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
6042 pages_in_upl
= upl_size
/ PAGE_SIZE
;
6049 vnode_pageout(vp
, NULL
, (upl_offset_t
)0, upl_f_offset
, (upl_size_t
)upl_size
,
6050 UPL_MSYNC
| UPL_VNODE_PAGER
| UPL_KEEPCACHED
, &error
);
6054 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, size
, 0, 0, 0);
6057 * by asking for UPL_COPYOUT_FROM and UPL_RET_ONLY_DIRTY, we get the following desirable behavior
6059 * - only pages that are currently dirty are returned... these are the ones we need to clean
6060 * - the hardware dirty bit is cleared when the page is gathered into the UPL... the software dirty bit is set
6061 * - 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
6062 * - when we commit the page, the software dirty bit is cleared... the hardware dirty bit is untouched so that if
6063 * someone dirties this page while the I/O is in progress, we don't lose track of the new state
6065 * when the I/O completes, we no longer ask for an explicit clear of the DIRTY state (either soft or hard)
6068 if ((vp
->v_flag
& VNOCACHE_DATA
) || (flags
& IO_NOCACHE
)) {
6069 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
| UPL_WILL_BE_DUMPED
;
6071 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
;
6074 kret
= ubc_create_upl_kernel(vp
,
6080 VM_KERN_MEMORY_FILE
);
6081 if (kret
!= KERN_SUCCESS
) {
6082 panic("cluster_push: failed to get pagelist");
6085 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
, upl
, upl_f_offset
, 0, 0, 0);
6088 * since we only asked for the dirty pages back
6089 * it's possible that we may only get a few or even none, so...
6090 * before we start marching forward, we must make sure we know
6091 * where the last present page is in the UPL, otherwise we could
6092 * end up working with a freed upl due to the FREE_ON_EMPTY semantics
6093 * employed by commit_range and abort_range.
6095 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
6096 if (upl_page_present(pl
, last_pg
)) {
6100 pages_in_upl
= last_pg
+ 1;
6102 if (pages_in_upl
== 0) {
6103 ubc_upl_abort(upl
, 0);
6105 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 2, 0, 0, 0);
6109 for (last_pg
= 0; last_pg
< pages_in_upl
;) {
6111 * find the next dirty page in the UPL
6112 * this will become the first page in the
6113 * next I/O to generate
6115 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
6116 if (upl_dirty_page(pl
, start_pg
)) {
6119 if (upl_page_present(pl
, start_pg
)) {
6121 * RET_ONLY_DIRTY will return non-dirty 'precious' pages
6122 * just release these unchanged since we're not going
6123 * to steal them or change their state
6125 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
6128 if (start_pg
>= pages_in_upl
) {
6130 * done... no more dirty pages to push
6134 if (start_pg
> last_pg
) {
6136 * skipped over some non-dirty pages
6138 size
-= ((start_pg
- last_pg
) * PAGE_SIZE
);
6142 * find a range of dirty pages to write
6144 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
6145 if (!upl_dirty_page(pl
, last_pg
)) {
6149 upl_offset
= start_pg
* PAGE_SIZE
;
6151 io_size
= min(size
, (last_pg
- start_pg
) * PAGE_SIZE
);
6153 io_flags
= CL_THROTTLE
| CL_COMMIT
| CL_AGE
| bflag
;
6155 if (!(flags
& IO_SYNC
)) {
6156 io_flags
|= CL_ASYNC
;
6159 if (flags
& IO_CLOSE
) {
6160 io_flags
|= CL_CLOSE
;
6163 if (flags
& IO_NOCACHE
) {
6164 io_flags
|= CL_NOCACHE
;
6167 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
6168 io_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
6170 if (error
== 0 && retval
) {
6176 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 3, error
, 0, 0);
6183 * sparse_cluster_switch is called with the write behind lock held
6186 sparse_cluster_switch(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int (*callback
)(buf_t
, void *), void *callback_arg
, boolean_t vm_initiated
)
6191 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_START
, kdebug_vnode(vp
), wbp
->cl_scmap
, wbp
->cl_number
, 0, 0);
6193 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
6195 struct cl_extent cl
;
6197 for (cl
.b_addr
= wbp
->cl_clusters
[cl_index
].b_addr
; cl
.b_addr
< wbp
->cl_clusters
[cl_index
].e_addr
; cl
.b_addr
++) {
6198 if (ubc_page_op(vp
, (off_t
)(cl
.b_addr
* PAGE_SIZE_64
), 0, NULL
, &flags
) == KERN_SUCCESS
) {
6199 if (flags
& UPL_POP_DIRTY
) {
6200 cl
.e_addr
= cl
.b_addr
+ 1;
6202 error
= sparse_cluster_add(wbp
, &(wbp
->cl_scmap
), vp
, &cl
, EOF
, callback
, callback_arg
, vm_initiated
);
6211 wbp
->cl_number
-= cl_index
;
6213 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_END
, kdebug_vnode(vp
), wbp
->cl_scmap
, wbp
->cl_number
, error
, 0);
6220 * sparse_cluster_push must be called with the write-behind lock held if the scmap is
6221 * still associated with the write-behind context... however, if the scmap has been disassociated
6222 * from the write-behind context (the cluster_push case), the wb lock is not held
6225 sparse_cluster_push(struct cl_writebehind
*wbp
, void **scmap
, vnode_t vp
, off_t EOF
, int push_flag
,
6226 int io_flags
, int (*callback
)(buf_t
, void *), void *callback_arg
, boolean_t vm_initiated
)
6228 struct cl_extent cl
;
6234 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_START
, kdebug_vnode(vp
), (*scmap
), 0, push_flag
, 0);
6236 if (push_flag
& PUSH_ALL
) {
6237 vfs_drt_control(scmap
, 1);
6245 if (vfs_drt_get_cluster(scmap
, &offset
, &length
) != KERN_SUCCESS
) {
6249 if (vm_initiated
== TRUE
) {
6250 lck_mtx_unlock(&wbp
->cl_lockw
);
6253 cl
.b_addr
= (daddr64_t
)(offset
/ PAGE_SIZE_64
);
6254 cl
.e_addr
= (daddr64_t
)((offset
+ length
) / PAGE_SIZE_64
);
6256 retval
= cluster_push_now(vp
, &cl
, EOF
, io_flags
, callback
, callback_arg
, vm_initiated
);
6257 if (error
== 0 && retval
) {
6261 if (vm_initiated
== TRUE
) {
6262 lck_mtx_lock(&wbp
->cl_lockw
);
6264 if (*scmap
!= l_scmap
) {
6270 if (vfs_drt_mark_pages(scmap
, offset
, length
, NULL
) != KERN_SUCCESS
) {
6271 panic("Failed to restore dirty state on failure\n");
6277 if (!(push_flag
& PUSH_ALL
)) {
6281 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_END
, kdebug_vnode(vp
), (*scmap
), error
, 0, 0);
6288 * sparse_cluster_add is called with the write behind lock held
6291 sparse_cluster_add(struct cl_writebehind
*wbp
, void **scmap
, vnode_t vp
, struct cl_extent
*cl
, off_t EOF
,
6292 int (*callback
)(buf_t
, void *), void *callback_arg
, boolean_t vm_initiated
)
6298 int push_flag
= 0; /* Is this a valid value? */
6300 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_START
, (*scmap
), 0, cl
->b_addr
, (int)cl
->e_addr
, 0);
6302 offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
6303 length
= ((u_int
)(cl
->e_addr
- cl
->b_addr
)) * PAGE_SIZE
;
6305 while (vfs_drt_mark_pages(scmap
, offset
, length
, &new_dirty
) != KERN_SUCCESS
) {
6307 * no room left in the map
6308 * only a partial update was done
6309 * push out some pages and try again
6312 if (vfs_get_scmap_push_behavior_internal(scmap
, &push_flag
)) {
6316 error
= sparse_cluster_push(wbp
, scmap
, vp
, EOF
, push_flag
, 0, callback
, callback_arg
, vm_initiated
);
6322 offset
+= (new_dirty
* PAGE_SIZE_64
);
6323 length
-= (new_dirty
* PAGE_SIZE
);
6325 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_END
, kdebug_vnode(vp
), (*scmap
), error
, 0, 0);
6332 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
)
6334 upl_page_info_t
*pl
;
6344 if (flags
& IO_PASSIVE
) {
6350 if (flags
& IO_NOCACHE
) {
6351 bflag
|= CL_NOCACHE
;
6354 upl_flags
= UPL_SET_LITE
;
6356 if (!(flags
& CL_READ
)) {
6358 * "write" operation: let the UPL subsystem know
6359 * that we intend to modify the buffer cache pages
6362 upl_flags
|= UPL_WILL_MODIFY
;
6365 * indicate that there is no need to pull the
6366 * mapping for this page... we're only going
6367 * to read from it, not modify it.
6369 upl_flags
|= UPL_FILE_IO
;
6371 kret
= ubc_create_upl_kernel(vp
,
6372 uio
->uio_offset
& ~PAGE_MASK_64
,
6377 VM_KERN_MEMORY_FILE
);
6379 if (kret
!= KERN_SUCCESS
) {
6383 if (!upl_valid_page(pl
, 0)) {
6385 * issue a synchronous read to cluster_io
6387 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
6388 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
6390 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
6396 ubc_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + (addr64_t
)(uio
->uio_offset
& PAGE_MASK_64
);
6399 * NOTE: There is no prototype for the following in BSD. It, and the definitions
6400 * of the defines for cppvPsrc, cppvPsnk, cppvFsnk, and cppvFsrc will be found in
6401 * osfmk/ppc/mappings.h. They are not included here because there appears to be no
6402 * way to do so without exporting them to kexts as well.
6404 if (flags
& CL_READ
) {
6405 // copypv(ubc_paddr, usr_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsnk); /* Copy physical to physical and flush the destination */
6406 copypv(ubc_paddr
, usr_paddr
, xsize
, 2 | 1 | 4); /* Copy physical to physical and flush the destination */
6408 // copypv(usr_paddr, ubc_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsrc); /* Copy physical to physical and flush the source */
6409 copypv(usr_paddr
, ubc_paddr
, xsize
, 2 | 1 | 8); /* Copy physical to physical and flush the source */
6411 if (!(flags
& CL_READ
) || (upl_valid_page(pl
, 0) && upl_dirty_page(pl
, 0))) {
6413 * issue a synchronous write to cluster_io
6415 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
6416 bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
6419 uio_update(uio
, (user_size_t
)xsize
);
6423 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
6425 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
6428 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, abort_flags
);
6434 cluster_copy_upl_data(struct uio
*uio
, upl_t upl
, int upl_offset
, int *io_resid
)
6442 upl_page_info_t
*pl
;
6447 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
6448 (int)uio
->uio_offset
, upl_offset
, xsize
, 0, 0);
6450 segflg
= uio
->uio_segflg
;
6453 case UIO_USERSPACE32
:
6454 case UIO_USERISPACE32
:
6455 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
6459 case UIO_USERISPACE
:
6460 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
6463 case UIO_USERSPACE64
:
6464 case UIO_USERISPACE64
:
6465 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
6469 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
6472 pl
= ubc_upl_pageinfo(upl
);
6474 pg_index
= upl_offset
/ PAGE_SIZE
;
6475 pg_offset
= upl_offset
& PAGE_MASK
;
6476 csize
= min(PAGE_SIZE
- pg_offset
, xsize
);
6479 while (xsize
&& retval
== 0) {
6482 paddr
= ((addr64_t
)upl_phys_page(pl
, pg_index
) << PAGE_SHIFT
) + pg_offset
;
6483 if ((uio
->uio_rw
== UIO_WRITE
) && (upl_dirty_page(pl
, pg_index
) == FALSE
)) {
6487 retval
= uiomove64(paddr
, csize
, uio
);
6492 csize
= min(PAGE_SIZE
, xsize
);
6496 uio
->uio_segflg
= segflg
;
6498 task_update_logical_writes(current_task(), (dirty_count
* PAGE_SIZE
), TASK_WRITE_DEFERRED
, upl_lookup_vnode(upl
));
6499 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
6500 (int)uio
->uio_offset
, xsize
, retval
, segflg
, 0);
6507 cluster_copy_ubc_data(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
)
6509 return cluster_copy_ubc_data_internal(vp
, uio
, io_resid
, mark_dirty
, 1);
6514 cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
)
6521 memory_object_control_t control
;
6523 io_size
= *io_resid
;
6525 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
6526 (int)uio
->uio_offset
, io_size
, mark_dirty
, take_reference
, 0);
6528 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
6530 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
6531 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
6532 (int)uio
->uio_offset
, io_size
, retval
, 3, 0);
6536 segflg
= uio
->uio_segflg
;
6539 case UIO_USERSPACE32
:
6540 case UIO_USERISPACE32
:
6541 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
6544 case UIO_USERSPACE64
:
6545 case UIO_USERISPACE64
:
6546 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
6550 case UIO_USERISPACE
:
6551 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
6555 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
6559 if ((io_size
= *io_resid
)) {
6560 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
6561 xsize
= uio_resid(uio
);
6563 retval
= memory_object_control_uiomove(control
, uio
->uio_offset
- start_offset
, uio
,
6564 start_offset
, io_size
, mark_dirty
, take_reference
);
6565 xsize
-= uio_resid(uio
);
6568 uio
->uio_segflg
= segflg
;
6569 *io_resid
= io_size
;
6571 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
6572 (int)uio
->uio_offset
, io_size
, retval
, 0x80000000 | segflg
, 0);
6579 is_file_clean(vnode_t vp
, off_t filesize
)
6583 int total_dirty
= 0;
6585 for (f_offset
= 0; f_offset
< filesize
; f_offset
+= PAGE_SIZE_64
) {
6586 if (ubc_page_op(vp
, f_offset
, 0, NULL
, &flags
) == KERN_SUCCESS
) {
6587 if (flags
& UPL_POP_DIRTY
) {
6602 * Dirty region tracking/clustering mechanism.
6604 * This code (vfs_drt_*) provides a mechanism for tracking and clustering
6605 * dirty regions within a larger space (file). It is primarily intended to
6606 * support clustering in large files with many dirty areas.
6608 * The implementation assumes that the dirty regions are pages.
6610 * To represent dirty pages within the file, we store bit vectors in a
6611 * variable-size circular hash.
6615 * Bitvector size. This determines the number of pages we group in a
6616 * single hashtable entry. Each hashtable entry is aligned to this
6617 * size within the file.
6619 #define DRT_BITVECTOR_PAGES ((1024 * 256) / PAGE_SIZE)
6622 * File offset handling.
6624 * DRT_ADDRESS_MASK is dependent on DRT_BITVECTOR_PAGES;
6625 * the correct formula is (~((DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1))
6627 #define DRT_ADDRESS_MASK (~((DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1))
6628 #define DRT_ALIGN_ADDRESS(addr) ((addr) & DRT_ADDRESS_MASK)
6631 * Hashtable address field handling.
6633 * The low-order bits of the hashtable address are used to conserve
6636 * DRT_HASH_COUNT_MASK must be large enough to store the range
6637 * 0-DRT_BITVECTOR_PAGES inclusive, as well as have one value
6638 * to indicate that the bucket is actually unoccupied.
6640 #define DRT_HASH_GET_ADDRESS(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_ADDRESS_MASK)
6641 #define DRT_HASH_SET_ADDRESS(scm, i, a) \
6643 (scm)->scm_hashtable[(i)].dhe_control = \
6644 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_ADDRESS_MASK) | DRT_ALIGN_ADDRESS(a); \
6646 #define DRT_HASH_COUNT_MASK 0x1ff
6647 #define DRT_HASH_GET_COUNT(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_HASH_COUNT_MASK)
6648 #define DRT_HASH_SET_COUNT(scm, i, c) \
6650 (scm)->scm_hashtable[(i)].dhe_control = \
6651 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_HASH_COUNT_MASK) | ((c) & DRT_HASH_COUNT_MASK); \
6653 #define DRT_HASH_CLEAR(scm, i) \
6655 (scm)->scm_hashtable[(i)].dhe_control = 0; \
6657 #define DRT_HASH_VACATE(scm, i) DRT_HASH_SET_COUNT((scm), (i), DRT_HASH_COUNT_MASK)
6658 #define DRT_HASH_VACANT(scm, i) (DRT_HASH_GET_COUNT((scm), (i)) == DRT_HASH_COUNT_MASK)
6659 #define DRT_HASH_COPY(oscm, oi, scm, i) \
6661 (scm)->scm_hashtable[(i)].dhe_control = (oscm)->scm_hashtable[(oi)].dhe_control; \
6662 DRT_BITVECTOR_COPY(oscm, oi, scm, i); \
6668 * Hash table moduli.
6670 * Since the hashtable entry's size is dependent on the size of
6671 * the bitvector, and since the hashtable size is constrained to
6672 * both being prime and fitting within the desired allocation
6673 * size, these values need to be manually determined.
6675 * For DRT_BITVECTOR_SIZE = 64, the entry size is 16 bytes.
6677 * The small hashtable allocation is 4096 bytes, so the modulus is 251.
6678 * The large hashtable allocation is 32768 bytes, so the modulus is 2039.
6679 * The xlarge hashtable allocation is 131072 bytes, so the modulus is 8179.
6682 #define DRT_HASH_SMALL_MODULUS 251
6683 #define DRT_HASH_LARGE_MODULUS 2039
6684 #define DRT_HASH_XLARGE_MODULUS 8179
6687 * Physical memory required before the large hash modulus is permitted.
6689 * On small memory systems, the large hash modulus can lead to phsyical
6690 * memory starvation, so we avoid using it there.
6692 #define DRT_HASH_LARGE_MEMORY_REQUIRED (1024LL * 1024LL * 1024LL) /* 1GiB */
6693 #define DRT_HASH_XLARGE_MEMORY_REQUIRED (8 * 1024LL * 1024LL * 1024LL) /* 8GiB */
6695 #define DRT_SMALL_ALLOCATION 4096 /* 80 bytes spare */
6696 #define DRT_LARGE_ALLOCATION 32768 /* 144 bytes spare */
6697 #define DRT_XLARGE_ALLOCATION 131072 /* 208 bytes spare */
6701 * Hash table moduli.
6703 * Since the hashtable entry's size is dependent on the size of
6704 * the bitvector, and since the hashtable size is constrained to
6705 * both being prime and fitting within the desired allocation
6706 * size, these values need to be manually determined.
6708 * For DRT_BITVECTOR_SIZE = 64, the entry size is 16 bytes.
6710 * The small hashtable allocation is 16384 bytes, so the modulus is 1019.
6711 * The large hashtable allocation is 131072 bytes, so the modulus is 8179.
6712 * The xlarge hashtable allocation is 524288 bytes, so the modulus is 32749.
6715 #define DRT_HASH_SMALL_MODULUS 1019
6716 #define DRT_HASH_LARGE_MODULUS 8179
6717 #define DRT_HASH_XLARGE_MODULUS 32749
6720 * Physical memory required before the large hash modulus is permitted.
6722 * On small memory systems, the large hash modulus can lead to phsyical
6723 * memory starvation, so we avoid using it there.
6725 #define DRT_HASH_LARGE_MEMORY_REQUIRED (4 * 1024LL * 1024LL * 1024LL) /* 4GiB */
6726 #define DRT_HASH_XLARGE_MEMORY_REQUIRED (32 * 1024LL * 1024LL * 1024LL) /* 32GiB */
6728 #define DRT_SMALL_ALLOCATION 16384 /* 80 bytes spare */
6729 #define DRT_LARGE_ALLOCATION 131072 /* 208 bytes spare */
6730 #define DRT_XLARGE_ALLOCATION 524288 /* 304 bytes spare */
6734 /* *** nothing below here has secret dependencies on DRT_BITVECTOR_PAGES *** */
6739 struct vfs_drt_hashentry
{
6740 u_int64_t dhe_control
;
6742 * dhe_bitvector was declared as dhe_bitvector[DRT_BITVECTOR_PAGES / 32];
6743 * DRT_BITVECTOR_PAGES is defined as ((1024 * 256) / PAGE_SIZE)
6744 * Since PAGE_SIZE is only known at boot time,
6745 * -define MAX_DRT_BITVECTOR_PAGES for smallest supported page size (4k)
6746 * -declare dhe_bitvector array for largest possible length
6748 #define MAX_DRT_BITVECTOR_PAGES (1024 * 256)/( 4 * 1024)
6749 u_int32_t dhe_bitvector
[MAX_DRT_BITVECTOR_PAGES
/ 32];
6753 * Hashtable bitvector handling.
6755 * Bitvector fields are 32 bits long.
6758 #define DRT_HASH_SET_BIT(scm, i, bit) \
6759 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] |= (1 << ((bit) % 32))
6761 #define DRT_HASH_CLEAR_BIT(scm, i, bit) \
6762 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] &= ~(1 << ((bit) % 32))
6764 #define DRT_HASH_TEST_BIT(scm, i, bit) \
6765 ((scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] & (1 << ((bit) % 32)))
6767 #define DRT_BITVECTOR_CLEAR(scm, i) \
6768 bzero(&(scm)->scm_hashtable[(i)].dhe_bitvector[0], (MAX_DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
6770 #define DRT_BITVECTOR_COPY(oscm, oi, scm, i) \
6771 bcopy(&(oscm)->scm_hashtable[(oi)].dhe_bitvector[0], \
6772 &(scm)->scm_hashtable[(i)].dhe_bitvector[0], \
6773 (MAX_DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
6776 * Dirty Region Tracking structure.
6778 * The hashtable is allocated entirely inside the DRT structure.
6780 * The hash is a simple circular prime modulus arrangement, the structure
6781 * is resized from small to large if it overflows.
6784 struct vfs_drt_clustermap
{
6785 u_int32_t scm_magic
; /* sanity/detection */
6786 #define DRT_SCM_MAGIC 0x12020003
6787 u_int32_t scm_modulus
; /* current ring size */
6788 u_int32_t scm_buckets
; /* number of occupied buckets */
6789 u_int32_t scm_lastclean
; /* last entry we cleaned */
6790 u_int32_t scm_iskips
; /* number of slot skips */
6792 struct vfs_drt_hashentry scm_hashtable
[0];
6796 #define DRT_HASH(scm, addr) ((addr) % (scm)->scm_modulus)
6797 #define DRT_HASH_NEXT(scm, addr) (((addr) + 1) % (scm)->scm_modulus)
6800 * Debugging codes and arguments.
6802 #define DRT_DEBUG_EMPTYFREE (FSDBG_CODE(DBG_FSRW, 82)) /* nil */
6803 #define DRT_DEBUG_RETCLUSTER (FSDBG_CODE(DBG_FSRW, 83)) /* offset, length */
6804 #define DRT_DEBUG_ALLOC (FSDBG_CODE(DBG_FSRW, 84)) /* copycount */
6805 #define DRT_DEBUG_INSERT (FSDBG_CODE(DBG_FSRW, 85)) /* offset, iskip */
6806 #define DRT_DEBUG_MARK (FSDBG_CODE(DBG_FSRW, 86)) /* offset, length,
6809 /* 1 (clean, no map) */
6810 /* 2 (map alloc fail) */
6811 /* 3, resid (partial) */
6812 #define DRT_DEBUG_6 (FSDBG_CODE(DBG_FSRW, 87))
6813 #define DRT_DEBUG_SCMDATA (FSDBG_CODE(DBG_FSRW, 88)) /* modulus, buckets,
6814 * lastclean, iskips */
6817 static kern_return_t
vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
);
6818 static kern_return_t
vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
);
6819 static kern_return_t
vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
,
6820 u_int64_t offset
, int *indexp
);
6821 static kern_return_t
vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
,
6825 static kern_return_t
vfs_drt_do_mark_pages(
6831 static void vfs_drt_trace(
6832 struct vfs_drt_clustermap
*cmap
,
6841 * Allocate and initialise a sparse cluster map.
6843 * Will allocate a new map, resize or compact an existing map.
6845 * XXX we should probably have at least one intermediate map size,
6846 * as the 1:16 ratio seems a bit drastic.
6848 static kern_return_t
6849 vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
)
6851 struct vfs_drt_clustermap
*cmap
= NULL
, *ocmap
= NULL
;
6852 kern_return_t kret
= KERN_SUCCESS
;
6853 u_int64_t offset
= 0;
6855 int modulus_size
= 0, map_size
= 0, active_buckets
= 0, index
= 0, copycount
= 0;
6858 if (cmapp
!= NULL
) {
6863 * Decide on the size of the new map.
6865 if (ocmap
== NULL
) {
6866 modulus_size
= DRT_HASH_SMALL_MODULUS
;
6867 map_size
= DRT_SMALL_ALLOCATION
;
6869 /* count the number of active buckets in the old map */
6871 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
6872 if (!DRT_HASH_VACANT(ocmap
, i
) &&
6873 (DRT_HASH_GET_COUNT(ocmap
, i
) != 0)) {
6878 * If we're currently using the small allocation, check to
6879 * see whether we should grow to the large one.
6881 if (ocmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) {
6883 * If the ring is nearly full and we are allowed to
6884 * use the large modulus, upgrade.
6886 if ((active_buckets
> (DRT_HASH_SMALL_MODULUS
- 5)) &&
6887 (max_mem
>= DRT_HASH_LARGE_MEMORY_REQUIRED
)) {
6888 modulus_size
= DRT_HASH_LARGE_MODULUS
;
6889 map_size
= DRT_LARGE_ALLOCATION
;
6891 modulus_size
= DRT_HASH_SMALL_MODULUS
;
6892 map_size
= DRT_SMALL_ALLOCATION
;
6894 } else if (ocmap
->scm_modulus
== DRT_HASH_LARGE_MODULUS
) {
6895 if ((active_buckets
> (DRT_HASH_LARGE_MODULUS
- 5)) &&
6896 (max_mem
>= DRT_HASH_XLARGE_MEMORY_REQUIRED
)) {
6897 modulus_size
= DRT_HASH_XLARGE_MODULUS
;
6898 map_size
= DRT_XLARGE_ALLOCATION
;
6900 modulus_size
= DRT_HASH_LARGE_MODULUS
;
6901 map_size
= DRT_LARGE_ALLOCATION
;
6904 /* already using the xlarge modulus */
6905 modulus_size
= DRT_HASH_XLARGE_MODULUS
;
6906 map_size
= DRT_XLARGE_ALLOCATION
;
6909 * If the ring is completely full, there's
6910 * nothing useful for us to do. Behave as
6911 * though we had compacted into the new
6914 if (active_buckets
>= DRT_HASH_XLARGE_MODULUS
) {
6915 return KERN_SUCCESS
;
6921 * Allocate and initialise the new map.
6924 kret
= kmem_alloc(kernel_map
, (vm_offset_t
*)&cmap
, map_size
, VM_KERN_MEMORY_FILE
);
6925 if (kret
!= KERN_SUCCESS
) {
6928 cmap
->scm_magic
= DRT_SCM_MAGIC
;
6929 cmap
->scm_modulus
= modulus_size
;
6930 cmap
->scm_buckets
= 0;
6931 cmap
->scm_lastclean
= 0;
6932 cmap
->scm_iskips
= 0;
6933 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
6934 DRT_HASH_CLEAR(cmap
, i
);
6935 DRT_HASH_VACATE(cmap
, i
);
6936 DRT_BITVECTOR_CLEAR(cmap
, i
);
6940 * If there's an old map, re-hash entries from it into the new map.
6943 if (ocmap
!= NULL
) {
6944 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
6945 /* skip empty buckets */
6946 if (DRT_HASH_VACANT(ocmap
, i
) ||
6947 (DRT_HASH_GET_COUNT(ocmap
, i
) == 0)) {
6951 offset
= DRT_HASH_GET_ADDRESS(ocmap
, i
);
6952 kret
= vfs_drt_get_index(&cmap
, offset
, &index
, 1);
6953 if (kret
!= KERN_SUCCESS
) {
6954 /* XXX need to bail out gracefully here */
6955 panic("vfs_drt: new cluster map mysteriously too small");
6959 DRT_HASH_COPY(ocmap
, i
, cmap
, index
);
6964 /* log what we've done */
6965 vfs_drt_trace(cmap
, DRT_DEBUG_ALLOC
, copycount
, 0, 0, 0);
6968 * It's important to ensure that *cmapp always points to
6969 * a valid map, so we must overwrite it before freeing
6973 if (ocmap
!= NULL
) {
6974 /* emit stats into trace buffer */
6975 vfs_drt_trace(ocmap
, DRT_DEBUG_SCMDATA
,
6978 ocmap
->scm_lastclean
,
6981 vfs_drt_free_map(ocmap
);
6983 return KERN_SUCCESS
;
6988 * Free a sparse cluster map.
6990 static kern_return_t
6991 vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
)
6993 vm_size_t map_size
= 0;
6995 if (cmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) {
6996 map_size
= DRT_SMALL_ALLOCATION
;
6997 } else if (cmap
->scm_modulus
== DRT_HASH_LARGE_MODULUS
) {
6998 map_size
= DRT_LARGE_ALLOCATION
;
6999 } else if (cmap
->scm_modulus
== DRT_HASH_XLARGE_MODULUS
) {
7000 map_size
= DRT_XLARGE_ALLOCATION
;
7002 panic("vfs_drt_free_map: Invalid modulus %d\n", cmap
->scm_modulus
);
7005 kmem_free(kernel_map
, (vm_offset_t
)cmap
, map_size
);
7006 return KERN_SUCCESS
;
7011 * Find the hashtable slot currently occupied by an entry for the supplied offset.
7013 static kern_return_t
7014 vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
, u_int64_t offset
, int *indexp
)
7019 offset
= DRT_ALIGN_ADDRESS(offset
);
7020 index
= DRT_HASH(cmap
, offset
);
7022 /* traverse the hashtable */
7023 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
7025 * If the slot is vacant, we can stop.
7027 if (DRT_HASH_VACANT(cmap
, index
)) {
7032 * If the address matches our offset, we have success.
7034 if (DRT_HASH_GET_ADDRESS(cmap
, index
) == offset
) {
7036 return KERN_SUCCESS
;
7040 * Move to the next slot, try again.
7042 index
= DRT_HASH_NEXT(cmap
, index
);
7047 return KERN_FAILURE
;
7051 * Find the hashtable slot for the supplied offset. If we haven't allocated
7052 * one yet, allocate one and populate the address field. Note that it will
7053 * not have a nonzero page count and thus will still technically be free, so
7054 * in the case where we are called to clean pages, the slot will remain free.
7056 static kern_return_t
7057 vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
, u_int64_t offset
, int *indexp
, int recursed
)
7059 struct vfs_drt_clustermap
*cmap
;
7066 /* look for an existing entry */
7067 kret
= vfs_drt_search_index(cmap
, offset
, indexp
);
7068 if (kret
== KERN_SUCCESS
) {
7072 /* need to allocate an entry */
7073 offset
= DRT_ALIGN_ADDRESS(offset
);
7074 index
= DRT_HASH(cmap
, offset
);
7076 /* scan from the index forwards looking for a vacant slot */
7077 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
7079 if (DRT_HASH_VACANT(cmap
, index
) || DRT_HASH_GET_COUNT(cmap
, index
) == 0) {
7080 cmap
->scm_buckets
++;
7081 if (index
< cmap
->scm_lastclean
) {
7082 cmap
->scm_lastclean
= index
;
7084 DRT_HASH_SET_ADDRESS(cmap
, index
, offset
);
7085 DRT_HASH_SET_COUNT(cmap
, index
, 0);
7086 DRT_BITVECTOR_CLEAR(cmap
, index
);
7088 vfs_drt_trace(cmap
, DRT_DEBUG_INSERT
, (int)offset
, i
, 0, 0);
7089 return KERN_SUCCESS
;
7091 cmap
->scm_iskips
+= i
;
7092 index
= DRT_HASH_NEXT(cmap
, index
);
7096 * We haven't found a vacant slot, so the map is full. If we're not
7097 * already recursed, try reallocating/compacting it.
7100 return KERN_FAILURE
;
7102 kret
= vfs_drt_alloc_map(cmapp
);
7103 if (kret
== KERN_SUCCESS
) {
7104 /* now try to insert again */
7105 kret
= vfs_drt_get_index(cmapp
, offset
, indexp
, 1);
7111 * Implementation of set dirty/clean.
7113 * In the 'clean' case, not finding a map is OK.
7115 static kern_return_t
7116 vfs_drt_do_mark_pages(
7123 struct vfs_drt_clustermap
*cmap
, **cmapp
;
7125 int i
, index
, pgoff
, pgcount
, setcount
, ecount
;
7127 cmapp
= (struct vfs_drt_clustermap
**)private;
7130 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_START
, (int)offset
, (int)length
, dirty
, 0);
7132 if (setcountp
!= NULL
) {
7136 /* allocate a cluster map if we don't already have one */
7138 /* no cluster map, nothing to clean */
7140 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 1, 0, 0, 0);
7141 return KERN_SUCCESS
;
7143 kret
= vfs_drt_alloc_map(cmapp
);
7144 if (kret
!= KERN_SUCCESS
) {
7145 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 2, 0, 0, 0);
7152 * Iterate over the length of the region.
7154 while (length
> 0) {
7156 * Get the hashtable index for this offset.
7158 * XXX this will add blank entries if we are clearing a range
7159 * that hasn't been dirtied.
7161 kret
= vfs_drt_get_index(cmapp
, offset
, &index
, 0);
7162 cmap
= *cmapp
; /* may have changed! */
7163 /* this may be a partial-success return */
7164 if (kret
!= KERN_SUCCESS
) {
7165 if (setcountp
!= NULL
) {
7166 *setcountp
= setcount
;
7168 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 3, (int)length
, 0, 0);
7174 * Work out how many pages we're modifying in this
7177 pgoff
= (offset
- DRT_ALIGN_ADDRESS(offset
)) / PAGE_SIZE
;
7178 pgcount
= min((length
/ PAGE_SIZE
), (DRT_BITVECTOR_PAGES
- pgoff
));
7181 * Iterate over pages, dirty/clearing as we go.
7183 ecount
= DRT_HASH_GET_COUNT(cmap
, index
);
7184 for (i
= 0; i
< pgcount
; i
++) {
7186 if (!DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
7187 if (ecount
>= DRT_BITVECTOR_PAGES
) {
7188 panic("ecount >= DRT_BITVECTOR_PAGES, cmap = %p, index = %d, bit = %d", cmap
, index
, pgoff
+ i
);
7190 DRT_HASH_SET_BIT(cmap
, index
, pgoff
+ i
);
7195 if (DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
7197 panic("ecount <= 0, cmap = %p, index = %d, bit = %d", cmap
, index
, pgoff
+ i
);
7200 DRT_HASH_CLEAR_BIT(cmap
, index
, pgoff
+ i
);
7206 DRT_HASH_SET_COUNT(cmap
, index
, ecount
);
7208 offset
+= pgcount
* PAGE_SIZE
;
7209 length
-= pgcount
* PAGE_SIZE
;
7211 if (setcountp
!= NULL
) {
7212 *setcountp
= setcount
;
7215 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 0, setcount
, 0, 0);
7217 return KERN_SUCCESS
;
7221 * Mark a set of pages as dirty/clean.
7223 * This is a public interface.
7226 * Pointer to storage suitable for holding a pointer. Note that
7227 * this must either be NULL or a value set by this function.
7230 * Current file size in bytes.
7233 * Offset of the first page to be marked as dirty, in bytes. Must be
7237 * Length of dirty region, in bytes. Must be a multiple of PAGE_SIZE.
7240 * Number of pages newly marked dirty by this call (optional).
7242 * Returns KERN_SUCCESS if all the pages were successfully marked.
7244 static kern_return_t
7245 vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
)
7247 /* XXX size unused, drop from interface */
7248 return vfs_drt_do_mark_pages(cmapp
, offset
, length
, setcountp
, 1);
7252 static kern_return_t
7253 vfs_drt_unmark_pages(void **cmapp
, off_t offset
, u_int length
)
7255 return vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0);
7260 * Get a cluster of dirty pages.
7262 * This is a public interface.
7265 * Pointer to storage managed by drt_mark_pages. Note that this must
7266 * be NULL or a value set by drt_mark_pages.
7269 * Returns the byte offset into the file of the first page in the cluster.
7272 * Returns the length in bytes of the cluster of dirty pages.
7274 * Returns success if a cluster was found. If KERN_FAILURE is returned, there
7275 * are no dirty pages meeting the minmum size criteria. Private storage will
7276 * be released if there are no more dirty pages left in the map
7279 static kern_return_t
7280 vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
)
7282 struct vfs_drt_clustermap
*cmap
;
7286 int index
, i
, fs
, ls
;
7289 if ((cmapp
== NULL
) || (*cmapp
== NULL
)) {
7290 return KERN_FAILURE
;
7294 /* walk the hashtable */
7295 for (offset
= 0, j
= 0; j
< cmap
->scm_modulus
; offset
+= (DRT_BITVECTOR_PAGES
* PAGE_SIZE
), j
++) {
7296 index
= DRT_HASH(cmap
, offset
);
7298 if (DRT_HASH_VACANT(cmap
, index
) || (DRT_HASH_GET_COUNT(cmap
, index
) == 0)) {
7302 /* scan the bitfield for a string of bits */
7305 for (i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
7306 if (DRT_HASH_TEST_BIT(cmap
, index
, i
)) {
7312 /* didn't find any bits set */
7313 panic("vfs_drt: entry summary count > 0 but no bits set in map, cmap = %p, index = %d, count = %lld",
7314 cmap
, index
, DRT_HASH_GET_COUNT(cmap
, index
));
7316 for (ls
= 0; i
< DRT_BITVECTOR_PAGES
; i
++, ls
++) {
7317 if (!DRT_HASH_TEST_BIT(cmap
, index
, i
)) {
7322 /* compute offset and length, mark pages clean */
7323 offset
= DRT_HASH_GET_ADDRESS(cmap
, index
) + (PAGE_SIZE
* fs
);
7324 length
= ls
* PAGE_SIZE
;
7325 vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0);
7326 cmap
->scm_lastclean
= index
;
7328 /* return successful */
7329 *offsetp
= (off_t
)offset
;
7332 vfs_drt_trace(cmap
, DRT_DEBUG_RETCLUSTER
, (int)offset
, (int)length
, 0, 0);
7333 return KERN_SUCCESS
;
7336 * We didn't find anything... hashtable is empty
7337 * emit stats into trace buffer and
7340 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
7343 cmap
->scm_lastclean
,
7346 vfs_drt_free_map(cmap
);
7349 return KERN_FAILURE
;
7353 static kern_return_t
7354 vfs_drt_control(void **cmapp
, int op_type
)
7356 struct vfs_drt_clustermap
*cmap
;
7359 if ((cmapp
== NULL
) || (*cmapp
== NULL
)) {
7360 return KERN_FAILURE
;
7366 /* emit stats into trace buffer */
7367 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
7370 cmap
->scm_lastclean
,
7373 vfs_drt_free_map(cmap
);
7378 cmap
->scm_lastclean
= 0;
7381 return KERN_SUCCESS
;
7387 * Emit a summary of the state of the clustermap into the trace buffer
7388 * along with some caller-provided data.
7392 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, int code
, int arg1
, int arg2
, int arg3
, int arg4
)
7394 KERNEL_DEBUG(code
, arg1
, arg2
, arg3
, arg4
, 0);
7398 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, __unused
int code
,
7399 __unused
int arg1
, __unused
int arg2
, __unused
int arg3
,
7407 * Perform basic sanity check on the hash entry summary count
7408 * vs. the actual bits set in the entry.
7411 vfs_drt_sanity(struct vfs_drt_clustermap
*cmap
)
7416 for (index
= 0; index
< cmap
->scm_modulus
; index
++) {
7417 if (DRT_HASH_VACANT(cmap
, index
)) {
7421 for (bits_on
= 0, i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
7422 if (DRT_HASH_TEST_BIT(cmap
, index
, i
)) {
7426 if (bits_on
!= DRT_HASH_GET_COUNT(cmap
, index
)) {
7427 panic("bits_on = %d, index = %d\n", bits_on
, index
);
7434 * Internal interface only.
7436 static kern_return_t
7437 vfs_get_scmap_push_behavior_internal(void **cmapp
, int *push_flag
)
7439 struct vfs_drt_clustermap
*cmap
;
7442 if ((cmapp
== NULL
) || (*cmapp
== NULL
) || (push_flag
== NULL
)) {
7443 return KERN_FAILURE
;
7447 if (cmap
->scm_modulus
== DRT_HASH_XLARGE_MODULUS
) {
7449 * If we have a full xlarge sparse cluster,
7450 * we push it out all at once so the cluster
7451 * map can be available to absorb more I/Os.
7452 * This is done on large memory configs so
7453 * the small I/Os don't interfere with the
7456 *push_flag
= PUSH_ALL
;
7458 return KERN_SUCCESS
;