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>
103 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
108 #define CL_WRITE 0x02
109 #define CL_ASYNC 0x04
110 #define CL_COMMIT 0x08
111 #define CL_PAGEOUT 0x10
113 #define CL_NOZERO 0x40
114 #define CL_PAGEIN 0x80
115 #define CL_DEV_MEMORY 0x100
116 #define CL_PRESERVE 0x200
117 #define CL_THROTTLE 0x400
118 #define CL_KEEPCACHED 0x800
119 #define CL_DIRECT_IO 0x1000
120 #define CL_PASSIVE 0x2000
121 #define CL_IOSTREAMING 0x4000
122 #define CL_CLOSE 0x8000
123 #define CL_ENCRYPTED 0x10000
124 #define CL_RAW_ENCRYPTED 0x20000
125 #define CL_NOCACHE 0x40000
127 #define MAX_VECTOR_UPL_ELEMENTS 8
128 #define MAX_VECTOR_UPL_SIZE (2 * MAX_UPL_SIZE_BYTES)
130 #define CLUSTER_IO_WAITING ((buf_t)1)
132 extern upl_t
vector_upl_create(vm_offset_t
);
133 extern boolean_t
vector_upl_is_valid(upl_t
);
134 extern boolean_t
vector_upl_set_subupl(upl_t
,upl_t
, u_int32_t
);
135 extern void vector_upl_set_pagelist(upl_t
);
136 extern void vector_upl_set_iostate(upl_t
, upl_t
, vm_offset_t
, u_int32_t
);
140 u_int io_completed
; /* amount of io that has currently completed */
141 u_int io_issued
; /* amount of io that was successfully issued */
142 int io_error
; /* error code of first error encountered */
143 int io_wanted
; /* someone is sleeping waiting for a change in state */
146 struct cl_direct_read_lock
{
147 LIST_ENTRY(cl_direct_read_lock
) chain
;
153 #define CL_DIRECT_READ_LOCK_BUCKETS 61
155 static LIST_HEAD(cl_direct_read_locks
, cl_direct_read_lock
)
156 cl_direct_read_locks
[CL_DIRECT_READ_LOCK_BUCKETS
];
158 static lck_spin_t cl_direct_read_spin_lock
;
160 static lck_grp_t
*cl_mtx_grp
;
161 static lck_attr_t
*cl_mtx_attr
;
162 static lck_grp_attr_t
*cl_mtx_grp_attr
;
163 static lck_mtx_t
*cl_transaction_mtxp
;
170 #define PUSH_DELAY 0x01
171 #define PUSH_ALL 0x02
172 #define PUSH_SYNC 0x04
175 static void cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
);
176 static void cluster_wait_IO(buf_t cbp_head
, int async
);
177 static void cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
);
179 static int cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
);
181 static int cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
182 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*)(buf_t
, void *), void *callback_arg
);
183 static int cluster_iodone(buf_t bp
, void *callback_arg
);
184 static int cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
, vnode_t vp
);
185 static int cluster_is_throttled(vnode_t vp
);
187 static void cluster_iostate_wait(struct clios
*iostate
, u_int target
, const char *wait_name
);
189 static void cluster_syncup(vnode_t vp
, off_t newEOF
, int (*)(buf_t
, void *), void *callback_arg
, int flags
);
191 static void cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int take_reference
);
192 static int cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
);
194 static int cluster_read_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t filesize
, int flags
,
195 int (*)(buf_t
, void *), void *callback_arg
);
196 static int cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
197 int flags
, int (*)(buf_t
, void *), void *callback_arg
);
198 static int cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
199 int (*)(buf_t
, void *), void *callback_arg
, int flags
);
201 static int cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
,
202 off_t headOff
, off_t tailOff
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
203 static int cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
,
204 int *write_type
, u_int32_t
*write_length
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
205 static int cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
,
206 int *write_type
, u_int32_t
*write_length
, int (*)(buf_t
, void *), void *callback_arg
, int bflag
);
208 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
);
210 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
);
211 static void cluster_read_ahead(vnode_t vp
, struct cl_extent
*extent
, off_t filesize
, struct cl_readahead
*ra
, int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
);
213 static int cluster_push_now(vnode_t vp
, struct cl_extent
*, off_t EOF
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
215 static int cluster_try_push(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int push_flag
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
217 static void sparse_cluster_switch(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int (*)(buf_t
, void *), void *callback_arg
);
218 static void sparse_cluster_push(void **cmapp
, vnode_t vp
, off_t EOF
, int push_flag
, int io_flags
, int (*)(buf_t
, void *), void *callback_arg
);
219 static void sparse_cluster_add(void **cmapp
, vnode_t vp
, struct cl_extent
*, off_t EOF
, int (*)(buf_t
, void *), void *callback_arg
);
221 static kern_return_t
vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
);
222 static kern_return_t
vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
);
223 static kern_return_t
vfs_drt_control(void **cmapp
, int op_type
);
227 * For throttled IO to check whether
228 * a block is cached by the boot cache
229 * and thus it can avoid delaying the IO.
231 * bootcache_contains_block is initially
232 * NULL. The BootCache will set it while
233 * the cache is active and clear it when
234 * the cache is jettisoned.
236 * Returns 0 if the block is not
237 * contained in the cache, 1 if it is
240 * The function pointer remains valid
241 * after the cache has been evicted even
242 * if bootcache_contains_block has been
245 * See rdar://9974130 The new throttling mechanism breaks the boot cache for throttled IOs
247 int (*bootcache_contains_block
)(dev_t device
, u_int64_t blkno
) = NULL
;
251 * limit the internal I/O size so that we
252 * can represent it in a 32 bit int
254 #define MAX_IO_REQUEST_SIZE (1024 * 1024 * 512)
255 #define MAX_IO_CONTIG_SIZE MAX_UPL_SIZE_BYTES
258 * The MIN_DIRECT_WRITE_SIZE governs how much I/O should be issued before we consider
259 * allowing the caller to bypass the buffer cache. For small I/Os (less than 16k),
260 * we have not historically allowed the write to bypass the UBC.
262 #define MIN_DIRECT_WRITE_SIZE (16384)
264 #define WRITE_THROTTLE 6
265 #define WRITE_THROTTLE_SSD 2
266 #define WRITE_BEHIND 1
267 #define WRITE_BEHIND_SSD 1
270 #define PREFETCH_SSD 2
271 uint32_t speculative_prefetch_max
= (MAX_UPL_SIZE_BYTES
* 3); /* maximum bytes in a specluative read-ahead */
272 uint32_t speculative_prefetch_max_iosize
= (512 * 1024); /* maximum I/O size to use in a specluative read-ahead on SSDs*/
275 #define IO_SCALE(vp, base) (vp->v_mount->mnt_ioscale * (base))
276 #define MAX_CLUSTER_SIZE(vp) (cluster_max_io_size(vp->v_mount, CL_WRITE))
277 #define MAX_PREFETCH(vp, size, is_ssd) (size * IO_SCALE(vp, ((is_ssd && !ignore_is_ssd) ? PREFETCH_SSD : PREFETCH)))
279 int ignore_is_ssd
= 0;
280 int speculative_reads_disabled
= 0;
283 * throttle the number of async writes that
284 * can be outstanding on a single vnode
285 * before we issue a synchronous write
287 #define THROTTLE_MAXCNT 0
289 uint32_t throttle_max_iosize
= (128 * 1024);
291 #define THROTTLE_MAX_IOSIZE (throttle_max_iosize)
293 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_max_iosize
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_max_iosize
, 0, "");
299 * allocate lock group attribute and group
301 cl_mtx_grp_attr
= lck_grp_attr_alloc_init();
302 cl_mtx_grp
= lck_grp_alloc_init("cluster I/O", cl_mtx_grp_attr
);
305 * allocate the lock attribute
307 cl_mtx_attr
= lck_attr_alloc_init();
309 cl_transaction_mtxp
= lck_mtx_alloc_init(cl_mtx_grp
, cl_mtx_attr
);
311 if (cl_transaction_mtxp
== NULL
)
312 panic("cluster_init: failed to allocate cl_transaction_mtxp");
314 lck_spin_init(&cl_direct_read_spin_lock
, cl_mtx_grp
, cl_mtx_attr
);
316 for (int i
= 0; i
< CL_DIRECT_READ_LOCK_BUCKETS
; ++i
)
317 LIST_INIT(&cl_direct_read_locks
[i
]);
322 cluster_max_io_size(mount_t mp
, int type
)
324 uint32_t max_io_size
;
331 segcnt
= mp
->mnt_segreadcnt
;
332 maxcnt
= mp
->mnt_maxreadcnt
;
335 segcnt
= mp
->mnt_segwritecnt
;
336 maxcnt
= mp
->mnt_maxwritecnt
;
339 segcnt
= min(mp
->mnt_segreadcnt
, mp
->mnt_segwritecnt
);
340 maxcnt
= min(mp
->mnt_maxreadcnt
, mp
->mnt_maxwritecnt
);
343 if (segcnt
> (MAX_UPL_SIZE_BYTES
>> PAGE_SHIFT
)) {
345 * don't allow a size beyond the max UPL size we can create
347 segcnt
= MAX_UPL_SIZE_BYTES
>> PAGE_SHIFT
;
349 max_io_size
= min((segcnt
* PAGE_SIZE
), maxcnt
);
351 if (max_io_size
< MAX_UPL_TRANSFER_BYTES
) {
353 * don't allow a size smaller than the old fixed limit
355 max_io_size
= MAX_UPL_TRANSFER_BYTES
;
358 * make sure the size specified is a multiple of PAGE_SIZE
360 max_io_size
&= ~PAGE_MASK
;
362 return (max_io_size
);
368 #define CLW_ALLOCATE 0x01
369 #define CLW_RETURNLOCKED 0x02
370 #define CLW_IONOCACHE 0x04
371 #define CLW_IOPASSIVE 0x08
374 * if the read ahead context doesn't yet exist,
375 * allocate and initialize it...
376 * the vnode lock serializes multiple callers
377 * during the actual assignment... first one
378 * to grab the lock wins... the other callers
379 * will release the now unnecessary storage
381 * once the context is present, try to grab (but don't block on)
382 * the lock associated with it... if someone
383 * else currently owns it, than the read
384 * will run without read-ahead. this allows
385 * multiple readers to run in parallel and
386 * since there's only 1 read ahead context,
387 * there's no real loss in only allowing 1
388 * reader to have read-ahead enabled.
390 static struct cl_readahead
*
391 cluster_get_rap(vnode_t vp
)
393 struct ubc_info
*ubc
;
394 struct cl_readahead
*rap
;
398 if ((rap
= ubc
->cl_rahead
) == NULL
) {
399 MALLOC_ZONE(rap
, struct cl_readahead
*, sizeof *rap
, M_CLRDAHEAD
, M_WAITOK
);
401 bzero(rap
, sizeof *rap
);
403 lck_mtx_init(&rap
->cl_lockr
, cl_mtx_grp
, cl_mtx_attr
);
407 if (ubc
->cl_rahead
== NULL
)
408 ubc
->cl_rahead
= rap
;
410 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
411 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
412 rap
= ubc
->cl_rahead
;
416 if (lck_mtx_try_lock(&rap
->cl_lockr
) == TRUE
)
419 return ((struct cl_readahead
*)NULL
);
424 * if the write behind context doesn't yet exist,
425 * and CLW_ALLOCATE is specified, allocate and initialize it...
426 * the vnode lock serializes multiple callers
427 * during the actual assignment... first one
428 * to grab the lock wins... the other callers
429 * will release the now unnecessary storage
431 * if CLW_RETURNLOCKED is set, grab (blocking if necessary)
432 * the lock associated with the write behind context before
436 static struct cl_writebehind
*
437 cluster_get_wbp(vnode_t vp
, int flags
)
439 struct ubc_info
*ubc
;
440 struct cl_writebehind
*wbp
;
444 if ((wbp
= ubc
->cl_wbehind
) == NULL
) {
446 if ( !(flags
& CLW_ALLOCATE
))
447 return ((struct cl_writebehind
*)NULL
);
449 MALLOC_ZONE(wbp
, struct cl_writebehind
*, sizeof *wbp
, M_CLWRBEHIND
, M_WAITOK
);
451 bzero(wbp
, sizeof *wbp
);
452 lck_mtx_init(&wbp
->cl_lockw
, cl_mtx_grp
, cl_mtx_attr
);
456 if (ubc
->cl_wbehind
== NULL
)
457 ubc
->cl_wbehind
= wbp
;
459 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
460 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
461 wbp
= ubc
->cl_wbehind
;
465 if (flags
& CLW_RETURNLOCKED
)
466 lck_mtx_lock(&wbp
->cl_lockw
);
473 cluster_syncup(vnode_t vp
, off_t newEOF
, int (*callback
)(buf_t
, void *), void *callback_arg
, int flags
)
475 struct cl_writebehind
*wbp
;
477 if ((wbp
= cluster_get_wbp(vp
, 0)) != NULL
) {
479 if (wbp
->cl_number
) {
480 lck_mtx_lock(&wbp
->cl_lockw
);
482 cluster_try_push(wbp
, vp
, newEOF
, PUSH_ALL
| flags
, 0, callback
, callback_arg
);
484 lck_mtx_unlock(&wbp
->cl_lockw
);
491 cluster_io_present_in_BC(vnode_t vp
, off_t f_offset
)
495 int (*bootcache_check_fn
)(dev_t device
, u_int64_t blkno
) = bootcache_contains_block
;
497 if (bootcache_check_fn
) {
498 if (VNOP_BLOCKMAP(vp
, f_offset
, PAGE_SIZE
, &blkno
, &io_size
, NULL
, VNODE_READ
, NULL
))
504 if (bootcache_check_fn(vp
->v_mount
->mnt_devvp
->v_rdev
, blkno
))
512 cluster_is_throttled(vnode_t vp
)
514 return (throttle_io_will_be_throttled(-1, vp
->v_mount
));
519 cluster_iostate_wait(struct clios
*iostate
, u_int target
, const char *wait_name
)
522 lck_mtx_lock(&iostate
->io_mtxp
);
524 while ((iostate
->io_issued
- iostate
->io_completed
) > target
) {
526 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
527 iostate
->io_issued
, iostate
->io_completed
, target
, 0, 0);
529 iostate
->io_wanted
= 1;
530 msleep((caddr_t
)&iostate
->io_wanted
, &iostate
->io_mtxp
, PRIBIO
+ 1, wait_name
, NULL
);
532 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
533 iostate
->io_issued
, iostate
->io_completed
, target
, 0, 0);
535 lck_mtx_unlock(&iostate
->io_mtxp
);
538 static void cluster_handle_associated_upl(struct clios
*iostate
, upl_t upl
,
539 upl_offset_t upl_offset
, upl_size_t size
)
544 upl_t associated_upl
= upl_associated_upl(upl
);
550 printf("1: %d %d\n", upl_offset
, upl_offset
+ size
);
554 * The associated UPL is page aligned to file offsets whereas the
555 * UPL it's attached to has different alignment requirements. The
556 * upl_offset that we have refers to @upl. The code that follows
557 * has to deal with the first and last pages in this transaction
558 * which might straddle pages in the associated UPL. To keep
559 * track of these pages, we use the mark bits: if the mark bit is
560 * set, we know another transaction has completed its part of that
561 * page and so we can unlock that page here.
563 * The following illustrates what we have to deal with:
565 * MEM u <------------ 1 PAGE ------------> e
566 * +-------------+----------------------+-----------------
567 * | |######################|#################
568 * +-------------+----------------------+-----------------
569 * FILE | <--- a ---> o <------------ 1 PAGE ------------>
571 * So here we show a write to offset @o. The data that is to be
572 * written is in a buffer that is not page aligned; it has offset
573 * @a in the page. The upl that carries the data starts in memory
574 * at @u. The associated upl starts in the file at offset @o. A
575 * transaction will always end on a page boundary (like @e above)
576 * except for the very last transaction in the group. We cannot
577 * unlock the page at @o in the associated upl until both the
578 * transaction ending at @e and the following transaction (that
579 * starts at @e) has completed.
583 * We record whether or not the two UPLs are aligned as the mark
584 * bit in the first page of @upl.
586 upl_page_info_t
*pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
587 bool is_unaligned
= upl_page_get_mark(pl
, 0);
590 upl_page_info_t
*assoc_pl
= UPL_GET_INTERNAL_PAGE_LIST(associated_upl
);
592 upl_offset_t upl_end
= upl_offset
+ size
;
593 assert(upl_end
>= PAGE_SIZE
);
595 upl_size_t assoc_upl_size
= upl_get_size(associated_upl
);
598 * In the very first transaction in the group, upl_offset will
599 * not be page aligned, but after that it will be and in that
600 * case we want the preceding page in the associated UPL hence
605 upl_offset
= trunc_page_32(upl_offset
- 1);
607 lck_mtx_lock_spin(&iostate
->io_mtxp
);
609 // Look at the first page...
611 && !upl_page_get_mark(assoc_pl
, upl_offset
>> PAGE_SHIFT
)) {
613 * The first page isn't marked so let another transaction
614 * completion handle it.
616 upl_page_set_mark(assoc_pl
, upl_offset
>> PAGE_SHIFT
, true);
617 upl_offset
+= PAGE_SIZE
;
620 // And now the last page...
623 * This needs to be > rather than >= because if it's equal, it
624 * means there's another transaction that is sharing the last
627 if (upl_end
> assoc_upl_size
)
628 upl_end
= assoc_upl_size
;
630 upl_end
= trunc_page_32(upl_end
);
631 const int last_pg
= (upl_end
>> PAGE_SHIFT
) - 1;
633 if (!upl_page_get_mark(assoc_pl
, last_pg
)) {
635 * The last page isn't marked so mark the page and let another
636 * transaction completion handle it.
638 upl_page_set_mark(assoc_pl
, last_pg
, true);
639 upl_end
-= PAGE_SIZE
;
643 lck_mtx_unlock(&iostate
->io_mtxp
);
646 printf("2: %d %d\n", upl_offset
, upl_end
);
649 if (upl_end
<= upl_offset
)
652 size
= upl_end
- upl_offset
;
654 assert(!(upl_offset
& PAGE_MASK
));
655 assert(!(size
& PAGE_MASK
));
661 * We can unlock these pages now and as this is for a
662 * direct/uncached write, we want to dump the pages too.
664 kern_return_t kr
= upl_abort_range(associated_upl
, upl_offset
, size
,
665 UPL_ABORT_DUMP_PAGES
, &empty
);
670 upl_set_associated_upl(upl
, NULL
);
671 upl_deallocate(associated_upl
);
676 cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
, vnode_t vp
)
678 int upl_abort_code
= 0;
682 if ((io_flags
& (B_PHYS
| B_CACHE
)) == (B_PHYS
| B_CACHE
))
684 * direct write of any flavor, or a direct read that wasn't aligned
686 ubc_upl_commit_range(upl
, upl_offset
, abort_size
, UPL_COMMIT_FREE_ON_EMPTY
);
688 if (io_flags
& B_PAGEIO
) {
689 if (io_flags
& B_READ
)
694 if (io_flags
& B_CACHE
)
696 * leave pages in the cache unchanged on error
698 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
699 else if (page_out
&& ((error
!= ENXIO
) || vnode_isswap(vp
)))
701 * transient error... leave pages unchanged
703 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
705 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
707 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
709 ubc_upl_abort_range(upl
, upl_offset
, abort_size
, upl_abort_code
);
711 return (upl_abort_code
);
716 cluster_iodone(buf_t bp
, void *callback_arg
)
727 int transaction_size
= 0;
734 struct clios
*iostate
;
735 boolean_t transaction_complete
= FALSE
;
737 __IGNORE_WCASTALIGN(cbp_head
= (buf_t
)(bp
->b_trans_head
));
739 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_START
,
740 cbp_head
, bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
742 if (cbp_head
->b_trans_next
|| !(cbp_head
->b_flags
& B_EOT
)) {
743 lck_mtx_lock_spin(cl_transaction_mtxp
);
745 bp
->b_flags
|= B_TDONE
;
747 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
749 * all I/O requests that are part of this transaction
750 * have to complete before we can process it
752 if ( !(cbp
->b_flags
& B_TDONE
)) {
754 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
755 cbp_head
, cbp
, cbp
->b_bcount
, cbp
->b_flags
, 0);
757 lck_mtx_unlock(cl_transaction_mtxp
);
762 if (cbp
->b_trans_next
== CLUSTER_IO_WAITING
) {
763 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
764 cbp_head
, cbp
, cbp
->b_bcount
, cbp
->b_flags
, 0);
766 lck_mtx_unlock(cl_transaction_mtxp
);
772 if (cbp
->b_flags
& B_EOT
)
773 transaction_complete
= TRUE
;
775 lck_mtx_unlock(cl_transaction_mtxp
);
777 if (transaction_complete
== FALSE
) {
778 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
779 cbp_head
, 0, 0, 0, 0);
789 upl_offset
= cbp
->b_uploffset
;
791 b_flags
= cbp
->b_flags
;
792 real_bp
= cbp
->b_real_bp
;
793 zero_offset
= cbp
->b_validend
;
794 iostate
= (struct clios
*)cbp
->b_iostate
;
797 real_bp
->b_dev
= cbp
->b_dev
;
800 if ((cbp
->b_flags
& B_ERROR
) && error
== 0)
801 error
= cbp
->b_error
;
803 total_resid
+= cbp
->b_resid
;
804 total_size
+= cbp
->b_bcount
;
806 cbp_next
= cbp
->b_trans_next
;
808 if (cbp_next
== NULL
)
810 * compute the overall size of the transaction
811 * in case we created one that has 'holes' in it
812 * 'total_size' represents the amount of I/O we
813 * did, not the span of the transaction w/r to the UPL
815 transaction_size
= cbp
->b_uploffset
+ cbp
->b_bcount
- upl_offset
;
823 if (ISSET(b_flags
, B_COMMIT_UPL
)) {
824 cluster_handle_associated_upl(iostate
,
830 if (error
== 0 && total_resid
)
834 int (*cliodone_func
)(buf_t
, void *) = (int (*)(buf_t
, void *))(cbp_head
->b_cliodone
);
836 if (cliodone_func
!= NULL
) {
837 cbp_head
->b_bcount
= transaction_size
;
839 error
= (*cliodone_func
)(cbp_head
, callback_arg
);
843 cluster_zero(upl
, zero_offset
, PAGE_SIZE
- (zero_offset
& PAGE_MASK
), real_bp
);
845 free_io_buf(cbp_head
);
851 * someone has issued multiple I/Os asynchrounsly
852 * and is waiting for them to complete (streaming)
854 lck_mtx_lock_spin(&iostate
->io_mtxp
);
856 if (error
&& iostate
->io_error
== 0)
857 iostate
->io_error
= error
;
859 iostate
->io_completed
+= total_size
;
861 if (iostate
->io_wanted
) {
863 * someone is waiting for the state of
864 * this io stream to change
866 iostate
->io_wanted
= 0;
869 lck_mtx_unlock(&iostate
->io_mtxp
);
872 wakeup((caddr_t
)&iostate
->io_wanted
);
875 if (b_flags
& B_COMMIT_UPL
) {
876 pg_offset
= upl_offset
& PAGE_MASK
;
877 commit_size
= (pg_offset
+ transaction_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
880 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, commit_size
, error
, b_flags
, vp
);
882 upl_flags
= UPL_COMMIT_FREE_ON_EMPTY
;
884 if ((b_flags
& B_PHYS
) && (b_flags
& B_READ
))
885 upl_flags
|= UPL_COMMIT_SET_DIRTY
;
888 upl_flags
|= UPL_COMMIT_INACTIVATE
;
890 ubc_upl_commit_range(upl
, upl_offset
- pg_offset
, commit_size
, upl_flags
);
895 real_bp
->b_flags
|= B_ERROR
;
896 real_bp
->b_error
= error
;
898 real_bp
->b_resid
= total_resid
;
900 buf_biodone(real_bp
);
902 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
903 upl
, upl_offset
- pg_offset
, commit_size
, (error
<< 24) | upl_flags
, 0);
910 cluster_throttle_io_limit(vnode_t vp
, uint32_t *limit
)
912 if (cluster_is_throttled(vp
)) {
913 *limit
= THROTTLE_MAX_IOSIZE
;
921 cluster_zero(upl_t upl
, upl_offset_t upl_offset
, int size
, buf_t bp
)
924 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_START
,
925 upl_offset
, size
, bp
, 0, 0);
927 if (bp
== NULL
|| bp
->b_datap
== 0) {
931 pl
= ubc_upl_pageinfo(upl
);
933 if (upl_device_page(pl
) == TRUE
) {
934 zero_addr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + upl_offset
;
936 bzero_phys_nc(zero_addr
, size
);
943 page_index
= upl_offset
/ PAGE_SIZE
;
944 page_offset
= upl_offset
& PAGE_MASK
;
946 zero_addr
= ((addr64_t
)upl_phys_page(pl
, page_index
) << PAGE_SHIFT
) + page_offset
;
947 zero_cnt
= min(PAGE_SIZE
- page_offset
, size
);
949 bzero_phys(zero_addr
, zero_cnt
);
952 upl_offset
+= zero_cnt
;
956 bzero((caddr_t
)((vm_offset_t
)bp
->b_datap
+ upl_offset
), size
);
958 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_END
,
959 upl_offset
, size
, 0, 0, 0);
964 cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
)
966 cbp_head
->b_validend
= zero_offset
;
967 cbp_tail
->b_flags
|= B_EOT
;
971 cluster_wait_IO(buf_t cbp_head
, int async
)
977 * Async callback completion will not normally generate a
978 * wakeup upon I/O completion. To get woken up, we set
979 * b_trans_next (which is safe for us to modify) on the last
980 * buffer to CLUSTER_IO_WAITING so that cluster_iodone knows
981 * to wake us up when all buffers as part of this transaction
982 * are completed. This is done under the umbrella of
983 * cl_transaction_mtxp which is also taken in cluster_iodone.
988 lck_mtx_lock_spin(cl_transaction_mtxp
);
990 for (cbp
= cbp_head
; cbp
; last
= cbp
, cbp
= cbp
->b_trans_next
) {
991 if (!ISSET(cbp
->b_flags
, B_TDONE
))
996 last
->b_trans_next
= CLUSTER_IO_WAITING
;
998 DTRACE_IO1(wait__start
, buf_t
, last
);
1000 msleep(last
, cl_transaction_mtxp
, PSPIN
| (PRIBIO
+1), "cluster_wait_IO", NULL
);
1003 * We should only have been woken up if all the
1004 * buffers are completed, but just in case...
1007 for (cbp
= cbp_head
; cbp
!= CLUSTER_IO_WAITING
; cbp
= cbp
->b_trans_next
) {
1008 if (!ISSET(cbp
->b_flags
, B_TDONE
)) {
1014 DTRACE_IO1(wait__done
, buf_t
, last
);
1016 last
->b_trans_next
= NULL
;
1019 lck_mtx_unlock(cl_transaction_mtxp
);
1021 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
1027 cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
)
1031 boolean_t isswapout
= FALSE
;
1034 * cluster_complete_transaction will
1035 * only be called if we've issued a complete chain in synchronous mode
1036 * or, we've already done a cluster_wait_IO on an incomplete chain
1039 for (cbp
= *cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
1043 * we've already waited on all of the I/Os in this transaction,
1044 * so mark all of the buf_t's in this transaction as B_TDONE
1045 * so that cluster_iodone sees the transaction as completed
1047 for (cbp
= *cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
1048 cbp
->b_flags
|= B_TDONE
;
1051 if ((flags
& (CL_ASYNC
| CL_PAGEOUT
)) == CL_PAGEOUT
&& vnode_isswap(cbp
->b_vp
))
1054 error
= cluster_iodone(cbp
, callback_arg
);
1056 if ( !(flags
& CL_ASYNC
) && error
&& *retval
== 0) {
1057 if (((flags
& (CL_PAGEOUT
| CL_KEEPCACHED
)) != CL_PAGEOUT
) || (error
!= ENXIO
))
1059 else if (isswapout
== TRUE
)
1062 *cbp_head
= (buf_t
)NULL
;
1067 cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
1068 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1077 buf_t cbp_head
= NULL
;
1078 buf_t cbp_tail
= NULL
;
1079 int trans_count
= 0;
1080 int max_trans_count
;
1086 int zero_offset
= 0;
1087 int async_throttle
= 0;
1089 vm_offset_t upl_end_offset
;
1090 boolean_t need_EOT
= FALSE
;
1093 * we currently don't support buffers larger than a page
1095 if (real_bp
&& non_rounded_size
> PAGE_SIZE
)
1096 panic("%s(): Called with real buffer of size %d bytes which "
1097 "is greater than the maximum allowed size of "
1098 "%d bytes (the system PAGE_SIZE).\n",
1099 __FUNCTION__
, non_rounded_size
, PAGE_SIZE
);
1104 * we don't want to do any funny rounding of the size for IO requests
1105 * coming through the DIRECT or CONTIGUOUS paths... those pages don't
1106 * belong to us... we can't extend (nor do we need to) the I/O to fill
1109 if (mp
->mnt_devblocksize
> 1 && !(flags
& (CL_DEV_MEMORY
| CL_DIRECT_IO
))) {
1111 * round the requested size up so that this I/O ends on a
1112 * page boundary in case this is a 'write'... if the filesystem
1113 * has blocks allocated to back the page beyond the EOF, we want to
1114 * make sure to write out the zero's that are sitting beyond the EOF
1115 * so that in case the filesystem doesn't explicitly zero this area
1116 * if a hole is created via a lseek/write beyond the current EOF,
1117 * it will return zeros when it's read back from the disk. If the
1118 * physical allocation doesn't extend for the whole page, we'll
1119 * only write/read from the disk up to the end of this allocation
1120 * via the extent info returned from the VNOP_BLOCKMAP call.
1122 pg_offset
= upl_offset
& PAGE_MASK
;
1124 size
= (((non_rounded_size
+ pg_offset
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - pg_offset
;
1127 * anyone advertising a blocksize of 1 byte probably
1128 * can't deal with us rounding up the request size
1129 * AFP is one such filesystem/device
1131 size
= non_rounded_size
;
1133 upl_end_offset
= upl_offset
+ size
;
1135 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_START
, (int)f_offset
, size
, upl_offset
, flags
, 0);
1138 * Set the maximum transaction size to the maximum desired number of
1141 max_trans_count
= 8;
1142 if (flags
& CL_DEV_MEMORY
)
1143 max_trans_count
= 16;
1145 if (flags
& CL_READ
) {
1147 bmap_flags
= VNODE_READ
;
1149 max_iosize
= mp
->mnt_maxreadcnt
;
1150 max_vectors
= mp
->mnt_segreadcnt
;
1153 bmap_flags
= VNODE_WRITE
;
1155 max_iosize
= mp
->mnt_maxwritecnt
;
1156 max_vectors
= mp
->mnt_segwritecnt
;
1158 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_NONE
, max_iosize
, max_vectors
, mp
->mnt_devblocksize
, 0, 0);
1161 * make sure the maximum iosize is a
1162 * multiple of the page size
1164 max_iosize
&= ~PAGE_MASK
;
1167 * Ensure the maximum iosize is sensible.
1170 max_iosize
= PAGE_SIZE
;
1172 if (flags
& CL_THROTTLE
) {
1173 if ( !(flags
& CL_PAGEOUT
) && cluster_is_throttled(vp
)) {
1174 if (max_iosize
> THROTTLE_MAX_IOSIZE
)
1175 max_iosize
= THROTTLE_MAX_IOSIZE
;
1176 async_throttle
= THROTTLE_MAXCNT
;
1178 if ( (flags
& CL_DEV_MEMORY
) )
1179 async_throttle
= IO_SCALE(vp
, VNODE_ASYNC_THROTTLE
);
1182 u_int max_cluster_size
;
1185 if (vp
->v_mount
->mnt_minsaturationbytecount
) {
1186 max_cluster_size
= vp
->v_mount
->mnt_minsaturationbytecount
;
1190 max_cluster_size
= MAX_CLUSTER_SIZE(vp
);
1192 if ((vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
) && !ignore_is_ssd
)
1193 scale
= WRITE_THROTTLE_SSD
;
1195 scale
= WRITE_THROTTLE
;
1197 if (max_iosize
> max_cluster_size
)
1198 max_cluster
= max_cluster_size
;
1200 max_cluster
= max_iosize
;
1202 if (size
< max_cluster
)
1205 if (flags
& CL_CLOSE
)
1206 scale
+= MAX_CLUSTERS
;
1208 async_throttle
= min(IO_SCALE(vp
, VNODE_ASYNC_THROTTLE
), ((scale
* max_cluster_size
) / max_cluster
) - 1);
1214 if (flags
& (CL_PAGEIN
| CL_PAGEOUT
))
1215 io_flags
|= B_PAGEIO
;
1216 if (flags
& (CL_IOSTREAMING
))
1217 io_flags
|= B_IOSTREAMING
;
1218 if (flags
& CL_COMMIT
)
1219 io_flags
|= B_COMMIT_UPL
;
1220 if (flags
& CL_DIRECT_IO
)
1222 if (flags
& (CL_PRESERVE
| CL_KEEPCACHED
))
1223 io_flags
|= B_CACHE
;
1224 if (flags
& CL_PASSIVE
)
1225 io_flags
|= B_PASSIVE
;
1226 if (flags
& CL_ENCRYPTED
)
1227 io_flags
|= B_ENCRYPTED_IO
;
1229 if (vp
->v_flag
& VSYSTEM
)
1232 if ((flags
& CL_READ
) && ((upl_offset
+ non_rounded_size
) & PAGE_MASK
) && (!(flags
& CL_NOZERO
))) {
1234 * then we are going to end up
1235 * with a page that we can't complete (the file size wasn't a multiple
1236 * of PAGE_SIZE and we're trying to read to the end of the file
1237 * so we'll go ahead and zero out the portion of the page we can't
1238 * read in from the file
1240 zero_offset
= upl_offset
+ non_rounded_size
;
1241 } else if (!ISSET(flags
, CL_READ
) && ISSET(flags
, CL_DIRECT_IO
)) {
1242 assert(ISSET(flags
, CL_COMMIT
));
1244 // For a direct/uncached write, we need to lock pages...
1249 * Create a UPL to lock the pages in the cache whilst the
1250 * write is in progress.
1252 ubc_create_upl(vp
, f_offset
, non_rounded_size
, &cached_upl
,
1253 NULL
, UPL_SET_LITE
);
1256 * Attach this UPL to the other UPL so that we can find it
1259 upl_set_associated_upl(upl
, cached_upl
);
1261 if (upl_offset
& PAGE_MASK
) {
1263 * The two UPLs are not aligned, so mark the first page in
1264 * @upl so that cluster_handle_associated_upl can handle
1267 upl_page_info_t
*pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
1268 upl_page_set_mark(pl
, 0, true);
1275 u_int io_size_wanted
;
1278 if (size
> max_iosize
)
1279 io_size
= max_iosize
;
1283 io_size_wanted
= io_size
;
1284 io_size_tmp
= (size_t)io_size
;
1286 if ((error
= VNOP_BLOCKMAP(vp
, f_offset
, io_size
, &blkno
, &io_size_tmp
, NULL
, bmap_flags
, NULL
)))
1289 if (io_size_tmp
> io_size_wanted
)
1290 io_size
= io_size_wanted
;
1292 io_size
= (u_int
)io_size_tmp
;
1294 if (real_bp
&& (real_bp
->b_blkno
== real_bp
->b_lblkno
))
1295 real_bp
->b_blkno
= blkno
;
1297 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 24)) | DBG_FUNC_NONE
,
1298 (int)f_offset
, (int)(blkno
>>32), (int)blkno
, io_size
, 0);
1302 * vnop_blockmap didn't return an error... however, it did
1303 * return an extent size of 0 which means we can't
1304 * make forward progress on this I/O... a hole in the
1305 * file would be returned as a blkno of -1 with a non-zero io_size
1306 * a real extent is returned with a blkno != -1 and a non-zero io_size
1311 if ( !(flags
& CL_READ
) && blkno
== -1) {
1315 if (upl_get_internal_vectorupl(upl
))
1316 panic("Vector UPLs should not take this code-path\n");
1318 * we're writing into a 'hole'
1320 if (flags
& CL_PAGEOUT
) {
1322 * if we got here via cluster_pageout
1323 * then just error the request and return
1324 * the 'hole' should already have been covered
1330 * we can get here if the cluster code happens to
1331 * pick up a page that was dirtied via mmap vs
1332 * a 'write' and the page targets a 'hole'...
1333 * i.e. the writes to the cluster were sparse
1334 * and the file was being written for the first time
1336 * we can also get here if the filesystem supports
1337 * 'holes' that are less than PAGE_SIZE.... because
1338 * we can't know if the range in the page that covers
1339 * the 'hole' has been dirtied via an mmap or not,
1340 * we have to assume the worst and try to push the
1341 * entire page to storage.
1343 * Try paging out the page individually before
1344 * giving up entirely and dumping it (the pageout
1345 * path will insure that the zero extent accounting
1346 * has been taken care of before we get back into cluster_io)
1348 * go direct to vnode_pageout so that we don't have to
1349 * unbusy the page from the UPL... we used to do this
1350 * so that we could call ubc_msync, but that results
1351 * in a potential deadlock if someone else races us to acquire
1352 * that page and wins and in addition needs one of the pages
1353 * we're continuing to hold in the UPL
1355 pageout_flags
= UPL_MSYNC
| UPL_VNODE_PAGER
| UPL_NESTED_PAGEOUT
;
1357 if ( !(flags
& CL_ASYNC
))
1358 pageout_flags
|= UPL_IOSYNC
;
1359 if ( !(flags
& CL_COMMIT
))
1360 pageout_flags
|= UPL_NOCOMMIT
;
1366 * first we have to wait for the the current outstanding I/Os
1367 * to complete... EOT hasn't been set yet on this transaction
1368 * so the pages won't be released just because all of the current
1369 * I/O linked to this transaction has completed...
1371 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1374 * we've got a transcation that
1375 * includes the page we're about to push out through vnode_pageout...
1376 * find the last bp in the list which will be the one that
1377 * includes the head of this page and round it's iosize down
1378 * to a page boundary...
1380 for (last_cbp
= cbp
= cbp_head
; cbp
->b_trans_next
; cbp
= cbp
->b_trans_next
)
1383 cbp
->b_bcount
&= ~PAGE_MASK
;
1385 if (cbp
->b_bcount
== 0) {
1387 * this buf no longer has any I/O associated with it
1391 if (cbp
== cbp_head
) {
1393 * the buf we just freed was the only buf in
1394 * this transaction... so there's no I/O to do
1399 * remove the buf we just freed from
1400 * the transaction list
1402 last_cbp
->b_trans_next
= NULL
;
1403 cbp_tail
= last_cbp
;
1408 * there was more to the current transaction
1409 * than just the page we are pushing out via vnode_pageout...
1410 * mark it as finished and complete it... we've already
1411 * waited for the I/Os to complete above in the call to cluster_wait_IO
1413 cluster_EOT(cbp_head
, cbp_tail
, 0);
1415 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1420 if (vnode_pageout(vp
, upl
, trunc_page(upl_offset
), trunc_page_64(f_offset
), PAGE_SIZE
, pageout_flags
, NULL
) != PAGER_SUCCESS
) {
1423 e_offset
= round_page_64(f_offset
+ 1);
1424 io_size
= e_offset
- f_offset
;
1426 f_offset
+= io_size
;
1427 upl_offset
+= io_size
;
1429 if (size
>= io_size
)
1434 * keep track of how much of the original request
1435 * that we've actually completed... non_rounded_size
1436 * may go negative due to us rounding the request
1437 * to a page size multiple (i.e. size > non_rounded_size)
1439 non_rounded_size
-= io_size
;
1441 if (non_rounded_size
<= 0) {
1443 * we've transferred all of the data in the original
1444 * request, but we were unable to complete the tail
1445 * of the last page because the file didn't have
1446 * an allocation to back that portion... this is ok.
1452 flags
&= ~CL_COMMIT
;
1457 lblkno
= (daddr64_t
)(f_offset
/ 0x1000);
1459 * we have now figured out how much I/O we can do - this is in 'io_size'
1460 * pg_offset is the starting point in the first page for the I/O
1461 * pg_count is the number of full and partial pages that 'io_size' encompasses
1463 pg_offset
= upl_offset
& PAGE_MASK
;
1465 if (flags
& CL_DEV_MEMORY
) {
1467 * treat physical requests as one 'giant' page
1471 pg_count
= (io_size
+ pg_offset
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1473 if ((flags
& CL_READ
) && blkno
== -1) {
1474 vm_offset_t commit_offset
;
1476 int complete_transaction_now
= 0;
1479 * if we're reading and blkno == -1, then we've got a
1480 * 'hole' in the file that we need to deal with by zeroing
1481 * out the affected area in the upl
1483 if (io_size
>= (u_int
)non_rounded_size
) {
1485 * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
1486 * than 'zero_offset' will be non-zero
1487 * if the 'hole' returned by vnop_blockmap extends all the way to the eof
1488 * (indicated by the io_size finishing off the I/O request for this UPL)
1489 * than we're not going to issue an I/O for the
1490 * last page in this upl... we need to zero both the hole and the tail
1491 * of the page beyond the EOF, since the delayed zero-fill won't kick in
1493 bytes_to_zero
= non_rounded_size
;
1494 if (!(flags
& CL_NOZERO
))
1495 bytes_to_zero
= (((upl_offset
+ io_size
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - upl_offset
;
1499 bytes_to_zero
= io_size
;
1503 cluster_zero(upl
, upl_offset
, bytes_to_zero
, real_bp
);
1509 * if there is a current I/O chain pending
1510 * then the first page of the group we just zero'd
1511 * will be handled by the I/O completion if the zero
1512 * fill started in the middle of the page
1514 commit_offset
= (upl_offset
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1516 pg_resid
= commit_offset
- upl_offset
;
1518 if (bytes_to_zero
>= pg_resid
) {
1520 * the last page of the current I/O
1521 * has been completed...
1522 * compute the number of fully zero'd
1523 * pages that are beyond it
1524 * plus the last page if its partial
1525 * and we have no more I/O to issue...
1526 * otherwise a partial page is left
1527 * to begin the next I/O
1529 if ((int)io_size
>= non_rounded_size
)
1530 pg_count
= (bytes_to_zero
- pg_resid
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1532 pg_count
= (bytes_to_zero
- pg_resid
) / PAGE_SIZE
;
1534 complete_transaction_now
= 1;
1538 * no pending I/O to deal with
1539 * so, commit all of the fully zero'd pages
1540 * plus the last page if its partial
1541 * and we have no more I/O to issue...
1542 * otherwise a partial page is left
1543 * to begin the next I/O
1545 if ((int)io_size
>= non_rounded_size
)
1546 pg_count
= (pg_offset
+ bytes_to_zero
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1548 pg_count
= (pg_offset
+ bytes_to_zero
) / PAGE_SIZE
;
1550 commit_offset
= upl_offset
& ~PAGE_MASK
;
1553 // Associated UPL is currently only used in the direct write path
1554 assert(!upl_associated_upl(upl
));
1556 if ( (flags
& CL_COMMIT
) && pg_count
) {
1557 ubc_upl_commit_range(upl
, commit_offset
, pg_count
* PAGE_SIZE
,
1558 UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
);
1560 upl_offset
+= io_size
;
1561 f_offset
+= io_size
;
1565 * keep track of how much of the original request
1566 * that we've actually completed... non_rounded_size
1567 * may go negative due to us rounding the request
1568 * to a page size multiple (i.e. size > non_rounded_size)
1570 non_rounded_size
-= io_size
;
1572 if (non_rounded_size
<= 0) {
1574 * we've transferred all of the data in the original
1575 * request, but we were unable to complete the tail
1576 * of the last page because the file didn't have
1577 * an allocation to back that portion... this is ok.
1581 if (cbp_head
&& (complete_transaction_now
|| size
== 0)) {
1582 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1584 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1586 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1592 if (pg_count
> max_vectors
) {
1593 if (((pg_count
- max_vectors
) * PAGE_SIZE
) > io_size
) {
1594 io_size
= PAGE_SIZE
- pg_offset
;
1597 io_size
-= (pg_count
- max_vectors
) * PAGE_SIZE
;
1598 pg_count
= max_vectors
;
1602 * If the transaction is going to reach the maximum number of
1603 * desired elements, truncate the i/o to the nearest page so
1604 * that the actual i/o is initiated after this buffer is
1605 * created and added to the i/o chain.
1607 * I/O directed to physically contiguous memory
1608 * doesn't have a requirement to make sure we 'fill' a page
1610 if ( !(flags
& CL_DEV_MEMORY
) && trans_count
>= max_trans_count
&&
1611 ((upl_offset
+ io_size
) & PAGE_MASK
)) {
1612 vm_offset_t aligned_ofs
;
1614 aligned_ofs
= (upl_offset
+ io_size
) & ~PAGE_MASK
;
1616 * If the io_size does not actually finish off even a
1617 * single page we have to keep adding buffers to the
1618 * transaction despite having reached the desired limit.
1620 * Eventually we get here with the page being finished
1621 * off (and exceeded) and then we truncate the size of
1622 * this i/o request so that it is page aligned so that
1623 * we can finally issue the i/o on the transaction.
1625 if (aligned_ofs
> upl_offset
) {
1626 io_size
= aligned_ofs
- upl_offset
;
1631 if ( !(mp
->mnt_kern_flag
& MNTK_VIRTUALDEV
))
1633 * if we're not targeting a virtual device i.e. a disk image
1634 * it's safe to dip into the reserve pool since real devices
1635 * can complete this I/O request without requiring additional
1636 * bufs from the alloc_io_buf pool
1639 else if ((flags
& CL_ASYNC
) && !(flags
& CL_PAGEOUT
))
1641 * Throttle the speculative IO
1647 cbp
= alloc_io_buf(vp
, priv
);
1649 if (flags
& CL_PAGEOUT
) {
1653 * since blocks are in offsets of 0x1000, scale
1654 * iteration to (PAGE_SIZE * pg_count) of blks.
1656 for (i
= 0; i
< (PAGE_SIZE
* pg_count
)/0x1000; i
++) {
1657 if (buf_invalblkno(vp
, lblkno
+ i
, 0) == EBUSY
)
1658 panic("BUSY bp found in cluster_io");
1661 if (flags
& CL_ASYNC
) {
1662 if (buf_setcallback(cbp
, (void *)cluster_iodone
, callback_arg
))
1663 panic("buf_setcallback failed\n");
1665 cbp
->b_cliodone
= (void *)callback
;
1666 cbp
->b_flags
|= io_flags
;
1667 if (flags
& CL_NOCACHE
)
1668 cbp
->b_attr
.ba_flags
|= BA_NOCACHE
;
1670 cbp
->b_lblkno
= lblkno
;
1671 cbp
->b_blkno
= blkno
;
1672 cbp
->b_bcount
= io_size
;
1674 if (buf_setupl(cbp
, upl
, upl_offset
))
1675 panic("buf_setupl failed\n");
1677 upl_set_blkno(upl
, upl_offset
, io_size
, blkno
);
1679 cbp
->b_trans_next
= (buf_t
)NULL
;
1681 if ((cbp
->b_iostate
= (void *)iostate
))
1683 * caller wants to track the state of this
1684 * io... bump the amount issued against this stream
1686 iostate
->io_issued
+= io_size
;
1688 if (flags
& CL_READ
) {
1689 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 26)) | DBG_FUNC_NONE
,
1690 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1693 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 27)) | DBG_FUNC_NONE
,
1694 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1698 cbp_tail
->b_trans_next
= cbp
;
1704 if ( (cbp_head
->b_real_bp
= real_bp
) )
1705 real_bp
= (buf_t
)NULL
;
1707 *(buf_t
*)(&cbp
->b_trans_head
) = cbp_head
;
1711 upl_offset
+= io_size
;
1712 f_offset
+= io_size
;
1715 * keep track of how much of the original request
1716 * that we've actually completed... non_rounded_size
1717 * may go negative due to us rounding the request
1718 * to a page size multiple (i.e. size > non_rounded_size)
1720 non_rounded_size
-= io_size
;
1722 if (non_rounded_size
<= 0) {
1724 * we've transferred all of the data in the original
1725 * request, but we were unable to complete the tail
1726 * of the last page because the file didn't have
1727 * an allocation to back that portion... this is ok.
1733 * we have no more I/O to issue, so go
1734 * finish the final transaction
1737 } else if ( ((flags
& CL_DEV_MEMORY
) || (upl_offset
& PAGE_MASK
) == 0) &&
1738 ((flags
& CL_ASYNC
) || trans_count
> max_trans_count
) ) {
1740 * I/O directed to physically contiguous memory...
1741 * which doesn't have a requirement to make sure we 'fill' a page
1743 * the current I/O we've prepared fully
1744 * completes the last page in this request
1746 * it's either an ASYNC request or
1747 * we've already accumulated more than 8 I/O's into
1748 * this transaction so mark it as complete so that
1749 * it can finish asynchronously or via the cluster_complete_transaction
1750 * below if the request is synchronous
1754 if (need_EOT
== TRUE
)
1755 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1757 if (flags
& CL_THROTTLE
)
1758 (void)vnode_waitforwrites(vp
, async_throttle
, 0, 0, "cluster_io");
1760 if ( !(io_flags
& B_READ
))
1761 vnode_startwrite(vp
);
1763 if (flags
& CL_RAW_ENCRYPTED
) {
1765 * User requested raw encrypted bytes.
1766 * Twiddle the bit in the ba_flags for the buffer
1768 cbp
->b_attr
.ba_flags
|= BA_RAW_ENCRYPTED_IO
;
1771 (void) VNOP_STRATEGY(cbp
);
1773 if (need_EOT
== TRUE
) {
1774 if ( !(flags
& CL_ASYNC
))
1775 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 1);
1789 * Wait until all of the outstanding I/O
1790 * for this partial transaction has completed
1792 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1795 * Rewind the upl offset to the beginning of the
1798 upl_offset
= cbp_head
->b_uploffset
;
1801 if (ISSET(flags
, CL_COMMIT
)) {
1802 cluster_handle_associated_upl(iostate
, upl
, upl_offset
,
1803 upl_end_offset
- upl_offset
);
1806 // Free all the IO buffers in this transaction
1807 for (cbp
= cbp_head
; cbp
;) {
1810 size
+= cbp
->b_bcount
;
1811 io_size
+= cbp
->b_bcount
;
1813 cbp_next
= cbp
->b_trans_next
;
1819 int need_wakeup
= 0;
1822 * update the error condition for this stream
1823 * since we never really issued the io
1824 * just go ahead and adjust it back
1826 lck_mtx_lock_spin(&iostate
->io_mtxp
);
1828 if (iostate
->io_error
== 0)
1829 iostate
->io_error
= error
;
1830 iostate
->io_issued
-= io_size
;
1832 if (iostate
->io_wanted
) {
1834 * someone is waiting for the state of
1835 * this io stream to change
1837 iostate
->io_wanted
= 0;
1840 lck_mtx_unlock(&iostate
->io_mtxp
);
1843 wakeup((caddr_t
)&iostate
->io_wanted
);
1846 if (flags
& CL_COMMIT
) {
1849 pg_offset
= upl_offset
& PAGE_MASK
;
1850 abort_size
= (upl_end_offset
- upl_offset
+ PAGE_MASK
) & ~PAGE_MASK
;
1852 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, abort_size
, error
, io_flags
, vp
);
1854 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 28)) | DBG_FUNC_NONE
,
1855 upl
, upl_offset
- pg_offset
, abort_size
, (error
<< 24) | upl_flags
, 0);
1859 } else if (cbp_head
)
1860 panic("%s(): cbp_head is not NULL.\n", __FUNCTION__
);
1864 * can get here if we either encountered an error
1865 * or we completely zero-filled the request and
1869 real_bp
->b_flags
|= B_ERROR
;
1870 real_bp
->b_error
= error
;
1872 buf_biodone(real_bp
);
1874 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_END
, (int)f_offset
, size
, upl_offset
, retval
, 0);
1879 #define reset_vector_run_state() \
1880 issueVectorUPL = vector_upl_offset = vector_upl_index = vector_upl_iosize = vector_upl_size = 0;
1883 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
,
1884 int io_flag
, buf_t real_bp
, struct clios
*iostate
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1886 vector_upl_set_pagelist(vector_upl
);
1888 if(io_flag
& CL_READ
) {
1889 if(vector_upl_offset
== 0 && ((vector_upl_iosize
& PAGE_MASK
)==0))
1890 io_flag
&= ~CL_PRESERVE
; /*don't zero fill*/
1892 io_flag
|= CL_PRESERVE
; /*zero fill*/
1894 return (cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, real_bp
, iostate
, callback
, callback_arg
));
1899 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
)
1901 int pages_in_prefetch
;
1903 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_START
,
1904 (int)f_offset
, size
, (int)filesize
, 0, 0);
1906 if (f_offset
>= filesize
) {
1907 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1908 (int)f_offset
, 0, 0, 0, 0);
1911 if ((off_t
)size
> (filesize
- f_offset
))
1912 size
= filesize
- f_offset
;
1913 pages_in_prefetch
= (size
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1915 advisory_read_ext(vp
, filesize
, f_offset
, size
, callback
, callback_arg
, bflag
);
1917 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1918 (int)f_offset
+ size
, pages_in_prefetch
, 0, 1, 0);
1920 return (pages_in_prefetch
);
1926 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
,
1931 int size_of_prefetch
;
1935 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_START
,
1936 (int)extent
->b_addr
, (int)extent
->e_addr
, (int)rap
->cl_lastr
, 0, 0);
1938 if (extent
->b_addr
== rap
->cl_lastr
&& extent
->b_addr
== extent
->e_addr
) {
1939 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1940 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 0, 0);
1943 if (rap
->cl_lastr
== -1 || (extent
->b_addr
!= rap
->cl_lastr
&& extent
->b_addr
!= (rap
->cl_lastr
+ 1))) {
1947 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1948 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 1, 0);
1952 max_prefetch
= MAX_PREFETCH(vp
, cluster_max_io_size(vp
->v_mount
, CL_READ
), (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
));
1954 if (max_prefetch
> speculative_prefetch_max
)
1955 max_prefetch
= speculative_prefetch_max
;
1957 if (max_prefetch
<= PAGE_SIZE
) {
1958 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1959 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 6, 0);
1962 if (extent
->e_addr
< rap
->cl_maxra
&& rap
->cl_ralen
>= 4) {
1963 if ((rap
->cl_maxra
- extent
->e_addr
) > (rap
->cl_ralen
/ 4)) {
1965 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1966 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 2, 0);
1970 r_addr
= max(extent
->e_addr
, rap
->cl_maxra
) + 1;
1971 f_offset
= (off_t
)(r_addr
* PAGE_SIZE_64
);
1973 size_of_prefetch
= 0;
1975 ubc_range_op(vp
, f_offset
, f_offset
+ PAGE_SIZE_64
, UPL_ROP_PRESENT
, &size_of_prefetch
);
1977 if (size_of_prefetch
) {
1978 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1979 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 3, 0);
1982 if (f_offset
< filesize
) {
1983 daddr64_t read_size
;
1985 rap
->cl_ralen
= rap
->cl_ralen
? min(max_prefetch
/ PAGE_SIZE
, rap
->cl_ralen
<< 1) : 1;
1987 read_size
= (extent
->e_addr
+ 1) - extent
->b_addr
;
1989 if (read_size
> rap
->cl_ralen
) {
1990 if (read_size
> max_prefetch
/ PAGE_SIZE
)
1991 rap
->cl_ralen
= max_prefetch
/ PAGE_SIZE
;
1993 rap
->cl_ralen
= read_size
;
1995 size_of_prefetch
= cluster_read_prefetch(vp
, f_offset
, rap
->cl_ralen
* PAGE_SIZE
, filesize
, callback
, callback_arg
, bflag
);
1997 if (size_of_prefetch
)
1998 rap
->cl_maxra
= (r_addr
+ size_of_prefetch
) - 1;
2000 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
2001 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 4, 0);
2006 cluster_pageout(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
2007 int size
, off_t filesize
, int flags
)
2009 return cluster_pageout_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
2015 cluster_pageout_ext(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
2016 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2023 local_flags
= CL_PAGEOUT
| CL_THROTTLE
;
2025 if ((flags
& UPL_IOSYNC
) == 0)
2026 local_flags
|= CL_ASYNC
;
2027 if ((flags
& UPL_NOCOMMIT
) == 0)
2028 local_flags
|= CL_COMMIT
;
2029 if ((flags
& UPL_KEEPCACHED
))
2030 local_flags
|= CL_KEEPCACHED
;
2031 if (flags
& UPL_PAGING_ENCRYPTED
)
2032 local_flags
|= CL_ENCRYPTED
;
2035 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 52)) | DBG_FUNC_NONE
,
2036 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
2039 * If they didn't specify any I/O, then we are done...
2040 * we can't issue an abort because we don't know how
2041 * big the upl really is
2046 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
2047 if (local_flags
& CL_COMMIT
)
2048 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
2052 * can't page-in from a negative offset
2053 * or if we're starting beyond the EOF
2054 * or if the file offset isn't page aligned
2055 * or the size requested isn't a multiple of PAGE_SIZE
2057 if (f_offset
< 0 || f_offset
>= filesize
||
2058 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
)) {
2059 if (local_flags
& CL_COMMIT
)
2060 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
2063 max_size
= filesize
- f_offset
;
2065 if (size
< max_size
)
2070 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2072 if (size
> rounded_size
) {
2073 if (local_flags
& CL_COMMIT
)
2074 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
, size
- rounded_size
,
2075 UPL_ABORT_FREE_ON_EMPTY
);
2077 return (cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
2078 local_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
));
2083 cluster_pagein(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
2084 int size
, off_t filesize
, int flags
)
2086 return cluster_pagein_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
2091 cluster_pagein_ext(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
2092 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2098 int local_flags
= 0;
2100 if (upl
== NULL
|| size
< 0)
2101 panic("cluster_pagein: NULL upl passed in");
2103 if ((flags
& UPL_IOSYNC
) == 0)
2104 local_flags
|= CL_ASYNC
;
2105 if ((flags
& UPL_NOCOMMIT
) == 0)
2106 local_flags
|= CL_COMMIT
;
2107 if (flags
& UPL_IOSTREAMING
)
2108 local_flags
|= CL_IOSTREAMING
;
2109 if (flags
& UPL_PAGING_ENCRYPTED
)
2110 local_flags
|= CL_ENCRYPTED
;
2113 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 56)) | DBG_FUNC_NONE
,
2114 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
2117 * can't page-in from a negative offset
2118 * or if we're starting beyond the EOF
2119 * or if the file offset isn't page aligned
2120 * or the size requested isn't a multiple of PAGE_SIZE
2122 if (f_offset
< 0 || f_offset
>= filesize
||
2123 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
) || (upl_offset
& PAGE_MASK
)) {
2124 if (local_flags
& CL_COMMIT
)
2125 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
2128 max_size
= filesize
- f_offset
;
2130 if (size
< max_size
)
2135 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2137 if (size
> rounded_size
&& (local_flags
& CL_COMMIT
))
2138 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
,
2139 size
- rounded_size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
2141 retval
= cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
2142 local_flags
| CL_READ
| CL_PAGEIN
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
2149 cluster_bp(buf_t bp
)
2151 return cluster_bp_ext(bp
, NULL
, NULL
);
2156 cluster_bp_ext(buf_t bp
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2161 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 19)) | DBG_FUNC_START
,
2162 bp
, (int)bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
2164 if (bp
->b_flags
& B_READ
)
2165 flags
= CL_ASYNC
| CL_READ
;
2168 if (bp
->b_flags
& B_PASSIVE
)
2169 flags
|= CL_PASSIVE
;
2171 f_offset
= ubc_blktooff(bp
->b_vp
, bp
->b_lblkno
);
2173 return (cluster_io(bp
->b_vp
, bp
->b_upl
, 0, f_offset
, bp
->b_bcount
, flags
, bp
, (struct clios
*)NULL
, callback
, callback_arg
));
2179 cluster_write(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
, int xflags
)
2181 return cluster_write_ext(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, xflags
, NULL
, NULL
);
2186 cluster_write_ext(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
,
2187 int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2189 user_ssize_t cur_resid
;
2194 int write_type
= IO_COPY
;
2195 u_int32_t write_length
;
2199 if (flags
& IO_PASSIVE
)
2204 if (vp
->v_flag
& VNOCACHE_DATA
){
2205 flags
|= IO_NOCACHE
;
2206 bflag
|= CL_NOCACHE
;
2211 * this call is being made to zero-fill some range in the file
2213 retval
= cluster_write_copy(vp
, NULL
, (u_int32_t
)0, oldEOF
, newEOF
, headOff
, tailOff
, flags
, callback
, callback_arg
);
2218 * do a write through the cache if one of the following is true....
2219 * NOCACHE is not true or NODIRECT is true
2220 * the uio request doesn't target USERSPACE
2221 * otherwise, find out if we want the direct or contig variant for
2222 * the first vector in the uio request
2224 if ( ((flags
& (IO_NOCACHE
| IO_NODIRECT
)) == IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) )
2225 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
2227 if ( (flags
& (IO_TAILZEROFILL
| IO_HEADZEROFILL
)) && write_type
== IO_DIRECT
)
2229 * must go through the cached variant in this case
2231 write_type
= IO_COPY
;
2233 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< newEOF
&& retval
== 0) {
2235 switch (write_type
) {
2239 * make sure the uio_resid isn't too big...
2240 * internally, we want to handle all of the I/O in
2241 * chunk sizes that fit in a 32 bit int
2243 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
)) {
2245 * we're going to have to call cluster_write_copy
2248 * only want the last call to cluster_write_copy to
2249 * have the IO_TAILZEROFILL flag set and only the
2250 * first call should have IO_HEADZEROFILL
2252 zflags
= flags
& ~IO_TAILZEROFILL
;
2253 flags
&= ~IO_HEADZEROFILL
;
2255 write_length
= MAX_IO_REQUEST_SIZE
;
2258 * last call to cluster_write_copy
2262 write_length
= (u_int32_t
)cur_resid
;
2264 retval
= cluster_write_copy(vp
, uio
, write_length
, oldEOF
, newEOF
, headOff
, tailOff
, zflags
, callback
, callback_arg
);
2268 zflags
= flags
& ~(IO_TAILZEROFILL
| IO_HEADZEROFILL
);
2270 if (flags
& IO_HEADZEROFILL
) {
2272 * only do this once per request
2274 flags
&= ~IO_HEADZEROFILL
;
2276 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, uio
->uio_offset
,
2277 headOff
, (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
2281 retval
= cluster_write_contig(vp
, uio
, newEOF
, &write_type
, &write_length
, callback
, callback_arg
, bflag
);
2283 if (retval
== 0 && (flags
& IO_TAILZEROFILL
) && uio_resid(uio
) == 0) {
2285 * we're done with the data from the user specified buffer(s)
2286 * and we've been requested to zero fill at the tail
2287 * treat this as an IO_HEADZEROFILL which doesn't require a uio
2288 * by rearranging the args and passing in IO_HEADZEROFILL
2290 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, tailOff
, uio
->uio_offset
,
2291 (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
2297 * cluster_write_direct is never called with IO_TAILZEROFILL || IO_HEADZEROFILL
2299 retval
= cluster_write_direct(vp
, uio
, oldEOF
, newEOF
, &write_type
, &write_length
, flags
, callback
, callback_arg
);
2303 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
2307 * in case we end up calling cluster_write_copy (from cluster_write_direct)
2308 * multiple times to service a multi-vector request that is not aligned properly
2309 * we need to update the oldEOF so that we
2310 * don't zero-fill the head of a page if we've successfully written
2311 * data to that area... 'cluster_write_copy' will zero-fill the head of a
2312 * page that is beyond the oldEOF if the write is unaligned... we only
2313 * want that to happen for the very first page of the cluster_write,
2314 * NOT the first page of each vector making up a multi-vector write.
2316 if (uio
->uio_offset
> oldEOF
)
2317 oldEOF
= uio
->uio_offset
;
2324 cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
2325 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2328 upl_page_info_t
*pl
;
2329 vm_offset_t upl_offset
;
2330 vm_offset_t vector_upl_offset
= 0;
2331 u_int32_t io_req_size
;
2332 u_int32_t offset_in_file
;
2333 u_int32_t offset_in_iovbase
;
2336 upl_size_t upl_size
, vector_upl_size
= 0;
2337 vm_size_t upl_needed_size
;
2338 mach_msg_type_number_t pages_in_pl
;
2339 upl_control_flags_t upl_flags
;
2341 mach_msg_type_number_t i
;
2342 int force_data_sync
;
2345 struct clios iostate
;
2346 user_addr_t iov_base
;
2347 u_int32_t mem_alignment_mask
;
2348 u_int32_t devblocksize
;
2349 u_int32_t max_io_size
;
2350 u_int32_t max_upl_size
;
2351 u_int32_t max_vector_size
;
2352 u_int32_t bytes_outstanding_limit
;
2353 boolean_t io_throttled
= FALSE
;
2355 u_int32_t vector_upl_iosize
= 0;
2356 int issueVectorUPL
= 0,useVectorUPL
= (uio
->uio_iovcnt
> 1);
2357 off_t v_upl_uio_offset
= 0;
2358 int vector_upl_index
=0;
2359 upl_t vector_upl
= NULL
;
2363 * When we enter this routine, we know
2364 * -- the resid will not exceed iov_len
2366 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_START
,
2367 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
2369 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
2371 io_flag
= CL_ASYNC
| CL_PRESERVE
| CL_COMMIT
| CL_THROTTLE
| CL_DIRECT_IO
;
2373 if (flags
& IO_PASSIVE
)
2374 io_flag
|= CL_PASSIVE
;
2376 if (flags
& IO_NOCACHE
)
2377 io_flag
|= CL_NOCACHE
;
2379 if (flags
& IO_SKIP_ENCRYPTION
)
2380 io_flag
|= CL_ENCRYPTED
;
2382 iostate
.io_completed
= 0;
2383 iostate
.io_issued
= 0;
2384 iostate
.io_error
= 0;
2385 iostate
.io_wanted
= 0;
2387 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
2389 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
2390 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
2392 if (devblocksize
== 1) {
2394 * the AFP client advertises a devblocksize of 1
2395 * however, its BLOCKMAP routine maps to physical
2396 * blocks that are PAGE_SIZE in size...
2397 * therefore we can't ask for I/Os that aren't page aligned
2398 * or aren't multiples of PAGE_SIZE in size
2399 * by setting devblocksize to PAGE_SIZE, we re-instate
2400 * the old behavior we had before the mem_alignment_mask
2401 * changes went in...
2403 devblocksize
= PAGE_SIZE
;
2407 io_req_size
= *write_length
;
2408 iov_base
= uio_curriovbase(uio
);
2410 offset_in_file
= (u_int32_t
)uio
->uio_offset
& PAGE_MASK
;
2411 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
2413 if (offset_in_file
|| offset_in_iovbase
) {
2415 * one of the 2 important offsets is misaligned
2416 * so fire an I/O through the cache for this entire vector
2418 goto wait_for_dwrites
;
2420 if (iov_base
& (devblocksize
- 1)) {
2422 * the offset in memory must be on a device block boundary
2423 * so that we can guarantee that we can generate an
2424 * I/O that ends on a page boundary in cluster_io
2426 goto wait_for_dwrites
;
2429 task_update_logical_writes(current_task(), (io_req_size
& ~PAGE_MASK
), TASK_WRITE_IMMEDIATE
, vp
);
2430 while (io_req_size
>= PAGE_SIZE
&& uio
->uio_offset
< newEOF
&& retval
== 0) {
2433 if ( (throttle_type
= cluster_is_throttled(vp
)) ) {
2435 * we're in the throttle window, at the very least
2436 * we want to limit the size of the I/O we're about
2439 if ( (flags
& IO_RETURN_ON_THROTTLE
) && throttle_type
== THROTTLE_NOW
) {
2441 * we're in the throttle window and at least 1 I/O
2442 * has already been issued by a throttleable thread
2443 * in this window, so return with EAGAIN to indicate
2444 * to the FS issuing the cluster_write call that it
2445 * should now throttle after dropping any locks
2447 throttle_info_update_by_mount(vp
->v_mount
);
2449 io_throttled
= TRUE
;
2450 goto wait_for_dwrites
;
2452 max_vector_size
= THROTTLE_MAX_IOSIZE
;
2453 max_io_size
= THROTTLE_MAX_IOSIZE
;
2455 max_vector_size
= MAX_VECTOR_UPL_SIZE
;
2456 max_io_size
= max_upl_size
;
2460 cluster_syncup(vp
, newEOF
, callback
, callback_arg
, callback
? PUSH_SYNC
: 0);
2463 io_size
= io_req_size
& ~PAGE_MASK
;
2464 iov_base
= uio_curriovbase(uio
);
2466 if (io_size
> max_io_size
)
2467 io_size
= max_io_size
;
2469 if(useVectorUPL
&& (iov_base
& PAGE_MASK
)) {
2471 * We have an iov_base that's not page-aligned.
2472 * Issue all I/O's that have been collected within
2473 * this Vectored UPL.
2475 if(vector_upl_index
) {
2476 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
);
2477 reset_vector_run_state();
2481 * After this point, if we are using the Vector UPL path and the base is
2482 * not page-aligned then the UPL with that base will be the first in the vector UPL.
2486 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
2487 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
2489 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_START
,
2490 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
2492 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
2493 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
2495 upl_size
= upl_needed_size
;
2496 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
2497 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
2498 | UPL_MEMORY_TAG_MAKE(VM_KERN_MEMORY_FILE
);
2500 kret
= vm_map_get_upl(map
,
2501 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2509 if (kret
!= KERN_SUCCESS
) {
2510 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2513 * failed to get pagelist
2515 * we may have already spun some portion of this request
2516 * off as async requests... we need to wait for the I/O
2517 * to complete before returning
2519 goto wait_for_dwrites
;
2521 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2522 pages_in_pl
= upl_size
/ PAGE_SIZE
;
2524 for (i
= 0; i
< pages_in_pl
; i
++) {
2525 if (!upl_valid_page(pl
, i
))
2528 if (i
== pages_in_pl
)
2532 * didn't get all the pages back that we
2533 * needed... release this upl and try again
2535 ubc_upl_abort(upl
, 0);
2537 if (force_data_sync
>= 3) {
2538 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2539 i
, pages_in_pl
, upl_size
, kret
, 0);
2541 * for some reason, we couldn't acquire a hold on all
2542 * the pages needed in the user's address space
2544 * we may have already spun some portion of this request
2545 * off as async requests... we need to wait for the I/O
2546 * to complete before returning
2548 goto wait_for_dwrites
;
2552 * Consider the possibility that upl_size wasn't satisfied.
2554 if (upl_size
< upl_needed_size
) {
2555 if (upl_size
&& upl_offset
== 0)
2560 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2561 (int)upl_offset
, upl_size
, (int)iov_base
, io_size
, 0);
2564 ubc_upl_abort(upl
, 0);
2566 * we may have already spun some portion of this request
2567 * off as async requests... we need to wait for the I/O
2568 * to complete before returning
2570 goto wait_for_dwrites
;
2574 vm_offset_t end_off
= ((iov_base
+ io_size
) & PAGE_MASK
);
2578 * After this point, if we are using a vector UPL, then
2579 * either all the UPL elements end on a page boundary OR
2580 * this UPL is the last element because it does not end
2581 * on a page boundary.
2586 * we want push out these writes asynchronously so that we can overlap
2587 * the preparation of the next I/O
2588 * if there are already too many outstanding writes
2589 * wait until some complete before issuing the next
2591 if (vp
->v_mount
->mnt_minsaturationbytecount
)
2592 bytes_outstanding_limit
= vp
->v_mount
->mnt_minsaturationbytecount
;
2594 bytes_outstanding_limit
= max_upl_size
* IO_SCALE(vp
, 2);
2596 cluster_iostate_wait(&iostate
, bytes_outstanding_limit
, "cluster_write_direct");
2598 if (iostate
.io_error
) {
2600 * one of the earlier writes we issued ran into a hard error
2601 * don't issue any more writes, cleanup the UPL
2602 * that was just created but not used, then
2603 * go wait for all writes that are part of this stream
2604 * to complete before returning the error to the caller
2606 ubc_upl_abort(upl
, 0);
2608 goto wait_for_dwrites
;
2611 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_START
,
2612 (int)upl_offset
, (int)uio
->uio_offset
, io_size
, io_flag
, 0);
2615 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
2616 io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2619 if(!vector_upl_index
) {
2620 vector_upl
= vector_upl_create(upl_offset
);
2621 v_upl_uio_offset
= uio
->uio_offset
;
2622 vector_upl_offset
= upl_offset
;
2625 vector_upl_set_subupl(vector_upl
,upl
,upl_size
);
2626 vector_upl_set_iostate(vector_upl
, upl
, vector_upl_size
, upl_size
);
2628 vector_upl_iosize
+= io_size
;
2629 vector_upl_size
+= upl_size
;
2631 if(issueVectorUPL
|| vector_upl_index
== MAX_VECTOR_UPL_ELEMENTS
|| vector_upl_size
>= max_vector_size
) {
2632 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
);
2633 reset_vector_run_state();
2638 * update the uio structure to
2639 * reflect the I/O that we just issued
2641 uio_update(uio
, (user_size_t
)io_size
);
2644 * in case we end up calling through to cluster_write_copy to finish
2645 * the tail of this request, we need to update the oldEOF so that we
2646 * don't zero-fill the head of a page if we've successfully written
2647 * data to that area... 'cluster_write_copy' will zero-fill the head of a
2648 * page that is beyond the oldEOF if the write is unaligned... we only
2649 * want that to happen for the very first page of the cluster_write,
2650 * NOT the first page of each vector making up a multi-vector write.
2652 if (uio
->uio_offset
> oldEOF
)
2653 oldEOF
= uio
->uio_offset
;
2655 io_req_size
-= io_size
;
2657 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_END
,
2658 (int)upl_offset
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
2662 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0) {
2664 retval
= cluster_io_type(uio
, write_type
, write_length
, MIN_DIRECT_WRITE_SIZE
);
2666 if (retval
== 0 && *write_type
== IO_DIRECT
) {
2668 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_NONE
,
2669 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
2677 if (retval
== 0 && iostate
.io_error
== 0 && useVectorUPL
&& vector_upl_index
) {
2678 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
);
2679 reset_vector_run_state();
2682 * make sure all async writes issued as part of this stream
2683 * have completed before we return
2685 cluster_iostate_wait(&iostate
, 0, "cluster_write_direct");
2687 if (iostate
.io_error
)
2688 retval
= iostate
.io_error
;
2690 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
2692 if (io_throttled
== TRUE
&& retval
== 0)
2695 if (io_req_size
&& retval
== 0) {
2697 * we couldn't handle the tail of this request in DIRECT mode
2698 * so fire it through the copy path
2700 * note that flags will never have IO_HEADZEROFILL or IO_TAILZEROFILL set
2701 * so we can just pass 0 in for the headOff and tailOff
2703 if (uio
->uio_offset
> oldEOF
)
2704 oldEOF
= uio
->uio_offset
;
2706 retval
= cluster_write_copy(vp
, uio
, io_req_size
, oldEOF
, newEOF
, (off_t
)0, (off_t
)0, flags
, callback
, callback_arg
);
2708 *write_type
= IO_UNKNOWN
;
2710 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
2711 (int)uio
->uio_offset
, io_req_size
, retval
, 4, 0);
2718 cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
2719 int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
)
2721 upl_page_info_t
*pl
;
2722 addr64_t src_paddr
= 0;
2723 upl_t upl
[MAX_VECTS
];
2724 vm_offset_t upl_offset
;
2725 u_int32_t tail_size
= 0;
2728 upl_size_t upl_size
;
2729 vm_size_t upl_needed_size
;
2730 mach_msg_type_number_t pages_in_pl
;
2731 upl_control_flags_t upl_flags
;
2733 struct clios iostate
;
2738 user_addr_t iov_base
;
2739 u_int32_t devblocksize
;
2740 u_int32_t mem_alignment_mask
;
2743 * When we enter this routine, we know
2744 * -- the io_req_size will not exceed iov_len
2745 * -- the target address is physically contiguous
2747 cluster_syncup(vp
, newEOF
, callback
, callback_arg
, callback
? PUSH_SYNC
: 0);
2749 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
2750 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
2752 iostate
.io_completed
= 0;
2753 iostate
.io_issued
= 0;
2754 iostate
.io_error
= 0;
2755 iostate
.io_wanted
= 0;
2757 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
2760 io_size
= *write_length
;
2762 iov_base
= uio_curriovbase(uio
);
2764 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
2765 upl_needed_size
= upl_offset
+ io_size
;
2768 upl_size
= upl_needed_size
;
2769 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
2770 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
2771 | UPL_MEMORY_TAG_MAKE(VM_KERN_MEMORY_FILE
);
2773 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
2774 kret
= vm_map_get_upl(map
,
2775 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2776 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, 0);
2778 if (kret
!= KERN_SUCCESS
) {
2780 * failed to get pagelist
2783 goto wait_for_cwrites
;
2788 * Consider the possibility that upl_size wasn't satisfied.
2790 if (upl_size
< upl_needed_size
) {
2792 * This is a failure in the physical memory case.
2795 goto wait_for_cwrites
;
2797 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
2799 src_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + (addr64_t
)upl_offset
;
2801 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
2802 u_int32_t head_size
;
2804 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
2806 if (head_size
> io_size
)
2807 head_size
= io_size
;
2809 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, head_size
, 0, callback
, callback_arg
);
2812 goto wait_for_cwrites
;
2814 upl_offset
+= head_size
;
2815 src_paddr
+= head_size
;
2816 io_size
-= head_size
;
2818 iov_base
+= head_size
;
2820 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
2822 * request doesn't set up on a memory boundary
2823 * the underlying DMA engine can handle...
2824 * return an error instead of going through
2825 * the slow copy path since the intent of this
2826 * path is direct I/O from device memory
2829 goto wait_for_cwrites
;
2832 tail_size
= io_size
& (devblocksize
- 1);
2833 io_size
-= tail_size
;
2835 while (io_size
&& error
== 0) {
2837 if (io_size
> MAX_IO_CONTIG_SIZE
)
2838 xsize
= MAX_IO_CONTIG_SIZE
;
2842 * request asynchronously so that we can overlap
2843 * the preparation of the next I/O... we'll do
2844 * the commit after all the I/O has completed
2845 * since its all issued against the same UPL
2846 * if there are already too many outstanding writes
2847 * wait until some have completed before issuing the next
2849 cluster_iostate_wait(&iostate
, MAX_IO_CONTIG_SIZE
* IO_SCALE(vp
, 2), "cluster_write_contig");
2851 if (iostate
.io_error
) {
2853 * one of the earlier writes we issued ran into a hard error
2854 * don't issue any more writes...
2855 * go wait for all writes that are part of this stream
2856 * to complete before returning the error to the caller
2858 goto wait_for_cwrites
;
2861 * issue an asynchronous write to cluster_io
2863 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
,
2864 xsize
, CL_DEV_MEMORY
| CL_ASYNC
| bflag
, (buf_t
)NULL
, (struct clios
*)&iostate
, callback
, callback_arg
);
2868 * The cluster_io write completed successfully,
2869 * update the uio structure
2871 uio_update(uio
, (user_size_t
)xsize
);
2873 upl_offset
+= xsize
;
2878 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
) {
2880 error
= cluster_io_type(uio
, write_type
, write_length
, 0);
2882 if (error
== 0 && *write_type
== IO_CONTIG
) {
2887 *write_type
= IO_UNKNOWN
;
2891 * make sure all async writes that are part of this stream
2892 * have completed before we proceed
2894 cluster_iostate_wait(&iostate
, 0, "cluster_write_contig");
2896 if (iostate
.io_error
)
2897 error
= iostate
.io_error
;
2899 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
2901 if (error
== 0 && tail_size
)
2902 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, tail_size
, 0, callback
, callback_arg
);
2904 for (n
= 0; n
< num_upl
; n
++)
2906 * just release our hold on each physically contiguous
2907 * region without changing any state
2909 ubc_upl_abort(upl
[n
], 0);
2916 * need to avoid a race between an msync of a range of pages dirtied via mmap
2917 * vs a filesystem such as HFS deciding to write a 'hole' to disk via cluster_write's
2918 * zerofill mechanism before it has seen the VNOP_PAGEOUTs for the pages being msync'd
2920 * we should never force-zero-fill pages that are already valid in the cache...
2921 * the entire page contains valid data (either from disk, zero-filled or dirtied
2922 * via an mmap) so we can only do damage by trying to zero-fill
2926 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
)
2929 boolean_t need_cluster_zero
= TRUE
;
2931 if ((flags
& (IO_NOZEROVALID
| IO_NOZERODIRTY
))) {
2933 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off
& PAGE_MASK_64
));
2934 zero_pg_index
= (int)((zero_off
- upl_f_offset
) / PAGE_SIZE_64
);
2936 if (upl_valid_page(pl
, zero_pg_index
)) {
2938 * never force zero valid pages - dirty or clean
2939 * we'll leave these in the UPL for cluster_write_copy to deal with
2941 need_cluster_zero
= FALSE
;
2944 if (need_cluster_zero
== TRUE
)
2945 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2947 return (bytes_to_zero
);
2952 cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
, off_t headOff
,
2953 off_t tailOff
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2955 upl_page_info_t
*pl
;
2957 vm_offset_t upl_offset
= 0;
2970 long long total_size
;
2973 long long zero_cnt1
;
2975 off_t write_off
= 0;
2977 boolean_t first_pass
= FALSE
;
2978 struct cl_extent cl
;
2979 struct cl_writebehind
*wbp
;
2981 u_int max_cluster_pgcount
;
2985 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
2986 (int)uio
->uio_offset
, io_req_size
, (int)oldEOF
, (int)newEOF
, 0);
2988 io_resid
= io_req_size
;
2990 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
2991 0, 0, (int)oldEOF
, (int)newEOF
, 0);
2995 if (flags
& IO_PASSIVE
)
2999 if (flags
& IO_NOCACHE
)
3000 bflag
|= CL_NOCACHE
;
3002 if (flags
& IO_SKIP_ENCRYPTION
)
3003 bflag
|= CL_ENCRYPTED
;
3010 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
3011 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
3013 if (flags
& IO_HEADZEROFILL
) {
3015 * some filesystems (HFS is one) don't support unallocated holes within a file...
3016 * so we zero fill the intervening space between the old EOF and the offset
3017 * where the next chunk of real data begins.... ftruncate will also use this
3018 * routine to zero fill to the new EOF when growing a file... in this case, the
3019 * uio structure will not be provided
3022 if (headOff
< uio
->uio_offset
) {
3023 zero_cnt
= uio
->uio_offset
- headOff
;
3026 } else if (headOff
< newEOF
) {
3027 zero_cnt
= newEOF
- headOff
;
3031 if (uio
&& uio
->uio_offset
> oldEOF
) {
3032 zero_off
= uio
->uio_offset
& ~PAGE_MASK_64
;
3034 if (zero_off
>= oldEOF
) {
3035 zero_cnt
= uio
->uio_offset
- zero_off
;
3037 flags
|= IO_HEADZEROFILL
;
3041 if (flags
& IO_TAILZEROFILL
) {
3043 zero_off1
= uio
->uio_offset
+ io_req_size
;
3045 if (zero_off1
< tailOff
)
3046 zero_cnt1
= tailOff
- zero_off1
;
3049 if (uio
&& newEOF
> oldEOF
) {
3050 zero_off1
= uio
->uio_offset
+ io_req_size
;
3052 if (zero_off1
== newEOF
&& (zero_off1
& PAGE_MASK_64
)) {
3053 zero_cnt1
= PAGE_SIZE_64
- (zero_off1
& PAGE_MASK_64
);
3055 flags
|= IO_TAILZEROFILL
;
3059 if (zero_cnt
== 0 && uio
== (struct uio
*) 0) {
3060 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
3061 retval
, 0, 0, 0, 0);
3065 write_off
= uio
->uio_offset
;
3066 write_cnt
= uio_resid(uio
);
3068 * delay updating the sequential write info
3069 * in the control block until we've obtained
3074 while ((total_size
= (io_resid
+ zero_cnt
+ zero_cnt1
)) && retval
== 0) {
3076 * for this iteration of the loop, figure out where our starting point is
3079 start_offset
= (int)(zero_off
& PAGE_MASK_64
);
3080 upl_f_offset
= zero_off
- start_offset
;
3081 } else if (io_resid
) {
3082 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3083 upl_f_offset
= uio
->uio_offset
- start_offset
;
3085 start_offset
= (int)(zero_off1
& PAGE_MASK_64
);
3086 upl_f_offset
= zero_off1
- start_offset
;
3088 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 46)) | DBG_FUNC_NONE
,
3089 (int)zero_off
, (int)zero_cnt
, (int)zero_off1
, (int)zero_cnt1
, 0);
3091 if (total_size
> max_io_size
)
3092 total_size
= max_io_size
;
3094 cl
.b_addr
= (daddr64_t
)(upl_f_offset
/ PAGE_SIZE_64
);
3096 if (uio
&& ((flags
& (IO_SYNC
| IO_HEADZEROFILL
| IO_TAILZEROFILL
)) == 0)) {
3098 * assumption... total_size <= io_resid
3099 * because IO_HEADZEROFILL and IO_TAILZEROFILL not set
3101 if ((start_offset
+ total_size
) > max_io_size
)
3102 total_size
= max_io_size
- start_offset
;
3103 xfer_resid
= total_size
;
3105 retval
= cluster_copy_ubc_data_internal(vp
, uio
, &xfer_resid
, 1, 1);
3110 io_resid
-= (total_size
- xfer_resid
);
3111 total_size
= xfer_resid
;
3112 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3113 upl_f_offset
= uio
->uio_offset
- start_offset
;
3115 if (total_size
== 0) {
3118 * the write did not finish on a page boundary
3119 * which will leave upl_f_offset pointing to the
3120 * beginning of the last page written instead of
3121 * the page beyond it... bump it in this case
3122 * so that the cluster code records the last page
3125 upl_f_offset
+= PAGE_SIZE_64
;
3133 * compute the size of the upl needed to encompass
3134 * the requested write... limit each call to cluster_io
3135 * to the maximum UPL size... cluster_io will clip if
3136 * this exceeds the maximum io_size for the device,
3137 * make sure to account for
3138 * a starting offset that's not page aligned
3140 upl_size
= (start_offset
+ total_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
3142 if (upl_size
> max_io_size
)
3143 upl_size
= max_io_size
;
3145 pages_in_upl
= upl_size
/ PAGE_SIZE
;
3146 io_size
= upl_size
- start_offset
;
3148 if ((long long)io_size
> total_size
)
3149 io_size
= total_size
;
3151 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, io_size
, total_size
, 0, 0);
3155 * Gather the pages from the buffer cache.
3156 * The UPL_WILL_MODIFY flag lets the UPL subsystem know
3157 * that we intend to modify these pages.
3159 kret
= ubc_create_upl(vp
,
3164 UPL_SET_LITE
| (( uio
!=NULL
&& (uio
->uio_flags
& UIO_FLAGS_IS_COMPRESSED_FILE
)) ? 0 : UPL_WILL_MODIFY
));
3165 if (kret
!= KERN_SUCCESS
)
3166 panic("cluster_write_copy: failed to get pagelist");
3168 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
,
3169 upl
, (int)upl_f_offset
, start_offset
, 0, 0);
3171 if (start_offset
&& upl_f_offset
< oldEOF
&& !upl_valid_page(pl
, 0)) {
3175 * we're starting in the middle of the first page of the upl
3176 * and the page isn't currently valid, so we're going to have
3177 * to read it in first... this is a synchronous operation
3179 read_size
= PAGE_SIZE
;
3181 if ((upl_f_offset
+ read_size
) > oldEOF
)
3182 read_size
= oldEOF
- upl_f_offset
;
3184 retval
= cluster_io(vp
, upl
, 0, upl_f_offset
, read_size
,
3185 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
3188 * we had an error during the read which causes us to abort
3189 * the current cluster_write request... before we do, we need
3190 * to release the rest of the pages in the upl without modifying
3191 * there state and mark the failed page in error
3193 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
3195 if (upl_size
> PAGE_SIZE
)
3196 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3198 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
3199 upl
, 0, 0, retval
, 0);
3203 if ((start_offset
== 0 || upl_size
> PAGE_SIZE
) && ((start_offset
+ io_size
) & PAGE_MASK
)) {
3205 * the last offset we're writing to in this upl does not end on a page
3206 * boundary... if it's not beyond the old EOF, then we'll also need to
3207 * pre-read this page in if it isn't already valid
3209 upl_offset
= upl_size
- PAGE_SIZE
;
3211 if ((upl_f_offset
+ start_offset
+ io_size
) < oldEOF
&&
3212 !upl_valid_page(pl
, upl_offset
/ PAGE_SIZE
)) {
3215 read_size
= PAGE_SIZE
;
3217 if ((off_t
)(upl_f_offset
+ upl_offset
+ read_size
) > oldEOF
)
3218 read_size
= oldEOF
- (upl_f_offset
+ upl_offset
);
3220 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, read_size
,
3221 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
3224 * we had an error during the read which causes us to abort
3225 * the current cluster_write request... before we do, we
3226 * need to release the rest of the pages in the upl without
3227 * modifying there state and mark the failed page in error
3229 ubc_upl_abort_range(upl
, upl_offset
, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
3231 if (upl_size
> PAGE_SIZE
)
3232 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3234 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
3235 upl
, 0, 0, retval
, 0);
3240 xfer_resid
= io_size
;
3241 io_offset
= start_offset
;
3243 while (zero_cnt
&& xfer_resid
) {
3245 if (zero_cnt
< (long long)xfer_resid
)
3246 bytes_to_zero
= zero_cnt
;
3248 bytes_to_zero
= xfer_resid
;
3250 bytes_to_zero
= cluster_zero_range(upl
, pl
, flags
, io_offset
, zero_off
, upl_f_offset
, bytes_to_zero
);
3252 xfer_resid
-= bytes_to_zero
;
3253 zero_cnt
-= bytes_to_zero
;
3254 zero_off
+= bytes_to_zero
;
3255 io_offset
+= bytes_to_zero
;
3257 if (xfer_resid
&& io_resid
) {
3258 u_int32_t io_requested
;
3260 bytes_to_move
= min(io_resid
, xfer_resid
);
3261 io_requested
= bytes_to_move
;
3263 retval
= cluster_copy_upl_data(uio
, upl
, io_offset
, (int *)&io_requested
);
3266 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3268 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
3269 upl
, 0, 0, retval
, 0);
3271 io_resid
-= bytes_to_move
;
3272 xfer_resid
-= bytes_to_move
;
3273 io_offset
+= bytes_to_move
;
3276 while (xfer_resid
&& zero_cnt1
&& retval
== 0) {
3278 if (zero_cnt1
< (long long)xfer_resid
)
3279 bytes_to_zero
= zero_cnt1
;
3281 bytes_to_zero
= xfer_resid
;
3283 bytes_to_zero
= cluster_zero_range(upl
, pl
, flags
, io_offset
, zero_off1
, upl_f_offset
, bytes_to_zero
);
3285 xfer_resid
-= bytes_to_zero
;
3286 zero_cnt1
-= bytes_to_zero
;
3287 zero_off1
+= bytes_to_zero
;
3288 io_offset
+= bytes_to_zero
;
3292 int ret_cluster_try_push
;
3294 io_size
+= start_offset
;
3296 if ((upl_f_offset
+ io_size
) >= newEOF
&& (u_int
)io_size
< upl_size
) {
3298 * if we're extending the file with this write
3299 * we'll zero fill the rest of the page so that
3300 * if the file gets extended again in such a way as to leave a
3301 * hole starting at this EOF, we'll have zero's in the correct spot
3303 cluster_zero(upl
, io_size
, upl_size
- io_size
, NULL
);
3306 * release the upl now if we hold one since...
3307 * 1) pages in it may be present in the sparse cluster map
3308 * and may span 2 separate buckets there... if they do and
3309 * we happen to have to flush a bucket to make room and it intersects
3310 * this upl, a deadlock may result on page BUSY
3311 * 2) we're delaying the I/O... from this point forward we're just updating
3312 * the cluster state... no need to hold the pages, so commit them
3313 * 3) IO_SYNC is set...
3314 * because we had to ask for a UPL that provides currenty non-present pages, the
3315 * UPL has been automatically set to clear the dirty flags (both software and hardware)
3316 * upon committing it... this is not the behavior we want since it's possible for
3317 * pages currently present as part of a mapped file to be dirtied while the I/O is in flight.
3318 * we'll pick these pages back up later with the correct behavior specified.
3319 * 4) we don't want to hold pages busy in a UPL and then block on the cluster lock... if a flush
3320 * of this vnode is in progress, we will deadlock if the pages being flushed intersect the pages
3321 * we hold since the flushing context is holding the cluster lock.
3323 ubc_upl_commit_range(upl
, 0, upl_size
,
3324 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
3327 * calculate the last logical block number
3328 * that this delayed I/O encompassed
3330 cl
.e_addr
= (daddr64_t
)((upl_f_offset
+ (off_t
)upl_size
) / PAGE_SIZE_64
);
3332 if (flags
& IO_SYNC
) {
3334 * if the IO_SYNC flag is set than we need to
3335 * bypass any clusters and immediately issue
3341 * take the lock to protect our accesses
3342 * of the writebehind and sparse cluster state
3344 wbp
= cluster_get_wbp(vp
, CLW_ALLOCATE
| CLW_RETURNLOCKED
);
3346 if (wbp
->cl_scmap
) {
3348 if ( !(flags
& IO_NOCACHE
)) {
3350 * we've fallen into the sparse
3351 * cluster method of delaying dirty pages
3353 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, newEOF
, callback
, callback_arg
);
3355 lck_mtx_unlock(&wbp
->cl_lockw
);
3360 * must have done cached writes that fell into
3361 * the sparse cluster mechanism... we've switched
3362 * to uncached writes on the file, so go ahead
3363 * and push whatever's in the sparse map
3364 * and switch back to normal clustering
3368 sparse_cluster_push(&(wbp
->cl_scmap
), vp
, newEOF
, PUSH_ALL
, 0, callback
, callback_arg
);
3370 * no clusters of either type present at this point
3371 * so just go directly to start_new_cluster since
3372 * we know we need to delay this I/O since we've
3373 * already released the pages back into the cache
3374 * to avoid the deadlock with sparse_cluster_push
3376 goto start_new_cluster
;
3379 if (write_off
== wbp
->cl_last_write
)
3380 wbp
->cl_seq_written
+= write_cnt
;
3382 wbp
->cl_seq_written
= write_cnt
;
3384 wbp
->cl_last_write
= write_off
+ write_cnt
;
3388 if (wbp
->cl_number
== 0)
3390 * no clusters currently present
3392 goto start_new_cluster
;
3394 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
3396 * check each cluster that we currently hold
3397 * try to merge some or all of this write into
3398 * one or more of the existing clusters... if
3399 * any portion of the write remains, start a
3402 if (cl
.b_addr
>= wbp
->cl_clusters
[cl_index
].b_addr
) {
3404 * the current write starts at or after the current cluster
3406 if (cl
.e_addr
<= (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
3408 * we have a write that fits entirely
3409 * within the existing cluster limits
3411 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
)
3413 * update our idea of where the cluster ends
3415 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
3418 if (cl
.b_addr
< (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
3420 * we have a write that starts in the middle of the current cluster
3421 * but extends beyond the cluster's limit... we know this because
3422 * of the previous checks
3423 * we'll extend the current cluster to the max
3424 * and update the b_addr for the current write to reflect that
3425 * the head of it was absorbed into this cluster...
3426 * note that we'll always have a leftover tail in this case since
3427 * full absorbtion would have occurred in the clause above
3429 wbp
->cl_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
;
3431 cl
.b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
;
3434 * we come here for the case where the current write starts
3435 * beyond the limit of the existing cluster or we have a leftover
3436 * tail after a partial absorbtion
3438 * in either case, we'll check the remaining clusters before
3439 * starting a new one
3443 * the current write starts in front of the cluster we're currently considering
3445 if ((wbp
->cl_clusters
[cl_index
].e_addr
- cl
.b_addr
) <= max_cluster_pgcount
) {
3447 * we can just merge the new request into
3448 * this cluster and leave it in the cache
3449 * since the resulting cluster is still
3450 * less than the maximum allowable size
3452 wbp
->cl_clusters
[cl_index
].b_addr
= cl
.b_addr
;
3454 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
) {
3456 * the current write completely
3457 * envelops the existing cluster and since
3458 * each write is limited to at most max_cluster_pgcount pages
3459 * we can just use the start and last blocknos of the write
3460 * to generate the cluster limits
3462 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
3468 * if we were to combine this write with the current cluster
3469 * we would exceed the cluster size limit.... so,
3470 * let's see if there's any overlap of the new I/O with
3471 * the cluster we're currently considering... in fact, we'll
3472 * stretch the cluster out to it's full limit and see if we
3473 * get an intersection with the current write
3476 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
) {
3478 * the current write extends into the proposed cluster
3479 * clip the length of the current write after first combining it's
3480 * tail with the newly shaped cluster
3482 wbp
->cl_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
;
3484 cl
.e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
;
3487 * if we get here, there was no way to merge
3488 * any portion of this write with this cluster
3489 * or we could only merge part of it which
3490 * will leave a tail...
3491 * we'll check the remaining clusters before starting a new one
3495 if (cl_index
< wbp
->cl_number
)
3497 * we found an existing cluster(s) that we
3498 * could entirely merge this I/O into
3502 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) &&
3503 wbp
->cl_number
== MAX_CLUSTERS
&&
3504 wbp
->cl_seq_written
>= (MAX_CLUSTERS
* (max_cluster_pgcount
* PAGE_SIZE
))) {
3507 if (vp
->v_mount
->mnt_minsaturationbytecount
) {
3508 n
= vp
->v_mount
->mnt_minsaturationbytecount
/ MAX_CLUSTER_SIZE(vp
);
3510 if (n
> MAX_CLUSTERS
)
3516 if (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
)
3517 n
= WRITE_BEHIND_SSD
;
3522 cluster_try_push(wbp
, vp
, newEOF
, 0, 0, callback
, callback_arg
);
3524 if (wbp
->cl_number
< MAX_CLUSTERS
) {
3526 * we didn't find an existing cluster to
3527 * merge into, but there's room to start
3530 goto start_new_cluster
;
3533 * no exisitng cluster to merge with and no
3534 * room to start a new one... we'll try
3535 * pushing one of the existing ones... if none of
3536 * them are able to be pushed, we'll switch
3537 * to the sparse cluster mechanism
3538 * cluster_try_push updates cl_number to the
3539 * number of remaining clusters... and
3540 * returns the number of currently unused clusters
3542 ret_cluster_try_push
= 0;
3545 * if writes are not deferred, call cluster push immediately
3547 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
)) {
3549 ret_cluster_try_push
= cluster_try_push(wbp
, vp
, newEOF
, (flags
& IO_NOCACHE
) ? 0 : PUSH_DELAY
, 0, callback
, callback_arg
);
3553 * execute following regardless of writes being deferred or not
3555 if (ret_cluster_try_push
== 0) {
3557 * no more room in the normal cluster mechanism
3558 * so let's switch to the more expansive but expensive
3559 * sparse mechanism....
3561 sparse_cluster_switch(wbp
, vp
, newEOF
, callback
, callback_arg
);
3562 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, newEOF
, callback
, callback_arg
);
3564 lck_mtx_unlock(&wbp
->cl_lockw
);
3569 wbp
->cl_clusters
[wbp
->cl_number
].b_addr
= cl
.b_addr
;
3570 wbp
->cl_clusters
[wbp
->cl_number
].e_addr
= cl
.e_addr
;
3572 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
= 0;
3574 if (flags
& IO_NOCACHE
)
3575 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IONOCACHE
;
3577 if (bflag
& CL_PASSIVE
)
3578 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IOPASSIVE
;
3582 lck_mtx_unlock(&wbp
->cl_lockw
);
3587 * we don't hold the lock at this point
3589 * we've already dropped the current upl, so pick it back up with COPYOUT_FROM set
3590 * so that we correctly deal with a change in state of the hardware modify bit...
3591 * we do this via cluster_push_now... by passing along the IO_SYNC flag, we force
3592 * cluster_push_now to wait until all the I/Os have completed... cluster_push_now is also
3593 * responsible for generating the correct sized I/O(s)
3595 retval
= cluster_push_now(vp
, &cl
, newEOF
, flags
, callback
, callback_arg
);
3598 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
, retval
, 0, io_resid
, 0, 0);
3606 cluster_read(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
)
3608 return cluster_read_ext(vp
, uio
, filesize
, xflags
, NULL
, NULL
);
3613 cluster_read_ext(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
3617 user_ssize_t cur_resid
;
3619 u_int32_t read_length
= 0;
3620 int read_type
= IO_COPY
;
3624 if (vp
->v_flag
& VNOCACHE_DATA
)
3625 flags
|= IO_NOCACHE
;
3626 if ((vp
->v_flag
& VRAOFF
) || speculative_reads_disabled
)
3629 if (flags
& IO_SKIP_ENCRYPTION
)
3630 flags
|= IO_ENCRYPTED
;
3633 * do a read through the cache if one of the following is true....
3634 * NOCACHE is not true
3635 * the uio request doesn't target USERSPACE
3636 * Alternatively, if IO_ENCRYPTED is set, then we want to bypass the cache as well.
3637 * Reading encrypted data from a CP filesystem should never result in the data touching
3640 * otherwise, find out if we want the direct or contig variant for
3641 * the first vector in the uio request
3643 if ( ((flags
& IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
)) || (flags
& IO_ENCRYPTED
) ) {
3645 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3648 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< filesize
&& retval
== 0) {
3650 switch (read_type
) {
3654 * make sure the uio_resid isn't too big...
3655 * internally, we want to handle all of the I/O in
3656 * chunk sizes that fit in a 32 bit int
3658 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
))
3659 io_size
= MAX_IO_REQUEST_SIZE
;
3661 io_size
= (u_int32_t
)cur_resid
;
3663 retval
= cluster_read_copy(vp
, uio
, io_size
, filesize
, flags
, callback
, callback_arg
);
3667 retval
= cluster_read_direct(vp
, uio
, filesize
, &read_type
, &read_length
, flags
, callback
, callback_arg
);
3671 retval
= cluster_read_contig(vp
, uio
, filesize
, &read_type
, &read_length
, callback
, callback_arg
, flags
);
3675 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3685 cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int take_reference
)
3688 int abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
3690 if ((range
= last_pg
- start_pg
)) {
3692 abort_flags
|= UPL_ABORT_REFERENCE
;
3694 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, range
* PAGE_SIZE
, abort_flags
);
3700 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
)
3702 upl_page_info_t
*pl
;
3704 vm_offset_t upl_offset
;
3713 off_t last_ioread_offset
;
3714 off_t last_request_offset
;
3718 u_int32_t size_of_prefetch
;
3721 u_int32_t max_rd_size
;
3722 u_int32_t max_io_size
;
3723 u_int32_t max_prefetch
;
3724 u_int rd_ahead_enabled
= 1;
3725 u_int prefetch_enabled
= 1;
3726 struct cl_readahead
* rap
;
3727 struct clios iostate
;
3728 struct cl_extent extent
;
3730 int take_reference
= 1;
3731 int policy
= IOPOL_DEFAULT
;
3732 boolean_t iolock_inited
= FALSE
;
3734 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_START
,
3735 (int)uio
->uio_offset
, io_req_size
, (int)filesize
, flags
, 0);
3737 if (flags
& IO_ENCRYPTED
) {
3738 panic ("encrypted blocks will hit UBC!");
3741 policy
= throttle_get_io_policy(NULL
);
3743 if (policy
== THROTTLE_LEVEL_TIER3
|| policy
== THROTTLE_LEVEL_TIER2
|| (flags
& IO_NOCACHE
))
3746 if (flags
& IO_PASSIVE
)
3751 if (flags
& IO_NOCACHE
)
3752 bflag
|= CL_NOCACHE
;
3754 if (flags
& IO_SKIP_ENCRYPTION
)
3755 bflag
|= CL_ENCRYPTED
;
3757 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
3758 max_prefetch
= MAX_PREFETCH(vp
, max_io_size
, (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
));
3759 max_rd_size
= max_prefetch
;
3761 last_request_offset
= uio
->uio_offset
+ io_req_size
;
3763 if (last_request_offset
> filesize
)
3764 last_request_offset
= filesize
;
3766 if ((flags
& (IO_RAOFF
|IO_NOCACHE
)) || ((last_request_offset
& ~PAGE_MASK_64
) == (uio
->uio_offset
& ~PAGE_MASK_64
))) {
3767 rd_ahead_enabled
= 0;
3770 if (cluster_is_throttled(vp
)) {
3772 * we're in the throttle window, at the very least
3773 * we want to limit the size of the I/O we're about
3776 rd_ahead_enabled
= 0;
3777 prefetch_enabled
= 0;
3779 max_rd_size
= THROTTLE_MAX_IOSIZE
;
3781 if ((rap
= cluster_get_rap(vp
)) == NULL
)
3782 rd_ahead_enabled
= 0;
3784 extent
.b_addr
= uio
->uio_offset
/ PAGE_SIZE_64
;
3785 extent
.e_addr
= (last_request_offset
- 1) / PAGE_SIZE_64
;
3788 if (rap
!= NULL
&& rap
->cl_ralen
&& (rap
->cl_lastr
== extent
.b_addr
|| (rap
->cl_lastr
+ 1) == extent
.b_addr
)) {
3790 * determine if we already have a read-ahead in the pipe courtesy of the
3791 * last read systemcall that was issued...
3792 * if so, pick up it's extent to determine where we should start
3793 * with respect to any read-ahead that might be necessary to
3794 * garner all the data needed to complete this read systemcall
3796 last_ioread_offset
= (rap
->cl_maxra
* PAGE_SIZE_64
) + PAGE_SIZE_64
;
3798 if (last_ioread_offset
< uio
->uio_offset
)
3799 last_ioread_offset
= (off_t
)0;
3800 else if (last_ioread_offset
> last_request_offset
)
3801 last_ioread_offset
= last_request_offset
;
3803 last_ioread_offset
= (off_t
)0;
3805 while (io_req_size
&& uio
->uio_offset
< filesize
&& retval
== 0) {
3807 max_size
= filesize
- uio
->uio_offset
;
3809 if ((off_t
)(io_req_size
) < max_size
)
3810 io_size
= io_req_size
;
3814 if (!(flags
& IO_NOCACHE
)) {
3818 u_int32_t io_requested
;
3821 * if we keep finding the pages we need already in the cache, then
3822 * don't bother to call cluster_read_prefetch since it costs CPU cycles
3823 * to determine that we have all the pages we need... once we miss in
3824 * the cache and have issued an I/O, than we'll assume that we're likely
3825 * to continue to miss in the cache and it's to our advantage to try and prefetch
3827 if (last_request_offset
&& last_ioread_offset
&& (size_of_prefetch
= (last_request_offset
- last_ioread_offset
))) {
3828 if ((last_ioread_offset
- uio
->uio_offset
) <= max_rd_size
&& prefetch_enabled
) {
3830 * we've already issued I/O for this request and
3831 * there's still work to do and
3832 * our prefetch stream is running dry, so issue a
3833 * pre-fetch I/O... the I/O latency will overlap
3834 * with the copying of the data
3836 if (size_of_prefetch
> max_rd_size
)
3837 size_of_prefetch
= max_rd_size
;
3839 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
3841 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
3843 if (last_ioread_offset
> last_request_offset
)
3844 last_ioread_offset
= last_request_offset
;
3848 * limit the size of the copy we're about to do so that
3849 * we can notice that our I/O pipe is running dry and
3850 * get the next I/O issued before it does go dry
3852 if (last_ioread_offset
&& io_size
> (max_io_size
/ 4))
3853 io_resid
= (max_io_size
/ 4);
3857 io_requested
= io_resid
;
3859 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_resid
, 0, take_reference
);
3861 xsize
= io_requested
- io_resid
;
3864 io_req_size
-= xsize
;
3866 if (retval
|| io_resid
)
3868 * if we run into a real error or
3869 * a page that is not in the cache
3870 * we need to leave streaming mode
3874 if (rd_ahead_enabled
&& (io_size
== 0 || last_ioread_offset
== last_request_offset
)) {
3876 * we're already finished the I/O for this read request
3877 * let's see if we should do a read-ahead
3879 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
3886 if (extent
.e_addr
< rap
->cl_lastr
)
3888 rap
->cl_lastr
= extent
.e_addr
;
3893 * recompute max_size since cluster_copy_ubc_data_internal
3894 * may have advanced uio->uio_offset
3896 max_size
= filesize
- uio
->uio_offset
;
3899 iostate
.io_completed
= 0;
3900 iostate
.io_issued
= 0;
3901 iostate
.io_error
= 0;
3902 iostate
.io_wanted
= 0;
3904 if ( (flags
& IO_RETURN_ON_THROTTLE
) ) {
3905 if (cluster_is_throttled(vp
) == THROTTLE_NOW
) {
3906 if ( !cluster_io_present_in_BC(vp
, uio
->uio_offset
)) {
3908 * we're in the throttle window and at least 1 I/O
3909 * has already been issued by a throttleable thread
3910 * in this window, so return with EAGAIN to indicate
3911 * to the FS issuing the cluster_read call that it
3912 * should now throttle after dropping any locks
3914 throttle_info_update_by_mount(vp
->v_mount
);
3923 * compute the size of the upl needed to encompass
3924 * the requested read... limit each call to cluster_io
3925 * to the maximum UPL size... cluster_io will clip if
3926 * this exceeds the maximum io_size for the device,
3927 * make sure to account for
3928 * a starting offset that's not page aligned
3930 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3931 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
3933 if (io_size
> max_rd_size
)
3934 io_size
= max_rd_size
;
3936 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
3938 if (flags
& IO_NOCACHE
) {
3939 if (upl_size
> max_io_size
)
3940 upl_size
= max_io_size
;
3942 if (upl_size
> max_io_size
/ 4) {
3943 upl_size
= max_io_size
/ 4;
3944 upl_size
&= ~PAGE_MASK
;
3947 upl_size
= PAGE_SIZE
;
3950 pages_in_upl
= upl_size
/ PAGE_SIZE
;
3952 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_START
,
3953 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3955 kret
= ubc_create_upl(vp
,
3960 UPL_FILE_IO
| UPL_SET_LITE
);
3961 if (kret
!= KERN_SUCCESS
)
3962 panic("cluster_read_copy: failed to get pagelist");
3964 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_END
,
3965 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3968 * scan from the beginning of the upl looking for the first
3969 * non-valid page.... this will become the first page in
3970 * the request we're going to make to 'cluster_io'... if all
3971 * of the pages are valid, we won't call through to 'cluster_io'
3973 for (start_pg
= 0; start_pg
< pages_in_upl
; start_pg
++) {
3974 if (!upl_valid_page(pl
, start_pg
))
3979 * scan from the starting invalid page looking for a valid
3980 * page before the end of the upl is reached, if we
3981 * find one, then it will be the last page of the request to
3984 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
3985 if (upl_valid_page(pl
, last_pg
))
3989 if (start_pg
< last_pg
) {
3991 * we found a range of 'invalid' pages that must be filled
3992 * if the last page in this range is the last page of the file
3993 * we may have to clip the size of it to keep from reading past
3994 * the end of the last physical block associated with the file
3996 if (iolock_inited
== FALSE
) {
3997 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
3999 iolock_inited
= TRUE
;
4001 upl_offset
= start_pg
* PAGE_SIZE
;
4002 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
4004 if ((off_t
)(upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
4005 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
4008 * issue an asynchronous read to cluster_io
4011 error
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
,
4012 io_size
, CL_READ
| CL_ASYNC
| bflag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4015 if (extent
.e_addr
< rap
->cl_maxra
) {
4017 * we've just issued a read for a block that should have been
4018 * in the cache courtesy of the read-ahead engine... something
4019 * has gone wrong with the pipeline, so reset the read-ahead
4020 * logic which will cause us to restart from scratch
4028 * if the read completed successfully, or there was no I/O request
4029 * issued, than copy the data into user land via 'cluster_upl_copy_data'
4030 * we'll first add on any 'valid'
4031 * pages that were present in the upl when we acquired it.
4035 for (uio_last
= last_pg
; uio_last
< pages_in_upl
; uio_last
++) {
4036 if (!upl_valid_page(pl
, uio_last
))
4039 if (uio_last
< pages_in_upl
) {
4041 * there were some invalid pages beyond the valid pages
4042 * that we didn't issue an I/O for, just release them
4043 * unchanged now, so that any prefetch/readahed can
4046 ubc_upl_abort_range(upl
, uio_last
* PAGE_SIZE
,
4047 (pages_in_upl
- uio_last
) * PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
4051 * compute size to transfer this round, if io_req_size is
4052 * still non-zero after this attempt, we'll loop around and
4053 * set up for another I/O.
4055 val_size
= (uio_last
* PAGE_SIZE
) - start_offset
;
4057 if (val_size
> max_size
)
4058 val_size
= max_size
;
4060 if (val_size
> io_req_size
)
4061 val_size
= io_req_size
;
4063 if ((uio
->uio_offset
+ val_size
) > last_ioread_offset
)
4064 last_ioread_offset
= uio
->uio_offset
+ val_size
;
4066 if ((size_of_prefetch
= (last_request_offset
- last_ioread_offset
)) && prefetch_enabled
) {
4068 if ((last_ioread_offset
- (uio
->uio_offset
+ val_size
)) <= upl_size
) {
4070 * if there's still I/O left to do for this request, and...
4071 * we're not in hard throttle mode, and...
4072 * we're close to using up the previous prefetch, then issue a
4073 * new pre-fetch I/O... the I/O latency will overlap
4074 * with the copying of the data
4076 if (size_of_prefetch
> max_rd_size
)
4077 size_of_prefetch
= max_rd_size
;
4079 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
4081 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
4083 if (last_ioread_offset
> last_request_offset
)
4084 last_ioread_offset
= last_request_offset
;
4087 } else if ((uio
->uio_offset
+ val_size
) == last_request_offset
) {
4089 * this transfer will finish this request, so...
4090 * let's try to read ahead if we're in
4091 * a sequential access pattern and we haven't
4092 * explicitly disabled it
4094 if (rd_ahead_enabled
)
4095 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
4098 if (extent
.e_addr
< rap
->cl_lastr
)
4100 rap
->cl_lastr
= extent
.e_addr
;
4103 if (iolock_inited
== TRUE
)
4104 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
4106 if (iostate
.io_error
)
4107 error
= iostate
.io_error
;
4109 u_int32_t io_requested
;
4111 io_requested
= val_size
;
4113 retval
= cluster_copy_upl_data(uio
, upl
, start_offset
, (int *)&io_requested
);
4115 io_req_size
-= (val_size
- io_requested
);
4118 if (iolock_inited
== TRUE
)
4119 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
4121 if (start_pg
< last_pg
) {
4123 * compute the range of pages that we actually issued an I/O for
4124 * and either commit them as valid if the I/O succeeded
4125 * or abort them if the I/O failed or we're not supposed to
4126 * keep them in the cache
4128 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
4130 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
, upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
4132 if (error
|| (flags
& IO_NOCACHE
))
4133 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
4134 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
4136 int commit_flags
= UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
;
4139 commit_flags
|= UPL_COMMIT_INACTIVATE
;
4141 commit_flags
|= UPL_COMMIT_SPECULATE
;
4143 ubc_upl_commit_range(upl
, start_pg
* PAGE_SIZE
, io_size
, commit_flags
);
4145 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
4147 if ((last_pg
- start_pg
) < pages_in_upl
) {
4149 * the set of pages that we issued an I/O for did not encompass
4150 * the entire upl... so just release these without modifying
4154 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
4157 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
4158 upl
, -1, pages_in_upl
- (last_pg
- start_pg
), 0, 0);
4161 * handle any valid pages at the beginning of
4162 * the upl... release these appropriately
4164 cluster_read_upl_release(upl
, 0, start_pg
, take_reference
);
4167 * handle any valid pages immediately after the
4168 * pages we issued I/O for... ... release these appropriately
4170 cluster_read_upl_release(upl
, last_pg
, uio_last
, take_reference
);
4172 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, upl
, -1, -1, 0, 0);
4179 if (cluster_is_throttled(vp
)) {
4181 * we're in the throttle window, at the very least
4182 * we want to limit the size of the I/O we're about
4185 rd_ahead_enabled
= 0;
4186 prefetch_enabled
= 0;
4187 max_rd_size
= THROTTLE_MAX_IOSIZE
;
4189 if (max_rd_size
== THROTTLE_MAX_IOSIZE
) {
4191 * coming out of throttled state
4193 if (policy
!= THROTTLE_LEVEL_TIER3
&& policy
!= THROTTLE_LEVEL_TIER2
) {
4195 rd_ahead_enabled
= 1;
4196 prefetch_enabled
= 1;
4198 max_rd_size
= max_prefetch
;
4199 last_ioread_offset
= 0;
4204 if (iolock_inited
== TRUE
) {
4206 * cluster_io returned an error after it
4207 * had already issued some I/O. we need
4208 * to wait for that I/O to complete before
4209 * we can destroy the iostate mutex...
4210 * 'retval' already contains the early error
4211 * so no need to pick it up from iostate.io_error
4213 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
4215 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
4218 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
4219 (int)uio
->uio_offset
, io_req_size
, rap
->cl_lastr
, retval
, 0);
4221 lck_mtx_unlock(&rap
->cl_lockr
);
4223 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
4224 (int)uio
->uio_offset
, io_req_size
, 0, retval
, 0);
4231 * We don't want another read/write lock for every vnode in the system
4232 * so we keep a hash of them here. There should never be very many of
4233 * these around at any point in time.
4235 cl_direct_read_lock_t
*cluster_lock_direct_read(vnode_t vp
, lck_rw_type_t type
)
4237 struct cl_direct_read_locks
*head
4238 = &cl_direct_read_locks
[(uintptr_t)vp
/ sizeof(*vp
)
4239 % CL_DIRECT_READ_LOCK_BUCKETS
];
4241 struct cl_direct_read_lock
*lck
, *new_lck
= NULL
;
4244 lck_spin_lock(&cl_direct_read_spin_lock
);
4246 LIST_FOREACH(lck
, head
, chain
) {
4247 if (lck
->vp
== vp
) {
4249 lck_spin_unlock(&cl_direct_read_spin_lock
);
4251 // Someone beat us to it, ditch the allocation
4252 lck_rw_destroy(&new_lck
->rw_lock
, cl_mtx_grp
);
4253 FREE(new_lck
, M_TEMP
);
4255 lck_rw_lock(&lck
->rw_lock
, type
);
4261 // Use the lock we allocated
4262 LIST_INSERT_HEAD(head
, new_lck
, chain
);
4263 lck_spin_unlock(&cl_direct_read_spin_lock
);
4264 lck_rw_lock(&new_lck
->rw_lock
, type
);
4268 lck_spin_unlock(&cl_direct_read_spin_lock
);
4270 // Allocate a new lock
4271 MALLOC(new_lck
, cl_direct_read_lock_t
*, sizeof(*new_lck
),
4273 lck_rw_init(&new_lck
->rw_lock
, cl_mtx_grp
, cl_mtx_attr
);
4275 new_lck
->ref_count
= 1;
4277 // Got to go round again
4281 void cluster_unlock_direct_read(cl_direct_read_lock_t
*lck
)
4283 lck_rw_done(&lck
->rw_lock
);
4285 lck_spin_lock(&cl_direct_read_spin_lock
);
4286 if (lck
->ref_count
== 1) {
4287 LIST_REMOVE(lck
, chain
);
4288 lck_spin_unlock(&cl_direct_read_spin_lock
);
4289 lck_rw_destroy(&lck
->rw_lock
, cl_mtx_grp
);
4293 lck_spin_unlock(&cl_direct_read_spin_lock
);
4298 cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
4299 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
4302 upl_page_info_t
*pl
;
4304 vm_offset_t upl_offset
, vector_upl_offset
= 0;
4305 upl_size_t upl_size
, vector_upl_size
= 0;
4306 vm_size_t upl_needed_size
;
4307 unsigned int pages_in_pl
;
4308 upl_control_flags_t upl_flags
;
4311 int force_data_sync
;
4313 int no_zero_fill
= 0;
4316 struct clios iostate
;
4317 user_addr_t iov_base
;
4318 u_int32_t io_req_size
;
4319 u_int32_t offset_in_file
;
4320 u_int32_t offset_in_iovbase
;
4324 u_int32_t devblocksize
;
4325 u_int32_t mem_alignment_mask
;
4326 u_int32_t max_upl_size
;
4327 u_int32_t max_rd_size
;
4328 u_int32_t max_rd_ahead
;
4329 u_int32_t max_vector_size
;
4330 boolean_t strict_uncached_IO
= FALSE
;
4331 boolean_t io_throttled
= FALSE
;
4333 u_int32_t vector_upl_iosize
= 0;
4334 int issueVectorUPL
= 0,useVectorUPL
= (uio
->uio_iovcnt
> 1);
4335 off_t v_upl_uio_offset
= 0;
4336 int vector_upl_index
=0;
4337 upl_t vector_upl
= NULL
;
4338 cl_direct_read_lock_t
*lock
= NULL
;
4340 user_addr_t orig_iov_base
= 0;
4341 user_addr_t last_iov_base
= 0;
4342 user_addr_t next_iov_base
= 0;
4344 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_START
,
4345 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
4347 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
4349 max_rd_size
= max_upl_size
;
4350 max_rd_ahead
= max_rd_size
* IO_SCALE(vp
, 2);
4352 io_flag
= CL_COMMIT
| CL_READ
| CL_ASYNC
| CL_NOZERO
| CL_DIRECT_IO
;
4354 if (flags
& IO_PASSIVE
)
4355 io_flag
|= CL_PASSIVE
;
4357 if (flags
& IO_ENCRYPTED
) {
4358 io_flag
|= CL_RAW_ENCRYPTED
;
4361 if (flags
& IO_NOCACHE
) {
4362 io_flag
|= CL_NOCACHE
;
4365 if (flags
& IO_SKIP_ENCRYPTION
)
4366 io_flag
|= CL_ENCRYPTED
;
4368 iostate
.io_completed
= 0;
4369 iostate
.io_issued
= 0;
4370 iostate
.io_error
= 0;
4371 iostate
.io_wanted
= 0;
4373 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
4375 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
4376 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
4378 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
4379 (int)devblocksize
, (int)mem_alignment_mask
, 0, 0, 0);
4381 if (devblocksize
== 1) {
4383 * the AFP client advertises a devblocksize of 1
4384 * however, its BLOCKMAP routine maps to physical
4385 * blocks that are PAGE_SIZE in size...
4386 * therefore we can't ask for I/Os that aren't page aligned
4387 * or aren't multiples of PAGE_SIZE in size
4388 * by setting devblocksize to PAGE_SIZE, we re-instate
4389 * the old behavior we had before the mem_alignment_mask
4390 * changes went in...
4392 devblocksize
= PAGE_SIZE
;
4395 strict_uncached_IO
= ubc_strict_uncached_IO(vp
);
4397 orig_iov_base
= uio_curriovbase(uio
);
4398 last_iov_base
= orig_iov_base
;
4401 io_req_size
= *read_length
;
4402 iov_base
= uio_curriovbase(uio
);
4404 offset_in_file
= (u_int32_t
)uio
->uio_offset
& (devblocksize
- 1);
4405 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
4407 if (offset_in_file
|| offset_in_iovbase
) {
4409 * one of the 2 important offsets is misaligned
4410 * so fire an I/O through the cache for this entire vector
4414 if (iov_base
& (devblocksize
- 1)) {
4416 * the offset in memory must be on a device block boundary
4417 * so that we can guarantee that we can generate an
4418 * I/O that ends on a page boundary in cluster_io
4423 max_io_size
= filesize
- uio
->uio_offset
;
4426 * The user must request IO in aligned chunks. If the
4427 * offset into the file is bad, or the userland pointer
4428 * is non-aligned, then we cannot service the encrypted IO request.
4430 if (flags
& IO_ENCRYPTED
) {
4431 if (misaligned
|| (io_req_size
& (devblocksize
- 1)))
4434 max_io_size
= roundup(max_io_size
, devblocksize
);
4437 if ((off_t
)io_req_size
> max_io_size
)
4438 io_req_size
= max_io_size
;
4441 * When we get to this point, we know...
4442 * -- the offset into the file is on a devblocksize boundary
4445 while (io_req_size
&& retval
== 0) {
4448 if (cluster_is_throttled(vp
)) {
4450 * we're in the throttle window, at the very least
4451 * we want to limit the size of the I/O we're about
4454 max_rd_size
= THROTTLE_MAX_IOSIZE
;
4455 max_rd_ahead
= THROTTLE_MAX_IOSIZE
- 1;
4456 max_vector_size
= THROTTLE_MAX_IOSIZE
;
4458 max_rd_size
= max_upl_size
;
4459 max_rd_ahead
= max_rd_size
* IO_SCALE(vp
, 2);
4460 max_vector_size
= MAX_VECTOR_UPL_SIZE
;
4462 io_start
= io_size
= io_req_size
;
4465 * First look for pages already in the cache
4466 * and move them to user space. But only do this
4467 * check if we are not retrieving encrypted data directly
4468 * from the filesystem; those blocks should never
4471 * cluster_copy_ubc_data returns the resid
4474 if ((strict_uncached_IO
== FALSE
) && ((flags
& IO_ENCRYPTED
) == 0)) {
4475 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_size
, 0, 0);
4478 * calculate the number of bytes actually copied
4479 * starting size - residual
4481 xsize
= io_start
- io_size
;
4483 io_req_size
-= xsize
;
4485 if(useVectorUPL
&& (xsize
|| (iov_base
& PAGE_MASK
))) {
4487 * We found something in the cache or we have an iov_base that's not
4490 * Issue all I/O's that have been collected within this Vectored UPL.
4492 if(vector_upl_index
) {
4493 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
);
4494 reset_vector_run_state();
4501 * After this point, if we are using the Vector UPL path and the base is
4502 * not page-aligned then the UPL with that base will be the first in the vector UPL.
4507 * check to see if we are finished with this request.
4509 * If we satisfied this IO already, then io_req_size will be 0.
4510 * Otherwise, see if the IO was mis-aligned and needs to go through
4511 * the UBC to deal with the 'tail'.
4514 if (io_req_size
== 0 || (misaligned
)) {
4516 * see if there's another uio vector to
4517 * process that's of type IO_DIRECT
4519 * break out of while loop to get there
4524 * assume the request ends on a device block boundary
4526 io_min
= devblocksize
;
4529 * we can handle I/O's in multiples of the device block size
4530 * however, if io_size isn't a multiple of devblocksize we
4531 * want to clip it back to the nearest page boundary since
4532 * we are going to have to go through cluster_read_copy to
4533 * deal with the 'overhang'... by clipping it to a PAGE_SIZE
4534 * multiple, we avoid asking the drive for the same physical
4535 * blocks twice.. once for the partial page at the end of the
4536 * request and a 2nd time for the page we read into the cache
4537 * (which overlaps the end of the direct read) in order to
4538 * get at the overhang bytes
4540 if (io_size
& (devblocksize
- 1)) {
4541 assert(!(flags
& IO_ENCRYPTED
));
4543 * Clip the request to the previous page size boundary
4544 * since request does NOT end on a device block boundary
4546 io_size
&= ~PAGE_MASK
;
4549 if (retval
|| io_size
< io_min
) {
4551 * either an error or we only have the tail left to
4552 * complete via the copy path...
4553 * we may have already spun some portion of this request
4554 * off as async requests... we need to wait for the I/O
4555 * to complete before returning
4557 goto wait_for_dreads
;
4561 * Don't re-check the UBC data if we are looking for uncached IO
4562 * or asking for encrypted blocks.
4564 if ((strict_uncached_IO
== FALSE
) && ((flags
& IO_ENCRYPTED
) == 0)) {
4566 if ((xsize
= io_size
) > max_rd_size
)
4567 xsize
= max_rd_size
;
4573 * We hold a lock here between the time we check the
4574 * cache and the time we issue I/O. This saves us
4575 * from having to lock the pages in the cache. Not
4576 * all clients will care about this lock but some
4577 * clients may want to guarantee stability between
4578 * here and when the I/O is issued in which case they
4579 * will take the lock exclusively.
4581 lock
= cluster_lock_direct_read(vp
, LCK_RW_TYPE_SHARED
);
4584 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ xsize
, UPL_ROP_ABSENT
, (int *)&io_size
);
4588 * a page must have just come into the cache
4589 * since the first page in this range is no
4590 * longer absent, go back and re-evaluate
4595 if ( (flags
& IO_RETURN_ON_THROTTLE
) ) {
4596 if (cluster_is_throttled(vp
) == THROTTLE_NOW
) {
4597 if ( !cluster_io_present_in_BC(vp
, uio
->uio_offset
)) {
4599 * we're in the throttle window and at least 1 I/O
4600 * has already been issued by a throttleable thread
4601 * in this window, so return with EAGAIN to indicate
4602 * to the FS issuing the cluster_read call that it
4603 * should now throttle after dropping any locks
4605 throttle_info_update_by_mount(vp
->v_mount
);
4607 io_throttled
= TRUE
;
4608 goto wait_for_dreads
;
4612 if (io_size
> max_rd_size
)
4613 io_size
= max_rd_size
;
4615 iov_base
= uio_curriovbase(uio
);
4617 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
4618 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
4620 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_START
,
4621 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
4623 if (upl_offset
== 0 && ((io_size
& PAGE_MASK
) == 0))
4628 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
4629 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
4631 upl_size
= upl_needed_size
;
4632 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
4633 | UPL_MEMORY_TAG_MAKE(VM_KERN_MEMORY_FILE
);
4635 upl_flags
|= UPL_NOZEROFILL
;
4636 if (force_data_sync
)
4637 upl_flags
|= UPL_FORCE_DATA_SYNC
;
4639 kret
= vm_map_create_upl(map
,
4640 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4641 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
);
4643 if (kret
!= KERN_SUCCESS
) {
4644 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4645 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4647 * failed to get pagelist
4649 * we may have already spun some portion of this request
4650 * off as async requests... we need to wait for the I/O
4651 * to complete before returning
4653 goto wait_for_dreads
;
4655 pages_in_pl
= upl_size
/ PAGE_SIZE
;
4656 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
4658 for (i
= 0; i
< pages_in_pl
; i
++) {
4659 if (!upl_page_present(pl
, i
))
4662 if (i
== pages_in_pl
)
4665 ubc_upl_abort(upl
, 0);
4667 if (force_data_sync
>= 3) {
4668 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4669 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4671 goto wait_for_dreads
;
4674 * Consider the possibility that upl_size wasn't satisfied.
4676 if (upl_size
< upl_needed_size
) {
4677 if (upl_size
&& upl_offset
== 0)
4683 ubc_upl_abort(upl
, 0);
4684 goto wait_for_dreads
;
4686 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4687 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4690 vm_offset_t end_off
= ((iov_base
+ io_size
) & PAGE_MASK
);
4694 * After this point, if we are using a vector UPL, then
4695 * either all the UPL elements end on a page boundary OR
4696 * this UPL is the last element because it does not end
4697 * on a page boundary.
4702 * request asynchronously so that we can overlap
4703 * the preparation of the next I/O
4704 * if there are already too many outstanding reads
4705 * wait until some have completed before issuing the next read
4707 cluster_iostate_wait(&iostate
, max_rd_ahead
, "cluster_read_direct");
4709 if (iostate
.io_error
) {
4711 * one of the earlier reads we issued ran into a hard error
4712 * don't issue any more reads, cleanup the UPL
4713 * that was just created but not used, then
4714 * go wait for any other reads to complete before
4715 * returning the error to the caller
4717 ubc_upl_abort(upl
, 0);
4719 goto wait_for_dreads
;
4721 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_START
,
4722 upl
, (int)upl_offset
, (int)uio
->uio_offset
, io_size
, 0);
4726 io_flag
&= ~CL_PRESERVE
;
4728 io_flag
|= CL_PRESERVE
;
4730 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
, io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4734 if(!vector_upl_index
) {
4735 vector_upl
= vector_upl_create(upl_offset
);
4736 v_upl_uio_offset
= uio
->uio_offset
;
4737 vector_upl_offset
= upl_offset
;
4740 vector_upl_set_subupl(vector_upl
,upl
, upl_size
);
4741 vector_upl_set_iostate(vector_upl
, upl
, vector_upl_size
, upl_size
);
4743 vector_upl_size
+= upl_size
;
4744 vector_upl_iosize
+= io_size
;
4746 if(issueVectorUPL
|| vector_upl_index
== MAX_VECTOR_UPL_ELEMENTS
|| vector_upl_size
>= max_vector_size
) {
4747 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
);
4748 reset_vector_run_state();
4751 last_iov_base
= iov_base
+ io_size
;
4754 // We don't need to wait for the I/O to complete
4755 cluster_unlock_direct_read(lock
);
4760 * update the uio structure
4762 if ((flags
& IO_ENCRYPTED
) && (max_io_size
< io_size
)) {
4763 uio_update(uio
, (user_size_t
)max_io_size
);
4766 uio_update(uio
, (user_size_t
)io_size
);
4769 io_req_size
-= io_size
;
4771 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_END
,
4772 upl
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
4776 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0 && uio
->uio_offset
< filesize
) {
4778 retval
= cluster_io_type(uio
, read_type
, read_length
, 0);
4780 if (retval
== 0 && *read_type
== IO_DIRECT
) {
4782 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
4783 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
4791 if(retval
== 0 && iostate
.io_error
== 0 && useVectorUPL
&& vector_upl_index
) {
4792 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
);
4793 reset_vector_run_state();
4796 // We don't need to wait for the I/O to complete
4798 cluster_unlock_direct_read(lock
);
4801 * make sure all async reads that are part of this stream
4802 * have completed before we return
4804 cluster_iostate_wait(&iostate
, 0, "cluster_read_direct");
4806 if (iostate
.io_error
)
4807 retval
= iostate
.io_error
;
4809 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
4811 if (io_throttled
== TRUE
&& retval
== 0)
4814 for (next_iov_base
= orig_iov_base
; next_iov_base
< last_iov_base
; next_iov_base
+= PAGE_SIZE
) {
4816 * This is specifically done for pmap accounting purposes.
4817 * vm_pre_fault() will call vm_fault() to enter the page into
4818 * the pmap if there isn't _a_ physical page for that VA already.
4820 vm_pre_fault(vm_map_trunc_page(next_iov_base
, PAGE_MASK
));
4823 if (io_req_size
&& retval
== 0) {
4825 * we couldn't handle the tail of this request in DIRECT mode
4826 * so fire it through the copy path
4828 retval
= cluster_read_copy(vp
, uio
, io_req_size
, filesize
, flags
, callback
, callback_arg
);
4830 *read_type
= IO_UNKNOWN
;
4832 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
4833 (int)uio
->uio_offset
, (int)uio_resid(uio
), io_req_size
, retval
, 0);
4840 cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
4841 int (*callback
)(buf_t
, void *), void *callback_arg
, int flags
)
4843 upl_page_info_t
*pl
;
4844 upl_t upl
[MAX_VECTS
];
4845 vm_offset_t upl_offset
;
4846 addr64_t dst_paddr
= 0;
4847 user_addr_t iov_base
;
4849 upl_size_t upl_size
;
4850 vm_size_t upl_needed_size
;
4851 mach_msg_type_number_t pages_in_pl
;
4852 upl_control_flags_t upl_flags
;
4854 struct clios iostate
;
4861 u_int32_t devblocksize
;
4862 u_int32_t mem_alignment_mask
;
4863 u_int32_t tail_size
= 0;
4866 if (flags
& IO_PASSIVE
)
4871 if (flags
& IO_NOCACHE
)
4872 bflag
|= CL_NOCACHE
;
4875 * When we enter this routine, we know
4876 * -- the read_length will not exceed the current iov_len
4877 * -- the target address is physically contiguous for read_length
4879 cluster_syncup(vp
, filesize
, callback
, callback_arg
, PUSH_SYNC
);
4881 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
4882 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
4884 iostate
.io_completed
= 0;
4885 iostate
.io_issued
= 0;
4886 iostate
.io_error
= 0;
4887 iostate
.io_wanted
= 0;
4889 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
4892 io_size
= *read_length
;
4894 max_size
= filesize
- uio
->uio_offset
;
4896 if (io_size
> max_size
)
4899 iov_base
= uio_curriovbase(uio
);
4901 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
4902 upl_needed_size
= upl_offset
+ io_size
;
4905 upl_size
= upl_needed_size
;
4906 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
4907 | UPL_MEMORY_TAG_MAKE(VM_KERN_MEMORY_FILE
);
4910 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_START
,
4911 (int)upl_offset
, (int)upl_size
, (int)iov_base
, io_size
, 0);
4913 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
4914 kret
= vm_map_get_upl(map
,
4915 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4916 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, 0);
4918 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_END
,
4919 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4921 if (kret
!= KERN_SUCCESS
) {
4923 * failed to get pagelist
4926 goto wait_for_creads
;
4930 if (upl_size
< upl_needed_size
) {
4932 * The upl_size wasn't satisfied.
4935 goto wait_for_creads
;
4937 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
4939 dst_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + (addr64_t
)upl_offset
;
4941 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
4942 u_int32_t head_size
;
4944 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
4946 if (head_size
> io_size
)
4947 head_size
= io_size
;
4949 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, head_size
, CL_READ
, callback
, callback_arg
);
4952 goto wait_for_creads
;
4954 upl_offset
+= head_size
;
4955 dst_paddr
+= head_size
;
4956 io_size
-= head_size
;
4958 iov_base
+= head_size
;
4960 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
4962 * request doesn't set up on a memory boundary
4963 * the underlying DMA engine can handle...
4964 * return an error instead of going through
4965 * the slow copy path since the intent of this
4966 * path is direct I/O to device memory
4969 goto wait_for_creads
;
4972 tail_size
= io_size
& (devblocksize
- 1);
4974 io_size
-= tail_size
;
4976 while (io_size
&& error
== 0) {
4978 if (io_size
> MAX_IO_CONTIG_SIZE
)
4979 xsize
= MAX_IO_CONTIG_SIZE
;
4983 * request asynchronously so that we can overlap
4984 * the preparation of the next I/O... we'll do
4985 * the commit after all the I/O has completed
4986 * since its all issued against the same UPL
4987 * if there are already too many outstanding reads
4988 * wait until some have completed before issuing the next
4990 cluster_iostate_wait(&iostate
, MAX_IO_CONTIG_SIZE
* IO_SCALE(vp
, 2), "cluster_read_contig");
4992 if (iostate
.io_error
) {
4994 * one of the earlier reads we issued ran into a hard error
4995 * don't issue any more reads...
4996 * go wait for any other reads to complete before
4997 * returning the error to the caller
4999 goto wait_for_creads
;
5001 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
, xsize
,
5002 CL_READ
| CL_NOZERO
| CL_DEV_MEMORY
| CL_ASYNC
| bflag
,
5003 (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
5005 * The cluster_io read was issued successfully,
5006 * update the uio structure
5009 uio_update(uio
, (user_size_t
)xsize
);
5012 upl_offset
+= xsize
;
5016 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
&& uio
->uio_offset
< filesize
) {
5018 error
= cluster_io_type(uio
, read_type
, read_length
, 0);
5020 if (error
== 0 && *read_type
== IO_CONTIG
) {
5025 *read_type
= IO_UNKNOWN
;
5029 * make sure all async reads that are part of this stream
5030 * have completed before we proceed
5032 cluster_iostate_wait(&iostate
, 0, "cluster_read_contig");
5034 if (iostate
.io_error
)
5035 error
= iostate
.io_error
;
5037 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
5039 if (error
== 0 && tail_size
)
5040 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, tail_size
, CL_READ
, callback
, callback_arg
);
5042 for (n
= 0; n
< num_upl
; n
++)
5044 * just release our hold on each physically contiguous
5045 * region without changing any state
5047 ubc_upl_abort(upl
[n
], 0);
5054 cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
)
5056 user_size_t iov_len
;
5057 user_addr_t iov_base
= 0;
5059 upl_size_t upl_size
;
5060 upl_control_flags_t upl_flags
;
5064 * skip over any emtpy vectors
5066 uio_update(uio
, (user_size_t
)0);
5068 iov_len
= uio_curriovlen(uio
);
5070 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_START
, uio
, (int)iov_len
, 0, 0, 0);
5073 iov_base
= uio_curriovbase(uio
);
5075 * make sure the size of the vector isn't too big...
5076 * internally, we want to handle all of the I/O in
5077 * chunk sizes that fit in a 32 bit int
5079 if (iov_len
> (user_size_t
)MAX_IO_REQUEST_SIZE
)
5080 upl_size
= MAX_IO_REQUEST_SIZE
;
5082 upl_size
= (u_int32_t
)iov_len
;
5084 upl_flags
= UPL_QUERY_OBJECT_TYPE
| UPL_MEMORY_TAG_MAKE(VM_KERN_MEMORY_FILE
);
5086 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
5087 if ((vm_map_get_upl(map
,
5088 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
5089 &upl_size
, &upl
, NULL
, NULL
, &upl_flags
, 0)) != KERN_SUCCESS
) {
5091 * the user app must have passed in an invalid address
5098 *io_length
= upl_size
;
5100 if (upl_flags
& UPL_PHYS_CONTIG
)
5101 *io_type
= IO_CONTIG
;
5102 else if (iov_len
>= min_length
)
5103 *io_type
= IO_DIRECT
;
5108 * nothing left to do for this uio
5111 *io_type
= IO_UNKNOWN
;
5113 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_END
, iov_base
, *io_type
, *io_length
, retval
, 0);
5120 * generate advisory I/O's in the largest chunks possible
5121 * the completed pages will be released into the VM cache
5124 advisory_read(vnode_t vp
, off_t filesize
, off_t f_offset
, int resid
)
5126 return advisory_read_ext(vp
, filesize
, f_offset
, resid
, NULL
, NULL
, CL_PASSIVE
);
5130 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
)
5132 upl_page_info_t
*pl
;
5134 vm_offset_t upl_offset
;
5147 uint32_t max_io_size
;
5150 if ( !UBCINFOEXISTS(vp
))
5156 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
5158 if ((vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
) && !ignore_is_ssd
) {
5159 if (max_io_size
> speculative_prefetch_max_iosize
)
5160 max_io_size
= speculative_prefetch_max_iosize
;
5163 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_START
,
5164 (int)f_offset
, resid
, (int)filesize
, 0, 0);
5166 while (resid
&& f_offset
< filesize
&& retval
== 0) {
5168 * compute the size of the upl needed to encompass
5169 * the requested read... limit each call to cluster_io
5170 * to the maximum UPL size... cluster_io will clip if
5171 * this exceeds the maximum io_size for the device,
5172 * make sure to account for
5173 * a starting offset that's not page aligned
5175 start_offset
= (int)(f_offset
& PAGE_MASK_64
);
5176 upl_f_offset
= f_offset
- (off_t
)start_offset
;
5177 max_size
= filesize
- f_offset
;
5179 if (resid
< max_size
)
5184 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
5185 if ((uint32_t)upl_size
> max_io_size
)
5186 upl_size
= max_io_size
;
5190 * return the number of contiguously present pages in the cache
5191 * starting at upl_f_offset within the file
5193 ubc_range_op(vp
, upl_f_offset
, upl_f_offset
+ upl_size
, UPL_ROP_PRESENT
, &skip_range
);
5197 * skip over pages already present in the cache
5199 io_size
= skip_range
- start_offset
;
5201 f_offset
+= io_size
;
5204 if (skip_range
== upl_size
)
5207 * have to issue some real I/O
5208 * at this point, we know it's starting on a page boundary
5209 * because we've skipped over at least the first page in the request
5212 upl_f_offset
+= skip_range
;
5213 upl_size
-= skip_range
;
5215 pages_in_upl
= upl_size
/ PAGE_SIZE
;
5217 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_START
,
5218 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
5220 kret
= ubc_create_upl(vp
,
5225 UPL_RET_ONLY_ABSENT
| UPL_SET_LITE
);
5226 if (kret
!= KERN_SUCCESS
)
5231 * before we start marching forward, we must make sure we end on
5232 * a present page, otherwise we will be working with a freed
5235 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
5236 if (upl_page_present(pl
, last_pg
))
5239 pages_in_upl
= last_pg
+ 1;
5242 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_END
,
5243 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
5246 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
5248 * scan from the beginning of the upl looking for the first
5249 * page that is present.... this will become the first page in
5250 * the request we're going to make to 'cluster_io'... if all
5251 * of the pages are absent, we won't call through to 'cluster_io'
5253 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
5254 if (upl_page_present(pl
, start_pg
))
5259 * scan from the starting present page looking for an absent
5260 * page before the end of the upl is reached, if we
5261 * find one, then it will terminate the range of pages being
5262 * presented to 'cluster_io'
5264 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
5265 if (!upl_page_present(pl
, last_pg
))
5269 if (last_pg
> start_pg
) {
5271 * we found a range of pages that must be filled
5272 * if the last page in this range is the last page of the file
5273 * we may have to clip the size of it to keep from reading past
5274 * the end of the last physical block associated with the file
5276 upl_offset
= start_pg
* PAGE_SIZE
;
5277 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
5279 if ((off_t
)(upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
5280 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
5283 * issue an asynchronous read to cluster_io
5285 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
5286 CL_ASYNC
| CL_READ
| CL_COMMIT
| CL_AGE
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5292 ubc_upl_abort(upl
, 0);
5294 io_size
= upl_size
- start_offset
;
5296 if (io_size
> resid
)
5298 f_offset
+= io_size
;
5302 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_END
,
5303 (int)f_offset
, resid
, retval
, 0, 0);
5310 cluster_push(vnode_t vp
, int flags
)
5312 return cluster_push_ext(vp
, flags
, NULL
, NULL
);
5317 cluster_push_ext(vnode_t vp
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5320 int my_sparse_wait
= 0;
5321 struct cl_writebehind
*wbp
;
5323 if ( !UBCINFOEXISTS(vp
)) {
5324 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, kdebug_vnode(vp
), flags
, 0, -1, 0);
5327 /* return if deferred write is set */
5328 if (((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) && (flags
& IO_DEFWRITE
)) {
5331 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) == NULL
) {
5332 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, kdebug_vnode(vp
), flags
, 0, -2, 0);
5335 if (!ISSET(flags
, IO_SYNC
) && wbp
->cl_number
== 0 && wbp
->cl_scmap
== NULL
) {
5336 lck_mtx_unlock(&wbp
->cl_lockw
);
5338 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, kdebug_vnode(vp
), flags
, 0, -3, 0);
5341 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_START
,
5342 wbp
->cl_scmap
, wbp
->cl_number
, flags
, 0, 0);
5345 * if we have an fsync in progress, we don't want to allow any additional
5346 * sync/fsync/close(s) to occur until it finishes.
5347 * note that its possible for writes to continue to occur to this file
5348 * while we're waiting and also once the fsync starts to clean if we're
5349 * in the sparse map case
5351 while (wbp
->cl_sparse_wait
) {
5352 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_START
, kdebug_vnode(vp
), 0, 0, 0, 0);
5354 msleep((caddr_t
)&wbp
->cl_sparse_wait
, &wbp
->cl_lockw
, PRIBIO
+ 1, "cluster_push_ext", NULL
);
5356 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_END
, kdebug_vnode(vp
), 0, 0, 0, 0);
5358 if (flags
& IO_SYNC
) {
5360 wbp
->cl_sparse_wait
= 1;
5363 * this is an fsync (or equivalent)... we must wait for any existing async
5364 * cleaning operations to complete before we evaulate the current state
5365 * and finish cleaning... this insures that all writes issued before this
5366 * fsync actually get cleaned to the disk before this fsync returns
5368 while (wbp
->cl_sparse_pushes
) {
5369 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 98)) | DBG_FUNC_START
, kdebug_vnode(vp
), 0, 0, 0, 0);
5371 msleep((caddr_t
)&wbp
->cl_sparse_pushes
, &wbp
->cl_lockw
, PRIBIO
+ 1, "cluster_push_ext", NULL
);
5373 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 98)) | DBG_FUNC_END
, kdebug_vnode(vp
), 0, 0, 0, 0);
5376 if (wbp
->cl_scmap
) {
5379 if (wbp
->cl_sparse_pushes
< SPARSE_PUSH_LIMIT
) {
5381 scmap
= wbp
->cl_scmap
;
5382 wbp
->cl_scmap
= NULL
;
5384 wbp
->cl_sparse_pushes
++;
5386 lck_mtx_unlock(&wbp
->cl_lockw
);
5388 sparse_cluster_push(&scmap
, vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
);
5390 lck_mtx_lock(&wbp
->cl_lockw
);
5392 wbp
->cl_sparse_pushes
--;
5394 if (wbp
->cl_sparse_wait
&& wbp
->cl_sparse_pushes
== 0)
5395 wakeup((caddr_t
)&wbp
->cl_sparse_pushes
);
5397 sparse_cluster_push(&(wbp
->cl_scmap
), vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
);
5401 retval
= cluster_try_push(wbp
, vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
);
5403 lck_mtx_unlock(&wbp
->cl_lockw
);
5405 if (flags
& IO_SYNC
)
5406 (void)vnode_waitforwrites(vp
, 0, 0, 0, "cluster_push");
5408 if (my_sparse_wait
) {
5410 * I'm the owner of the serialization token
5411 * clear it and wakeup anyone that is waiting
5414 lck_mtx_lock(&wbp
->cl_lockw
);
5416 wbp
->cl_sparse_wait
= 0;
5417 wakeup((caddr_t
)&wbp
->cl_sparse_wait
);
5419 lck_mtx_unlock(&wbp
->cl_lockw
);
5421 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_END
,
5422 wbp
->cl_scmap
, wbp
->cl_number
, retval
, 0, 0);
5428 __private_extern__
void
5429 cluster_release(struct ubc_info
*ubc
)
5431 struct cl_writebehind
*wbp
;
5432 struct cl_readahead
*rap
;
5434 if ((wbp
= ubc
->cl_wbehind
)) {
5436 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, ubc
, wbp
->cl_scmap
, 0, 0, 0);
5439 vfs_drt_control(&(wbp
->cl_scmap
), 0);
5441 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, ubc
, 0, 0, 0, 0);
5444 rap
= ubc
->cl_rahead
;
5447 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
5448 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
5450 if ((rap
= ubc
->cl_rahead
)) {
5451 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
5452 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
5454 ubc
->cl_rahead
= NULL
;
5455 ubc
->cl_wbehind
= NULL
;
5457 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_END
, ubc
, rap
, wbp
, 0, 0);
5462 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
)
5469 struct cl_wextent l_clusters
[MAX_CLUSTERS
];
5470 u_int max_cluster_pgcount
;
5473 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
5475 * the write behind context exists and has
5476 * already been locked...
5478 if (wbp
->cl_number
== 0)
5480 * no clusters to push
5481 * return number of empty slots
5483 return (MAX_CLUSTERS
);
5486 * make a local 'sorted' copy of the clusters
5487 * and clear wbp->cl_number so that new clusters can
5490 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
5491 for (min_index
= -1, cl_index1
= 0; cl_index1
< wbp
->cl_number
; cl_index1
++) {
5492 if (wbp
->cl_clusters
[cl_index1
].b_addr
== wbp
->cl_clusters
[cl_index1
].e_addr
)
5494 if (min_index
== -1)
5495 min_index
= cl_index1
;
5496 else if (wbp
->cl_clusters
[cl_index1
].b_addr
< wbp
->cl_clusters
[min_index
].b_addr
)
5497 min_index
= cl_index1
;
5499 if (min_index
== -1)
5502 l_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[min_index
].b_addr
;
5503 l_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
5504 l_clusters
[cl_index
].io_flags
= wbp
->cl_clusters
[min_index
].io_flags
;
5506 wbp
->cl_clusters
[min_index
].b_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
5512 /* skip switching to the sparse cluster mechanism if on diskimage */
5513 if ( ((push_flag
& PUSH_DELAY
) && cl_len
== MAX_CLUSTERS
) &&
5514 !(vp
->v_mount
->mnt_kern_flag
& MNTK_VIRTUALDEV
) ) {
5518 * determine if we appear to be writing the file sequentially
5519 * if not, by returning without having pushed any clusters
5520 * we will cause this vnode to be pushed into the sparse cluster mechanism
5521 * used for managing more random I/O patterns
5523 * we know that we've got all clusters currently in use and the next write doesn't fit into one of them...
5524 * that's why we're in try_push with PUSH_DELAY...
5526 * check to make sure that all the clusters except the last one are 'full'... and that each cluster
5527 * is adjacent to the next (i.e. we're looking for sequential writes) they were sorted above
5528 * so we can just make a simple pass through, up to, but not including the last one...
5529 * note that e_addr is not inclusive, so it will be equal to the b_addr of the next cluster if they
5532 * we let the last one be partial as long as it was adjacent to the previous one...
5533 * we need to do this to deal with multi-threaded servers that might write an I/O or 2 out
5534 * of order... if this occurs at the tail of the last cluster, we don't want to fall into the sparse cluster world...
5536 for (i
= 0; i
< MAX_CLUSTERS
- 1; i
++) {
5537 if ((l_clusters
[i
].e_addr
- l_clusters
[i
].b_addr
) != max_cluster_pgcount
)
5539 if (l_clusters
[i
].e_addr
!= l_clusters
[i
+1].b_addr
)
5543 for (cl_index
= 0; cl_index
< cl_len
; cl_index
++) {
5545 struct cl_extent cl
;
5547 flags
= io_flags
& (IO_PASSIVE
|IO_CLOSE
);
5550 * try to push each cluster in turn...
5552 if (l_clusters
[cl_index
].io_flags
& CLW_IONOCACHE
)
5553 flags
|= IO_NOCACHE
;
5555 if (l_clusters
[cl_index
].io_flags
& CLW_IOPASSIVE
)
5556 flags
|= IO_PASSIVE
;
5558 if (push_flag
& PUSH_SYNC
)
5561 cl
.b_addr
= l_clusters
[cl_index
].b_addr
;
5562 cl
.e_addr
= l_clusters
[cl_index
].e_addr
;
5564 cluster_push_now(vp
, &cl
, EOF
, flags
, callback
, callback_arg
);
5566 l_clusters
[cl_index
].b_addr
= 0;
5567 l_clusters
[cl_index
].e_addr
= 0;
5571 if ( !(push_flag
& PUSH_ALL
) )
5575 if (cl_len
> cl_pushed
) {
5577 * we didn't push all of the clusters, so
5578 * lets try to merge them back in to the vnode
5580 if ((MAX_CLUSTERS
- wbp
->cl_number
) < (cl_len
- cl_pushed
)) {
5582 * we picked up some new clusters while we were trying to
5583 * push the old ones... this can happen because I've dropped
5584 * the vnode lock... the sum of the
5585 * leftovers plus the new cluster count exceeds our ability
5586 * to represent them, so switch to the sparse cluster mechanism
5588 * collect the active public clusters...
5590 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
);
5592 for (cl_index
= 0, cl_index1
= 0; cl_index
< cl_len
; cl_index
++) {
5593 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
5595 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
5596 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
5597 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
5602 * update the cluster count
5604 wbp
->cl_number
= cl_index1
;
5607 * and collect the original clusters that were moved into the
5608 * local storage for sorting purposes
5610 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
);
5614 * we've got room to merge the leftovers back in
5615 * just append them starting at the next 'hole'
5616 * represented by wbp->cl_number
5618 for (cl_index
= 0, cl_index1
= wbp
->cl_number
; cl_index
< cl_len
; cl_index
++) {
5619 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
5622 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
5623 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
5624 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
5629 * update the cluster count
5631 wbp
->cl_number
= cl_index1
;
5634 return (MAX_CLUSTERS
- wbp
->cl_number
);
5640 cluster_push_now(vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5642 upl_page_info_t
*pl
;
5644 vm_offset_t upl_offset
;
5659 if (flags
& IO_PASSIVE
)
5664 if (flags
& IO_SKIP_ENCRYPTION
)
5665 bflag
|= CL_ENCRYPTED
;
5667 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_START
,
5668 (int)cl
->b_addr
, (int)cl
->e_addr
, (int)EOF
, flags
, 0);
5670 if ((pages_in_upl
= (int)(cl
->e_addr
- cl
->b_addr
)) == 0) {
5671 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 0, 0, 0, 0);
5675 upl_size
= pages_in_upl
* PAGE_SIZE
;
5676 upl_f_offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
5678 if (upl_f_offset
+ upl_size
>= EOF
) {
5680 if (upl_f_offset
>= EOF
) {
5682 * must have truncated the file and missed
5683 * clearing a dangling cluster (i.e. it's completely
5684 * beyond the new EOF
5686 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 1, 0, 0, 0);
5690 size
= EOF
- upl_f_offset
;
5692 upl_size
= (size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
5693 pages_in_upl
= upl_size
/ PAGE_SIZE
;
5697 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, size
, 0, 0, 0);
5700 * by asking for UPL_COPYOUT_FROM and UPL_RET_ONLY_DIRTY, we get the following desirable behavior
5702 * - only pages that are currently dirty are returned... these are the ones we need to clean
5703 * - the hardware dirty bit is cleared when the page is gathered into the UPL... the software dirty bit is set
5704 * - 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
5705 * - when we commit the page, the software dirty bit is cleared... the hardware dirty bit is untouched so that if
5706 * someone dirties this page while the I/O is in progress, we don't lose track of the new state
5708 * when the I/O completes, we no longer ask for an explicit clear of the DIRTY state (either soft or hard)
5711 if ((vp
->v_flag
& VNOCACHE_DATA
) || (flags
& IO_NOCACHE
))
5712 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
| UPL_WILL_BE_DUMPED
;
5714 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
;
5716 kret
= ubc_create_upl(vp
,
5722 if (kret
!= KERN_SUCCESS
)
5723 panic("cluster_push: failed to get pagelist");
5725 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
, upl
, upl_f_offset
, 0, 0, 0);
5728 * since we only asked for the dirty pages back
5729 * it's possible that we may only get a few or even none, so...
5730 * before we start marching forward, we must make sure we know
5731 * where the last present page is in the UPL, otherwise we could
5732 * end up working with a freed upl due to the FREE_ON_EMPTY semantics
5733 * employed by commit_range and abort_range.
5735 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
5736 if (upl_page_present(pl
, last_pg
))
5739 pages_in_upl
= last_pg
+ 1;
5741 if (pages_in_upl
== 0) {
5742 ubc_upl_abort(upl
, 0);
5744 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 2, 0, 0, 0);
5748 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
5750 * find the next dirty page in the UPL
5751 * this will become the first page in the
5752 * next I/O to generate
5754 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
5755 if (upl_dirty_page(pl
, start_pg
))
5757 if (upl_page_present(pl
, start_pg
))
5759 * RET_ONLY_DIRTY will return non-dirty 'precious' pages
5760 * just release these unchanged since we're not going
5761 * to steal them or change their state
5763 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
5765 if (start_pg
>= pages_in_upl
)
5767 * done... no more dirty pages to push
5770 if (start_pg
> last_pg
)
5772 * skipped over some non-dirty pages
5774 size
-= ((start_pg
- last_pg
) * PAGE_SIZE
);
5777 * find a range of dirty pages to write
5779 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
5780 if (!upl_dirty_page(pl
, last_pg
))
5783 upl_offset
= start_pg
* PAGE_SIZE
;
5785 io_size
= min(size
, (last_pg
- start_pg
) * PAGE_SIZE
);
5787 io_flags
= CL_THROTTLE
| CL_COMMIT
| CL_AGE
| bflag
;
5789 if ( !(flags
& IO_SYNC
))
5790 io_flags
|= CL_ASYNC
;
5792 if (flags
& IO_CLOSE
)
5793 io_flags
|= CL_CLOSE
;
5795 if (flags
& IO_NOCACHE
)
5796 io_flags
|= CL_NOCACHE
;
5798 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
5799 io_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5801 if (error
== 0 && retval
)
5806 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 3, 0, 0, 0);
5813 * sparse_cluster_switch is called with the write behind lock held
5816 sparse_cluster_switch(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5820 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_START
, kdebug_vnode(vp
), wbp
->cl_scmap
, 0, 0, 0);
5822 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
5824 struct cl_extent cl
;
5826 for (cl
.b_addr
= wbp
->cl_clusters
[cl_index
].b_addr
; cl
.b_addr
< wbp
->cl_clusters
[cl_index
].e_addr
; cl
.b_addr
++) {
5828 if (ubc_page_op(vp
, (off_t
)(cl
.b_addr
* PAGE_SIZE_64
), 0, NULL
, &flags
) == KERN_SUCCESS
) {
5829 if (flags
& UPL_POP_DIRTY
) {
5830 cl
.e_addr
= cl
.b_addr
+ 1;
5832 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, EOF
, callback
, callback_arg
);
5839 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_END
, kdebug_vnode(vp
), wbp
->cl_scmap
, 0, 0, 0);
5844 * sparse_cluster_push must be called with the write-behind lock held if the scmap is
5845 * still associated with the write-behind context... however, if the scmap has been disassociated
5846 * from the write-behind context (the cluster_push case), the wb lock is not held
5849 sparse_cluster_push(void **scmap
, vnode_t vp
, off_t EOF
, int push_flag
, int io_flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5851 struct cl_extent cl
;
5855 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_START
, kdebug_vnode(vp
), (*scmap
), 0, push_flag
, 0);
5857 if (push_flag
& PUSH_ALL
)
5858 vfs_drt_control(scmap
, 1);
5861 if (vfs_drt_get_cluster(scmap
, &offset
, &length
) != KERN_SUCCESS
)
5864 cl
.b_addr
= (daddr64_t
)(offset
/ PAGE_SIZE_64
);
5865 cl
.e_addr
= (daddr64_t
)((offset
+ length
) / PAGE_SIZE_64
);
5867 cluster_push_now(vp
, &cl
, EOF
, io_flags
& (IO_PASSIVE
|IO_CLOSE
), callback
, callback_arg
);
5869 if ( !(push_flag
& PUSH_ALL
) )
5872 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_END
, kdebug_vnode(vp
), (*scmap
), 0, 0, 0);
5877 * sparse_cluster_add is called with the write behind lock held
5880 sparse_cluster_add(void **scmap
, vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5886 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_START
, (*scmap
), 0, cl
->b_addr
, (int)cl
->e_addr
, 0);
5888 offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
5889 length
= ((u_int
)(cl
->e_addr
- cl
->b_addr
)) * PAGE_SIZE
;
5891 while (vfs_drt_mark_pages(scmap
, offset
, length
, &new_dirty
) != KERN_SUCCESS
) {
5893 * no room left in the map
5894 * only a partial update was done
5895 * push out some pages and try again
5897 sparse_cluster_push(scmap
, vp
, EOF
, 0, 0, callback
, callback_arg
);
5899 offset
+= (new_dirty
* PAGE_SIZE_64
);
5900 length
-= (new_dirty
* PAGE_SIZE
);
5902 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_END
, kdebug_vnode(vp
), (*scmap
), 0, 0, 0);
5907 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
)
5909 upl_page_info_t
*pl
;
5919 if (flags
& IO_PASSIVE
)
5924 if (flags
& IO_NOCACHE
)
5925 bflag
|= CL_NOCACHE
;
5927 upl_flags
= UPL_SET_LITE
;
5929 if ( !(flags
& CL_READ
) ) {
5931 * "write" operation: let the UPL subsystem know
5932 * that we intend to modify the buffer cache pages
5935 upl_flags
|= UPL_WILL_MODIFY
;
5938 * indicate that there is no need to pull the
5939 * mapping for this page... we're only going
5940 * to read from it, not modify it.
5942 upl_flags
|= UPL_FILE_IO
;
5944 kret
= ubc_create_upl(vp
,
5945 uio
->uio_offset
& ~PAGE_MASK_64
,
5951 if (kret
!= KERN_SUCCESS
)
5954 if (!upl_valid_page(pl
, 0)) {
5956 * issue a synchronous read to cluster_io
5958 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
5959 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5961 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
5967 ubc_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + (addr64_t
)(uio
->uio_offset
& PAGE_MASK_64
);
5970 * NOTE: There is no prototype for the following in BSD. It, and the definitions
5971 * of the defines for cppvPsrc, cppvPsnk, cppvFsnk, and cppvFsrc will be found in
5972 * osfmk/ppc/mappings.h. They are not included here because there appears to be no
5973 * way to do so without exporting them to kexts as well.
5975 if (flags
& CL_READ
)
5976 // copypv(ubc_paddr, usr_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsnk); /* Copy physical to physical and flush the destination */
5977 copypv(ubc_paddr
, usr_paddr
, xsize
, 2 | 1 | 4); /* Copy physical to physical and flush the destination */
5979 // copypv(usr_paddr, ubc_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsrc); /* Copy physical to physical and flush the source */
5980 copypv(usr_paddr
, ubc_paddr
, xsize
, 2 | 1 | 8); /* Copy physical to physical and flush the source */
5982 if ( !(flags
& CL_READ
) || (upl_valid_page(pl
, 0) && upl_dirty_page(pl
, 0))) {
5984 * issue a synchronous write to cluster_io
5986 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
5987 bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5990 uio_update(uio
, (user_size_t
)xsize
);
5993 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
5995 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
5997 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, abort_flags
);
6003 cluster_copy_upl_data(struct uio
*uio
, upl_t upl
, int upl_offset
, int *io_resid
)
6011 upl_page_info_t
*pl
;
6016 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
6017 (int)uio
->uio_offset
, upl_offset
, xsize
, 0, 0);
6019 segflg
= uio
->uio_segflg
;
6023 case UIO_USERSPACE32
:
6024 case UIO_USERISPACE32
:
6025 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
6029 case UIO_USERISPACE
:
6030 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
6033 case UIO_USERSPACE64
:
6034 case UIO_USERISPACE64
:
6035 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
6039 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
6043 pl
= ubc_upl_pageinfo(upl
);
6045 pg_index
= upl_offset
/ PAGE_SIZE
;
6046 pg_offset
= upl_offset
& PAGE_MASK
;
6047 csize
= min(PAGE_SIZE
- pg_offset
, xsize
);
6050 while (xsize
&& retval
== 0) {
6053 paddr
= ((addr64_t
)upl_phys_page(pl
, pg_index
) << PAGE_SHIFT
) + pg_offset
;
6054 if ((uio
->uio_rw
== UIO_WRITE
) && (upl_dirty_page(pl
, pg_index
) == FALSE
))
6057 retval
= uiomove64(paddr
, csize
, uio
);
6062 csize
= min(PAGE_SIZE
, xsize
);
6066 uio
->uio_segflg
= segflg
;
6068 task_update_logical_writes(current_task(), (dirty_count
* PAGE_SIZE
), TASK_WRITE_DEFERRED
, upl_lookup_vnode(upl
));
6069 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
6070 (int)uio
->uio_offset
, xsize
, retval
, segflg
, 0);
6077 cluster_copy_ubc_data(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
)
6080 return (cluster_copy_ubc_data_internal(vp
, uio
, io_resid
, mark_dirty
, 1));
6085 cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
)
6092 memory_object_control_t control
;
6094 io_size
= *io_resid
;
6096 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
6097 (int)uio
->uio_offset
, io_size
, mark_dirty
, take_reference
, 0);
6099 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
6101 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
6102 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
6103 (int)uio
->uio_offset
, io_size
, retval
, 3, 0);
6107 segflg
= uio
->uio_segflg
;
6111 case UIO_USERSPACE32
:
6112 case UIO_USERISPACE32
:
6113 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
6116 case UIO_USERSPACE64
:
6117 case UIO_USERISPACE64
:
6118 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
6122 case UIO_USERISPACE
:
6123 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
6127 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
6131 if ( (io_size
= *io_resid
) ) {
6132 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
6133 xsize
= uio_resid(uio
);
6135 retval
= memory_object_control_uiomove(control
, uio
->uio_offset
- start_offset
, uio
,
6136 start_offset
, io_size
, mark_dirty
, take_reference
);
6137 xsize
-= uio_resid(uio
);
6140 uio
->uio_segflg
= segflg
;
6141 *io_resid
= io_size
;
6143 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
6144 (int)uio
->uio_offset
, io_size
, retval
, 0x80000000 | segflg
, 0);
6151 is_file_clean(vnode_t vp
, off_t filesize
)
6155 int total_dirty
= 0;
6157 for (f_offset
= 0; f_offset
< filesize
; f_offset
+= PAGE_SIZE_64
) {
6158 if (ubc_page_op(vp
, f_offset
, 0, NULL
, &flags
) == KERN_SUCCESS
) {
6159 if (flags
& UPL_POP_DIRTY
) {
6173 * Dirty region tracking/clustering mechanism.
6175 * This code (vfs_drt_*) provides a mechanism for tracking and clustering
6176 * dirty regions within a larger space (file). It is primarily intended to
6177 * support clustering in large files with many dirty areas.
6179 * The implementation assumes that the dirty regions are pages.
6181 * To represent dirty pages within the file, we store bit vectors in a
6182 * variable-size circular hash.
6186 * Bitvector size. This determines the number of pages we group in a
6187 * single hashtable entry. Each hashtable entry is aligned to this
6188 * size within the file.
6190 #define DRT_BITVECTOR_PAGES ((1024 * 1024) / PAGE_SIZE)
6193 * File offset handling.
6195 * DRT_ADDRESS_MASK is dependent on DRT_BITVECTOR_PAGES;
6196 * the correct formula is (~((DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1))
6198 #define DRT_ADDRESS_MASK (~((DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1))
6199 #define DRT_ALIGN_ADDRESS(addr) ((addr) & DRT_ADDRESS_MASK)
6202 * Hashtable address field handling.
6204 * The low-order bits of the hashtable address are used to conserve
6207 * DRT_HASH_COUNT_MASK must be large enough to store the range
6208 * 0-DRT_BITVECTOR_PAGES inclusive, as well as have one value
6209 * to indicate that the bucket is actually unoccupied.
6211 #define DRT_HASH_GET_ADDRESS(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_ADDRESS_MASK)
6212 #define DRT_HASH_SET_ADDRESS(scm, i, a) \
6214 (scm)->scm_hashtable[(i)].dhe_control = \
6215 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_ADDRESS_MASK) | DRT_ALIGN_ADDRESS(a); \
6217 #define DRT_HASH_COUNT_MASK 0x1ff
6218 #define DRT_HASH_GET_COUNT(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_HASH_COUNT_MASK)
6219 #define DRT_HASH_SET_COUNT(scm, i, c) \
6221 (scm)->scm_hashtable[(i)].dhe_control = \
6222 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_HASH_COUNT_MASK) | ((c) & DRT_HASH_COUNT_MASK); \
6224 #define DRT_HASH_CLEAR(scm, i) \
6226 (scm)->scm_hashtable[(i)].dhe_control = 0; \
6228 #define DRT_HASH_VACATE(scm, i) DRT_HASH_SET_COUNT((scm), (i), DRT_HASH_COUNT_MASK)
6229 #define DRT_HASH_VACANT(scm, i) (DRT_HASH_GET_COUNT((scm), (i)) == DRT_HASH_COUNT_MASK)
6230 #define DRT_HASH_COPY(oscm, oi, scm, i) \
6232 (scm)->scm_hashtable[(i)].dhe_control = (oscm)->scm_hashtable[(oi)].dhe_control; \
6233 DRT_BITVECTOR_COPY(oscm, oi, scm, i); \
6238 * Hash table moduli.
6240 * Since the hashtable entry's size is dependent on the size of
6241 * the bitvector, and since the hashtable size is constrained to
6242 * both being prime and fitting within the desired allocation
6243 * size, these values need to be manually determined.
6245 * For DRT_BITVECTOR_SIZE = 256, the entry size is 40 bytes.
6247 * The small hashtable allocation is 1024 bytes, so the modulus is 23.
6248 * The large hashtable allocation is 16384 bytes, so the modulus is 401.
6250 #define DRT_HASH_SMALL_MODULUS 23
6251 #define DRT_HASH_LARGE_MODULUS 401
6254 * Physical memory required before the large hash modulus is permitted.
6256 * On small memory systems, the large hash modulus can lead to phsyical
6257 * memory starvation, so we avoid using it there.
6259 #define DRT_HASH_LARGE_MEMORY_REQUIRED (1024LL * 1024LL * 1024LL) /* 1GiB */
6261 #define DRT_SMALL_ALLOCATION 1024 /* 104 bytes spare */
6262 #define DRT_LARGE_ALLOCATION 16384 /* 344 bytes spare */
6264 /* *** nothing below here has secret dependencies on DRT_BITVECTOR_PAGES *** */
6267 * Hashtable bitvector handling.
6269 * Bitvector fields are 32 bits long.
6272 #define DRT_HASH_SET_BIT(scm, i, bit) \
6273 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] |= (1 << ((bit) % 32))
6275 #define DRT_HASH_CLEAR_BIT(scm, i, bit) \
6276 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] &= ~(1 << ((bit) % 32))
6278 #define DRT_HASH_TEST_BIT(scm, i, bit) \
6279 ((scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] & (1 << ((bit) % 32)))
6281 #define DRT_BITVECTOR_CLEAR(scm, i) \
6282 bzero(&(scm)->scm_hashtable[(i)].dhe_bitvector[0], (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
6284 #define DRT_BITVECTOR_COPY(oscm, oi, scm, i) \
6285 bcopy(&(oscm)->scm_hashtable[(oi)].dhe_bitvector[0], \
6286 &(scm)->scm_hashtable[(i)].dhe_bitvector[0], \
6287 (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
6294 struct vfs_drt_hashentry
{
6295 u_int64_t dhe_control
;
6297 * dhe_bitvector was declared as dhe_bitvector[DRT_BITVECTOR_PAGES / 32];
6298 * DRT_BITVECTOR_PAGES is defined as ((1024 * 1024) / PAGE_SIZE)
6299 * Since PAGE_SIZE is only known at boot time,
6300 * -define MAX_DRT_BITVECTOR_PAGES for smallest supported page size (4k)
6301 * -declare dhe_bitvector array for largest possible length
6303 #define MAX_DRT_BITVECTOR_PAGES (1024 * 1024)/( 4 * 1024)
6304 u_int32_t dhe_bitvector
[MAX_DRT_BITVECTOR_PAGES
/32];
6308 * Dirty Region Tracking structure.
6310 * The hashtable is allocated entirely inside the DRT structure.
6312 * The hash is a simple circular prime modulus arrangement, the structure
6313 * is resized from small to large if it overflows.
6316 struct vfs_drt_clustermap
{
6317 u_int32_t scm_magic
; /* sanity/detection */
6318 #define DRT_SCM_MAGIC 0x12020003
6319 u_int32_t scm_modulus
; /* current ring size */
6320 u_int32_t scm_buckets
; /* number of occupied buckets */
6321 u_int32_t scm_lastclean
; /* last entry we cleaned */
6322 u_int32_t scm_iskips
; /* number of slot skips */
6324 struct vfs_drt_hashentry scm_hashtable
[0];
6328 #define DRT_HASH(scm, addr) ((addr) % (scm)->scm_modulus)
6329 #define DRT_HASH_NEXT(scm, addr) (((addr) + 1) % (scm)->scm_modulus)
6332 * Debugging codes and arguments.
6334 #define DRT_DEBUG_EMPTYFREE (FSDBG_CODE(DBG_FSRW, 82)) /* nil */
6335 #define DRT_DEBUG_RETCLUSTER (FSDBG_CODE(DBG_FSRW, 83)) /* offset, length */
6336 #define DRT_DEBUG_ALLOC (FSDBG_CODE(DBG_FSRW, 84)) /* copycount */
6337 #define DRT_DEBUG_INSERT (FSDBG_CODE(DBG_FSRW, 85)) /* offset, iskip */
6338 #define DRT_DEBUG_MARK (FSDBG_CODE(DBG_FSRW, 86)) /* offset, length,
6341 /* 1 (clean, no map) */
6342 /* 2 (map alloc fail) */
6343 /* 3, resid (partial) */
6344 #define DRT_DEBUG_6 (FSDBG_CODE(DBG_FSRW, 87))
6345 #define DRT_DEBUG_SCMDATA (FSDBG_CODE(DBG_FSRW, 88)) /* modulus, buckets,
6346 * lastclean, iskips */
6349 static kern_return_t
vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
);
6350 static kern_return_t
vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
);
6351 static kern_return_t
vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
,
6352 u_int64_t offset
, int *indexp
);
6353 static kern_return_t
vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
,
6357 static kern_return_t
vfs_drt_do_mark_pages(
6363 static void vfs_drt_trace(
6364 struct vfs_drt_clustermap
*cmap
,
6373 * Allocate and initialise a sparse cluster map.
6375 * Will allocate a new map, resize or compact an existing map.
6377 * XXX we should probably have at least one intermediate map size,
6378 * as the 1:16 ratio seems a bit drastic.
6380 static kern_return_t
6381 vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
)
6383 struct vfs_drt_clustermap
*cmap
, *ocmap
;
6387 int nsize
, active_buckets
, index
, copycount
;
6394 * Decide on the size of the new map.
6396 if (ocmap
== NULL
) {
6397 nsize
= DRT_HASH_SMALL_MODULUS
;
6399 /* count the number of active buckets in the old map */
6401 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
6402 if (!DRT_HASH_VACANT(ocmap
, i
) &&
6403 (DRT_HASH_GET_COUNT(ocmap
, i
) != 0))
6407 * If we're currently using the small allocation, check to
6408 * see whether we should grow to the large one.
6410 if (ocmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) {
6412 * If the ring is nearly full and we are allowed to
6413 * use the large modulus, upgrade.
6415 if ((active_buckets
> (DRT_HASH_SMALL_MODULUS
- 5)) &&
6416 (max_mem
>= DRT_HASH_LARGE_MEMORY_REQUIRED
)) {
6417 nsize
= DRT_HASH_LARGE_MODULUS
;
6419 nsize
= DRT_HASH_SMALL_MODULUS
;
6422 /* already using the large modulus */
6423 nsize
= DRT_HASH_LARGE_MODULUS
;
6425 * If the ring is completely full, there's
6426 * nothing useful for us to do. Behave as
6427 * though we had compacted into the new
6430 if (active_buckets
>= DRT_HASH_LARGE_MODULUS
)
6431 return(KERN_SUCCESS
);
6436 * Allocate and initialise the new map.
6439 kret
= kmem_alloc(kernel_map
, (vm_offset_t
*)&cmap
,
6440 (nsize
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
, VM_KERN_MEMORY_FILE
);
6441 if (kret
!= KERN_SUCCESS
)
6443 cmap
->scm_magic
= DRT_SCM_MAGIC
;
6444 cmap
->scm_modulus
= nsize
;
6445 cmap
->scm_buckets
= 0;
6446 cmap
->scm_lastclean
= 0;
6447 cmap
->scm_iskips
= 0;
6448 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
6449 DRT_HASH_CLEAR(cmap
, i
);
6450 DRT_HASH_VACATE(cmap
, i
);
6451 DRT_BITVECTOR_CLEAR(cmap
, i
);
6455 * If there's an old map, re-hash entries from it into the new map.
6458 if (ocmap
!= NULL
) {
6459 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
6460 /* skip empty buckets */
6461 if (DRT_HASH_VACANT(ocmap
, i
) ||
6462 (DRT_HASH_GET_COUNT(ocmap
, i
) == 0))
6465 offset
= DRT_HASH_GET_ADDRESS(ocmap
, i
);
6466 kret
= vfs_drt_get_index(&cmap
, offset
, &index
, 1);
6467 if (kret
!= KERN_SUCCESS
) {
6468 /* XXX need to bail out gracefully here */
6469 panic("vfs_drt: new cluster map mysteriously too small");
6473 DRT_HASH_COPY(ocmap
, i
, cmap
, index
);
6478 /* log what we've done */
6479 vfs_drt_trace(cmap
, DRT_DEBUG_ALLOC
, copycount
, 0, 0, 0);
6482 * It's important to ensure that *cmapp always points to
6483 * a valid map, so we must overwrite it before freeing
6487 if (ocmap
!= NULL
) {
6488 /* emit stats into trace buffer */
6489 vfs_drt_trace(ocmap
, DRT_DEBUG_SCMDATA
,
6492 ocmap
->scm_lastclean
,
6495 vfs_drt_free_map(ocmap
);
6497 return(KERN_SUCCESS
);
6502 * Free a sparse cluster map.
6504 static kern_return_t
6505 vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
)
6507 kmem_free(kernel_map
, (vm_offset_t
)cmap
,
6508 (cmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
6509 return(KERN_SUCCESS
);
6514 * Find the hashtable slot currently occupied by an entry for the supplied offset.
6516 static kern_return_t
6517 vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
, u_int64_t offset
, int *indexp
)
6522 offset
= DRT_ALIGN_ADDRESS(offset
);
6523 index
= DRT_HASH(cmap
, offset
);
6525 /* traverse the hashtable */
6526 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
6529 * If the slot is vacant, we can stop.
6531 if (DRT_HASH_VACANT(cmap
, index
))
6535 * If the address matches our offset, we have success.
6537 if (DRT_HASH_GET_ADDRESS(cmap
, index
) == offset
) {
6539 return(KERN_SUCCESS
);
6543 * Move to the next slot, try again.
6545 index
= DRT_HASH_NEXT(cmap
, index
);
6550 return(KERN_FAILURE
);
6554 * Find the hashtable slot for the supplied offset. If we haven't allocated
6555 * one yet, allocate one and populate the address field. Note that it will
6556 * not have a nonzero page count and thus will still technically be free, so
6557 * in the case where we are called to clean pages, the slot will remain free.
6559 static kern_return_t
6560 vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
, u_int64_t offset
, int *indexp
, int recursed
)
6562 struct vfs_drt_clustermap
*cmap
;
6569 /* look for an existing entry */
6570 kret
= vfs_drt_search_index(cmap
, offset
, indexp
);
6571 if (kret
== KERN_SUCCESS
)
6574 /* need to allocate an entry */
6575 offset
= DRT_ALIGN_ADDRESS(offset
);
6576 index
= DRT_HASH(cmap
, offset
);
6578 /* scan from the index forwards looking for a vacant slot */
6579 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
6581 if (DRT_HASH_VACANT(cmap
, index
) || DRT_HASH_GET_COUNT(cmap
,index
) == 0) {
6582 cmap
->scm_buckets
++;
6583 if (index
< cmap
->scm_lastclean
)
6584 cmap
->scm_lastclean
= index
;
6585 DRT_HASH_SET_ADDRESS(cmap
, index
, offset
);
6586 DRT_HASH_SET_COUNT(cmap
, index
, 0);
6587 DRT_BITVECTOR_CLEAR(cmap
, index
);
6589 vfs_drt_trace(cmap
, DRT_DEBUG_INSERT
, (int)offset
, i
, 0, 0);
6590 return(KERN_SUCCESS
);
6592 cmap
->scm_iskips
+= i
;
6593 index
= DRT_HASH_NEXT(cmap
, index
);
6597 * We haven't found a vacant slot, so the map is full. If we're not
6598 * already recursed, try reallocating/compacting it.
6601 return(KERN_FAILURE
);
6602 kret
= vfs_drt_alloc_map(cmapp
);
6603 if (kret
== KERN_SUCCESS
) {
6604 /* now try to insert again */
6605 kret
= vfs_drt_get_index(cmapp
, offset
, indexp
, 1);
6611 * Implementation of set dirty/clean.
6613 * In the 'clean' case, not finding a map is OK.
6615 static kern_return_t
6616 vfs_drt_do_mark_pages(
6623 struct vfs_drt_clustermap
*cmap
, **cmapp
;
6625 int i
, index
, pgoff
, pgcount
, setcount
, ecount
;
6627 cmapp
= (struct vfs_drt_clustermap
**)private;
6630 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_START
, (int)offset
, (int)length
, dirty
, 0);
6632 if (setcountp
!= NULL
)
6635 /* allocate a cluster map if we don't already have one */
6637 /* no cluster map, nothing to clean */
6639 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 1, 0, 0, 0);
6640 return(KERN_SUCCESS
);
6642 kret
= vfs_drt_alloc_map(cmapp
);
6643 if (kret
!= KERN_SUCCESS
) {
6644 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 2, 0, 0, 0);
6651 * Iterate over the length of the region.
6653 while (length
> 0) {
6655 * Get the hashtable index for this offset.
6657 * XXX this will add blank entries if we are clearing a range
6658 * that hasn't been dirtied.
6660 kret
= vfs_drt_get_index(cmapp
, offset
, &index
, 0);
6661 cmap
= *cmapp
; /* may have changed! */
6662 /* this may be a partial-success return */
6663 if (kret
!= KERN_SUCCESS
) {
6664 if (setcountp
!= NULL
)
6665 *setcountp
= setcount
;
6666 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 3, (int)length
, 0, 0);
6672 * Work out how many pages we're modifying in this
6675 pgoff
= (offset
- DRT_ALIGN_ADDRESS(offset
)) / PAGE_SIZE
;
6676 pgcount
= min((length
/ PAGE_SIZE
), (DRT_BITVECTOR_PAGES
- pgoff
));
6679 * Iterate over pages, dirty/clearing as we go.
6681 ecount
= DRT_HASH_GET_COUNT(cmap
, index
);
6682 for (i
= 0; i
< pgcount
; i
++) {
6684 if (!DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
6685 DRT_HASH_SET_BIT(cmap
, index
, pgoff
+ i
);
6690 if (DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
6691 DRT_HASH_CLEAR_BIT(cmap
, index
, pgoff
+ i
);
6697 DRT_HASH_SET_COUNT(cmap
, index
, ecount
);
6699 offset
+= pgcount
* PAGE_SIZE
;
6700 length
-= pgcount
* PAGE_SIZE
;
6702 if (setcountp
!= NULL
)
6703 *setcountp
= setcount
;
6705 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 0, setcount
, 0, 0);
6707 return(KERN_SUCCESS
);
6711 * Mark a set of pages as dirty/clean.
6713 * This is a public interface.
6716 * Pointer to storage suitable for holding a pointer. Note that
6717 * this must either be NULL or a value set by this function.
6720 * Current file size in bytes.
6723 * Offset of the first page to be marked as dirty, in bytes. Must be
6727 * Length of dirty region, in bytes. Must be a multiple of PAGE_SIZE.
6730 * Number of pages newly marked dirty by this call (optional).
6732 * Returns KERN_SUCCESS if all the pages were successfully marked.
6734 static kern_return_t
6735 vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
)
6737 /* XXX size unused, drop from interface */
6738 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, setcountp
, 1));
6742 static kern_return_t
6743 vfs_drt_unmark_pages(void **cmapp
, off_t offset
, u_int length
)
6745 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0));
6750 * Get a cluster of dirty pages.
6752 * This is a public interface.
6755 * Pointer to storage managed by drt_mark_pages. Note that this must
6756 * be NULL or a value set by drt_mark_pages.
6759 * Returns the byte offset into the file of the first page in the cluster.
6762 * Returns the length in bytes of the cluster of dirty pages.
6764 * Returns success if a cluster was found. If KERN_FAILURE is returned, there
6765 * are no dirty pages meeting the minmum size criteria. Private storage will
6766 * be released if there are no more dirty pages left in the map
6769 static kern_return_t
6770 vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
)
6772 struct vfs_drt_clustermap
*cmap
;
6776 int index
, i
, fs
, ls
;
6779 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
6780 return(KERN_FAILURE
);
6783 /* walk the hashtable */
6784 for (offset
= 0, j
= 0; j
< cmap
->scm_modulus
; offset
+= (DRT_BITVECTOR_PAGES
* PAGE_SIZE
), j
++) {
6785 index
= DRT_HASH(cmap
, offset
);
6787 if (DRT_HASH_VACANT(cmap
, index
) || (DRT_HASH_GET_COUNT(cmap
, index
) == 0))
6790 /* scan the bitfield for a string of bits */
6793 for (i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
6794 if (DRT_HASH_TEST_BIT(cmap
, index
, i
)) {
6800 /* didn't find any bits set */
6801 panic("vfs_drt: entry summary count > 0 but no bits set in map");
6803 for (ls
= 0; i
< DRT_BITVECTOR_PAGES
; i
++, ls
++) {
6804 if (!DRT_HASH_TEST_BIT(cmap
, index
, i
))
6808 /* compute offset and length, mark pages clean */
6809 offset
= DRT_HASH_GET_ADDRESS(cmap
, index
) + (PAGE_SIZE
* fs
);
6810 length
= ls
* PAGE_SIZE
;
6811 vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0);
6812 cmap
->scm_lastclean
= index
;
6814 /* return successful */
6815 *offsetp
= (off_t
)offset
;
6818 vfs_drt_trace(cmap
, DRT_DEBUG_RETCLUSTER
, (int)offset
, (int)length
, 0, 0);
6819 return(KERN_SUCCESS
);
6822 * We didn't find anything... hashtable is empty
6823 * emit stats into trace buffer and
6826 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
6829 cmap
->scm_lastclean
,
6832 vfs_drt_free_map(cmap
);
6835 return(KERN_FAILURE
);
6839 static kern_return_t
6840 vfs_drt_control(void **cmapp
, int op_type
)
6842 struct vfs_drt_clustermap
*cmap
;
6845 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
6846 return(KERN_FAILURE
);
6851 /* emit stats into trace buffer */
6852 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
6855 cmap
->scm_lastclean
,
6858 vfs_drt_free_map(cmap
);
6863 cmap
->scm_lastclean
= 0;
6866 return(KERN_SUCCESS
);
6872 * Emit a summary of the state of the clustermap into the trace buffer
6873 * along with some caller-provided data.
6877 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, int code
, int arg1
, int arg2
, int arg3
, int arg4
)
6879 KERNEL_DEBUG(code
, arg1
, arg2
, arg3
, arg4
, 0);
6883 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, __unused
int code
,
6884 __unused
int arg1
, __unused
int arg2
, __unused
int arg3
,
6892 * Perform basic sanity check on the hash entry summary count
6893 * vs. the actual bits set in the entry.
6896 vfs_drt_sanity(struct vfs_drt_clustermap
*cmap
)
6901 for (index
= 0; index
< cmap
->scm_modulus
; index
++) {
6902 if (DRT_HASH_VACANT(cmap
, index
))
6905 for (bits_on
= 0, i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
6906 if (DRT_HASH_TEST_BIT(cmap
, index
, i
))
6909 if (bits_on
!= DRT_HASH_GET_COUNT(cmap
, index
))
6910 panic("bits_on = %d, index = %d\n", bits_on
, index
);