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