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