2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)vfs_cluster.c 8.10 (Berkeley) 3/28/95
58 #include <sys/param.h>
59 #include <sys/proc_internal.h>
60 #include <sys/buf_internal.h>
61 #include <sys/mount_internal.h>
62 #include <sys/vnode_internal.h>
63 #include <sys/trace.h>
64 #include <sys/malloc.h>
66 #include <sys/kernel.h>
67 #include <sys/resourcevar.h>
68 #include <sys/uio_internal.h>
69 #include <libkern/libkern.h>
70 #include <machine/machine_routines.h>
72 #include <sys/ubc_internal.h>
74 #include <mach/mach_types.h>
75 #include <mach/memory_object_types.h>
76 #include <mach/vm_map.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_pageout.h>
83 #include <sys/kdebug.h>
88 #define CL_COMMIT 0x04
89 #define CL_PAGEOUT 0x10
92 #define CL_NOZERO 0x80
93 #define CL_PAGEIN 0x100
94 #define CL_DEV_MEMORY 0x200
95 #define CL_PRESERVE 0x400
96 #define CL_THROTTLE 0x800
97 #define CL_KEEPCACHED 0x1000
101 u_int io_completed
; /* amount of io that has currently completed */
102 u_int io_issued
; /* amount of io that was successfully issued */
103 int io_error
; /* error code of first error encountered */
104 int io_wanted
; /* someone is sleeping waiting for a change in state */
107 static lck_grp_t
*cl_mtx_grp
;
108 static lck_attr_t
*cl_mtx_attr
;
109 static lck_grp_attr_t
*cl_mtx_grp_attr
;
110 static lck_mtx_t
*cl_mtxp
;
113 static int cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
114 int flags
, buf_t real_bp
, struct clios
*iostate
);
115 static int cluster_iodone(buf_t bp
, void *dummy
);
116 static int cluster_rd_prefetch(vnode_t vp
, off_t f_offset
, u_int size
, off_t filesize
);
117 static int cluster_hard_throttle_on(vnode_t vp
);
119 static int cluster_read_x(vnode_t vp
, struct uio
*uio
, off_t filesize
, int flags
);
120 static int cluster_write_x(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
,
121 off_t headOff
, off_t tailOff
, int flags
);
122 static int cluster_nocopy_read(vnode_t vp
, struct uio
*uio
, off_t filesize
);
123 static int cluster_nocopy_write(vnode_t vp
, struct uio
*uio
, off_t newEOF
);
124 static int cluster_phys_read(vnode_t vp
, struct uio
*uio
, off_t filesize
);
125 static int cluster_phys_write(vnode_t vp
, struct uio
*uio
, off_t newEOF
);
126 static int cluster_align_phys_io(vnode_t vp
, struct uio
*uio
, addr64_t usr_paddr
, int xsize
, int flags
);
128 static void cluster_rd_ahead(vnode_t vp
, struct cl_extent
*extent
, off_t filesize
, struct cl_readahead
*ra
);
130 static int cluster_push_x(vnode_t vp
, struct cl_extent
*, off_t EOF
, int flags
);
131 static void cluster_push_EOF(vnode_t vp
, off_t EOF
);
133 static int cluster_try_push(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int can_delay
, int push_all
);
135 static void sparse_cluster_switch(struct cl_writebehind
*, vnode_t vp
, off_t EOF
);
136 static void sparse_cluster_push(struct cl_writebehind
*, vnode_t vp
, off_t EOF
, int push_all
);
137 static void sparse_cluster_add(struct cl_writebehind
*, vnode_t vp
, struct cl_extent
*, off_t EOF
);
139 static kern_return_t
vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, int *setcountp
);
140 static kern_return_t
vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
);
141 static kern_return_t
vfs_drt_control(void **cmapp
, int op_type
);
143 int is_file_clean(vnode_t
, off_t
);
146 * throttle the number of async writes that
147 * can be outstanding on a single vnode
148 * before we issue a synchronous write
150 #define HARD_THROTTLE_MAXCNT 0
151 #define HARD_THROTTLE_MAXSIZE (64 * 1024)
153 int hard_throttle_on_root
= 0;
154 struct timeval priority_IO_timestamp_for_root
;
160 * allocate lock group attribute and group
162 cl_mtx_grp_attr
= lck_grp_attr_alloc_init();
163 //lck_grp_attr_setstat(cl_mtx_grp_attr);
164 cl_mtx_grp
= lck_grp_alloc_init("cluster I/O", cl_mtx_grp_attr
);
167 * allocate the lock attribute
169 cl_mtx_attr
= lck_attr_alloc_init();
170 //lck_attr_setdebug(clf_mtx_attr);
173 * allocate and initialize mutex's used to protect updates and waits
174 * on the cluster_io context
176 cl_mtxp
= lck_mtx_alloc_init(cl_mtx_grp
, cl_mtx_attr
);
179 panic("cluster_init: failed to allocate cl_mtxp");
184 #define CLW_ALLOCATE 0x01
185 #define CLW_RETURNLOCKED 0x02
187 * if the read ahead context doesn't yet exist,
188 * allocate and initialize it...
189 * the vnode lock serializes multiple callers
190 * during the actual assignment... first one
191 * to grab the lock wins... the other callers
192 * will release the now unnecessary storage
194 * once the context is present, try to grab (but don't block on)
195 * the lock associated with it... if someone
196 * else currently owns it, than the read
197 * will run without read-ahead. this allows
198 * multiple readers to run in parallel and
199 * since there's only 1 read ahead context,
200 * there's no real loss in only allowing 1
201 * reader to have read-ahead enabled.
203 static struct cl_readahead
*
204 cluster_get_rap(vnode_t vp
)
206 struct ubc_info
*ubc
;
207 struct cl_readahead
*rap
;
211 if ((rap
= ubc
->cl_rahead
) == NULL
) {
212 MALLOC_ZONE(rap
, struct cl_readahead
*, sizeof *rap
, M_CLRDAHEAD
, M_WAITOK
);
214 bzero(rap
, sizeof *rap
);
216 lck_mtx_init(&rap
->cl_lockr
, cl_mtx_grp
, cl_mtx_attr
);
220 if (ubc
->cl_rahead
== NULL
)
221 ubc
->cl_rahead
= rap
;
223 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
224 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
225 rap
= ubc
->cl_rahead
;
229 if (lck_mtx_try_lock(&rap
->cl_lockr
) == TRUE
)
232 return ((struct cl_readahead
*)NULL
);
237 * if the write behind context doesn't yet exist,
238 * and CLW_ALLOCATE is specified, allocate and initialize it...
239 * the vnode lock serializes multiple callers
240 * during the actual assignment... first one
241 * to grab the lock wins... the other callers
242 * will release the now unnecessary storage
244 * if CLW_RETURNLOCKED is set, grab (blocking if necessary)
245 * the lock associated with the write behind context before
249 static struct cl_writebehind
*
250 cluster_get_wbp(vnode_t vp
, int flags
)
252 struct ubc_info
*ubc
;
253 struct cl_writebehind
*wbp
;
257 if ((wbp
= ubc
->cl_wbehind
) == NULL
) {
259 if ( !(flags
& CLW_ALLOCATE
))
260 return ((struct cl_writebehind
*)NULL
);
262 MALLOC_ZONE(wbp
, struct cl_writebehind
*, sizeof *wbp
, M_CLWRBEHIND
, M_WAITOK
);
264 bzero(wbp
, sizeof *wbp
);
265 lck_mtx_init(&wbp
->cl_lockw
, cl_mtx_grp
, cl_mtx_attr
);
269 if (ubc
->cl_wbehind
== NULL
)
270 ubc
->cl_wbehind
= wbp
;
272 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
273 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
274 wbp
= ubc
->cl_wbehind
;
278 if (flags
& CLW_RETURNLOCKED
)
279 lck_mtx_lock(&wbp
->cl_lockw
);
286 cluster_hard_throttle_on(vnode_t vp
)
288 static struct timeval hard_throttle_maxelapsed
= { 0, 200000 };
290 if (vp
->v_mount
->mnt_kern_flag
& MNTK_ROOTDEV
) {
291 struct timeval elapsed
;
293 if (hard_throttle_on_root
)
296 microuptime(&elapsed
);
297 timevalsub(&elapsed
, &priority_IO_timestamp_for_root
);
299 if (timevalcmp(&elapsed
, &hard_throttle_maxelapsed
, <))
307 cluster_iodone(buf_t bp
, __unused
void *dummy
)
320 struct clios
*iostate
;
324 cbp_head
= (buf_t
)(bp
->b_trans_head
);
326 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_START
,
327 (int)cbp_head
, bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
329 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
) {
331 * all I/O requests that are part of this transaction
332 * have to complete before we can process it
334 if ( !(cbp
->b_flags
& B_DONE
)) {
336 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
337 (int)cbp_head
, (int)cbp
, cbp
->b_bcount
, cbp
->b_flags
, 0);
347 upl_offset
= cbp
->b_uploffset
;
349 b_flags
= cbp
->b_flags
;
350 real_bp
= cbp
->b_real_bp
;
351 zero_offset
= cbp
->b_validend
;
352 iostate
= (struct clios
*)cbp
->b_iostate
;
355 real_bp
->b_dev
= cbp
->b_dev
;
358 if ((cbp
->b_flags
& B_ERROR
) && error
== 0)
359 error
= cbp
->b_error
;
361 total_resid
+= cbp
->b_resid
;
362 total_size
+= cbp
->b_bcount
;
364 cbp_next
= cbp
->b_trans_next
;
371 cluster_zero(upl
, zero_offset
, PAGE_SIZE
- (zero_offset
& PAGE_MASK
), real_bp
);
377 * someone has issued multiple I/Os asynchrounsly
378 * and is waiting for them to complete (streaming)
380 lck_mtx_lock(cl_mtxp
);
382 if (error
&& iostate
->io_error
== 0)
383 iostate
->io_error
= error
;
385 iostate
->io_completed
+= total_size
;
387 if (iostate
->io_wanted
) {
389 * someone is waiting for the state of
390 * this io stream to change
392 iostate
->io_wanted
= 0;
395 lck_mtx_unlock(cl_mtxp
);
398 wakeup((caddr_t
)&iostate
->io_wanted
);
400 if ((b_flags
& B_NEED_IODONE
) && real_bp
) {
402 real_bp
->b_flags
|= B_ERROR
;
403 real_bp
->b_error
= error
;
405 real_bp
->b_resid
= total_resid
;
407 buf_biodone(real_bp
);
409 if (error
== 0 && total_resid
)
412 if (b_flags
& B_COMMIT_UPL
) {
413 pg_offset
= upl_offset
& PAGE_MASK
;
414 commit_size
= (pg_offset
+ total_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
416 if (error
|| (b_flags
& B_NOCACHE
)) {
421 if (b_flags
& B_PAGEIO
) {
422 if (b_flags
& B_READ
)
427 if (b_flags
& B_CACHE
) /* leave pages in the cache unchanged on error */
428 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
429 else if (page_out
&& (error
!= ENXIO
)) /* transient error */
430 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
432 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
434 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
436 ubc_upl_abort_range(upl
, upl_offset
- pg_offset
, commit_size
,
439 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
440 (int)upl
, upl_offset
- pg_offset
, commit_size
,
441 0x80000000|upl_abort_code
, 0);
444 int upl_commit_flags
= UPL_COMMIT_FREE_ON_EMPTY
;
446 if ((b_flags
& B_PHYS
) && (b_flags
& B_READ
))
447 upl_commit_flags
|= UPL_COMMIT_SET_DIRTY
;
450 upl_commit_flags
|= UPL_COMMIT_INACTIVATE
;
452 ubc_upl_commit_range(upl
, upl_offset
- pg_offset
, commit_size
,
455 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
456 (int)upl
, upl_offset
- pg_offset
, commit_size
,
457 upl_commit_flags
, 0);
460 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 20)) | DBG_FUNC_END
,
461 (int)upl
, upl_offset
, 0, error
, 0);
469 cluster_zero(upl_t upl
, vm_offset_t upl_offset
, int size
, buf_t bp
)
473 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_START
,
474 upl_offset
, size
, (int)bp
, 0, 0);
476 if (bp
== NULL
|| bp
->b_datap
== 0) {
478 pl
= ubc_upl_pageinfo(upl
);
486 page_index
= upl_offset
/ PAGE_SIZE
;
487 page_offset
= upl_offset
& PAGE_MASK
;
489 zero_addr
= ((addr64_t
)upl_phys_page(pl
, page_index
) << 12) + page_offset
;
490 zero_cnt
= min(PAGE_SIZE
- page_offset
, size
);
492 bzero_phys(zero_addr
, zero_cnt
);
495 upl_offset
+= zero_cnt
;
498 bzero((caddr_t
)((vm_offset_t
)bp
->b_datap
+ upl_offset
), size
);
500 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 23)) | DBG_FUNC_END
,
501 upl_offset
, size
, 0, 0, 0);
506 cluster_io(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
, int non_rounded_size
,
507 int flags
, buf_t real_bp
, struct clios
*iostate
)
516 buf_t cbp_head
= NULL
;
517 buf_t cbp_tail
= NULL
;
525 int async_throttle
= 0;
530 if (mp
->mnt_devblocksize
> 1) {
532 * round the requested size up so that this I/O ends on a
533 * page boundary in case this is a 'write'... if the filesystem
534 * has blocks allocated to back the page beyond the EOF, we want to
535 * make sure to write out the zero's that are sitting beyond the EOF
536 * so that in case the filesystem doesn't explicitly zero this area
537 * if a hole is created via a lseek/write beyond the current EOF,
538 * it will return zeros when it's read back from the disk. If the
539 * physical allocation doesn't extend for the whole page, we'll
540 * only write/read from the disk up to the end of this allocation
541 * via the extent info returned from the VNOP_BLOCKMAP call.
543 pg_offset
= upl_offset
& PAGE_MASK
;
545 size
= (((non_rounded_size
+ pg_offset
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - pg_offset
;
548 * anyone advertising a blocksize of 1 byte probably
549 * can't deal with us rounding up the request size
550 * AFP is one such filesystem/device
552 size
= non_rounded_size
;
554 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_START
,
555 (int)f_offset
, size
, upl_offset
, flags
, 0);
557 if (flags
& CL_READ
) {
559 bmap_flags
= VNODE_READ
;
561 max_iosize
= mp
->mnt_maxreadcnt
;
562 max_vectors
= mp
->mnt_segreadcnt
;
565 bmap_flags
= VNODE_WRITE
;
567 max_iosize
= mp
->mnt_maxwritecnt
;
568 max_vectors
= mp
->mnt_segwritecnt
;
570 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_NONE
, max_iosize
, max_vectors
, mp
->mnt_devblocksize
, 0, 0);
573 * make sure the maximum iosize is a
574 * multiple of the page size
576 max_iosize
&= ~PAGE_MASK
;
578 if (flags
& CL_THROTTLE
) {
579 if ( !(flags
& CL_PAGEOUT
) && cluster_hard_throttle_on(vp
)) {
580 if (max_iosize
> HARD_THROTTLE_MAXSIZE
)
581 max_iosize
= HARD_THROTTLE_MAXSIZE
;
582 async_throttle
= HARD_THROTTLE_MAXCNT
;
584 async_throttle
= VNODE_ASYNC_THROTTLE
;
589 io_flags
|= B_NOCACHE
;
590 if (flags
& (CL_PAGEIN
| CL_PAGEOUT
))
591 io_flags
|= B_PAGEIO
;
592 if (flags
& CL_COMMIT
)
593 io_flags
|= B_COMMIT_UPL
;
594 if (flags
& CL_PRESERVE
)
596 if (flags
& CL_KEEPCACHED
)
599 if ((flags
& CL_READ
) && ((upl_offset
+ non_rounded_size
) & PAGE_MASK
) && (!(flags
& CL_NOZERO
))) {
601 * then we are going to end up
602 * with a page that we can't complete (the file size wasn't a multiple
603 * of PAGE_SIZE and we're trying to read to the end of the file
604 * so we'll go ahead and zero out the portion of the page we can't
605 * read in from the file
607 zero_offset
= upl_offset
+ non_rounded_size
;
614 if (size
> max_iosize
)
615 io_size
= max_iosize
;
619 if ((error
= VNOP_BLOCKMAP(vp
, f_offset
, io_size
, &blkno
, (size_t *)&io_size
, NULL
, bmap_flags
, NULL
))) {
622 if (real_bp
&& (real_bp
->b_blkno
== real_bp
->b_lblkno
))
623 real_bp
->b_blkno
= blkno
;
625 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 24)) | DBG_FUNC_NONE
,
626 (int)f_offset
, (int)blkno
, io_size
, zero_offset
, 0);
630 * vnop_blockmap didn't return an error... however, it did
631 * return an extent size of 0 which means we can't
632 * make forward progress on this I/O... a hole in the
633 * file would be returned as a blkno of -1 with a non-zero io_size
634 * a real extent is returned with a blkno != -1 and a non-zero io_size
639 if ( !(flags
& CL_READ
) && blkno
== -1) {
643 * we're writing into a 'hole'
645 if (flags
& CL_PAGEOUT
) {
647 * if we got here via cluster_pageout
648 * then just error the request and return
649 * the 'hole' should already have been covered
654 if ( !(flags
& CL_COMMIT
)) {
656 * currently writes always request the commit to happen
657 * as part of the io completion... however, if the CL_COMMIT
658 * flag isn't specified, than we can't issue the abort_range
659 * since the call site is going to abort or commit the same upl..
660 * in this case we can only return an error
666 * we can get here if the cluster code happens to
667 * pick up a page that was dirtied via mmap vs
668 * a 'write' and the page targets a 'hole'...
669 * i.e. the writes to the cluster were sparse
670 * and the file was being written for the first time
672 * we can also get here if the filesystem supports
673 * 'holes' that are less than PAGE_SIZE.... because
674 * we can't know if the range in the page that covers
675 * the 'hole' has been dirtied via an mmap or not,
676 * we have to assume the worst and try to push the
677 * entire page to storage.
679 * Try paging out the page individually before
680 * giving up entirely and dumping it (the pageout
681 * path will insure that the zero extent accounting
682 * has been taken care of before we get back into cluster_io)
684 ubc_upl_abort_range(upl
, trunc_page(upl_offset
), PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
686 e_offset
= round_page_64(f_offset
+ 1);
688 if (ubc_sync_range(vp
, f_offset
, e_offset
, UBC_PUSHDIRTY
) == 0) {
692 io_size
= e_offset
- f_offset
;
695 upl_offset
+= io_size
;
702 * keep track of how much of the original request
703 * that we've actually completed... non_rounded_size
704 * may go negative due to us rounding the request
705 * to a page size multiple (i.e. size > non_rounded_size)
707 non_rounded_size
-= io_size
;
709 if (non_rounded_size
<= 0) {
711 * we've transferred all of the data in the original
712 * request, but we were unable to complete the tail
713 * of the last page because the file didn't have
714 * an allocation to back that portion... this is ok.
720 lblkno
= (daddr64_t
)(f_offset
/ PAGE_SIZE_64
);
722 * we have now figured out how much I/O we can do - this is in 'io_size'
723 * pg_offset is the starting point in the first page for the I/O
724 * pg_count is the number of full and partial pages that 'io_size' encompasses
726 pg_offset
= upl_offset
& PAGE_MASK
;
728 if (flags
& CL_DEV_MEMORY
) {
730 * currently, can't deal with reading 'holes' in file
737 * treat physical requests as one 'giant' page
741 pg_count
= (io_size
+ pg_offset
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
743 if ((flags
& CL_READ
) && blkno
== -1) {
747 * if we're reading and blkno == -1, then we've got a
748 * 'hole' in the file that we need to deal with by zeroing
749 * out the affected area in the upl
751 if (zero_offset
&& io_size
== size
) {
753 * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
754 * than 'zero_offset' will be non-zero
755 * if the 'hole' returned by vnop_blockmap extends all the way to the eof
756 * (indicated by the io_size finishing off the I/O request for this UPL)
757 * than we're not going to issue an I/O for the
758 * last page in this upl... we need to zero both the hole and the tail
759 * of the page beyond the EOF, since the delayed zero-fill won't kick in
761 bytes_to_zero
= (((upl_offset
+ io_size
) + (PAGE_SIZE
- 1)) & ~PAGE_MASK
) - upl_offset
;
765 bytes_to_zero
= io_size
;
767 cluster_zero(upl
, upl_offset
, bytes_to_zero
, real_bp
);
771 * if there is a current I/O chain pending
772 * then the first page of the group we just zero'd
773 * will be handled by the I/O completion if the zero
774 * fill started in the middle of the page
776 pg_count
= (io_size
- pg_offset
) / PAGE_SIZE
;
779 * no pending I/O to pick up that first page
780 * so, we have to make sure it gets committed
782 * set the pg_offset to 0 so that the upl_commit_range
783 * starts with this page
785 pg_count
= (io_size
+ pg_offset
) / PAGE_SIZE
;
788 if (io_size
== size
&& ((upl_offset
+ io_size
) & PAGE_MASK
))
790 * if we're done with the request for this UPL
791 * then we have to make sure to commit the last page
792 * even if we only partially zero-filled it
798 pg_resid
= PAGE_SIZE
- pg_offset
;
802 if (flags
& CL_COMMIT
)
803 ubc_upl_commit_range(upl
,
804 (upl_offset
+ pg_resid
) & ~PAGE_MASK
,
805 pg_count
* PAGE_SIZE
,
806 UPL_COMMIT_CLEAR_DIRTY
| UPL_COMMIT_FREE_ON_EMPTY
);
808 upl_offset
+= io_size
;
812 * keep track of how much of the original request
813 * that we've actually completed... non_rounded_size
814 * may go negative due to us rounding the request
815 * to a page size multiple (i.e. size > non_rounded_size)
817 non_rounded_size
-= io_size
;
819 if (non_rounded_size
<= 0) {
821 * we've transferred all of the data in the original
822 * request, but we were unable to complete the tail
823 * of the last page because the file didn't have
824 * an allocation to back that portion... this is ok.
828 if (cbp_head
&& pg_count
)
833 if (pg_count
> max_vectors
) {
834 if (((pg_count
- max_vectors
) * PAGE_SIZE
) > io_size
) {
835 io_size
= PAGE_SIZE
- pg_offset
;
838 io_size
-= (pg_count
- max_vectors
) * PAGE_SIZE
;
839 pg_count
= max_vectors
;
843 if ( !(mp
->mnt_kern_flag
& MNTK_VIRTUALDEV
))
845 * if we're not targeting a virtual device i.e. a disk image
846 * it's safe to dip into the reserve pool since real devices
847 * can complete this I/O request without requiring additional
848 * bufs from the alloc_io_buf pool
851 else if ((flags
& CL_ASYNC
) && !(flags
& CL_PAGEOUT
))
853 * Throttle the speculative IO
859 cbp
= alloc_io_buf(vp
, priv
);
861 if (flags
& CL_PAGEOUT
) {
864 for (i
= 0; i
< pg_count
; i
++) {
865 if (buf_invalblkno(vp
, lblkno
+ i
, 0) == EBUSY
)
866 panic("BUSY bp found in cluster_io");
869 if (flags
& CL_ASYNC
) {
870 if (buf_setcallback(cbp
, (void *)cluster_iodone
, NULL
))
871 panic("buf_setcallback failed\n");
873 cbp
->b_flags
|= io_flags
;
875 cbp
->b_lblkno
= lblkno
;
876 cbp
->b_blkno
= blkno
;
877 cbp
->b_bcount
= io_size
;
879 if (buf_setupl(cbp
, upl
, upl_offset
))
880 panic("buf_setupl failed\n");
882 cbp
->b_trans_next
= (buf_t
)NULL
;
884 if ((cbp
->b_iostate
= (void *)iostate
))
886 * caller wants to track the state of this
887 * io... bump the amount issued against this stream
889 iostate
->io_issued
+= io_size
;
891 if (flags
& CL_READ
) {
892 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 26)) | DBG_FUNC_NONE
,
893 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
896 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 27)) | DBG_FUNC_NONE
,
897 (int)cbp
->b_lblkno
, (int)cbp
->b_blkno
, upl_offset
, io_size
, 0);
901 cbp_tail
->b_trans_next
= cbp
;
907 (buf_t
)(cbp
->b_trans_head
) = cbp_head
;
910 upl_offset
+= io_size
;
914 * keep track of how much of the original request
915 * that we've actually completed... non_rounded_size
916 * may go negative due to us rounding the request
917 * to a page size multiple (i.e. size > non_rounded_size)
919 non_rounded_size
-= io_size
;
921 if (non_rounded_size
<= 0) {
923 * we've transferred all of the data in the original
924 * request, but we were unable to complete the tail
925 * of the last page because the file didn't have
926 * an allocation to back that portion... this is ok.
930 if ( (!(upl_offset
& PAGE_MASK
) && !(flags
& CL_DEV_MEMORY
) && ((flags
& CL_ASYNC
) || trans_count
> 8)) || size
== 0) {
932 * if we have no more I/O to issue or
933 * the current I/O we've prepared fully
934 * completes the last page in this request
935 * and it's either an ASYNC request or
936 * we've already accumulated more than 8 I/O's into
937 * this transaction and it's not an I/O directed to
938 * special DEVICE memory
939 * then go ahead and issue the I/O
943 cbp_head
->b_flags
|= B_NEED_IODONE
;
944 cbp_head
->b_real_bp
= real_bp
;
946 cbp_head
->b_real_bp
= (buf_t
)NULL
;
950 * we're about to issue the last I/O for this upl
951 * if this was a read to the eof and the eof doesn't
952 * finish on a page boundary, than we need to zero-fill
953 * the rest of the page....
955 cbp_head
->b_validend
= zero_offset
;
957 cbp_head
->b_validend
= 0;
959 if (flags
& CL_THROTTLE
)
960 (void)vnode_waitforwrites(vp
, async_throttle
, 0, 0, (char *)"cluster_io");
962 for (cbp
= cbp_head
; cbp
;) {
965 if ( !(io_flags
& B_READ
))
966 vnode_startwrite(vp
);
968 cbp_next
= cbp
->b_trans_next
;
970 (void) VNOP_STRATEGY(cbp
);
973 if ( !(flags
& CL_ASYNC
)) {
976 for (cbp
= cbp_head
; cbp
; cbp
= cbp
->b_trans_next
)
979 if ((error
= cluster_iodone(cbp_head
, (void *)&dummy
))) {
980 if (((flags
& (CL_PAGEOUT
| CL_KEEPCACHED
)) == CL_PAGEOUT
) && (error
== ENXIO
))
981 error
= 0; /* drop the error */
989 cbp_head
= (buf_t
)NULL
;
990 cbp_tail
= (buf_t
)NULL
;
1000 for (cbp
= cbp_head
; cbp
;) {
1003 upl_offset
-= cbp
->b_bcount
;
1004 size
+= cbp
->b_bcount
;
1005 io_size
+= cbp
->b_bcount
;
1007 cbp_next
= cbp
->b_trans_next
;
1012 int need_wakeup
= 0;
1015 * update the error condition for this stream
1016 * since we never really issued the io
1017 * just go ahead and adjust it back
1019 lck_mtx_lock(cl_mtxp
);
1021 if (iostate
->io_error
== 0)
1022 iostate
->io_error
= error
;
1023 iostate
->io_issued
-= io_size
;
1025 if (iostate
->io_wanted
) {
1027 * someone is waiting for the state of
1028 * this io stream to change
1030 iostate
->io_wanted
= 0;
1033 lck_mtx_unlock(cl_mtxp
);
1036 wakeup((caddr_t
)&iostate
->io_wanted
);
1038 pg_offset
= upl_offset
& PAGE_MASK
;
1039 abort_size
= (size
+ pg_offset
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1041 if (flags
& CL_COMMIT
) {
1044 if (flags
& CL_PRESERVE
) {
1045 ubc_upl_commit_range(upl
, upl_offset
- pg_offset
, abort_size
,
1046 UPL_COMMIT_FREE_ON_EMPTY
);
1048 if ((flags
& CL_PAGEOUT
) && (error
!= ENXIO
)) /* transient error */
1049 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
;
1050 else if (flags
& CL_PAGEIN
)
1051 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
;
1053 upl_abort_code
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
1055 ubc_upl_abort_range(upl
, upl_offset
- pg_offset
, abort_size
,
1058 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 28)) | DBG_FUNC_NONE
,
1059 (int)upl
, upl_offset
- pg_offset
, abort_size
, error
, 0);
1062 real_bp
->b_flags
|= B_ERROR
;
1063 real_bp
->b_error
= error
;
1065 buf_biodone(real_bp
);
1070 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 22)) | DBG_FUNC_END
,
1071 (int)f_offset
, size
, upl_offset
, retval
, 0);
1078 cluster_rd_prefetch(vnode_t vp
, off_t f_offset
, u_int size
, off_t filesize
)
1080 int pages_in_prefetch
;
1082 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_START
,
1083 (int)f_offset
, size
, (int)filesize
, 0, 0);
1085 if (f_offset
>= filesize
) {
1086 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1087 (int)f_offset
, 0, 0, 0, 0);
1090 if (size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
1091 size
= (MAX_UPL_TRANSFER
* PAGE_SIZE
);
1093 size
= (size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1095 if ((off_t
)size
> (filesize
- f_offset
))
1096 size
= filesize
- f_offset
;
1097 pages_in_prefetch
= (size
+ (PAGE_SIZE
- 1)) / PAGE_SIZE
;
1099 advisory_read(vp
, filesize
, f_offset
, size
);
1101 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 49)) | DBG_FUNC_END
,
1102 (int)f_offset
+ size
, pages_in_prefetch
, 0, 1, 0);
1104 return (pages_in_prefetch
);
1110 cluster_rd_ahead(vnode_t vp
, struct cl_extent
*extent
, off_t filesize
, struct cl_readahead
*rap
)
1114 int size_of_prefetch
;
1117 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_START
,
1118 (int)extent
->b_addr
, (int)extent
->e_addr
, (int)rap
->cl_lastr
, 0, 0);
1120 if (extent
->b_addr
== rap
->cl_lastr
&& extent
->b_addr
== extent
->e_addr
) {
1121 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1122 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 0, 0);
1125 if (rap
->cl_lastr
== -1 || (extent
->b_addr
!= rap
->cl_lastr
&& extent
->b_addr
!= (rap
->cl_lastr
+ 1) &&
1126 (extent
->b_addr
!= (rap
->cl_maxra
+ 1) || rap
->cl_ralen
== 0))) {
1130 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1131 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 1, 0);
1135 if (extent
->e_addr
< rap
->cl_maxra
) {
1136 if ((rap
->cl_maxra
- extent
->e_addr
) > (MAX_UPL_TRANSFER
/ 4)) {
1138 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1139 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 2, 0);
1143 r_addr
= max(extent
->e_addr
, rap
->cl_maxra
) + 1;
1144 f_offset
= (off_t
)(r_addr
* PAGE_SIZE_64
);
1146 size_of_prefetch
= 0;
1148 ubc_range_op(vp
, f_offset
, f_offset
+ PAGE_SIZE_64
, UPL_ROP_PRESENT
, &size_of_prefetch
);
1150 if (size_of_prefetch
) {
1151 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1152 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 3, 0);
1155 if (f_offset
< filesize
) {
1156 daddr64_t read_size
;
1158 rap
->cl_ralen
= rap
->cl_ralen
? min(MAX_UPL_TRANSFER
, rap
->cl_ralen
<< 1) : 1;
1160 read_size
= (extent
->e_addr
+ 1) - extent
->b_addr
;
1162 if (read_size
> rap
->cl_ralen
) {
1163 if (read_size
> MAX_UPL_TRANSFER
)
1164 rap
->cl_ralen
= MAX_UPL_TRANSFER
;
1166 rap
->cl_ralen
= read_size
;
1168 size_of_prefetch
= cluster_rd_prefetch(vp
, f_offset
, rap
->cl_ralen
* PAGE_SIZE
, filesize
);
1170 if (size_of_prefetch
)
1171 rap
->cl_maxra
= (r_addr
+ size_of_prefetch
) - 1;
1173 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 48)) | DBG_FUNC_END
,
1174 rap
->cl_ralen
, (int)rap
->cl_maxra
, (int)rap
->cl_lastr
, 4, 0);
1178 cluster_pageout(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
,
1179 int size
, off_t filesize
, int flags
)
1185 struct cl_writebehind
*wbp
;
1187 if (vp
->v_mount
->mnt_kern_flag
& MNTK_VIRTUALDEV
)
1189 * if we know we're issuing this I/O to a virtual device (i.e. disk image)
1190 * then we don't want to enforce this throttle... if we do, we can
1191 * potentially deadlock since we're stalling the pageout thread at a time
1192 * when the disk image might need additional memory (which won't be available
1193 * if the pageout thread can't run)... instead we'll just depend on the throttle
1194 * that the pageout thread now has in place to deal with external files
1196 local_flags
= CL_PAGEOUT
;
1198 local_flags
= CL_PAGEOUT
| CL_THROTTLE
;
1200 if ((flags
& UPL_IOSYNC
) == 0)
1201 local_flags
|= CL_ASYNC
;
1202 if ((flags
& UPL_NOCOMMIT
) == 0)
1203 local_flags
|= CL_COMMIT
;
1204 if ((flags
& UPL_KEEPCACHED
))
1205 local_flags
|= CL_KEEPCACHED
;
1208 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 52)) | DBG_FUNC_NONE
,
1209 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
1212 * If they didn't specify any I/O, then we are done...
1213 * we can't issue an abort because we don't know how
1214 * big the upl really is
1219 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
1220 if (local_flags
& CL_COMMIT
)
1221 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
1225 * can't page-in from a negative offset
1226 * or if we're starting beyond the EOF
1227 * or if the file offset isn't page aligned
1228 * or the size requested isn't a multiple of PAGE_SIZE
1230 if (f_offset
< 0 || f_offset
>= filesize
||
1231 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
)) {
1232 if (local_flags
& CL_COMMIT
)
1233 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
1236 max_size
= filesize
- f_offset
;
1238 if (size
< max_size
)
1243 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1245 if (size
> rounded_size
) {
1246 if (local_flags
& CL_COMMIT
)
1247 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
, size
- rounded_size
,
1248 UPL_ABORT_FREE_ON_EMPTY
);
1250 if ((wbp
= cluster_get_wbp(vp
, 0)) != NULL
)
1251 wbp
->cl_hasbeenpaged
= 1;
1253 return (cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
1254 local_flags
, (buf_t
)NULL
, (struct clios
*)NULL
));
1258 cluster_pagein(vnode_t vp
, upl_t upl
, vm_offset_t upl_offset
, off_t f_offset
,
1259 int size
, off_t filesize
, int flags
)
1265 int local_flags
= 0;
1267 if (upl
== NULL
|| size
< 0)
1268 panic("cluster_pagein: NULL upl passed in");
1270 if ((flags
& UPL_IOSYNC
) == 0)
1271 local_flags
|= CL_ASYNC
;
1272 if ((flags
& UPL_NOCOMMIT
) == 0)
1273 local_flags
|= CL_COMMIT
;
1276 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 56)) | DBG_FUNC_NONE
,
1277 (int)f_offset
, size
, (int)filesize
, local_flags
, 0);
1280 * can't page-in from a negative offset
1281 * or if we're starting beyond the EOF
1282 * or if the file offset isn't page aligned
1283 * or the size requested isn't a multiple of PAGE_SIZE
1285 if (f_offset
< 0 || f_offset
>= filesize
||
1286 (f_offset
& PAGE_MASK_64
) || (size
& PAGE_MASK
) || (upl_offset
& PAGE_MASK
)) {
1287 if (local_flags
& CL_COMMIT
)
1288 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
1291 max_size
= filesize
- f_offset
;
1293 if (size
< max_size
)
1298 rounded_size
= (io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
1300 if (size
> rounded_size
&& (local_flags
& CL_COMMIT
))
1301 ubc_upl_abort_range(upl
, upl_offset
+ rounded_size
,
1302 size
- rounded_size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
1304 retval
= cluster_io(vp
, upl
, upl_offset
, f_offset
, io_size
,
1305 local_flags
| CL_READ
| CL_PAGEIN
, (buf_t
)NULL
, (struct clios
*)NULL
);
1307 if (retval
== 0 && !(flags
& UPL_NORDAHEAD
) && !(vp
->v_flag
& VRAOFF
)) {
1308 struct cl_readahead
*rap
;
1310 rap
= cluster_get_rap(vp
);
1313 struct cl_extent extent
;
1315 extent
.b_addr
= (daddr64_t
)(f_offset
/ PAGE_SIZE_64
);
1316 extent
.e_addr
= (daddr64_t
)((f_offset
+ ((off_t
)io_size
- 1)) / PAGE_SIZE_64
);
1318 if (rounded_size
== PAGE_SIZE
) {
1320 * we haven't read the last page in of the file yet
1321 * so let's try to read ahead if we're in
1322 * a sequential access pattern
1324 cluster_rd_ahead(vp
, &extent
, filesize
, rap
);
1326 rap
->cl_lastr
= extent
.e_addr
;
1328 lck_mtx_unlock(&rap
->cl_lockr
);
1335 cluster_bp(buf_t bp
)
1340 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 19)) | DBG_FUNC_START
,
1341 (int)bp
, (int)bp
->b_lblkno
, bp
->b_bcount
, bp
->b_flags
, 0);
1343 if (bp
->b_flags
& B_READ
)
1344 flags
= CL_ASYNC
| CL_READ
;
1348 f_offset
= ubc_blktooff(bp
->b_vp
, bp
->b_lblkno
);
1350 return (cluster_io(bp
->b_vp
, bp
->b_upl
, 0, f_offset
, bp
->b_bcount
, flags
, bp
, (struct clios
*)NULL
));
1354 cluster_write(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
, int xflags
)
1367 if (vp
->v_flag
& VNOCACHE_DATA
)
1368 flags
|= IO_NOCACHE
;
1370 if ( (!(flags
& IO_NOCACHE
)) || (!uio
) || (!UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))) {
1372 * go do a write through the cache if one of the following is true....
1373 * NOCACHE is not true
1374 * there is no uio structure or it doesn't target USERSPACE
1376 return (cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
));
1380 if (IS_VALID_UIO_SEGFLG(uio
->uio_segflg
) == 0) {
1381 panic("%s :%d - invalid uio_segflg\n", __FILE__
, __LINE__
);
1383 #endif /* LP64_DEBUG */
1385 while (uio_resid(uio
) && uio
->uio_offset
< newEOF
&& retval
== 0) {
1386 user_size_t iov_len
;
1387 user_addr_t iov_base
;
1390 * we know we have a resid, so this is safe
1391 * skip over any emtpy vectors
1393 uio_update(uio
, (user_size_t
)0);
1395 iov_len
= uio_curriovlen(uio
);
1396 iov_base
= uio_curriovbase(uio
);
1398 upl_size
= PAGE_SIZE
;
1399 upl_flags
= UPL_QUERY_OBJECT_TYPE
;
1401 // LP64todo - fix this!
1402 if ((vm_map_get_upl(current_map(),
1403 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
1404 &upl_size
, &upl
, NULL
, NULL
, &upl_flags
, 0)) != KERN_SUCCESS
) {
1406 * the user app must have passed in an invalid address
1412 * We check every vector target but if it is physically
1413 * contiguous space, we skip the sanity checks.
1415 if (upl_flags
& UPL_PHYS_CONTIG
) {
1418 zflags
= flags
& ~IO_TAILZEROFILL
;
1419 zflags
|= IO_HEADZEROFILL
;
1421 if (flags
& IO_HEADZEROFILL
) {
1423 * in case we have additional vectors, we don't want to do this again
1425 flags
&= ~IO_HEADZEROFILL
;
1427 if ((retval
= cluster_write_x(vp
, (struct uio
*)0, 0, uio
->uio_offset
, headOff
, 0, zflags
)))
1430 retval
= cluster_phys_write(vp
, uio
, newEOF
);
1432 if (uio_resid(uio
) == 0 && (flags
& IO_TAILZEROFILL
)) {
1433 return (cluster_write_x(vp
, (struct uio
*)0, 0, tailOff
, uio
->uio_offset
, 0, zflags
));
1436 else if ((uio_resid(uio
) < PAGE_SIZE
) || (flags
& (IO_TAILZEROFILL
| IO_HEADZEROFILL
))) {
1438 * we're here because we're don't have a physically contiguous target buffer
1439 * go do a write through the cache if one of the following is true....
1440 * the total xfer size is less than a page...
1441 * we're being asked to ZEROFILL either the head or the tail of the I/O...
1443 return (cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
));
1445 // LP64todo - fix this!
1446 else if (((int)uio
->uio_offset
& PAGE_MASK
) || (CAST_DOWN(int, iov_base
) & PAGE_MASK
)) {
1447 if (((int)uio
->uio_offset
& PAGE_MASK
) == (CAST_DOWN(int, iov_base
) & PAGE_MASK
)) {
1449 * Bring the file offset write up to a pagesize boundary
1450 * this will also bring the base address to a page boundary
1451 * since they both are currently on the same offset within a page
1452 * note: if we get here, uio->uio_resid is greater than PAGE_SIZE
1453 * so the computed clip_size must always be less than the current uio_resid
1455 clip_size
= (PAGE_SIZE
- (uio
->uio_offset
& PAGE_MASK_64
));
1458 * Fake the resid going into the cluster_write_x call
1459 * and restore it on the way out.
1461 // LP64todo - fix this
1462 prev_resid
= uio_resid(uio
);
1463 uio_setresid(uio
, clip_size
);
1465 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
);
1467 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
1470 * can't get both the file offset and the buffer offset aligned to a page boundary
1471 * so fire an I/O through the cache for this entire vector
1473 // LP64todo - fix this
1474 clip_size
= iov_len
;
1475 // LP64todo - fix this
1476 prev_resid
= uio_resid(uio
);
1477 uio_setresid(uio
, clip_size
);
1479 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
);
1481 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
1485 * If we come in here, we know the offset into
1486 * the file is on a pagesize boundary and the
1487 * target buffer address is also on a page boundary
1489 max_io_size
= newEOF
- uio
->uio_offset
;
1490 // LP64todo - fix this
1491 clip_size
= uio_resid(uio
);
1492 if (iov_len
< clip_size
)
1493 // LP64todo - fix this!
1494 clip_size
= iov_len
;
1495 if (max_io_size
< clip_size
)
1496 clip_size
= max_io_size
;
1498 if (clip_size
< PAGE_SIZE
) {
1500 * Take care of tail end of write in this vector
1502 // LP64todo - fix this
1503 prev_resid
= uio_resid(uio
);
1504 uio_setresid(uio
, clip_size
);
1506 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
);
1508 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
1510 /* round clip_size down to a multiple of pagesize */
1511 clip_size
= clip_size
& ~(PAGE_MASK
);
1512 // LP64todo - fix this
1513 prev_resid
= uio_resid(uio
);
1514 uio_setresid(uio
, clip_size
);
1516 retval
= cluster_nocopy_write(vp
, uio
, newEOF
);
1518 if ((retval
== 0) && uio_resid(uio
))
1519 retval
= cluster_write_x(vp
, uio
, oldEOF
, newEOF
, headOff
, tailOff
, flags
);
1521 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
1531 cluster_nocopy_write(vnode_t vp
, struct uio
*uio
, off_t newEOF
)
1534 upl_page_info_t
*pl
;
1535 vm_offset_t upl_offset
;
1539 int upl_needed_size
;
1544 int force_data_sync
;
1546 struct clios iostate
;
1547 struct cl_writebehind
*wbp
;
1550 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_START
,
1551 (int)uio
->uio_offset
, (int)uio_resid(uio
),
1555 * When we enter this routine, we know
1556 * -- the offset into the file is on a pagesize boundary
1557 * -- the resid is a page multiple
1558 * -- the resid will not exceed iov_len
1561 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) != NULL
) {
1563 cluster_try_push(wbp
, vp
, newEOF
, 0, 1);
1565 lck_mtx_unlock(&wbp
->cl_lockw
);
1567 iostate
.io_completed
= 0;
1568 iostate
.io_issued
= 0;
1569 iostate
.io_error
= 0;
1570 iostate
.io_wanted
= 0;
1572 while (uio_resid(uio
) && uio
->uio_offset
< newEOF
&& error
== 0) {
1573 user_addr_t iov_base
;
1575 io_size
= uio_resid(uio
);
1577 if (io_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
1578 io_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
1580 iov_base
= uio_curriovbase(uio
);
1582 // LP64todo - fix this!
1583 upl_offset
= CAST_DOWN(vm_offset_t
, iov_base
) & PAGE_MASK
;
1585 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
1587 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_START
,
1588 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
1590 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
1592 upl_size
= upl_needed_size
;
1593 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
1594 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
1596 // LP64todo - fix this!
1597 kret
= vm_map_get_upl(current_map(),
1598 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
1606 if (kret
!= KERN_SUCCESS
) {
1607 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
1610 * cluster_nocopy_write: failed to get pagelist
1612 * we may have already spun some portion of this request
1613 * off as async requests... we need to wait for the I/O
1614 * to complete before returning
1616 goto wait_for_writes
;
1618 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
1619 pages_in_pl
= upl_size
/ PAGE_SIZE
;
1621 for (i
= 0; i
< pages_in_pl
; i
++) {
1622 if (!upl_valid_page(pl
, i
))
1625 if (i
== pages_in_pl
)
1629 * didn't get all the pages back that we
1630 * needed... release this upl and try again
1632 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
1633 UPL_ABORT_FREE_ON_EMPTY
);
1635 if (force_data_sync
>= 3) {
1636 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
1637 i
, pages_in_pl
, upl_size
, kret
, 0);
1639 * for some reason, we couldn't acquire a hold on all
1640 * the pages needed in the user's address space
1642 * we may have already spun some portion of this request
1643 * off as async requests... we need to wait for the I/O
1644 * to complete before returning
1646 goto wait_for_writes
;
1650 * Consider the possibility that upl_size wasn't satisfied.
1652 if (upl_size
!= upl_needed_size
)
1653 io_size
= (upl_size
- (int)upl_offset
) & ~PAGE_MASK
;
1655 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 76)) | DBG_FUNC_END
,
1656 (int)upl_offset
, upl_size
, (int)iov_base
, io_size
, 0);
1659 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
1660 UPL_ABORT_FREE_ON_EMPTY
);
1662 * we may have already spun some portion of this request
1663 * off as async requests... we need to wait for the I/O
1664 * to complete before returning
1666 goto wait_for_writes
;
1669 * Now look for pages already in the cache
1670 * and throw them away.
1671 * uio->uio_offset is page aligned within the file
1672 * io_size is a multiple of PAGE_SIZE
1674 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ io_size
, UPL_ROP_DUMP
, NULL
);
1677 * we want push out these writes asynchronously so that we can overlap
1678 * the preparation of the next I/O
1679 * if there are already too many outstanding writes
1680 * wait until some complete before issuing the next
1682 lck_mtx_lock(cl_mtxp
);
1684 while ((iostate
.io_issued
- iostate
.io_completed
) > (2 * MAX_UPL_TRANSFER
* PAGE_SIZE
)) {
1685 iostate
.io_wanted
= 1;
1686 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_nocopy_write", 0);
1688 lck_mtx_unlock(cl_mtxp
);
1690 if (iostate
.io_error
) {
1692 * one of the earlier writes we issued ran into a hard error
1693 * don't issue any more writes, cleanup the UPL
1694 * that was just created but not used, then
1695 * go wait for all writes that are part of this stream
1696 * to complete before returning the error to the caller
1698 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
,
1699 UPL_ABORT_FREE_ON_EMPTY
);
1701 goto wait_for_writes
;
1703 io_flag
= CL_ASYNC
| CL_PRESERVE
| CL_COMMIT
| CL_THROTTLE
;
1705 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_START
,
1706 (int)upl_offset
, (int)uio
->uio_offset
, io_size
, io_flag
, 0);
1708 error
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
1709 io_size
, io_flag
, (buf_t
)NULL
, &iostate
);
1711 uio_update(uio
, (user_size_t
)io_size
);
1713 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 77)) | DBG_FUNC_END
,
1714 (int)upl_offset
, (int)uio
->uio_offset
, (int)uio_resid(uio
), error
, 0);
1720 * make sure all async writes issued as part of this stream
1721 * have completed before we return
1723 lck_mtx_lock(cl_mtxp
);
1725 while (iostate
.io_issued
!= iostate
.io_completed
) {
1726 iostate
.io_wanted
= 1;
1727 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_nocopy_write", 0);
1729 lck_mtx_unlock(cl_mtxp
);
1731 if (iostate
.io_error
)
1732 error
= iostate
.io_error
;
1734 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 75)) | DBG_FUNC_END
,
1735 (int)uio
->uio_offset
, (int)uio
->uio_resid
, error
, 4, 0);
1742 cluster_phys_write(vnode_t vp
, struct uio
*uio
, off_t newEOF
)
1744 upl_page_info_t
*pl
;
1747 vm_offset_t upl_offset
;
1751 int upl_needed_size
;
1756 user_addr_t iov_base
;
1758 struct cl_writebehind
*wbp
;
1760 devblocksize
= vp
->v_mount
->mnt_devblocksize
;
1762 * When we enter this routine, we know
1763 * -- the resid will not exceed iov_len
1764 * -- the vector target address is physcially contiguous
1766 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) != NULL
) {
1768 cluster_try_push(wbp
, vp
, newEOF
, 0, 1);
1770 lck_mtx_unlock(&wbp
->cl_lockw
);
1773 if (IS_VALID_UIO_SEGFLG(uio
->uio_segflg
) == 0) {
1774 panic("%s :%d - invalid uio_segflg\n", __FILE__
, __LINE__
);
1776 #endif /* LP64_DEBUG */
1778 // LP64todo - fix this!
1779 io_size
= (int)uio_curriovlen(uio
);
1780 iov_base
= uio_curriovbase(uio
);
1782 upl_offset
= CAST_DOWN(upl_offset_t
, iov_base
) & PAGE_MASK
;
1783 upl_needed_size
= upl_offset
+ io_size
;
1786 upl_size
= upl_needed_size
;
1787 upl_flags
= UPL_FILE_IO
| UPL_COPYOUT_FROM
| UPL_NO_SYNC
|
1788 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
1790 // LP64todo - fix this!
1791 kret
= vm_map_get_upl(current_map(),
1792 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
1793 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
, 0);
1795 if (kret
!= KERN_SUCCESS
) {
1797 * cluster_phys_write: failed to get pagelist
1798 * note: return kret here
1803 * Consider the possibility that upl_size wasn't satisfied.
1804 * This is a failure in the physical memory case.
1806 if (upl_size
< upl_needed_size
) {
1807 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
1810 pl
= ubc_upl_pageinfo(upl
);
1812 src_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)upl_offset
;
1814 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
1817 head_size
= devblocksize
- (int)(uio
->uio_offset
& (devblocksize
- 1));
1819 if (head_size
> io_size
)
1820 head_size
= io_size
;
1822 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, head_size
, 0);
1825 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
1829 upl_offset
+= head_size
;
1830 src_paddr
+= head_size
;
1831 io_size
-= head_size
;
1833 tail_size
= io_size
& (devblocksize
- 1);
1834 io_size
-= tail_size
;
1838 * issue a synchronous write to cluster_io
1840 error
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
,
1841 io_size
, CL_DEV_MEMORY
, (buf_t
)NULL
, (struct clios
*)NULL
);
1845 * The cluster_io write completed successfully,
1846 * update the uio structure
1848 uio_update(uio
, (user_size_t
)io_size
);
1850 src_paddr
+= io_size
;
1853 error
= cluster_align_phys_io(vp
, uio
, src_paddr
, tail_size
, 0);
1856 * just release our hold on the physically contiguous
1857 * region without changing any state
1859 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
1866 cluster_write_x(vnode_t vp
, struct uio
*uio
, off_t oldEOF
, off_t newEOF
, off_t headOff
, off_t tailOff
, int flags
)
1868 upl_page_info_t
*pl
;
1870 vm_offset_t upl_offset
= 0;
1883 long long total_size
;
1886 long long zero_cnt1
;
1888 struct cl_extent cl
;
1890 struct cl_writebehind
*wbp
;
1892 if ((wbp
= cluster_get_wbp(vp
, 0)) != NULL
)
1894 if (wbp
->cl_hasbeenpaged
) {
1896 * this vnode had pages cleaned to it by
1897 * the pager which indicates that either
1898 * it's not very 'hot', or the system is
1899 * being overwhelmed by a lot of dirty
1900 * data being delayed in the VM cache...
1901 * in either event, we'll push our remaining
1902 * delayed data at this point... this will
1903 * be more efficient than paging out 1 page at
1904 * a time, and will also act as a throttle
1905 * by delaying this client from writing any
1906 * more data until all his delayed data has
1907 * at least been queued to the uderlying driver.
1909 if (wbp
->cl_number
|| wbp
->cl_scmap
)
1910 cluster_push_EOF(vp
, newEOF
);
1912 wbp
->cl_hasbeenpaged
= 0;
1916 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
1917 (int)uio
->uio_offset
, uio_resid(uio
), (int)oldEOF
, (int)newEOF
, 0);
1919 // LP64todo - fix this
1920 io_resid
= uio_resid(uio
);
1922 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_START
,
1923 0, 0, (int)oldEOF
, (int)newEOF
, 0);
1932 if (flags
& IO_HEADZEROFILL
) {
1934 * some filesystems (HFS is one) don't support unallocated holes within a file...
1935 * so we zero fill the intervening space between the old EOF and the offset
1936 * where the next chunk of real data begins.... ftruncate will also use this
1937 * routine to zero fill to the new EOF when growing a file... in this case, the
1938 * uio structure will not be provided
1941 if (headOff
< uio
->uio_offset
) {
1942 zero_cnt
= uio
->uio_offset
- headOff
;
1945 } else if (headOff
< newEOF
) {
1946 zero_cnt
= newEOF
- headOff
;
1950 if (flags
& IO_TAILZEROFILL
) {
1952 // LP64todo - fix this
1953 zero_off1
= uio
->uio_offset
+ uio_resid(uio
);
1955 if (zero_off1
< tailOff
)
1956 zero_cnt1
= tailOff
- zero_off1
;
1959 if (zero_cnt
== 0 && uio
== (struct uio
*) 0) {
1960 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
1961 retval
, 0, 0, 0, 0);
1965 while ((total_size
= (io_resid
+ zero_cnt
+ zero_cnt1
)) && retval
== 0) {
1967 * for this iteration of the loop, figure out where our starting point is
1970 start_offset
= (int)(zero_off
& PAGE_MASK_64
);
1971 upl_f_offset
= zero_off
- start_offset
;
1972 } else if (io_resid
) {
1973 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
1974 upl_f_offset
= uio
->uio_offset
- start_offset
;
1976 start_offset
= (int)(zero_off1
& PAGE_MASK_64
);
1977 upl_f_offset
= zero_off1
- start_offset
;
1979 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 46)) | DBG_FUNC_NONE
,
1980 (int)zero_off
, (int)zero_cnt
, (int)zero_off1
, (int)zero_cnt1
, 0);
1982 if (total_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
1983 total_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
1985 cl
.b_addr
= (daddr64_t
)(upl_f_offset
/ PAGE_SIZE_64
);
1987 if (uio
&& ((flags
& (IO_NOCACHE
| IO_SYNC
| IO_HEADZEROFILL
| IO_TAILZEROFILL
)) == 0)) {
1989 * assumption... total_size <= io_resid
1990 * because IO_HEADZEROFILL and IO_TAILZEROFILL not set
1992 if ((start_offset
+ total_size
) > (MAX_UPL_TRANSFER
* PAGE_SIZE
))
1993 total_size
-= start_offset
;
1994 xfer_resid
= total_size
;
1996 retval
= cluster_copy_ubc_data(vp
, uio
, &xfer_resid
, 1);
2001 io_resid
-= (total_size
- xfer_resid
);
2002 total_size
= xfer_resid
;
2003 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2004 upl_f_offset
= uio
->uio_offset
- start_offset
;
2006 if (total_size
== 0) {
2009 * the write did not finish on a page boundary
2010 * which will leave upl_f_offset pointing to the
2011 * beginning of the last page written instead of
2012 * the page beyond it... bump it in this case
2013 * so that the cluster code records the last page
2016 upl_f_offset
+= PAGE_SIZE_64
;
2024 * compute the size of the upl needed to encompass
2025 * the requested write... limit each call to cluster_io
2026 * to the maximum UPL size... cluster_io will clip if
2027 * this exceeds the maximum io_size for the device,
2028 * make sure to account for
2029 * a starting offset that's not page aligned
2031 upl_size
= (start_offset
+ total_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2033 if (upl_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
2034 upl_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
2036 pages_in_upl
= upl_size
/ PAGE_SIZE
;
2037 io_size
= upl_size
- start_offset
;
2039 if ((long long)io_size
> total_size
)
2040 io_size
= total_size
;
2042 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, io_size
, total_size
, 0, 0);
2046 * Gather the pages from the buffer cache.
2047 * The UPL_WILL_MODIFY flag lets the UPL subsystem know
2048 * that we intend to modify these pages.
2050 kret
= ubc_create_upl(vp
,
2055 UPL_SET_LITE
| UPL_WILL_MODIFY
);
2056 if (kret
!= KERN_SUCCESS
)
2057 panic("cluster_write: failed to get pagelist");
2059 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
,
2060 (int)upl
, (int)upl_f_offset
, start_offset
, 0, 0);
2062 if (start_offset
&& !upl_valid_page(pl
, 0)) {
2066 * we're starting in the middle of the first page of the upl
2067 * and the page isn't currently valid, so we're going to have
2068 * to read it in first... this is a synchronous operation
2070 read_size
= PAGE_SIZE
;
2072 if ((upl_f_offset
+ read_size
) > newEOF
)
2073 read_size
= newEOF
- upl_f_offset
;
2075 retval
= cluster_io(vp
, upl
, 0, upl_f_offset
, read_size
,
2076 CL_READ
, (buf_t
)NULL
, (struct clios
*)NULL
);
2079 * we had an error during the read which causes us to abort
2080 * the current cluster_write request... before we do, we need
2081 * to release the rest of the pages in the upl without modifying
2082 * there state and mark the failed page in error
2084 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
);
2086 if (upl_size
> PAGE_SIZE
)
2087 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2089 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2090 (int)upl
, 0, 0, retval
, 0);
2094 if ((start_offset
== 0 || upl_size
> PAGE_SIZE
) && ((start_offset
+ io_size
) & PAGE_MASK
)) {
2096 * the last offset we're writing to in this upl does not end on a page
2097 * boundary... if it's not beyond the old EOF, then we'll also need to
2098 * pre-read this page in if it isn't already valid
2100 upl_offset
= upl_size
- PAGE_SIZE
;
2102 if ((upl_f_offset
+ start_offset
+ io_size
) < oldEOF
&&
2103 !upl_valid_page(pl
, upl_offset
/ PAGE_SIZE
)) {
2106 read_size
= PAGE_SIZE
;
2108 if ((upl_f_offset
+ upl_offset
+ read_size
) > newEOF
)
2109 read_size
= newEOF
- (upl_f_offset
+ upl_offset
);
2111 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, read_size
,
2112 CL_READ
, (buf_t
)NULL
, (struct clios
*)NULL
);
2115 * we had an error during the read which causes us to abort
2116 * the current cluster_write request... before we do, we
2117 * need to release the rest of the pages in the upl without
2118 * modifying there state and mark the failed page in error
2120 ubc_upl_abort_range(upl
, upl_offset
, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
);
2122 if (upl_size
> PAGE_SIZE
)
2123 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
2125 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2126 (int)upl
, 0, 0, retval
, 0);
2131 xfer_resid
= io_size
;
2132 io_offset
= start_offset
;
2134 while (zero_cnt
&& xfer_resid
) {
2136 if (zero_cnt
< (long long)xfer_resid
)
2137 bytes_to_zero
= zero_cnt
;
2139 bytes_to_zero
= xfer_resid
;
2141 if ( !(flags
& (IO_NOZEROVALID
| IO_NOZERODIRTY
))) {
2142 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2146 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off
& PAGE_MASK_64
));
2147 zero_pg_index
= (int)((zero_off
- upl_f_offset
) / PAGE_SIZE_64
);
2149 if ( !upl_valid_page(pl
, zero_pg_index
)) {
2150 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2152 } else if ((flags
& (IO_NOZERODIRTY
| IO_NOZEROVALID
)) == IO_NOZERODIRTY
&&
2153 !upl_dirty_page(pl
, zero_pg_index
)) {
2154 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2157 xfer_resid
-= bytes_to_zero
;
2158 zero_cnt
-= bytes_to_zero
;
2159 zero_off
+= bytes_to_zero
;
2160 io_offset
+= bytes_to_zero
;
2162 if (xfer_resid
&& io_resid
) {
2163 bytes_to_move
= min(io_resid
, xfer_resid
);
2165 retval
= cluster_copy_upl_data(uio
, upl
, io_offset
, bytes_to_move
);
2169 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
2171 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 45)) | DBG_FUNC_NONE
,
2172 (int)upl
, 0, 0, retval
, 0);
2174 io_resid
-= bytes_to_move
;
2175 xfer_resid
-= bytes_to_move
;
2176 io_offset
+= bytes_to_move
;
2179 while (xfer_resid
&& zero_cnt1
&& retval
== 0) {
2181 if (zero_cnt1
< (long long)xfer_resid
)
2182 bytes_to_zero
= zero_cnt1
;
2184 bytes_to_zero
= xfer_resid
;
2186 if ( !(flags
& (IO_NOZEROVALID
| IO_NOZERODIRTY
))) {
2187 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2191 bytes_to_zero
= min(bytes_to_zero
, PAGE_SIZE
- (int)(zero_off1
& PAGE_MASK_64
));
2192 zero_pg_index
= (int)((zero_off1
- upl_f_offset
) / PAGE_SIZE_64
);
2194 if ( !upl_valid_page(pl
, zero_pg_index
)) {
2195 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2196 } else if ((flags
& (IO_NOZERODIRTY
| IO_NOZEROVALID
)) == IO_NOZERODIRTY
&&
2197 !upl_dirty_page(pl
, zero_pg_index
)) {
2198 cluster_zero(upl
, io_offset
, bytes_to_zero
, NULL
);
2201 xfer_resid
-= bytes_to_zero
;
2202 zero_cnt1
-= bytes_to_zero
;
2203 zero_off1
+= bytes_to_zero
;
2204 io_offset
+= bytes_to_zero
;
2211 io_size
+= start_offset
;
2213 if ((upl_f_offset
+ io_size
) >= newEOF
&& io_size
< upl_size
) {
2215 * if we're extending the file with this write
2216 * we'll zero fill the rest of the page so that
2217 * if the file gets extended again in such a way as to leave a
2218 * hole starting at this EOF, we'll have zero's in the correct spot
2220 cluster_zero(upl
, io_size
, upl_size
- io_size
, NULL
);
2222 if (flags
& IO_SYNC
)
2224 * if the IO_SYNC flag is set than we need to
2225 * bypass any clusters and immediately issue
2231 * take the lock to protect our accesses
2232 * of the writebehind and sparse cluster state
2234 wbp
= cluster_get_wbp(vp
, CLW_ALLOCATE
| CLW_RETURNLOCKED
);
2237 * calculate the last logical block number
2238 * that this delayed I/O encompassed
2240 cl
.e_addr
= (daddr64_t
)((upl_f_offset
+ (off_t
)upl_size
) / PAGE_SIZE_64
);
2242 if (wbp
->cl_scmap
) {
2244 if ( !(flags
& IO_NOCACHE
)) {
2246 * we've fallen into the sparse
2247 * cluster method of delaying dirty pages
2248 * first, we need to release the upl if we hold one
2249 * since pages in it may be present in the sparse cluster map
2250 * and may span 2 separate buckets there... if they do and
2251 * we happen to have to flush a bucket to make room and it intersects
2252 * this upl, a deadlock may result on page BUSY
2255 ubc_upl_commit_range(upl
, 0, upl_size
,
2256 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2258 sparse_cluster_add(wbp
, vp
, &cl
, newEOF
);
2260 lck_mtx_unlock(&wbp
->cl_lockw
);
2265 * must have done cached writes that fell into
2266 * the sparse cluster mechanism... we've switched
2267 * to uncached writes on the file, so go ahead
2268 * and push whatever's in the sparse map
2269 * and switch back to normal clustering
2271 * see the comment above concerning a possible deadlock...
2274 ubc_upl_commit_range(upl
, 0, upl_size
,
2275 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2277 * setting upl_size to 0 keeps us from committing a
2278 * second time in the start_new_cluster path
2282 sparse_cluster_push(wbp
, vp
, newEOF
, 1);
2286 * no clusters of either type present at this point
2287 * so just go directly to start_new_cluster since
2288 * we know we need to delay this I/O since we've
2289 * already released the pages back into the cache
2290 * to avoid the deadlock with sparse_cluster_push
2292 goto start_new_cluster
;
2296 if (wbp
->cl_number
== 0)
2298 * no clusters currently present
2300 goto start_new_cluster
;
2302 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
2304 * check each cluster that we currently hold
2305 * try to merge some or all of this write into
2306 * one or more of the existing clusters... if
2307 * any portion of the write remains, start a
2310 if (cl
.b_addr
>= wbp
->cl_clusters
[cl_index
].b_addr
) {
2312 * the current write starts at or after the current cluster
2314 if (cl
.e_addr
<= (wbp
->cl_clusters
[cl_index
].b_addr
+ MAX_UPL_TRANSFER
)) {
2316 * we have a write that fits entirely
2317 * within the existing cluster limits
2319 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
)
2321 * update our idea of where the cluster ends
2323 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
2326 if (cl
.b_addr
< (wbp
->cl_clusters
[cl_index
].b_addr
+ MAX_UPL_TRANSFER
)) {
2328 * we have a write that starts in the middle of the current cluster
2329 * but extends beyond the cluster's limit... we know this because
2330 * of the previous checks
2331 * we'll extend the current cluster to the max
2332 * and update the b_addr for the current write to reflect that
2333 * the head of it was absorbed into this cluster...
2334 * note that we'll always have a leftover tail in this case since
2335 * full absorbtion would have occurred in the clause above
2337 wbp
->cl_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
+ MAX_UPL_TRANSFER
;
2340 daddr64_t start_pg_in_upl
;
2342 start_pg_in_upl
= (daddr64_t
)(upl_f_offset
/ PAGE_SIZE_64
);
2344 if (start_pg_in_upl
< wbp
->cl_clusters
[cl_index
].e_addr
) {
2345 intersection
= (int)((wbp
->cl_clusters
[cl_index
].e_addr
- start_pg_in_upl
) * PAGE_SIZE
);
2347 ubc_upl_commit_range(upl
, upl_offset
, intersection
,
2348 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2349 upl_f_offset
+= intersection
;
2350 upl_offset
+= intersection
;
2351 upl_size
-= intersection
;
2354 cl
.b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
;
2357 * we come here for the case where the current write starts
2358 * beyond the limit of the existing cluster or we have a leftover
2359 * tail after a partial absorbtion
2361 * in either case, we'll check the remaining clusters before
2362 * starting a new one
2366 * the current write starts in front of the cluster we're currently considering
2368 if ((wbp
->cl_clusters
[cl_index
].e_addr
- cl
.b_addr
) <= MAX_UPL_TRANSFER
) {
2370 * we can just merge the new request into
2371 * this cluster and leave it in the cache
2372 * since the resulting cluster is still
2373 * less than the maximum allowable size
2375 wbp
->cl_clusters
[cl_index
].b_addr
= cl
.b_addr
;
2377 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
) {
2379 * the current write completely
2380 * envelops the existing cluster and since
2381 * each write is limited to at most MAX_UPL_TRANSFER bytes
2382 * we can just use the start and last blocknos of the write
2383 * to generate the cluster limits
2385 wbp
->cl_clusters
[cl_index
].e_addr
= cl
.e_addr
;
2391 * if we were to combine this write with the current cluster
2392 * we would exceed the cluster size limit.... so,
2393 * let's see if there's any overlap of the new I/O with
2394 * the cluster we're currently considering... in fact, we'll
2395 * stretch the cluster out to it's full limit and see if we
2396 * get an intersection with the current write
2399 if (cl
.e_addr
> wbp
->cl_clusters
[cl_index
].e_addr
- MAX_UPL_TRANSFER
) {
2401 * the current write extends into the proposed cluster
2402 * clip the length of the current write after first combining it's
2403 * tail with the newly shaped cluster
2405 wbp
->cl_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[cl_index
].e_addr
- MAX_UPL_TRANSFER
;
2408 intersection
= (int)((cl
.e_addr
- wbp
->cl_clusters
[cl_index
].b_addr
) * PAGE_SIZE
);
2410 if (intersection
> upl_size
)
2412 * because the current write may consist of a number of pages found in the cache
2413 * which are not part of the UPL, we may have an intersection that exceeds
2414 * the size of the UPL that is also part of this write
2416 intersection
= upl_size
;
2418 ubc_upl_commit_range(upl
, upl_offset
+ (upl_size
- intersection
), intersection
,
2419 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2420 upl_size
-= intersection
;
2422 cl
.e_addr
= wbp
->cl_clusters
[cl_index
].b_addr
;
2425 * if we get here, there was no way to merge
2426 * any portion of this write with this cluster
2427 * or we could only merge part of it which
2428 * will leave a tail...
2429 * we'll check the remaining clusters before starting a new one
2433 if (cl_index
< wbp
->cl_number
)
2435 * we found an existing cluster(s) that we
2436 * could entirely merge this I/O into
2440 if (wbp
->cl_number
< MAX_CLUSTERS
&& !(flags
& IO_NOCACHE
))
2442 * we didn't find an existing cluster to
2443 * merge into, but there's room to start
2446 goto start_new_cluster
;
2449 * no exisitng cluster to merge with and no
2450 * room to start a new one... we'll try
2451 * pushing one of the existing ones... if none of
2452 * them are able to be pushed, we'll switch
2453 * to the sparse cluster mechanism
2454 * cluster_try_push updates cl_number to the
2455 * number of remaining clusters... and
2456 * returns the number of currently unused clusters
2458 int ret_cluster_try_push
= 0;
2459 /* if writes are not deferred, call cluster push immediately */
2460 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
)) {
2461 if (flags
& IO_NOCACHE
)
2466 ret_cluster_try_push
= cluster_try_push(wbp
, vp
, newEOF
, can_delay
, 0);
2469 /* execute following regardless writes are deferred or not */
2470 if (ret_cluster_try_push
== 0) {
2472 * no more room in the normal cluster mechanism
2473 * so let's switch to the more expansive but expensive
2474 * sparse mechanism....
2475 * first, we need to release the upl if we hold one
2476 * since pages in it may be present in the sparse cluster map (after the cluster_switch)
2477 * and may span 2 separate buckets there... if they do and
2478 * we happen to have to flush a bucket to make room and it intersects
2479 * this upl, a deadlock may result on page BUSY
2482 ubc_upl_commit_range(upl
, upl_offset
, upl_size
,
2483 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2485 sparse_cluster_switch(wbp
, vp
, newEOF
);
2486 sparse_cluster_add(wbp
, vp
, &cl
, newEOF
);
2488 lck_mtx_unlock(&wbp
->cl_lockw
);
2493 * we pushed one cluster successfully, so we must be sequentially writing this file
2494 * otherwise, we would have failed and fallen into the sparse cluster support
2495 * so let's take the opportunity to push out additional clusters as long as we
2496 * remain below the throttle... this will give us better I/O locality if we're
2497 * in a copy loop (i.e. we won't jump back and forth between the read and write points
2498 * however, we don't want to push so much out that the write throttle kicks in and
2499 * hangs this thread up until some of the I/O completes...
2501 if (!((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
)) {
2502 while (wbp
->cl_number
&& (vp
->v_numoutput
<= (VNODE_ASYNC_THROTTLE
/ 2)))
2503 cluster_try_push(wbp
, vp
, newEOF
, 0, 0);
2507 wbp
->cl_clusters
[wbp
->cl_number
].b_addr
= cl
.b_addr
;
2508 wbp
->cl_clusters
[wbp
->cl_number
].e_addr
= cl
.e_addr
;
2510 if (flags
& IO_NOCACHE
)
2511 wbp
->cl_clusters
[wbp
->cl_number
].io_nocache
= 1;
2513 wbp
->cl_clusters
[wbp
->cl_number
].io_nocache
= 0;
2517 ubc_upl_commit_range(upl
, upl_offset
, upl_size
,
2518 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2520 lck_mtx_unlock(&wbp
->cl_lockw
);
2525 * we don't hold the vnode lock at this point
2527 * because we had to ask for a UPL that provides currenty non-present pages, the
2528 * UPL has been automatically set to clear the dirty flags (both software and hardware)
2529 * upon committing it... this is not the behavior we want since it's possible for
2530 * pages currently present as part of a mapped file to be dirtied while the I/O is in flight.
2531 * in order to maintain some semblance of coherency with mapped writes
2532 * we need to drop the current upl and pick it back up with COPYOUT_FROM set
2533 * so that we correctly deal with a change in state of the hardware modify bit...
2534 * we do this via cluster_push_x... by passing along the IO_SYNC flag, we force
2535 * cluster_push_x to wait until all the I/Os have completed... cluster_push_x is also
2536 * responsible for generating the correct sized I/O(s)
2538 ubc_upl_commit_range(upl
, 0, upl_size
,
2539 UPL_COMMIT_SET_DIRTY
| UPL_COMMIT_INACTIVATE
| UPL_COMMIT_FREE_ON_EMPTY
);
2541 cl
.e_addr
= (upl_f_offset
+ (off_t
)upl_size
) / PAGE_SIZE_64
;
2543 retval
= cluster_push_x(vp
, &cl
, newEOF
, flags
);
2546 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 40)) | DBG_FUNC_END
,
2547 retval
, 0, io_resid
, 0, 0);
2553 cluster_read(vnode_t vp
, struct uio
*uio
, off_t filesize
, int xflags
)
2566 if (vp
->v_flag
& VNOCACHE_DATA
)
2567 flags
|= IO_NOCACHE
;
2568 if (vp
->v_flag
& VRAOFF
)
2571 if (!((flags
& IO_NOCACHE
) && UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))) {
2573 * go do a read through the cache if one of the following is true....
2574 * NOCACHE is not true
2575 * the uio request doesn't target USERSPACE
2577 return (cluster_read_x(vp
, uio
, filesize
, flags
));
2581 if (IS_VALID_UIO_SEGFLG(uio
->uio_segflg
) == 0) {
2582 panic("%s :%d - invalid uio_segflg\n", __FILE__
, __LINE__
);
2584 #endif /* LP64_DEBUG */
2586 while (uio_resid(uio
) && uio
->uio_offset
< filesize
&& retval
== 0) {
2587 user_size_t iov_len
;
2588 user_addr_t iov_base
;
2591 * we know we have a resid, so this is safe
2592 * skip over any emtpy vectors
2594 uio_update(uio
, (user_size_t
)0);
2596 iov_len
= uio_curriovlen(uio
);
2597 iov_base
= uio_curriovbase(uio
);
2599 upl_size
= PAGE_SIZE
;
2600 upl_flags
= UPL_QUERY_OBJECT_TYPE
;
2602 // LP64todo - fix this!
2603 if ((vm_map_get_upl(current_map(),
2604 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
2605 &upl_size
, &upl
, NULL
, NULL
, &upl_flags
, 0)) != KERN_SUCCESS
) {
2607 * the user app must have passed in an invalid address
2613 * We check every vector target but if it is physically
2614 * contiguous space, we skip the sanity checks.
2616 if (upl_flags
& UPL_PHYS_CONTIG
) {
2617 retval
= cluster_phys_read(vp
, uio
, filesize
);
2619 else if (uio_resid(uio
) < PAGE_SIZE
) {
2621 * we're here because we're don't have a physically contiguous target buffer
2622 * go do a read through the cache if
2623 * the total xfer size is less than a page...
2625 return (cluster_read_x(vp
, uio
, filesize
, flags
));
2627 // LP64todo - fix this!
2628 else if (((int)uio
->uio_offset
& PAGE_MASK
) || (CAST_DOWN(int, iov_base
) & PAGE_MASK
)) {
2629 if (((int)uio
->uio_offset
& PAGE_MASK
) == (CAST_DOWN(int, iov_base
) & PAGE_MASK
)) {
2631 * Bring the file offset read up to a pagesize boundary
2632 * this will also bring the base address to a page boundary
2633 * since they both are currently on the same offset within a page
2634 * note: if we get here, uio->uio_resid is greater than PAGE_SIZE
2635 * so the computed clip_size must always be less than the current uio_resid
2637 clip_size
= (PAGE_SIZE
- (int)(uio
->uio_offset
& PAGE_MASK_64
));
2640 * Fake the resid going into the cluster_read_x call
2641 * and restore it on the way out.
2643 prev_resid
= uio_resid(uio
);
2644 // LP64todo - fix this
2645 uio_setresid(uio
, clip_size
);
2647 retval
= cluster_read_x(vp
, uio
, filesize
, flags
);
2649 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
2652 * can't get both the file offset and the buffer offset aligned to a page boundary
2653 * so fire an I/O through the cache for this entire vector
2655 // LP64todo - fix this!
2656 clip_size
= iov_len
;
2657 prev_resid
= uio_resid(uio
);
2658 uio_setresid(uio
, clip_size
);
2660 retval
= cluster_read_x(vp
, uio
, filesize
, flags
);
2662 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
2666 * If we come in here, we know the offset into
2667 * the file is on a pagesize boundary
2669 max_io_size
= filesize
- uio
->uio_offset
;
2670 // LP64todo - fix this
2671 clip_size
= uio_resid(uio
);
2672 if (iov_len
< clip_size
)
2673 clip_size
= iov_len
;
2674 if (max_io_size
< clip_size
)
2675 clip_size
= (int)max_io_size
;
2677 if (clip_size
< PAGE_SIZE
) {
2679 * Take care of the tail end of the read in this vector.
2681 // LP64todo - fix this
2682 prev_resid
= uio_resid(uio
);
2683 uio_setresid(uio
, clip_size
);
2685 retval
= cluster_read_x(vp
, uio
, filesize
, flags
);
2687 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
2689 /* round clip_size down to a multiple of pagesize */
2690 clip_size
= clip_size
& ~(PAGE_MASK
);
2691 // LP64todo - fix this
2692 prev_resid
= uio_resid(uio
);
2693 uio_setresid(uio
, clip_size
);
2695 retval
= cluster_nocopy_read(vp
, uio
, filesize
);
2697 if ((retval
==0) && uio_resid(uio
))
2698 retval
= cluster_read_x(vp
, uio
, filesize
, flags
);
2700 uio_setresid(uio
, prev_resid
- (clip_size
- uio_resid(uio
)));
2709 cluster_read_x(vnode_t vp
, struct uio
*uio
, off_t filesize
, int flags
)
2711 upl_page_info_t
*pl
;
2713 vm_offset_t upl_offset
;
2722 off_t last_ioread_offset
;
2723 off_t last_request_offset
;
2724 u_int size_of_prefetch
;
2729 u_int max_rd_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
2730 u_int rd_ahead_enabled
= 1;
2731 u_int prefetch_enabled
= 1;
2732 struct cl_readahead
* rap
;
2733 struct clios iostate
;
2734 struct cl_extent extent
;
2736 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_START
,
2737 (int)uio
->uio_offset
, uio_resid(uio
), (int)filesize
, 0, 0);
2739 // LP64todo - fix this
2740 last_request_offset
= uio
->uio_offset
+ uio_resid(uio
);
2742 if ((flags
& (IO_RAOFF
|IO_NOCACHE
)) ||
2743 ((last_request_offset
& ~PAGE_MASK_64
) == (uio
->uio_offset
& ~PAGE_MASK_64
))) {
2744 rd_ahead_enabled
= 0;
2747 if (cluster_hard_throttle_on(vp
)) {
2748 rd_ahead_enabled
= 0;
2749 prefetch_enabled
= 0;
2751 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
2753 if ((rap
= cluster_get_rap(vp
)) == NULL
)
2754 rd_ahead_enabled
= 0;
2756 if (last_request_offset
> filesize
)
2757 last_request_offset
= filesize
;
2758 extent
.b_addr
= uio
->uio_offset
/ PAGE_SIZE_64
;
2759 extent
.e_addr
= (last_request_offset
- 1) / PAGE_SIZE_64
;
2761 if (rap
!= NULL
&& rap
->cl_ralen
&& (rap
->cl_lastr
== extent
.b_addr
|| (rap
->cl_lastr
+ 1) == extent
.b_addr
)) {
2763 * determine if we already have a read-ahead in the pipe courtesy of the
2764 * last read systemcall that was issued...
2765 * if so, pick up it's extent to determine where we should start
2766 * with respect to any read-ahead that might be necessary to
2767 * garner all the data needed to complete this read systemcall
2769 last_ioread_offset
= (rap
->cl_maxra
* PAGE_SIZE_64
) + PAGE_SIZE_64
;
2771 if (last_ioread_offset
< uio
->uio_offset
)
2772 last_ioread_offset
= (off_t
)0;
2773 else if (last_ioread_offset
> last_request_offset
)
2774 last_ioread_offset
= last_request_offset
;
2776 last_ioread_offset
= (off_t
)0;
2778 while (uio_resid(uio
) && uio
->uio_offset
< filesize
&& retval
== 0) {
2780 * compute the size of the upl needed to encompass
2781 * the requested read... limit each call to cluster_io
2782 * to the maximum UPL size... cluster_io will clip if
2783 * this exceeds the maximum io_size for the device,
2784 * make sure to account for
2785 * a starting offset that's not page aligned
2787 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2788 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
2789 max_size
= filesize
- uio
->uio_offset
;
2791 // LP64todo - fix this!
2792 if ((off_t
)((unsigned int)uio_resid(uio
)) < max_size
)
2793 io_size
= uio_resid(uio
);
2797 if (!(flags
& IO_NOCACHE
)) {
2804 * if we keep finding the pages we need already in the cache, then
2805 * don't bother to call cluster_rd_prefetch since it costs CPU cycles
2806 * to determine that we have all the pages we need... once we miss in
2807 * the cache and have issued an I/O, than we'll assume that we're likely
2808 * to continue to miss in the cache and it's to our advantage to try and prefetch
2810 if (last_request_offset
&& last_ioread_offset
&& (size_of_prefetch
= (last_request_offset
- last_ioread_offset
))) {
2811 if ((last_ioread_offset
- uio
->uio_offset
) <= max_rd_size
&& prefetch_enabled
) {
2813 * we've already issued I/O for this request and
2814 * there's still work to do and
2815 * our prefetch stream is running dry, so issue a
2816 * pre-fetch I/O... the I/O latency will overlap
2817 * with the copying of the data
2819 if (size_of_prefetch
> max_rd_size
)
2820 size_of_prefetch
= max_rd_size
;
2822 size_of_prefetch
= cluster_rd_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
);
2824 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
2826 if (last_ioread_offset
> last_request_offset
)
2827 last_ioread_offset
= last_request_offset
;
2831 * limit the size of the copy we're about to do so that
2832 * we can notice that our I/O pipe is running dry and
2833 * get the next I/O issued before it does go dry
2835 if (last_ioread_offset
&& io_size
> ((MAX_UPL_TRANSFER
* PAGE_SIZE
) / 4))
2836 io_resid
= ((MAX_UPL_TRANSFER
* PAGE_SIZE
) / 4);
2840 io_requested
= io_resid
;
2842 retval
= cluster_copy_ubc_data(vp
, uio
, &io_resid
, 0);
2844 io_size
-= (io_requested
- io_resid
);
2846 if (retval
|| io_resid
)
2848 * if we run into a real error or
2849 * a page that is not in the cache
2850 * we need to leave streaming mode
2854 if ((io_size
== 0 || last_ioread_offset
== last_request_offset
) && rd_ahead_enabled
) {
2856 * we're already finished the I/O for this read request
2857 * let's see if we should do a read-ahead
2859 cluster_rd_ahead(vp
, &extent
, filesize
, rap
);
2866 if (extent
.e_addr
< rap
->cl_lastr
)
2868 rap
->cl_lastr
= extent
.e_addr
;
2872 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
2873 upl_f_offset
= uio
->uio_offset
- (off_t
)start_offset
;
2874 max_size
= filesize
- uio
->uio_offset
;
2876 if (io_size
> max_rd_size
)
2877 io_size
= max_rd_size
;
2879 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
2881 if (upl_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
) / 4)
2882 upl_size
= (MAX_UPL_TRANSFER
* PAGE_SIZE
) / 4;
2883 pages_in_upl
= upl_size
/ PAGE_SIZE
;
2885 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_START
,
2886 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
2888 kret
= ubc_create_upl(vp
,
2894 if (kret
!= KERN_SUCCESS
)
2895 panic("cluster_read: failed to get pagelist");
2897 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 33)) | DBG_FUNC_END
,
2898 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
2901 * scan from the beginning of the upl looking for the first
2902 * non-valid page.... this will become the first page in
2903 * the request we're going to make to 'cluster_io'... if all
2904 * of the pages are valid, we won't call through to 'cluster_io'
2906 for (start_pg
= 0; start_pg
< pages_in_upl
; start_pg
++) {
2907 if (!upl_valid_page(pl
, start_pg
))
2912 * scan from the starting invalid page looking for a valid
2913 * page before the end of the upl is reached, if we
2914 * find one, then it will be the last page of the request to
2917 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
2918 if (upl_valid_page(pl
, last_pg
))
2921 iostate
.io_completed
= 0;
2922 iostate
.io_issued
= 0;
2923 iostate
.io_error
= 0;
2924 iostate
.io_wanted
= 0;
2926 if (start_pg
< last_pg
) {
2928 * we found a range of 'invalid' pages that must be filled
2929 * if the last page in this range is the last page of the file
2930 * we may have to clip the size of it to keep from reading past
2931 * the end of the last physical block associated with the file
2933 upl_offset
= start_pg
* PAGE_SIZE
;
2934 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
2936 if ((upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
2937 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
2940 * issue an asynchronous read to cluster_io
2943 error
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
,
2944 io_size
, CL_READ
| CL_ASYNC
, (buf_t
)NULL
, &iostate
);
2948 * if the read completed successfully, or there was no I/O request
2949 * issued, than copy the data into user land via 'cluster_upl_copy_data'
2950 * we'll first add on any 'valid'
2951 * pages that were present in the upl when we acquired it.
2955 for (uio_last
= last_pg
; uio_last
< pages_in_upl
; uio_last
++) {
2956 if (!upl_valid_page(pl
, uio_last
))
2960 * compute size to transfer this round, if uio->uio_resid is
2961 * still non-zero after this attempt, we'll loop around and
2962 * set up for another I/O.
2964 val_size
= (uio_last
* PAGE_SIZE
) - start_offset
;
2966 if (val_size
> max_size
)
2967 val_size
= max_size
;
2969 if (val_size
> uio_resid(uio
))
2970 // LP64todo - fix this
2971 val_size
= uio_resid(uio
);
2973 if (last_ioread_offset
== 0)
2974 last_ioread_offset
= uio
->uio_offset
+ val_size
;
2976 if ((size_of_prefetch
= (last_request_offset
- last_ioread_offset
)) && prefetch_enabled
) {
2978 * if there's still I/O left to do for this request, and...
2979 * we're not in hard throttle mode, then issue a
2980 * pre-fetch I/O... the I/O latency will overlap
2981 * with the copying of the data
2983 size_of_prefetch
= cluster_rd_prefetch(vp
, last_ioread_offset
, size_of_prefetch
, filesize
);
2985 last_ioread_offset
+= (off_t
)(size_of_prefetch
* PAGE_SIZE
);
2987 if (last_ioread_offset
> last_request_offset
)
2988 last_ioread_offset
= last_request_offset
;
2990 } else if ((uio
->uio_offset
+ val_size
) == last_request_offset
) {
2992 * this transfer will finish this request, so...
2993 * let's try to read ahead if we're in
2994 * a sequential access pattern and we haven't
2995 * explicitly disabled it
2997 if (rd_ahead_enabled
)
2998 cluster_rd_ahead(vp
, &extent
, filesize
, rap
);
3001 if (extent
.e_addr
< rap
->cl_lastr
)
3003 rap
->cl_lastr
= extent
.e_addr
;
3006 lck_mtx_lock(cl_mtxp
);
3008 while (iostate
.io_issued
!= iostate
.io_completed
) {
3009 iostate
.io_wanted
= 1;
3010 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_read_x", 0);
3012 lck_mtx_unlock(cl_mtxp
);
3014 if (iostate
.io_error
)
3015 error
= iostate
.io_error
;
3017 retval
= cluster_copy_upl_data(uio
, upl
, start_offset
, val_size
);
3019 if (start_pg
< last_pg
) {
3021 * compute the range of pages that we actually issued an I/O for
3022 * and either commit them as valid if the I/O succeeded
3023 * or abort them if the I/O failed
3025 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
3027 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
3028 (int)upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
3030 if (error
|| (flags
& IO_NOCACHE
))
3031 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
3032 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3034 ubc_upl_commit_range(upl
, start_pg
* PAGE_SIZE
, io_size
,
3035 UPL_COMMIT_CLEAR_DIRTY
|
3036 UPL_COMMIT_FREE_ON_EMPTY
|
3037 UPL_COMMIT_INACTIVATE
);
3039 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
,
3040 (int)upl
, start_pg
* PAGE_SIZE
, io_size
, error
, 0);
3042 if ((last_pg
- start_pg
) < pages_in_upl
) {
3047 * the set of pages that we issued an I/O for did not encompass
3048 * the entire upl... so just release these without modifying
3052 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3054 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_START
,
3055 (int)upl
, -1, pages_in_upl
- (last_pg
- start_pg
), 0, 0);
3059 * we found some already valid pages at the beginning of
3060 * the upl commit these back to the inactive list with
3063 for (cur_pg
= 0; cur_pg
< start_pg
; cur_pg
++) {
3064 commit_flags
= UPL_COMMIT_FREE_ON_EMPTY
3065 | UPL_COMMIT_INACTIVATE
;
3067 if (upl_dirty_page(pl
, cur_pg
))
3068 commit_flags
|= UPL_COMMIT_SET_DIRTY
;
3070 if ( !(commit_flags
& UPL_COMMIT_SET_DIRTY
) && (flags
& IO_NOCACHE
))
3071 ubc_upl_abort_range(upl
, cur_pg
* PAGE_SIZE
, PAGE_SIZE
,
3072 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3074 ubc_upl_commit_range(upl
, cur_pg
* PAGE_SIZE
,
3075 PAGE_SIZE
, commit_flags
);
3078 if (last_pg
< uio_last
) {
3080 * we found some already valid pages immediately after the
3081 * pages we issued I/O for, commit these back to the
3082 * inactive list with reference cleared
3084 for (cur_pg
= last_pg
; cur_pg
< uio_last
; cur_pg
++) {
3085 commit_flags
= UPL_COMMIT_FREE_ON_EMPTY
3086 | UPL_COMMIT_INACTIVATE
;
3088 if (upl_dirty_page(pl
, cur_pg
))
3089 commit_flags
|= UPL_COMMIT_SET_DIRTY
;
3091 if ( !(commit_flags
& UPL_COMMIT_SET_DIRTY
) && (flags
& IO_NOCACHE
))
3092 ubc_upl_abort_range(upl
, cur_pg
* PAGE_SIZE
, PAGE_SIZE
,
3093 UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
3095 ubc_upl_commit_range(upl
, cur_pg
* PAGE_SIZE
,
3096 PAGE_SIZE
, commit_flags
);
3099 if (uio_last
< pages_in_upl
) {
3101 * there were some invalid pages beyond the valid pages
3102 * that we didn't issue an I/O for, just release them
3105 ubc_upl_abort_range(upl
, uio_last
* PAGE_SIZE
,
3106 (pages_in_upl
- uio_last
) * PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
3109 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 35)) | DBG_FUNC_END
,
3110 (int)upl
, -1, -1, 0, 0);
3116 if ( uio_resid(uio
) ) {
3117 if (cluster_hard_throttle_on(vp
)) {
3118 rd_ahead_enabled
= 0;
3119 prefetch_enabled
= 0;
3121 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
3124 rd_ahead_enabled
= 1;
3125 prefetch_enabled
= 1;
3127 max_rd_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
3132 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
3133 (int)uio
->uio_offset
, uio_resid(uio
), rap
->cl_lastr
, retval
, 0);
3135 lck_mtx_unlock(&rap
->cl_lockr
);
3137 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 32)) | DBG_FUNC_END
,
3138 (int)uio
->uio_offset
, uio_resid(uio
), 0, retval
, 0);
3146 cluster_nocopy_read(vnode_t vp
, struct uio
*uio
, off_t filesize
)
3149 upl_page_info_t
*pl
;
3150 vm_offset_t upl_offset
;
3154 int upl_needed_size
;
3159 int force_data_sync
;
3161 int no_zero_fill
= 0;
3163 struct clios iostate
;
3164 u_int max_rd_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
3165 u_int max_rd_ahead
= MAX_UPL_TRANSFER
* PAGE_SIZE
* 2;
3168 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_START
,
3169 (int)uio
->uio_offset
, uio_resid(uio
), (int)filesize
, 0, 0);
3172 * When we enter this routine, we know
3173 * -- the offset into the file is on a pagesize boundary
3174 * -- the resid is a page multiple
3175 * -- the resid will not exceed iov_len
3178 iostate
.io_completed
= 0;
3179 iostate
.io_issued
= 0;
3180 iostate
.io_error
= 0;
3181 iostate
.io_wanted
= 0;
3183 while (uio_resid(uio
) && uio
->uio_offset
< filesize
&& retval
== 0) {
3184 user_addr_t iov_base
;
3186 if (cluster_hard_throttle_on(vp
)) {
3187 max_rd_size
= HARD_THROTTLE_MAXSIZE
;
3188 max_rd_ahead
= HARD_THROTTLE_MAXSIZE
- 1;
3190 max_rd_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
3191 max_rd_ahead
= MAX_UPL_TRANSFER
* PAGE_SIZE
* 8;
3193 max_io_size
= filesize
- uio
->uio_offset
;
3195 // LP64todo - fix this
3196 if (max_io_size
< (off_t
)((unsigned int)uio_resid(uio
)))
3197 io_size
= max_io_size
;
3199 io_size
= uio_resid(uio
);
3202 * First look for pages already in the cache
3203 * and move them to user space.
3205 retval
= cluster_copy_ubc_data(vp
, uio
, &io_size
, 0);
3209 * we may have already spun some portion of this request
3210 * off as async requests... we need to wait for the I/O
3211 * to complete before returning
3213 goto wait_for_reads
;
3216 * If we are already finished with this read, then return
3220 * we may have already spun some portion of this request
3221 * off as async requests... we need to wait for the I/O
3222 * to complete before returning
3224 goto wait_for_reads
;
3226 max_io_size
= io_size
;
3228 if (max_io_size
> max_rd_size
)
3229 max_io_size
= max_rd_size
;
3233 ubc_range_op(vp
, uio
->uio_offset
, uio
->uio_offset
+ max_io_size
, UPL_ROP_ABSENT
, &io_size
);
3237 * we may have already spun some portion of this request
3238 * off as async requests... we need to wait for the I/O
3239 * to complete before returning
3241 goto wait_for_reads
;
3243 iov_base
= uio_curriovbase(uio
);
3245 // LP64todo - fix this!
3246 upl_offset
= CAST_DOWN(vm_offset_t
, iov_base
) & PAGE_MASK
;
3247 upl_needed_size
= (upl_offset
+ io_size
+ (PAGE_SIZE
-1)) & ~PAGE_MASK
;
3249 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_START
,
3250 (int)upl_offset
, upl_needed_size
, (int)iov_base
, io_size
, 0);
3252 if (upl_offset
== 0 && ((io_size
& PAGE_MASK
) == 0)) {
3254 abort_flag
= UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
;
3257 abort_flag
= UPL_ABORT_FREE_ON_EMPTY
;
3259 for (force_data_sync
= 0; force_data_sync
< 3; force_data_sync
++) {
3261 upl_size
= upl_needed_size
;
3262 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
3265 upl_flags
|= UPL_NOZEROFILL
;
3266 if (force_data_sync
)
3267 upl_flags
|= UPL_FORCE_DATA_SYNC
;
3269 // LP64todo - fix this!
3270 kret
= vm_map_create_upl(current_map(),
3271 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
3272 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
);
3274 if (kret
!= KERN_SUCCESS
) {
3275 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
3276 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
3278 * cluster_nocopy_read: failed to get pagelist
3280 * we may have already spun some portion of this request
3281 * off as async requests... we need to wait for the I/O
3282 * to complete before returning
3284 goto wait_for_reads
;
3286 pages_in_pl
= upl_size
/ PAGE_SIZE
;
3287 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
3289 for (i
= 0; i
< pages_in_pl
; i
++) {
3290 if (!upl_valid_page(pl
, i
))
3293 if (i
== pages_in_pl
)
3296 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
, abort_flag
);
3298 if (force_data_sync
>= 3) {
3299 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
3300 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
3302 goto wait_for_reads
;
3305 * Consider the possibility that upl_size wasn't satisfied.
3307 if (upl_size
!= upl_needed_size
)
3308 io_size
= (upl_size
- (int)upl_offset
) & ~PAGE_MASK
;
3311 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
, abort_flag
);
3312 goto wait_for_reads
;
3314 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 72)) | DBG_FUNC_END
,
3315 (int)upl_offset
, upl_size
, io_size
, kret
, 0);
3318 * request asynchronously so that we can overlap
3319 * the preparation of the next I/O
3320 * if there are already too many outstanding reads
3321 * wait until some have completed before issuing the next read
3323 lck_mtx_lock(cl_mtxp
);
3325 while ((iostate
.io_issued
- iostate
.io_completed
) > max_rd_ahead
) {
3326 iostate
.io_wanted
= 1;
3327 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_nocopy_read", 0);
3329 lck_mtx_unlock(cl_mtxp
);
3331 if (iostate
.io_error
) {
3333 * one of the earlier reads we issued ran into a hard error
3334 * don't issue any more reads, cleanup the UPL
3335 * that was just created but not used, then
3336 * go wait for any other reads to complete before
3337 * returning the error to the caller
3339 ubc_upl_abort_range(upl
, (upl_offset
& ~PAGE_MASK
), upl_size
, abort_flag
);
3341 goto wait_for_reads
;
3343 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_START
,
3344 (int)upl
, (int)upl_offset
, (int)uio
->uio_offset
, io_size
, 0);
3346 retval
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
, io_size
,
3347 CL_PRESERVE
| CL_COMMIT
| CL_READ
| CL_ASYNC
| CL_NOZERO
,
3348 (buf_t
)NULL
, &iostate
);
3351 * update the uio structure
3353 uio_update(uio
, (user_size_t
)io_size
);
3355 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 73)) | DBG_FUNC_END
,
3356 (int)upl
, (int)uio
->uio_offset
, (int)uio_resid(uio
), retval
, 0);
3362 * make sure all async reads that are part of this stream
3363 * have completed before we return
3365 lck_mtx_lock(cl_mtxp
);
3367 while (iostate
.io_issued
!= iostate
.io_completed
) {
3368 iostate
.io_wanted
= 1;
3369 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_nocopy_read", 0);
3371 lck_mtx_unlock(cl_mtxp
);
3373 if (iostate
.io_error
)
3374 retval
= iostate
.io_error
;
3376 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 70)) | DBG_FUNC_END
,
3377 (int)uio
->uio_offset
, (int)uio_resid(uio
), 6, retval
, 0);
3384 cluster_phys_read(vnode_t vp
, struct uio
*uio
, off_t filesize
)
3386 upl_page_info_t
*pl
;
3388 vm_offset_t upl_offset
;
3392 user_size_t iov_len
;
3393 user_addr_t iov_base
;
3396 int upl_needed_size
;
3400 struct clios iostate
;
3404 devblocksize
= vp
->v_mount
->mnt_devblocksize
;
3406 * When we enter this routine, we know
3407 * -- the resid will not exceed iov_len
3408 * -- the target address is physically contiguous
3412 if (IS_VALID_UIO_SEGFLG(uio
->uio_segflg
) == 0) {
3413 panic("%s :%d - invalid uio_segflg\n", __FILE__
, __LINE__
);
3415 #endif /* LP64_DEBUG */
3417 iov_len
= uio_curriovlen(uio
);
3418 iov_base
= uio_curriovbase(uio
);
3420 max_size
= filesize
- uio
->uio_offset
;
3422 // LP64todo - fix this!
3423 if (max_size
< 0 || (u_int64_t
)max_size
> iov_len
)
3428 // LP64todo - fix this!
3429 upl_offset
= CAST_DOWN(vm_offset_t
, iov_base
) & PAGE_MASK
;
3430 upl_needed_size
= upl_offset
+ io_size
;
3434 upl_size
= upl_needed_size
;
3435 upl_flags
= UPL_FILE_IO
| UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
| UPL_SET_LITE
| UPL_SET_IO_WIRE
;
3437 kret
= vm_map_get_upl(current_map(),
3438 (vm_map_offset_t
)(iov_base
& ~((user_addr_t
)PAGE_MASK
)),
3439 &upl_size
, &upl
, NULL
, &pages_in_pl
, &upl_flags
, 0);
3441 if (kret
!= KERN_SUCCESS
) {
3443 * cluster_phys_read: failed to get pagelist
3447 if (upl_size
< upl_needed_size
) {
3449 * The upl_size wasn't satisfied.
3451 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3455 pl
= ubc_upl_pageinfo(upl
);
3457 dst_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)upl_offset
;
3459 while (((uio
->uio_offset
& (devblocksize
- 1)) || io_size
< devblocksize
) && io_size
) {
3462 head_size
= devblocksize
- (int)(uio
->uio_offset
& (devblocksize
- 1));
3464 if (head_size
> io_size
)
3465 head_size
= io_size
;
3467 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, head_size
, CL_READ
);
3470 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3474 upl_offset
+= head_size
;
3475 dst_paddr
+= head_size
;
3476 io_size
-= head_size
;
3478 tail_size
= io_size
& (devblocksize
- 1);
3479 io_size
-= tail_size
;
3481 iostate
.io_completed
= 0;
3482 iostate
.io_issued
= 0;
3483 iostate
.io_error
= 0;
3484 iostate
.io_wanted
= 0;
3486 while (io_size
&& error
== 0) {
3489 if (io_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
3490 xsize
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
3494 * request asynchronously so that we can overlap
3495 * the preparation of the next I/O... we'll do
3496 * the commit after all the I/O has completed
3497 * since its all issued against the same UPL
3498 * if there are already too many outstanding reads
3499 * wait until some have completed before issuing the next
3501 lck_mtx_lock(cl_mtxp
);
3503 while ((iostate
.io_issued
- iostate
.io_completed
) > (8 * MAX_UPL_TRANSFER
* PAGE_SIZE
)) {
3504 iostate
.io_wanted
= 1;
3505 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_phys_read", 0);
3507 lck_mtx_unlock(cl_mtxp
);
3509 error
= cluster_io(vp
, upl
, upl_offset
, uio
->uio_offset
, xsize
,
3510 CL_READ
| CL_NOZERO
| CL_DEV_MEMORY
| CL_ASYNC
,
3511 (buf_t
)NULL
, &iostate
);
3513 * The cluster_io read was issued successfully,
3514 * update the uio structure
3517 uio_update(uio
, (user_size_t
)xsize
);
3520 upl_offset
+= xsize
;
3525 * make sure all async reads that are part of this stream
3526 * have completed before we proceed
3528 lck_mtx_lock(cl_mtxp
);
3530 while (iostate
.io_issued
!= iostate
.io_completed
) {
3531 iostate
.io_wanted
= 1;
3532 msleep((caddr_t
)&iostate
.io_wanted
, cl_mtxp
, PRIBIO
+ 1, "cluster_phys_read", 0);
3534 lck_mtx_unlock(cl_mtxp
);
3536 if (iostate
.io_error
)
3537 error
= iostate
.io_error
;
3539 if (error
== 0 && tail_size
)
3540 error
= cluster_align_phys_io(vp
, uio
, dst_paddr
, tail_size
, CL_READ
);
3543 * just release our hold on the physically contiguous
3544 * region without changing any state
3546 ubc_upl_abort_range(upl
, 0, upl_size
, UPL_ABORT_FREE_ON_EMPTY
);
3553 * generate advisory I/O's in the largest chunks possible
3554 * the completed pages will be released into the VM cache
3557 advisory_read(vnode_t vp
, off_t filesize
, off_t f_offset
, int resid
)
3559 upl_page_info_t
*pl
;
3561 vm_offset_t upl_offset
;
3575 if ( !UBCINFOEXISTS(vp
))
3578 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_START
,
3579 (int)f_offset
, resid
, (int)filesize
, 0, 0);
3581 while (resid
&& f_offset
< filesize
&& retval
== 0) {
3583 * compute the size of the upl needed to encompass
3584 * the requested read... limit each call to cluster_io
3585 * to the maximum UPL size... cluster_io will clip if
3586 * this exceeds the maximum io_size for the device,
3587 * make sure to account for
3588 * a starting offset that's not page aligned
3590 start_offset
= (int)(f_offset
& PAGE_MASK_64
);
3591 upl_f_offset
= f_offset
- (off_t
)start_offset
;
3592 max_size
= filesize
- f_offset
;
3594 if (resid
< max_size
)
3599 upl_size
= (start_offset
+ io_size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
3600 if (upl_size
> (MAX_UPL_TRANSFER
* PAGE_SIZE
))
3601 upl_size
= MAX_UPL_TRANSFER
* PAGE_SIZE
;
3605 * return the number of contiguously present pages in the cache
3606 * starting at upl_f_offset within the file
3608 ubc_range_op(vp
, upl_f_offset
, upl_f_offset
+ upl_size
, UPL_ROP_PRESENT
, &skip_range
);
3612 * skip over pages already present in the cache
3614 io_size
= skip_range
- start_offset
;
3616 f_offset
+= io_size
;
3619 if (skip_range
== upl_size
)
3622 * have to issue some real I/O
3623 * at this point, we know it's starting on a page boundary
3624 * because we've skipped over at least the first page in the request
3627 upl_f_offset
+= skip_range
;
3628 upl_size
-= skip_range
;
3630 pages_in_upl
= upl_size
/ PAGE_SIZE
;
3632 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_START
,
3633 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3635 kret
= ubc_create_upl(vp
,
3640 UPL_RET_ONLY_ABSENT
| UPL_SET_LITE
);
3641 if (kret
!= KERN_SUCCESS
)
3646 * before we start marching forward, we must make sure we end on
3647 * a present page, otherwise we will be working with a freed
3650 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
3651 if (upl_page_present(pl
, last_pg
))
3654 pages_in_upl
= last_pg
+ 1;
3657 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 61)) | DBG_FUNC_END
,
3658 (int)upl
, (int)upl_f_offset
, upl_size
, start_offset
, 0);
3661 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
3663 * scan from the beginning of the upl looking for the first
3664 * page that is present.... this will become the first page in
3665 * the request we're going to make to 'cluster_io'... if all
3666 * of the pages are absent, we won't call through to 'cluster_io'
3668 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
3669 if (upl_page_present(pl
, start_pg
))
3674 * scan from the starting present page looking for an absent
3675 * page before the end of the upl is reached, if we
3676 * find one, then it will terminate the range of pages being
3677 * presented to 'cluster_io'
3679 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
3680 if (!upl_page_present(pl
, last_pg
))
3684 if (last_pg
> start_pg
) {
3686 * we found a range of pages that must be filled
3687 * if the last page in this range is the last page of the file
3688 * we may have to clip the size of it to keep from reading past
3689 * the end of the last physical block associated with the file
3691 upl_offset
= start_pg
* PAGE_SIZE
;
3692 io_size
= (last_pg
- start_pg
) * PAGE_SIZE
;
3694 if ((upl_f_offset
+ upl_offset
+ io_size
) > filesize
)
3695 io_size
= filesize
- (upl_f_offset
+ upl_offset
);
3698 * issue an asynchronous read to cluster_io
3700 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
3701 CL_ASYNC
| CL_READ
| CL_COMMIT
| CL_AGE
, (buf_t
)NULL
, (struct clios
*)NULL
);
3707 ubc_upl_abort(upl
, 0);
3709 io_size
= upl_size
- start_offset
;
3711 if (io_size
> resid
)
3713 f_offset
+= io_size
;
3717 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 60)) | DBG_FUNC_END
,
3718 (int)f_offset
, resid
, retval
, 0, 0);
3725 cluster_push(vnode_t vp
, int flags
)
3728 struct cl_writebehind
*wbp
;
3730 if ( !UBCINFOEXISTS(vp
)) {
3731 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, (int)vp
, flags
, 0, -1, 0);
3734 /* return if deferred write is set */
3735 if (((unsigned int)vfs_flags(vp
->v_mount
) & MNT_DEFWRITE
) && (flags
& IO_DEFWRITE
)) {
3738 if ((wbp
= cluster_get_wbp(vp
, CLW_RETURNLOCKED
)) == NULL
) {
3739 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, (int)vp
, flags
, 0, -2, 0);
3742 if (wbp
->cl_number
== 0 && wbp
->cl_scmap
== NULL
) {
3743 lck_mtx_unlock(&wbp
->cl_lockw
);
3745 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_NONE
, (int)vp
, flags
, 0, -3, 0);
3748 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_START
,
3749 (int)wbp
->cl_scmap
, wbp
->cl_number
, flags
, 0, 0);
3751 if (wbp
->cl_scmap
) {
3752 sparse_cluster_push(wbp
, vp
, ubc_getsize(vp
), 1);
3756 retval
= cluster_try_push(wbp
, vp
, ubc_getsize(vp
), 0, 1);
3758 lck_mtx_unlock(&wbp
->cl_lockw
);
3760 if (flags
& IO_SYNC
)
3761 (void)vnode_waitforwrites(vp
, 0, 0, 0, (char *)"cluster_push");
3763 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_END
,
3764 (int)wbp
->cl_scmap
, wbp
->cl_number
, retval
, 0, 0);
3770 __private_extern__
void
3771 cluster_release(struct ubc_info
*ubc
)
3773 struct cl_writebehind
*wbp
;
3774 struct cl_readahead
*rap
;
3776 if ((wbp
= ubc
->cl_wbehind
)) {
3778 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, (int)ubc
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
3781 vfs_drt_control(&(wbp
->cl_scmap
), 0);
3783 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_START
, (int)ubc
, 0, 0, 0, 0);
3786 rap
= ubc
->cl_rahead
;
3789 lck_mtx_destroy(&wbp
->cl_lockw
, cl_mtx_grp
);
3790 FREE_ZONE((void *)wbp
, sizeof *wbp
, M_CLWRBEHIND
);
3792 if ((rap
= ubc
->cl_rahead
)) {
3793 lck_mtx_destroy(&rap
->cl_lockr
, cl_mtx_grp
);
3794 FREE_ZONE((void *)rap
, sizeof *rap
, M_CLRDAHEAD
);
3796 ubc
->cl_rahead
= NULL
;
3797 ubc
->cl_wbehind
= NULL
;
3799 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 81)) | DBG_FUNC_END
, (int)ubc
, (int)rap
, (int)wbp
, 0, 0);
3804 cluster_push_EOF(vnode_t vp
, off_t EOF
)
3806 struct cl_writebehind
*wbp
;
3808 wbp
= cluster_get_wbp(vp
, CLW_ALLOCATE
| CLW_RETURNLOCKED
);
3810 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_START
,
3811 (int)wbp
->cl_scmap
, wbp
->cl_number
, (int)EOF
, 0, 0);
3814 sparse_cluster_push(wbp
, vp
, EOF
, 1);
3816 cluster_try_push(wbp
, vp
, EOF
, 0, 1);
3818 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 53)) | DBG_FUNC_END
,
3819 (int)wbp
->cl_scmap
, wbp
->cl_number
, 0, 0, 0);
3821 lck_mtx_unlock(&wbp
->cl_lockw
);
3826 cluster_try_push(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int can_delay
, int push_all
)
3833 struct cl_wextent l_clusters
[MAX_CLUSTERS
];
3836 * the write behind context exists and has
3837 * already been locked...
3839 * make a local 'sorted' copy of the clusters
3840 * and clear wbp->cl_number so that new clusters can
3843 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
3844 for (min_index
= -1, cl_index1
= 0; cl_index1
< wbp
->cl_number
; cl_index1
++) {
3845 if (wbp
->cl_clusters
[cl_index1
].b_addr
== wbp
->cl_clusters
[cl_index1
].e_addr
)
3847 if (min_index
== -1)
3848 min_index
= cl_index1
;
3849 else if (wbp
->cl_clusters
[cl_index1
].b_addr
< wbp
->cl_clusters
[min_index
].b_addr
)
3850 min_index
= cl_index1
;
3852 if (min_index
== -1)
3854 l_clusters
[cl_index
].b_addr
= wbp
->cl_clusters
[min_index
].b_addr
;
3855 l_clusters
[cl_index
].e_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
3856 l_clusters
[cl_index
].io_nocache
= wbp
->cl_clusters
[min_index
].io_nocache
;
3858 wbp
->cl_clusters
[min_index
].b_addr
= wbp
->cl_clusters
[min_index
].e_addr
;
3864 if (can_delay
&& cl_len
== MAX_CLUSTERS
) {
3868 * determine if we appear to be writing the file sequentially
3869 * if not, by returning without having pushed any clusters
3870 * we will cause this vnode to be pushed into the sparse cluster mechanism
3871 * used for managing more random I/O patterns
3873 * we know that we've got all clusters currently in use and the next write doesn't fit into one of them...
3874 * that's why we're in try_push with can_delay true...
3876 * check to make sure that all the clusters except the last one are 'full'... and that each cluster
3877 * is adjacent to the next (i.e. we're looking for sequential writes) they were sorted above
3878 * so we can just make a simple pass through, up to, but not including the last one...
3879 * note that e_addr is not inclusive, so it will be equal to the b_addr of the next cluster if they
3882 * we let the last one be partial as long as it was adjacent to the previous one...
3883 * we need to do this to deal with multi-threaded servers that might write an I/O or 2 out
3884 * of order... if this occurs at the tail of the last cluster, we don't want to fall into the sparse cluster world...
3886 for (i
= 0; i
< MAX_CLUSTERS
- 1; i
++) {
3887 if ((l_clusters
[i
].e_addr
- l_clusters
[i
].b_addr
) != MAX_UPL_TRANSFER
)
3889 if (l_clusters
[i
].e_addr
!= l_clusters
[i
+1].b_addr
)
3894 * drop the lock while we're firing off the I/Os...
3895 * this is safe since I'm working off of a private sorted copy
3896 * of the clusters, and I'm going to re-evaluate the public
3897 * state after I retake the lock
3899 lck_mtx_unlock(&wbp
->cl_lockw
);
3901 for (cl_index
= 0; cl_index
< cl_len
; cl_index
++) {
3903 struct cl_extent cl
;
3906 * try to push each cluster in turn...
3908 if (l_clusters
[cl_index
].io_nocache
)
3912 cl
.b_addr
= l_clusters
[cl_index
].b_addr
;
3913 cl
.e_addr
= l_clusters
[cl_index
].e_addr
;
3915 cluster_push_x(vp
, &cl
, EOF
, flags
);
3917 l_clusters
[cl_index
].b_addr
= 0;
3918 l_clusters
[cl_index
].e_addr
= 0;
3925 lck_mtx_lock(&wbp
->cl_lockw
);
3928 if (cl_len
> cl_pushed
) {
3930 * we didn't push all of the clusters, so
3931 * lets try to merge them back in to the vnode
3933 if ((MAX_CLUSTERS
- wbp
->cl_number
) < (cl_len
- cl_pushed
)) {
3935 * we picked up some new clusters while we were trying to
3936 * push the old ones... this can happen because I've dropped
3937 * the vnode lock... the sum of the
3938 * leftovers plus the new cluster count exceeds our ability
3939 * to represent them, so switch to the sparse cluster mechanism
3941 * collect the active public clusters...
3943 sparse_cluster_switch(wbp
, vp
, EOF
);
3945 for (cl_index
= 0, cl_index1
= 0; cl_index
< cl_len
; cl_index
++) {
3946 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
3948 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
3949 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
3950 wbp
->cl_clusters
[cl_index1
].io_nocache
= l_clusters
[cl_index
].io_nocache
;
3955 * update the cluster count
3957 wbp
->cl_number
= cl_index1
;
3960 * and collect the original clusters that were moved into the
3961 * local storage for sorting purposes
3963 sparse_cluster_switch(wbp
, vp
, EOF
);
3967 * we've got room to merge the leftovers back in
3968 * just append them starting at the next 'hole'
3969 * represented by wbp->cl_number
3971 for (cl_index
= 0, cl_index1
= wbp
->cl_number
; cl_index
< cl_len
; cl_index
++) {
3972 if (l_clusters
[cl_index
].b_addr
== l_clusters
[cl_index
].e_addr
)
3975 wbp
->cl_clusters
[cl_index1
].b_addr
= l_clusters
[cl_index
].b_addr
;
3976 wbp
->cl_clusters
[cl_index1
].e_addr
= l_clusters
[cl_index
].e_addr
;
3977 wbp
->cl_clusters
[cl_index1
].io_nocache
= l_clusters
[cl_index
].io_nocache
;
3982 * update the cluster count
3984 wbp
->cl_number
= cl_index1
;
3987 return(MAX_CLUSTERS
- wbp
->cl_number
);
3993 cluster_push_x(vnode_t vp
, struct cl_extent
*cl
, off_t EOF
, int flags
)
3995 upl_page_info_t
*pl
;
3997 vm_offset_t upl_offset
;
4012 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_START
,
4013 (int)cl
->b_addr
, (int)cl
->e_addr
, (int)EOF
, flags
, 0);
4015 if ((pages_in_upl
= (int)(cl
->e_addr
- cl
->b_addr
)) == 0) {
4016 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 0, 0, 0, 0);
4020 upl_size
= pages_in_upl
* PAGE_SIZE
;
4021 upl_f_offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
4023 if (upl_f_offset
+ upl_size
>= EOF
) {
4025 if (upl_f_offset
>= EOF
) {
4027 * must have truncated the file and missed
4028 * clearing a dangling cluster (i.e. it's completely
4029 * beyond the new EOF
4031 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 1, 0, 0, 0);
4035 size
= EOF
- upl_f_offset
;
4037 upl_size
= (size
+ (PAGE_SIZE
- 1)) & ~PAGE_MASK
;
4038 pages_in_upl
= upl_size
/ PAGE_SIZE
;
4042 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_START
, upl_size
, size
, 0, 0, 0);
4045 * by asking for UPL_COPYOUT_FROM and UPL_RET_ONLY_DIRTY, we get the following desirable behavior
4047 * - only pages that are currently dirty are returned... these are the ones we need to clean
4048 * - the hardware dirty bit is cleared when the page is gathered into the UPL... the software dirty bit is set
4049 * - 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
4050 * - when we commit the page, the software dirty bit is cleared... the hardware dirty bit is untouched so that if
4051 * someone dirties this page while the I/O is in progress, we don't lose track of the new state
4053 * when the I/O completes, we no longer ask for an explicit clear of the DIRTY state (either soft or hard)
4056 if ((vp
->v_flag
& VNOCACHE_DATA
) || (flags
& IO_NOCACHE
))
4057 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
| UPL_WILL_BE_DUMPED
;
4059 upl_flags
= UPL_COPYOUT_FROM
| UPL_RET_ONLY_DIRTY
| UPL_SET_LITE
;
4061 kret
= ubc_create_upl(vp
,
4067 if (kret
!= KERN_SUCCESS
)
4068 panic("cluster_push: failed to get pagelist");
4070 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 41)) | DBG_FUNC_END
, (int)upl
, upl_f_offset
, 0, 0, 0);
4073 * since we only asked for the dirty pages back
4074 * it's possible that we may only get a few or even none, so...
4075 * before we start marching forward, we must make sure we know
4076 * where the last present page is in the UPL, otherwise we could
4077 * end up working with a freed upl due to the FREE_ON_EMPTY semantics
4078 * employed by commit_range and abort_range.
4080 for (last_pg
= pages_in_upl
- 1; last_pg
>= 0; last_pg
--) {
4081 if (upl_page_present(pl
, last_pg
))
4084 pages_in_upl
= last_pg
+ 1;
4086 if (pages_in_upl
== 0) {
4087 ubc_upl_abort(upl
, 0);
4089 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 2, 0, 0, 0);
4093 for (last_pg
= 0; last_pg
< pages_in_upl
; ) {
4095 * find the next dirty page in the UPL
4096 * this will become the first page in the
4097 * next I/O to generate
4099 for (start_pg
= last_pg
; start_pg
< pages_in_upl
; start_pg
++) {
4100 if (upl_dirty_page(pl
, start_pg
))
4102 if (upl_page_present(pl
, start_pg
))
4104 * RET_ONLY_DIRTY will return non-dirty 'precious' pages
4105 * just release these unchanged since we're not going
4106 * to steal them or change their state
4108 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
4110 if (start_pg
>= pages_in_upl
)
4112 * done... no more dirty pages to push
4115 if (start_pg
> last_pg
)
4117 * skipped over some non-dirty pages
4119 size
-= ((start_pg
- last_pg
) * PAGE_SIZE
);
4122 * find a range of dirty pages to write
4124 for (last_pg
= start_pg
; last_pg
< pages_in_upl
; last_pg
++) {
4125 if (!upl_dirty_page(pl
, last_pg
))
4128 upl_offset
= start_pg
* PAGE_SIZE
;
4130 io_size
= min(size
, (last_pg
- start_pg
) * PAGE_SIZE
);
4132 io_flags
= CL_THROTTLE
| CL_COMMIT
;
4134 if ( !(flags
& IO_SYNC
))
4135 io_flags
|= CL_ASYNC
;
4137 retval
= cluster_io(vp
, upl
, upl_offset
, upl_f_offset
+ upl_offset
, io_size
,
4138 io_flags
, (buf_t
)NULL
, (struct clios
*)NULL
);
4140 if (error
== 0 && retval
)
4145 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 51)) | DBG_FUNC_END
, 1, 3, 0, 0, 0);
4152 * sparse_cluster_switch is called with the write behind lock held
4155 sparse_cluster_switch(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
)
4159 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_START
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
4161 if (wbp
->cl_scmap
== NULL
)
4162 wbp
->cl_scdirty
= 0;
4164 for (cl_index
= 0; cl_index
< wbp
->cl_number
; cl_index
++) {
4166 struct cl_extent cl
;
4168 for (cl
.b_addr
= wbp
->cl_clusters
[cl_index
].b_addr
; cl
.b_addr
< wbp
->cl_clusters
[cl_index
].e_addr
; cl
.b_addr
++) {
4170 if (ubc_page_op(vp
, (off_t
)(cl
.b_addr
* PAGE_SIZE_64
), 0, 0, &flags
) == KERN_SUCCESS
) {
4171 if (flags
& UPL_POP_DIRTY
) {
4172 cl
.e_addr
= cl
.b_addr
+ 1;
4174 sparse_cluster_add(wbp
, vp
, &cl
, EOF
);
4181 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 78)) | DBG_FUNC_END
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
4186 * sparse_cluster_push is called with the write behind lock held
4189 sparse_cluster_push(struct cl_writebehind
*wbp
, vnode_t vp
, off_t EOF
, int push_all
)
4191 struct cl_extent cl
;
4195 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_START
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, push_all
, 0);
4198 vfs_drt_control(&(wbp
->cl_scmap
), 1);
4201 if (vfs_drt_get_cluster(&(wbp
->cl_scmap
), &offset
, &length
) != KERN_SUCCESS
)
4204 cl
.b_addr
= (daddr64_t
)(offset
/ PAGE_SIZE_64
);
4205 cl
.e_addr
= (daddr64_t
)((offset
+ length
) / PAGE_SIZE_64
);
4207 wbp
->cl_scdirty
-= (int)(cl
.e_addr
- cl
.b_addr
);
4209 cluster_push_x(vp
, &cl
, EOF
, 0);
4214 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 79)) | DBG_FUNC_END
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
4219 * sparse_cluster_add is called with the write behind lock held
4222 sparse_cluster_add(struct cl_writebehind
*wbp
, vnode_t vp
, struct cl_extent
*cl
, off_t EOF
)
4228 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);
4230 offset
= (off_t
)(cl
->b_addr
* PAGE_SIZE_64
);
4231 length
= ((u_int
)(cl
->e_addr
- cl
->b_addr
)) * PAGE_SIZE
;
4233 while (vfs_drt_mark_pages(&(wbp
->cl_scmap
), offset
, length
, &new_dirty
) != KERN_SUCCESS
) {
4235 * no room left in the map
4236 * only a partial update was done
4237 * push out some pages and try again
4239 wbp
->cl_scdirty
+= new_dirty
;
4241 sparse_cluster_push(wbp
, vp
, EOF
, 0);
4243 offset
+= (new_dirty
* PAGE_SIZE_64
);
4244 length
-= (new_dirty
* PAGE_SIZE
);
4246 wbp
->cl_scdirty
+= new_dirty
;
4248 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 80)) | DBG_FUNC_END
, (int)vp
, (int)wbp
->cl_scmap
, wbp
->cl_scdirty
, 0, 0);
4253 cluster_align_phys_io(vnode_t vp
, struct uio
*uio
, addr64_t usr_paddr
, int xsize
, int flags
)
4255 upl_page_info_t
*pl
;
4264 upl_flags
= UPL_SET_LITE
;
4265 if (! (flags
& CL_READ
)) {
4267 * "write" operation: let the UPL subsystem know
4268 * that we intend to modify the buffer cache pages
4271 upl_flags
|= UPL_WILL_MODIFY
;
4274 kret
= ubc_create_upl(vp
,
4275 uio
->uio_offset
& ~PAGE_MASK_64
,
4281 if (kret
!= KERN_SUCCESS
)
4284 if (!upl_valid_page(pl
, 0)) {
4286 * issue a synchronous read to cluster_io
4288 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
4289 CL_READ
, (buf_t
)NULL
, (struct clios
*)NULL
);
4291 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, UPL_ABORT_DUMP_PAGES
| UPL_ABORT_FREE_ON_EMPTY
);
4297 ubc_paddr
= ((addr64_t
)upl_phys_page(pl
, 0) << 12) + (addr64_t
)(uio
->uio_offset
& PAGE_MASK_64
);
4300 * NOTE: There is no prototype for the following in BSD. It, and the definitions
4301 * of the defines for cppvPsrc, cppvPsnk, cppvFsnk, and cppvFsrc will be found in
4302 * osfmk/ppc/mappings.h. They are not included here because there appears to be no
4303 * way to do so without exporting them to kexts as well.
4305 if (flags
& CL_READ
)
4306 // copypv(ubc_paddr, usr_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsnk); /* Copy physical to physical and flush the destination */
4307 copypv(ubc_paddr
, usr_paddr
, xsize
, 2 | 1 | 4); /* Copy physical to physical and flush the destination */
4309 // copypv(usr_paddr, ubc_paddr, xsize, cppvPsrc | cppvPsnk | cppvFsrc); /* Copy physical to physical and flush the source */
4310 copypv(usr_paddr
, ubc_paddr
, xsize
, 2 | 1 | 8); /* Copy physical to physical and flush the source */
4312 if ( !(flags
& CL_READ
) || (upl_valid_page(pl
, 0) && upl_dirty_page(pl
, 0))) {
4314 * issue a synchronous write to cluster_io
4316 error
= cluster_io(vp
, upl
, 0, uio
->uio_offset
& ~PAGE_MASK_64
, PAGE_SIZE
,
4317 0, (buf_t
)NULL
, (struct clios
*)NULL
);
4320 uio_update(uio
, (user_size_t
)xsize
);
4323 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
;
4325 abort_flags
= UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_DUMP_PAGES
;
4327 ubc_upl_abort_range(upl
, 0, PAGE_SIZE
, abort_flags
);
4335 cluster_copy_upl_data(struct uio
*uio
, upl_t upl
, int upl_offset
, int xsize
)
4342 upl_page_info_t
*pl
;
4344 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
4345 (int)uio
->uio_offset
, uio_resid(uio
), upl_offset
, xsize
, 0);
4347 segflg
= uio
->uio_segflg
;
4351 case UIO_USERSPACE32
:
4352 case UIO_USERISPACE32
:
4353 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
4357 case UIO_USERISPACE
:
4358 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
4361 case UIO_USERSPACE64
:
4362 case UIO_USERISPACE64
:
4363 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
4366 case UIO_SYSSPACE32
:
4367 uio
->uio_segflg
= UIO_PHYS_SYSSPACE32
;
4371 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
4374 case UIO_SYSSPACE64
:
4375 uio
->uio_segflg
= UIO_PHYS_SYSSPACE64
;
4378 pl
= ubc_upl_pageinfo(upl
);
4380 pg_index
= upl_offset
/ PAGE_SIZE
;
4381 pg_offset
= upl_offset
& PAGE_MASK
;
4382 csize
= min(PAGE_SIZE
- pg_offset
, xsize
);
4384 while (xsize
&& retval
== 0) {
4387 paddr
= ((addr64_t
)upl_phys_page(pl
, pg_index
) << 12) + pg_offset
;
4389 retval
= uiomove64(paddr
, csize
, uio
);
4394 csize
= min(PAGE_SIZE
, xsize
);
4396 uio
->uio_segflg
= segflg
;
4398 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
4399 (int)uio
->uio_offset
, uio_resid(uio
), retval
, segflg
, 0);
4406 cluster_copy_ubc_data(vnode_t vp
, struct uio
*uio
, int *io_resid
, int mark_dirty
)
4413 memory_object_control_t control
;
4416 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_START
,
4417 (int)uio
->uio_offset
, uio_resid(uio
), 0, *io_resid
, 0);
4419 control
= ubc_getobject(vp
, UBC_FLAGS_NONE
);
4420 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
4421 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
4422 (int)uio
->uio_offset
, uio_resid(uio
), retval
, 3, 0);
4426 segflg
= uio
->uio_segflg
;
4430 case UIO_USERSPACE32
:
4431 case UIO_USERISPACE32
:
4432 uio
->uio_segflg
= UIO_PHYS_USERSPACE32
;
4435 case UIO_USERSPACE64
:
4436 case UIO_USERISPACE64
:
4437 uio
->uio_segflg
= UIO_PHYS_USERSPACE64
;
4440 case UIO_SYSSPACE32
:
4441 uio
->uio_segflg
= UIO_PHYS_SYSSPACE32
;
4444 case UIO_SYSSPACE64
:
4445 uio
->uio_segflg
= UIO_PHYS_SYSSPACE64
;
4449 case UIO_USERISPACE
:
4450 uio
->uio_segflg
= UIO_PHYS_USERSPACE
;
4454 uio
->uio_segflg
= UIO_PHYS_SYSSPACE
;
4458 if ( (io_size
= *io_resid
) ) {
4459 start_offset
= (int)(uio
->uio_offset
& PAGE_MASK_64
);
4460 xsize
= uio_resid(uio
);
4462 retval
= memory_object_control_uiomove(control
, uio
->uio_offset
- start_offset
,
4463 uio
, start_offset
, io_size
, mark_dirty
);
4464 xsize
-= uio_resid(uio
);
4467 uio
->uio_segflg
= segflg
;
4468 *io_resid
= io_size
;
4470 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW
, 34)) | DBG_FUNC_END
,
4471 (int)uio
->uio_offset
, uio_resid(uio
), retval
, 0x80000000 | segflg
, 0);
4478 is_file_clean(vnode_t vp
, off_t filesize
)
4482 int total_dirty
= 0;
4484 for (f_offset
= 0; f_offset
< filesize
; f_offset
+= PAGE_SIZE_64
) {
4485 if (ubc_page_op(vp
, f_offset
, 0, 0, &flags
) == KERN_SUCCESS
) {
4486 if (flags
& UPL_POP_DIRTY
) {
4500 * Dirty region tracking/clustering mechanism.
4502 * This code (vfs_drt_*) provides a mechanism for tracking and clustering
4503 * dirty regions within a larger space (file). It is primarily intended to
4504 * support clustering in large files with many dirty areas.
4506 * The implementation assumes that the dirty regions are pages.
4508 * To represent dirty pages within the file, we store bit vectors in a
4509 * variable-size circular hash.
4513 * Bitvector size. This determines the number of pages we group in a
4514 * single hashtable entry. Each hashtable entry is aligned to this
4515 * size within the file.
4517 #define DRT_BITVECTOR_PAGES 256
4520 * File offset handling.
4522 * DRT_ADDRESS_MASK is dependent on DRT_BITVECTOR_PAGES;
4523 * the correct formula is (~(DRT_BITVECTOR_PAGES * PAGE_SIZE) - 1)
4525 #define DRT_ADDRESS_MASK (~((1 << 20) - 1))
4526 #define DRT_ALIGN_ADDRESS(addr) ((addr) & DRT_ADDRESS_MASK)
4529 * Hashtable address field handling.
4531 * The low-order bits of the hashtable address are used to conserve
4534 * DRT_HASH_COUNT_MASK must be large enough to store the range
4535 * 0-DRT_BITVECTOR_PAGES inclusive, as well as have one value
4536 * to indicate that the bucket is actually unoccupied.
4538 #define DRT_HASH_GET_ADDRESS(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_ADDRESS_MASK)
4539 #define DRT_HASH_SET_ADDRESS(scm, i, a) \
4541 (scm)->scm_hashtable[(i)].dhe_control = \
4542 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_ADDRESS_MASK) | DRT_ALIGN_ADDRESS(a); \
4544 #define DRT_HASH_COUNT_MASK 0x1ff
4545 #define DRT_HASH_GET_COUNT(scm, i) ((scm)->scm_hashtable[(i)].dhe_control & DRT_HASH_COUNT_MASK)
4546 #define DRT_HASH_SET_COUNT(scm, i, c) \
4548 (scm)->scm_hashtable[(i)].dhe_control = \
4549 ((scm)->scm_hashtable[(i)].dhe_control & ~DRT_HASH_COUNT_MASK) | ((c) & DRT_HASH_COUNT_MASK); \
4551 #define DRT_HASH_CLEAR(scm, i) \
4553 (scm)->scm_hashtable[(i)].dhe_control = 0; \
4555 #define DRT_HASH_VACATE(scm, i) DRT_HASH_SET_COUNT((scm), (i), DRT_HASH_COUNT_MASK)
4556 #define DRT_HASH_VACANT(scm, i) (DRT_HASH_GET_COUNT((scm), (i)) == DRT_HASH_COUNT_MASK)
4557 #define DRT_HASH_COPY(oscm, oi, scm, i) \
4559 (scm)->scm_hashtable[(i)].dhe_control = (oscm)->scm_hashtable[(oi)].dhe_control; \
4560 DRT_BITVECTOR_COPY(oscm, oi, scm, i); \
4565 * Hash table moduli.
4567 * Since the hashtable entry's size is dependent on the size of
4568 * the bitvector, and since the hashtable size is constrained to
4569 * both being prime and fitting within the desired allocation
4570 * size, these values need to be manually determined.
4572 * For DRT_BITVECTOR_SIZE = 256, the entry size is 40 bytes.
4574 * The small hashtable allocation is 1024 bytes, so the modulus is 23.
4575 * The large hashtable allocation is 16384 bytes, so the modulus is 401.
4577 #define DRT_HASH_SMALL_MODULUS 23
4578 #define DRT_HASH_LARGE_MODULUS 401
4580 #define DRT_SMALL_ALLOCATION 1024 /* 104 bytes spare */
4581 #define DRT_LARGE_ALLOCATION 16384 /* 344 bytes spare */
4583 /* *** nothing below here has secret dependencies on DRT_BITVECTOR_PAGES *** */
4586 * Hashtable bitvector handling.
4588 * Bitvector fields are 32 bits long.
4591 #define DRT_HASH_SET_BIT(scm, i, bit) \
4592 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] |= (1 << ((bit) % 32))
4594 #define DRT_HASH_CLEAR_BIT(scm, i, bit) \
4595 (scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] &= ~(1 << ((bit) % 32))
4597 #define DRT_HASH_TEST_BIT(scm, i, bit) \
4598 ((scm)->scm_hashtable[(i)].dhe_bitvector[(bit) / 32] & (1 << ((bit) % 32)))
4600 #define DRT_BITVECTOR_CLEAR(scm, i) \
4601 bzero(&(scm)->scm_hashtable[(i)].dhe_bitvector[0], (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
4603 #define DRT_BITVECTOR_COPY(oscm, oi, scm, i) \
4604 bcopy(&(oscm)->scm_hashtable[(oi)].dhe_bitvector[0], \
4605 &(scm)->scm_hashtable[(i)].dhe_bitvector[0], \
4606 (DRT_BITVECTOR_PAGES / 32) * sizeof(u_int32_t))
4613 struct vfs_drt_hashentry
{
4614 u_int64_t dhe_control
;
4615 u_int32_t dhe_bitvector
[DRT_BITVECTOR_PAGES
/ 32];
4619 * Dirty Region Tracking structure.
4621 * The hashtable is allocated entirely inside the DRT structure.
4623 * The hash is a simple circular prime modulus arrangement, the structure
4624 * is resized from small to large if it overflows.
4627 struct vfs_drt_clustermap
{
4628 u_int32_t scm_magic
; /* sanity/detection */
4629 #define DRT_SCM_MAGIC 0x12020003
4630 u_int32_t scm_modulus
; /* current ring size */
4631 u_int32_t scm_buckets
; /* number of occupied buckets */
4632 u_int32_t scm_lastclean
; /* last entry we cleaned */
4633 u_int32_t scm_iskips
; /* number of slot skips */
4635 struct vfs_drt_hashentry scm_hashtable
[0];
4639 #define DRT_HASH(scm, addr) ((addr) % (scm)->scm_modulus)
4640 #define DRT_HASH_NEXT(scm, addr) (((addr) + 1) % (scm)->scm_modulus)
4643 * Debugging codes and arguments.
4645 #define DRT_DEBUG_EMPTYFREE (FSDBG_CODE(DBG_FSRW, 82)) /* nil */
4646 #define DRT_DEBUG_RETCLUSTER (FSDBG_CODE(DBG_FSRW, 83)) /* offset, length */
4647 #define DRT_DEBUG_ALLOC (FSDBG_CODE(DBG_FSRW, 84)) /* copycount */
4648 #define DRT_DEBUG_INSERT (FSDBG_CODE(DBG_FSRW, 85)) /* offset, iskip */
4649 #define DRT_DEBUG_MARK (FSDBG_CODE(DBG_FSRW, 86)) /* offset, length,
4652 /* 1 (clean, no map) */
4653 /* 2 (map alloc fail) */
4654 /* 3, resid (partial) */
4655 #define DRT_DEBUG_6 (FSDBG_CODE(DBG_FSRW, 87))
4656 #define DRT_DEBUG_SCMDATA (FSDBG_CODE(DBG_FSRW, 88)) /* modulus, buckets,
4657 * lastclean, iskips */
4660 static kern_return_t
vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
);
4661 static kern_return_t
vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
);
4662 static kern_return_t
vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
,
4663 u_int64_t offset
, int *indexp
);
4664 static kern_return_t
vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
,
4668 static kern_return_t
vfs_drt_do_mark_pages(
4674 static void vfs_drt_trace(
4675 struct vfs_drt_clustermap
*cmap
,
4684 * Allocate and initialise a sparse cluster map.
4686 * Will allocate a new map, resize or compact an existing map.
4688 * XXX we should probably have at least one intermediate map size,
4689 * as the 1:16 ratio seems a bit drastic.
4691 static kern_return_t
4692 vfs_drt_alloc_map(struct vfs_drt_clustermap
**cmapp
)
4694 struct vfs_drt_clustermap
*cmap
, *ocmap
;
4697 int nsize
, i
, active_buckets
, index
, copycount
;
4704 * Decide on the size of the new map.
4706 if (ocmap
== NULL
) {
4707 nsize
= DRT_HASH_SMALL_MODULUS
;
4709 /* count the number of active buckets in the old map */
4711 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
4712 if (!DRT_HASH_VACANT(ocmap
, i
) &&
4713 (DRT_HASH_GET_COUNT(ocmap
, i
) != 0))
4717 * If we're currently using the small allocation, check to
4718 * see whether we should grow to the large one.
4720 if (ocmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) {
4721 /* if the ring is nearly full */
4722 if (active_buckets
> (DRT_HASH_SMALL_MODULUS
- 5)) {
4723 nsize
= DRT_HASH_LARGE_MODULUS
;
4725 nsize
= DRT_HASH_SMALL_MODULUS
;
4728 /* already using the large modulus */
4729 nsize
= DRT_HASH_LARGE_MODULUS
;
4731 * If the ring is completely full, there's
4732 * nothing useful for us to do. Behave as
4733 * though we had compacted into the new
4736 if (active_buckets
>= DRT_HASH_LARGE_MODULUS
)
4737 return(KERN_SUCCESS
);
4742 * Allocate and initialise the new map.
4745 kret
= kmem_alloc(kernel_map
, (vm_offset_t
*)&cmap
,
4746 (nsize
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
4747 if (kret
!= KERN_SUCCESS
)
4749 cmap
->scm_magic
= DRT_SCM_MAGIC
;
4750 cmap
->scm_modulus
= nsize
;
4751 cmap
->scm_buckets
= 0;
4752 cmap
->scm_lastclean
= 0;
4753 cmap
->scm_iskips
= 0;
4754 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
4755 DRT_HASH_CLEAR(cmap
, i
);
4756 DRT_HASH_VACATE(cmap
, i
);
4757 DRT_BITVECTOR_CLEAR(cmap
, i
);
4761 * If there's an old map, re-hash entries from it into the new map.
4764 if (ocmap
!= NULL
) {
4765 for (i
= 0; i
< ocmap
->scm_modulus
; i
++) {
4766 /* skip empty buckets */
4767 if (DRT_HASH_VACANT(ocmap
, i
) ||
4768 (DRT_HASH_GET_COUNT(ocmap
, i
) == 0))
4771 offset
= DRT_HASH_GET_ADDRESS(ocmap
, i
);
4772 kret
= vfs_drt_get_index(&cmap
, offset
, &index
, 1);
4773 if (kret
!= KERN_SUCCESS
) {
4774 /* XXX need to bail out gracefully here */
4775 panic("vfs_drt: new cluster map mysteriously too small");
4778 DRT_HASH_COPY(ocmap
, i
, cmap
, index
);
4783 /* log what we've done */
4784 vfs_drt_trace(cmap
, DRT_DEBUG_ALLOC
, copycount
, 0, 0, 0);
4787 * It's important to ensure that *cmapp always points to
4788 * a valid map, so we must overwrite it before freeing
4792 if (ocmap
!= NULL
) {
4793 /* emit stats into trace buffer */
4794 vfs_drt_trace(ocmap
, DRT_DEBUG_SCMDATA
,
4797 ocmap
->scm_lastclean
,
4800 vfs_drt_free_map(ocmap
);
4802 return(KERN_SUCCESS
);
4807 * Free a sparse cluster map.
4809 static kern_return_t
4810 vfs_drt_free_map(struct vfs_drt_clustermap
*cmap
)
4812 kmem_free(kernel_map
, (vm_offset_t
)cmap
,
4813 (cmap
->scm_modulus
== DRT_HASH_SMALL_MODULUS
) ? DRT_SMALL_ALLOCATION
: DRT_LARGE_ALLOCATION
);
4814 return(KERN_SUCCESS
);
4819 * Find the hashtable slot currently occupied by an entry for the supplied offset.
4821 static kern_return_t
4822 vfs_drt_search_index(struct vfs_drt_clustermap
*cmap
, u_int64_t offset
, int *indexp
)
4826 offset
= DRT_ALIGN_ADDRESS(offset
);
4827 index
= DRT_HASH(cmap
, offset
);
4829 /* traverse the hashtable */
4830 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
4833 * If the slot is vacant, we can stop.
4835 if (DRT_HASH_VACANT(cmap
, index
))
4839 * If the address matches our offset, we have success.
4841 if (DRT_HASH_GET_ADDRESS(cmap
, index
) == offset
) {
4843 return(KERN_SUCCESS
);
4847 * Move to the next slot, try again.
4849 index
= DRT_HASH_NEXT(cmap
, index
);
4854 return(KERN_FAILURE
);
4858 * Find the hashtable slot for the supplied offset. If we haven't allocated
4859 * one yet, allocate one and populate the address field. Note that it will
4860 * not have a nonzero page count and thus will still technically be free, so
4861 * in the case where we are called to clean pages, the slot will remain free.
4863 static kern_return_t
4864 vfs_drt_get_index(struct vfs_drt_clustermap
**cmapp
, u_int64_t offset
, int *indexp
, int recursed
)
4866 struct vfs_drt_clustermap
*cmap
;
4872 /* look for an existing entry */
4873 kret
= vfs_drt_search_index(cmap
, offset
, indexp
);
4874 if (kret
== KERN_SUCCESS
)
4877 /* need to allocate an entry */
4878 offset
= DRT_ALIGN_ADDRESS(offset
);
4879 index
= DRT_HASH(cmap
, offset
);
4881 /* scan from the index forwards looking for a vacant slot */
4882 for (i
= 0; i
< cmap
->scm_modulus
; i
++) {
4884 if (DRT_HASH_VACANT(cmap
, index
) || DRT_HASH_GET_COUNT(cmap
,index
) == 0) {
4885 cmap
->scm_buckets
++;
4886 if (index
< cmap
->scm_lastclean
)
4887 cmap
->scm_lastclean
= index
;
4888 DRT_HASH_SET_ADDRESS(cmap
, index
, offset
);
4889 DRT_HASH_SET_COUNT(cmap
, index
, 0);
4890 DRT_BITVECTOR_CLEAR(cmap
, index
);
4892 vfs_drt_trace(cmap
, DRT_DEBUG_INSERT
, (int)offset
, i
, 0, 0);
4893 return(KERN_SUCCESS
);
4895 cmap
->scm_iskips
+= i
;
4896 index
= DRT_HASH_NEXT(cmap
, index
);
4900 * We haven't found a vacant slot, so the map is full. If we're not
4901 * already recursed, try reallocating/compacting it.
4904 return(KERN_FAILURE
);
4905 kret
= vfs_drt_alloc_map(cmapp
);
4906 if (kret
== KERN_SUCCESS
) {
4907 /* now try to insert again */
4908 kret
= vfs_drt_get_index(cmapp
, offset
, indexp
, 1);
4914 * Implementation of set dirty/clean.
4916 * In the 'clean' case, not finding a map is OK.
4918 static kern_return_t
4919 vfs_drt_do_mark_pages(
4926 struct vfs_drt_clustermap
*cmap
, **cmapp
;
4928 int i
, index
, pgoff
, pgcount
, setcount
, ecount
;
4930 cmapp
= (struct vfs_drt_clustermap
**)private;
4933 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_START
, (int)offset
, (int)length
, dirty
, 0);
4935 if (setcountp
!= NULL
)
4938 /* allocate a cluster map if we don't already have one */
4940 /* no cluster map, nothing to clean */
4942 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 1, 0, 0, 0);
4943 return(KERN_SUCCESS
);
4945 kret
= vfs_drt_alloc_map(cmapp
);
4946 if (kret
!= KERN_SUCCESS
) {
4947 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 2, 0, 0, 0);
4954 * Iterate over the length of the region.
4956 while (length
> 0) {
4958 * Get the hashtable index for this offset.
4960 * XXX this will add blank entries if we are clearing a range
4961 * that hasn't been dirtied.
4963 kret
= vfs_drt_get_index(cmapp
, offset
, &index
, 0);
4964 cmap
= *cmapp
; /* may have changed! */
4965 /* this may be a partial-success return */
4966 if (kret
!= KERN_SUCCESS
) {
4967 if (setcountp
!= NULL
)
4968 *setcountp
= setcount
;
4969 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 3, (int)length
, 0, 0);
4975 * Work out how many pages we're modifying in this
4978 pgoff
= (offset
- DRT_ALIGN_ADDRESS(offset
)) / PAGE_SIZE
;
4979 pgcount
= min((length
/ PAGE_SIZE
), (DRT_BITVECTOR_PAGES
- pgoff
));
4982 * Iterate over pages, dirty/clearing as we go.
4984 ecount
= DRT_HASH_GET_COUNT(cmap
, index
);
4985 for (i
= 0; i
< pgcount
; i
++) {
4987 if (!DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
4988 DRT_HASH_SET_BIT(cmap
, index
, pgoff
+ i
);
4993 if (DRT_HASH_TEST_BIT(cmap
, index
, pgoff
+ i
)) {
4994 DRT_HASH_CLEAR_BIT(cmap
, index
, pgoff
+ i
);
5000 DRT_HASH_SET_COUNT(cmap
, index
, ecount
);
5002 offset
+= pgcount
* PAGE_SIZE
;
5003 length
-= pgcount
* PAGE_SIZE
;
5005 if (setcountp
!= NULL
)
5006 *setcountp
= setcount
;
5008 vfs_drt_trace(cmap
, DRT_DEBUG_MARK
| DBG_FUNC_END
, 0, setcount
, 0, 0);
5010 return(KERN_SUCCESS
);
5014 * Mark a set of pages as dirty/clean.
5016 * This is a public interface.
5019 * Pointer to storage suitable for holding a pointer. Note that
5020 * this must either be NULL or a value set by this function.
5023 * Current file size in bytes.
5026 * Offset of the first page to be marked as dirty, in bytes. Must be
5030 * Length of dirty region, in bytes. Must be a multiple of PAGE_SIZE.
5033 * Number of pages newly marked dirty by this call (optional).
5035 * Returns KERN_SUCCESS if all the pages were successfully marked.
5037 static kern_return_t
5038 vfs_drt_mark_pages(void **cmapp
, off_t offset
, u_int length
, int *setcountp
)
5040 /* XXX size unused, drop from interface */
5041 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, setcountp
, 1));
5045 static kern_return_t
5046 vfs_drt_unmark_pages(void **cmapp
, off_t offset
, u_int length
)
5048 return(vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0));
5053 * Get a cluster of dirty pages.
5055 * This is a public interface.
5058 * Pointer to storage managed by drt_mark_pages. Note that this must
5059 * be NULL or a value set by drt_mark_pages.
5062 * Returns the byte offset into the file of the first page in the cluster.
5065 * Returns the length in bytes of the cluster of dirty pages.
5067 * Returns success if a cluster was found. If KERN_FAILURE is returned, there
5068 * are no dirty pages meeting the minmum size criteria. Private storage will
5069 * be released if there are no more dirty pages left in the map
5072 static kern_return_t
5073 vfs_drt_get_cluster(void **cmapp
, off_t
*offsetp
, u_int
*lengthp
)
5075 struct vfs_drt_clustermap
*cmap
;
5078 int index
, i
, j
, fs
, ls
;
5081 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
5082 return(KERN_FAILURE
);
5085 /* walk the hashtable */
5086 for (offset
= 0, j
= 0; j
< cmap
->scm_modulus
; offset
+= (DRT_BITVECTOR_PAGES
* PAGE_SIZE
), j
++) {
5087 index
= DRT_HASH(cmap
, offset
);
5089 if (DRT_HASH_VACANT(cmap
, index
) || (DRT_HASH_GET_COUNT(cmap
, index
) == 0))
5092 /* scan the bitfield for a string of bits */
5095 for (i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
5096 if (DRT_HASH_TEST_BIT(cmap
, index
, i
)) {
5102 /* didn't find any bits set */
5103 panic("vfs_drt: entry summary count > 0 but no bits set in map");
5105 for (ls
= 0; i
< DRT_BITVECTOR_PAGES
; i
++, ls
++) {
5106 if (!DRT_HASH_TEST_BIT(cmap
, index
, i
))
5110 /* compute offset and length, mark pages clean */
5111 offset
= DRT_HASH_GET_ADDRESS(cmap
, index
) + (PAGE_SIZE
* fs
);
5112 length
= ls
* PAGE_SIZE
;
5113 vfs_drt_do_mark_pages(cmapp
, offset
, length
, NULL
, 0);
5114 cmap
->scm_lastclean
= index
;
5116 /* return successful */
5117 *offsetp
= (off_t
)offset
;
5120 vfs_drt_trace(cmap
, DRT_DEBUG_RETCLUSTER
, (int)offset
, (int)length
, 0, 0);
5121 return(KERN_SUCCESS
);
5124 * We didn't find anything... hashtable is empty
5125 * emit stats into trace buffer and
5128 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
5131 cmap
->scm_lastclean
,
5134 vfs_drt_free_map(cmap
);
5137 return(KERN_FAILURE
);
5141 static kern_return_t
5142 vfs_drt_control(void **cmapp
, int op_type
)
5144 struct vfs_drt_clustermap
*cmap
;
5147 if ((cmapp
== NULL
) || (*cmapp
== NULL
))
5148 return(KERN_FAILURE
);
5153 /* emit stats into trace buffer */
5154 vfs_drt_trace(cmap
, DRT_DEBUG_SCMDATA
,
5157 cmap
->scm_lastclean
,
5160 vfs_drt_free_map(cmap
);
5165 cmap
->scm_lastclean
= 0;
5168 return(KERN_SUCCESS
);
5174 * Emit a summary of the state of the clustermap into the trace buffer
5175 * along with some caller-provided data.
5179 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, int code
, int arg1
, int arg2
, int arg3
, int arg4
)
5181 KERNEL_DEBUG(code
, arg1
, arg2
, arg3
, arg4
, 0);
5185 vfs_drt_trace(__unused
struct vfs_drt_clustermap
*cmap
, __unused
int code
,
5186 __unused
int arg1
, __unused
int arg2
, __unused
int arg3
,
5194 * Perform basic sanity check on the hash entry summary count
5195 * vs. the actual bits set in the entry.
5198 vfs_drt_sanity(struct vfs_drt_clustermap
*cmap
)
5203 for (index
= 0; index
< cmap
->scm_modulus
; index
++) {
5204 if (DRT_HASH_VACANT(cmap
, index
))
5207 for (bits_on
= 0, i
= 0; i
< DRT_BITVECTOR_PAGES
; i
++) {
5208 if (DRT_HASH_TEST_BIT(cmap
, index
, i
))
5211 if (bits_on
!= DRT_HASH_GET_COUNT(cmap
, index
))
5212 panic("bits_on = %d, index = %d\n", bits_on
, index
);