michael@0: /*- michael@0: * Copyright (c) 1990, 1993, 1994 michael@0: * The Regents of the University of California. All rights reserved. michael@0: * michael@0: * This code is derived from software contributed to Berkeley by michael@0: * Margo Seltzer. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. ***REMOVED*** - see michael@0: * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change michael@0: * 4. Neither the name of the University nor the names of its contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: */ michael@0: michael@0: #if defined(LIBC_SCCS) && !defined(lint) michael@0: static char sccsid[] = "@(#)hash_buf.c 8.5 (Berkeley) 7/15/94"; michael@0: #endif /* LIBC_SCCS and not lint */ michael@0: michael@0: /* michael@0: * PACKAGE: hash michael@0: * michael@0: * DESCRIPTION: michael@0: * Contains buffer management michael@0: * michael@0: * ROUTINES: michael@0: * External michael@0: * __buf_init michael@0: * __get_buf michael@0: * __buf_free michael@0: * __reclaim_buf michael@0: * Internal michael@0: * newbuf michael@0: */ michael@0: #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh) michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef DEBUG michael@0: #include michael@0: #endif michael@0: michael@0: #include "mcom_db.h" michael@0: #include "hash.h" michael@0: #include "page.h" michael@0: /* #include "extern.h" */ michael@0: michael@0: static BUFHEAD *newbuf __P((HTAB *, uint32, BUFHEAD *)); michael@0: michael@0: /* Unlink B from its place in the lru */ michael@0: #define BUF_REMOVE(B) { \ michael@0: (B)->prev->next = (B)->next; \ michael@0: (B)->next->prev = (B)->prev; \ michael@0: } michael@0: michael@0: /* Insert B after P */ michael@0: #define BUF_INSERT(B, P) { \ michael@0: (B)->next = (P)->next; \ michael@0: (B)->prev = (P); \ michael@0: (P)->next = (B); \ michael@0: (B)->next->prev = (B); \ michael@0: } michael@0: michael@0: #define MRU hashp->bufhead.next michael@0: #define LRU hashp->bufhead.prev michael@0: michael@0: #define MRU_INSERT(B) BUF_INSERT((B), &hashp->bufhead) michael@0: #define LRU_INSERT(B) BUF_INSERT((B), LRU) michael@0: michael@0: /* michael@0: * We are looking for a buffer with address "addr". If prev_bp is NULL, then michael@0: * address is a bucket index. If prev_bp is not NULL, then it points to the michael@0: * page previous to an overflow page that we are trying to find. michael@0: * michael@0: * CAVEAT: The buffer header accessed via prev_bp's ovfl field may no longer michael@0: * be valid. Therefore, you must always verify that its address matches the michael@0: * address you are seeking. michael@0: */ michael@0: extern BUFHEAD * michael@0: __get_buf(HTAB *hashp, uint32 addr, BUFHEAD *prev_bp, int newpage) michael@0: /* If prev_bp set, indicates a new overflow page. */ michael@0: { michael@0: register BUFHEAD *bp; michael@0: register uint32 is_disk_mask; michael@0: register int is_disk, segment_ndx = 0; michael@0: SEGMENT segp = 0; michael@0: michael@0: is_disk = 0; michael@0: is_disk_mask = 0; michael@0: if (prev_bp) { michael@0: bp = prev_bp->ovfl; michael@0: if (!bp || (bp->addr != addr)) michael@0: bp = NULL; michael@0: if (!newpage) michael@0: is_disk = BUF_DISK; michael@0: } else { michael@0: /* Grab buffer out of directory */ michael@0: segment_ndx = addr & (hashp->SGSIZE - 1); michael@0: michael@0: /* valid segment ensured by __call_hash() */ michael@0: segp = hashp->dir[addr >> hashp->SSHIFT]; michael@0: #ifdef DEBUG michael@0: assert(segp != NULL); michael@0: #endif michael@0: michael@0: bp = PTROF(segp[segment_ndx]); michael@0: michael@0: is_disk_mask = ISDISK(segp[segment_ndx]); michael@0: is_disk = is_disk_mask || !hashp->new_file; michael@0: } michael@0: michael@0: if (!bp) { michael@0: bp = newbuf(hashp, addr, prev_bp); michael@0: if (!bp) michael@0: return(NULL); michael@0: if(__get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0)) michael@0: { michael@0: /* free bp and its page */ michael@0: if(prev_bp) michael@0: { michael@0: /* if prev_bp is set then the new page that michael@0: * failed is hooked onto prev_bp as an overflow page. michael@0: * if we don't remove the pointer to the bad page michael@0: * we may try and access it later and we will die michael@0: * horribly because it will have already been michael@0: * free'd and overwritten with bogus data. michael@0: */ michael@0: prev_bp->ovfl = NULL; michael@0: } michael@0: BUF_REMOVE(bp); michael@0: free(bp->page); michael@0: free(bp); michael@0: return (NULL); michael@0: } michael@0: michael@0: if (!prev_bp) michael@0: { michael@0: #if 0 michael@0: /* 16 bit windows and mac can't handle the michael@0: * oring of the is disk flag. michael@0: */ michael@0: segp[segment_ndx] = michael@0: (BUFHEAD *)((ptrdiff_t)bp | is_disk_mask); michael@0: #else michael@0: /* set the is_disk thing inside the structure michael@0: */ michael@0: bp->is_disk = is_disk_mask; michael@0: segp[segment_ndx] = bp; michael@0: #endif michael@0: } michael@0: } else { michael@0: BUF_REMOVE(bp); michael@0: MRU_INSERT(bp); michael@0: } michael@0: return (bp); michael@0: } michael@0: michael@0: /* michael@0: * We need a buffer for this page. Either allocate one, or evict a resident michael@0: * one (if we have as many buffers as we're allowed) and put this one in. michael@0: * michael@0: * If newbuf finds an error (returning NULL), it also sets errno. michael@0: */ michael@0: static BUFHEAD * michael@0: newbuf(HTAB *hashp, uint32 addr, BUFHEAD *prev_bp) michael@0: { michael@0: register BUFHEAD *bp; /* The buffer we're going to use */ michael@0: register BUFHEAD *xbp; /* Temp pointer */ michael@0: register BUFHEAD *next_xbp; michael@0: SEGMENT segp; michael@0: int segment_ndx; michael@0: uint16 oaddr, *shortp; michael@0: michael@0: oaddr = 0; michael@0: bp = LRU; michael@0: /* michael@0: * If LRU buffer is pinned, the buffer pool is too small. We need to michael@0: * allocate more buffers. michael@0: */ michael@0: if (hashp->nbufs || (bp->flags & BUF_PIN)) { michael@0: /* Allocate a new one */ michael@0: if ((bp = (BUFHEAD *)malloc(sizeof(BUFHEAD))) == NULL) michael@0: return (NULL); michael@0: michael@0: /* this memset is supposedly unnecessary but lets add michael@0: * it anyways. michael@0: */ michael@0: memset(bp, 0xff, sizeof(BUFHEAD)); michael@0: michael@0: if ((bp->page = (char *)malloc((size_t)hashp->BSIZE)) == NULL) { michael@0: free(bp); michael@0: return (NULL); michael@0: } michael@0: michael@0: /* this memset is supposedly unnecessary but lets add michael@0: * it anyways. michael@0: */ michael@0: memset(bp->page, 0xff, (size_t)hashp->BSIZE); michael@0: michael@0: if (hashp->nbufs) michael@0: hashp->nbufs--; michael@0: } else { michael@0: /* Kick someone out */ michael@0: BUF_REMOVE(bp); michael@0: /* michael@0: * If this is an overflow page with addr 0, it's already been michael@0: * flushed back in an overflow chain and initialized. michael@0: */ michael@0: if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) { michael@0: /* michael@0: * Set oaddr before __put_page so that you get it michael@0: * before bytes are swapped. michael@0: */ michael@0: shortp = (uint16 *)bp->page; michael@0: if (shortp[0]) michael@0: { michael@0: if(shortp[0] > (hashp->BSIZE / sizeof(uint16))) michael@0: { michael@0: return(NULL); michael@0: } michael@0: oaddr = shortp[shortp[0] - 1]; michael@0: } michael@0: if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page, michael@0: bp->addr, (int)IS_BUCKET(bp->flags), 0)) michael@0: return (NULL); michael@0: /* michael@0: * Update the pointer to this page (i.e. invalidate it). michael@0: * michael@0: * If this is a new file (i.e. we created it at open michael@0: * time), make sure that we mark pages which have been michael@0: * written to disk so we retrieve them from disk later, michael@0: * rather than allocating new pages. michael@0: */ michael@0: if (IS_BUCKET(bp->flags)) { michael@0: segment_ndx = bp->addr & (hashp->SGSIZE - 1); michael@0: segp = hashp->dir[bp->addr >> hashp->SSHIFT]; michael@0: #ifdef DEBUG michael@0: assert(segp != NULL); michael@0: #endif michael@0: michael@0: if (hashp->new_file && michael@0: ((bp->flags & BUF_MOD) || michael@0: ISDISK(segp[segment_ndx]))) michael@0: segp[segment_ndx] = (BUFHEAD *)BUF_DISK; michael@0: else michael@0: segp[segment_ndx] = NULL; michael@0: } michael@0: /* michael@0: * Since overflow pages can only be access by means of michael@0: * their bucket, free overflow pages associated with michael@0: * this bucket. michael@0: */ michael@0: for (xbp = bp; xbp->ovfl;) { michael@0: next_xbp = xbp->ovfl; michael@0: xbp->ovfl = 0; michael@0: xbp = next_xbp; michael@0: michael@0: /* leave pinned pages alone, we are still using michael@0: * them. */ michael@0: if (xbp->flags & BUF_PIN) { michael@0: continue; michael@0: } michael@0: michael@0: /* Check that ovfl pointer is up date. */ michael@0: if (IS_BUCKET(xbp->flags) || michael@0: (oaddr != xbp->addr)) michael@0: break; michael@0: michael@0: shortp = (uint16 *)xbp->page; michael@0: if (shortp[0]) michael@0: { michael@0: /* LJM is the number of reported michael@0: * pages way too much? michael@0: */ michael@0: if(shortp[0] > hashp->BSIZE/sizeof(uint16)) michael@0: return NULL; michael@0: /* set before __put_page */ michael@0: oaddr = shortp[shortp[0] - 1]; michael@0: } michael@0: if ((xbp->flags & BUF_MOD) && __put_page(hashp, michael@0: xbp->page, xbp->addr, 0, 0)) michael@0: return (NULL); michael@0: xbp->addr = 0; michael@0: xbp->flags = 0; michael@0: BUF_REMOVE(xbp); michael@0: LRU_INSERT(xbp); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Now assign this buffer */ michael@0: bp->addr = addr; michael@0: #ifdef DEBUG1 michael@0: (void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n", michael@0: bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0); michael@0: #endif michael@0: bp->ovfl = NULL; michael@0: if (prev_bp) { michael@0: /* michael@0: * If prev_bp is set, this is an overflow page, hook it in to michael@0: * the buffer overflow links. michael@0: */ michael@0: #ifdef DEBUG1 michael@0: (void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n", michael@0: prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0), michael@0: (bp ? bp->addr : 0)); michael@0: #endif michael@0: prev_bp->ovfl = bp; michael@0: bp->flags = 0; michael@0: } else michael@0: bp->flags = BUF_BUCKET; michael@0: MRU_INSERT(bp); michael@0: return (bp); michael@0: } michael@0: michael@0: extern void __buf_init(HTAB *hashp, int32 nbytes) michael@0: { michael@0: BUFHEAD *bfp; michael@0: int npages; michael@0: michael@0: bfp = &(hashp->bufhead); michael@0: npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT; michael@0: npages = PR_MAX(npages, MIN_BUFFERS); michael@0: michael@0: hashp->nbufs = npages; michael@0: bfp->next = bfp; michael@0: bfp->prev = bfp; michael@0: /* michael@0: * This space is calloc'd so these are already null. michael@0: * michael@0: * bfp->ovfl = NULL; michael@0: * bfp->flags = 0; michael@0: * bfp->page = NULL; michael@0: * bfp->addr = 0; michael@0: */ michael@0: } michael@0: michael@0: extern int michael@0: __buf_free(HTAB *hashp, int do_free, int to_disk) michael@0: { michael@0: BUFHEAD *bp; michael@0: int status = -1; michael@0: michael@0: /* Need to make sure that buffer manager has been initialized */ michael@0: if (!LRU) michael@0: return (0); michael@0: for (bp = LRU; bp != &hashp->bufhead;) { michael@0: /* Check that the buffer is valid */ michael@0: if (bp->addr || IS_BUCKET(bp->flags)) { michael@0: if (to_disk && (bp->flags & BUF_MOD) && michael@0: (status = __put_page(hashp, bp->page, michael@0: bp->addr, IS_BUCKET(bp->flags), 0))) { michael@0: michael@0: if (do_free) { michael@0: if (bp->page) michael@0: free(bp->page); michael@0: BUF_REMOVE(bp); michael@0: free(bp); michael@0: } michael@0: michael@0: return (status); michael@0: } michael@0: } michael@0: /* Check if we are freeing stuff */ michael@0: if (do_free) { michael@0: if (bp->page) michael@0: free(bp->page); michael@0: BUF_REMOVE(bp); michael@0: free(bp); michael@0: bp = LRU; michael@0: } else michael@0: bp = bp->prev; michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: extern void michael@0: __reclaim_buf(HTAB *hashp, BUFHEAD *bp) michael@0: { michael@0: bp->ovfl = 0; michael@0: bp->addr = 0; michael@0: bp->flags = 0; michael@0: BUF_REMOVE(bp); michael@0: LRU_INSERT(bp); michael@0: }