Home | History | Annotate | Line # | Download | only in hash
hash_buf.c revision 1.7
      1 /*	$NetBSD: hash_buf.c,v 1.7 1997/07/13 18:52:04 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Margo Seltzer.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 #if 0
     42 static char sccsid[] = "@(#)hash_buf.c	8.5 (Berkeley) 7/15/94";
     43 #else
     44 __RCSID("$NetBSD: hash_buf.c,v 1.7 1997/07/13 18:52:04 christos Exp $");
     45 #endif
     46 #endif /* LIBC_SCCS and not lint */
     47 
     48 /*
     49  * PACKAGE: hash
     50  *
     51  * DESCRIPTION:
     52  *	Contains buffer management
     53  *
     54  * ROUTINES:
     55  * External
     56  *	__buf_init
     57  *	__get_buf
     58  *	__buf_free
     59  *	__reclaim_buf
     60  * Internal
     61  *	newbuf
     62  */
     63 
     64 #include <sys/param.h>
     65 
     66 #include <errno.h>
     67 #include <stddef.h>
     68 #include <stdio.h>
     69 #include <stdlib.h>
     70 
     71 #ifdef DEBUG
     72 #include <assert.h>
     73 #endif
     74 
     75 #include <db.h>
     76 #include "hash.h"
     77 #include "page.h"
     78 #include "extern.h"
     79 
     80 static BUFHEAD *newbuf __P((HTAB *, u_int32_t, BUFHEAD *));
     81 
     82 /* Unlink B from its place in the lru */
     83 #define BUF_REMOVE(B) { \
     84 	(B)->prev->next = (B)->next; \
     85 	(B)->next->prev = (B)->prev; \
     86 }
     87 
     88 /* Insert B after P */
     89 #define BUF_INSERT(B, P) { \
     90 	(B)->next = (P)->next; \
     91 	(B)->prev = (P); \
     92 	(P)->next = (B); \
     93 	(B)->next->prev = (B); \
     94 }
     95 
     96 #define	MRU	hashp->bufhead.next
     97 #define	LRU	hashp->bufhead.prev
     98 
     99 #define MRU_INSERT(B)	BUF_INSERT((B), &hashp->bufhead)
    100 #define LRU_INSERT(B)	BUF_INSERT((B), LRU)
    101 
    102 /*
    103  * We are looking for a buffer with address "addr".  If prev_bp is NULL, then
    104  * address is a bucket index.  If prev_bp is not NULL, then it points to the
    105  * page previous to an overflow page that we are trying to find.
    106  *
    107  * CAVEAT:  The buffer header accessed via prev_bp's ovfl field may no longer
    108  * be valid.  Therefore, you must always verify that its address matches the
    109  * address you are seeking.
    110  */
    111 extern BUFHEAD *
    112 __get_buf(hashp, addr, prev_bp, newpage)
    113 	HTAB *hashp;
    114 	u_int32_t addr;
    115 	BUFHEAD *prev_bp;
    116 	int newpage;	/* If prev_bp set, indicates a new overflow page. */
    117 {
    118 	register BUFHEAD *bp;
    119 	register u_int32_t is_disk_mask;
    120 	register int is_disk, segment_ndx = 0;	/* pacify gcc */
    121 	SEGMENT segp = NULL;	/* pacify gcc */
    122 
    123 	is_disk = 0;
    124 	is_disk_mask = 0;
    125 	if (prev_bp) {
    126 		bp = prev_bp->ovfl;
    127 		if (!bp || (bp->addr != addr))
    128 			bp = NULL;
    129 		if (!newpage)
    130 			is_disk = BUF_DISK;
    131 	} else {
    132 		/* Grab buffer out of directory */
    133 		segment_ndx = addr & (hashp->SGSIZE - 1);
    134 
    135 		/* valid segment ensured by __call_hash() */
    136 		segp = hashp->dir[addr >> hashp->SSHIFT];
    137 #ifdef DEBUG
    138 		assert(segp != NULL);
    139 #endif
    140 		bp = PTROF(segp[segment_ndx]);
    141 		is_disk_mask = ISDISK(segp[segment_ndx]);
    142 		is_disk = is_disk_mask || !hashp->new_file;
    143 	}
    144 
    145 	if (!bp) {
    146 		bp = newbuf(hashp, addr, prev_bp);
    147 		if (!bp ||
    148 		    __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
    149 			return (NULL);
    150 		if (!prev_bp)
    151 			segp[segment_ndx] =
    152 			    (BUFHEAD *)((ptrdiff_t)bp | is_disk_mask);
    153 	} else {
    154 		BUF_REMOVE(bp);
    155 		MRU_INSERT(bp);
    156 	}
    157 	return (bp);
    158 }
    159 
    160 /*
    161  * We need a buffer for this page. Either allocate one, or evict a resident
    162  * one (if we have as many buffers as we're allowed) and put this one in.
    163  *
    164  * If newbuf finds an error (returning NULL), it also sets errno.
    165  */
    166 static BUFHEAD *
    167 newbuf(hashp, addr, prev_bp)
    168 	HTAB *hashp;
    169 	u_int32_t addr;
    170 	BUFHEAD *prev_bp;
    171 {
    172 	register BUFHEAD *bp;		/* The buffer we're going to use */
    173 	register BUFHEAD *xbp;		/* Temp pointer */
    174 	register BUFHEAD *next_xbp;
    175 	SEGMENT segp;
    176 	int segment_ndx;
    177 	u_int16_t oaddr, *shortp;
    178 
    179 	oaddr = 0;
    180 	bp = LRU;
    181 	/*
    182 	 * If LRU buffer is pinned, the buffer pool is too small. We need to
    183 	 * allocate more buffers.
    184 	 */
    185 	if (hashp->nbufs || (bp->flags & BUF_PIN)) {
    186 		/* Allocate a new one */
    187 		if ((bp = (BUFHEAD *)malloc(sizeof(BUFHEAD))) == NULL)
    188 			return (NULL);
    189 #ifdef PURIFY
    190 		memset(bp, 0xff, sizeof(BUFHEAD));
    191 #endif
    192 		if ((bp->page = (char *)malloc(hashp->BSIZE)) == NULL) {
    193 			free(bp);
    194 			return (NULL);
    195 		}
    196 #ifdef PURIFY
    197 		memset(bp->page, 0xff, hashp->BSIZE);
    198 #endif
    199 		if (hashp->nbufs)
    200 			hashp->nbufs--;
    201 	} else {
    202 		/* Kick someone out */
    203 		BUF_REMOVE(bp);
    204 		/*
    205 		 * If this is an overflow page with addr 0, it's already been
    206 		 * flushed back in an overflow chain and initialized.
    207 		 */
    208 		if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
    209 			/*
    210 			 * Set oaddr before __put_page so that you get it
    211 			 * before bytes are swapped.
    212 			 */
    213 			shortp = (u_int16_t *)bp->page;
    214 			if (shortp[0])
    215 				oaddr = shortp[shortp[0] - 1];
    216 			if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
    217 			    bp->addr, (int)IS_BUCKET(bp->flags), 0))
    218 				return (NULL);
    219 			/*
    220 			 * Update the pointer to this page (i.e. invalidate it).
    221 			 *
    222 			 * If this is a new file (i.e. we created it at open
    223 			 * time), make sure that we mark pages which have been
    224 			 * written to disk so we retrieve them from disk later,
    225 			 * rather than allocating new pages.
    226 			 */
    227 			if (IS_BUCKET(bp->flags)) {
    228 				segment_ndx = bp->addr & (hashp->SGSIZE - 1);
    229 				segp = hashp->dir[bp->addr >> hashp->SSHIFT];
    230 #ifdef DEBUG
    231 				assert(segp != NULL);
    232 #endif
    233 
    234 				if (hashp->new_file &&
    235 				    ((bp->flags & BUF_MOD) ||
    236 				    ISDISK(segp[segment_ndx])))
    237 					segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
    238 				else
    239 					segp[segment_ndx] = NULL;
    240 			}
    241 			/*
    242 			 * Since overflow pages can only be access by means of
    243 			 * their bucket, free overflow pages associated with
    244 			 * this bucket.
    245 			 */
    246 			for (xbp = bp; xbp->ovfl;) {
    247 				next_xbp = xbp->ovfl;
    248 				xbp->ovfl = 0;
    249 				xbp = next_xbp;
    250 
    251 				/* Check that ovfl pointer is up date. */
    252 				if (IS_BUCKET(xbp->flags) ||
    253 				    (oaddr != xbp->addr))
    254 					break;
    255 
    256 				shortp = (u_int16_t *)xbp->page;
    257 				if (shortp[0])
    258 					/* set before __put_page */
    259 					oaddr = shortp[shortp[0] - 1];
    260 				if ((xbp->flags & BUF_MOD) && __put_page(hashp,
    261 				    xbp->page, xbp->addr, 0, 0))
    262 					return (NULL);
    263 				xbp->addr = 0;
    264 				xbp->flags = 0;
    265 				BUF_REMOVE(xbp);
    266 				LRU_INSERT(xbp);
    267 			}
    268 		}
    269 	}
    270 
    271 	/* Now assign this buffer */
    272 	bp->addr = addr;
    273 #ifdef DEBUG1
    274 	(void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
    275 	    bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
    276 #endif
    277 	bp->ovfl = NULL;
    278 	if (prev_bp) {
    279 		/*
    280 		 * If prev_bp is set, this is an overflow page, hook it in to
    281 		 * the buffer overflow links.
    282 		 */
    283 #ifdef DEBUG1
    284 		(void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
    285 		    prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0),
    286 		    (bp ? bp->addr : 0));
    287 #endif
    288 		prev_bp->ovfl = bp;
    289 		bp->flags = 0;
    290 	} else
    291 		bp->flags = BUF_BUCKET;
    292 	MRU_INSERT(bp);
    293 	return (bp);
    294 }
    295 
    296 extern void
    297 __buf_init(hashp, nbytes)
    298 	HTAB *hashp;
    299 	int nbytes;
    300 {
    301 	BUFHEAD *bfp;
    302 	int npages;
    303 
    304 	bfp = &(hashp->bufhead);
    305 	npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
    306 	npages = MAX(npages, MIN_BUFFERS);
    307 
    308 	hashp->nbufs = npages;
    309 	bfp->next = bfp;
    310 	bfp->prev = bfp;
    311 	/*
    312 	 * This space is calloc'd so these are already null.
    313 	 *
    314 	 * bfp->ovfl = NULL;
    315 	 * bfp->flags = 0;
    316 	 * bfp->page = NULL;
    317 	 * bfp->addr = 0;
    318 	 */
    319 }
    320 
    321 extern int
    322 __buf_free(hashp, do_free, to_disk)
    323 	HTAB *hashp;
    324 	int do_free, to_disk;
    325 {
    326 	BUFHEAD *bp;
    327 
    328 	/* Need to make sure that buffer manager has been initialized */
    329 	if (!LRU)
    330 		return (0);
    331 	for (bp = LRU; bp != &hashp->bufhead;) {
    332 		/* Check that the buffer is valid */
    333 		if (bp->addr || IS_BUCKET(bp->flags)) {
    334 			if (to_disk && (bp->flags & BUF_MOD) &&
    335 			    __put_page(hashp, bp->page,
    336 			    bp->addr, IS_BUCKET(bp->flags), 0))
    337 				return (-1);
    338 		}
    339 		/* Check if we are freeing stuff */
    340 		if (do_free) {
    341 			if (bp->page)
    342 				free(bp->page);
    343 			BUF_REMOVE(bp);
    344 			free(bp);
    345 			bp = LRU;
    346 		} else
    347 			bp = bp->prev;
    348 	}
    349 	return (0);
    350 }
    351 
    352 extern void
    353 __reclaim_buf(hashp, bp)
    354 	HTAB *hashp;
    355 	BUFHEAD *bp;
    356 {
    357 	bp->ovfl = 0;
    358 	bp->addr = 0;
    359 	bp->flags = 0;
    360 	BUF_REMOVE(bp);
    361 	LRU_INSERT(bp);
    362 }
    363