xref: /freebsd/lib/libc/db/hash/hash_page.c (revision 9233c4d9)
158f0484fSRodney W. Grimes /*-
2f1e396bcSPaul Traina  * Copyright (c) 1990, 1993, 1994
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
658f0484fSRodney W. Grimes  * Margo Seltzer.
758f0484fSRodney W. Grimes  *
858f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes  * are met:
1158f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1658f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1858f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1958f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes  *    without specific prior written permission.
2358f0484fSRodney W. Grimes  *
2458f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes  * SUCH DAMAGE.
3592927338SJason Evans  *
3692927338SJason Evans  * $FreeBSD$
3758f0484fSRodney W. Grimes  */
3858f0484fSRodney W. Grimes 
3958f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
40f1e396bcSPaul Traina static char sccsid[] = "@(#)hash_page.c	8.7 (Berkeley) 8/16/94";
4158f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes /*
4458f0484fSRodney W. Grimes  * PACKAGE:  hashing
4558f0484fSRodney W. Grimes  *
4658f0484fSRodney W. Grimes  * DESCRIPTION:
4758f0484fSRodney W. Grimes  *	Page manipulation for hashing package.
4858f0484fSRodney W. Grimes  *
4958f0484fSRodney W. Grimes  * ROUTINES:
5058f0484fSRodney W. Grimes  *
5158f0484fSRodney W. Grimes  * External
5258f0484fSRodney W. Grimes  *	__get_page
5358f0484fSRodney W. Grimes  *	__add_ovflpage
5458f0484fSRodney W. Grimes  * Internal
5558f0484fSRodney W. Grimes  *	overflow_page
5658f0484fSRodney W. Grimes  *	open_temp
5758f0484fSRodney W. Grimes  */
5858f0484fSRodney W. Grimes 
5958f0484fSRodney W. Grimes #include <sys/types.h>
6058f0484fSRodney W. Grimes 
6158f0484fSRodney W. Grimes #include <errno.h>
6258f0484fSRodney W. Grimes #include <fcntl.h>
6358f0484fSRodney W. Grimes #include <signal.h>
6458f0484fSRodney W. Grimes #include <stdio.h>
6558f0484fSRodney W. Grimes #include <stdlib.h>
6658f0484fSRodney W. Grimes #include <string.h>
6758f0484fSRodney W. Grimes #include <unistd.h>
6858f0484fSRodney W. Grimes #ifdef DEBUG
6958f0484fSRodney W. Grimes #include <assert.h>
7058f0484fSRodney W. Grimes #endif
7158f0484fSRodney W. Grimes 
7258f0484fSRodney W. Grimes #include <db.h>
7358f0484fSRodney W. Grimes #include "hash.h"
7458f0484fSRodney W. Grimes #include "page.h"
7558f0484fSRodney W. Grimes #include "extern.h"
7658f0484fSRodney W. Grimes 
77f1e396bcSPaul Traina static u_int32_t	*fetch_bitmap __P((HTAB *, int));
78f1e396bcSPaul Traina static u_int32_t	 first_free __P((u_int32_t));
7958f0484fSRodney W. Grimes static int	 open_temp __P((HTAB *));
80f1e396bcSPaul Traina static u_int16_t	 overflow_page __P((HTAB *));
8158f0484fSRodney W. Grimes static void	 putpair __P((char *, const DBT *, const DBT *));
82f1e396bcSPaul Traina static void	 squeeze_key __P((u_int16_t *, const DBT *, const DBT *));
8358f0484fSRodney W. Grimes static int	 ugly_split
84f1e396bcSPaul Traina 		    __P((HTAB *, u_int32_t, BUFHEAD *, BUFHEAD *, int, int));
8558f0484fSRodney W. Grimes 
8658f0484fSRodney W. Grimes #define	PAGE_INIT(P) { \
87f1e396bcSPaul Traina 	((u_int16_t *)(P))[0] = 0; \
88f1e396bcSPaul Traina 	((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
89f1e396bcSPaul Traina 	((u_int16_t *)(P))[2] = hashp->BSIZE; \
9058f0484fSRodney W. Grimes }
9158f0484fSRodney W. Grimes 
9258f0484fSRodney W. Grimes /*
9358f0484fSRodney W. Grimes  * This is called AFTER we have verified that there is room on the page for
9458f0484fSRodney W. Grimes  * the pair (PAIRFITS has returned true) so we go right ahead and start moving
9558f0484fSRodney W. Grimes  * stuff on.
9658f0484fSRodney W. Grimes  */
9758f0484fSRodney W. Grimes static void
9858f0484fSRodney W. Grimes putpair(p, key, val)
9958f0484fSRodney W. Grimes 	char *p;
10058f0484fSRodney W. Grimes 	const DBT *key, *val;
10158f0484fSRodney W. Grimes {
102f1e396bcSPaul Traina 	register u_int16_t *bp, n, off;
10358f0484fSRodney W. Grimes 
104f1e396bcSPaul Traina 	bp = (u_int16_t *)p;
10558f0484fSRodney W. Grimes 
10658f0484fSRodney W. Grimes 	/* Enter the key first. */
10758f0484fSRodney W. Grimes 	n = bp[0];
10858f0484fSRodney W. Grimes 
10958f0484fSRodney W. Grimes 	off = OFFSET(bp) - key->size;
11058f0484fSRodney W. Grimes 	memmove(p + off, key->data, key->size);
11158f0484fSRodney W. Grimes 	bp[++n] = off;
11258f0484fSRodney W. Grimes 
11358f0484fSRodney W. Grimes 	/* Now the data. */
11458f0484fSRodney W. Grimes 	off -= val->size;
11558f0484fSRodney W. Grimes 	memmove(p + off, val->data, val->size);
11658f0484fSRodney W. Grimes 	bp[++n] = off;
11758f0484fSRodney W. Grimes 
11858f0484fSRodney W. Grimes 	/* Adjust page info. */
11958f0484fSRodney W. Grimes 	bp[0] = n;
120f1e396bcSPaul Traina 	bp[n + 1] = off - ((n + 3) * sizeof(u_int16_t));
12158f0484fSRodney W. Grimes 	bp[n + 2] = off;
12258f0484fSRodney W. Grimes }
12358f0484fSRodney W. Grimes 
12458f0484fSRodney W. Grimes /*
12558f0484fSRodney W. Grimes  * Returns:
12658f0484fSRodney W. Grimes  *	 0 OK
12758f0484fSRodney W. Grimes  *	-1 error
12858f0484fSRodney W. Grimes  */
12958f0484fSRodney W. Grimes extern int
13058f0484fSRodney W. Grimes __delpair(hashp, bufp, ndx)
13158f0484fSRodney W. Grimes 	HTAB *hashp;
13258f0484fSRodney W. Grimes 	BUFHEAD *bufp;
13358f0484fSRodney W. Grimes 	register int ndx;
13458f0484fSRodney W. Grimes {
135f1e396bcSPaul Traina 	register u_int16_t *bp, newoff;
13658f0484fSRodney W. Grimes 	register int n;
137f1e396bcSPaul Traina 	u_int16_t pairlen;
13858f0484fSRodney W. Grimes 
139f1e396bcSPaul Traina 	bp = (u_int16_t *)bufp->page;
14058f0484fSRodney W. Grimes 	n = bp[0];
14158f0484fSRodney W. Grimes 
14258f0484fSRodney W. Grimes 	if (bp[ndx + 1] < REAL_KEY)
14358f0484fSRodney W. Grimes 		return (__big_delete(hashp, bufp));
14458f0484fSRodney W. Grimes 	if (ndx != 1)
14558f0484fSRodney W. Grimes 		newoff = bp[ndx - 1];
14658f0484fSRodney W. Grimes 	else
14758f0484fSRodney W. Grimes 		newoff = hashp->BSIZE;
14858f0484fSRodney W. Grimes 	pairlen = newoff - bp[ndx + 1];
14958f0484fSRodney W. Grimes 
15058f0484fSRodney W. Grimes 	if (ndx != (n - 1)) {
15158f0484fSRodney W. Grimes 		/* Hard Case -- need to shuffle keys */
15258f0484fSRodney W. Grimes 		register int i;
15358f0484fSRodney W. Grimes 		register char *src = bufp->page + (int)OFFSET(bp);
15458f0484fSRodney W. Grimes 		register char *dst = src + (int)pairlen;
15558f0484fSRodney W. Grimes 		memmove(dst, src, bp[ndx + 1] - OFFSET(bp));
15658f0484fSRodney W. Grimes 
15758f0484fSRodney W. Grimes 		/* Now adjust the pointers */
15858f0484fSRodney W. Grimes 		for (i = ndx + 2; i <= n; i += 2) {
15958f0484fSRodney W. Grimes 			if (bp[i + 1] == OVFLPAGE) {
16058f0484fSRodney W. Grimes 				bp[i - 2] = bp[i];
16158f0484fSRodney W. Grimes 				bp[i - 1] = bp[i + 1];
16258f0484fSRodney W. Grimes 			} else {
16358f0484fSRodney W. Grimes 				bp[i - 2] = bp[i] + pairlen;
16458f0484fSRodney W. Grimes 				bp[i - 1] = bp[i + 1] + pairlen;
16558f0484fSRodney W. Grimes 			}
16658f0484fSRodney W. Grimes 		}
16758f0484fSRodney W. Grimes 	}
16858f0484fSRodney W. Grimes 	/* Finally adjust the page data */
16958f0484fSRodney W. Grimes 	bp[n] = OFFSET(bp) + pairlen;
170f1e396bcSPaul Traina 	bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(u_int16_t);
17158f0484fSRodney W. Grimes 	bp[0] = n - 2;
17258f0484fSRodney W. Grimes 	hashp->NKEYS--;
17358f0484fSRodney W. Grimes 
17458f0484fSRodney W. Grimes 	bufp->flags |= BUF_MOD;
17558f0484fSRodney W. Grimes 	return (0);
17658f0484fSRodney W. Grimes }
17758f0484fSRodney W. Grimes /*
17858f0484fSRodney W. Grimes  * Returns:
17958f0484fSRodney W. Grimes  *	 0 ==> OK
18058f0484fSRodney W. Grimes  *	-1 ==> Error
18158f0484fSRodney W. Grimes  */
18258f0484fSRodney W. Grimes extern int
18358f0484fSRodney W. Grimes __split_page(hashp, obucket, nbucket)
18458f0484fSRodney W. Grimes 	HTAB *hashp;
185f1e396bcSPaul Traina 	u_int32_t obucket, nbucket;
18658f0484fSRodney W. Grimes {
18758f0484fSRodney W. Grimes 	register BUFHEAD *new_bufp, *old_bufp;
188f1e396bcSPaul Traina 	register u_int16_t *ino;
18958f0484fSRodney W. Grimes 	register char *np;
19058f0484fSRodney W. Grimes 	DBT key, val;
19158f0484fSRodney W. Grimes 	int n, ndx, retval;
192f1e396bcSPaul Traina 	u_int16_t copyto, diff, off, moved;
19358f0484fSRodney W. Grimes 	char *op;
19458f0484fSRodney W. Grimes 
195f1e396bcSPaul Traina 	copyto = (u_int16_t)hashp->BSIZE;
196f1e396bcSPaul Traina 	off = (u_int16_t)hashp->BSIZE;
19758f0484fSRodney W. Grimes 	old_bufp = __get_buf(hashp, obucket, NULL, 0);
19858f0484fSRodney W. Grimes 	if (old_bufp == NULL)
19958f0484fSRodney W. Grimes 		return (-1);
20058f0484fSRodney W. Grimes 	new_bufp = __get_buf(hashp, nbucket, NULL, 0);
20158f0484fSRodney W. Grimes 	if (new_bufp == NULL)
20258f0484fSRodney W. Grimes 		return (-1);
20358f0484fSRodney W. Grimes 
20458f0484fSRodney W. Grimes 	old_bufp->flags |= (BUF_MOD | BUF_PIN);
20558f0484fSRodney W. Grimes 	new_bufp->flags |= (BUF_MOD | BUF_PIN);
20658f0484fSRodney W. Grimes 
207f1e396bcSPaul Traina 	ino = (u_int16_t *)(op = old_bufp->page);
20858f0484fSRodney W. Grimes 	np = new_bufp->page;
20958f0484fSRodney W. Grimes 
21058f0484fSRodney W. Grimes 	moved = 0;
21158f0484fSRodney W. Grimes 
21258f0484fSRodney W. Grimes 	for (n = 1, ndx = 1; n < ino[0]; n += 2) {
21358f0484fSRodney W. Grimes 		if (ino[n + 1] < REAL_KEY) {
21458f0484fSRodney W. Grimes 			retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
21558f0484fSRodney W. Grimes 			    (int)copyto, (int)moved);
21658f0484fSRodney W. Grimes 			old_bufp->flags &= ~BUF_PIN;
21758f0484fSRodney W. Grimes 			new_bufp->flags &= ~BUF_PIN;
21858f0484fSRodney W. Grimes 			return (retval);
21958f0484fSRodney W. Grimes 
22058f0484fSRodney W. Grimes 		}
22158f0484fSRodney W. Grimes 		key.data = (u_char *)op + ino[n];
22258f0484fSRodney W. Grimes 		key.size = off - ino[n];
22358f0484fSRodney W. Grimes 
22458f0484fSRodney W. Grimes 		if (__call_hash(hashp, key.data, key.size) == obucket) {
22558f0484fSRodney W. Grimes 			/* Don't switch page */
22658f0484fSRodney W. Grimes 			diff = copyto - off;
22758f0484fSRodney W. Grimes 			if (diff) {
22858f0484fSRodney W. Grimes 				copyto = ino[n + 1] + diff;
22958f0484fSRodney W. Grimes 				memmove(op + copyto, op + ino[n + 1],
23058f0484fSRodney W. Grimes 				    off - ino[n + 1]);
23158f0484fSRodney W. Grimes 				ino[ndx] = copyto + ino[n] - ino[n + 1];
23258f0484fSRodney W. Grimes 				ino[ndx + 1] = copyto;
23358f0484fSRodney W. Grimes 			} else
23458f0484fSRodney W. Grimes 				copyto = ino[n + 1];
23558f0484fSRodney W. Grimes 			ndx += 2;
23658f0484fSRodney W. Grimes 		} else {
23758f0484fSRodney W. Grimes 			/* Switch page */
23858f0484fSRodney W. Grimes 			val.data = (u_char *)op + ino[n + 1];
23958f0484fSRodney W. Grimes 			val.size = ino[n] - ino[n + 1];
24058f0484fSRodney W. Grimes 			putpair(np, &key, &val);
24158f0484fSRodney W. Grimes 			moved += 2;
24258f0484fSRodney W. Grimes 		}
24358f0484fSRodney W. Grimes 
24458f0484fSRodney W. Grimes 		off = ino[n + 1];
24558f0484fSRodney W. Grimes 	}
24658f0484fSRodney W. Grimes 
24758f0484fSRodney W. Grimes 	/* Now clean up the page */
24858f0484fSRodney W. Grimes 	ino[0] -= moved;
249f1e396bcSPaul Traina 	FREESPACE(ino) = copyto - sizeof(u_int16_t) * (ino[0] + 3);
25058f0484fSRodney W. Grimes 	OFFSET(ino) = copyto;
25158f0484fSRodney W. Grimes 
25258f0484fSRodney W. Grimes #ifdef DEBUG3
25358f0484fSRodney W. Grimes 	(void)fprintf(stderr, "split %d/%d\n",
254f1e396bcSPaul Traina 	    ((u_int16_t *)np)[0] / 2,
255f1e396bcSPaul Traina 	    ((u_int16_t *)op)[0] / 2);
25658f0484fSRodney W. Grimes #endif
25758f0484fSRodney W. Grimes 	/* unpin both pages */
25858f0484fSRodney W. Grimes 	old_bufp->flags &= ~BUF_PIN;
25958f0484fSRodney W. Grimes 	new_bufp->flags &= ~BUF_PIN;
26058f0484fSRodney W. Grimes 	return (0);
26158f0484fSRodney W. Grimes }
26258f0484fSRodney W. Grimes 
26358f0484fSRodney W. Grimes /*
26458f0484fSRodney W. Grimes  * Called when we encounter an overflow or big key/data page during split
26558f0484fSRodney W. Grimes  * handling.  This is special cased since we have to begin checking whether
26658f0484fSRodney W. Grimes  * the key/data pairs fit on their respective pages and because we may need
26758f0484fSRodney W. Grimes  * overflow pages for both the old and new pages.
26858f0484fSRodney W. Grimes  *
26958f0484fSRodney W. Grimes  * The first page might be a page with regular key/data pairs in which case
27058f0484fSRodney W. Grimes  * we have a regular overflow condition and just need to go on to the next
27158f0484fSRodney W. Grimes  * page or it might be a big key/data pair in which case we need to fix the
27258f0484fSRodney W. Grimes  * big key/data pair.
27358f0484fSRodney W. Grimes  *
27458f0484fSRodney W. Grimes  * Returns:
27558f0484fSRodney W. Grimes  *	 0 ==> success
27658f0484fSRodney W. Grimes  *	-1 ==> failure
27758f0484fSRodney W. Grimes  */
27858f0484fSRodney W. Grimes static int
27958f0484fSRodney W. Grimes ugly_split(hashp, obucket, old_bufp, new_bufp, copyto, moved)
28058f0484fSRodney W. Grimes 	HTAB *hashp;
281f1e396bcSPaul Traina 	u_int32_t obucket;	/* Same as __split_page. */
28258f0484fSRodney W. Grimes 	BUFHEAD *old_bufp, *new_bufp;
28358f0484fSRodney W. Grimes 	int copyto;	/* First byte on page which contains key/data values. */
28458f0484fSRodney W. Grimes 	int moved;	/* Number of pairs moved to new page. */
28558f0484fSRodney W. Grimes {
28658f0484fSRodney W. Grimes 	register BUFHEAD *bufp;	/* Buffer header for ino */
287f1e396bcSPaul Traina 	register u_int16_t *ino;	/* Page keys come off of */
288f1e396bcSPaul Traina 	register u_int16_t *np;	/* New page */
289f1e396bcSPaul Traina 	register u_int16_t *op;	/* Page keys go on to if they aren't moving */
29058f0484fSRodney W. Grimes 
29158f0484fSRodney W. Grimes 	BUFHEAD *last_bfp;	/* Last buf header OVFL needing to be freed */
29258f0484fSRodney W. Grimes 	DBT key, val;
29358f0484fSRodney W. Grimes 	SPLIT_RETURN ret;
294f1e396bcSPaul Traina 	u_int16_t n, off, ov_addr, scopyto;
29558f0484fSRodney W. Grimes 	char *cino;		/* Character value of ino */
29658f0484fSRodney W. Grimes 
29758f0484fSRodney W. Grimes 	bufp = old_bufp;
298f1e396bcSPaul Traina 	ino = (u_int16_t *)old_bufp->page;
299f1e396bcSPaul Traina 	np = (u_int16_t *)new_bufp->page;
300f1e396bcSPaul Traina 	op = (u_int16_t *)old_bufp->page;
30158f0484fSRodney W. Grimes 	last_bfp = NULL;
302f1e396bcSPaul Traina 	scopyto = (u_int16_t)copyto;	/* ANSI */
30358f0484fSRodney W. Grimes 
30458f0484fSRodney W. Grimes 	n = ino[0] - 1;
30558f0484fSRodney W. Grimes 	while (n < ino[0]) {
30658f0484fSRodney W. Grimes 		if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
30758f0484fSRodney W. Grimes 			if (__big_split(hashp, old_bufp,
30858f0484fSRodney W. Grimes 			    new_bufp, bufp, bufp->addr, obucket, &ret))
30958f0484fSRodney W. Grimes 				return (-1);
31058f0484fSRodney W. Grimes 			old_bufp = ret.oldp;
31158f0484fSRodney W. Grimes 			if (!old_bufp)
31258f0484fSRodney W. Grimes 				return (-1);
313f1e396bcSPaul Traina 			op = (u_int16_t *)old_bufp->page;
31458f0484fSRodney W. Grimes 			new_bufp = ret.newp;
31558f0484fSRodney W. Grimes 			if (!new_bufp)
31658f0484fSRodney W. Grimes 				return (-1);
317f1e396bcSPaul Traina 			np = (u_int16_t *)new_bufp->page;
31858f0484fSRodney W. Grimes 			bufp = ret.nextp;
31958f0484fSRodney W. Grimes 			if (!bufp)
32058f0484fSRodney W. Grimes 				return (0);
32158f0484fSRodney W. Grimes 			cino = (char *)bufp->page;
322f1e396bcSPaul Traina 			ino = (u_int16_t *)cino;
32358f0484fSRodney W. Grimes 			last_bfp = ret.nextp;
32458f0484fSRodney W. Grimes 		} else if (ino[n + 1] == OVFLPAGE) {
32558f0484fSRodney W. Grimes 			ov_addr = ino[n];
32658f0484fSRodney W. Grimes 			/*
32758f0484fSRodney W. Grimes 			 * Fix up the old page -- the extra 2 are the fields
32858f0484fSRodney W. Grimes 			 * which contained the overflow information.
32958f0484fSRodney W. Grimes 			 */
33058f0484fSRodney W. Grimes 			ino[0] -= (moved + 2);
33158f0484fSRodney W. Grimes 			FREESPACE(ino) =
332f1e396bcSPaul Traina 			    scopyto - sizeof(u_int16_t) * (ino[0] + 3);
33358f0484fSRodney W. Grimes 			OFFSET(ino) = scopyto;
33458f0484fSRodney W. Grimes 
33558f0484fSRodney W. Grimes 			bufp = __get_buf(hashp, ov_addr, bufp, 0);
33658f0484fSRodney W. Grimes 			if (!bufp)
33758f0484fSRodney W. Grimes 				return (-1);
33858f0484fSRodney W. Grimes 
339f1e396bcSPaul Traina 			ino = (u_int16_t *)bufp->page;
34058f0484fSRodney W. Grimes 			n = 1;
34158f0484fSRodney W. Grimes 			scopyto = hashp->BSIZE;
34258f0484fSRodney W. Grimes 			moved = 0;
34358f0484fSRodney W. Grimes 
34458f0484fSRodney W. Grimes 			if (last_bfp)
34558f0484fSRodney W. Grimes 				__free_ovflpage(hashp, last_bfp);
34658f0484fSRodney W. Grimes 			last_bfp = bufp;
34758f0484fSRodney W. Grimes 		}
34858f0484fSRodney W. Grimes 		/* Move regular sized pairs of there are any */
34958f0484fSRodney W. Grimes 		off = hashp->BSIZE;
35058f0484fSRodney W. Grimes 		for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
35158f0484fSRodney W. Grimes 			cino = (char *)ino;
35258f0484fSRodney W. Grimes 			key.data = (u_char *)cino + ino[n];
35358f0484fSRodney W. Grimes 			key.size = off - ino[n];
35458f0484fSRodney W. Grimes 			val.data = (u_char *)cino + ino[n + 1];
35558f0484fSRodney W. Grimes 			val.size = ino[n] - ino[n + 1];
35658f0484fSRodney W. Grimes 			off = ino[n + 1];
35758f0484fSRodney W. Grimes 
35858f0484fSRodney W. Grimes 			if (__call_hash(hashp, key.data, key.size) == obucket) {
35958f0484fSRodney W. Grimes 				/* Keep on old page */
36058f0484fSRodney W. Grimes 				if (PAIRFITS(op, (&key), (&val)))
36158f0484fSRodney W. Grimes 					putpair((char *)op, &key, &val);
36258f0484fSRodney W. Grimes 				else {
36358f0484fSRodney W. Grimes 					old_bufp =
36458f0484fSRodney W. Grimes 					    __add_ovflpage(hashp, old_bufp);
36558f0484fSRodney W. Grimes 					if (!old_bufp)
36658f0484fSRodney W. Grimes 						return (-1);
367f1e396bcSPaul Traina 					op = (u_int16_t *)old_bufp->page;
36858f0484fSRodney W. Grimes 					putpair((char *)op, &key, &val);
36958f0484fSRodney W. Grimes 				}
37058f0484fSRodney W. Grimes 				old_bufp->flags |= BUF_MOD;
37158f0484fSRodney W. Grimes 			} else {
37258f0484fSRodney W. Grimes 				/* Move to new page */
37358f0484fSRodney W. Grimes 				if (PAIRFITS(np, (&key), (&val)))
37458f0484fSRodney W. Grimes 					putpair((char *)np, &key, &val);
37558f0484fSRodney W. Grimes 				else {
37658f0484fSRodney W. Grimes 					new_bufp =
37758f0484fSRodney W. Grimes 					    __add_ovflpage(hashp, new_bufp);
37858f0484fSRodney W. Grimes 					if (!new_bufp)
37958f0484fSRodney W. Grimes 						return (-1);
380f1e396bcSPaul Traina 					np = (u_int16_t *)new_bufp->page;
38158f0484fSRodney W. Grimes 					putpair((char *)np, &key, &val);
38258f0484fSRodney W. Grimes 				}
38358f0484fSRodney W. Grimes 				new_bufp->flags |= BUF_MOD;
38458f0484fSRodney W. Grimes 			}
38558f0484fSRodney W. Grimes 		}
38658f0484fSRodney W. Grimes 	}
38758f0484fSRodney W. Grimes 	if (last_bfp)
38858f0484fSRodney W. Grimes 		__free_ovflpage(hashp, last_bfp);
38958f0484fSRodney W. Grimes 	return (0);
39058f0484fSRodney W. Grimes }
39158f0484fSRodney W. Grimes 
39258f0484fSRodney W. Grimes /*
39358f0484fSRodney W. Grimes  * Add the given pair to the page
39458f0484fSRodney W. Grimes  *
39558f0484fSRodney W. Grimes  * Returns:
39658f0484fSRodney W. Grimes  *	0 ==> OK
39758f0484fSRodney W. Grimes  *	1 ==> failure
39858f0484fSRodney W. Grimes  */
39958f0484fSRodney W. Grimes extern int
40058f0484fSRodney W. Grimes __addel(hashp, bufp, key, val)
40158f0484fSRodney W. Grimes 	HTAB *hashp;
40258f0484fSRodney W. Grimes 	BUFHEAD *bufp;
40358f0484fSRodney W. Grimes 	const DBT *key, *val;
40458f0484fSRodney W. Grimes {
405f1e396bcSPaul Traina 	register u_int16_t *bp, *sop;
40658f0484fSRodney W. Grimes 	int do_expand;
40758f0484fSRodney W. Grimes 
408f1e396bcSPaul Traina 	bp = (u_int16_t *)bufp->page;
40958f0484fSRodney W. Grimes 	do_expand = 0;
41058f0484fSRodney W. Grimes 	while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
41158f0484fSRodney W. Grimes 		/* Exception case */
41258f0484fSRodney W. Grimes 		if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
41358f0484fSRodney W. Grimes 			/* This is the last page of a big key/data pair
41458f0484fSRodney W. Grimes 			   and we need to add another page */
41558f0484fSRodney W. Grimes 			break;
41658f0484fSRodney W. Grimes 		else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
41758f0484fSRodney W. Grimes 			bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
41858f0484fSRodney W. Grimes 			if (!bufp)
41958f0484fSRodney W. Grimes 				return (-1);
420f1e396bcSPaul Traina 			bp = (u_int16_t *)bufp->page;
42158f0484fSRodney W. Grimes 		} else
42258f0484fSRodney W. Grimes 			/* Try to squeeze key on this page */
42358f0484fSRodney W. Grimes 			if (FREESPACE(bp) > PAIRSIZE(key, val)) {
42458f0484fSRodney W. Grimes 				squeeze_key(bp, key, val);
42558f0484fSRodney W. Grimes 				return (0);
42658f0484fSRodney W. Grimes 			} else {
42758f0484fSRodney W. Grimes 				bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
42858f0484fSRodney W. Grimes 				if (!bufp)
42958f0484fSRodney W. Grimes 					return (-1);
430f1e396bcSPaul Traina 				bp = (u_int16_t *)bufp->page;
43158f0484fSRodney W. Grimes 			}
43258f0484fSRodney W. Grimes 
43358f0484fSRodney W. Grimes 	if (PAIRFITS(bp, key, val))
43458f0484fSRodney W. Grimes 		putpair(bufp->page, key, val);
43558f0484fSRodney W. Grimes 	else {
43658f0484fSRodney W. Grimes 		do_expand = 1;
43758f0484fSRodney W. Grimes 		bufp = __add_ovflpage(hashp, bufp);
43858f0484fSRodney W. Grimes 		if (!bufp)
43958f0484fSRodney W. Grimes 			return (-1);
440f1e396bcSPaul Traina 		sop = (u_int16_t *)bufp->page;
44158f0484fSRodney W. Grimes 
44258f0484fSRodney W. Grimes 		if (PAIRFITS(sop, key, val))
44358f0484fSRodney W. Grimes 			putpair((char *)sop, key, val);
44458f0484fSRodney W. Grimes 		else
44558f0484fSRodney W. Grimes 			if (__big_insert(hashp, bufp, key, val))
44658f0484fSRodney W. Grimes 				return (-1);
44758f0484fSRodney W. Grimes 	}
44858f0484fSRodney W. Grimes 	bufp->flags |= BUF_MOD;
44958f0484fSRodney W. Grimes 	/*
45058f0484fSRodney W. Grimes 	 * If the average number of keys per bucket exceeds the fill factor,
45158f0484fSRodney W. Grimes 	 * expand the table.
45258f0484fSRodney W. Grimes 	 */
45358f0484fSRodney W. Grimes 	hashp->NKEYS++;
45458f0484fSRodney W. Grimes 	if (do_expand ||
45558f0484fSRodney W. Grimes 	    (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
45658f0484fSRodney W. Grimes 		return (__expand_table(hashp));
45758f0484fSRodney W. Grimes 	return (0);
45858f0484fSRodney W. Grimes }
45958f0484fSRodney W. Grimes 
46058f0484fSRodney W. Grimes /*
46158f0484fSRodney W. Grimes  *
46258f0484fSRodney W. Grimes  * Returns:
46358f0484fSRodney W. Grimes  *	pointer on success
46458f0484fSRodney W. Grimes  *	NULL on error
46558f0484fSRodney W. Grimes  */
46658f0484fSRodney W. Grimes extern BUFHEAD *
46758f0484fSRodney W. Grimes __add_ovflpage(hashp, bufp)
46858f0484fSRodney W. Grimes 	HTAB *hashp;
46958f0484fSRodney W. Grimes 	BUFHEAD *bufp;
47058f0484fSRodney W. Grimes {
471f1e396bcSPaul Traina 	register u_int16_t *sp;
472f1e396bcSPaul Traina 	u_int16_t ndx, ovfl_num;
47358f0484fSRodney W. Grimes #ifdef DEBUG1
47458f0484fSRodney W. Grimes 	int tmp1, tmp2;
47558f0484fSRodney W. Grimes #endif
476f1e396bcSPaul Traina 	sp = (u_int16_t *)bufp->page;
47758f0484fSRodney W. Grimes 
47858f0484fSRodney W. Grimes 	/* Check if we are dynamically determining the fill factor */
47958f0484fSRodney W. Grimes 	if (hashp->FFACTOR == DEF_FFACTOR) {
48058f0484fSRodney W. Grimes 		hashp->FFACTOR = sp[0] >> 1;
48158f0484fSRodney W. Grimes 		if (hashp->FFACTOR < MIN_FFACTOR)
48258f0484fSRodney W. Grimes 			hashp->FFACTOR = MIN_FFACTOR;
48358f0484fSRodney W. Grimes 	}
48458f0484fSRodney W. Grimes 	bufp->flags |= BUF_MOD;
48558f0484fSRodney W. Grimes 	ovfl_num = overflow_page(hashp);
48658f0484fSRodney W. Grimes #ifdef DEBUG1
48758f0484fSRodney W. Grimes 	tmp1 = bufp->addr;
48858f0484fSRodney W. Grimes 	tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
48958f0484fSRodney W. Grimes #endif
49058f0484fSRodney W. Grimes 	if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
49158f0484fSRodney W. Grimes 		return (NULL);
49258f0484fSRodney W. Grimes 	bufp->ovfl->flags |= BUF_MOD;
49358f0484fSRodney W. Grimes #ifdef DEBUG1
49458f0484fSRodney W. Grimes 	(void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
49558f0484fSRodney W. Grimes 	    tmp1, tmp2, bufp->ovfl->addr);
49658f0484fSRodney W. Grimes #endif
49758f0484fSRodney W. Grimes 	ndx = sp[0];
49858f0484fSRodney W. Grimes 	/*
49958f0484fSRodney W. Grimes 	 * Since a pair is allocated on a page only if there's room to add
50058f0484fSRodney W. Grimes 	 * an overflow page, we know that the OVFL information will fit on
50158f0484fSRodney W. Grimes 	 * the page.
50258f0484fSRodney W. Grimes 	 */
50358f0484fSRodney W. Grimes 	sp[ndx + 4] = OFFSET(sp);
50458f0484fSRodney W. Grimes 	sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
50558f0484fSRodney W. Grimes 	sp[ndx + 1] = ovfl_num;
50658f0484fSRodney W. Grimes 	sp[ndx + 2] = OVFLPAGE;
50758f0484fSRodney W. Grimes 	sp[0] = ndx + 2;
50858f0484fSRodney W. Grimes #ifdef HASH_STATISTICS
50958f0484fSRodney W. Grimes 	hash_overflows++;
51058f0484fSRodney W. Grimes #endif
51158f0484fSRodney W. Grimes 	return (bufp->ovfl);
51258f0484fSRodney W. Grimes }
51358f0484fSRodney W. Grimes 
51458f0484fSRodney W. Grimes /*
51558f0484fSRodney W. Grimes  * Returns:
51658f0484fSRodney W. Grimes  *	 0 indicates SUCCESS
51758f0484fSRodney W. Grimes  *	-1 indicates FAILURE
51858f0484fSRodney W. Grimes  */
51958f0484fSRodney W. Grimes extern int
52058f0484fSRodney W. Grimes __get_page(hashp, p, bucket, is_bucket, is_disk, is_bitmap)
52158f0484fSRodney W. Grimes 	HTAB *hashp;
52258f0484fSRodney W. Grimes 	char *p;
523f1e396bcSPaul Traina 	u_int32_t bucket;
52458f0484fSRodney W. Grimes 	int is_bucket, is_disk, is_bitmap;
52558f0484fSRodney W. Grimes {
52658f0484fSRodney W. Grimes 	register int fd, page, size;
52758f0484fSRodney W. Grimes 	int rsize;
528f1e396bcSPaul Traina 	u_int16_t *bp;
52958f0484fSRodney W. Grimes 
53058f0484fSRodney W. Grimes 	fd = hashp->fp;
53158f0484fSRodney W. Grimes 	size = hashp->BSIZE;
53258f0484fSRodney W. Grimes 
53358f0484fSRodney W. Grimes 	if ((fd == -1) || !is_disk) {
53458f0484fSRodney W. Grimes 		PAGE_INIT(p);
53558f0484fSRodney W. Grimes 		return (0);
53658f0484fSRodney W. Grimes 	}
53758f0484fSRodney W. Grimes 	if (is_bucket)
53858f0484fSRodney W. Grimes 		page = BUCKET_TO_PAGE(bucket);
53958f0484fSRodney W. Grimes 	else
54058f0484fSRodney W. Grimes 		page = OADDR_TO_PAGE(bucket);
54158f0484fSRodney W. Grimes 	if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
5429233c4d9SJason Evans 	    ((rsize = _read(fd, p, size)) == -1))
54358f0484fSRodney W. Grimes 		return (-1);
544f1e396bcSPaul Traina 	bp = (u_int16_t *)p;
54558f0484fSRodney W. Grimes 	if (!rsize)
54658f0484fSRodney W. Grimes 		bp[0] = 0;	/* We hit the EOF, so initialize a new page */
54758f0484fSRodney W. Grimes 	else
54858f0484fSRodney W. Grimes 		if (rsize != size) {
54958f0484fSRodney W. Grimes 			errno = EFTYPE;
55058f0484fSRodney W. Grimes 			return (-1);
55158f0484fSRodney W. Grimes 		}
55258f0484fSRodney W. Grimes 	if (!is_bitmap && !bp[0]) {
55358f0484fSRodney W. Grimes 		PAGE_INIT(p);
55458f0484fSRodney W. Grimes 	} else
55558f0484fSRodney W. Grimes 		if (hashp->LORDER != BYTE_ORDER) {
55658f0484fSRodney W. Grimes 			register int i, max;
55758f0484fSRodney W. Grimes 
55858f0484fSRodney W. Grimes 			if (is_bitmap) {
55958f0484fSRodney W. Grimes 				max = hashp->BSIZE >> 2; /* divide by 4 */
56058f0484fSRodney W. Grimes 				for (i = 0; i < max; i++)
561f1e396bcSPaul Traina 					M_32_SWAP(((int *)p)[i]);
56258f0484fSRodney W. Grimes 			} else {
56358f0484fSRodney W. Grimes 				M_16_SWAP(bp[0]);
56458f0484fSRodney W. Grimes 				max = bp[0] + 2;
56558f0484fSRodney W. Grimes 				for (i = 1; i <= max; i++)
56658f0484fSRodney W. Grimes 					M_16_SWAP(bp[i]);
56758f0484fSRodney W. Grimes 			}
56858f0484fSRodney W. Grimes 		}
56958f0484fSRodney W. Grimes 	return (0);
57058f0484fSRodney W. Grimes }
57158f0484fSRodney W. Grimes 
57258f0484fSRodney W. Grimes /*
57358f0484fSRodney W. Grimes  * Write page p to disk
57458f0484fSRodney W. Grimes  *
57558f0484fSRodney W. Grimes  * Returns:
57658f0484fSRodney W. Grimes  *	 0 ==> OK
57758f0484fSRodney W. Grimes  *	-1 ==>failure
57858f0484fSRodney W. Grimes  */
57958f0484fSRodney W. Grimes extern int
58058f0484fSRodney W. Grimes __put_page(hashp, p, bucket, is_bucket, is_bitmap)
58158f0484fSRodney W. Grimes 	HTAB *hashp;
58258f0484fSRodney W. Grimes 	char *p;
583f1e396bcSPaul Traina 	u_int32_t bucket;
58458f0484fSRodney W. Grimes 	int is_bucket, is_bitmap;
58558f0484fSRodney W. Grimes {
58658f0484fSRodney W. Grimes 	register int fd, page, size;
58758f0484fSRodney W. Grimes 	int wsize;
58858f0484fSRodney W. Grimes 
58958f0484fSRodney W. Grimes 	size = hashp->BSIZE;
59058f0484fSRodney W. Grimes 	if ((hashp->fp == -1) && open_temp(hashp))
59158f0484fSRodney W. Grimes 		return (-1);
59258f0484fSRodney W. Grimes 	fd = hashp->fp;
59358f0484fSRodney W. Grimes 
59458f0484fSRodney W. Grimes 	if (hashp->LORDER != BYTE_ORDER) {
59558f0484fSRodney W. Grimes 		register int i;
59658f0484fSRodney W. Grimes 		register int max;
59758f0484fSRodney W. Grimes 
59858f0484fSRodney W. Grimes 		if (is_bitmap) {
59958f0484fSRodney W. Grimes 			max = hashp->BSIZE >> 2;	/* divide by 4 */
60058f0484fSRodney W. Grimes 			for (i = 0; i < max; i++)
601f1e396bcSPaul Traina 				M_32_SWAP(((int *)p)[i]);
60258f0484fSRodney W. Grimes 		} else {
603f1e396bcSPaul Traina 			max = ((u_int16_t *)p)[0] + 2;
60458f0484fSRodney W. Grimes 			for (i = 0; i <= max; i++)
605f1e396bcSPaul Traina 				M_16_SWAP(((u_int16_t *)p)[i]);
60658f0484fSRodney W. Grimes 		}
60758f0484fSRodney W. Grimes 	}
60858f0484fSRodney W. Grimes 	if (is_bucket)
60958f0484fSRodney W. Grimes 		page = BUCKET_TO_PAGE(bucket);
61058f0484fSRodney W. Grimes 	else
61158f0484fSRodney W. Grimes 		page = OADDR_TO_PAGE(bucket);
61258f0484fSRodney W. Grimes 	if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) ||
6139233c4d9SJason Evans 	    ((wsize = _write(fd, p, size)) == -1))
61458f0484fSRodney W. Grimes 		/* Errno is set */
61558f0484fSRodney W. Grimes 		return (-1);
61658f0484fSRodney W. Grimes 	if (wsize != size) {
61758f0484fSRodney W. Grimes 		errno = EFTYPE;
61858f0484fSRodney W. Grimes 		return (-1);
61958f0484fSRodney W. Grimes 	}
62058f0484fSRodney W. Grimes 	return (0);
62158f0484fSRodney W. Grimes }
62258f0484fSRodney W. Grimes 
62358f0484fSRodney W. Grimes #define BYTE_MASK	((1 << INT_BYTE_SHIFT) -1)
62458f0484fSRodney W. Grimes /*
62558f0484fSRodney W. Grimes  * Initialize a new bitmap page.  Bitmap pages are left in memory
62658f0484fSRodney W. Grimes  * once they are read in.
62758f0484fSRodney W. Grimes  */
62858f0484fSRodney W. Grimes extern int
629f1e396bcSPaul Traina __ibitmap(hashp, pnum, nbits, ndx)
63058f0484fSRodney W. Grimes 	HTAB *hashp;
63158f0484fSRodney W. Grimes 	int pnum, nbits, ndx;
63258f0484fSRodney W. Grimes {
633f1e396bcSPaul Traina 	u_int32_t *ip;
63458f0484fSRodney W. Grimes 	int clearbytes, clearints;
63558f0484fSRodney W. Grimes 
636f1e396bcSPaul Traina 	if ((ip = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
63758f0484fSRodney W. Grimes 		return (1);
63858f0484fSRodney W. Grimes 	hashp->nmaps++;
63958f0484fSRodney W. Grimes 	clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
64058f0484fSRodney W. Grimes 	clearbytes = clearints << INT_TO_BYTE;
64158f0484fSRodney W. Grimes 	(void)memset((char *)ip, 0, clearbytes);
64258f0484fSRodney W. Grimes 	(void)memset(((char *)ip) + clearbytes, 0xFF,
64358f0484fSRodney W. Grimes 	    hashp->BSIZE - clearbytes);
64458f0484fSRodney W. Grimes 	ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
64558f0484fSRodney W. Grimes 	SETBIT(ip, 0);
646f1e396bcSPaul Traina 	hashp->BITMAPS[ndx] = (u_int16_t)pnum;
64758f0484fSRodney W. Grimes 	hashp->mapp[ndx] = ip;
64858f0484fSRodney W. Grimes 	return (0);
64958f0484fSRodney W. Grimes }
65058f0484fSRodney W. Grimes 
651f1e396bcSPaul Traina static u_int32_t
65258f0484fSRodney W. Grimes first_free(map)
653f1e396bcSPaul Traina 	u_int32_t map;
65458f0484fSRodney W. Grimes {
655f1e396bcSPaul Traina 	register u_int32_t i, mask;
65658f0484fSRodney W. Grimes 
65758f0484fSRodney W. Grimes 	mask = 0x1;
65858f0484fSRodney W. Grimes 	for (i = 0; i < BITS_PER_MAP; i++) {
65958f0484fSRodney W. Grimes 		if (!(mask & map))
66058f0484fSRodney W. Grimes 			return (i);
66158f0484fSRodney W. Grimes 		mask = mask << 1;
66258f0484fSRodney W. Grimes 	}
66358f0484fSRodney W. Grimes 	return (i);
66458f0484fSRodney W. Grimes }
66558f0484fSRodney W. Grimes 
666f1e396bcSPaul Traina static u_int16_t
66758f0484fSRodney W. Grimes overflow_page(hashp)
66858f0484fSRodney W. Grimes 	HTAB *hashp;
66958f0484fSRodney W. Grimes {
670f1e396bcSPaul Traina 	register u_int32_t *freep;
67158f0484fSRodney W. Grimes 	register int max_free, offset, splitnum;
672f1e396bcSPaul Traina 	u_int16_t addr;
67358f0484fSRodney W. Grimes 	int bit, first_page, free_bit, free_page, i, in_use_bits, j;
67458f0484fSRodney W. Grimes #ifdef DEBUG2
67558f0484fSRodney W. Grimes 	int tmp1, tmp2;
67658f0484fSRodney W. Grimes #endif
67758f0484fSRodney W. Grimes 	splitnum = hashp->OVFL_POINT;
67858f0484fSRodney W. Grimes 	max_free = hashp->SPARES[splitnum];
67958f0484fSRodney W. Grimes 
68058f0484fSRodney W. Grimes 	free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
68158f0484fSRodney W. Grimes 	free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
68258f0484fSRodney W. Grimes 
68358f0484fSRodney W. Grimes 	/* Look through all the free maps to find the first free block */
68458f0484fSRodney W. Grimes 	first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
68558f0484fSRodney W. Grimes 	for ( i = first_page; i <= free_page; i++ ) {
686f1e396bcSPaul Traina 		if (!(freep = (u_int32_t *)hashp->mapp[i]) &&
68758f0484fSRodney W. Grimes 		    !(freep = fetch_bitmap(hashp, i)))
688f1e396bcSPaul Traina 			return (0);
68958f0484fSRodney W. Grimes 		if (i == free_page)
69058f0484fSRodney W. Grimes 			in_use_bits = free_bit;
69158f0484fSRodney W. Grimes 		else
69258f0484fSRodney W. Grimes 			in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
69358f0484fSRodney W. Grimes 
69458f0484fSRodney W. Grimes 		if (i == first_page) {
69558f0484fSRodney W. Grimes 			bit = hashp->LAST_FREED &
69658f0484fSRodney W. Grimes 			    ((hashp->BSIZE << BYTE_SHIFT) - 1);
69758f0484fSRodney W. Grimes 			j = bit / BITS_PER_MAP;
69858f0484fSRodney W. Grimes 			bit = bit & ~(BITS_PER_MAP - 1);
69958f0484fSRodney W. Grimes 		} else {
70058f0484fSRodney W. Grimes 			bit = 0;
70158f0484fSRodney W. Grimes 			j = 0;
70258f0484fSRodney W. Grimes 		}
70358f0484fSRodney W. Grimes 		for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
70458f0484fSRodney W. Grimes 			if (freep[j] != ALL_SET)
70558f0484fSRodney W. Grimes 				goto found;
70658f0484fSRodney W. Grimes 	}
70758f0484fSRodney W. Grimes 
70858f0484fSRodney W. Grimes 	/* No Free Page Found */
70958f0484fSRodney W. Grimes 	hashp->LAST_FREED = hashp->SPARES[splitnum];
71058f0484fSRodney W. Grimes 	hashp->SPARES[splitnum]++;
71158f0484fSRodney W. Grimes 	offset = hashp->SPARES[splitnum] -
71258f0484fSRodney W. Grimes 	    (splitnum ? hashp->SPARES[splitnum - 1] : 0);
71358f0484fSRodney W. Grimes 
71458f0484fSRodney W. Grimes #define	OVMSG	"HASH: Out of overflow pages.  Increase page size\n"
71558f0484fSRodney W. Grimes 	if (offset > SPLITMASK) {
71658f0484fSRodney W. Grimes 		if (++splitnum >= NCACHED) {
7179233c4d9SJason Evans 			(void)_write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
718f1e396bcSPaul Traina 			return (0);
71958f0484fSRodney W. Grimes 		}
72058f0484fSRodney W. Grimes 		hashp->OVFL_POINT = splitnum;
72158f0484fSRodney W. Grimes 		hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
72258f0484fSRodney W. Grimes 		hashp->SPARES[splitnum-1]--;
72358f0484fSRodney W. Grimes 		offset = 1;
72458f0484fSRodney W. Grimes 	}
72558f0484fSRodney W. Grimes 
72658f0484fSRodney W. Grimes 	/* Check if we need to allocate a new bitmap page */
72758f0484fSRodney W. Grimes 	if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
72858f0484fSRodney W. Grimes 		free_page++;
72958f0484fSRodney W. Grimes 		if (free_page >= NCACHED) {
7309233c4d9SJason Evans 			(void)_write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
731f1e396bcSPaul Traina 			return (0);
73258f0484fSRodney W. Grimes 		}
73358f0484fSRodney W. Grimes 		/*
73458f0484fSRodney W. Grimes 		 * This is tricky.  The 1 indicates that you want the new page
73558f0484fSRodney W. Grimes 		 * allocated with 1 clear bit.  Actually, you are going to
73658f0484fSRodney W. Grimes 		 * allocate 2 pages from this map.  The first is going to be
73758f0484fSRodney W. Grimes 		 * the map page, the second is the overflow page we were
73858f0484fSRodney W. Grimes 		 * looking for.  The init_bitmap routine automatically, sets
73958f0484fSRodney W. Grimes 		 * the first bit of itself to indicate that the bitmap itself
74058f0484fSRodney W. Grimes 		 * is in use.  We would explicitly set the second bit, but
74158f0484fSRodney W. Grimes 		 * don't have to if we tell init_bitmap not to leave it clear
74258f0484fSRodney W. Grimes 		 * in the first place.
74358f0484fSRodney W. Grimes 		 */
744f1e396bcSPaul Traina 		if (__ibitmap(hashp,
745f1e396bcSPaul Traina 		    (int)OADDR_OF(splitnum, offset), 1, free_page))
746f1e396bcSPaul Traina 			return (0);
74758f0484fSRodney W. Grimes 		hashp->SPARES[splitnum]++;
74858f0484fSRodney W. Grimes #ifdef DEBUG2
74958f0484fSRodney W. Grimes 		free_bit = 2;
75058f0484fSRodney W. Grimes #endif
75158f0484fSRodney W. Grimes 		offset++;
75258f0484fSRodney W. Grimes 		if (offset > SPLITMASK) {
75358f0484fSRodney W. Grimes 			if (++splitnum >= NCACHED) {
7549233c4d9SJason Evans 				(void)_write(STDERR_FILENO, OVMSG,
75558f0484fSRodney W. Grimes 				    sizeof(OVMSG) - 1);
756f1e396bcSPaul Traina 				return (0);
75758f0484fSRodney W. Grimes 			}
75858f0484fSRodney W. Grimes 			hashp->OVFL_POINT = splitnum;
75958f0484fSRodney W. Grimes 			hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
76058f0484fSRodney W. Grimes 			hashp->SPARES[splitnum-1]--;
76158f0484fSRodney W. Grimes 			offset = 0;
76258f0484fSRodney W. Grimes 		}
76358f0484fSRodney W. Grimes 	} else {
76458f0484fSRodney W. Grimes 		/*
76558f0484fSRodney W. Grimes 		 * Free_bit addresses the last used bit.  Bump it to address
76658f0484fSRodney W. Grimes 		 * the first available bit.
76758f0484fSRodney W. Grimes 		 */
76858f0484fSRodney W. Grimes 		free_bit++;
76958f0484fSRodney W. Grimes 		SETBIT(freep, free_bit);
77058f0484fSRodney W. Grimes 	}
77158f0484fSRodney W. Grimes 
77258f0484fSRodney W. Grimes 	/* Calculate address of the new overflow page */
77358f0484fSRodney W. Grimes 	addr = OADDR_OF(splitnum, offset);
77458f0484fSRodney W. Grimes #ifdef DEBUG2
77558f0484fSRodney W. Grimes 	(void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
77658f0484fSRodney W. Grimes 	    addr, free_bit, free_page);
77758f0484fSRodney W. Grimes #endif
77858f0484fSRodney W. Grimes 	return (addr);
77958f0484fSRodney W. Grimes 
78058f0484fSRodney W. Grimes found:
78158f0484fSRodney W. Grimes 	bit = bit + first_free(freep[j]);
78258f0484fSRodney W. Grimes 	SETBIT(freep, bit);
78358f0484fSRodney W. Grimes #ifdef DEBUG2
78458f0484fSRodney W. Grimes 	tmp1 = bit;
78558f0484fSRodney W. Grimes 	tmp2 = i;
78658f0484fSRodney W. Grimes #endif
78758f0484fSRodney W. Grimes 	/*
78858f0484fSRodney W. Grimes 	 * Bits are addressed starting with 0, but overflow pages are addressed
78958f0484fSRodney W. Grimes 	 * beginning at 1. Bit is a bit addressnumber, so we need to increment
79058f0484fSRodney W. Grimes 	 * it to convert it to a page number.
79158f0484fSRodney W. Grimes 	 */
79258f0484fSRodney W. Grimes 	bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
79358f0484fSRodney W. Grimes 	if (bit >= hashp->LAST_FREED)
79458f0484fSRodney W. Grimes 		hashp->LAST_FREED = bit - 1;
79558f0484fSRodney W. Grimes 
79658f0484fSRodney W. Grimes 	/* Calculate the split number for this page */
79758f0484fSRodney W. Grimes 	for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++);
79858f0484fSRodney W. Grimes 	offset = (i ? bit - hashp->SPARES[i - 1] : bit);
79958f0484fSRodney W. Grimes 	if (offset >= SPLITMASK)
800f1e396bcSPaul Traina 		return (0);	/* Out of overflow pages */
80158f0484fSRodney W. Grimes 	addr = OADDR_OF(i, offset);
80258f0484fSRodney W. Grimes #ifdef DEBUG2
80358f0484fSRodney W. Grimes 	(void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
80458f0484fSRodney W. Grimes 	    addr, tmp1, tmp2);
80558f0484fSRodney W. Grimes #endif
80658f0484fSRodney W. Grimes 
80758f0484fSRodney W. Grimes 	/* Allocate and return the overflow page */
80858f0484fSRodney W. Grimes 	return (addr);
80958f0484fSRodney W. Grimes }
81058f0484fSRodney W. Grimes 
81158f0484fSRodney W. Grimes /*
81258f0484fSRodney W. Grimes  * Mark this overflow page as free.
81358f0484fSRodney W. Grimes  */
81458f0484fSRodney W. Grimes extern void
81558f0484fSRodney W. Grimes __free_ovflpage(hashp, obufp)
81658f0484fSRodney W. Grimes 	HTAB *hashp;
81758f0484fSRodney W. Grimes 	BUFHEAD *obufp;
81858f0484fSRodney W. Grimes {
819f1e396bcSPaul Traina 	register u_int16_t addr;
820f1e396bcSPaul Traina 	u_int32_t *freep;
82158f0484fSRodney W. Grimes 	int bit_address, free_page, free_bit;
822f1e396bcSPaul Traina 	u_int16_t ndx;
82358f0484fSRodney W. Grimes 
82458f0484fSRodney W. Grimes 	addr = obufp->addr;
82558f0484fSRodney W. Grimes #ifdef DEBUG1
82658f0484fSRodney W. Grimes 	(void)fprintf(stderr, "Freeing %d\n", addr);
82758f0484fSRodney W. Grimes #endif
828f1e396bcSPaul Traina 	ndx = (((u_int16_t)addr) >> SPLITSHIFT);
82958f0484fSRodney W. Grimes 	bit_address =
83058f0484fSRodney W. Grimes 	    (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
83158f0484fSRodney W. Grimes 	 if (bit_address < hashp->LAST_FREED)
83258f0484fSRodney W. Grimes 		hashp->LAST_FREED = bit_address;
83358f0484fSRodney W. Grimes 	free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
83458f0484fSRodney W. Grimes 	free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
83558f0484fSRodney W. Grimes 
83658f0484fSRodney W. Grimes 	if (!(freep = hashp->mapp[free_page]))
83758f0484fSRodney W. Grimes 		freep = fetch_bitmap(hashp, free_page);
83858f0484fSRodney W. Grimes #ifdef DEBUG
83958f0484fSRodney W. Grimes 	/*
84058f0484fSRodney W. Grimes 	 * This had better never happen.  It means we tried to read a bitmap
84158f0484fSRodney W. Grimes 	 * that has already had overflow pages allocated off it, and we
84258f0484fSRodney W. Grimes 	 * failed to read it from the file.
84358f0484fSRodney W. Grimes 	 */
84458f0484fSRodney W. Grimes 	if (!freep)
84558f0484fSRodney W. Grimes 		assert(0);
84658f0484fSRodney W. Grimes #endif
84758f0484fSRodney W. Grimes 	CLRBIT(freep, free_bit);
84858f0484fSRodney W. Grimes #ifdef DEBUG2
84958f0484fSRodney W. Grimes 	(void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
85058f0484fSRodney W. Grimes 	    obufp->addr, free_bit, free_page);
85158f0484fSRodney W. Grimes #endif
85258f0484fSRodney W. Grimes 	__reclaim_buf(hashp, obufp);
85358f0484fSRodney W. Grimes }
85458f0484fSRodney W. Grimes 
85558f0484fSRodney W. Grimes /*
85658f0484fSRodney W. Grimes  * Returns:
85758f0484fSRodney W. Grimes  *	 0 success
85858f0484fSRodney W. Grimes  *	-1 failure
85958f0484fSRodney W. Grimes  */
86058f0484fSRodney W. Grimes static int
86158f0484fSRodney W. Grimes open_temp(hashp)
86258f0484fSRodney W. Grimes 	HTAB *hashp;
86358f0484fSRodney W. Grimes {
86458f0484fSRodney W. Grimes 	sigset_t set, oset;
86558f0484fSRodney W. Grimes 	static char namestr[] = "_hashXXXXXX";
86658f0484fSRodney W. Grimes 
86758f0484fSRodney W. Grimes 	/* Block signals; make sure file goes away at process exit. */
86858f0484fSRodney W. Grimes 	(void)sigfillset(&set);
86958f0484fSRodney W. Grimes 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
87058f0484fSRodney W. Grimes 	if ((hashp->fp = mkstemp(namestr)) != -1) {
87158f0484fSRodney W. Grimes 		(void)unlink(namestr);
8729233c4d9SJason Evans 		(void)_fcntl(hashp->fp, F_SETFD, 1);
87358f0484fSRodney W. Grimes 	}
87458f0484fSRodney W. Grimes 	(void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
87558f0484fSRodney W. Grimes 	return (hashp->fp != -1 ? 0 : -1);
87658f0484fSRodney W. Grimes }
87758f0484fSRodney W. Grimes 
87858f0484fSRodney W. Grimes /*
87958f0484fSRodney W. Grimes  * We have to know that the key will fit, but the last entry on the page is
88058f0484fSRodney W. Grimes  * an overflow pair, so we need to shift things.
88158f0484fSRodney W. Grimes  */
88258f0484fSRodney W. Grimes static void
88358f0484fSRodney W. Grimes squeeze_key(sp, key, val)
884f1e396bcSPaul Traina 	u_int16_t *sp;
88558f0484fSRodney W. Grimes 	const DBT *key, *val;
88658f0484fSRodney W. Grimes {
88758f0484fSRodney W. Grimes 	register char *p;
888f1e396bcSPaul Traina 	u_int16_t free_space, n, off, pageno;
88958f0484fSRodney W. Grimes 
89058f0484fSRodney W. Grimes 	p = (char *)sp;
89158f0484fSRodney W. Grimes 	n = sp[0];
89258f0484fSRodney W. Grimes 	free_space = FREESPACE(sp);
89358f0484fSRodney W. Grimes 	off = OFFSET(sp);
89458f0484fSRodney W. Grimes 
89558f0484fSRodney W. Grimes 	pageno = sp[n - 1];
89658f0484fSRodney W. Grimes 	off -= key->size;
89758f0484fSRodney W. Grimes 	sp[n - 1] = off;
89858f0484fSRodney W. Grimes 	memmove(p + off, key->data, key->size);
89958f0484fSRodney W. Grimes 	off -= val->size;
90058f0484fSRodney W. Grimes 	sp[n] = off;
90158f0484fSRodney W. Grimes 	memmove(p + off, val->data, val->size);
90258f0484fSRodney W. Grimes 	sp[0] = n + 2;
90358f0484fSRodney W. Grimes 	sp[n + 1] = pageno;
90458f0484fSRodney W. Grimes 	sp[n + 2] = OVFLPAGE;
90558f0484fSRodney W. Grimes 	FREESPACE(sp) = free_space - PAIRSIZE(key, val);
90658f0484fSRodney W. Grimes 	OFFSET(sp) = off;
90758f0484fSRodney W. Grimes }
90858f0484fSRodney W. Grimes 
909f1e396bcSPaul Traina static u_int32_t *
91058f0484fSRodney W. Grimes fetch_bitmap(hashp, ndx)
91158f0484fSRodney W. Grimes 	HTAB *hashp;
91258f0484fSRodney W. Grimes 	int ndx;
91358f0484fSRodney W. Grimes {
91458f0484fSRodney W. Grimes 	if (ndx >= hashp->nmaps)
91558f0484fSRodney W. Grimes 		return (NULL);
916f1e396bcSPaul Traina 	if ((hashp->mapp[ndx] = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
91758f0484fSRodney W. Grimes 		return (NULL);
91858f0484fSRodney W. Grimes 	if (__get_page(hashp,
91958f0484fSRodney W. Grimes 	    (char *)hashp->mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
92058f0484fSRodney W. Grimes 		free(hashp->mapp[ndx]);
92158f0484fSRodney W. Grimes 		return (NULL);
92258f0484fSRodney W. Grimes 	}
92358f0484fSRodney W. Grimes 	return (hashp->mapp[ndx]);
92458f0484fSRodney W. Grimes }
92558f0484fSRodney W. Grimes 
92658f0484fSRodney W. Grimes #ifdef DEBUG4
92758f0484fSRodney W. Grimes int
92858f0484fSRodney W. Grimes print_chain(addr)
92958f0484fSRodney W. Grimes 	int addr;
93058f0484fSRodney W. Grimes {
93158f0484fSRodney W. Grimes 	BUFHEAD *bufp;
93258f0484fSRodney W. Grimes 	short *bp, oaddr;
93358f0484fSRodney W. Grimes 
93458f0484fSRodney W. Grimes 	(void)fprintf(stderr, "%d ", addr);
93558f0484fSRodney W. Grimes 	bufp = __get_buf(hashp, addr, NULL, 0);
93658f0484fSRodney W. Grimes 	bp = (short *)bufp->page;
93758f0484fSRodney W. Grimes 	while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
93858f0484fSRodney W. Grimes 		((bp[0] > 2) && bp[2] < REAL_KEY))) {
93958f0484fSRodney W. Grimes 		oaddr = bp[bp[0] - 1];
94058f0484fSRodney W. Grimes 		(void)fprintf(stderr, "%d ", (int)oaddr);
94158f0484fSRodney W. Grimes 		bufp = __get_buf(hashp, (int)oaddr, bufp, 0);
94258f0484fSRodney W. Grimes 		bp = (short *)bufp->page;
94358f0484fSRodney W. Grimes 	}
94458f0484fSRodney W. Grimes 	(void)fprintf(stderr, "\n");
94558f0484fSRodney W. Grimes }
94658f0484fSRodney W. Grimes #endif
947