Home | History | Annotate | Line # | Download | only in mpool
mpool.c revision 1.22
      1  1.22  christos /*	$NetBSD: mpool.c,v 1.22 2016/09/24 20:11:12 christos Exp $	*/
      2   1.5       cgd 
      3   1.1       cgd /*-
      4   1.6       cgd  * Copyright (c) 1990, 1993, 1994
      5   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.14       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.18     joerg #if HAVE_NBTOOL_CONFIG_H
     33  1.18     joerg #include "nbtool_config.h"
     34  1.18     joerg #endif
     35  1.18     joerg 
     36   1.7  christos #include <sys/cdefs.h>
     37  1.22  christos __RCSID("$NetBSD: mpool.c,v 1.22 2016/09/24 20:11:12 christos Exp $");
     38   1.1       cgd 
     39   1.8       jtc #include "namespace.h"
     40   1.6       cgd #include <sys/queue.h>
     41   1.1       cgd #include <sys/stat.h>
     42   1.1       cgd 
     43   1.1       cgd #include <errno.h>
     44   1.1       cgd #include <stdio.h>
     45   1.1       cgd #include <stdlib.h>
     46   1.1       cgd #include <string.h>
     47   1.1       cgd #include <unistd.h>
     48   1.1       cgd 
     49   1.1       cgd #include <db.h>
     50   1.6       cgd 
     51   1.1       cgd #define	__MPOOLINTERFACE_PRIVATE
     52   1.6       cgd #include <mpool.h>
     53   1.8       jtc 
     54   1.8       jtc #ifdef __weak_alias
     55  1.12   mycroft __weak_alias(mpool_close,_mpool_close)
     56  1.12   mycroft __weak_alias(mpool_filter,_mpool_filter)
     57  1.12   mycroft __weak_alias(mpool_get,_mpool_get)
     58  1.22  christos __weak_alias(mpool_getf,_mpool_getf)
     59  1.12   mycroft __weak_alias(mpool_new,_mpool_new)
     60  1.22  christos __weak_alias(mpool_newf,_mpool_newf)
     61  1.12   mycroft __weak_alias(mpool_open,_mpool_open)
     62  1.12   mycroft __weak_alias(mpool_put,_mpool_put)
     63  1.12   mycroft __weak_alias(mpool_sync,_mpool_sync)
     64   1.8       jtc #endif
     65   1.1       cgd 
     66  1.16  christos static BKT *mpool_bkt(MPOOL *);
     67  1.16  christos static BKT *mpool_look(MPOOL *, pgno_t);
     68  1.16  christos static int  mpool_write(MPOOL *, BKT *);
     69   1.1       cgd 
     70   1.1       cgd /*
     71   1.6       cgd  * mpool_open --
     72   1.6       cgd  *	Initialize a memory pool.
     73   1.1       cgd  */
     74  1.10  christos /*ARGSUSED*/
     75   1.1       cgd MPOOL *
     76  1.16  christos mpool_open(void *key, int fd, pgno_t pagesize, pgno_t maxcache)
     77   1.1       cgd {
     78   1.1       cgd 	struct stat sb;
     79   1.1       cgd 	MPOOL *mp;
     80   1.1       cgd 	int entry;
     81   1.1       cgd 
     82   1.6       cgd 	/*
     83   1.6       cgd 	 * Get information about the file.
     84   1.6       cgd 	 *
     85   1.6       cgd 	 * XXX
     86   1.6       cgd 	 * We don't currently handle pipes, although we should.
     87   1.6       cgd 	 */
     88   1.1       cgd 	if (fstat(fd, &sb))
     89  1.21  christos 		return NULL;
     90   1.1       cgd 	if (!S_ISREG(sb.st_mode)) {
     91   1.1       cgd 		errno = ESPIPE;
     92  1.21  christos 		return NULL;
     93   1.1       cgd 	}
     94   1.1       cgd 
     95   1.6       cgd 	/* Allocate and initialize the MPOOL cookie. */
     96  1.21  christos 	if ((mp = calloc(1, sizeof(*mp))) == NULL)
     97   1.1       cgd 		return (NULL);
     98  1.20  christos 	TAILQ_INIT(&mp->lqh);
     99   1.1       cgd 	for (entry = 0; entry < HASHSIZE; ++entry)
    100  1.20  christos 		TAILQ_INIT(&mp->hqh[entry]);
    101   1.1       cgd 	mp->maxcache = maxcache;
    102  1.10  christos 	mp->npages = (pgno_t)(sb.st_size / pagesize);
    103   1.1       cgd 	mp->pagesize = pagesize;
    104   1.1       cgd 	mp->fd = fd;
    105  1.21  christos 	return mp;
    106   1.1       cgd }
    107   1.1       cgd 
    108   1.1       cgd /*
    109   1.6       cgd  * mpool_filter --
    110   1.6       cgd  *	Initialize input/output filters.
    111   1.1       cgd  */
    112   1.1       cgd void
    113  1.16  christos mpool_filter(MPOOL *mp, void (*pgin)(void *, pgno_t, void *),
    114  1.16  christos     void (*pgout)(void *, pgno_t, void *), void *pgcookie)
    115   1.1       cgd {
    116   1.1       cgd 	mp->pgin = pgin;
    117   1.1       cgd 	mp->pgout = pgout;
    118   1.1       cgd 	mp->pgcookie = pgcookie;
    119   1.1       cgd }
    120   1.1       cgd 
    121   1.1       cgd /*
    122   1.6       cgd  * mpool_new --
    123   1.6       cgd  *	Get a new page of memory.
    124   1.1       cgd  */
    125   1.1       cgd void *
    126  1.22  christos mpool_newf(MPOOL *mp, pgno_t *pgnoaddr, unsigned int flags)
    127   1.1       cgd {
    128   1.6       cgd 	struct _hqh *head;
    129   1.6       cgd 	BKT *bp;
    130   1.1       cgd 
    131   1.6       cgd 	if (mp->npages == MAX_PAGE_NUMBER) {
    132   1.6       cgd 		(void)fprintf(stderr, "mpool_new: page allocation overflow.\n");
    133   1.6       cgd 		abort();
    134   1.6       cgd 	}
    135   1.1       cgd #ifdef STATISTICS
    136   1.1       cgd 	++mp->pagenew;
    137   1.1       cgd #endif
    138   1.1       cgd 	/*
    139   1.6       cgd 	 * Get a BKT from the cache.  Assign a new page number, attach
    140   1.6       cgd 	 * it to the head of the hash chain, the tail of the lru chain,
    141   1.6       cgd 	 * and return.
    142   1.1       cgd 	 */
    143   1.6       cgd 	if ((bp = mpool_bkt(mp)) == NULL)
    144  1.21  christos 		return NULL;
    145  1.22  christos 
    146  1.22  christos 	if (flags == MPOOL_PAGE_REQUEST) {
    147  1.22  christos 		mp->npages++;
    148  1.22  christos 		bp->pgno = *pgnoaddr;
    149  1.22  christos 	} else
    150  1.22  christos 		bp->pgno = *pgnoaddr = mp->npages++;
    151  1.22  christos 
    152  1.22  christos 	bp->flags = MPOOL_PINNED | MPOOL_INUSE;
    153   1.6       cgd 
    154   1.6       cgd 	head = &mp->hqh[HASHKEY(bp->pgno)];
    155  1.20  christos 	TAILQ_INSERT_HEAD(head, bp, hq);
    156  1.20  christos 	TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
    157  1.21  christos 	return bp->page;
    158   1.1       cgd }
    159   1.1       cgd 
    160  1.22  christos void *
    161  1.22  christos mpool_new(MPOOL *mp, pgno_t *pgnoaddr)
    162  1.22  christos {
    163  1.22  christos 	return mpool_newf(mp, pgnoaddr, 0);
    164  1.22  christos }
    165  1.22  christos 
    166  1.22  christos int
    167  1.22  christos mpool_delete(MPOOL *mp, void *page)
    168  1.22  christos {
    169  1.22  christos 	struct _hqh *head;
    170  1.22  christos 	BKT *bp;
    171  1.22  christos 
    172  1.22  christos 	bp = (void *)((char *)page - sizeof(BKT));
    173  1.22  christos 
    174  1.22  christos #ifdef DEBUG
    175  1.22  christos 	if (!(bp->flags & MPOOL_PINNED)) {
    176  1.22  christos 	       (void)fprintf(stderr,
    177  1.22  christos 		   "%s: page %d not pinned\n", __func__, bp->pgno);
    178  1.22  christos 	       abort();
    179  1.22  christos 	}
    180  1.22  christos #endif
    181  1.22  christos 
    182  1.22  christos 	/* Remove from the hash and lru queues. */
    183  1.22  christos 	head = &mp->hqh[HASHKEY(bp->pgno)];
    184  1.22  christos 	TAILQ_REMOVE(head, bp, hq);
    185  1.22  christos 	TAILQ_REMOVE(&mp->lqh, bp, q);
    186  1.22  christos 
    187  1.22  christos 	free(bp);
    188  1.22  christos 	return RET_SUCCESS;
    189  1.22  christos }
    190  1.22  christos 
    191   1.1       cgd /*
    192   1.6       cgd  * mpool_get
    193   1.6       cgd  *	Get a page.
    194   1.1       cgd  */
    195  1.10  christos /*ARGSUSED*/
    196   1.1       cgd void *
    197  1.22  christos mpool_getf(MPOOL *mp, pgno_t pgno, unsigned int flags)
    198   1.1       cgd {
    199   1.6       cgd 	struct _hqh *head;
    200   1.6       cgd 	BKT *bp;
    201   1.1       cgd 	off_t off;
    202  1.16  christos 	ssize_t nr;
    203   1.1       cgd 
    204   1.6       cgd 	/* Check for attempt to retrieve a non-existent page. */
    205   1.6       cgd 	if (pgno >= mp->npages) {
    206   1.6       cgd 		errno = EINVAL;
    207  1.21  christos 		return NULL;
    208   1.6       cgd 	}
    209   1.6       cgd 
    210   1.1       cgd #ifdef STATISTICS
    211   1.6       cgd 	++mp->pageget;
    212   1.1       cgd #endif
    213   1.6       cgd 
    214   1.6       cgd 	/* Check for a page that is cached. */
    215   1.6       cgd 	if ((bp = mpool_look(mp, pgno)) != NULL) {
    216   1.1       cgd #ifdef DEBUG
    217  1.22  christos 		if (!(flags & MPOOL_IGNOREPIN) && bp->flags & MPOOL_PINNED) {
    218   1.6       cgd 			(void)fprintf(stderr,
    219   1.6       cgd 			    "mpool_get: page %d already pinned\n", bp->pgno);
    220   1.6       cgd 			abort();
    221   1.6       cgd 		}
    222   1.6       cgd #endif
    223   1.6       cgd 		/*
    224   1.6       cgd 		 * Move the page to the head of the hash chain and the tail
    225   1.6       cgd 		 * of the lru chain.
    226   1.6       cgd 		 */
    227   1.6       cgd 		head = &mp->hqh[HASHKEY(bp->pgno)];
    228  1.20  christos 		TAILQ_REMOVE(head, bp, hq);
    229  1.20  christos 		TAILQ_INSERT_HEAD(head, bp, hq);
    230  1.20  christos 		TAILQ_REMOVE(&mp->lqh, bp, q);
    231  1.20  christos 		TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
    232   1.6       cgd 
    233   1.6       cgd 		/* Return a pinned page. */
    234  1.22  christos 		if (!(flags & MPOOL_IGNOREPIN))
    235  1.22  christos 			bp->flags |= MPOOL_PINNED;
    236  1.21  christos 		return bp->page;
    237   1.1       cgd 	}
    238   1.1       cgd 
    239   1.1       cgd 	/* Get a page from the cache. */
    240   1.6       cgd 	if ((bp = mpool_bkt(mp)) == NULL)
    241  1.21  christos 		return NULL;
    242   1.1       cgd 
    243   1.6       cgd 	/* Read in the contents. */
    244   1.1       cgd #ifdef STATISTICS
    245   1.1       cgd 	++mp->pageread;
    246   1.1       cgd #endif
    247   1.1       cgd 	off = mp->pagesize * pgno;
    248  1.22  christos 	if (off / mp->pagesize != pgno) {
    249  1.22  christos 		/* Run past the end of the file, or at least the part we
    250  1.22  christos 		   can address without large-file support?  */
    251  1.22  christos 		errno = E2BIG;
    252  1.22  christos  		return NULL;
    253  1.22  christos 	}
    254  1.22  christos 
    255  1.10  christos 	if ((nr = pread(mp->fd, bp->page, (size_t)mp->pagesize, off)) != (int)mp->pagesize) {
    256  1.22  christos 		if (nr > 0) {
    257   1.1       cgd 			errno = EFTYPE;
    258  1.22  christos 			return NULL;
    259  1.22  christos 		} else if (nr == 0) {
    260  1.22  christos 			/*
    261  1.22  christos 			 * A zero-length reads, means you need to create a
    262  1.22  christos 			 * new page.
    263  1.22  christos 			 */
    264  1.22  christos 			memset(bp->page, 0, mp->pagesize);
    265  1.22  christos 		} else
    266  1.22  christos 			return NULL;
    267   1.1       cgd 	}
    268   1.1       cgd 
    269   1.6       cgd 	/* Set the page number, pin the page. */
    270   1.6       cgd 	bp->pgno = pgno;
    271  1.22  christos 	if (!(flags & MPOOL_IGNOREPIN))
    272  1.22  christos 		bp->flags = MPOOL_PINNED;
    273  1.22  christos bp->flags |= MPOOL_INUSE;
    274   1.6       cgd 
    275   1.6       cgd 	/*
    276   1.6       cgd 	 * Add the page to the head of the hash chain and the tail
    277   1.6       cgd 	 * of the lru chain.
    278   1.6       cgd 	 */
    279   1.6       cgd 	head = &mp->hqh[HASHKEY(bp->pgno)];
    280  1.20  christos 	TAILQ_INSERT_HEAD(head, bp, hq);
    281  1.20  christos 	TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
    282   1.6       cgd 
    283   1.6       cgd 	/* Run through the user's filter. */
    284   1.6       cgd 	if (mp->pgin != NULL)
    285   1.6       cgd 		(mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
    286   1.6       cgd 
    287  1.21  christos 	return bp->page;
    288   1.1       cgd }
    289   1.1       cgd 
    290  1.22  christos void *
    291  1.22  christos mpool_get(MPOOL *mp, pgno_t pgno)
    292  1.22  christos {
    293  1.22  christos 	return mpool_getf(mp, pgno, 0);
    294  1.22  christos }
    295  1.22  christos 
    296   1.1       cgd /*
    297   1.6       cgd  * mpool_put
    298   1.6       cgd  *	Return a page.
    299   1.1       cgd  */
    300  1.10  christos /*ARGSUSED*/
    301   1.1       cgd int
    302  1.16  christos mpool_put(MPOOL *mp, void *page, u_int flags)
    303   1.1       cgd {
    304   1.6       cgd 	BKT *bp;
    305   1.1       cgd 
    306   1.1       cgd #ifdef STATISTICS
    307   1.1       cgd 	++mp->pageput;
    308   1.1       cgd #endif
    309  1.21  christos 	bp = (void *)((intptr_t)page - sizeof(BKT));
    310   1.1       cgd #ifdef DEBUG
    311   1.6       cgd 	if (!(bp->flags & MPOOL_PINNED)) {
    312   1.6       cgd 		(void)fprintf(stderr,
    313   1.6       cgd 		    "mpool_put: page %d not pinned\n", bp->pgno);
    314   1.6       cgd 		abort();
    315   1.1       cgd 	}
    316   1.1       cgd #endif
    317   1.6       cgd 	bp->flags &= ~MPOOL_PINNED;
    318  1.22  christos 	if (flags & MPOOL_DIRTY)
    319  1.22  christos 		bp->flags |= flags & MPOOL_DIRTY;
    320   1.1       cgd 	return (RET_SUCCESS);
    321   1.1       cgd }
    322   1.1       cgd 
    323   1.1       cgd /*
    324   1.6       cgd  * mpool_close
    325   1.6       cgd  *	Close the buffer pool.
    326   1.1       cgd  */
    327   1.1       cgd int
    328  1.16  christos mpool_close(MPOOL *mp)
    329   1.1       cgd {
    330   1.6       cgd 	BKT *bp;
    331   1.1       cgd 
    332   1.1       cgd 	/* Free up any space allocated to the lru pages. */
    333  1.20  christos 	while (!TAILQ_EMPTY(&mp->lqh)) {
    334  1.20  christos 		bp = TAILQ_FIRST(&mp->lqh);
    335  1.20  christos 		TAILQ_REMOVE(&mp->lqh, bp, q);
    336   1.6       cgd 		free(bp);
    337   1.1       cgd 	}
    338   1.6       cgd 
    339   1.6       cgd 	/* Free the MPOOL cookie. */
    340   1.1       cgd 	free(mp);
    341  1.21  christos 	return RET_SUCCESS;
    342   1.1       cgd }
    343   1.1       cgd 
    344   1.1       cgd /*
    345   1.6       cgd  * mpool_sync
    346   1.6       cgd  *	Sync the pool to disk.
    347   1.1       cgd  */
    348   1.1       cgd int
    349  1.16  christos mpool_sync(MPOOL *mp)
    350   1.1       cgd {
    351   1.6       cgd 	BKT *bp;
    352   1.1       cgd 
    353   1.6       cgd 	/* Walk the lru chain, flushing any dirty pages to disk. */
    354  1.20  christos 	TAILQ_FOREACH(bp, &mp->lqh, q)
    355   1.6       cgd 		if (bp->flags & MPOOL_DIRTY &&
    356   1.6       cgd 		    mpool_write(mp, bp) == RET_ERROR)
    357  1.21  christos 			return RET_ERROR;
    358   1.6       cgd 
    359   1.6       cgd 	/* Sync the file descriptor. */
    360  1.21  christos 	return fsync(mp->fd) ? RET_ERROR : RET_SUCCESS;
    361   1.1       cgd }
    362   1.1       cgd 
    363   1.1       cgd /*
    364   1.6       cgd  * mpool_bkt
    365   1.6       cgd  *	Get a page from the cache (or create one).
    366   1.1       cgd  */
    367   1.1       cgd static BKT *
    368  1.16  christos mpool_bkt(MPOOL *mp)
    369   1.1       cgd {
    370   1.6       cgd 	struct _hqh *head;
    371   1.6       cgd 	BKT *bp;
    372   1.1       cgd 
    373   1.6       cgd 	/* If under the max cached, always create a new page. */
    374   1.1       cgd 	if (mp->curcache < mp->maxcache)
    375   1.1       cgd 		goto new;
    376   1.1       cgd 
    377   1.1       cgd 	/*
    378   1.6       cgd 	 * If the cache is max'd out, walk the lru list for a buffer we
    379   1.6       cgd 	 * can flush.  If we find one, write it (if necessary) and take it
    380   1.6       cgd 	 * off any lists.  If we don't find anything we grow the cache anyway.
    381   1.1       cgd 	 * The cache never shrinks.
    382   1.1       cgd 	 */
    383  1.20  christos 	TAILQ_FOREACH(bp, &mp->lqh, q)
    384   1.6       cgd 		if (!(bp->flags & MPOOL_PINNED)) {
    385   1.6       cgd 			/* Flush if dirty. */
    386   1.6       cgd 			if (bp->flags & MPOOL_DIRTY &&
    387   1.6       cgd 			    mpool_write(mp, bp) == RET_ERROR)
    388  1.21  christos 				return NULL;
    389   1.1       cgd #ifdef STATISTICS
    390   1.1       cgd 			++mp->pageflush;
    391   1.1       cgd #endif
    392   1.6       cgd 			/* Remove from the hash and lru queues. */
    393   1.6       cgd 			head = &mp->hqh[HASHKEY(bp->pgno)];
    394  1.20  christos 			TAILQ_REMOVE(head, bp, hq);
    395  1.20  christos 			TAILQ_REMOVE(&mp->lqh, bp, q);
    396   1.1       cgd #ifdef DEBUG
    397  1.15  christos 			{
    398  1.15  christos 				void *spage = bp->page;
    399  1.15  christos 				(void)memset(bp, 0xff,
    400  1.15  christos 				    (size_t)(sizeof(BKT) + mp->pagesize));
    401   1.6       cgd 				bp->page = spage;
    402   1.1       cgd 			}
    403   1.1       cgd #endif
    404  1.21  christos 			return bp;
    405   1.1       cgd 		}
    406   1.1       cgd 
    407  1.19  christos new:	if ((bp = calloc(1, (size_t)(sizeof(BKT) + mp->pagesize))) == NULL)
    408  1.21  christos 		return NULL;
    409   1.1       cgd #ifdef STATISTICS
    410   1.1       cgd 	++mp->pagealloc;
    411   1.1       cgd #endif
    412   1.6       cgd #if defined(DEBUG) || defined(PURIFY)
    413  1.15  christos 	(void)memset(bp, 0xff, (size_t)(sizeof(BKT) + mp->pagesize));
    414   1.1       cgd #endif
    415  1.21  christos 	bp->page = (void *)((intptr_t)bp + sizeof(BKT));
    416   1.1       cgd 	++mp->curcache;
    417  1.21  christos 	return bp;
    418   1.1       cgd }
    419   1.1       cgd 
    420   1.1       cgd /*
    421   1.6       cgd  * mpool_write
    422   1.6       cgd  *	Write a page to disk.
    423   1.1       cgd  */
    424   1.1       cgd static int
    425  1.16  christos mpool_write(MPOOL *mp, BKT *bp)
    426   1.1       cgd {
    427   1.1       cgd 	off_t off;
    428   1.1       cgd 
    429   1.1       cgd #ifdef STATISTICS
    430   1.1       cgd 	++mp->pagewrite;
    431   1.1       cgd #endif
    432   1.6       cgd 
    433   1.6       cgd 	/* Run through the user's filter. */
    434   1.6       cgd 	if (mp->pgout)
    435   1.6       cgd 		(mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
    436   1.6       cgd 
    437   1.6       cgd 	off = mp->pagesize * bp->pgno;
    438  1.22  christos 	if (off / mp->pagesize != bp->pgno) {
    439  1.22  christos 		/* Run past the end of the file, or at least the part we
    440  1.22  christos 		   can address without large-file support?  */
    441  1.22  christos 		errno = E2BIG;
    442  1.22  christos 		return RET_ERROR;
    443  1.22  christos 	}
    444  1.22  christos 
    445  1.21  christos 	if (pwrite(mp->fd, bp->page, (size_t)mp->pagesize, off) !=
    446  1.21  christos 	    (ssize_t)mp->pagesize)
    447  1.21  christos 		return RET_ERROR;
    448  1.11       scw 
    449  1.11       scw 	/*
    450  1.11       scw 	 * Re-run through the input filter since this page may soon be
    451  1.11       scw 	 * accessed via the cache, and whatever the user's output filter
    452  1.11       scw 	 * did may screw things up if we don't let the input filter
    453  1.11       scw 	 * restore the in-core copy.
    454  1.11       scw 	 */
    455  1.11       scw 	if (mp->pgin)
    456  1.11       scw 		(mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
    457   1.6       cgd 
    458   1.6       cgd 	bp->flags &= ~MPOOL_DIRTY;
    459  1.21  christos 	return RET_SUCCESS;
    460   1.1       cgd }
    461   1.1       cgd 
    462   1.1       cgd /*
    463   1.6       cgd  * mpool_look
    464   1.6       cgd  *	Lookup a page in the cache.
    465   1.1       cgd  */
    466   1.1       cgd static BKT *
    467  1.16  christos mpool_look(MPOOL *mp, pgno_t pgno)
    468   1.1       cgd {
    469   1.6       cgd 	struct _hqh *head;
    470   1.6       cgd 	BKT *bp;
    471   1.1       cgd 
    472   1.6       cgd 	head = &mp->hqh[HASHKEY(pgno)];
    473  1.20  christos 	TAILQ_FOREACH(bp, head, hq)
    474   1.6       cgd 		if (bp->pgno == pgno) {
    475   1.1       cgd #ifdef STATISTICS
    476   1.1       cgd 			++mp->cachehit;
    477   1.1       cgd #endif
    478  1.21  christos 			return bp;
    479   1.1       cgd 		}
    480   1.1       cgd #ifdef STATISTICS
    481   1.1       cgd 	++mp->cachemiss;
    482   1.1       cgd #endif
    483  1.21  christos 	return NULL;
    484   1.1       cgd }
    485   1.1       cgd 
    486   1.1       cgd #ifdef STATISTICS
    487   1.1       cgd /*
    488   1.6       cgd  * mpool_stat
    489   1.6       cgd  *	Print out cache statistics.
    490   1.1       cgd  */
    491   1.1       cgd void
    492   1.1       cgd mpool_stat(mp)
    493   1.1       cgd 	MPOOL *mp;
    494   1.1       cgd {
    495   1.6       cgd 	BKT *bp;
    496   1.1       cgd 	int cnt;
    497  1.16  christos 	const char *sep;
    498   1.1       cgd 
    499  1.16  christos 	(void)fprintf(stderr, "%lu pages in the file\n", (u_long)mp->npages);
    500   1.1       cgd 	(void)fprintf(stderr,
    501   1.1       cgd 	    "page size %lu, cacheing %lu pages of %lu page max cache\n",
    502  1.16  christos 	    (u_long)mp->pagesize, (u_long)mp->curcache, (u_long)mp->maxcache);
    503   1.1       cgd 	(void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
    504   1.1       cgd 	    mp->pageput, mp->pageget, mp->pagenew);
    505   1.1       cgd 	(void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
    506   1.1       cgd 	    mp->pagealloc, mp->pageflush);
    507   1.1       cgd 	if (mp->cachehit + mp->cachemiss)
    508   1.1       cgd 		(void)fprintf(stderr,
    509   1.1       cgd 		    "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
    510   1.1       cgd 		    ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
    511   1.1       cgd 		    * 100, mp->cachehit, mp->cachemiss);
    512   1.1       cgd 	(void)fprintf(stderr, "%lu page reads, %lu page writes\n",
    513   1.1       cgd 	    mp->pageread, mp->pagewrite);
    514   1.1       cgd 
    515   1.1       cgd 	sep = "";
    516   1.1       cgd 	cnt = 0;
    517  1.20  christos 	TAILQ_FOREACH(bp, &mp->lqh, q) {
    518   1.6       cgd 		(void)fprintf(stderr, "%s%d", sep, bp->pgno);
    519   1.6       cgd 		if (bp->flags & MPOOL_DIRTY)
    520   1.1       cgd 			(void)fprintf(stderr, "d");
    521   1.6       cgd 		if (bp->flags & MPOOL_PINNED)
    522   1.1       cgd 			(void)fprintf(stderr, "P");
    523   1.1       cgd 		if (++cnt == 10) {
    524   1.1       cgd 			sep = "\n";
    525   1.1       cgd 			cnt = 0;
    526   1.1       cgd 		} else
    527   1.1       cgd 			sep = ", ";
    528   1.1       cgd 
    529   1.1       cgd 	}
    530   1.1       cgd 	(void)fprintf(stderr, "\n");
    531   1.1       cgd }
    532   1.1       cgd #endif
    533