1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include "namespace.h"
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/stat.h>
36
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include "un-namespace.h"
43
44 #include <db.h>
45
46 #define __MPOOLINTERFACE_PRIVATE
47 #include <mpool.h>
48
49 static BKT *mpool_bkt(MPOOL *);
50 static BKT *mpool_look(MPOOL *, pgno_t);
51 static int mpool_write(MPOOL *, BKT *);
52
53 /*
54 * mpool_open --
55 * Initialize a memory pool.
56 */
57 /* ARGSUSED */
58 MPOOL *
mpool_open(void * key,int fd,pgno_t pagesize,pgno_t maxcache)59 mpool_open(void *key, int fd, pgno_t pagesize, pgno_t maxcache)
60 {
61 struct stat sb;
62 MPOOL *mp;
63 int entry;
64
65 /*
66 * Get information about the file.
67 *
68 * XXX
69 * We don't currently handle pipes, although we should.
70 */
71 if (_fstat(fd, &sb))
72 return (NULL);
73 if (!S_ISREG(sb.st_mode)) {
74 errno = ESPIPE;
75 return (NULL);
76 }
77
78 /* Allocate and initialize the MPOOL cookie. */
79 if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL)
80 return (NULL);
81 TAILQ_INIT(&mp->lqh);
82 for (entry = 0; entry < HASHSIZE; ++entry)
83 TAILQ_INIT(&mp->hqh[entry]);
84 mp->maxcache = maxcache;
85 mp->npages = sb.st_size / pagesize;
86 mp->pagesize = pagesize;
87 mp->fd = fd;
88 return (mp);
89 }
90
91 /*
92 * mpool_filter --
93 * Initialize input/output filters.
94 */
95 void
mpool_filter(MPOOL * mp,void (* pgin)(void *,pgno_t,void *),void (* pgout)(void *,pgno_t,void *),void * pgcookie)96 mpool_filter(MPOOL *mp, void (*pgin) (void *, pgno_t, void *),
97 void (*pgout) (void *, pgno_t, void *), void *pgcookie)
98 {
99 mp->pgin = pgin;
100 mp->pgout = pgout;
101 mp->pgcookie = pgcookie;
102 }
103
104 /*
105 * mpool_new --
106 * Get a new page of memory.
107 */
108 void *
mpool_new(MPOOL * mp,pgno_t * pgnoaddr,u_int flags)109 mpool_new(MPOOL *mp, pgno_t *pgnoaddr, u_int flags)
110 {
111 struct _hqh *head;
112 BKT *bp;
113
114 if (mp->npages == MAX_PAGE_NUMBER) {
115 (void)fprintf(stderr, "mpool_new: page allocation overflow.\n");
116 abort();
117 }
118 #ifdef STATISTICS
119 ++mp->pagenew;
120 #endif
121 /*
122 * Get a BKT from the cache. Assign a new page number, attach
123 * it to the head of the hash chain, the tail of the lru chain,
124 * and return.
125 */
126 if ((bp = mpool_bkt(mp)) == NULL)
127 return (NULL);
128 if (flags == MPOOL_PAGE_REQUEST) {
129 mp->npages++;
130 bp->pgno = *pgnoaddr;
131 } else
132 bp->pgno = *pgnoaddr = mp->npages++;
133
134 bp->flags = MPOOL_PINNED | MPOOL_INUSE;
135
136 head = &mp->hqh[HASHKEY(bp->pgno)];
137 TAILQ_INSERT_HEAD(head, bp, hq);
138 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
139 return (bp->page);
140 }
141
142 int
mpool_delete(MPOOL * mp,void * page)143 mpool_delete(MPOOL *mp, void *page)
144 {
145 struct _hqh *head;
146 BKT *bp;
147
148 bp = (BKT *)((char *)page - sizeof(BKT));
149
150 #ifdef DEBUG
151 if (!(bp->flags & MPOOL_PINNED)) {
152 (void)fprintf(stderr,
153 "mpool_delete: page %d not pinned\n", bp->pgno);
154 abort();
155 }
156 #endif
157
158 /* Remove from the hash and lru queues. */
159 head = &mp->hqh[HASHKEY(bp->pgno)];
160 TAILQ_REMOVE(head, bp, hq);
161 TAILQ_REMOVE(&mp->lqh, bp, q);
162
163 free(bp);
164 mp->curcache--;
165 return (RET_SUCCESS);
166 }
167
168 /*
169 * mpool_get
170 * Get a page.
171 */
172 /* ARGSUSED */
173 void *
mpool_get(MPOOL * mp,pgno_t pgno,u_int flags)174 mpool_get(MPOOL *mp, pgno_t pgno,
175 u_int flags) /* XXX not used? */
176 {
177 struct _hqh *head;
178 BKT *bp;
179 off_t off;
180 int nr;
181
182 #ifdef STATISTICS
183 ++mp->pageget;
184 #endif
185
186 /* Check for a page that is cached. */
187 if ((bp = mpool_look(mp, pgno)) != NULL) {
188 #ifdef DEBUG
189 if (!(flags & MPOOL_IGNOREPIN) && bp->flags & MPOOL_PINNED) {
190 (void)fprintf(stderr,
191 "mpool_get: page %d already pinned\n", bp->pgno);
192 abort();
193 }
194 #endif
195 /*
196 * Move the page to the head of the hash chain and the tail
197 * of the lru chain.
198 */
199 head = &mp->hqh[HASHKEY(bp->pgno)];
200 TAILQ_REMOVE(head, bp, hq);
201 TAILQ_INSERT_HEAD(head, bp, hq);
202 TAILQ_REMOVE(&mp->lqh, bp, q);
203 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
204
205 /* Return a pinned page. */
206 bp->flags |= MPOOL_PINNED;
207 return (bp->page);
208 }
209
210 /* Get a page from the cache. */
211 if ((bp = mpool_bkt(mp)) == NULL)
212 return (NULL);
213
214 /* Read in the contents. */
215 off = mp->pagesize * pgno;
216 if ((nr = pread(mp->fd, bp->page, mp->pagesize, off)) != (ssize_t)mp->pagesize) {
217 int serrno;
218
219 switch (nr) {
220 case -1:
221 /* errno is set for us by pread(). */
222 serrno = errno;
223 free(bp);
224 errno = serrno;
225 mp->curcache--;
226 return (NULL);
227 case 0:
228 /*
229 * A zero-length read means you need to create a
230 * new page.
231 */
232 memset(bp->page, 0, mp->pagesize);
233 break;
234 default:
235 /* A partial read is definitely bad. */
236 free(bp);
237 mp->curcache--;
238 errno = EINVAL;
239 return (NULL);
240 }
241 }
242 #ifdef STATISTICS
243 ++mp->pageread;
244 #endif
245
246 /* Set the page number, pin the page. */
247 bp->pgno = pgno;
248 if (!(flags & MPOOL_IGNOREPIN))
249 bp->flags = MPOOL_PINNED;
250 bp->flags |= MPOOL_INUSE;
251
252 /*
253 * Add the page to the head of the hash chain and the tail
254 * of the lru chain.
255 */
256 head = &mp->hqh[HASHKEY(bp->pgno)];
257 TAILQ_INSERT_HEAD(head, bp, hq);
258 TAILQ_INSERT_TAIL(&mp->lqh, bp, q);
259
260 /* Run through the user's filter. */
261 if (mp->pgin != NULL)
262 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
263
264 return (bp->page);
265 }
266
267 /*
268 * mpool_put
269 * Return a page.
270 */
271 /* ARGSUSED */
272 int
mpool_put(MPOOL * mp,void * page,u_int flags)273 mpool_put(MPOOL *mp, void *page, u_int flags)
274 {
275 BKT *bp;
276
277 #ifdef STATISTICS
278 ++mp->pageput;
279 #endif
280 bp = (BKT *)((char *)page - sizeof(BKT));
281 #ifdef DEBUG
282 if (!(bp->flags & MPOOL_PINNED)) {
283 (void)fprintf(stderr,
284 "mpool_put: page %d not pinned\n", bp->pgno);
285 abort();
286 }
287 #endif
288 bp->flags &= ~MPOOL_PINNED;
289 if (flags & MPOOL_DIRTY)
290 bp->flags |= flags & MPOOL_DIRTY;
291 return (RET_SUCCESS);
292 }
293
294 /*
295 * mpool_close
296 * Close the buffer pool.
297 */
298 int
mpool_close(MPOOL * mp)299 mpool_close(MPOOL *mp)
300 {
301 BKT *bp;
302
303 /* Free up any space allocated to the lru pages. */
304 while (!TAILQ_EMPTY(&mp->lqh)) {
305 bp = TAILQ_FIRST(&mp->lqh);
306 TAILQ_REMOVE(&mp->lqh, bp, q);
307 free(bp);
308 }
309
310 /* Free the MPOOL cookie. */
311 free(mp);
312 return (RET_SUCCESS);
313 }
314
315 /*
316 * mpool_sync
317 * Sync the pool to disk.
318 */
319 int
mpool_sync(MPOOL * mp)320 mpool_sync(MPOOL *mp)
321 {
322 BKT *bp;
323
324 /* Walk the lru chain, flushing any dirty pages to disk. */
325 TAILQ_FOREACH(bp, &mp->lqh, q)
326 if (bp->flags & MPOOL_DIRTY &&
327 mpool_write(mp, bp) == RET_ERROR)
328 return (RET_ERROR);
329
330 /* Sync the file descriptor. */
331 return (_fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
332 }
333
334 /*
335 * mpool_bkt
336 * Get a page from the cache (or create one).
337 */
338 static BKT *
mpool_bkt(MPOOL * mp)339 mpool_bkt(MPOOL *mp)
340 {
341 struct _hqh *head;
342 BKT *bp;
343
344 /* If under the max cached, always create a new page. */
345 if (mp->curcache < mp->maxcache)
346 goto new;
347
348 /*
349 * If the cache is max'd out, walk the lru list for a buffer we
350 * can flush. If we find one, write it (if necessary) and take it
351 * off any lists. If we don't find anything we grow the cache anyway.
352 * The cache never shrinks.
353 */
354 TAILQ_FOREACH(bp, &mp->lqh, q)
355 if (!(bp->flags & MPOOL_PINNED)) {
356 /* Flush if dirty. */
357 if (bp->flags & MPOOL_DIRTY &&
358 mpool_write(mp, bp) == RET_ERROR)
359 return (NULL);
360 #ifdef STATISTICS
361 ++mp->pageflush;
362 #endif
363 /* Remove from the hash and lru queues. */
364 head = &mp->hqh[HASHKEY(bp->pgno)];
365 TAILQ_REMOVE(head, bp, hq);
366 TAILQ_REMOVE(&mp->lqh, bp, q);
367 #ifdef DEBUG
368 { void *spage;
369 spage = bp->page;
370 memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
371 bp->page = spage;
372 }
373 #endif
374 bp->flags = 0;
375 return (bp);
376 }
377
378 new: if ((bp = (BKT *)calloc(1, sizeof(BKT) + mp->pagesize)) == NULL)
379 return (NULL);
380 #ifdef STATISTICS
381 ++mp->pagealloc;
382 #endif
383 bp->page = (char *)bp + sizeof(BKT);
384 bp->flags = 0;
385 ++mp->curcache;
386 return (bp);
387 }
388
389 /*
390 * mpool_write
391 * Write a page to disk.
392 */
393 static int
mpool_write(MPOOL * mp,BKT * bp)394 mpool_write(MPOOL *mp, BKT *bp)
395 {
396 off_t off;
397
398 #ifdef STATISTICS
399 ++mp->pagewrite;
400 #endif
401
402 /* Run through the user's filter. */
403 if (mp->pgout)
404 (mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
405
406 off = mp->pagesize * bp->pgno;
407 if (pwrite(mp->fd, bp->page, mp->pagesize, off) != (ssize_t)mp->pagesize)
408 return (RET_ERROR);
409
410 /*
411 * Re-run through the input filter since this page may soon be
412 * accessed via the cache, and whatever the user's output filter
413 * did may screw things up if we don't let the input filter
414 * restore the in-core copy.
415 */
416 if (mp->pgin)
417 (mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
418
419 bp->flags &= ~MPOOL_DIRTY;
420 return (RET_SUCCESS);
421 }
422
423 /*
424 * mpool_look
425 * Lookup a page in the cache.
426 */
427 static BKT *
mpool_look(MPOOL * mp,pgno_t pgno)428 mpool_look(MPOOL *mp, pgno_t pgno)
429 {
430 struct _hqh *head;
431 BKT *bp;
432
433 head = &mp->hqh[HASHKEY(pgno)];
434 TAILQ_FOREACH(bp, head, hq)
435 if ((bp->pgno == pgno) &&
436 ((bp->flags & MPOOL_INUSE) == MPOOL_INUSE)) {
437 #ifdef STATISTICS
438 ++mp->cachehit;
439 #endif
440 return (bp);
441 }
442 #ifdef STATISTICS
443 ++mp->cachemiss;
444 #endif
445 return (NULL);
446 }
447
448 #ifdef STATISTICS
449 /*
450 * mpool_stat
451 * Print out cache statistics.
452 */
453 void
mpool_stat(MPOOL * mp)454 mpool_stat(MPOOL *mp)
455 {
456 BKT *bp;
457 int cnt;
458 char *sep;
459
460 (void)fprintf(stderr, "%lu pages in the file\n", mp->npages);
461 (void)fprintf(stderr,
462 "page size %lu, caching %lu pages of %lu page max cache\n",
463 mp->pagesize, mp->curcache, mp->maxcache);
464 (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
465 mp->pageput, mp->pageget, mp->pagenew);
466 (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
467 mp->pagealloc, mp->pageflush);
468 if (mp->cachehit + mp->cachemiss)
469 (void)fprintf(stderr,
470 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
471 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
472 * 100, mp->cachehit, mp->cachemiss);
473 (void)fprintf(stderr, "%lu page reads, %lu page writes\n",
474 mp->pageread, mp->pagewrite);
475
476 sep = "";
477 cnt = 0;
478 TAILQ_FOREACH(bp, &mp->lqh, q) {
479 (void)fprintf(stderr, "%s%d", sep, bp->pgno);
480 if (bp->flags & MPOOL_DIRTY)
481 (void)fprintf(stderr, "d");
482 if (bp->flags & MPOOL_PINNED)
483 (void)fprintf(stderr, "P");
484 if (++cnt == 10) {
485 sep = "\n";
486 cnt = 0;
487 } else
488 sep = ", ";
489
490 }
491 (void)fprintf(stderr, "\n");
492 }
493 #endif
494