2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)hash_buf.c 8.5 (Berkeley) 7/15/94
33 * $FreeBSD: head/lib/libc/db/hash/hash_buf.c 190493 2009-03-28 06:40:48Z delphij $
40 * Contains buffer management
52 #include <sys/param.h>
68 static BUFHEAD *newbuf(HTAB *, uint32_t, BUFHEAD *);
70 /* Unlink B from its place in the lru */
71 #define BUF_REMOVE(B) { \
72 (B)->prev->next = (B)->next; \
73 (B)->next->prev = (B)->prev; \
76 /* Insert B after P */
77 #define BUF_INSERT(B, P) { \
78 (B)->next = (P)->next; \
81 (B)->next->prev = (B); \
84 #define MRU hashp->bufhead.next
85 #define LRU hashp->bufhead.prev
87 #define MRU_INSERT(B) BUF_INSERT((B), &hashp->bufhead)
88 #define LRU_INSERT(B) BUF_INSERT((B), LRU)
91 * We are looking for a buffer with address "addr". If prev_bp is NULL, then
92 * address is a bucket index. If prev_bp is not NULL, then it points to the
93 * page previous to an overflow page that we are trying to find.
95 * CAVEAT: The buffer header accessed via prev_bp's ovfl field may no longer
96 * be valid. Therefore, you must always verify that its address matches the
97 * address you are seeking.
100 __get_buf(HTAB *hashp, uint32_t addr,
101 BUFHEAD *prev_bp, /* If prev_bp set, indicates a new overflow page. */
105 uint32_t is_disk_mask;
106 int is_disk, segment_ndx;
113 if (!bp || (bp->addr != addr))
118 /* Grab buffer out of directory */
119 segment_ndx = addr & (hashp->SGSIZE - 1);
121 /* valid segment ensured by __call_hash() */
122 segp = hashp->dir[addr >> hashp->SSHIFT];
124 assert(segp != NULL);
126 bp = PTROF(segp[segment_ndx]);
127 is_disk_mask = ISDISK(segp[segment_ndx]);
128 is_disk = is_disk_mask || !hashp->new_file;
132 bp = newbuf(hashp, addr, prev_bp);
134 __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
138 (BUFHEAD *)((ptrdiff_t)bp | is_disk_mask);
147 * We need a buffer for this page. Either allocate one, or evict a resident
148 * one (if we have as many buffers as we're allowed) and put this one in.
150 * If newbuf finds an error (returning NULL), it also sets errno.
153 newbuf(HTAB *hashp, uint32_t addr, BUFHEAD *prev_bp)
155 BUFHEAD *bp; /* The buffer we're going to use */
156 BUFHEAD *xbp; /* Temp pointer */
160 uint16_t oaddr, *shortp;
165 /* It is bad to overwrite the page under the cursor. */
166 if (bp == hashp->cpage) {
172 /* If prev_bp is part of bp overflow, create a new buffer. */
173 if (hashp->nbufs == 0 && prev_bp && bp->ovfl) {
176 for (ovfl = bp->ovfl; ovfl ; ovfl = ovfl->ovfl) {
177 if (ovfl == prev_bp) {
185 * If LRU buffer is pinned, the buffer pool is too small. We need to
186 * allocate more buffers.
188 if (hashp->nbufs || (bp->flags & BUF_PIN) || bp == hashp->cpage) {
189 /* Allocate a new one */
190 if ((bp = (BUFHEAD *)calloc(1, sizeof(BUFHEAD))) == NULL)
192 if ((bp->page = (char *)calloc(1, hashp->BSIZE)) == NULL) {
199 /* Kick someone out */
202 * If this is an overflow page with addr 0, it's already been
203 * flushed back in an overflow chain and initialized.
205 if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
207 * Set oaddr before __put_page so that you get it
208 * before bytes are swapped.
210 shortp = (uint16_t *)bp->page;
212 oaddr = shortp[shortp[0] - 1];
213 if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
214 bp->addr, (int)IS_BUCKET(bp->flags), 0))
217 * Update the pointer to this page (i.e. invalidate it).
219 * If this is a new file (i.e. we created it at open
220 * time), make sure that we mark pages which have been
221 * written to disk so we retrieve them from disk later,
222 * rather than allocating new pages.
224 if (IS_BUCKET(bp->flags)) {
225 segment_ndx = bp->addr & (hashp->SGSIZE - 1);
226 segp = hashp->dir[bp->addr >> hashp->SSHIFT];
228 assert(segp != NULL);
231 if (hashp->new_file &&
232 ((bp->flags & BUF_MOD) ||
233 ISDISK(segp[segment_ndx])))
234 segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
236 segp[segment_ndx] = NULL;
239 * Since overflow pages can only be access by means of
240 * their bucket, free overflow pages associated with
243 for (xbp = bp; xbp->ovfl;) {
244 next_xbp = xbp->ovfl;
248 /* Check that ovfl pointer is up date. */
249 if (IS_BUCKET(xbp->flags) ||
250 (oaddr != xbp->addr))
253 shortp = (uint16_t *)xbp->page;
255 /* set before __put_page */
256 oaddr = shortp[shortp[0] - 1];
257 if ((xbp->flags & BUF_MOD) && __put_page(hashp,
258 xbp->page, xbp->addr, 0, 0))
268 /* Now assign this buffer */
271 fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
272 bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
277 * If prev_bp is set, this is an overflow page, hook it in to
278 * the buffer overflow links.
281 fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
282 prev_bp->addr, (prev_bp->ovfl ? prev_bp->ovfl->addr : 0),
283 (bp ? bp->addr : 0));
288 bp->flags = BUF_BUCKET;
294 __buf_init(HTAB *hashp, int nbytes)
299 bfp = &(hashp->bufhead);
300 npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
301 npages = MAX(npages, MIN_BUFFERS);
303 hashp->nbufs = npages;
307 * This space is calloc'd so these are already null.
317 __buf_free(HTAB *hashp, int do_free, int to_disk)
321 /* Need to make sure that buffer manager has been initialized */
324 for (bp = LRU; bp != &hashp->bufhead;) {
325 /* Check that the buffer is valid */
326 if (bp->addr || IS_BUCKET(bp->flags)) {
327 if (to_disk && (bp->flags & BUF_MOD) &&
328 __put_page(hashp, bp->page,
329 bp->addr, IS_BUCKET(bp->flags), 0))
332 /* Check if we are freeing stuff */
335 memset(bp->page, 0, hashp->BSIZE);
348 __reclaim_buf(HTAB *hashp, BUFHEAD *bp)