2008-02-15 16:46:04 +03:00
|
|
|
/* $NetBSD: vfs_bio.c,v 1.188 2008/02/15 13:46:04 ad Exp $ */
|
2008-01-02 14:48:20 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2007 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Andrew Doran.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
1994-07-03 11:57:32 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1982, 1986, 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1994 Christopher G. Demetriou
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1994-07-03 11:57:32 +04:00
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)vfs_bio.c 8.6 (Berkeley) 1/11/94
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some references:
|
|
|
|
* Bach: The Design of the UNIX Operating System (Prentice Hall, 1986)
|
|
|
|
* Leffler, et al.: The Design and Implementation of the 4.3BSD
|
|
|
|
* UNIX Operating System (Addison Welley, 1989)
|
|
|
|
*/
|
|
|
|
|
2007-09-16 19:17:36 +04:00
|
|
|
#include <sys/cdefs.h>
|
2008-02-15 16:46:04 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: vfs_bio.c,v 1.188 2008/02/15 13:46:04 ad Exp $");
|
2007-09-16 19:17:36 +04:00
|
|
|
|
2006-04-05 04:52:16 +04:00
|
|
|
#include "fs_ffs.h"
|
2003-12-30 15:33:13 +03:00
|
|
|
#include "opt_bufcache.h"
|
2002-05-13 03:06:27 +04:00
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2003-12-30 15:33:13 +03:00
|
|
|
#include <sys/kernel.h>
|
1994-07-03 11:57:32 +04:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/resourcevar.h>
|
2003-12-30 15:33:13 +03:00
|
|
|
#include <sys/sysctl.h>
|
1995-04-10 04:46:51 +04:00
|
|
|
#include <sys/conf.h>
|
2006-05-15 01:15:11 +04:00
|
|
|
#include <sys/kauth.h>
|
2008-01-02 14:48:20 +03:00
|
|
|
#include <sys/intr.h>
|
|
|
|
#include <sys/cpu.h>
|
1996-04-22 05:38:12 +04:00
|
|
|
|
2000-11-27 11:39:39 +03:00
|
|
|
#include <uvm/uvm.h>
|
2000-11-15 01:16:38 +03:00
|
|
|
|
1999-11-15 21:49:07 +03:00
|
|
|
#include <miscfs/specfs/specdev.h>
|
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
#ifndef BUFPAGES
|
|
|
|
# define BUFPAGES 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BUFCACHE
|
|
|
|
# if (BUFCACHE < 5) || (BUFCACHE > 95)
|
|
|
|
# error BUFCACHE is not between 5 and 95
|
|
|
|
# endif
|
|
|
|
#else
|
2004-01-30 14:32:16 +03:00
|
|
|
# define BUFCACHE 15
|
2003-12-30 15:33:13 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
u_int nbuf; /* XXX - for softdep_lockedbufs */
|
|
|
|
u_int bufpages = BUFPAGES; /* optional hardwired count */
|
|
|
|
u_int bufcache = BUFCACHE; /* max % of RAM to use for buffer cache */
|
|
|
|
|
2004-09-18 20:01:03 +04:00
|
|
|
/* Function prototypes */
|
2004-09-18 20:37:12 +04:00
|
|
|
struct bqueue;
|
2004-09-18 20:01:03 +04:00
|
|
|
|
2004-10-04 04:46:05 +04:00
|
|
|
static void buf_setwm(void);
|
2004-09-18 20:01:03 +04:00
|
|
|
static int buf_trim(void);
|
|
|
|
static void *bufpool_page_alloc(struct pool *, int);
|
|
|
|
static void bufpool_page_free(struct pool *, void *);
|
2008-01-02 14:48:20 +03:00
|
|
|
static buf_t *bio_doread(struct vnode *, daddr_t, int,
|
2006-05-15 01:15:11 +04:00
|
|
|
kauth_cred_t, int);
|
2008-01-02 14:48:20 +03:00
|
|
|
static buf_t *getnewbuf(int, int, int);
|
2004-09-18 20:01:03 +04:00
|
|
|
static int buf_lotsfree(void);
|
|
|
|
static int buf_canrelease(void);
|
2008-01-02 14:48:20 +03:00
|
|
|
static u_long buf_mempoolidx(u_long);
|
|
|
|
static u_long buf_roundsize(u_long);
|
|
|
|
static void *buf_malloc(size_t);
|
2007-03-04 08:59:00 +03:00
|
|
|
static void buf_mrelease(void *, size_t);
|
2008-01-02 14:48:20 +03:00
|
|
|
static void binsheadfree(buf_t *, struct bqueue *);
|
|
|
|
static void binstailfree(buf_t *, struct bqueue *);
|
2004-09-18 20:01:03 +04:00
|
|
|
int count_lock_queue(void); /* XXX */
|
|
|
|
#ifdef DEBUG
|
2008-01-02 14:48:20 +03:00
|
|
|
static int checkfreelist(buf_t *, struct bqueue *);
|
2004-09-18 20:01:03 +04:00
|
|
|
#endif
|
2008-01-02 14:48:20 +03:00
|
|
|
static void biointr(void *);
|
|
|
|
static void biodone2(buf_t *);
|
|
|
|
static void bref(buf_t *);
|
|
|
|
static void brele(buf_t *);
|
2003-12-30 15:33:13 +03:00
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
/*
|
|
|
|
* Definitions for the buffer hash lists.
|
|
|
|
*/
|
|
|
|
#define BUFHASH(dvp, lbn) \
|
2000-11-27 11:39:39 +03:00
|
|
|
(&bufhashtbl[(((long)(dvp) >> 8) + (int)(lbn)) & bufhash])
|
1994-07-03 11:57:32 +04:00
|
|
|
LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash;
|
|
|
|
u_long bufhash;
|
2008-01-02 14:48:20 +03:00
|
|
|
struct bio_ops *bioopsp; /* I/O operation notification */
|
1994-07-03 11:57:32 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Definitions for the buffer free lists.
|
|
|
|
*/
|
2003-12-30 15:33:13 +03:00
|
|
|
#define BQUEUES 3 /* number of free buffer queues */
|
1994-07-03 11:57:32 +04:00
|
|
|
|
|
|
|
#define BQ_LOCKED 0 /* super-blocks &c */
|
|
|
|
#define BQ_LRU 1 /* lru, useful buffers */
|
|
|
|
#define BQ_AGE 2 /* rubbish */
|
|
|
|
|
2004-09-18 20:37:12 +04:00
|
|
|
struct bqueue {
|
2008-01-02 14:48:20 +03:00
|
|
|
TAILQ_HEAD(, buf) bq_queue;
|
|
|
|
uint64_t bq_bytes;
|
|
|
|
buf_t *bq_marker;
|
2004-09-18 20:37:12 +04:00
|
|
|
} bufqueues[BQUEUES];
|
2008-01-02 14:48:20 +03:00
|
|
|
|
|
|
|
static kcondvar_t needbuffer_cv;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2003-02-06 00:38:38 +03:00
|
|
|
/*
|
|
|
|
* Buffer queue lock.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
kmutex_t bufcache_lock;
|
|
|
|
kmutex_t buffer_lock;
|
2003-02-06 00:38:38 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
/* Software ISR for completed transfers. */
|
|
|
|
static void *biodone_sih;
|
2006-01-15 11:27:07 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
/* Buffer pool for I/O buffers. */
|
|
|
|
static pool_cache_t buf_cache;
|
|
|
|
static pool_cache_t bufio_cache;
|
2000-02-14 23:12:02 +03:00
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
/* XXX - somewhat gross.. */
|
|
|
|
#if MAXBSIZE == 0x2000
|
|
|
|
#define NMEMPOOLS 5
|
Add one more buffer pool, for 512-byte buffers. On the one hand, most
systems will never, ever need this -- because they use 8k/1k or even,
these days, 16k/2k or 32k/4k filesystems. On the other hand, when you
do need this, you *really* need it: on anoncvs.netbsd.org, for instance,
where /tmp is 4k/512 and the filesystem contains tens or even hundreds
of thousands of single-frag directories, this essentially doubles the
efficiency of the allocator. Since the overhead of keeping one extra
pool around is minimal, just add it by default.
2006-03-18 02:43:37 +03:00
|
|
|
#elif MAXBSIZE == 0x4000
|
2003-12-30 15:33:13 +03:00
|
|
|
#define NMEMPOOLS 6
|
Add one more buffer pool, for 512-byte buffers. On the one hand, most
systems will never, ever need this -- because they use 8k/1k or even,
these days, 16k/2k or 32k/4k filesystems. On the other hand, when you
do need this, you *really* need it: on anoncvs.netbsd.org, for instance,
where /tmp is 4k/512 and the filesystem contains tens or even hundreds
of thousands of single-frag directories, this essentially doubles the
efficiency of the allocator. Since the overhead of keeping one extra
pool around is minimal, just add it by default.
2006-03-18 02:43:37 +03:00
|
|
|
#elif MAXBSIZE == 0x8000
|
2003-12-30 15:33:13 +03:00
|
|
|
#define NMEMPOOLS 7
|
Add one more buffer pool, for 512-byte buffers. On the one hand, most
systems will never, ever need this -- because they use 8k/1k or even,
these days, 16k/2k or 32k/4k filesystems. On the other hand, when you
do need this, you *really* need it: on anoncvs.netbsd.org, for instance,
where /tmp is 4k/512 and the filesystem contains tens or even hundreds
of thousands of single-frag directories, this essentially doubles the
efficiency of the allocator. Since the overhead of keeping one extra
pool around is minimal, just add it by default.
2006-03-18 02:43:37 +03:00
|
|
|
#else
|
|
|
|
#define NMEMPOOLS 8
|
2003-12-30 15:33:13 +03:00
|
|
|
#endif
|
|
|
|
|
Add one more buffer pool, for 512-byte buffers. On the one hand, most
systems will never, ever need this -- because they use 8k/1k or even,
these days, 16k/2k or 32k/4k filesystems. On the other hand, when you
do need this, you *really* need it: on anoncvs.netbsd.org, for instance,
where /tmp is 4k/512 and the filesystem contains tens or even hundreds
of thousands of single-frag directories, this essentially doubles the
efficiency of the allocator. Since the overhead of keeping one extra
pool around is minimal, just add it by default.
2006-03-18 02:43:37 +03:00
|
|
|
#define MEMPOOL_INDEX_OFFSET 9 /* smallest pool is 512 bytes */
|
2003-12-30 15:33:13 +03:00
|
|
|
#if (1 << (NMEMPOOLS + MEMPOOL_INDEX_OFFSET - 1)) != MAXBSIZE
|
|
|
|
#error update vfs_bio buffer memory parameters
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Buffer memory pools */
|
2003-12-30 23:40:39 +03:00
|
|
|
static struct pool bmempools[NMEMPOOLS];
|
2003-12-30 15:33:13 +03:00
|
|
|
|
2004-01-06 16:51:09 +03:00
|
|
|
struct vm_map *buf_map;
|
2003-12-30 15:33:13 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Buffer memory pool allocator.
|
|
|
|
*/
|
2003-12-30 23:40:39 +03:00
|
|
|
static void *
|
2006-11-01 13:17:58 +03:00
|
|
|
bufpool_page_alloc(struct pool *pp, int flags)
|
2003-12-30 15:33:13 +03:00
|
|
|
{
|
2004-01-19 14:57:42 +03:00
|
|
|
|
2005-04-01 15:59:21 +04:00
|
|
|
return (void *)uvm_km_alloc(buf_map,
|
|
|
|
MAXBSIZE, MAXBSIZE,
|
|
|
|
((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
|
|
|
|
| UVM_KMF_WIRED);
|
2003-12-30 15:33:13 +03:00
|
|
|
}
|
|
|
|
|
2003-12-30 23:40:39 +03:00
|
|
|
static void
|
2006-11-01 13:17:58 +03:00
|
|
|
bufpool_page_free(struct pool *pp, void *v)
|
2003-12-30 15:33:13 +03:00
|
|
|
{
|
2005-04-01 15:59:21 +04:00
|
|
|
|
|
|
|
uvm_km_free(buf_map, (vaddr_t)v, MAXBSIZE, UVM_KMF_WIRED);
|
2003-12-30 15:33:13 +03:00
|
|
|
}
|
|
|
|
|
2003-12-30 23:40:39 +03:00
|
|
|
static struct pool_allocator bufmempool_allocator = {
|
2006-09-04 01:39:29 +04:00
|
|
|
.pa_alloc = bufpool_page_alloc,
|
|
|
|
.pa_free = bufpool_page_free,
|
|
|
|
.pa_pagesz = MAXBSIZE,
|
2003-12-30 15:33:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Buffer memory management variables */
|
2008-01-02 14:48:20 +03:00
|
|
|
u_long bufmem_valimit;
|
|
|
|
u_long bufmem_hiwater;
|
|
|
|
u_long bufmem_lowater;
|
|
|
|
u_long bufmem;
|
2003-12-30 15:33:13 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MD code can call this to set a hard limit on the amount
|
|
|
|
* of virtual memory used by the buffer cache.
|
|
|
|
*/
|
2003-12-30 23:40:39 +03:00
|
|
|
int
|
|
|
|
buf_setvalimit(vsize_t sz)
|
2003-12-30 15:33:13 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
/* We need to accommodate at least NMEMPOOLS of MAXBSIZE each */
|
|
|
|
if (sz < NMEMPOOLS * MAXBSIZE)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
bufmem_valimit = sz;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-04 04:46:05 +04:00
|
|
|
static void
|
|
|
|
buf_setwm(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
bufmem_hiwater = buf_memcalc();
|
|
|
|
/* lowater is approx. 2% of memory (with bufcache = 15) */
|
|
|
|
#define BUFMEM_WMSHIFT 3
|
|
|
|
#define BUFMEM_HIWMMIN (64 * 1024 << BUFMEM_WMSHIFT)
|
|
|
|
if (bufmem_hiwater < BUFMEM_HIWMMIN)
|
|
|
|
/* Ensure a reasonable minimum value */
|
|
|
|
bufmem_hiwater = BUFMEM_HIWMMIN;
|
|
|
|
bufmem_lowater = bufmem_hiwater >> BUFMEM_WMSHIFT;
|
|
|
|
}
|
|
|
|
|
2003-12-02 07:18:19 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
int debug_verify_freelist = 0;
|
2004-09-18 20:37:12 +04:00
|
|
|
static int
|
2008-01-02 14:48:20 +03:00
|
|
|
checkfreelist(buf_t *bp, struct bqueue *dp)
|
2003-12-02 07:18:19 +03:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *b;
|
|
|
|
|
|
|
|
if (!debug_verify_freelist)
|
|
|
|
return 1;
|
2004-09-18 20:37:12 +04:00
|
|
|
|
|
|
|
TAILQ_FOREACH(b, &dp->bq_queue, b_freelist) {
|
2003-12-30 15:33:13 +03:00
|
|
|
if (b == bp)
|
|
|
|
return 1;
|
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
return 0;
|
2003-12-02 07:18:19 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-09-18 20:37:12 +04:00
|
|
|
/*
|
|
|
|
* Insq/Remq for the buffer hash lists.
|
|
|
|
* Call with buffer queue locked.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
static void
|
|
|
|
binsheadfree(buf_t *bp, struct bqueue *dp)
|
2004-09-18 20:37:12 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(bp->b_freelistindex == -1);
|
|
|
|
TAILQ_INSERT_HEAD(&dp->bq_queue, bp, b_freelist);
|
|
|
|
dp->bq_bytes += bp->b_bufsize;
|
|
|
|
bp->b_freelistindex = dp - bufqueues;
|
|
|
|
}
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
static void
|
|
|
|
binstailfree(buf_t *bp, struct bqueue *dp)
|
2004-09-18 20:37:12 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(bp->b_freelistindex == -1);
|
|
|
|
TAILQ_INSERT_TAIL(&dp->bq_queue, bp, b_freelist);
|
|
|
|
dp->bq_bytes += bp->b_bufsize;
|
|
|
|
bp->b_freelistindex = dp - bufqueues;
|
|
|
|
}
|
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
void
|
2008-01-02 14:48:20 +03:00
|
|
|
bremfree(buf_t *bp)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2004-09-18 20:37:12 +04:00
|
|
|
struct bqueue *dp;
|
|
|
|
int bqidx = bp->b_freelistindex;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(mutex_owned(&bufcache_lock));
|
2003-09-07 15:57:43 +04:00
|
|
|
|
2004-09-18 20:37:12 +04:00
|
|
|
KASSERT(bqidx != -1);
|
|
|
|
dp = &bufqueues[bqidx];
|
2008-01-02 14:48:20 +03:00
|
|
|
KDASSERT(checkfreelist(bp, dp));
|
2004-09-18 20:37:12 +04:00
|
|
|
KASSERT(dp->bq_bytes >= bp->b_bufsize);
|
|
|
|
TAILQ_REMOVE(&dp->bq_queue, bp, b_freelist);
|
|
|
|
dp->bq_bytes -= bp->b_bufsize;
|
2008-01-02 14:48:20 +03:00
|
|
|
|
|
|
|
/* For the sysctl helper. */
|
|
|
|
if (bp == dp->bq_marker)
|
|
|
|
dp->bq_marker = NULL;
|
|
|
|
|
2004-09-18 20:37:12 +04:00
|
|
|
#if defined(DIAGNOSTIC)
|
|
|
|
bp->b_freelistindex = -1;
|
|
|
|
#endif /* defined(DIAGNOSTIC) */
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
/*
|
|
|
|
* Add a reference to an buffer structure that came from buf_cache.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
bref(buf_t *bp)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(mutex_owned(&bufcache_lock));
|
|
|
|
KASSERT(bp->b_refcnt > 0);
|
|
|
|
|
|
|
|
bp->b_refcnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free an unused buffer structure that came from buf_cache.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
brele(buf_t *bp)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(mutex_owned(&bufcache_lock));
|
|
|
|
KASSERT(bp->b_refcnt > 0);
|
|
|
|
|
|
|
|
if (bp->b_refcnt-- == 1) {
|
|
|
|
buf_destroy(bp);
|
|
|
|
#ifdef DEBUG
|
|
|
|
memset((char *)bp, 0, sizeof(*bp));
|
|
|
|
#endif
|
|
|
|
pool_cache_put(buf_cache, bp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-30 23:40:39 +03:00
|
|
|
u_long
|
|
|
|
buf_memcalc(void)
|
2003-12-30 15:33:13 +03:00
|
|
|
{
|
|
|
|
u_long n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the upper bound of memory to use for buffers.
|
|
|
|
*
|
|
|
|
* - If bufpages is specified, use that as the number
|
|
|
|
* pages.
|
|
|
|
*
|
|
|
|
* - Otherwise, use bufcache as the percentage of
|
|
|
|
* physical memory.
|
|
|
|
*/
|
|
|
|
if (bufpages != 0) {
|
|
|
|
n = bufpages;
|
|
|
|
} else {
|
|
|
|
if (bufcache < 5) {
|
|
|
|
printf("forcing bufcache %d -> 5", bufcache);
|
|
|
|
bufcache = 5;
|
|
|
|
}
|
|
|
|
if (bufcache > 95) {
|
|
|
|
printf("forcing bufcache %d -> 95", bufcache);
|
|
|
|
bufcache = 95;
|
|
|
|
}
|
|
|
|
n = physmem / 100 * bufcache;
|
|
|
|
}
|
|
|
|
|
|
|
|
n <<= PAGE_SHIFT;
|
|
|
|
if (bufmem_valimit != 0 && n > bufmem_valimit)
|
|
|
|
n = bufmem_valimit;
|
|
|
|
|
|
|
|
return (n);
|
|
|
|
}
|
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
/*
|
|
|
|
* Initialize buffers and hash links for buffers.
|
|
|
|
*/
|
|
|
|
void
|
2003-12-30 23:40:39 +03:00
|
|
|
bufinit(void)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2004-09-18 20:37:12 +04:00
|
|
|
struct bqueue *dp;
|
2004-06-20 22:29:47 +04:00
|
|
|
int use_std;
|
2003-12-30 15:33:13 +03:00
|
|
|
u_int i;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_init(&bufcache_lock, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
mutex_init(&buffer_lock, MUTEX_DEFAULT, IPL_NONE);
|
|
|
|
cv_init(&needbuffer_cv, "needbuf");
|
|
|
|
|
2000-02-14 23:12:02 +03:00
|
|
|
/*
|
2003-12-30 15:33:13 +03:00
|
|
|
* Initialize buffer cache memory parameters.
|
|
|
|
*/
|
|
|
|
bufmem = 0;
|
2004-10-04 04:46:05 +04:00
|
|
|
buf_setwm();
|
2003-12-30 15:33:13 +03:00
|
|
|
|
|
|
|
if (bufmem_valimit != 0) {
|
|
|
|
vaddr_t minaddr = 0, maxaddr;
|
|
|
|
buf_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
|
2007-02-22 09:34:42 +03:00
|
|
|
bufmem_valimit, 0, false, 0);
|
2003-12-30 15:33:13 +03:00
|
|
|
if (buf_map == NULL)
|
|
|
|
panic("bufinit: cannot allocate submap");
|
|
|
|
} else
|
|
|
|
buf_map = kernel_map;
|
|
|
|
|
|
|
|
/* On "small" machines use small pool page sizes where possible */
|
2004-06-20 22:29:47 +04:00
|
|
|
use_std = (physmem < atop(16*1024*1024));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Also use them on systems that can map the pool pages using
|
|
|
|
* a direct-mapped segment.
|
|
|
|
*/
|
|
|
|
#ifdef PMAP_MAP_POOLPAGE
|
|
|
|
use_std = 1;
|
|
|
|
#endif
|
2003-12-30 15:33:13 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_cache = pool_cache_init(sizeof(buf_t), 0, 0, 0,
|
|
|
|
"bufpl", NULL, IPL_SOFTBIO, NULL, NULL, NULL);
|
|
|
|
bufio_cache = pool_cache_init(sizeof(buf_t), 0, 0, 0,
|
|
|
|
"biopl", NULL, IPL_BIO, NULL, NULL, NULL);
|
2007-08-11 23:56:53 +04:00
|
|
|
|
2006-05-25 18:27:28 +04:00
|
|
|
bufmempool_allocator.pa_backingmap = buf_map;
|
2003-12-30 15:33:13 +03:00
|
|
|
for (i = 0; i < NMEMPOOLS; i++) {
|
|
|
|
struct pool_allocator *pa;
|
|
|
|
struct pool *pp = &bmempools[i];
|
|
|
|
u_int size = 1 << (i + MEMPOOL_INDEX_OFFSET);
|
|
|
|
char *name = malloc(8, M_TEMP, M_WAITOK);
|
2006-10-16 20:50:12 +04:00
|
|
|
if (__predict_true(size >= 1024))
|
|
|
|
(void)snprintf(name, 8, "buf%dk", size / 1024);
|
|
|
|
else
|
|
|
|
(void)snprintf(name, 8, "buf%db", size);
|
2004-06-20 22:29:47 +04:00
|
|
|
pa = (size <= PAGE_SIZE && use_std)
|
2003-12-30 15:33:13 +03:00
|
|
|
? &pool_allocator_nointr
|
|
|
|
: &bufmempool_allocator;
|
2007-03-12 21:18:22 +03:00
|
|
|
pool_init(pp, size, 0, 0, 0, name, pa, IPL_NONE);
|
2003-12-30 15:33:13 +03:00
|
|
|
pool_setlowat(pp, 1);
|
2004-06-20 22:17:09 +04:00
|
|
|
pool_sethiwat(pp, 1);
|
2003-12-30 15:33:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the buffer queues */
|
2004-09-18 20:37:12 +04:00
|
|
|
for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++) {
|
|
|
|
TAILQ_INIT(&dp->bq_queue);
|
|
|
|
dp->bq_bytes = 0;
|
|
|
|
}
|
2003-12-30 15:33:13 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Estimate hash table size based on the amount of memory we
|
|
|
|
* intend to use for the buffer cache. The average buffer
|
|
|
|
* size is dependent on our clients (i.e. filesystems).
|
|
|
|
*
|
|
|
|
* For now, use an empirical 3K per buffer.
|
|
|
|
*/
|
|
|
|
nbuf = (bufmem_hiwater / 1024) / 3;
|
2000-11-08 17:28:12 +03:00
|
|
|
bufhashtbl = hashinit(nbuf, HASH_LIST, M_CACHE, M_WAITOK, &bufhash);
|
2003-12-30 15:33:13 +03:00
|
|
|
}
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
void
|
|
|
|
bufinit2(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
biodone_sih = softint_establish(SOFTINT_BIO | SOFTINT_MPSAFE, biointr,
|
|
|
|
NULL);
|
|
|
|
if (biodone_sih == NULL)
|
|
|
|
panic("bufinit2: can't establish soft interrupt");
|
|
|
|
}
|
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
static int
|
|
|
|
buf_lotsfree(void)
|
|
|
|
{
|
2004-01-30 14:32:16 +03:00
|
|
|
int try, thresh;
|
2004-06-20 22:55:58 +04:00
|
|
|
|
|
|
|
/* Always allocate if doing copy on write */
|
2007-05-17 18:51:11 +04:00
|
|
|
if (curlwp->l_pflag & LP_UFSCOW)
|
2004-06-20 22:55:58 +04:00
|
|
|
return 1;
|
2004-01-30 14:32:16 +03:00
|
|
|
|
2004-03-26 03:31:55 +03:00
|
|
|
/* Always allocate if less than the low water mark. */
|
|
|
|
if (bufmem < bufmem_lowater)
|
2004-01-30 14:32:16 +03:00
|
|
|
return 1;
|
2005-02-27 00:34:55 +03:00
|
|
|
|
2004-03-26 03:31:55 +03:00
|
|
|
/* Never allocate if greater than the high water mark. */
|
|
|
|
if (bufmem > bufmem_hiwater)
|
|
|
|
return 0;
|
2004-01-30 14:32:16 +03:00
|
|
|
|
Fix bug noted by yamt@netbsd.org: the UVM free target is in *pages*,
so the last change has us comparing pages to bytes instead of pages
to buffers! The consequence was to try to free radically less memory
than UVM wanted us to -- though always at least one buffer, which is
probably why the results weren't dire.
This does suggest that buf_canrelease() could be a *lot* more
conservative about how much to release than "2 * page deficit". In
fact, serious trouble seems to ensue if it's not -- when anything
else on the system demands enough pages, we slam down to the low
water mark nd stay there. I've adjusted it to use min(page defecit,
buffer memory / 16), which still isn't quite right but seems better.
Another change: consider the case of an infinite loop that does
"tar xzf pkgsrc.tar.gz ; rm -rf pkgsrc". Each time the rm runs,
all the dead metadata will go on the AGE list -- and, until we hit
the high-water mark, stay there, at which point it may be slowly
recycled. Two adjustments seem to solve this: 1) whack buf_lotsfree()
to return 0 if there's anything on the AGE list; 2) whack buf_canrelease()
to count the memory used by the AGE list and always return at least
that much.
This basically turns the AGE list into a "delayed free" list, since we
can't entirely eliminate it as we can't free pool items from interrupt
context (e.g. from biodone()).
To consider: with the bookkeeping corrected, should buf_drain() move
back to the _end_ of the pagedaemon, and should the calculation then
try to give back at least the current defecit?
2004-02-11 20:36:31 +03:00
|
|
|
/* If there's anything on the AGE list, it should be eaten. */
|
2004-09-18 20:37:12 +04:00
|
|
|
if (TAILQ_FIRST(&bufqueues[BQ_AGE].bq_queue) != NULL)
|
Fix bug noted by yamt@netbsd.org: the UVM free target is in *pages*,
so the last change has us comparing pages to bytes instead of pages
to buffers! The consequence was to try to free radically less memory
than UVM wanted us to -- though always at least one buffer, which is
probably why the results weren't dire.
This does suggest that buf_canrelease() could be a *lot* more
conservative about how much to release than "2 * page deficit". In
fact, serious trouble seems to ensue if it's not -- when anything
else on the system demands enough pages, we slam down to the low
water mark nd stay there. I've adjusted it to use min(page defecit,
buffer memory / 16), which still isn't quite right but seems better.
Another change: consider the case of an infinite loop that does
"tar xzf pkgsrc.tar.gz ; rm -rf pkgsrc". Each time the rm runs,
all the dead metadata will go on the AGE list -- and, until we hit
the high-water mark, stay there, at which point it may be slowly
recycled. Two adjustments seem to solve this: 1) whack buf_lotsfree()
to return 0 if there's anything on the AGE list; 2) whack buf_canrelease()
to count the memory used by the AGE list and always return at least
that much.
This basically turns the AGE list into a "delayed free" list, since we
can't entirely eliminate it as we can't free pool items from interrupt
context (e.g. from biodone()).
To consider: with the bookkeeping corrected, should buf_drain() move
back to the _end_ of the pagedaemon, and should the calculation then
try to give back at least the current defecit?
2004-02-11 20:36:31 +03:00
|
|
|
return 0;
|
|
|
|
|
2004-03-26 03:31:55 +03:00
|
|
|
/*
|
|
|
|
* The probabily of getting a new allocation is inversely
|
|
|
|
* proportional to the current size of the cache, using
|
|
|
|
* a granularity of 16 steps.
|
|
|
|
*/
|
2004-01-30 14:32:16 +03:00
|
|
|
try = random() & 0x0000000fL;
|
|
|
|
|
2004-03-26 03:31:55 +03:00
|
|
|
/* Don't use "16 * bufmem" here to avoid a 32-bit overflow. */
|
2004-10-04 05:24:18 +04:00
|
|
|
thresh = (bufmem - bufmem_lowater) /
|
|
|
|
((bufmem_hiwater - bufmem_lowater) / 16);
|
2004-01-30 14:32:16 +03:00
|
|
|
|
2004-10-04 05:24:18 +04:00
|
|
|
if (try >= thresh)
|
2004-01-30 14:32:16 +03:00
|
|
|
return 1;
|
|
|
|
|
2004-03-26 03:31:55 +03:00
|
|
|
/* Otherwise don't allocate. */
|
2004-01-30 14:32:16 +03:00
|
|
|
return 0;
|
2003-12-30 15:33:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-02-16 12:34:15 +03:00
|
|
|
* Return estimate of bytes we think need to be
|
2003-12-30 15:33:13 +03:00
|
|
|
* released to help resolve low memory conditions.
|
2004-02-16 12:34:15 +03:00
|
|
|
*
|
2008-01-02 14:48:20 +03:00
|
|
|
* => called with bufcache_lock held.
|
2003-12-30 15:33:13 +03:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
buf_canrelease(void)
|
|
|
|
{
|
Fix bug noted by yamt@netbsd.org: the UVM free target is in *pages*,
so the last change has us comparing pages to bytes instead of pages
to buffers! The consequence was to try to free radically less memory
than UVM wanted us to -- though always at least one buffer, which is
probably why the results weren't dire.
This does suggest that buf_canrelease() could be a *lot* more
conservative about how much to release than "2 * page deficit". In
fact, serious trouble seems to ensue if it's not -- when anything
else on the system demands enough pages, we slam down to the low
water mark nd stay there. I've adjusted it to use min(page defecit,
buffer memory / 16), which still isn't quite right but seems better.
Another change: consider the case of an infinite loop that does
"tar xzf pkgsrc.tar.gz ; rm -rf pkgsrc". Each time the rm runs,
all the dead metadata will go on the AGE list -- and, until we hit
the high-water mark, stay there, at which point it may be slowly
recycled. Two adjustments seem to solve this: 1) whack buf_lotsfree()
to return 0 if there's anything on the AGE list; 2) whack buf_canrelease()
to count the memory used by the AGE list and always return at least
that much.
This basically turns the AGE list into a "delayed free" list, since we
can't entirely eliminate it as we can't free pool items from interrupt
context (e.g. from biodone()).
To consider: with the bookkeeping corrected, should buf_drain() move
back to the _end_ of the pagedaemon, and should the calculation then
try to give back at least the current defecit?
2004-02-11 20:36:31 +03:00
|
|
|
int pagedemand, ninvalid = 0;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(mutex_owned(&bufcache_lock));
|
2004-02-16 12:34:15 +03:00
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
if (bufmem < bufmem_lowater)
|
|
|
|
return 0;
|
|
|
|
|
2005-01-10 18:29:50 +03:00
|
|
|
if (bufmem > bufmem_hiwater)
|
|
|
|
return bufmem - bufmem_hiwater;
|
|
|
|
|
2004-09-18 20:37:12 +04:00
|
|
|
ninvalid += bufqueues[BQ_AGE].bq_bytes;
|
2004-02-22 04:00:41 +03:00
|
|
|
|
Fix bug noted by yamt@netbsd.org: the UVM free target is in *pages*,
so the last change has us comparing pages to bytes instead of pages
to buffers! The consequence was to try to free radically less memory
than UVM wanted us to -- though always at least one buffer, which is
probably why the results weren't dire.
This does suggest that buf_canrelease() could be a *lot* more
conservative about how much to release than "2 * page deficit". In
fact, serious trouble seems to ensue if it's not -- when anything
else on the system demands enough pages, we slam down to the low
water mark nd stay there. I've adjusted it to use min(page defecit,
buffer memory / 16), which still isn't quite right but seems better.
Another change: consider the case of an infinite loop that does
"tar xzf pkgsrc.tar.gz ; rm -rf pkgsrc". Each time the rm runs,
all the dead metadata will go on the AGE list -- and, until we hit
the high-water mark, stay there, at which point it may be slowly
recycled. Two adjustments seem to solve this: 1) whack buf_lotsfree()
to return 0 if there's anything on the AGE list; 2) whack buf_canrelease()
to count the memory used by the AGE list and always return at least
that much.
This basically turns the AGE list into a "delayed free" list, since we
can't entirely eliminate it as we can't free pool items from interrupt
context (e.g. from biodone()).
To consider: with the bookkeeping corrected, should buf_drain() move
back to the _end_ of the pagedaemon, and should the calculation then
try to give back at least the current defecit?
2004-02-11 20:36:31 +03:00
|
|
|
pagedemand = uvmexp.freetarg - uvmexp.free;
|
|
|
|
if (pagedemand < 0)
|
|
|
|
return ninvalid;
|
|
|
|
return MAX(ninvalid, MIN(2 * MAXBSIZE,
|
|
|
|
MIN((bufmem - bufmem_lowater) / 16, pagedemand * PAGE_SIZE)));
|
2003-12-30 15:33:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Buffer memory allocation helper functions
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
static u_long
|
2003-12-30 23:40:39 +03:00
|
|
|
buf_mempoolidx(u_long size)
|
2003-12-30 15:33:13 +03:00
|
|
|
{
|
|
|
|
u_int n = 0;
|
|
|
|
|
|
|
|
size -= 1;
|
|
|
|
size >>= MEMPOOL_INDEX_OFFSET;
|
|
|
|
while (size) {
|
|
|
|
size >>= 1;
|
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
if (n >= NMEMPOOLS)
|
|
|
|
panic("buf mem pool index %d", n);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
static u_long
|
2003-12-30 23:40:39 +03:00
|
|
|
buf_roundsize(u_long size)
|
2003-12-30 15:33:13 +03:00
|
|
|
{
|
|
|
|
/* Round up to nearest power of 2 */
|
|
|
|
return (1 << (buf_mempoolidx(size) + MEMPOOL_INDEX_OFFSET));
|
|
|
|
}
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
static void *
|
2003-12-30 23:40:39 +03:00
|
|
|
buf_malloc(size_t size)
|
2003-12-30 15:33:13 +03:00
|
|
|
{
|
|
|
|
u_int n = buf_mempoolidx(size);
|
2007-03-04 08:59:00 +03:00
|
|
|
void *addr;
|
2003-12-30 15:33:13 +03:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
addr = pool_get(&bmempools[n], PR_NOWAIT);
|
|
|
|
if (addr != NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* No memory, see if we can free some. If so, try again */
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
|
|
|
if (buf_drain(1) > 0) {
|
|
|
|
mutex_exit(&bufcache_lock);
|
2003-12-30 15:33:13 +03:00
|
|
|
continue;
|
2008-01-02 14:48:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (curlwp == uvm.pagedaemon_lwp) {
|
|
|
|
mutex_exit(&bufcache_lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-12-30 15:33:13 +03:00
|
|
|
|
|
|
|
/* Wait for buffers to arrive on the LRU queue */
|
2008-01-02 14:48:20 +03:00
|
|
|
cv_timedwait(&needbuffer_cv, &bufcache_lock, hz / 4);
|
|
|
|
mutex_exit(&bufcache_lock);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
2003-12-30 15:33:13 +03:00
|
|
|
|
|
|
|
return addr;
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
2003-12-30 23:40:39 +03:00
|
|
|
static void
|
2007-03-04 08:59:00 +03:00
|
|
|
buf_mrelease(void *addr, size_t size)
|
2003-12-30 15:33:13 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
pool_put(&bmempools[buf_mempoolidx(size)], addr);
|
|
|
|
}
|
|
|
|
|
2004-09-18 20:01:03 +04:00
|
|
|
/*
|
|
|
|
* bread()/breadn() helper.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
static buf_t *
|
2006-11-01 13:17:58 +03:00
|
|
|
bio_doread(struct vnode *vp, daddr_t blkno, int size, kauth_cred_t cred,
|
|
|
|
int async)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *bp;
|
2004-04-21 05:05:31 +04:00
|
|
|
struct mount *mp;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
1994-11-22 04:31:02 +03:00
|
|
|
bp = getblk(vp, blkno, size, 0, 0);
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (bp == NULL) {
|
|
|
|
panic("bio_doread: no such buf");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
/*
|
1994-11-22 04:31:02 +03:00
|
|
|
* If buffer does not have data valid, start a read.
|
2008-01-02 14:48:20 +03:00
|
|
|
* Note that if buffer is BC_INVAL, getblk() won't return it.
|
2003-02-06 00:38:38 +03:00
|
|
|
* Therefore, it's valid if its I/O has completed or been delayed.
|
1994-07-03 11:57:32 +04:00
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
if (!ISSET(bp->b_oflags, (BO_DONE | BO_DELWRI))) {
|
2000-11-27 11:39:39 +03:00
|
|
|
/* Start I/O for the buffer. */
|
1994-11-22 04:31:02 +03:00
|
|
|
SET(bp->b_flags, B_READ | async);
|
2004-01-10 17:39:50 +03:00
|
|
|
if (async)
|
|
|
|
BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
|
|
|
|
else
|
|
|
|
BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
|
2004-01-25 21:02:03 +03:00
|
|
|
VOP_STRATEGY(vp, bp);
|
1994-07-03 11:57:32 +04:00
|
|
|
|
1994-11-22 04:31:02 +03:00
|
|
|
/* Pay for the read. */
|
2007-05-17 18:51:11 +04:00
|
|
|
curproc->p_stats->p_ru.ru_inblock++;
|
2008-01-02 14:48:20 +03:00
|
|
|
} else if (async)
|
2007-10-08 22:02:53 +04:00
|
|
|
brelse(bp, 0);
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2004-04-25 16:41:12 +04:00
|
|
|
if (vp->v_type == VBLK)
|
|
|
|
mp = vp->v_specmountpoint;
|
|
|
|
else
|
|
|
|
mp = vp->v_mount;
|
2004-04-21 05:05:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Collect statistics on synchronous and asynchronous reads.
|
|
|
|
* Reads from block devices are charged to their associated
|
|
|
|
* filesystem (if any).
|
|
|
|
*/
|
|
|
|
if (mp != NULL) {
|
|
|
|
if (async == 0)
|
|
|
|
mp->mnt_stat.f_syncreads++;
|
|
|
|
else
|
|
|
|
mp->mnt_stat.f_asyncreads++;
|
|
|
|
}
|
|
|
|
|
1994-11-22 04:31:02 +03:00
|
|
|
return (bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a disk block.
|
|
|
|
* This algorithm described in Bach (p.54).
|
|
|
|
*/
|
1996-02-04 05:17:43 +03:00
|
|
|
int
|
2006-05-15 01:15:11 +04:00
|
|
|
bread(struct vnode *vp, daddr_t blkno, int size, kauth_cred_t cred,
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t **bpp)
|
1994-11-22 04:31:02 +03:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *bp;
|
1994-11-22 04:31:02 +03:00
|
|
|
|
|
|
|
/* Get buffer for block. */
|
|
|
|
bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2002-03-17 02:49:59 +03:00
|
|
|
/* Wait for the read to complete, and return result. */
|
1994-07-03 11:57:32 +04:00
|
|
|
return (biowait(bp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read-ahead multiple disk blocks. The first is sync, the rest async.
|
|
|
|
* Trivial modification to the breada algorithm presented in Bach (p.55).
|
|
|
|
*/
|
1996-02-04 05:17:43 +03:00
|
|
|
int
|
2003-12-30 23:40:39 +03:00
|
|
|
breadn(struct vnode *vp, daddr_t blkno, int size, daddr_t *rablks,
|
2008-01-02 14:48:20 +03:00
|
|
|
int *rasizes, int nrablks, kauth_cred_t cred, buf_t **bpp)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *bp;
|
1994-07-03 11:57:32 +04:00
|
|
|
int i;
|
|
|
|
|
1994-11-22 04:31:02 +03:00
|
|
|
bp = *bpp = bio_doread(vp, blkno, size, cred, 0);
|
1994-07-03 11:57:32 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For each of the read-ahead blocks, start a read, if necessary.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
1994-07-03 11:57:32 +04:00
|
|
|
for (i = 0; i < nrablks; i++) {
|
|
|
|
/* If it's in the cache, just go on to next one. */
|
|
|
|
if (incore(vp, rablks[i]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Get a buffer for the read-ahead block */
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
1994-11-22 04:31:02 +03:00
|
|
|
(void) bio_doread(vp, rablks[i], rasizes[i], cred, B_ASYNC);
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2002-03-17 02:49:59 +03:00
|
|
|
/* Otherwise, we had to start a read for it; wait until it's valid. */
|
1994-07-03 11:57:32 +04:00
|
|
|
return (biowait(bp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read with single-block read-ahead. Defined in Bach (p.55), but
|
|
|
|
* implemented as a call to breadn().
|
|
|
|
* XXX for compatibility with old file systems.
|
|
|
|
*/
|
1996-02-04 05:17:43 +03:00
|
|
|
int
|
2003-12-30 23:40:39 +03:00
|
|
|
breada(struct vnode *vp, daddr_t blkno, int size, daddr_t rablkno,
|
2008-01-02 14:48:20 +03:00
|
|
|
int rabsize, kauth_cred_t cred, buf_t **bpp)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
1994-11-22 04:31:02 +03:00
|
|
|
|
2005-02-27 00:34:55 +03:00
|
|
|
return (breadn(vp, blkno, size, &rablkno, &rabsize, 1, cred, bpp));
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Block write. Described in Bach (p.56)
|
|
|
|
*/
|
1996-02-04 05:17:43 +03:00
|
|
|
int
|
2008-01-02 14:48:20 +03:00
|
|
|
bwrite(buf_t *bp)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
int rv, sync, wasdelayed;
|
1999-11-15 21:49:07 +03:00
|
|
|
struct vnode *vp;
|
|
|
|
struct mount *mp;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(ISSET(bp->b_cflags, BC_BUSY));
|
2003-02-06 00:38:38 +03:00
|
|
|
|
2001-04-01 20:16:56 +04:00
|
|
|
vp = bp->b_vp;
|
|
|
|
if (vp != NULL) {
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(bp->b_objlock == &vp->v_interlock);
|
2001-04-01 20:16:56 +04:00
|
|
|
if (vp->v_type == VBLK)
|
|
|
|
mp = vp->v_specmountpoint;
|
|
|
|
else
|
|
|
|
mp = vp->v_mount;
|
|
|
|
} else {
|
|
|
|
mp = NULL;
|
|
|
|
}
|
|
|
|
|
1995-07-12 11:56:31 +04:00
|
|
|
/*
|
|
|
|
* Remember buffer type, to switch on it later. If the write was
|
|
|
|
* synchronous, but the file system was mounted with MNT_ASYNC,
|
2005-02-27 00:34:55 +03:00
|
|
|
* convert it to a delayed write.
|
1995-07-12 11:56:31 +04:00
|
|
|
* XXX note that this relies on delayed tape writes being converted
|
|
|
|
* to async, not sync writes (which is safe, but ugly).
|
|
|
|
*/
|
1994-07-03 11:57:32 +04:00
|
|
|
sync = !ISSET(bp->b_flags, B_ASYNC);
|
2001-04-01 20:16:56 +04:00
|
|
|
if (sync && mp != NULL && ISSET(mp->mnt_flag, MNT_ASYNC)) {
|
1995-07-12 11:39:00 +04:00
|
|
|
bdwrite(bp);
|
|
|
|
return (0);
|
|
|
|
}
|
1996-06-19 00:50:23 +04:00
|
|
|
|
1999-11-15 21:49:07 +03:00
|
|
|
/*
|
|
|
|
* Collect statistics on synchronous and asynchronous writes.
|
|
|
|
* Writes to block devices are charged to their associated
|
|
|
|
* filesystem (if any).
|
|
|
|
*/
|
2001-04-01 20:16:56 +04:00
|
|
|
if (mp != NULL) {
|
|
|
|
if (sync)
|
|
|
|
mp->mnt_stat.f_syncwrites++;
|
1999-11-15 21:49:07 +03:00
|
|
|
else
|
2001-04-01 20:16:56 +04:00
|
|
|
mp->mnt_stat.f_asyncwrites++;
|
1999-11-15 21:49:07 +03:00
|
|
|
}
|
|
|
|
|
1996-06-19 00:50:23 +04:00
|
|
|
/*
|
|
|
|
* Pay for the I/O operation and make sure the buf is on the correct
|
|
|
|
* vnode queue.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
bp->b_error = 0;
|
2008-01-07 15:50:38 +03:00
|
|
|
wasdelayed = ISSET(bp->b_oflags, BO_DELWRI);
|
|
|
|
CLR(bp->b_flags, B_READ);
|
|
|
|
if (wasdelayed) {
|
|
|
|
mutex_enter(&bufcache_lock);
|
|
|
|
mutex_enter(bp->b_objlock);
|
|
|
|
CLR(bp->b_oflags, BO_DONE | BO_DELWRI);
|
1996-06-19 00:50:23 +04:00
|
|
|
reassignbuf(bp, bp->b_vp);
|
2008-01-07 15:50:38 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
|
|
|
} else {
|
2007-05-17 18:51:11 +04:00
|
|
|
curproc->p_stats->p_ru.ru_oublock++;
|
2008-01-07 15:50:38 +03:00
|
|
|
mutex_enter(bp->b_objlock);
|
|
|
|
CLR(bp->b_oflags, BO_DONE | BO_DELWRI);
|
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
if (vp != NULL)
|
|
|
|
vp->v_numoutput++;
|
|
|
|
mutex_exit(bp->b_objlock);
|
1994-08-29 05:47:02 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
/* Initiate disk write. */
|
2004-01-10 17:39:50 +03:00
|
|
|
if (sync)
|
|
|
|
BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
|
|
|
|
else
|
|
|
|
BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
|
|
|
|
|
2004-01-25 21:02:03 +03:00
|
|
|
VOP_STRATEGY(vp, bp);
|
1994-07-03 11:57:32 +04:00
|
|
|
|
1994-11-22 04:31:02 +03:00
|
|
|
if (sync) {
|
1996-06-19 00:50:23 +04:00
|
|
|
/* If I/O was synchronous, wait for it to complete. */
|
1994-07-03 11:57:32 +04:00
|
|
|
rv = biowait(bp);
|
|
|
|
|
1994-11-22 04:31:02 +03:00
|
|
|
/* Release the buffer. */
|
2007-10-08 22:02:53 +04:00
|
|
|
brelse(bp, 0);
|
1994-11-22 04:31:02 +03:00
|
|
|
|
|
|
|
return (rv);
|
|
|
|
} else {
|
|
|
|
return (0);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-12-30 23:40:39 +03:00
|
|
|
vn_bwrite(void *v)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
1996-02-04 05:17:43 +03:00
|
|
|
struct vop_bwrite_args *ap = v;
|
1994-11-22 04:31:02 +03:00
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
return (bwrite(ap->a_bp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delayed write.
|
|
|
|
*
|
|
|
|
* The buffer is marked dirty, but is not queued for I/O.
|
|
|
|
* This routine should be used when the buffer is expected
|
|
|
|
* to be modified again soon, typically a small write that
|
|
|
|
* partially fills a buffer.
|
|
|
|
*
|
|
|
|
* NB: magnetic tapes cannot be delayed; they must be
|
|
|
|
* written in the order that the writes are requested.
|
|
|
|
*
|
|
|
|
* Described in Leffler, et al. (pp. 208-213).
|
|
|
|
*/
|
|
|
|
void
|
2008-01-02 14:48:20 +03:00
|
|
|
bdwrite(buf_t *bp)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
|
|
|
|
KASSERT(ISSET(bp->b_cflags, BC_BUSY));
|
1994-07-03 11:57:32 +04:00
|
|
|
|
1996-06-19 00:50:23 +04:00
|
|
|
/* If this is a tape block, write the block now. */
|
2007-07-10 00:51:58 +04:00
|
|
|
if (bdev_type(bp->b_dev) == D_TAPE) {
|
2003-02-06 14:46:49 +03:00
|
|
|
bawrite(bp);
|
|
|
|
return;
|
1996-06-19 00:50:23 +04:00
|
|
|
}
|
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
/*
|
|
|
|
* If the block hasn't been seen before:
|
|
|
|
* (1) Mark it as having been seen,
|
1996-06-18 02:21:31 +04:00
|
|
|
* (2) Charge for the write,
|
|
|
|
* (3) Make sure it's on its vnode's correct block list.
|
1994-07-03 11:57:32 +04:00
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(bp->b_vp == NULL || bp->b_objlock == &bp->b_vp->v_interlock);
|
2003-11-08 07:22:35 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if (!ISSET(bp->b_oflags, BO_DELWRI)) {
|
2008-01-07 15:50:38 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
|
|
|
mutex_enter(bp->b_objlock);
|
2008-01-02 14:48:20 +03:00
|
|
|
SET(bp->b_oflags, BO_DELWRI);
|
2007-05-17 18:51:11 +04:00
|
|
|
curproc->p_stats->p_ru.ru_oublock++;
|
1994-07-03 11:57:32 +04:00
|
|
|
reassignbuf(bp, bp->b_vp);
|
2008-01-07 15:50:38 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
|
|
|
} else {
|
|
|
|
mutex_enter(bp->b_objlock);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
/* Otherwise, the "write" is done, so mark and release the buffer. */
|
2008-01-02 14:48:20 +03:00
|
|
|
CLR(bp->b_oflags, BO_DONE);
|
|
|
|
mutex_exit(bp->b_objlock);
|
1999-11-24 02:52:40 +03:00
|
|
|
|
2007-10-08 22:02:53 +04:00
|
|
|
brelse(bp, 0);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Asynchronous block write; just an asynchronous bwrite().
|
|
|
|
*/
|
|
|
|
void
|
2008-01-02 14:48:20 +03:00
|
|
|
bawrite(buf_t *bp)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(ISSET(bp->b_cflags, BC_BUSY));
|
2003-11-08 07:22:35 +03:00
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
SET(bp->b_flags, B_ASYNC);
|
|
|
|
VOP_BWRITE(bp);
|
|
|
|
}
|
|
|
|
|
1999-11-15 21:49:07 +03:00
|
|
|
/*
|
|
|
|
* Same as first half of bdwrite, mark buffer dirty, but do not release it.
|
2008-01-02 14:48:20 +03:00
|
|
|
* Call with the buffer interlock held.
|
|
|
|
*
|
|
|
|
* Note: called only from biodone() through ffs softdep's io_complete()
|
1999-11-15 21:49:07 +03:00
|
|
|
*/
|
|
|
|
void
|
2008-01-02 14:48:20 +03:00
|
|
|
bdirty(buf_t *bp)
|
1999-11-15 21:49:07 +03:00
|
|
|
{
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(mutex_owned(&bufcache_lock));
|
|
|
|
KASSERT(bp->b_objlock == &bp->b_vp->v_interlock);
|
|
|
|
KASSERT(mutex_owned(bp->b_objlock));
|
|
|
|
KASSERT(ISSET(bp->b_cflags, BC_BUSY));
|
1999-11-24 02:52:40 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
CLR(bp->b_cflags, BC_AGE);
|
1999-11-26 20:18:15 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if (!ISSET(bp->b_oflags, BO_DELWRI)) {
|
|
|
|
SET(bp->b_oflags, BO_DELWRI);
|
2007-05-17 18:51:11 +04:00
|
|
|
curproc->p_stats->p_ru.ru_oublock++;
|
1999-11-15 21:49:07 +03:00
|
|
|
reassignbuf(bp, bp->b_vp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
/*
|
|
|
|
* Release a buffer on to the free lists.
|
|
|
|
* Described in Bach (p. 46).
|
|
|
|
*/
|
|
|
|
void
|
2008-01-02 14:48:20 +03:00
|
|
|
brelsel(buf_t *bp, int set)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2004-09-18 20:37:12 +04:00
|
|
|
struct bqueue *bufq;
|
2008-01-02 14:48:20 +03:00
|
|
|
struct vnode *vp;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(mutex_owned(&bufcache_lock));
|
2003-02-06 00:38:38 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
SET(bp->b_cflags, set);
|
2007-10-08 22:02:53 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(ISSET(bp->b_cflags, BC_BUSY));
|
|
|
|
KASSERT(bp->b_iodone == NULL);
|
2003-11-08 07:22:35 +03:00
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
/* Wake up any processes waiting for any buffer to become free. */
|
2008-01-02 14:48:20 +03:00
|
|
|
cv_signal(&needbuffer_cv);
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
/* Wake up any proceeses waiting for _this_ buffer to become */
|
|
|
|
if (ISSET(bp->b_cflags, BC_WANTED) != 0) {
|
|
|
|
CLR(bp->b_cflags, BC_WANTED|BC_AGE);
|
|
|
|
cv_broadcast(&bp->b_busy);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine which queue the buffer should be on, then put it there.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* If it's locked, don't report an error; try again later. */
|
2008-02-15 16:30:56 +03:00
|
|
|
if (ISSET(bp->b_flags, B_LOCKED))
|
2007-07-29 16:15:35 +04:00
|
|
|
bp->b_error = 0;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
|
|
|
/* If it's not cacheable, or an error, mark it invalid. */
|
2008-01-02 14:48:20 +03:00
|
|
|
if (ISSET(bp->b_cflags, BC_NOCACHE) || bp->b_error != 0)
|
|
|
|
SET(bp->b_cflags, BC_INVAL);
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if (ISSET(bp->b_cflags, BC_VFLUSH)) {
|
1997-04-10 01:12:10 +04:00
|
|
|
/*
|
|
|
|
* This is a delayed write buffer that was just flushed to
|
|
|
|
* disk. It is still on the LRU queue. If it's become
|
|
|
|
* invalid, then we need to move it to a different queue;
|
|
|
|
* otherwise leave it in its current position.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
CLR(bp->b_cflags, BC_VFLUSH);
|
2008-02-15 16:30:56 +03:00
|
|
|
if (!ISSET(bp->b_cflags, BC_INVAL|BC_AGE) &&
|
|
|
|
!ISSET(bp->b_flags, B_LOCKED) && bp->b_error == 0) {
|
2008-01-02 14:48:20 +03:00
|
|
|
KDASSERT(checkfreelist(bp, &bufqueues[BQ_LRU]));
|
1997-04-10 01:12:10 +04:00
|
|
|
goto already_queued;
|
2003-12-02 07:18:19 +03:00
|
|
|
} else {
|
1997-04-10 01:12:10 +04:00
|
|
|
bremfree(bp);
|
2003-12-02 07:18:19 +03:00
|
|
|
}
|
1997-04-10 01:12:10 +04:00
|
|
|
}
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KDASSERT(checkfreelist(bp, &bufqueues[BQ_AGE]));
|
|
|
|
KDASSERT(checkfreelist(bp, &bufqueues[BQ_LRU]));
|
|
|
|
KDASSERT(checkfreelist(bp, &bufqueues[BQ_LOCKED]));
|
2003-12-02 07:18:19 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if ((bp->b_bufsize <= 0) || ISSET(bp->b_cflags, BC_INVAL)) {
|
1994-07-03 11:57:32 +04:00
|
|
|
/*
|
|
|
|
* If it's invalid or empty, dissociate it from its vnode
|
|
|
|
* and put on the head of the appropriate queue.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
if (bioopsp != NULL)
|
|
|
|
(*bioopsp->io_deallocate)(bp);
|
|
|
|
|
|
|
|
mutex_enter(bp->b_objlock);
|
|
|
|
CLR(bp->b_oflags, BO_DONE|BO_DELWRI);
|
|
|
|
if ((vp = bp->b_vp) != NULL) {
|
|
|
|
KASSERT(bp->b_objlock == &vp->v_interlock);
|
1999-11-15 21:49:07 +03:00
|
|
|
reassignbuf(bp, bp->b_vp);
|
|
|
|
brelvp(bp);
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&vp->v_interlock);
|
|
|
|
} else {
|
|
|
|
KASSERT(bp->b_objlock == &buffer_lock);
|
|
|
|
mutex_exit(bp->b_objlock);
|
1999-11-15 21:49:07 +03:00
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
if (bp->b_bufsize <= 0)
|
|
|
|
/* no data */
|
2003-12-30 15:33:13 +03:00
|
|
|
goto already_queued;
|
1994-07-03 11:57:32 +04:00
|
|
|
else
|
|
|
|
/* invalid data */
|
|
|
|
bufq = &bufqueues[BQ_AGE];
|
|
|
|
binsheadfree(bp, bufq);
|
2008-01-02 14:48:20 +03:00
|
|
|
} else {
|
1994-07-03 11:57:32 +04:00
|
|
|
/*
|
|
|
|
* It has valid data. Put it on the end of the appropriate
|
|
|
|
* queue, so that it'll stick around for as long as possible.
|
2000-04-12 15:33:43 +04:00
|
|
|
* If buf is AGE, but has dependencies, must put it on last
|
|
|
|
* bufqueue to be scanned, ie LRU. This protects against the
|
|
|
|
* livelock where BQ_AGE only has buffers with dependencies,
|
|
|
|
* and we thus never get to the dependent buffers in BQ_LRU.
|
1994-07-03 11:57:32 +04:00
|
|
|
*/
|
2008-02-15 16:30:56 +03:00
|
|
|
if (ISSET(bp->b_flags, B_LOCKED)) {
|
1994-07-03 11:57:32 +04:00
|
|
|
/* locked in core */
|
|
|
|
bufq = &bufqueues[BQ_LOCKED];
|
2008-01-02 14:48:20 +03:00
|
|
|
} else if (!ISSET(bp->b_cflags, BC_AGE)) {
|
1994-07-03 11:57:32 +04:00
|
|
|
/* valid data */
|
|
|
|
bufq = &bufqueues[BQ_LRU];
|
2008-01-02 14:48:20 +03:00
|
|
|
} else {
|
2000-04-12 15:33:43 +04:00
|
|
|
/* stale but valid data */
|
|
|
|
int has_deps;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if (bioopsp != NULL)
|
|
|
|
has_deps = (*bioopsp->io_countdeps)(bp, 0);
|
2000-04-12 15:33:43 +04:00
|
|
|
else
|
|
|
|
has_deps = 0;
|
|
|
|
bufq = has_deps ? &bufqueues[BQ_LRU] :
|
|
|
|
&bufqueues[BQ_AGE];
|
|
|
|
}
|
1994-07-03 11:57:32 +04:00
|
|
|
binstailfree(bp, bufq);
|
|
|
|
}
|
1997-04-10 01:12:10 +04:00
|
|
|
already_queued:
|
1994-07-03 11:57:32 +04:00
|
|
|
/* Unlock the buffer. */
|
2008-01-02 14:48:20 +03:00
|
|
|
CLR(bp->b_cflags, BC_AGE|BC_BUSY|BC_NOCACHE);
|
|
|
|
CLR(bp->b_flags, B_ASYNC);
|
|
|
|
|
|
|
|
if (bp->b_bufsize <= 0)
|
|
|
|
brele(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
brelse(buf_t *bp, int set)
|
|
|
|
{
|
|
|
|
|
|
|
|
mutex_enter(&bufcache_lock);
|
|
|
|
brelsel(bp, set);
|
|
|
|
mutex_exit(&bufcache_lock);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine if a block is in the cache.
|
|
|
|
* Just look on what would be its hash chain. If it's there, return
|
|
|
|
* a pointer to it, unless it's marked invalid. If it's marked invalid,
|
|
|
|
* we normally don't return the buffer, unless the caller explicitly
|
|
|
|
* wants us to.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *
|
2003-12-30 23:40:39 +03:00
|
|
|
incore(struct vnode *vp, daddr_t blkno)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *bp;
|
|
|
|
|
|
|
|
KASSERT(mutex_owned(&bufcache_lock));
|
1994-07-03 11:57:32 +04:00
|
|
|
|
|
|
|
/* Search hash chain */
|
2002-09-04 05:32:31 +04:00
|
|
|
LIST_FOREACH(bp, BUFHASH(vp, blkno), b_hash) {
|
1994-07-03 11:57:32 +04:00
|
|
|
if (bp->b_lblkno == blkno && bp->b_vp == vp &&
|
2008-01-02 14:48:20 +03:00
|
|
|
!ISSET(bp->b_cflags, BC_INVAL)) {
|
|
|
|
KASSERT(bp->b_objlock == &vp->v_interlock);
|
|
|
|
return (bp);
|
|
|
|
}
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
2000-11-27 11:39:39 +03:00
|
|
|
return (NULL);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *
|
2003-12-30 23:40:39 +03:00
|
|
|
getblk(struct vnode *vp, daddr_t blkno, int size, int slpflag, int slptimeo)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
int err, preserve;
|
|
|
|
buf_t *bp;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
|
|
|
loop:
|
2000-11-27 11:39:39 +03:00
|
|
|
bp = incore(vp, blkno);
|
|
|
|
if (bp != NULL) {
|
2008-02-15 16:46:04 +03:00
|
|
|
err = bbusy(bp, ((slpflag & PCATCH) != 0), slptimeo, NULL);
|
2008-01-02 14:48:20 +03:00
|
|
|
if (err != 0) {
|
|
|
|
if (err == EPASSTHROUGH)
|
|
|
|
goto loop;
|
|
|
|
mutex_exit(&bufcache_lock);
|
|
|
|
return (NULL);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
1998-10-28 02:48:22 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
2008-01-02 14:48:20 +03:00
|
|
|
if (ISSET(bp->b_oflags, BO_DONE|BO_DELWRI) &&
|
2002-02-11 02:14:18 +03:00
|
|
|
bp->b_bcount < size && vp->v_type != VBLK)
|
2000-11-27 11:39:39 +03:00
|
|
|
panic("getblk: block size invariant failed");
|
1998-10-28 02:48:22 +03:00
|
|
|
#endif
|
2000-11-27 11:39:39 +03:00
|
|
|
bremfree(bp);
|
2003-12-30 15:33:13 +03:00
|
|
|
preserve = 1;
|
2000-11-27 11:39:39 +03:00
|
|
|
} else {
|
2008-01-02 14:48:20 +03:00
|
|
|
if ((bp = getnewbuf(slpflag, slptimeo, 0)) == NULL)
|
|
|
|
goto loop;
|
|
|
|
|
|
|
|
if (incore(vp, blkno) != NULL) {
|
|
|
|
/* The block has come into memory in the meantime. */
|
|
|
|
brelsel(bp, 0);
|
|
|
|
goto loop;
|
2003-02-06 00:38:38 +03:00
|
|
|
}
|
2000-11-27 11:39:39 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
LIST_INSERT_HEAD(BUFHASH(vp, blkno), bp, b_hash);
|
2000-02-07 23:16:47 +03:00
|
|
|
bp->b_blkno = bp->b_lblkno = bp->b_rawblkno = blkno;
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&vp->v_interlock);
|
1994-07-03 11:57:32 +04:00
|
|
|
bgetvp(vp, bp);
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&vp->v_interlock);
|
2003-12-30 15:33:13 +03:00
|
|
|
preserve = 0;
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
|
|
|
|
2003-09-24 14:44:44 +04:00
|
|
|
/*
|
2008-02-15 16:30:56 +03:00
|
|
|
* LFS can't track total size of B_LOCKED buffer (locked_queue_bytes)
|
2003-09-24 14:44:44 +04:00
|
|
|
* if we re-size buffers here.
|
|
|
|
*/
|
2008-02-15 16:30:56 +03:00
|
|
|
if (ISSET(bp->b_flags, B_LOCKED)) {
|
2003-09-24 14:44:44 +04:00
|
|
|
KASSERT(bp->b_bufsize >= size);
|
|
|
|
} else {
|
2008-01-02 14:48:20 +03:00
|
|
|
if (allocbuf(bp, size, preserve)) {
|
|
|
|
mutex_enter(&bufcache_lock);
|
|
|
|
LIST_REMOVE(bp, b_hash);
|
|
|
|
mutex_exit(&bufcache_lock);
|
|
|
|
brelse(bp, BC_INVAL);
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-09-24 14:44:44 +04:00
|
|
|
}
|
2004-01-10 17:39:50 +03:00
|
|
|
BIO_SETPRIO(bp, BPRIO_DEFAULT);
|
1994-07-03 11:57:32 +04:00
|
|
|
return (bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get an empty, disassociated buffer of given size.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *
|
2003-12-30 23:40:39 +03:00
|
|
|
geteblk(int size)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *bp;
|
|
|
|
int error;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
|
|
|
while ((bp = getnewbuf(0, 0, 0)) == NULL)
|
1994-07-03 11:57:32 +04:00
|
|
|
;
|
2003-02-06 00:38:38 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
SET(bp->b_cflags, BC_INVAL);
|
|
|
|
LIST_INSERT_HEAD(&invalhash, bp, b_hash);
|
|
|
|
mutex_exit(&bufcache_lock);
|
2004-01-10 17:43:05 +03:00
|
|
|
BIO_SETPRIO(bp, BPRIO_DEFAULT);
|
2008-01-02 14:48:20 +03:00
|
|
|
error = allocbuf(bp, size, 0);
|
|
|
|
KASSERT(error == 0);
|
1994-07-03 11:57:32 +04:00
|
|
|
return (bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand or contract the actual memory allocated to a buffer.
|
|
|
|
*
|
|
|
|
* If the buffer shrinks, data is lost, so it's up to the
|
|
|
|
* caller to have written it out *first*; this routine will not
|
|
|
|
* start a write. If the buffer grows, it's the callers
|
|
|
|
* responsibility to fill out the buffer's additional contents.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
int
|
|
|
|
allocbuf(buf_t *bp, int size, int preserve)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2003-12-30 15:33:13 +03:00
|
|
|
vsize_t oldsize, desired_size;
|
2007-03-04 08:59:00 +03:00
|
|
|
void *addr;
|
2008-01-02 14:48:20 +03:00
|
|
|
int delta;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
desired_size = buf_roundsize(size);
|
1994-07-03 11:57:32 +04:00
|
|
|
if (desired_size > MAXBSIZE)
|
2003-12-30 15:33:13 +03:00
|
|
|
printf("allocbuf: buffer larger than MAXBSIZE requested");
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
bp->b_bcount = size;
|
|
|
|
|
|
|
|
oldsize = bp->b_bufsize;
|
|
|
|
if (oldsize == desired_size)
|
2008-01-02 14:48:20 +03:00
|
|
|
return 0;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
|
|
|
/*
|
2003-12-30 15:33:13 +03:00
|
|
|
* If we want a buffer of a different size, re-allocate the
|
|
|
|
* buffer's memory; copy old content only if needed.
|
1994-07-03 11:57:32 +04:00
|
|
|
*/
|
2003-12-30 15:33:13 +03:00
|
|
|
addr = buf_malloc(desired_size);
|
2008-01-02 14:48:20 +03:00
|
|
|
if (addr == NULL)
|
|
|
|
return ENOMEM;
|
2003-12-30 15:33:13 +03:00
|
|
|
if (preserve)
|
|
|
|
memcpy(addr, bp->b_data, MIN(oldsize,desired_size));
|
|
|
|
if (bp->b_data != NULL)
|
|
|
|
buf_mrelease(bp->b_data, oldsize);
|
|
|
|
bp->b_data = addr;
|
|
|
|
bp->b_bufsize = desired_size;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
|
|
|
/*
|
2008-01-02 14:48:20 +03:00
|
|
|
* Update overall buffer memory counter (protected by bufcache_lock)
|
1994-07-03 11:57:32 +04:00
|
|
|
*/
|
2003-12-30 15:33:13 +03:00
|
|
|
delta = (long)desired_size - (long)oldsize;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
2003-12-30 15:33:13 +03:00
|
|
|
if ((bufmem += delta) > bufmem_hiwater) {
|
|
|
|
/*
|
|
|
|
* Need to trim overall memory usage.
|
|
|
|
*/
|
|
|
|
while (buf_canrelease()) {
|
2006-01-15 12:01:02 +03:00
|
|
|
if (curcpu()->ci_schedstate.spc_flags &
|
|
|
|
SPCF_SHOULDYIELD) {
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
2007-02-10 00:55:00 +03:00
|
|
|
preempt();
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
2006-01-15 12:01:02 +03:00
|
|
|
}
|
2003-12-30 15:33:13 +03:00
|
|
|
if (buf_trim() == 0)
|
|
|
|
break;
|
|
|
|
}
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
|
|
|
return 0;
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a buffer which is available for use.
|
|
|
|
* Select something from a free list.
|
2005-02-27 00:34:55 +03:00
|
|
|
* Preference is to AGE list, then LRU list.
|
2003-02-06 00:38:38 +03:00
|
|
|
*
|
2008-01-02 14:48:20 +03:00
|
|
|
* Called with the buffer queues locked.
|
2003-02-06 00:38:38 +03:00
|
|
|
* Return buffer locked.
|
1994-07-03 11:57:32 +04:00
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *
|
2003-12-30 23:40:39 +03:00
|
|
|
getnewbuf(int slpflag, int slptimeo, int from_bufq)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *bp;
|
|
|
|
struct vnode *vp;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
start:
|
|
|
|
KASSERT(mutex_owned(&bufcache_lock));
|
2003-02-06 00:38:38 +03:00
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
/*
|
2008-01-02 14:48:20 +03:00
|
|
|
* Get a new buffer from the pool.
|
2003-12-30 15:33:13 +03:00
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
if (!from_bufq && buf_lotsfree()) {
|
|
|
|
mutex_exit(&bufcache_lock);
|
|
|
|
bp = pool_cache_get(buf_cache, PR_NOWAIT);
|
|
|
|
if (bp != NULL) {
|
|
|
|
memset((char *)bp, 0, sizeof(*bp));
|
|
|
|
buf_init(bp);
|
|
|
|
bp->b_dev = NODEV;
|
|
|
|
bp->b_vnbufs.le_next = NOLIST;
|
|
|
|
bp->b_cflags = BC_BUSY;
|
|
|
|
bp->b_refcnt = 1;
|
|
|
|
mutex_enter(&bufcache_lock);
|
2004-09-18 20:37:12 +04:00
|
|
|
#if defined(DIAGNOSTIC)
|
2008-01-02 14:48:20 +03:00
|
|
|
bp->b_freelistindex = -1;
|
2004-09-18 20:37:12 +04:00
|
|
|
#endif /* defined(DIAGNOSTIC) */
|
2008-01-02 14:48:20 +03:00
|
|
|
return (bp);
|
|
|
|
}
|
|
|
|
mutex_enter(&bufcache_lock);
|
2003-12-30 15:33:13 +03:00
|
|
|
}
|
|
|
|
|
2004-09-18 20:37:12 +04:00
|
|
|
if ((bp = TAILQ_FIRST(&bufqueues[BQ_AGE].bq_queue)) != NULL ||
|
|
|
|
(bp = TAILQ_FIRST(&bufqueues[BQ_LRU].bq_queue)) != NULL) {
|
1994-07-03 11:57:32 +04:00
|
|
|
bremfree(bp);
|
|
|
|
} else {
|
2004-10-03 12:47:48 +04:00
|
|
|
/*
|
|
|
|
* XXX: !from_bufq should be removed.
|
|
|
|
*/
|
2007-07-10 00:51:58 +04:00
|
|
|
if (!from_bufq || curlwp != uvm.pagedaemon_lwp) {
|
2004-10-03 12:47:48 +04:00
|
|
|
/* wait for a free buffer of any kind */
|
2008-01-02 14:48:20 +03:00
|
|
|
if ((slpflag & PCATCH) != 0)
|
|
|
|
(void)cv_timedwait_sig(&needbuffer_cv,
|
|
|
|
&bufcache_lock, slptimeo);
|
|
|
|
else
|
|
|
|
(void)cv_timedwait(&needbuffer_cv,
|
|
|
|
&bufcache_lock, slptimeo);
|
2004-10-03 12:47:48 +04:00
|
|
|
}
|
2000-11-27 11:39:39 +03:00
|
|
|
return (NULL);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (bp->b_bufsize <= 0)
|
|
|
|
panic("buffer %p: on queue but empty", bp);
|
|
|
|
#endif
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if (ISSET(bp->b_cflags, BC_VFLUSH)) {
|
1997-04-10 01:12:10 +04:00
|
|
|
/*
|
|
|
|
* This is a delayed write buffer being flushed to disk. Make
|
|
|
|
* sure it gets aged out of the queue when it's finished, and
|
|
|
|
* leave it off the LRU queue.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
CLR(bp->b_cflags, BC_VFLUSH);
|
|
|
|
SET(bp->b_cflags, BC_AGE);
|
1997-04-10 01:12:10 +04:00
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
/* Buffer is no longer on free lists. */
|
2008-01-02 14:48:20 +03:00
|
|
|
SET(bp->b_cflags, BC_BUSY);
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2001-03-10 21:43:55 +03:00
|
|
|
/*
|
|
|
|
* If buffer was a delayed write, start it and return NULL
|
|
|
|
* (since we might sleep while starting the write).
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
if (ISSET(bp->b_oflags, BO_DELWRI)) {
|
1997-04-10 01:12:10 +04:00
|
|
|
/*
|
|
|
|
* This buffer has gone through the LRU, so make sure it gets
|
|
|
|
* reused ASAP.
|
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
SET(bp->b_cflags, BC_AGE);
|
|
|
|
mutex_exit(&bufcache_lock);
|
1997-04-10 01:12:10 +04:00
|
|
|
bawrite(bp);
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
2001-03-10 21:43:55 +03:00
|
|
|
return (NULL);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
vp = bp->b_vp;
|
|
|
|
if (bioopsp != NULL)
|
|
|
|
(*bioopsp->io_deallocate)(bp);
|
1999-11-15 21:49:07 +03:00
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
/* clear out various other fields */
|
2008-01-02 14:48:20 +03:00
|
|
|
bp->b_cflags = BC_BUSY;
|
|
|
|
bp->b_oflags = 0;
|
|
|
|
bp->b_flags = 0;
|
1994-07-03 11:57:32 +04:00
|
|
|
bp->b_dev = NODEV;
|
2008-01-02 14:48:20 +03:00
|
|
|
bp->b_blkno = 0;
|
|
|
|
bp->b_lblkno = 0;
|
|
|
|
bp->b_rawblkno = 0;
|
1994-07-03 11:57:32 +04:00
|
|
|
bp->b_iodone = 0;
|
|
|
|
bp->b_error = 0;
|
|
|
|
bp->b_resid = 0;
|
|
|
|
bp->b_bcount = 0;
|
2005-02-27 00:34:55 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
LIST_REMOVE(bp, b_hash);
|
|
|
|
|
|
|
|
/* Disassociate us from our vnode, if we had one... */
|
|
|
|
if (vp != NULL) {
|
|
|
|
mutex_enter(&vp->v_interlock);
|
|
|
|
brelvp(bp);
|
|
|
|
mutex_exit(&vp->v_interlock);
|
|
|
|
}
|
|
|
|
|
2005-02-27 00:34:55 +03:00
|
|
|
return (bp);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
/*
|
|
|
|
* Attempt to free an aged buffer off the queues.
|
2008-01-02 14:48:20 +03:00
|
|
|
* Called with queue lock held.
|
2003-12-30 15:33:13 +03:00
|
|
|
* Returns the amount of buffer memory freed.
|
|
|
|
*/
|
2004-09-18 20:01:03 +04:00
|
|
|
static int
|
2003-12-30 23:40:39 +03:00
|
|
|
buf_trim(void)
|
2003-12-30 15:33:13 +03:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *bp;
|
2003-12-30 15:33:13 +03:00
|
|
|
long size = 0;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(mutex_owned(&bufcache_lock));
|
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
/* Instruct getnewbuf() to get buffers off the queues */
|
2003-12-30 23:40:39 +03:00
|
|
|
if ((bp = getnewbuf(PCATCH, 1, 1)) == NULL)
|
2003-12-30 15:33:13 +03:00
|
|
|
return 0;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT((bp->b_cflags & BC_WANTED) == 0);
|
2003-12-30 15:33:13 +03:00
|
|
|
size = bp->b_bufsize;
|
|
|
|
bufmem -= size;
|
|
|
|
if (size > 0) {
|
|
|
|
buf_mrelease(bp->b_data, size);
|
|
|
|
bp->b_bcount = bp->b_bufsize = 0;
|
|
|
|
}
|
|
|
|
/* brelse() will return the buffer to the global buffer pool */
|
2008-01-02 14:48:20 +03:00
|
|
|
brelsel(bp, 0);
|
2003-12-30 15:33:13 +03:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2003-12-30 23:40:39 +03:00
|
|
|
int
|
|
|
|
buf_drain(int n)
|
2003-12-30 15:33:13 +03:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
int size = 0, sz;
|
2003-12-30 15:33:13 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(mutex_owned(&bufcache_lock));
|
2004-02-16 12:34:15 +03:00
|
|
|
|
2004-10-03 12:47:48 +04:00
|
|
|
while (size < n && bufmem > bufmem_lowater) {
|
|
|
|
sz = buf_trim();
|
|
|
|
if (sz <= 0)
|
|
|
|
break;
|
|
|
|
size += sz;
|
|
|
|
}
|
2004-01-30 14:32:16 +03:00
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
1994-07-03 11:57:32 +04:00
|
|
|
/*
|
|
|
|
* Wait for operations on the buffer to complete.
|
|
|
|
* When they do, extract and return the I/O's error value.
|
|
|
|
*/
|
|
|
|
int
|
2008-01-02 14:48:20 +03:00
|
|
|
biowait(buf_t *bp)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
|
|
|
|
mutex_enter(bp->b_objlock);
|
|
|
|
while (!ISSET(bp->b_oflags, BO_DONE | BO_DELWRI))
|
|
|
|
cv_wait(&bp->b_done, bp->b_objlock);
|
|
|
|
mutex_exit(bp->b_objlock);
|
|
|
|
|
|
|
|
return bp->b_error;
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark I/O complete on a buffer.
|
|
|
|
*
|
|
|
|
* If a callback has been requested, e.g. the pageout
|
|
|
|
* daemon, do so. Otherwise, awaken waiting processes.
|
|
|
|
*
|
|
|
|
* [ Leffler, et al., says on p.247:
|
|
|
|
* "This routine wakes up the blocked process, frees the buffer
|
|
|
|
* for an asynchronous write, or, for a request by the pagedaemon
|
|
|
|
* process, invokes a procedure specified in the buffer structure" ]
|
|
|
|
*
|
|
|
|
* In real life, the pagedaemon (or other system processes) wants
|
|
|
|
* to do async stuff to, and doesn't want the buffer brelse()'d.
|
|
|
|
* (for swap pager, that puts swap buffers on the free lists (!!!),
|
|
|
|
* for the vn device, that puts malloc'd buffers on the free lists!)
|
|
|
|
*/
|
|
|
|
void
|
2008-01-02 14:48:20 +03:00
|
|
|
biodone(buf_t *bp)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
int s;
|
1999-11-24 02:52:40 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(!ISSET(bp->b_oflags, BO_DONE));
|
|
|
|
|
|
|
|
if (cpu_intr_p()) {
|
|
|
|
/* From interrupt mode: defer to a soft interrupt. */
|
|
|
|
s = splvm();
|
|
|
|
TAILQ_INSERT_TAIL(&curcpu()->ci_data.cpu_biodone, bp, b_actq);
|
|
|
|
softint_schedule(biodone_sih);
|
|
|
|
splx(s);
|
|
|
|
} else {
|
|
|
|
/* Process now - the buffer may be freed soon. */
|
|
|
|
biodone2(bp);
|
|
|
|
}
|
|
|
|
}
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
static void
|
|
|
|
biodone2(buf_t *bp)
|
|
|
|
{
|
|
|
|
void (*callout)(buf_t *);
|
1999-11-15 21:49:07 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if (bioopsp != NULL)
|
|
|
|
(*bioopsp->io_complete)(bp);
|
|
|
|
|
|
|
|
mutex_enter(bp->b_objlock);
|
|
|
|
/* Note that the transfer is done. */
|
|
|
|
if (ISSET(bp->b_oflags, BO_DONE))
|
|
|
|
panic("biodone2 already");
|
2008-02-02 19:51:34 +03:00
|
|
|
CLR(bp->b_flags, B_COWDONE);
|
2008-01-02 14:48:20 +03:00
|
|
|
SET(bp->b_oflags, BO_DONE);
|
|
|
|
BIO_SETPRIO(bp, BPRIO_DEFAULT);
|
|
|
|
|
|
|
|
/* Wake up waiting writers. */
|
|
|
|
if (!ISSET(bp->b_flags, B_READ))
|
1994-07-03 11:57:32 +04:00
|
|
|
vwakeup(bp);
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if ((callout = bp->b_iodone) != NULL) {
|
|
|
|
/* Note callout done, then call out. */
|
|
|
|
KERNEL_LOCK(1, NULL); /* XXXSMP */
|
|
|
|
bp->b_iodone = NULL;
|
|
|
|
mutex_exit(bp->b_objlock);
|
|
|
|
(*callout)(bp);
|
|
|
|
KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
|
|
|
|
} else if (ISSET(bp->b_flags, B_ASYNC)) {
|
|
|
|
/* If async, release. */
|
|
|
|
mutex_exit(bp->b_objlock);
|
|
|
|
brelse(bp, 0);
|
1999-11-15 21:49:07 +03:00
|
|
|
} else {
|
2008-01-02 14:48:20 +03:00
|
|
|
/* Otherwise just wake up waiters in biowait(). */
|
|
|
|
cv_broadcast(&bp->b_done);
|
|
|
|
mutex_exit(bp->b_objlock);
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
biointr(void *cookie)
|
|
|
|
{
|
|
|
|
struct cpu_info *ci;
|
|
|
|
buf_t *bp;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
ci = curcpu();
|
1999-11-24 02:52:40 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
while (!TAILQ_EMPTY(&ci->ci_data.cpu_biodone)) {
|
|
|
|
KASSERT(curcpu() == ci);
|
|
|
|
|
|
|
|
s = splvm();
|
|
|
|
bp = TAILQ_FIRST(&ci->ci_data.cpu_biodone);
|
|
|
|
TAILQ_REMOVE(&ci->ci_data.cpu_biodone, bp, b_actq);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
biodone2(bp);
|
|
|
|
}
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a count of buffers on the "locked" queue.
|
|
|
|
*/
|
|
|
|
int
|
2003-12-30 23:40:39 +03:00
|
|
|
count_lock_queue(void)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *bp;
|
2000-03-30 13:27:11 +04:00
|
|
|
int n = 0;
|
1994-07-03 11:57:32 +04:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
2004-09-18 20:37:12 +04:00
|
|
|
TAILQ_FOREACH(bp, &bufqueues[BQ_LOCKED].bq_queue, b_freelist)
|
1994-07-03 11:57:32 +04:00
|
|
|
n++;
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
1994-07-03 11:57:32 +04:00
|
|
|
return (n);
|
|
|
|
}
|
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
/*
|
|
|
|
* Wait for all buffers to complete I/O
|
|
|
|
* Return the number of "stuck" buffers.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
buf_syncwait(void)
|
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *bp;
|
|
|
|
int iter, nbusy, nbusy_prev = 0, dcount, ihash;
|
2003-12-30 15:33:13 +03:00
|
|
|
|
|
|
|
dcount = 10000;
|
|
|
|
for (iter = 0; iter < 20;) {
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
2003-12-30 15:33:13 +03:00
|
|
|
nbusy = 0;
|
|
|
|
for (ihash = 0; ihash < bufhash+1; ihash++) {
|
|
|
|
LIST_FOREACH(bp, &bufhashtbl[ihash], b_hash) {
|
2008-01-02 14:48:20 +03:00
|
|
|
if ((bp->b_cflags & (BC_BUSY|BC_INVAL)) == BC_BUSY)
|
|
|
|
nbusy += ((bp->b_flags & B_READ) == 0);
|
2003-12-30 15:33:13 +03:00
|
|
|
/*
|
|
|
|
* With soft updates, some buffers that are
|
|
|
|
* written will be remarked as dirty until other
|
|
|
|
* buffers are written.
|
|
|
|
*/
|
|
|
|
if (bp->b_vp && bp->b_vp->v_mount
|
|
|
|
&& (bp->b_vp->v_mount->mnt_flag & MNT_SOFTDEP)
|
2008-01-02 14:48:20 +03:00
|
|
|
&& (bp->b_oflags & BO_DELWRI)) {
|
2003-12-30 15:33:13 +03:00
|
|
|
bremfree(bp);
|
2008-01-02 14:48:20 +03:00
|
|
|
bp->b_cflags |= BC_BUSY;
|
2003-12-30 15:33:13 +03:00
|
|
|
nbusy++;
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
2003-12-30 15:33:13 +03:00
|
|
|
bawrite(bp);
|
|
|
|
if (dcount-- <= 0) {
|
|
|
|
printf("softdep ");
|
|
|
|
goto fail;
|
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
2003-12-30 15:33:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
2003-12-30 15:33:13 +03:00
|
|
|
|
|
|
|
if (nbusy == 0)
|
|
|
|
break;
|
|
|
|
if (nbusy_prev == 0)
|
|
|
|
nbusy_prev = nbusy;
|
|
|
|
printf("%d ", nbusy);
|
|
|
|
tsleep(&nbusy, PRIBIO, "bflush",
|
|
|
|
(iter == 0) ? 1 : hz / 25 * iter);
|
|
|
|
if (nbusy >= nbusy_prev) /* we didn't flush anything */
|
|
|
|
iter++;
|
|
|
|
else
|
|
|
|
nbusy_prev = nbusy;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nbusy) {
|
|
|
|
fail:;
|
|
|
|
#if defined(DEBUG) || defined(DEBUG_HALT_BUSY)
|
|
|
|
printf("giving up\nPrinting vnodes for busy buffers\n");
|
|
|
|
for (ihash = 0; ihash < bufhash+1; ihash++) {
|
|
|
|
LIST_FOREACH(bp, &bufhashtbl[ihash], b_hash) {
|
2008-01-02 14:48:20 +03:00
|
|
|
if ((bp->b_cflags & (BC_BUSY|BC_INVAL)) == BC_BUSY &&
|
|
|
|
(bp->b_flags & B_READ) == 0)
|
2003-12-30 15:33:13 +03:00
|
|
|
vprint(NULL, bp->b_vp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return nbusy;
|
|
|
|
}
|
|
|
|
|
2004-02-19 06:56:30 +03:00
|
|
|
static void
|
2008-01-02 14:48:20 +03:00
|
|
|
sysctl_fillbuf(buf_t *i, struct buf_sysctl *o)
|
2004-02-19 06:56:30 +03:00
|
|
|
{
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
o->b_flags = i->b_flags | i->b_cflags | i->b_oflags;
|
2004-02-19 06:56:30 +03:00
|
|
|
o->b_error = i->b_error;
|
|
|
|
o->b_prio = i->b_prio;
|
|
|
|
o->b_dev = i->b_dev;
|
|
|
|
o->b_bufsize = i->b_bufsize;
|
|
|
|
o->b_bcount = i->b_bcount;
|
|
|
|
o->b_resid = i->b_resid;
|
2007-12-24 18:00:20 +03:00
|
|
|
o->b_addr = PTRTOUINT64(i->b_data);
|
2004-02-19 06:56:30 +03:00
|
|
|
o->b_blkno = i->b_blkno;
|
|
|
|
o->b_rawblkno = i->b_rawblkno;
|
|
|
|
o->b_iodone = PTRTOUINT64(i->b_iodone);
|
|
|
|
o->b_proc = PTRTOUINT64(i->b_proc);
|
|
|
|
o->b_vp = PTRTOUINT64(i->b_vp);
|
|
|
|
o->b_saveaddr = PTRTOUINT64(i->b_saveaddr);
|
|
|
|
o->b_lblkno = i->b_lblkno;
|
|
|
|
}
|
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
#define KERN_BUFSLOP 20
|
|
|
|
static int
|
|
|
|
sysctl_dobuf(SYSCTLFN_ARGS)
|
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *bp;
|
2004-02-19 06:56:30 +03:00
|
|
|
struct buf_sysctl bs;
|
2008-01-02 14:48:20 +03:00
|
|
|
struct bqueue *bq;
|
2003-12-30 15:33:13 +03:00
|
|
|
char *dp;
|
2004-02-19 06:56:30 +03:00
|
|
|
u_int i, op, arg;
|
|
|
|
size_t len, needed, elem_size, out_size;
|
2008-01-02 14:48:20 +03:00
|
|
|
int error, elem_count, retries;
|
2004-02-19 06:56:30 +03:00
|
|
|
|
|
|
|
if (namelen == 1 && name[0] == CTL_QUERY)
|
2005-06-09 06:19:59 +04:00
|
|
|
return (sysctl_query(SYSCTLFN_CALL(rnode)));
|
2004-02-19 06:56:30 +03:00
|
|
|
|
|
|
|
if (namelen != 4)
|
|
|
|
return (EINVAL);
|
2003-12-30 15:33:13 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
retries = 100;
|
|
|
|
retry:
|
2003-12-30 15:33:13 +03:00
|
|
|
dp = oldp;
|
2004-02-19 06:56:30 +03:00
|
|
|
len = (oldp != NULL) ? *oldlenp : 0;
|
|
|
|
op = name[0];
|
|
|
|
arg = name[1];
|
|
|
|
elem_size = name[2];
|
|
|
|
elem_count = name[3];
|
|
|
|
out_size = MIN(sizeof(bs), elem_size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* at the moment, these are just "placeholders" to make the
|
|
|
|
* API for retrieving kern.buf data more extensible in the
|
|
|
|
* future.
|
|
|
|
*
|
|
|
|
* XXX kern.buf currently has "netbsd32" issues. hopefully
|
|
|
|
* these will be resolved at a later point.
|
|
|
|
*/
|
|
|
|
if (op != KERN_BUF_ALL || arg != KERN_BUF_ALL ||
|
|
|
|
elem_size < 1 || elem_count < 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
2003-12-30 15:33:13 +03:00
|
|
|
error = 0;
|
|
|
|
needed = 0;
|
2008-01-07 19:12:52 +03:00
|
|
|
sysctl_unlock();
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
2003-12-30 15:33:13 +03:00
|
|
|
for (i = 0; i < BQUEUES; i++) {
|
2008-01-02 14:48:20 +03:00
|
|
|
bq = &bufqueues[i];
|
|
|
|
TAILQ_FOREACH(bp, &bq->bq_queue, b_freelist) {
|
|
|
|
bq->bq_marker = bp;
|
2004-02-19 06:56:30 +03:00
|
|
|
if (len >= elem_size && elem_count > 0) {
|
|
|
|
sysctl_fillbuf(bp, &bs);
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
2004-02-19 06:56:30 +03:00
|
|
|
error = copyout(&bs, dp, out_size);
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
2003-12-30 15:33:13 +03:00
|
|
|
if (error)
|
2008-01-02 14:48:20 +03:00
|
|
|
break;
|
|
|
|
if (bq->bq_marker != bp) {
|
|
|
|
/*
|
|
|
|
* This sysctl node is only for
|
|
|
|
* statistics. Retry; if the
|
|
|
|
* queue keeps changing, then
|
|
|
|
* bail out.
|
|
|
|
*/
|
|
|
|
if (retries-- == 0) {
|
|
|
|
error = EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_exit(&bufcache_lock);
|
|
|
|
goto retry;
|
|
|
|
}
|
2003-12-30 15:33:13 +03:00
|
|
|
dp += elem_size;
|
|
|
|
len -= elem_size;
|
|
|
|
}
|
2004-02-19 06:56:30 +03:00
|
|
|
if (elem_count > 0) {
|
|
|
|
needed += elem_size;
|
|
|
|
if (elem_count != INT_MAX)
|
|
|
|
elem_count--;
|
|
|
|
}
|
2003-12-30 15:33:13 +03:00
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
if (error != 0)
|
|
|
|
break;
|
2003-12-30 15:33:13 +03:00
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(&bufcache_lock);
|
2008-01-07 19:12:52 +03:00
|
|
|
sysctl_relock();
|
2003-12-30 15:33:13 +03:00
|
|
|
|
2004-02-19 06:56:30 +03:00
|
|
|
*oldlenp = needed;
|
|
|
|
if (oldp == NULL)
|
2008-01-02 14:48:20 +03:00
|
|
|
*oldlenp += KERN_BUFSLOP * sizeof(buf_t);
|
2003-12-30 15:33:13 +03:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2007-10-22 03:27:16 +04:00
|
|
|
static int
|
|
|
|
sysctl_bufvm_update(SYSCTLFN_ARGS)
|
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
int t, error, rv;
|
2007-10-22 03:27:16 +04:00
|
|
|
struct sysctlnode node;
|
|
|
|
|
|
|
|
node = *rnode;
|
|
|
|
node.sysctl_data = &t;
|
2008-01-02 14:48:20 +03:00
|
|
|
t = *(int *)rnode->sysctl_data;
|
2007-10-22 03:27:16 +04:00
|
|
|
error = sysctl_lookup(SYSCTLFN_CALL(&node));
|
|
|
|
if (error || newp == NULL)
|
|
|
|
return (error);
|
|
|
|
|
2005-03-31 06:34:10 +04:00
|
|
|
if (t < 0)
|
|
|
|
return EINVAL;
|
2008-01-02 14:48:20 +03:00
|
|
|
if (rnode->sysctl_data == &bufcache) {
|
|
|
|
if (t > 100)
|
|
|
|
return (EINVAL);
|
|
|
|
bufcache = t;
|
|
|
|
buf_setwm();
|
|
|
|
} else if (rnode->sysctl_data == &bufmem_lowater) {
|
2005-03-31 06:34:10 +04:00
|
|
|
if (bufmem_hiwater - t < 16)
|
2004-10-04 05:24:18 +04:00
|
|
|
return (EINVAL);
|
2003-12-30 15:33:13 +03:00
|
|
|
bufmem_lowater = t;
|
2004-02-19 06:56:30 +03:00
|
|
|
} else if (rnode->sysctl_data == &bufmem_hiwater) {
|
2005-03-31 06:34:10 +04:00
|
|
|
if (t - bufmem_lowater < 16)
|
2004-10-04 05:24:18 +04:00
|
|
|
return (EINVAL);
|
2003-12-30 15:33:13 +03:00
|
|
|
bufmem_hiwater = t;
|
|
|
|
} else
|
|
|
|
return (EINVAL);
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
/* Drain until below new high water mark */
|
2008-01-07 19:12:52 +03:00
|
|
|
sysctl_unlock();
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&bufcache_lock);
|
|
|
|
while ((t = bufmem - bufmem_hiwater) >= 0) {
|
|
|
|
rv = buf_drain(t / (2 * 1024));
|
|
|
|
if (rv <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_exit(&bufcache_lock);
|
2008-01-07 19:12:52 +03:00
|
|
|
sysctl_relock();
|
2003-12-30 15:33:13 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSCTL_SETUP(sysctl_kern_buf_setup, "sysctl kern.buf subtree setup")
|
|
|
|
{
|
|
|
|
|
2004-03-24 18:34:46 +03:00
|
|
|
sysctl_createv(clog, 0, NULL, NULL,
|
|
|
|
CTLFLAG_PERMANENT,
|
2004-01-06 16:51:09 +03:00
|
|
|
CTLTYPE_NODE, "kern", NULL,
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
CTL_KERN, CTL_EOL);
|
2004-03-24 18:34:46 +03:00
|
|
|
sysctl_createv(clog, 0, NULL, NULL,
|
|
|
|
CTLFLAG_PERMANENT,
|
2004-05-25 08:30:32 +04:00
|
|
|
CTLTYPE_NODE, "buf",
|
|
|
|
SYSCTL_DESCR("Kernel buffer cache information"),
|
2003-12-30 15:33:13 +03:00
|
|
|
sysctl_dobuf, 0, NULL, 0,
|
|
|
|
CTL_KERN, KERN_BUF, CTL_EOL);
|
2004-01-06 16:51:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SYSCTL_SETUP(sysctl_vm_buf_setup, "sysctl vm.buf* subtree setup")
|
|
|
|
{
|
|
|
|
|
2004-03-24 18:34:46 +03:00
|
|
|
sysctl_createv(clog, 0, NULL, NULL,
|
|
|
|
CTLFLAG_PERMANENT,
|
2004-01-06 16:51:09 +03:00
|
|
|
CTLTYPE_NODE, "vm", NULL,
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
CTL_VM, CTL_EOL);
|
2003-12-30 15:33:13 +03:00
|
|
|
|
2004-03-24 18:34:46 +03:00
|
|
|
sysctl_createv(clog, 0, NULL, NULL,
|
|
|
|
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
|
2004-05-25 08:30:32 +04:00
|
|
|
CTLTYPE_INT, "bufcache",
|
2004-12-05 09:12:54 +03:00
|
|
|
SYSCTL_DESCR("Percentage of physical memory to use for "
|
2004-05-25 08:30:32 +04:00
|
|
|
"buffer cache"),
|
2008-01-02 14:48:20 +03:00
|
|
|
sysctl_bufvm_update, 0, &bufcache, 0,
|
2004-02-19 06:56:30 +03:00
|
|
|
CTL_VM, CTL_CREATE, CTL_EOL);
|
2004-03-26 03:31:55 +03:00
|
|
|
sysctl_createv(clog, 0, NULL, NULL,
|
|
|
|
CTLFLAG_PERMANENT|CTLFLAG_READONLY,
|
2008-01-02 14:48:20 +03:00
|
|
|
CTLTYPE_INT, "bufmem",
|
2004-12-05 09:12:54 +03:00
|
|
|
SYSCTL_DESCR("Amount of kernel memory used by buffer "
|
2004-05-25 08:30:32 +04:00
|
|
|
"cache"),
|
2004-03-26 03:31:55 +03:00
|
|
|
NULL, 0, &bufmem, 0,
|
|
|
|
CTL_VM, CTL_CREATE, CTL_EOL);
|
2004-03-24 18:34:46 +03:00
|
|
|
sysctl_createv(clog, 0, NULL, NULL,
|
|
|
|
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
|
2008-01-02 14:48:20 +03:00
|
|
|
CTLTYPE_INT, "bufmem_lowater",
|
2004-05-25 08:30:32 +04:00
|
|
|
SYSCTL_DESCR("Minimum amount of kernel memory to "
|
|
|
|
"reserve for buffer cache"),
|
2004-02-19 06:56:30 +03:00
|
|
|
sysctl_bufvm_update, 0, &bufmem_lowater, 0,
|
|
|
|
CTL_VM, CTL_CREATE, CTL_EOL);
|
2004-03-24 18:34:46 +03:00
|
|
|
sysctl_createv(clog, 0, NULL, NULL,
|
|
|
|
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
|
2008-01-02 14:48:20 +03:00
|
|
|
CTLTYPE_INT, "bufmem_hiwater",
|
2004-05-25 08:30:32 +04:00
|
|
|
SYSCTL_DESCR("Maximum amount of kernel memory to use "
|
|
|
|
"for buffer cache"),
|
2004-02-19 06:56:30 +03:00
|
|
|
sysctl_bufvm_update, 0, &bufmem_hiwater, 0,
|
|
|
|
CTL_VM, CTL_CREATE, CTL_EOL);
|
2003-12-30 15:33:13 +03:00
|
|
|
}
|
|
|
|
|
1995-06-20 14:42:33 +04:00
|
|
|
#ifdef DEBUG
|
1994-07-03 11:57:32 +04:00
|
|
|
/*
|
|
|
|
* Print out statistics on the current allocation of the buffer pool.
|
|
|
|
* Can be enabled to print out on every ``sync'' by setting "syncprt"
|
|
|
|
* in vfs_syscalls.c using sysctl.
|
|
|
|
*/
|
|
|
|
void
|
2003-12-30 23:40:39 +03:00
|
|
|
vfs_bufstats(void)
|
1994-07-03 11:57:32 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
int i, j, count;
|
|
|
|
buf_t *bp;
|
2004-09-18 20:37:12 +04:00
|
|
|
struct bqueue *dp;
|
2000-11-18 18:58:13 +03:00
|
|
|
int counts[(MAXBSIZE / PAGE_SIZE) + 1];
|
2005-05-30 02:24:14 +04:00
|
|
|
static const char *bname[BQUEUES] = { "LOCKED", "LRU", "AGE" };
|
1994-07-03 11:57:32 +04:00
|
|
|
|
|
|
|
for (dp = bufqueues, i = 0; dp < &bufqueues[BQUEUES]; dp++, i++) {
|
|
|
|
count = 0;
|
2000-11-15 01:16:38 +03:00
|
|
|
for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
|
1994-07-03 11:57:32 +04:00
|
|
|
counts[j] = 0;
|
2004-09-18 20:37:12 +04:00
|
|
|
TAILQ_FOREACH(bp, &dp->bq_queue, b_freelist) {
|
2000-11-15 01:16:38 +03:00
|
|
|
counts[bp->b_bufsize/PAGE_SIZE]++;
|
1994-07-03 11:57:32 +04:00
|
|
|
count++;
|
|
|
|
}
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("%s: total-%d", bname[i], count);
|
2000-11-15 01:16:38 +03:00
|
|
|
for (j = 0; j <= MAXBSIZE/PAGE_SIZE; j++)
|
1994-07-03 11:57:32 +04:00
|
|
|
if (counts[j] != 0)
|
2000-11-15 01:16:38 +03:00
|
|
|
printf(", %d-%d", j * PAGE_SIZE, counts[j]);
|
1996-10-13 06:32:29 +04:00
|
|
|
printf("\n");
|
1994-07-03 11:57:32 +04:00
|
|
|
}
|
|
|
|
}
|
1995-06-20 14:42:33 +04:00
|
|
|
#endif /* DEBUG */
|
2006-01-04 13:13:05 +03:00
|
|
|
|
2006-01-05 13:18:20 +03:00
|
|
|
/* ------------------------------ */
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *
|
|
|
|
getiobuf(struct vnode *vp, bool waitok)
|
2006-01-04 13:13:05 +03:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *bp;
|
2006-01-04 13:13:05 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
bp = pool_cache_get(bufio_cache, (waitok ? PR_WAITOK : PR_NOWAIT));
|
|
|
|
if (bp == NULL)
|
|
|
|
return bp;
|
2006-01-04 13:13:05 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_init(bp);
|
2006-01-04 13:13:05 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if ((bp->b_vp = vp) == NULL)
|
|
|
|
bp->b_objlock = &buffer_lock;
|
|
|
|
else
|
|
|
|
bp->b_objlock = &vp->v_interlock;
|
|
|
|
|
|
|
|
return bp;
|
2006-01-04 13:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-01-02 14:48:20 +03:00
|
|
|
putiobuf(buf_t *bp)
|
2006-01-04 13:13:05 +03:00
|
|
|
{
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_destroy(bp);
|
|
|
|
pool_cache_put(bufio_cache, bp);
|
2006-01-04 13:13:05 +03:00
|
|
|
}
|
2006-01-11 03:44:41 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* nestiobuf_iodone: b_iodone callback for nested buffers.
|
|
|
|
*/
|
|
|
|
|
2006-11-14 18:50:00 +03:00
|
|
|
void
|
2008-01-02 14:48:20 +03:00
|
|
|
nestiobuf_iodone(buf_t *bp)
|
2006-01-11 03:44:41 +03:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
buf_t *mbp = bp->b_private;
|
2006-01-11 03:44:41 +03:00
|
|
|
int error;
|
2006-01-21 17:09:35 +03:00
|
|
|
int donebytes;
|
2006-01-11 03:44:41 +03:00
|
|
|
|
2006-01-21 17:09:35 +03:00
|
|
|
KASSERT(bp->b_bcount <= bp->b_bufsize);
|
2006-01-11 03:44:41 +03:00
|
|
|
KASSERT(mbp != bp);
|
2006-01-21 17:09:35 +03:00
|
|
|
|
|
|
|
error = 0;
|
2008-01-02 14:48:20 +03:00
|
|
|
if (bp->b_error == 0 &&
|
|
|
|
(bp->b_bcount < bp->b_bufsize || bp->b_resid > 0)) {
|
2006-01-21 17:09:35 +03:00
|
|
|
/*
|
|
|
|
* Not all got transfered, raise an error. We have no way to
|
|
|
|
* propagate these conditions to mbp.
|
|
|
|
*/
|
|
|
|
error = EIO;
|
2006-01-11 03:44:41 +03:00
|
|
|
}
|
2006-01-21 17:09:35 +03:00
|
|
|
|
2006-02-04 15:02:35 +03:00
|
|
|
donebytes = bp->b_bufsize;
|
2006-01-21 17:09:35 +03:00
|
|
|
|
2006-01-11 03:44:41 +03:00
|
|
|
putiobuf(bp);
|
|
|
|
nestiobuf_done(mbp, donebytes, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nestiobuf_setup: setup a "nested" buffer.
|
|
|
|
*
|
|
|
|
* => 'mbp' is a "master" buffer which is being divided into sub pieces.
|
|
|
|
* => 'bp' should be a buffer allocated by getiobuf or getiobuf_nowait.
|
|
|
|
* => 'offset' is a byte offset in the master buffer.
|
|
|
|
* => 'size' is a size in bytes of this nested buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2008-01-02 14:48:20 +03:00
|
|
|
nestiobuf_setup(buf_t *mbp, buf_t *bp, int offset, size_t size)
|
2006-01-11 03:44:41 +03:00
|
|
|
{
|
|
|
|
const int b_read = mbp->b_flags & B_READ;
|
|
|
|
struct vnode *vp = mbp->b_vp;
|
|
|
|
|
|
|
|
KASSERT(mbp->b_bcount >= offset + size);
|
|
|
|
bp->b_vp = vp;
|
2008-01-02 14:48:20 +03:00
|
|
|
bp->b_objlock = mbp->b_objlock;
|
|
|
|
bp->b_cflags = BC_BUSY;
|
|
|
|
bp->b_flags = B_ASYNC | b_read;
|
2006-01-11 03:44:41 +03:00
|
|
|
bp->b_iodone = nestiobuf_iodone;
|
2007-03-04 08:59:00 +03:00
|
|
|
bp->b_data = (char *)mbp->b_data + offset;
|
2006-01-11 03:44:41 +03:00
|
|
|
bp->b_resid = bp->b_bcount = size;
|
|
|
|
bp->b_bufsize = bp->b_bcount;
|
|
|
|
bp->b_private = mbp;
|
|
|
|
BIO_COPYPRIO(bp, mbp);
|
|
|
|
if (!b_read && vp != NULL) {
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(&vp->v_interlock);
|
|
|
|
vp->v_numoutput++;
|
|
|
|
mutex_exit(&vp->v_interlock);
|
2006-01-11 03:44:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nestiobuf_done: propagate completion to the master buffer.
|
|
|
|
*
|
|
|
|
* => 'donebytes' specifies how many bytes in the 'mbp' is completed.
|
|
|
|
* => 'error' is an errno(2) that 'donebytes' has been completed with.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2008-01-02 14:48:20 +03:00
|
|
|
nestiobuf_done(buf_t *mbp, int donebytes, int error)
|
2006-01-11 03:44:41 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
if (donebytes == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_enter(mbp->b_objlock);
|
2006-01-11 03:44:41 +03:00
|
|
|
KASSERT(mbp->b_resid >= donebytes);
|
|
|
|
mbp->b_resid -= donebytes;
|
2008-01-02 14:48:20 +03:00
|
|
|
mbp->b_error = error;
|
2006-01-11 03:44:41 +03:00
|
|
|
if (mbp->b_resid == 0) {
|
2008-01-02 14:48:20 +03:00
|
|
|
mutex_exit(mbp->b_objlock);
|
2006-01-11 03:44:41 +03:00
|
|
|
biodone(mbp);
|
2008-01-02 14:48:20 +03:00
|
|
|
} else
|
|
|
|
mutex_exit(mbp->b_objlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
buf_init(buf_t *bp)
|
|
|
|
{
|
|
|
|
|
|
|
|
LIST_INIT(&bp->b_dep);
|
|
|
|
cv_init(&bp->b_busy, "biolock");
|
|
|
|
cv_init(&bp->b_done, "biowait");
|
|
|
|
bp->b_dev = NODEV;
|
|
|
|
bp->b_error = 0;
|
|
|
|
bp->b_flags = 0;
|
|
|
|
bp->b_cflags = 0;
|
|
|
|
bp->b_oflags = 0;
|
|
|
|
bp->b_objlock = &buffer_lock;
|
|
|
|
bp->b_iodone = NULL;
|
|
|
|
BIO_SETPRIO(bp, BPRIO_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
buf_destroy(buf_t *bp)
|
|
|
|
{
|
|
|
|
|
|
|
|
cv_destroy(&bp->b_done);
|
|
|
|
cv_destroy(&bp->b_busy);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-02-15 16:46:04 +03:00
|
|
|
bbusy(buf_t *bp, bool intr, int timo, kmutex_t *interlock)
|
2008-01-02 14:48:20 +03:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
KASSERT(mutex_owned(&bufcache_lock));
|
|
|
|
|
|
|
|
if ((bp->b_cflags & BC_BUSY) != 0) {
|
|
|
|
if (curlwp == uvm.pagedaemon_lwp)
|
|
|
|
return EDEADLK;
|
|
|
|
bp->b_cflags |= BC_WANTED;
|
|
|
|
bref(bp);
|
2008-02-15 16:46:04 +03:00
|
|
|
if (interlock != NULL)
|
|
|
|
mutex_exit(interlock);
|
2008-01-02 14:48:20 +03:00
|
|
|
if (intr) {
|
|
|
|
error = cv_timedwait_sig(&bp->b_busy, &bufcache_lock,
|
|
|
|
timo);
|
|
|
|
} else {
|
|
|
|
error = cv_timedwait(&bp->b_busy, &bufcache_lock,
|
|
|
|
timo);
|
|
|
|
}
|
|
|
|
brele(bp);
|
2008-02-15 16:46:04 +03:00
|
|
|
if (interlock != NULL)
|
|
|
|
mutex_enter(interlock);
|
2008-01-02 14:48:20 +03:00
|
|
|
if (error != 0)
|
|
|
|
return error;
|
|
|
|
return EPASSTHROUGH;
|
2006-01-11 03:44:41 +03:00
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
bp->b_cflags |= BC_BUSY;
|
|
|
|
|
|
|
|
return 0;
|
2006-01-11 03:44:41 +03:00
|
|
|
}
|