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