]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/vfs/vfs_bio.c
xnu-1699.24.8.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_bio.c
index 026109d2133e32b5c9bce7f10de286194fdf1b05..0d474ed28733707be4778394b33babb91d0539a6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
 #include <kern/sched_prim.h>   /* thread_block() */
 
 #include <vm/vm_kern.h>
+#include <vm/vm_pageout.h>
 
 #include <sys/kdebug.h>
 
 #include <libkern/OSAtomic.h>
+#include <libkern/OSDebug.h>
 #include <sys/ubc_internal.h>
 
 #include <sys/sdt.h>
 
+
 #if BALANCE_QUEUES
 static __inline__ void bufqinc(int q);
 static __inline__ void bufqdec(int q);
 #endif
 
-static int     bcleanbuf(buf_t bp);
+int    bcleanbuf(buf_t bp, boolean_t discard);
 static int     brecover_data(buf_t bp);
 static boolean_t incore(vnode_t vp, daddr64_t blkno);
 /* timeout is in msecs */
@@ -123,6 +126,13 @@ static void        buf_reassign(buf_t bp, vnode_t newvp);
 static errno_t buf_acquire_locked(buf_t bp, int flags, int slpflag, int slptimeo);
 static int     buf_iterprepare(vnode_t vp, struct buflists *, int flags);
 static void    buf_itercomplete(vnode_t vp, struct buflists *, int flags);
+static boolean_t buffer_cache_gc(int);
+static buf_t   buf_brelse_shadow(buf_t bp);
+static void    buf_free_meta_store(buf_t bp);
+
+static buf_t   buf_create_shadow_internal(buf_t bp, boolean_t force_copy,
+                                          uintptr_t external_storage, void (*iodone)(buf_t, void *), void *arg, int priv);
+
 
 __private_extern__ int  bdwrite_internal(buf_t, int);
 
@@ -153,6 +163,7 @@ long nbdwrite = 0;
 int blaundrycnt = 0;
 static int boot_nbuf_headers = 0;
 
+static TAILQ_HEAD(delayqueue, buf) delaybufqueue;
 
 static TAILQ_HEAD(ioqueue, buf) iobufqueue;
 static TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
@@ -165,6 +176,8 @@ static lck_grp_attr_t   *buf_mtx_grp_attr;
 static lck_mtx_t       *iobuffer_mtxp;
 static lck_mtx_t       *buf_mtxp;
 
+static int buf_busycount;
+
 static __inline__ int
 buf_timestamp(void)
 {
@@ -226,7 +239,7 @@ int lru_is_stale = LRU_IS_STALE;
 int age_is_stale = AGE_IS_STALE;
 int meta_is_stale = META_IS_STALE;
 
-
+#define MAXLAUNDRY     10
 
 /* LIST_INSERT_HEAD() with assertions */
 static __inline__ void
@@ -273,7 +286,28 @@ bremhash(buf_t     bp)
        *bp->b_hash.le_prev = (bp)->b_hash.le_next;
 }
 
+/*
+ * buf_mtxp held.
+ */
+static __inline__ void
+bmovelaundry(buf_t bp)
+{
+       bp->b_whichq = BQ_LAUNDRY;
+       bp->b_timestamp = buf_timestamp();
+       binstailfree(bp, &bufqueues[BQ_LAUNDRY], BQ_LAUNDRY);
+       blaundrycnt++;
+}
 
+static __inline__ void
+buf_release_credentials(buf_t bp)
+{
+       if (IS_VALID_CRED(bp->b_rcred)) {
+               kauth_cred_unref(&bp->b_rcred);
+       }
+       if (IS_VALID_CRED(bp->b_wcred)) {
+               kauth_cred_unref(&bp->b_wcred);
+       }
+}
 
 
 int
@@ -304,12 +338,23 @@ buf_markdelayed(buf_t bp) {
        if (!ISSET(bp->b_flags, B_DELWRI)) {
                SET(bp->b_flags, B_DELWRI);
 
-               OSAddAtomic(1, &nbdwrite);
+               OSAddAtomicLong(1, &nbdwrite);
                buf_reassign(bp, bp->b_vp);
        }
         SET(bp->b_flags, B_DONE);
 }
 
+void
+buf_markclean(buf_t bp) {
+
+       if (ISSET(bp->b_flags, B_DELWRI)) {
+               CLR(bp->b_flags, B_DELWRI);
+
+               OSAddAtomicLong(-1, &nbdwrite);
+               buf_reassign(bp, bp->b_vp);
+       }
+}
+
 void
 buf_markeintr(buf_t bp) {
   
@@ -337,6 +382,29 @@ buf_markfua(buf_t bp) {
         SET(bp->b_flags, B_FUA);
 }
 
+#ifdef CONFIG_PROTECT
+void *
+buf_getcpaddr(buf_t bp) {
+       return bp->b_cpentry;
+}
+
+void 
+buf_setcpaddr(buf_t bp, void *cp_entry_addr) {
+       bp->b_cpentry = (struct cprotect *) cp_entry_addr;
+}
+
+#else
+void *
+buf_getcpaddr(buf_t bp __unused) {
+       return NULL;
+}
+
+void 
+buf_setcpaddr(buf_t bp __unused, void *cp_entry_addr __unused) {
+       return;
+}
+#endif /* CONFIG_PROTECT */
+
 errno_t
 buf_error(buf_t bp) {
         
@@ -543,15 +611,179 @@ buf_clone(buf_t bp, int io_offset, int io_size, void (*iodone)(buf_t, void *), v
 }
 
 
+int
+buf_shadow(buf_t bp)
+{
+       if (bp->b_lflags & BL_SHADOW)
+               return 1;
+       return 0;
+}
+
+
+buf_t
+buf_create_shadow_priv(buf_t bp, boolean_t force_copy, uintptr_t external_storage, void (*iodone)(buf_t, void *), void *arg)
+{
+       return (buf_create_shadow_internal(bp, force_copy, external_storage, iodone, arg, 1));
+}
+
+buf_t
+buf_create_shadow(buf_t bp, boolean_t force_copy, uintptr_t external_storage, void (*iodone)(buf_t, void *), void *arg)
+{
+       return (buf_create_shadow_internal(bp, force_copy, external_storage, iodone, arg, 0));
+}
+
+
+static buf_t
+buf_create_shadow_internal(buf_t bp, boolean_t force_copy, uintptr_t external_storage, void (*iodone)(buf_t, void *), void *arg, int priv)
+{
+        buf_t  io_bp;
+
+       KERNEL_DEBUG(0xbbbbc000 | DBG_FUNC_START, bp, 0, 0, 0, 0);
+
+       if ( !(bp->b_flags & B_META) || (bp->b_lflags & BL_IOBUF)) {
+
+               KERNEL_DEBUG(0xbbbbc000 | DBG_FUNC_END, bp, 0, 0, 0, 0);
+               return (NULL);
+       }
+#ifdef BUF_MAKE_PRIVATE
+       if (bp->b_shadow_ref && bp->b_data_ref == 0 && external_storage == 0)
+               panic("buf_create_shadow: %p is in the private state (%d, %d)", bp, bp->b_shadow_ref, bp->b_data_ref);
+#endif
+       io_bp = alloc_io_buf(bp->b_vp, priv);
+
+       io_bp->b_flags = bp->b_flags & (B_META | B_ZALLOC | B_ASYNC | B_READ | B_FUA);
+       io_bp->b_blkno = bp->b_blkno;
+       io_bp->b_lblkno = bp->b_lblkno;
+
+       if (iodone) {
+               io_bp->b_transaction = arg;
+               io_bp->b_iodone = iodone;
+               io_bp->b_flags |= B_CALL;
+       }
+       if (force_copy == FALSE) {
+               io_bp->b_bcount = bp->b_bcount;
+               io_bp->b_bufsize = bp->b_bufsize;
+
+               if (external_storage) {
+                       io_bp->b_datap = external_storage;
+#ifdef BUF_MAKE_PRIVATE
+                       io_bp->b_data_store = NULL;
+#endif
+               } else {
+                       io_bp->b_datap = bp->b_datap;
+#ifdef BUF_MAKE_PRIVATE
+                       io_bp->b_data_store = bp;
+#endif
+               }
+               *(buf_t *)(&io_bp->b_orig) = bp;
+
+               lck_mtx_lock_spin(buf_mtxp);
+
+               io_bp->b_lflags |= BL_SHADOW;
+               io_bp->b_shadow = bp->b_shadow;
+               bp->b_shadow = io_bp;
+               bp->b_shadow_ref++;
+
+#ifdef BUF_MAKE_PRIVATE
+               if (external_storage)
+                       io_bp->b_lflags |= BL_EXTERNAL;
+               else
+                       bp->b_data_ref++;
+#endif
+               lck_mtx_unlock(buf_mtxp);
+       } else {
+               if (external_storage) {
+#ifdef BUF_MAKE_PRIVATE
+                       io_bp->b_lflags |= BL_EXTERNAL;
+#endif
+                       io_bp->b_bcount = bp->b_bcount;
+                       io_bp->b_bufsize = bp->b_bufsize;
+                       io_bp->b_datap = external_storage;
+               } else {
+                       allocbuf(io_bp, bp->b_bcount);
+
+                       io_bp->b_lflags |= BL_IOBUF_ALLOC;
+               }
+               bcopy((caddr_t)bp->b_datap, (caddr_t)io_bp->b_datap, bp->b_bcount);
+
+#ifdef BUF_MAKE_PRIVATE
+               io_bp->b_data_store = NULL;
+#endif
+       }
+       KERNEL_DEBUG(0xbbbbc000 | DBG_FUNC_END, bp, bp->b_shadow_ref, 0, io_bp, 0);
+
+       return (io_bp);
+}
+
+
+#ifdef BUF_MAKE_PRIVATE
+errno_t
+buf_make_private(buf_t bp)
+{
+       buf_t   ds_bp;
+       buf_t   t_bp;
+       struct buf my_buf;
+
+       KERNEL_DEBUG(0xbbbbc004 | DBG_FUNC_START, bp, bp->b_shadow_ref, 0, 0, 0);
+
+       if (bp->b_shadow_ref == 0 || bp->b_data_ref == 0 || ISSET(bp->b_lflags, BL_SHADOW)) {
+
+               KERNEL_DEBUG(0xbbbbc004 | DBG_FUNC_END, bp, bp->b_shadow_ref, 0, EINVAL, 0);
+               return (EINVAL);
+       }
+       my_buf.b_flags = B_META;
+       my_buf.b_datap = (uintptr_t)NULL;
+       allocbuf(&my_buf, bp->b_bcount);
+
+       bcopy((caddr_t)bp->b_datap, (caddr_t)my_buf.b_datap, bp->b_bcount);
+
+       lck_mtx_lock_spin(buf_mtxp);
+
+       for (t_bp = bp->b_shadow; t_bp; t_bp = t_bp->b_shadow) {
+               if ( !ISSET(bp->b_lflags, BL_EXTERNAL))
+                       break;
+       }
+       ds_bp = t_bp;
+
+       if (ds_bp == NULL && bp->b_data_ref)
+               panic("buf_make_private: b_data_ref != 0 && ds_bp == NULL");
+
+       if (ds_bp && (bp->b_data_ref == 0 || bp->b_shadow_ref == 0))
+               panic("buf_make_private: ref_count == 0 && ds_bp != NULL");
+
+       if (ds_bp == NULL) {
+               lck_mtx_unlock(buf_mtxp);
+
+               buf_free_meta_store(&my_buf);
+
+               KERNEL_DEBUG(0xbbbbc004 | DBG_FUNC_END, bp, bp->b_shadow_ref, 0, EINVAL, 0);
+               return (EINVAL);
+       }
+       for (t_bp = bp->b_shadow; t_bp; t_bp = t_bp->b_shadow) {
+               if ( !ISSET(t_bp->b_lflags, BL_EXTERNAL))
+                       t_bp->b_data_store = ds_bp;
+       }
+       ds_bp->b_data_ref = bp->b_data_ref;
+
+       bp->b_data_ref = 0;
+       bp->b_datap = my_buf.b_datap;
+
+       lck_mtx_unlock(buf_mtxp);
+
+       KERNEL_DEBUG(0xbbbbc004 | DBG_FUNC_END, bp, bp->b_shadow_ref, 0, 0, 0);
+       return (0);
+}
+#endif
+
 
 void
 buf_setfilter(buf_t bp, void (*filter)(buf_t, void *), void *transaction,
-             void **old_iodone, void **old_transaction)
+                         void (**old_iodone)(buf_t, void *), void **old_transaction)
 {
-        if (old_iodone)
-               *old_iodone = (void *)(bp->b_iodone);
+       if (old_iodone)
+               *old_iodone = bp->b_iodone;
        if (old_transaction)
-               *old_transaction = (void *)(bp->b_transaction);
+               *old_transaction = bp->b_transaction;
 
        bp->b_transaction = transaction;
        bp->b_iodone = filter;
@@ -627,13 +859,13 @@ buf_setfsprivate(buf_t bp, void *fsprivate) {
         bp->b_fsprivate = fsprivate;
 }
 
-ucred_t
+kauth_cred_t
 buf_rcred(buf_t bp) {
 
         return (bp->b_rcred);
 }
 
-ucred_t
+kauth_cred_t
 buf_wcred(buf_t bp) {
 
         return (bp->b_wcred);
@@ -662,7 +894,7 @@ errno_t
 buf_map(buf_t bp, caddr_t *io_addr)
 {
         buf_t          real_bp;
-       vm_offset_t     vaddr;
+        vm_offset_t    vaddr;
         kern_return_t  kret;
 
         if ( !(bp->b_flags & B_CLUSTER)) {
@@ -745,8 +977,6 @@ buf_clear(buf_t bp) {
        bp->b_resid = 0;
 }
 
-
-
 /*
  * Read or write a buffer that is not contiguous on disk.
  * buffer is marked done/error at the conclusion
@@ -858,6 +1088,13 @@ buf_strategy(vnode_t devvp, void *ap)
        vnode_t vp = bp->b_vp;
        int     bmap_flags;
         errno_t error;
+#if CONFIG_DTRACE
+       int dtrace_io_start_flag = 0;    /* We only want to trip the io:::start
+                                         * probe once, with the true phisical
+                                         * block in place (b_blkno)
+                                         */
+
+#endif 
 
        if (vp == NULL || vp->v_type == VCHR || vp->v_type == VBLK)
                panic("buf_strategy: b_vp == NULL || vtype == VCHR | VBLK\n");
@@ -867,7 +1104,6 @@ buf_strategy(vnode_t devvp, void *ap)
         * end up issuing the I/O...
         */
        bp->b_dev = devvp->v_rdev;
-       DTRACE_IO1(start, buf_t, bp);
 
        if (bp->b_flags & B_READ)
                bmap_flags = VNODE_READ;
@@ -883,6 +1119,7 @@ buf_strategy(vnode_t devvp, void *ap)
                         * to deal with filesystem block sizes
                         * that aren't equal to the page size
                         */
+                       DTRACE_IO1(start, buf_t, bp);
                        return (cluster_bp(bp));
                }
                if (bp->b_blkno == bp->b_lblkno) {
@@ -890,27 +1127,53 @@ buf_strategy(vnode_t devvp, void *ap)
                        size_t  contig_bytes;
                  
                        if ((error = VNOP_BLKTOOFF(vp, bp->b_lblkno, &f_offset))) {
+                               DTRACE_IO1(start, buf_t, bp);
                                buf_seterror(bp, error);
                                buf_biodone(bp);
 
                                return (error);
                        }
                        if ((error = VNOP_BLOCKMAP(vp, f_offset, bp->b_bcount, &bp->b_blkno, &contig_bytes, NULL, bmap_flags, NULL))) {
+                               DTRACE_IO1(start, buf_t, bp);
                                buf_seterror(bp, error);
                                buf_biodone(bp);
 
                                return (error);
                        }
-                       if (bp->b_blkno == -1)
+                       
+                       DTRACE_IO1(start, buf_t, bp);
+#if CONFIG_DTRACE
+                       dtrace_io_start_flag = 1;
+#endif /* CONFIG_DTRACE */                     
+                       
+                       if ((bp->b_blkno == -1) || (contig_bytes == 0)) {
+                               /* Set block number to force biodone later */
+                               bp->b_blkno = -1;
                                buf_clear(bp);
-                       else if ((long)contig_bytes < bp->b_bcount)
+                       }
+                       else if ((long)contig_bytes < bp->b_bcount) {
                                return (buf_strategy_fragmented(devvp, bp, f_offset, contig_bytes));
+                       }
                }
+               
+#if CONFIG_DTRACE
+               if (dtrace_io_start_flag == 0) {
+                       DTRACE_IO1(start, buf_t, bp);
+                       dtrace_io_start_flag = 1;
+               }
+#endif /* CONFIG_DTRACE */
+               
                if (bp->b_blkno == -1) {
                        buf_biodone(bp);
                        return (0);
                }
        }
+
+#if CONFIG_DTRACE
+       if (dtrace_io_start_flag == 0)
+               DTRACE_IO1(start, buf_t, bp);
+#endif /* CONFIG_DTRACE */
+       
        /*
         * we can issue the I/O because...
         * either B_CLUSTER is set which
@@ -1038,10 +1301,15 @@ int
 buf_invalidateblks(vnode_t vp, int flags, int slpflag, int slptimeo)
 {
        buf_t   bp;
+       int     aflags;
        int     error = 0;
        int     must_rescan = 1;
        struct  buflists local_iterblkhd;
 
+
+       if (LIST_EMPTY(&vp->v_cleanblkhd) && LIST_EMPTY(&vp->v_dirtyblkhd))
+               return (0);
+
        lck_mtx_lock(buf_mtxp);
 
        for (;;) {
@@ -1064,6 +1332,7 @@ buf_invalidateblks(vnode_t vp, int flags, int slpflag, int slptimeo)
                        goto try_dirty_list;
                }
                while (!LIST_EMPTY(&local_iterblkhd)) {
+
                        bp = LIST_FIRST(&local_iterblkhd);
 
                        LIST_REMOVE(bp, b_vnbufs);
@@ -1075,7 +1344,12 @@ buf_invalidateblks(vnode_t vp, int flags, int slpflag, int slptimeo)
                        if ((flags & BUF_SKIP_META) && (bp->b_lblkno < 0 || ISSET(bp->b_flags, B_META)))
                                continue;
 
-                       if ( (error = (int)buf_acquire_locked(bp, BAC_REMOVE | BAC_SKIP_LOCKED, slpflag, slptimeo)) ) {
+                       aflags = BAC_REMOVE;
+
+                       if ( !(flags & BUF_INVALIDATE_LOCKED) )
+                               aflags |= BAC_SKIP_LOCKED;
+
+                       if ( (error = (int)buf_acquire_locked(bp, aflags, slpflag, slptimeo)) ) {
                                if (error == EDEADLK)
                                        /*      
                                         * this buffer was marked B_LOCKED... 
@@ -1103,6 +1377,10 @@ buf_invalidateblks(vnode_t vp, int flags, int slpflag, int slptimeo)
                        }
                        lck_mtx_unlock(buf_mtxp);
 
+                       if (bp->b_flags & B_LOCKED)
+                               KERNEL_DEBUG(0xbbbbc038, bp, 0, 0, 0, 0);
+
+                       CLR(bp->b_flags, B_LOCKED);
                        SET(bp->b_flags, B_INVAL);
                        buf_brelse(bp);
 
@@ -1137,7 +1415,12 @@ try_dirty_list:
                        if ((flags & BUF_SKIP_META) && (bp->b_lblkno < 0 || ISSET(bp->b_flags, B_META)))
                                continue;
 
-                       if ( (error = (int)buf_acquire_locked(bp, BAC_REMOVE | BAC_SKIP_LOCKED, slpflag, slptimeo)) ) {
+                       aflags = BAC_REMOVE;
+
+                       if ( !(flags & BUF_INVALIDATE_LOCKED) )
+                               aflags |= BAC_SKIP_LOCKED;
+
+                       if ( (error = (int)buf_acquire_locked(bp, aflags, slpflag, slptimeo)) ) {
                                if (error == EDEADLK)
                                        /*      
                                         * this buffer was marked B_LOCKED... 
@@ -1165,6 +1448,10 @@ try_dirty_list:
                        }
                        lck_mtx_unlock(buf_mtxp);
 
+                       if (bp->b_flags & B_LOCKED)
+                               KERNEL_DEBUG(0xbbbbc038, bp, 0, 0, 1, 0);
+
+                       CLR(bp->b_flags, B_LOCKED);
                        SET(bp->b_flags, B_INVAL);
 
                        if (ISSET(bp->b_flags, B_DELWRI) && (flags & BUF_WRITE_DATA))
@@ -1327,6 +1614,19 @@ bremfree_locked(buf_t bp)
 {
        struct bqueues *dp = NULL;
        int whichq;
+
+       whichq = bp->b_whichq;
+
+       if (whichq == -1) {
+               if (bp->b_shadow_ref == 0)
+                       panic("bremfree_locked: %p not on freelist", bp);
+               /*
+                * there are clones pointing to 'bp'...
+                * therefore, it was not put on a freelist
+                * when buf_brelse was last called on 'bp'
+                */
+               return;
+       }
        /*
         * We only calculate the head of the freelist when removing
         * the last element of the list as that is the only time that
@@ -1334,8 +1634,6 @@ bremfree_locked(buf_t bp)
         *
         * NB: This makes an assumption about how tailq's are implemented.
         */
-       whichq = bp->b_whichq;
-
        if (bp->b_freelist.tqe_next == NULL) {
                dp = &bufqueues[whichq];
 
@@ -1352,6 +1650,7 @@ bremfree_locked(buf_t bp)
 
        bp->b_whichq = -1;
        bp->b_timestamp = 0; 
+       bp->b_shadow = 0;
 }
 
 /*
@@ -1399,7 +1698,7 @@ brelvp_locked(buf_t bp)
 static void
 buf_reassign(buf_t bp, vnode_t newvp)
 {
-       register struct buflists *listheadp;
+       struct buflists *listheadp;
 
        if (newvp == NULL) {
                printf("buf_reassign: NULL");
@@ -1454,6 +1753,8 @@ bufinit(void)
                TAILQ_INIT(dp);
        bufhashtbl = hashinit(nbuf_hashelements, M_CACHE, &bufhash);
 
+       buf_busycount = 0;
+
        /* Initialize the buffer headers */
        for (i = 0; i < max_nbuf_headers; i++) {
                nbuf_headers++;
@@ -1467,8 +1768,11 @@ bufinit(void)
                binsheadfree(bp, dp, BQ_EMPTY);
                binshash(bp, &invalhash);
        }
-
        boot_nbuf_headers = nbuf_headers;
+
+       TAILQ_INIT(&iobufqueue);
+       TAILQ_INIT(&delaybufqueue);
+
        for (; i < nbuf_headers + niobuf_headers; i++) {
                bp = &buf_headers[i];
                bufhdrinit(bp);
@@ -1513,6 +1817,11 @@ bufinit(void)
        /* start the bcleanbuf() thread */
        bcleanbuf_thread_init();
 
+       /* Register a callout for relieving vm pressure */
+       if (vm_set_buffer_cleanup_callout(buffer_cache_gc) != KERN_SUCCESS) {
+               panic("Couldn't register buffer cache callout for vm pressure!\n");
+       }
+
 #if BALANCE_QUEUES
        {
        static void bufq_balance_thread_init(void) __attribute__((section("__TEXT, initcode")));
@@ -1561,8 +1870,10 @@ bufzoneinit(void)
                                        meta_zones[i].mz_max,
                                        PAGE_SIZE,
                                        meta_zones[i].mz_name);
+               zone_change(meta_zones[i].mz_zone, Z_CALLERACCT, FALSE);
        }
        buf_hdr_zone = zinit(sizeof(struct buf), 32, PAGE_SIZE, "buf headers");
+       zone_change(buf_hdr_zone, Z_CALLERACCT, FALSE);
 }
 
 static __inline__ zone_t
@@ -1584,7 +1895,7 @@ getbufzone(size_t size)
 
 
 static struct buf *
-bio_doread(vnode_t vp, daddr64_t blkno, int size, ucred_t cred, int async, int queuetype)
+bio_doread(vnode_t vp, daddr64_t blkno, int size, kauth_cred_t cred, int async, int queuetype)
 {
        buf_t   bp;
 
@@ -1613,7 +1924,7 @@ bio_doread(vnode_t vp, daddr64_t blkno, int size, ucred_t cred, int async, int q
 
                /* Pay for the read. */
                if (p && p->p_stats) 
-                       OSIncrementAtomic(&p->p_stats->p_ru.ru_inblock);                /* XXX */
+                       OSIncrementAtomicLong(&p->p_stats->p_ru.ru_inblock);            /* XXX */
 
                if (async) {
                        /*
@@ -1640,7 +1951,7 @@ bio_doread(vnode_t vp, daddr64_t blkno, int size, ucred_t cred, int async, int q
  */
 static errno_t
 do_breadn_for_type(vnode_t vp, daddr64_t blkno, int size, daddr64_t *rablks, int *rasizes, 
-                  int nrablks, ucred_t cred, buf_t *bpp, int queuetype)
+                  int nrablks, kauth_cred_t cred, buf_t *bpp, int queuetype)
 {
        buf_t   bp;
        int     i;
@@ -1669,7 +1980,7 @@ do_breadn_for_type(vnode_t vp, daddr64_t blkno, int size, daddr64_t *rablks, int
  * This algorithm described in Bach (p.54).
  */
 errno_t
-buf_bread(vnode_t vp, daddr64_t blkno, int size, ucred_t cred, buf_t *bpp)
+buf_bread(vnode_t vp, daddr64_t blkno, int size, kauth_cred_t cred, buf_t *bpp)
 {
        buf_t   bp;
 
@@ -1685,7 +1996,7 @@ buf_bread(vnode_t vp, daddr64_t blkno, int size, ucred_t cred, buf_t *bpp)
  * This algorithm described in Bach (p.54).
  */
 errno_t
-buf_meta_bread(vnode_t vp, daddr64_t blkno, int size, ucred_t cred, buf_t *bpp)
+buf_meta_bread(vnode_t vp, daddr64_t blkno, int size, kauth_cred_t cred, buf_t *bpp)
 {
        buf_t   bp;
 
@@ -1700,7 +2011,7 @@ buf_meta_bread(vnode_t vp, daddr64_t blkno, int size, ucred_t cred, buf_t *bpp)
  * Read-ahead multiple disk blocks. The first is sync, the rest async.
  */
 errno_t
-buf_breadn(vnode_t vp, daddr64_t blkno, int size, daddr64_t *rablks, int *rasizes, int nrablks, ucred_t cred, buf_t *bpp)
+buf_breadn(vnode_t vp, daddr64_t blkno, int size, daddr64_t *rablks, int *rasizes, int nrablks, kauth_cred_t cred, buf_t *bpp)
 {
        return (do_breadn_for_type(vp, blkno, size, rablks, rasizes, nrablks, cred, bpp, BLK_READ));
 }
@@ -1710,7 +2021,7 @@ buf_breadn(vnode_t vp, daddr64_t blkno, int size, daddr64_t *rablks, int *rasize
  * [buf_breadn() for meta-data]
  */
 errno_t
-buf_meta_breadn(vnode_t vp, daddr64_t blkno, int size, daddr64_t *rablks, int *rasizes, int nrablks, ucred_t cred, buf_t *bpp)
+buf_meta_breadn(vnode_t vp, daddr64_t blkno, int size, daddr64_t *rablks, int *rasizes, int nrablks, kauth_cred_t cred, buf_t *bpp)
 {
        return (do_breadn_for_type(vp, blkno, size, rablks, rasizes, nrablks, cred, bpp, BLK_META));
 }
@@ -1736,7 +2047,7 @@ buf_bwrite(buf_t bp)
        CLR(bp->b_flags, (B_READ | B_DONE | B_ERROR | B_DELWRI));
 
        if (wasdelayed)
-               OSAddAtomic(-1, &nbdwrite);
+               OSAddAtomicLong(-1, &nbdwrite);
 
        if (!sync) {
                /*
@@ -1749,7 +2060,7 @@ buf_bwrite(buf_t bp)
                        buf_reassign(bp, vp);
                else
                if (p && p->p_stats) 
-                       OSIncrementAtomic(&p->p_stats->p_ru.ru_oublock);        /* XXX */
+                       OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock);    /* XXX */
        }
        trace(TR_BUFWRITE, pack(vp, bp->b_bcount), bp->b_lblkno);
 
@@ -1774,7 +2085,7 @@ buf_bwrite(buf_t bp)
                        buf_reassign(bp, vp);
                else
                if (p && p->p_stats) 
-                       OSIncrementAtomic(&p->p_stats->p_ru.ru_oublock);        /* XXX */
+                       OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock);    /* XXX */
 
                /* Release the buffer. */
                // XXXdbg - only if the unused bit is set
@@ -1809,11 +2120,11 @@ vn_bwrite(struct vnop_bwrite_args *ap)
  *
  * Described in Leffler, et al. (pp. 208-213).
  *
- * Note: With the abilitty to allocate additional buffer
+ * Note: With the ability to allocate additional buffer
  * headers, we can get in to the situation where "too" many 
  * buf_bdwrite()s can create situation where the kernel can create
  * buffers faster than the disks can service. Doing a buf_bawrite() in
- * cases were we have "too many" outstanding buf_bdwrite()s avoids that.
+ * cases where we have "too many" outstanding buf_bdwrite()s avoids that.
  */
 __private_extern__ int
 bdwrite_internal(buf_t bp, int return_error)
@@ -1830,17 +2141,11 @@ bdwrite_internal(buf_t bp, int return_error)
        if (!ISSET(bp->b_flags, B_DELWRI)) {
                SET(bp->b_flags, B_DELWRI);
                if (p && p->p_stats) 
-                       OSIncrementAtomic(&p->p_stats->p_ru.ru_oublock);        /* XXX */
-               OSAddAtomic(1, &nbdwrite);
+                       OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock);    /* XXX */
+               OSAddAtomicLong(1, &nbdwrite);
                buf_reassign(bp, vp);
        }
 
-       /* If this is a tape block, write it the block now. */
-       if (ISSET(bp->b_flags, B_TAPE)) {
-               VNOP_BWRITE(bp);
-               return (0);
-       }
-
        /*
         * if we're not LOCKED, but the total number of delayed writes
         * has climbed above 75% of the total buffers in the system
@@ -1921,6 +2226,116 @@ buf_bawrite(buf_t bp)
 }
 
 
+
+static void
+buf_free_meta_store(buf_t bp)
+{
+       if (bp->b_bufsize) {
+               if (ISSET(bp->b_flags, B_ZALLOC)) {
+                       zone_t z;
+
+                       z = getbufzone(bp->b_bufsize);
+                       zfree(z, (void *)bp->b_datap);
+               } else
+                       kmem_free(kernel_map, bp->b_datap, bp->b_bufsize); 
+
+               bp->b_datap = (uintptr_t)NULL;
+               bp->b_bufsize = 0;
+       }
+}
+
+
+static buf_t
+buf_brelse_shadow(buf_t bp)
+{
+       buf_t   bp_head;
+       buf_t   bp_temp;
+       buf_t   bp_return = NULL;
+#ifdef BUF_MAKE_PRIVATE
+       buf_t   bp_data;
+       int     data_ref = 0;
+#endif
+       lck_mtx_lock_spin(buf_mtxp);
+
+       bp_head = (buf_t)bp->b_orig;
+
+       if (bp_head->b_whichq != -1)
+               panic("buf_brelse_shadow: bp_head on freelist %d\n", bp_head->b_whichq);
+
+#ifdef BUF_MAKE_PRIVATE
+       if (bp_data = bp->b_data_store) {
+               bp_data->b_data_ref--;
+               /*
+                * snapshot the ref count so that we can check it 
+                * outside of the lock... we only want the guy going
+                * from 1 -> 0 to try and release the storage
+                */
+               data_ref = bp_data->b_data_ref;
+       }
+#endif
+       KERNEL_DEBUG(0xbbbbc008 | DBG_FUNC_START, bp, bp_head, bp_head->b_shadow_ref, 0, 0);
+
+       bp_head->b_shadow_ref--;
+
+       for (bp_temp = bp_head; bp_temp && bp != bp_temp->b_shadow; bp_temp = bp_temp->b_shadow);
+
+       if (bp_temp == NULL)
+               panic("buf_brelse_shadow: bp not on list %p", bp_head);
+
+       bp_temp->b_shadow = bp_temp->b_shadow->b_shadow;
+
+#ifdef BUF_MAKE_PRIVATE
+       /*
+        * we're about to free the current 'owner' of the data buffer and
+        * there is at least one other shadow buf_t still pointing at it
+        * so transfer it to the first shadow buf left in the chain
+        */
+       if (bp == bp_data && data_ref) {
+               if ((bp_data = bp_head->b_shadow) == NULL)
+                       panic("buf_brelse_shadow: data_ref mismatch bp(%p)", bp);
+
+               for (bp_temp = bp_data; bp_temp; bp_temp = bp_temp->b_shadow)
+                       bp_temp->b_data_store = bp_data;
+               bp_data->b_data_ref = data_ref;
+       }
+#endif
+       if (bp_head->b_shadow_ref == 0 && bp_head->b_shadow)
+               panic("buf_relse_shadow: b_shadow != NULL && b_shadow_ref == 0  bp(%p)", bp); 
+       if (bp_head->b_shadow_ref && bp_head->b_shadow == 0)
+               panic("buf_relse_shadow: b_shadow == NULL && b_shadow_ref != 0  bp(%p)", bp); 
+
+       if (bp_head->b_shadow_ref == 0) {
+               if (!ISSET(bp_head->b_lflags, BL_BUSY)) {
+
+                       CLR(bp_head->b_flags, B_AGE);
+                       bp_head->b_timestamp = buf_timestamp();
+
+                       if (ISSET(bp_head->b_flags, B_LOCKED)) {
+                               bp_head->b_whichq = BQ_LOCKED;
+                               binstailfree(bp_head, &bufqueues[BQ_LOCKED], BQ_LOCKED);
+                       } else {
+                               bp_head->b_whichq = BQ_META;
+                               binstailfree(bp_head, &bufqueues[BQ_META], BQ_META);
+                       }
+               } else if (ISSET(bp_head->b_lflags, BL_WAITSHADOW)) {
+                       CLR(bp_head->b_lflags, BL_WAITSHADOW);
+
+                       bp_return = bp_head;
+               }
+       }
+       lck_mtx_unlock(buf_mtxp);
+#ifdef BUF_MAKE_PRIVATE        
+       if (bp == bp_data && data_ref == 0)
+               buf_free_meta_store(bp);
+
+       bp->b_data_store = NULL;
+#endif
+       KERNEL_DEBUG(0xbbbbc008 | DBG_FUNC_END, bp, 0, 0, 0, 0);
+
+       return (bp_return);
+}
+
+
 /*
  * Release a buffer on to the free lists.
  * Described in Bach (p. 46).
@@ -1939,23 +2354,29 @@ buf_brelse(buf_t bp)
                panic("buf_brelse: bad buffer = %p\n", bp);
 
 #ifdef JOE_DEBUG
-       bp->b_stackbrelse[0] = (int)__builtin_return_address(0);
-       bp->b_stackbrelse[1] = (int)__builtin_return_address(1);
-       bp->b_stackbrelse[2] = (int)__builtin_return_address(2);
-       bp->b_stackbrelse[3] = (int)__builtin_return_address(3);
-       bp->b_stackbrelse[4] = (int)__builtin_return_address(4);
-       bp->b_stackbrelse[5] = (int)__builtin_return_address(5);
+       (void) OSBacktrace(&bp->b_stackbrelse[0], 6);
 
        bp->b_lastbrelse = current_thread();
        bp->b_tag = 0;
 #endif
        if (bp->b_lflags & BL_IOBUF) {
+               buf_t   shadow_master_bp = NULL;
+
+               if (ISSET(bp->b_lflags, BL_SHADOW))
+                       shadow_master_bp = buf_brelse_shadow(bp);
+               else if (ISSET(bp->b_lflags, BL_IOBUF_ALLOC))
+                        buf_free_meta_store(bp);
                free_io_buf(bp);
+
+               if (shadow_master_bp) {
+                       bp = shadow_master_bp;
+                       goto finish_shadow_master;
+               }
                return;
        }
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 388)) | DBG_FUNC_START,
-                    bp->b_lblkno * PAGE_SIZE, (int)bp, (int)bp->b_datap,
+                    bp->b_lblkno * PAGE_SIZE, bp, bp->b_datap,
                     bp->b_flags, 0);
 
        trace(TR_BRELSE, pack(bp->b_vp, bp->b_bufsize), bp->b_lblkno);
@@ -1970,7 +2391,7 @@ buf_brelse(buf_t bp)
        if (ISSET(bp->b_flags, B_META) && ISSET(bp->b_flags, B_INVAL)) {
                if (ISSET(bp->b_flags, B_FILTER)) {     /* if necessary, call out */
                        void    (*iodone_func)(struct buf *, void *) = bp->b_iodone;
-                       void    *arg = (void *)bp->b_transaction;
+                       void    *arg = bp->b_transaction;
 
                        CLR(bp->b_flags, B_FILTER);     /* but note callout done */
                        bp->b_iodone = NULL;
@@ -1991,7 +2412,7 @@ buf_brelse(buf_t bp)
                kern_return_t kret;
                int           upl_flags;
 
-               if ( (upl == NULL) ) {
+               if (upl == NULL) {
                        if ( !ISSET(bp->b_flags, B_INVAL)) {
                                kret = ubc_create_upl(bp->b_vp, 
                                                      ubc_blktooff(bp->b_vp, bp->b_lblkno),
@@ -2002,8 +2423,8 @@ buf_brelse(buf_t bp)
 
                                if (kret != KERN_SUCCESS)
                                        panic("brelse: Failed to create UPL");
-#ifdef  UPL_DEBUG
-                               upl_ubc_alias_set(upl, bp, 5);
+#if  UPL_DEBUG
+                               upl_ubc_alias_set(upl, (uintptr_t) bp, (uintptr_t) 5);
 #endif /* UPL_DEBUG */
                        }
                } else {
@@ -2050,44 +2471,49 @@ buf_brelse(buf_t bp)
        if (ISSET(bp->b_flags, (B_NOCACHE|B_ERROR)))
                SET(bp->b_flags, B_INVAL);
        
-       if ((bp->b_bufsize <= 0) || ISSET(bp->b_flags, B_INVAL)) {
+       if ((bp->b_bufsize <= 0) || 
+                       ISSET(bp->b_flags, B_INVAL) || 
+                       (ISSET(bp->b_lflags, BL_WANTDEALLOC) && !ISSET(bp->b_flags, B_DELWRI))) {
+
+               boolean_t       delayed_buf_free_meta_store = FALSE;
+
                /*
                 * If it's invalid or empty, dissociate it from its vnode,
                 * release its storage if B_META, and
                 * clean it up a bit and put it on the EMPTY queue
                 */
                if (ISSET(bp->b_flags, B_DELWRI))
-                       OSAddAtomic(-1, &nbdwrite);
+                       OSAddAtomicLong(-1, &nbdwrite);
 
                if (ISSET(bp->b_flags, B_META)) {
-                       if (bp->b_bufsize) {
-                               if (ISSET(bp->b_flags, B_ZALLOC)) {
-                                       zone_t z;
-
-                                       z = getbufzone(bp->b_bufsize);
-                                       zfree(z, (void *)bp->b_datap);
-                               } else
-                                       kmem_free(kernel_map, bp->b_datap, bp->b_bufsize); 
-
-                                bp->b_datap = (uintptr_t)NULL;
-                                bp->b_bufsize = 0;
-                       }
+                       if (bp->b_shadow_ref)
+                               delayed_buf_free_meta_store = TRUE;
+                       else
+                               buf_free_meta_store(bp);
                }
                /*
                 * nuke any credentials we were holding
                 */
-               if (IS_VALID_CRED(bp->b_rcred)) {
-                       kauth_cred_unref(&bp->b_rcred);
-               }
-               if (IS_VALID_CRED(bp->b_wcred)) {
-                       kauth_cred_unref(&bp->b_wcred);
+               buf_release_credentials(bp);
+
+               lck_mtx_lock_spin(buf_mtxp);
+
+               if (bp->b_shadow_ref) {
+                       SET(bp->b_lflags, BL_WAITSHADOW);
+                       
+                       lck_mtx_unlock(buf_mtxp);
+                       
+                       return;
                }
-               CLR(bp->b_flags, (B_META | B_ZALLOC | B_DELWRI | B_LOCKED | B_AGE | B_ASYNC | B_NOCACHE | B_FUA));
+               if (delayed_buf_free_meta_store == TRUE) {
 
-               bufq = &bufqueues[BQ_EMPTY];
-               bp->b_whichq = BQ_EMPTY;
+                       lck_mtx_unlock(buf_mtxp);
+finish_shadow_master:
+                       buf_free_meta_store(bp);
 
-               lck_mtx_lock_spin(buf_mtxp);
+                       lck_mtx_lock_spin(buf_mtxp);
+               }
+               CLR(bp->b_flags, (B_META | B_ZALLOC | B_DELWRI | B_LOCKED | B_AGE | B_ASYNC | B_NOCACHE | B_FUA));
 
                if (bp->b_vp)
                        brelvp_locked(bp);
@@ -2096,8 +2522,10 @@ buf_brelse(buf_t bp)
                BLISTNONE(bp);
                binshash(bp, &invalhash);
 
-               binsheadfree(bp, bufq, BQ_EMPTY);
+               bp->b_whichq = BQ_EMPTY;
+               binsheadfree(bp, &bufqueues[BQ_EMPTY], BQ_EMPTY);
        } else {
+
                /*
                 * It has valid data.  Put it on the end of the appropriate
                 * queue, so that it'll stick around for as long as possible.
@@ -2112,13 +2540,32 @@ buf_brelse(buf_t bp)
                        whichq = BQ_LRU;                /* valid data */
                bufq = &bufqueues[whichq];
 
-               CLR(bp->b_flags, (B_AGE | B_ASYNC | B_NOCACHE));
-               bp->b_whichq = whichq;
                bp->b_timestamp = buf_timestamp();
 
-               lck_mtx_lock_spin(buf_mtxp);
-
-               binstailfree(bp, bufq, whichq);
+               lck_mtx_lock_spin(buf_mtxp);
+               
+               /*
+                * the buf_brelse_shadow routine doesn't take 'ownership'
+                * of the parent buf_t... it updates state that is protected by
+                * the buf_mtxp, and checks for BL_BUSY to determine whether to
+                * put the buf_t back on a free list.  b_shadow_ref is protected
+                * by the lock, and since we have not yet cleared B_BUSY, we need
+                * to check it while holding the lock to insure that one of us
+                * puts this buf_t back on a free list when it is safe to do so
+                */
+               if (bp->b_shadow_ref == 0) {
+                       CLR(bp->b_flags, (B_AGE | B_ASYNC | B_NOCACHE));
+                       bp->b_whichq = whichq;
+                       binstailfree(bp, bufq, whichq);
+               } else {
+                       /*
+                        * there are still cloned buf_t's pointing
+                        * at this guy... need to keep it off the
+                        * freelists until a buf_brelse is done on 
+                        * the last clone
+                        */
+                       CLR(bp->b_flags, (B_ASYNC | B_NOCACHE));
+               }
        }
        if (needbuffer) {
                /*
@@ -2141,6 +2588,7 @@ buf_brelse(buf_t bp)
         * Unlock the buffer.
         */
        CLR(bp->b_lflags, (BL_BUSY | BL_WANTED));
+       buf_busycount--;
 
        lck_mtx_unlock(buf_mtxp);
 
@@ -2157,7 +2605,7 @@ buf_brelse(buf_t bp)
                wakeup(bp);
        }
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 388)) | DBG_FUNC_END,
-                    (int)bp, (int)bp->b_datap, bp->b_flags, 0, 0);
+                    bp, bp->b_datap, bp->b_flags, 0, 0);
 }
 
 /*
@@ -2226,14 +2674,14 @@ buf_getblk(vnode_t vp, daddr64_t blkno, int size, int slpflag, int slptimeo, int
        struct  bufhashhdr *dp;
 
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 386)) | DBG_FUNC_START,
-                    (int)(blkno * PAGE_SIZE), size, operation, 0, 0);
+                    (uintptr_t)(blkno * PAGE_SIZE), size, operation, 0, 0);
 
        ret_only_valid = operation & BLK_ONLYVALID;
        operation &= ~BLK_ONLYVALID;
        dp = BUFHASH(vp, blkno);
 start:
        lck_mtx_lock_spin(buf_mtxp);
-start_locked:
+
        if ((bp = incore_locked(vp, blkno, dp))) {
                /*
                 * Found in the Buffer Cache
@@ -2249,7 +2697,6 @@ start_locked:
                                SET(bp->b_lflags, BL_WANTED);
                                bufstats.bufs_busyincore++;
 
-                               lck_mtx_convert_spin(buf_mtxp);
                                /*
                                 * don't retake the mutex after being awakened...
                                 * the time out is in msecs 
@@ -2257,6 +2704,9 @@ start_locked:
                                ts.tv_sec = (slptimeo/1000);
                                ts.tv_nsec = (slptimeo % 1000) * 10  * NSEC_PER_USEC * 1000;
 
+                               KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 396)) | DBG_FUNC_NONE,
+                                            (uintptr_t)blkno, size, operation, 0, 0);
+
                                err = msleep(bp, buf_mtxp, slpflag | PDROP | (PRIBIO + 1), "buf_getblk", &ts);
 
                                /*
@@ -2283,6 +2733,7 @@ start_locked:
                         */
                        SET(bp->b_lflags, BL_BUSY);
                        SET(bp->b_flags, B_CACHE);
+                       buf_busycount++;
 
                        bremfree_locked(bp);
                        bufstats.bufs_incore++;
@@ -2329,7 +2780,7 @@ start_locked:
                                        } else 
                                                CLR(bp->b_flags, (B_DONE | B_CACHE | B_WASDIRTY | B_DELWRI));
 
-                                       kret = ubc_upl_map(upl, (vm_address_t *)&(bp->b_datap));
+                                       kret = ubc_upl_map(upl, (vm_offset_t*)&(bp->b_datap));
 
                                        if (kret != KERN_SUCCESS)
                                                panic("getblk: ubc_upl_map() failed with (%d)", kret);
@@ -2356,13 +2807,11 @@ start_locked:
                        lck_mtx_unlock(buf_mtxp);
                        return (NULL);
                }
-               lck_mtx_convert_spin(buf_mtxp);
-
                if ((vnode_isreg(vp) == 0) || (UBCINFOEXISTS(vp) == 0) /*|| (vnode_issystem(vp) == 1)*/)
                        operation = BLK_META;
 
                if ((bp = getnewbuf(slpflag, slptimeo, &queue)) == NULL)
-                       goto start_locked;
+                       goto start;
 
                /*
                 * getnewbuf may block for a number of different reasons...
@@ -2418,7 +2867,7 @@ start_locked:
                         * in bufstats are protected with either
                         * buf_mtxp or iobuffer_mtxp
                         */
-                       OSAddAtomic(1, &bufstats.bufs_miss);
+                       OSAddAtomicLong(1, &bufstats.bufs_miss);
                        break;
 
                case BLK_WRITE:
@@ -2448,8 +2897,8 @@ start_locked:
 
                        if (kret != KERN_SUCCESS)
                                panic("Failed to create UPL");
-#ifdef  UPL_DEBUG
-                       upl_ubc_alias_set(upl, bp, 4);
+#if  UPL_DEBUG
+                       upl_ubc_alias_set(upl, (uintptr_t) bp, (uintptr_t) 4);
 #endif /* UPL_DEBUG */
                        bp->b_upl = upl;
 
@@ -2462,7 +2911,7 @@ start_locked:
 
                                SET(bp->b_flags, B_CACHE | B_DONE);
 
-                               OSAddAtomic(1, &bufstats.bufs_vmhits);
+                               OSAddAtomicLong(1, &bufstats.bufs_vmhits);
 
                                bp->b_validoff = 0;
                                bp->b_dirtyoff = 0;
@@ -2493,9 +2942,9 @@ start_locked:
                                if ((long)contig_bytes < bp->b_bcount)
                                        bp->b_blkno = bp->b_lblkno;
                        } else {
-                               OSAddAtomic(1, &bufstats.bufs_miss);
+                               OSAddAtomicLong(1, &bufstats.bufs_miss);
                        }
-                       kret = ubc_upl_map(upl, (vm_address_t *)&(bp->b_datap));
+                       kret = ubc_upl_map(upl, (vm_offset_t *)&(bp->b_datap));
 
                        if (kret != KERN_SUCCESS)
                                panic("getblk: ubc_upl_map() failed with (%d)", kret);
@@ -2508,15 +2957,10 @@ start_locked:
                }
        }
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 386)) | DBG_FUNC_END,
-                    (int)bp, (int)bp->b_datap, bp->b_flags, 3, 0);
+                    bp, bp->b_datap, bp->b_flags, 3, 0);
 
 #ifdef JOE_DEBUG
-       bp->b_stackgetblk[0] = (int)__builtin_return_address(0);
-       bp->b_stackgetblk[1] = (int)__builtin_return_address(1);
-       bp->b_stackgetblk[2] = (int)__builtin_return_address(2);
-       bp->b_stackgetblk[3] = (int)__builtin_return_address(3);
-       bp->b_stackgetblk[4] = (int)__builtin_return_address(4);
-       bp->b_stackgetblk[5] = (int)__builtin_return_address(5);
+       (void) OSBacktrace(&bp->b_stackgetblk[0], 6);
 #endif
        return (bp);
 }
@@ -2527,13 +2971,15 @@ start_locked:
 buf_t
 buf_geteblk(int size)
 {
-       buf_t   bp;
+       buf_t   bp = NULL;
        int queue = BQ_EMPTY;
 
-       lck_mtx_lock(buf_mtxp);
+       do {
+               lck_mtx_lock_spin(buf_mtxp);
+
+               bp = getnewbuf(0, 0, &queue);
+       } while (bp == NULL);
 
-       while ((bp = getnewbuf(0, 0, &queue)) == 0)
-               ;
        SET(bp->b_flags, (B_META|B_INVAL));
 
 #if DIAGNOSTIC
@@ -2551,6 +2997,23 @@ buf_geteblk(int size)
        return (bp);
 }
 
+uint32_t
+buf_redundancy_flags(buf_t bp)
+{
+       return bp->b_redundancy_flags;
+}
+
+void
+buf_set_redundancy_flags(buf_t bp, uint32_t flags)
+{
+       SET(bp->b_redundancy_flags, flags);
+}
+
+void
+buf_clear_redundancy_flags(buf_t bp, uint32_t flags)
+{
+       CLR(bp->b_redundancy_flags, flags);
+}
 
 /*
  * With UBC, there is no need to expand / shrink the file data 
@@ -2595,7 +3058,7 @@ allocbuf(buf_t bp, int size)
                                                *(void **)(&bp->b_datap) = zalloc(z);
                                        } else {
                                                bp->b_datap = (uintptr_t)NULL;
-                                               kmem_alloc_wired(kernel_map, (vm_offset_t *)&bp->b_datap, desired_size);
+                                               kmem_alloc_kobject(kernel_map, (vm_offset_t *)&bp->b_datap, desired_size);
                                                CLR(bp->b_flags, B_ZALLOC);
                                        }
                                        bcopy((void *)elem, (caddr_t)bp->b_datap, bp->b_bufsize);
@@ -2608,7 +3071,7 @@ allocbuf(buf_t bp, int size)
                                if ((vm_size_t)bp->b_bufsize < desired_size) {
                                        /* reallocate to a bigger size */
                                        bp->b_datap = (uintptr_t)NULL;
-                                       kmem_alloc_wired(kernel_map, (vm_offset_t *)&bp->b_datap, desired_size);
+                                       kmem_alloc_kobject(kernel_map, (vm_offset_t *)&bp->b_datap, desired_size);
                                        bcopy((const void *)elem, (caddr_t)bp->b_datap, bp->b_bufsize);
                                        kmem_free(kernel_map, elem, bp->b_bufsize); 
                                } else {
@@ -2624,7 +3087,7 @@ allocbuf(buf_t bp, int size)
                                *(void **)(&bp->b_datap) = zalloc(z);
                                SET(bp->b_flags, B_ZALLOC);
                        } else
-                               kmem_alloc_wired(kernel_map, (vm_offset_t *)&bp->b_datap, desired_size);
+                               kmem_alloc_kobject(kernel_map, (vm_offset_t *)&bp->b_datap, desired_size);
                }
 
                if (bp->b_datap == 0)
@@ -2654,7 +3117,8 @@ allocbuf(buf_t bp, int size)
  *     on which it was found.
  *
  *     buf_mtxp is held upon entry
- *     returns with buf_mtxp locked
+ *     returns with buf_mtxp locked if new buf available
+ *     returns with buf_mtxp UNlocked if new buf NOT available
  */
 
 static buf_t
@@ -2732,7 +3196,7 @@ add_newbufs:
                        SET(bp->b_flags, B_HDRALLOC);
                        *queue = BQ_EMPTY;
                }
-               lck_mtx_lock(buf_mtxp);
+               lck_mtx_lock_spin(buf_mtxp);
 
                if (bp) {
                        binshash(bp, &invalhash);
@@ -2751,7 +3215,8 @@ add_newbufs:
                ts.tv_sec = (slptimeo/1000);
                /* the hz value is 100; which leads to 10ms */
                ts.tv_nsec = (slptimeo % 1000) * NSEC_PER_USEC * 1000 * 10;
-               msleep(&needbuffer, buf_mtxp, slpflag|(PRIBIO+1), "getnewbuf", &ts);
+
+               msleep(&needbuffer, buf_mtxp, slpflag | PDROP | (PRIBIO+1), "getnewbuf", &ts);
                return (NULL);
        }
 
@@ -2813,10 +3278,10 @@ add_newbufs:
        }
 found:
        if (ISSET(bp->b_flags, B_LOCKED) || ISSET(bp->b_lflags, BL_BUSY))
-               panic("getnewbuf: bp @ %p is LOCKED or BUSY! (flags 0x%lx)\n", bp, bp->b_flags);
+               panic("getnewbuf: bp @ %p is LOCKED or BUSY! (flags 0x%x)\n", bp, bp->b_flags);
 
        /* Clean it */
-       if (bcleanbuf(bp)) {
+       if (bcleanbuf(bp, FALSE)) {
                /*
                 * moved to the laundry thread, buffer not ready
                 */
@@ -2829,15 +3294,15 @@ found:
 
 /* 
  * Clean a buffer.
- * Returns 0 is buffer is ready to use,
+ * Returns 0 if buffer is ready to use,
  * Returns 1 if issued a buf_bawrite() to indicate 
  * that the buffer is not ready.
  * 
  * buf_mtxp is held upon entry
  * returns with buf_mtxp locked
  */
-static int
-bcleanbuf(buf_t bp)
+int
+bcleanbuf(buf_t bp, boolean_t discard)
 {
        /* Remove from the queue */
        bremfree_locked(bp);
@@ -2851,10 +3316,11 @@ bcleanbuf(buf_t bp)
         * it on the LAUNDRY queue, and return 1
         */
        if (ISSET(bp->b_flags, B_DELWRI)) {
-               bp->b_whichq = BQ_LAUNDRY;
-               bp->b_timestamp = buf_timestamp();
-               binstailfree(bp, &bufqueues[BQ_LAUNDRY], BQ_LAUNDRY);
-               blaundrycnt++;
+               if (discard) {
+                       SET(bp->b_lflags, BL_WANTDEALLOC);
+               }
+
+               bmovelaundry(bp);
 
                lck_mtx_unlock(buf_mtxp);
 
@@ -2864,7 +3330,7 @@ bcleanbuf(buf_t bp)
                 */
                (void)thread_block(THREAD_CONTINUE_NULL);
 
-               lck_mtx_lock(buf_mtxp);
+               lck_mtx_lock_spin(buf_mtxp);
 
                return (1);
        }
@@ -2876,7 +3342,8 @@ bcleanbuf(buf_t bp)
         * Buffer is no longer on any free list... we own it
         */
        SET(bp->b_lflags, BL_BUSY);
-
+       buf_busycount++;
+       
        bremhash(bp);
 
        /*
@@ -2889,57 +3356,53 @@ bcleanbuf(buf_t bp)
 
        BLISTNONE(bp);
 
-       if (ISSET(bp->b_flags, B_META)) {
-               vm_offset_t elem;
-
-               elem = (vm_offset_t)bp->b_datap;
-               bp->b_datap = (uintptr_t)0xdeadbeef;
-
-               if (ISSET(bp->b_flags, B_ZALLOC)) {
-                       zone_t z;
-
-                       z = getbufzone(bp->b_bufsize);
-                       zfree(z, (void *)elem);
-               } else
-                       kmem_free(kernel_map, elem, bp->b_bufsize); 
-       }
+       if (ISSET(bp->b_flags, B_META))
+               buf_free_meta_store(bp);
 
        trace(TR_BRELSE, pack(bp->b_vp, bp->b_bufsize), bp->b_lblkno);
 
-       /* clear out various other fields */
-       bp->b_bufsize = 0;
-       bp->b_datap = (uintptr_t)NULL;
-       bp->b_upl = (void *)NULL;
-       /*
-        * preserve the state of whether this buffer
-        * was allocated on the fly or not...
-        * the only other flag that should be set at
-        * this point is BL_BUSY...
-        */
+       buf_release_credentials(bp);
+
+       /* If discarding, just move to the empty queue */
+       if (discard) {
+               lck_mtx_lock_spin(buf_mtxp);
+               CLR(bp->b_flags, (B_META | B_ZALLOC | B_DELWRI | B_LOCKED | B_AGE | B_ASYNC | B_NOCACHE | B_FUA));
+               bp->b_whichq = BQ_EMPTY;
+               binshash(bp, &invalhash);
+               binsheadfree(bp, &bufqueues[BQ_EMPTY], BQ_EMPTY);
+               CLR(bp->b_lflags, BL_BUSY);
+               buf_busycount--;
+       } else {
+               /* Not discarding: clean up and prepare for reuse */
+               bp->b_bufsize = 0;
+               bp->b_datap = (uintptr_t)NULL;
+               bp->b_upl = (void *)NULL;
+               /*
+                * preserve the state of whether this buffer
+                * was allocated on the fly or not...
+                * the only other flag that should be set at
+                * this point is BL_BUSY...
+                */
 #ifdef JOE_DEBUG
-       bp->b_owner = current_thread();
-       bp->b_tag   = 3;
+               bp->b_owner = current_thread();
+               bp->b_tag   = 3;
+#endif
+               bp->b_lflags = BL_BUSY;
+               bp->b_flags = (bp->b_flags & B_HDRALLOC);
+               bp->b_dev = NODEV;
+               bp->b_blkno = bp->b_lblkno = 0;
+               bp->b_iodone = NULL;
+               bp->b_error = 0;
+               bp->b_resid = 0;
+               bp->b_bcount = 0;
+               bp->b_dirtyoff = bp->b_dirtyend = 0;
+               bp->b_validoff = bp->b_validend = 0;
+#ifdef CONFIG_PROTECT
+               bp->b_cpentry = 0;
 #endif
-       bp->b_lflags = BL_BUSY;
-       bp->b_flags = (bp->b_flags & B_HDRALLOC);
-       bp->b_dev = NODEV;
-       bp->b_blkno = bp->b_lblkno = 0;
-       bp->b_iodone = NULL;
-       bp->b_error = 0;
-       bp->b_resid = 0;
-       bp->b_bcount = 0;
-       bp->b_dirtyoff = bp->b_dirtyend = 0;
-       bp->b_validoff = bp->b_validend = 0;
 
-       /* nuke any credentials we were holding */
-       if (IS_VALID_CRED(bp->b_rcred)) {
-               kauth_cred_unref(&bp->b_rcred);
-       }
-       if (IS_VALID_CRED(bp->b_wcred)) {
-               kauth_cred_unref(&bp->b_wcred);
+               lck_mtx_lock_spin(buf_mtxp);
        }
-       lck_mtx_lock(buf_mtxp);
-
        return (0);
 }
 
@@ -2954,8 +3417,9 @@ buf_invalblkno(vnode_t vp, daddr64_t lblkno, int flags)
 
        dp = BUFHASH(vp, lblkno);
 
-       lck_mtx_lock(buf_mtxp);
 relook:        
+       lck_mtx_lock_spin(buf_mtxp);
+
        if ((bp = incore_locked(vp, lblkno, dp)) == (struct buf *)0) {
                lck_mtx_unlock(buf_mtxp);
                return (0);
@@ -2967,10 +3431,9 @@ relook:
                }
                SET(bp->b_lflags, BL_WANTED);
 
-               error = msleep((caddr_t)bp, buf_mtxp, (PRIBIO + 1), "buf_invalblkno", NULL);
+               error = msleep((caddr_t)bp, buf_mtxp, PDROP | (PRIBIO + 1), "buf_invalblkno", NULL);
 
                if (error) {
-                       lck_mtx_unlock(buf_mtxp);
                        return (error);
                }
                goto relook;
@@ -2978,6 +3441,7 @@ relook:
        bremfree_locked(bp);
        SET(bp->b_lflags, BL_BUSY);
        SET(bp->b_flags, B_INVAL);
+       buf_busycount++;
 #ifdef JOE_DEBUG
        bp->b_owner = current_thread();
        bp->b_tag   = 4;
@@ -3011,6 +3475,7 @@ buf_drop(buf_t bp)
         * Unlock the buffer.
         */
        CLR(bp->b_lflags, (BL_BUSY | BL_WANTED));
+       buf_busycount--;
 
        lck_mtx_unlock(buf_mtxp);
 
@@ -3027,7 +3492,7 @@ errno_t
 buf_acquire(buf_t bp, int flags, int slpflag, int slptimeo) {
         errno_t error;
 
-        lck_mtx_lock(buf_mtxp);
+        lck_mtx_lock_spin(buf_mtxp);
 
        error = buf_acquire_locked(bp, flags, slpflag, slptimeo);
 
@@ -3052,7 +3517,7 @@ buf_acquire_locked(buf_t bp, int flags, int slpflag, int slptimeo)
        }
         if (ISSET(bp->b_lflags, BL_BUSY)) {
                /*      
-                * since the mutex_lock may block, the buffer
+                * since the lck_mtx_lock may block, the buffer
                 * may become BUSY, so we need to
                 * recheck for a NOWAIT request
                 */
@@ -3072,6 +3537,8 @@ buf_acquire_locked(buf_t bp, int flags, int slpflag, int slptimeo)
        if (flags & BAC_REMOVE)
                bremfree_locked(bp);
        SET(bp->b_lflags, BL_BUSY);
+       buf_busycount++;
+
 #ifdef JOE_DEBUG
        bp->b_owner = current_thread();
        bp->b_tag   = 5;
@@ -3087,15 +3554,17 @@ buf_acquire_locked(buf_t bp, int flags, int slpflag, int slptimeo)
 errno_t
 buf_biowait(buf_t bp)
 {
-       lck_mtx_lock(buf_mtxp);
+       while (!ISSET(bp->b_flags, B_DONE)) {
 
-       DTRACE_IO1(wait__start, buf_t, bp);
-       while (!ISSET(bp->b_flags, B_DONE))
-               (void) msleep(bp, buf_mtxp, (PRIBIO+1), "buf_biowait", NULL);
-       DTRACE_IO1(wait__done, buf_t, bp);
+               lck_mtx_lock_spin(buf_mtxp);
 
-       lck_mtx_unlock(buf_mtxp);
-       
+               if (!ISSET(bp->b_flags, B_DONE)) {
+                       DTRACE_IO1(wait__start, buf_t, bp);
+                       (void) msleep(bp, buf_mtxp, PDROP | (PRIBIO+1), "buf_biowait", NULL);
+                       DTRACE_IO1(wait__done, buf_t, bp);
+               } else
+                       lck_mtx_unlock(buf_mtxp);
+       }
        /* check for interruption of I/O (e.g. via NFS), then errors. */
        if (ISSET(bp->b_flags, B_EINTR)) {
                CLR(bp->b_flags, B_EINTR);
@@ -3106,21 +3575,6 @@ buf_biowait(buf_t bp)
                return (0);
 }
 
-/*
- * Wait for the callback operation on a B_CALL buffer to complete.
- */
-void
-buf_biowait_callback(buf_t bp)
-{
-       lck_mtx_lock(buf_mtxp);
-
-       DTRACE_IO1(wait__start, buf_t, bp);
-       while (!ISSET(bp->b_lflags, BL_CALLDONE))
-               (void) msleep(bp, buf_mtxp, (PRIBIO+1), "buf_biowait", NULL);
-       DTRACE_IO1(wait__done, buf_t, bp);
-
-       lck_mtx_unlock(buf_mtxp);
-}
 
 /*
  * Mark I/O complete on a buffer.
@@ -3144,8 +3598,10 @@ extern int hard_throttle_on_root;
 void
 buf_biodone(buf_t bp)
 {
+       mount_t mp;
+       
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 387)) | DBG_FUNC_START,
-                    (int)bp, (int)bp->b_datap, bp->b_flags, 0, 0);
+                    bp, bp->b_datap, bp->b_flags, 0, 0);
 
        if (ISSET(bp->b_flags, B_DONE))
                panic("biodone already");
@@ -3154,8 +3610,17 @@ buf_biodone(buf_t bp)
                fslog_io_error(bp);
        }
 
-       if (bp->b_vp && bp->b_vp->v_mount && (bp->b_flags & B_READ) == 0) {
-               update_last_io_time(bp->b_vp->v_mount);
+       if (bp->b_vp && bp->b_vp->v_mount) {
+               mp = bp->b_vp->v_mount;
+       } else {
+               mp = NULL;
+       }
+       
+       if (mp && (bp->b_flags & B_READ) == 0) {
+               update_last_io_time(mp);
+               INCR_PENDING_IO(-(pending_io_t)buf_count(bp), mp->mnt_pending_write_size);
+       } else if (mp) {
+               INCR_PENDING_IO(-(pending_io_t)buf_count(bp), mp->mnt_pending_read_size);
        }
 
         if (kdebug_enable) {
@@ -3171,21 +3636,29 @@ buf_biodone(buf_t bp)
                else if (bp->b_flags & B_PAGEIO)
                        code |= DKIO_PAGING;
 
+               if (bp->b_flags & B_THROTTLED_IO)
+                       code |= DKIO_THROTTLE;
+               else if (bp->b_flags & B_PASSIVE)
+                       code |= DKIO_PASSIVE;
+
                KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_DKRW, code) | DBG_FUNC_NONE,
-                                     (unsigned int)bp, (unsigned int)bp->b_vp,
+                              bp, (uintptr_t)bp->b_vp,
                                      bp->b_resid, bp->b_error, 0);
         }
        if ((bp->b_vp != NULLVP) &&
-           ((bp->b_flags & (B_PAGEIO | B_READ)) == (B_PAGEIO | B_READ)) &&
+           ((bp->b_flags & (B_IOSTREAMING | B_PAGEIO | B_READ)) == (B_PAGEIO | B_READ)) &&
            (bp->b_vp->v_mount->mnt_kern_flag & MNTK_ROOTDEV)) {
                microuptime(&priority_IO_timestamp_for_root);
                hard_throttle_on_root = 0;
        }
+
        /*
         * I/O was done, so don't believe
-        * the DIRTY state from VM anymore
+        * the DIRTY state from VM anymore...
+        * and we need to reset the THROTTLED/PASSIVE
+        * indicators
         */
-       CLR(bp->b_flags, B_WASDIRTY);
+       CLR(bp->b_flags, (B_WASDIRTY | B_THROTTLED_IO | B_PASSIVE));
        DTRACE_IO1(done, buf_t, bp);
 
        if (!ISSET(bp->b_flags, B_READ) && !ISSET(bp->b_flags, B_RAW))
@@ -3198,46 +3671,26 @@ buf_biodone(buf_t bp)
 
        if (ISSET(bp->b_flags, (B_CALL | B_FILTER))) {  /* if necessary, call out */
                void    (*iodone_func)(struct buf *, void *) = bp->b_iodone;
-               void    *arg = (void *)bp->b_transaction;
+               void    *arg = bp->b_transaction;
                int     callout = ISSET(bp->b_flags, B_CALL);
 
+               if (iodone_func == NULL)
+                       panic("biodone: bp @ %p has NULL b_iodone!\n", bp);                     
+
                CLR(bp->b_flags, (B_CALL | B_FILTER));  /* filters and callouts are one-shot */
                bp->b_iodone = NULL;
                bp->b_transaction = NULL;
 
-               if (iodone_func == NULL) {
-                       panic("biodone: bp @ %p has NULL b_iodone!\n", bp);                     
-               } else { 
-                       if (callout)
-                               SET(bp->b_flags, B_DONE);       /* note that it's done */
-                       (*iodone_func)(bp, arg);
-               }
-               if (callout) {
-                       int need_wakeup = 0;
+               if (callout)
+                       SET(bp->b_flags, B_DONE);       /* note that it's done */
 
-                       /*
+               (*iodone_func)(bp, arg);
+
+               if (callout) {
+                       /*
                         * assumes that the callback function takes
                         * ownership of the bp and deals with releasing it if necessary
-                        * BL_WANTED indicates that we've decided to wait on the
-                        * completion of this I/O in a synchronous manner... we
-                        * still call the callback function, but in addition we
-                        * will do a wakeup... BL_CALLDONE indicates that the callback
-                        * routine has completed and its ok for the waiter to take
-                        * 'ownership' of this bp back
                         */
-                       lck_mtx_lock_spin(buf_mtxp);
-
-                       if (bp->b_lflags & BL_WANTED) {
-                               CLR(bp->b_lflags, BL_WANTED);
-                               need_wakeup = 1;
-                       }
-                       SET(bp->b_lflags, BL_CALLDONE);
-
-                       lck_mtx_unlock(buf_mtxp);
-                       
-                       if (need_wakeup)
-                               wakeup(bp);
-
                        goto biodone_done;
                }
                /*
@@ -3278,7 +3731,7 @@ buf_biodone(buf_t bp)
        }
 biodone_done:
        KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 387)) | DBG_FUNC_END,
-                    (int)bp, (int)bp->b_datap, bp->b_flags, 0, 0);
+                 (uintptr_t)bp, (uintptr_t)bp->b_datap, bp->b_flags, 0, 0);
 }
 
 /*
@@ -3290,7 +3743,7 @@ count_lock_queue(void)
        buf_t   bp;
        int     n = 0;
 
-       lck_mtx_lock(buf_mtxp);
+       lck_mtx_lock_spin(buf_mtxp);
 
        for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
            bp = bp->b_freelist.tqe_next)
@@ -3306,16 +3759,7 @@ count_lock_queue(void)
 int
 count_busy_buffers(void)
 {
-       buf_t   bp;
-       int     nbusy = 0;
-
-       lck_mtx_lock(buf_mtxp);
-       for (bp = &buf_headers[boot_nbuf_headers]; --bp >= buf_headers; )
-               if (!ISSET(bp->b_flags, B_INVAL) && ISSET(bp->b_lflags, BL_BUSY))
-                       nbusy++;
-       lck_mtx_unlock(buf_mtxp);
-
-       return (nbusy);
+       return buf_busycount + bufstats.bufs_iobufinuse;
 }
 
 #if DIAGNOSTIC
@@ -3328,8 +3772,8 @@ void
 vfs_bufstats()
 {
        int i, j, count;
-       register struct buf *bp;
-       register struct bqueues *dp;
+       struct buf *bp;
+       struct bqueues *dp;
        int counts[MAXBSIZE/CLBYTES+1];
        static char *bname[BQUEUES] =
                { "LOCKED", "LRU", "AGE", "EMPTY", "META", "LAUNDRY" };
@@ -3356,7 +3800,7 @@ vfs_bufstats()
 }
 #endif /* DIAGNOSTIC */
 
-#define        NRESERVEDIOBUFS 64
+#define        NRESERVEDIOBUFS 128
 
 
 buf_t
@@ -3364,14 +3808,14 @@ alloc_io_buf(vnode_t vp, int priv)
 {
        buf_t   bp;
 
-       lck_mtx_lock(iobuffer_mtxp);
+       lck_mtx_lock_spin(iobuffer_mtxp);
 
        while (((niobuf_headers - NRESERVEDIOBUFS < bufstats.bufs_iobufinuse) && !priv) || 
               (bp = iobufqueue.tqh_first) == NULL) {
                bufstats.bufs_iobufsleeps++;
 
                need_iobuffer = 1;
-               (void) msleep(&need_iobuffer, iobuffer_mtxp, (PRIBIO+1), (const char *)"alloc_io_buf", NULL);
+               (void) msleep(&need_iobuffer, iobuffer_mtxp, PSPIN | (PRIBIO+1), (const char *)"alloc_io_buf", NULL);
        }
        TAILQ_REMOVE(&iobufqueue, bp, b_freelist);
 
@@ -3393,6 +3837,7 @@ alloc_io_buf(vnode_t vp, int priv)
        bp->b_datap = 0;
        bp->b_flags = 0;
        bp->b_lflags = BL_BUSY | BL_IOBUF;
+       bp->b_redundancy_flags = 0;
        bp->b_blkno = bp->b_lblkno = 0;
 #ifdef JOE_DEBUG
        bp->b_owner = current_thread();
@@ -3405,6 +3850,9 @@ alloc_io_buf(vnode_t vp, int priv)
        bp->b_bufsize = 0;
        bp->b_upl = NULL;
        bp->b_vp = vp;
+#ifdef CONFIG_PROTECT
+       bp->b_cpentry = 0;
+#endif
 
        if (vp && (vp->v_type == VBLK || vp->v_type == VCHR))
                bp->b_dev = vp->v_rdev;
@@ -3443,6 +3891,9 @@ free_io_buf(buf_t bp)
                need_iobuffer = 0;
                need_wakeup = 1;
        }
+       if (bufstats.bufs_iobufinuse <= 0)
+               panic("free_io_buf: bp(%p) - bufstats.bufs_iobufinuse < 0", bp);
+
        bufstats.bufs_iobufinuse--;
 
        lck_mtx_unlock(iobuffer_mtxp);
@@ -3455,7 +3906,7 @@ free_io_buf(buf_t bp)
 void
 buf_list_lock(void)
 {
-        lck_mtx_lock(buf_mtxp);
+        lck_mtx_lock_spin(buf_mtxp);
 }
 
 void
@@ -3474,10 +3925,15 @@ buf_list_unlock(void)
 static void
 bcleanbuf_thread_init(void)
 {
+       thread_t        thread = THREAD_NULL;
+
        /* create worker thread */
-       kernel_thread(kernel_task, bcleanbuf_thread);
+       kernel_thread_start((thread_continue_t)bcleanbuf_thread, NULL, &thread);
+       thread_deallocate(thread);
 }
 
+typedef int (*bcleanbufcontinuation)(int);
+
 static void
 bcleanbuf_thread(void)
 {
@@ -3486,11 +3942,12 @@ bcleanbuf_thread(void)
        int loopcnt = 0;
 
        for (;;) {
-               lck_mtx_lock(buf_mtxp);
-
-               while ( (bp = TAILQ_FIRST(&bufqueues[BQ_LAUNDRY])) == NULL)
-                       (void)msleep((void *)&bufqueues[BQ_LAUNDRY], buf_mtxp, PRIBIO, "blaundry", NULL);
+               lck_mtx_lock_spin(buf_mtxp);
 
+               while ( (bp = TAILQ_FIRST(&bufqueues[BQ_LAUNDRY])) == NULL) {
+                       (void)msleep0(&bufqueues[BQ_LAUNDRY], buf_mtxp, PRIBIO|PDROP, "blaundry", 0, (bcleanbufcontinuation)bcleanbuf_thread);
+               }
+               
                /*
                 * Remove from the queue
                 */
@@ -3500,6 +3957,7 @@ bcleanbuf_thread(void)
                 * Buffer is no longer on any free list
                 */
                SET(bp->b_lflags, BL_BUSY);
+               buf_busycount++;
 
 #ifdef JOE_DEBUG
                bp->b_owner = current_thread();
@@ -3521,20 +3979,27 @@ bcleanbuf_thread(void)
                        binstailfree(bp, &bufqueues[BQ_LAUNDRY], BQ_LAUNDRY);
                        blaundrycnt++;
 
-                       /* we never leave a busy page on the laundary queue */
+                       /* we never leave a busy page on the laundry queue */
                        CLR(bp->b_lflags, BL_BUSY);
+                       buf_busycount--;
 #ifdef JOE_DEBUG
                        bp->b_owner = current_thread();
                        bp->b_tag   = 11;
 #endif
 
                        lck_mtx_unlock(buf_mtxp);
-
-                       if (loopcnt > 10) {
-                               (void)tsleep((void *)&bufqueues[BQ_LAUNDRY], PRIBIO, "blaundry", 1);
+                       
+                       if (loopcnt > MAXLAUNDRY) {
+                               /*
+                                * bawrite_internal() can return errors if we're throttled. If we've
+                                * done several I/Os and failed, give the system some time to unthrottle
+                                * the vnode
+                                */
+                               (void)tsleep((void *)&bufqueues[BQ_LAUNDRY], PRIBIO, "blaundry", 1);
                                loopcnt = 0;
                        } else {
-                               (void)thread_block(THREAD_CONTINUE_NULL);
+                               /* give other threads a chance to run */
+                               (void)thread_block(THREAD_CONTINUE_NULL);
                                loopcnt++;
                        }
                }
@@ -3584,7 +4049,7 @@ brecover_data(buf_t bp)
        }
        bp->b_upl = upl;
                                        
-       kret = ubc_upl_map(upl, (vm_address_t *)&(bp->b_datap));
+       kret = ubc_upl_map(upl, (vm_offset_t *)&(bp->b_datap));
 
        if (kret != KERN_SUCCESS)
                panic("getblk: ubc_upl_map() failed with (%d)", kret);
@@ -3598,6 +4063,135 @@ dump_buffer:
        return(0);
 }
 
+boolean_t 
+buffer_cache_gc(int all)
+{
+       buf_t bp;
+       boolean_t did_large_zfree = FALSE;
+       boolean_t need_wakeup = FALSE;
+       int now = buf_timestamp();
+       uint32_t found = 0, total_found = 0;
+       struct bqueues privq;
+       int thresh_hold = BUF_STALE_THRESHHOLD;
+
+       if (all)
+               thresh_hold = 0;
+       /* 
+        * We only care about metadata (incore storage comes from zalloc()).
+        * No more than 1024 buffers total, and only those not accessed within the
+        * last 30s.  We will also only examine 128 buffers during a single grab
+        * of the lock in order to limit lock hold time.
+        */
+       lck_mtx_lock(buf_mtxp);
+       do {
+               found = 0;
+               TAILQ_INIT(&privq);
+               need_wakeup = FALSE;
+
+               while (((bp = TAILQ_FIRST(&bufqueues[BQ_META]))) && 
+                               (now > bp->b_timestamp) &&
+                               (now - bp->b_timestamp > thresh_hold) && 
+                               (found < BUF_MAX_GC_BATCH_SIZE)) {
+
+                       /* Remove from free list */
+                       bremfree_locked(bp);
+                       found++;
+
+#ifdef JOE_DEBUG
+                       bp->b_owner = current_thread();
+                       bp->b_tag   = 12;
+#endif
+
+                       /* If dirty, move to laundry queue and remember to do wakeup */
+                       if (ISSET(bp->b_flags, B_DELWRI)) {
+                               SET(bp->b_lflags, BL_WANTDEALLOC);
+
+                               bmovelaundry(bp);
+                               need_wakeup = TRUE;
+
+                               continue;
+                       }
+
+                       /* 
+                        * Mark busy and put on private list.  We could technically get 
+                        * away without setting BL_BUSY here.
+                        */
+                       SET(bp->b_lflags, BL_BUSY);
+                       buf_busycount++;
+
+                       /* 
+                        * Remove from hash and dissociate from vp.
+                        */
+                       bremhash(bp);
+                       if (bp->b_vp) {
+                               brelvp_locked(bp);
+                       }
+
+                       TAILQ_INSERT_TAIL(&privq, bp, b_freelist);
+               }
+
+               if (found == 0) {
+                       break;
+               }
+
+               /* Drop lock for batch processing */
+               lck_mtx_unlock(buf_mtxp);
+
+               /* Wakeup and yield for laundry if need be */
+               if (need_wakeup) {
+                       wakeup(&bufqueues[BQ_LAUNDRY]);
+                       (void)thread_block(THREAD_CONTINUE_NULL);
+               }
+
+               /* Clean up every buffer on private list */
+               TAILQ_FOREACH(bp, &privq, b_freelist) {
+                       /* Take note if we've definitely freed at least a page to a zone */
+                       if ((ISSET(bp->b_flags, B_ZALLOC)) && (buf_size(bp) >= PAGE_SIZE)) {
+                               did_large_zfree = TRUE;
+                       }    
+
+                       trace(TR_BRELSE, pack(bp->b_vp, bp->b_bufsize), bp->b_lblkno);
+
+                       /* Free Storage */
+                       buf_free_meta_store(bp);
+
+                       /* Release credentials */
+                       buf_release_credentials(bp);
+
+                       /* Prepare for moving to empty queue */
+                       CLR(bp->b_flags, (B_META | B_ZALLOC | B_DELWRI | B_LOCKED 
+                                               | B_AGE | B_ASYNC | B_NOCACHE | B_FUA));
+                       bp->b_whichq = BQ_EMPTY;
+                       BLISTNONE(bp);
+               }
+
+               lck_mtx_lock(buf_mtxp);
+
+               /* Back under lock, move them all to invalid hash and clear busy */
+               TAILQ_FOREACH(bp, &privq, b_freelist) {
+                       binshash(bp, &invalhash);
+                       CLR(bp->b_lflags, BL_BUSY);
+                       buf_busycount--;
+
+#ifdef JOE_DEBUG
+                       if (bp->b_owner != current_thread()) {
+                               panic("Buffer stolen from buffer_cache_gc()");
+                       }
+                       bp->b_owner = current_thread();
+                       bp->b_tag   = 13;
+#endif
+               }
+
+               /* And do a big bulk move to the empty queue */
+               TAILQ_CONCAT(&bufqueues[BQ_EMPTY], &privq, b_freelist);
+               total_found += found;
+
+       } while ((all || (total_found < BUF_MAX_GC_COUNT)) && (found == BUF_MAX_GC_BATCH_SIZE));
+
+       lck_mtx_unlock(buf_mtxp);
+
+       return did_large_zfree;
+}
 
 
 /*
@@ -3656,6 +4250,8 @@ bflushq(int whichq, mount_t mp)
                bp->b_tag   = 7;
 #endif
                SET(bp->b_lflags, BL_BUSY);
+               buf_busycount++;
+
                flush_table[buf_count] = bp;
                buf_count++;
                total_writes++;
@@ -3816,6 +4412,7 @@ bufqdec(int q)
 static void
 bufq_balance_thread_init(void)
 {
+       thread_t        thread = THREAD_NULL;
 
        if (bufqscanwait++ == 0) {
 
@@ -3872,7 +4469,8 @@ bufq_balance_thread_init(void)
        }
 
        /* create worker thread */
-       kernel_thread(kernel_task, bufqscan_thread);
+       kernel_thread_start((thread_continue_t)bufqscan_thread, NULL, &thread);
+       thread_deallocate(thread);
 }
 
 /* The workloop for the buffer balancing thread */
@@ -3961,7 +4559,7 @@ balancebufq(int q)
                
                /* check if it's stale */
                if ((t - bp->b_timestamp) > bufqlim[q].bl_stale) {
-                       if (bcleanbuf(bp)) {
+                       if (bcleanbuf(bp, FALSE)) {
                                /* buf_bawrite() issued, bp not ready */
                                moretodo = 1;
                        } else {