2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)vfs_cluster.c 8.10 (Berkeley) 3/28/95
66 #include <sys/param.h>
67 #include <sys/proc_internal.h>
68 #include <sys/buf_internal.h>
69 #include <sys/mount_internal.h>
70 #include <sys/vnode_internal.h>
71 #include <sys/trace.h>
72 #include <sys/malloc.h>
74 #include <sys/kernel.h>
75 #include <sys/resourcevar.h>
76 #include <sys/uio_internal.h>
77 #include <libkern/libkern.h>
78 #include <machine/machine_routines.h>
80 #include <sys/ubc_internal.h>
82 #include <mach/mach_types.h>
83 #include <mach/memory_object_types.h>
84 #include <mach/vm_map.h>
87 #include <vm/vm_kern.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_pageout.h>
91 #include <sys/kdebug.h>
96 #define CL_COMMIT 0x04
97 #define CL_PAGEOUT 0x10
100 #define CL_NOZERO 0x80
101 #define CL_PAGEIN 0x100
102 #define CL_DEV_MEMORY 0x200
103 #define CL_PRESERVE 0x400
104 #define CL_THROTTLE 0x800
105 #define CL_KEEPCACHED 0x1000
109 u_int io_completed
; /* amount of io that has currently completed */
110 u_int io_issued
; /* amount of io that was successfully issued */
111 int io_error
; /* error code of first error encountered */
112 int io_wanted
; /* someone is sleeping waiting for a change in state */
115 static lck_grp_t
*cl_mtx_grp
;
116 static lck_attr_t
*cl_mtx_attr
;
117 static lck_grp_attr_t
*cl_mtx_grp_attr
;
118 static lck_mtx_t
*cl_mtxp
;
121 static int cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
122 int flags
, buf_t real_bp
, struct clios
*iostate
);
123 static int cluster_iodone(buf_t bp
, void *dummy
);
124 static int cluster_rd_prefetch(vnode_t vp
, off_t f_offset
, u_int size
, off_t filesize
);
125 static int cluster_hard_throttle_on(vnode_t vp
);
127 static int cluster_read_x(vnode_t vp
, struct uio
*uio
, off_t filesize
, int flags
);
128 static int cluster_write_x(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
,
129 off_t headOff
, off_t tailOff
, int flags
);
130 static int cluster_nocopy_read(vnode_t vp
, struct uio
*uio
, off_t filesize
);
131 static int cluster_nocopy_write(vnode_t vp
, struct uio
*uio
, off_t newEOF
);
132 static int cluster_phys_read(vnode_t vp
, struct uio
*uio
, off_t filesize
);
133 static int cluster_phys_write(vnode_t vp
, struct uio
*uio
, off_t newEOF
);
134 static int cluster_align_phys_io(vnode_t vp
, struct uio
*uio
, addr64_t usr_paddr
, int xsize
, int flags
);
136 static void cluster_rd_ahead(vnode_t vp
, struct cl_extent
*extent
, off_t filesize
, struct cl_readahead
*ra
);
138 static int cluster_push_x(vnode_t vp
, struct cl_extent
*, off_t EOF
, int flags
);
139 static void cluster_push_EOF(vnode_t vp
, off_t EOF
);
141 static int cluster_try_push(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int can_delay
, int push_all
);
143 static void sparse_cluster_switch(struct cl_writebehind
*, vnode_t vp
, off_t EOF
);
144 static void sparse_cluster_push(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int push_all
);
145 static void sparse_cluster_add(struct cl_writebehind
*, vnode_t vp
, struct cl_extent
*, off_t EOF
);
147 static kern_return_t
vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, int *setcountp
);
148 static kern_return_t
vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
);
149 static kern_return_t
vfs_drt_control(void **cmapp
, int op_type
);
151 int is_file_clean(vnode_t
, off_t
);
154 * throttle the number of async writes that
155 * can be outstanding on a single vnode
156 * before we issue a synchronous write
158 #define HARD_THROTTLE_MAXCNT 0
159 #define HARD_THROTTLE_MAXSIZE (64 * 1024)
161 int hard_throttle_on_root
= 0;
162 struct timeval priority_IO_timestamp_for_root
;
168 * allocate lock group attribute and group
170 cl_mtx_grp_attr
= lck_grp_attr_alloc_init();
171 //lck_grp_attr_setstat(cl_mtx_grp_attr);
172 cl_mtx_grp
= lck_grp_alloc_init("cluster I/O", cl_mtx_grp_attr
);
175 * allocate the lock attribute
177 cl_mtx_attr
= lck_attr_alloc_init();
178 //lck_attr_setdebug(clf_mtx_attr);
181 * allocate and initialize mutex's used to protect updates and waits
182 * on the cluster_io context
184 cl_mtxp
= lck_mtx_alloc_init(cl_mtx_grp
, cl_mtx_attr
);
187 panic("cluster_init: failed to allocate cl_mtxp");
192 #define CLW_ALLOCATE 0x01
193 #define CLW_RETURNLOCKED 0x02
195 * if the read ahead context doesn't yet exist,
196 * allocate and initialize it...
197 * the vnode lock serializes multiple callers
198 * during the actual assignment... first one
199 * to grab the lock wins... the other callers
200 * will release the now unnecessary storage
202 * once the context is present, try to grab (but don't block on)
203 * the lock associated with it... if someone
204 * else currently owns it, than the read
205 * will run without read-ahead. this allows
206 * multiple readers to run in parallel and
207 * since there's only 1 read ahead context,
208 * there's no real loss in only allowing 1
209 * reader to have read-ahead enabled.
211 static struct cl_readahead
*
212 cluster_get_rap(vnode_t vp
)
214 struct ubc_info
*ubc
;
215 struct cl_readahead
*rap
;
219 if ((rap
= ubc
->cl_rahead
) == NULL
) {
220 MALLOC_ZONE(rap
, struct cl_readahead
*, sizeof *rap
, M_CLRDAHEAD
, M_WAITOK
);
222 bzero(rap
, sizeof *rap
);
224 lck_mtx_init(&rap
->cl_lockr
, cl_mtx_grp
, cl_mtx_attr
);
228 if (ubc
->cl_rahead
== NULL
)
229 ubc
->cl_rahead
= rap
;
231 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
232 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
233 rap
= ubc
->cl_rahead
;
237 if (lck_mtx_try_lock(&rap
->cl_lockr
) == TRUE
)
240 return ((struct cl_readahead
*)NULL
);
245 * if the write behind context doesn't yet exist,
246 * and CLW_ALLOCATE is specified, allocate and initialize it...
247 * the vnode lock serializes multiple callers
248 * during the actual assignment... first one
249 * to grab the lock wins... the other callers
250 * will release the now unnecessary storage
252 * if CLW_RETURNLOCKED is set, grab (blocking if necessary)
253 * the lock associated with the write behind context before
257 static struct cl_writebehind
*
258 cluster_get_wbp(vnode_t vp
, int flags
)
260 struct ubc_info
*ubc
;
261 struct cl_writebehind
*wbp
;
265 if ((wbp
= ubc
->cl_wbehind
) == NULL
) {
267 if ( !(flags
& CLW_ALLOCATE
))
268 return ((struct cl_writebehind
*)NULL
);
270 MALLOC_ZONE(wbp
, struct cl_writebehind
*, sizeof *wbp
, M_CLWRBEHIND
, M_WAITOK
);
272 bzero(wbp
, sizeof *wbp
);
273 lck_mtx_init(&wbp
->cl_lockw
, cl_mtx_grp
, cl_mtx_attr
);
277 if (ubc
->cl_wbehind
== NULL
)
278 ubc
->cl_wbehind
= wbp
;
280 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
281 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
282 wbp
= ubc
->cl_wbehind
;
286 if (flags
& CLW_RETURNLOCKED
)
287 lck_mtx_lock(&wbp
->cl_lockw
);
294 cluster_hard_throttle_on(vnode_t vp
)
296 static struct timeval hard_throttle_maxelapsed
= { 0, 200000 };
298 if (vp
->v_mount
->mnt_kern_flag
& MNTK_ROOTDEV
) {
299 struct timeval elapsed
;
301 if (hard_throttle_on_root
)
304 microuptime(&elapsed
);
305 timevalsub(&elapsed
, &priority_IO_timestamp_for_root
);
307 if (timevalcmp(&elapsed
, &hard_throttle_maxelapsed
, <))
315 cluster_iodone(buf_t bp
, __unused
void *dummy
)
328 struct clios
*iostate
;
332 cbp_head
= (buf_t
)(bp
->b_trans_head
);
334 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_START
,
335 (int)cbp_head
, bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
337 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
339 * all I/O requests that are part of this transaction
340 * have to complete before we can process it
342 if ( !(cbp
->b_flags
& B_DONE
)) {
344 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
345 (int)cbp_head
, (int)cbp
, cbp
->b_bcount
, cbp
->b_flags
, 0);
355 upl_offset
= cbp
->b_uploffset
;
357 b_flags
= cbp
->b_flags
;
358 real_bp
= cbp
->b_real_bp
;
359 zero_offset
= cbp
->b_validend
;
360 iostate
= (struct clios
*)cbp
->b_iostate
;
363 real_bp
->b_dev
= cbp
->b_dev
;
366 if ((cbp
->b_flags
& B_ERROR
) && error
== 0)
367 error
= cbp
->b_error
;
369 total_resid
+= cbp
->b_resid
;
370 total_size
+= cbp
->b_bcount
;
372 cbp_next
= cbp
->b_trans_next
;
379 cluster_zero(upl
, zero_offset
, PAGE_SIZE
- (zero_offset
& PAGE_MASK
), real_bp
);
385 * someone has issued multiple I/Os asynchrounsly
386 * and is waiting for them to complete (streaming)
388 lck_mtx_lock(cl_mtxp
);
390 if (error
&& iostate
->io_error
== 0)
391 iostate
->io_error
= error
;
393 iostate
->io_completed
+= total_size
;
395 if (iostate
->io_wanted
) {
397 * someone is waiting for the state of
398 * this io stream to change
400 iostate
->io_wanted
= 0;
403 lck_mtx_unlock(cl_mtxp
);
406 wakeup((caddr_t
)&iostate
->io_wanted
);
408 if ((b_flags
& B_NEED_IODONE
) && real_bp
) {
410 real_bp
->b_flags
|= B_ERROR
;
411 real_bp
->b_error
= error
;
413 real_bp
->b_resid
= total_resid
;
415 buf_biodone(real_bp
);
417 if (error
== 0 && total_resid
)
420 if (b_flags
& B_COMMIT_UPL
) {
421 pg_offset
= upl_offset
& PAGE_MASK
;
422 commit_size
= (pg_offset
+ total_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
424 if (error
|| (b_flags
& B_NOCACHE
)) {
429 if (b_flags
& B_PAGEIO
) {
430 if (b_flags
& B_READ
)
435 if (b_flags
& B_CACHE
) /* leave pages in the cache unchanged on error */
436 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
437 else if (page_out
&& (error
!= ENXIO
)) /* transient error */
438 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
440 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
442 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
444 ubc_upl_abort_range(upl
, upl_offset
- pg_offset
, commit_size
,
447 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
448 (int)upl
, upl_offset
- pg_offset
, commit_size
,
449 0x80000000|upl_abort_code
, 0);
452 int upl_commit_flags
= UPL_COMMIT_FREE_ON_EMPTY
;
454 if ((b_flags
& B_PHYS
) && (b_flags
& B_READ
))
455 upl_commit_flags
|= UPL_COMMIT_SET_DIRTY
;
458 upl_commit_flags
|= UPL_COMMIT_INACTIVATE
;
460 ubc_upl_commit_range(upl
, upl_offset
- pg_offset
, commit_size
,
463 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
464 (int)upl
, upl_offset
- pg_offset
, commit_size
,
465 upl_commit_flags
, 0);
468 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
469 (int)upl
, upl_offset
, 0, error
, 0);
477 cluster_zero(upl_t upl
, vm_offset_t upl_offset
, int size
, buf_t bp
)
481 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_START
,
482 upl_offset
, size
, (int)bp
, 0, 0);
484 if (bp
== NULL
|| bp
->b_datap
== 0) {
486 pl
= ubc_upl_pageinfo(upl
);
494 page_index
= upl_offset
/ PAGE_SIZE
;
495 page_offset
= upl_offset
& PAGE_MASK
;
497 zero_addr
= ((addr64_t
)upl_phys_page(pl
, page_index
) << 12) + page_offset
;
498 zero_cnt
= min(PAGE_SIZE
- page_offset
, size
);
500 bzero_phys(zero_addr
, zero_cnt
);
503 upl_offset
+= zero_cnt
;
506 bzero((caddr_t
)((vm_offset_t
)bp
->b_datap
+ upl_offset
), size
);
508 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_END
,
509 upl_offset
, size
, 0, 0, 0);
514 cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
515 int flags
, buf_t real_bp
, struct clios
*iostate
)
524 buf_t cbp_head
= NULL
;
525 buf_t cbp_tail
= NULL
;
533 int async_throttle
= 0;
538 if (mp
->mnt_devblocksize
> 1) {
540 * round the requested size up so that this I/O ends on a
541 * page boundary in case this is a 'write'... if the filesystem
542 * has blocks allocated to back the page beyond the EOF, we want to
543 * make sure to write out the zero's that are sitting beyond the EOF
544 * so that in case the filesystem doesn't explicitly zero this area
545 * if a hole is created via a lseek/write beyond the current EOF,
546 * it will return zeros when it's read back from the disk. If the
547 * physical allocation doesn't extend for the whole page, we'll
548 * only write/read from the disk up to the end of this allocation
549 * via the extent info returned from the VNOP_BLOCKMAP call.
551 pg_offset
= upl_offset
& PAGE_MASK
;
553 size
= (((non_rounded_size
+ pg_offset
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - pg_offset
;
556 * anyone advertising a blocksize of 1 byte probably
557 * can't deal with us rounding up the request size
558 * AFP is one such filesystem/device
560 size
= non_rounded_size
;
562 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_START
,
563 (int)f_offset
, size
, upl_offset
, flags
, 0);
565 if (flags
& CL_READ
) {
567 bmap_flags
= VNODE_READ
;
569 max_iosize
= mp
->mnt_maxreadcnt
;
570 max_vectors
= mp
->mnt_segreadcnt
;
573 bmap_flags
= VNODE_WRITE
;
575 max_iosize
= mp
->mnt_maxwritecnt
;
576 max_vectors
= mp
->mnt_segwritecnt
;
578 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_NONE
, max_iosize
, max_vectors
, mp
->mnt_devblocksize
, 0, 0);
581 * make sure the maximum iosize is a
582 * multiple of the page size
584 max_iosize
&= ~PAGE_MASK
;
586 if (flags
& CL_THROTTLE
) {
587 if ( !(flags
& CL_PAGEOUT
) && cluster_hard_throttle_on(vp
)) {
588 if (max_iosize
> HARD_THROTTLE_MAXSIZE
)
589 max_iosize
= HARD_THROTTLE_MAXSIZE
;
590 async_throttle
= HARD_THROTTLE_MAXCNT
;
592 async_throttle
= VNODE_ASYNC_THROTTLE
;
597 io_flags
|= B_NOCACHE
;
598 if (flags
& (CL_PAGEIN
| CL_PAGEOUT
))
599 io_flags
|= B_PAGEIO
;
600 if (flags
& CL_COMMIT
)
601 io_flags
|= B_COMMIT_UPL
;
602 if (flags
& CL_PRESERVE
)
604 if (flags
& CL_KEEPCACHED
)
607 if ((flags
& CL_READ
) && ((upl_offset
+ non_rounded_size
) & PAGE_MASK
) && (!(flags
& CL_NOZERO
))) {
609 * then we are going to end up
610 * with a page that we can't complete (the file size wasn't a multiple
611 * of PAGE_SIZE and we're trying to read to the end of the file
612 * so we'll go ahead and zero out the portion of the page we can't
613 * read in from the file
615 zero_offset
= upl_offset
+ non_rounded_size
;
622 if (size
> max_iosize
)
623 io_size
= max_iosize
;
627 if ((error
= VNOP_BLOCKMAP(vp
, f_offset
, io_size
, &blkno
, (size_t *)&io_size
, NULL
, bmap_flags
, NULL
))) {
630 if (real_bp
&& (real_bp
->b_blkno
== real_bp
->b_lblkno
))
631 real_bp
->b_blkno
= blkno
;
633 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 24)) | DBG_FUNC_NONE
,
634 (int)f_offset
, (int)blkno
, io_size
, zero_offset
, 0);
638 * vnop_blockmap didn't return an error... however, it did
639 * return an extent size of 0 which means we can't
640 * make forward progress on this I/O... a hole in the
641 * file would be returned as a blkno of -1 with a non-zero io_size
642 * a real extent is returned with a blkno != -1 and a non-zero io_size
647 if ( !(flags
& CL_READ
) && blkno
== -1) {
651 * we're writing into a 'hole'
653 if (flags
& CL_PAGEOUT
) {
655 * if we got here via cluster_pageout
656 * then just error the request and return
657 * the 'hole' should already have been covered
662 if ( !(flags
& CL_COMMIT
)) {
664 * currently writes always request the commit to happen
665 * as part of the io completion... however, if the CL_COMMIT
666 * flag isn't specified, than we can't issue the abort_range
667 * since the call site is going to abort or commit the same upl..
668 * in this case we can only return an error
674 * we can get here if the cluster code happens to
675 * pick up a page that was dirtied via mmap vs
676 * a 'write' and the page targets a 'hole'...
677 * i.e. the writes to the cluster were sparse
678 * and the file was being written for the first time
680 * we can also get here if the filesystem supports
681 * 'holes' that are less than PAGE_SIZE.... because
682 * we can't know if the range in the page that covers
683 * the 'hole' has been dirtied via an mmap or not,
684 * we have to assume the worst and try to push the
685 * entire page to storage.
687 * Try paging out the page individually before
688 * giving up entirely and dumping it (the pageout
689 * path will insure that the zero extent accounting
690 * has been taken care of before we get back into cluster_io)
692 ubc_upl_abort_range(upl
, trunc_page(upl_offset
), PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
694 e_offset
= round_page_64(f_offset
+ 1);
696 if (ubc_sync_range(vp
, f_offset
, e_offset
, UBC_PUSHDIRTY
) == 0) {
700 io_size
= e_offset
- f_offset
;
703 upl_offset
+= io_size
;
710 * keep track of how much of the original request
711 * that we've actually completed... non_rounded_size
712 * may go negative due to us rounding the request
713 * to a page size multiple (i.e. size > non_rounded_size)
715 non_rounded_size
-= io_size
;
717 if (non_rounded_size
<= 0) {
719 * we've transferred all of the data in the original
720 * request, but we were unable to complete the tail
721 * of the last page because the file didn't have
722 * an allocation to back that portion... this is ok.
728 lblkno
= (daddr64_t
)(f_offset
/ PAGE_SIZE_64
);
730 * we have now figured out how much I/O we can do - this is in 'io_size'
731 * pg_offset is the starting point in the first page for the I/O
732 * pg_count is the number of full and partial pages that 'io_size' encompasses
734 pg_offset
= upl_offset
& PAGE_MASK
;
736 if (flags
& CL_DEV_MEMORY
) {
738 * currently, can't deal with reading 'holes' in file
745 * treat physical requests as one 'giant' page
749 pg_count
= (io_size
+ pg_offset
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
751 if ((flags
& CL_READ
) && blkno
== -1) {
755 * if we're reading and blkno == -1, then we've got a
756 * 'hole' in the file that we need to deal with by zeroing
757 * out the affected area in the upl
759 if (zero_offset
&& io_size
== size
) {
761 * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
762 * than 'zero_offset' will be non-zero
763 * if the 'hole' returned by vnop_blockmap extends all the way to the eof
764 * (indicated by the io_size finishing off the I/O request for this UPL)
765 * than we're not going to issue an I/O for the
766 * last page in this upl... we need to zero both the hole and the tail
767 * of the page beyond the EOF, since the delayed zero-fill won't kick in
769 bytes_to_zero
= (((upl_offset
+ io_size
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - upl_offset
;
773 bytes_to_zero
= io_size
;
775 cluster_zero(upl
, upl_offset
, bytes_to_zero
, real_bp
);
779 * if there is a current I/O chain pending
780 * then the first page of the group we just zero'd
781 * will be handled by the I/O completion if the zero
782 * fill started in the middle of the page
784 pg_count
= (io_size
- pg_offset
) / PAGE_SIZE
;
787 * no pending I/O to pick up that first page
788 * so, we have to make sure it gets committed
790 * set the pg_offset to 0 so that the upl_commit_range
791 * starts with this page
793 pg_count
= (io_size
+ pg_offset
) / PAGE_SIZE
;
796 if (io_size
== size
&& ((upl_offset
+ io_size
) & PAGE_MASK
))
798 * if we're done with the request for this UPL
799 * then we have to make sure to commit the last page
800 * even if we only partially zero-filled it
806 pg_resid
= PAGE_SIZE
- pg_offset
;
810 if (flags
& CL_COMMIT
)
811 ubc_upl_commit_range(upl
,
812 (upl_offset
+ pg_resid
) & ~PAGE_MASK
,
813 pg_count
* PAGE_SIZE
,
814 UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
);
816 upl_offset
+= io_size
;
820 * keep track of how much of the original request
821 * that we've actually completed... non_rounded_size
822 * may go negative due to us rounding the request
823 * to a page size multiple (i.e. size > non_rounded_size)
825 non_rounded_size
-= io_size
;
827 if (non_rounded_size
<= 0) {
829 * we've transferred all of the data in the original
830 * request, but we were unable to complete the tail
831 * of the last page because the file didn't have
832 * an allocation to back that portion... this is ok.
836 if (cbp_head
&& pg_count
)
841 if (pg_count
> max_vectors
) {
842 if (((pg_count
- max_vectors
) * PAGE_SIZE
) > io_size
) {
843 io_size
= PAGE_SIZE
- pg_offset
;
846 io_size
-= (pg_count
- max_vectors
) * PAGE_SIZE
;
847 pg_count
= max_vectors
;
851 if ( !(mp
->mnt_kern_flag
& MNTK_VIRTUALDEV
))
853 * if we're not targeting a virtual device i.e. a disk image
854 * it's safe to dip into the reserve pool since real devices
855 * can complete this I/O request without requiring additional
856 * bufs from the alloc_io_buf pool
859 else if ((flags
& CL_ASYNC
) && !(flags
& CL_PAGEOUT
))
861 * Throttle the speculative IO
867 cbp
= alloc_io_buf(vp
, priv
);
869 if (flags
& CL_PAGEOUT
) {
872 for (i
= 0; i
< pg_count
; i
++) {
873 if (buf_invalblkno(vp
, lblkno
+ i
, 0) == EBUSY
)
874 panic("BUSY bp found in cluster_io");
877 if (flags
& CL_ASYNC
) {
878 if (buf_setcallback(cbp
, (void *)cluster_iodone
, NULL
))
879 panic("buf_setcallback failed\n");
881 cbp
->b_flags
|= io_flags
;
883 cbp
->b_lblkno
= lblkno
;
884 cbp
->b_blkno
= blkno
;
885 cbp
->b_bcount
= io_size
;
887 if (buf_setupl(cbp
, upl
, upl_offset
))
888 panic("buf_setupl failed\n");
890 cbp
->b_trans_next
= (buf_t
)NULL
;
892 if ((cbp
->b_iostate
= (void *)iostate
))
894 * caller wants to track the state of this
895 * io... bump the amount issued against this stream
897 iostate
->io_issued
+= io_size
;
899 if (flags
& CL_READ
) {
900 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 26)) | DBG_FUNC_NONE
,
901 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
904 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 27)) | DBG_FUNC_NONE
,
905 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
909 cbp_tail
->b_trans_next
= cbp
;
915 (buf_t
)(cbp
->b_trans_head
) = cbp_head
;
918 upl_offset
+= io_size
;
922 * keep track of how much of the original request
923 * that we've actually completed... non_rounded_size
924 * may go negative due to us rounding the request
925 * to a page size multiple (i.e. size > non_rounded_size)
927 non_rounded_size
-= io_size
;
929 if (non_rounded_size
<= 0) {
931 * we've transferred all of the data in the original
932 * request, but we were unable to complete the tail
933 * of the last page because the file didn't have
934 * an allocation to back that portion... this is ok.
938 if ( (!(upl_offset
& PAGE_MASK
) && !(flags
& CL_DEV_MEMORY
) && ((flags
& CL_ASYNC
) || trans_count
> 8)) || size
== 0) {
940 * if we have no more I/O to issue or
941 * the current I/O we've prepared fully
942 * completes the last page in this request
943 * and it's either an ASYNC request or
944 * we've already accumulated more than 8 I/O's into
945 * this transaction and it's not an I/O directed to
946 * special DEVICE memory
947 * then go ahead and issue the I/O
951 cbp_head
->b_flags
|= B_NEED_IODONE
;
952 cbp_head
->b_real_bp
= real_bp
;
954 cbp_head
->b_real_bp
= (buf_t
)NULL
;
958 * we're about to issue the last I/O for this upl
959 * if this was a read to the eof and the eof doesn't
960 * finish on a page boundary, than we need to zero-fill
961 * the rest of the page....
963 cbp_head
->b_validend
= zero_offset
;
965 cbp_head
->b_validend
= 0;
967 if (flags
& CL_THROTTLE
)
968 (void)vnode_waitforwrites(vp
, async_throttle
, 0, 0, (char *)"cluster_io");
970 for (cbp
= cbp_head
; cbp
;) {
973 if ( !(io_flags
& B_READ
))
974 vnode_startwrite(vp
);
976 cbp_next
= cbp
->b_trans_next
;
978 (void) VNOP_STRATEGY(cbp
);
981 if ( !(flags
& CL_ASYNC
)) {
984 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
987 if ((error
= cluster_iodone(cbp_head
, (void *)&dummy
))) {
988 if (((flags
& (CL_PAGEOUT
| CL_KEEPCACHED
)) == CL_PAGEOUT
) && (error
== ENXIO
))
989 error
= 0; /* drop the error */
997 cbp_head
= (buf_t
)NULL
;
998 cbp_tail
= (buf_t
)NULL
;
1008 for (cbp
= cbp_head
; cbp
;) {
1011 upl_offset
-= cbp
->b_bcount
;
1012 size
+= cbp
->b_bcount
;
1013 io_size
+= cbp
->b_bcount
;
1015 cbp_next
= cbp
->b_trans_next
;
1020 int need_wakeup
= 0;
1023 * update the error condition for this stream
1024 * since we never really issued the io
1025 * just go ahead and adjust it back
1027 lck_mtx_lock(cl_mtxp
);
1029 if (iostate
->io_error
== 0)
1030 iostate
->io_error
= error
;
1031 iostate
->io_issued
-= io_size
;
1033 if (iostate
->io_wanted
) {
1035 * someone is waiting for the state of
1036 * this io stream to change
1038 iostate
->io_wanted
= 0;
1041 lck_mtx_unlock(cl_mtxp
);
1044 wakeup((caddr_t
)&iostate
->io_wanted
);
1046 pg_offset
= upl_offset
& PAGE_MASK
;
1047 abort_size
= (size
+ pg_offset
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1049 if (flags
& CL_COMMIT
) {
1052 if (flags
& CL_PRESERVE
) {
1053 ubc_upl_commit_range(upl
, upl_offset
- pg_offset
, abort_size
,
1054 UPL_COMMIT_FREE_ON_EMPTY
);
1056 if ((flags
& CL_PAGEOUT
) && (error
!= ENXIO
)) /* transient error */
1057 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
1058 else if (flags
& CL_PAGEIN
)
1059 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
1061 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
1063 ubc_upl_abort_range(upl
, upl_offset
- pg_offset
, abort_size
,
1066 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 28)) | DBG_FUNC_NONE
,
1067 (int)upl
, upl_offset
- pg_offset
, abort_size
, error
, 0);
1070 real_bp
->b_flags
|= B_ERROR
;
1071 real_bp
->b_error
= error
;
1073 buf_biodone(real_bp
);
1078 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_END
,
1079 (int)f_offset
, size
, upl_offset
, retval
, 0);
1086 cluster_rd_prefetch(vnode_t vp
, off_t f_offset
, u_int size
, off_t filesize
)
1088 int pages_in_prefetch
;
1090 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_START
,
1091 (int)f_offset
, size
, (int)filesize
, 0, 0);
1093 if (f_offset
>= filesize
) {
1094 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1095 (int)f_offset
, 0, 0, 0, 0);
1098 if (size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
1099 size
= (MAX_UPL_TRANSFER
* PAGE_SIZE
);
1101 size
= (size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1103 if ((off_t
)size
> (filesize
- f_offset
))
1104 size
= filesize
- f_offset
;
1105 pages_in_prefetch
= (size
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1107 advisory_read(vp
, filesize
, f_offset
, size
);
1109 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1110 (int)f_offset
+ size
, pages_in_prefetch
, 0, 1, 0);
1112 return (pages_in_prefetch
);
1118 cluster_rd_ahead(vnode_t vp
, struct cl_extent
*extent
, off_t filesize
, struct cl_readahead
*rap
)
1122 int size_of_prefetch
;
1125 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_START
,
1126 (int)extent
->b_addr
, (int)extent
->e_addr
, (int)rap
->cl_lastr
, 0, 0);
1128 if (extent
->b_addr
== rap
->cl_lastr
&& extent
->b_addr
== extent
->e_addr
) {
1129 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1130 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 0, 0);
1133 if (rap
->cl_lastr
== -1 || (extent
->b_addr
!= rap
->cl_lastr
&& extent
->b_addr
!= (rap
->cl_lastr
+ 1) &&
1134 (extent
->b_addr
!= (rap
->cl_maxra
+ 1) || rap
->cl_ralen
== 0))) {
1138 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1139 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 1, 0);
1143 if (extent
->e_addr
< rap
->cl_maxra
) {
1144 if ((rap
->cl_maxra
- extent
->e_addr
) > (MAX_UPL_TRANSFER
/ 4)) {
1146 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1147 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 2, 0);
1151 r_addr
= max(extent
->e_addr
, rap
->cl_maxra
) + 1;
1152 f_offset
= (off_t
)(r_addr
* PAGE_SIZE_64
);
1154 size_of_prefetch
= 0;
1156 ubc_range_op(vp
, f_offset
, f_offset
+ PAGE_SIZE_64
, UPL_ROP_PRESENT
, &size_of_prefetch
);
1158 if (size_of_prefetch
) {
1159 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1160 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 3, 0);
1163 if (f_offset
< filesize
) {
1164 daddr64_t read_size
;
1166 rap
->cl_ralen
= rap
->cl_ralen
? min(MAX_UPL_TRANSFER
, rap
->cl_ralen
<< 1) : 1;
1168 read_size
= (extent
->e_addr
+ 1) - extent
->b_addr
;
1170 if (read_size
> rap
->cl_ralen
) {
1171 if (read_size
> MAX_UPL_TRANSFER
)
1172 rap
->cl_ralen
= MAX_UPL_TRANSFER
;
1174 rap
->cl_ralen
= read_size
;
1176 size_of_prefetch
= cluster_rd_prefetch(vp
, f_offset
, rap
->cl_ralen
* PAGE_SIZE
, filesize
);
1178 if (size_of_prefetch
)
1179 rap
->cl_maxra
= (r_addr
+ size_of_prefetch
) - 1;
1181 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1182 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 4, 0);
1186 cluster_pageout(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
,
1187 int size
, off_t filesize
, int flags
)
1193 struct cl_writebehind
*wbp
;
1195 if (vp
->v_mount
->mnt_kern_flag
& MNTK_VIRTUALDEV
)
1197 * if we know we're issuing this I/O to a virtual device (i.e. disk image)
1198 * then we don't want to enforce this throttle... if we do, we can
1199 * potentially deadlock since we're stalling the pageout thread at a time
1200 * when the disk image might need additional memory (which won't be available
1201 * if the pageout thread can't run)... instead we'll just depend on the throttle
1202 * that the pageout thread now has in place to deal with external files
1204 local_flags
= CL_PAGEOUT
;
1206 local_flags
= CL_PAGEOUT
| CL_THROTTLE
;
1208 if ((flags
& UPL_IOSYNC
) == 0)
1209 local_flags
|= CL_ASYNC
;
1210 if ((flags
& UPL_NOCOMMIT
) == 0)
1211 local_flags
|= CL_COMMIT
;
1212 if ((flags
& UPL_KEEPCACHED
))
1213 local_flags
|= CL_KEEPCACHED
;
1216 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 52)) | DBG_FUNC_NONE
,
1217 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
1220 * If they didn't specify any I/O, then we are done...
1221 * we can't issue an abort because we don't know how
1222 * big the upl really is
1227 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
1228 if (local_flags
& CL_COMMIT
)
1229 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
1233 * can't page-in from a negative offset
1234 * or if we're starting beyond the EOF
1235 * or if the file offset isn't page aligned
1236 * or the size requested isn't a multiple of PAGE_SIZE
1238 if (f_offset
< 0 || f_offset
>= filesize
||
1239 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
)) {
1240 if (local_flags
& CL_COMMIT
)
1241 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
1244 max_size
= filesize
- f_offset
;
1246 if (size
< max_size
)
1251 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1253 if (size
> rounded_size
) {
1254 if (local_flags
& CL_COMMIT
)
1255 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
, size
- rounded_size
,
1256 UPL_ABORT_FREE_ON_EMPTY
);
1258 if ((wbp
= cluster_get_wbp(vp
, 0)) != NULL
)
1259 wbp
->cl_hasbeenpaged
= 1;
1261 return (cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
1262 local_flags
, (buf_t
)NULL
, (struct clios
*)NULL
));
1266 cluster_pagein(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
,
1267 int size
, off_t filesize
, int flags
)
1273 int local_flags
= 0;
1275 if (upl
== NULL
|| size
< 0)
1276 panic("cluster_pagein: NULL upl passed in");
1278 if ((flags
& UPL_IOSYNC
) == 0)
1279 local_flags
|= CL_ASYNC
;
1280 if ((flags
& UPL_NOCOMMIT
) == 0)
1281 local_flags
|= CL_COMMIT
;
1284 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 56)) | DBG_FUNC_NONE
,
1285 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
1288 * can't page-in from a negative offset
1289 * or if we're starting beyond the EOF
1290 * or if the file offset isn't page aligned
1291 * or the size requested isn't a multiple of PAGE_SIZE
1293 if (f_offset
< 0 || f_offset
>= filesize
||
1294 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
) || (upl_offset
& PAGE_MASK
)) {
1295 if (local_flags
& CL_COMMIT
)
1296 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
1299 max_size
= filesize
- f_offset
;
1301 if (size
< max_size
)
1306 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1308 if (size
> rounded_size
&& (local_flags
& CL_COMMIT
))
1309 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
,
1310 size
- rounded_size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
1312 retval
= cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
1313 local_flags
| CL_READ
| CL_PAGEIN
, (buf_t
)NULL
, (struct clios
*)NULL
);
1315 if (retval
== 0 && !(flags
& UPL_NORDAHEAD
) && !(vp
->v_flag
& VRAOFF
)) {
1316 struct cl_readahead
*rap
;
1318 rap
= cluster_get_rap(vp
);
1321 struct cl_extent extent
;
1323 extent
.b_addr
= (daddr64_t
)(f_offset
/ PAGE_SIZE_64
);
1324 extent
.e_addr
= (daddr64_t
)((f_offset
+ ((off_t
)io_size
- 1)) / PAGE_SIZE_64
);
1326 if (rounded_size
== PAGE_SIZE
) {
1328 * we haven't read the last page in of the file yet
1329 * so let's try to read ahead if we're in
1330 * a sequential access pattern
1332 cluster_rd_ahead(vp
, &extent
, filesize
, rap
);
1334 rap
->cl_lastr
= extent
.e_addr
;
1336 lck_mtx_unlock(&rap
->cl_lockr
);
1343 cluster_bp(buf_t bp
)
1348 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 19)) | DBG_FUNC_START
,
1349 (int)bp
, (int)bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
1351 if (bp
->b_flags
& B_READ
)
1352 flags
= CL_ASYNC
| CL_READ
;
1356 f_offset
= ubc_blktooff(bp
->b_vp
, bp
->b_lblkno
);
1358 return (cluster_io(bp
->b_vp
, bp
->b_upl
, 0, f_offset
, bp
->b_bcount
, flags
, bp
, (struct clios
*)NULL
));
1362 cluster_write(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
, int xflags
)
1375 if (vp
->v_flag
& VNOCACHE_DATA
)
1376 flags
|= IO_NOCACHE
;
1378 if ( (!(flags
& IO_NOCACHE
)) || (!uio
) || (!UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))) {
1380 * go do a write through the cache if one of the following is true....
1381 * NOCACHE is not true
1382 * there is no uio structure or it doesn't target USERSPACE
1384 return (cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
));
1388 if (IS_VALID_UIO_SEGFLG(uio
->uio_segflg
) == 0) {
1389 panic("%s :%d - invalid uio_segflg\n", __FILE__
, __LINE__
);
1391 #endif /* LP64_DEBUG */
1393 while (uio_resid(uio
) && uio
->uio_offset
< newEOF
&& retval
== 0) {
1394 user_size_t iov_len
;
1395 user_addr_t iov_base
;
1398 * we know we have a resid, so this is safe
1399 * skip over any emtpy vectors
1401 uio_update(uio
, (user_size_t
)0);
1403 iov_len
= uio_curriovlen(uio
);
1404 iov_base
= uio_curriovbase(uio
);
1406 upl_size
= PAGE_SIZE
;
1407 upl_flags
= UPL_QUERY_OBJECT_TYPE
;
1409 // LP64todo - fix this!
1410 if ((vm_map_get_upl(current_map(),
1411 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
1412 &upl_size
, &upl
, NULL
, NULL
, &upl_flags
, 0)) != KERN_SUCCESS
) {
1414 * the user app must have passed in an invalid address
1420 * We check every vector target but if it is physically
1421 * contiguous space, we skip the sanity checks.
1423 if (upl_flags
& UPL_PHYS_CONTIG
) {
1426 zflags
= flags
& ~IO_TAILZEROFILL
;
1427 zflags
|= IO_HEADZEROFILL
;
1429 if (flags
& IO_HEADZEROFILL
) {
1431 * in case we have additional vectors, we don't want to do this again
1433 flags
&= ~IO_HEADZEROFILL
;
1435 if ((retval
= cluster_write_x(vp
, (struct uio
*)0, 0, uio
->uio_offset
, headOff
, 0, zflags
)))
1438 retval
= cluster_phys_write(vp
, uio
, newEOF
);
1440 if (uio_resid(uio
) == 0 && (flags
& IO_TAILZEROFILL
)) {
1441 return (cluster_write_x(vp
, (struct uio
*)0, 0, tailOff
, uio
->uio_offset
, 0, zflags
));
1444 else if ((uio_resid(uio
) < PAGE_SIZE
) || (flags
& (IO_TAILZEROFILL
| IO_HEADZEROFILL
))) {
1446 * we're here because we're don't have a physically contiguous target buffer
1447 * go do a write through the cache if one of the following is true....
1448 * the total xfer size is less than a page...
1449 * we're being asked to ZEROFILL either the head or the tail of the I/O...
1451 return (cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
));
1453 // LP64todo - fix this!
1454 else if (((int)uio
->uio_offset
& PAGE_MASK
) || (CAST_DOWN(int, iov_base
) & PAGE_MASK
)) {
1455 if (((int)uio
->uio_offset
& PAGE_MASK
) == (CAST_DOWN(int, iov_base
) & PAGE_MASK
)) {
1457 * Bring the file offset write up to a pagesize boundary
1458 * this will also bring the base address to a page boundary
1459 * since they both are currently on the same offset within a page
1460 * note: if we get here, uio->uio_resid is greater than PAGE_SIZE
1461 * so the computed clip_size must always be less than the current uio_resid
1463 clip_size
= (PAGE_SIZE
- (uio
->uio_offset
& PAGE_MASK_64
));
1466 * Fake the resid going into the cluster_write_x call
1467 * and restore it on the way out.
1469 // LP64todo - fix this
1470 prev_resid
= uio_resid(uio
);
1471 uio_setresid(uio
, clip_size
);
1473 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
);
1475 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
1478 * can't get both the file offset and the buffer offset aligned to a page boundary
1479 * so fire an I/O through the cache for this entire vector
1481 // LP64todo - fix this
1482 clip_size
= iov_len
;
1483 // LP64todo - fix this
1484 prev_resid
= uio_resid(uio
);
1485 uio_setresid(uio
, clip_size
);
1487 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
);
1489 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
1493 * If we come in here, we know the offset into
1494 * the file is on a pagesize boundary and the
1495 * target buffer address is also on a page boundary
1497 max_io_size
= newEOF
- uio
->uio_offset
;
1498 // LP64todo - fix this
1499 clip_size
= uio_resid(uio
);
1500 if (iov_len
< clip_size
)
1501 // LP64todo - fix this!
1502 clip_size
= iov_len
;
1503 if (max_io_size
< clip_size
)
1504 clip_size
= max_io_size
;
1506 if (clip_size
< PAGE_SIZE
) {
1508 * Take care of tail end of write in this vector
1510 // LP64todo - fix this
1511 prev_resid
= uio_resid(uio
);
1512 uio_setresid(uio
, clip_size
);
1514 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
);
1516 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
1518 /* round clip_size down to a multiple of pagesize */
1519 clip_size
= clip_size
& ~(PAGE_MASK
);
1520 // LP64todo - fix this
1521 prev_resid
= uio_resid(uio
);
1522 uio_setresid(uio
, clip_size
);
1524 retval
= cluster_nocopy_write(vp
, uio
, newEOF
);
1526 if ((retval
== 0) && uio_resid(uio
))
1527 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
);
1529 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
1539 cluster_nocopy_write(vnode_t vp
, struct uio
*uio
, off_t newEOF
)
1542 upl_page_info_t
*pl
;
1543 vm_offset_t upl_offset
;
1547 int upl_needed_size
;
1552 int force_data_sync
;
1554 struct clios iostate
;
1555 struct cl_writebehind
*wbp
;
1558 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_START
,
1559 (int)uio
->uio_offset
, (int)uio_resid(uio
),
1563 * When we enter this routine, we know
1564 * -- the offset into the file is on a pagesize boundary
1565 * -- the resid is a page multiple
1566 * -- the resid will not exceed iov_len
1569 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) != NULL
) {
1571 cluster_try_push(wbp
, vp
, newEOF
, 0, 1);
1573 lck_mtx_unlock(&wbp
->cl_lockw
);
1575 iostate
.io_completed
= 0;
1576 iostate
.io_issued
= 0;
1577 iostate
.io_error
= 0;
1578 iostate
.io_wanted
= 0;
1580 while (uio_resid(uio
) && uio
->uio_offset
< newEOF
&& error
== 0) {
1581 user_addr_t iov_base
;
1583 io_size
= uio_resid(uio
);
1585 if (io_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
1586 io_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
1588 iov_base
= uio_curriovbase(uio
);
1590 // LP64todo - fix this!
1591 upl_offset
= CAST_DOWN(vm_offset_t
, iov_base
) & PAGE_MASK
;
1593 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
1595 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_START
,
1596 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
1598 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
1600 upl_size
= upl_needed_size
;
1601 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
1602 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
1604 // LP64todo - fix this!
1605 kret
= vm_map_get_upl(current_map(),
1606 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
1614 if (kret
!= KERN_SUCCESS
) {
1615 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
1618 * cluster_nocopy_write: failed to get pagelist
1620 * we may have already spun some portion of this request
1621 * off as async requests... we need to wait for the I/O
1622 * to complete before returning
1624 goto wait_for_writes
;
1626 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
1627 pages_in_pl
= upl_size
/ PAGE_SIZE
;
1629 for (i
= 0; i
< pages_in_pl
; i
++) {
1630 if (!upl_valid_page(pl
, i
))
1633 if (i
== pages_in_pl
)
1637 * didn't get all the pages back that we
1638 * needed... release this upl and try again
1640 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
1641 UPL_ABORT_FREE_ON_EMPTY
);
1643 if (force_data_sync
>= 3) {
1644 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
1645 i
, pages_in_pl
, upl_size
, kret
, 0);
1647 * for some reason, we couldn't acquire a hold on all
1648 * the pages needed in the user's address space
1650 * we may have already spun some portion of this request
1651 * off as async requests... we need to wait for the I/O
1652 * to complete before returning
1654 goto wait_for_writes
;
1658 * Consider the possibility that upl_size wasn't satisfied.
1660 if (upl_size
!= upl_needed_size
)
1661 io_size
= (upl_size
- (int)upl_offset
) & ~PAGE_MASK
;
1663 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
1664 (int)upl_offset
, upl_size
, (int)iov_base
, io_size
, 0);
1667 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
1668 UPL_ABORT_FREE_ON_EMPTY
);
1670 * we may have already spun some portion of this request
1671 * off as async requests... we need to wait for the I/O
1672 * to complete before returning
1674 goto wait_for_writes
;
1677 * Now look for pages already in the cache
1678 * and throw them away.
1679 * uio->uio_offset is page aligned within the file
1680 * io_size is a multiple of PAGE_SIZE
1682 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ io_size
, UPL_ROP_DUMP
, NULL
);
1685 * we want push out these writes asynchronously so that we can overlap
1686 * the preparation of the next I/O
1687 * if there are already too many outstanding writes
1688 * wait until some complete before issuing the next
1690 lck_mtx_lock(cl_mtxp
);
1692 while ((iostate
.io_issued
- iostate
.io_completed
) > (2 * MAX_UPL_TRANSFER
* PAGE_SIZE
)) {
1693 iostate
.io_wanted
= 1;
1694 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_nocopy_write", 0);
1696 lck_mtx_unlock(cl_mtxp
);
1698 if (iostate
.io_error
) {
1700 * one of the earlier writes we issued ran into a hard error
1701 * don't issue any more writes, cleanup the UPL
1702 * that was just created but not used, then
1703 * go wait for all writes that are part of this stream
1704 * to complete before returning the error to the caller
1706 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
1707 UPL_ABORT_FREE_ON_EMPTY
);
1709 goto wait_for_writes
;
1711 io_flag
= CL_ASYNC
| CL_PRESERVE
| CL_COMMIT
| CL_THROTTLE
;
1713 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_START
,
1714 (int)upl_offset
, (int)uio
->uio_offset
, io_size
, io_flag
, 0);
1716 error
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
1717 io_size
, io_flag
, (buf_t
)NULL
, &iostate
);
1719 uio_update(uio
, (user_size_t
)io_size
);
1721 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_END
,
1722 (int)upl_offset
, (int)uio
->uio_offset
, (int)uio_resid(uio
), error
, 0);
1728 * make sure all async writes issued as part of this stream
1729 * have completed before we return
1731 lck_mtx_lock(cl_mtxp
);
1733 while (iostate
.io_issued
!= iostate
.io_completed
) {
1734 iostate
.io_wanted
= 1;
1735 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_nocopy_write", 0);
1737 lck_mtx_unlock(cl_mtxp
);
1739 if (iostate
.io_error
)
1740 error
= iostate
.io_error
;
1742 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
1743 (int)uio
->uio_offset
, (int)uio
->uio_resid
, error
, 4, 0);
1750 cluster_phys_write(vnode_t vp
, struct uio
*uio
, off_t newEOF
)
1752 upl_page_info_t
*pl
;
1755 vm_offset_t upl_offset
;
1759 int upl_needed_size
;
1764 user_addr_t iov_base
;
1766 struct cl_writebehind
*wbp
;
1768 devblocksize
= vp
->v_mount
->mnt_devblocksize
;
1770 * When we enter this routine, we know
1771 * -- the resid will not exceed iov_len
1772 * -- the vector target address is physcially contiguous
1774 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) != NULL
) {
1776 cluster_try_push(wbp
, vp
, newEOF
, 0, 1);
1778 lck_mtx_unlock(&wbp
->cl_lockw
);
1781 if (IS_VALID_UIO_SEGFLG(uio
->uio_segflg
) == 0) {
1782 panic("%s :%d - invalid uio_segflg\n", __FILE__
, __LINE__
);
1784 #endif /* LP64_DEBUG */
1786 // LP64todo - fix this!
1787 io_size
= (int)uio_curriovlen(uio
);
1788 iov_base
= uio_curriovbase(uio
);
1790 upl_offset
= CAST_DOWN(upl_offset_t
, iov_base
) & PAGE_MASK
;
1791 upl_needed_size
= upl_offset
+ io_size
;
1794 upl_size
= upl_needed_size
;
1795 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
1796 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
1798 // LP64todo - fix this!
1799 kret
= vm_map_get_upl(current_map(),
1800 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
1801 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
, 0);
1803 if (kret
!= KERN_SUCCESS
) {
1805 * cluster_phys_write: failed to get pagelist
1806 * note: return kret here
1811 * Consider the possibility that upl_size wasn't satisfied.
1812 * This is a failure in the physical memory case.
1814 if (upl_size
< upl_needed_size
) {
1815 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
1818 pl
= ubc_upl_pageinfo(upl
);
1820 src_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)upl_offset
;
1822 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
1825 head_size
= devblocksize
- (int)(uio
->uio_offset
& (devblocksize
- 1));
1827 if (head_size
> io_size
)
1828 head_size
= io_size
;
1830 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, head_size
, 0);
1833 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
1837 upl_offset
+= head_size
;
1838 src_paddr
+= head_size
;
1839 io_size
-= head_size
;
1841 tail_size
= io_size
& (devblocksize
- 1);
1842 io_size
-= tail_size
;
1846 * issue a synchronous write to cluster_io
1848 error
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
1849 io_size
, CL_DEV_MEMORY
, (buf_t
)NULL
, (struct clios
*)NULL
);
1853 * The cluster_io write completed successfully,
1854 * update the uio structure
1856 uio_update(uio
, (user_size_t
)io_size
);
1858 src_paddr
+= io_size
;
1861 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, tail_size
, 0);
1864 * just release our hold on the physically contiguous
1865 * region without changing any state
1867 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
1874 cluster_write_x(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
, int flags
)
1876 upl_page_info_t
*pl
;
1878 vm_offset_t upl_offset
= 0;
1891 long long total_size
;
1894 long long zero_cnt1
;
1896 struct cl_extent cl
;
1898 struct cl_writebehind
*wbp
;
1900 if ((wbp
= cluster_get_wbp(vp
, 0)) != NULL
)
1902 if (wbp
->cl_hasbeenpaged
) {
1904 * this vnode had pages cleaned to it by
1905 * the pager which indicates that either
1906 * it's not very 'hot', or the system is
1907 * being overwhelmed by a lot of dirty
1908 * data being delayed in the VM cache...
1909 * in either event, we'll push our remaining
1910 * delayed data at this point... this will
1911 * be more efficient than paging out 1 page at
1912 * a time, and will also act as a throttle
1913 * by delaying this client from writing any
1914 * more data until all his delayed data has
1915 * at least been queued to the uderlying driver.
1917 if (wbp
->cl_number
|| wbp
->cl_scmap
)
1918 cluster_push_EOF(vp
, newEOF
);
1920 wbp
->cl_hasbeenpaged
= 0;
1924 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
1925 (int)uio
->uio_offset
, uio_resid(uio
), (int)oldEOF
, (int)newEOF
, 0);
1927 // LP64todo - fix this
1928 io_resid
= uio_resid(uio
);
1930 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
1931 0, 0, (int)oldEOF
, (int)newEOF
, 0);
1940 if (flags
& IO_HEADZEROFILL
) {
1942 * some filesystems (HFS is one) don't support unallocated holes within a file...
1943 * so we zero fill the intervening space between the old EOF and the offset
1944 * where the next chunk of real data begins.... ftruncate will also use this
1945 * routine to zero fill to the new EOF when growing a file... in this case, the
1946 * uio structure will not be provided
1949 if (headOff
< uio
->uio_offset
) {
1950 zero_cnt
= uio
->uio_offset
- headOff
;
1953 } else if (headOff
< newEOF
) {
1954 zero_cnt
= newEOF
- headOff
;
1958 if (flags
& IO_TAILZEROFILL
) {
1960 // LP64todo - fix this
1961 zero_off1
= uio
->uio_offset
+ uio_resid(uio
);
1963 if (zero_off1
< tailOff
)
1964 zero_cnt1
= tailOff
- zero_off1
;
1967 if (zero_cnt
== 0 && uio
== (struct uio
*) 0) {
1968 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
1969 retval
, 0, 0, 0, 0);
1973 while ((total_size
= (io_resid
+ zero_cnt
+ zero_cnt1
)) && retval
== 0) {
1975 * for this iteration of the loop, figure out where our starting point is
1978 start_offset
= (int)(zero_off
& PAGE_MASK_64
);
1979 upl_f_offset
= zero_off
- start_offset
;
1980 } else if (io_resid
) {
1981 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
1982 upl_f_offset
= uio
->uio_offset
- start_offset
;
1984 start_offset
= (int)(zero_off1
& PAGE_MASK_64
);
1985 upl_f_offset
= zero_off1
- start_offset
;
1987 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 46)) | DBG_FUNC_NONE
,
1988 (int)zero_off
, (int)zero_cnt
, (int)zero_off1
, (int)zero_cnt1
, 0);
1990 if (total_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
1991 total_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
1993 cl
.b_addr
= (daddr64_t
)(upl_f_offset
/ PAGE_SIZE_64
);
1995 if (uio
&& ((flags
& (IO_NOCACHE
| IO_SYNC
| IO_HEADZEROFILL
| IO_TAILZEROFILL
)) == 0)) {
1997 * assumption... total_size <= io_resid
1998 * because IO_HEADZEROFILL and IO_TAILZEROFILL not set
2000 if ((start_offset
+ total_size
) > (MAX_UPL_TRANSFER
* PAGE_SIZE
))
2001 total_size
-= start_offset
;
2002 xfer_resid
= total_size
;
2004 retval
= cluster_copy_ubc_data(vp
, uio
, &xfer_resid
, 1);
2009 io_resid
-= (total_size
- xfer_resid
);
2010 total_size
= xfer_resid
;
2011 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2012 upl_f_offset
= uio
->uio_offset
- start_offset
;
2014 if (total_size
== 0) {
2017 * the write did not finish on a page boundary
2018 * which will leave upl_f_offset pointing to the
2019 * beginning of the last page written instead of
2020 * the page beyond it... bump it in this case
2021 * so that the cluster code records the last page
2024 upl_f_offset
+= PAGE_SIZE_64
;
2032 * compute the size of the upl needed to encompass
2033 * the requested write... limit each call to cluster_io
2034 * to the maximum UPL size... cluster_io will clip if
2035 * this exceeds the maximum io_size for the device,
2036 * make sure to account for
2037 * a starting offset that's not page aligned
2039 upl_size
= (start_offset
+ total_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2041 if (upl_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
2042 upl_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
2044 pages_in_upl
= upl_size
/ PAGE_SIZE
;
2045 io_size
= upl_size
- start_offset
;
2047 if ((long long)io_size
> total_size
)
2048 io_size
= total_size
;
2050 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, io_size
, total_size
, 0, 0);
2054 * Gather the pages from the buffer cache.
2055 * The UPL_WILL_MODIFY flag lets the UPL subsystem know
2056 * that we intend to modify these pages.
2058 kret
= ubc_create_upl(vp
,
2063 UPL_SET_LITE
| UPL_WILL_MODIFY
);
2064 if (kret
!= KERN_SUCCESS
)
2065 panic("cluster_write: failed to get pagelist");
2067 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
,
2068 (int)upl
, (int)upl_f_offset
, start_offset
, 0, 0);
2070 if (start_offset
&& !upl_valid_page(pl
, 0)) {
2074 * we're starting in the middle of the first page of the upl
2075 * and the page isn't currently valid, so we're going to have
2076 * to read it in first... this is a synchronous operation
2078 read_size
= PAGE_SIZE
;
2080 if ((upl_f_offset
+ read_size
) > newEOF
)
2081 read_size
= newEOF
- upl_f_offset
;
2083 retval
= cluster_io(vp
, upl
, 0, upl_f_offset
, read_size
,
2084 CL_READ
, (buf_t
)NULL
, (struct clios
*)NULL
);
2087 * we had an error during the read which causes us to abort
2088 * the current cluster_write request... before we do, we need
2089 * to release the rest of the pages in the upl without modifying
2090 * there state and mark the failed page in error
2092 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
);
2094 if (upl_size
> PAGE_SIZE
)
2095 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2097 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2098 (int)upl
, 0, 0, retval
, 0);
2102 if ((start_offset
== 0 || upl_size
> PAGE_SIZE
) && ((start_offset
+ io_size
) & PAGE_MASK
)) {
2104 * the last offset we're writing to in this upl does not end on a page
2105 * boundary... if it's not beyond the old EOF, then we'll also need to
2106 * pre-read this page in if it isn't already valid
2108 upl_offset
= upl_size
- PAGE_SIZE
;
2110 if ((upl_f_offset
+ start_offset
+ io_size
) < oldEOF
&&
2111 !upl_valid_page(pl
, upl_offset
/ PAGE_SIZE
)) {
2114 read_size
= PAGE_SIZE
;
2116 if ((upl_f_offset
+ upl_offset
+ read_size
) > newEOF
)
2117 read_size
= newEOF
- (upl_f_offset
+ upl_offset
);
2119 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, read_size
,
2120 CL_READ
, (buf_t
)NULL
, (struct clios
*)NULL
);
2123 * we had an error during the read which causes us to abort
2124 * the current cluster_write request... before we do, we
2125 * need to release the rest of the pages in the upl without
2126 * modifying there state and mark the failed page in error
2128 ubc_upl_abort_range(upl
, upl_offset
, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
);
2130 if (upl_size
> PAGE_SIZE
)
2131 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2133 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2134 (int)upl
, 0, 0, retval
, 0);
2139 xfer_resid
= io_size
;
2140 io_offset
= start_offset
;
2142 while (zero_cnt
&& xfer_resid
) {
2144 if (zero_cnt
< (long long)xfer_resid
)
2145 bytes_to_zero
= zero_cnt
;
2147 bytes_to_zero
= xfer_resid
;
2149 if ( !(flags
& (IO_NOZEROVALID
| IO_NOZERODIRTY
))) {
2150 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2154 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off
& PAGE_MASK_64
));
2155 zero_pg_index
= (int)((zero_off
- upl_f_offset
) / PAGE_SIZE_64
);
2157 if ( !upl_valid_page(pl
, zero_pg_index
)) {
2158 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2160 } else if ((flags
& (IO_NOZERODIRTY
| IO_NOZEROVALID
)) == IO_NOZERODIRTY
&&
2161 !upl_dirty_page(pl
, zero_pg_index
)) {
2162 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2165 xfer_resid
-= bytes_to_zero
;
2166 zero_cnt
-= bytes_to_zero
;
2167 zero_off
+= bytes_to_zero
;
2168 io_offset
+= bytes_to_zero
;
2170 if (xfer_resid
&& io_resid
) {
2171 bytes_to_move
= min(io_resid
, xfer_resid
);
2173 retval
= cluster_copy_upl_data(uio
, upl
, io_offset
, bytes_to_move
);
2177 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
2179 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2180 (int)upl
, 0, 0, retval
, 0);
2182 io_resid
-= bytes_to_move
;
2183 xfer_resid
-= bytes_to_move
;
2184 io_offset
+= bytes_to_move
;
2187 while (xfer_resid
&& zero_cnt1
&& retval
== 0) {
2189 if (zero_cnt1
< (long long)xfer_resid
)
2190 bytes_to_zero
= zero_cnt1
;
2192 bytes_to_zero
= xfer_resid
;
2194 if ( !(flags
& (IO_NOZEROVALID
| IO_NOZERODIRTY
))) {
2195 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2199 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off1
& PAGE_MASK_64
));
2200 zero_pg_index
= (int)((zero_off1
- upl_f_offset
) / PAGE_SIZE_64
);
2202 if ( !upl_valid_page(pl
, zero_pg_index
)) {
2203 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2204 } else if ((flags
& (IO_NOZERODIRTY
| IO_NOZEROVALID
)) == IO_NOZERODIRTY
&&
2205 !upl_dirty_page(pl
, zero_pg_index
)) {
2206 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2209 xfer_resid
-= bytes_to_zero
;
2210 zero_cnt1
-= bytes_to_zero
;
2211 zero_off1
+= bytes_to_zero
;
2212 io_offset
+= bytes_to_zero
;
2219 io_size
+= start_offset
;
2221 if ((upl_f_offset
+ io_size
) >= newEOF
&& io_size
< upl_size
) {
2223 * if we're extending the file with this write
2224 * we'll zero fill the rest of the page so that
2225 * if the file gets extended again in such a way as to leave a
2226 * hole starting at this EOF, we'll have zero's in the correct spot
2228 cluster_zero(upl
, io_size
, upl_size
- io_size
, NULL
);
2230 if (flags
& IO_SYNC
)
2232 * if the IO_SYNC flag is set than we need to
2233 * bypass any clusters and immediately issue
2239 * take the lock to protect our accesses
2240 * of the writebehind and sparse cluster state
2242 wbp
= cluster_get_wbp(vp
, CLW_ALLOCATE
| CLW_RETURNLOCKED
);
2245 * calculate the last logical block number
2246 * that this delayed I/O encompassed
2248 cl
.e_addr
= (daddr64_t
)((upl_f_offset
+ (off_t
)upl_size
) / PAGE_SIZE_64
);
2250 if (wbp
->cl_scmap
) {
2252 if ( !(flags
& IO_NOCACHE
)) {
2254 * we've fallen into the sparse
2255 * cluster method of delaying dirty pages
2256 * first, we need to release the upl if we hold one
2257 * since pages in it may be present in the sparse cluster map
2258 * and may span 2 separate buckets there... if they do and
2259 * we happen to have to flush a bucket to make room and it intersects
2260 * this upl, a deadlock may result on page BUSY
2263 ubc_upl_commit_range(upl
, 0, upl_size
,
2264 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2266 sparse_cluster_add(wbp
, vp
, &cl
, newEOF
);
2268 lck_mtx_unlock(&wbp
->cl_lockw
);
2273 * must have done cached writes that fell into
2274 * the sparse cluster mechanism... we've switched
2275 * to uncached writes on the file, so go ahead
2276 * and push whatever's in the sparse map
2277 * and switch back to normal clustering
2279 * see the comment above concerning a possible deadlock...
2282 ubc_upl_commit_range(upl
, 0, upl_size
,
2283 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2285 * setting upl_size to 0 keeps us from committing a
2286 * second time in the start_new_cluster path
2290 sparse_cluster_push(wbp
, vp
, newEOF
, 1);
2294 * no clusters of either type present at this point
2295 * so just go directly to start_new_cluster since
2296 * we know we need to delay this I/O since we've
2297 * already released the pages back into the cache
2298 * to avoid the deadlock with sparse_cluster_push
2300 goto start_new_cluster
;
2304 if (wbp
->cl_number
== 0)
2306 * no clusters currently present
2308 goto start_new_cluster
;
2310 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
2312 * check each cluster that we currently hold
2313 * try to merge some or all of this write into
2314 * one or more of the existing clusters... if
2315 * any portion of the write remains, start a
2318 if (cl
.b_addr
>= wbp
->cl_clusters
[cl_index
].b_addr
) {
2320 * the current write starts at or after the current cluster
2322 if (cl
.e_addr
<= (wbp
->cl_clusters
[cl_index
].b_addr
+ MAX_UPL_TRANSFER
)) {
2324 * we have a write that fits entirely
2325 * within the existing cluster limits
2327 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
)
2329 * update our idea of where the cluster ends
2331 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
2334 if (cl
.b_addr
< (wbp
->cl_clusters
[cl_index
].b_addr
+ MAX_UPL_TRANSFER
)) {
2336 * we have a write that starts in the middle of the current cluster
2337 * but extends beyond the cluster's limit... we know this because
2338 * of the previous checks
2339 * we'll extend the current cluster to the max
2340 * and update the b_addr for the current write to reflect that
2341 * the head of it was absorbed into this cluster...
2342 * note that we'll always have a leftover tail in this case since
2343 * full absorbtion would have occurred in the clause above
2345 wbp
->cl_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
+ MAX_UPL_TRANSFER
;
2348 daddr64_t start_pg_in_upl
;
2350 start_pg_in_upl
= (daddr64_t
)(upl_f_offset
/ PAGE_SIZE_64
);
2352 if (start_pg_in_upl
< wbp
->cl_clusters
[cl_index
].e_addr
) {
2353 intersection
= (int)((wbp
->cl_clusters
[cl_index
].e_addr
- start_pg_in_upl
) * PAGE_SIZE
);
2355 ubc_upl_commit_range(upl
, upl_offset
, intersection
,
2356 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2357 upl_f_offset
+= intersection
;
2358 upl_offset
+= intersection
;
2359 upl_size
-= intersection
;
2362 cl
.b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
;
2365 * we come here for the case where the current write starts
2366 * beyond the limit of the existing cluster or we have a leftover
2367 * tail after a partial absorbtion
2369 * in either case, we'll check the remaining clusters before
2370 * starting a new one
2374 * the current write starts in front of the cluster we're currently considering
2376 if ((wbp
->cl_clusters
[cl_index
].e_addr
- cl
.b_addr
) <= MAX_UPL_TRANSFER
) {
2378 * we can just merge the new request into
2379 * this cluster and leave it in the cache
2380 * since the resulting cluster is still
2381 * less than the maximum allowable size
2383 wbp
->cl_clusters
[cl_index
].b_addr
= cl
.b_addr
;
2385 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
) {
2387 * the current write completely
2388 * envelops the existing cluster and since
2389 * each write is limited to at most MAX_UPL_TRANSFER bytes
2390 * we can just use the start and last blocknos of the write
2391 * to generate the cluster limits
2393 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
2399 * if we were to combine this write with the current cluster
2400 * we would exceed the cluster size limit.... so,
2401 * let's see if there's any overlap of the new I/O with
2402 * the cluster we're currently considering... in fact, we'll
2403 * stretch the cluster out to it's full limit and see if we
2404 * get an intersection with the current write
2407 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
- MAX_UPL_TRANSFER
) {
2409 * the current write extends into the proposed cluster
2410 * clip the length of the current write after first combining it's
2411 * tail with the newly shaped cluster
2413 wbp
->cl_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
- MAX_UPL_TRANSFER
;
2416 intersection
= (int)((cl
.e_addr
- wbp
->cl_clusters
[cl_index
].b_addr
) * PAGE_SIZE
);
2418 if (intersection
> upl_size
)
2420 * because the current write may consist of a number of pages found in the cache
2421 * which are not part of the UPL, we may have an intersection that exceeds
2422 * the size of the UPL that is also part of this write
2424 intersection
= upl_size
;
2426 ubc_upl_commit_range(upl
, upl_offset
+ (upl_size
- intersection
), intersection
,
2427 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2428 upl_size
-= intersection
;
2430 cl
.e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
;
2433 * if we get here, there was no way to merge
2434 * any portion of this write with this cluster
2435 * or we could only merge part of it which
2436 * will leave a tail...
2437 * we'll check the remaining clusters before starting a new one
2441 if (cl_index
< wbp
->cl_number
)
2443 * we found an existing cluster(s) that we
2444 * could entirely merge this I/O into
2448 if (wbp
->cl_number
< MAX_CLUSTERS
&& !(flags
& IO_NOCACHE
))
2450 * we didn't find an existing cluster to
2451 * merge into, but there's room to start
2454 goto start_new_cluster
;
2457 * no exisitng cluster to merge with and no
2458 * room to start a new one... we'll try
2459 * pushing one of the existing ones... if none of
2460 * them are able to be pushed, we'll switch
2461 * to the sparse cluster mechanism
2462 * cluster_try_push updates cl_number to the
2463 * number of remaining clusters... and
2464 * returns the number of currently unused clusters
2466 int ret_cluster_try_push
= 0;
2467 /* if writes are not deferred, call cluster push immediately */
2468 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
)) {
2469 if (flags
& IO_NOCACHE
)
2474 ret_cluster_try_push
= cluster_try_push(wbp
, vp
, newEOF
, can_delay
, 0);
2477 /* execute following regardless writes are deferred or not */
2478 if (ret_cluster_try_push
== 0) {
2480 * no more room in the normal cluster mechanism
2481 * so let's switch to the more expansive but expensive
2482 * sparse mechanism....
2483 * first, we need to release the upl if we hold one
2484 * since pages in it may be present in the sparse cluster map (after the cluster_switch)
2485 * and may span 2 separate buckets there... if they do and
2486 * we happen to have to flush a bucket to make room and it intersects
2487 * this upl, a deadlock may result on page BUSY
2490 ubc_upl_commit_range(upl
, upl_offset
, upl_size
,
2491 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2493 sparse_cluster_switch(wbp
, vp
, newEOF
);
2494 sparse_cluster_add(wbp
, vp
, &cl
, newEOF
);
2496 lck_mtx_unlock(&wbp
->cl_lockw
);
2501 * we pushed one cluster successfully, so we must be sequentially writing this file
2502 * otherwise, we would have failed and fallen into the sparse cluster support
2503 * so let's take the opportunity to push out additional clusters as long as we
2504 * remain below the throttle... this will give us better I/O locality if we're
2505 * in a copy loop (i.e. we won't jump back and forth between the read and write points
2506 * however, we don't want to push so much out that the write throttle kicks in and
2507 * hangs this thread up until some of the I/O completes...
2509 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
)) {
2510 while (wbp
->cl_number
&& (vp
->v_numoutput
<= (VNODE_ASYNC_THROTTLE
/ 2)))
2511 cluster_try_push(wbp
, vp
, newEOF
, 0, 0);
2515 wbp
->cl_clusters
[wbp
->cl_number
].b_addr
= cl
.b_addr
;
2516 wbp
->cl_clusters
[wbp
->cl_number
].e_addr
= cl
.e_addr
;
2518 if (flags
& IO_NOCACHE
)
2519 wbp
->cl_clusters
[wbp
->cl_number
].io_nocache
= 1;
2521 wbp
->cl_clusters
[wbp
->cl_number
].io_nocache
= 0;
2525 ubc_upl_commit_range(upl
, upl_offset
, upl_size
,
2526 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2528 lck_mtx_unlock(&wbp
->cl_lockw
);
2533 * we don't hold the vnode lock at this point
2535 * because we had to ask for a UPL that provides currenty non-present pages, the
2536 * UPL has been automatically set to clear the dirty flags (both software and hardware)
2537 * upon committing it... this is not the behavior we want since it's possible for
2538 * pages currently present as part of a mapped file to be dirtied while the I/O is in flight.
2539 * in order to maintain some semblance of coherency with mapped writes
2540 * we need to drop the current upl and pick it back up with COPYOUT_FROM set
2541 * so that we correctly deal with a change in state of the hardware modify bit...
2542 * we do this via cluster_push_x... by passing along the IO_SYNC flag, we force
2543 * cluster_push_x to wait until all the I/Os have completed... cluster_push_x is also
2544 * responsible for generating the correct sized I/O(s)
2546 ubc_upl_commit_range(upl
, 0, upl_size
,
2547 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2549 cl
.e_addr
= (upl_f_offset
+ (off_t
)upl_size
) / PAGE_SIZE_64
;
2551 retval
= cluster_push_x(vp
, &cl
, newEOF
, flags
);
2554 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
2555 retval
, 0, io_resid
, 0, 0);
2561 cluster_read(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
)
2574 if (vp
->v_flag
& VNOCACHE_DATA
)
2575 flags
|= IO_NOCACHE
;
2576 if (vp
->v_flag
& VRAOFF
)
2579 if (!((flags
& IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))) {
2581 * go do a read through the cache if one of the following is true....
2582 * NOCACHE is not true
2583 * the uio request doesn't target USERSPACE
2585 return (cluster_read_x(vp
, uio
, filesize
, flags
));
2589 if (IS_VALID_UIO_SEGFLG(uio
->uio_segflg
) == 0) {
2590 panic("%s :%d - invalid uio_segflg\n", __FILE__
, __LINE__
);
2592 #endif /* LP64_DEBUG */
2594 while (uio_resid(uio
) && uio
->uio_offset
< filesize
&& retval
== 0) {
2595 user_size_t iov_len
;
2596 user_addr_t iov_base
;
2599 * we know we have a resid, so this is safe
2600 * skip over any emtpy vectors
2602 uio_update(uio
, (user_size_t
)0);
2604 iov_len
= uio_curriovlen(uio
);
2605 iov_base
= uio_curriovbase(uio
);
2607 upl_size
= PAGE_SIZE
;
2608 upl_flags
= UPL_QUERY_OBJECT_TYPE
;
2610 // LP64todo - fix this!
2611 if ((vm_map_get_upl(current_map(),
2612 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2613 &upl_size
, &upl
, NULL
, NULL
, &upl_flags
, 0)) != KERN_SUCCESS
) {
2615 * the user app must have passed in an invalid address
2621 * We check every vector target but if it is physically
2622 * contiguous space, we skip the sanity checks.
2624 if (upl_flags
& UPL_PHYS_CONTIG
) {
2625 retval
= cluster_phys_read(vp
, uio
, filesize
);
2627 else if (uio_resid(uio
) < PAGE_SIZE
) {
2629 * we're here because we're don't have a physically contiguous target buffer
2630 * go do a read through the cache if
2631 * the total xfer size is less than a page...
2633 return (cluster_read_x(vp
, uio
, filesize
, flags
));
2635 // LP64todo - fix this!
2636 else if (((int)uio
->uio_offset
& PAGE_MASK
) || (CAST_DOWN(int, iov_base
) & PAGE_MASK
)) {
2637 if (((int)uio
->uio_offset
& PAGE_MASK
) == (CAST_DOWN(int, iov_base
) & PAGE_MASK
)) {
2639 * Bring the file offset read up to a pagesize boundary
2640 * this will also bring the base address to a page boundary
2641 * since they both are currently on the same offset within a page
2642 * note: if we get here, uio->uio_resid is greater than PAGE_SIZE
2643 * so the computed clip_size must always be less than the current uio_resid
2645 clip_size
= (PAGE_SIZE
- (int)(uio
->uio_offset
& PAGE_MASK_64
));
2648 * Fake the resid going into the cluster_read_x call
2649 * and restore it on the way out.
2651 prev_resid
= uio_resid(uio
);
2652 // LP64todo - fix this
2653 uio_setresid(uio
, clip_size
);
2655 retval
= cluster_read_x(vp
, uio
, filesize
, flags
);
2657 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
2660 * can't get both the file offset and the buffer offset aligned to a page boundary
2661 * so fire an I/O through the cache for this entire vector
2663 // LP64todo - fix this!
2664 clip_size
= iov_len
;
2665 prev_resid
= uio_resid(uio
);
2666 uio_setresid(uio
, clip_size
);
2668 retval
= cluster_read_x(vp
, uio
, filesize
, flags
);
2670 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
2674 * If we come in here, we know the offset into
2675 * the file is on a pagesize boundary
2677 max_io_size
= filesize
- uio
->uio_offset
;
2678 // LP64todo - fix this
2679 clip_size
= uio_resid(uio
);
2680 if (iov_len
< clip_size
)
2681 clip_size
= iov_len
;
2682 if (max_io_size
< clip_size
)
2683 clip_size
= (int)max_io_size
;
2685 if (clip_size
< PAGE_SIZE
) {
2687 * Take care of the tail end of the read in this vector.
2689 // LP64todo - fix this
2690 prev_resid
= uio_resid(uio
);
2691 uio_setresid(uio
, clip_size
);
2693 retval
= cluster_read_x(vp
, uio
, filesize
, flags
);
2695 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
2697 /* round clip_size down to a multiple of pagesize */
2698 clip_size
= clip_size
& ~(PAGE_MASK
);
2699 // LP64todo - fix this
2700 prev_resid
= uio_resid(uio
);
2701 uio_setresid(uio
, clip_size
);
2703 retval
= cluster_nocopy_read(vp
, uio
, filesize
);
2705 if ((retval
==0) && uio_resid(uio
))
2706 retval
= cluster_read_x(vp
, uio
, filesize
, flags
);
2708 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
2717 cluster_read_x(vnode_t vp
, struct uio
*uio
, off_t filesize
, int flags
)
2719 upl_page_info_t
*pl
;
2721 vm_offset_t upl_offset
;
2730 off_t last_ioread_offset
;
2731 off_t last_request_offset
;
2732 u_int size_of_prefetch
;
2737 u_int max_rd_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
2738 u_int rd_ahead_enabled
= 1;
2739 u_int prefetch_enabled
= 1;
2740 struct cl_readahead
* rap
;
2741 struct clios iostate
;
2742 struct cl_extent extent
;
2744 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_START
,
2745 (int)uio
->uio_offset
, uio_resid(uio
), (int)filesize
, 0, 0);
2747 // LP64todo - fix this
2748 last_request_offset
= uio
->uio_offset
+ uio_resid(uio
);
2750 if ((flags
& (IO_RAOFF
|IO_NOCACHE
)) ||
2751 ((last_request_offset
& ~PAGE_MASK_64
) == (uio
->uio_offset
& ~PAGE_MASK_64
))) {
2752 rd_ahead_enabled
= 0;
2755 if (cluster_hard_throttle_on(vp
)) {
2756 rd_ahead_enabled
= 0;
2757 prefetch_enabled
= 0;
2759 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
2761 if ((rap
= cluster_get_rap(vp
)) == NULL
)
2762 rd_ahead_enabled
= 0;
2764 if (last_request_offset
> filesize
)
2765 last_request_offset
= filesize
;
2766 extent
.b_addr
= uio
->uio_offset
/ PAGE_SIZE_64
;
2767 extent
.e_addr
= (last_request_offset
- 1) / PAGE_SIZE_64
;
2769 if (rap
!= NULL
&& rap
->cl_ralen
&& (rap
->cl_lastr
== extent
.b_addr
|| (rap
->cl_lastr
+ 1) == extent
.b_addr
)) {
2771 * determine if we already have a read-ahead in the pipe courtesy of the
2772 * last read systemcall that was issued...
2773 * if so, pick up it's extent to determine where we should start
2774 * with respect to any read-ahead that might be necessary to
2775 * garner all the data needed to complete this read systemcall
2777 last_ioread_offset
= (rap
->cl_maxra
* PAGE_SIZE_64
) + PAGE_SIZE_64
;
2779 if (last_ioread_offset
< uio
->uio_offset
)
2780 last_ioread_offset
= (off_t
)0;
2781 else if (last_ioread_offset
> last_request_offset
)
2782 last_ioread_offset
= last_request_offset
;
2784 last_ioread_offset
= (off_t
)0;
2786 while (uio_resid(uio
) && uio
->uio_offset
< filesize
&& retval
== 0) {
2788 * compute the size of the upl needed to encompass
2789 * the requested read... limit each call to cluster_io
2790 * to the maximum UPL size... cluster_io will clip if
2791 * this exceeds the maximum io_size for the device,
2792 * make sure to account for
2793 * a starting offset that's not page aligned
2795 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2796 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
2797 max_size
= filesize
- uio
->uio_offset
;
2799 // LP64todo - fix this!
2800 if ((off_t
)((unsigned int)uio_resid(uio
)) < max_size
)
2801 io_size
= uio_resid(uio
);
2805 if (!(flags
& IO_NOCACHE
)) {
2812 * if we keep finding the pages we need already in the cache, then
2813 * don't bother to call cluster_rd_prefetch since it costs CPU cycles
2814 * to determine that we have all the pages we need... once we miss in
2815 * the cache and have issued an I/O, than we'll assume that we're likely
2816 * to continue to miss in the cache and it's to our advantage to try and prefetch
2818 if (last_request_offset
&& last_ioread_offset
&& (size_of_prefetch
= (last_request_offset
- last_ioread_offset
))) {
2819 if ((last_ioread_offset
- uio
->uio_offset
) <= max_rd_size
&& prefetch_enabled
) {
2821 * we've already issued I/O for this request and
2822 * there's still work to do and
2823 * our prefetch stream is running dry, so issue a
2824 * pre-fetch I/O... the I/O latency will overlap
2825 * with the copying of the data
2827 if (size_of_prefetch
> max_rd_size
)
2828 size_of_prefetch
= max_rd_size
;
2830 size_of_prefetch
= cluster_rd_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
);
2832 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
2834 if (last_ioread_offset
> last_request_offset
)
2835 last_ioread_offset
= last_request_offset
;
2839 * limit the size of the copy we're about to do so that
2840 * we can notice that our I/O pipe is running dry and
2841 * get the next I/O issued before it does go dry
2843 if (last_ioread_offset
&& io_size
> ((MAX_UPL_TRANSFER
* PAGE_SIZE
) / 4))
2844 io_resid
= ((MAX_UPL_TRANSFER
* PAGE_SIZE
) / 4);
2848 io_requested
= io_resid
;
2850 retval
= cluster_copy_ubc_data(vp
, uio
, &io_resid
, 0);
2852 io_size
-= (io_requested
- io_resid
);
2854 if (retval
|| io_resid
)
2856 * if we run into a real error or
2857 * a page that is not in the cache
2858 * we need to leave streaming mode
2862 if ((io_size
== 0 || last_ioread_offset
== last_request_offset
) && rd_ahead_enabled
) {
2864 * we're already finished the I/O for this read request
2865 * let's see if we should do a read-ahead
2867 cluster_rd_ahead(vp
, &extent
, filesize
, rap
);
2874 if (extent
.e_addr
< rap
->cl_lastr
)
2876 rap
->cl_lastr
= extent
.e_addr
;
2880 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2881 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
2882 max_size
= filesize
- uio
->uio_offset
;
2884 if (io_size
> max_rd_size
)
2885 io_size
= max_rd_size
;
2887 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2889 if (upl_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
) / 4)
2890 upl_size
= (MAX_UPL_TRANSFER
* PAGE_SIZE
) / 4;
2891 pages_in_upl
= upl_size
/ PAGE_SIZE
;
2893 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_START
,
2894 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
2896 kret
= ubc_create_upl(vp
,
2902 if (kret
!= KERN_SUCCESS
)
2903 panic("cluster_read: failed to get pagelist");
2905 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_END
,
2906 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
2909 * scan from the beginning of the upl looking for the first
2910 * non-valid page.... this will become the first page in
2911 * the request we're going to make to 'cluster_io'... if all
2912 * of the pages are valid, we won't call through to 'cluster_io'
2914 for (start_pg
= 0; start_pg
< pages_in_upl
; start_pg
++) {
2915 if (!upl_valid_page(pl
, start_pg
))
2920 * scan from the starting invalid page looking for a valid
2921 * page before the end of the upl is reached, if we
2922 * find one, then it will be the last page of the request to
2925 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
2926 if (upl_valid_page(pl
, last_pg
))
2929 iostate
.io_completed
= 0;
2930 iostate
.io_issued
= 0;
2931 iostate
.io_error
= 0;
2932 iostate
.io_wanted
= 0;
2934 if (start_pg
< last_pg
) {
2936 * we found a range of 'invalid' pages that must be filled
2937 * if the last page in this range is the last page of the file
2938 * we may have to clip the size of it to keep from reading past
2939 * the end of the last physical block associated with the file
2941 upl_offset
= start_pg
* PAGE_SIZE
;
2942 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
2944 if ((upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
2945 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
2948 * issue an asynchronous read to cluster_io
2951 error
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
,
2952 io_size
, CL_READ
| CL_ASYNC
, (buf_t
)NULL
, &iostate
);
2956 * if the read completed successfully, or there was no I/O request
2957 * issued, than copy the data into user land via 'cluster_upl_copy_data'
2958 * we'll first add on any 'valid'
2959 * pages that were present in the upl when we acquired it.
2963 for (uio_last
= last_pg
; uio_last
< pages_in_upl
; uio_last
++) {
2964 if (!upl_valid_page(pl
, uio_last
))
2968 * compute size to transfer this round, if uio->uio_resid is
2969 * still non-zero after this attempt, we'll loop around and
2970 * set up for another I/O.
2972 val_size
= (uio_last
* PAGE_SIZE
) - start_offset
;
2974 if (val_size
> max_size
)
2975 val_size
= max_size
;
2977 if (val_size
> uio_resid(uio
))
2978 // LP64todo - fix this
2979 val_size
= uio_resid(uio
);
2981 if (last_ioread_offset
== 0)
2982 last_ioread_offset
= uio
->uio_offset
+ val_size
;
2984 if ((size_of_prefetch
= (last_request_offset
- last_ioread_offset
)) && prefetch_enabled
) {
2986 * if there's still I/O left to do for this request, and...
2987 * we're not in hard throttle mode, then issue a
2988 * pre-fetch I/O... the I/O latency will overlap
2989 * with the copying of the data
2991 size_of_prefetch
= cluster_rd_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
);
2993 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
2995 if (last_ioread_offset
> last_request_offset
)
2996 last_ioread_offset
= last_request_offset
;
2998 } else if ((uio
->uio_offset
+ val_size
) == last_request_offset
) {
3000 * this transfer will finish this request, so...
3001 * let's try to read ahead if we're in
3002 * a sequential access pattern and we haven't
3003 * explicitly disabled it
3005 if (rd_ahead_enabled
)
3006 cluster_rd_ahead(vp
, &extent
, filesize
, rap
);
3009 if (extent
.e_addr
< rap
->cl_lastr
)
3011 rap
->cl_lastr
= extent
.e_addr
;
3014 lck_mtx_lock(cl_mtxp
);
3016 while (iostate
.io_issued
!= iostate
.io_completed
) {
3017 iostate
.io_wanted
= 1;
3018 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_read_x", 0);
3020 lck_mtx_unlock(cl_mtxp
);
3022 if (iostate
.io_error
)
3023 error
= iostate
.io_error
;
3025 retval
= cluster_copy_upl_data(uio
, upl
, start_offset
, val_size
);
3027 if (start_pg
< last_pg
) {
3029 * compute the range of pages that we actually issued an I/O for
3030 * and either commit them as valid if the I/O succeeded
3031 * or abort them if the I/O failed
3033 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
3035 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
3036 (int)upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
3038 if (error
|| (flags
& IO_NOCACHE
))
3039 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
3040 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3042 ubc_upl_commit_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
3043 UPL_COMMIT_CLEAR_DIRTY
|
3044 UPL_COMMIT_FREE_ON_EMPTY
|
3045 UPL_COMMIT_INACTIVATE
);
3047 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
,
3048 (int)upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
3050 if ((last_pg
- start_pg
) < pages_in_upl
) {
3055 * the set of pages that we issued an I/O for did not encompass
3056 * the entire upl... so just release these without modifying
3060 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3062 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
3063 (int)upl
, -1, pages_in_upl
- (last_pg
- start_pg
), 0, 0);
3067 * we found some already valid pages at the beginning of
3068 * the upl commit these back to the inactive list with
3071 for (cur_pg
= 0; cur_pg
< start_pg
; cur_pg
++) {
3072 commit_flags
= UPL_COMMIT_FREE_ON_EMPTY
3073 | UPL_COMMIT_INACTIVATE
;
3075 if (upl_dirty_page(pl
, cur_pg
))
3076 commit_flags
|= UPL_COMMIT_SET_DIRTY
;
3078 if ( !(commit_flags
& UPL_COMMIT_SET_DIRTY
) && (flags
& IO_NOCACHE
))
3079 ubc_upl_abort_range(upl
, cur_pg
* PAGE_SIZE
, PAGE_SIZE
,
3080 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3082 ubc_upl_commit_range(upl
, cur_pg
* PAGE_SIZE
,
3083 PAGE_SIZE
, commit_flags
);
3086 if (last_pg
< uio_last
) {
3088 * we found some already valid pages immediately after the
3089 * pages we issued I/O for, commit these back to the
3090 * inactive list with reference cleared
3092 for (cur_pg
= last_pg
; cur_pg
< uio_last
; cur_pg
++) {
3093 commit_flags
= UPL_COMMIT_FREE_ON_EMPTY
3094 | UPL_COMMIT_INACTIVATE
;
3096 if (upl_dirty_page(pl
, cur_pg
))
3097 commit_flags
|= UPL_COMMIT_SET_DIRTY
;
3099 if ( !(commit_flags
& UPL_COMMIT_SET_DIRTY
) && (flags
& IO_NOCACHE
))
3100 ubc_upl_abort_range(upl
, cur_pg
* PAGE_SIZE
, PAGE_SIZE
,
3101 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3103 ubc_upl_commit_range(upl
, cur_pg
* PAGE_SIZE
,
3104 PAGE_SIZE
, commit_flags
);
3107 if (uio_last
< pages_in_upl
) {
3109 * there were some invalid pages beyond the valid pages
3110 * that we didn't issue an I/O for, just release them
3113 ubc_upl_abort_range(upl
, uio_last
* PAGE_SIZE
,
3114 (pages_in_upl
- uio_last
) * PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
3117 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
,
3118 (int)upl
, -1, -1, 0, 0);
3124 if ( uio_resid(uio
) ) {
3125 if (cluster_hard_throttle_on(vp
)) {
3126 rd_ahead_enabled
= 0;
3127 prefetch_enabled
= 0;
3129 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
3132 rd_ahead_enabled
= 1;
3133 prefetch_enabled
= 1;
3135 max_rd_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
3140 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
3141 (int)uio
->uio_offset
, uio_resid(uio
), rap
->cl_lastr
, retval
, 0);
3143 lck_mtx_unlock(&rap
->cl_lockr
);
3145 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
3146 (int)uio
->uio_offset
, uio_resid(uio
), 0, retval
, 0);
3154 cluster_nocopy_read(vnode_t vp
, struct uio
*uio
, off_t filesize
)
3157 upl_page_info_t
*pl
;
3158 vm_offset_t upl_offset
;
3162 int upl_needed_size
;
3167 int force_data_sync
;
3169 int no_zero_fill
= 0;
3171 struct clios iostate
;
3172 u_int max_rd_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
3173 u_int max_rd_ahead
= MAX_UPL_TRANSFER
* PAGE_SIZE
* 2;
3176 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_START
,
3177 (int)uio
->uio_offset
, uio_resid(uio
), (int)filesize
, 0, 0);
3180 * When we enter this routine, we know
3181 * -- the offset into the file is on a pagesize boundary
3182 * -- the resid is a page multiple
3183 * -- the resid will not exceed iov_len
3186 iostate
.io_completed
= 0;
3187 iostate
.io_issued
= 0;
3188 iostate
.io_error
= 0;
3189 iostate
.io_wanted
= 0;
3191 while (uio_resid(uio
) && uio
->uio_offset
< filesize
&& retval
== 0) {
3192 user_addr_t iov_base
;
3194 if (cluster_hard_throttle_on(vp
)) {
3195 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
3196 max_rd_ahead
= HARD_THROTTLE_MAXSIZE
- 1;
3198 max_rd_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
3199 max_rd_ahead
= MAX_UPL_TRANSFER
* PAGE_SIZE
* 8;
3201 max_io_size
= filesize
- uio
->uio_offset
;
3203 // LP64todo - fix this
3204 if (max_io_size
< (off_t
)((unsigned int)uio_resid(uio
)))
3205 io_size
= max_io_size
;
3207 io_size
= uio_resid(uio
);
3210 * First look for pages already in the cache
3211 * and move them to user space.
3213 retval
= cluster_copy_ubc_data(vp
, uio
, &io_size
, 0);
3217 * we may have already spun some portion of this request
3218 * off as async requests... we need to wait for the I/O
3219 * to complete before returning
3221 goto wait_for_reads
;
3224 * If we are already finished with this read, then return
3228 * we may have already spun some portion of this request
3229 * off as async requests... we need to wait for the I/O
3230 * to complete before returning
3232 goto wait_for_reads
;
3234 max_io_size
= io_size
;
3236 if (max_io_size
> max_rd_size
)
3237 max_io_size
= max_rd_size
;
3241 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ max_io_size
, UPL_ROP_ABSENT
, &io_size
);
3245 * we may have already spun some portion of this request
3246 * off as async requests... we need to wait for the I/O
3247 * to complete before returning
3249 goto wait_for_reads
;
3251 iov_base
= uio_curriovbase(uio
);
3253 // LP64todo - fix this!
3254 upl_offset
= CAST_DOWN(vm_offset_t
, iov_base
) & PAGE_MASK
;
3255 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
3257 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_START
,
3258 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
3260 if (upl_offset
== 0 && ((io_size
& PAGE_MASK
) == 0)) {
3262 abort_flag
= UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
;
3265 abort_flag
= UPL_ABORT_FREE_ON_EMPTY
;
3267 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
3269 upl_size
= upl_needed_size
;
3270 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
3273 upl_flags
|= UPL_NOZEROFILL
;
3274 if (force_data_sync
)
3275 upl_flags
|= UPL_FORCE_DATA_SYNC
;
3277 // LP64todo - fix this!
3278 kret
= vm_map_create_upl(current_map(),
3279 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
3280 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
);
3282 if (kret
!= KERN_SUCCESS
) {
3283 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
3284 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
3286 * cluster_nocopy_read: failed to get pagelist
3288 * we may have already spun some portion of this request
3289 * off as async requests... we need to wait for the I/O
3290 * to complete before returning
3292 goto wait_for_reads
;
3294 pages_in_pl
= upl_size
/ PAGE_SIZE
;
3295 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
3297 for (i
= 0; i
< pages_in_pl
; i
++) {
3298 if (!upl_valid_page(pl
, i
))
3301 if (i
== pages_in_pl
)
3304 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
, abort_flag
);
3306 if (force_data_sync
>= 3) {
3307 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
3308 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
3310 goto wait_for_reads
;
3313 * Consider the possibility that upl_size wasn't satisfied.
3315 if (upl_size
!= upl_needed_size
)
3316 io_size
= (upl_size
- (int)upl_offset
) & ~PAGE_MASK
;
3319 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
, abort_flag
);
3320 goto wait_for_reads
;
3322 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
3323 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
3326 * request asynchronously so that we can overlap
3327 * the preparation of the next I/O
3328 * if there are already too many outstanding reads
3329 * wait until some have completed before issuing the next read
3331 lck_mtx_lock(cl_mtxp
);
3333 while ((iostate
.io_issued
- iostate
.io_completed
) > max_rd_ahead
) {
3334 iostate
.io_wanted
= 1;
3335 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_nocopy_read", 0);
3337 lck_mtx_unlock(cl_mtxp
);
3339 if (iostate
.io_error
) {
3341 * one of the earlier reads we issued ran into a hard error
3342 * don't issue any more reads, cleanup the UPL
3343 * that was just created but not used, then
3344 * go wait for any other reads to complete before
3345 * returning the error to the caller
3347 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
, abort_flag
);
3349 goto wait_for_reads
;
3351 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_START
,
3352 (int)upl
, (int)upl_offset
, (int)uio
->uio_offset
, io_size
, 0);
3354 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
, io_size
,
3355 CL_PRESERVE
| CL_COMMIT
| CL_READ
| CL_ASYNC
| CL_NOZERO
,
3356 (buf_t
)NULL
, &iostate
);
3359 * update the uio structure
3361 uio_update(uio
, (user_size_t
)io_size
);
3363 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_END
,
3364 (int)upl
, (int)uio
->uio_offset
, (int)uio_resid(uio
), retval
, 0);
3370 * make sure all async reads that are part of this stream
3371 * have completed before we return
3373 lck_mtx_lock(cl_mtxp
);
3375 while (iostate
.io_issued
!= iostate
.io_completed
) {
3376 iostate
.io_wanted
= 1;
3377 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_nocopy_read", 0);
3379 lck_mtx_unlock(cl_mtxp
);
3381 if (iostate
.io_error
)
3382 retval
= iostate
.io_error
;
3384 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
3385 (int)uio
->uio_offset
, (int)uio_resid(uio
), 6, retval
, 0);
3392 cluster_phys_read(vnode_t vp
, struct uio
*uio
, off_t filesize
)
3394 upl_page_info_t
*pl
;
3396 vm_offset_t upl_offset
;
3400 user_size_t iov_len
;
3401 user_addr_t iov_base
;
3404 int upl_needed_size
;
3408 struct clios iostate
;
3412 devblocksize
= vp
->v_mount
->mnt_devblocksize
;
3414 * When we enter this routine, we know
3415 * -- the resid will not exceed iov_len
3416 * -- the target address is physically contiguous
3420 if (IS_VALID_UIO_SEGFLG(uio
->uio_segflg
) == 0) {
3421 panic("%s :%d - invalid uio_segflg\n", __FILE__
, __LINE__
);
3423 #endif /* LP64_DEBUG */
3425 iov_len
= uio_curriovlen(uio
);
3426 iov_base
= uio_curriovbase(uio
);
3428 max_size
= filesize
- uio
->uio_offset
;
3430 // LP64todo - fix this!
3431 if (max_size
< 0 || (u_int64_t
)max_size
> iov_len
)
3436 // LP64todo - fix this!
3437 upl_offset
= CAST_DOWN(vm_offset_t
, iov_base
) & PAGE_MASK
;
3438 upl_needed_size
= upl_offset
+ io_size
;
3442 upl_size
= upl_needed_size
;
3443 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
3445 kret
= vm_map_get_upl(current_map(),
3446 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
3447 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
, 0);
3449 if (kret
!= KERN_SUCCESS
) {
3451 * cluster_phys_read: failed to get pagelist
3455 if (upl_size
< upl_needed_size
) {
3457 * The upl_size wasn't satisfied.
3459 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3463 pl
= ubc_upl_pageinfo(upl
);
3465 dst_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)upl_offset
;
3467 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
3470 head_size
= devblocksize
- (int)(uio
->uio_offset
& (devblocksize
- 1));
3472 if (head_size
> io_size
)
3473 head_size
= io_size
;
3475 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, head_size
, CL_READ
);
3478 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3482 upl_offset
+= head_size
;
3483 dst_paddr
+= head_size
;
3484 io_size
-= head_size
;
3486 tail_size
= io_size
& (devblocksize
- 1);
3487 io_size
-= tail_size
;
3489 iostate
.io_completed
= 0;
3490 iostate
.io_issued
= 0;
3491 iostate
.io_error
= 0;
3492 iostate
.io_wanted
= 0;
3494 while (io_size
&& error
== 0) {
3497 if (io_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
3498 xsize
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
3502 * request asynchronously so that we can overlap
3503 * the preparation of the next I/O... we'll do
3504 * the commit after all the I/O has completed
3505 * since its all issued against the same UPL
3506 * if there are already too many outstanding reads
3507 * wait until some have completed before issuing the next
3509 lck_mtx_lock(cl_mtxp
);
3511 while ((iostate
.io_issued
- iostate
.io_completed
) > (8 * MAX_UPL_TRANSFER
* PAGE_SIZE
)) {
3512 iostate
.io_wanted
= 1;
3513 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_phys_read", 0);
3515 lck_mtx_unlock(cl_mtxp
);
3517 error
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
, xsize
,
3518 CL_READ
| CL_NOZERO
| CL_DEV_MEMORY
| CL_ASYNC
,
3519 (buf_t
)NULL
, &iostate
);
3521 * The cluster_io read was issued successfully,
3522 * update the uio structure
3525 uio_update(uio
, (user_size_t
)xsize
);
3528 upl_offset
+= xsize
;
3533 * make sure all async reads that are part of this stream
3534 * have completed before we proceed
3536 lck_mtx_lock(cl_mtxp
);
3538 while (iostate
.io_issued
!= iostate
.io_completed
) {
3539 iostate
.io_wanted
= 1;
3540 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_phys_read", 0);
3542 lck_mtx_unlock(cl_mtxp
);
3544 if (iostate
.io_error
)
3545 error
= iostate
.io_error
;
3547 if (error
== 0 && tail_size
)
3548 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, tail_size
, CL_READ
);
3551 * just release our hold on the physically contiguous
3552 * region without changing any state
3554 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3561 * generate advisory I/O's in the largest chunks possible
3562 * the completed pages will be released into the VM cache
3565 advisory_read(vnode_t vp
, off_t filesize
, off_t f_offset
, int resid
)
3567 upl_page_info_t
*pl
;
3569 vm_offset_t upl_offset
;
3583 if ( !UBCINFOEXISTS(vp
))
3586 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_START
,
3587 (int)f_offset
, resid
, (int)filesize
, 0, 0);
3589 while (resid
&& f_offset
< filesize
&& retval
== 0) {
3591 * compute the size of the upl needed to encompass
3592 * the requested read... limit each call to cluster_io
3593 * to the maximum UPL size... cluster_io will clip if
3594 * this exceeds the maximum io_size for the device,
3595 * make sure to account for
3596 * a starting offset that's not page aligned
3598 start_offset
= (int)(f_offset
& PAGE_MASK_64
);
3599 upl_f_offset
= f_offset
- (off_t
)start_offset
;
3600 max_size
= filesize
- f_offset
;
3602 if (resid
< max_size
)
3607 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
3608 if (upl_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
3609 upl_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
3613 * return the number of contiguously present pages in the cache
3614 * starting at upl_f_offset within the file
3616 ubc_range_op(vp
, upl_f_offset
, upl_f_offset
+ upl_size
, UPL_ROP_PRESENT
, &skip_range
);
3620 * skip over pages already present in the cache
3622 io_size
= skip_range
- start_offset
;
3624 f_offset
+= io_size
;
3627 if (skip_range
== upl_size
)
3630 * have to issue some real I/O
3631 * at this point, we know it's starting on a page boundary
3632 * because we've skipped over at least the first page in the request
3635 upl_f_offset
+= skip_range
;
3636 upl_size
-= skip_range
;
3638 pages_in_upl
= upl_size
/ PAGE_SIZE
;
3640 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_START
,
3641 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3643 kret
= ubc_create_upl(vp
,
3648 UPL_RET_ONLY_ABSENT
| UPL_SET_LITE
);
3649 if (kret
!= KERN_SUCCESS
)
3654 * before we start marching forward, we must make sure we end on
3655 * a present page, otherwise we will be working with a freed
3658 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
3659 if (upl_page_present(pl
, last_pg
))
3662 pages_in_upl
= last_pg
+ 1;
3665 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_END
,
3666 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3669 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
3671 * scan from the beginning of the upl looking for the first
3672 * page that is present.... this will become the first page in
3673 * the request we're going to make to 'cluster_io'... if all
3674 * of the pages are absent, we won't call through to 'cluster_io'
3676 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
3677 if (upl_page_present(pl
, start_pg
))
3682 * scan from the starting present page looking for an absent
3683 * page before the end of the upl is reached, if we
3684 * find one, then it will terminate the range of pages being
3685 * presented to 'cluster_io'
3687 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
3688 if (!upl_page_present(pl
, last_pg
))
3692 if (last_pg
> start_pg
) {
3694 * we found a range of pages that must be filled
3695 * if the last page in this range is the last page of the file
3696 * we may have to clip the size of it to keep from reading past
3697 * the end of the last physical block associated with the file
3699 upl_offset
= start_pg
* PAGE_SIZE
;
3700 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
3702 if ((upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
3703 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
3706 * issue an asynchronous read to cluster_io
3708 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
3709 CL_ASYNC
| CL_READ
| CL_COMMIT
| CL_AGE
, (buf_t
)NULL
, (struct clios
*)NULL
);
3715 ubc_upl_abort(upl
, 0);
3717 io_size
= upl_size
- start_offset
;
3719 if (io_size
> resid
)
3721 f_offset
+= io_size
;
3725 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_END
,
3726 (int)f_offset
, resid
, retval
, 0, 0);
3733 cluster_push(vnode_t vp
, int flags
)
3736 struct cl_writebehind
*wbp
;
3738 if ( !UBCINFOEXISTS(vp
)) {
3739 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, (int)vp
, flags
, 0, -1, 0);
3742 /* return if deferred write is set */
3743 if (((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) && (flags
& IO_DEFWRITE
)) {
3746 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) == NULL
) {
3747 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, (int)vp
, flags
, 0, -2, 0);
3750 if (wbp
->cl_number
== 0 && wbp
->cl_scmap
== NULL
) {
3751 lck_mtx_unlock(&wbp
->cl_lockw
);
3753 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, (int)vp
, flags
, 0, -3, 0);
3756 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_START
,
3757 (int)wbp
->cl_scmap
, wbp
->cl_number
, flags
, 0, 0);
3759 if (wbp
->cl_scmap
) {
3760 sparse_cluster_push(wbp
, vp
, ubc_getsize(vp
), 1);
3764 retval
= cluster_try_push(wbp
, vp
, ubc_getsize(vp
), 0, 1);
3766 lck_mtx_unlock(&wbp
->cl_lockw
);
3768 if (flags
& IO_SYNC
)
3769 (void)vnode_waitforwrites(vp
, 0, 0, 0, (char *)"cluster_push");
3771 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_END
,
3772 (int)wbp
->cl_scmap
, wbp
->cl_number
, retval
, 0, 0);
3778 __private_extern__
void
3779 cluster_release(struct ubc_info
*ubc
)
3781 struct cl_writebehind
*wbp
;
3782 struct cl_readahead
*rap
;
3784 if ((wbp
= ubc
->cl_wbehind
)) {
3786 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, (int)ubc
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
3789 vfs_drt_control(&(wbp
->cl_scmap
), 0);
3791 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, (int)ubc
, 0, 0, 0, 0);
3794 rap
= ubc
->cl_rahead
;
3797 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
3798 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
3800 if ((rap
= ubc
->cl_rahead
)) {
3801 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
3802 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
3804 ubc
->cl_rahead
= NULL
;
3805 ubc
->cl_wbehind
= NULL
;
3807 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_END
, (int)ubc
, (int)rap
, (int)wbp
, 0, 0);
3812 cluster_push_EOF(vnode_t vp
, off_t EOF
)
3814 struct cl_writebehind
*wbp
;
3816 wbp
= cluster_get_wbp(vp
, CLW_ALLOCATE
| CLW_RETURNLOCKED
);
3818 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_START
,
3819 (int)wbp
->cl_scmap
, wbp
->cl_number
, (int)EOF
, 0, 0);
3822 sparse_cluster_push(wbp
, vp
, EOF
, 1);
3824 cluster_try_push(wbp
, vp
, EOF
, 0, 1);
3826 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_END
,
3827 (int)wbp
->cl_scmap
, wbp
->cl_number
, 0, 0, 0);
3829 lck_mtx_unlock(&wbp
->cl_lockw
);
3834 cluster_try_push(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int can_delay
, int push_all
)
3841 struct cl_wextent l_clusters
[MAX_CLUSTERS
];
3844 * the write behind context exists and has
3845 * already been locked...
3847 * make a local 'sorted' copy of the clusters
3848 * and clear wbp->cl_number so that new clusters can
3851 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
3852 for (min_index
= -1, cl_index1
= 0; cl_index1
< wbp
->cl_number
; cl_index1
++) {
3853 if (wbp
->cl_clusters
[cl_index1
].b_addr
== wbp
->cl_clusters
[cl_index1
].e_addr
)
3855 if (min_index
== -1)
3856 min_index
= cl_index1
;
3857 else if (wbp
->cl_clusters
[cl_index1
].b_addr
< wbp
->cl_clusters
[min_index
].b_addr
)
3858 min_index
= cl_index1
;
3860 if (min_index
== -1)
3862 l_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[min_index
].b_addr
;
3863 l_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
3864 l_clusters
[cl_index
].io_nocache
= wbp
->cl_clusters
[min_index
].io_nocache
;
3866 wbp
->cl_clusters
[min_index
].b_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
3872 if (can_delay
&& cl_len
== MAX_CLUSTERS
) {
3876 * determine if we appear to be writing the file sequentially
3877 * if not, by returning without having pushed any clusters
3878 * we will cause this vnode to be pushed into the sparse cluster mechanism
3879 * used for managing more random I/O patterns
3881 * we know that we've got all clusters currently in use and the next write doesn't fit into one of them...
3882 * that's why we're in try_push with can_delay true...
3884 * check to make sure that all the clusters except the last one are 'full'... and that each cluster
3885 * is adjacent to the next (i.e. we're looking for sequential writes) they were sorted above
3886 * so we can just make a simple pass through, up to, but not including the last one...
3887 * note that e_addr is not inclusive, so it will be equal to the b_addr of the next cluster if they
3890 * we let the last one be partial as long as it was adjacent to the previous one...
3891 * we need to do this to deal with multi-threaded servers that might write an I/O or 2 out
3892 * of order... if this occurs at the tail of the last cluster, we don't want to fall into the sparse cluster world...
3894 for (i
= 0; i
< MAX_CLUSTERS
- 1; i
++) {
3895 if ((l_clusters
[i
].e_addr
- l_clusters
[i
].b_addr
) != MAX_UPL_TRANSFER
)
3897 if (l_clusters
[i
].e_addr
!= l_clusters
[i
+1].b_addr
)
3902 * drop the lock while we're firing off the I/Os...
3903 * this is safe since I'm working off of a private sorted copy
3904 * of the clusters, and I'm going to re-evaluate the public
3905 * state after I retake the lock
3907 lck_mtx_unlock(&wbp
->cl_lockw
);
3909 for (cl_index
= 0; cl_index
< cl_len
; cl_index
++) {
3911 struct cl_extent cl
;
3914 * try to push each cluster in turn...
3916 if (l_clusters
[cl_index
].io_nocache
)
3920 cl
.b_addr
= l_clusters
[cl_index
].b_addr
;
3921 cl
.e_addr
= l_clusters
[cl_index
].e_addr
;
3923 cluster_push_x(vp
, &cl
, EOF
, flags
);
3925 l_clusters
[cl_index
].b_addr
= 0;
3926 l_clusters
[cl_index
].e_addr
= 0;
3933 lck_mtx_lock(&wbp
->cl_lockw
);
3936 if (cl_len
> cl_pushed
) {
3938 * we didn't push all of the clusters, so
3939 * lets try to merge them back in to the vnode
3941 if ((MAX_CLUSTERS
- wbp
->cl_number
) < (cl_len
- cl_pushed
)) {
3943 * we picked up some new clusters while we were trying to
3944 * push the old ones... this can happen because I've dropped
3945 * the vnode lock... the sum of the
3946 * leftovers plus the new cluster count exceeds our ability
3947 * to represent them, so switch to the sparse cluster mechanism
3949 * collect the active public clusters...
3951 sparse_cluster_switch(wbp
, vp
, EOF
);
3953 for (cl_index
= 0, cl_index1
= 0; cl_index
< cl_len
; cl_index
++) {
3954 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
3956 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
3957 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
3958 wbp
->cl_clusters
[cl_index1
].io_nocache
= l_clusters
[cl_index
].io_nocache
;
3963 * update the cluster count
3965 wbp
->cl_number
= cl_index1
;
3968 * and collect the original clusters that were moved into the
3969 * local storage for sorting purposes
3971 sparse_cluster_switch(wbp
, vp
, EOF
);
3975 * we've got room to merge the leftovers back in
3976 * just append them starting at the next 'hole'
3977 * represented by wbp->cl_number
3979 for (cl_index
= 0, cl_index1
= wbp
->cl_number
; cl_index
< cl_len
; cl_index
++) {
3980 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
3983 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
3984 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
3985 wbp
->cl_clusters
[cl_index1
].io_nocache
= l_clusters
[cl_index
].io_nocache
;
3990 * update the cluster count
3992 wbp
->cl_number
= cl_index1
;
3995 return(MAX_CLUSTERS
- wbp
->cl_number
);
4001 cluster_push_x(vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int flags
)
4003 upl_page_info_t
*pl
;
4005 vm_offset_t upl_offset
;
4020 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_START
,
4021 (int)cl
->b_addr
, (int)cl
->e_addr
, (int)EOF
, flags
, 0);
4023 if ((pages_in_upl
= (int)(cl
->e_addr
- cl
->b_addr
)) == 0) {
4024 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 0, 0, 0, 0);
4028 upl_size
= pages_in_upl
* PAGE_SIZE
;
4029 upl_f_offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
4031 if (upl_f_offset
+ upl_size
>= EOF
) {
4033 if (upl_f_offset
>= EOF
) {
4035 * must have truncated the file and missed
4036 * clearing a dangling cluster (i.e. it's completely
4037 * beyond the new EOF
4039 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 1, 0, 0, 0);
4043 size
= EOF
- upl_f_offset
;
4045 upl_size
= (size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
4046 pages_in_upl
= upl_size
/ PAGE_SIZE
;
4050 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, size
, 0, 0, 0);
4053 * by asking for UPL_COPYOUT_FROM and UPL_RET_ONLY_DIRTY, we get the following desirable behavior
4055 * - only pages that are currently dirty are returned... these are the ones we need to clean
4056 * - the hardware dirty bit is cleared when the page is gathered into the UPL... the software dirty bit is set
4057 * - 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
4058 * - when we commit the page, the software dirty bit is cleared... the hardware dirty bit is untouched so that if
4059 * someone dirties this page while the I/O is in progress, we don't lose track of the new state
4061 * when the I/O completes, we no longer ask for an explicit clear of the DIRTY state (either soft or hard)
4064 if ((vp
->v_flag
& VNOCACHE_DATA
) || (flags
& IO_NOCACHE
))
4065 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
| UPL_WILL_BE_DUMPED
;
4067 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
;
4069 kret
= ubc_create_upl(vp
,
4075 if (kret
!= KERN_SUCCESS
)
4076 panic("cluster_push: failed to get pagelist");
4078 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
, (int)upl
, upl_f_offset
, 0, 0, 0);
4081 * since we only asked for the dirty pages back
4082 * it's possible that we may only get a few or even none, so...
4083 * before we start marching forward, we must make sure we know
4084 * where the last present page is in the UPL, otherwise we could
4085 * end up working with a freed upl due to the FREE_ON_EMPTY semantics
4086 * employed by commit_range and abort_range.
4088 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
4089 if (upl_page_present(pl
, last_pg
))
4092 pages_in_upl
= last_pg
+ 1;
4094 if (pages_in_upl
== 0) {
4095 ubc_upl_abort(upl
, 0);
4097 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 2, 0, 0, 0);
4101 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
4103 * find the next dirty page in the UPL
4104 * this will become the first page in the
4105 * next I/O to generate
4107 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
4108 if (upl_dirty_page(pl
, start_pg
))
4110 if (upl_page_present(pl
, start_pg
))
4112 * RET_ONLY_DIRTY will return non-dirty 'precious' pages
4113 * just release these unchanged since we're not going
4114 * to steal them or change their state
4116 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
4118 if (start_pg
>= pages_in_upl
)
4120 * done... no more dirty pages to push
4123 if (start_pg
> last_pg
)
4125 * skipped over some non-dirty pages
4127 size
-= ((start_pg
- last_pg
) * PAGE_SIZE
);
4130 * find a range of dirty pages to write
4132 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
4133 if (!upl_dirty_page(pl
, last_pg
))
4136 upl_offset
= start_pg
* PAGE_SIZE
;
4138 io_size
= min(size
, (last_pg
- start_pg
) * PAGE_SIZE
);
4140 io_flags
= CL_THROTTLE
| CL_COMMIT
;
4142 if ( !(flags
& IO_SYNC
))
4143 io_flags
|= CL_ASYNC
;
4145 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
4146 io_flags
, (buf_t
)NULL
, (struct clios
*)NULL
);
4148 if (error
== 0 && retval
)
4153 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 3, 0, 0, 0);
4160 * sparse_cluster_switch is called with the write behind lock held
4163 sparse_cluster_switch(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
)
4167 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_START
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
4169 if (wbp
->cl_scmap
== NULL
)
4170 wbp
->cl_scdirty
= 0;
4172 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
4174 struct cl_extent cl
;
4176 for (cl
.b_addr
= wbp
->cl_clusters
[cl_index
].b_addr
; cl
.b_addr
< wbp
->cl_clusters
[cl_index
].e_addr
; cl
.b_addr
++) {
4178 if (ubc_page_op(vp
, (off_t
)(cl
.b_addr
* PAGE_SIZE_64
), 0, 0, &flags
) == KERN_SUCCESS
) {
4179 if (flags
& UPL_POP_DIRTY
) {
4180 cl
.e_addr
= cl
.b_addr
+ 1;
4182 sparse_cluster_add(wbp
, vp
, &cl
, EOF
);
4189 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_END
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
4194 * sparse_cluster_push is called with the write behind lock held
4197 sparse_cluster_push(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int push_all
)
4199 struct cl_extent cl
;
4203 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_START
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, push_all
, 0);
4206 vfs_drt_control(&(wbp
->cl_scmap
), 1);
4209 if (vfs_drt_get_cluster(&(wbp
->cl_scmap
), &offset
, &length
) != KERN_SUCCESS
)
4212 cl
.b_addr
= (daddr64_t
)(offset
/ PAGE_SIZE_64
);
4213 cl
.e_addr
= (daddr64_t
)((offset
+ length
) / PAGE_SIZE_64
);
4215 wbp
->cl_scdirty
-= (int)(cl
.e_addr
- cl
.b_addr
);
4217 cluster_push_x(vp
, &cl
, EOF
, 0);
4222 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_END
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
4227 * sparse_cluster_add is called with the write behind lock held
4230 sparse_cluster_add(struct cl_writebehind
*wbp
, vnode_t vp
, struct cl_extent
*cl
, off_t EOF
)
4236 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_START
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, (int)cl
->b_addr
, (int)cl
->e_addr
, 0);
4238 offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
4239 length
= ((u_int
)(cl
->e_addr
- cl
->b_addr
)) * PAGE_SIZE
;
4241 while (vfs_drt_mark_pages(&(wbp
->cl_scmap
), offset
, length
, &new_dirty
) != KERN_SUCCESS
) {
4243 * no room left in the map
4244 * only a partial update was done
4245 * push out some pages and try again
4247 wbp
->cl_scdirty
+= new_dirty
;
4249 sparse_cluster_push(wbp
, vp
, EOF
, 0);
4251 offset
+= (new_dirty
* PAGE_SIZE_64
);
4252 length
-= (new_dirty
* PAGE_SIZE
);
4254 wbp
->cl_scdirty
+= new_dirty
;
4256 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_END
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
4261 cluster_align_phys_io(vnode_t vp
, struct uio
*uio
, addr64_t usr_paddr
, int xsize
, int flags
)
4263 upl_page_info_t
*pl
;
4272 upl_flags
= UPL_SET_LITE
;
4273 if (! (flags
& CL_READ
)) {
4275 * "write" operation: let the UPL subsystem know
4276 * that we intend to modify the buffer cache pages
4279 upl_flags
|= UPL_WILL_MODIFY
;
4282 kret
= ubc_create_upl(vp
,
4283 uio
->uio_offset
& ~PAGE_MASK_64
,
4289 if (kret
!= KERN_SUCCESS
)
4292 if (!upl_valid_page(pl
, 0)) {
4294 * issue a synchronous read to cluster_io
4296 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
4297 CL_READ
, (buf_t
)NULL
, (struct clios
*)NULL
);
4299 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
4305 ubc_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)(uio
->uio_offset
& PAGE_MASK_64
);
4308 * NOTE: There is no prototype for the following in BSD. It, and the definitions
4309 * of the defines for cppvPsrc, cppvPsnk, cppvFsnk, and cppvFsrc will be found in
4310 * osfmk/ppc/mappings.h. They are not included here because there appears to be no
4311 * way to do so without exporting them to kexts as well.
4313 if (flags
& CL_READ
)
4314 // copypv(ubc_paddr, usr_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsnk); /* Copy physical to physical and flush the destination */
4315 copypv(ubc_paddr
, usr_paddr
, xsize
, 2 | 1 | 4); /* Copy physical to physical and flush the destination */
4317 // copypv(usr_paddr, ubc_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsrc); /* Copy physical to physical and flush the source */
4318 copypv(usr_paddr
, ubc_paddr
, xsize
, 2 | 1 | 8); /* Copy physical to physical and flush the source */
4320 if ( !(flags
& CL_READ
) || (upl_valid_page(pl
, 0) && upl_dirty_page(pl
, 0))) {
4322 * issue a synchronous write to cluster_io
4324 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
4325 0, (buf_t
)NULL
, (struct clios
*)NULL
);
4328 uio_update(uio
, (user_size_t
)xsize
);
4331 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
4333 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
4335 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, abort_flags
);
4343 cluster_copy_upl_data(struct uio
*uio
, upl_t upl
, int upl_offset
, int xsize
)
4350 upl_page_info_t
*pl
;
4352 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
4353 (int)uio
->uio_offset
, uio_resid(uio
), upl_offset
, xsize
, 0);
4355 segflg
= uio
->uio_segflg
;
4359 case UIO_USERSPACE32
:
4360 case UIO_USERISPACE32
:
4361 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
4365 case UIO_USERISPACE
:
4366 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
4369 case UIO_USERSPACE64
:
4370 case UIO_USERISPACE64
:
4371 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
4374 case UIO_SYSSPACE32
:
4375 uio
->uio_segflg
= UIO_PHYS_SYSSPACE32
;
4379 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
4382 case UIO_SYSSPACE64
:
4383 uio
->uio_segflg
= UIO_PHYS_SYSSPACE64
;
4386 pl
= ubc_upl_pageinfo(upl
);
4388 pg_index
= upl_offset
/ PAGE_SIZE
;
4389 pg_offset
= upl_offset
& PAGE_MASK
;
4390 csize
= min(PAGE_SIZE
- pg_offset
, xsize
);
4392 while (xsize
&& retval
== 0) {
4395 paddr
= ((addr64_t
)upl_phys_page(pl
, pg_index
) << 12) + pg_offset
;
4397 retval
= uiomove64(paddr
, csize
, uio
);
4402 csize
= min(PAGE_SIZE
, xsize
);
4404 uio
->uio_segflg
= segflg
;
4406 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
4407 (int)uio
->uio_offset
, uio_resid(uio
), retval
, segflg
, 0);
4414 cluster_copy_ubc_data(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
)
4421 memory_object_control_t control
;
4424 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
4425 (int)uio
->uio_offset
, uio_resid(uio
), 0, *io_resid
, 0);
4427 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
4428 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
4429 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
4430 (int)uio
->uio_offset
, uio_resid(uio
), retval
, 3, 0);
4434 segflg
= uio
->uio_segflg
;
4438 case UIO_USERSPACE32
:
4439 case UIO_USERISPACE32
:
4440 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
4443 case UIO_USERSPACE64
:
4444 case UIO_USERISPACE64
:
4445 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
4448 case UIO_SYSSPACE32
:
4449 uio
->uio_segflg
= UIO_PHYS_SYSSPACE32
;
4452 case UIO_SYSSPACE64
:
4453 uio
->uio_segflg
= UIO_PHYS_SYSSPACE64
;
4457 case UIO_USERISPACE
:
4458 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
4462 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
4466 if ( (io_size
= *io_resid
) ) {
4467 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
4468 xsize
= uio_resid(uio
);
4470 retval
= memory_object_control_uiomove(control
, uio
->uio_offset
- start_offset
,
4471 uio
, start_offset
, io_size
, mark_dirty
);
4472 xsize
-= uio_resid(uio
);
4475 uio
->uio_segflg
= segflg
;
4476 *io_resid
= io_size
;
4478 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
4479 (int)uio
->uio_offset
, uio_resid(uio
), retval
, 0x80000000 | segflg
, 0);
4486 is_file_clean(vnode_t vp
, off_t filesize
)
4490 int total_dirty
= 0;
4492 for (f_offset
= 0; f_offset
< filesize
; f_offset
+= PAGE_SIZE_64
) {
4493 if (ubc_page_op(vp
, f_offset
, 0, 0, &flags
) == KERN_SUCCESS
) {
4494 if (flags
& UPL_POP_DIRTY
) {
4508 * Dirty region tracking/clustering mechanism.
4510 * This code (vfs_drt_*) provides a mechanism for tracking and clustering
4511 * dirty regions within a larger space (file). It is primarily intended to
4512 * support clustering in large files with many dirty areas.
4514 * The implementation assumes that the dirty regions are pages.
4516 * To represent dirty pages within the file, we store bit vectors in a
4517 * variable-size circular hash.
4521 * Bitvector size. This determines the number of pages we group in a
4522 * single hashtable entry. Each hashtable entry is aligned to this
4523 * size within the file.
4525 #define DRT_BITVECTOR_PAGES 256
4528 * File offset handling.
4530 * DRT_ADDRESS_MASK is dependent on DRT_BITVECTOR_PAGES;
4531 * the correct formula is (~(DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1)
4533 #define DRT_ADDRESS_MASK (~((1 << 20) - 1))
4534 #define DRT_ALIGN_ADDRESS(addr) ((addr) & DRT_ADDRESS_MASK)
4537 * Hashtable address field handling.
4539 * The low-order bits of the hashtable address are used to conserve
4542 * DRT_HASH_COUNT_MASK must be large enough to store the range
4543 * 0-DRT_BITVECTOR_PAGES inclusive, as well as have one value
4544 * to indicate that the bucket is actually unoccupied.
4546 #define DRT_HASH_GET_ADDRESS(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_ADDRESS_MASK)
4547 #define DRT_HASH_SET_ADDRESS(scm, i, a) \
4549 (scm)->scm_hashtable[(i)].dhe_control = \
4550 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_ADDRESS_MASK) | DRT_ALIGN_ADDRESS(a); \
4552 #define DRT_HASH_COUNT_MASK 0x1ff
4553 #define DRT_HASH_GET_COUNT(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_HASH_COUNT_MASK)
4554 #define DRT_HASH_SET_COUNT(scm, i, c) \
4556 (scm)->scm_hashtable[(i)].dhe_control = \
4557 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_HASH_COUNT_MASK) | ((c) & DRT_HASH_COUNT_MASK); \
4559 #define DRT_HASH_CLEAR(scm, i) \
4561 (scm)->scm_hashtable[(i)].dhe_control = 0; \
4563 #define DRT_HASH_VACATE(scm, i) DRT_HASH_SET_COUNT((scm), (i), DRT_HASH_COUNT_MASK)
4564 #define DRT_HASH_VACANT(scm, i) (DRT_HASH_GET_COUNT((scm), (i)) == DRT_HASH_COUNT_MASK)
4565 #define DRT_HASH_COPY(oscm, oi, scm, i) \
4567 (scm)->scm_hashtable[(i)].dhe_control = (oscm)->scm_hashtable[(oi)].dhe_control; \
4568 DRT_BITVECTOR_COPY(oscm, oi, scm, i); \
4573 * Hash table moduli.
4575 * Since the hashtable entry's size is dependent on the size of
4576 * the bitvector, and since the hashtable size is constrained to
4577 * both being prime and fitting within the desired allocation
4578 * size, these values need to be manually determined.
4580 * For DRT_BITVECTOR_SIZE = 256, the entry size is 40 bytes.
4582 * The small hashtable allocation is 1024 bytes, so the modulus is 23.
4583 * The large hashtable allocation is 16384 bytes, so the modulus is 401.
4585 #define DRT_HASH_SMALL_MODULUS 23
4586 #define DRT_HASH_LARGE_MODULUS 401
4588 #define DRT_SMALL_ALLOCATION 1024 /* 104 bytes spare */
4589 #define DRT_LARGE_ALLOCATION 16384 /* 344 bytes spare */
4591 /* *** nothing below here has secret dependencies on DRT_BITVECTOR_PAGES *** */
4594 * Hashtable bitvector handling.
4596 * Bitvector fields are 32 bits long.
4599 #define DRT_HASH_SET_BIT(scm, i, bit) \
4600 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] |= (1 << ((bit) % 32))
4602 #define DRT_HASH_CLEAR_BIT(scm, i, bit) \
4603 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] &= ~(1 << ((bit) % 32))
4605 #define DRT_HASH_TEST_BIT(scm, i, bit) \
4606 ((scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] & (1 << ((bit) % 32)))
4608 #define DRT_BITVECTOR_CLEAR(scm, i) \
4609 bzero(&(scm)->scm_hashtable[(i)].dhe_bitvector[0], (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
4611 #define DRT_BITVECTOR_COPY(oscm, oi, scm, i) \
4612 bcopy(&(oscm)->scm_hashtable[(oi)].dhe_bitvector[0], \
4613 &(scm)->scm_hashtable[(i)].dhe_bitvector[0], \
4614 (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
4621 struct vfs_drt_hashentry
{
4622 u_int64_t dhe_control
;
4623 u_int32_t dhe_bitvector
[DRT_BITVECTOR_PAGES
/ 32];
4627 * Dirty Region Tracking structure.
4629 * The hashtable is allocated entirely inside the DRT structure.
4631 * The hash is a simple circular prime modulus arrangement, the structure
4632 * is resized from small to large if it overflows.
4635 struct vfs_drt_clustermap
{
4636 u_int32_t scm_magic
; /* sanity/detection */
4637 #define DRT_SCM_MAGIC 0x12020003
4638 u_int32_t scm_modulus
; /* current ring size */
4639 u_int32_t scm_buckets
; /* number of occupied buckets */
4640 u_int32_t scm_lastclean
; /* last entry we cleaned */
4641 u_int32_t scm_iskips
; /* number of slot skips */
4643 struct vfs_drt_hashentry scm_hashtable
[0];
4647 #define DRT_HASH(scm, addr) ((addr) % (scm)->scm_modulus)
4648 #define DRT_HASH_NEXT(scm, addr) (((addr) + 1) % (scm)->scm_modulus)
4651 * Debugging codes and arguments.
4653 #define DRT_DEBUG_EMPTYFREE (FSDBG_CODE(DBG_FSRW, 82)) /* nil */
4654 #define DRT_DEBUG_RETCLUSTER (FSDBG_CODE(DBG_FSRW, 83)) /* offset, length */
4655 #define DRT_DEBUG_ALLOC (FSDBG_CODE(DBG_FSRW, 84)) /* copycount */
4656 #define DRT_DEBUG_INSERT (FSDBG_CODE(DBG_FSRW, 85)) /* offset, iskip */
4657 #define DRT_DEBUG_MARK (FSDBG_CODE(DBG_FSRW, 86)) /* offset, length,
4660 /* 1 (clean, no map) */
4661 /* 2 (map alloc fail) */
4662 /* 3, resid (partial) */
4663 #define DRT_DEBUG_6 (FSDBG_CODE(DBG_FSRW, 87))
4664 #define DRT_DEBUG_SCMDATA (FSDBG_CODE(DBG_FSRW, 88)) /* modulus, buckets,
4665 * lastclean, iskips */
4668 static kern_return_t
vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
);
4669 static kern_return_t
vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
);
4670 static kern_return_t
vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
,
4671 u_int64_t offset
, int *indexp
);
4672 static kern_return_t
vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
,
4676 static kern_return_t
vfs_drt_do_mark_pages(
4682 static void vfs_drt_trace(
4683 struct vfs_drt_clustermap
*cmap
,
4692 * Allocate and initialise a sparse cluster map.
4694 * Will allocate a new map, resize or compact an existing map.
4696 * XXX we should probably have at least one intermediate map size,
4697 * as the 1:16 ratio seems a bit drastic.
4699 static kern_return_t
4700 vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
)
4702 struct vfs_drt_clustermap
*cmap
, *ocmap
;
4705 int nsize
, i
, active_buckets
, index
, copycount
;
4712 * Decide on the size of the new map.
4714 if (ocmap
== NULL
) {
4715 nsize
= DRT_HASH_SMALL_MODULUS
;
4717 /* count the number of active buckets in the old map */
4719 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
4720 if (!DRT_HASH_VACANT(ocmap
, i
) &&
4721 (DRT_HASH_GET_COUNT(ocmap
, i
) != 0))
4725 * If we're currently using the small allocation, check to
4726 * see whether we should grow to the large one.
4728 if (ocmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) {
4729 /* if the ring is nearly full */
4730 if (active_buckets
> (DRT_HASH_SMALL_MODULUS
- 5)) {
4731 nsize
= DRT_HASH_LARGE_MODULUS
;
4733 nsize
= DRT_HASH_SMALL_MODULUS
;
4736 /* already using the large modulus */
4737 nsize
= DRT_HASH_LARGE_MODULUS
;
4739 * If the ring is completely full, there's
4740 * nothing useful for us to do. Behave as
4741 * though we had compacted into the new
4744 if (active_buckets
>= DRT_HASH_LARGE_MODULUS
)
4745 return(KERN_SUCCESS
);
4750 * Allocate and initialise the new map.
4753 kret
= kmem_alloc(kernel_map
, (vm_offset_t
*)&cmap
,
4754 (nsize
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
4755 if (kret
!= KERN_SUCCESS
)
4757 cmap
->scm_magic
= DRT_SCM_MAGIC
;
4758 cmap
->scm_modulus
= nsize
;
4759 cmap
->scm_buckets
= 0;
4760 cmap
->scm_lastclean
= 0;
4761 cmap
->scm_iskips
= 0;
4762 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
4763 DRT_HASH_CLEAR(cmap
, i
);
4764 DRT_HASH_VACATE(cmap
, i
);
4765 DRT_BITVECTOR_CLEAR(cmap
, i
);
4769 * If there's an old map, re-hash entries from it into the new map.
4772 if (ocmap
!= NULL
) {
4773 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
4774 /* skip empty buckets */
4775 if (DRT_HASH_VACANT(ocmap
, i
) ||
4776 (DRT_HASH_GET_COUNT(ocmap
, i
) == 0))
4779 offset
= DRT_HASH_GET_ADDRESS(ocmap
, i
);
4780 kret
= vfs_drt_get_index(&cmap
, offset
, &index
, 1);
4781 if (kret
!= KERN_SUCCESS
) {
4782 /* XXX need to bail out gracefully here */
4783 panic("vfs_drt: new cluster map mysteriously too small");
4786 DRT_HASH_COPY(ocmap
, i
, cmap
, index
);
4791 /* log what we've done */
4792 vfs_drt_trace(cmap
, DRT_DEBUG_ALLOC
, copycount
, 0, 0, 0);
4795 * It's important to ensure that *cmapp always points to
4796 * a valid map, so we must overwrite it before freeing
4800 if (ocmap
!= NULL
) {
4801 /* emit stats into trace buffer */
4802 vfs_drt_trace(ocmap
, DRT_DEBUG_SCMDATA
,
4805 ocmap
->scm_lastclean
,
4808 vfs_drt_free_map(ocmap
);
4810 return(KERN_SUCCESS
);
4815 * Free a sparse cluster map.
4817 static kern_return_t
4818 vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
)
4820 kmem_free(kernel_map
, (vm_offset_t
)cmap
,
4821 (cmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
4822 return(KERN_SUCCESS
);
4827 * Find the hashtable slot currently occupied by an entry for the supplied offset.
4829 static kern_return_t
4830 vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
, u_int64_t offset
, int *indexp
)
4834 offset
= DRT_ALIGN_ADDRESS(offset
);
4835 index
= DRT_HASH(cmap
, offset
);
4837 /* traverse the hashtable */
4838 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
4841 * If the slot is vacant, we can stop.
4843 if (DRT_HASH_VACANT(cmap
, index
))
4847 * If the address matches our offset, we have success.
4849 if (DRT_HASH_GET_ADDRESS(cmap
, index
) == offset
) {
4851 return(KERN_SUCCESS
);
4855 * Move to the next slot, try again.
4857 index
= DRT_HASH_NEXT(cmap
, index
);
4862 return(KERN_FAILURE
);
4866 * Find the hashtable slot for the supplied offset. If we haven't allocated
4867 * one yet, allocate one and populate the address field. Note that it will
4868 * not have a nonzero page count and thus will still technically be free, so
4869 * in the case where we are called to clean pages, the slot will remain free.
4871 static kern_return_t
4872 vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
, u_int64_t offset
, int *indexp
, int recursed
)
4874 struct vfs_drt_clustermap
*cmap
;
4880 /* look for an existing entry */
4881 kret
= vfs_drt_search_index(cmap
, offset
, indexp
);
4882 if (kret
== KERN_SUCCESS
)
4885 /* need to allocate an entry */
4886 offset
= DRT_ALIGN_ADDRESS(offset
);
4887 index
= DRT_HASH(cmap
, offset
);
4889 /* scan from the index forwards looking for a vacant slot */
4890 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
4892 if (DRT_HASH_VACANT(cmap
, index
) || DRT_HASH_GET_COUNT(cmap
,index
) == 0) {
4893 cmap
->scm_buckets
++;
4894 if (index
< cmap
->scm_lastclean
)
4895 cmap
->scm_lastclean
= index
;
4896 DRT_HASH_SET_ADDRESS(cmap
, index
, offset
);
4897 DRT_HASH_SET_COUNT(cmap
, index
, 0);
4898 DRT_BITVECTOR_CLEAR(cmap
, index
);
4900 vfs_drt_trace(cmap
, DRT_DEBUG_INSERT
, (int)offset
, i
, 0, 0);
4901 return(KERN_SUCCESS
);
4903 cmap
->scm_iskips
+= i
;
4904 index
= DRT_HASH_NEXT(cmap
, index
);
4908 * We haven't found a vacant slot, so the map is full. If we're not
4909 * already recursed, try reallocating/compacting it.
4912 return(KERN_FAILURE
);
4913 kret
= vfs_drt_alloc_map(cmapp
);
4914 if (kret
== KERN_SUCCESS
) {
4915 /* now try to insert again */
4916 kret
= vfs_drt_get_index(cmapp
, offset
, indexp
, 1);
4922 * Implementation of set dirty/clean.
4924 * In the 'clean' case, not finding a map is OK.
4926 static kern_return_t
4927 vfs_drt_do_mark_pages(
4934 struct vfs_drt_clustermap
*cmap
, **cmapp
;
4936 int i
, index
, pgoff
, pgcount
, setcount
, ecount
;
4938 cmapp
= (struct vfs_drt_clustermap
**)private;
4941 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_START
, (int)offset
, (int)length
, dirty
, 0);
4943 if (setcountp
!= NULL
)
4946 /* allocate a cluster map if we don't already have one */
4948 /* no cluster map, nothing to clean */
4950 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 1, 0, 0, 0);
4951 return(KERN_SUCCESS
);
4953 kret
= vfs_drt_alloc_map(cmapp
);
4954 if (kret
!= KERN_SUCCESS
) {
4955 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 2, 0, 0, 0);
4962 * Iterate over the length of the region.
4964 while (length
> 0) {
4966 * Get the hashtable index for this offset.
4968 * XXX this will add blank entries if we are clearing a range
4969 * that hasn't been dirtied.
4971 kret
= vfs_drt_get_index(cmapp
, offset
, &index
, 0);
4972 cmap
= *cmapp
; /* may have changed! */
4973 /* this may be a partial-success return */
4974 if (kret
!= KERN_SUCCESS
) {
4975 if (setcountp
!= NULL
)
4976 *setcountp
= setcount
;
4977 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 3, (int)length
, 0, 0);
4983 * Work out how many pages we're modifying in this
4986 pgoff
= (offset
- DRT_ALIGN_ADDRESS(offset
)) / PAGE_SIZE
;
4987 pgcount
= min((length
/ PAGE_SIZE
), (DRT_BITVECTOR_PAGES
- pgoff
));
4990 * Iterate over pages, dirty/clearing as we go.
4992 ecount
= DRT_HASH_GET_COUNT(cmap
, index
);
4993 for (i
= 0; i
< pgcount
; i
++) {
4995 if (!DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
4996 DRT_HASH_SET_BIT(cmap
, index
, pgoff
+ i
);
5001 if (DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
5002 DRT_HASH_CLEAR_BIT(cmap
, index
, pgoff
+ i
);
5008 DRT_HASH_SET_COUNT(cmap
, index
, ecount
);
5010 offset
+= pgcount
* PAGE_SIZE
;
5011 length
-= pgcount
* PAGE_SIZE
;
5013 if (setcountp
!= NULL
)
5014 *setcountp
= setcount
;
5016 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 0, setcount
, 0, 0);
5018 return(KERN_SUCCESS
);
5022 * Mark a set of pages as dirty/clean.
5024 * This is a public interface.
5027 * Pointer to storage suitable for holding a pointer. Note that
5028 * this must either be NULL or a value set by this function.
5031 * Current file size in bytes.
5034 * Offset of the first page to be marked as dirty, in bytes. Must be
5038 * Length of dirty region, in bytes. Must be a multiple of PAGE_SIZE.
5041 * Number of pages newly marked dirty by this call (optional).
5043 * Returns KERN_SUCCESS if all the pages were successfully marked.
5045 static kern_return_t
5046 vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, int *setcountp
)
5048 /* XXX size unused, drop from interface */
5049 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, setcountp
, 1));
5053 static kern_return_t
5054 vfs_drt_unmark_pages(void **cmapp
, off_t offset
, u_int length
)
5056 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0));
5061 * Get a cluster of dirty pages.
5063 * This is a public interface.
5066 * Pointer to storage managed by drt_mark_pages. Note that this must
5067 * be NULL or a value set by drt_mark_pages.
5070 * Returns the byte offset into the file of the first page in the cluster.
5073 * Returns the length in bytes of the cluster of dirty pages.
5075 * Returns success if a cluster was found. If KERN_FAILURE is returned, there
5076 * are no dirty pages meeting the minmum size criteria. Private storage will
5077 * be released if there are no more dirty pages left in the map
5080 static kern_return_t
5081 vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
)
5083 struct vfs_drt_clustermap
*cmap
;
5086 int index
, i
, j
, fs
, ls
;
5089 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
5090 return(KERN_FAILURE
);
5093 /* walk the hashtable */
5094 for (offset
= 0, j
= 0; j
< cmap
->scm_modulus
; offset
+= (DRT_BITVECTOR_PAGES
* PAGE_SIZE
), j
++) {
5095 index
= DRT_HASH(cmap
, offset
);
5097 if (DRT_HASH_VACANT(cmap
, index
) || (DRT_HASH_GET_COUNT(cmap
, index
) == 0))
5100 /* scan the bitfield for a string of bits */
5103 for (i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
5104 if (DRT_HASH_TEST_BIT(cmap
, index
, i
)) {
5110 /* didn't find any bits set */
5111 panic("vfs_drt: entry summary count > 0 but no bits set in map");
5113 for (ls
= 0; i
< DRT_BITVECTOR_PAGES
; i
++, ls
++) {
5114 if (!DRT_HASH_TEST_BIT(cmap
, index
, i
))
5118 /* compute offset and length, mark pages clean */
5119 offset
= DRT_HASH_GET_ADDRESS(cmap
, index
) + (PAGE_SIZE
* fs
);
5120 length
= ls
* PAGE_SIZE
;
5121 vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0);
5122 cmap
->scm_lastclean
= index
;
5124 /* return successful */
5125 *offsetp
= (off_t
)offset
;
5128 vfs_drt_trace(cmap
, DRT_DEBUG_RETCLUSTER
, (int)offset
, (int)length
, 0, 0);
5129 return(KERN_SUCCESS
);
5132 * We didn't find anything... hashtable is empty
5133 * emit stats into trace buffer and
5136 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
5139 cmap
->scm_lastclean
,
5142 vfs_drt_free_map(cmap
);
5145 return(KERN_FAILURE
);
5149 static kern_return_t
5150 vfs_drt_control(void **cmapp
, int op_type
)
5152 struct vfs_drt_clustermap
*cmap
;
5155 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
5156 return(KERN_FAILURE
);
5161 /* emit stats into trace buffer */
5162 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
5165 cmap
->scm_lastclean
,
5168 vfs_drt_free_map(cmap
);
5173 cmap
->scm_lastclean
= 0;
5176 return(KERN_SUCCESS
);
5182 * Emit a summary of the state of the clustermap into the trace buffer
5183 * along with some caller-provided data.
5187 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, int code
, int arg1
, int arg2
, int arg3
, int arg4
)
5189 KERNEL_DEBUG(code
, arg1
, arg2
, arg3
, arg4
, 0);
5193 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, __unused
int code
,
5194 __unused
int arg1
, __unused
int arg2
, __unused
int arg3
,
5202 * Perform basic sanity check on the hash entry summary count
5203 * vs. the actual bits set in the entry.
5206 vfs_drt_sanity(struct vfs_drt_clustermap
*cmap
)
5211 for (index
= 0; index
< cmap
->scm_modulus
; index
++) {
5212 if (DRT_HASH_VACANT(cmap
, index
))
5215 for (bits_on
= 0, i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
5216 if (DRT_HASH_TEST_BIT(cmap
, index
, i
))
5219 if (bits_on
!= DRT_HASH_GET_COUNT(cmap
, index
))
5220 panic("bits_on = %d, index = %d\n", bits_on
, index
);