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>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_map.h>
90 #include <vm/vm_pageout.h>
91 #include <vm/vm_fault.h>
93 #include <sys/kdebug.h>
94 #include <libkern/OSAtomic.h>
102 #define KERNEL_DEBUG KERNEL_DEBUG_CONSTANT
107 #define CL_WRITE 0x02
108 #define CL_ASYNC 0x04
109 #define CL_COMMIT 0x08
110 #define CL_PAGEOUT 0x10
112 #define CL_NOZERO 0x40
113 #define CL_PAGEIN 0x80
114 #define CL_DEV_MEMORY 0x100
115 #define CL_PRESERVE 0x200
116 #define CL_THROTTLE 0x400
117 #define CL_KEEPCACHED 0x800
118 #define CL_DIRECT_IO 0x1000
119 #define CL_PASSIVE 0x2000
120 #define CL_IOSTREAMING 0x4000
121 #define CL_CLOSE 0x8000
122 #define CL_ENCRYPTED 0x10000
123 #define CL_RAW_ENCRYPTED 0x20000
124 #define CL_NOCACHE 0x40000
126 #define MAX_VECTOR_UPL_ELEMENTS 8
127 #define MAX_VECTOR_UPL_SIZE (2 * MAX_UPL_SIZE_BYTES)
129 extern upl_t
vector_upl_create(vm_offset_t
);
130 extern boolean_t
vector_upl_is_valid(upl_t
);
131 extern boolean_t
vector_upl_set_subupl(upl_t
,upl_t
, u_int32_t
);
132 extern void vector_upl_set_pagelist(upl_t
);
133 extern void vector_upl_set_iostate(upl_t
, upl_t
, vm_offset_t
, u_int32_t
);
137 u_int io_completed
; /* amount of io that has currently completed */
138 u_int io_issued
; /* amount of io that was successfully issued */
139 int io_error
; /* error code of first error encountered */
140 int io_wanted
; /* someone is sleeping waiting for a change in state */
143 struct cl_direct_read_lock
{
144 LIST_ENTRY(cl_direct_read_lock
) chain
;
150 #define CL_DIRECT_READ_LOCK_BUCKETS 61
152 static LIST_HEAD(cl_direct_read_locks
, cl_direct_read_lock
)
153 cl_direct_read_locks
[CL_DIRECT_READ_LOCK_BUCKETS
];
155 static lck_spin_t cl_direct_read_spin_lock
;
157 static lck_grp_t
*cl_mtx_grp
;
158 static lck_attr_t
*cl_mtx_attr
;
159 static lck_grp_attr_t
*cl_mtx_grp_attr
;
160 static lck_mtx_t
*cl_transaction_mtxp
;
167 #define PUSH_DELAY 0x01
168 #define PUSH_ALL 0x02
169 #define PUSH_SYNC 0x04
172 static void cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
);
173 static void cluster_wait_IO(buf_t cbp_head
, int async
);
174 static void cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
);
176 static int cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
);
178 static int cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
179 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*)(buf_t
, void *), void *callback_arg
);
180 static int cluster_iodone(buf_t bp
, void *callback_arg
);
181 static int cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
, vnode_t vp
);
182 static int cluster_is_throttled(vnode_t vp
);
184 static void cluster_iostate_wait(struct clios
*iostate
, u_int target
, const char *wait_name
);
186 static void cluster_syncup(vnode_t vp
, off_t newEOF
, int (*)(buf_t
, void *), void *callback_arg
, int flags
);
188 static void cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int take_reference
);
189 static int cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
);
191 static int cluster_read_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t filesize
, int flags
,
192 int (*)(buf_t
, void *), void *callback_arg
);
193 static int cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
194 int flags
, int (*)(buf_t
, void *), void *callback_arg
);
195 static int cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
196 int (*)(buf_t
, void *), void *callback_arg
, int flags
);
198 static int cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
,
199 off_t headOff
, off_t tailOff
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
200 static int cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
,
201 int *write_type
, u_int32_t
*write_length
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
202 static int cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
,
203 int *write_type
, u_int32_t
*write_length
, int (*)(buf_t
, void *), void *callback_arg
, int bflag
);
205 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
);
207 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
);
208 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
);
210 static int cluster_push_now(vnode_t vp
, struct cl_extent
*, off_t EOF
, int flags
, int (*)(buf_t
, void *), void *callback_arg
);
212 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
);
214 static void sparse_cluster_switch(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int (*)(buf_t
, void *), void *callback_arg
);
215 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
);
216 static void sparse_cluster_add(void **cmapp
, vnode_t vp
, struct cl_extent
*, off_t EOF
, int (*)(buf_t
, void *), void *callback_arg
);
218 static kern_return_t
vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
);
219 static kern_return_t
vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
);
220 static kern_return_t
vfs_drt_control(void **cmapp
, int op_type
);
224 * For throttled IO to check whether
225 * a block is cached by the boot cache
226 * and thus it can avoid delaying the IO.
228 * bootcache_contains_block is initially
229 * NULL. The BootCache will set it while
230 * the cache is active and clear it when
231 * the cache is jettisoned.
233 * Returns 0 if the block is not
234 * contained in the cache, 1 if it is
237 * The function pointer remains valid
238 * after the cache has been evicted even
239 * if bootcache_contains_block has been
242 * See rdar://9974130 The new throttling mechanism breaks the boot cache for throttled IOs
244 int (*bootcache_contains_block
)(dev_t device
, u_int64_t blkno
) = NULL
;
248 * limit the internal I/O size so that we
249 * can represent it in a 32 bit int
251 #define MAX_IO_REQUEST_SIZE (1024 * 1024 * 512)
252 #define MAX_IO_CONTIG_SIZE MAX_UPL_SIZE_BYTES
255 * The MIN_DIRECT_WRITE_SIZE governs how much I/O should be issued before we consider
256 * allowing the caller to bypass the buffer cache. For small I/Os (less than 16k),
257 * we have not historically allowed the write to bypass the UBC.
259 #define MIN_DIRECT_WRITE_SIZE (16384)
261 #define WRITE_THROTTLE 6
262 #define WRITE_THROTTLE_SSD 2
263 #define WRITE_BEHIND 1
264 #define WRITE_BEHIND_SSD 1
267 #define PREFETCH_SSD 2
268 uint32_t speculative_prefetch_max
= (MAX_UPL_SIZE_BYTES
* 3); /* maximum bytes in a specluative read-ahead */
269 uint32_t speculative_prefetch_max_iosize
= (512 * 1024); /* maximum I/O size to use in a specluative read-ahead on SSDs*/
272 #define IO_SCALE(vp, base) (vp->v_mount->mnt_ioscale * (base))
273 #define MAX_CLUSTER_SIZE(vp) (cluster_max_io_size(vp->v_mount, CL_WRITE))
274 #define MAX_PREFETCH(vp, size, is_ssd) (size * IO_SCALE(vp, ((is_ssd && !ignore_is_ssd) ? PREFETCH_SSD : PREFETCH)))
276 int ignore_is_ssd
= 0;
277 int speculative_reads_disabled
= 0;
280 * throttle the number of async writes that
281 * can be outstanding on a single vnode
282 * before we issue a synchronous write
284 #define THROTTLE_MAXCNT 0
286 uint32_t throttle_max_iosize
= (128 * 1024);
288 #define THROTTLE_MAX_IOSIZE (throttle_max_iosize)
290 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_max_iosize
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_max_iosize
, 0, "");
296 * allocate lock group attribute and group
298 cl_mtx_grp_attr
= lck_grp_attr_alloc_init();
299 cl_mtx_grp
= lck_grp_alloc_init("cluster I/O", cl_mtx_grp_attr
);
302 * allocate the lock attribute
304 cl_mtx_attr
= lck_attr_alloc_init();
306 cl_transaction_mtxp
= lck_mtx_alloc_init(cl_mtx_grp
, cl_mtx_attr
);
308 if (cl_transaction_mtxp
== NULL
)
309 panic("cluster_init: failed to allocate cl_transaction_mtxp");
311 lck_spin_init(&cl_direct_read_spin_lock
, cl_mtx_grp
, cl_mtx_attr
);
313 for (int i
= 0; i
< CL_DIRECT_READ_LOCK_BUCKETS
; ++i
)
314 LIST_INIT(&cl_direct_read_locks
[i
]);
319 cluster_max_io_size(mount_t mp
, int type
)
321 uint32_t max_io_size
;
328 segcnt
= mp
->mnt_segreadcnt
;
329 maxcnt
= mp
->mnt_maxreadcnt
;
332 segcnt
= mp
->mnt_segwritecnt
;
333 maxcnt
= mp
->mnt_maxwritecnt
;
336 segcnt
= min(mp
->mnt_segreadcnt
, mp
->mnt_segwritecnt
);
337 maxcnt
= min(mp
->mnt_maxreadcnt
, mp
->mnt_maxwritecnt
);
340 if (segcnt
> (MAX_UPL_SIZE_BYTES
>> PAGE_SHIFT
)) {
342 * don't allow a size beyond the max UPL size we can create
344 segcnt
= MAX_UPL_SIZE_BYTES
>> PAGE_SHIFT
;
346 max_io_size
= min((segcnt
* PAGE_SIZE
), maxcnt
);
348 if (max_io_size
< MAX_UPL_TRANSFER_BYTES
) {
350 * don't allow a size smaller than the old fixed limit
352 max_io_size
= MAX_UPL_TRANSFER_BYTES
;
355 * make sure the size specified is a multiple of PAGE_SIZE
357 max_io_size
&= ~PAGE_MASK
;
359 return (max_io_size
);
365 #define CLW_ALLOCATE 0x01
366 #define CLW_RETURNLOCKED 0x02
367 #define CLW_IONOCACHE 0x04
368 #define CLW_IOPASSIVE 0x08
371 * if the read ahead context doesn't yet exist,
372 * allocate and initialize it...
373 * the vnode lock serializes multiple callers
374 * during the actual assignment... first one
375 * to grab the lock wins... the other callers
376 * will release the now unnecessary storage
378 * once the context is present, try to grab (but don't block on)
379 * the lock associated with it... if someone
380 * else currently owns it, than the read
381 * will run without read-ahead. this allows
382 * multiple readers to run in parallel and
383 * since there's only 1 read ahead context,
384 * there's no real loss in only allowing 1
385 * reader to have read-ahead enabled.
387 static struct cl_readahead
*
388 cluster_get_rap(vnode_t vp
)
390 struct ubc_info
*ubc
;
391 struct cl_readahead
*rap
;
395 if ((rap
= ubc
->cl_rahead
) == NULL
) {
396 MALLOC_ZONE(rap
, struct cl_readahead
*, sizeof *rap
, M_CLRDAHEAD
, M_WAITOK
);
398 bzero(rap
, sizeof *rap
);
400 lck_mtx_init(&rap
->cl_lockr
, cl_mtx_grp
, cl_mtx_attr
);
404 if (ubc
->cl_rahead
== NULL
)
405 ubc
->cl_rahead
= rap
;
407 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
408 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
409 rap
= ubc
->cl_rahead
;
413 if (lck_mtx_try_lock(&rap
->cl_lockr
) == TRUE
)
416 return ((struct cl_readahead
*)NULL
);
421 * if the write behind context doesn't yet exist,
422 * and CLW_ALLOCATE is specified, allocate and initialize it...
423 * the vnode lock serializes multiple callers
424 * during the actual assignment... first one
425 * to grab the lock wins... the other callers
426 * will release the now unnecessary storage
428 * if CLW_RETURNLOCKED is set, grab (blocking if necessary)
429 * the lock associated with the write behind context before
433 static struct cl_writebehind
*
434 cluster_get_wbp(vnode_t vp
, int flags
)
436 struct ubc_info
*ubc
;
437 struct cl_writebehind
*wbp
;
441 if ((wbp
= ubc
->cl_wbehind
) == NULL
) {
443 if ( !(flags
& CLW_ALLOCATE
))
444 return ((struct cl_writebehind
*)NULL
);
446 MALLOC_ZONE(wbp
, struct cl_writebehind
*, sizeof *wbp
, M_CLWRBEHIND
, M_WAITOK
);
448 bzero(wbp
, sizeof *wbp
);
449 lck_mtx_init(&wbp
->cl_lockw
, cl_mtx_grp
, cl_mtx_attr
);
453 if (ubc
->cl_wbehind
== NULL
)
454 ubc
->cl_wbehind
= wbp
;
456 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
457 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
458 wbp
= ubc
->cl_wbehind
;
462 if (flags
& CLW_RETURNLOCKED
)
463 lck_mtx_lock(&wbp
->cl_lockw
);
470 cluster_syncup(vnode_t vp
, off_t newEOF
, int (*callback
)(buf_t
, void *), void *callback_arg
, int flags
)
472 struct cl_writebehind
*wbp
;
474 if ((wbp
= cluster_get_wbp(vp
, 0)) != NULL
) {
476 if (wbp
->cl_number
) {
477 lck_mtx_lock(&wbp
->cl_lockw
);
479 cluster_try_push(wbp
, vp
, newEOF
, PUSH_ALL
| flags
, 0, callback
, callback_arg
);
481 lck_mtx_unlock(&wbp
->cl_lockw
);
488 cluster_io_present_in_BC(vnode_t vp
, off_t f_offset
)
492 int (*bootcache_check_fn
)(dev_t device
, u_int64_t blkno
) = bootcache_contains_block
;
494 if (bootcache_check_fn
) {
495 if (VNOP_BLOCKMAP(vp
, f_offset
, PAGE_SIZE
, &blkno
, &io_size
, NULL
, VNODE_READ
, NULL
))
501 if (bootcache_check_fn(vp
->v_mount
->mnt_devvp
->v_rdev
, blkno
))
509 cluster_is_throttled(vnode_t vp
)
511 return (throttle_io_will_be_throttled(-1, vp
->v_mount
));
516 cluster_iostate_wait(struct clios
*iostate
, u_int target
, const char *wait_name
)
519 lck_mtx_lock(&iostate
->io_mtxp
);
521 while ((iostate
->io_issued
- iostate
->io_completed
) > target
) {
523 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_START
,
524 iostate
->io_issued
, iostate
->io_completed
, target
, 0, 0);
526 iostate
->io_wanted
= 1;
527 msleep((caddr_t
)&iostate
->io_wanted
, &iostate
->io_mtxp
, PRIBIO
+ 1, wait_name
, NULL
);
529 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 95)) | DBG_FUNC_END
,
530 iostate
->io_issued
, iostate
->io_completed
, target
, 0, 0);
532 lck_mtx_unlock(&iostate
->io_mtxp
);
535 static void cluster_handle_associated_upl(struct clios
*iostate
, upl_t upl
,
536 upl_offset_t upl_offset
, upl_size_t size
)
541 upl_t associated_upl
= upl_associated_upl(upl
);
547 printf("1: %d %d\n", upl_offset
, upl_offset
+ size
);
551 * The associated UPL is page aligned to file offsets whereas the
552 * UPL it's attached to has different alignment requirements. The
553 * upl_offset that we have refers to @upl. The code that follows
554 * has to deal with the first and last pages in this transaction
555 * which might straddle pages in the associated UPL. To keep
556 * track of these pages, we use the mark bits: if the mark bit is
557 * set, we know another transaction has completed its part of that
558 * page and so we can unlock that page here.
560 * The following illustrates what we have to deal with:
562 * MEM u <------------ 1 PAGE ------------> e
563 * +-------------+----------------------+-----------------
564 * | |######################|#################
565 * +-------------+----------------------+-----------------
566 * FILE | <--- a ---> o <------------ 1 PAGE ------------>
568 * So here we show a write to offset @o. The data that is to be
569 * written is in a buffer that is not page aligned; it has offset
570 * @a in the page. The upl that carries the data starts in memory
571 * at @u. The associated upl starts in the file at offset @o. A
572 * transaction will always end on a page boundary (like @e above)
573 * except for the very last transaction in the group. We cannot
574 * unlock the page at @o in the associated upl until both the
575 * transaction ending at @e and the following transaction (that
576 * starts at @e) has completed.
580 * We record whether or not the two UPLs are aligned as the mark
581 * bit in the first page of @upl.
583 upl_page_info_t
*pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
584 bool is_unaligned
= upl_page_get_mark(pl
, 0);
587 upl_page_info_t
*assoc_pl
= UPL_GET_INTERNAL_PAGE_LIST(associated_upl
);
589 upl_offset_t upl_end
= upl_offset
+ size
;
590 assert(upl_end
>= PAGE_SIZE
);
592 upl_size_t assoc_upl_size
= upl_get_size(associated_upl
);
595 * In the very first transaction in the group, upl_offset will
596 * not be page aligned, but after that it will be and in that
597 * case we want the preceding page in the associated UPL hence
602 upl_offset
= trunc_page_32(upl_offset
- 1);
604 lck_mtx_lock_spin(&iostate
->io_mtxp
);
606 // Look at the first page...
608 && !upl_page_get_mark(assoc_pl
, upl_offset
>> PAGE_SHIFT
)) {
610 * The first page isn't marked so let another transaction
611 * completion handle it.
613 upl_page_set_mark(assoc_pl
, upl_offset
>> PAGE_SHIFT
, true);
614 upl_offset
+= PAGE_SIZE
;
617 // And now the last page...
620 * This needs to be > rather than >= because if it's equal, it
621 * means there's another transaction that is sharing the last
624 if (upl_end
> assoc_upl_size
)
625 upl_end
= assoc_upl_size
;
627 upl_end
= trunc_page_32(upl_end
);
628 const int last_pg
= (upl_end
>> PAGE_SHIFT
) - 1;
630 if (!upl_page_get_mark(assoc_pl
, last_pg
)) {
632 * The last page isn't marked so mark the page and let another
633 * transaction completion handle it.
635 upl_page_set_mark(assoc_pl
, last_pg
, true);
636 upl_end
-= PAGE_SIZE
;
640 lck_mtx_unlock(&iostate
->io_mtxp
);
643 printf("2: %d %d\n", upl_offset
, upl_end
);
646 if (upl_end
<= upl_offset
)
649 size
= upl_end
- upl_offset
;
651 assert(!(upl_offset
& PAGE_MASK
));
652 assert(!(size
& PAGE_MASK
));
658 * We can unlock these pages now and as this is for a
659 * direct/uncached write, we want to dump the pages too.
661 kern_return_t kr
= upl_abort_range(associated_upl
, upl_offset
, size
,
662 UPL_ABORT_DUMP_PAGES
, &empty
);
667 upl_set_associated_upl(upl
, NULL
);
668 upl_deallocate(associated_upl
);
673 cluster_ioerror(upl_t upl
, int upl_offset
, int abort_size
, int error
, int io_flags
, vnode_t vp
)
675 int upl_abort_code
= 0;
679 if ((io_flags
& (B_PHYS
| B_CACHE
)) == (B_PHYS
| B_CACHE
))
681 * direct write of any flavor, or a direct read that wasn't aligned
683 ubc_upl_commit_range(upl
, upl_offset
, abort_size
, UPL_COMMIT_FREE_ON_EMPTY
);
685 if (io_flags
& B_PAGEIO
) {
686 if (io_flags
& B_READ
)
691 if (io_flags
& B_CACHE
)
693 * leave pages in the cache unchanged on error
695 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
696 else if (page_out
&& ((error
!= ENXIO
) || vnode_isswap(vp
)))
698 * transient error... leave pages unchanged
700 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
702 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
704 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
706 ubc_upl_abort_range(upl
, upl_offset
, abort_size
, upl_abort_code
);
708 return (upl_abort_code
);
713 cluster_iodone(buf_t bp
, void *callback_arg
)
724 int transaction_size
= 0;
731 struct clios
*iostate
;
732 boolean_t transaction_complete
= FALSE
;
734 __IGNORE_WCASTALIGN(cbp_head
= (buf_t
)(bp
->b_trans_head
));
736 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_START
,
737 cbp_head
, bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
739 if (cbp_head
->b_trans_next
|| !(cbp_head
->b_flags
& B_EOT
)) {
740 boolean_t need_wakeup
= FALSE
;
742 lck_mtx_lock_spin(cl_transaction_mtxp
);
744 bp
->b_flags
|= B_TDONE
;
746 if (bp
->b_flags
& B_TWANTED
) {
747 CLR(bp
->b_flags
, B_TWANTED
);
750 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
752 * all I/O requests that are part of this transaction
753 * have to complete before we can process it
755 if ( !(cbp
->b_flags
& B_TDONE
)) {
757 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
758 cbp_head
, cbp
, cbp
->b_bcount
, cbp
->b_flags
, 0);
760 lck_mtx_unlock(cl_transaction_mtxp
);
762 if (need_wakeup
== TRUE
)
767 if (cbp
->b_flags
& B_EOT
)
768 transaction_complete
= TRUE
;
770 lck_mtx_unlock(cl_transaction_mtxp
);
772 if (need_wakeup
== TRUE
)
775 if (transaction_complete
== FALSE
) {
776 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
777 cbp_head
, 0, 0, 0, 0);
787 upl_offset
= cbp
->b_uploffset
;
789 b_flags
= cbp
->b_flags
;
790 real_bp
= cbp
->b_real_bp
;
791 zero_offset
= cbp
->b_validend
;
792 iostate
= (struct clios
*)cbp
->b_iostate
;
795 real_bp
->b_dev
= cbp
->b_dev
;
798 if ((cbp
->b_flags
& B_ERROR
) && error
== 0)
799 error
= cbp
->b_error
;
801 total_resid
+= cbp
->b_resid
;
802 total_size
+= cbp
->b_bcount
;
804 cbp_next
= cbp
->b_trans_next
;
806 if (cbp_next
== NULL
)
808 * compute the overall size of the transaction
809 * in case we created one that has 'holes' in it
810 * 'total_size' represents the amount of I/O we
811 * did, not the span of the transaction w/r to the UPL
813 transaction_size
= cbp
->b_uploffset
+ cbp
->b_bcount
- upl_offset
;
821 if (ISSET(b_flags
, B_COMMIT_UPL
)) {
822 cluster_handle_associated_upl(iostate
,
828 if (error
== 0 && total_resid
)
832 int (*cliodone_func
)(buf_t
, void *) = (int (*)(buf_t
, void *))(cbp_head
->b_cliodone
);
834 if (cliodone_func
!= NULL
) {
835 cbp_head
->b_bcount
= transaction_size
;
837 error
= (*cliodone_func
)(cbp_head
, callback_arg
);
841 cluster_zero(upl
, zero_offset
, PAGE_SIZE
- (zero_offset
& PAGE_MASK
), real_bp
);
843 free_io_buf(cbp_head
);
849 * someone has issued multiple I/Os asynchrounsly
850 * and is waiting for them to complete (streaming)
852 lck_mtx_lock_spin(&iostate
->io_mtxp
);
854 if (error
&& iostate
->io_error
== 0)
855 iostate
->io_error
= error
;
857 iostate
->io_completed
+= total_size
;
859 if (iostate
->io_wanted
) {
861 * someone is waiting for the state of
862 * this io stream to change
864 iostate
->io_wanted
= 0;
867 lck_mtx_unlock(&iostate
->io_mtxp
);
870 wakeup((caddr_t
)&iostate
->io_wanted
);
873 if (b_flags
& B_COMMIT_UPL
) {
874 pg_offset
= upl_offset
& PAGE_MASK
;
875 commit_size
= (pg_offset
+ transaction_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
878 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, commit_size
, error
, b_flags
, vp
);
880 upl_flags
= UPL_COMMIT_FREE_ON_EMPTY
;
882 if ((b_flags
& B_PHYS
) && (b_flags
& B_READ
))
883 upl_flags
|= UPL_COMMIT_SET_DIRTY
;
886 upl_flags
|= UPL_COMMIT_INACTIVATE
;
888 ubc_upl_commit_range(upl
, upl_offset
- pg_offset
, commit_size
, upl_flags
);
893 real_bp
->b_flags
|= B_ERROR
;
894 real_bp
->b_error
= error
;
896 real_bp
->b_resid
= total_resid
;
898 buf_biodone(real_bp
);
900 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
901 upl
, upl_offset
- pg_offset
, commit_size
, (error
<< 24) | upl_flags
, 0);
908 cluster_throttle_io_limit(vnode_t vp
, uint32_t *limit
)
910 if (cluster_is_throttled(vp
)) {
911 *limit
= THROTTLE_MAX_IOSIZE
;
919 cluster_zero(upl_t upl
, upl_offset_t upl_offset
, int size
, buf_t bp
)
922 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_START
,
923 upl_offset
, size
, bp
, 0, 0);
925 if (bp
== NULL
|| bp
->b_datap
== 0) {
929 pl
= ubc_upl_pageinfo(upl
);
931 if (upl_device_page(pl
) == TRUE
) {
932 zero_addr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + upl_offset
;
934 bzero_phys_nc(zero_addr
, size
);
941 page_index
= upl_offset
/ PAGE_SIZE
;
942 page_offset
= upl_offset
& PAGE_MASK
;
944 zero_addr
= ((addr64_t
)upl_phys_page(pl
, page_index
) << PAGE_SHIFT
) + page_offset
;
945 zero_cnt
= min(PAGE_SIZE
- page_offset
, size
);
947 bzero_phys(zero_addr
, zero_cnt
);
950 upl_offset
+= zero_cnt
;
954 bzero((caddr_t
)((vm_offset_t
)bp
->b_datap
+ upl_offset
), size
);
956 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_END
,
957 upl_offset
, size
, 0, 0, 0);
962 cluster_EOT(buf_t cbp_head
, buf_t cbp_tail
, int zero_offset
)
964 cbp_head
->b_validend
= zero_offset
;
965 cbp_tail
->b_flags
|= B_EOT
;
969 cluster_wait_IO(buf_t cbp_head
, int async
)
975 * async callback completion will not normally
976 * generate a wakeup upon I/O completion...
977 * by setting B_TWANTED, we will force a wakeup
978 * to occur as any outstanding I/Os complete...
979 * I/Os already completed will have B_TDONE already
980 * set and we won't cause us to block
981 * note that we're actually waiting for the bp to have
982 * completed the callback function... only then
983 * can we safely take back ownership of the bp
985 lck_mtx_lock_spin(cl_transaction_mtxp
);
987 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
988 cbp
->b_flags
|= B_TWANTED
;
990 lck_mtx_unlock(cl_transaction_mtxp
);
992 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
995 while (!ISSET(cbp
->b_flags
, B_TDONE
)) {
997 lck_mtx_lock_spin(cl_transaction_mtxp
);
999 if (!ISSET(cbp
->b_flags
, B_TDONE
)) {
1000 DTRACE_IO1(wait__start
, buf_t
, cbp
);
1001 (void) msleep(cbp
, cl_transaction_mtxp
, PDROP
| (PRIBIO
+1), "cluster_wait_IO", NULL
);
1002 DTRACE_IO1(wait__done
, buf_t
, cbp
);
1004 lck_mtx_unlock(cl_transaction_mtxp
);
1012 cluster_complete_transaction(buf_t
*cbp_head
, void *callback_arg
, int *retval
, int flags
, int needwait
)
1016 boolean_t isswapout
= FALSE
;
1019 * cluster_complete_transaction will
1020 * only be called if we've issued a complete chain in synchronous mode
1021 * or, we've already done a cluster_wait_IO on an incomplete chain
1024 for (cbp
= *cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
1028 * we've already waited on all of the I/Os in this transaction,
1029 * so mark all of the buf_t's in this transaction as B_TDONE
1030 * so that cluster_iodone sees the transaction as completed
1032 for (cbp
= *cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
1033 cbp
->b_flags
|= B_TDONE
;
1036 if ((flags
& (CL_ASYNC
| CL_PAGEOUT
)) == CL_PAGEOUT
&& vnode_isswap(cbp
->b_vp
))
1039 error
= cluster_iodone(cbp
, callback_arg
);
1041 if ( !(flags
& CL_ASYNC
) && error
&& *retval
== 0) {
1042 if (((flags
& (CL_PAGEOUT
| CL_KEEPCACHED
)) != CL_PAGEOUT
) || (error
!= ENXIO
))
1044 else if (isswapout
== TRUE
)
1047 *cbp_head
= (buf_t
)NULL
;
1052 cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
1053 int flags
, buf_t real_bp
, struct clios
*iostate
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1062 buf_t cbp_head
= NULL
;
1063 buf_t cbp_tail
= NULL
;
1064 int trans_count
= 0;
1065 int max_trans_count
;
1071 int zero_offset
= 0;
1072 int async_throttle
= 0;
1074 vm_offset_t upl_end_offset
;
1075 boolean_t need_EOT
= FALSE
;
1078 * we currently don't support buffers larger than a page
1080 if (real_bp
&& non_rounded_size
> PAGE_SIZE
)
1081 panic("%s(): Called with real buffer of size %d bytes which "
1082 "is greater than the maximum allowed size of "
1083 "%d bytes (the system PAGE_SIZE).\n",
1084 __FUNCTION__
, non_rounded_size
, PAGE_SIZE
);
1089 * we don't want to do any funny rounding of the size for IO requests
1090 * coming through the DIRECT or CONTIGUOUS paths... those pages don't
1091 * belong to us... we can't extend (nor do we need to) the I/O to fill
1094 if (mp
->mnt_devblocksize
> 1 && !(flags
& (CL_DEV_MEMORY
| CL_DIRECT_IO
))) {
1096 * round the requested size up so that this I/O ends on a
1097 * page boundary in case this is a 'write'... if the filesystem
1098 * has blocks allocated to back the page beyond the EOF, we want to
1099 * make sure to write out the zero's that are sitting beyond the EOF
1100 * so that in case the filesystem doesn't explicitly zero this area
1101 * if a hole is created via a lseek/write beyond the current EOF,
1102 * it will return zeros when it's read back from the disk. If the
1103 * physical allocation doesn't extend for the whole page, we'll
1104 * only write/read from the disk up to the end of this allocation
1105 * via the extent info returned from the VNOP_BLOCKMAP call.
1107 pg_offset
= upl_offset
& PAGE_MASK
;
1109 size
= (((non_rounded_size
+ pg_offset
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - pg_offset
;
1112 * anyone advertising a blocksize of 1 byte probably
1113 * can't deal with us rounding up the request size
1114 * AFP is one such filesystem/device
1116 size
= non_rounded_size
;
1118 upl_end_offset
= upl_offset
+ size
;
1120 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_START
, (int)f_offset
, size
, upl_offset
, flags
, 0);
1123 * Set the maximum transaction size to the maximum desired number of
1126 max_trans_count
= 8;
1127 if (flags
& CL_DEV_MEMORY
)
1128 max_trans_count
= 16;
1130 if (flags
& CL_READ
) {
1132 bmap_flags
= VNODE_READ
;
1134 max_iosize
= mp
->mnt_maxreadcnt
;
1135 max_vectors
= mp
->mnt_segreadcnt
;
1138 bmap_flags
= VNODE_WRITE
;
1140 max_iosize
= mp
->mnt_maxwritecnt
;
1141 max_vectors
= mp
->mnt_segwritecnt
;
1143 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_NONE
, max_iosize
, max_vectors
, mp
->mnt_devblocksize
, 0, 0);
1146 * make sure the maximum iosize is a
1147 * multiple of the page size
1149 max_iosize
&= ~PAGE_MASK
;
1152 * Ensure the maximum iosize is sensible.
1155 max_iosize
= PAGE_SIZE
;
1157 if (flags
& CL_THROTTLE
) {
1158 if ( !(flags
& CL_PAGEOUT
) && cluster_is_throttled(vp
)) {
1159 if (max_iosize
> THROTTLE_MAX_IOSIZE
)
1160 max_iosize
= THROTTLE_MAX_IOSIZE
;
1161 async_throttle
= THROTTLE_MAXCNT
;
1163 if ( (flags
& CL_DEV_MEMORY
) )
1164 async_throttle
= IO_SCALE(vp
, VNODE_ASYNC_THROTTLE
);
1167 u_int max_cluster_size
;
1170 max_cluster_size
= MAX_CLUSTER_SIZE(vp
);
1172 if (max_iosize
> max_cluster_size
)
1173 max_cluster
= max_cluster_size
;
1175 max_cluster
= max_iosize
;
1177 if (size
< max_cluster
)
1180 if ((vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
) && !ignore_is_ssd
)
1181 scale
= WRITE_THROTTLE_SSD
;
1183 scale
= WRITE_THROTTLE
;
1185 if (flags
& CL_CLOSE
)
1186 scale
+= MAX_CLUSTERS
;
1188 async_throttle
= min(IO_SCALE(vp
, VNODE_ASYNC_THROTTLE
), ((scale
* max_cluster_size
) / max_cluster
) - 1);
1194 if (flags
& (CL_PAGEIN
| CL_PAGEOUT
))
1195 io_flags
|= B_PAGEIO
;
1196 if (flags
& (CL_IOSTREAMING
))
1197 io_flags
|= B_IOSTREAMING
;
1198 if (flags
& CL_COMMIT
)
1199 io_flags
|= B_COMMIT_UPL
;
1200 if (flags
& CL_DIRECT_IO
)
1202 if (flags
& (CL_PRESERVE
| CL_KEEPCACHED
))
1203 io_flags
|= B_CACHE
;
1204 if (flags
& CL_PASSIVE
)
1205 io_flags
|= B_PASSIVE
;
1206 if (flags
& CL_ENCRYPTED
)
1207 io_flags
|= B_ENCRYPTED_IO
;
1209 if (vp
->v_flag
& VSYSTEM
)
1212 if ((flags
& CL_READ
) && ((upl_offset
+ non_rounded_size
) & PAGE_MASK
) && (!(flags
& CL_NOZERO
))) {
1214 * then we are going to end up
1215 * with a page that we can't complete (the file size wasn't a multiple
1216 * of PAGE_SIZE and we're trying to read to the end of the file
1217 * so we'll go ahead and zero out the portion of the page we can't
1218 * read in from the file
1220 zero_offset
= upl_offset
+ non_rounded_size
;
1221 } else if (!ISSET(flags
, CL_READ
) && ISSET(flags
, CL_DIRECT_IO
)) {
1222 assert(ISSET(flags
, CL_COMMIT
));
1224 // For a direct/uncached write, we need to lock pages...
1229 * Create a UPL to lock the pages in the cache whilst the
1230 * write is in progress.
1232 ubc_create_upl(vp
, f_offset
, non_rounded_size
, &cached_upl
,
1233 NULL
, UPL_SET_LITE
);
1236 * Attach this UPL to the other UPL so that we can find it
1239 upl_set_associated_upl(upl
, cached_upl
);
1241 if (upl_offset
& PAGE_MASK
) {
1243 * The two UPLs are not aligned, so mark the first page in
1244 * @upl so that cluster_handle_associated_upl can handle
1247 upl_page_info_t
*pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
1248 upl_page_set_mark(pl
, 0, true);
1255 u_int io_size_wanted
;
1258 if (size
> max_iosize
)
1259 io_size
= max_iosize
;
1263 io_size_wanted
= io_size
;
1264 io_size_tmp
= (size_t)io_size
;
1266 if ((error
= VNOP_BLOCKMAP(vp
, f_offset
, io_size
, &blkno
, &io_size_tmp
, NULL
, bmap_flags
, NULL
)))
1269 if (io_size_tmp
> io_size_wanted
)
1270 io_size
= io_size_wanted
;
1272 io_size
= (u_int
)io_size_tmp
;
1274 if (real_bp
&& (real_bp
->b_blkno
== real_bp
->b_lblkno
))
1275 real_bp
->b_blkno
= blkno
;
1277 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 24)) | DBG_FUNC_NONE
,
1278 (int)f_offset
, (int)(blkno
>>32), (int)blkno
, io_size
, 0);
1282 * vnop_blockmap didn't return an error... however, it did
1283 * return an extent size of 0 which means we can't
1284 * make forward progress on this I/O... a hole in the
1285 * file would be returned as a blkno of -1 with a non-zero io_size
1286 * a real extent is returned with a blkno != -1 and a non-zero io_size
1291 if ( !(flags
& CL_READ
) && blkno
== -1) {
1295 if (upl_get_internal_vectorupl(upl
))
1296 panic("Vector UPLs should not take this code-path\n");
1298 * we're writing into a 'hole'
1300 if (flags
& CL_PAGEOUT
) {
1302 * if we got here via cluster_pageout
1303 * then just error the request and return
1304 * the 'hole' should already have been covered
1310 * we can get here if the cluster code happens to
1311 * pick up a page that was dirtied via mmap vs
1312 * a 'write' and the page targets a 'hole'...
1313 * i.e. the writes to the cluster were sparse
1314 * and the file was being written for the first time
1316 * we can also get here if the filesystem supports
1317 * 'holes' that are less than PAGE_SIZE.... because
1318 * we can't know if the range in the page that covers
1319 * the 'hole' has been dirtied via an mmap or not,
1320 * we have to assume the worst and try to push the
1321 * entire page to storage.
1323 * Try paging out the page individually before
1324 * giving up entirely and dumping it (the pageout
1325 * path will insure that the zero extent accounting
1326 * has been taken care of before we get back into cluster_io)
1328 * go direct to vnode_pageout so that we don't have to
1329 * unbusy the page from the UPL... we used to do this
1330 * so that we could call ubc_msync, but that results
1331 * in a potential deadlock if someone else races us to acquire
1332 * that page and wins and in addition needs one of the pages
1333 * we're continuing to hold in the UPL
1335 pageout_flags
= UPL_MSYNC
| UPL_VNODE_PAGER
| UPL_NESTED_PAGEOUT
;
1337 if ( !(flags
& CL_ASYNC
))
1338 pageout_flags
|= UPL_IOSYNC
;
1339 if ( !(flags
& CL_COMMIT
))
1340 pageout_flags
|= UPL_NOCOMMIT
;
1346 * first we have to wait for the the current outstanding I/Os
1347 * to complete... EOT hasn't been set yet on this transaction
1348 * so the pages won't be released just because all of the current
1349 * I/O linked to this transaction has completed...
1351 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1354 * we've got a transcation that
1355 * includes the page we're about to push out through vnode_pageout...
1356 * find the last bp in the list which will be the one that
1357 * includes the head of this page and round it's iosize down
1358 * to a page boundary...
1360 for (last_cbp
= cbp
= cbp_head
; cbp
->b_trans_next
; cbp
= cbp
->b_trans_next
)
1363 cbp
->b_bcount
&= ~PAGE_MASK
;
1365 if (cbp
->b_bcount
== 0) {
1367 * this buf no longer has any I/O associated with it
1371 if (cbp
== cbp_head
) {
1373 * the buf we just freed was the only buf in
1374 * this transaction... so there's no I/O to do
1379 * remove the buf we just freed from
1380 * the transaction list
1382 last_cbp
->b_trans_next
= NULL
;
1383 cbp_tail
= last_cbp
;
1388 * there was more to the current transaction
1389 * than just the page we are pushing out via vnode_pageout...
1390 * mark it as finished and complete it... we've already
1391 * waited for the I/Os to complete above in the call to cluster_wait_IO
1393 cluster_EOT(cbp_head
, cbp_tail
, 0);
1395 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1400 if (vnode_pageout(vp
, upl
, trunc_page(upl_offset
), trunc_page_64(f_offset
), PAGE_SIZE
, pageout_flags
, NULL
) != PAGER_SUCCESS
) {
1403 e_offset
= round_page_64(f_offset
+ 1);
1404 io_size
= e_offset
- f_offset
;
1406 f_offset
+= io_size
;
1407 upl_offset
+= io_size
;
1409 if (size
>= io_size
)
1414 * keep track of how much of the original request
1415 * that we've actually completed... non_rounded_size
1416 * may go negative due to us rounding the request
1417 * to a page size multiple (i.e. size > non_rounded_size)
1419 non_rounded_size
-= io_size
;
1421 if (non_rounded_size
<= 0) {
1423 * we've transferred all of the data in the original
1424 * request, but we were unable to complete the tail
1425 * of the last page because the file didn't have
1426 * an allocation to back that portion... this is ok.
1432 flags
&= ~CL_COMMIT
;
1437 lblkno
= (daddr64_t
)(f_offset
/ 0x1000);
1439 * we have now figured out how much I/O we can do - this is in 'io_size'
1440 * pg_offset is the starting point in the first page for the I/O
1441 * pg_count is the number of full and partial pages that 'io_size' encompasses
1443 pg_offset
= upl_offset
& PAGE_MASK
;
1445 if (flags
& CL_DEV_MEMORY
) {
1447 * treat physical requests as one 'giant' page
1451 pg_count
= (io_size
+ pg_offset
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1453 if ((flags
& CL_READ
) && blkno
== -1) {
1454 vm_offset_t commit_offset
;
1456 int complete_transaction_now
= 0;
1459 * if we're reading and blkno == -1, then we've got a
1460 * 'hole' in the file that we need to deal with by zeroing
1461 * out the affected area in the upl
1463 if (io_size
>= (u_int
)non_rounded_size
) {
1465 * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
1466 * than 'zero_offset' will be non-zero
1467 * if the 'hole' returned by vnop_blockmap extends all the way to the eof
1468 * (indicated by the io_size finishing off the I/O request for this UPL)
1469 * than we're not going to issue an I/O for the
1470 * last page in this upl... we need to zero both the hole and the tail
1471 * of the page beyond the EOF, since the delayed zero-fill won't kick in
1473 bytes_to_zero
= non_rounded_size
;
1474 if (!(flags
& CL_NOZERO
))
1475 bytes_to_zero
= (((upl_offset
+ io_size
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - upl_offset
;
1479 bytes_to_zero
= io_size
;
1483 cluster_zero(upl
, upl_offset
, bytes_to_zero
, real_bp
);
1489 * if there is a current I/O chain pending
1490 * then the first page of the group we just zero'd
1491 * will be handled by the I/O completion if the zero
1492 * fill started in the middle of the page
1494 commit_offset
= (upl_offset
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1496 pg_resid
= commit_offset
- upl_offset
;
1498 if (bytes_to_zero
>= pg_resid
) {
1500 * the last page of the current I/O
1501 * has been completed...
1502 * compute the number of fully zero'd
1503 * pages that are beyond it
1504 * plus the last page if its partial
1505 * and we have no more I/O to issue...
1506 * otherwise a partial page is left
1507 * to begin the next I/O
1509 if ((int)io_size
>= non_rounded_size
)
1510 pg_count
= (bytes_to_zero
- pg_resid
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1512 pg_count
= (bytes_to_zero
- pg_resid
) / PAGE_SIZE
;
1514 complete_transaction_now
= 1;
1518 * no pending I/O to deal with
1519 * so, commit all of the fully zero'd pages
1520 * plus the last page if its partial
1521 * and we have no more I/O to issue...
1522 * otherwise a partial page is left
1523 * to begin the next I/O
1525 if ((int)io_size
>= non_rounded_size
)
1526 pg_count
= (pg_offset
+ bytes_to_zero
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1528 pg_count
= (pg_offset
+ bytes_to_zero
) / PAGE_SIZE
;
1530 commit_offset
= upl_offset
& ~PAGE_MASK
;
1533 // Associated UPL is currently only used in the direct write path
1534 assert(!upl_associated_upl(upl
));
1536 if ( (flags
& CL_COMMIT
) && pg_count
) {
1537 ubc_upl_commit_range(upl
, commit_offset
, pg_count
* PAGE_SIZE
,
1538 UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
);
1540 upl_offset
+= io_size
;
1541 f_offset
+= io_size
;
1545 * keep track of how much of the original request
1546 * that we've actually completed... non_rounded_size
1547 * may go negative due to us rounding the request
1548 * to a page size multiple (i.e. size > non_rounded_size)
1550 non_rounded_size
-= io_size
;
1552 if (non_rounded_size
<= 0) {
1554 * we've transferred all of the data in the original
1555 * request, but we were unable to complete the tail
1556 * of the last page because the file didn't have
1557 * an allocation to back that portion... this is ok.
1561 if (cbp_head
&& (complete_transaction_now
|| size
== 0)) {
1562 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1564 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1566 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 0);
1572 if (pg_count
> max_vectors
) {
1573 if (((pg_count
- max_vectors
) * PAGE_SIZE
) > io_size
) {
1574 io_size
= PAGE_SIZE
- pg_offset
;
1577 io_size
-= (pg_count
- max_vectors
) * PAGE_SIZE
;
1578 pg_count
= max_vectors
;
1582 * If the transaction is going to reach the maximum number of
1583 * desired elements, truncate the i/o to the nearest page so
1584 * that the actual i/o is initiated after this buffer is
1585 * created and added to the i/o chain.
1587 * I/O directed to physically contiguous memory
1588 * doesn't have a requirement to make sure we 'fill' a page
1590 if ( !(flags
& CL_DEV_MEMORY
) && trans_count
>= max_trans_count
&&
1591 ((upl_offset
+ io_size
) & PAGE_MASK
)) {
1592 vm_offset_t aligned_ofs
;
1594 aligned_ofs
= (upl_offset
+ io_size
) & ~PAGE_MASK
;
1596 * If the io_size does not actually finish off even a
1597 * single page we have to keep adding buffers to the
1598 * transaction despite having reached the desired limit.
1600 * Eventually we get here with the page being finished
1601 * off (and exceeded) and then we truncate the size of
1602 * this i/o request so that it is page aligned so that
1603 * we can finally issue the i/o on the transaction.
1605 if (aligned_ofs
> upl_offset
) {
1606 io_size
= aligned_ofs
- upl_offset
;
1611 if ( !(mp
->mnt_kern_flag
& MNTK_VIRTUALDEV
))
1613 * if we're not targeting a virtual device i.e. a disk image
1614 * it's safe to dip into the reserve pool since real devices
1615 * can complete this I/O request without requiring additional
1616 * bufs from the alloc_io_buf pool
1619 else if ((flags
& CL_ASYNC
) && !(flags
& CL_PAGEOUT
))
1621 * Throttle the speculative IO
1627 cbp
= alloc_io_buf(vp
, priv
);
1629 if (flags
& CL_PAGEOUT
) {
1633 * since blocks are in offsets of 0x1000, scale
1634 * iteration to (PAGE_SIZE * pg_count) of blks.
1636 for (i
= 0; i
< (PAGE_SIZE
* pg_count
)/0x1000; i
++) {
1637 if (buf_invalblkno(vp
, lblkno
+ i
, 0) == EBUSY
)
1638 panic("BUSY bp found in cluster_io");
1641 if (flags
& CL_ASYNC
) {
1642 if (buf_setcallback(cbp
, (void *)cluster_iodone
, callback_arg
))
1643 panic("buf_setcallback failed\n");
1645 cbp
->b_cliodone
= (void *)callback
;
1646 cbp
->b_flags
|= io_flags
;
1647 if (flags
& CL_NOCACHE
)
1648 cbp
->b_attr
.ba_flags
|= BA_NOCACHE
;
1650 cbp
->b_lblkno
= lblkno
;
1651 cbp
->b_blkno
= blkno
;
1652 cbp
->b_bcount
= io_size
;
1654 if (buf_setupl(cbp
, upl
, upl_offset
))
1655 panic("buf_setupl failed\n");
1657 upl_set_blkno(upl
, upl_offset
, io_size
, blkno
);
1659 cbp
->b_trans_next
= (buf_t
)NULL
;
1661 if ((cbp
->b_iostate
= (void *)iostate
))
1663 * caller wants to track the state of this
1664 * io... bump the amount issued against this stream
1666 iostate
->io_issued
+= io_size
;
1668 if (flags
& CL_READ
) {
1669 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 26)) | DBG_FUNC_NONE
,
1670 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1673 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 27)) | DBG_FUNC_NONE
,
1674 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
1678 cbp_tail
->b_trans_next
= cbp
;
1684 if ( (cbp_head
->b_real_bp
= real_bp
) )
1685 real_bp
= (buf_t
)NULL
;
1687 *(buf_t
*)(&cbp
->b_trans_head
) = cbp_head
;
1691 upl_offset
+= io_size
;
1692 f_offset
+= io_size
;
1695 * keep track of how much of the original request
1696 * that we've actually completed... non_rounded_size
1697 * may go negative due to us rounding the request
1698 * to a page size multiple (i.e. size > non_rounded_size)
1700 non_rounded_size
-= io_size
;
1702 if (non_rounded_size
<= 0) {
1704 * we've transferred all of the data in the original
1705 * request, but we were unable to complete the tail
1706 * of the last page because the file didn't have
1707 * an allocation to back that portion... this is ok.
1713 * we have no more I/O to issue, so go
1714 * finish the final transaction
1717 } else if ( ((flags
& CL_DEV_MEMORY
) || (upl_offset
& PAGE_MASK
) == 0) &&
1718 ((flags
& CL_ASYNC
) || trans_count
> max_trans_count
) ) {
1720 * I/O directed to physically contiguous memory...
1721 * which doesn't have a requirement to make sure we 'fill' a page
1723 * the current I/O we've prepared fully
1724 * completes the last page in this request
1726 * it's either an ASYNC request or
1727 * we've already accumulated more than 8 I/O's into
1728 * this transaction so mark it as complete so that
1729 * it can finish asynchronously or via the cluster_complete_transaction
1730 * below if the request is synchronous
1734 if (need_EOT
== TRUE
)
1735 cluster_EOT(cbp_head
, cbp_tail
, size
== 0 ? zero_offset
: 0);
1737 if (flags
& CL_THROTTLE
)
1738 (void)vnode_waitforwrites(vp
, async_throttle
, 0, 0, "cluster_io");
1740 if ( !(io_flags
& B_READ
))
1741 vnode_startwrite(vp
);
1743 if (flags
& CL_RAW_ENCRYPTED
) {
1745 * User requested raw encrypted bytes.
1746 * Twiddle the bit in the ba_flags for the buffer
1748 cbp
->b_attr
.ba_flags
|= BA_RAW_ENCRYPTED_IO
;
1751 (void) VNOP_STRATEGY(cbp
);
1753 if (need_EOT
== TRUE
) {
1754 if ( !(flags
& CL_ASYNC
))
1755 cluster_complete_transaction(&cbp_head
, callback_arg
, &retval
, flags
, 1);
1769 * Wait until all of the outstanding I/O
1770 * for this partial transaction has completed
1772 cluster_wait_IO(cbp_head
, (flags
& CL_ASYNC
));
1775 * Rewind the upl offset to the beginning of the
1778 upl_offset
= cbp_head
->b_uploffset
;
1781 if (ISSET(flags
, CL_COMMIT
)) {
1782 cluster_handle_associated_upl(iostate
, upl
, upl_offset
,
1783 upl_end_offset
- upl_offset
);
1786 // Free all the IO buffers in this transaction
1787 for (cbp
= cbp_head
; cbp
;) {
1790 size
+= cbp
->b_bcount
;
1791 io_size
+= cbp
->b_bcount
;
1793 cbp_next
= cbp
->b_trans_next
;
1799 int need_wakeup
= 0;
1802 * update the error condition for this stream
1803 * since we never really issued the io
1804 * just go ahead and adjust it back
1806 lck_mtx_lock_spin(&iostate
->io_mtxp
);
1808 if (iostate
->io_error
== 0)
1809 iostate
->io_error
= error
;
1810 iostate
->io_issued
-= io_size
;
1812 if (iostate
->io_wanted
) {
1814 * someone is waiting for the state of
1815 * this io stream to change
1817 iostate
->io_wanted
= 0;
1820 lck_mtx_unlock(&iostate
->io_mtxp
);
1823 wakeup((caddr_t
)&iostate
->io_wanted
);
1826 if (flags
& CL_COMMIT
) {
1829 pg_offset
= upl_offset
& PAGE_MASK
;
1830 abort_size
= (upl_end_offset
- upl_offset
+ PAGE_MASK
) & ~PAGE_MASK
;
1832 upl_flags
= cluster_ioerror(upl
, upl_offset
- pg_offset
, abort_size
, error
, io_flags
, vp
);
1834 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 28)) | DBG_FUNC_NONE
,
1835 upl
, upl_offset
- pg_offset
, abort_size
, (error
<< 24) | upl_flags
, 0);
1839 } else if (cbp_head
)
1840 panic("%s(): cbp_head is not NULL.\n", __FUNCTION__
);
1844 * can get here if we either encountered an error
1845 * or we completely zero-filled the request and
1849 real_bp
->b_flags
|= B_ERROR
;
1850 real_bp
->b_error
= error
;
1852 buf_biodone(real_bp
);
1854 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_END
, (int)f_offset
, size
, upl_offset
, retval
, 0);
1859 #define reset_vector_run_state() \
1860 issueVectorUPL = vector_upl_offset = vector_upl_index = vector_upl_iosize = vector_upl_size = 0;
1863 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
,
1864 int io_flag
, buf_t real_bp
, struct clios
*iostate
, int (*callback
)(buf_t
, void *), void *callback_arg
)
1866 vector_upl_set_pagelist(vector_upl
);
1868 if(io_flag
& CL_READ
) {
1869 if(vector_upl_offset
== 0 && ((vector_upl_iosize
& PAGE_MASK
)==0))
1870 io_flag
&= ~CL_PRESERVE
; /*don't zero fill*/
1872 io_flag
|= CL_PRESERVE
; /*zero fill*/
1874 return (cluster_io(vp
, vector_upl
, vector_upl_offset
, v_upl_uio_offset
, vector_upl_iosize
, io_flag
, real_bp
, iostate
, callback
, callback_arg
));
1879 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
)
1881 int pages_in_prefetch
;
1883 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_START
,
1884 (int)f_offset
, size
, (int)filesize
, 0, 0);
1886 if (f_offset
>= filesize
) {
1887 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1888 (int)f_offset
, 0, 0, 0, 0);
1891 if ((off_t
)size
> (filesize
- f_offset
))
1892 size
= filesize
- f_offset
;
1893 pages_in_prefetch
= (size
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1895 advisory_read_ext(vp
, filesize
, f_offset
, size
, callback
, callback_arg
, bflag
);
1897 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1898 (int)f_offset
+ size
, pages_in_prefetch
, 0, 1, 0);
1900 return (pages_in_prefetch
);
1906 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
,
1911 int size_of_prefetch
;
1915 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_START
,
1916 (int)extent
->b_addr
, (int)extent
->e_addr
, (int)rap
->cl_lastr
, 0, 0);
1918 if (extent
->b_addr
== rap
->cl_lastr
&& extent
->b_addr
== extent
->e_addr
) {
1919 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1920 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 0, 0);
1923 if (rap
->cl_lastr
== -1 || (extent
->b_addr
!= rap
->cl_lastr
&& extent
->b_addr
!= (rap
->cl_lastr
+ 1))) {
1927 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1928 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 1, 0);
1932 max_prefetch
= MAX_PREFETCH(vp
, cluster_max_io_size(vp
->v_mount
, CL_READ
), (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
));
1934 if (max_prefetch
> speculative_prefetch_max
)
1935 max_prefetch
= speculative_prefetch_max
;
1937 if (max_prefetch
<= PAGE_SIZE
) {
1938 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1939 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 6, 0);
1942 if (extent
->e_addr
< rap
->cl_maxra
&& rap
->cl_ralen
>= 4) {
1943 if ((rap
->cl_maxra
- extent
->e_addr
) > (rap
->cl_ralen
/ 4)) {
1945 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1946 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 2, 0);
1950 r_addr
= max(extent
->e_addr
, rap
->cl_maxra
) + 1;
1951 f_offset
= (off_t
)(r_addr
* PAGE_SIZE_64
);
1953 size_of_prefetch
= 0;
1955 ubc_range_op(vp
, f_offset
, f_offset
+ PAGE_SIZE_64
, UPL_ROP_PRESENT
, &size_of_prefetch
);
1957 if (size_of_prefetch
) {
1958 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1959 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 3, 0);
1962 if (f_offset
< filesize
) {
1963 daddr64_t read_size
;
1965 rap
->cl_ralen
= rap
->cl_ralen
? min(max_prefetch
/ PAGE_SIZE
, rap
->cl_ralen
<< 1) : 1;
1967 read_size
= (extent
->e_addr
+ 1) - extent
->b_addr
;
1969 if (read_size
> rap
->cl_ralen
) {
1970 if (read_size
> max_prefetch
/ PAGE_SIZE
)
1971 rap
->cl_ralen
= max_prefetch
/ PAGE_SIZE
;
1973 rap
->cl_ralen
= read_size
;
1975 size_of_prefetch
= cluster_read_prefetch(vp
, f_offset
, rap
->cl_ralen
* PAGE_SIZE
, filesize
, callback
, callback_arg
, bflag
);
1977 if (size_of_prefetch
)
1978 rap
->cl_maxra
= (r_addr
+ size_of_prefetch
) - 1;
1980 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1981 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 4, 0);
1986 cluster_pageout(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
1987 int size
, off_t filesize
, int flags
)
1989 return cluster_pageout_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
1995 cluster_pageout_ext(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
1996 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2003 local_flags
= CL_PAGEOUT
| CL_THROTTLE
;
2005 if ((flags
& UPL_IOSYNC
) == 0)
2006 local_flags
|= CL_ASYNC
;
2007 if ((flags
& UPL_NOCOMMIT
) == 0)
2008 local_flags
|= CL_COMMIT
;
2009 if ((flags
& UPL_KEEPCACHED
))
2010 local_flags
|= CL_KEEPCACHED
;
2011 if (flags
& UPL_PAGING_ENCRYPTED
)
2012 local_flags
|= CL_ENCRYPTED
;
2015 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 52)) | DBG_FUNC_NONE
,
2016 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
2019 * If they didn't specify any I/O, then we are done...
2020 * we can't issue an abort because we don't know how
2021 * big the upl really is
2026 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
2027 if (local_flags
& CL_COMMIT
)
2028 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
2032 * can't page-in from a negative offset
2033 * or if we're starting beyond the EOF
2034 * or if the file offset isn't page aligned
2035 * or the size requested isn't a multiple of PAGE_SIZE
2037 if (f_offset
< 0 || f_offset
>= filesize
||
2038 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
)) {
2039 if (local_flags
& CL_COMMIT
)
2040 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
2043 max_size
= filesize
- f_offset
;
2045 if (size
< max_size
)
2050 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2052 if (size
> rounded_size
) {
2053 if (local_flags
& CL_COMMIT
)
2054 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
, size
- rounded_size
,
2055 UPL_ABORT_FREE_ON_EMPTY
);
2057 return (cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
2058 local_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
));
2063 cluster_pagein(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
2064 int size
, off_t filesize
, int flags
)
2066 return cluster_pagein_ext(vp
, upl
, upl_offset
, f_offset
, size
, filesize
, flags
, NULL
, NULL
);
2071 cluster_pagein_ext(vnode_t vp
, upl_t upl
, upl_offset_t upl_offset
, off_t f_offset
,
2072 int size
, off_t filesize
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2078 int local_flags
= 0;
2080 if (upl
== NULL
|| size
< 0)
2081 panic("cluster_pagein: NULL upl passed in");
2083 if ((flags
& UPL_IOSYNC
) == 0)
2084 local_flags
|= CL_ASYNC
;
2085 if ((flags
& UPL_NOCOMMIT
) == 0)
2086 local_flags
|= CL_COMMIT
;
2087 if (flags
& UPL_IOSTREAMING
)
2088 local_flags
|= CL_IOSTREAMING
;
2089 if (flags
& UPL_PAGING_ENCRYPTED
)
2090 local_flags
|= CL_ENCRYPTED
;
2093 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 56)) | DBG_FUNC_NONE
,
2094 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
2097 * can't page-in from a negative offset
2098 * or if we're starting beyond the EOF
2099 * or if the file offset isn't page aligned
2100 * or the size requested isn't a multiple of PAGE_SIZE
2102 if (f_offset
< 0 || f_offset
>= filesize
||
2103 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
) || (upl_offset
& PAGE_MASK
)) {
2104 if (local_flags
& CL_COMMIT
)
2105 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
2108 max_size
= filesize
- f_offset
;
2110 if (size
< max_size
)
2115 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2117 if (size
> rounded_size
&& (local_flags
& CL_COMMIT
))
2118 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
,
2119 size
- rounded_size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
2121 retval
= cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
2122 local_flags
| CL_READ
| CL_PAGEIN
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
2129 cluster_bp(buf_t bp
)
2131 return cluster_bp_ext(bp
, NULL
, NULL
);
2136 cluster_bp_ext(buf_t bp
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2141 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 19)) | DBG_FUNC_START
,
2142 bp
, (int)bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
2144 if (bp
->b_flags
& B_READ
)
2145 flags
= CL_ASYNC
| CL_READ
;
2148 if (bp
->b_flags
& B_PASSIVE
)
2149 flags
|= CL_PASSIVE
;
2151 f_offset
= ubc_blktooff(bp
->b_vp
, bp
->b_lblkno
);
2153 return (cluster_io(bp
->b_vp
, bp
->b_upl
, 0, f_offset
, bp
->b_bcount
, flags
, bp
, (struct clios
*)NULL
, callback
, callback_arg
));
2159 cluster_write(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
, int xflags
)
2161 return cluster_write_ext(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, xflags
, NULL
, NULL
);
2166 cluster_write_ext(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
,
2167 int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2169 user_ssize_t cur_resid
;
2174 int write_type
= IO_COPY
;
2175 u_int32_t write_length
;
2179 if (flags
& IO_PASSIVE
)
2184 if (vp
->v_flag
& VNOCACHE_DATA
){
2185 flags
|= IO_NOCACHE
;
2186 bflag
|= CL_NOCACHE
;
2191 * this call is being made to zero-fill some range in the file
2193 retval
= cluster_write_copy(vp
, NULL
, (u_int32_t
)0, oldEOF
, newEOF
, headOff
, tailOff
, flags
, callback
, callback_arg
);
2198 * do a write through the cache if one of the following is true....
2199 * NOCACHE is not true or NODIRECT is true
2200 * the uio request doesn't target USERSPACE
2201 * otherwise, find out if we want the direct or contig variant for
2202 * the first vector in the uio request
2204 if ( ((flags
& (IO_NOCACHE
| IO_NODIRECT
)) == IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) )
2205 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
2207 if ( (flags
& (IO_TAILZEROFILL
| IO_HEADZEROFILL
)) && write_type
== IO_DIRECT
)
2209 * must go through the cached variant in this case
2211 write_type
= IO_COPY
;
2213 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< newEOF
&& retval
== 0) {
2215 switch (write_type
) {
2219 * make sure the uio_resid isn't too big...
2220 * internally, we want to handle all of the I/O in
2221 * chunk sizes that fit in a 32 bit int
2223 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
)) {
2225 * we're going to have to call cluster_write_copy
2228 * only want the last call to cluster_write_copy to
2229 * have the IO_TAILZEROFILL flag set and only the
2230 * first call should have IO_HEADZEROFILL
2232 zflags
= flags
& ~IO_TAILZEROFILL
;
2233 flags
&= ~IO_HEADZEROFILL
;
2235 write_length
= MAX_IO_REQUEST_SIZE
;
2238 * last call to cluster_write_copy
2242 write_length
= (u_int32_t
)cur_resid
;
2244 retval
= cluster_write_copy(vp
, uio
, write_length
, oldEOF
, newEOF
, headOff
, tailOff
, zflags
, callback
, callback_arg
);
2248 zflags
= flags
& ~(IO_TAILZEROFILL
| IO_HEADZEROFILL
);
2250 if (flags
& IO_HEADZEROFILL
) {
2252 * only do this once per request
2254 flags
&= ~IO_HEADZEROFILL
;
2256 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, uio
->uio_offset
,
2257 headOff
, (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
2261 retval
= cluster_write_contig(vp
, uio
, newEOF
, &write_type
, &write_length
, callback
, callback_arg
, bflag
);
2263 if (retval
== 0 && (flags
& IO_TAILZEROFILL
) && uio_resid(uio
) == 0) {
2265 * we're done with the data from the user specified buffer(s)
2266 * and we've been requested to zero fill at the tail
2267 * treat this as an IO_HEADZEROFILL which doesn't require a uio
2268 * by rearranging the args and passing in IO_HEADZEROFILL
2270 retval
= cluster_write_copy(vp
, (struct uio
*)0, (u_int32_t
)0, (off_t
)0, tailOff
, uio
->uio_offset
,
2271 (off_t
)0, zflags
| IO_HEADZEROFILL
| IO_SYNC
, callback
, callback_arg
);
2277 * cluster_write_direct is never called with IO_TAILZEROFILL || IO_HEADZEROFILL
2279 retval
= cluster_write_direct(vp
, uio
, oldEOF
, newEOF
, &write_type
, &write_length
, flags
, callback
, callback_arg
);
2283 retval
= cluster_io_type(uio
, &write_type
, &write_length
, MIN_DIRECT_WRITE_SIZE
);
2287 * in case we end up calling cluster_write_copy (from cluster_write_direct)
2288 * multiple times to service a multi-vector request that is not aligned properly
2289 * we need to update the oldEOF so that we
2290 * don't zero-fill the head of a page if we've successfully written
2291 * data to that area... 'cluster_write_copy' will zero-fill the head of a
2292 * page that is beyond the oldEOF if the write is unaligned... we only
2293 * want that to happen for the very first page of the cluster_write,
2294 * NOT the first page of each vector making up a multi-vector write.
2296 if (uio
->uio_offset
> oldEOF
)
2297 oldEOF
= uio
->uio_offset
;
2304 cluster_write_direct(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
2305 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2308 upl_page_info_t
*pl
;
2309 vm_offset_t upl_offset
;
2310 vm_offset_t vector_upl_offset
= 0;
2311 u_int32_t io_req_size
;
2312 u_int32_t offset_in_file
;
2313 u_int32_t offset_in_iovbase
;
2316 upl_size_t upl_size
, vector_upl_size
= 0;
2317 vm_size_t upl_needed_size
;
2318 mach_msg_type_number_t pages_in_pl
;
2319 upl_control_flags_t upl_flags
;
2321 mach_msg_type_number_t i
;
2322 int force_data_sync
;
2325 struct clios iostate
;
2326 user_addr_t iov_base
;
2327 u_int32_t mem_alignment_mask
;
2328 u_int32_t devblocksize
;
2329 u_int32_t max_io_size
;
2330 u_int32_t max_upl_size
;
2331 u_int32_t max_vector_size
;
2332 boolean_t io_throttled
= FALSE
;
2334 u_int32_t vector_upl_iosize
= 0;
2335 int issueVectorUPL
= 0,useVectorUPL
= (uio
->uio_iovcnt
> 1);
2336 off_t v_upl_uio_offset
= 0;
2337 int vector_upl_index
=0;
2338 upl_t vector_upl
= NULL
;
2342 * When we enter this routine, we know
2343 * -- the resid will not exceed iov_len
2345 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_START
,
2346 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
2348 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
2350 io_flag
= CL_ASYNC
| CL_PRESERVE
| CL_COMMIT
| CL_THROTTLE
| CL_DIRECT_IO
;
2352 if (flags
& IO_PASSIVE
)
2353 io_flag
|= CL_PASSIVE
;
2355 if (flags
& IO_NOCACHE
)
2356 io_flag
|= CL_NOCACHE
;
2358 if (flags
& IO_SKIP_ENCRYPTION
)
2359 io_flag
|= CL_ENCRYPTED
;
2361 iostate
.io_completed
= 0;
2362 iostate
.io_issued
= 0;
2363 iostate
.io_error
= 0;
2364 iostate
.io_wanted
= 0;
2366 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
2368 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
2369 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
2371 if (devblocksize
== 1) {
2373 * the AFP client advertises a devblocksize of 1
2374 * however, its BLOCKMAP routine maps to physical
2375 * blocks that are PAGE_SIZE in size...
2376 * therefore we can't ask for I/Os that aren't page aligned
2377 * or aren't multiples of PAGE_SIZE in size
2378 * by setting devblocksize to PAGE_SIZE, we re-instate
2379 * the old behavior we had before the mem_alignment_mask
2380 * changes went in...
2382 devblocksize
= PAGE_SIZE
;
2386 io_req_size
= *write_length
;
2387 iov_base
= uio_curriovbase(uio
);
2389 offset_in_file
= (u_int32_t
)uio
->uio_offset
& PAGE_MASK
;
2390 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
2392 if (offset_in_file
|| offset_in_iovbase
) {
2394 * one of the 2 important offsets is misaligned
2395 * so fire an I/O through the cache for this entire vector
2397 goto wait_for_dwrites
;
2399 if (iov_base
& (devblocksize
- 1)) {
2401 * the offset in memory must be on a device block boundary
2402 * so that we can guarantee that we can generate an
2403 * I/O that ends on a page boundary in cluster_io
2405 goto wait_for_dwrites
;
2408 while (io_req_size
>= PAGE_SIZE
&& uio
->uio_offset
< newEOF
&& retval
== 0) {
2411 if ( (throttle_type
= cluster_is_throttled(vp
)) ) {
2413 * we're in the throttle window, at the very least
2414 * we want to limit the size of the I/O we're about
2417 if ( (flags
& IO_RETURN_ON_THROTTLE
) && throttle_type
== THROTTLE_NOW
) {
2419 * we're in the throttle window and at least 1 I/O
2420 * has already been issued by a throttleable thread
2421 * in this window, so return with EAGAIN to indicate
2422 * to the FS issuing the cluster_write call that it
2423 * should now throttle after dropping any locks
2425 throttle_info_update_by_mount(vp
->v_mount
);
2427 io_throttled
= TRUE
;
2428 goto wait_for_dwrites
;
2430 max_vector_size
= THROTTLE_MAX_IOSIZE
;
2431 max_io_size
= THROTTLE_MAX_IOSIZE
;
2433 max_vector_size
= MAX_VECTOR_UPL_SIZE
;
2434 max_io_size
= max_upl_size
;
2438 cluster_syncup(vp
, newEOF
, callback
, callback_arg
, callback
? PUSH_SYNC
: 0);
2441 io_size
= io_req_size
& ~PAGE_MASK
;
2442 iov_base
= uio_curriovbase(uio
);
2444 if (io_size
> max_io_size
)
2445 io_size
= max_io_size
;
2447 if(useVectorUPL
&& (iov_base
& PAGE_MASK
)) {
2449 * We have an iov_base that's not page-aligned.
2450 * Issue all I/O's that have been collected within
2451 * this Vectored UPL.
2453 if(vector_upl_index
) {
2454 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
);
2455 reset_vector_run_state();
2459 * After this point, if we are using the Vector UPL path and the base is
2460 * not page-aligned then the UPL with that base will be the first in the vector UPL.
2464 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
2465 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
2467 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_START
,
2468 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
2470 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
2471 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
2473 upl_size
= upl_needed_size
;
2474 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
2475 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
2476 | UPL_MEMORY_TAG_MAKE(VM_KERN_MEMORY_FILE
);
2478 kret
= vm_map_get_upl(map
,
2479 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2487 if (kret
!= KERN_SUCCESS
) {
2488 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2491 * failed to get pagelist
2493 * we may have already spun some portion of this request
2494 * off as async requests... we need to wait for the I/O
2495 * to complete before returning
2497 goto wait_for_dwrites
;
2499 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2500 pages_in_pl
= upl_size
/ PAGE_SIZE
;
2502 for (i
= 0; i
< pages_in_pl
; i
++) {
2503 if (!upl_valid_page(pl
, i
))
2506 if (i
== pages_in_pl
)
2510 * didn't get all the pages back that we
2511 * needed... release this upl and try again
2513 ubc_upl_abort(upl
, 0);
2515 if (force_data_sync
>= 3) {
2516 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2517 i
, pages_in_pl
, upl_size
, kret
, 0);
2519 * for some reason, we couldn't acquire a hold on all
2520 * the pages needed in the user's address space
2522 * we may have already spun some portion of this request
2523 * off as async requests... we need to wait for the I/O
2524 * to complete before returning
2526 goto wait_for_dwrites
;
2530 * Consider the possibility that upl_size wasn't satisfied.
2532 if (upl_size
< upl_needed_size
) {
2533 if (upl_size
&& upl_offset
== 0)
2538 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
2539 (int)upl_offset
, upl_size
, (int)iov_base
, io_size
, 0);
2542 ubc_upl_abort(upl
, 0);
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 vm_offset_t end_off
= ((iov_base
+ io_size
) & PAGE_MASK
);
2556 * After this point, if we are using a vector UPL, then
2557 * either all the UPL elements end on a page boundary OR
2558 * this UPL is the last element because it does not end
2559 * on a page boundary.
2564 * we want push out these writes asynchronously so that we can overlap
2565 * the preparation of the next I/O
2566 * if there are already too many outstanding writes
2567 * wait until some complete before issuing the next
2569 cluster_iostate_wait(&iostate
, max_upl_size
* IO_SCALE(vp
, 2), "cluster_write_direct");
2571 if (iostate
.io_error
) {
2573 * one of the earlier writes we issued ran into a hard error
2574 * don't issue any more writes, cleanup the UPL
2575 * that was just created but not used, then
2576 * go wait for all writes that are part of this stream
2577 * to complete before returning the error to the caller
2579 ubc_upl_abort(upl
, 0);
2581 goto wait_for_dwrites
;
2584 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_START
,
2585 (int)upl_offset
, (int)uio
->uio_offset
, io_size
, io_flag
, 0);
2588 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
2589 io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
2592 if(!vector_upl_index
) {
2593 vector_upl
= vector_upl_create(upl_offset
);
2594 v_upl_uio_offset
= uio
->uio_offset
;
2595 vector_upl_offset
= upl_offset
;
2598 vector_upl_set_subupl(vector_upl
,upl
,upl_size
);
2599 vector_upl_set_iostate(vector_upl
, upl
, vector_upl_size
, upl_size
);
2601 vector_upl_iosize
+= io_size
;
2602 vector_upl_size
+= upl_size
;
2604 if(issueVectorUPL
|| vector_upl_index
== MAX_VECTOR_UPL_ELEMENTS
|| vector_upl_size
>= max_vector_size
) {
2605 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
);
2606 reset_vector_run_state();
2611 * update the uio structure to
2612 * reflect the I/O that we just issued
2614 uio_update(uio
, (user_size_t
)io_size
);
2617 * in case we end up calling through to cluster_write_copy to finish
2618 * the tail of this request, we need to update the oldEOF so that we
2619 * don't zero-fill the head of a page if we've successfully written
2620 * data to that area... 'cluster_write_copy' will zero-fill the head of a
2621 * page that is beyond the oldEOF if the write is unaligned... we only
2622 * want that to happen for the very first page of the cluster_write,
2623 * NOT the first page of each vector making up a multi-vector write.
2625 if (uio
->uio_offset
> oldEOF
)
2626 oldEOF
= uio
->uio_offset
;
2628 io_req_size
-= io_size
;
2630 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_END
,
2631 (int)upl_offset
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
2635 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0) {
2637 retval
= cluster_io_type(uio
, write_type
, write_length
, MIN_DIRECT_WRITE_SIZE
);
2639 if (retval
== 0 && *write_type
== IO_DIRECT
) {
2641 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_NONE
,
2642 (int)uio
->uio_offset
, *write_length
, (int)newEOF
, 0, 0);
2650 if (retval
== 0 && iostate
.io_error
== 0 && useVectorUPL
&& vector_upl_index
) {
2651 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
);
2652 reset_vector_run_state();
2655 * make sure all async writes issued as part of this stream
2656 * have completed before we return
2658 cluster_iostate_wait(&iostate
, 0, "cluster_write_direct");
2660 if (iostate
.io_error
)
2661 retval
= iostate
.io_error
;
2663 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
2665 if (io_throttled
== TRUE
&& retval
== 0)
2668 if (io_req_size
&& retval
== 0) {
2670 * we couldn't handle the tail of this request in DIRECT mode
2671 * so fire it through the copy path
2673 * note that flags will never have IO_HEADZEROFILL or IO_TAILZEROFILL set
2674 * so we can just pass 0 in for the headOff and tailOff
2676 if (uio
->uio_offset
> oldEOF
)
2677 oldEOF
= uio
->uio_offset
;
2679 retval
= cluster_write_copy(vp
, uio
, io_req_size
, oldEOF
, newEOF
, (off_t
)0, (off_t
)0, flags
, callback
, callback_arg
);
2681 *write_type
= IO_UNKNOWN
;
2683 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
2684 (int)uio
->uio_offset
, io_req_size
, retval
, 4, 0);
2691 cluster_write_contig(vnode_t vp
, struct uio
*uio
, off_t newEOF
, int *write_type
, u_int32_t
*write_length
,
2692 int (*callback
)(buf_t
, void *), void *callback_arg
, int bflag
)
2694 upl_page_info_t
*pl
;
2695 addr64_t src_paddr
= 0;
2696 upl_t upl
[MAX_VECTS
];
2697 vm_offset_t upl_offset
;
2698 u_int32_t tail_size
= 0;
2701 upl_size_t upl_size
;
2702 vm_size_t upl_needed_size
;
2703 mach_msg_type_number_t pages_in_pl
;
2704 upl_control_flags_t upl_flags
;
2706 struct clios iostate
;
2711 user_addr_t iov_base
;
2712 u_int32_t devblocksize
;
2713 u_int32_t mem_alignment_mask
;
2716 * When we enter this routine, we know
2717 * -- the io_req_size will not exceed iov_len
2718 * -- the target address is physically contiguous
2720 cluster_syncup(vp
, newEOF
, callback
, callback_arg
, callback
? PUSH_SYNC
: 0);
2722 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
2723 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
2725 iostate
.io_completed
= 0;
2726 iostate
.io_issued
= 0;
2727 iostate
.io_error
= 0;
2728 iostate
.io_wanted
= 0;
2730 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
2733 io_size
= *write_length
;
2735 iov_base
= uio_curriovbase(uio
);
2737 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
2738 upl_needed_size
= upl_offset
+ io_size
;
2741 upl_size
= upl_needed_size
;
2742 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
2743 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
2744 | UPL_MEMORY_TAG_MAKE(VM_KERN_MEMORY_FILE
);
2746 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
2747 kret
= vm_map_get_upl(map
,
2748 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2749 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, 0);
2751 if (kret
!= KERN_SUCCESS
) {
2753 * failed to get pagelist
2756 goto wait_for_cwrites
;
2761 * Consider the possibility that upl_size wasn't satisfied.
2763 if (upl_size
< upl_needed_size
) {
2765 * This is a failure in the physical memory case.
2768 goto wait_for_cwrites
;
2770 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
2772 src_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + (addr64_t
)upl_offset
;
2774 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
2775 u_int32_t head_size
;
2777 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
2779 if (head_size
> io_size
)
2780 head_size
= io_size
;
2782 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, head_size
, 0, callback
, callback_arg
);
2785 goto wait_for_cwrites
;
2787 upl_offset
+= head_size
;
2788 src_paddr
+= head_size
;
2789 io_size
-= head_size
;
2791 iov_base
+= head_size
;
2793 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
2795 * request doesn't set up on a memory boundary
2796 * the underlying DMA engine can handle...
2797 * return an error instead of going through
2798 * the slow copy path since the intent of this
2799 * path is direct I/O from device memory
2802 goto wait_for_cwrites
;
2805 tail_size
= io_size
& (devblocksize
- 1);
2806 io_size
-= tail_size
;
2808 while (io_size
&& error
== 0) {
2810 if (io_size
> MAX_IO_CONTIG_SIZE
)
2811 xsize
= MAX_IO_CONTIG_SIZE
;
2815 * request asynchronously so that we can overlap
2816 * the preparation of the next I/O... we'll do
2817 * the commit after all the I/O has completed
2818 * since its all issued against the same UPL
2819 * if there are already too many outstanding writes
2820 * wait until some have completed before issuing the next
2822 cluster_iostate_wait(&iostate
, MAX_IO_CONTIG_SIZE
* IO_SCALE(vp
, 2), "cluster_write_contig");
2824 if (iostate
.io_error
) {
2826 * one of the earlier writes we issued ran into a hard error
2827 * don't issue any more writes...
2828 * go wait for all writes that are part of this stream
2829 * to complete before returning the error to the caller
2831 goto wait_for_cwrites
;
2834 * issue an asynchronous write to cluster_io
2836 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
,
2837 xsize
, CL_DEV_MEMORY
| CL_ASYNC
| bflag
, (buf_t
)NULL
, (struct clios
*)&iostate
, callback
, callback_arg
);
2841 * The cluster_io write completed successfully,
2842 * update the uio structure
2844 uio_update(uio
, (user_size_t
)xsize
);
2846 upl_offset
+= xsize
;
2851 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
) {
2853 error
= cluster_io_type(uio
, write_type
, write_length
, 0);
2855 if (error
== 0 && *write_type
== IO_CONTIG
) {
2860 *write_type
= IO_UNKNOWN
;
2864 * make sure all async writes that are part of this stream
2865 * have completed before we proceed
2867 cluster_iostate_wait(&iostate
, 0, "cluster_write_contig");
2869 if (iostate
.io_error
)
2870 error
= iostate
.io_error
;
2872 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
2874 if (error
== 0 && tail_size
)
2875 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, tail_size
, 0, callback
, callback_arg
);
2877 for (n
= 0; n
< num_upl
; n
++)
2879 * just release our hold on each physically contiguous
2880 * region without changing any state
2882 ubc_upl_abort(upl
[n
], 0);
2889 * need to avoid a race between an msync of a range of pages dirtied via mmap
2890 * vs a filesystem such as HFS deciding to write a 'hole' to disk via cluster_write's
2891 * zerofill mechanism before it has seen the VNOP_PAGEOUTs for the pages being msync'd
2893 * we should never force-zero-fill pages that are already valid in the cache...
2894 * the entire page contains valid data (either from disk, zero-filled or dirtied
2895 * via an mmap) so we can only do damage by trying to zero-fill
2899 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
)
2902 boolean_t need_cluster_zero
= TRUE
;
2904 if ((flags
& (IO_NOZEROVALID
| IO_NOZERODIRTY
))) {
2906 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off
& PAGE_MASK_64
));
2907 zero_pg_index
= (int)((zero_off
- upl_f_offset
) / PAGE_SIZE_64
);
2909 if (upl_valid_page(pl
, zero_pg_index
)) {
2911 * never force zero valid pages - dirty or clean
2912 * we'll leave these in the UPL for cluster_write_copy to deal with
2914 need_cluster_zero
= FALSE
;
2917 if (need_cluster_zero
== TRUE
)
2918 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2920 return (bytes_to_zero
);
2925 cluster_write_copy(vnode_t vp
, struct uio
*uio
, u_int32_t io_req_size
, off_t oldEOF
, off_t newEOF
, off_t headOff
,
2926 off_t tailOff
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
2928 upl_page_info_t
*pl
;
2930 vm_offset_t upl_offset
= 0;
2943 long long total_size
;
2946 long long zero_cnt1
;
2948 off_t write_off
= 0;
2950 boolean_t first_pass
= FALSE
;
2951 struct cl_extent cl
;
2952 struct cl_writebehind
*wbp
;
2954 u_int max_cluster_pgcount
;
2958 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
2959 (int)uio
->uio_offset
, io_req_size
, (int)oldEOF
, (int)newEOF
, 0);
2961 io_resid
= io_req_size
;
2963 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
2964 0, 0, (int)oldEOF
, (int)newEOF
, 0);
2968 if (flags
& IO_PASSIVE
)
2972 if (flags
& IO_NOCACHE
)
2973 bflag
|= CL_NOCACHE
;
2975 if (flags
& IO_SKIP_ENCRYPTION
)
2976 bflag
|= CL_ENCRYPTED
;
2983 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
2984 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_WRITE
);
2986 if (flags
& IO_HEADZEROFILL
) {
2988 * some filesystems (HFS is one) don't support unallocated holes within a file...
2989 * so we zero fill the intervening space between the old EOF and the offset
2990 * where the next chunk of real data begins.... ftruncate will also use this
2991 * routine to zero fill to the new EOF when growing a file... in this case, the
2992 * uio structure will not be provided
2995 if (headOff
< uio
->uio_offset
) {
2996 zero_cnt
= uio
->uio_offset
- headOff
;
2999 } else if (headOff
< newEOF
) {
3000 zero_cnt
= newEOF
- headOff
;
3004 if (uio
&& uio
->uio_offset
> oldEOF
) {
3005 zero_off
= uio
->uio_offset
& ~PAGE_MASK_64
;
3007 if (zero_off
>= oldEOF
) {
3008 zero_cnt
= uio
->uio_offset
- zero_off
;
3010 flags
|= IO_HEADZEROFILL
;
3014 if (flags
& IO_TAILZEROFILL
) {
3016 zero_off1
= uio
->uio_offset
+ io_req_size
;
3018 if (zero_off1
< tailOff
)
3019 zero_cnt1
= tailOff
- zero_off1
;
3022 if (uio
&& newEOF
> oldEOF
) {
3023 zero_off1
= uio
->uio_offset
+ io_req_size
;
3025 if (zero_off1
== newEOF
&& (zero_off1
& PAGE_MASK_64
)) {
3026 zero_cnt1
= PAGE_SIZE_64
- (zero_off1
& PAGE_MASK_64
);
3028 flags
|= IO_TAILZEROFILL
;
3032 if (zero_cnt
== 0 && uio
== (struct uio
*) 0) {
3033 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
3034 retval
, 0, 0, 0, 0);
3038 write_off
= uio
->uio_offset
;
3039 write_cnt
= uio_resid(uio
);
3041 * delay updating the sequential write info
3042 * in the control block until we've obtained
3047 while ((total_size
= (io_resid
+ zero_cnt
+ zero_cnt1
)) && retval
== 0) {
3049 * for this iteration of the loop, figure out where our starting point is
3052 start_offset
= (int)(zero_off
& PAGE_MASK_64
);
3053 upl_f_offset
= zero_off
- start_offset
;
3054 } else if (io_resid
) {
3055 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3056 upl_f_offset
= uio
->uio_offset
- start_offset
;
3058 start_offset
= (int)(zero_off1
& PAGE_MASK_64
);
3059 upl_f_offset
= zero_off1
- start_offset
;
3061 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 46)) | DBG_FUNC_NONE
,
3062 (int)zero_off
, (int)zero_cnt
, (int)zero_off1
, (int)zero_cnt1
, 0);
3064 if (total_size
> max_io_size
)
3065 total_size
= max_io_size
;
3067 cl
.b_addr
= (daddr64_t
)(upl_f_offset
/ PAGE_SIZE_64
);
3069 if (uio
&& ((flags
& (IO_SYNC
| IO_HEADZEROFILL
| IO_TAILZEROFILL
)) == 0)) {
3071 * assumption... total_size <= io_resid
3072 * because IO_HEADZEROFILL and IO_TAILZEROFILL not set
3074 if ((start_offset
+ total_size
) > max_io_size
)
3075 total_size
= max_io_size
- start_offset
;
3076 xfer_resid
= total_size
;
3078 retval
= cluster_copy_ubc_data_internal(vp
, uio
, &xfer_resid
, 1, 1);
3083 io_resid
-= (total_size
- xfer_resid
);
3084 total_size
= xfer_resid
;
3085 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3086 upl_f_offset
= uio
->uio_offset
- start_offset
;
3088 if (total_size
== 0) {
3091 * the write did not finish on a page boundary
3092 * which will leave upl_f_offset pointing to the
3093 * beginning of the last page written instead of
3094 * the page beyond it... bump it in this case
3095 * so that the cluster code records the last page
3098 upl_f_offset
+= PAGE_SIZE_64
;
3106 * compute the size of the upl needed to encompass
3107 * the requested write... limit each call to cluster_io
3108 * to the maximum UPL size... cluster_io will clip if
3109 * this exceeds the maximum io_size for the device,
3110 * make sure to account for
3111 * a starting offset that's not page aligned
3113 upl_size
= (start_offset
+ total_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
3115 if (upl_size
> max_io_size
)
3116 upl_size
= max_io_size
;
3118 pages_in_upl
= upl_size
/ PAGE_SIZE
;
3119 io_size
= upl_size
- start_offset
;
3121 if ((long long)io_size
> total_size
)
3122 io_size
= total_size
;
3124 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, io_size
, total_size
, 0, 0);
3128 * Gather the pages from the buffer cache.
3129 * The UPL_WILL_MODIFY flag lets the UPL subsystem know
3130 * that we intend to modify these pages.
3132 kret
= ubc_create_upl(vp
,
3137 UPL_SET_LITE
| (( uio
!=NULL
&& (uio
->uio_flags
& UIO_FLAGS_IS_COMPRESSED_FILE
)) ? 0 : UPL_WILL_MODIFY
));
3138 if (kret
!= KERN_SUCCESS
)
3139 panic("cluster_write_copy: failed to get pagelist");
3141 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
,
3142 upl
, (int)upl_f_offset
, start_offset
, 0, 0);
3144 if (start_offset
&& upl_f_offset
< oldEOF
&& !upl_valid_page(pl
, 0)) {
3148 * we're starting in the middle of the first page of the upl
3149 * and the page isn't currently valid, so we're going to have
3150 * to read it in first... this is a synchronous operation
3152 read_size
= PAGE_SIZE
;
3154 if ((upl_f_offset
+ read_size
) > oldEOF
)
3155 read_size
= oldEOF
- upl_f_offset
;
3157 retval
= cluster_io(vp
, upl
, 0, upl_f_offset
, read_size
,
3158 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
3161 * we had an error during the read which causes us to abort
3162 * the current cluster_write request... before we do, we need
3163 * to release the rest of the pages in the upl without modifying
3164 * there state and mark the failed page in error
3166 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
3168 if (upl_size
> PAGE_SIZE
)
3169 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3171 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
3172 upl
, 0, 0, retval
, 0);
3176 if ((start_offset
== 0 || upl_size
> PAGE_SIZE
) && ((start_offset
+ io_size
) & PAGE_MASK
)) {
3178 * the last offset we're writing to in this upl does not end on a page
3179 * boundary... if it's not beyond the old EOF, then we'll also need to
3180 * pre-read this page in if it isn't already valid
3182 upl_offset
= upl_size
- PAGE_SIZE
;
3184 if ((upl_f_offset
+ start_offset
+ io_size
) < oldEOF
&&
3185 !upl_valid_page(pl
, upl_offset
/ PAGE_SIZE
)) {
3188 read_size
= PAGE_SIZE
;
3190 if ((off_t
)(upl_f_offset
+ upl_offset
+ read_size
) > oldEOF
)
3191 read_size
= oldEOF
- (upl_f_offset
+ upl_offset
);
3193 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, read_size
,
3194 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
3197 * we had an error during the read which causes us to abort
3198 * the current cluster_write request... before we do, we
3199 * need to release the rest of the pages in the upl without
3200 * modifying there state and mark the failed page in error
3202 ubc_upl_abort_range(upl
, upl_offset
, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
|UPL_ABORT_FREE_ON_EMPTY
);
3204 if (upl_size
> PAGE_SIZE
)
3205 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3207 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
3208 upl
, 0, 0, retval
, 0);
3213 xfer_resid
= io_size
;
3214 io_offset
= start_offset
;
3216 while (zero_cnt
&& xfer_resid
) {
3218 if (zero_cnt
< (long long)xfer_resid
)
3219 bytes_to_zero
= zero_cnt
;
3221 bytes_to_zero
= xfer_resid
;
3223 bytes_to_zero
= cluster_zero_range(upl
, pl
, flags
, io_offset
, zero_off
, upl_f_offset
, bytes_to_zero
);
3225 xfer_resid
-= bytes_to_zero
;
3226 zero_cnt
-= bytes_to_zero
;
3227 zero_off
+= bytes_to_zero
;
3228 io_offset
+= bytes_to_zero
;
3230 if (xfer_resid
&& io_resid
) {
3231 u_int32_t io_requested
;
3233 bytes_to_move
= min(io_resid
, xfer_resid
);
3234 io_requested
= bytes_to_move
;
3236 retval
= cluster_copy_upl_data(uio
, upl
, io_offset
, (int *)&io_requested
);
3239 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3241 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
3242 upl
, 0, 0, retval
, 0);
3244 io_resid
-= bytes_to_move
;
3245 xfer_resid
-= bytes_to_move
;
3246 io_offset
+= bytes_to_move
;
3249 while (xfer_resid
&& zero_cnt1
&& retval
== 0) {
3251 if (zero_cnt1
< (long long)xfer_resid
)
3252 bytes_to_zero
= zero_cnt1
;
3254 bytes_to_zero
= xfer_resid
;
3256 bytes_to_zero
= cluster_zero_range(upl
, pl
, flags
, io_offset
, zero_off1
, upl_f_offset
, bytes_to_zero
);
3258 xfer_resid
-= bytes_to_zero
;
3259 zero_cnt1
-= bytes_to_zero
;
3260 zero_off1
+= bytes_to_zero
;
3261 io_offset
+= bytes_to_zero
;
3265 int ret_cluster_try_push
;
3267 io_size
+= start_offset
;
3269 if ((upl_f_offset
+ io_size
) >= newEOF
&& (u_int
)io_size
< upl_size
) {
3271 * if we're extending the file with this write
3272 * we'll zero fill the rest of the page so that
3273 * if the file gets extended again in such a way as to leave a
3274 * hole starting at this EOF, we'll have zero's in the correct spot
3276 cluster_zero(upl
, io_size
, upl_size
- io_size
, NULL
);
3279 * release the upl now if we hold one since...
3280 * 1) pages in it may be present in the sparse cluster map
3281 * and may span 2 separate buckets there... if they do and
3282 * we happen to have to flush a bucket to make room and it intersects
3283 * this upl, a deadlock may result on page BUSY
3284 * 2) we're delaying the I/O... from this point forward we're just updating
3285 * the cluster state... no need to hold the pages, so commit them
3286 * 3) IO_SYNC is set...
3287 * because we had to ask for a UPL that provides currenty non-present pages, the
3288 * UPL has been automatically set to clear the dirty flags (both software and hardware)
3289 * upon committing it... this is not the behavior we want since it's possible for
3290 * pages currently present as part of a mapped file to be dirtied while the I/O is in flight.
3291 * we'll pick these pages back up later with the correct behavior specified.
3292 * 4) we don't want to hold pages busy in a UPL and then block on the cluster lock... if a flush
3293 * of this vnode is in progress, we will deadlock if the pages being flushed intersect the pages
3294 * we hold since the flushing context is holding the cluster lock.
3296 ubc_upl_commit_range(upl
, 0, upl_size
,
3297 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
3300 * calculate the last logical block number
3301 * that this delayed I/O encompassed
3303 cl
.e_addr
= (daddr64_t
)((upl_f_offset
+ (off_t
)upl_size
) / PAGE_SIZE_64
);
3305 if (flags
& IO_SYNC
) {
3307 * if the IO_SYNC flag is set than we need to
3308 * bypass any clusters and immediately issue
3314 * take the lock to protect our accesses
3315 * of the writebehind and sparse cluster state
3317 wbp
= cluster_get_wbp(vp
, CLW_ALLOCATE
| CLW_RETURNLOCKED
);
3319 if (wbp
->cl_scmap
) {
3321 if ( !(flags
& IO_NOCACHE
)) {
3323 * we've fallen into the sparse
3324 * cluster method of delaying dirty pages
3326 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, newEOF
, callback
, callback_arg
);
3328 lck_mtx_unlock(&wbp
->cl_lockw
);
3333 * must have done cached writes that fell into
3334 * the sparse cluster mechanism... we've switched
3335 * to uncached writes on the file, so go ahead
3336 * and push whatever's in the sparse map
3337 * and switch back to normal clustering
3341 sparse_cluster_push(&(wbp
->cl_scmap
), vp
, newEOF
, PUSH_ALL
, 0, callback
, callback_arg
);
3343 * no clusters of either type present at this point
3344 * so just go directly to start_new_cluster since
3345 * we know we need to delay this I/O since we've
3346 * already released the pages back into the cache
3347 * to avoid the deadlock with sparse_cluster_push
3349 goto start_new_cluster
;
3352 if (write_off
== wbp
->cl_last_write
)
3353 wbp
->cl_seq_written
+= write_cnt
;
3355 wbp
->cl_seq_written
= write_cnt
;
3357 wbp
->cl_last_write
= write_off
+ write_cnt
;
3361 if (wbp
->cl_number
== 0)
3363 * no clusters currently present
3365 goto start_new_cluster
;
3367 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
3369 * check each cluster that we currently hold
3370 * try to merge some or all of this write into
3371 * one or more of the existing clusters... if
3372 * any portion of the write remains, start a
3375 if (cl
.b_addr
>= wbp
->cl_clusters
[cl_index
].b_addr
) {
3377 * the current write starts at or after the current cluster
3379 if (cl
.e_addr
<= (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
3381 * we have a write that fits entirely
3382 * within the existing cluster limits
3384 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
)
3386 * update our idea of where the cluster ends
3388 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
3391 if (cl
.b_addr
< (wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
)) {
3393 * we have a write that starts in the middle of the current cluster
3394 * but extends beyond the cluster's limit... we know this because
3395 * of the previous checks
3396 * we'll extend the current cluster to the max
3397 * and update the b_addr for the current write to reflect that
3398 * the head of it was absorbed into this cluster...
3399 * note that we'll always have a leftover tail in this case since
3400 * full absorbtion would have occurred in the clause above
3402 wbp
->cl_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
+ max_cluster_pgcount
;
3404 cl
.b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
;
3407 * we come here for the case where the current write starts
3408 * beyond the limit of the existing cluster or we have a leftover
3409 * tail after a partial absorbtion
3411 * in either case, we'll check the remaining clusters before
3412 * starting a new one
3416 * the current write starts in front of the cluster we're currently considering
3418 if ((wbp
->cl_clusters
[cl_index
].e_addr
- cl
.b_addr
) <= max_cluster_pgcount
) {
3420 * we can just merge the new request into
3421 * this cluster and leave it in the cache
3422 * since the resulting cluster is still
3423 * less than the maximum allowable size
3425 wbp
->cl_clusters
[cl_index
].b_addr
= cl
.b_addr
;
3427 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
) {
3429 * the current write completely
3430 * envelops the existing cluster and since
3431 * each write is limited to at most max_cluster_pgcount pages
3432 * we can just use the start and last blocknos of the write
3433 * to generate the cluster limits
3435 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
3441 * if we were to combine this write with the current cluster
3442 * we would exceed the cluster size limit.... so,
3443 * let's see if there's any overlap of the new I/O with
3444 * the cluster we're currently considering... in fact, we'll
3445 * stretch the cluster out to it's full limit and see if we
3446 * get an intersection with the current write
3449 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
) {
3451 * the current write extends into the proposed cluster
3452 * clip the length of the current write after first combining it's
3453 * tail with the newly shaped cluster
3455 wbp
->cl_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
- max_cluster_pgcount
;
3457 cl
.e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
;
3460 * if we get here, there was no way to merge
3461 * any portion of this write with this cluster
3462 * or we could only merge part of it which
3463 * will leave a tail...
3464 * we'll check the remaining clusters before starting a new one
3468 if (cl_index
< wbp
->cl_number
)
3470 * we found an existing cluster(s) that we
3471 * could entirely merge this I/O into
3475 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) &&
3476 wbp
->cl_number
== MAX_CLUSTERS
&&
3477 wbp
->cl_seq_written
>= (MAX_CLUSTERS
* (max_cluster_pgcount
* PAGE_SIZE
))) {
3480 if (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
)
3481 n
= WRITE_BEHIND_SSD
;
3486 cluster_try_push(wbp
, vp
, newEOF
, 0, 0, callback
, callback_arg
);
3488 if (wbp
->cl_number
< MAX_CLUSTERS
) {
3490 * we didn't find an existing cluster to
3491 * merge into, but there's room to start
3494 goto start_new_cluster
;
3497 * no exisitng cluster to merge with and no
3498 * room to start a new one... we'll try
3499 * pushing one of the existing ones... if none of
3500 * them are able to be pushed, we'll switch
3501 * to the sparse cluster mechanism
3502 * cluster_try_push updates cl_number to the
3503 * number of remaining clusters... and
3504 * returns the number of currently unused clusters
3506 ret_cluster_try_push
= 0;
3509 * if writes are not deferred, call cluster push immediately
3511 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
)) {
3513 ret_cluster_try_push
= cluster_try_push(wbp
, vp
, newEOF
, (flags
& IO_NOCACHE
) ? 0 : PUSH_DELAY
, 0, callback
, callback_arg
);
3517 * execute following regardless of writes being deferred or not
3519 if (ret_cluster_try_push
== 0) {
3521 * no more room in the normal cluster mechanism
3522 * so let's switch to the more expansive but expensive
3523 * sparse mechanism....
3525 sparse_cluster_switch(wbp
, vp
, newEOF
, callback
, callback_arg
);
3526 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, newEOF
, callback
, callback_arg
);
3528 lck_mtx_unlock(&wbp
->cl_lockw
);
3533 wbp
->cl_clusters
[wbp
->cl_number
].b_addr
= cl
.b_addr
;
3534 wbp
->cl_clusters
[wbp
->cl_number
].e_addr
= cl
.e_addr
;
3536 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
= 0;
3538 if (flags
& IO_NOCACHE
)
3539 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IONOCACHE
;
3541 if (bflag
& CL_PASSIVE
)
3542 wbp
->cl_clusters
[wbp
->cl_number
].io_flags
|= CLW_IOPASSIVE
;
3546 lck_mtx_unlock(&wbp
->cl_lockw
);
3551 * we don't hold the lock at this point
3553 * we've already dropped the current upl, so pick it back up with COPYOUT_FROM set
3554 * so that we correctly deal with a change in state of the hardware modify bit...
3555 * we do this via cluster_push_now... by passing along the IO_SYNC flag, we force
3556 * cluster_push_now to wait until all the I/Os have completed... cluster_push_now is also
3557 * responsible for generating the correct sized I/O(s)
3559 retval
= cluster_push_now(vp
, &cl
, newEOF
, flags
, callback
, callback_arg
);
3562 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
, retval
, 0, io_resid
, 0, 0);
3570 cluster_read(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
)
3572 return cluster_read_ext(vp
, uio
, filesize
, xflags
, NULL
, NULL
);
3577 cluster_read_ext(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
3581 user_ssize_t cur_resid
;
3583 u_int32_t read_length
= 0;
3584 int read_type
= IO_COPY
;
3588 if (vp
->v_flag
& VNOCACHE_DATA
)
3589 flags
|= IO_NOCACHE
;
3590 if ((vp
->v_flag
& VRAOFF
) || speculative_reads_disabled
)
3593 if (flags
& IO_SKIP_ENCRYPTION
)
3594 flags
|= IO_ENCRYPTED
;
3596 * If we're doing an encrypted IO, then first check to see
3597 * if the IO requested was page aligned. If not, then bail
3600 if (flags
& IO_ENCRYPTED
) {
3601 if (read_length
& PAGE_MASK
) {
3608 * do a read through the cache if one of the following is true....
3609 * NOCACHE is not true
3610 * the uio request doesn't target USERSPACE
3611 * Alternatively, if IO_ENCRYPTED is set, then we want to bypass the cache as well.
3612 * Reading encrypted data from a CP filesystem should never result in the data touching
3615 * otherwise, find out if we want the direct or contig variant for
3616 * the first vector in the uio request
3618 if ( ((flags
& IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
)) || (flags
& IO_ENCRYPTED
) ) {
3620 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3623 while ((cur_resid
= uio_resid(uio
)) && uio
->uio_offset
< filesize
&& retval
== 0) {
3625 switch (read_type
) {
3629 * make sure the uio_resid isn't too big...
3630 * internally, we want to handle all of the I/O in
3631 * chunk sizes that fit in a 32 bit int
3633 if (cur_resid
> (user_ssize_t
)(MAX_IO_REQUEST_SIZE
))
3634 io_size
= MAX_IO_REQUEST_SIZE
;
3636 io_size
= (u_int32_t
)cur_resid
;
3638 retval
= cluster_read_copy(vp
, uio
, io_size
, filesize
, flags
, callback
, callback_arg
);
3642 retval
= cluster_read_direct(vp
, uio
, filesize
, &read_type
, &read_length
, flags
, callback
, callback_arg
);
3646 retval
= cluster_read_contig(vp
, uio
, filesize
, &read_type
, &read_length
, callback
, callback_arg
, flags
);
3650 retval
= cluster_io_type(uio
, &read_type
, &read_length
, 0);
3660 cluster_read_upl_release(upl_t upl
, int start_pg
, int last_pg
, int take_reference
)
3663 int abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
3665 if ((range
= last_pg
- start_pg
)) {
3667 abort_flags
|= UPL_ABORT_REFERENCE
;
3669 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, range
* PAGE_SIZE
, abort_flags
);
3675 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
)
3677 upl_page_info_t
*pl
;
3679 vm_offset_t upl_offset
;
3688 off_t last_ioread_offset
;
3689 off_t last_request_offset
;
3693 u_int32_t size_of_prefetch
;
3696 u_int32_t max_rd_size
;
3697 u_int32_t max_io_size
;
3698 u_int32_t max_prefetch
;
3699 u_int rd_ahead_enabled
= 1;
3700 u_int prefetch_enabled
= 1;
3701 struct cl_readahead
* rap
;
3702 struct clios iostate
;
3703 struct cl_extent extent
;
3705 int take_reference
= 1;
3706 int policy
= IOPOL_DEFAULT
;
3707 boolean_t iolock_inited
= FALSE
;
3709 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_START
,
3710 (int)uio
->uio_offset
, io_req_size
, (int)filesize
, flags
, 0);
3712 if (flags
& IO_ENCRYPTED
) {
3713 panic ("encrypted blocks will hit UBC!");
3716 policy
= throttle_get_io_policy(NULL
);
3718 if (policy
== THROTTLE_LEVEL_TIER3
|| policy
== THROTTLE_LEVEL_TIER2
|| (flags
& IO_NOCACHE
))
3721 if (flags
& IO_PASSIVE
)
3726 if (flags
& IO_NOCACHE
)
3727 bflag
|= CL_NOCACHE
;
3729 if (flags
& IO_SKIP_ENCRYPTION
)
3730 bflag
|= CL_ENCRYPTED
;
3732 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
3733 max_prefetch
= MAX_PREFETCH(vp
, max_io_size
, (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
));
3734 max_rd_size
= max_prefetch
;
3736 last_request_offset
= uio
->uio_offset
+ io_req_size
;
3738 if (last_request_offset
> filesize
)
3739 last_request_offset
= filesize
;
3741 if ((flags
& (IO_RAOFF
|IO_NOCACHE
)) || ((last_request_offset
& ~PAGE_MASK_64
) == (uio
->uio_offset
& ~PAGE_MASK_64
))) {
3742 rd_ahead_enabled
= 0;
3745 if (cluster_is_throttled(vp
)) {
3747 * we're in the throttle window, at the very least
3748 * we want to limit the size of the I/O we're about
3751 rd_ahead_enabled
= 0;
3752 prefetch_enabled
= 0;
3754 max_rd_size
= THROTTLE_MAX_IOSIZE
;
3756 if ((rap
= cluster_get_rap(vp
)) == NULL
)
3757 rd_ahead_enabled
= 0;
3759 extent
.b_addr
= uio
->uio_offset
/ PAGE_SIZE_64
;
3760 extent
.e_addr
= (last_request_offset
- 1) / PAGE_SIZE_64
;
3763 if (rap
!= NULL
&& rap
->cl_ralen
&& (rap
->cl_lastr
== extent
.b_addr
|| (rap
->cl_lastr
+ 1) == extent
.b_addr
)) {
3765 * determine if we already have a read-ahead in the pipe courtesy of the
3766 * last read systemcall that was issued...
3767 * if so, pick up it's extent to determine where we should start
3768 * with respect to any read-ahead that might be necessary to
3769 * garner all the data needed to complete this read systemcall
3771 last_ioread_offset
= (rap
->cl_maxra
* PAGE_SIZE_64
) + PAGE_SIZE_64
;
3773 if (last_ioread_offset
< uio
->uio_offset
)
3774 last_ioread_offset
= (off_t
)0;
3775 else if (last_ioread_offset
> last_request_offset
)
3776 last_ioread_offset
= last_request_offset
;
3778 last_ioread_offset
= (off_t
)0;
3780 while (io_req_size
&& uio
->uio_offset
< filesize
&& retval
== 0) {
3782 max_size
= filesize
- uio
->uio_offset
;
3784 if ((off_t
)(io_req_size
) < max_size
)
3785 io_size
= io_req_size
;
3789 if (!(flags
& IO_NOCACHE
)) {
3793 u_int32_t io_requested
;
3796 * if we keep finding the pages we need already in the cache, then
3797 * don't bother to call cluster_read_prefetch since it costs CPU cycles
3798 * to determine that we have all the pages we need... once we miss in
3799 * the cache and have issued an I/O, than we'll assume that we're likely
3800 * to continue to miss in the cache and it's to our advantage to try and prefetch
3802 if (last_request_offset
&& last_ioread_offset
&& (size_of_prefetch
= (last_request_offset
- last_ioread_offset
))) {
3803 if ((last_ioread_offset
- uio
->uio_offset
) <= max_rd_size
&& prefetch_enabled
) {
3805 * we've already issued I/O for this request and
3806 * there's still work to do and
3807 * our prefetch stream is running dry, so issue a
3808 * pre-fetch I/O... the I/O latency will overlap
3809 * with the copying of the data
3811 if (size_of_prefetch
> max_rd_size
)
3812 size_of_prefetch
= max_rd_size
;
3814 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
3816 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
3818 if (last_ioread_offset
> last_request_offset
)
3819 last_ioread_offset
= last_request_offset
;
3823 * limit the size of the copy we're about to do so that
3824 * we can notice that our I/O pipe is running dry and
3825 * get the next I/O issued before it does go dry
3827 if (last_ioread_offset
&& io_size
> (max_io_size
/ 4))
3828 io_resid
= (max_io_size
/ 4);
3832 io_requested
= io_resid
;
3834 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_resid
, 0, take_reference
);
3836 xsize
= io_requested
- io_resid
;
3839 io_req_size
-= xsize
;
3841 if (retval
|| io_resid
)
3843 * if we run into a real error or
3844 * a page that is not in the cache
3845 * we need to leave streaming mode
3849 if (rd_ahead_enabled
&& (io_size
== 0 || last_ioread_offset
== last_request_offset
)) {
3851 * we're already finished the I/O for this read request
3852 * let's see if we should do a read-ahead
3854 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
3861 if (extent
.e_addr
< rap
->cl_lastr
)
3863 rap
->cl_lastr
= extent
.e_addr
;
3868 * recompute max_size since cluster_copy_ubc_data_internal
3869 * may have advanced uio->uio_offset
3871 max_size
= filesize
- uio
->uio_offset
;
3874 iostate
.io_completed
= 0;
3875 iostate
.io_issued
= 0;
3876 iostate
.io_error
= 0;
3877 iostate
.io_wanted
= 0;
3879 if ( (flags
& IO_RETURN_ON_THROTTLE
) ) {
3880 if (cluster_is_throttled(vp
) == THROTTLE_NOW
) {
3881 if ( !cluster_io_present_in_BC(vp
, uio
->uio_offset
)) {
3883 * we're in the throttle window and at least 1 I/O
3884 * has already been issued by a throttleable thread
3885 * in this window, so return with EAGAIN to indicate
3886 * to the FS issuing the cluster_read call that it
3887 * should now throttle after dropping any locks
3889 throttle_info_update_by_mount(vp
->v_mount
);
3898 * compute the size of the upl needed to encompass
3899 * the requested read... limit each call to cluster_io
3900 * to the maximum UPL size... cluster_io will clip if
3901 * this exceeds the maximum io_size for the device,
3902 * make sure to account for
3903 * a starting offset that's not page aligned
3905 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
3906 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
3908 if (io_size
> max_rd_size
)
3909 io_size
= max_rd_size
;
3911 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
3913 if (flags
& IO_NOCACHE
) {
3914 if (upl_size
> max_io_size
)
3915 upl_size
= max_io_size
;
3917 if (upl_size
> max_io_size
/ 4) {
3918 upl_size
= max_io_size
/ 4;
3919 upl_size
&= ~PAGE_MASK
;
3922 upl_size
= PAGE_SIZE
;
3925 pages_in_upl
= upl_size
/ PAGE_SIZE
;
3927 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_START
,
3928 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3930 kret
= ubc_create_upl(vp
,
3935 UPL_FILE_IO
| UPL_SET_LITE
);
3936 if (kret
!= KERN_SUCCESS
)
3937 panic("cluster_read_copy: failed to get pagelist");
3939 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_END
,
3940 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3943 * scan from the beginning of the upl looking for the first
3944 * non-valid page.... this will become the first page in
3945 * the request we're going to make to 'cluster_io'... if all
3946 * of the pages are valid, we won't call through to 'cluster_io'
3948 for (start_pg
= 0; start_pg
< pages_in_upl
; start_pg
++) {
3949 if (!upl_valid_page(pl
, start_pg
))
3954 * scan from the starting invalid page looking for a valid
3955 * page before the end of the upl is reached, if we
3956 * find one, then it will be the last page of the request to
3959 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
3960 if (upl_valid_page(pl
, last_pg
))
3964 if (start_pg
< last_pg
) {
3966 * we found a range of 'invalid' pages that must be filled
3967 * if the last page in this range is the last page of the file
3968 * we may have to clip the size of it to keep from reading past
3969 * the end of the last physical block associated with the file
3971 if (iolock_inited
== FALSE
) {
3972 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
3974 iolock_inited
= TRUE
;
3976 upl_offset
= start_pg
* PAGE_SIZE
;
3977 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
3979 if ((off_t
)(upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
3980 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
3983 * issue an asynchronous read to cluster_io
3986 error
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
,
3987 io_size
, CL_READ
| CL_ASYNC
| bflag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
3990 if (extent
.e_addr
< rap
->cl_maxra
) {
3992 * we've just issued a read for a block that should have been
3993 * in the cache courtesy of the read-ahead engine... something
3994 * has gone wrong with the pipeline, so reset the read-ahead
3995 * logic which will cause us to restart from scratch
4003 * if the read completed successfully, or there was no I/O request
4004 * issued, than copy the data into user land via 'cluster_upl_copy_data'
4005 * we'll first add on any 'valid'
4006 * pages that were present in the upl when we acquired it.
4010 for (uio_last
= last_pg
; uio_last
< pages_in_upl
; uio_last
++) {
4011 if (!upl_valid_page(pl
, uio_last
))
4014 if (uio_last
< pages_in_upl
) {
4016 * there were some invalid pages beyond the valid pages
4017 * that we didn't issue an I/O for, just release them
4018 * unchanged now, so that any prefetch/readahed can
4021 ubc_upl_abort_range(upl
, uio_last
* PAGE_SIZE
,
4022 (pages_in_upl
- uio_last
) * PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
4026 * compute size to transfer this round, if io_req_size is
4027 * still non-zero after this attempt, we'll loop around and
4028 * set up for another I/O.
4030 val_size
= (uio_last
* PAGE_SIZE
) - start_offset
;
4032 if (val_size
> max_size
)
4033 val_size
= max_size
;
4035 if (val_size
> io_req_size
)
4036 val_size
= io_req_size
;
4038 if ((uio
->uio_offset
+ val_size
) > last_ioread_offset
)
4039 last_ioread_offset
= uio
->uio_offset
+ val_size
;
4041 if ((size_of_prefetch
= (last_request_offset
- last_ioread_offset
)) && prefetch_enabled
) {
4043 if ((last_ioread_offset
- (uio
->uio_offset
+ val_size
)) <= upl_size
) {
4045 * if there's still I/O left to do for this request, and...
4046 * we're not in hard throttle mode, and...
4047 * we're close to using up the previous prefetch, then issue a
4048 * new pre-fetch I/O... the I/O latency will overlap
4049 * with the copying of the data
4051 if (size_of_prefetch
> max_rd_size
)
4052 size_of_prefetch
= max_rd_size
;
4054 size_of_prefetch
= cluster_read_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
, callback
, callback_arg
, bflag
);
4056 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
4058 if (last_ioread_offset
> last_request_offset
)
4059 last_ioread_offset
= last_request_offset
;
4062 } else if ((uio
->uio_offset
+ val_size
) == last_request_offset
) {
4064 * this transfer will finish this request, so...
4065 * let's try to read ahead if we're in
4066 * a sequential access pattern and we haven't
4067 * explicitly disabled it
4069 if (rd_ahead_enabled
)
4070 cluster_read_ahead(vp
, &extent
, filesize
, rap
, callback
, callback_arg
, bflag
);
4073 if (extent
.e_addr
< rap
->cl_lastr
)
4075 rap
->cl_lastr
= extent
.e_addr
;
4078 if (iolock_inited
== TRUE
)
4079 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
4081 if (iostate
.io_error
)
4082 error
= iostate
.io_error
;
4084 u_int32_t io_requested
;
4086 io_requested
= val_size
;
4088 retval
= cluster_copy_upl_data(uio
, upl
, start_offset
, (int *)&io_requested
);
4090 io_req_size
-= (val_size
- io_requested
);
4093 if (iolock_inited
== TRUE
)
4094 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
4096 if (start_pg
< last_pg
) {
4098 * compute the range of pages that we actually issued an I/O for
4099 * and either commit them as valid if the I/O succeeded
4100 * or abort them if the I/O failed or we're not supposed to
4101 * keep them in the cache
4103 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
4105 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
, upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
4107 if (error
|| (flags
& IO_NOCACHE
))
4108 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
4109 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
4111 int commit_flags
= UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
;
4114 commit_flags
|= UPL_COMMIT_INACTIVATE
;
4116 commit_flags
|= UPL_COMMIT_SPECULATE
;
4118 ubc_upl_commit_range(upl
, start_pg
* PAGE_SIZE
, io_size
, commit_flags
);
4120 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
4122 if ((last_pg
- start_pg
) < pages_in_upl
) {
4124 * the set of pages that we issued an I/O for did not encompass
4125 * the entire upl... so just release these without modifying
4129 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
4132 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
4133 upl
, -1, pages_in_upl
- (last_pg
- start_pg
), 0, 0);
4136 * handle any valid pages at the beginning of
4137 * the upl... release these appropriately
4139 cluster_read_upl_release(upl
, 0, start_pg
, take_reference
);
4142 * handle any valid pages immediately after the
4143 * pages we issued I/O for... ... release these appropriately
4145 cluster_read_upl_release(upl
, last_pg
, uio_last
, take_reference
);
4147 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
, upl
, -1, -1, 0, 0);
4154 if (cluster_is_throttled(vp
)) {
4156 * we're in the throttle window, at the very least
4157 * we want to limit the size of the I/O we're about
4160 rd_ahead_enabled
= 0;
4161 prefetch_enabled
= 0;
4162 max_rd_size
= THROTTLE_MAX_IOSIZE
;
4164 if (max_rd_size
== THROTTLE_MAX_IOSIZE
) {
4166 * coming out of throttled state
4168 if (policy
!= THROTTLE_LEVEL_TIER3
&& policy
!= THROTTLE_LEVEL_TIER2
) {
4170 rd_ahead_enabled
= 1;
4171 prefetch_enabled
= 1;
4173 max_rd_size
= max_prefetch
;
4174 last_ioread_offset
= 0;
4179 if (iolock_inited
== TRUE
) {
4181 * cluster_io returned an error after it
4182 * had already issued some I/O. we need
4183 * to wait for that I/O to complete before
4184 * we can destroy the iostate mutex...
4185 * 'retval' already contains the early error
4186 * so no need to pick it up from iostate.io_error
4188 cluster_iostate_wait(&iostate
, 0, "cluster_read_copy");
4190 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
4193 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
4194 (int)uio
->uio_offset
, io_req_size
, rap
->cl_lastr
, retval
, 0);
4196 lck_mtx_unlock(&rap
->cl_lockr
);
4198 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
4199 (int)uio
->uio_offset
, io_req_size
, 0, retval
, 0);
4206 * We don't want another read/write lock for every vnode in the system
4207 * so we keep a hash of them here. There should never be very many of
4208 * these around at any point in time.
4210 cl_direct_read_lock_t
*cluster_lock_direct_read(vnode_t vp
, lck_rw_type_t type
)
4212 struct cl_direct_read_locks
*head
4213 = &cl_direct_read_locks
[(uintptr_t)vp
/ sizeof(*vp
)
4214 % CL_DIRECT_READ_LOCK_BUCKETS
];
4216 struct cl_direct_read_lock
*lck
, *new_lck
= NULL
;
4219 lck_spin_lock(&cl_direct_read_spin_lock
);
4221 LIST_FOREACH(lck
, head
, chain
) {
4222 if (lck
->vp
== vp
) {
4224 lck_spin_unlock(&cl_direct_read_spin_lock
);
4226 // Someone beat us to it, ditch the allocation
4227 lck_rw_destroy(&new_lck
->rw_lock
, cl_mtx_grp
);
4228 FREE(new_lck
, M_TEMP
);
4230 lck_rw_lock(&lck
->rw_lock
, type
);
4236 // Use the lock we allocated
4237 LIST_INSERT_HEAD(head
, new_lck
, chain
);
4238 lck_spin_unlock(&cl_direct_read_spin_lock
);
4239 lck_rw_lock(&new_lck
->rw_lock
, type
);
4243 lck_spin_unlock(&cl_direct_read_spin_lock
);
4245 // Allocate a new lock
4246 MALLOC(new_lck
, cl_direct_read_lock_t
*, sizeof(*new_lck
),
4248 lck_rw_init(&new_lck
->rw_lock
, cl_mtx_grp
, cl_mtx_attr
);
4250 new_lck
->ref_count
= 1;
4252 // Got to go round again
4256 void cluster_unlock_direct_read(cl_direct_read_lock_t
*lck
)
4258 lck_rw_done(&lck
->rw_lock
);
4260 lck_spin_lock(&cl_direct_read_spin_lock
);
4261 if (lck
->ref_count
== 1) {
4262 LIST_REMOVE(lck
, chain
);
4263 lck_spin_unlock(&cl_direct_read_spin_lock
);
4264 lck_rw_destroy(&lck
->rw_lock
, cl_mtx_grp
);
4268 lck_spin_unlock(&cl_direct_read_spin_lock
);
4273 cluster_read_direct(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
4274 int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
4277 upl_page_info_t
*pl
;
4279 vm_offset_t upl_offset
, vector_upl_offset
= 0;
4280 upl_size_t upl_size
, vector_upl_size
= 0;
4281 vm_size_t upl_needed_size
;
4282 unsigned int pages_in_pl
;
4283 upl_control_flags_t upl_flags
;
4286 int force_data_sync
;
4288 int no_zero_fill
= 0;
4291 struct clios iostate
;
4292 user_addr_t iov_base
;
4293 u_int32_t io_req_size
;
4294 u_int32_t offset_in_file
;
4295 u_int32_t offset_in_iovbase
;
4299 u_int32_t devblocksize
;
4300 u_int32_t mem_alignment_mask
;
4301 u_int32_t max_upl_size
;
4302 u_int32_t max_rd_size
;
4303 u_int32_t max_rd_ahead
;
4304 u_int32_t max_vector_size
;
4305 boolean_t strict_uncached_IO
= FALSE
;
4306 boolean_t io_throttled
= FALSE
;
4308 u_int32_t vector_upl_iosize
= 0;
4309 int issueVectorUPL
= 0,useVectorUPL
= (uio
->uio_iovcnt
> 1);
4310 off_t v_upl_uio_offset
= 0;
4311 int vector_upl_index
=0;
4312 upl_t vector_upl
= NULL
;
4313 cl_direct_read_lock_t
*lock
= NULL
;
4315 user_addr_t orig_iov_base
= 0;
4316 user_addr_t last_iov_base
= 0;
4317 user_addr_t next_iov_base
= 0;
4319 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_START
,
4320 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
4322 max_upl_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
4324 max_rd_size
= max_upl_size
;
4325 max_rd_ahead
= max_rd_size
* IO_SCALE(vp
, 2);
4327 io_flag
= CL_COMMIT
| CL_READ
| CL_ASYNC
| CL_NOZERO
| CL_DIRECT_IO
;
4329 if (flags
& IO_PASSIVE
)
4330 io_flag
|= CL_PASSIVE
;
4332 if (flags
& IO_ENCRYPTED
) {
4333 io_flag
|= CL_RAW_ENCRYPTED
;
4336 if (flags
& IO_NOCACHE
) {
4337 io_flag
|= CL_NOCACHE
;
4340 if (flags
& IO_SKIP_ENCRYPTION
)
4341 io_flag
|= CL_ENCRYPTED
;
4343 iostate
.io_completed
= 0;
4344 iostate
.io_issued
= 0;
4345 iostate
.io_error
= 0;
4346 iostate
.io_wanted
= 0;
4348 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
4350 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
4351 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
4353 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
4354 (int)devblocksize
, (int)mem_alignment_mask
, 0, 0, 0);
4356 if (devblocksize
== 1) {
4358 * the AFP client advertises a devblocksize of 1
4359 * however, its BLOCKMAP routine maps to physical
4360 * blocks that are PAGE_SIZE in size...
4361 * therefore we can't ask for I/Os that aren't page aligned
4362 * or aren't multiples of PAGE_SIZE in size
4363 * by setting devblocksize to PAGE_SIZE, we re-instate
4364 * the old behavior we had before the mem_alignment_mask
4365 * changes went in...
4367 devblocksize
= PAGE_SIZE
;
4370 strict_uncached_IO
= ubc_strict_uncached_IO(vp
);
4372 orig_iov_base
= uio_curriovbase(uio
);
4373 last_iov_base
= orig_iov_base
;
4376 io_req_size
= *read_length
;
4377 iov_base
= uio_curriovbase(uio
);
4379 max_io_size
= filesize
- uio
->uio_offset
;
4381 if ((off_t
)io_req_size
> max_io_size
)
4382 io_req_size
= max_io_size
;
4384 offset_in_file
= (u_int32_t
)uio
->uio_offset
& (devblocksize
- 1);
4385 offset_in_iovbase
= (u_int32_t
)iov_base
& mem_alignment_mask
;
4387 if (offset_in_file
|| offset_in_iovbase
) {
4389 * one of the 2 important offsets is misaligned
4390 * so fire an I/O through the cache for this entire vector
4394 if (iov_base
& (devblocksize
- 1)) {
4396 * the offset in memory must be on a device block boundary
4397 * so that we can guarantee that we can generate an
4398 * I/O that ends on a page boundary in cluster_io
4404 * The user must request IO in aligned chunks. If the
4405 * offset into the file is bad, or the userland pointer
4406 * is non-aligned, then we cannot service the encrypted IO request.
4408 if ((flags
& IO_ENCRYPTED
) && (misaligned
)) {
4413 * When we get to this point, we know...
4414 * -- the offset into the file is on a devblocksize boundary
4417 while (io_req_size
&& retval
== 0) {
4420 if (cluster_is_throttled(vp
)) {
4422 * we're in the throttle window, at the very least
4423 * we want to limit the size of the I/O we're about
4426 max_rd_size
= THROTTLE_MAX_IOSIZE
;
4427 max_rd_ahead
= THROTTLE_MAX_IOSIZE
- 1;
4428 max_vector_size
= THROTTLE_MAX_IOSIZE
;
4430 max_rd_size
= max_upl_size
;
4431 max_rd_ahead
= max_rd_size
* IO_SCALE(vp
, 2);
4432 max_vector_size
= MAX_VECTOR_UPL_SIZE
;
4434 io_start
= io_size
= io_req_size
;
4437 * First look for pages already in the cache
4438 * and move them to user space. But only do this
4439 * check if we are not retrieving encrypted data directly
4440 * from the filesystem; those blocks should never
4443 * cluster_copy_ubc_data returns the resid
4446 if ((strict_uncached_IO
== FALSE
) && ((flags
& IO_ENCRYPTED
) == 0)) {
4447 retval
= cluster_copy_ubc_data_internal(vp
, uio
, (int *)&io_size
, 0, 0);
4450 * calculate the number of bytes actually copied
4451 * starting size - residual
4453 xsize
= io_start
- io_size
;
4455 io_req_size
-= xsize
;
4457 if(useVectorUPL
&& (xsize
|| (iov_base
& PAGE_MASK
))) {
4459 * We found something in the cache or we have an iov_base that's not
4462 * Issue all I/O's that have been collected within this Vectored UPL.
4464 if(vector_upl_index
) {
4465 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
);
4466 reset_vector_run_state();
4473 * After this point, if we are using the Vector UPL path and the base is
4474 * not page-aligned then the UPL with that base will be the first in the vector UPL.
4479 * check to see if we are finished with this request.
4481 * If we satisfied this IO already, then io_req_size will be 0.
4482 * Otherwise, see if the IO was mis-aligned and needs to go through
4483 * the UBC to deal with the 'tail'.
4486 if (io_req_size
== 0 || (misaligned
)) {
4488 * see if there's another uio vector to
4489 * process that's of type IO_DIRECT
4491 * break out of while loop to get there
4496 * assume the request ends on a device block boundary
4498 io_min
= devblocksize
;
4501 * we can handle I/O's in multiples of the device block size
4502 * however, if io_size isn't a multiple of devblocksize we
4503 * want to clip it back to the nearest page boundary since
4504 * we are going to have to go through cluster_read_copy to
4505 * deal with the 'overhang'... by clipping it to a PAGE_SIZE
4506 * multiple, we avoid asking the drive for the same physical
4507 * blocks twice.. once for the partial page at the end of the
4508 * request and a 2nd time for the page we read into the cache
4509 * (which overlaps the end of the direct read) in order to
4510 * get at the overhang bytes
4512 if (io_size
& (devblocksize
- 1)) {
4513 if (flags
& IO_ENCRYPTED
) {
4515 * Normally, we'd round down to the previous page boundary to
4516 * let the UBC manage the zero-filling of the file past the EOF.
4517 * But if we're doing encrypted IO, we can't let any of
4518 * the data hit the UBC. This means we have to do the full
4519 * IO to the upper block boundary of the device block that
4520 * contains the EOF. The user will be responsible for not
4521 * interpreting data PAST the EOF in its buffer.
4523 * So just bump the IO back up to a multiple of devblocksize
4525 io_size
= ((io_size
+ devblocksize
) & ~(devblocksize
- 1));
4530 * Clip the request to the previous page size boundary
4531 * since request does NOT end on a device block boundary
4533 io_size
&= ~PAGE_MASK
;
4538 if (retval
|| io_size
< io_min
) {
4540 * either an error or we only have the tail left to
4541 * complete via the copy path...
4542 * we may have already spun some portion of this request
4543 * off as async requests... we need to wait for the I/O
4544 * to complete before returning
4546 goto wait_for_dreads
;
4550 * Don't re-check the UBC data if we are looking for uncached IO
4551 * or asking for encrypted blocks.
4553 if ((strict_uncached_IO
== FALSE
) && ((flags
& IO_ENCRYPTED
) == 0)) {
4555 if ((xsize
= io_size
) > max_rd_size
)
4556 xsize
= max_rd_size
;
4562 * We hold a lock here between the time we check the
4563 * cache and the time we issue I/O. This saves us
4564 * from having to lock the pages in the cache. Not
4565 * all clients will care about this lock but some
4566 * clients may want to guarantee stability between
4567 * here and when the I/O is issued in which case they
4568 * will take the lock exclusively.
4570 lock
= cluster_lock_direct_read(vp
, LCK_RW_TYPE_SHARED
);
4573 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ xsize
, UPL_ROP_ABSENT
, (int *)&io_size
);
4577 * a page must have just come into the cache
4578 * since the first page in this range is no
4579 * longer absent, go back and re-evaluate
4584 if ( (flags
& IO_RETURN_ON_THROTTLE
) ) {
4585 if (cluster_is_throttled(vp
) == THROTTLE_NOW
) {
4586 if ( !cluster_io_present_in_BC(vp
, uio
->uio_offset
)) {
4588 * we're in the throttle window and at least 1 I/O
4589 * has already been issued by a throttleable thread
4590 * in this window, so return with EAGAIN to indicate
4591 * to the FS issuing the cluster_read call that it
4592 * should now throttle after dropping any locks
4594 throttle_info_update_by_mount(vp
->v_mount
);
4596 io_throttled
= TRUE
;
4597 goto wait_for_dreads
;
4601 if (io_size
> max_rd_size
)
4602 io_size
= max_rd_size
;
4604 iov_base
= uio_curriovbase(uio
);
4606 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
4607 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
4609 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_START
,
4610 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
4612 if (upl_offset
== 0 && ((io_size
& PAGE_MASK
) == 0))
4617 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
4618 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
4620 upl_size
= upl_needed_size
;
4621 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
4622 | UPL_MEMORY_TAG_MAKE(VM_KERN_MEMORY_FILE
);
4624 upl_flags
|= UPL_NOZEROFILL
;
4625 if (force_data_sync
)
4626 upl_flags
|= UPL_FORCE_DATA_SYNC
;
4628 kret
= vm_map_create_upl(map
,
4629 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4630 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
);
4632 if (kret
!= KERN_SUCCESS
) {
4633 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4634 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4636 * failed to get pagelist
4638 * we may have already spun some portion of this request
4639 * off as async requests... we need to wait for the I/O
4640 * to complete before returning
4642 goto wait_for_dreads
;
4644 pages_in_pl
= upl_size
/ PAGE_SIZE
;
4645 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
4647 for (i
= 0; i
< pages_in_pl
; i
++) {
4648 if (!upl_page_present(pl
, i
))
4651 if (i
== pages_in_pl
)
4654 ubc_upl_abort(upl
, 0);
4656 if (force_data_sync
>= 3) {
4657 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4658 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4660 goto wait_for_dreads
;
4663 * Consider the possibility that upl_size wasn't satisfied.
4665 if (upl_size
< upl_needed_size
) {
4666 if (upl_size
&& upl_offset
== 0)
4672 ubc_upl_abort(upl
, 0);
4673 goto wait_for_dreads
;
4675 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
4676 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4679 vm_offset_t end_off
= ((iov_base
+ io_size
) & PAGE_MASK
);
4683 * After this point, if we are using a vector UPL, then
4684 * either all the UPL elements end on a page boundary OR
4685 * this UPL is the last element because it does not end
4686 * on a page boundary.
4691 * request asynchronously so that we can overlap
4692 * the preparation of the next I/O
4693 * if there are already too many outstanding reads
4694 * wait until some have completed before issuing the next read
4696 cluster_iostate_wait(&iostate
, max_rd_ahead
, "cluster_read_direct");
4698 if (iostate
.io_error
) {
4700 * one of the earlier reads we issued ran into a hard error
4701 * don't issue any more reads, cleanup the UPL
4702 * that was just created but not used, then
4703 * go wait for any other reads to complete before
4704 * returning the error to the caller
4706 ubc_upl_abort(upl
, 0);
4708 goto wait_for_dreads
;
4710 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_START
,
4711 upl
, (int)upl_offset
, (int)uio
->uio_offset
, io_size
, 0);
4715 io_flag
&= ~CL_PRESERVE
;
4717 io_flag
|= CL_PRESERVE
;
4719 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
, io_size
, io_flag
, (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
4723 if(!vector_upl_index
) {
4724 vector_upl
= vector_upl_create(upl_offset
);
4725 v_upl_uio_offset
= uio
->uio_offset
;
4726 vector_upl_offset
= upl_offset
;
4729 vector_upl_set_subupl(vector_upl
,upl
, upl_size
);
4730 vector_upl_set_iostate(vector_upl
, upl
, vector_upl_size
, upl_size
);
4732 vector_upl_size
+= upl_size
;
4733 vector_upl_iosize
+= io_size
;
4735 if(issueVectorUPL
|| vector_upl_index
== MAX_VECTOR_UPL_ELEMENTS
|| vector_upl_size
>= max_vector_size
) {
4736 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
);
4737 reset_vector_run_state();
4740 last_iov_base
= iov_base
+ io_size
;
4743 // We don't need to wait for the I/O to complete
4744 cluster_unlock_direct_read(lock
);
4749 * update the uio structure
4751 if ((flags
& IO_ENCRYPTED
) && (max_io_size
< io_size
)) {
4752 uio_update(uio
, (user_size_t
)max_io_size
);
4755 uio_update(uio
, (user_size_t
)io_size
);
4758 * Under normal circumstances, the io_size should not be
4759 * bigger than the io_req_size, but we may have had to round up
4760 * to the end of the page in the encrypted IO case. In that case only,
4761 * ensure that we only decrement io_req_size to 0.
4763 if ((flags
& IO_ENCRYPTED
) && (io_size
> io_req_size
)) {
4767 io_req_size
-= io_size
;
4770 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_END
,
4771 upl
, (int)uio
->uio_offset
, io_req_size
, retval
, 0);
4775 if (retval
== 0 && iostate
.io_error
== 0 && io_req_size
== 0 && uio
->uio_offset
< filesize
) {
4777 retval
= cluster_io_type(uio
, read_type
, read_length
, 0);
4779 if (retval
== 0 && *read_type
== IO_DIRECT
) {
4781 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_NONE
,
4782 (int)uio
->uio_offset
, (int)filesize
, *read_type
, *read_length
, 0);
4790 if(retval
== 0 && iostate
.io_error
== 0 && useVectorUPL
&& vector_upl_index
) {
4791 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
);
4792 reset_vector_run_state();
4795 // We don't need to wait for the I/O to complete
4797 cluster_unlock_direct_read(lock
);
4800 * make sure all async reads that are part of this stream
4801 * have completed before we return
4803 cluster_iostate_wait(&iostate
, 0, "cluster_read_direct");
4805 if (iostate
.io_error
)
4806 retval
= iostate
.io_error
;
4808 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
4810 if (io_throttled
== TRUE
&& retval
== 0)
4813 for (next_iov_base
= orig_iov_base
; next_iov_base
< last_iov_base
; next_iov_base
+= PAGE_SIZE
) {
4815 * This is specifically done for pmap accounting purposes.
4816 * vm_pre_fault() will call vm_fault() to enter the page into
4817 * the pmap if there isn't _a_ physical page for that VA already.
4819 vm_pre_fault(vm_map_trunc_page(next_iov_base
, PAGE_MASK
));
4822 if (io_req_size
&& retval
== 0) {
4824 * we couldn't handle the tail of this request in DIRECT mode
4825 * so fire it through the copy path
4827 retval
= cluster_read_copy(vp
, uio
, io_req_size
, filesize
, flags
, callback
, callback_arg
);
4829 *read_type
= IO_UNKNOWN
;
4831 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
4832 (int)uio
->uio_offset
, (int)uio_resid(uio
), io_req_size
, retval
, 0);
4839 cluster_read_contig(vnode_t vp
, struct uio
*uio
, off_t filesize
, int *read_type
, u_int32_t
*read_length
,
4840 int (*callback
)(buf_t
, void *), void *callback_arg
, int flags
)
4842 upl_page_info_t
*pl
;
4843 upl_t upl
[MAX_VECTS
];
4844 vm_offset_t upl_offset
;
4845 addr64_t dst_paddr
= 0;
4846 user_addr_t iov_base
;
4848 upl_size_t upl_size
;
4849 vm_size_t upl_needed_size
;
4850 mach_msg_type_number_t pages_in_pl
;
4851 upl_control_flags_t upl_flags
;
4853 struct clios iostate
;
4860 u_int32_t devblocksize
;
4861 u_int32_t mem_alignment_mask
;
4862 u_int32_t tail_size
= 0;
4865 if (flags
& IO_PASSIVE
)
4870 if (flags
& IO_NOCACHE
)
4871 bflag
|= CL_NOCACHE
;
4874 * When we enter this routine, we know
4875 * -- the read_length will not exceed the current iov_len
4876 * -- the target address is physically contiguous for read_length
4878 cluster_syncup(vp
, filesize
, callback
, callback_arg
, PUSH_SYNC
);
4880 devblocksize
= (u_int32_t
)vp
->v_mount
->mnt_devblocksize
;
4881 mem_alignment_mask
= (u_int32_t
)vp
->v_mount
->mnt_alignmentmask
;
4883 iostate
.io_completed
= 0;
4884 iostate
.io_issued
= 0;
4885 iostate
.io_error
= 0;
4886 iostate
.io_wanted
= 0;
4888 lck_mtx_init(&iostate
.io_mtxp
, cl_mtx_grp
, cl_mtx_attr
);
4891 io_size
= *read_length
;
4893 max_size
= filesize
- uio
->uio_offset
;
4895 if (io_size
> max_size
)
4898 iov_base
= uio_curriovbase(uio
);
4900 upl_offset
= (vm_offset_t
)((u_int32_t
)iov_base
& PAGE_MASK
);
4901 upl_needed_size
= upl_offset
+ io_size
;
4904 upl_size
= upl_needed_size
;
4905 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
4906 | UPL_MEMORY_TAG_MAKE(VM_KERN_MEMORY_FILE
);
4909 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_START
,
4910 (int)upl_offset
, (int)upl_size
, (int)iov_base
, io_size
, 0);
4912 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
4913 kret
= vm_map_get_upl(map
,
4914 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
4915 &upl_size
, &upl
[cur_upl
], NULL
, &pages_in_pl
, &upl_flags
, 0);
4917 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 92)) | DBG_FUNC_END
,
4918 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
4920 if (kret
!= KERN_SUCCESS
) {
4922 * failed to get pagelist
4925 goto wait_for_creads
;
4929 if (upl_size
< upl_needed_size
) {
4931 * The upl_size wasn't satisfied.
4934 goto wait_for_creads
;
4936 pl
= ubc_upl_pageinfo(upl
[cur_upl
]);
4938 dst_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + (addr64_t
)upl_offset
;
4940 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
4941 u_int32_t head_size
;
4943 head_size
= devblocksize
- (u_int32_t
)(uio
->uio_offset
& (devblocksize
- 1));
4945 if (head_size
> io_size
)
4946 head_size
= io_size
;
4948 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, head_size
, CL_READ
, callback
, callback_arg
);
4951 goto wait_for_creads
;
4953 upl_offset
+= head_size
;
4954 dst_paddr
+= head_size
;
4955 io_size
-= head_size
;
4957 iov_base
+= head_size
;
4959 if ((u_int32_t
)iov_base
& mem_alignment_mask
) {
4961 * request doesn't set up on a memory boundary
4962 * the underlying DMA engine can handle...
4963 * return an error instead of going through
4964 * the slow copy path since the intent of this
4965 * path is direct I/O to device memory
4968 goto wait_for_creads
;
4971 tail_size
= io_size
& (devblocksize
- 1);
4973 io_size
-= tail_size
;
4975 while (io_size
&& error
== 0) {
4977 if (io_size
> MAX_IO_CONTIG_SIZE
)
4978 xsize
= MAX_IO_CONTIG_SIZE
;
4982 * request asynchronously so that we can overlap
4983 * the preparation of the next I/O... we'll do
4984 * the commit after all the I/O has completed
4985 * since its all issued against the same UPL
4986 * if there are already too many outstanding reads
4987 * wait until some have completed before issuing the next
4989 cluster_iostate_wait(&iostate
, MAX_IO_CONTIG_SIZE
* IO_SCALE(vp
, 2), "cluster_read_contig");
4991 if (iostate
.io_error
) {
4993 * one of the earlier reads we issued ran into a hard error
4994 * don't issue any more reads...
4995 * go wait for any other reads to complete before
4996 * returning the error to the caller
4998 goto wait_for_creads
;
5000 error
= cluster_io(vp
, upl
[cur_upl
], upl_offset
, uio
->uio_offset
, xsize
,
5001 CL_READ
| CL_NOZERO
| CL_DEV_MEMORY
| CL_ASYNC
| bflag
,
5002 (buf_t
)NULL
, &iostate
, callback
, callback_arg
);
5004 * The cluster_io read was issued successfully,
5005 * update the uio structure
5008 uio_update(uio
, (user_size_t
)xsize
);
5011 upl_offset
+= xsize
;
5015 if (error
== 0 && iostate
.io_error
== 0 && tail_size
== 0 && num_upl
< MAX_VECTS
&& uio
->uio_offset
< filesize
) {
5017 error
= cluster_io_type(uio
, read_type
, read_length
, 0);
5019 if (error
== 0 && *read_type
== IO_CONTIG
) {
5024 *read_type
= IO_UNKNOWN
;
5028 * make sure all async reads that are part of this stream
5029 * have completed before we proceed
5031 cluster_iostate_wait(&iostate
, 0, "cluster_read_contig");
5033 if (iostate
.io_error
)
5034 error
= iostate
.io_error
;
5036 lck_mtx_destroy(&iostate
.io_mtxp
, cl_mtx_grp
);
5038 if (error
== 0 && tail_size
)
5039 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, tail_size
, CL_READ
, callback
, callback_arg
);
5041 for (n
= 0; n
< num_upl
; n
++)
5043 * just release our hold on each physically contiguous
5044 * region without changing any state
5046 ubc_upl_abort(upl
[n
], 0);
5053 cluster_io_type(struct uio
*uio
, int *io_type
, u_int32_t
*io_length
, u_int32_t min_length
)
5055 user_size_t iov_len
;
5056 user_addr_t iov_base
= 0;
5058 upl_size_t upl_size
;
5059 upl_control_flags_t upl_flags
;
5063 * skip over any emtpy vectors
5065 uio_update(uio
, (user_size_t
)0);
5067 iov_len
= uio_curriovlen(uio
);
5069 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_START
, uio
, (int)iov_len
, 0, 0, 0);
5072 iov_base
= uio_curriovbase(uio
);
5074 * make sure the size of the vector isn't too big...
5075 * internally, we want to handle all of the I/O in
5076 * chunk sizes that fit in a 32 bit int
5078 if (iov_len
> (user_size_t
)MAX_IO_REQUEST_SIZE
)
5079 upl_size
= MAX_IO_REQUEST_SIZE
;
5081 upl_size
= (u_int32_t
)iov_len
;
5083 upl_flags
= UPL_QUERY_OBJECT_TYPE
| UPL_MEMORY_TAG_MAKE(VM_KERN_MEMORY_FILE
);
5085 vm_map_t map
= UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
) ? current_map() : kernel_map
;
5086 if ((vm_map_get_upl(map
,
5087 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
5088 &upl_size
, &upl
, NULL
, NULL
, &upl_flags
, 0)) != KERN_SUCCESS
) {
5090 * the user app must have passed in an invalid address
5097 *io_length
= upl_size
;
5099 if (upl_flags
& UPL_PHYS_CONTIG
)
5100 *io_type
= IO_CONTIG
;
5101 else if (iov_len
>= min_length
)
5102 *io_type
= IO_DIRECT
;
5107 * nothing left to do for this uio
5110 *io_type
= IO_UNKNOWN
;
5112 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 94)) | DBG_FUNC_END
, iov_base
, *io_type
, *io_length
, retval
, 0);
5119 * generate advisory I/O's in the largest chunks possible
5120 * the completed pages will be released into the VM cache
5123 advisory_read(vnode_t vp
, off_t filesize
, off_t f_offset
, int resid
)
5125 return advisory_read_ext(vp
, filesize
, f_offset
, resid
, NULL
, NULL
, CL_PASSIVE
);
5129 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
)
5131 upl_page_info_t
*pl
;
5133 vm_offset_t upl_offset
;
5146 uint32_t max_io_size
;
5149 if ( !UBCINFOEXISTS(vp
))
5155 max_io_size
= cluster_max_io_size(vp
->v_mount
, CL_READ
);
5157 if ((vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
) && !ignore_is_ssd
) {
5158 if (max_io_size
> speculative_prefetch_max_iosize
)
5159 max_io_size
= speculative_prefetch_max_iosize
;
5162 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_START
,
5163 (int)f_offset
, resid
, (int)filesize
, 0, 0);
5165 while (resid
&& f_offset
< filesize
&& retval
== 0) {
5167 * compute the size of the upl needed to encompass
5168 * the requested read... limit each call to cluster_io
5169 * to the maximum UPL size... cluster_io will clip if
5170 * this exceeds the maximum io_size for the device,
5171 * make sure to account for
5172 * a starting offset that's not page aligned
5174 start_offset
= (int)(f_offset
& PAGE_MASK_64
);
5175 upl_f_offset
= f_offset
- (off_t
)start_offset
;
5176 max_size
= filesize
- f_offset
;
5178 if (resid
< max_size
)
5183 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
5184 if ((uint32_t)upl_size
> max_io_size
)
5185 upl_size
= max_io_size
;
5189 * return the number of contiguously present pages in the cache
5190 * starting at upl_f_offset within the file
5192 ubc_range_op(vp
, upl_f_offset
, upl_f_offset
+ upl_size
, UPL_ROP_PRESENT
, &skip_range
);
5196 * skip over pages already present in the cache
5198 io_size
= skip_range
- start_offset
;
5200 f_offset
+= io_size
;
5203 if (skip_range
== upl_size
)
5206 * have to issue some real I/O
5207 * at this point, we know it's starting on a page boundary
5208 * because we've skipped over at least the first page in the request
5211 upl_f_offset
+= skip_range
;
5212 upl_size
-= skip_range
;
5214 pages_in_upl
= upl_size
/ PAGE_SIZE
;
5216 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_START
,
5217 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
5219 kret
= ubc_create_upl(vp
,
5224 UPL_RET_ONLY_ABSENT
| UPL_SET_LITE
);
5225 if (kret
!= KERN_SUCCESS
)
5230 * before we start marching forward, we must make sure we end on
5231 * a present page, otherwise we will be working with a freed
5234 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
5235 if (upl_page_present(pl
, last_pg
))
5238 pages_in_upl
= last_pg
+ 1;
5241 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_END
,
5242 upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
5245 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
5247 * scan from the beginning of the upl looking for the first
5248 * page that is present.... this will become the first page in
5249 * the request we're going to make to 'cluster_io'... if all
5250 * of the pages are absent, we won't call through to 'cluster_io'
5252 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
5253 if (upl_page_present(pl
, start_pg
))
5258 * scan from the starting present page looking for an absent
5259 * page before the end of the upl is reached, if we
5260 * find one, then it will terminate the range of pages being
5261 * presented to 'cluster_io'
5263 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
5264 if (!upl_page_present(pl
, last_pg
))
5268 if (last_pg
> start_pg
) {
5270 * we found a range of pages that must be filled
5271 * if the last page in this range is the last page of the file
5272 * we may have to clip the size of it to keep from reading past
5273 * the end of the last physical block associated with the file
5275 upl_offset
= start_pg
* PAGE_SIZE
;
5276 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
5278 if ((off_t
)(upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
5279 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
5282 * issue an asynchronous read to cluster_io
5284 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
5285 CL_ASYNC
| CL_READ
| CL_COMMIT
| CL_AGE
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5291 ubc_upl_abort(upl
, 0);
5293 io_size
= upl_size
- start_offset
;
5295 if (io_size
> resid
)
5297 f_offset
+= io_size
;
5301 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_END
,
5302 (int)f_offset
, resid
, retval
, 0, 0);
5309 cluster_push(vnode_t vp
, int flags
)
5311 return cluster_push_ext(vp
, flags
, NULL
, NULL
);
5316 cluster_push_ext(vnode_t vp
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5319 int my_sparse_wait
= 0;
5320 struct cl_writebehind
*wbp
;
5322 if ( !UBCINFOEXISTS(vp
)) {
5323 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, vp
, flags
, 0, -1, 0);
5326 /* return if deferred write is set */
5327 if (((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) && (flags
& IO_DEFWRITE
)) {
5330 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) == NULL
) {
5331 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, vp
, flags
, 0, -2, 0);
5334 if (!ISSET(flags
, IO_SYNC
) && wbp
->cl_number
== 0 && wbp
->cl_scmap
== NULL
) {
5335 lck_mtx_unlock(&wbp
->cl_lockw
);
5337 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, vp
, flags
, 0, -3, 0);
5340 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_START
,
5341 wbp
->cl_scmap
, wbp
->cl_number
, flags
, 0, 0);
5344 * if we have an fsync in progress, we don't want to allow any additional
5345 * sync/fsync/close(s) to occur until it finishes.
5346 * note that its possible for writes to continue to occur to this file
5347 * while we're waiting and also once the fsync starts to clean if we're
5348 * in the sparse map case
5350 while (wbp
->cl_sparse_wait
) {
5351 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_START
, vp
, 0, 0, 0, 0);
5353 msleep((caddr_t
)&wbp
->cl_sparse_wait
, &wbp
->cl_lockw
, PRIBIO
+ 1, "cluster_push_ext", NULL
);
5355 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_END
, vp
, 0, 0, 0, 0);
5357 if (flags
& IO_SYNC
) {
5359 wbp
->cl_sparse_wait
= 1;
5362 * this is an fsync (or equivalent)... we must wait for any existing async
5363 * cleaning operations to complete before we evaulate the current state
5364 * and finish cleaning... this insures that all writes issued before this
5365 * fsync actually get cleaned to the disk before this fsync returns
5367 while (wbp
->cl_sparse_pushes
) {
5368 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 98)) | DBG_FUNC_START
, vp
, 0, 0, 0, 0);
5370 msleep((caddr_t
)&wbp
->cl_sparse_pushes
, &wbp
->cl_lockw
, PRIBIO
+ 1, "cluster_push_ext", NULL
);
5372 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 98)) | DBG_FUNC_END
, vp
, 0, 0, 0, 0);
5375 if (wbp
->cl_scmap
) {
5378 if (wbp
->cl_sparse_pushes
< SPARSE_PUSH_LIMIT
) {
5380 scmap
= wbp
->cl_scmap
;
5381 wbp
->cl_scmap
= NULL
;
5383 wbp
->cl_sparse_pushes
++;
5385 lck_mtx_unlock(&wbp
->cl_lockw
);
5387 sparse_cluster_push(&scmap
, vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
);
5389 lck_mtx_lock(&wbp
->cl_lockw
);
5391 wbp
->cl_sparse_pushes
--;
5393 if (wbp
->cl_sparse_wait
&& wbp
->cl_sparse_pushes
== 0)
5394 wakeup((caddr_t
)&wbp
->cl_sparse_pushes
);
5396 sparse_cluster_push(&(wbp
->cl_scmap
), vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
);
5400 retval
= cluster_try_push(wbp
, vp
, ubc_getsize(vp
), PUSH_ALL
, flags
, callback
, callback_arg
);
5402 lck_mtx_unlock(&wbp
->cl_lockw
);
5404 if (flags
& IO_SYNC
)
5405 (void)vnode_waitforwrites(vp
, 0, 0, 0, "cluster_push");
5407 if (my_sparse_wait
) {
5409 * I'm the owner of the serialization token
5410 * clear it and wakeup anyone that is waiting
5413 lck_mtx_lock(&wbp
->cl_lockw
);
5415 wbp
->cl_sparse_wait
= 0;
5416 wakeup((caddr_t
)&wbp
->cl_sparse_wait
);
5418 lck_mtx_unlock(&wbp
->cl_lockw
);
5420 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_END
,
5421 wbp
->cl_scmap
, wbp
->cl_number
, retval
, 0, 0);
5427 __private_extern__
void
5428 cluster_release(struct ubc_info
*ubc
)
5430 struct cl_writebehind
*wbp
;
5431 struct cl_readahead
*rap
;
5433 if ((wbp
= ubc
->cl_wbehind
)) {
5435 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, ubc
, wbp
->cl_scmap
, 0, 0, 0);
5438 vfs_drt_control(&(wbp
->cl_scmap
), 0);
5440 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, ubc
, 0, 0, 0, 0);
5443 rap
= ubc
->cl_rahead
;
5446 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
5447 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
5449 if ((rap
= ubc
->cl_rahead
)) {
5450 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
5451 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
5453 ubc
->cl_rahead
= NULL
;
5454 ubc
->cl_wbehind
= NULL
;
5456 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_END
, ubc
, rap
, wbp
, 0, 0);
5461 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
)
5468 struct cl_wextent l_clusters
[MAX_CLUSTERS
];
5469 u_int max_cluster_pgcount
;
5472 max_cluster_pgcount
= MAX_CLUSTER_SIZE(vp
) / PAGE_SIZE
;
5474 * the write behind context exists and has
5475 * already been locked...
5477 if (wbp
->cl_number
== 0)
5479 * no clusters to push
5480 * return number of empty slots
5482 return (MAX_CLUSTERS
);
5485 * make a local 'sorted' copy of the clusters
5486 * and clear wbp->cl_number so that new clusters can
5489 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
5490 for (min_index
= -1, cl_index1
= 0; cl_index1
< wbp
->cl_number
; cl_index1
++) {
5491 if (wbp
->cl_clusters
[cl_index1
].b_addr
== wbp
->cl_clusters
[cl_index1
].e_addr
)
5493 if (min_index
== -1)
5494 min_index
= cl_index1
;
5495 else if (wbp
->cl_clusters
[cl_index1
].b_addr
< wbp
->cl_clusters
[min_index
].b_addr
)
5496 min_index
= cl_index1
;
5498 if (min_index
== -1)
5501 l_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[min_index
].b_addr
;
5502 l_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
5503 l_clusters
[cl_index
].io_flags
= wbp
->cl_clusters
[min_index
].io_flags
;
5505 wbp
->cl_clusters
[min_index
].b_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
5511 if ( (push_flag
& PUSH_DELAY
) && cl_len
== MAX_CLUSTERS
) {
5515 * determine if we appear to be writing the file sequentially
5516 * if not, by returning without having pushed any clusters
5517 * we will cause this vnode to be pushed into the sparse cluster mechanism
5518 * used for managing more random I/O patterns
5520 * we know that we've got all clusters currently in use and the next write doesn't fit into one of them...
5521 * that's why we're in try_push with PUSH_DELAY...
5523 * check to make sure that all the clusters except the last one are 'full'... and that each cluster
5524 * is adjacent to the next (i.e. we're looking for sequential writes) they were sorted above
5525 * so we can just make a simple pass through, up to, but not including the last one...
5526 * note that e_addr is not inclusive, so it will be equal to the b_addr of the next cluster if they
5529 * we let the last one be partial as long as it was adjacent to the previous one...
5530 * we need to do this to deal with multi-threaded servers that might write an I/O or 2 out
5531 * of order... if this occurs at the tail of the last cluster, we don't want to fall into the sparse cluster world...
5533 for (i
= 0; i
< MAX_CLUSTERS
- 1; i
++) {
5534 if ((l_clusters
[i
].e_addr
- l_clusters
[i
].b_addr
) != max_cluster_pgcount
)
5536 if (l_clusters
[i
].e_addr
!= l_clusters
[i
+1].b_addr
)
5540 for (cl_index
= 0; cl_index
< cl_len
; cl_index
++) {
5542 struct cl_extent cl
;
5544 flags
= io_flags
& (IO_PASSIVE
|IO_CLOSE
);
5547 * try to push each cluster in turn...
5549 if (l_clusters
[cl_index
].io_flags
& CLW_IONOCACHE
)
5550 flags
|= IO_NOCACHE
;
5552 if (l_clusters
[cl_index
].io_flags
& CLW_IOPASSIVE
)
5553 flags
|= IO_PASSIVE
;
5555 if (push_flag
& PUSH_SYNC
)
5558 cl
.b_addr
= l_clusters
[cl_index
].b_addr
;
5559 cl
.e_addr
= l_clusters
[cl_index
].e_addr
;
5561 cluster_push_now(vp
, &cl
, EOF
, flags
, callback
, callback_arg
);
5563 l_clusters
[cl_index
].b_addr
= 0;
5564 l_clusters
[cl_index
].e_addr
= 0;
5568 if ( !(push_flag
& PUSH_ALL
) )
5572 if (cl_len
> cl_pushed
) {
5574 * we didn't push all of the clusters, so
5575 * lets try to merge them back in to the vnode
5577 if ((MAX_CLUSTERS
- wbp
->cl_number
) < (cl_len
- cl_pushed
)) {
5579 * we picked up some new clusters while we were trying to
5580 * push the old ones... this can happen because I've dropped
5581 * the vnode lock... the sum of the
5582 * leftovers plus the new cluster count exceeds our ability
5583 * to represent them, so switch to the sparse cluster mechanism
5585 * collect the active public clusters...
5587 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
);
5589 for (cl_index
= 0, cl_index1
= 0; cl_index
< cl_len
; cl_index
++) {
5590 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
5592 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
5593 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
5594 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
5599 * update the cluster count
5601 wbp
->cl_number
= cl_index1
;
5604 * and collect the original clusters that were moved into the
5605 * local storage for sorting purposes
5607 sparse_cluster_switch(wbp
, vp
, EOF
, callback
, callback_arg
);
5611 * we've got room to merge the leftovers back in
5612 * just append them starting at the next 'hole'
5613 * represented by wbp->cl_number
5615 for (cl_index
= 0, cl_index1
= wbp
->cl_number
; cl_index
< cl_len
; cl_index
++) {
5616 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
5619 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
5620 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
5621 wbp
->cl_clusters
[cl_index1
].io_flags
= l_clusters
[cl_index
].io_flags
;
5626 * update the cluster count
5628 wbp
->cl_number
= cl_index1
;
5631 return (MAX_CLUSTERS
- wbp
->cl_number
);
5637 cluster_push_now(vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int flags
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5639 upl_page_info_t
*pl
;
5641 vm_offset_t upl_offset
;
5656 if (flags
& IO_PASSIVE
)
5661 if (flags
& IO_SKIP_ENCRYPTION
)
5662 bflag
|= CL_ENCRYPTED
;
5664 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_START
,
5665 (int)cl
->b_addr
, (int)cl
->e_addr
, (int)EOF
, flags
, 0);
5667 if ((pages_in_upl
= (int)(cl
->e_addr
- cl
->b_addr
)) == 0) {
5668 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 0, 0, 0, 0);
5672 upl_size
= pages_in_upl
* PAGE_SIZE
;
5673 upl_f_offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
5675 if (upl_f_offset
+ upl_size
>= EOF
) {
5677 if (upl_f_offset
>= EOF
) {
5679 * must have truncated the file and missed
5680 * clearing a dangling cluster (i.e. it's completely
5681 * beyond the new EOF
5683 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 1, 0, 0, 0);
5687 size
= EOF
- upl_f_offset
;
5689 upl_size
= (size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
5690 pages_in_upl
= upl_size
/ PAGE_SIZE
;
5694 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, size
, 0, 0, 0);
5697 * by asking for UPL_COPYOUT_FROM and UPL_RET_ONLY_DIRTY, we get the following desirable behavior
5699 * - only pages that are currently dirty are returned... these are the ones we need to clean
5700 * - the hardware dirty bit is cleared when the page is gathered into the UPL... the software dirty bit is set
5701 * - 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
5702 * - when we commit the page, the software dirty bit is cleared... the hardware dirty bit is untouched so that if
5703 * someone dirties this page while the I/O is in progress, we don't lose track of the new state
5705 * when the I/O completes, we no longer ask for an explicit clear of the DIRTY state (either soft or hard)
5708 if ((vp
->v_flag
& VNOCACHE_DATA
) || (flags
& IO_NOCACHE
))
5709 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
| UPL_WILL_BE_DUMPED
;
5711 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
;
5713 kret
= ubc_create_upl(vp
,
5719 if (kret
!= KERN_SUCCESS
)
5720 panic("cluster_push: failed to get pagelist");
5722 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
, upl
, upl_f_offset
, 0, 0, 0);
5725 * since we only asked for the dirty pages back
5726 * it's possible that we may only get a few or even none, so...
5727 * before we start marching forward, we must make sure we know
5728 * where the last present page is in the UPL, otherwise we could
5729 * end up working with a freed upl due to the FREE_ON_EMPTY semantics
5730 * employed by commit_range and abort_range.
5732 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
5733 if (upl_page_present(pl
, last_pg
))
5736 pages_in_upl
= last_pg
+ 1;
5738 if (pages_in_upl
== 0) {
5739 ubc_upl_abort(upl
, 0);
5741 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 2, 0, 0, 0);
5745 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
5747 * find the next dirty page in the UPL
5748 * this will become the first page in the
5749 * next I/O to generate
5751 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
5752 if (upl_dirty_page(pl
, start_pg
))
5754 if (upl_page_present(pl
, start_pg
))
5756 * RET_ONLY_DIRTY will return non-dirty 'precious' pages
5757 * just release these unchanged since we're not going
5758 * to steal them or change their state
5760 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
5762 if (start_pg
>= pages_in_upl
)
5764 * done... no more dirty pages to push
5767 if (start_pg
> last_pg
)
5769 * skipped over some non-dirty pages
5771 size
-= ((start_pg
- last_pg
) * PAGE_SIZE
);
5774 * find a range of dirty pages to write
5776 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
5777 if (!upl_dirty_page(pl
, last_pg
))
5780 upl_offset
= start_pg
* PAGE_SIZE
;
5782 io_size
= min(size
, (last_pg
- start_pg
) * PAGE_SIZE
);
5784 io_flags
= CL_THROTTLE
| CL_COMMIT
| CL_AGE
| bflag
;
5786 if ( !(flags
& IO_SYNC
))
5787 io_flags
|= CL_ASYNC
;
5789 if (flags
& IO_CLOSE
)
5790 io_flags
|= CL_CLOSE
;
5792 if (flags
& IO_NOCACHE
)
5793 io_flags
|= CL_NOCACHE
;
5795 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
5796 io_flags
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5798 if (error
== 0 && retval
)
5803 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 3, 0, 0, 0);
5810 * sparse_cluster_switch is called with the write behind lock held
5813 sparse_cluster_switch(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5817 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_START
, vp
, wbp
->cl_scmap
, 0, 0, 0);
5819 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
5821 struct cl_extent cl
;
5823 for (cl
.b_addr
= wbp
->cl_clusters
[cl_index
].b_addr
; cl
.b_addr
< wbp
->cl_clusters
[cl_index
].e_addr
; cl
.b_addr
++) {
5825 if (ubc_page_op(vp
, (off_t
)(cl
.b_addr
* PAGE_SIZE_64
), 0, NULL
, &flags
) == KERN_SUCCESS
) {
5826 if (flags
& UPL_POP_DIRTY
) {
5827 cl
.e_addr
= cl
.b_addr
+ 1;
5829 sparse_cluster_add(&(wbp
->cl_scmap
), vp
, &cl
, EOF
, callback
, callback_arg
);
5836 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_END
, vp
, wbp
->cl_scmap
, 0, 0, 0);
5841 * sparse_cluster_push must be called with the write-behind lock held if the scmap is
5842 * still associated with the write-behind context... however, if the scmap has been disassociated
5843 * from the write-behind context (the cluster_push case), the wb lock is not held
5846 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
)
5848 struct cl_extent cl
;
5852 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_START
, vp
, (*scmap
), 0, push_flag
, 0);
5854 if (push_flag
& PUSH_ALL
)
5855 vfs_drt_control(scmap
, 1);
5858 if (vfs_drt_get_cluster(scmap
, &offset
, &length
) != KERN_SUCCESS
)
5861 cl
.b_addr
= (daddr64_t
)(offset
/ PAGE_SIZE_64
);
5862 cl
.e_addr
= (daddr64_t
)((offset
+ length
) / PAGE_SIZE_64
);
5864 cluster_push_now(vp
, &cl
, EOF
, io_flags
& (IO_PASSIVE
|IO_CLOSE
), callback
, callback_arg
);
5866 if ( !(push_flag
& PUSH_ALL
) )
5869 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_END
, vp
, (*scmap
), 0, 0, 0);
5874 * sparse_cluster_add is called with the write behind lock held
5877 sparse_cluster_add(void **scmap
, vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int (*callback
)(buf_t
, void *), void *callback_arg
)
5883 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_START
, (*scmap
), 0, cl
->b_addr
, (int)cl
->e_addr
, 0);
5885 offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
5886 length
= ((u_int
)(cl
->e_addr
- cl
->b_addr
)) * PAGE_SIZE
;
5888 while (vfs_drt_mark_pages(scmap
, offset
, length
, &new_dirty
) != KERN_SUCCESS
) {
5890 * no room left in the map
5891 * only a partial update was done
5892 * push out some pages and try again
5894 sparse_cluster_push(scmap
, vp
, EOF
, 0, 0, callback
, callback_arg
);
5896 offset
+= (new_dirty
* PAGE_SIZE_64
);
5897 length
-= (new_dirty
* PAGE_SIZE
);
5899 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_END
, vp
, (*scmap
), 0, 0, 0);
5904 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
)
5906 upl_page_info_t
*pl
;
5916 if (flags
& IO_PASSIVE
)
5921 if (flags
& IO_NOCACHE
)
5922 bflag
|= CL_NOCACHE
;
5924 upl_flags
= UPL_SET_LITE
;
5926 if ( !(flags
& CL_READ
) ) {
5928 * "write" operation: let the UPL subsystem know
5929 * that we intend to modify the buffer cache pages
5932 upl_flags
|= UPL_WILL_MODIFY
;
5935 * indicate that there is no need to pull the
5936 * mapping for this page... we're only going
5937 * to read from it, not modify it.
5939 upl_flags
|= UPL_FILE_IO
;
5941 kret
= ubc_create_upl(vp
,
5942 uio
->uio_offset
& ~PAGE_MASK_64
,
5948 if (kret
!= KERN_SUCCESS
)
5951 if (!upl_valid_page(pl
, 0)) {
5953 * issue a synchronous read to cluster_io
5955 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
5956 CL_READ
| bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5958 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
5964 ubc_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << PAGE_SHIFT
) + (addr64_t
)(uio
->uio_offset
& PAGE_MASK_64
);
5967 * NOTE: There is no prototype for the following in BSD. It, and the definitions
5968 * of the defines for cppvPsrc, cppvPsnk, cppvFsnk, and cppvFsrc will be found in
5969 * osfmk/ppc/mappings.h. They are not included here because there appears to be no
5970 * way to do so without exporting them to kexts as well.
5972 if (flags
& CL_READ
)
5973 // copypv(ubc_paddr, usr_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsnk); /* Copy physical to physical and flush the destination */
5974 copypv(ubc_paddr
, usr_paddr
, xsize
, 2 | 1 | 4); /* Copy physical to physical and flush the destination */
5976 // copypv(usr_paddr, ubc_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsrc); /* Copy physical to physical and flush the source */
5977 copypv(usr_paddr
, ubc_paddr
, xsize
, 2 | 1 | 8); /* Copy physical to physical and flush the source */
5979 if ( !(flags
& CL_READ
) || (upl_valid_page(pl
, 0) && upl_dirty_page(pl
, 0))) {
5981 * issue a synchronous write to cluster_io
5983 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
5984 bflag
, (buf_t
)NULL
, (struct clios
*)NULL
, callback
, callback_arg
);
5987 uio_update(uio
, (user_size_t
)xsize
);
5990 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
5992 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
5994 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, abort_flags
);
6002 cluster_copy_upl_data(struct uio
*uio
, upl_t upl
, int upl_offset
, int *io_resid
)
6010 upl_page_info_t
*pl
;
6014 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
6015 (int)uio
->uio_offset
, upl_offset
, xsize
, 0, 0);
6017 segflg
= uio
->uio_segflg
;
6021 case UIO_USERSPACE32
:
6022 case UIO_USERISPACE32
:
6023 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
6027 case UIO_USERISPACE
:
6028 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
6031 case UIO_USERSPACE64
:
6032 case UIO_USERISPACE64
:
6033 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
6037 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
6041 pl
= ubc_upl_pageinfo(upl
);
6043 pg_index
= upl_offset
/ PAGE_SIZE
;
6044 pg_offset
= upl_offset
& PAGE_MASK
;
6045 csize
= min(PAGE_SIZE
- pg_offset
, xsize
);
6047 while (xsize
&& retval
== 0) {
6050 paddr
= ((addr64_t
)upl_phys_page(pl
, pg_index
) << PAGE_SHIFT
) + pg_offset
;
6052 retval
= uiomove64(paddr
, csize
, uio
);
6057 csize
= min(PAGE_SIZE
, xsize
);
6061 uio
->uio_segflg
= segflg
;
6063 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
6064 (int)uio
->uio_offset
, xsize
, retval
, segflg
, 0);
6071 cluster_copy_ubc_data(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
)
6074 return (cluster_copy_ubc_data_internal(vp
, uio
, io_resid
, mark_dirty
, 1));
6079 cluster_copy_ubc_data_internal(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
, int take_reference
)
6086 memory_object_control_t control
;
6088 io_size
= *io_resid
;
6090 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
6091 (int)uio
->uio_offset
, io_size
, mark_dirty
, take_reference
, 0);
6093 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
6095 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
6096 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
6097 (int)uio
->uio_offset
, io_size
, retval
, 3, 0);
6101 segflg
= uio
->uio_segflg
;
6105 case UIO_USERSPACE32
:
6106 case UIO_USERISPACE32
:
6107 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
6110 case UIO_USERSPACE64
:
6111 case UIO_USERISPACE64
:
6112 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
6116 case UIO_USERISPACE
:
6117 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
6121 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
6125 if ( (io_size
= *io_resid
) ) {
6126 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
6127 xsize
= uio_resid(uio
);
6129 retval
= memory_object_control_uiomove(control
, uio
->uio_offset
- start_offset
, uio
,
6130 start_offset
, io_size
, mark_dirty
, take_reference
);
6131 xsize
-= uio_resid(uio
);
6134 uio
->uio_segflg
= segflg
;
6135 *io_resid
= io_size
;
6137 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
6138 (int)uio
->uio_offset
, io_size
, retval
, 0x80000000 | segflg
, 0);
6145 is_file_clean(vnode_t vp
, off_t filesize
)
6149 int total_dirty
= 0;
6151 for (f_offset
= 0; f_offset
< filesize
; f_offset
+= PAGE_SIZE_64
) {
6152 if (ubc_page_op(vp
, f_offset
, 0, NULL
, &flags
) == KERN_SUCCESS
) {
6153 if (flags
& UPL_POP_DIRTY
) {
6167 * Dirty region tracking/clustering mechanism.
6169 * This code (vfs_drt_*) provides a mechanism for tracking and clustering
6170 * dirty regions within a larger space (file). It is primarily intended to
6171 * support clustering in large files with many dirty areas.
6173 * The implementation assumes that the dirty regions are pages.
6175 * To represent dirty pages within the file, we store bit vectors in a
6176 * variable-size circular hash.
6180 * Bitvector size. This determines the number of pages we group in a
6181 * single hashtable entry. Each hashtable entry is aligned to this
6182 * size within the file.
6184 #define DRT_BITVECTOR_PAGES ((1024 * 1024) / PAGE_SIZE)
6187 * File offset handling.
6189 * DRT_ADDRESS_MASK is dependent on DRT_BITVECTOR_PAGES;
6190 * the correct formula is (~((DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1))
6192 #define DRT_ADDRESS_MASK (~((DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1))
6193 #define DRT_ALIGN_ADDRESS(addr) ((addr) & DRT_ADDRESS_MASK)
6196 * Hashtable address field handling.
6198 * The low-order bits of the hashtable address are used to conserve
6201 * DRT_HASH_COUNT_MASK must be large enough to store the range
6202 * 0-DRT_BITVECTOR_PAGES inclusive, as well as have one value
6203 * to indicate that the bucket is actually unoccupied.
6205 #define DRT_HASH_GET_ADDRESS(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_ADDRESS_MASK)
6206 #define DRT_HASH_SET_ADDRESS(scm, i, a) \
6208 (scm)->scm_hashtable[(i)].dhe_control = \
6209 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_ADDRESS_MASK) | DRT_ALIGN_ADDRESS(a); \
6211 #define DRT_HASH_COUNT_MASK 0x1ff
6212 #define DRT_HASH_GET_COUNT(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_HASH_COUNT_MASK)
6213 #define DRT_HASH_SET_COUNT(scm, i, c) \
6215 (scm)->scm_hashtable[(i)].dhe_control = \
6216 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_HASH_COUNT_MASK) | ((c) & DRT_HASH_COUNT_MASK); \
6218 #define DRT_HASH_CLEAR(scm, i) \
6220 (scm)->scm_hashtable[(i)].dhe_control = 0; \
6222 #define DRT_HASH_VACATE(scm, i) DRT_HASH_SET_COUNT((scm), (i), DRT_HASH_COUNT_MASK)
6223 #define DRT_HASH_VACANT(scm, i) (DRT_HASH_GET_COUNT((scm), (i)) == DRT_HASH_COUNT_MASK)
6224 #define DRT_HASH_COPY(oscm, oi, scm, i) \
6226 (scm)->scm_hashtable[(i)].dhe_control = (oscm)->scm_hashtable[(oi)].dhe_control; \
6227 DRT_BITVECTOR_COPY(oscm, oi, scm, i); \
6232 * Hash table moduli.
6234 * Since the hashtable entry's size is dependent on the size of
6235 * the bitvector, and since the hashtable size is constrained to
6236 * both being prime and fitting within the desired allocation
6237 * size, these values need to be manually determined.
6239 * For DRT_BITVECTOR_SIZE = 256, the entry size is 40 bytes.
6241 * The small hashtable allocation is 1024 bytes, so the modulus is 23.
6242 * The large hashtable allocation is 16384 bytes, so the modulus is 401.
6244 #define DRT_HASH_SMALL_MODULUS 23
6245 #define DRT_HASH_LARGE_MODULUS 401
6248 * Physical memory required before the large hash modulus is permitted.
6250 * On small memory systems, the large hash modulus can lead to phsyical
6251 * memory starvation, so we avoid using it there.
6253 #define DRT_HASH_LARGE_MEMORY_REQUIRED (1024LL * 1024LL * 1024LL) /* 1GiB */
6255 #define DRT_SMALL_ALLOCATION 1024 /* 104 bytes spare */
6256 #define DRT_LARGE_ALLOCATION 16384 /* 344 bytes spare */
6258 /* *** nothing below here has secret dependencies on DRT_BITVECTOR_PAGES *** */
6261 * Hashtable bitvector handling.
6263 * Bitvector fields are 32 bits long.
6266 #define DRT_HASH_SET_BIT(scm, i, bit) \
6267 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] |= (1 << ((bit) % 32))
6269 #define DRT_HASH_CLEAR_BIT(scm, i, bit) \
6270 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] &= ~(1 << ((bit) % 32))
6272 #define DRT_HASH_TEST_BIT(scm, i, bit) \
6273 ((scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] & (1 << ((bit) % 32)))
6275 #define DRT_BITVECTOR_CLEAR(scm, i) \
6276 bzero(&(scm)->scm_hashtable[(i)].dhe_bitvector[0], (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
6278 #define DRT_BITVECTOR_COPY(oscm, oi, scm, i) \
6279 bcopy(&(oscm)->scm_hashtable[(oi)].dhe_bitvector[0], \
6280 &(scm)->scm_hashtable[(i)].dhe_bitvector[0], \
6281 (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
6288 struct vfs_drt_hashentry
{
6289 u_int64_t dhe_control
;
6291 * dhe_bitvector was declared as dhe_bitvector[DRT_BITVECTOR_PAGES / 32];
6292 * DRT_BITVECTOR_PAGES is defined as ((1024 * 1024) / PAGE_SIZE)
6293 * Since PAGE_SIZE is only known at boot time,
6294 * -define MAX_DRT_BITVECTOR_PAGES for smallest supported page size (4k)
6295 * -declare dhe_bitvector array for largest possible length
6297 #define MAX_DRT_BITVECTOR_PAGES (1024 * 1024)/( 4 * 1024)
6298 u_int32_t dhe_bitvector
[MAX_DRT_BITVECTOR_PAGES
/32];
6302 * Dirty Region Tracking structure.
6304 * The hashtable is allocated entirely inside the DRT structure.
6306 * The hash is a simple circular prime modulus arrangement, the structure
6307 * is resized from small to large if it overflows.
6310 struct vfs_drt_clustermap
{
6311 u_int32_t scm_magic
; /* sanity/detection */
6312 #define DRT_SCM_MAGIC 0x12020003
6313 u_int32_t scm_modulus
; /* current ring size */
6314 u_int32_t scm_buckets
; /* number of occupied buckets */
6315 u_int32_t scm_lastclean
; /* last entry we cleaned */
6316 u_int32_t scm_iskips
; /* number of slot skips */
6318 struct vfs_drt_hashentry scm_hashtable
[0];
6322 #define DRT_HASH(scm, addr) ((addr) % (scm)->scm_modulus)
6323 #define DRT_HASH_NEXT(scm, addr) (((addr) + 1) % (scm)->scm_modulus)
6326 * Debugging codes and arguments.
6328 #define DRT_DEBUG_EMPTYFREE (FSDBG_CODE(DBG_FSRW, 82)) /* nil */
6329 #define DRT_DEBUG_RETCLUSTER (FSDBG_CODE(DBG_FSRW, 83)) /* offset, length */
6330 #define DRT_DEBUG_ALLOC (FSDBG_CODE(DBG_FSRW, 84)) /* copycount */
6331 #define DRT_DEBUG_INSERT (FSDBG_CODE(DBG_FSRW, 85)) /* offset, iskip */
6332 #define DRT_DEBUG_MARK (FSDBG_CODE(DBG_FSRW, 86)) /* offset, length,
6335 /* 1 (clean, no map) */
6336 /* 2 (map alloc fail) */
6337 /* 3, resid (partial) */
6338 #define DRT_DEBUG_6 (FSDBG_CODE(DBG_FSRW, 87))
6339 #define DRT_DEBUG_SCMDATA (FSDBG_CODE(DBG_FSRW, 88)) /* modulus, buckets,
6340 * lastclean, iskips */
6343 static kern_return_t
vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
);
6344 static kern_return_t
vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
);
6345 static kern_return_t
vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
,
6346 u_int64_t offset
, int *indexp
);
6347 static kern_return_t
vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
,
6351 static kern_return_t
vfs_drt_do_mark_pages(
6357 static void vfs_drt_trace(
6358 struct vfs_drt_clustermap
*cmap
,
6367 * Allocate and initialise a sparse cluster map.
6369 * Will allocate a new map, resize or compact an existing map.
6371 * XXX we should probably have at least one intermediate map size,
6372 * as the 1:16 ratio seems a bit drastic.
6374 static kern_return_t
6375 vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
)
6377 struct vfs_drt_clustermap
*cmap
, *ocmap
;
6381 int nsize
, active_buckets
, index
, copycount
;
6388 * Decide on the size of the new map.
6390 if (ocmap
== NULL
) {
6391 nsize
= DRT_HASH_SMALL_MODULUS
;
6393 /* count the number of active buckets in the old map */
6395 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
6396 if (!DRT_HASH_VACANT(ocmap
, i
) &&
6397 (DRT_HASH_GET_COUNT(ocmap
, i
) != 0))
6401 * If we're currently using the small allocation, check to
6402 * see whether we should grow to the large one.
6404 if (ocmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) {
6406 * If the ring is nearly full and we are allowed to
6407 * use the large modulus, upgrade.
6409 if ((active_buckets
> (DRT_HASH_SMALL_MODULUS
- 5)) &&
6410 (max_mem
>= DRT_HASH_LARGE_MEMORY_REQUIRED
)) {
6411 nsize
= DRT_HASH_LARGE_MODULUS
;
6413 nsize
= DRT_HASH_SMALL_MODULUS
;
6416 /* already using the large modulus */
6417 nsize
= DRT_HASH_LARGE_MODULUS
;
6419 * If the ring is completely full, there's
6420 * nothing useful for us to do. Behave as
6421 * though we had compacted into the new
6424 if (active_buckets
>= DRT_HASH_LARGE_MODULUS
)
6425 return(KERN_SUCCESS
);
6430 * Allocate and initialise the new map.
6433 kret
= kmem_alloc(kernel_map
, (vm_offset_t
*)&cmap
,
6434 (nsize
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
, VM_KERN_MEMORY_FILE
);
6435 if (kret
!= KERN_SUCCESS
)
6437 cmap
->scm_magic
= DRT_SCM_MAGIC
;
6438 cmap
->scm_modulus
= nsize
;
6439 cmap
->scm_buckets
= 0;
6440 cmap
->scm_lastclean
= 0;
6441 cmap
->scm_iskips
= 0;
6442 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
6443 DRT_HASH_CLEAR(cmap
, i
);
6444 DRT_HASH_VACATE(cmap
, i
);
6445 DRT_BITVECTOR_CLEAR(cmap
, i
);
6449 * If there's an old map, re-hash entries from it into the new map.
6452 if (ocmap
!= NULL
) {
6453 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
6454 /* skip empty buckets */
6455 if (DRT_HASH_VACANT(ocmap
, i
) ||
6456 (DRT_HASH_GET_COUNT(ocmap
, i
) == 0))
6459 offset
= DRT_HASH_GET_ADDRESS(ocmap
, i
);
6460 kret
= vfs_drt_get_index(&cmap
, offset
, &index
, 1);
6461 if (kret
!= KERN_SUCCESS
) {
6462 /* XXX need to bail out gracefully here */
6463 panic("vfs_drt: new cluster map mysteriously too small");
6467 DRT_HASH_COPY(ocmap
, i
, cmap
, index
);
6472 /* log what we've done */
6473 vfs_drt_trace(cmap
, DRT_DEBUG_ALLOC
, copycount
, 0, 0, 0);
6476 * It's important to ensure that *cmapp always points to
6477 * a valid map, so we must overwrite it before freeing
6481 if (ocmap
!= NULL
) {
6482 /* emit stats into trace buffer */
6483 vfs_drt_trace(ocmap
, DRT_DEBUG_SCMDATA
,
6486 ocmap
->scm_lastclean
,
6489 vfs_drt_free_map(ocmap
);
6491 return(KERN_SUCCESS
);
6496 * Free a sparse cluster map.
6498 static kern_return_t
6499 vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
)
6501 kmem_free(kernel_map
, (vm_offset_t
)cmap
,
6502 (cmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
6503 return(KERN_SUCCESS
);
6508 * Find the hashtable slot currently occupied by an entry for the supplied offset.
6510 static kern_return_t
6511 vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
, u_int64_t offset
, int *indexp
)
6516 offset
= DRT_ALIGN_ADDRESS(offset
);
6517 index
= DRT_HASH(cmap
, offset
);
6519 /* traverse the hashtable */
6520 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
6523 * If the slot is vacant, we can stop.
6525 if (DRT_HASH_VACANT(cmap
, index
))
6529 * If the address matches our offset, we have success.
6531 if (DRT_HASH_GET_ADDRESS(cmap
, index
) == offset
) {
6533 return(KERN_SUCCESS
);
6537 * Move to the next slot, try again.
6539 index
= DRT_HASH_NEXT(cmap
, index
);
6544 return(KERN_FAILURE
);
6548 * Find the hashtable slot for the supplied offset. If we haven't allocated
6549 * one yet, allocate one and populate the address field. Note that it will
6550 * not have a nonzero page count and thus will still technically be free, so
6551 * in the case where we are called to clean pages, the slot will remain free.
6553 static kern_return_t
6554 vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
, u_int64_t offset
, int *indexp
, int recursed
)
6556 struct vfs_drt_clustermap
*cmap
;
6563 /* look for an existing entry */
6564 kret
= vfs_drt_search_index(cmap
, offset
, indexp
);
6565 if (kret
== KERN_SUCCESS
)
6568 /* need to allocate an entry */
6569 offset
= DRT_ALIGN_ADDRESS(offset
);
6570 index
= DRT_HASH(cmap
, offset
);
6572 /* scan from the index forwards looking for a vacant slot */
6573 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
6575 if (DRT_HASH_VACANT(cmap
, index
) || DRT_HASH_GET_COUNT(cmap
,index
) == 0) {
6576 cmap
->scm_buckets
++;
6577 if (index
< cmap
->scm_lastclean
)
6578 cmap
->scm_lastclean
= index
;
6579 DRT_HASH_SET_ADDRESS(cmap
, index
, offset
);
6580 DRT_HASH_SET_COUNT(cmap
, index
, 0);
6581 DRT_BITVECTOR_CLEAR(cmap
, index
);
6583 vfs_drt_trace(cmap
, DRT_DEBUG_INSERT
, (int)offset
, i
, 0, 0);
6584 return(KERN_SUCCESS
);
6586 cmap
->scm_iskips
+= i
;
6587 index
= DRT_HASH_NEXT(cmap
, index
);
6591 * We haven't found a vacant slot, so the map is full. If we're not
6592 * already recursed, try reallocating/compacting it.
6595 return(KERN_FAILURE
);
6596 kret
= vfs_drt_alloc_map(cmapp
);
6597 if (kret
== KERN_SUCCESS
) {
6598 /* now try to insert again */
6599 kret
= vfs_drt_get_index(cmapp
, offset
, indexp
, 1);
6605 * Implementation of set dirty/clean.
6607 * In the 'clean' case, not finding a map is OK.
6609 static kern_return_t
6610 vfs_drt_do_mark_pages(
6617 struct vfs_drt_clustermap
*cmap
, **cmapp
;
6619 int i
, index
, pgoff
, pgcount
, setcount
, ecount
;
6621 cmapp
= (struct vfs_drt_clustermap
**)private;
6624 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_START
, (int)offset
, (int)length
, dirty
, 0);
6626 if (setcountp
!= NULL
)
6629 /* allocate a cluster map if we don't already have one */
6631 /* no cluster map, nothing to clean */
6633 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 1, 0, 0, 0);
6634 return(KERN_SUCCESS
);
6636 kret
= vfs_drt_alloc_map(cmapp
);
6637 if (kret
!= KERN_SUCCESS
) {
6638 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 2, 0, 0, 0);
6645 * Iterate over the length of the region.
6647 while (length
> 0) {
6649 * Get the hashtable index for this offset.
6651 * XXX this will add blank entries if we are clearing a range
6652 * that hasn't been dirtied.
6654 kret
= vfs_drt_get_index(cmapp
, offset
, &index
, 0);
6655 cmap
= *cmapp
; /* may have changed! */
6656 /* this may be a partial-success return */
6657 if (kret
!= KERN_SUCCESS
) {
6658 if (setcountp
!= NULL
)
6659 *setcountp
= setcount
;
6660 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 3, (int)length
, 0, 0);
6666 * Work out how many pages we're modifying in this
6669 pgoff
= (offset
- DRT_ALIGN_ADDRESS(offset
)) / PAGE_SIZE
;
6670 pgcount
= min((length
/ PAGE_SIZE
), (DRT_BITVECTOR_PAGES
- pgoff
));
6673 * Iterate over pages, dirty/clearing as we go.
6675 ecount
= DRT_HASH_GET_COUNT(cmap
, index
);
6676 for (i
= 0; i
< pgcount
; i
++) {
6678 if (!DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
6679 DRT_HASH_SET_BIT(cmap
, index
, pgoff
+ i
);
6684 if (DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
6685 DRT_HASH_CLEAR_BIT(cmap
, index
, pgoff
+ i
);
6691 DRT_HASH_SET_COUNT(cmap
, index
, ecount
);
6693 offset
+= pgcount
* PAGE_SIZE
;
6694 length
-= pgcount
* PAGE_SIZE
;
6696 if (setcountp
!= NULL
)
6697 *setcountp
= setcount
;
6699 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 0, setcount
, 0, 0);
6701 return(KERN_SUCCESS
);
6705 * Mark a set of pages as dirty/clean.
6707 * This is a public interface.
6710 * Pointer to storage suitable for holding a pointer. Note that
6711 * this must either be NULL or a value set by this function.
6714 * Current file size in bytes.
6717 * Offset of the first page to be marked as dirty, in bytes. Must be
6721 * Length of dirty region, in bytes. Must be a multiple of PAGE_SIZE.
6724 * Number of pages newly marked dirty by this call (optional).
6726 * Returns KERN_SUCCESS if all the pages were successfully marked.
6728 static kern_return_t
6729 vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, u_int
*setcountp
)
6731 /* XXX size unused, drop from interface */
6732 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, setcountp
, 1));
6736 static kern_return_t
6737 vfs_drt_unmark_pages(void **cmapp
, off_t offset
, u_int length
)
6739 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0));
6744 * Get a cluster of dirty pages.
6746 * This is a public interface.
6749 * Pointer to storage managed by drt_mark_pages. Note that this must
6750 * be NULL or a value set by drt_mark_pages.
6753 * Returns the byte offset into the file of the first page in the cluster.
6756 * Returns the length in bytes of the cluster of dirty pages.
6758 * Returns success if a cluster was found. If KERN_FAILURE is returned, there
6759 * are no dirty pages meeting the minmum size criteria. Private storage will
6760 * be released if there are no more dirty pages left in the map
6763 static kern_return_t
6764 vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
)
6766 struct vfs_drt_clustermap
*cmap
;
6770 int index
, i
, fs
, ls
;
6773 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
6774 return(KERN_FAILURE
);
6777 /* walk the hashtable */
6778 for (offset
= 0, j
= 0; j
< cmap
->scm_modulus
; offset
+= (DRT_BITVECTOR_PAGES
* PAGE_SIZE
), j
++) {
6779 index
= DRT_HASH(cmap
, offset
);
6781 if (DRT_HASH_VACANT(cmap
, index
) || (DRT_HASH_GET_COUNT(cmap
, index
) == 0))
6784 /* scan the bitfield for a string of bits */
6787 for (i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
6788 if (DRT_HASH_TEST_BIT(cmap
, index
, i
)) {
6794 /* didn't find any bits set */
6795 panic("vfs_drt: entry summary count > 0 but no bits set in map");
6797 for (ls
= 0; i
< DRT_BITVECTOR_PAGES
; i
++, ls
++) {
6798 if (!DRT_HASH_TEST_BIT(cmap
, index
, i
))
6802 /* compute offset and length, mark pages clean */
6803 offset
= DRT_HASH_GET_ADDRESS(cmap
, index
) + (PAGE_SIZE
* fs
);
6804 length
= ls
* PAGE_SIZE
;
6805 vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0);
6806 cmap
->scm_lastclean
= index
;
6808 /* return successful */
6809 *offsetp
= (off_t
)offset
;
6812 vfs_drt_trace(cmap
, DRT_DEBUG_RETCLUSTER
, (int)offset
, (int)length
, 0, 0);
6813 return(KERN_SUCCESS
);
6816 * We didn't find anything... hashtable is empty
6817 * emit stats into trace buffer and
6820 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
6823 cmap
->scm_lastclean
,
6826 vfs_drt_free_map(cmap
);
6829 return(KERN_FAILURE
);
6833 static kern_return_t
6834 vfs_drt_control(void **cmapp
, int op_type
)
6836 struct vfs_drt_clustermap
*cmap
;
6839 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
6840 return(KERN_FAILURE
);
6845 /* emit stats into trace buffer */
6846 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
6849 cmap
->scm_lastclean
,
6852 vfs_drt_free_map(cmap
);
6857 cmap
->scm_lastclean
= 0;
6860 return(KERN_SUCCESS
);
6866 * Emit a summary of the state of the clustermap into the trace buffer
6867 * along with some caller-provided data.
6871 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, int code
, int arg1
, int arg2
, int arg3
, int arg4
)
6873 KERNEL_DEBUG(code
, arg1
, arg2
, arg3
, arg4
, 0);
6877 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, __unused
int code
,
6878 __unused
int arg1
, __unused
int arg2
, __unused
int arg3
,
6886 * Perform basic sanity check on the hash entry summary count
6887 * vs. the actual bits set in the entry.
6890 vfs_drt_sanity(struct vfs_drt_clustermap
*cmap
)
6895 for (index
= 0; index
< cmap
->scm_modulus
; index
++) {
6896 if (DRT_HASH_VACANT(cmap
, index
))
6899 for (bits_on
= 0, i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
6900 if (DRT_HASH_TEST_BIT(cmap
, index
, i
))
6903 if (bits_on
!= DRT_HASH_GET_COUNT(cmap
, index
))
6904 panic("bits_on = %d, index = %d\n", bits_on
, index
);