/*
- * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2016 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 */
/*-
#endif /* DIAGNOSTIC */
#include <kern/task.h>
#include <kern/zalloc.h>
-#include <kern/lock.h>
+#include <kern/locks.h>
+#include <kern/thread.h>
+
+#include <sys/fslog.h> /* fslog_io_error() */
+#include <sys/disk.h> /* dk_error_description_t */
+
+#include <mach/mach_types.h>
+#include <mach/memory_object_types.h>
+#include <kern/sched_prim.h> /* thread_block() */
#include <vm/vm_kern.h>
+#include <vm/vm_pageout.h>
#include <sys/kdebug.h>
-#include <machine/spl.h>
-#if BALANCE_QUEUES
-static __inline__ void bufqinc(int q);
-static __inline__ void bufqdec(int q);
-#endif
+#include <libkern/OSAtomic.h>
+#include <libkern/OSDebug.h>
+#include <sys/ubc_internal.h>
-static int bcleanbuf(buf_t bp);
+#include <sys/sdt.h>
+
+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);
-static buf_t incore_locked(vnode_t vp, daddr64_t blkno);
/* timeout is in msecs */
static buf_t getnewbuf(int slpflag, int slptimeo, int *queue);
static void bremfree_locked(buf_t bp);
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);
+
+int bdwrite_internal(buf_t, int);
/* zone allocated buffer headers */
static void bufzoneinit(void);
LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash;
u_long bufhash;
+static buf_t incore_locked(vnode_t vp, daddr64_t blkno, struct bufhashhdr *dp);
+
/* Definitions for the buffer stats. */
struct bufstats bufstats;
/* Number of delayed write buffers */
-int nbdwrite = 0;
+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];
static lck_mtx_t *iobuffer_mtxp;
static lck_mtx_t *buf_mtxp;
+static int buf_busycount;
+
static __inline__ int
buf_timestamp(void)
{
/*
* Insq/Remq for the buffer free lists.
*/
-#if BALANCE_QUEUES
-#define binsheadfree(bp, dp, whichq) do { \
- TAILQ_INSERT_HEAD(dp, bp, b_freelist); \
- bufqinc((whichq)); \
- (bp)->b_whichq = whichq; \
- (bp)->b_timestamp = buf_timestamp(); \
- } while (0)
-
-#define binstailfree(bp, dp, whichq) do { \
- TAILQ_INSERT_TAIL(dp, bp, b_freelist); \
- bufqinc((whichq)); \
- (bp)->b_whichq = whichq; \
- (bp)->b_timestamp = buf_timestamp(); \
- } while (0)
-#else
#define binsheadfree(bp, dp, whichq) do { \
TAILQ_INSERT_HEAD(dp, bp, b_freelist); \
- (bp)->b_whichq = whichq; \
- (bp)->b_timestamp = buf_timestamp(); \
} while (0)
#define binstailfree(bp, dp, whichq) do { \
TAILQ_INSERT_TAIL(dp, bp, b_freelist); \
- (bp)->b_whichq = whichq; \
- (bp)->b_timestamp = buf_timestamp(); \
} while (0)
-#endif
-
#define BHASHENTCHECK(bp) \
if ((bp)->b_hash.le_prev != (struct buf **)0xdeadbeef) \
- panic("%x: b_hash.le_prev is not deadbeef", (bp));
+ panic("%p: b_hash.le_prev is not deadbeef", (bp));
#define BLISTNONE(bp) \
(bp)->b_hash.le_next = (struct buf *)0; \
int lru_is_stale = LRU_IS_STALE;
int age_is_stale = AGE_IS_STALE;
int meta_is_stale = META_IS_STALE;
-static int boot_nbuf = 0;
+#define MAXLAUNDRY 10
/* LIST_INSERT_HEAD() with assertions */
static __inline__ void
*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
void
buf_markdelayed(buf_t bp) {
- SET(bp->b_flags, B_DELWRI);
- buf_reassign(bp, bp->b_vp);
+ if (!ISSET(bp->b_flags, B_DELWRI)) {
+ SET(bp->b_flags, B_DELWRI);
+
+ 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
SET(bp->b_flags, B_EINTR);
}
+
void
buf_markaged(buf_t bp) {
SET(bp->b_flags, B_AGE);
}
+int
+buf_fua(buf_t bp) {
+
+ if ((bp->b_flags & B_FUA) == B_FUA)
+ return 1;
+ return 0;
+}
+
+void
+buf_markfua(buf_t bp) {
+
+ SET(bp->b_flags, B_FUA);
+}
+
+#if CONFIG_PROTECT
+cpx_t bufattr_cpx(bufattr_t bap)
+{
+ return bap->ba_cpx;
+}
+
+void bufattr_setcpx(bufattr_t bap, cpx_t cpx)
+{
+ bap->ba_cpx = cpx;
+}
+
+void
+buf_setcpoff (buf_t bp, uint64_t foffset) {
+ bp->b_attr.ba_cp_file_off = foffset;
+}
+
+uint64_t
+bufattr_cpoff(bufattr_t bap) {
+ return bap->ba_cp_file_off;
+}
+
+void
+bufattr_setcpoff(bufattr_t bap, uint64_t foffset) {
+ bap->ba_cp_file_off = foffset;
+}
+
+#else // !CONTECT_PROTECT
+
+uint64_t
+bufattr_cpoff(bufattr_t bap __unused) {
+ return 0;
+}
+
+void
+bufattr_setcpoff(__unused bufattr_t bap, __unused uint64_t foffset) {
+ return;
+}
+
+struct cpx *bufattr_cpx(__unused bufattr_t bap)
+{
+ return NULL;
+}
+
+void bufattr_setcpx(__unused bufattr_t bap, __unused struct cpx *cpx)
+{
+}
+
+#endif /* !CONFIG_PROTECT */
+
+bufattr_t
+bufattr_alloc() {
+ bufattr_t bap;
+ MALLOC(bap, bufattr_t, sizeof(struct bufattr), M_TEMP, M_WAITOK);
+ if (bap == NULL)
+ return NULL;
+
+ bzero(bap, sizeof(struct bufattr));
+ return bap;
+}
+
+void
+bufattr_free(bufattr_t bap) {
+ if (bap)
+ FREE(bap, M_TEMP);
+}
+
+bufattr_t
+bufattr_dup(bufattr_t bap) {
+ bufattr_t new_bufattr;
+ MALLOC(new_bufattr, bufattr_t, sizeof(struct bufattr), M_TEMP, M_WAITOK);
+ if (new_bufattr == NULL)
+ return NULL;
+
+ /* Copy the provided one into the new copy */
+ memcpy (new_bufattr, bap, sizeof(struct bufattr));
+ return new_bufattr;
+}
+
+int
+bufattr_rawencrypted(bufattr_t bap) {
+ if ( (bap->ba_flags & BA_RAW_ENCRYPTED_IO) )
+ return 1;
+ return 0;
+}
+
+int
+bufattr_throttled(bufattr_t bap) {
+ return (GET_BUFATTR_IO_TIER(bap));
+}
+
+int
+bufattr_passive(bufattr_t bap) {
+ if ( (bap->ba_flags & BA_PASSIVE) )
+ return 1;
+ return 0;
+}
+
+int
+bufattr_nocache(bufattr_t bap) {
+ if ( (bap->ba_flags & BA_NOCACHE) )
+ return 1;
+ return 0;
+}
+
+int
+bufattr_meta(bufattr_t bap) {
+ if ( (bap->ba_flags & BA_META) )
+ return 1;
+ return 0;
+}
+
+void
+bufattr_markmeta(bufattr_t bap) {
+ SET(bap->ba_flags, BA_META);
+}
+
+int
+bufattr_delayidlesleep(bufattr_t bap)
+{
+ if ( (bap->ba_flags & BA_DELAYIDLESLEEP) )
+ return 1;
+ return 0;
+}
+
+bufattr_t
+buf_attr(buf_t bp) {
+ return &bp->b_attr;
+}
+
+void
+buf_markstatic(buf_t bp __unused) {
+ SET(bp->b_flags, B_STATICCONTENT);
+}
+
+int
+buf_static(buf_t bp) {
+ if ( (bp->b_flags & B_STATICCONTENT) )
+ return 1;
+ return 0;
+}
+
+void
+bufattr_markgreedymode(bufattr_t bap) {
+ SET(bap->ba_flags, BA_GREEDY_MODE);
+}
+
+int
+bufattr_greedymode(bufattr_t bap) {
+ if ( (bap->ba_flags & BA_GREEDY_MODE) )
+ return 1;
+ return 0;
+}
+
+void
+bufattr_markisochronous(bufattr_t bap) {
+ SET(bap->ba_flags, BA_ISOCHRONOUS);
+}
+
+int
+bufattr_isochronous(bufattr_t bap) {
+ if ( (bap->ba_flags & BA_ISOCHRONOUS) )
+ return 1;
+ return 0;
+}
+
+void
+bufattr_markquickcomplete(bufattr_t bap) {
+ SET(bap->ba_flags, BA_QUICK_COMPLETE);
+}
+
+int
+bufattr_quickcomplete(bufattr_t bap) {
+ if ( (bap->ba_flags & BA_QUICK_COMPLETE) )
+ return 1;
+ return 0;
+}
+
errno_t
buf_error(buf_t bp) {
void
buf_reset(buf_t bp, int32_t io_flags) {
- CLR(bp->b_flags, (B_READ | B_WRITE | B_ERROR | B_DONE | B_INVAL | B_ASYNC | B_NOCACHE));
+ CLR(bp->b_flags, (B_READ | B_WRITE | B_ERROR | B_DONE | B_INVAL | B_ASYNC | B_NOCACHE | B_FUA));
SET(bp->b_flags, (io_flags & (B_ASYNC | B_READ | B_WRITE | B_NOCACHE)));
bp->b_error = 0;
void *
buf_callback(buf_t bp)
{
- if ( !(bp->b_lflags & BL_IOBUF) )
- return ((void *) NULL);
if ( !(bp->b_flags & B_CALL) )
return ((void *) NULL);
errno_t
buf_setcallback(buf_t bp, void (*callback)(buf_t, void *), void *transaction)
{
+ assert(!ISSET(bp->b_flags, B_FILTER) && ISSET(bp->b_lflags, BL_BUSY));
- if ( !(bp->b_lflags & BL_IOBUF) )
- return (EINVAL);
-
if (callback)
bp->b_flags |= (B_CALL | B_ASYNC);
else
}
io_bp = alloc_io_buf(bp->b_vp, 0);
- io_bp->b_flags = bp->b_flags & (B_COMMIT_UPL | B_META | B_PAGEIO | B_CLUSTER | B_PHYS | B_ASYNC | B_READ);
+ io_bp->b_flags = bp->b_flags & (B_COMMIT_UPL | B_META | B_PAGEIO | B_CLUSTER | B_PHYS | B_RAW | B_ASYNC | B_READ | B_FUA);
if (iodone) {
io_bp->b_transaction = arg;
}
+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);
+ assert(ISSET(bp->b_lflags, BL_BUSY));
+
+ 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;
- bp->b_flags |= B_FILTER;
+ if (filter)
+ bp->b_flags |= B_FILTER;
+ else
+ bp->b_flags &= ~B_FILTER;
}
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);
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)) {
kret = ubc_upl_map(bp->b_upl, &vaddr); /* Map it in */
if (kret != KERN_SUCCESS) {
- *io_addr = 0;
+ *io_addr = NULL;
return(ENOMEM);
}
if (real_bp && real_bp->b_datap)
return (0);
- if (bp->b_lflags & BL_IOBUF) {
+ if ((bp->b_lflags & BL_IOBUF) &&
+ ((bp->b_flags & (B_PAGEIO | B_READ)) != (B_PAGEIO | B_READ))) {
/*
+ * ignore pageins... the 'right' thing will
+ * happen due to the way we handle speculative
+ * clusters...
+ *
* when we commit these pages, we'll hit
* it with UPL_COMMIT_INACTIVE which
* will clear the reference bit that got
bp->b_resid = 0;
}
-
-
/*
* Read or write a buffer that is not contiguous on disk.
* buffer is marked done/error at the conclusion
io_bp->b_blkno = io_blkno;
buf_reset(io_bp, io_direction);
+
/*
- * Call the device to do the I/O and wait for it
+ * Call the device to do the I/O and wait for it. Make sure the appropriate party is charged for write
*/
+
+ if (!ISSET(bp->b_flags, B_READ))
+ OSAddAtomic(1, &devvp->v_numoutput);
+
if ((error = VNOP_STRATEGY(io_bp)))
break;
if ((error = (int)buf_biowait(io_bp)))
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 physical
+ * 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");
* 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) {
- off_t f_offset;
+ off_t f_offset;
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);
+ return (error);
}
- if ((error = VNOP_BLOCKMAP(vp, f_offset, bp->b_bcount, &bp->b_blkno, &contig_bytes, NULL, bmap_flags, NULL))) {
+
+ 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 */
+
+#if CONFIG_PROTECT
+ /* Capture f_offset in the bufattr*/
+ cpx_t cpx = bufattr_cpx(buf_attr(bp));
+ if (cpx) {
+ /* No need to go here for older EAs */
+ if(cpx_use_offset_for_iv(cpx) && !cpx_synthetic_offset_for_iv(cpx)) {
+ off_t f_offset;
+ if ((error = VNOP_BLKTOOFF(bp->b_vp, bp->b_lblkno, &f_offset)))
+ return error;
+
+ /*
+ * Attach the file offset to this buffer. The
+ * bufattr attributes will be passed down the stack
+ * until they reach IOFlashStorage. IOFlashStorage
+ * will retain the offset in a local variable when it
+ * issues its I/Os to the NAND controller.
+ *
+ * Note that LwVM may end up splitting this I/O
+ * into sub-I/Os if it crosses a chunk boundary. In this
+ * case, LwVM will update this field when it dispatches
+ * each I/O to IOFlashStorage. But from our perspective
+ * we have only issued a single I/O.
+ */
+ buf_setcpoff(bp, f_offset);
+ CP_DEBUG((CPDBG_OFFSET_IO | DBG_FUNC_NONE), (uint32_t) f_offset, (uint32_t) bp->b_lblkno, (uint32_t) bp->b_blkno, (uint32_t) bp->b_bcount, 0);
+ }
+ }
+#endif
+
/*
* we can issue the I/O because...
* either B_CLUSTER is set which
* means that the I/O is properly set
* up to be a multiple of the page size, or
* we were able to successfully set up the
- * phsyical block mapping
+ * physical block mapping
*/
- return (VOCALL(devvp->v_op, VOFFSET(vnop_strategy), ap));
+ error = VOCALL(devvp->v_op, VOFFSET(vnop_strategy), ap);
+ DTRACE_FSINFO(strategy, vnode_t, vp);
+ return (error);
}
buf_t
buf_alloc(vnode_t vp)
{
- return(alloc_io_buf(vp, 0));
+ return(alloc_io_buf(vp, is_vm_privileged()));
}
void
}
+/*
+ * iterate buffers for the specified vp.
+ * if BUF_SCAN_DIRTY is set, do the dirty list
+ * if BUF_SCAN_CLEAN is set, do the clean list
+ * if neither flag is set, default to BUF_SCAN_DIRTY
+ * if BUF_NOTIFY_BUSY is set, call the callout function using a NULL bp for busy pages
+ */
+
+struct buf_iterate_info_t {
+ int flag;
+ struct buflists *listhead;
+};
void
-buf_iterate(vnode_t vp, int (*callout)(buf_t, void *), int flags, void *arg) {
+buf_iterate(vnode_t vp, int (*callout)(buf_t, void *), int flags, void *arg)
+{
buf_t bp;
int retval;
struct buflists local_iterblkhd;
int lock_flags = BAC_NOWAIT | BAC_REMOVE;
+ int notify_busy = flags & BUF_NOTIFY_BUSY;
+ struct buf_iterate_info_t list[2];
+ int num_lists, i;
if (flags & BUF_SKIP_LOCKED)
lock_flags |= BAC_SKIP_LOCKED;
if (flags & BUF_SKIP_NONLOCKED)
lock_flags |= BAC_SKIP_NONLOCKED;
- lck_mtx_lock(buf_mtxp);
-
- if (buf_iterprepare(vp, &local_iterblkhd, VBI_DIRTY)) {
- lck_mtx_unlock(buf_mtxp);
- return;
+ if ( !(flags & (BUF_SCAN_DIRTY | BUF_SCAN_CLEAN)))
+ flags |= BUF_SCAN_DIRTY;
+
+ num_lists = 0;
+
+ if (flags & BUF_SCAN_DIRTY) {
+ list[num_lists].flag = VBI_DIRTY;
+ list[num_lists].listhead = &vp->v_dirtyblkhd;
+ num_lists++;
+ }
+ if (flags & BUF_SCAN_CLEAN) {
+ list[num_lists].flag = VBI_CLEAN;
+ list[num_lists].listhead = &vp->v_cleanblkhd;
+ num_lists++;
}
- while (!LIST_EMPTY(&local_iterblkhd)) {
- bp = LIST_FIRST(&local_iterblkhd);
- LIST_REMOVE(bp, b_vnbufs);
- LIST_INSERT_HEAD(&vp->v_dirtyblkhd, bp, b_vnbufs);
- if (buf_acquire_locked(bp, lock_flags, 0, 0))
- continue;
+ for (i = 0; i < num_lists; i++) {
+ lck_mtx_lock(buf_mtxp);
+
+ if (buf_iterprepare(vp, &local_iterblkhd, list[i].flag)) {
+ lck_mtx_unlock(buf_mtxp);
+ continue;
+ }
+ while (!LIST_EMPTY(&local_iterblkhd)) {
+ bp = LIST_FIRST(&local_iterblkhd);
+ LIST_REMOVE(bp, b_vnbufs);
+ LIST_INSERT_HEAD(list[i].listhead, bp, b_vnbufs);
- lck_mtx_unlock(buf_mtxp);
+ if (buf_acquire_locked(bp, lock_flags, 0, 0)) {
+ if (notify_busy) {
+ bp = NULL;
+ } else {
+ continue;
+ }
+ }
- retval = callout(bp, arg);
+ lck_mtx_unlock(buf_mtxp);
- switch (retval) {
- case BUF_RETURNED:
- buf_brelse(bp);
- break;
- case BUF_CLAIMED:
- break;
- case BUF_RETURNED_DONE:
- buf_brelse(bp);
- lck_mtx_lock(buf_mtxp);
- goto out;
- case BUF_CLAIMED_DONE:
- lck_mtx_lock(buf_mtxp);
- goto out;
- }
- lck_mtx_lock(buf_mtxp);
- }
-out:
- buf_itercomplete(vp, &local_iterblkhd, VBI_DIRTY);
+ retval = callout(bp, arg);
- lck_mtx_unlock(buf_mtxp);
-}
+ switch (retval) {
+ case BUF_RETURNED:
+ if (bp)
+ buf_brelse(bp);
+ break;
+ case BUF_CLAIMED:
+ break;
+ case BUF_RETURNED_DONE:
+ if (bp)
+ buf_brelse(bp);
+ lck_mtx_lock(buf_mtxp);
+ goto out;
+ case BUF_CLAIMED_DONE:
+ lck_mtx_lock(buf_mtxp);
+ goto out;
+ }
+ lck_mtx_lock(buf_mtxp);
+ } /* while list has more nodes */
+ out:
+ buf_itercomplete(vp, &local_iterblkhd, list[i].flag);
+ lck_mtx_unlock(buf_mtxp);
+ } /* for each list */
+} /* buf_iterate */
/*
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 (;;) {
goto try_dirty_list;
}
while (!LIST_EMPTY(&local_iterblkhd)) {
+
bp = LIST_FIRST(&local_iterblkhd);
LIST_REMOVE(bp, b_vnbufs);
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...
}
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);
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...
}
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))
}
void
-buf_flushdirtyblks(vnode_t vp, int wait, int flags, char *msg) {
+buf_flushdirtyblks(vnode_t vp, int wait, int flags, const char *msg) {
+
+ (void) buf_flushdirtyblks_skipinfo(vp, wait, flags, msg);
+ return;
+}
+
+int
+buf_flushdirtyblks_skipinfo(vnode_t vp, int wait, int flags, const char *msg) {
buf_t bp;
int writes_issued = 0;
errno_t error;
int busy = 0;
struct buflists local_iterblkhd;
int lock_flags = BAC_NOWAIT | BAC_REMOVE;
+ int any_locked = 0;
if (flags & BUF_SKIP_LOCKED)
lock_flags |= BAC_SKIP_LOCKED;
bp = LIST_FIRST(&local_iterblkhd);
LIST_REMOVE(bp, b_vnbufs);
LIST_INSERT_HEAD(&vp->v_dirtyblkhd, bp, b_vnbufs);
-
- if ((error = buf_acquire_locked(bp, lock_flags, 0, 0)) == EBUSY)
- busy++;
- if (error)
- continue;
+
+ if ((error = buf_acquire_locked(bp, lock_flags, 0, 0)) == EBUSY) {
+ busy++;
+ }
+ if (error) {
+ /*
+ * If we passed in BUF_SKIP_LOCKED or BUF_SKIP_NONLOCKED,
+ * we may want to do somethign differently if a locked or unlocked
+ * buffer was encountered (depending on the arg specified).
+ * In this case, we know that one of those two was set, and the
+ * buf acquisition failed above.
+ *
+ * If it failed with EDEADLK, then save state which can be emitted
+ * later on to the caller. Most callers should not care.
+ */
+ if (error == EDEADLK) {
+ any_locked++;
+ }
+ continue;
+ }
lck_mtx_unlock(buf_mtxp);
bp->b_flags &= ~B_LOCKED;
goto loop;
}
}
+
+ return any_locked;
}
while (vp->v_iterblkflags & VBI_ITER) {
vp->v_iterblkflags |= VBI_ITERWANT;
- msleep(&vp->v_iterblkflags, buf_mtxp, 0, "buf_iterprepare", 0);
+ msleep(&vp->v_iterblkflags, buf_mtxp, 0, "buf_iterprepare", NULL);
}
if (LIST_EMPTY(listheadp)) {
LIST_INIT(iterheadp);
bremfree_locked(buf_t bp)
{
struct bqueues *dp = NULL;
- int whichq = -1;
+ 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
* NB: This makes an assumption about how tailq's are implemented.
*/
if (bp->b_freelist.tqe_next == NULL) {
- for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
- if (dp->tqh_last == &bp->b_freelist.tqe_next)
- break;
- if (dp == &bufqueues[BQUEUES])
+ dp = &bufqueues[whichq];
+
+ if (dp->tqh_last != &bp->b_freelist.tqe_next)
panic("bremfree: lost tail");
}
TAILQ_REMOVE(dp, bp, b_freelist);
- whichq = bp->b_whichq;
-#if BALANCE_QUEUES
- bufqdec(whichq);
-#endif
+
+ if (whichq == BQ_LAUNDRY)
+ blaundrycnt--;
+
bp->b_whichq = -1;
bp->b_timestamp = 0;
+ bp->b_shadow = 0;
}
/*
* Associate a buffer with a vnode.
+ * buf_mtxp must be locked on entry
*/
static void
-bgetvp(vnode_t vp, buf_t bp)
+bgetvp_locked(vnode_t vp, buf_t bp)
{
if (bp->b_vp != vp)
- panic("bgetvp: not free");
+ panic("bgetvp_locked: not free");
if (vp->v_type == VBLK || vp->v_type == VCHR)
bp->b_dev = vp->v_rdev;
/*
* Insert onto list for new vnode.
*/
- lck_mtx_lock(buf_mtxp);
bufinsvn(bp, &vp->v_cleanblkhd);
- lck_mtx_unlock(buf_mtxp);
}
/*
* Disassociate a buffer from a vnode.
+ * buf_mtxp must be locked on entry
*/
static void
-brelvp(buf_t bp)
+brelvp_locked(buf_t bp)
{
- vnode_t vp;
-
- if ((vp = bp->b_vp) == (vnode_t)NULL)
- panic("brelvp: NULL vp");
/*
* Delete from old vnode list, if on one.
*/
- lck_mtx_lock(buf_mtxp);
if (bp->b_vnbufs.le_next != NOLIST)
bufremvn(bp);
- lck_mtx_unlock(buf_mtxp);
bp->b_vp = (vnode_t)NULL;
}
static void
buf_reassign(buf_t bp, vnode_t newvp)
{
- register struct buflists *listheadp;
+ struct buflists *listheadp;
if (newvp == NULL) {
printf("buf_reassign: NULL");
return;
}
- lck_mtx_lock(buf_mtxp);
+ lck_mtx_lock_spin(buf_mtxp);
/*
* Delete from old vnode list, if on one.
* Initialize buffers and hash links for buffers.
*/
__private_extern__ void
-bufinit()
+bufinit(void)
{
buf_t bp;
struct bqueues *dp;
int i;
- int metabuf;
- long whichq;
- nbuf = 0;
+ nbuf_headers = 0;
/* Initialize the buffer queues ('freelists') and the hash table */
for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
TAILQ_INIT(dp);
bufhashtbl = hashinit(nbuf_hashelements, M_CACHE, &bufhash);
- metabuf = max_nbuf_headers/8; /* reserved for meta buf */
+ buf_busycount = 0;
/* Initialize the buffer headers */
for (i = 0; i < max_nbuf_headers; i++) {
- nbuf++;
- bp = &buf[i];
+ nbuf_headers++;
+ bp = &buf_headers[i];
bufhdrinit(bp);
- /*
- * metabuf buffer headers on the meta-data list and
- * rest of the buffer headers on the empty list
- */
- if (--metabuf)
- whichq = BQ_META;
- else
- whichq = BQ_EMPTY;
-
BLISTNONE(bp);
- dp = &bufqueues[whichq];
- binsheadfree(bp, dp, whichq);
+ dp = &bufqueues[BQ_EMPTY];
+ bp->b_whichq = BQ_EMPTY;
+ bp->b_timestamp = buf_timestamp();
+ binsheadfree(bp, dp, BQ_EMPTY);
binshash(bp, &invalhash);
}
+ boot_nbuf_headers = nbuf_headers;
- boot_nbuf = nbuf;
+ TAILQ_INIT(&iobufqueue);
+ TAILQ_INIT(&delaybufqueue);
- for (; i < nbuf + niobuf; i++) {
- bp = &buf[i];
+ for (; i < nbuf_headers + niobuf_headers; i++) {
+ bp = &buf_headers[i];
bufhdrinit(bp);
+ bp->b_whichq = -1;
binsheadfree(bp, &iobufqueue, -1);
}
- /*
+ /*
* allocate lock group attribute and group
*/
- buf_mtx_grp_attr = lck_grp_attr_alloc_init();
+ buf_mtx_grp_attr = lck_grp_attr_alloc_init();
buf_mtx_grp = lck_grp_alloc_init("buffer cache", buf_mtx_grp_attr);
/*
cluster_init();
printf("using %d buffer headers and %d cluster IO buffer headers\n",
- nbuf, niobuf);
+ nbuf_headers, niobuf_headers);
/* Set up zones used by the buffer cache */
bufzoneinit();
/* start the bcleanbuf() thread */
bcleanbuf_thread_init();
-#if BALANCE_QUEUES
- {
- static void bufq_balance_thread_init();
- /* create a thread to do dynamic buffer queue balancing */
- bufq_balance_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");
+ }
+
+}
+
+/*
+ * Zones for the meta data buffers
+ */
+
+#define MINMETA 512
+#define MAXMETA 8192
+
+struct meta_zone_entry {
+ zone_t mz_zone;
+ vm_size_t mz_size;
+ vm_size_t mz_max;
+ const char *mz_name;
+};
+
+struct meta_zone_entry meta_zones[] = {
+ {NULL, (MINMETA * 1), 128 * (MINMETA * 1), "buf.512" },
+ {NULL, (MINMETA * 2), 64 * (MINMETA * 2), "buf.1024" },
+ {NULL, (MINMETA * 4), 16 * (MINMETA * 4), "buf.2048" },
+ {NULL, (MINMETA * 8), 512 * (MINMETA * 8), "buf.4096" },
+ {NULL, (MINMETA * 16), 512 * (MINMETA * 16), "buf.8192" },
+ {NULL, 0, 0, "" } /* End */
+};
+
+/*
+ * Initialize the meta data zones
+ */
+static void
+bufzoneinit(void)
+{
+ int i;
+
+ for (i = 0; meta_zones[i].mz_size != 0; i++) {
+ meta_zones[i].mz_zone =
+ zinit(meta_zones[i].mz_size,
+ 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
+getbufzone(size_t size)
+{
+ int i;
+
+ if ((size % 512) || (size < MINMETA) || (size > MAXMETA))
+ panic("getbufzone: incorect size = %lu", size);
+
+ for (i = 0; meta_zones[i].mz_size != 0; i++) {
+ if (meta_zones[i].mz_size >= size)
+ break;
}
-#endif /* notyet */
+
+ return (meta_zones[i].mz_zone);
}
+
+
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;
/* Start I/O for the buffer (keeping credentials). */
SET(bp->b_flags, B_READ | async);
- if (cred != NOCRED && bp->b_rcred == NOCRED) {
+ if (IS_VALID_CRED(cred) && !IS_VALID_CRED(bp->b_rcred)) {
kauth_cred_ref(cred);
bp->b_rcred = cred;
}
trace(TR_BREADMISS, pack(vp, size), blkno);
/* Pay for the read. */
- if (p && p->p_stats)
- p->p_stats->p_ru.ru_inblock++; /* XXX */
+ if (p && p->p_stats) {
+ OSIncrementAtomicLong(&p->p_stats->p_ru.ru_inblock); /* XXX */
+ }
if (async) {
/*
*/
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;
* 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;
* 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;
* 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));
}
* [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));
}
CLR(bp->b_flags, (B_READ | B_DONE | B_ERROR | B_DELWRI));
if (wasdelayed)
- OSAddAtomic(-1, &nbdwrite);
+ OSAddAtomicLong(-1, &nbdwrite);
if (!sync) {
/*
*/
if (wasdelayed)
buf_reassign(bp, vp);
- else
- if (p && p->p_stats)
- p->p_stats->p_ru.ru_oublock++; /* XXX */
+ else
+ if (p && p->p_stats) {
+ OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock); /* XXX */
+ }
}
trace(TR_BUFWRITE, pack(vp, bp->b_bcount), bp->b_lblkno);
if (wasdelayed)
buf_reassign(bp, vp);
else
- if (p && p->p_stats)
- p->p_stats->p_ru.ru_oublock++; /* XXX */
+ if (p && p->p_stats) {
+ OSIncrementAtomicLong(&p->p_stats->p_ru.ru_oublock); /* XXX */
+ }
/* Release the buffer. */
- // XXXdbg - only if the unused bit is set
- if (!ISSET(bp->b_flags, B_NORELSE)) {
- buf_brelse(bp);
- } else {
- CLR(bp->b_flags, B_NORELSE);
- }
+ buf_brelse(bp);
return (rv);
} else {
}
int
-vn_bwrite(ap)
- struct vnop_bwrite_args *ap;
+vn_bwrite(struct vnop_bwrite_args *ap)
{
return (buf_bwrite(ap->a_bp));
}
*
* 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
+int
bdwrite_internal(buf_t bp, int return_error)
{
proc_t p = current_proc();
*/
if (!ISSET(bp->b_flags, B_DELWRI)) {
SET(bp->b_flags, B_DELWRI);
- if (p && p->p_stats)
- p->p_stats->p_ru.ru_oublock++; /* XXX */
- OSAddAtomic(1, &nbdwrite);
+ if (p && p->p_stats) {
+ 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
* buffer is part of a transaction and can't go to disk until
* the LOCKED bit is cleared.
*/
- if (!ISSET(bp->b_flags, B_LOCKED) && nbdwrite > ((nbuf/4)*3)) {
+ if (!ISSET(bp->b_flags, B_LOCKED) && nbdwrite > ((nbuf_headers/4)*3)) {
if (return_error)
return (EAGAIN);
/*
* If the vnode has "too many" write operations in progress
* wait for them to finish the IO
*/
- (void)vnode_waitforwrites(vp, VNODE_ASYNC_THROTTLE, 0, 0, (char *)"buf_bdwrite");
+ (void)vnode_waitforwrites(vp, VNODE_ASYNC_THROTTLE, 0, 0, "buf_bdwrite");
return (buf_bawrite(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
+ int need_wakeup = 0;
+
+ lck_mtx_lock_spin(buf_mtxp);
+
+ __IGNORE_WCASTALIGN(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;
+ }
+ if (ISSET(bp_head->b_lflags, BL_WANTED_REF)) {
+ CLR(bp_head->b_lflags, BL_WANTED_REF);
+ need_wakeup = 1;
+ }
+ }
+ lck_mtx_unlock(buf_mtxp);
+
+ if (need_wakeup)
+ wakeup(bp_head);
+
+#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).
if (bp->b_whichq != -1 || !(bp->b_lflags & BL_BUSY))
- panic("buf_brelse: bad buffer = %x\n", bp);
+ panic("buf_brelse: bad buffer = %p\n", bp);
#ifdef JOE_DEBUG
- bp->b_stackbrelse[0] = __builtin_return_address(0);
- bp->b_stackbrelse[1] = __builtin_return_address(1);
- bp->b_stackbrelse[2] = __builtin_return_address(2);
- bp->b_stackbrelse[3] = __builtin_return_address(3);
- bp->b_stackbrelse[4] = __builtin_return_address(4);
- bp->b_stackbrelse[5] = __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);
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;
bp->b_transaction = NULL;
if (iodone_func == NULL) {
- panic("brelse: bp @ 0x%x has NULL b_iodone!\n", bp);
+ panic("brelse: bp @ %p has NULL b_iodone!\n", bp);
}
(*iodone_func)(bp, arg);
}
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),
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 {
}
} else {
if ( (upl) )
- panic("brelse: UPL set for non VREG; vp=%x", bp->b_vp);
+ panic("brelse: UPL set for non VREG; vp=%p", bp->b_vp);
}
/*
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
- * and put on the head of the appropriate queue.
+ * 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 (bp->b_vp)
- brelvp(bp);
-
if (ISSET(bp->b_flags, B_DELWRI))
- OSAddAtomic(-1, &nbdwrite);
+ OSAddAtomicLong(-1, &nbdwrite);
- CLR(bp->b_flags, (B_DELWRI | B_LOCKED | B_AGE | B_ASYNC | B_NOCACHE));
+ if (ISSET(bp->b_flags, B_META)) {
+ if (bp->b_shadow_ref)
+ delayed_buf_free_meta_store = TRUE;
+ else
+ buf_free_meta_store(bp);
+ }
/*
- * Determine which queue the buffer should be on, then put it there.
+ * nuke any credentials we were holding
*/
- if (bp->b_bufsize <= 0)
- whichq = BQ_EMPTY; /* no data */
- else if (ISSET(bp->b_flags, B_META))
- whichq = BQ_META; /* meta-data */
- else
- whichq = BQ_AGE; /* invalid data */
- bufq = &bufqueues[whichq];
+ buf_release_credentials(bp);
- lck_mtx_lock(buf_mtxp);
+ lck_mtx_lock_spin(buf_mtxp);
+
+ if (bp->b_shadow_ref) {
+ SET(bp->b_lflags, BL_WAITSHADOW);
+
+ lck_mtx_unlock(buf_mtxp);
+
+ return;
+ }
+ if (delayed_buf_free_meta_store == TRUE) {
- binsheadfree(bp, bufq, whichq);
+ lck_mtx_unlock(buf_mtxp);
+finish_shadow_master:
+ buf_free_meta_store(bp);
+
+ 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);
+
+ bremhash(bp);
+ BLISTNONE(bp);
+ binshash(bp, &invalhash);
+
+ 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.
whichq = BQ_LRU; /* valid data */
bufq = &bufqueues[whichq];
- CLR(bp->b_flags, (B_AGE | B_ASYNC | B_NOCACHE));
+ bp->b_timestamp = buf_timestamp();
- lck_mtx_lock(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) {
/*
* Unlock the buffer.
*/
CLR(bp->b_lflags, (BL_BUSY | BL_WANTED));
+ buf_busycount--;
lck_mtx_unlock(buf_mtxp);
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);
}
/*
incore(vnode_t vp, daddr64_t blkno)
{
boolean_t retval;
+ struct bufhashhdr *dp;
- lck_mtx_lock(buf_mtxp);
+ dp = BUFHASH(vp, blkno);
+
+ lck_mtx_lock_spin(buf_mtxp);
- if (incore_locked(vp, blkno))
+ if (incore_locked(vp, blkno, dp))
retval = TRUE;
else
retval = FALSE;
static buf_t
-incore_locked(vnode_t vp, daddr64_t blkno)
+incore_locked(vnode_t vp, daddr64_t blkno, struct bufhashhdr *dp)
{
struct buf *bp;
- bp = BUFHASH(vp, blkno)->lh_first;
-
/* Search hash chain */
- for (; bp != NULL; bp = bp->b_hash.le_next) {
+ for (bp = dp->lh_first; bp != NULL; bp = bp->b_hash.le_next) {
if (bp->b_lblkno == blkno && bp->b_vp == vp &&
!ISSET(bp->b_flags, B_INVAL)) {
return (bp);
}
}
- return (0);
+ return (NULL);
}
-/* XXX FIXME -- Update the comment to reflect the UBC changes (please) -- */
-/*
- * Get a block of requested size that is associated with
- * a given vnode and block offset. If it is found in the
- * block cache, mark it as having been found, make it busy
- * and return it. Otherwise, return an empty block of the
- * correct size. It is up to the caller to insure that the
- * cached blocks be of the correct size.
- */
-buf_t
+void
+buf_wait_for_shadow_io(vnode_t vp, daddr64_t blkno)
+{
+ buf_t bp;
+ struct bufhashhdr *dp;
+
+ dp = BUFHASH(vp, blkno);
+
+ lck_mtx_lock_spin(buf_mtxp);
+
+ for (;;) {
+ if ((bp = incore_locked(vp, blkno, dp)) == NULL)
+ break;
+
+ if (bp->b_shadow_ref == 0)
+ break;
+
+ SET(bp->b_lflags, BL_WANTED_REF);
+
+ (void) msleep(bp, buf_mtxp, PSPIN | (PRIBIO+1), "buf_wait_for_shadow", NULL);
+ }
+ lck_mtx_unlock(buf_mtxp);
+}
+
+/* XXX FIXME -- Update the comment to reflect the UBC changes (please) -- */
+/*
+ * Get a block of requested size that is associated with
+ * a given vnode and block offset. If it is found in the
+ * block cache, mark it as having been found, make it busy
+ * and return it. Otherwise, return an empty block of the
+ * correct size. It is up to the caller to insure that the
+ * cached blocks be of the correct size.
+ */
+buf_t
buf_getblk(vnode_t vp, daddr64_t blkno, int size, int slpflag, int slptimeo, int operation)
{
buf_t bp;
int ret_only_valid;
struct timespec ts;
int upl_flags;
+ 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(buf_mtxp);
-start_locked:
- if ((bp = incore_locked(vp, blkno))) {
+ lck_mtx_lock_spin(buf_mtxp);
+
+ if ((bp = incore_locked(vp, blkno, dp))) {
/*
* Found in the Buffer Cache
*/
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);
/*
return (NULL);
goto start;
/*NOTREACHED*/
- break;
default:
/*
break;
}
} else {
+ int clear_bdone;
+
/*
* buffer in core and not busy
*/
- if ( (bp->b_upl) )
- panic("buffer has UPL, but not marked BUSY: %x", bp);
SET(bp->b_lflags, BL_BUSY);
SET(bp->b_flags, B_CACHE);
-#ifdef JOE_DEBUG
- bp->b_owner = current_thread();
- bp->b_tag = 1;
-#endif
+ buf_busycount++;
+
bremfree_locked(bp);
bufstats.bufs_incore++;
lck_mtx_unlock(buf_mtxp);
+#ifdef JOE_DEBUG
+ bp->b_owner = current_thread();
+ bp->b_tag = 1;
+#endif
+ if ( (bp->b_upl) )
+ panic("buffer has UPL, but not marked BUSY: %p", bp);
- if ( !ret_only_valid)
- allocbuf(bp, size);
+ clear_bdone = FALSE;
+ if (!ret_only_valid) {
+ /*
+ * If the number bytes that are valid is going
+ * to increase (even if we end up not doing a
+ * reallocation through allocbuf) we have to read
+ * the new size first.
+ *
+ * This is required in cases where we doing a read
+ * modify write of a already valid data on disk but
+ * in cases where the data on disk beyond (blkno + b_bcount)
+ * is invalid, we may end up doing extra I/O.
+ */
+ if (operation == BLK_META && bp->b_bcount < size) {
+ /*
+ * Since we are going to read in the whole size first
+ * we first have to ensure that any pending delayed write
+ * is flushed to disk first.
+ */
+ if (ISSET(bp->b_flags, B_DELWRI)) {
+ CLR(bp->b_flags, B_CACHE);
+ buf_bwrite(bp);
+ goto start;
+ }
+ /*
+ * clear B_DONE before returning from
+ * this function so that the caller can
+ * can issue a read for the new size.
+ */
+ clear_bdone = TRUE;
+ }
+
+ if (bp->b_bufsize != size)
+ allocbuf(bp, size);
+ }
upl_flags = 0;
switch (operation) {
} 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);
/*NOTREACHED*/
break;
}
+
+ if (clear_bdone)
+ CLR(bp->b_flags, B_DONE);
}
} else { /* not incore() */
int queue = BQ_EMPTY; /* Start with no preference */
lck_mtx_unlock(buf_mtxp);
return (NULL);
}
-
- if ((UBCINVALID(vp)) || !(UBCINFOEXISTS(vp)))
+ 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...
* the hash... if we see it incore at this point we dump
* the buffer we were working on and start over
*/
- if (incore_locked(vp, blkno)) {
+ if (incore_locked(vp, blkno, dp)) {
SET(bp->b_flags, B_INVAL);
binshash(bp, &invalhash);
*/
binshash(bp, BUFHASH(vp, blkno));
- lck_mtx_unlock(buf_mtxp);
+ bgetvp_locked(vp, bp);
- bgetvp(vp, bp);
+ lck_mtx_unlock(buf_mtxp);
allocbuf(bp, size);
* 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:
size_t contig_bytes;
int bmap_flags;
+#if DEVELOPMENT || DEBUG
+ /*
+ * Apple implemented file systems use UBC excludively; they should
+ * not call in here."
+ */
+ const char* excldfs[] = {"hfs", "afpfs", "smbfs", "acfs",
+ "exfat", "msdos", "webdav", NULL};
+
+ for (int i = 0; excldfs[i] != NULL; i++) {
+ if (vp->v_mount &&
+ !strcmp(vp->v_mount->mnt_vfsstat.f_fstypename,
+ excldfs[i])) {
+ panic("%s %s calls buf_getblk",
+ excldfs[i],
+ operation == BLK_READ ? "BLK_READ" : "BLK_WRITE");
+ }
+ }
+#endif
+
if ( (bp->b_upl) )
- panic("bp already has UPL: %x",bp);
+ panic("bp already has UPL: %p",bp);
f_offset = ubc_blktooff(vp, blkno);
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;
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;
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);
}
}
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] = __builtin_return_address(0);
- bp->b_stackgetblk[1] = __builtin_return_address(1);
- bp->b_stackgetblk[2] = __builtin_return_address(2);
- bp->b_stackgetblk[3] = __builtin_return_address(3);
- bp->b_stackgetblk[4] = __builtin_return_address(4);
- bp->b_stackgetblk[5] = __builtin_return_address(5);
+ (void) OSBacktrace(&bp->b_stackgetblk[0], 6);
#endif
return (bp);
}
* Get an empty, disassociated buffer of given size.
*/
buf_t
-buf_geteblk(size)
- int size;
+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
return (bp);
}
-/*
- * Zones for the meta data buffers
- */
+uint32_t
+buf_redundancy_flags(buf_t bp)
+{
+ return bp->b_redundancy_flags;
+}
-#define MINMETA 512
-#define MAXMETA 4096
+void
+buf_set_redundancy_flags(buf_t bp, uint32_t flags)
+{
+ SET(bp->b_redundancy_flags, flags);
+}
-struct meta_zone_entry {
- zone_t mz_zone;
- vm_size_t mz_size;
- vm_size_t mz_max;
- char *mz_name;
-};
+void
+buf_clear_redundancy_flags(buf_t bp, uint32_t flags)
+{
+ CLR(bp->b_redundancy_flags, flags);
+}
-struct meta_zone_entry meta_zones[] = {
- {NULL, (MINMETA * 1), 128 * (MINMETA * 1), "buf.512" },
- {NULL, (MINMETA * 2), 64 * (MINMETA * 2), "buf.1024" },
- {NULL, (MINMETA * 4), 16 * (MINMETA * 4), "buf.2048" },
- {NULL, (MINMETA * 8), 512 * (MINMETA * 8), "buf.4096" },
- {NULL, 0, 0, "" } /* End */
-};
-/*
- * Initialize the meta data zones
- */
-static void
-bufzoneinit(void)
+
+static void *
+recycle_buf_from_pool(int nsize)
{
- int i;
+ buf_t bp;
+ void *ptr = NULL;
- for (i = 0; meta_zones[i].mz_size != 0; i++) {
- meta_zones[i].mz_zone =
- zinit(meta_zones[i].mz_size,
- meta_zones[i].mz_max,
- PAGE_SIZE,
- meta_zones[i].mz_name);
+ lck_mtx_lock_spin(buf_mtxp);
+
+ TAILQ_FOREACH(bp, &bufqueues[BQ_META], b_freelist) {
+ if (ISSET(bp->b_flags, B_DELWRI) || bp->b_bufsize != nsize)
+ continue;
+ ptr = (void *)bp->b_datap;
+ bp->b_bufsize = 0;
+
+ bcleanbuf(bp, TRUE);
+ break;
}
- buf_hdr_zone = zinit(sizeof(struct buf), 32, PAGE_SIZE, "buf headers");
+ lck_mtx_unlock(buf_mtxp);
+
+ return (ptr);
}
-static __inline__ zone_t
-getbufzone(size_t size)
+
+
+int zalloc_nopagewait_failed = 0;
+int recycle_buf_failed = 0;
+
+static void *
+grab_memory_for_meta_buf(int nsize)
{
- int i;
+ zone_t z;
+ void *ptr;
+ boolean_t was_vmpriv;
- if ((size % 512) || (size < MINMETA) || (size > MAXMETA))
- panic("getbufzone: incorect size = %d", size);
+ z = getbufzone(nsize);
- for (i = 0; meta_zones[i].mz_size != 0; i++) {
- if (meta_zones[i].mz_size >= size)
- break;
- }
+ /*
+ * make sure we're NOT priviliged so that
+ * if a vm_page_grab is needed, it won't
+ * block if we're out of free pages... if
+ * it blocks, then we can't honor the
+ * nopagewait request
+ */
+ was_vmpriv = set_vm_privilege(FALSE);
- return (meta_zones[i].mz_zone);
+ ptr = zalloc_nopagewait(z);
+
+ if (was_vmpriv == TRUE)
+ set_vm_privilege(TRUE);
+
+ if (ptr == NULL) {
+
+ zalloc_nopagewait_failed++;
+
+ ptr = recycle_buf_from_pool(nsize);
+
+ if (ptr == NULL) {
+
+ recycle_buf_failed++;
+
+ if (was_vmpriv == FALSE)
+ set_vm_privilege(TRUE);
+
+ ptr = zalloc(z);
+
+ if (was_vmpriv == FALSE)
+ set_vm_privilege(FALSE);
+ }
+ }
+ return (ptr);
}
/*
panic("allocbuf: buffer larger than MAXBSIZE requested");
if (ISSET(bp->b_flags, B_META)) {
- zone_t zprev, z;
int nsize = roundup(size, MINMETA);
if (bp->b_datap) {
if (ISSET(bp->b_flags, B_ZALLOC)) {
if (bp->b_bufsize < nsize) {
+ zone_t zprev;
+
/* reallocate to a bigger size */
zprev = getbufzone(bp->b_bufsize);
if (nsize <= MAXMETA) {
desired_size = nsize;
- z = getbufzone(nsize);
- bp->b_datap = (uintptr_t)zalloc(z);
+
+ /* b_datap not really a ptr */
+ *(void **)(&bp->b_datap) = grab_memory_for_meta_buf(nsize);
} 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, VM_KERN_MEMORY_FILE);
CLR(bp->b_flags, B_ZALLOC);
}
bcopy((void *)elem, (caddr_t)bp->b_datap, bp->b_bufsize);
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, VM_KERN_MEMORY_FILE);
bcopy((const void *)elem, (caddr_t)bp->b_datap, bp->b_bufsize);
kmem_free(kernel_map, elem, bp->b_bufsize);
} else {
/* new allocation */
if (nsize <= MAXMETA) {
desired_size = nsize;
- z = getbufzone(nsize);
- bp->b_datap = (uintptr_t)zalloc(z);
+
+ /* b_datap not really a ptr */
+ *(void **)(&bp->b_datap) = grab_memory_for_meta_buf(nsize);
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, VM_KERN_MEMORY_FILE);
}
+
+ if (bp->b_datap == 0)
+ panic("allocbuf: NULL b_datap");
}
bp->b_bufsize = desired_size;
bp->b_bcount = 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
/*
* invalid request gets empty queue
*/
- if ((*queue > BQUEUES) || (*queue < 0)
+ if ((*queue >= BQUEUES) || (*queue < 0)
|| (*queue == BQ_LAUNDRY) || (*queue == BQ_LOCKED))
*queue = BQ_EMPTY;
- /* need to grow number of bufs, add another one rather than recycling */
- if (nbuf < max_nbuf_headers) {
+
+
+ if (*queue == BQ_EMPTY && (bp = bufqueues[*queue].tqh_first))
+ goto found;
+
+ /*
+ * need to grow number of bufs, add another one rather than recycling
+ */
+ if (nbuf_headers < max_nbuf_headers) {
/*
* Increment count now as lock
* is dropped for allocation.
* That avoids over commits
*/
- nbuf++;
+ nbuf_headers++;
goto add_newbufs;
}
-
- /*
- * (*queue == BQUEUES) means no preference
- */
- if (*queue != BQUEUES) {
- /* Try for the requested queue first */
- bp = bufqueues[*queue].tqh_first;
- if (bp)
- goto found;
- }
+ /* Try for the requested queue first */
+ bp = bufqueues[*queue].tqh_first;
+ if (bp)
+ goto found;
/* Unable to use requested queue */
age_bp = bufqueues[BQ_AGE].tqh_first;
/* Create a new temporary buffer header */
bp = (struct buf *)zalloc(buf_hdr_zone);
-
- lck_mtx_lock(buf_mtxp);
-
+
if (bp) {
bufhdrinit(bp);
+ bp->b_whichq = BQ_EMPTY;
+ bp->b_timestamp = buf_timestamp();
BLISTNONE(bp);
- binshash(bp, &invalhash);
SET(bp->b_flags, B_HDRALLOC);
*queue = BQ_EMPTY;
+ }
+ lck_mtx_lock_spin(buf_mtxp);
+
+ if (bp) {
+ binshash(bp, &invalhash);
binsheadfree(bp, &bufqueues[BQ_EMPTY], BQ_EMPTY);
buf_hdr_count++;
goto found;
}
/* subtract already accounted bufcount */
- nbuf--;
+ nbuf_headers--;
bufstats.bufs_sleeps++;
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), (char *)"getnewbuf", &ts);
- return (0);
+
+ msleep(&needbuffer, buf_mtxp, slpflag | PDROP | (PRIBIO+1), "getnewbuf", &ts);
+ return (NULL);
}
/* Buffer available either on AGE or LRU or META */
}
found:
if (ISSET(bp->b_flags, B_LOCKED) || ISSET(bp->b_lflags, BL_BUSY))
- panic("getnewbuf: bp @ 0x%x is LOCKED or BUSY! (flags 0x%x)\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
*/
/*
* 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)
{
- ucred_t cred;
-
-
/* Remove from the queue */
bremfree_locked(bp);
- /* Buffer is no longer on free lists. */
- SET(bp->b_lflags, BL_BUSY);
#ifdef JOE_DEBUG
bp->b_owner = current_thread();
bp->b_tag = 2;
* it on the LAUNDRY queue, and return 1
*/
if (ISSET(bp->b_flags, B_DELWRI)) {
- binstailfree(bp, &bufqueues[BQ_LAUNDRY], BQ_LAUNDRY);
- blaundrycnt++;
+ if (discard) {
+ SET(bp->b_lflags, BL_WANTDEALLOC);
+ }
+
+ bmovelaundry(bp);
lck_mtx_unlock(buf_mtxp);
- wakeup(&blaundrycnt);
- /* and give it a chance to run */
+ wakeup(&bufqueues[BQ_LAUNDRY]);
+ /*
+ * and give it a chance to run
+ */
(void)thread_block(THREAD_CONTINUE_NULL);
- lck_mtx_lock(buf_mtxp);
+ lck_mtx_lock_spin(buf_mtxp);
+
return (1);
}
+#ifdef JOE_DEBUG
+ bp->b_owner = current_thread();
+ bp->b_tag = 8;
+#endif
+ /*
+ * Buffer is no longer on any free list... we own it
+ */
+ SET(bp->b_lflags, BL_BUSY);
+ buf_busycount++;
+
bremhash(bp);
- lck_mtx_unlock(buf_mtxp);
-
- BLISTNONE(bp);
/*
* disassociate us from our vnode, if we had one...
*/
if (bp->b_vp)
- brelvp(bp);
-
- if (ISSET(bp->b_flags, B_META)) {
- vm_offset_t elem;
+ brelvp_locked(bp);
- elem = (vm_offset_t)bp->b_datap;
- bp->b_datap = (uintptr_t)0xdeadbeef;
+ lck_mtx_unlock(buf_mtxp);
- if (ISSET(bp->b_flags, B_ZALLOC)) {
- zone_t z;
+ BLISTNONE(bp);
- 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;
+ bp->b_fsprivate = (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;
-
- /* nuke any credentials we were holding */
- cred = bp->b_rcred;
- if (cred != NOCRED) {
- bp->b_rcred = NOCRED;
- kauth_cred_rele(cred);
- }
- cred = bp->b_wcred;
- if (cred != NOCRED) {
- bp->b_wcred = NOCRED;
- kauth_cred_rele(cred);
+ bp->b_lflags = BL_BUSY;
+ bp->b_flags = (bp->b_flags & B_HDRALLOC);
+ bp->b_redundancy_flags = 0;
+ 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;
+ bzero(&bp->b_attr, sizeof(struct bufattr));
+
+ lck_mtx_lock_spin(buf_mtxp);
}
- lck_mtx_lock(buf_mtxp);
-
return (0);
}
{
buf_t bp;
errno_t error;
+ struct bufhashhdr *dp;
+
+ dp = BUFHASH(vp, lblkno);
- lck_mtx_lock(buf_mtxp);
relook:
- if ((bp = incore_locked(vp, lblkno)) == (struct buf *)0) {
+ lck_mtx_lock_spin(buf_mtxp);
+
+ if ((bp = incore_locked(vp, lblkno, dp)) == (struct buf *)0) {
lck_mtx_unlock(buf_mtxp);
return (0);
}
}
SET(bp->b_lflags, BL_WANTED);
- error = msleep((caddr_t)bp, buf_mtxp, (PRIBIO + 1), (char *)"buf_invalblkno", 0);
+ error = msleep((caddr_t)bp, buf_mtxp, PDROP | (PRIBIO + 1), "buf_invalblkno", NULL);
- if (error)
+ if (error) {
return (error);
+ }
goto 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;
{
int need_wakeup = 0;
- lck_mtx_lock(buf_mtxp);
+ lck_mtx_lock_spin(buf_mtxp);
if (ISSET(bp->b_lflags, BL_WANTED)) {
/*
*/
need_wakeup = 1;
}
+#ifdef JOE_DEBUG
+ bp->b_owner = current_thread();
+ bp->b_tag = 9;
+#endif
/*
* Unlock the buffer.
*/
CLR(bp->b_lflags, (BL_BUSY | BL_WANTED));
+ buf_busycount--;
lck_mtx_unlock(buf_mtxp);
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);
}
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
*/
/* the hz value is 100; which leads to 10ms */
ts.tv_sec = (slptimeo/100);
ts.tv_nsec = (slptimeo % 100) * 10 * NSEC_PER_USEC * 1000;
- error = msleep((caddr_t)bp, buf_mtxp, slpflag | (PRIBIO + 1), (char *)"buf_acquire", &ts);
+ error = msleep((caddr_t)bp, buf_mtxp, slpflag | (PRIBIO + 1), "buf_acquire", &ts);
if (error)
return (error);
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;
errno_t
buf_biowait(buf_t bp)
{
- lck_mtx_lock(buf_mtxp);
+ while (!ISSET(bp->b_flags, B_DONE)) {
- while (!ISSET(bp->b_flags, B_DONE))
- (void) msleep(bp, buf_mtxp, (PRIBIO+1), (char *)"buf_biowait", 0);
+ 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);
return (0);
}
+
/*
* Mark I/O complete on a buffer.
*
* (for swap pager, that puts swap buffers on the free lists (!!!),
* for the vn device, that puts malloc'd buffers on the free lists!)
*/
-extern struct timeval priority_IO_timestamp_for_root;
-extern int hard_throttle_on_root;
void
buf_biodone(buf_t bp)
{
+ mount_t mp;
+ struct bufattr *bap;
+
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");
- if (kdebug_enable) {
- int code = DKIO_DONE;
+ bap = &bp->b_attr;
+
+ if (bp->b_vp && bp->b_vp->v_mount) {
+ mp = bp->b_vp->v_mount;
+ } else {
+ mp = NULL;
+ }
+
+ if (ISSET(bp->b_flags, B_ERROR)) {
+ if (mp && (MNT_ROOTFS & mp->mnt_flag)) {
+ dk_error_description_t desc;
+ bzero(&desc, sizeof(desc));
+ desc.description = panic_disk_error_description;
+ desc.description_size = panic_disk_error_description_size;
+ VNOP_IOCTL(mp->mnt_devvp, DKIOCGETERRORDESCRIPTION, (caddr_t)&desc, 0, vfs_context_kernel());
+ }
+ }
+
+ 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);
+ }
+
+ throttle_info_end_io(bp);
+
+ if (kdebug_enable) {
+ int code = DKIO_DONE;
+ int io_tier = GET_BUFATTR_IO_TIER(bap);
if (bp->b_flags & B_READ)
code |= DKIO_READ;
else if (bp->b_flags & B_PAGEIO)
code |= DKIO_PAGING;
- KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_DKRW, code) | DBG_FUNC_NONE,
- (unsigned int)bp, (unsigned int)bp->b_vp,
- bp->b_resid, bp->b_error, 0);
+ if (io_tier != 0)
+ code |= DKIO_THROTTLE;
+
+ code |= ((io_tier << DKIO_TIER_SHIFT) & DKIO_TIER_MASK);
+
+ if (bp->b_flags & B_PASSIVE)
+ code |= DKIO_PASSIVE;
+
+ if (bap->ba_flags & BA_NOCACHE)
+ code |= DKIO_NOCACHE;
+
+ if (bap->ba_flags & BA_IO_TIER_UPGRADE) {
+ code |= DKIO_TIER_UPGRADE;
+ }
+
+ KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, FSDBG_CODE(DBG_DKRW, code) | DBG_FUNC_NONE,
+ buf_kernel_addrperm_addr(bp), (uintptr_t)VM_KERNEL_ADDRPERM(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_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_PASSIVE));
+ CLR(bap->ba_flags, (BA_META | BA_NOCACHE | BA_DELAYIDLESLEEP | BA_IO_TIER_UPGRADE));
+
+ SET_BUFATTR_IO_TIER(bap, 0);
+
+ DTRACE_IO1(done, buf_t, bp);
if (!ISSET(bp->b_flags, B_READ) && !ISSET(bp->b_flags, B_RAW))
/*
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 @ 0x%x 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)
- /*
- * assumes that the call back function takes
+ 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
*/
- goto biodone_done;
+ goto biodone_done;
+ }
/*
* in this case the call back function is acting
* strictly as a filter... it does not take
* they do get to run, their going to re-set
* BL_WANTED and go back to sleep
*/
- lck_mtx_lock(buf_mtxp);
+ lck_mtx_lock_spin(buf_mtxp);
CLR(bp->b_lflags, BL_WANTED);
SET(bp->b_flags, B_DONE); /* note that it's done */
}
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);
+}
+
+/*
+ * Obfuscate buf pointers.
+ */
+vm_offset_t
+buf_kernel_addrperm_addr(void * addr)
+{
+ if ((vm_offset_t)addr == 0)
+ return 0;
+ else
+ return ((vm_offset_t)addr + buf_kernel_addrperm);
}
/*
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)
/*
* Return a count of 'busy' buffers. Used at the time of shutdown.
+ * note: This is also called from the mach side in debug context in kdp.c
*/
int
count_busy_buffers(void)
{
- buf_t bp;
- int nbusy = 0;
-
- lck_mtx_lock(buf_mtxp);
- for (bp = &buf[boot_nbuf]; --bp >= buf; )
- 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
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" };
}
#endif /* DIAGNOSTIC */
-#define NRESERVEDIOBUFS 64
+#define NRESERVEDIOBUFS 128
+#define MNT_VIRTUALDEV_MAX_IOBUFS 16
+#define VIRTUALDEV_MAX_IOBUFS ((40*niobuf_headers)/100)
buf_t
alloc_io_buf(vnode_t vp, int priv)
{
buf_t bp;
+ mount_t mp = NULL;
+ int alloc_for_virtualdev = FALSE;
- lck_mtx_lock(iobuffer_mtxp);
+ lck_mtx_lock_spin(iobuffer_mtxp);
+
+ /*
+ * We subject iobuf requests for diskimages to additional restrictions.
+ *
+ * a) A single diskimage mount cannot use up more than
+ * MNT_VIRTUALDEV_MAX_IOBUFS. However,vm privileged (pageout) requests
+ * are not subject to this restriction.
+ * b) iobuf headers used by all diskimage headers by all mount
+ * points cannot exceed VIRTUALDEV_MAX_IOBUFS.
+ */
+ if (vp && ((mp = vp->v_mount)) && mp != dead_mountp &&
+ mp->mnt_kern_flag & MNTK_VIRTUALDEV) {
+ alloc_for_virtualdev = TRUE;
+ while ((!priv && mp->mnt_iobufinuse > MNT_VIRTUALDEV_MAX_IOBUFS) ||
+ bufstats.bufs_iobufinuse_vdev > VIRTUALDEV_MAX_IOBUFS) {
+ bufstats.bufs_iobufsleeps++;
+
+ need_iobuffer = 1;
+ (void)msleep(&need_iobuffer, iobuffer_mtxp,
+ PSPIN | (PRIBIO+1), (const char *)"alloc_io_buf (1)",
+ NULL);
+ }
+ }
- while (((niobuf - NRESERVEDIOBUFS < bufstats.bufs_iobufinuse) && !priv) ||
+ 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", 0);
+ (void)msleep(&need_iobuffer, iobuffer_mtxp, PSPIN | (PRIBIO+1),
+ (const char *)"alloc_io_buf (2)", NULL);
}
TAILQ_REMOVE(&iobufqueue, bp, b_freelist);
if (bufstats.bufs_iobufinuse > bufstats.bufs_iobufmax)
bufstats.bufs_iobufmax = bufstats.bufs_iobufinuse;
+ if (alloc_for_virtualdev) {
+ mp->mnt_iobufinuse++;
+ bufstats.bufs_iobufinuse_vdev++;
+ }
+
lck_mtx_unlock(iobuffer_mtxp);
/*
bp->b_datap = 0;
bp->b_flags = 0;
bp->b_lflags = BL_BUSY | BL_IOBUF;
+ if (alloc_for_virtualdev)
+ bp->b_lflags |= BL_IOBUF_VDEV;
+ bp->b_redundancy_flags = 0;
bp->b_blkno = bp->b_lblkno = 0;
#ifdef JOE_DEBUG
bp->b_owner = current_thread();
bp->b_bcount = 0;
bp->b_bufsize = 0;
bp->b_upl = NULL;
+ bp->b_fsprivate = (void *)NULL;
bp->b_vp = vp;
+ bzero(&bp->b_attr, sizeof(struct bufattr));
if (vp && (vp->v_type == VBLK || vp->v_type == VCHR))
bp->b_dev = vp->v_rdev;
void
free_io_buf(buf_t bp)
{
- int need_wakeup = 0;
+ int need_wakeup = 0;
+ int free_for_virtualdev = FALSE;
+ mount_t mp = NULL;
+
+ /* Was this iobuf for a diskimage ? */
+ if (bp->b_lflags & BL_IOBUF_VDEV) {
+ free_for_virtualdev = TRUE;
+ if (bp->b_vp)
+ mp = bp->b_vp->v_mount;
+ }
/*
* put buffer back on the head of the iobufqueue
bp->b_vp = NULL;
bp->b_flags = B_INVAL;
- lck_mtx_lock(iobuffer_mtxp);
+ /* Zero out the bufattr and its flags before relinquishing this iobuf */
+ bzero (&bp->b_attr, sizeof(struct bufattr));
+
+ lck_mtx_lock_spin(iobuffer_mtxp);
binsheadfree(bp, &iobufqueue, -1);
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--;
+ if (free_for_virtualdev) {
+ bufstats.bufs_iobufinuse_vdev--;
+ if (mp && mp != dead_mountp)
+ mp->mnt_iobufinuse--;
+ }
+
lck_mtx_unlock(iobuffer_mtxp);
if (need_wakeup)
}
+void
+buf_list_lock(void)
+{
+ lck_mtx_lock_spin(buf_mtxp);
+}
+
+void
+buf_list_unlock(void)
+{
+ lck_mtx_unlock(buf_mtxp);
+}
/*
* If getnewbuf() calls bcleanbuf() on the same thread
* there is a potential for stack overrun and deadlocks.
* So we always handoff the work to a worker thread for completion
*/
-#include <mach/mach_types.h>
-#include <mach/memory_object_types.h>
-#include <kern/sched_prim.h>
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);
+
+__attribute__((noreturn))
static void
bcleanbuf_thread(void)
{
int loopcnt = 0;
for (;;) {
- lck_mtx_lock(buf_mtxp);
+ lck_mtx_lock_spin(buf_mtxp);
- while (blaundrycnt == 0)
- (void)msleep((void *)&blaundrycnt, buf_mtxp, PRIBIO, "blaundry", 0);
-
- bp = TAILQ_FIRST(&bufqueues[BQ_LAUNDRY]);
+ 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
*/
bremfree_locked(bp);
- blaundrycnt--;
+
+ /*
+ * Buffer is no longer on any free list
+ */
+ SET(bp->b_lflags, BL_BUSY);
+ buf_busycount++;
+
+#ifdef JOE_DEBUG
+ bp->b_owner = current_thread();
+ bp->b_tag = 10;
+#endif
lck_mtx_unlock(buf_mtxp);
/*
error = bawrite_internal(bp, 0);
if (error) {
- lck_mtx_lock(buf_mtxp);
+ bp->b_whichq = BQ_LAUNDRY;
+ bp->b_timestamp = buf_timestamp();
+
+ lck_mtx_lock_spin(buf_mtxp);
binstailfree(bp, &bufqueues[BQ_LAUNDRY], BQ_LAUNDRY);
blaundrycnt++;
- lck_mtx_unlock(buf_mtxp);
+ /* 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
- if (loopcnt > 10) {
- (void)tsleep((void *)&blaundrycnt, PRIBIO, "blaundry", 1);
+ lck_mtx_unlock(buf_mtxp);
+
+ 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++;
}
}
}
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);
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;
+ struct bqueues privq;
+ int thresh_hold = BUF_STALE_THRESHHOLD;
+
+ if (all)
+ thresh_hold = 0;
+ /*
+ * We only care about metadata (incore storage comes from zalloc()).
+ * Unless "all" is set (used to evict meta data buffers in preparation
+ * for deep sleep), we only evict up to BUF_MAX_GC_BATCH_SIZE buffers
+ * that have not been accessed in the last BUF_STALE_THRESHOLD seconds.
+ * BUF_MAX_GC_BATCH_SIZE controls both the hold time of the global lock
+ * "buf_mtxp" and the length of time we spend compute bound in the GC
+ * thread which calls this function
+ */
+ 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);
+
+ } while (all && (found == BUF_MAX_GC_BATCH_SIZE));
+
+ lck_mtx_unlock(buf_mtxp);
+
+ return did_large_zfree;
+}
/*
bp->b_tag = 7;
#endif
SET(bp->b_lflags, BL_BUSY);
+ buf_busycount++;
+
flush_table[buf_count] = bp;
buf_count++;
total_writes++;
return (total_writes);
}
#endif
-
-
-#if BALANCE_QUEUES
-
-/* XXX move this to a separate file */
-
-/*
- * NOTE: THIS CODE HAS NOT BEEN UPDATED
- * WITH RESPECT TO THE NEW LOCKING MODEL
- */
-
-
-/*
- * Dynamic Scaling of the Buffer Queues
- */
-
-typedef long long blsize_t;
-
-blsize_t MAXNBUF; /* initialize to (sane_size / PAGE_SIZE) */
-/* Global tunable limits */
-blsize_t nbufh; /* number of buffer headers */
-blsize_t nbuflow; /* minimum number of buffer headers required */
-blsize_t nbufhigh; /* maximum number of buffer headers allowed */
-blsize_t nbuftarget; /* preferred number of buffer headers */
-
-/*
- * assertions:
- *
- * 1. 0 < nbuflow <= nbufh <= nbufhigh
- * 2. nbufhigh <= MAXNBUF
- * 3. 0 < nbuflow <= nbuftarget <= nbufhigh
- * 4. nbufh can not be set by sysctl().
- */
-
-/* Per queue tunable limits */
-
-struct bufqlim {
- blsize_t bl_nlow; /* minimum number of buffer headers required */
- blsize_t bl_num; /* number of buffer headers on the queue */
- blsize_t bl_nlhigh; /* maximum number of buffer headers allowed */
- blsize_t bl_target; /* preferred number of buffer headers */
- long bl_stale; /* Seconds after which a buffer is considered stale */
-} bufqlim[BQUEUES];
-
-/*
- * assertions:
- *
- * 1. 0 <= bl_nlow <= bl_num <= bl_nlhigh
- * 2. bl_nlhigh <= MAXNBUF
- * 3. bufqlim[BQ_META].bl_nlow != 0
- * 4. bufqlim[BQ_META].bl_nlow > (number of possible concurrent
- * file system IO operations)
- * 5. bl_num can not be set by sysctl().
- * 6. bl_nhigh <= nbufhigh
- */
-
-/*
- * Rationale:
- * ----------
- * Defining it blsize_t as long permits 2^31 buffer headers per queue.
- * Which can describe (2^31 * PAGE_SIZE) memory per queue.
- *
- * These limits are exported to by means of sysctl().
- * It was decided to define blsize_t as a 64 bit quantity.
- * This will make sure that we will not be required to change it
- * as long as we do not exceed 64 bit address space for the kernel.
- *
- * low and high numbers parameters initialized at compile time
- * and boot arguments can be used to override them. sysctl()
- * would not change the value. sysctl() can get all the values
- * but can set only target. num is the current level.
- *
- * Advantages of having a "bufqscan" thread doing the balancing are,
- * Keep enough bufs on BQ_EMPTY.
- * getnewbuf() by default will always select a buffer from the BQ_EMPTY.
- * getnewbuf() perfoms best if a buffer was found there.
- * Also this minimizes the possibility of starting IO
- * from getnewbuf(). That's a performance win, too.
- *
- * Localize complex logic [balancing as well as time aging]
- * to balancebufq().
- *
- * Simplify getnewbuf() logic by elimination of time aging code.
- */
-
-/*
- * Algorithm:
- * -----------
- * The goal of the dynamic scaling of the buffer queues to to keep
- * the size of the LRU close to bl_target. Buffers on a queue would
- * be time aged.
- *
- * There would be a thread which will be responsible for "balancing"
- * the buffer cache queues.
- *
- * The scan order would be: AGE, LRU, META, EMPTY.
- */
-
-long bufqscanwait = 0;
-
-static void bufqscan_thread();
-static int balancebufq(int q);
-static int btrimempty(int n);
-static __inline__ int initbufqscan(void);
-static __inline__ int nextbufq(int q);
-static void buqlimprt(int all);
-
-
-static __inline__ void
-bufqinc(int q)
-{
- if ((q < 0) || (q >= BQUEUES))
- return;
-
- bufqlim[q].bl_num++;
- return;
-}
-
-static __inline__ void
-bufqdec(int q)
-{
- if ((q < 0) || (q >= BQUEUES))
- return;
-
- bufqlim[q].bl_num--;
- return;
-}
-
-static void
-bufq_balance_thread_init()
-{
-
- if (bufqscanwait++ == 0) {
-
- /* Initalize globals */
- MAXNBUF = (sane_size / PAGE_SIZE);
- nbufh = nbuf;
- nbuflow = min(nbufh, 100);
- nbufhigh = min(MAXNBUF, max(nbufh, 2048));
- nbuftarget = (sane_size >> 5) / PAGE_SIZE;
- nbuftarget = max(nbuflow, nbuftarget);
- nbuftarget = min(nbufhigh, nbuftarget);
-
- /*
- * Initialize the bufqlim
- */
-
- /* LOCKED queue */
- bufqlim[BQ_LOCKED].bl_nlow = 0;
- bufqlim[BQ_LOCKED].bl_nlhigh = 32;
- bufqlim[BQ_LOCKED].bl_target = 0;
- bufqlim[BQ_LOCKED].bl_stale = 30;
-
- /* LRU queue */
- bufqlim[BQ_LRU].bl_nlow = 0;
- bufqlim[BQ_LRU].bl_nlhigh = nbufhigh/4;
- bufqlim[BQ_LRU].bl_target = nbuftarget/4;
- bufqlim[BQ_LRU].bl_stale = LRU_IS_STALE;
-
- /* AGE queue */
- bufqlim[BQ_AGE].bl_nlow = 0;
- bufqlim[BQ_AGE].bl_nlhigh = nbufhigh/4;
- bufqlim[BQ_AGE].bl_target = nbuftarget/4;
- bufqlim[BQ_AGE].bl_stale = AGE_IS_STALE;
-
- /* EMPTY queue */
- bufqlim[BQ_EMPTY].bl_nlow = 0;
- bufqlim[BQ_EMPTY].bl_nlhigh = nbufhigh/4;
- bufqlim[BQ_EMPTY].bl_target = nbuftarget/4;
- bufqlim[BQ_EMPTY].bl_stale = 600000;
-
- /* META queue */
- bufqlim[BQ_META].bl_nlow = 0;
- bufqlim[BQ_META].bl_nlhigh = nbufhigh/4;
- bufqlim[BQ_META].bl_target = nbuftarget/4;
- bufqlim[BQ_META].bl_stale = META_IS_STALE;
-
- /* LAUNDRY queue */
- bufqlim[BQ_LOCKED].bl_nlow = 0;
- bufqlim[BQ_LOCKED].bl_nlhigh = 32;
- bufqlim[BQ_LOCKED].bl_target = 0;
- bufqlim[BQ_LOCKED].bl_stale = 30;
-
- buqlimprt(1);
- }
-
- /* create worker thread */
- kernel_thread(kernel_task, bufqscan_thread);
-}
-
-/* The workloop for the buffer balancing thread */
-static void
-bufqscan_thread()
-{
- int moretodo = 0;
-
- for(;;) {
- do {
- int q; /* buffer queue to process */
-
- q = initbufqscan();
- for (; q; ) {
- moretodo |= balancebufq(q);
- q = nextbufq(q);
- }
- } while (moretodo);
-
-#if DIAGNOSTIC
- vfs_bufstats();
- buqlimprt(0);
-#endif
- (void)tsleep((void *)&bufqscanwait, PRIBIO, "bufqscanwait", 60 * hz);
- moretodo = 0;
- }
-}
-
-/* Seed for the buffer queue balancing */
-static __inline__ int
-initbufqscan()
-{
- /* Start with AGE queue */
- return (BQ_AGE);
-}
-
-/* Pick next buffer queue to balance */
-static __inline__ int
-nextbufq(int q)
-{
- int order[] = { BQ_AGE, BQ_LRU, BQ_META, BQ_EMPTY, 0 };
-
- q++;
- q %= sizeof(order);
- return (order[q]);
-}
-
-/* function to balance the buffer queues */
-static int
-balancebufq(int q)
-{
- int moretodo = 0;
- int s = splbio();
- int n, t;
-
- /* reject invalid q */
- if ((q < 0) || (q >= BQUEUES))
- goto out;
-
- /* LOCKED or LAUNDRY queue MUST not be balanced */
- if ((q == BQ_LOCKED) || (q == BQ_LAUNDRY))
- goto out;
-
- n = (bufqlim[q].bl_num - bufqlim[q].bl_target);
-
- /* If queue has less than target nothing more to do */
- if (n < 0)
- goto out;
-
- if ( n > 8 ) {
- /* Balance only a small amount (12.5%) at a time */
- n >>= 3;
- }
-
- /* EMPTY queue needs special handling */
- if (q == BQ_EMPTY) {
- moretodo |= btrimempty(n);
- goto out;
- }
-
- t = buf_timestamp():
-
- for (; n > 0; n--) {
- struct buf *bp = bufqueues[q].tqh_first;
- if (!bp)
- break;
-
- /* check if it's stale */
- if ((t - bp->b_timestamp) > bufqlim[q].bl_stale) {
- if (bcleanbuf(bp)) {
- /* buf_bawrite() issued, bp not ready */
- moretodo = 1;
- } else {
- /* release the cleaned buffer to BQ_EMPTY */
- SET(bp->b_flags, B_INVAL);
- buf_brelse(bp);
- }
- } else
- break;
- }
-
-out:
- splx(s);
- return (moretodo);
-}
-
-static int
-btrimempty(int n)
-{
- /*
- * When struct buf are allocated dynamically, this would
- * reclaim upto 'n' struct buf from the empty queue.
- */
-
- return (0);
-}
-
-static void
-buqlimprt(int all)
-{
- int i;
- static char *bname[BQUEUES] =
- { "LOCKED", "LRU", "AGE", "EMPTY", "META", "LAUNDRY" };
-
- if (all)
- for (i = 0; i < BQUEUES; i++) {
- printf("%s : ", bname[i]);
- printf("min = %ld, ", (long)bufqlim[i].bl_nlow);
- printf("cur = %ld, ", (long)bufqlim[i].bl_num);
- printf("max = %ld, ", (long)bufqlim[i].bl_nlhigh);
- printf("target = %ld, ", (long)bufqlim[i].bl_target);
- printf("stale after %ld seconds\n", bufqlim[i].bl_stale);
- }
- else
- for (i = 0; i < BQUEUES; i++) {
- printf("%s : ", bname[i]);
- printf("cur = %ld, ", (long)bufqlim[i].bl_num);
- }
-}
-
-#endif
-
-