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