2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)mpool.c 8.7 (Berkeley) 11/2/95";
34 #endif /* LIBC_SCCS and not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "namespace.h"
39 #include <sys/param.h>
40 #include <sys/queue.h>
48 #include "un-namespace.h"
52 #define __MPOOLINTERFACE_PRIVATE
55 static BKT *mpool_bkt(MPOOL *);
56 static BKT *mpool_look(MPOOL *, pgno_t);
57 static int mpool_write(MPOOL *, BKT *);
61 * Initialize a memory pool.
65 mpool_open(void *key, int fd, pgno_t pagesize, pgno_t maxcache)
72 * Get information about the file.
75 * We don't currently handle pipes, although we should.
79 if (!S_ISREG(sb.st_mode)) {
84 /* Allocate and initialize the MPOOL cookie. */
85 if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL)
88 for (entry = 0; entry < HASHSIZE; ++entry)
89 TAILQ_INIT(&mp->hqh[entry]);
90 mp->maxcache = maxcache;
91 mp->npages = sb.st_size / pagesize;
92 mp->pagesize = pagesize;
99 * Initialize input/output filters.
102 mpool_filter(MPOOL *mp, void (*pgin) (void *, pgno_t, void *),
103 void (*pgout) (void *, pgno_t, void *), void *pgcookie)
107 mp->pgcookie = pgcookie;
112 * Get a new page of memory.
115 mpool_new(MPOOL *mp, pgno_t *pgnoaddr, u_int flags)
120 if (mp->npages == MAX_PAGE_NUMBER) {
121 (void)fprintf(stderr, "mpool_new: page allocation overflow.\n");
128 * Get a BKT from the cache. Assign a new page number, attach
129 * it to the head of the hash chain, the tail of the lru chain,
132 if ((bp = mpool_bkt(mp)) == NULL)
134 if (flags == MPOOL_PAGE_REQUEST) {
136 bp->pgno = *pgnoaddr;
138 bp->pgno = *pgnoaddr = mp->npages++;
140 bp->flags = MPOOL_PINNED | MPOOL_INUSE;
142 head = &mp->hqh[HASHKEY(bp->pgno)];
143 TAILQ_INSERT_HEAD(head, bp, hq);
144 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
149 mpool_delete(MPOOL *mp, void *page)
154 bp = (BKT *)((char *)page - sizeof(BKT));
157 if (!(bp->flags & MPOOL_PINNED)) {
158 (void)fprintf(stderr,
159 "mpool_delete: page %d not pinned\n", bp->pgno);
164 /* Remove from the hash and lru queues. */
165 head = &mp->hqh[HASHKEY(bp->pgno)];
166 TAILQ_REMOVE(head, bp, hq);
167 TAILQ_REMOVE(&mp->lqh, bp, q);
171 return (RET_SUCCESS);
180 mpool_get(MPOOL *mp, pgno_t pgno,
181 u_int flags) /* XXX not used? */
192 /* Check for a page that is cached. */
193 if ((bp = mpool_look(mp, pgno)) != NULL) {
195 if (!(flags & MPOOL_IGNOREPIN) && bp->flags & MPOOL_PINNED) {
196 (void)fprintf(stderr,
197 "mpool_get: page %d already pinned\n", bp->pgno);
202 * Move the page to the head of the hash chain and the tail
205 head = &mp->hqh[HASHKEY(bp->pgno)];
206 TAILQ_REMOVE(head, bp, hq);
207 TAILQ_INSERT_HEAD(head, bp, hq);
208 TAILQ_REMOVE(&mp->lqh, bp, q);
209 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
211 /* Return a pinned page. */
212 bp->flags |= MPOOL_PINNED;
216 /* Get a page from the cache. */
217 if ((bp = mpool_bkt(mp)) == NULL)
220 /* Read in the contents. */
221 off = mp->pagesize * pgno;
222 if ((nr = pread(mp->fd, bp->page, mp->pagesize, off)) != (ssize_t)mp->pagesize) {
225 /* errno is set for us by pread(). */
231 * A zero-length read means you need to create a
234 memset(bp->page, 0, mp->pagesize);
237 /* A partial read is definitely bad. */
248 /* Set the page number, pin the page. */
250 if (!(flags & MPOOL_IGNOREPIN))
251 bp->flags = MPOOL_PINNED;
252 bp->flags |= MPOOL_INUSE;
255 * Add the page to the head of the hash chain and the tail
258 head = &mp->hqh[HASHKEY(bp->pgno)];
259 TAILQ_INSERT_HEAD(head, bp, hq);
260 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
262 /* Run through the user's filter. */
263 if (mp->pgin != NULL)
264 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
275 mpool_put(MPOOL *mp, void *page, u_int flags)
282 bp = (BKT *)((char *)page - sizeof(BKT));
284 if (!(bp->flags & MPOOL_PINNED)) {
285 (void)fprintf(stderr,
286 "mpool_put: page %d not pinned\n", bp->pgno);
290 bp->flags &= ~MPOOL_PINNED;
291 if (flags & MPOOL_DIRTY)
292 bp->flags |= flags & MPOOL_DIRTY;
293 return (RET_SUCCESS);
298 * Close the buffer pool.
301 mpool_close(MPOOL *mp)
305 /* Free up any space allocated to the lru pages. */
306 while (!TAILQ_EMPTY(&mp->lqh)) {
307 bp = TAILQ_FIRST(&mp->lqh);
308 TAILQ_REMOVE(&mp->lqh, bp, q);
312 /* Free the MPOOL cookie. */
314 return (RET_SUCCESS);
319 * Sync the pool to disk.
322 mpool_sync(MPOOL *mp)
326 /* Walk the lru chain, flushing any dirty pages to disk. */
327 TAILQ_FOREACH(bp, &mp->lqh, q)
328 if (bp->flags & MPOOL_DIRTY &&
329 mpool_write(mp, bp) == RET_ERROR)
332 /* Sync the file descriptor. */
333 return (_fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
338 * Get a page from the cache (or create one).
346 /* If under the max cached, always create a new page. */
347 if (mp->curcache < mp->maxcache)
351 * If the cache is max'd out, walk the lru list for a buffer we
352 * can flush. If we find one, write it (if necessary) and take it
353 * off any lists. If we don't find anything we grow the cache anyway.
354 * The cache never shrinks.
356 TAILQ_FOREACH(bp, &mp->lqh, q)
357 if (!(bp->flags & MPOOL_PINNED)) {
358 /* Flush if dirty. */
359 if (bp->flags & MPOOL_DIRTY &&
360 mpool_write(mp, bp) == RET_ERROR)
365 /* Remove from the hash and lru queues. */
366 head = &mp->hqh[HASHKEY(bp->pgno)];
367 TAILQ_REMOVE(head, bp, hq);
368 TAILQ_REMOVE(&mp->lqh, bp, q);
372 memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
380 new: if ((bp = (BKT *)calloc(1, sizeof(BKT) + mp->pagesize)) == NULL)
385 bp->page = (char *)bp + sizeof(BKT);
393 * Write a page to disk.
396 mpool_write(MPOOL *mp, BKT *bp)
404 /* Run through the user's filter. */
406 (mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
408 off = mp->pagesize * bp->pgno;
409 if (pwrite(mp->fd, bp->page, mp->pagesize, off) != (ssize_t)mp->pagesize)
413 * Re-run through the input filter since this page may soon be
414 * accessed via the cache, and whatever the user's output filter
415 * did may screw things up if we don't let the input filter
416 * restore the in-core copy.
419 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
421 bp->flags &= ~MPOOL_DIRTY;
422 return (RET_SUCCESS);
427 * Lookup a page in the cache.
430 mpool_look(MPOOL *mp, pgno_t pgno)
435 head = &mp->hqh[HASHKEY(pgno)];
436 TAILQ_FOREACH(bp, head, hq)
437 if ((bp->pgno == pgno) &&
438 ((bp->flags & MPOOL_INUSE) == MPOOL_INUSE)) {
453 * Print out cache statistics.
456 mpool_stat(MPOOL *mp)
462 (void)fprintf(stderr, "%lu pages in the file\n", mp->npages);
463 (void)fprintf(stderr,
464 "page size %lu, cacheing %lu pages of %lu page max cache\n",
465 mp->pagesize, mp->curcache, mp->maxcache);
466 (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
467 mp->pageput, mp->pageget, mp->pagenew);
468 (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
469 mp->pagealloc, mp->pageflush);
470 if (mp->cachehit + mp->cachemiss)
471 (void)fprintf(stderr,
472 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
473 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
474 * 100, mp->cachehit, mp->cachemiss);
475 (void)fprintf(stderr, "%lu page reads, %lu page writes\n",
476 mp->pageread, mp->pagewrite);
480 TAILQ_FOREACH(bp, &mp->lqh, q) {
481 (void)fprintf(stderr, "%s%d", sep, bp->pgno);
482 if (bp->flags & MPOOL_DIRTY)
483 (void)fprintf(stderr, "d");
484 if (bp->flags & MPOOL_PINNED)
485 (void)fprintf(stderr, "P");
493 (void)fprintf(stderr, "\n");