]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/vfs/vfs_cluster.c
xnu-1228.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_cluster.c
index 69dd9891055fca593c31f1abf2988557aa64ef44..8d3657909e9a96051b082c955932698f8e136eab 100644 (file)
@@ -1,23 +1,29 @@
 /*
- * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
 /*
@@ -70,6 +76,7 @@
 #include <machine/machine_routines.h>
 
 #include <sys/ubc_internal.h>
+#include <vm/vnode_pager.h>
 
 #include <mach/mach_types.h>
 #include <mach/memory_object_types.h>
 
 #include <sys/kdebug.h>
 
-
-#define CL_READ      0x01
-#define CL_ASYNC     0x02
-#define CL_COMMIT    0x04
-#define CL_PAGEOUT   0x10
-#define CL_AGE       0x20
-#define CL_DUMP      0x40
-#define CL_NOZERO    0x80
-#define CL_PAGEIN    0x100
-#define CL_DEV_MEMORY 0x200
-#define CL_PRESERVE   0x400
-#define CL_THROTTLE   0x800
-#define CL_KEEPCACHED 0x1000
+#define CL_READ                0x01
+#define CL_ASYNC       0x02
+#define CL_COMMIT      0x04
+#define CL_PAGEOUT     0x10
+#define CL_AGE         0x20
+#define CL_NOZERO      0x40
+#define CL_PAGEIN      0x80
+#define CL_DEV_MEMORY  0x100
+#define CL_PRESERVE    0x200
+#define CL_THROTTLE    0x400
+#define CL_KEEPCACHED  0x800
+#define CL_DIRECT_IO   0x1000
+#define CL_PASSIVE     0x2000
 
 
 struct clios {
@@ -110,38 +117,84 @@ static lck_grp_attr_t   *cl_mtx_grp_attr;
 static lck_mtx_t       *cl_mtxp;
 
 
+#define        IO_UNKNOWN      0
+#define        IO_DIRECT       1
+#define IO_CONTIG      2
+#define IO_COPY                3
+
+#define        PUSH_DELAY      0x01
+#define PUSH_ALL       0x02
+#define        PUSH_SYNC       0x04
+
+
+static void cluster_EOT(buf_t cbp_head, buf_t cbp_tail, int zero_offset);
+static void cluster_wait_IO(buf_t cbp_head, int async);
+static void cluster_complete_transaction(buf_t *cbp_head, void *callback_arg, int *retval, int flags, int needwait);
+
+static int cluster_io_type(struct uio *uio, int *io_type, u_int32_t *io_length, u_int32_t min_length);
+
 static int cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int non_rounded_size,
-                     int flags, buf_t real_bp, struct clios *iostate);
-static int cluster_iodone(buf_t bp, void *dummy);
-static int cluster_rd_prefetch(vnode_t vp, off_t f_offset, u_int size, off_t filesize);
+                     int flags, buf_t real_bp, struct clios *iostate, int (*)(buf_t, void *), void *callback_arg);
+static int cluster_iodone(buf_t bp, void *callback_arg);
+static int cluster_ioerror(upl_t upl, int upl_offset, int abort_size, int error, int io_flags);
 static int cluster_hard_throttle_on(vnode_t vp);
 
-static int cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags);
-static int cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF,
-                          off_t headOff, off_t tailOff, int flags);
-static int cluster_nocopy_read(vnode_t vp, struct uio *uio, off_t filesize);
-static int cluster_nocopy_write(vnode_t vp, struct uio *uio, off_t newEOF);
-static int cluster_phys_read(vnode_t vp, struct uio *uio, off_t filesize);
-static int cluster_phys_write(vnode_t vp, struct uio *uio, off_t newEOF);
-static int cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, int xsize, int flags);
+static void cluster_syncup(vnode_t vp, off_t newEOF, int (*)(buf_t, void *), void *callback_arg);
+
+static void cluster_read_upl_release(upl_t upl, int start_pg, int last_pg, int flags);
+static int cluster_copy_ubc_data_internal(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty, int take_reference);
+
+static int cluster_read_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size,  off_t filesize, int flags,
+                            int (*)(buf_t, void *), void *callback_arg);
+static int cluster_read_direct(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
+                              int flags, int (*)(buf_t, void *), void *callback_arg);
+static int cluster_read_contig(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
+                              int (*)(buf_t, void *), void *callback_arg, int flags);
 
-static void    cluster_rd_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl_readahead *ra);
+static int cluster_write_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size, off_t oldEOF, off_t newEOF,
+                             off_t headOff, off_t tailOff, int flags, int (*)(buf_t, void *), void *callback_arg);
+static int cluster_write_direct(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF,
+                               int *write_type, u_int32_t *write_length, int flags, int (*)(buf_t, void *), void *callback_arg);
+static int cluster_write_contig(vnode_t vp, struct uio *uio, off_t newEOF,
+                               int *write_type, u_int32_t *write_length, int (*)(buf_t, void *), void *callback_arg, int bflag);
 
-static int     cluster_push_x(vnode_t vp, struct cl_extent *, off_t EOF, int flags);
-static void    cluster_push_EOF(vnode_t vp, off_t EOF);
+static int cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, u_int32_t xsize, int flags, int (*)(buf_t, void *), void *callback_arg);
 
-static int     cluster_try_push(struct cl_writebehind *, vnode_t vp, off_t EOF, int can_delay, int push_all);
+static int     cluster_read_prefetch(vnode_t vp, off_t f_offset, u_int size, off_t filesize, int (*callback)(buf_t, void *), void *callback_arg, int bflag);
+static void    cluster_read_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl_readahead *ra, int (*callback)(buf_t, void *), void *callback_arg, int bflag);
 
-static void    sparse_cluster_switch(struct cl_writebehind *, vnode_t vp, off_t EOF);
-static void    sparse_cluster_push(struct cl_writebehind *, vnode_t vp, off_t EOF, int push_all);
-static void    sparse_cluster_add(struct cl_writebehind *, vnode_t vp, struct cl_extent *, off_t EOF);
+static int     cluster_push_now(vnode_t vp, struct cl_extent *, off_t EOF, int flags, int (*)(buf_t, void *), void *callback_arg);
 
-static kern_return_t vfs_drt_mark_pages(void **cmapp, off_t offset, u_int length, int *setcountp);
+static int     cluster_try_push(struct cl_writebehind *, vnode_t vp, off_t EOF, int push_flag, int (*)(buf_t, void *), void *callback_arg);
+
+static void    sparse_cluster_switch(struct cl_writebehind *, vnode_t vp, off_t EOF, int (*)(buf_t, void *), void *callback_arg);
+static void    sparse_cluster_push(struct cl_writebehind *, vnode_t vp, off_t EOF, int push_flag, int (*)(buf_t, void *), void *callback_arg);
+static void    sparse_cluster_add(struct cl_writebehind *, vnode_t vp, struct cl_extent *, off_t EOF, int (*)(buf_t, void *), void *callback_arg);
+
+static kern_return_t vfs_drt_mark_pages(void **cmapp, off_t offset, u_int length, u_int *setcountp);
 static kern_return_t vfs_drt_get_cluster(void **cmapp, off_t *offsetp, u_int *lengthp);
 static kern_return_t vfs_drt_control(void **cmapp, int op_type);
 
 int    is_file_clean(vnode_t, off_t);
 
+/*
+ * limit the internal I/O size so that we
+ * can represent it in a 32 bit int
+ */
+#define MAX_IO_REQUEST_SIZE    (1024 * 1024 * 256)
+#define MAX_IO_CONTIG_SIZE     (1024 * 1024 * 8)
+#define MAX_VECTS      16
+
+/*
+ * note:  MAX_CLUSTER_SIZE CANNOT be larger than MAX_UPL_TRANSFER
+ */
+#define MAX_CLUSTER_SIZE       (MAX_UPL_TRANSFER)
+#define MAX_PREFETCH           (MAX_CLUSTER_SIZE * PAGE_SIZE * 2)
+#define MIN_DIRECT_WRITE_SIZE  (4 * PAGE_SIZE)
+
+
+int speculative_reads_disabled = 0;
+
 /*
  * throttle the number of async writes that
  * can be outstanding on a single vnode
@@ -160,14 +213,12 @@ cluster_init(void) {
         * allocate lock group attribute and group
         */
         cl_mtx_grp_attr = lck_grp_attr_alloc_init();
-       //lck_grp_attr_setstat(cl_mtx_grp_attr);
        cl_mtx_grp = lck_grp_alloc_init("cluster I/O", cl_mtx_grp_attr);
                
        /*
         * allocate the lock attribute
         */
        cl_mtx_attr = lck_attr_alloc_init();
-       //lck_attr_setdebug(clf_mtx_attr);
 
        /*
         * allocate and initialize mutex's used to protect updates and waits
@@ -183,6 +234,9 @@ cluster_init(void) {
 
 #define CLW_ALLOCATE           0x01
 #define CLW_RETURNLOCKED       0x02
+#define CLW_IONOCACHE          0x04
+#define CLW_IOPASSIVE  0x08
+
 /*
  * if the read ahead context doesn't yet exist,
  * allocate and initialize it...
@@ -222,7 +276,7 @@ cluster_get_rap(vnode_t vp)
                else {
                        lck_mtx_destroy(&rap->cl_lockr, cl_mtx_grp);
                        FREE_ZONE((void *)rap, sizeof *rap, M_CLRDAHEAD);
-                               rap = ubc->cl_rahead;
+                       rap = ubc->cl_rahead;
                }
                vnode_unlock(vp);
        }
@@ -271,7 +325,7 @@ cluster_get_wbp(vnode_t vp, int flags)
                else {
                        lck_mtx_destroy(&wbp->cl_lockw, cl_mtx_grp);
                        FREE_ZONE((void *)wbp, sizeof *wbp, M_CLWRBEHIND);
-                               wbp = ubc->cl_wbehind;
+                       wbp = ubc->cl_wbehind;
                }
                vnode_unlock(vp);
        }
@@ -282,6 +336,24 @@ cluster_get_wbp(vnode_t vp, int flags)
 }
 
 
+static void
+cluster_syncup(vnode_t vp, off_t newEOF, int (*callback)(buf_t, void *), void *callback_arg)
+{
+       struct cl_writebehind *wbp;
+
+       if ((wbp = cluster_get_wbp(vp, 0)) != NULL) {
+         
+               if (wbp->cl_number) {
+                       lck_mtx_lock(&wbp->cl_lockw);
+
+                       cluster_try_push(wbp, vp, newEOF, PUSH_ALL | PUSH_SYNC, callback, callback_arg);
+
+                       lck_mtx_unlock(&wbp->cl_lockw);
+               }
+       }
+}
+
+
 static int 
 cluster_hard_throttle_on(vnode_t vp)
 {
@@ -304,7 +376,47 @@ cluster_hard_throttle_on(vnode_t vp)
 
 
 static int
-cluster_iodone(buf_t bp, __unused void *dummy)
+cluster_ioerror(upl_t upl, int upl_offset, int abort_size, int error, int io_flags)
+{
+        int upl_abort_code = 0;
+       int page_in  = 0;
+       int page_out = 0;
+
+       if (io_flags & B_PHYS)
+               /*
+                * direct write of any flavor, or a direct read that wasn't aligned
+                */
+               ubc_upl_commit_range(upl, upl_offset, abort_size, UPL_COMMIT_FREE_ON_EMPTY);
+       else {
+               if (io_flags & B_PAGEIO) {
+                       if (io_flags & B_READ)
+                               page_in  = 1;
+                       else
+                               page_out = 1;
+               }
+               if (io_flags & B_CACHE)
+                       /*
+                        * leave pages in the cache unchanged on error
+                        */
+                       upl_abort_code = UPL_ABORT_FREE_ON_EMPTY;
+               else if (page_out && (error != ENXIO))
+                       /*
+                        * transient error... leave pages unchanged
+                        */
+                       upl_abort_code = UPL_ABORT_FREE_ON_EMPTY;
+               else if (page_in)
+                       upl_abort_code = UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR;
+               else
+                       upl_abort_code = UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_DUMP_PAGES;
+
+               ubc_upl_abort_range(upl, upl_offset, abort_size, upl_abort_code);
+       }
+       return (upl_abort_code);
+}
+
+
+static int
+cluster_iodone(buf_t bp, void *callback_arg)
 {
         int    b_flags;
         int    error;
@@ -312,14 +424,17 @@ cluster_iodone(buf_t bp, __unused void *dummy)
        int     total_resid;
        int     upl_offset;
        int     zero_offset;
+       int     pg_offset = 0;
+        int    commit_size = 0;
+        int    upl_flags = 0;
+       int     transaction_size = 0;
        upl_t   upl;
        buf_t   cbp;
        buf_t   cbp_head;
        buf_t   cbp_next;
        buf_t   real_bp;
        struct  clios *iostate;
-       int     commit_size;
-       int     pg_offset;
+       boolean_t       transaction_complete = FALSE;
 
        cbp_head = (buf_t)(bp->b_trans_head);
 
@@ -336,8 +451,16 @@ cluster_iodone(buf_t bp, __unused void *dummy)
                        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
                                     (int)cbp_head, (int)cbp, cbp->b_bcount, cbp->b_flags, 0);
 
-                       return 0;
+                       return 0;
                }
+               if (cbp->b_flags & B_EOT)
+                       transaction_complete = TRUE;
+       }
+       if (transaction_complete == FALSE) {
+               KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
+                            (int)cbp_head, 0, 0, 0, 0);
+
+               return 0;
        }
        error       = 0;
        total_size  = 0;
@@ -363,13 +486,37 @@ cluster_iodone(buf_t bp, __unused void *dummy)
 
                cbp_next = cbp->b_trans_next;
 
-               free_io_buf(cbp);
+               if (cbp_next == NULL)
+                       /*
+                        * compute the overall size of the transaction
+                        * in case we created one that has 'holes' in it
+                        * 'total_size' represents the amount of I/O we
+                        * did, not the span of the transaction w/r to the UPL
+                        */
+                       transaction_size = cbp->b_uploffset + cbp->b_bcount - upl_offset;
+
+               if (cbp != cbp_head)
+                       free_io_buf(cbp);
 
                cbp = cbp_next;
        }
+       if (error == 0 && total_resid)
+               error = EIO;
+
+       if (error == 0) {
+               int     (*cliodone_func)(buf_t, void *) = (int (*)(buf_t, void *))(cbp_head->b_cliodone);
+
+               if (cliodone_func != NULL) {
+                       cbp_head->b_bcount = transaction_size;
+
+                       error = (*cliodone_func)(cbp_head, callback_arg);
+               }
+       }
        if (zero_offset)
                cluster_zero(upl, zero_offset, PAGE_SIZE - (zero_offset & PAGE_MASK), real_bp);
 
+        free_io_buf(cbp_head);
+
        if (iostate) {
                int need_wakeup = 0;
 
@@ -377,7 +524,7 @@ cluster_iodone(buf_t bp, __unused void *dummy)
                 * someone has issued multiple I/Os asynchrounsly
                 * and is waiting for them to complete (streaming)
                 */
-               lck_mtx_lock(cl_mtxp);
+               lck_mtx_lock_spin(cl_mtxp);
 
                if (error && iostate->io_error == 0)
                        iostate->io_error = error;
@@ -397,69 +544,37 @@ cluster_iodone(buf_t bp, __unused void *dummy)
                if (need_wakeup)
                        wakeup((caddr_t)&iostate->io_wanted);
        }
-       if ((b_flags & B_NEED_IODONE) && real_bp) {
-               if (error) {
-                       real_bp->b_flags |= B_ERROR;
-                       real_bp->b_error = error;
-               }
-               real_bp->b_resid = total_resid;
-
-               buf_biodone(real_bp);
-       }
-       if (error == 0 && total_resid)
-               error = EIO;
 
        if (b_flags & B_COMMIT_UPL) {
-               pg_offset   = upl_offset & PAGE_MASK;
-               commit_size = (pg_offset + total_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
-
-               if (error || (b_flags & B_NOCACHE)) {
-                       int upl_abort_code;
-                       int page_in  = 0;
-                       int page_out = 0;
-
-                       if (b_flags & B_PAGEIO) {
-                               if (b_flags & B_READ)
-                                       page_in  = 1;
-                               else
-                                       page_out = 1;
-                       }
-                       if (b_flags & B_CACHE)          /* leave pages in the cache unchanged on error */
-                               upl_abort_code = UPL_ABORT_FREE_ON_EMPTY;
-                       else if (page_out && (error != ENXIO)) /* transient error */
-                               upl_abort_code = UPL_ABORT_FREE_ON_EMPTY;
-                       else if (page_in)
-                               upl_abort_code = UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR;
-                       else
-                               upl_abort_code = UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_DUMP_PAGES;
-
-                       ubc_upl_abort_range(upl, upl_offset - pg_offset, commit_size,
-                                                   upl_abort_code);
 
-                       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
-                                    (int)upl, upl_offset - pg_offset, commit_size,
-                                    0x80000000|upl_abort_code, 0);
+               pg_offset   = upl_offset & PAGE_MASK;
+               commit_size = (pg_offset + transaction_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
 
-               } else {
-                       int upl_commit_flags = UPL_COMMIT_FREE_ON_EMPTY;
+               if (error)
+                       upl_flags = cluster_ioerror(upl, upl_offset - pg_offset, commit_size, error, b_flags);
+               else {
+                       upl_flags = UPL_COMMIT_FREE_ON_EMPTY;
 
                        if ((b_flags & B_PHYS) && (b_flags & B_READ)) 
-                               upl_commit_flags |= UPL_COMMIT_SET_DIRTY;
+                               upl_flags |= UPL_COMMIT_SET_DIRTY;
 
                        if (b_flags & B_AGE)
-                               upl_commit_flags |= UPL_COMMIT_INACTIVATE;
+                               upl_flags |= UPL_COMMIT_INACTIVATE;
 
-                       ubc_upl_commit_range(upl, upl_offset - pg_offset, commit_size,
-                                       upl_commit_flags);
-
-                       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
-                                    (int)upl, upl_offset - pg_offset, commit_size,
-                                    upl_commit_flags, 0);
+                       ubc_upl_commit_range(upl, upl_offset - pg_offset, commit_size, upl_flags);
                }
-       } else {
-               KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
-                            (int)upl, upl_offset, 0, error, 0);
        }
+       if ((b_flags & B_NEED_IODONE) && real_bp) {
+               if (error) {
+                       real_bp->b_flags |= B_ERROR;
+                       real_bp->b_error = error;
+               }
+               real_bp->b_resid = total_resid;
+
+               buf_biodone(real_bp);
+       }
+       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 20)) | DBG_FUNC_END,
+                    (int)upl, upl_offset - pg_offset, commit_size, (error << 24) | upl_flags, 0);
 
        return (error);
 }
@@ -468,31 +583,37 @@ cluster_iodone(buf_t bp, __unused void *dummy)
 void
 cluster_zero(upl_t upl, vm_offset_t upl_offset, int size, buf_t bp)
 {
-       upl_page_info_t *pl;
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 23)) | DBG_FUNC_START,
                     upl_offset, size, (int)bp, 0, 0);
 
        if (bp == NULL || bp->b_datap == 0) {
+               upl_page_info_t *pl;
+               addr64_t        zero_addr;
 
                pl = ubc_upl_pageinfo(upl);
 
-               while (size) {
-                       int           page_offset;
-                       int           page_index;
-                       addr64_t      zero_addr;
-                       int           zero_cnt;
+               if (upl_device_page(pl) == TRUE) {
+                       zero_addr = ((addr64_t)upl_phys_page(pl, 0) << 12) + upl_offset;
+
+                       bzero_phys_nc(zero_addr, size);
+               } else {
+                       while (size) {
+                               int     page_offset;
+                               int     page_index;
+                               int     zero_cnt;
 
-                       page_index  = upl_offset / PAGE_SIZE;
-                       page_offset = upl_offset & PAGE_MASK;
+                               page_index  = upl_offset / PAGE_SIZE;
+                               page_offset = upl_offset & PAGE_MASK;
 
-                       zero_addr = ((addr64_t)upl_phys_page(pl, page_index) << 12) + page_offset;
-                       zero_cnt  = min(PAGE_SIZE - page_offset, size);
+                               zero_addr = ((addr64_t)upl_phys_page(pl, page_index) << 12) + page_offset;
+                               zero_cnt  = min(PAGE_SIZE - page_offset, size);
 
-                       bzero_phys(zero_addr, zero_cnt);
+                               bzero_phys(zero_addr, zero_cnt);
 
-                       size       -= zero_cnt;
-                       upl_offset += zero_cnt;
+                               size       -= zero_cnt;
+                               upl_offset += zero_cnt;
+                       }
                }
        } else
                bzero((caddr_t)((vm_offset_t)bp->b_datap + upl_offset), size);
@@ -502,9 +623,75 @@ cluster_zero(upl_t upl, vm_offset_t upl_offset, int size, buf_t bp)
 }
 
 
+static void
+cluster_EOT(buf_t cbp_head, buf_t cbp_tail, int zero_offset)
+{
+        cbp_head->b_validend = zero_offset;
+        cbp_tail->b_flags |= B_EOT;
+}
+
+static void
+cluster_wait_IO(buf_t cbp_head, int async)
+{
+        buf_t  cbp;
+
+       if (async) {
+               /*
+                * async callback completion will not normally
+                * generate a wakeup upon I/O completion...
+                * by setting BL_WANTED, we will force a wakeup
+                * to occur as any outstanding I/Os complete... 
+                * I/Os already completed will have BL_CALLDONE already
+                * set and we won't block in buf_biowait_callback..
+                * note that we're actually waiting for the bp to have
+                * completed the callback function... only then
+                * can we safely take back ownership of the bp
+                * need the main buf mutex in order to safely
+                * update b_lflags
+                */
+               buf_list_lock();
+
+               for (cbp = cbp_head; cbp; cbp = cbp->b_trans_next)
+                     cbp->b_lflags |= BL_WANTED;
+
+               buf_list_unlock();
+       }
+       for (cbp = cbp_head; cbp; cbp = cbp->b_trans_next) {
+               if (async)
+                       buf_biowait_callback(cbp);
+               else
+                       buf_biowait(cbp);
+       }
+}
+
+static void
+cluster_complete_transaction(buf_t *cbp_head, void *callback_arg, int *retval, int flags, int needwait)
+{
+        buf_t  cbp;
+       int     error;
+
+       /*
+        * cluster_complete_transaction will
+        * only be called if we've issued a complete chain in synchronous mode
+        * or, we've already done a cluster_wait_IO on an incomplete chain
+        */
+        if (needwait) {
+               for (cbp = *cbp_head; cbp; cbp = cbp->b_trans_next)
+                       buf_biowait(cbp);
+       }
+       error = cluster_iodone(*cbp_head, callback_arg);
+
+       if ( !(flags & CL_ASYNC) && error && *retval == 0) {
+               if (((flags & (CL_PAGEOUT | CL_KEEPCACHED)) != CL_PAGEOUT) || (error != ENXIO))
+                       *retval = error;
+       }
+       *cbp_head = (buf_t)NULL;
+}
+
+
 static int
 cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int non_rounded_size,
-          int flags, buf_t real_bp, struct clios *iostate)
+          int flags, buf_t real_bp, struct clios *iostate, int (*callback)(buf_t, void *), void *callback_arg)
 {
        buf_t   cbp;
        u_int   size;
@@ -516,6 +703,7 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
        buf_t   cbp_head = NULL;
        buf_t   cbp_tail = NULL;
        int     trans_count = 0;
+       int     max_trans_count;
        u_int   pg_count;
        int     pg_offset;
        u_int   max_iosize;
@@ -524,10 +712,27 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
        int     zero_offset = 0;
        int     async_throttle = 0;
        mount_t mp;
+       vm_offset_t upl_end_offset;
+       boolean_t   need_EOT = FALSE;
+
+       /*
+        * we currently don't support buffers larger than a page
+        */
+       if (real_bp && non_rounded_size > PAGE_SIZE)
+               panic("%s(): Called with real buffer of size %d bytes which "
+                               "is greater than the maximum allowed size of "
+                               "%d bytes (the system PAGE_SIZE).\n",
+                               __FUNCTION__, non_rounded_size, PAGE_SIZE);
 
        mp = vp->v_mount;
 
-       if (mp->mnt_devblocksize > 1) {
+       /*
+        * we don't want to do any funny rounding of the size for IO requests
+        * coming through the DIRECT or CONTIGUOUS paths...  those pages don't
+        * belong to us... we can't extend (nor do we need to) the I/O to fill
+        * out a page
+        */
+       if (mp->mnt_devblocksize > 1 && !(flags & (CL_DEV_MEMORY | CL_DIRECT_IO))) {
                /*
                 * round the requested size up so that this I/O ends on a
                 * page boundary in case this is a 'write'... if the filesystem
@@ -551,17 +756,26 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                 */
                size = non_rounded_size;
        }
-       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_START,
-                    (int)f_offset, size, upl_offset, flags, 0);
+       upl_end_offset = upl_offset + size;
+
+       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_START, (int)f_offset, size, upl_offset, flags, 0);
+
+       /*
+        * Set the maximum transaction size to the maximum desired number of
+        * buffers.
+        */
+       max_trans_count = 8;
+       if (flags & CL_DEV_MEMORY)
+               max_trans_count = 16;
 
        if (flags & CL_READ) {
-               io_flags = (B_READ);
+               io_flags = B_READ;
                bmap_flags = VNODE_READ;
 
                max_iosize  = mp->mnt_maxreadcnt;
                max_vectors = mp->mnt_segreadcnt;
        } else {
-               io_flags = 0;
+               io_flags = B_WRITE;
                bmap_flags = VNODE_WRITE;
 
                max_iosize  = mp->mnt_maxwritecnt;
@@ -575,18 +789,37 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
         */
        max_iosize  &= ~PAGE_MASK;
 
+       /*
+        * Ensure the maximum iosize is sensible.
+        */
+       if (!max_iosize)
+               max_iosize = PAGE_SIZE;
+
        if (flags & CL_THROTTLE) {
                if ( !(flags & CL_PAGEOUT) && cluster_hard_throttle_on(vp)) {
                        if (max_iosize > HARD_THROTTLE_MAXSIZE)
                                max_iosize = HARD_THROTTLE_MAXSIZE;
                        async_throttle = HARD_THROTTLE_MAXCNT;
-               } else
-                       async_throttle = VNODE_ASYNC_THROTTLE;
+               } else {
+                       if ( (flags & CL_DEV_MEMORY) )
+                               async_throttle = VNODE_ASYNC_THROTTLE;
+                       else {
+                               u_int max_cluster;
+                               
+                               if (max_iosize > (MAX_CLUSTER_SIZE * PAGE_SIZE))
+                                       max_cluster = (MAX_CLUSTER_SIZE * PAGE_SIZE);
+                               else
+                                       max_cluster = max_iosize;
+
+                               if (size < max_cluster)
+                                       max_cluster = size;
+
+                               async_throttle = min(VNODE_ASYNC_THROTTLE, (MAX_PREFETCH / max_cluster) - 1);
+                       }
+               }
        }
        if (flags & CL_AGE)
                io_flags |= B_AGE;
-       if (flags & CL_DUMP)
-               io_flags |= B_NOCACHE;
        if (flags & (CL_PAGEIN | CL_PAGEOUT))
                io_flags |= B_PAGEIO;
        if (flags & CL_COMMIT)
@@ -595,6 +828,10 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                io_flags |= B_PHYS;
        if (flags & CL_KEEPCACHED)
                io_flags |= B_CACHE;
+       if (flags & CL_PASSIVE)
+               io_flags |= B_PASSIVE;
+       if (vp->v_flag & VSYSTEM)
+               io_flags |= B_META;
 
        if ((flags & CL_READ) && ((upl_offset + non_rounded_size) & PAGE_MASK) && (!(flags & CL_NOZERO))) {
                /*
@@ -607,23 +844,28 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                zero_offset = upl_offset + non_rounded_size;
        }
        while (size) {
-               int     pg_resid;
                daddr64_t blkno;
                daddr64_t lblkno;
+               u_int   io_size_wanted;
 
                if (size > max_iosize)
                        io_size = max_iosize;
                else
                        io_size = size;
+
+               io_size_wanted = io_size;
                
-               if ((error = VNOP_BLOCKMAP(vp, f_offset, io_size, &blkno, (size_t *)&io_size, NULL, bmap_flags, NULL))) {
+               if ((error = VNOP_BLOCKMAP(vp, f_offset, io_size, &blkno, (size_t *)&io_size, NULL, bmap_flags, NULL)))
                        break;
-               }
+
+               if (io_size > io_size_wanted)
+                       io_size = io_size_wanted;
+
                if (real_bp && (real_bp->b_blkno == real_bp->b_lblkno))
                        real_bp->b_blkno = blkno;
 
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 24)) | DBG_FUNC_NONE,
-                            (int)f_offset, (int)blkno, io_size, zero_offset, 0);
+                            (int)f_offset, (int)(blkno>>32), (int)blkno, io_size, 0);
 
                if (io_size == 0) {
                        /*
@@ -637,7 +879,8 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                        break;
                }
                if ( !(flags & CL_READ) && blkno == -1) {
-                       off_t e_offset;
+                       off_t   e_offset;
+                       int     pageout_flags;
 
                        /*
                         * we're writing into a 'hole'
@@ -651,17 +894,6 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                                error = EINVAL;
                                break;
                        }
-                       if ( !(flags & CL_COMMIT)) {
-                               /*
-                                * currently writes always request the commit to happen
-                                * as part of the io completion... however, if the CL_COMMIT
-                                * flag isn't specified, than we can't issue the abort_range
-                                * since the call site is going to abort or commit the same upl..
-                                * in this case we can only return an error
-                                */
-                               error = EINVAL;
-                               break;
-                       }
                        /*
                         * we can get here if the cluster code happens to 
                         * pick up a page that was dirtied via mmap vs
@@ -680,15 +912,84 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                         * giving up entirely and dumping it (the pageout
                         * path will insure that the zero extent accounting
                         * has been taken care of before we get back into cluster_io)
+                        *
+                        * go direct to vnode_pageout so that we don't have to
+                        * unbusy the page from the UPL... we used to do this
+                        * so that we could call ubc_sync_range, but that results
+                        * in a potential deadlock if someone else races us to acquire
+                        * that page and wins and in addition needs one of the pages
+                        * we're continuing to hold in the UPL
                         */
-                       ubc_upl_abort_range(upl, trunc_page(upl_offset), PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
+                       pageout_flags = UPL_MSYNC | UPL_VNODE_PAGER | UPL_NESTED_PAGEOUT;
 
-                       e_offset = round_page_64(f_offset + 1);
+                       if ( !(flags & CL_ASYNC))
+                               pageout_flags |= UPL_IOSYNC;
+                       if ( !(flags & CL_COMMIT))
+                               pageout_flags |= UPL_NOCOMMIT;
+
+                       if (cbp_head) {
+                               buf_t last_cbp;
+
+                               /*
+                                * first we have to wait for the the current outstanding I/Os
+                                * to complete... EOT hasn't been set yet on this transaction
+                                * so the pages won't be released just because all of the current
+                                * I/O linked to this transaction has completed...
+                                */
+                               cluster_wait_IO(cbp_head, (flags & CL_ASYNC));
+
+                               /*
+                                * we've got a transcation that
+                                * includes the page we're about to push out through vnode_pageout...
+                                * find the last bp in the list which will be the one that
+                                * includes the head of this page and round it's iosize down
+                                * to a page boundary...
+                                */
+                                for (last_cbp = cbp = cbp_head; cbp->b_trans_next; cbp = cbp->b_trans_next)
+                                       last_cbp = cbp;
+
+                               cbp->b_bcount &= ~PAGE_MASK;
+
+                               if (cbp->b_bcount == 0) {
+                                       /*
+                                        * this buf no longer has any I/O associated with it
+                                        */
+                                       free_io_buf(cbp);
+
+                                       if (cbp == cbp_head) {
+                                               /*
+                                                * the buf we just freed was the only buf in
+                                                * this transaction... so there's no I/O to do
+                                                */
+                                               cbp_head = NULL;
+                                       } else {
+                                               /*
+                                                * remove the buf we just freed from
+                                                * the transaction list
+                                                */
+                                               last_cbp->b_trans_next = NULL;
+                                               cbp_tail = last_cbp;
+                                       }
+                               }
+                               if (cbp_head) {
+                                       /*
+                                        * there was more to the current transaction
+                                        * than just the page we are pushing out via vnode_pageout...
+                                        * mark it as finished and complete it... we've already
+                                        * waited for the I/Os to complete above in the call to cluster_wait_IO
+                                        */
+                                       cluster_EOT(cbp_head, cbp_tail, 0);
+
+                                       cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 0);
 
-                       if (ubc_sync_range(vp, f_offset, e_offset, UBC_PUSHDIRTY) == 0) {
+                                       trans_count = 0;
+                               }
+                       }
+                       if (vnode_pageout(vp, upl, trunc_page(upl_offset), trunc_page_64(f_offset), PAGE_SIZE, pageout_flags, NULL) != PAGER_SUCCESS) {
                                error = EINVAL;
                                break;
                        }
+                       e_offset = round_page_64(f_offset + 1);
                        io_size = e_offset - f_offset;
 
                        f_offset   += io_size;
@@ -726,13 +1027,6 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                pg_offset = upl_offset & PAGE_MASK;
 
                if (flags & CL_DEV_MEMORY) {
-                       /*
-                        * currently, can't deal with reading 'holes' in file
-                        */
-                       if (blkno == -1) {
-                               error = EINVAL;
-                               break;
-                       }
                        /*
                         * treat physical requests as one 'giant' page
                         */
@@ -741,14 +1035,16 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                        pg_count  = (io_size + pg_offset + (PAGE_SIZE - 1)) / PAGE_SIZE;
 
                if ((flags & CL_READ) && blkno == -1) {
+                       vm_offset_t  commit_offset;
                        int bytes_to_zero;
+                       int complete_transaction_now = 0;
 
                        /*
                         * if we're reading and blkno == -1, then we've got a
                         * 'hole' in the file that we need to deal with by zeroing
                         * out the affected area in the upl
                         */
-                       if (zero_offset && io_size == size) {
+                       if (io_size >= (u_int)non_rounded_size) {
                                /*
                                 * if this upl contains the EOF and it is not a multiple of PAGE_SIZE
                                 * than 'zero_offset' will be non-zero
@@ -758,56 +1054,73 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                                 * last page in this upl... we need to zero both the hole and the tail
                                 * of the page beyond the EOF, since the delayed zero-fill won't kick in 
                                 */
-                               bytes_to_zero = (((upl_offset + io_size) + (PAGE_SIZE - 1)) & ~PAGE_MASK) - upl_offset;
+                               bytes_to_zero = non_rounded_size;
+                               if (!(flags & CL_NOZERO))
+                                       bytes_to_zero = (((upl_offset + io_size) + (PAGE_SIZE - 1)) & ~PAGE_MASK) - upl_offset;
 
                                zero_offset = 0;
                        } else
                                bytes_to_zero = io_size;
 
-                       cluster_zero(upl, upl_offset, bytes_to_zero, real_bp);
+                       pg_count = 0;
+
+                       cluster_zero(upl, upl_offset, bytes_to_zero, real_bp);
                          
-                       if (cbp_head)
+                       if (cbp_head) {
+                               int     pg_resid;
+
                                /*
                                 * if there is a current I/O chain pending
                                 * then the first page of the group we just zero'd
                                 * will be handled by the I/O completion if the zero
                                 * fill started in the middle of the page
                                 */
-                               pg_count = (io_size - pg_offset) / PAGE_SIZE;
-                       else {
-                               /*
-                                * no pending I/O to pick up that first page
-                                * so, we have to make sure it gets committed
-                                * here.
-                                * set the pg_offset to 0 so that the upl_commit_range
-                                * starts with this page
-                                */
-                               pg_count = (io_size + pg_offset) / PAGE_SIZE;
-                               pg_offset = 0;
-                       }
-                       if (io_size == size && ((upl_offset + io_size) & PAGE_MASK))
+                               commit_offset = (upl_offset + (PAGE_SIZE - 1)) & ~PAGE_MASK;
+
+                               pg_resid = commit_offset - upl_offset;
+                                       
+                               if (bytes_to_zero >= pg_resid) {
+                                       /*
+                                        * the last page of the current I/O 
+                                        * has been completed...
+                                        * compute the number of fully zero'd 
+                                        * pages that are beyond it
+                                        * plus the last page if its partial
+                                        * and we have no more I/O to issue...
+                                        * otherwise a partial page is left
+                                        * to begin the next I/O
+                                        */
+                                       if ((int)io_size >= non_rounded_size)
+                                               pg_count = (bytes_to_zero - pg_resid + (PAGE_SIZE - 1)) / PAGE_SIZE;
+                                       else
+                                               pg_count = (bytes_to_zero - pg_resid) / PAGE_SIZE;
+                                       
+                                       complete_transaction_now = 1;
+                               }
+                       } else {
                                /*
-                                * if we're done with the request for this UPL
-                                * then we have to make sure to commit the last page
-                                * even if we only partially zero-filled it
+                                * no pending I/O to deal with
+                                * so, commit all of the fully zero'd pages
+                                * plus the last page if its partial
+                                * and we have no more I/O to issue...
+                                * otherwise a partial page is left
+                                * to begin the next I/O
                                 */
-                               pg_count++;
-
-                       if (pg_count) {
-                               if (pg_offset)
-                                       pg_resid = PAGE_SIZE - pg_offset;
+                               if ((int)io_size >= non_rounded_size)
+                                       pg_count = (pg_offset + bytes_to_zero + (PAGE_SIZE - 1)) / PAGE_SIZE;
                                else
-                                       pg_resid = 0;
+                                       pg_count = (pg_offset + bytes_to_zero) / PAGE_SIZE;
 
-                               if (flags & CL_COMMIT)
-                                       ubc_upl_commit_range(upl,
-                                                       (upl_offset + pg_resid) & ~PAGE_MASK, 
-                                                       pg_count * PAGE_SIZE,
-                                                       UPL_COMMIT_CLEAR_DIRTY | UPL_COMMIT_FREE_ON_EMPTY);
+                               commit_offset = upl_offset & ~PAGE_MASK;
+                       }
+                       if ( (flags & CL_COMMIT) && pg_count) {
+                               ubc_upl_commit_range(upl, commit_offset, pg_count * PAGE_SIZE,
+                                                    UPL_COMMIT_CLEAR_DIRTY | UPL_COMMIT_FREE_ON_EMPTY);
                        }
                        upl_offset += io_size;
                        f_offset   += io_size;
                        size       -= io_size;
+
                        /*
                         * keep track of how much of the original request
                         * that we've actually completed... non_rounded_size
@@ -825,10 +1138,16 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                                 */
                                size = 0;
                        }
-                       if (cbp_head && pg_count) 
-                               goto start_io;
-                       continue;
+                       if (cbp_head && (complete_transaction_now || size == 0))  {
+                               cluster_wait_IO(cbp_head, (flags & CL_ASYNC));
+
+                               cluster_EOT(cbp_head, cbp_tail, size == 0 ? zero_offset : 0);
 
+                               cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 0);
+
+                               trans_count = 0;
+                       }
+                       continue;
                }
                if (pg_count > max_vectors) {
                        if (((pg_count - max_vectors) * PAGE_SIZE) > io_size) {
@@ -839,6 +1158,35 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                                pg_count = max_vectors;
                        }
                }
+               /*
+                * If the transaction is going to reach the maximum number of
+                * desired elements, truncate the i/o to the nearest page so
+                * that the actual i/o is initiated after this buffer is
+                * created and added to the i/o chain.
+                *
+                * I/O directed to physically contiguous memory 
+                * doesn't have a requirement to make sure we 'fill' a page
+                */
+               if ( !(flags & CL_DEV_MEMORY) && trans_count >= max_trans_count &&
+                               ((upl_offset + io_size) & PAGE_MASK)) {
+                       vm_offset_t aligned_ofs;
+
+                       aligned_ofs = (upl_offset + io_size) & ~PAGE_MASK;
+                       /*
+                        * If the io_size does not actually finish off even a
+                        * single page we have to keep adding buffers to the
+                        * transaction despite having reached the desired limit.
+                        *
+                        * Eventually we get here with the page being finished
+                        * off (and exceeded) and then we truncate the size of
+                        * this i/o request so that it is page aligned so that
+                        * we can finally issue the i/o on the transaction.
+                        */
+                       if (aligned_ofs > upl_offset) {
+                               io_size = aligned_ofs - upl_offset;
+                               pg_count--;
+                       }
+               }
 
                if ( !(mp->mnt_kern_flag & MNTK_VIRTUALDEV))
                        /*
@@ -867,9 +1215,10 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                        }
                }
                if (flags & CL_ASYNC) {
-                       if (buf_setcallback(cbp, (void *)cluster_iodone, NULL))
+                       if (buf_setcallback(cbp, (void *)cluster_iodone, callback_arg))
                                panic("buf_setcallback failed\n");
                }
+               cbp->b_cliodone = (void *)callback;
                cbp->b_flags |= io_flags;
 
                cbp->b_lblkno = lblkno;
@@ -903,8 +1252,14 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                } else {
                        cbp_head = cbp;
                        cbp_tail = cbp;
+
+                       if ( (cbp_head->b_real_bp = real_bp) ) {
+                               cbp_head->b_flags |= B_NEED_IODONE;
+                               real_bp = (buf_t)NULL;
+                       }
                }
-               (buf_t)(cbp->b_trans_head) = cbp_head;
+               *(buf_t *)(&cbp->b_trans_head) = cbp_head;
+
                trans_count++;
 
                upl_offset += io_size;
@@ -927,86 +1282,77 @@ cluster_io(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset, int no
                         */
                        size = 0;
                }
-               if ( (!(upl_offset & PAGE_MASK) && !(flags & CL_DEV_MEMORY) && ((flags & CL_ASYNC) || trans_count > 8)) || size == 0) {
+               if (size == 0) {
+                       /*
+                        * we have no more I/O to issue, so go
+                        * finish the final transaction
+                        */
+                       need_EOT = TRUE;
+               } else if ( ((flags & CL_DEV_MEMORY) || (upl_offset & PAGE_MASK) == 0) &&
+                           ((flags & CL_ASYNC) || trans_count > max_trans_count) ) {
                        /*
-                        * if we have no more I/O to issue or
+                        * I/O directed to physically contiguous memory...
+                        * which doesn't have a requirement to make sure we 'fill' a page
+                        * or... 
                         * the current I/O we've prepared fully
                         * completes the last page in this request
-                        * and it's either an ASYNC request or 
+                        * and ...
+                        * it's either an ASYNC request or 
                         * we've already accumulated more than 8 I/O's into
-                        * this transaction and it's not an I/O directed to 
-                        * special DEVICE memory
-                        * then go ahead and issue the I/O
+                        * this transaction so mark it as complete so that
+                        * it can finish asynchronously or via the cluster_complete_transaction
+                        * below if the request is synchronous
                         */
-start_io:              
-                       if (real_bp) {
-                               cbp_head->b_flags |= B_NEED_IODONE;
-                               cbp_head->b_real_bp = real_bp;
-                       } else
-                               cbp_head->b_real_bp = (buf_t)NULL;
-
-                       if (size == 0) {
-                               /*
-                                * we're about to issue the last I/O for this upl
-                                * if this was a read to the eof and the eof doesn't
-                                * finish on a page boundary, than we need to zero-fill
-                                * the rest of the page....
-                                */
-                               cbp_head->b_validend = zero_offset;
-                       } else
-                               cbp_head->b_validend = 0;
-                         
-                       if (flags & CL_THROTTLE)
-                               (void)vnode_waitforwrites(vp, async_throttle, 0, 0, (char *)"cluster_io");
-                         
-                       for (cbp = cbp_head; cbp;) {
-                               buf_t   cbp_next;
+                       need_EOT = TRUE;
+               }
+               if (need_EOT == TRUE)
+                       cluster_EOT(cbp_head, cbp_tail, size == 0 ? zero_offset : 0);
 
-                               if ( !(io_flags & B_READ))
-                                       vnode_startwrite(vp);
+               if (flags & CL_THROTTLE)
+                       (void)vnode_waitforwrites(vp, async_throttle, 0, 0, "cluster_io");
 
-                               cbp_next = cbp->b_trans_next;
+               if ( !(io_flags & B_READ))
+                       vnode_startwrite(vp);
                                
-                               (void) VNOP_STRATEGY(cbp);
-                               cbp = cbp_next;
-                       }
-                       if ( !(flags & CL_ASYNC)) {
-                               int dummy;
-
-                               for (cbp = cbp_head; cbp; cbp = cbp->b_trans_next)
-                                       buf_biowait(cbp);
-
-                               if ((error = cluster_iodone(cbp_head, (void *)&dummy))) {
-                                       if (((flags & (CL_PAGEOUT | CL_KEEPCACHED)) == CL_PAGEOUT) && (error == ENXIO))
-                                               error = 0;      /* drop the error */
-                                       else {
-                                               if (retval == 0)
-                                                       retval = error;
-                                               error = 0;
-                                       }
-                               }
-                       }
-                       cbp_head = (buf_t)NULL;
-                       cbp_tail = (buf_t)NULL;
+               (void) VNOP_STRATEGY(cbp);
+
+               if (need_EOT == TRUE) {
+                       if ( !(flags & CL_ASYNC))
+                               cluster_complete_transaction(&cbp_head, callback_arg, &retval, flags, 1);
 
+                       need_EOT = FALSE;
                        trans_count = 0;
+                       cbp_head = NULL;
                }
-       }
+        }
        if (error) {
                int abort_size;
 
                io_size = 0;
                
-               for (cbp = cbp_head; cbp;) {
-                       buf_t   cbp_next;
-                       upl_offset -= cbp->b_bcount;
-                       size       += cbp->b_bcount;
-                       io_size    += cbp->b_bcount;
+               if (cbp_head) {
+                        /*
+                         * first wait until all of the outstanding I/O
+                         * for this partial transaction has completed
+                         */
+                       cluster_wait_IO(cbp_head, (flags & CL_ASYNC));
 
-                       cbp_next = cbp->b_trans_next;
-                       free_io_buf(cbp);
-                       cbp = cbp_next;
+                       /*
+                        * Rewind the upl offset to the beginning of the
+                        * transaction.
+                        */
+                       upl_offset = cbp_head->b_uploffset;
+
+                       for (cbp = cbp_head; cbp;) {
+                               buf_t   cbp_next;
+        
+                               size       += cbp->b_bcount;
+                               io_size    += cbp->b_bcount;
+
+                               cbp_next = cbp->b_trans_next;
+                               free_io_buf(cbp);
+                               cbp = cbp_next;
+                       }
                }
                if (iostate) {
                        int need_wakeup = 0;
@@ -1016,7 +1362,7 @@ start_io:
                         * since we never really issued the io
                         * just go ahead and adjust it back
                         */
-                       lck_mtx_lock(cl_mtxp);
+                       lck_mtx_lock_spin(cl_mtxp);
 
                        if (iostate->io_error == 0)
                                iostate->io_error = error;
@@ -1028,54 +1374,49 @@ start_io:
                                 * this io stream to change
                                 */
                                iostate->io_wanted = 0;
-                               need_wakeup = 0;
+                               need_wakeup = 1;
                        }
                        lck_mtx_unlock(cl_mtxp);
 
                        if (need_wakeup)
                                wakeup((caddr_t)&iostate->io_wanted);
                }
-               pg_offset  = upl_offset & PAGE_MASK;
-               abort_size = (size + pg_offset + (PAGE_SIZE - 1)) & ~PAGE_MASK;
-
                if (flags & CL_COMMIT) {
-                       int upl_abort_code;
+                       int     upl_flags;
 
-                       if (flags & CL_PRESERVE) {
-                               ubc_upl_commit_range(upl, upl_offset - pg_offset, abort_size,
-                                                    UPL_COMMIT_FREE_ON_EMPTY);
-                       } else {
-                               if ((flags & CL_PAGEOUT) && (error != ENXIO)) /* transient error */
-                                       upl_abort_code = UPL_ABORT_FREE_ON_EMPTY;
-                               else if (flags & CL_PAGEIN)
-                                       upl_abort_code = UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR;
-                               else
-                                       upl_abort_code = UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_DUMP_PAGES;
-
-                               ubc_upl_abort_range(upl, upl_offset - pg_offset, abort_size,
-                                               upl_abort_code);
-                       }
+                       pg_offset  = upl_offset & PAGE_MASK;
+                       abort_size = (upl_end_offset - upl_offset + PAGE_MASK) & ~PAGE_MASK;
+                       
+                       upl_flags = cluster_ioerror(upl, upl_offset - pg_offset, abort_size, error, io_flags);
+                       
                        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 28)) | DBG_FUNC_NONE,
-                                    (int)upl, upl_offset - pg_offset, abort_size, error, 0);
-               }
-               if (real_bp) {
-                       real_bp->b_flags |= B_ERROR;
-                       real_bp->b_error  = error;
-
-                       buf_biodone(real_bp);
+                                    (int)upl, upl_offset - pg_offset, abort_size, (error << 24) | upl_flags, 0);
                }
                if (retval == 0)
                        retval = error;
+       } else if (cbp_head)
+                       panic("%s(): cbp_head is not NULL.\n", __FUNCTION__);
+
+       if (real_bp) {
+               /*
+                * can get here if we either encountered an error
+                * or we completely zero-filled the request and
+                * no I/O was issued
+                */
+               if (error) {
+                       real_bp->b_flags |= B_ERROR;
+                       real_bp->b_error = error;
+               }
+               buf_biodone(real_bp);
        }
-       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_END,
-                    (int)f_offset, size, upl_offset, retval, 0);
+       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 22)) | DBG_FUNC_END, (int)f_offset, size, upl_offset, retval, 0);
 
        return (retval);
 }
 
 
 static int
-cluster_rd_prefetch(vnode_t vp, off_t f_offset, u_int size, off_t filesize)
+cluster_read_prefetch(vnode_t vp, off_t f_offset, u_int size, off_t filesize, int (*callback)(buf_t, void *), void *callback_arg, int bflag)
 {
        int           pages_in_prefetch;
 
@@ -1087,16 +1428,11 @@ cluster_rd_prefetch(vnode_t vp, off_t f_offset, u_int size, off_t filesize)
                             (int)f_offset, 0, 0, 0, 0);
                return(0);
        }
-       if (size > (MAX_UPL_TRANSFER * PAGE_SIZE))
-               size = (MAX_UPL_TRANSFER * PAGE_SIZE);
-       else
-               size = (size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
-
         if ((off_t)size > (filesize - f_offset))
                 size = filesize - f_offset;
        pages_in_prefetch = (size + (PAGE_SIZE - 1)) / PAGE_SIZE;
 
-       advisory_read(vp, filesize, f_offset, size);
+       advisory_read_ext(vp, filesize, f_offset, size, callback, callback_arg, bflag);
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 49)) | DBG_FUNC_END,
                     (int)f_offset + size, pages_in_prefetch, 0, 1, 0);
@@ -1107,7 +1443,8 @@ cluster_rd_prefetch(vnode_t vp, off_t f_offset, u_int size, off_t filesize)
 
 
 static void
-cluster_rd_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl_readahead *rap)
+cluster_read_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl_readahead *rap, int (*callback)(buf_t, void *), void *callback_arg,
+                  int bflag)
 {
        daddr64_t       r_addr;
        off_t           f_offset;
@@ -1122,8 +1459,7 @@ cluster_rd_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl
                             rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 0, 0);
                return;
        }
-       if (rap->cl_lastr == -1 || (extent->b_addr != rap->cl_lastr && extent->b_addr != (rap->cl_lastr + 1) &&
-                                  (extent->b_addr != (rap->cl_maxra + 1) || rap->cl_ralen == 0))) {
+       if (rap->cl_lastr == -1 || (extent->b_addr != rap->cl_lastr && extent->b_addr != (rap->cl_lastr + 1))) {
                rap->cl_ralen = 0;
                rap->cl_maxra = 0;
 
@@ -1133,7 +1469,7 @@ cluster_rd_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl
                return;
        }
        if (extent->e_addr < rap->cl_maxra) {
-               if ((rap->cl_maxra - extent->e_addr) > (MAX_UPL_TRANSFER / 4)) {
+               if ((rap->cl_maxra - extent->e_addr) > ((MAX_PREFETCH / PAGE_SIZE) / 4)) {
 
                        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 48)) | DBG_FUNC_END,
                                     rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 2, 0);
@@ -1155,17 +1491,17 @@ cluster_rd_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl
        if (f_offset < filesize) {
                daddr64_t read_size;
 
-               rap->cl_ralen = rap->cl_ralen ? min(MAX_UPL_TRANSFER, rap->cl_ralen << 1) : 1;
+               rap->cl_ralen = rap->cl_ralen ? min(MAX_PREFETCH / PAGE_SIZE, rap->cl_ralen << 1) : 1;
 
                read_size = (extent->e_addr + 1) - extent->b_addr;
 
                if (read_size > rap->cl_ralen) {
-                       if (read_size > MAX_UPL_TRANSFER)
-                               rap->cl_ralen = MAX_UPL_TRANSFER;
+                       if (read_size > MAX_PREFETCH / PAGE_SIZE)
+                               rap->cl_ralen = MAX_PREFETCH / PAGE_SIZE;
                        else
                                rap->cl_ralen = read_size;
                }
-               size_of_prefetch = cluster_rd_prefetch(vp, f_offset, rap->cl_ralen * PAGE_SIZE, filesize);
+               size_of_prefetch = cluster_read_prefetch(vp, f_offset, rap->cl_ralen * PAGE_SIZE, filesize, callback, callback_arg, bflag);
 
                if (size_of_prefetch)
                        rap->cl_maxra = (r_addr + size_of_prefetch) - 1;
@@ -1174,15 +1510,24 @@ cluster_rd_ahead(vnode_t vp, struct cl_extent *extent, off_t filesize, struct cl
                     rap->cl_ralen, (int)rap->cl_maxra, (int)rap->cl_lastr, 4, 0);
 }
 
+
 int
 cluster_pageout(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset,
                int size, off_t filesize, int flags)
+{
+        return cluster_pageout_ext(vp, upl, upl_offset, f_offset, size, filesize, flags, NULL, NULL);
+
+}
+
+
+int
+cluster_pageout_ext(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset,
+               int size, off_t filesize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
 {
        int           io_size;
        int           rounded_size;
         off_t         max_size;
        int           local_flags;
-       struct cl_writebehind *wbp;
 
        if (vp->v_mount->mnt_kern_flag & MNTK_VIRTUALDEV)
                /*
@@ -1203,6 +1548,8 @@ cluster_pageout(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset,
                local_flags |= CL_COMMIT;
        if ((flags & UPL_KEEPCACHED))
                local_flags |= CL_KEEPCACHED;
+       if (flags & IO_PASSIVE) 
+               local_flags |= CL_PASSIVE;
 
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 52)) | DBG_FUNC_NONE,
@@ -1247,16 +1594,22 @@ cluster_pageout(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset,
                        ubc_upl_abort_range(upl, upl_offset + rounded_size, size - rounded_size,
                                        UPL_ABORT_FREE_ON_EMPTY);
        }
-       if ((wbp = cluster_get_wbp(vp, 0)) != NULL)
-               wbp->cl_hasbeenpaged = 1;
-
        return (cluster_io(vp, upl, upl_offset, f_offset, io_size,
-                          local_flags, (buf_t)NULL, (struct clios *)NULL));
+                          local_flags, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg));
 }
 
+
 int
 cluster_pagein(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset,
               int size, off_t filesize, int flags)
+{
+        return cluster_pagein_ext(vp, upl, upl_offset, f_offset, size, filesize, flags, NULL, NULL);
+}
+
+
+int
+cluster_pagein_ext(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset,
+              int size, off_t filesize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
 {
        u_int         io_size;
        int           rounded_size;
@@ -1271,6 +1624,8 @@ cluster_pagein(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset,
                local_flags |= CL_ASYNC;
        if ((flags & UPL_NOCOMMIT) == 0) 
                local_flags |= CL_COMMIT;
+       if (flags & IO_PASSIVE) 
+               local_flags |= CL_PASSIVE;
 
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 56)) | DBG_FUNC_NONE,
@@ -1302,37 +1657,21 @@ cluster_pagein(vnode_t vp, upl_t upl, vm_offset_t upl_offset, off_t f_offset,
                                    size - rounded_size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
        
        retval = cluster_io(vp, upl, upl_offset, f_offset, io_size,
-                          local_flags | CL_READ | CL_PAGEIN, (buf_t)NULL, (struct clios *)NULL);
-
-       if (retval == 0 && !(flags & UPL_NORDAHEAD) && !(vp->v_flag & VRAOFF)) {
-               struct cl_readahead *rap;
-
-               rap = cluster_get_rap(vp);
+                           local_flags | CL_READ | CL_PAGEIN, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
 
-               if (rap != NULL) {
-                       struct cl_extent extent;
-
-                       extent.b_addr = (daddr64_t)(f_offset / PAGE_SIZE_64);
-                       extent.e_addr = (daddr64_t)((f_offset + ((off_t)io_size - 1)) / PAGE_SIZE_64);
-
-                       if (rounded_size == PAGE_SIZE) {
-                               /*
-                                * we haven't read the last page in of the file yet
-                                * so let's try to read ahead if we're in 
-                                * a sequential access pattern
-                                */
-                               cluster_rd_ahead(vp, &extent, filesize, rap);
-                       }
-                       rap->cl_lastr = extent.e_addr;
-
-                       lck_mtx_unlock(&rap->cl_lockr);
-               }
-       }
        return (retval);
 }
 
+
 int
 cluster_bp(buf_t bp)
+{
+       return cluster_bp_ext(bp, NULL, NULL);
+}
+
+
+int
+cluster_bp_ext(buf_t bp, int (*callback)(buf_t, void *), void *callback_arg)
 {
         off_t  f_offset;
        int    flags;
@@ -1344,244 +1683,245 @@ cluster_bp(buf_t bp)
                flags = CL_ASYNC | CL_READ;
        else
                flags = CL_ASYNC;
+       if (bp->b_flags & B_PASSIVE) 
+               flags |= CL_PASSIVE;
 
        f_offset = ubc_blktooff(bp->b_vp, bp->b_lblkno);
 
-        return (cluster_io(bp->b_vp, bp->b_upl, 0, f_offset, bp->b_bcount, flags, bp, (struct clios *)NULL));
+        return (cluster_io(bp->b_vp, bp->b_upl, 0, f_offset, bp->b_bcount, flags, bp, (struct clios *)NULL, callback, callback_arg));
 }
 
+
+
 int
 cluster_write(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t headOff, off_t tailOff, int xflags)
 {
-       int           prev_resid;
-       u_int         clip_size;
-       off_t         max_io_size;
-       int           upl_size;
-       int           upl_flags;
-       upl_t         upl;
-       int           retval = 0;
-       int           flags;
+        return cluster_write_ext(vp, uio, oldEOF, newEOF, headOff, tailOff, xflags, NULL, NULL);
+}
+
+
+int
+cluster_write_ext(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t headOff, off_t tailOff,
+                 int xflags, int (*callback)(buf_t, void *), void *callback_arg)
+{
+        user_ssize_t   cur_resid;
+       int             retval = 0;
+       int             flags;
+        int            zflags;
+       int             bflag;
+       int             write_type = IO_COPY;
+       u_int32_t       write_length;
 
        flags = xflags;
 
+       if (flags & IO_PASSIVE)
+           bflag = CL_PASSIVE;
+       else
+           bflag = 0;
+
        if (vp->v_flag & VNOCACHE_DATA)
                flags |= IO_NOCACHE;
 
-       if ( (!(flags & IO_NOCACHE)) || (!uio) || (!UIO_SEG_IS_USER_SPACE(uio->uio_segflg))) {
+        if (uio == NULL) {
                /*
-                * go do a write through the cache if one of the following is true....
-                *   NOCACHE is not true
-                *   there is no uio structure or it doesn't target USERSPACE
+                * no user data...
+                * this call is being made to zero-fill some range in the file
                 */
-               return (cluster_write_x(vp, uio, oldEOF, newEOF, headOff, tailOff, flags));
-       }
+               retval = cluster_write_copy(vp, NULL, (u_int32_t)0, oldEOF, newEOF, headOff, tailOff, flags, callback, callback_arg);
 
-#if LP64_DEBUG
-       if (IS_VALID_UIO_SEGFLG(uio->uio_segflg) == 0) {
-               panic("%s :%d - invalid uio_segflg\n", __FILE__, __LINE__); 
+               return(retval);
        }
-#endif /* LP64_DEBUG */
-       
-       while (uio_resid(uio) && uio->uio_offset < newEOF && retval == 0) {
-               user_size_t     iov_len;
-               user_addr_t     iov_base;
-
-               /*
-                * we know we have a resid, so this is safe
-                * skip over any emtpy vectors
+        /*
+         * do a write through the cache if one of the following is true....
+         *   NOCACHE is not true and
+         *   the uio request doesn't target USERSPACE
+         * otherwise, find out if we want the direct or contig variant for
+         * the first vector in the uio request
+         */
+        if ( (flags & IO_NOCACHE) && UIO_SEG_IS_USER_SPACE(uio->uio_segflg) )
+               retval = cluster_io_type(uio, &write_type, &write_length, MIN_DIRECT_WRITE_SIZE);
+
+        if ( (flags & (IO_TAILZEROFILL | IO_HEADZEROFILL)) && write_type == IO_DIRECT)
+               /*
+                * must go through the cached variant in this case
                 */
-               uio_update(uio, (user_size_t)0);
-
-               iov_len  = uio_curriovlen(uio);
-               iov_base = uio_curriovbase(uio);
+               write_type = IO_COPY;
 
-               upl_size  = PAGE_SIZE;
-               upl_flags = UPL_QUERY_OBJECT_TYPE;
+       while ((cur_resid = uio_resid(uio)) && uio->uio_offset < newEOF && retval == 0) {
+         
+               switch (write_type) {
 
-               // LP64todo - fix this!
-               if ((vm_map_get_upl(current_map(),
-                                   (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
-                                   &upl_size, &upl, NULL, NULL, &upl_flags, 0)) != KERN_SUCCESS) {
+               case IO_COPY:
                        /*
-                        * the user app must have passed in an invalid address
+                        * make sure the uio_resid isn't too big...
+                        * internally, we want to handle all of the I/O in
+                        * chunk sizes that fit in a 32 bit int
                         */
-                       return (EFAULT);
-               }             
-
-               /*
-                * We check every vector target but if it is physically
-                * contiguous space, we skip the sanity checks.
-                */
-               if (upl_flags & UPL_PHYS_CONTIG) {
-                       int zflags;
-
-                       zflags = flags & ~IO_TAILZEROFILL;
-                       zflags |= IO_HEADZEROFILL;
-
-                       if (flags & IO_HEADZEROFILL) {
+                       if (cur_resid > (user_ssize_t)(MAX_IO_REQUEST_SIZE)) {
                                /*
-                                * in case we have additional vectors, we don't want to do this again
-                                */
-                               flags &= ~IO_HEADZEROFILL;
-
-                               if ((retval = cluster_write_x(vp, (struct uio *)0, 0, uio->uio_offset, headOff, 0, zflags)))
-                                       return(retval);
-                       }
-                       retval = cluster_phys_write(vp, uio, newEOF);
-
-                       if (uio_resid(uio) == 0 && (flags & IO_TAILZEROFILL)) {
-                               return (cluster_write_x(vp, (struct uio *)0, 0, tailOff, uio->uio_offset, 0, zflags));
-                       }
-               }
-               else if ((uio_resid(uio) < PAGE_SIZE) || (flags & (IO_TAILZEROFILL | IO_HEADZEROFILL))) {
-                       /*
-                        * we're here because we're don't have a physically contiguous target buffer
-                        * go do a write through the cache if one of the following is true....
-                        *   the total xfer size is less than a page...
-                        *   we're being asked to ZEROFILL either the head or the tail of the I/O...
-                        */
-                       return (cluster_write_x(vp, uio, oldEOF, newEOF, headOff, tailOff, flags));
-               }
-               // LP64todo - fix this!
-               else if (((int)uio->uio_offset & PAGE_MASK) || (CAST_DOWN(int, iov_base) & PAGE_MASK)) {
-                       if (((int)uio->uio_offset & PAGE_MASK) == (CAST_DOWN(int, iov_base) & PAGE_MASK)) {
-                               /*
-                                * Bring the file offset write up to a pagesize boundary
-                                * this will also bring the base address to a page boundary
-                                * since they both are currently on the same offset within a page
-                                * note: if we get here, uio->uio_resid is greater than PAGE_SIZE
-                                * so the computed clip_size must always be less than the current uio_resid
+                                * we're going to have to call cluster_write_copy
+                                * more than once...
+                                *
+                                * only want the last call to cluster_write_copy to
+                                * have the IO_TAILZEROFILL flag set and only the
+                                * first call should have IO_HEADZEROFILL
                                 */
-                               clip_size = (PAGE_SIZE - (uio->uio_offset & PAGE_MASK_64));
+                               zflags = flags & ~IO_TAILZEROFILL;
+                               flags &= ~IO_HEADZEROFILL;
 
-                               /* 
-                                * Fake the resid going into the cluster_write_x call
-                                * and restore it on the way out.
+                               write_length = MAX_IO_REQUEST_SIZE;
+                       } else {
+                               /*
+                                * last call to cluster_write_copy
                                 */
-                               // LP64todo - fix this
-                               prev_resid = uio_resid(uio);
-                               uio_setresid(uio, clip_size);
+                               zflags = flags;
+                         
+                               write_length = (u_int32_t)cur_resid;
+                       }
+                       retval = cluster_write_copy(vp, uio, write_length, oldEOF, newEOF, headOff, tailOff, zflags, callback, callback_arg);
+                       break;
 
-                               retval = cluster_write_x(vp, uio, oldEOF, newEOF, headOff, tailOff, flags);
+               case IO_CONTIG:
+                       zflags = flags & ~(IO_TAILZEROFILL | IO_HEADZEROFILL);
 
-                               uio_setresid(uio, prev_resid - (clip_size - uio_resid(uio)));
-                       } else {
-                               /*
-                                * can't get both the file offset and the buffer offset aligned to a page boundary
-                                * so fire an I/O through the cache for this entire vector
+                       if (flags & IO_HEADZEROFILL) {
+                               /*
+                                * only do this once per request
                                 */
-                               // LP64todo - fix this
-                               clip_size = iov_len;
-                               // LP64todo - fix this
-                               prev_resid = uio_resid(uio);
-                               uio_setresid(uio, clip_size);
+                               flags &= ~IO_HEADZEROFILL;
 
-                               retval = cluster_write_x(vp, uio, oldEOF, newEOF, headOff, tailOff, flags);
-
-                               uio_setresid(uio, prev_resid - (clip_size - uio_resid(uio)));
+                               retval = cluster_write_copy(vp, (struct uio *)0, (u_int32_t)0, (off_t)0, uio->uio_offset,
+                                                           headOff, (off_t)0, zflags | IO_HEADZEROFILL | IO_SYNC, callback, callback_arg);
+                               if (retval)
+                                       break;
                        }
-               } else {
-                       /* 
-                        * If we come in here, we know the offset into
-                        * the file is on a pagesize boundary and the
-                        * target buffer address is also on a page boundary
-                        */
-                       max_io_size = newEOF - uio->uio_offset;
-                       // LP64todo - fix this
-                       clip_size = uio_resid(uio);
-                       if (iov_len < clip_size)
-                               // LP64todo - fix this!
-                               clip_size = iov_len;
-                       if (max_io_size < clip_size)
-                               clip_size = max_io_size;
-
-                       if (clip_size < PAGE_SIZE) {
-                               /*
-                                * Take care of tail end of write in this vector
+                       retval = cluster_write_contig(vp, uio, newEOF, &write_type, &write_length, callback, callback_arg, bflag);
+
+                       if (retval == 0 && (flags & IO_TAILZEROFILL) && uio_resid(uio) == 0) {
+                               /*
+                                * we're done with the data from the user specified buffer(s)
+                                * and we've been requested to zero fill at the tail
+                                * treat this as an IO_HEADZEROFILL which doesn't require a uio
+                                * by rearranging the args and passing in IO_HEADZEROFILL
                                 */
-                               // LP64todo - fix this
-                               prev_resid = uio_resid(uio);
-                               uio_setresid(uio, clip_size);
-
-                               retval = cluster_write_x(vp, uio, oldEOF, newEOF, headOff, tailOff, flags);
-
-                               uio_setresid(uio, prev_resid - (clip_size - uio_resid(uio)));
-                       } else {
-                               /* round clip_size down to a multiple of pagesize */
-                               clip_size = clip_size & ~(PAGE_MASK);
-                               // LP64todo - fix this
-                               prev_resid = uio_resid(uio);
-                               uio_setresid(uio, clip_size);
-
-                               retval = cluster_nocopy_write(vp, uio, newEOF);
-
-                               if ((retval == 0) && uio_resid(uio))
-                                       retval = cluster_write_x(vp, uio, oldEOF, newEOF, headOff, tailOff, flags);
-
-                               uio_setresid(uio, prev_resid - (clip_size - uio_resid(uio)));
+                               retval = cluster_write_copy(vp, (struct uio *)0, (u_int32_t)0, (off_t)0, tailOff, uio->uio_offset,
+                                                           (off_t)0, zflags | IO_HEADZEROFILL | IO_SYNC, callback, callback_arg);
                        }
-               } /* end else */
-       } /* end while */
+                       break;
 
-       return(retval);
+               case IO_DIRECT:
+                       /*
+                        * cluster_write_direct is never called with IO_TAILZEROFILL || IO_HEADZEROFILL
+                        */
+                       retval = cluster_write_direct(vp, uio, oldEOF, newEOF, &write_type, &write_length, flags, callback, callback_arg);
+                       break;
+
+               case IO_UNKNOWN:
+                       retval = cluster_io_type(uio, &write_type, &write_length, MIN_DIRECT_WRITE_SIZE);
+                       break;
+               }
+       }
+       return (retval);
 }
 
 
 static int
-cluster_nocopy_write(vnode_t vp, struct uio *uio, off_t newEOF)
+cluster_write_direct(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, int *write_type, u_int32_t *write_length,
+                    int flags, int (*callback)(buf_t, void *), void *callback_arg)
 {
        upl_t            upl;
        upl_page_info_t  *pl;
        vm_offset_t      upl_offset;
+       u_int32_t        io_req_size;
+       u_int32_t        offset_in_file;
+       u_int32_t        offset_in_iovbase;
        int              io_size;
        int              io_flag;
-       int              upl_size;
-       int              upl_needed_size;
-       int              pages_in_pl;
+       int              bflag;
+       vm_size_t        upl_size;
+       vm_size_t        upl_needed_size;
+       mach_msg_type_number_t  pages_in_pl;
        int              upl_flags;
        kern_return_t    kret;
-       int              i;
+       mach_msg_type_number_t  i;
        int              force_data_sync;
-       int              error  = 0;
+       int              retval = 0;
+       int              first_IO = 1;
        struct clios     iostate;
-       struct cl_writebehind *wbp;
-
+       user_addr_t      iov_base;
+       u_int32_t        mem_alignment_mask;
+       u_int32_t        devblocksize;
 
-       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_START,
-                    (int)uio->uio_offset, (int)uio_resid(uio), 
-                    (int)newEOF, 0, 0);
+       if (flags & IO_PASSIVE)
+           bflag = CL_PASSIVE;
+       else
+           bflag = 0;
 
        /*
         * When we enter this routine, we know
-        *  -- the offset into the file is on a pagesize boundary
-        *  -- the resid is a page multiple
         *  -- the resid will not exceed iov_len
         */
-       
-       if ((wbp = cluster_get_wbp(vp, CLW_RETURNLOCKED)) != NULL) {
-
-               cluster_try_push(wbp, vp, newEOF, 0, 1);
+       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_START,
+                    (int)uio->uio_offset, *write_length, (int)newEOF, 0, 0);
 
-               lck_mtx_unlock(&wbp->cl_lockw);
-       }
        iostate.io_completed = 0;
        iostate.io_issued = 0;
        iostate.io_error = 0;
        iostate.io_wanted = 0;
 
-       while (uio_resid(uio) && uio->uio_offset < newEOF && error == 0) {
-               user_addr_t     iov_base;
+       mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;
+       devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
+
+       if (devblocksize == 1) {
+               /*
+                * the AFP client advertises a devblocksize of 1
+                * however, its BLOCKMAP routine maps to physical
+                * blocks that are PAGE_SIZE in size...
+                * therefore we can't ask for I/Os that aren't page aligned
+                * or aren't multiples of PAGE_SIZE in size
+                * by setting devblocksize to PAGE_SIZE, we re-instate
+                * the old behavior we had before the mem_alignment_mask
+                * changes went in...
+                */
+               devblocksize = PAGE_SIZE;
+       }
+
+next_dwrite:
+       io_req_size = *write_length;
+       iov_base = uio_curriovbase(uio);
 
-               io_size = uio_resid(uio);
+       offset_in_file = (u_int32_t)uio->uio_offset & PAGE_MASK;
+       offset_in_iovbase = (u_int32_t)iov_base & mem_alignment_mask;
 
-               if (io_size > (MAX_UPL_TRANSFER * PAGE_SIZE))
-                       io_size = MAX_UPL_TRANSFER * PAGE_SIZE;
+       if (offset_in_file || offset_in_iovbase) {
+               /*
+                * one of the 2 important offsets is misaligned
+                * so fire an I/O through the cache for this entire vector
+                */
+               goto wait_for_dwrites;
+       }
+       if (iov_base & (devblocksize - 1)) {
+               /*
+                * the offset in memory must be on a device block boundary
+                * so that we can guarantee that we can generate an
+                * I/O that ends on a page boundary in cluster_io
+                */
+               goto wait_for_dwrites;
+        }
+
+       while (io_req_size >= PAGE_SIZE && uio->uio_offset < newEOF && retval == 0) {
 
+               if (first_IO) {
+                       cluster_syncup(vp, newEOF, callback, callback_arg);
+                       first_IO = 0;
+               }
+               io_size  = io_req_size & ~PAGE_MASK;
                iov_base = uio_curriovbase(uio);
 
-               // LP64todo - fix this!
-               upl_offset = CAST_DOWN(vm_offset_t, iov_base) & PAGE_MASK;
-               
+               if (io_size > (MAX_UPL_TRANSFER * PAGE_SIZE))
+                       io_size = MAX_UPL_TRANSFER * PAGE_SIZE;
+
+               upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
                upl_needed_size = (upl_offset + io_size + (PAGE_SIZE -1)) & ~PAGE_MASK;
 
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_START,
@@ -1593,7 +1933,6 @@ cluster_nocopy_write(vnode_t vp, struct uio *uio, off_t newEOF)
                        upl_flags = UPL_FILE_IO | UPL_COPYOUT_FROM | UPL_NO_SYNC |
                                    UPL_CLEAN_IN_PLACE | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;
 
-                       // LP64todo - fix this!
                        kret = vm_map_get_upl(current_map(),
                                              (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
                                              &upl_size,
@@ -1607,13 +1946,13 @@ cluster_nocopy_write(vnode_t vp, struct uio *uio, off_t newEOF)
                                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
                                             0, 0, 0, kret, 0);
                                /*
-                                * cluster_nocopy_write: failed to get pagelist
+                                * failed to get pagelist
                                 *
                                 * we may have already spun some portion of this request
                                 * off as async requests... we need to wait for the I/O
                                 * to complete before returning
                                 */
-                               goto wait_for_writes;
+                               goto wait_for_dwrites;
                        }
                        pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
                        pages_in_pl = upl_size / PAGE_SIZE;
@@ -1629,8 +1968,7 @@ cluster_nocopy_write(vnode_t vp, struct uio *uio, off_t newEOF)
                         * didn't get all the pages back that we
                         * needed... release this upl and try again
                         */
-                       ubc_upl_abort_range(upl, (upl_offset & ~PAGE_MASK), upl_size, 
-                                           UPL_ABORT_FREE_ON_EMPTY);
+                       ubc_upl_abort(upl, 0);
                }
                if (force_data_sync >= 3) {
                        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
@@ -1643,28 +1981,31 @@ cluster_nocopy_write(vnode_t vp, struct uio *uio, off_t newEOF)
                         * off as async requests... we need to wait for the I/O
                         * to complete before returning
                         */
-                       goto wait_for_writes;
+                       goto wait_for_dwrites;
                }
 
                /*
                 * Consider the possibility that upl_size wasn't satisfied.
                 */
-               if (upl_size != upl_needed_size)
-                       io_size = (upl_size - (int)upl_offset) & ~PAGE_MASK;
-
+               if (upl_size < upl_needed_size) {
+                       if (upl_size && upl_offset == 0)
+                               io_size = upl_size;
+                       else
+                               io_size = 0;
+               }
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 76)) | DBG_FUNC_END,
                             (int)upl_offset, upl_size, (int)iov_base, io_size, 0);                    
 
                if (io_size == 0) {
-                       ubc_upl_abort_range(upl, (upl_offset & ~PAGE_MASK), upl_size, 
-                                           UPL_ABORT_FREE_ON_EMPTY);
+                       ubc_upl_abort(upl, 0);
                        /*
                         * we may have already spun some portion of this request
                         * off as async requests... we need to wait for the I/O
                         * to complete before returning
                         */
-                       goto wait_for_writes;
+                       goto wait_for_dwrites;
                }
+
                /*
                 * Now look for pages already in the cache
                 * and throw them away.
@@ -1683,7 +2024,7 @@ cluster_nocopy_write(vnode_t vp, struct uio *uio, off_t newEOF)
 
                while ((iostate.io_issued - iostate.io_completed) > (2 * MAX_UPL_TRANSFER * PAGE_SIZE)) {
                        iostate.io_wanted = 1;
-                       msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_nocopy_write", 0);
+                       msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_write_direct", NULL);
                }       
                lck_mtx_unlock(cl_mtxp);
 
@@ -1695,91 +2036,126 @@ cluster_nocopy_write(vnode_t vp, struct uio *uio, off_t newEOF)
                         * go wait for all writes that are part of this stream
                         * to complete before returning the error to the caller
                         */
-                       ubc_upl_abort_range(upl, (upl_offset & ~PAGE_MASK), upl_size, 
-                                           UPL_ABORT_FREE_ON_EMPTY);
+                       ubc_upl_abort(upl, 0);
 
-                       goto wait_for_writes;
+                       goto wait_for_dwrites;
                }
-               io_flag = CL_ASYNC | CL_PRESERVE | CL_COMMIT | CL_THROTTLE;
+               io_flag = CL_ASYNC | CL_PRESERVE | CL_COMMIT | CL_THROTTLE | CL_DIRECT_IO | bflag;
 
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 77)) | DBG_FUNC_START,
                             (int)upl_offset, (int)uio->uio_offset, io_size, io_flag, 0);
 
-               error = cluster_io(vp, upl, upl_offset, uio->uio_offset,
-                                  io_size, io_flag, (buf_t)NULL, &iostate);
+               retval = cluster_io(vp, upl, upl_offset, uio->uio_offset,
+                                  io_size, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
 
+               /*
+                * update the uio structure to
+                * reflect the I/O that we just issued
+                */
                uio_update(uio, (user_size_t)io_size);
 
+               io_req_size -= io_size;
+
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 77)) | DBG_FUNC_END,
-                            (int)upl_offset, (int)uio->uio_offset, (int)uio_resid(uio), error, 0);
+                            (int)upl_offset, (int)uio->uio_offset, io_req_size, retval, 0);
 
        } /* end while */
 
-wait_for_writes:
-       /*
-        * make sure all async writes issued as part of this stream
-        * have completed before we return
-        */
-       lck_mtx_lock(cl_mtxp);
+        if (retval == 0 && iostate.io_error == 0 && io_req_size == 0) {
 
-       while (iostate.io_issued != iostate.io_completed) {
-               iostate.io_wanted = 1;
-               msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_nocopy_write", 0);
-       }       
-       lck_mtx_unlock(cl_mtxp);
+               retval = cluster_io_type(uio, write_type, write_length, MIN_DIRECT_WRITE_SIZE);
+
+               if (retval == 0 && *write_type == IO_DIRECT) {
 
+                       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_NONE,
+                                    (int)uio->uio_offset, *write_length, (int)newEOF, 0, 0);
+
+                       goto next_dwrite;
+               }
+        }
+
+wait_for_dwrites:
+       if (iostate.io_issued) {
+               /*
+                * make sure all async writes issued as part of this stream
+                * have completed before we return
+                */
+               lck_mtx_lock(cl_mtxp);
+
+               while (iostate.io_issued != iostate.io_completed) {
+                       iostate.io_wanted = 1;
+                       msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_write_direct", NULL);
+               }       
+               lck_mtx_unlock(cl_mtxp);
+       }
        if (iostate.io_error)
-               error = iostate.io_error;
+               retval = iostate.io_error;
+
+       if (io_req_size && retval == 0) {
+               /*
+                * we couldn't handle the tail of this request in DIRECT mode
+                * so fire it through the copy path
+                *
+                * note that flags will never have IO_HEADZEROFILL or IO_TAILZEROFILL set
+                * so we can just pass 0 in for the headOff and tailOff
+                */
+               retval = cluster_write_copy(vp, uio, io_req_size, oldEOF, newEOF, (off_t)0, (off_t)0, flags, callback, callback_arg);
 
+               *write_type = IO_UNKNOWN;
+       }
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 75)) | DBG_FUNC_END,
-                    (int)uio->uio_offset, (int)uio->uio_resid, error, 4, 0);
+                    (int)uio->uio_offset, io_req_size, retval, 4, 0);
 
-       return (error);
+       return (retval);
 }
 
 
 static int
-cluster_phys_write(vnode_t vp, struct uio *uio, off_t newEOF)
+cluster_write_contig(vnode_t vp, struct uio *uio, off_t newEOF, int *write_type, u_int32_t *write_length,
+                    int (*callback)(buf_t, void *), void *callback_arg, int bflag)
 {
        upl_page_info_t *pl;
-       addr64_t         src_paddr;
-       upl_t            upl;
+       addr64_t         src_paddr = 0;
+       upl_t            upl[MAX_VECTS];
        vm_offset_t      upl_offset;
-       int              tail_size;
-       int              io_size;
-       int              upl_size;
-       int              upl_needed_size;
-       int              pages_in_pl;
+       u_int32_t        tail_size = 0;
+       u_int32_t        io_size;
+       u_int32_t        xsize;
+       vm_size_t        upl_size;
+       vm_size_t        upl_needed_size;
+       mach_msg_type_number_t  pages_in_pl;
        int              upl_flags;
        kern_return_t    kret;
+        struct clios     iostate;
        int              error  = 0;
+       int              cur_upl = 0;
+       int              num_upl = 0;
+       int              n;
        user_addr_t      iov_base;
-       int              devblocksize;
-       struct cl_writebehind *wbp;
+       u_int32_t        devblocksize;
+       u_int32_t        mem_alignment_mask;
 
-       devblocksize = vp->v_mount->mnt_devblocksize;
        /*
         * When we enter this routine, we know
-        *  -- the resid will not exceed iov_len
-        *  -- the vector target address is physcially contiguous
+        *  -- the io_req_size will not exceed iov_len
+        *  -- the target address is physically contiguous
         */
-       if ((wbp = cluster_get_wbp(vp, CLW_RETURNLOCKED)) != NULL) {
+       cluster_syncup(vp, newEOF, callback, callback_arg);
 
-               cluster_try_push(wbp, vp, newEOF, 0, 1);
+       devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
+       mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;
 
-               lck_mtx_unlock(&wbp->cl_lockw);
-       }
-#if LP64_DEBUG
-       if (IS_VALID_UIO_SEGFLG(uio->uio_segflg) == 0) {
-               panic("%s :%d - invalid uio_segflg\n", __FILE__, __LINE__); 
-       }
-#endif /* LP64_DEBUG */
+        iostate.io_completed = 0;
+        iostate.io_issued = 0;
+        iostate.io_error = 0;
+        iostate.io_wanted = 0;
+
+next_cwrite:
+       io_size = *write_length;
 
-       // LP64todo - fix this!
-       io_size = (int)uio_curriovlen(uio);
        iov_base = uio_curriovbase(uio);
 
-       upl_offset = CAST_DOWN(upl_offset_t, iov_base) & PAGE_MASK;
+       upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
        upl_needed_size = upl_offset + io_size;
 
        pages_in_pl = 0;
@@ -1787,88 +2163,166 @@ cluster_phys_write(vnode_t vp, struct uio *uio, off_t newEOF)
        upl_flags = UPL_FILE_IO | UPL_COPYOUT_FROM | UPL_NO_SYNC | 
                    UPL_CLEAN_IN_PLACE | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;
 
-       // LP64todo - fix this!
        kret = vm_map_get_upl(current_map(),
                              (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
-                             &upl_size, &upl, NULL, &pages_in_pl, &upl_flags, 0);
+                             &upl_size, &upl[cur_upl], NULL, &pages_in_pl, &upl_flags, 0);
 
        if (kret != KERN_SUCCESS) {
                /*
-                * cluster_phys_write: failed to get pagelist
-                * note: return kret here
+                * failed to get pagelist
                 */
-             return(EINVAL);
+               error = EINVAL;
+               goto wait_for_cwrites;
        }
+       num_upl++;
+
        /*
         * Consider the possibility that upl_size wasn't satisfied.
-        * This is a failure in the physical memory case.
         */
        if (upl_size < upl_needed_size) {
-               ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_FREE_ON_EMPTY);
-               return(EINVAL);
+               /*
+                * This is a failure in the physical memory case.
+                */
+               error = EINVAL;
+               goto wait_for_cwrites;
        }
-       pl = ubc_upl_pageinfo(upl);
+       pl = ubc_upl_pageinfo(upl[cur_upl]);
 
        src_paddr = ((addr64_t)upl_phys_page(pl, 0) << 12) + (addr64_t)upl_offset;
 
        while (((uio->uio_offset & (devblocksize - 1)) || io_size < devblocksize) && io_size) {
-               int   head_size;
+               u_int32_t   head_size;
 
-               head_size = devblocksize - (int)(uio->uio_offset & (devblocksize - 1));
+               head_size = devblocksize - (u_int32_t)(uio->uio_offset & (devblocksize - 1));
 
                if (head_size > io_size)
                        head_size = io_size;
 
-               error = cluster_align_phys_io(vp, uio, src_paddr, head_size, 0);
+               error = cluster_align_phys_io(vp, uio, src_paddr, head_size, 0, callback, callback_arg);
 
-               if (error) {
-                       ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_FREE_ON_EMPTY);
+               if (error)
+                       goto wait_for_cwrites;
 
-                       return(EINVAL);
-               }
                upl_offset += head_size;
                src_paddr  += head_size;
                io_size    -= head_size;
+
+               iov_base   += head_size;
+       }
+       if ((u_int32_t)iov_base & mem_alignment_mask) {
+               /*
+                * request doesn't set up on a memory boundary
+                * the underlying DMA engine can handle...
+                * return an error instead of going through
+                * the slow copy path since the intent of this
+                * path is direct I/O from device memory
+                */
+               error = EINVAL;
+               goto wait_for_cwrites;
        }
+
        tail_size = io_size & (devblocksize - 1);
        io_size  -= tail_size;
 
-       if (io_size) {
-               /*
-                * issue a synchronous write to cluster_io
+       while (io_size && error == 0) {
+
+               if (io_size > MAX_IO_CONTIG_SIZE)
+                       xsize = MAX_IO_CONTIG_SIZE;
+               else
+                       xsize = io_size;
+               /*
+                * request asynchronously so that we can overlap
+                * the preparation of the next I/O... we'll do
+                * the commit after all the I/O has completed
+                * since its all issued against the same UPL
+                * if there are already too many outstanding writes
+                * wait until some have completed before issuing the next
                 */
-               error = cluster_io(vp, upl, upl_offset, uio->uio_offset,
-                                  io_size, CL_DEV_MEMORY, (buf_t)NULL, (struct clios *)NULL);
-       }
-       if (error == 0) {
+               if (iostate.io_issued) {
+                       lck_mtx_lock(cl_mtxp);
+
+                       while ((iostate.io_issued - iostate.io_completed) > (2 * MAX_IO_CONTIG_SIZE)) {
+                               iostate.io_wanted = 1;
+                               msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_write_contig", NULL);
+                       }
+                       lck_mtx_unlock(cl_mtxp);
+               }
+                if (iostate.io_error) {
+                        /*
+                         * one of the earlier writes we issued ran into a hard error
+                         * don't issue any more writes...
+                         * go wait for all writes that are part of this stream
+                         * to complete before returning the error to the caller
+                         */
+                       goto wait_for_cwrites;
+               }
                /*
-                * The cluster_io write completed successfully,
-                * update the uio structure
+                * issue an asynchronous write to cluster_io
                 */
-               uio_update(uio, (user_size_t)io_size);
+               error = cluster_io(vp, upl[cur_upl], upl_offset, uio->uio_offset,
+                                  xsize, CL_DEV_MEMORY | CL_ASYNC | bflag, (buf_t)NULL, (struct clios *)&iostate, callback, callback_arg);
 
-               src_paddr += io_size;
+               if (error == 0) {
+                       /*
+                        * The cluster_io write completed successfully,
+                        * update the uio structure
+                        */
+                       uio_update(uio, (user_size_t)xsize);
 
-               if (tail_size)
-                       error = cluster_align_phys_io(vp, uio, src_paddr, tail_size, 0);
+                       upl_offset += xsize;
+                       src_paddr  += xsize;
+                       io_size    -= xsize;
+               }
        }
+        if (error == 0 && iostate.io_error == 0 && tail_size == 0) {
+
+               error = cluster_io_type(uio, write_type, write_length, 0);
+
+               if (error == 0 && *write_type == IO_CONTIG) {
+                       cur_upl++;
+                        goto next_cwrite;
+               }
+       } else
+               *write_type = IO_UNKNOWN;
+
+wait_for_cwrites:
        /*
-        * just release our hold on the physically contiguous
-        * region without changing any state
-        */
-       ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_FREE_ON_EMPTY);
+         * make sure all async writes that are part of this stream
+         * have completed before we proceed
+         */
+        lck_mtx_lock(cl_mtxp);
+
+        while (iostate.io_issued != iostate.io_completed) {
+               iostate.io_wanted = 1;
+               msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_write_contig", NULL);
+        }
+        lck_mtx_unlock(cl_mtxp);
+
+        if (iostate.io_error)
+               error = iostate.io_error;
+
+       if (error == 0 && tail_size)
+               error = cluster_align_phys_io(vp, uio, src_paddr, tail_size, 0, callback, callback_arg);
+
+        for (n = 0; n < num_upl; n++)
+               /*
+                * just release our hold on each physically contiguous
+                * region without changing any state
+                */
+               ubc_upl_abort(upl[n], 0);
 
        return (error);
 }
 
 
 static int
-cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t headOff, off_t tailOff, int flags)
+cluster_write_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size, off_t oldEOF, off_t newEOF, off_t headOff,
+                  off_t tailOff, int flags, int (*callback)(buf_t, void *), void *callback_arg)
 {
        upl_page_info_t *pl;
        upl_t            upl;
        vm_offset_t      upl_offset = 0;
-       int              upl_size;
+       vm_size_t        upl_size;
        off_t            upl_f_offset;
        int              pages_in_upl;
        int              start_offset;
@@ -1888,36 +2342,18 @@ cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t h
        struct cl_extent cl;
         int              intersection;
        struct cl_writebehind *wbp;
+       int              bflag;
 
-       if ((wbp = cluster_get_wbp(vp, 0)) != NULL)
-       {
-               if (wbp->cl_hasbeenpaged) {
-                       /*
-                        * this vnode had pages cleaned to it by
-                        * the pager which indicates that either
-                        * it's not very 'hot', or the system is
-                        * being overwhelmed by a lot of dirty 
-                        * data being delayed in the VM cache...
-                        * in either event, we'll push our remaining
-                        * delayed data at this point...  this will
-                        * be more efficient than paging out 1 page at 
-                        * a time, and will also act as a throttle
-                        * by delaying this client from writing any
-                        * more data until all his delayed data has
-                        * at least been queued to the uderlying driver.
-                        */
-                       if (wbp->cl_number || wbp->cl_scmap)
-                               cluster_push_EOF(vp, newEOF);
+       if (flags & IO_PASSIVE)
+           bflag = CL_PASSIVE;
+       else
+           bflag = 0;
 
-                       wbp->cl_hasbeenpaged = 0;
-               }
-       }
        if (uio) {
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_START,
-                            (int)uio->uio_offset, uio_resid(uio), (int)oldEOF, (int)newEOF, 0);
+                            (int)uio->uio_offset, io_req_size, (int)oldEOF, (int)newEOF, 0);
 
-               // LP64todo - fix this
-               io_resid = uio_resid(uio);
+               io_resid = io_req_size;
        } else {
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_START,
                             0, 0, (int)oldEOF, (int)newEOF, 0);
@@ -1949,8 +2385,7 @@ cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t h
        }
        if (flags & IO_TAILZEROFILL) {
                if (uio) {
-                       // LP64todo - fix this
-                       zero_off1 = uio->uio_offset + uio_resid(uio);
+                       zero_off1 = uio->uio_offset + io_req_size;
 
                        if (zero_off1 < tailOff)
                                zero_cnt1 = tailOff - zero_off1;
@@ -1984,7 +2419,7 @@ cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t h
 
                cl.b_addr = (daddr64_t)(upl_f_offset / PAGE_SIZE_64);
                
-               if (uio && ((flags & (IO_NOCACHE | IO_SYNC | IO_HEADZEROFILL | IO_TAILZEROFILL)) == 0)) {
+               if (uio && ((flags & (IO_SYNC | IO_HEADZEROFILL | IO_TAILZEROFILL)) == 0)) {
                        /*
                         * assumption... total_size <= io_resid
                         * because IO_HEADZEROFILL and IO_TAILZEROFILL not set
@@ -1993,12 +2428,12 @@ cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t h
                                total_size -= start_offset;
                        xfer_resid = total_size;
 
-                       retval = cluster_copy_ubc_data(vp, uio, &xfer_resid, 1);
+                       retval = cluster_copy_ubc_data_internal(vp, uio, &xfer_resid, 1, 1);
                        
                        if (retval)
                                break;
 
-                       io_resid   -= (total_size - xfer_resid);
+                       io_resid    -= (total_size - xfer_resid);
                        total_size   = xfer_resid;
                        start_offset = (int)(uio->uio_offset & PAGE_MASK_64);
                        upl_f_offset = uio->uio_offset - start_offset;
@@ -2054,7 +2489,7 @@ cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t h
                                      &pl,
                                      UPL_SET_LITE | UPL_WILL_MODIFY);
                if (kret != KERN_SUCCESS)
-                       panic("cluster_write: failed to get pagelist");
+                       panic("cluster_write_copy: failed to get pagelist");
 
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 41)) | DBG_FUNC_END,
                        (int)upl, (int)upl_f_offset, start_offset, 0, 0);
@@ -2073,7 +2508,7 @@ cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t h
                                read_size = newEOF - upl_f_offset;
 
                        retval = cluster_io(vp, upl, 0, upl_f_offset, read_size,
-                                           CL_READ, (buf_t)NULL, (struct clios *)NULL);
+                                           CL_READ | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
                        if (retval) {
                                /*
                                 * we had an error during the read which causes us to abort
@@ -2109,7 +2544,7 @@ cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t h
                                        read_size = newEOF - (upl_f_offset + upl_offset);
 
                                retval = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset, read_size,
-                                                   CL_READ, (buf_t)NULL, (struct clios *)NULL);
+                                                   CL_READ | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
                                if (retval) {
                                        /*
                                         * we had an error during the read which causes us to abort
@@ -2160,9 +2595,12 @@ cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t h
                        io_offset  += bytes_to_zero;
                }
                if (xfer_resid && io_resid) {
+                       u_int32_t  io_requested;
+
                        bytes_to_move = min(io_resid, xfer_resid);
+                       io_requested = bytes_to_move;
 
-                       retval = cluster_copy_upl_data(uio, upl, io_offset, bytes_to_move);
+                       retval = cluster_copy_upl_data(uio, upl, io_offset, (int *)&io_requested);
 
                        if (retval) {
 
@@ -2171,7 +2609,7 @@ cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t h
                                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 45)) | DBG_FUNC_NONE,
                                             (int)upl, 0, 0, retval, 0);
                        } else {
-                               io_resid  -= bytes_to_move;
+                               io_resid   -= bytes_to_move;
                                xfer_resid -= bytes_to_move;
                                io_offset  += bytes_to_move;
                        }
@@ -2206,11 +2644,11 @@ cluster_write_x(vnode_t vp, struct uio *uio, off_t oldEOF, off_t newEOF, off_t h
 
                if (retval == 0) {
                        int cl_index;
-                       int can_delay;
+                       int ret_cluster_try_push;
 
                        io_size += start_offset;
 
-                       if ((upl_f_offset + io_size) >= newEOF && io_size < upl_size) {
+                       if ((upl_f_offset + io_size) >= newEOF && (u_int)io_size < upl_size) {
                                /*
                                 * if we're extending the file with this write
                                 * we'll zero fill the rest of the page so that
@@ -2255,7 +2693,7 @@ check_cluster:
                                                ubc_upl_commit_range(upl, 0, upl_size,
                                                                     UPL_COMMIT_SET_DIRTY | UPL_COMMIT_INACTIVATE | UPL_COMMIT_FREE_ON_EMPTY);
 
-                                       sparse_cluster_add(wbp, vp, &cl, newEOF);
+                                       sparse_cluster_add(wbp, vp, &cl, newEOF, callback, callback_arg);
 
                                        lck_mtx_unlock(&wbp->cl_lockw);
 
@@ -2279,7 +2717,7 @@ check_cluster:
                                         */
                                        upl_size = 0;
                                }
-                               sparse_cluster_push(wbp, vp, newEOF, 1);
+                               sparse_cluster_push(wbp, vp, newEOF, PUSH_ALL, callback, callback_arg);
 
                                wbp->cl_number = 0;
                                /*
@@ -2311,7 +2749,7 @@ check_cluster:
                                        /*
                                         * the current write starts at or after the current cluster
                                         */
-                                       if (cl.e_addr <= (wbp->cl_clusters[cl_index].b_addr + MAX_UPL_TRANSFER)) {
+                                       if (cl.e_addr <= (wbp->cl_clusters[cl_index].b_addr + MAX_CLUSTER_SIZE)) {
                                                /*
                                                 * we have a write that fits entirely
                                                 * within the existing cluster limits
@@ -2323,7 +2761,7 @@ check_cluster:
                                                        wbp->cl_clusters[cl_index].e_addr = cl.e_addr;
                                                break;
                                        }
-                                       if (cl.b_addr < (wbp->cl_clusters[cl_index].b_addr + MAX_UPL_TRANSFER)) {
+                                       if (cl.b_addr < (wbp->cl_clusters[cl_index].b_addr + MAX_CLUSTER_SIZE)) {
                                                /*
                                                 * we have a write that starts in the middle of the current cluster
                                                 * but extends beyond the cluster's limit... we know this because
@@ -2334,7 +2772,7 @@ check_cluster:
                                                 * note that we'll always have a leftover tail in this case since
                                                 * full absorbtion would have occurred in the clause above
                                                 */
-                                               wbp->cl_clusters[cl_index].e_addr = wbp->cl_clusters[cl_index].b_addr + MAX_UPL_TRANSFER;
+                                               wbp->cl_clusters[cl_index].e_addr = wbp->cl_clusters[cl_index].b_addr + MAX_CLUSTER_SIZE;
 
                                                if (upl_size) {
                                                        daddr64_t start_pg_in_upl;
@@ -2365,7 +2803,7 @@ check_cluster:
                                        /*
                                         * the current write starts in front of the cluster we're currently considering
                                         */
-                                       if ((wbp->cl_clusters[cl_index].e_addr - cl.b_addr) <= MAX_UPL_TRANSFER) {
+                                       if ((wbp->cl_clusters[cl_index].e_addr - cl.b_addr) <= MAX_CLUSTER_SIZE) {
                                                /*
                                                 * we can just merge the new request into
                                                 * this cluster and leave it in the cache
@@ -2378,7 +2816,7 @@ check_cluster:
                                                        /*
                                                         * the current write completely
                                                         * envelops the existing cluster and since
-                                                        * each write is limited to at most MAX_UPL_TRANSFER bytes
+                                                        * each write is limited to at most MAX_CLUSTER_SIZE pages
                                                         * we can just use the start and last blocknos of the write
                                                         * to generate the cluster limits
                                                         */
@@ -2396,18 +2834,18 @@ check_cluster:
                                         * get an intersection with the current write
                                         * 
                                         */
-                                       if (cl.e_addr > wbp->cl_clusters[cl_index].e_addr - MAX_UPL_TRANSFER) {
+                                       if (cl.e_addr > wbp->cl_clusters[cl_index].e_addr - MAX_CLUSTER_SIZE) {
                                                /*
                                                 * the current write extends into the proposed cluster
                                                 * clip the length of the current write after first combining it's
                                                 * tail with the newly shaped cluster
                                                 */
-                                               wbp->cl_clusters[cl_index].b_addr = wbp->cl_clusters[cl_index].e_addr - MAX_UPL_TRANSFER;
+                                               wbp->cl_clusters[cl_index].b_addr = wbp->cl_clusters[cl_index].e_addr - MAX_CLUSTER_SIZE;
 
                                                if (upl_size) {
                                                        intersection = (int)((cl.e_addr - wbp->cl_clusters[cl_index].b_addr) * PAGE_SIZE);
 
-                                                       if (intersection > upl_size)
+                                                       if ((u_int)intersection > upl_size)
                                                                /*
                                                                 * because the current write may consist of a number of pages found in the cache
                                                                 * which are not part of the UPL, we may have an intersection that exceeds
@@ -2437,7 +2875,7 @@ check_cluster:
                                 */
                                goto delay_io;
 
-                       if (wbp->cl_number < MAX_CLUSTERS && !(flags & IO_NOCACHE))
+                       if (wbp->cl_number < MAX_CLUSTERS)
                                /*
                                 * we didn't find an existing cluster to
                                 * merge into, but there's room to start
@@ -2455,18 +2893,19 @@ check_cluster:
                         * number of remaining clusters... and
                         * returns the number of currently unused clusters
                         */
-                       int ret_cluster_try_push = 0;
-                       /* if writes are not deferred, call cluster push immediately */
+                       ret_cluster_try_push = 0;
+
+                       /*
+                        * if writes are not deferred, call cluster push immediately
+                        */
                        if (!((unsigned int)vfs_flags(vp->v_mount) & MNT_DEFWRITE)) {
-                               if (flags & IO_NOCACHE)
-                                       can_delay = 0;
-                               else
-                                       can_delay = 1;
                                
-                               ret_cluster_try_push = cluster_try_push(wbp, vp, newEOF, can_delay, 0);
+                               ret_cluster_try_push = cluster_try_push(wbp, vp, newEOF, (flags & IO_NOCACHE) ? 0 : PUSH_DELAY, callback, callback_arg);
                        }
 
-                       /* execute following regardless writes are deferred or not */
+                       /*
+                        * execute following regardless of writes being deferred or not
+                        */
                        if (ret_cluster_try_push == 0) {
                                /*
                                 * no more room in the normal cluster mechanism
@@ -2482,8 +2921,8 @@ check_cluster:
                                        ubc_upl_commit_range(upl, upl_offset, upl_size,
                                                             UPL_COMMIT_SET_DIRTY | UPL_COMMIT_INACTIVATE | UPL_COMMIT_FREE_ON_EMPTY);
 
-                               sparse_cluster_switch(wbp, vp, newEOF);
-                               sparse_cluster_add(wbp, vp, &cl, newEOF);
+                               sparse_cluster_switch(wbp, vp, newEOF, callback, callback_arg);
+                               sparse_cluster_add(wbp, vp, &cl, newEOF, callback, callback_arg);
 
                                lck_mtx_unlock(&wbp->cl_lockw);
 
@@ -2492,25 +2931,27 @@ check_cluster:
                        /*
                         * we pushed one cluster successfully, so we must be sequentially writing this file
                         * otherwise, we would have failed and fallen into the sparse cluster support
-                        * so let's take the opportunity to push out additional clusters as long as we
-                        * remain below the throttle... this will give us better I/O locality if we're
-                        * in a copy loop (i.e.  we won't jump back and forth between the read and write points
-                        * however, we don't want to push so much out that the write throttle kicks in and
-                        * hangs this thread up until some of the I/O completes...
+                        * so let's take the opportunity to push out additional clusters...
+                        * this will give us better I/O locality if we're in a copy loop
+                        * (i.e.  we won't jump back and forth between the read and write points
                         */
                        if (!((unsigned int)vfs_flags(vp->v_mount) & MNT_DEFWRITE)) {
-                               while (wbp->cl_number && (vp->v_numoutput <= (VNODE_ASYNC_THROTTLE / 2)))
-                                       cluster_try_push(wbp, vp, newEOF, 0, 0);
+                               while (wbp->cl_number)
+                                       cluster_try_push(wbp, vp, newEOF, 0, callback, callback_arg);
                        }
 
 start_new_cluster:
                        wbp->cl_clusters[wbp->cl_number].b_addr = cl.b_addr;
                        wbp->cl_clusters[wbp->cl_number].e_addr = cl.e_addr;
 
+                       wbp->cl_clusters[wbp->cl_number].io_flags = 0;
+
                        if (flags & IO_NOCACHE)
-                               wbp->cl_clusters[wbp->cl_number].io_nocache = 1;
-                       else
-                               wbp->cl_clusters[wbp->cl_number].io_nocache = 0;
+                               wbp->cl_clusters[wbp->cl_number].io_flags |= CLW_IONOCACHE;
+
+                       if (bflag & CL_PASSIVE)
+                               wbp->cl_clusters[wbp->cl_number].io_flags |= CLW_IOPASSIVE;
+
                        wbp->cl_number++;
 delay_io:
                        if (upl_size)
@@ -2531,182 +2972,112 @@ issue_io:
                         * in order to maintain some semblance of coherency with mapped writes
                         * we need to drop the current upl and pick it back up with COPYOUT_FROM set
                         * so that we correctly deal with a change in state of the hardware modify bit...
-                        * we do this via cluster_push_x... by passing along the IO_SYNC flag, we force
-                        * cluster_push_x to wait until all the I/Os have completed... cluster_push_x is also
+                        * we do this via cluster_push_now... by passing along the IO_SYNC flag, we force
+                        * cluster_push_now to wait until all the I/Os have completed... cluster_push_now is also
                         * responsible for generating the correct sized I/O(s)
                         */
                        ubc_upl_commit_range(upl, 0, upl_size,
-                                                    UPL_COMMIT_SET_DIRTY | UPL_COMMIT_INACTIVATE | UPL_COMMIT_FREE_ON_EMPTY);
+                                            UPL_COMMIT_SET_DIRTY | UPL_COMMIT_INACTIVATE | UPL_COMMIT_FREE_ON_EMPTY);
 
                        cl.e_addr = (upl_f_offset + (off_t)upl_size) / PAGE_SIZE_64;
 
-                       retval = cluster_push_x(vp, &cl, newEOF, flags);
+                       retval = cluster_push_now(vp, &cl, newEOF, flags, callback, callback_arg);
                }
        }
-       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_END,
-                    retval, 0, io_resid, 0, 0);
+       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 40)) | DBG_FUNC_END, retval, 0, io_resid, 0, 0);
 
        return (retval);
 }
 
+
+
 int
 cluster_read(vnode_t vp, struct uio *uio, off_t filesize, int xflags)
 {
-       int           prev_resid;
-       u_int         clip_size;
-       off_t         max_io_size;
-       int           upl_size;
-       int           upl_flags;
-       upl_t         upl;
-       int           retval = 0;
-       int           flags;
+        return cluster_read_ext(vp, uio, filesize, xflags, NULL, NULL);
+}
+
+
+int
+cluster_read_ext(vnode_t vp, struct uio *uio, off_t filesize, int xflags, int (*callback)(buf_t, void *), void *callback_arg)
+{
+       int             retval = 0;
+       int             flags;
+       user_ssize_t    cur_resid;
+        u_int32_t      io_size;
+       u_int32_t       read_length = 0;
+       int             read_type = IO_COPY;
 
        flags = xflags;
 
        if (vp->v_flag & VNOCACHE_DATA)
                flags |= IO_NOCACHE;
-       if (vp->v_flag & VRAOFF)
+       if ((vp->v_flag & VRAOFF) || speculative_reads_disabled)
                flags |= IO_RAOFF;
 
-       if (!((flags & IO_NOCACHE) && UIO_SEG_IS_USER_SPACE(uio->uio_segflg))) {
-               /*
-                * go do a read through the cache if one of the following is true....
-                *   NOCACHE is not true
-                *   the uio request doesn't target USERSPACE
-                */
-               return (cluster_read_x(vp, uio, filesize, flags));
-       }
-
-#if LP64_DEBUG
-       if (IS_VALID_UIO_SEGFLG(uio->uio_segflg) == 0) {
-               panic("%s :%d - invalid uio_segflg\n", __FILE__, __LINE__); 
-       }
-#endif /* LP64_DEBUG */
-
-       while (uio_resid(uio) && uio->uio_offset < filesize && retval == 0) {
-               user_size_t     iov_len;
-               user_addr_t     iov_base;
-
-               /*
-                * we know we have a resid, so this is safe
-                * skip over any emtpy vectors
-                */
-               uio_update(uio, (user_size_t)0);
+        /*
+        * do a read through the cache if one of the following is true....
+        *   NOCACHE is not true
+        *   the uio request doesn't target USERSPACE
+        * otherwise, find out if we want the direct or contig variant for
+        * the first vector in the uio request
+        */
+       if ( (flags & IO_NOCACHE) && UIO_SEG_IS_USER_SPACE(uio->uio_segflg) )
+               retval = cluster_io_type(uio, &read_type, &read_length, 0);
 
-               iov_len  = uio_curriovlen(uio);
-               iov_base = uio_curriovbase(uio);
+       while ((cur_resid = uio_resid(uio)) && uio->uio_offset < filesize && retval == 0) {
 
-               upl_size  = PAGE_SIZE;
-               upl_flags = UPL_QUERY_OBJECT_TYPE;
-  
-               // LP64todo - fix this!
-               if ((vm_map_get_upl(current_map(),
-                                   (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
-                                   &upl_size, &upl, NULL, NULL, &upl_flags, 0)) != KERN_SUCCESS) {
+               switch (read_type) {
+               
+               case IO_COPY:
                        /*
-                        * the user app must have passed in an invalid address
+                        * make sure the uio_resid isn't too big...
+                        * internally, we want to handle all of the I/O in
+                        * chunk sizes that fit in a 32 bit int
                         */
-                       return (EFAULT);
-               }
+                       if (cur_resid > (user_ssize_t)(MAX_IO_REQUEST_SIZE))
+                               io_size = MAX_IO_REQUEST_SIZE;
+                       else
+                               io_size = (u_int32_t)cur_resid;
 
-               /*
-                * We check every vector target but if it is physically 
-                * contiguous space, we skip the sanity checks.
-                */
-               if (upl_flags & UPL_PHYS_CONTIG) {
-                       retval = cluster_phys_read(vp, uio, filesize);
-               }
-               else if (uio_resid(uio) < PAGE_SIZE) {
-                       /*
-                        * we're here because we're don't have a physically contiguous target buffer
-                        * go do a read through the cache if
-                        *   the total xfer size is less than a page...
-                        */
-                       return (cluster_read_x(vp, uio, filesize, flags));
-               }
-               // LP64todo - fix this!
-               else if (((int)uio->uio_offset & PAGE_MASK) || (CAST_DOWN(int, iov_base) & PAGE_MASK)) {
-                      if (((int)uio->uio_offset & PAGE_MASK) == (CAST_DOWN(int, iov_base) & PAGE_MASK)) {
-                              /*
-                               * Bring the file offset read up to a pagesize boundary
-                               * this will also bring the base address to a page boundary
-                               * since they both are currently on the same offset within a page
-                               * note: if we get here, uio->uio_resid is greater than PAGE_SIZE
-                               * so the computed clip_size must always be less than the current uio_resid
-                               */
-                              clip_size = (PAGE_SIZE - (int)(uio->uio_offset & PAGE_MASK_64));
-
-                              /* 
-                               * Fake the resid going into the cluster_read_x call
-                               * and restore it on the way out.
-                               */
-                              prev_resid = uio_resid(uio);
-                              // LP64todo - fix this
-                              uio_setresid(uio, clip_size);
-
-                              retval = cluster_read_x(vp, uio, filesize, flags);
-
-                              uio_setresid(uio, prev_resid - (clip_size - uio_resid(uio)));
-                      } else {
-                              /*
-                               * can't get both the file offset and the buffer offset aligned to a page boundary
-                               * so fire an I/O through the cache for this entire vector
-                               */
-                              // LP64todo - fix this!
-                              clip_size = iov_len;
-                              prev_resid = uio_resid(uio);
-                              uio_setresid(uio, clip_size);
-
-                              retval = cluster_read_x(vp, uio, filesize, flags);
-
-                              uio_setresid(uio, prev_resid - (clip_size - uio_resid(uio)));
-                      }
-               } else {
-                       /* 
-                        * If we come in here, we know the offset into
-                        * the file is on a pagesize boundary
-                        */
-                       max_io_size = filesize - uio->uio_offset;
-                       // LP64todo - fix this
-                       clip_size = uio_resid(uio);
-                       if (iov_len < clip_size)
-                               clip_size = iov_len;
-                       if (max_io_size < clip_size)
-                               clip_size = (int)max_io_size;
-
-                       if (clip_size < PAGE_SIZE) {
-                               /*
-                                * Take care of the tail end of the read in this vector.
-                                */
-                               // LP64todo - fix this
-                               prev_resid = uio_resid(uio);
-                               uio_setresid(uio, clip_size);
+                       retval = cluster_read_copy(vp, uio, io_size, filesize, flags, callback, callback_arg);
+                       break;
 
-                               retval = cluster_read_x(vp, uio, filesize, flags);
+               case IO_DIRECT:
+                       retval = cluster_read_direct(vp, uio, filesize, &read_type, &read_length, flags, callback, callback_arg);
+                       break;
 
-                               uio_setresid(uio, prev_resid - (clip_size - uio_resid(uio)));
-                       } else {
-                               /* round clip_size down to a multiple of pagesize */
-                               clip_size = clip_size & ~(PAGE_MASK);
-                               // LP64todo - fix this
-                               prev_resid = uio_resid(uio);
-                               uio_setresid(uio, clip_size);
+               case IO_CONTIG:
+                       retval = cluster_read_contig(vp, uio, filesize, &read_type, &read_length, callback, callback_arg, flags);
+                       break;
+                 
+               case IO_UNKNOWN:
+                       retval = cluster_io_type(uio, &read_type, &read_length, 0);
+                       break;
+               }
+       }
+       return (retval);
+}
 
-                               retval = cluster_nocopy_read(vp, uio, filesize);
 
-                               if ((retval==0) && uio_resid(uio))
-                                       retval = cluster_read_x(vp, uio, filesize, flags);
 
-                               uio_setresid(uio, prev_resid - (clip_size - uio_resid(uio)));
-                       }
-               } /* end else */
-       } /* end while */
+static void
+cluster_read_upl_release(upl_t upl, int start_pg, int last_pg, int flags)
+{
+       int range;
+       int abort_flags = UPL_ABORT_FREE_ON_EMPTY;
 
-       return(retval);
+       if ((range = last_pg - start_pg)) {
+               if ( !(flags & IO_NOCACHE))
+                       abort_flags |= UPL_ABORT_REFERENCE;
+
+               ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, range * PAGE_SIZE, abort_flags);
+       }
 }
 
+
 static int
-cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
+cluster_read_copy(vnode_t vp, struct uio *uio, u_int32_t io_req_size, off_t filesize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
 {
        upl_page_info_t *pl;
        upl_t            upl;
@@ -2721,26 +3092,44 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
        off_t            max_size;
        off_t            last_ioread_offset;
        off_t            last_request_offset;
-       u_int            size_of_prefetch;
-       u_int            io_size;
        kern_return_t    kret;
        int              error  = 0;
        int              retval = 0;
-       u_int            max_rd_size = MAX_UPL_TRANSFER * PAGE_SIZE;
+       u_int32_t        size_of_prefetch;
+       u_int32_t        xsize;
+       u_int32_t        io_size;
+       u_int32_t        max_rd_size = MAX_PREFETCH;
        u_int            rd_ahead_enabled = 1;
        u_int            prefetch_enabled = 1;
        struct cl_readahead *   rap;
        struct clios            iostate;
        struct cl_extent        extent;
+       int              bflag;
+       int              take_reference = 1;
+       struct uthread  *ut;
+       int              policy = IOPOL_DEFAULT;
+
+       policy = current_proc()->p_iopol_disk;
+
+       ut = get_bsdthread_info(current_thread());
+
+       if (ut->uu_iopol_disk != IOPOL_DEFAULT)
+               policy = ut->uu_iopol_disk;
+
+       if (policy == IOPOL_THROTTLE)
+               take_reference = 0;
+
+       if (flags & IO_PASSIVE)
+           bflag = CL_PASSIVE;
+       else
+           bflag = 0;
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 32)) | DBG_FUNC_START,
-                    (int)uio->uio_offset, uio_resid(uio), (int)filesize, 0, 0);
+                    (int)uio->uio_offset, io_req_size, (int)filesize, flags, 0);
                         
-       // LP64todo - fix this
-       last_request_offset = uio->uio_offset + uio_resid(uio);
+       last_request_offset = uio->uio_offset + io_req_size;
 
-       if ((flags & (IO_RAOFF|IO_NOCACHE)) ||
-               ((last_request_offset & ~PAGE_MASK_64) == (uio->uio_offset & ~PAGE_MASK_64))) {
+       if ((flags & (IO_RAOFF|IO_NOCACHE)) || ((last_request_offset & ~PAGE_MASK_64) == (uio->uio_offset & ~PAGE_MASK_64))) {
                rd_ahead_enabled = 0;
                rap = NULL;
        } else {
@@ -2775,7 +3164,7 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
        } else
                last_ioread_offset = (off_t)0;
 
-       while (uio_resid(uio) && uio->uio_offset < filesize && retval == 0) {
+       while (io_req_size && uio->uio_offset < filesize && retval == 0) {
                /*
                 * compute the size of the upl needed to encompass
                 * the requested read... limit each call to cluster_io
@@ -2788,21 +3177,20 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
                upl_f_offset = uio->uio_offset - (off_t)start_offset;
                max_size     = filesize - uio->uio_offset;
 
-       // LP64todo - fix this!
-               if ((off_t)((unsigned int)uio_resid(uio)) < max_size)
-                       io_size = uio_resid(uio);
+               if ((off_t)(io_req_size) < max_size)
+                       io_size = io_req_size;
                else
                        io_size = max_size;
 
                if (!(flags & IO_NOCACHE)) {
 
                        while (io_size) {
-                               u_int io_resid;
-                               u_int io_requested;
+                               u_int32_t io_resid;
+                               u_int32_t io_requested;
 
                                /*
                                 * if we keep finding the pages we need already in the cache, then
-                                * don't bother to call cluster_rd_prefetch since it costs CPU cycles
+                                * don't bother to call cluster_read_prefetch since it costs CPU cycles
                                 * to determine that we have all the pages we need... once we miss in
                                 * the cache and have issued an I/O, than we'll assume that we're likely
                                 * to continue to miss in the cache and it's to our advantage to try and prefetch
@@ -2819,7 +3207,7 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
                                                if (size_of_prefetch > max_rd_size)
                                                        size_of_prefetch = max_rd_size;
 
-                                               size_of_prefetch = cluster_rd_prefetch(vp, last_ioread_offset, size_of_prefetch, filesize);
+                                               size_of_prefetch = cluster_read_prefetch(vp, last_ioread_offset, size_of_prefetch, filesize, callback, callback_arg, bflag);
 
                                                last_ioread_offset += (off_t)(size_of_prefetch * PAGE_SIZE);
                                
@@ -2839,9 +3227,12 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
 
                                io_requested = io_resid;
 
-                               retval = cluster_copy_ubc_data(vp, uio, &io_resid, 0);
+                               retval = cluster_copy_ubc_data_internal(vp, uio, (int *)&io_resid, 0, take_reference);
+
+                               xsize = io_requested - io_resid;
 
-                               io_size -= (io_requested - io_resid);
+                               io_size -= xsize;
+                               io_req_size -= xsize;
 
                                if (retval || io_resid)
                                        /*
@@ -2856,7 +3247,7 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
                                         * we're already finished the I/O for this read request
                                         * let's see if we should do a read-ahead
                                         */
-                                       cluster_rd_ahead(vp, &extent, filesize, rap);
+                                       cluster_read_ahead(vp, &extent, filesize, rap, callback, callback_arg, bflag);
                                }
                        }
                        if (retval)
@@ -2878,8 +3269,13 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
 
                upl_size = (start_offset + io_size + (PAGE_SIZE - 1)) & ~PAGE_MASK;
 
-               if (upl_size > (MAX_UPL_TRANSFER * PAGE_SIZE) / 4)
-                       upl_size = (MAX_UPL_TRANSFER * PAGE_SIZE) / 4;
+               if (flags & IO_NOCACHE) {
+                       if (upl_size > (MAX_UPL_TRANSFER * PAGE_SIZE))
+                               upl_size = (MAX_UPL_TRANSFER * PAGE_SIZE);
+               } else {
+                       if (upl_size > (MAX_UPL_TRANSFER * PAGE_SIZE) / 4)
+                               upl_size = (MAX_UPL_TRANSFER * PAGE_SIZE) / 4;
+               }
                pages_in_upl = upl_size / PAGE_SIZE;
 
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 33)) | DBG_FUNC_START,
@@ -2890,9 +3286,9 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
                                      upl_size,
                                      &upl,
                                      &pl,
-                                     UPL_SET_LITE);
+                                     UPL_FILE_IO | UPL_SET_LITE);
                if (kret != KERN_SUCCESS)
-                       panic("cluster_read: failed to get pagelist");
+                       panic("cluster_read_copy: failed to get pagelist");
 
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 33)) | DBG_FUNC_END,
                             (int)upl, (int)upl_f_offset, upl_size, start_offset, 0);
@@ -2941,7 +3337,7 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
                         */
 
                        error = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset,
-                                          io_size, CL_READ | CL_ASYNC, (buf_t)NULL, &iostate);
+                                          io_size, CL_READ | CL_ASYNC | bflag, (buf_t)NULL, &iostate, callback, callback_arg);
                }
                if (error == 0) {
                        /*
@@ -2956,8 +3352,19 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
                                if (!upl_valid_page(pl, uio_last))
                                        break;
                        }
+                       if (uio_last < pages_in_upl) {
+                               /*
+                                * there were some invalid pages beyond the valid pages
+                                * that we didn't issue an I/O for, just release them
+                                * unchanged now, so that any prefetch/readahed can
+                                * include them
+                                */
+                               ubc_upl_abort_range(upl, uio_last * PAGE_SIZE,
+                                                   (pages_in_upl - uio_last) * PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
+                       }
+
                        /*
-                        * compute size to transfer this round,  if uio->uio_resid is
+                        * compute size to transfer this round,  if io_req_size is
                         * still non-zero after this attempt, we'll loop around and
                         * set up for another I/O.
                         */
@@ -2966,26 +3373,32 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
                        if (val_size > max_size)
                                val_size = max_size;
 
-                       if (val_size > uio_resid(uio))
-       // LP64todo - fix this
-                               val_size = uio_resid(uio);
+                       if (val_size > io_req_size)
+                               val_size = io_req_size;
 
-                       if (last_ioread_offset == 0)
+                       if ((uio->uio_offset + val_size) > last_ioread_offset)
                                last_ioread_offset = uio->uio_offset + val_size;
 
                        if ((size_of_prefetch = (last_request_offset - last_ioread_offset)) && prefetch_enabled) {
-                               /*
-                                * if there's still I/O left to do for this request, and...
-                                * we're not in hard throttle mode, then issue a
-                                * pre-fetch I/O... the I/O latency will overlap
-                                * with the copying of the data
-                                */
-                               size_of_prefetch = cluster_rd_prefetch(vp, last_ioread_offset, size_of_prefetch, filesize);
 
-                               last_ioread_offset += (off_t)(size_of_prefetch * PAGE_SIZE);
+                               if ((last_ioread_offset - (uio->uio_offset + val_size)) <= upl_size) {
+                                       /*
+                                        * if there's still I/O left to do for this request, and...
+                                        * we're not in hard throttle mode, and...
+                                        * we're close to using up the previous prefetch, then issue a
+                                        * new pre-fetch I/O... the I/O latency will overlap
+                                        * with the copying of the data
+                                        */
+                                       if (size_of_prefetch > max_rd_size)
+                                               size_of_prefetch = max_rd_size;
+
+                                       size_of_prefetch = cluster_read_prefetch(vp, last_ioread_offset, size_of_prefetch, filesize, callback, callback_arg, bflag);
+
+                                       last_ioread_offset += (off_t)(size_of_prefetch * PAGE_SIZE);
                                
-                               if (last_ioread_offset > last_request_offset)
-                                       last_ioread_offset = last_request_offset;
+                                       if (last_ioread_offset > last_request_offset)
+                                               last_ioread_offset = last_request_offset;
+                               }
 
                        } else if ((uio->uio_offset + val_size) == last_request_offset) {
                                /*
@@ -2995,7 +3408,7 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
                                 * explicitly disabled it
                                 */
                                if (rd_ahead_enabled)
-                                       cluster_rd_ahead(vp, &extent, filesize, rap);
+                                       cluster_read_ahead(vp, &extent, filesize, rap, callback, callback_arg, bflag);
                                        
                                if (rap != NULL) {
                                        if (extent.e_addr < rap->cl_lastr)
@@ -3007,42 +3420,43 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
 
                        while (iostate.io_issued != iostate.io_completed) {
                                iostate.io_wanted = 1;
-                               msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_read_x", 0);
+                               msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_read_copy", NULL);
                        }       
                        lck_mtx_unlock(cl_mtxp);
 
                        if (iostate.io_error)
                                error = iostate.io_error;
-                       else
-                               retval = cluster_copy_upl_data(uio, upl, start_offset, val_size);
+                       else {
+                               u_int32_t io_requested;
+
+                               io_requested = val_size;
+
+                               retval = cluster_copy_upl_data(uio, upl, start_offset, (int *)&io_requested);
+                               
+                               io_req_size -= (val_size - io_requested);
+                       }
                }
                if (start_pg < last_pg) {
                        /*
                         * compute the range of pages that we actually issued an I/O for
                         * and either commit them as valid if the I/O succeeded
-                        * or abort them if the I/O failed
+                        * or abort them if the I/O failed or we're not supposed to 
+                        * keep them in the cache
                         */
                        io_size = (last_pg - start_pg) * PAGE_SIZE;
 
-                       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_START,
-                                    (int)upl, start_pg * PAGE_SIZE, io_size, error, 0);
+                       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_START, (int)upl, start_pg * PAGE_SIZE, io_size, error, 0);
 
                        if (error || (flags & IO_NOCACHE))
                                ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, io_size,
-                                               UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);
+                                                   UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);
                        else
                                ubc_upl_commit_range(upl, start_pg * PAGE_SIZE, io_size, 
-                                                    UPL_COMMIT_CLEAR_DIRTY |
-                                                    UPL_COMMIT_FREE_ON_EMPTY | 
-                                                    UPL_COMMIT_INACTIVATE);
+                                                    UPL_COMMIT_CLEAR_DIRTY | UPL_COMMIT_FREE_ON_EMPTY | UPL_COMMIT_INACTIVATE);
 
-                       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_END,
-                                    (int)upl, start_pg * PAGE_SIZE, io_size, error, 0);
+                       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_END, (int)upl, start_pg * PAGE_SIZE, io_size, error, 0);
                }
                if ((last_pg - start_pg) < pages_in_upl) {
-                       int cur_pg;
-                       int commit_flags;
-
                        /*
                         * the set of pages that we issued an I/O for did not encompass
                         * the entire upl... so just release these without modifying
@@ -3051,91 +3465,57 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
                        if (error)
                                ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_FREE_ON_EMPTY);
                        else {
-                               KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_START,
-                                            (int)upl, -1, pages_in_upl - (last_pg - start_pg), 0, 0);
 
-                               if (start_pg) {
-                                       /*
-                                        * we found some already valid pages at the beginning of
-                                        * the upl commit these back to the inactive list with
-                                        * reference cleared
-                                        */
-                                       for (cur_pg = 0; cur_pg < start_pg; cur_pg++) {
-                                               commit_flags = UPL_COMMIT_FREE_ON_EMPTY 
-                                                                  | UPL_COMMIT_INACTIVATE;
-                                               
-                                               if (upl_dirty_page(pl, cur_pg))
-                                                       commit_flags |= UPL_COMMIT_SET_DIRTY;
-                                               
-                                               if ( !(commit_flags & UPL_COMMIT_SET_DIRTY) && (flags & IO_NOCACHE))
-                                                       ubc_upl_abort_range(upl, cur_pg * PAGE_SIZE, PAGE_SIZE,
-                                                               UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);
-                                               else
-                                                       ubc_upl_commit_range(upl, cur_pg * PAGE_SIZE, 
-                                                               PAGE_SIZE, commit_flags);
-                                       }
-                               }
-                               if (last_pg < uio_last) {
-                                       /*
-                                        * we found some already valid pages immediately after the
-                                        * pages we issued I/O for, commit these back to the
-                                        * inactive list with reference cleared
-                                        */
-                                       for (cur_pg = last_pg; cur_pg < uio_last; cur_pg++) {
-                                               commit_flags =  UPL_COMMIT_FREE_ON_EMPTY 
-                                                                               | UPL_COMMIT_INACTIVATE;
-
-                                               if (upl_dirty_page(pl, cur_pg))
-                                                       commit_flags |= UPL_COMMIT_SET_DIRTY;
-                                               
-                                               if ( !(commit_flags & UPL_COMMIT_SET_DIRTY) && (flags & IO_NOCACHE))
-                                                       ubc_upl_abort_range(upl, cur_pg * PAGE_SIZE, PAGE_SIZE,
-                                                               UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);
-                                               else
-                                                       ubc_upl_commit_range(upl, cur_pg * PAGE_SIZE, 
-                                                               PAGE_SIZE, commit_flags);
-                                       }
-                               }
-                               if (uio_last < pages_in_upl) {
-                                       /*
-                                        * there were some invalid pages beyond the valid pages
-                                        * that we didn't issue an I/O for, just release them
-                                        * unchanged
-                                        */
-                                       ubc_upl_abort_range(upl, uio_last * PAGE_SIZE,
-                                                           (pages_in_upl - uio_last) * PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
-                               }
+                               KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_START,
+                                            (int)upl, -1, pages_in_upl - (last_pg - start_pg), 0, 0);
+
+                               /*
+                                * handle any valid pages at the beginning of
+                                * the upl... release these appropriately
+                                */
+                               cluster_read_upl_release(upl, 0, start_pg, flags);
 
-                               KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_END,
-                                       (int)upl, -1, -1, 0, 0);
+                               /*
+                                * handle any valid pages immediately after the
+                                * pages we issued I/O for... ... release these appropriately
+                                */
+                               cluster_read_upl_release(upl, last_pg, uio_last, flags);
+
+                               KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 35)) | DBG_FUNC_END, (int)upl, -1, -1, 0, 0);
                        }
                }
                if (retval == 0)
                        retval = error;
 
-               if ( uio_resid(uio) ) {
+               if (io_req_size) {
                        if (cluster_hard_throttle_on(vp)) {
                                rd_ahead_enabled = 0;
                                prefetch_enabled = 0;
 
                                max_rd_size = HARD_THROTTLE_MAXSIZE;
                        } else {
-                               if (rap != NULL)
-                                       rd_ahead_enabled = 1;
-                               prefetch_enabled = 1;
+                               if (max_rd_size == HARD_THROTTLE_MAXSIZE) {
+                                       /*
+                                        * coming out of throttled state
+                                        */
+                                       if (rap != NULL)
+                                               rd_ahead_enabled = 1;
+                                       prefetch_enabled = 1;
 
-                               max_rd_size = MAX_UPL_TRANSFER * PAGE_SIZE;
+                                       max_rd_size = MAX_PREFETCH;
+                                       last_ioread_offset = 0;
+                               }
                        }
                }
        }
        if (rap != NULL) {
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 32)) | DBG_FUNC_END,
-                            (int)uio->uio_offset, uio_resid(uio), rap->cl_lastr, retval, 0);
+                            (int)uio->uio_offset, io_req_size, rap->cl_lastr, retval, 0);
 
                lck_mtx_unlock(&rap->cl_lockr);
        } else {
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 32)) | DBG_FUNC_END,
-                            (int)uio->uio_offset, uio_resid(uio), 0, retval, 0);
+                            (int)uio->uio_offset, io_req_size, 0, retval, 0);
        }
 
        return (retval);
@@ -3143,107 +3523,197 @@ cluster_read_x(vnode_t vp, struct uio *uio, off_t filesize, int flags)
 
 
 static int
-cluster_nocopy_read(vnode_t vp, struct uio *uio, off_t filesize)
+cluster_read_direct(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
+                   int flags, int (*callback)(buf_t, void *), void *callback_arg)
 {
        upl_t            upl;
        upl_page_info_t  *pl;
+        off_t           max_io_size;
        vm_offset_t      upl_offset;
-       off_t            max_io_size;
-       int              io_size;
-       int              upl_size;
-       int              upl_needed_size;
-       int              pages_in_pl;
+       vm_size_t        upl_size;
+       vm_size_t        upl_needed_size;
+       unsigned int     pages_in_pl;
        int              upl_flags;
+       int              bflag;
        kern_return_t    kret;
-       int              i;
+       unsigned int     i;
        int              force_data_sync;
        int              retval = 0;
        int              no_zero_fill = 0;
        int              abort_flag = 0;
+       int              io_flag = 0;
+       int              misaligned = 0;
        struct clios     iostate;
-       u_int            max_rd_size  = MAX_UPL_TRANSFER * PAGE_SIZE;
-       u_int            max_rd_ahead = MAX_UPL_TRANSFER * PAGE_SIZE * 2;
-
+       user_addr_t      iov_base;
+       u_int32_t        io_req_size;
+       u_int32_t        offset_in_file;
+       u_int32_t        offset_in_iovbase;
+       u_int32_t        io_size;
+       u_int32_t        io_min;
+        u_int32_t       xsize;
+       u_int32_t        devblocksize;
+       u_int32_t        mem_alignment_mask;
+       u_int32_t        max_rd_size  = MAX_UPL_TRANSFER * PAGE_SIZE;
+       u_int32_t        max_rd_ahead = MAX_PREFETCH;
+
+       if (flags & IO_PASSIVE)
+           bflag = CL_PASSIVE;
+       else
+           bflag = 0;
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_START,
-                    (int)uio->uio_offset, uio_resid(uio), (int)filesize, 0, 0);
-
-       /*
-        * When we enter this routine, we know
-        *  -- the offset into the file is on a pagesize boundary
-        *  -- the resid is a page multiple
-        *  -- the resid will not exceed iov_len
-        */
+                    (int)uio->uio_offset, (int)filesize, *read_type, *read_length, 0);
 
        iostate.io_completed = 0;
        iostate.io_issued = 0;
        iostate.io_error = 0;
        iostate.io_wanted = 0;
 
-       while (uio_resid(uio) && uio->uio_offset < filesize && retval == 0) {
-               user_addr_t     iov_base;
+       devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
+       mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;
+
+       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_NONE,
+                    (int)devblocksize, (int)mem_alignment_mask, 0, 0, 0);
+
+       if (devblocksize == 1) {
+               /*
+                * the AFP client advertises a devblocksize of 1
+                * however, its BLOCKMAP routine maps to physical
+                * blocks that are PAGE_SIZE in size...
+                * therefore we can't ask for I/Os that aren't page aligned
+                * or aren't multiples of PAGE_SIZE in size
+                * by setting devblocksize to PAGE_SIZE, we re-instate
+                * the old behavior we had before the mem_alignment_mask
+                * changes went in...
+                */
+               devblocksize = PAGE_SIZE;
+       }
+next_dread:
+       io_req_size = *read_length;
+       iov_base = uio_curriovbase(uio);
+
+        max_io_size = filesize - uio->uio_offset;
+
+       if ((off_t)io_req_size > max_io_size)
+               io_req_size = max_io_size;
+
+       offset_in_file = (u_int32_t)uio->uio_offset & (devblocksize - 1);
+       offset_in_iovbase = (u_int32_t)iov_base & mem_alignment_mask;
+
+       if (offset_in_file || offset_in_iovbase) {
+               /*
+                * one of the 2 important offsets is misaligned
+                * so fire an I/O through the cache for this entire vector
+                */
+               misaligned = 1;
+       }
+       if (iov_base & (devblocksize - 1)) {
+               /*
+                * the offset in memory must be on a device block boundary
+                * so that we can guarantee that we can generate an
+                * I/O that ends on a page boundary in cluster_io
+                */
+               misaligned = 1;
+        }
+       /*
+        * When we get to this point, we know...
+        *  -- the offset into the file is on a devblocksize boundary
+        */
+
+       while (io_req_size && retval == 0) {
+               u_int32_t io_start;
 
                if (cluster_hard_throttle_on(vp)) {
                        max_rd_size  = HARD_THROTTLE_MAXSIZE;
                        max_rd_ahead = HARD_THROTTLE_MAXSIZE - 1;
                } else {
                        max_rd_size  = MAX_UPL_TRANSFER * PAGE_SIZE;
-                       max_rd_ahead = MAX_UPL_TRANSFER * PAGE_SIZE * 8;
+                       max_rd_ahead = MAX_PREFETCH;
                }
-               max_io_size = filesize - uio->uio_offset;
-
-       // LP64todo - fix this
-               if (max_io_size < (off_t)((unsigned int)uio_resid(uio)))
-                       io_size = max_io_size;
-               else
-                       io_size = uio_resid(uio);
+               io_start = io_size = io_req_size;
 
                /*
                 * First look for pages already in the cache
                 * and move them to user space.
+                *
+                * cluster_copy_ubc_data returns the resid
+                * in io_size
                 */
-               retval = cluster_copy_ubc_data(vp, uio, &io_size, 0);
+               retval = cluster_copy_ubc_data_internal(vp, uio, (int *)&io_size, 0, 0);
                        
-               if (retval) {
-                       /*
-                        * we may have already spun some portion of this request
-                        * off as async requests... we need to wait for the I/O
-                        * to complete before returning
+               /*
+                * calculate the number of bytes actually copied
+                * starting size - residual
+                */
+               xsize = io_start - io_size;
+
+               io_req_size -= xsize;
+
+               /*
+                * check to see if we are finished with this request...
+                */
+               if (io_req_size == 0 || misaligned) {
+                       /*
+                        * see if there's another uio vector to
+                        * process that's of type IO_DIRECT
+                        *
+                        * break out of while loop to get there
                         */
-                       goto wait_for_reads;
+                       break;
                }
                /*
-                * If we are already finished with this read, then return
+                * assume the request ends on a device block boundary
                 */
-               if (io_size == 0) {
-                       /*
+               io_min = devblocksize;
+
+               /*
+                * we can handle I/O's in multiples of the device block size
+                * however, if io_size isn't a multiple of devblocksize we
+                * want to clip it back to the nearest page boundary since
+                * we are going to have to go through cluster_read_copy to
+                * deal with the 'overhang'... by clipping it to a PAGE_SIZE
+                * multiple, we avoid asking the drive for the same physical
+                * blocks twice.. once for the partial page at the end of the
+                * request and a 2nd time for the page we read into the cache
+                * (which overlaps the end of the direct read) in order to 
+                * get at the overhang bytes
+                */
+               if (io_size & (devblocksize - 1)) {
+                       /*
+                        * request does NOT end on a device block boundary
+                        * so clip it back to a PAGE_SIZE boundary
+                        */
+                       io_size &= ~PAGE_MASK;
+                       io_min = PAGE_SIZE;
+               }
+               if (retval || io_size < io_min) {
+                       /*
+                        * either an error or we only have the tail left to
+                        * complete via the copy path...
                         * we may have already spun some portion of this request
                         * off as async requests... we need to wait for the I/O
                         * to complete before returning
                         */
-                       goto wait_for_reads;
+                       goto wait_for_dreads;
                }
-               max_io_size = io_size;
-
-               if (max_io_size > max_rd_size)
-                       max_io_size = max_rd_size;
+               if ((xsize = io_size) > max_rd_size)
+                       xsize = max_rd_size;
 
                io_size = 0;
 
-               ubc_range_op(vp, uio->uio_offset, uio->uio_offset + max_io_size, UPL_ROP_ABSENT, &io_size);
+               ubc_range_op(vp, uio->uio_offset, uio->uio_offset + xsize, UPL_ROP_ABSENT, (int *)&io_size);
 
-               if (io_size == 0)
+               if (io_size == 0) {
                        /*
-                        * we may have already spun some portion of this request
-                        * off as async requests... we need to wait for the I/O
-                        * to complete before returning
+                        * a page must have just come into the cache
+                        * since the first page in this range is no
+                        * longer absent, go back and re-evaluate
                         */
-                       goto wait_for_reads;
-               
+                       continue;
+               }
                iov_base = uio_curriovbase(uio);
 
-               // LP64todo - fix this!
-               upl_offset = CAST_DOWN(vm_offset_t, iov_base) & PAGE_MASK;
+               upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
                upl_needed_size = (upl_offset + io_size + (PAGE_SIZE -1)) & ~PAGE_MASK;
 
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_START,
@@ -3266,7 +3736,6 @@ cluster_nocopy_read(vnode_t vp, struct uio *uio, off_t filesize)
                        if (force_data_sync)
                                upl_flags |= UPL_FORCE_DATA_SYNC;
 
-                       // LP64todo - fix this!
                        kret = vm_map_create_upl(current_map(),
                                                 (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
                                                 &upl_size, &upl, NULL, &pages_in_pl, &upl_flags);
@@ -3275,13 +3744,13 @@ cluster_nocopy_read(vnode_t vp, struct uio *uio, off_t filesize)
                                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_END,
                                             (int)upl_offset, upl_size, io_size, kret, 0);
                                /*
-                                * cluster_nocopy_read: failed to get pagelist
+                                * failed to get pagelist
                                 *
                                 * we may have already spun some portion of this request
                                 * off as async requests... we need to wait for the I/O
                                 * to complete before returning
                                 */
-                               goto wait_for_reads;
+                               goto wait_for_dreads;
                        }
                        pages_in_pl = upl_size / PAGE_SIZE;
                        pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
@@ -3293,23 +3762,26 @@ cluster_nocopy_read(vnode_t vp, struct uio *uio, off_t filesize)
                        if (i == pages_in_pl)
                                break;
 
-                       ubc_upl_abort_range(upl, (upl_offset & ~PAGE_MASK), upl_size, abort_flag);
+                       ubc_upl_abort(upl, abort_flag);
                }
                if (force_data_sync >= 3) {
                        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_END,
                                     (int)upl_offset, upl_size, io_size, kret, 0);
                  
-                       goto wait_for_reads;
+                       goto wait_for_dreads;
                }
                /*
                 * Consider the possibility that upl_size wasn't satisfied.
                 */
-               if (upl_size != upl_needed_size)
-                       io_size = (upl_size - (int)upl_offset) & ~PAGE_MASK;
-
+               if (upl_size < upl_needed_size) {
+                       if (upl_size && upl_offset == 0)
+                               io_size = upl_size;
+                       else
+                               io_size = 0;
+               }
                if (io_size == 0) {
-                       ubc_upl_abort_range(upl, (upl_offset & ~PAGE_MASK), upl_size, abort_flag);
-                       goto wait_for_reads;
+                       ubc_upl_abort(upl, abort_flag);
+                       goto wait_for_dreads;
                }
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 72)) | DBG_FUNC_END,
                             (int)upl_offset, upl_size, io_size, kret, 0);
@@ -3324,7 +3796,7 @@ cluster_nocopy_read(vnode_t vp, struct uio *uio, off_t filesize)
 
                while ((iostate.io_issued - iostate.io_completed) > max_rd_ahead) {
                        iostate.io_wanted = 1;
-                       msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_nocopy_read", 0);
+                       msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_read_direct", NULL);
                }       
                lck_mtx_unlock(cl_mtxp);
                        
@@ -3336,158 +3808,213 @@ cluster_nocopy_read(vnode_t vp, struct uio *uio, off_t filesize)
                         * go wait for any other reads to complete before
                         * returning the error to the caller
                         */
-                       ubc_upl_abort_range(upl, (upl_offset & ~PAGE_MASK), upl_size, abort_flag);
+                       ubc_upl_abort(upl, abort_flag);
 
-                       goto wait_for_reads;
+                       goto wait_for_dreads;
                }
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 73)) | DBG_FUNC_START,
                             (int)upl, (int)upl_offset, (int)uio->uio_offset, io_size, 0);
 
-               retval = cluster_io(vp, upl, upl_offset, uio->uio_offset, io_size,
-                                  CL_PRESERVE | CL_COMMIT | CL_READ | CL_ASYNC | CL_NOZERO,
-                                  (buf_t)NULL, &iostate);
+               if (no_zero_fill)
+                       io_flag = CL_COMMIT | CL_READ | CL_ASYNC | CL_NOZERO | CL_DIRECT_IO | bflag;
+               else
+                       io_flag = CL_COMMIT | CL_READ | CL_ASYNC | CL_NOZERO | CL_DIRECT_IO | CL_PRESERVE | bflag;
+
+               retval = cluster_io(vp, upl, upl_offset, uio->uio_offset, io_size, io_flag, (buf_t)NULL, &iostate, callback, callback_arg);
 
                /*
                 * update the uio structure
                 */
                uio_update(uio, (user_size_t)io_size);
 
+               io_req_size -= io_size;
+
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 73)) | DBG_FUNC_END,
-                            (int)upl, (int)uio->uio_offset, (int)uio_resid(uio), retval, 0);
+                            (int)upl, (int)uio->uio_offset, io_req_size, retval, 0);
 
        } /* end while */
 
-wait_for_reads:
-       /*
-        * make sure all async reads that are part of this stream
-        * have completed before we return
-        */
-       lck_mtx_lock(cl_mtxp);
+       if (retval == 0 && iostate.io_error == 0 && io_req_size == 0 && uio->uio_offset < filesize) {
 
-       while (iostate.io_issued != iostate.io_completed) {
-               iostate.io_wanted = 1;
-               msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_nocopy_read", 0);
-       }       
-       lck_mtx_unlock(cl_mtxp);
+               retval = cluster_io_type(uio, read_type, read_length, 0);
+         
+               if (retval == 0 && *read_type == IO_DIRECT) {
+
+                       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_NONE,
+                                    (int)uio->uio_offset, (int)filesize, *read_type, *read_length, 0);
+
+                       goto next_dread;
+               }
+       }
+
+wait_for_dreads:
+       if (iostate.io_issued) {
+               /*
+                * make sure all async reads that are part of this stream
+                * have completed before we return
+                */
+               lck_mtx_lock(cl_mtxp);
+
+               while (iostate.io_issued != iostate.io_completed) {
+                       iostate.io_wanted = 1;
+                       msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_read_direct", NULL);
+               }       
+               lck_mtx_unlock(cl_mtxp);
+       }
 
        if (iostate.io_error)
-               retval = iostate.io_error;
+               retval = iostate.io_error;
+
+       if (io_req_size && retval == 0) {
+               /*
+                * we couldn't handle the tail of this request in DIRECT mode
+                * so fire it through the copy path
+                */
+               retval = cluster_read_copy(vp, uio, io_req_size, filesize, flags, callback, callback_arg);
 
+               *read_type = IO_UNKNOWN;
+       }
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 70)) | DBG_FUNC_END,
-                    (int)uio->uio_offset, (int)uio_resid(uio), 6, retval, 0);
+                    (int)uio->uio_offset, (int)uio_resid(uio), io_req_size, retval, 0);
 
        return (retval);
 }
 
 
 static int
-cluster_phys_read(vnode_t vp, struct uio *uio, off_t filesize)
+cluster_read_contig(vnode_t vp, struct uio *uio, off_t filesize, int *read_type, u_int32_t *read_length,
+                   int (*callback)(buf_t, void *), void *callback_arg, int flags)
 {
        upl_page_info_t *pl;
-       upl_t            upl;
+       upl_t            upl[MAX_VECTS];
        vm_offset_t      upl_offset;
-       addr64_t         dst_paddr;
-       off_t            max_size;
-       int              io_size;
-       user_size_t      iov_len;
+       addr64_t         dst_paddr = 0;
        user_addr_t      iov_base;
-       int              tail_size;
-       int              upl_size;
-       int              upl_needed_size;
-       int              pages_in_pl;
+       off_t            max_size;
+       vm_size_t        upl_size;
+       vm_size_t        upl_needed_size;
+       mach_msg_type_number_t  pages_in_pl;
        int              upl_flags;
        kern_return_t    kret;
        struct clios     iostate;
-       int              error;
-       int              devblocksize;
+       int              error= 0;
+       int              cur_upl = 0;
+       int              num_upl = 0;
+       int              n;
+        u_int32_t       xsize;
+       u_int32_t        io_size;
+       u_int32_t        devblocksize;
+       u_int32_t        mem_alignment_mask;
+       u_int32_t        tail_size = 0;
+       int              bflag;
+
+       if (flags & IO_PASSIVE)
+           bflag = CL_PASSIVE;
+       else
+           bflag = 0;
 
-       devblocksize = vp->v_mount->mnt_devblocksize;
        /*
         * When we enter this routine, we know
-        *  -- the resid will not exceed iov_len
-        *  -- the target address is physically contiguous
+        *  -- the read_length will not exceed the current iov_len
+        *  -- the target address is physically contiguous for read_length
         */
+       cluster_syncup(vp, filesize, callback, callback_arg);
 
-#if LP64_DEBUG
-       if (IS_VALID_UIO_SEGFLG(uio->uio_segflg) == 0) {
-               panic("%s :%d - invalid uio_segflg\n", __FILE__, __LINE__); 
-       }
-#endif /* LP64_DEBUG */
+       devblocksize = (u_int32_t)vp->v_mount->mnt_devblocksize;
+       mem_alignment_mask = (u_int32_t)vp->v_mount->mnt_alignmentmask;
 
-       iov_len = uio_curriovlen(uio);
-       iov_base = uio_curriovbase(uio);
+       iostate.io_completed = 0;
+       iostate.io_issued = 0;
+       iostate.io_error = 0;
+       iostate.io_wanted = 0;
+
+next_cread:
+       io_size = *read_length;
 
        max_size = filesize - uio->uio_offset;
 
-       // LP64todo - fix this!
-       if (max_size < 0 || (u_int64_t)max_size > iov_len)
-               io_size = iov_len;
-       else
+       if (io_size > max_size)
                io_size = max_size;
 
-       // LP64todo - fix this!
-       upl_offset = CAST_DOWN(vm_offset_t, iov_base) & PAGE_MASK;
+       iov_base = uio_curriovbase(uio);
+
+       upl_offset = (vm_offset_t)((u_int32_t)iov_base & PAGE_MASK);
        upl_needed_size = upl_offset + io_size;
 
-       error       = 0;
        pages_in_pl = 0;
        upl_size = upl_needed_size;
        upl_flags = UPL_FILE_IO | UPL_NO_SYNC | UPL_CLEAN_IN_PLACE | UPL_SET_INTERNAL | UPL_SET_LITE | UPL_SET_IO_WIRE;
 
+
+       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 92)) | DBG_FUNC_START,
+                    (int)upl_offset, (int)upl_size, (int)iov_base, io_size, 0);
+
        kret = vm_map_get_upl(current_map(),
                              (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
-                             &upl_size, &upl, NULL, &pages_in_pl, &upl_flags, 0);
+                             &upl_size, &upl[cur_upl], NULL, &pages_in_pl, &upl_flags, 0);
+
+       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 92)) | DBG_FUNC_END,
+                    (int)upl_offset, upl_size, io_size, kret, 0);
 
        if (kret != KERN_SUCCESS) {
                /*
-                * cluster_phys_read: failed to get pagelist
+                * failed to get pagelist
                 */
-               return(EINVAL);
+               error = EINVAL;
+               goto wait_for_creads;
        }
+       num_upl++;
+
        if (upl_size < upl_needed_size) {
                /*
                 * The upl_size wasn't satisfied.
                 */
-               ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_FREE_ON_EMPTY);
-
-               return(EINVAL);
+               error = EINVAL;
+               goto wait_for_creads;
        }
-       pl = ubc_upl_pageinfo(upl);
+       pl = ubc_upl_pageinfo(upl[cur_upl]);
 
        dst_paddr = ((addr64_t)upl_phys_page(pl, 0) << 12) + (addr64_t)upl_offset;
 
        while (((uio->uio_offset & (devblocksize - 1)) || io_size < devblocksize) && io_size) {
-               int   head_size;
+               u_int32_t   head_size;
 
-               head_size = devblocksize - (int)(uio->uio_offset & (devblocksize - 1));
+               head_size = devblocksize - (u_int32_t)(uio->uio_offset & (devblocksize - 1));
 
                if (head_size > io_size)
                        head_size = io_size;
 
-               error = cluster_align_phys_io(vp, uio, dst_paddr, head_size, CL_READ);
+               error = cluster_align_phys_io(vp, uio, dst_paddr, head_size, CL_READ, callback, callback_arg);
 
-               if (error) {
-                       ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_FREE_ON_EMPTY);
+               if (error)
+                       goto wait_for_creads;
 
-                       return(EINVAL);
-               }
                upl_offset += head_size;
                dst_paddr  += head_size;
                io_size    -= head_size;
+
+               iov_base   += head_size;
        }
+       if ((u_int32_t)iov_base & mem_alignment_mask) {
+               /*
+                * request doesn't set up on a memory boundary
+                * the underlying DMA engine can handle...
+                * return an error instead of going through
+                * the slow copy path since the intent of this
+                * path is direct I/O to device memory
+                */
+               error = EINVAL;
+               goto wait_for_creads;
+       }
+
        tail_size = io_size & (devblocksize - 1);
-       io_size  -= tail_size;
 
-       iostate.io_completed = 0;
-       iostate.io_issued = 0;
-       iostate.io_error = 0;
-       iostate.io_wanted = 0;
+       io_size  -= tail_size;
 
        while (io_size && error == 0) {
-               int  xsize;
 
-               if (io_size > (MAX_UPL_TRANSFER * PAGE_SIZE))
-                       xsize = MAX_UPL_TRANSFER * PAGE_SIZE;
+               if (io_size > MAX_IO_CONTIG_SIZE)
+                       xsize = MAX_IO_CONTIG_SIZE;
                else
                        xsize = io_size;
                /*
@@ -3498,17 +4025,27 @@ cluster_phys_read(vnode_t vp, struct uio *uio, off_t filesize)
                 * if there are already too many outstanding reads
                 * wait until some have completed before issuing the next
                 */
-               lck_mtx_lock(cl_mtxp);
-
-               while ((iostate.io_issued - iostate.io_completed) > (8 * MAX_UPL_TRANSFER * PAGE_SIZE)) {
-                       iostate.io_wanted = 1;
-                       msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_phys_read", 0);
-               }       
-               lck_mtx_unlock(cl_mtxp);
+               if (iostate.io_issued) {
+                       lck_mtx_lock(cl_mtxp);
 
-               error = cluster_io(vp, upl, upl_offset, uio->uio_offset, xsize, 
-                                  CL_READ | CL_NOZERO | CL_DEV_MEMORY | CL_ASYNC,
-                                  (buf_t)NULL, &iostate);
+                       while ((iostate.io_issued - iostate.io_completed) > (3 * MAX_IO_CONTIG_SIZE)) {
+                               iostate.io_wanted = 1;
+                               msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_read_contig", NULL);
+                       }       
+                       lck_mtx_unlock(cl_mtxp);
+               }
+               if (iostate.io_error) {
+                       /*
+                        * one of the earlier reads we issued ran into a hard error
+                        * don't issue any more reads...
+                        * go wait for any other reads to complete before
+                        * returning the error to the caller
+                        */
+                       goto wait_for_creads;
+               }
+               error = cluster_io(vp, upl[cur_upl], upl_offset, uio->uio_offset, xsize, 
+                                  CL_READ | CL_NOZERO | CL_DEV_MEMORY | CL_ASYNC | bflag,
+                                  (buf_t)NULL, &iostate, callback, callback_arg);
                /*
                 * The cluster_io read was issued successfully,
                 * update the uio structure
@@ -3521,6 +4058,18 @@ cluster_phys_read(vnode_t vp, struct uio *uio, off_t filesize)
                        io_size    -= xsize;
                }
        }
+       if (error == 0 && iostate.io_error == 0 && tail_size == 0 && num_upl < MAX_VECTS && uio->uio_offset < filesize) {
+
+               error = cluster_io_type(uio, read_type, read_length, 0);
+         
+               if (error == 0 && *read_type == IO_CONTIG) {
+                       cur_upl++;
+                       goto next_cread;
+               }
+       } else
+               *read_type = IO_UNKNOWN;
+
+wait_for_creads:
        /*
         * make sure all async reads that are part of this stream
         * have completed before we proceed
@@ -3529,7 +4078,7 @@ cluster_phys_read(vnode_t vp, struct uio *uio, off_t filesize)
 
        while (iostate.io_issued != iostate.io_completed) {
                iostate.io_wanted = 1;
-               msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_phys_read", 0);
+               msleep((caddr_t)&iostate.io_wanted, cl_mtxp, PRIBIO + 1, "cluster_read_contig", NULL);
        }       
        lck_mtx_unlock(cl_mtxp);
 
@@ -3537,24 +4086,96 @@ cluster_phys_read(vnode_t vp, struct uio *uio, off_t filesize)
                error = iostate.io_error;
 
        if (error == 0 && tail_size)
-               error = cluster_align_phys_io(vp, uio, dst_paddr, tail_size, CL_READ);
+               error = cluster_align_phys_io(vp, uio, dst_paddr, tail_size, CL_READ, callback, callback_arg);
 
-       /*
-        * just release our hold on the physically contiguous
-        * region without changing any state
-        */
-       ubc_upl_abort_range(upl, 0, upl_size, UPL_ABORT_FREE_ON_EMPTY);
+       for (n = 0; n < num_upl; n++)
+               /*
+                * just release our hold on each physically contiguous
+                * region without changing any state
+                */
+               ubc_upl_abort(upl[n], 0);
        
        return (error);
 }
 
 
+static int
+cluster_io_type(struct uio *uio, int *io_type, u_int32_t *io_length, u_int32_t min_length)
+{
+        user_size_t     iov_len;
+       user_addr_t      iov_base = 0;
+       upl_t            upl;
+       vm_size_t        upl_size;
+       int              upl_flags;
+       int              retval = 0;
+
+        /*
+        * skip over any emtpy vectors
+        */
+        uio_update(uio, (user_size_t)0);
+
+       iov_len = uio_curriovlen(uio);
+
+       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 94)) | DBG_FUNC_START, (int)uio, (int)iov_len, 0, 0, 0);
+
+       if (iov_len) {
+               iov_base = uio_curriovbase(uio);
+               /*
+                * make sure the size of the vector isn't too big...
+                * internally, we want to handle all of the I/O in
+                * chunk sizes that fit in a 32 bit int
+                */
+               if (iov_len > (user_size_t)MAX_IO_REQUEST_SIZE)
+                       upl_size = MAX_IO_REQUEST_SIZE;
+               else
+                       upl_size = (u_int32_t)iov_len;
+
+               upl_flags = UPL_QUERY_OBJECT_TYPE;
+  
+               if ((vm_map_get_upl(current_map(),
+                                   (vm_map_offset_t)(iov_base & ~((user_addr_t)PAGE_MASK)),
+                                   &upl_size, &upl, NULL, NULL, &upl_flags, 0)) != KERN_SUCCESS) {
+                       /*
+                        * the user app must have passed in an invalid address
+                        */
+                       retval = EFAULT;
+               }
+               if (upl_size == 0)
+                       retval = EFAULT;
+
+               *io_length = upl_size;
+
+               if (upl_flags & UPL_PHYS_CONTIG)
+                       *io_type = IO_CONTIG;
+               else if (iov_len >= min_length)
+                       *io_type = IO_DIRECT;
+               else
+                       *io_type = IO_COPY;
+       } else {
+               /*
+                * nothing left to do for this uio
+                */
+               *io_length = 0;
+               *io_type   = IO_UNKNOWN;
+       }
+       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 94)) | DBG_FUNC_END, (int)iov_base, *io_type, *io_length, retval, 0);
+
+       return (retval);
+}
+
+
 /*
  * generate advisory I/O's in the largest chunks possible
  * the completed pages will be released into the VM cache
  */
 int
 advisory_read(vnode_t vp, off_t filesize, off_t f_offset, int resid)
+{
+        return advisory_read_ext(vp, filesize, f_offset, resid, NULL, NULL, CL_PASSIVE);
+}
+
+int
+advisory_read_ext(vnode_t vp, off_t filesize, off_t f_offset, int resid, int (*callback)(buf_t, void *), void *callback_arg, int bflag)
 {
        upl_page_info_t *pl;
        upl_t            upl;
@@ -3698,7 +4319,7 @@ advisory_read(vnode_t vp, off_t filesize, off_t f_offset, int resid)
                                 * issue an asynchronous read to cluster_io
                                 */
                                retval = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset, io_size,
-                                                   CL_ASYNC | CL_READ | CL_COMMIT | CL_AGE, (buf_t)NULL, (struct clios *)NULL);
+                                                   CL_ASYNC | CL_READ | CL_COMMIT | CL_AGE | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
 
                                issued_io = 1;
                        }
@@ -3723,6 +4344,13 @@ advisory_read(vnode_t vp, off_t filesize, off_t f_offset, int resid)
 
 int
 cluster_push(vnode_t vp, int flags)
+{
+        return cluster_push_ext(vp, flags, NULL, NULL);
+}
+
+
+int
+cluster_push_ext(vnode_t vp, int flags, int (*callback)(buf_t, void *), void *callback_arg)
 {
         int    retval;
        struct  cl_writebehind *wbp;
@@ -3749,16 +4377,16 @@ cluster_push(vnode_t vp, int flags)
                     (int)wbp->cl_scmap, wbp->cl_number, flags, 0, 0);
 
        if (wbp->cl_scmap) {
-               sparse_cluster_push(wbp, vp, ubc_getsize(vp), 1);
+               sparse_cluster_push(wbp, vp, ubc_getsize(vp), PUSH_ALL | IO_PASSIVE, callback, callback_arg);
 
                retval = 1;
        } else 
-               retval = cluster_try_push(wbp, vp, ubc_getsize(vp), 0, 1);
+               retval = cluster_try_push(wbp, vp, ubc_getsize(vp), PUSH_ALL | IO_PASSIVE, callback, callback_arg);
 
        lck_mtx_unlock(&wbp->cl_lockw);
 
        if (flags & IO_SYNC)
-               (void)vnode_waitforwrites(vp, 0, 0, 0, (char *)"cluster_push");
+               (void)vnode_waitforwrites(vp, 0, 0, 0, "cluster_push");
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_END,
                     (int)wbp->cl_scmap, wbp->cl_number, retval, 0, 0);
@@ -3800,30 +4428,8 @@ cluster_release(struct ubc_info *ubc)
 }
 
 
-static void
-cluster_push_EOF(vnode_t vp, off_t EOF)
-{
-        struct cl_writebehind *wbp;
-
-       wbp = cluster_get_wbp(vp, CLW_ALLOCATE | CLW_RETURNLOCKED);
-
-       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_START,
-                    (int)wbp->cl_scmap, wbp->cl_number, (int)EOF, 0, 0);
-
-       if (wbp->cl_scmap)
-               sparse_cluster_push(wbp, vp, EOF, 1);
-       else 
-               cluster_try_push(wbp, vp, EOF, 0, 1);
-
-       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 53)) | DBG_FUNC_END,
-                    (int)wbp->cl_scmap, wbp->cl_number, 0, 0, 0);
-
-       lck_mtx_unlock(&wbp->cl_lockw);
-}
-
-
 static int
-cluster_try_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int can_delay, int push_all)
+cluster_try_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int push_flag, int (*callback)(buf_t, void *), void *callback_arg)
 {
         int cl_index;
        int cl_index1;
@@ -3835,7 +4441,15 @@ cluster_try_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int can_dela
        /*
         * the write behind context exists and has
         * already been locked...
-        *
+        */
+       if (wbp->cl_number == 0)
+               /*
+                * no clusters to push
+                * return number of empty slots
+                */
+               return (MAX_CLUSTERS);
+        
+       /*
         * make a local 'sorted' copy of the clusters
         * and clear wbp->cl_number so that new clusters can
         * be developed
@@ -3853,7 +4467,7 @@ cluster_try_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int can_dela
                        break;
                l_clusters[cl_index].b_addr = wbp->cl_clusters[min_index].b_addr;
                l_clusters[cl_index].e_addr = wbp->cl_clusters[min_index].e_addr;
-               l_clusters[cl_index].io_nocache = wbp->cl_clusters[min_index].io_nocache;
+               l_clusters[cl_index].io_flags = wbp->cl_clusters[min_index].io_flags;
 
                wbp->cl_clusters[min_index].b_addr = wbp->cl_clusters[min_index].e_addr;
        }
@@ -3861,7 +4475,7 @@ cluster_try_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int can_dela
 
        cl_len = cl_index;
 
-       if (can_delay && cl_len == MAX_CLUSTERS) {
+       if ( (push_flag & PUSH_DELAY) && cl_len == MAX_CLUSTERS ) {
                int   i;
                
                /*
@@ -3871,7 +4485,7 @@ cluster_try_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int can_dela
                 * used for managing more random I/O patterns
                 *
                 * we know that we've got all clusters currently in use and the next write doesn't fit into one of them...
-                * that's why we're in try_push with can_delay true...
+                * that's why we're in try_push with PUSH_DELAY...
                 *
                 * check to make sure that all the clusters except the last one are 'full'... and that each cluster
                 * is adjacent to the next (i.e. we're looking for sequential writes) they were sorted above
@@ -3884,7 +4498,7 @@ cluster_try_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int can_dela
                 * of order... if this occurs at the tail of the last cluster, we don't want to fall into the sparse cluster world...
                 */
                for (i = 0; i < MAX_CLUSTERS - 1; i++) {
-                       if ((l_clusters[i].e_addr - l_clusters[i].b_addr) != MAX_UPL_TRANSFER)
+                       if ((l_clusters[i].e_addr - l_clusters[i].b_addr) != MAX_CLUSTER_SIZE)
                                goto dont_try;
                        if (l_clusters[i].e_addr != l_clusters[i+1].b_addr)
                                goto dont_try;
@@ -3895,31 +4509,43 @@ cluster_try_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int can_dela
         * this is safe since I'm working off of a private sorted copy
         * of the clusters, and I'm going to re-evaluate the public
         * state after I retake the lock
+        *
+        * we need to drop it to avoid a lock inversion when trying to
+        * grab pages into the UPL... another thread in 'write' may
+        * have these pages in its UPL and be blocked trying to
+        * gain the write-behind lock for this vnode
         */
        lck_mtx_unlock(&wbp->cl_lockw);
 
        for (cl_index = 0; cl_index < cl_len; cl_index++) {
-               int flags;
-               struct cl_extent cl;
+               int     flags;
+               struct  cl_extent cl;
 
                /*
                 * try to push each cluster in turn...
                 */
-               if (l_clusters[cl_index].io_nocache)
+               if (l_clusters[cl_index].io_flags & CLW_IONOCACHE)
                        flags = IO_NOCACHE;
                else
                        flags = 0;
+
+               if ((l_clusters[cl_index].io_flags & CLW_IOPASSIVE) || (push_flag & IO_PASSIVE))
+                       flags |= IO_PASSIVE;
+
+               if (push_flag & PUSH_SYNC)
+                       flags |= IO_SYNC;
+
                cl.b_addr = l_clusters[cl_index].b_addr;
                cl.e_addr = l_clusters[cl_index].e_addr;
 
-               cluster_push_x(vp, &cl, EOF, flags);
+               cluster_push_now(vp, &cl, EOF, flags, callback, callback_arg);
 
                l_clusters[cl_index].b_addr = 0;
                l_clusters[cl_index].e_addr = 0;
 
                cl_pushed++;
 
-               if (push_all == 0)
+               if ( !(push_flag & PUSH_ALL) )
                        break;
        }
        lck_mtx_lock(&wbp->cl_lockw);
@@ -3940,14 +4566,14 @@ dont_try:
                         *
                         * collect the active public clusters...
                         */
-                       sparse_cluster_switch(wbp, vp, EOF);
+                       sparse_cluster_switch(wbp, vp, EOF, callback, callback_arg);
 
                        for (cl_index = 0, cl_index1 = 0; cl_index < cl_len; cl_index++) {
                                if (l_clusters[cl_index].b_addr == l_clusters[cl_index].e_addr)
                                        continue;
                                wbp->cl_clusters[cl_index1].b_addr = l_clusters[cl_index].b_addr;
                                wbp->cl_clusters[cl_index1].e_addr = l_clusters[cl_index].e_addr;
-                               wbp->cl_clusters[cl_index1].io_nocache = l_clusters[cl_index].io_nocache;
+                               wbp->cl_clusters[cl_index1].io_flags = l_clusters[cl_index].io_flags;
 
                                cl_index1++;
                        }
@@ -3960,7 +4586,7 @@ dont_try:
                         * and collect the original clusters that were moved into the 
                         * local storage for sorting purposes
                         */
-                       sparse_cluster_switch(wbp, vp, EOF);
+                       sparse_cluster_switch(wbp, vp, EOF, callback, callback_arg);
 
                } else {
                        /*
@@ -3974,7 +4600,7 @@ dont_try:
 
                                wbp->cl_clusters[cl_index1].b_addr = l_clusters[cl_index].b_addr;
                                wbp->cl_clusters[cl_index1].e_addr = l_clusters[cl_index].e_addr;
-                               wbp->cl_clusters[cl_index1].io_nocache = l_clusters[cl_index].io_nocache;
+                               wbp->cl_clusters[cl_index1].io_flags = l_clusters[cl_index].io_flags;
 
                                cl_index1++;
                        }
@@ -3984,13 +4610,13 @@ dont_try:
                        wbp->cl_number = cl_index1;
                }
        }
-       return(MAX_CLUSTERS - wbp->cl_number);
+       return (MAX_CLUSTERS - wbp->cl_number);
 }
 
 
 
 static int
-cluster_push_x(vnode_t vp, struct cl_extent *cl, off_t EOF, int flags)
+cluster_push_now(vnode_t vp, struct cl_extent *cl, off_t EOF, int flags, int (*callback)(buf_t, void *), void *callback_arg)
 {
        upl_page_info_t *pl;
        upl_t            upl;
@@ -4003,11 +4629,16 @@ cluster_push_x(vnode_t vp, struct cl_extent *cl, off_t EOF, int flags)
        int              io_size;
        int              io_flags;
        int              upl_flags;
+       int              bflag;
        int              size;
        int              error = 0;
        int              retval;
        kern_return_t    kret;
 
+       if (flags & IO_PASSIVE)
+           bflag = CL_PASSIVE;
+       else
+           bflag = 0;
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 51)) | DBG_FUNC_START,
                     (int)cl->b_addr, (int)cl->e_addr, (int)EOF, flags, 0);
@@ -4129,13 +4760,13 @@ cluster_push_x(vnode_t vp, struct cl_extent *cl, off_t EOF, int flags)
 
                io_size = min(size, (last_pg - start_pg) * PAGE_SIZE);
 
-               io_flags = CL_THROTTLE | CL_COMMIT;
+               io_flags = CL_THROTTLE | CL_COMMIT | CL_AGE | bflag;
 
                if ( !(flags & IO_SYNC))
                        io_flags |= CL_ASYNC;
 
                retval = cluster_io(vp, upl, upl_offset, upl_f_offset + upl_offset, io_size,
-                                   io_flags, (buf_t)NULL, (struct clios *)NULL);
+                                   io_flags, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
 
                if (error == 0 && retval)
                        error = retval;
@@ -4152,7 +4783,7 @@ cluster_push_x(vnode_t vp, struct cl_extent *cl, off_t EOF, int flags)
  * sparse_cluster_switch is called with the write behind lock held
  */
 static void
-sparse_cluster_switch(struct cl_writebehind *wbp, vnode_t vp, off_t EOF)
+sparse_cluster_switch(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int (*callback)(buf_t, void *), void *callback_arg)
 {
         int    cl_index;
 
@@ -4167,11 +4798,11 @@ sparse_cluster_switch(struct cl_writebehind *wbp, vnode_t vp, off_t EOF)
 
                for (cl.b_addr = wbp->cl_clusters[cl_index].b_addr; cl.b_addr < wbp->cl_clusters[cl_index].e_addr; cl.b_addr++) {
 
-                       if (ubc_page_op(vp, (off_t)(cl.b_addr * PAGE_SIZE_64), 0, 0, &flags) == KERN_SUCCESS) {
+                       if (ubc_page_op(vp, (off_t)(cl.b_addr * PAGE_SIZE_64), 0, NULL, &flags) == KERN_SUCCESS) {
                                if (flags & UPL_POP_DIRTY) {
                                        cl.e_addr = cl.b_addr + 1;
 
-                                       sparse_cluster_add(wbp, vp, &cl, EOF);
+                                       sparse_cluster_add(wbp, vp, &cl, EOF, callback, callback_arg);
                                }
                        }
                }
@@ -4186,15 +4817,15 @@ sparse_cluster_switch(struct cl_writebehind *wbp, vnode_t vp, off_t EOF)
  * sparse_cluster_push is called with the write behind lock held
  */
 static void
-sparse_cluster_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int push_all)
+sparse_cluster_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int push_flag, int (*callback)(buf_t, void *), void *callback_arg)
 {
         struct cl_extent cl;
         off_t          offset;
        u_int           length;
 
-       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 79)) | DBG_FUNC_START, (int)vp, (int)wbp->cl_scmap, wbp->cl_scdirty, push_all, 0);
+       KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 79)) | DBG_FUNC_START, (int)vp, (int)wbp->cl_scmap, wbp->cl_scdirty, push_flag, 0);
 
-       if (push_all)
+       if (push_flag & PUSH_ALL)
                vfs_drt_control(&(wbp->cl_scmap), 1);
 
        for (;;) {
@@ -4206,9 +4837,24 @@ sparse_cluster_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int push_
 
                wbp->cl_scdirty -= (int)(cl.e_addr - cl.b_addr);
 
-               cluster_push_x(vp, &cl, EOF, 0);
+               /*
+                * drop the lock while we're firing off the I/Os...
+                * this is safe since I've already updated the state
+                * this lock is protecting and I'm going to re-evaluate
+                * the public state after I retake the lock
+                *
+                * we need to drop it to avoid a lock inversion when trying to
+                * grab pages into the UPL... another thread in 'write' may
+                * have these pages in its UPL and be blocked trying to
+                * gain the write-behind lock for this vnode
+                */
+               lck_mtx_unlock(&wbp->cl_lockw);
+
+               cluster_push_now(vp, &cl, EOF, push_flag & IO_PASSIVE, callback, callback_arg);
+
+               lck_mtx_lock(&wbp->cl_lockw);
 
-               if (push_all == 0)
+               if ( !(push_flag & PUSH_ALL) )
                        break;
        }
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 79)) | DBG_FUNC_END, (int)vp, (int)wbp->cl_scmap, wbp->cl_scdirty, 0, 0);
@@ -4219,7 +4865,7 @@ sparse_cluster_push(struct cl_writebehind *wbp, vnode_t vp, off_t EOF, int push_
  * sparse_cluster_add is called with the write behind lock held
  */
 static void
-sparse_cluster_add(struct cl_writebehind *wbp, vnode_t vp, struct cl_extent *cl, off_t EOF)
+sparse_cluster_add(struct cl_writebehind *wbp, vnode_t vp, struct cl_extent *cl, off_t EOF, int (*callback)(buf_t, void *), void *callback_arg)
 {
         u_int  new_dirty;
        u_int   length;
@@ -4238,7 +4884,7 @@ sparse_cluster_add(struct cl_writebehind *wbp, vnode_t vp, struct cl_extent *cl,
                 */
                wbp->cl_scdirty += new_dirty;
 
-               sparse_cluster_push(wbp, vp, EOF, 0);
+               sparse_cluster_push(wbp, vp, EOF, 0, callback, callback_arg);
 
                offset += (new_dirty * PAGE_SIZE_64);
                length -= (new_dirty * PAGE_SIZE);
@@ -4250,7 +4896,7 @@ sparse_cluster_add(struct cl_writebehind *wbp, vnode_t vp, struct cl_extent *cl,
 
 
 static int
-cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, int xsize, int flags)
+cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, u_int32_t xsize, int flags, int (*callback)(buf_t, void *), void *callback_arg)
 {
         upl_page_info_t  *pl;
         upl_t            upl;
@@ -4260,17 +4906,30 @@ cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, int xsize
        int              did_read = 0;
        int              abort_flags;
        int              upl_flags;
+       int              bflag;
+
+       if (flags & IO_PASSIVE)
+           bflag = CL_PASSIVE;
+       else
+           bflag = 0;
 
        upl_flags = UPL_SET_LITE;
-       if (! (flags & CL_READ)) {
+
+       if ( !(flags & CL_READ) ) {
                /*
                 * "write" operation:  let the UPL subsystem know
                 * that we intend to modify the buffer cache pages
                 * we're gathering.
                 */
                upl_flags |= UPL_WILL_MODIFY;
+       } else {
+               /*
+                * indicate that there is no need to pull the
+                * mapping for this page... we're only going
+                * to read from it, not modify it.
+                */
+               upl_flags |= UPL_FILE_IO;
        }
-
         kret = ubc_create_upl(vp,
                               uio->uio_offset & ~PAGE_MASK_64,
                               PAGE_SIZE,
@@ -4286,7 +4945,7 @@ cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, int xsize
                  * issue a synchronous read to cluster_io
                  */
                 error = cluster_io(vp, upl, 0, uio->uio_offset & ~PAGE_MASK_64, PAGE_SIZE,
-                                  CL_READ, (buf_t)NULL, (struct clios *)NULL);
+                                  CL_READ | bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
                 if (error) {
                           ubc_upl_abort_range(upl, 0, PAGE_SIZE, UPL_ABORT_DUMP_PAGES | UPL_ABORT_FREE_ON_EMPTY);
 
@@ -4314,9 +4973,9 @@ cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, int xsize
                 * issue a synchronous write to cluster_io
                 */
                error = cluster_io(vp, upl, 0, uio->uio_offset & ~PAGE_MASK_64, PAGE_SIZE,
-                                       0, (buf_t)NULL, (struct clios *)NULL);
+                                  bflag, (buf_t)NULL, (struct clios *)NULL, callback, callback_arg);
        }
-       if (error == 0)
+       if (error == 0) 
                uio_update(uio, (user_size_t)xsize);
 
        if (did_read)
@@ -4332,17 +4991,20 @@ cluster_align_phys_io(vnode_t vp, struct uio *uio, addr64_t usr_paddr, int xsize
 
 
 int
-cluster_copy_upl_data(struct uio *uio, upl_t upl, int upl_offset, int xsize)
+cluster_copy_upl_data(struct uio *uio, upl_t upl, int upl_offset, int *io_resid)
 {
         int       pg_offset;
        int       pg_index;
         int      csize;
        int       segflg;
        int       retval = 0;
+       int       xsize;
        upl_page_info_t *pl;
 
+       xsize = *io_resid;
+
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_START,
-                    (int)uio->uio_offset, uio_resid(uio), upl_offset, xsize, 0);
+                    (int)uio->uio_offset, upl_offset, xsize, 0, 0);
 
        segflg = uio->uio_segflg;
 
@@ -4393,10 +5055,12 @@ cluster_copy_upl_data(struct uio *uio, upl_t upl, int upl_offset, int xsize)
                xsize    -= csize;
                csize     = min(PAGE_SIZE, xsize);
        }
+       *io_resid = xsize;
+
        uio->uio_segflg = segflg;
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
-                    (int)uio->uio_offset, uio_resid(uio), retval, segflg, 0);
+                    (int)uio->uio_offset, xsize, retval, segflg, 0);
 
        return (retval);
 }
@@ -4404,6 +5068,14 @@ cluster_copy_upl_data(struct uio *uio, upl_t upl, int upl_offset, int xsize)
 
 int
 cluster_copy_ubc_data(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty)
+{
+
+       return (cluster_copy_ubc_data_internal(vp, uio, io_resid, mark_dirty, 1));
+}
+
+
+static int
+cluster_copy_ubc_data_internal(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty, int take_reference)
 {
        int       segflg;
        int       io_size;
@@ -4412,14 +5084,16 @@ cluster_copy_ubc_data(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty
        int       retval = 0;
        memory_object_control_t  control;
 
+       io_size = *io_resid;
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_START,
-                    (int)uio->uio_offset, uio_resid(uio), 0, *io_resid, 0);
+                    (int)uio->uio_offset, 0, io_size, 0, 0);
 
        control = ubc_getobject(vp, UBC_FLAGS_NONE);
+
        if (control == MEMORY_OBJECT_CONTROL_NULL) {
                KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
-                            (int)uio->uio_offset, uio_resid(uio), retval, 3, 0);
+                            (int)uio->uio_offset, io_size, retval, 3, 0);
 
                return(0);
        }
@@ -4459,8 +5133,8 @@ cluster_copy_ubc_data(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty
                start_offset = (int)(uio->uio_offset & PAGE_MASK_64);
                xsize = uio_resid(uio);
 
-               retval = memory_object_control_uiomove(control, uio->uio_offset - start_offset,
-                                                      uio, start_offset, io_size, mark_dirty);
+               retval = memory_object_control_uiomove(control, uio->uio_offset - start_offset, uio,
+                                                      start_offset, io_size, mark_dirty, take_reference);
                xsize -= uio_resid(uio);
                io_size -= xsize;
        }
@@ -4468,7 +5142,7 @@ cluster_copy_ubc_data(vnode_t vp, struct uio *uio, int *io_resid, int mark_dirty
        *io_resid       = io_size;
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 34)) | DBG_FUNC_END,
-                    (int)uio->uio_offset, uio_resid(uio), retval, 0x80000000 | segflg, 0);
+                    (int)uio->uio_offset, io_size, retval, 0x80000000 | segflg, 0);
 
        return(retval);
 }
@@ -4482,7 +5156,7 @@ is_file_clean(vnode_t vp, off_t filesize)
        int   total_dirty = 0;
 
        for (f_offset = 0; f_offset < filesize; f_offset += PAGE_SIZE_64) {
-               if (ubc_page_op(vp, f_offset, 0, 0, &flags) == KERN_SUCCESS) {
+               if (ubc_page_op(vp, f_offset, 0, NULL, &flags) == KERN_SUCCESS) {
                        if (flags & UPL_POP_DIRTY) {
                                total_dirty++;
                        }
@@ -4669,7 +5343,7 @@ static kern_return_t      vfs_drt_do_mark_pages(
        void            **cmapp,
        u_int64_t       offset,
        u_int           length,
-       int             *setcountp,
+       u_int           *setcountp,
        int             dirty);
 static void            vfs_drt_trace(
        struct vfs_drt_clustermap *cmap,
@@ -4694,7 +5368,8 @@ vfs_drt_alloc_map(struct vfs_drt_clustermap **cmapp)
        struct vfs_drt_clustermap *cmap, *ocmap;
        kern_return_t   kret;
        u_int64_t       offset;
-       int             nsize, i, active_buckets, index, copycount;
+       u_int32_t       i;
+       int             nsize, active_buckets, index, copycount;
 
        ocmap = NULL;
        if (cmapp != NULL)
@@ -4773,6 +5448,7 @@ vfs_drt_alloc_map(struct vfs_drt_clustermap **cmapp)
                        if (kret != KERN_SUCCESS) {
                                /* XXX need to bail out gracefully here */
                                panic("vfs_drt: new cluster map mysteriously too small");
+                               index = 0;
                        }
                        /* copy */
                        DRT_HASH_COPY(ocmap, i, cmap, index);
@@ -4821,7 +5497,8 @@ vfs_drt_free_map(struct vfs_drt_clustermap *cmap)
 static kern_return_t
 vfs_drt_search_index(struct vfs_drt_clustermap *cmap, u_int64_t offset, int *indexp)
 {
-       int             index, i;
+       int             index;
+       u_int32_t       i;
 
        offset = DRT_ALIGN_ADDRESS(offset);
        index = DRT_HASH(cmap, offset);
@@ -4865,7 +5542,8 @@ vfs_drt_get_index(struct vfs_drt_clustermap **cmapp, u_int64_t offset, int *inde
 {
        struct vfs_drt_clustermap *cmap;
        kern_return_t   kret;
-       int             index, i;
+       u_int32_t       index;
+       u_int32_t       i;
 
        cmap = *cmapp;
 
@@ -4920,7 +5598,7 @@ vfs_drt_do_mark_pages(
        void            **private,
        u_int64_t       offset,
        u_int           length,
-       int             *setcountp,
+       u_int           *setcountp,
        int             dirty)
 {
        struct vfs_drt_clustermap *cmap, **cmapp;
@@ -5035,7 +5713,7 @@ vfs_drt_do_mark_pages(
  * Returns KERN_SUCCESS if all the pages were successfully marked.
  */
 static kern_return_t
-vfs_drt_mark_pages(void **cmapp, off_t offset, u_int length, int *setcountp)
+vfs_drt_mark_pages(void **cmapp, off_t offset, u_int length, u_int *setcountp)
 {
        /* XXX size unused, drop from interface */
        return(vfs_drt_do_mark_pages(cmapp, offset, length, setcountp, 1));
@@ -5075,7 +5753,8 @@ vfs_drt_get_cluster(void **cmapp, off_t *offsetp, u_int *lengthp)
        struct vfs_drt_clustermap *cmap;
        u_int64_t       offset;
        u_int           length;
-       int             index, i, j, fs, ls;
+       u_int32_t       j;
+       int             index, i, fs, ls;
 
        /* sanity */
        if ((cmapp == NULL) || (*cmapp == NULL))