11573Srgrimes/*-
219029Speter * Copyright (c) 1990, 1993, 1994
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Margo Seltzer.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 4. Neither the name of the University nor the names of its contributors
171573Srgrimes *    may be used to endorse or promote products derived from this software
181573Srgrimes *    without specific prior written permission.
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
311573Srgrimes */
321573Srgrimes
331573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
3419029Speterstatic char sccsid[] = "@(#)hash_buf.c	8.5 (Berkeley) 7/15/94";
351573Srgrimes#endif /* LIBC_SCCS and not lint */
3692889Sobrien#include <sys/cdefs.h>
3792889Sobrien__FBSDID("$FreeBSD$");
381573Srgrimes
391573Srgrimes/*
401573Srgrimes * PACKAGE: hash
411573Srgrimes *
421573Srgrimes * DESCRIPTION:
431573Srgrimes *	Contains buffer management
441573Srgrimes *
451573Srgrimes * ROUTINES:
461573Srgrimes * External
471573Srgrimes *	__buf_init
481573Srgrimes *	__get_buf
491573Srgrimes *	__buf_free
501573Srgrimes *	__reclaim_buf
511573Srgrimes * Internal
521573Srgrimes *	newbuf
531573Srgrimes */
541573Srgrimes
551573Srgrimes#include <sys/param.h>
561573Srgrimes
5719029Speter#include <stddef.h>
581573Srgrimes#include <stdio.h>
591573Srgrimes#include <stdlib.h>
60190482Sdelphij#include <string.h>
6119029Speter
621573Srgrimes#ifdef DEBUG
631573Srgrimes#include <assert.h>
641573Srgrimes#endif
651573Srgrimes
661573Srgrimes#include <db.h>
671573Srgrimes#include "hash.h"
681573Srgrimes#include "page.h"
691573Srgrimes#include "extern.h"
701573Srgrimes
7192905Sobrienstatic BUFHEAD *newbuf(HTAB *, u_int32_t, BUFHEAD *);
721573Srgrimes
731573Srgrimes/* Unlink B from its place in the lru */
741573Srgrimes#define BUF_REMOVE(B) { \
751573Srgrimes	(B)->prev->next = (B)->next; \
761573Srgrimes	(B)->next->prev = (B)->prev; \
771573Srgrimes}
781573Srgrimes
791573Srgrimes/* Insert B after P */
801573Srgrimes#define BUF_INSERT(B, P) { \
811573Srgrimes	(B)->next = (P)->next; \
821573Srgrimes	(B)->prev = (P); \
831573Srgrimes	(P)->next = (B); \
841573Srgrimes	(B)->next->prev = (B); \
851573Srgrimes}
861573Srgrimes
871573Srgrimes#define	MRU	hashp->bufhead.next
881573Srgrimes#define	LRU	hashp->bufhead.prev
891573Srgrimes
901573Srgrimes#define MRU_INSERT(B)	BUF_INSERT((B), &hashp->bufhead)
911573Srgrimes#define LRU_INSERT(B)	BUF_INSERT((B), LRU)
921573Srgrimes
931573Srgrimes/*
941573Srgrimes * We are looking for a buffer with address "addr".  If prev_bp is NULL, then
951573Srgrimes * address is a bucket index.  If prev_bp is not NULL, then it points to the
961573Srgrimes * page previous to an overflow page that we are trying to find.
971573Srgrimes *
981573Srgrimes * CAVEAT:  The buffer header accessed via prev_bp's ovfl field may no longer
991573Srgrimes * be valid.  Therefore, you must always verify that its address matches the
1001573Srgrimes * address you are seeking.
1011573Srgrimes */
102189291SdelphijBUFHEAD *
103189291Sdelphij__get_buf(HTAB *hashp, u_int32_t addr,
104189291Sdelphij    BUFHEAD *prev_bp,	/* If prev_bp set, indicates a new overflow page. */
105189291Sdelphij    int newpage)
1061573Srgrimes{
10792889Sobrien	BUFHEAD *bp;
10892889Sobrien	u_int32_t is_disk_mask;
10992889Sobrien	int is_disk, segment_ndx;
1101573Srgrimes	SEGMENT segp;
1111573Srgrimes
1121573Srgrimes	is_disk = 0;
1131573Srgrimes	is_disk_mask = 0;
1141573Srgrimes	if (prev_bp) {
1151573Srgrimes		bp = prev_bp->ovfl;
1161573Srgrimes		if (!bp || (bp->addr != addr))
1171573Srgrimes			bp = NULL;
1181573Srgrimes		if (!newpage)
1191573Srgrimes			is_disk = BUF_DISK;
1201573Srgrimes	} else {
1211573Srgrimes		/* Grab buffer out of directory */
1221573Srgrimes		segment_ndx = addr & (hashp->SGSIZE - 1);
1231573Srgrimes
1241573Srgrimes		/* valid segment ensured by __call_hash() */
1251573Srgrimes		segp = hashp->dir[addr >> hashp->SSHIFT];
1261573Srgrimes#ifdef DEBUG
1271573Srgrimes		assert(segp != NULL);
1281573Srgrimes#endif
1291573Srgrimes		bp = PTROF(segp[segment_ndx]);
1301573Srgrimes		is_disk_mask = ISDISK(segp[segment_ndx]);
1311573Srgrimes		is_disk = is_disk_mask || !hashp->new_file;
1321573Srgrimes	}
1331573Srgrimes
1341573Srgrimes	if (!bp) {
1351573Srgrimes		bp = newbuf(hashp, addr, prev_bp);
1361573Srgrimes		if (!bp ||
1371573Srgrimes		    __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
1381573Srgrimes			return (NULL);
1391573Srgrimes		if (!prev_bp)
1401573Srgrimes			segp[segment_ndx] =
14119029Speter			    (BUFHEAD *)((ptrdiff_t)bp | is_disk_mask);
1421573Srgrimes	} else {
1431573Srgrimes		BUF_REMOVE(bp);
1441573Srgrimes		MRU_INSERT(bp);
1451573Srgrimes	}
1461573Srgrimes	return (bp);
1471573Srgrimes}
1481573Srgrimes
1491573Srgrimes/*
1501573Srgrimes * We need a buffer for this page. Either allocate one, or evict a resident
1511573Srgrimes * one (if we have as many buffers as we're allowed) and put this one in.
1521573Srgrimes *
1531573Srgrimes * If newbuf finds an error (returning NULL), it also sets errno.
1541573Srgrimes */
1551573Srgrimesstatic BUFHEAD *
156189291Sdelphijnewbuf(HTAB *hashp, u_int32_t addr, BUFHEAD *prev_bp)
1571573Srgrimes{
15892889Sobrien	BUFHEAD *bp;		/* The buffer we're going to use */
15992889Sobrien	BUFHEAD *xbp;		/* Temp pointer */
16092889Sobrien	BUFHEAD *next_xbp;
1611573Srgrimes	SEGMENT segp;
1621573Srgrimes	int segment_ndx;
16319029Speter	u_int16_t oaddr, *shortp;
1641573Srgrimes
1651573Srgrimes	oaddr = 0;
1661573Srgrimes	bp = LRU;
167190492Sdelphij
168190492Sdelphij        /* It is bad to overwrite the page under the cursor. */
169190492Sdelphij        if (bp == hashp->cpage) {
170190492Sdelphij                BUF_REMOVE(bp);
171190492Sdelphij                MRU_INSERT(bp);
172190492Sdelphij                bp = LRU;
173190492Sdelphij        }
174190492Sdelphij
175190492Sdelphij	/* If prev_bp is part of bp overflow, create a new buffer. */
176190492Sdelphij	if (hashp->nbufs == 0 && prev_bp && bp->ovfl) {
177190492Sdelphij		BUFHEAD *ovfl;
178190492Sdelphij
179190492Sdelphij		for (ovfl = bp->ovfl; ovfl ; ovfl = ovfl->ovfl) {
180190492Sdelphij			if (ovfl == prev_bp) {
181190492Sdelphij				hashp->nbufs++;
182190492Sdelphij				break;
183190492Sdelphij			}
184190492Sdelphij		}
185190492Sdelphij	}
186190492Sdelphij
1871573Srgrimes	/*
1881573Srgrimes	 * If LRU buffer is pinned, the buffer pool is too small. We need to
1891573Srgrimes	 * allocate more buffers.
1901573Srgrimes	 */
191190492Sdelphij	if (hashp->nbufs || (bp->flags & BUF_PIN) || bp == hashp->cpage) {
1921573Srgrimes		/* Allocate a new one */
193190482Sdelphij		if ((bp = (BUFHEAD *)calloc(1, sizeof(BUFHEAD))) == NULL)
1941573Srgrimes			return (NULL);
195190482Sdelphij		if ((bp->page = (char *)calloc(1, hashp->BSIZE)) == NULL) {
1961573Srgrimes			free(bp);
1971573Srgrimes			return (NULL);
1981573Srgrimes		}
1991573Srgrimes		if (hashp->nbufs)
2001573Srgrimes			hashp->nbufs--;
2011573Srgrimes	} else {
2021573Srgrimes		/* Kick someone out */
2031573Srgrimes		BUF_REMOVE(bp);
2041573Srgrimes		/*
2051573Srgrimes		 * If this is an overflow page with addr 0, it's already been
2061573Srgrimes		 * flushed back in an overflow chain and initialized.
2071573Srgrimes		 */
2081573Srgrimes		if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
2091573Srgrimes			/*
2101573Srgrimes			 * Set oaddr before __put_page so that you get it
2111573Srgrimes			 * before bytes are swapped.
2121573Srgrimes			 */
21319029Speter			shortp = (u_int16_t *)bp->page;
2141573Srgrimes			if (shortp[0])
2151573Srgrimes				oaddr = shortp[shortp[0] - 1];
2161573Srgrimes			if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
2171573Srgrimes			    bp->addr, (int)IS_BUCKET(bp->flags), 0))
2181573Srgrimes				return (NULL);
2191573Srgrimes			/*
2201573Srgrimes			 * Update the pointer to this page (i.e. invalidate it).
2211573Srgrimes			 *
2221573Srgrimes			 * If this is a new file (i.e. we created it at open
2231573Srgrimes			 * time), make sure that we mark pages which have been
2241573Srgrimes			 * written to disk so we retrieve them from disk later,
2251573Srgrimes			 * rather than allocating new pages.
2261573Srgrimes			 */
2271573Srgrimes			if (IS_BUCKET(bp->flags)) {
2281573Srgrimes				segment_ndx = bp->addr & (hashp->SGSIZE - 1);
2291573Srgrimes				segp = hashp->dir[bp->addr >> hashp->SSHIFT];
2301573Srgrimes#ifdef DEBUG
2311573Srgrimes				assert(segp != NULL);
2321573Srgrimes#endif
2331573Srgrimes
2341573Srgrimes				if (hashp->new_file &&
2351573Srgrimes				    ((bp->flags & BUF_MOD) ||
2361573Srgrimes				    ISDISK(segp[segment_ndx])))
2371573Srgrimes					segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
2381573Srgrimes				else
2391573Srgrimes					segp[segment_ndx] = NULL;
2401573Srgrimes			}
2411573Srgrimes			/*
2421573Srgrimes			 * Since overflow pages can only be access by means of
2431573Srgrimes			 * their bucket, free overflow pages associated with
2441573Srgrimes			 * this bucket.
2451573Srgrimes			 */
2461573Srgrimes			for (xbp = bp; xbp->ovfl;) {
2471573Srgrimes				next_xbp = xbp->ovfl;
2481573Srgrimes				xbp->ovfl = 0;
2491573Srgrimes				xbp = next_xbp;
2501573Srgrimes
2511573Srgrimes				/* Check that ovfl pointer is up date. */
2521573Srgrimes				if (IS_BUCKET(xbp->flags) ||
2531573Srgrimes				    (oaddr != xbp->addr))
2541573Srgrimes					break;
2551573Srgrimes
25619029Speter				shortp = (u_int16_t *)xbp->page;
2571573Srgrimes				if (shortp[0])
2581573Srgrimes					/* set before __put_page */
2591573Srgrimes					oaddr = shortp[shortp[0] - 1];
2601573Srgrimes				if ((xbp->flags & BUF_MOD) && __put_page(hashp,
2611573Srgrimes				    xbp->page, xbp->addr, 0, 0))
2621573Srgrimes					return (NULL);
2631573Srgrimes				xbp->addr = 0;
2641573Srgrimes				xbp->flags = 0;
2651573Srgrimes				BUF_REMOVE(xbp);
2661573Srgrimes				LRU_INSERT(xbp);
2671573Srgrimes			}
2681573Srgrimes		}
2691573Srgrimes	}
2701573Srgrimes
2711573Srgrimes	/* Now assign this buffer */
2721573Srgrimes	bp->addr = addr;
2731573Srgrimes#ifdef DEBUG1
2741573Srgrimes	(void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
2751573Srgrimes	    bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
2761573Srgrimes#endif
2771573Srgrimes	bp->ovfl = NULL;
2781573Srgrimes	if (prev_bp) {
2791573Srgrimes		/*
2801573Srgrimes		 * If prev_bp is set, this is an overflow page, hook it in to
2811573Srgrimes		 * the buffer overflow links.
2821573Srgrimes		 */
2831573Srgrimes#ifdef DEBUG1
2841573Srgrimes		(void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
285190493Sdelphij		    prev_bp->addr, (prev_bp->ovfl ? prev_bp->ovfl->addr : 0),
2861573Srgrimes		    (bp ? bp->addr : 0));
2871573Srgrimes#endif
2881573Srgrimes		prev_bp->ovfl = bp;
2891573Srgrimes		bp->flags = 0;
2901573Srgrimes	} else
2911573Srgrimes		bp->flags = BUF_BUCKET;
2921573Srgrimes	MRU_INSERT(bp);
2931573Srgrimes	return (bp);
2941573Srgrimes}
2951573Srgrimes
296189291Sdelphijvoid
297189291Sdelphij__buf_init(HTAB *hashp, int nbytes)
2981573Srgrimes{
2991573Srgrimes	BUFHEAD *bfp;
3001573Srgrimes	int npages;
3011573Srgrimes
3021573Srgrimes	bfp = &(hashp->bufhead);
3031573Srgrimes	npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
3041573Srgrimes	npages = MAX(npages, MIN_BUFFERS);
3051573Srgrimes
3061573Srgrimes	hashp->nbufs = npages;
3071573Srgrimes	bfp->next = bfp;
3081573Srgrimes	bfp->prev = bfp;
3091573Srgrimes	/*
3101573Srgrimes	 * This space is calloc'd so these are already null.
3111573Srgrimes	 *
3121573Srgrimes	 * bfp->ovfl = NULL;
3131573Srgrimes	 * bfp->flags = 0;
3141573Srgrimes	 * bfp->page = NULL;
3151573Srgrimes	 * bfp->addr = 0;
3161573Srgrimes	 */
3171573Srgrimes}
3181573Srgrimes
319189291Sdelphijint
320189291Sdelphij__buf_free(HTAB *hashp, int do_free, int to_disk)
3211573Srgrimes{
3221573Srgrimes	BUFHEAD *bp;
3231573Srgrimes
3241573Srgrimes	/* Need to make sure that buffer manager has been initialized */
3251573Srgrimes	if (!LRU)
3261573Srgrimes		return (0);
3271573Srgrimes	for (bp = LRU; bp != &hashp->bufhead;) {
3281573Srgrimes		/* Check that the buffer is valid */
3291573Srgrimes		if (bp->addr || IS_BUCKET(bp->flags)) {
3301573Srgrimes			if (to_disk && (bp->flags & BUF_MOD) &&
3311573Srgrimes			    __put_page(hashp, bp->page,
3321573Srgrimes			    bp->addr, IS_BUCKET(bp->flags), 0))
3331573Srgrimes				return (-1);
3341573Srgrimes		}
3351573Srgrimes		/* Check if we are freeing stuff */
3361573Srgrimes		if (do_free) {
337190482Sdelphij			if (bp->page) {
338190482Sdelphij				(void)memset(bp->page, 0, hashp->BSIZE);
3391573Srgrimes				free(bp->page);
340190482Sdelphij			}
3411573Srgrimes			BUF_REMOVE(bp);
3421573Srgrimes			free(bp);
3431573Srgrimes			bp = LRU;
3441573Srgrimes		} else
3451573Srgrimes			bp = bp->prev;
3461573Srgrimes	}
3471573Srgrimes	return (0);
3481573Srgrimes}
3491573Srgrimes
350189291Sdelphijvoid
351189291Sdelphij__reclaim_buf(HTAB *hashp, BUFHEAD *bp)
3521573Srgrimes{
3531573Srgrimes	bp->ovfl = 0;
3541573Srgrimes	bp->addr = 0;
3551573Srgrimes	bp->flags = 0;
3561573Srgrimes	BUF_REMOVE(bp);
3571573Srgrimes	LRU_INSERT(bp);
3581573Srgrimes}
359