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