146372Sbostic /*- 262489Sbostic * Copyright (c) 1990, 1993 362489Sbostic * The Regents of the University of California. All rights reserved. 446372Sbostic * 546372Sbostic * This code is derived from software contributed to Berkeley by 646372Sbostic * Margo Seltzer. 746372Sbostic * 846372Sbostic * %sccs.include.redist.c% 946372Sbostic */ 1046372Sbostic 1146372Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*66193Sbostic static char sccsid[] = "@(#)hash_page.c 8.3 (Berkeley) 02/21/94"; 1346372Sbostic #endif /* LIBC_SCCS and not lint */ 1446372Sbostic 1550997Sbostic /* 1650997Sbostic * PACKAGE: hashing 1750997Sbostic * 1850997Sbostic * DESCRIPTION: 1950997Sbostic * Page manipulation for hashing package. 2050997Sbostic * 2150997Sbostic * ROUTINES: 2250997Sbostic * 2350997Sbostic * External 2450997Sbostic * __get_page 2550997Sbostic * __add_ovflpage 2650997Sbostic * Internal 2750997Sbostic * overflow_page 2850997Sbostic * open_temp 2950997Sbostic */ 3046372Sbostic 3157586Sbostic #include <sys/types.h> 3257586Sbostic 3357586Sbostic #include <errno.h> 3446562Sbostic #include <fcntl.h> 3546416Sbostic #include <signal.h> 3646372Sbostic #include <stdio.h> 3746562Sbostic #include <stdlib.h> 3846562Sbostic #include <string.h> 3946500Sbostic #include <unistd.h> 4050997Sbostic #ifdef DEBUG 4150997Sbostic #include <assert.h> 4250997Sbostic #endif 4357586Sbostic 4457932Sbostic #include <db.h> 4546372Sbostic #include "hash.h" 4646372Sbostic #include "page.h" 4750997Sbostic #include "extern.h" 4846372Sbostic 4957586Sbostic static u_long *fetch_bitmap __P((HTAB *, int)); 5051055Sbostic static u_long first_free __P((u_long)); 5157586Sbostic static int open_temp __P((HTAB *)); 5257586Sbostic static u_short overflow_page __P((HTAB *)); 5351055Sbostic static void putpair __P((char *, const DBT *, const DBT *)); 5451055Sbostic static void squeeze_key __P((u_short *, const DBT *, const DBT *)); 5557586Sbostic static int ugly_split 5657586Sbostic __P((HTAB *, u_int, BUFHEAD *, BUFHEAD *, int, int)); 5746372Sbostic 5850997Sbostic #define PAGE_INIT(P) { \ 5950997Sbostic ((u_short *)(P))[0] = 0; \ 6050997Sbostic ((u_short *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_short); \ 6150997Sbostic ((u_short *)(P))[2] = hashp->BSIZE; \ 6246372Sbostic } 6346372Sbostic 6446372Sbostic /* 6550997Sbostic * This is called AFTER we have verified that there is room on the page for 6650997Sbostic * the pair (PAIRFITS has returned true) so we go right ahead and start moving 6750997Sbostic * stuff on. 6850997Sbostic */ 6946372Sbostic static void 7046372Sbostic putpair(p, key, val) 7150997Sbostic char *p; 7250997Sbostic const DBT *key, *val; 7346372Sbostic { 7450997Sbostic register u_short *bp, n, off; 7546372Sbostic 7650997Sbostic bp = (u_short *)p; 7750997Sbostic 7850997Sbostic /* Enter the key first. */ 7946372Sbostic n = bp[0]; 8046372Sbostic 8146372Sbostic off = OFFSET(bp) - key->size; 8258016Sbostic memmove(p + off, key->data, key->size); 8346372Sbostic bp[++n] = off; 8446372Sbostic 8550997Sbostic /* Now the data. */ 8646372Sbostic off -= val->size; 8758016Sbostic memmove(p + off, val->data, val->size); 8846372Sbostic bp[++n] = off; 8946372Sbostic 9050997Sbostic /* Adjust page info. */ 9146372Sbostic bp[0] = n; 9250997Sbostic bp[n + 1] = off - ((n + 3) * sizeof(u_short)); 9350997Sbostic bp[n + 2] = off; 9446372Sbostic } 9550997Sbostic 9646372Sbostic /* 9750997Sbostic * Returns: 9850997Sbostic * 0 OK 9950997Sbostic * -1 error 10050997Sbostic */ 10146372Sbostic extern int 10257586Sbostic __delpair(hashp, bufp, ndx) 10357586Sbostic HTAB *hashp; 10450997Sbostic BUFHEAD *bufp; 10550997Sbostic register int ndx; 10646372Sbostic { 10750997Sbostic register u_short *bp, newoff; 10850997Sbostic register int n; 10946372Sbostic u_short pairlen; 11046372Sbostic 11150997Sbostic bp = (u_short *)bufp->page; 11250997Sbostic n = bp[0]; 11346372Sbostic 11450997Sbostic if (bp[ndx + 1] < REAL_KEY) 11557586Sbostic return (__big_delete(hashp, bufp)); 11650997Sbostic if (ndx != 1) 11750997Sbostic newoff = bp[ndx - 1]; 11850997Sbostic else 11950997Sbostic newoff = hashp->BSIZE; 12050997Sbostic pairlen = newoff - bp[ndx + 1]; 12150997Sbostic 12250997Sbostic if (ndx != (n - 1)) { 12346372Sbostic /* Hard Case -- need to shuffle keys */ 12446372Sbostic register int i; 12546372Sbostic register char *src = bufp->page + (int)OFFSET(bp); 12646372Sbostic register char *dst = src + (int)pairlen; 12758016Sbostic memmove(dst, src, bp[ndx + 1] - OFFSET(bp)); 12846372Sbostic 12946372Sbostic /* Now adjust the pointers */ 13050997Sbostic for (i = ndx + 2; i <= n; i += 2) { 13150997Sbostic if (bp[i + 1] == OVFLPAGE) { 13250997Sbostic bp[i - 2] = bp[i]; 13350997Sbostic bp[i - 1] = bp[i + 1]; 13450997Sbostic } else { 13550997Sbostic bp[i - 2] = bp[i] + pairlen; 13650997Sbostic bp[i - 1] = bp[i + 1] + pairlen; 13750997Sbostic } 13846372Sbostic } 13946372Sbostic } 14046372Sbostic /* Finally adjust the page data */ 14146372Sbostic bp[n] = OFFSET(bp) + pairlen; 14250997Sbostic bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(u_short); 14350997Sbostic bp[0] = n - 2; 14446372Sbostic hashp->NKEYS--; 14546372Sbostic 14646372Sbostic bufp->flags |= BUF_MOD; 14746372Sbostic return (0); 14846372Sbostic } 14946372Sbostic /* 15050997Sbostic * Returns: 15150997Sbostic * 0 ==> OK 15250997Sbostic * -1 ==> Error 15350997Sbostic */ 15446372Sbostic extern int 15557586Sbostic __split_page(hashp, obucket, nbucket) 15657586Sbostic HTAB *hashp; 15750997Sbostic u_int obucket, nbucket; 15846372Sbostic { 15950997Sbostic register BUFHEAD *new_bufp, *old_bufp; 16046372Sbostic register u_short *ino; 16150997Sbostic register char *np; 16250997Sbostic DBT key, val; 16350997Sbostic int n, ndx, retval; 16450997Sbostic u_short copyto, diff, off, moved; 16550997Sbostic char *op; 16646372Sbostic 16750997Sbostic copyto = (u_short)hashp->BSIZE; 16850997Sbostic off = (u_short)hashp->BSIZE; 16957586Sbostic old_bufp = __get_buf(hashp, obucket, NULL, 0); 17053870Sbostic if (old_bufp == NULL) 17153870Sbostic return (-1); 17257586Sbostic new_bufp = __get_buf(hashp, nbucket, NULL, 0); 17353870Sbostic if (new_bufp == NULL) 17453870Sbostic return (-1); 17546372Sbostic 17650997Sbostic old_bufp->flags |= (BUF_MOD | BUF_PIN); 17750997Sbostic new_bufp->flags |= (BUF_MOD | BUF_PIN); 17846372Sbostic 17946372Sbostic ino = (u_short *)(op = old_bufp->page); 18046372Sbostic np = new_bufp->page; 18146372Sbostic 18246372Sbostic moved = 0; 18346372Sbostic 18450997Sbostic for (n = 1, ndx = 1; n < ino[0]; n += 2) { 18550997Sbostic if (ino[n + 1] < REAL_KEY) { 18657586Sbostic retval = ugly_split(hashp, obucket, old_bufp, new_bufp, 18751055Sbostic (int)copyto, (int)moved); 18850997Sbostic old_bufp->flags &= ~BUF_PIN; 18950997Sbostic new_bufp->flags &= ~BUF_PIN; 19050997Sbostic return (retval); 19150997Sbostic 19246372Sbostic } 19350997Sbostic key.data = (u_char *)op + ino[n]; 19446372Sbostic key.size = off - ino[n]; 19546372Sbostic 19657586Sbostic if (__call_hash(hashp, key.data, key.size) == obucket) { 19750997Sbostic /* Don't switch page */ 19850997Sbostic diff = copyto - off; 19950997Sbostic if (diff) { 20050997Sbostic copyto = ino[n + 1] + diff; 20158016Sbostic memmove(op + copyto, op + ino[n + 1], 20250997Sbostic off - ino[n + 1]); 20350997Sbostic ino[ndx] = copyto + ino[n] - ino[n + 1]; 20450997Sbostic ino[ndx + 1] = copyto; 20550997Sbostic } else 20650997Sbostic copyto = ino[n + 1]; 20750997Sbostic ndx += 2; 20846372Sbostic } else { 20950997Sbostic /* Switch page */ 21050997Sbostic val.data = (u_char *)op + ino[n + 1]; 21150997Sbostic val.size = ino[n] - ino[n + 1]; 21250997Sbostic putpair(np, &key, &val); 21350997Sbostic moved += 2; 21446372Sbostic } 21546372Sbostic 21650997Sbostic off = ino[n + 1]; 21746372Sbostic } 21846372Sbostic 21946372Sbostic /* Now clean up the page */ 22046372Sbostic ino[0] -= moved; 22150997Sbostic FREESPACE(ino) = copyto - sizeof(u_short) * (ino[0] + 3); 22246372Sbostic OFFSET(ino) = copyto; 22346372Sbostic 22446372Sbostic #ifdef DEBUG3 22550997Sbostic (void)fprintf(stderr, "split %d/%d\n", 22650997Sbostic ((u_short *)np)[0] / 2, 22750997Sbostic ((u_short *)op)[0] / 2); 22846372Sbostic #endif 22946460Sbostic /* unpin both pages */ 23046460Sbostic old_bufp->flags &= ~BUF_PIN; 23146460Sbostic new_bufp->flags &= ~BUF_PIN; 23250997Sbostic return (0); 23346372Sbostic } 23450997Sbostic 23546372Sbostic /* 23650997Sbostic * Called when we encounter an overflow or big key/data page during split 23750997Sbostic * handling. This is special cased since we have to begin checking whether 23850997Sbostic * the key/data pairs fit on their respective pages and because we may need 23950997Sbostic * overflow pages for both the old and new pages. 24050997Sbostic * 24150997Sbostic * The first page might be a page with regular key/data pairs in which case 24250997Sbostic * we have a regular overflow condition and just need to go on to the next 24350997Sbostic * page or it might be a big key/data pair in which case we need to fix the 24450997Sbostic * big key/data pair. 24550997Sbostic * 24650997Sbostic * Returns: 24750997Sbostic * 0 ==> success 24850997Sbostic * -1 ==> failure 24950997Sbostic */ 25046372Sbostic static int 25157586Sbostic ugly_split(hashp, obucket, old_bufp, new_bufp, copyto, moved) 25257586Sbostic HTAB *hashp; 25350997Sbostic u_int obucket; /* Same as __split_page. */ 25450997Sbostic BUFHEAD *old_bufp, *new_bufp; 25550997Sbostic int copyto; /* First byte on page which contains key/data values. */ 25650997Sbostic int moved; /* Number of pairs moved to new page. */ 25746372Sbostic { 25850997Sbostic register BUFHEAD *bufp; /* Buffer header for ino */ 25950997Sbostic register u_short *ino; /* Page keys come off of */ 26050997Sbostic register u_short *np; /* New page */ 26150997Sbostic register u_short *op; /* Page keys go on to if they aren't moving */ 26246372Sbostic 26350997Sbostic BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */ 26450997Sbostic DBT key, val; 26550997Sbostic SPLIT_RETURN ret; 26651055Sbostic u_short n, off, ov_addr, scopyto; 26750997Sbostic char *cino; /* Character value of ino */ 26846372Sbostic 26950997Sbostic bufp = old_bufp; 27050997Sbostic ino = (u_short *)old_bufp->page; 27150997Sbostic np = (u_short *)new_bufp->page; 27250997Sbostic op = (u_short *)old_bufp->page; 27350997Sbostic last_bfp = NULL; 27450997Sbostic scopyto = (u_short)copyto; /* ANSI */ 27546372Sbostic 27650997Sbostic n = ino[0] - 1; 27750997Sbostic while (n < ino[0]) { 27850997Sbostic if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) { 27957586Sbostic if (__big_split(hashp, old_bufp, 28064445Sbostic new_bufp, bufp, bufp->addr, obucket, &ret)) 28150997Sbostic return (-1); 28250997Sbostic old_bufp = ret.oldp; 28350997Sbostic if (!old_bufp) 28450997Sbostic return (-1); 28550997Sbostic op = (u_short *)old_bufp->page; 28650997Sbostic new_bufp = ret.newp; 28750997Sbostic if (!new_bufp) 28850997Sbostic return (-1); 28950997Sbostic np = (u_short *)new_bufp->page; 29050997Sbostic bufp = ret.nextp; 29150997Sbostic if (!bufp) 29250997Sbostic return (0); 29350997Sbostic cino = (char *)bufp->page; 29450997Sbostic ino = (u_short *)cino; 29550997Sbostic last_bfp = ret.nextp; 29650997Sbostic } else if (ino[n + 1] == OVFLPAGE) { 29750997Sbostic ov_addr = ino[n]; 29850997Sbostic /* 29950997Sbostic * Fix up the old page -- the extra 2 are the fields 30050997Sbostic * which contained the overflow information. 30150997Sbostic */ 30250997Sbostic ino[0] -= (moved + 2); 30350997Sbostic FREESPACE(ino) = 30450997Sbostic scopyto - sizeof(u_short) * (ino[0] + 3); 30550997Sbostic OFFSET(ino) = scopyto; 30646372Sbostic 30757586Sbostic bufp = __get_buf(hashp, ov_addr, bufp, 0); 30850997Sbostic if (!bufp) 30950997Sbostic return (-1); 31046372Sbostic 31150997Sbostic ino = (u_short *)bufp->page; 31250997Sbostic n = 1; 31350997Sbostic scopyto = hashp->BSIZE; 31450997Sbostic moved = 0; 31546372Sbostic 31650997Sbostic if (last_bfp) 31757586Sbostic __free_ovflpage(hashp, last_bfp); 31850997Sbostic last_bfp = bufp; 31950997Sbostic } 32050997Sbostic /* Move regular sized pairs of there are any */ 32150997Sbostic off = hashp->BSIZE; 32250997Sbostic for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) { 32350997Sbostic cino = (char *)ino; 32450997Sbostic key.data = (u_char *)cino + ino[n]; 32550997Sbostic key.size = off - ino[n]; 32650997Sbostic val.data = (u_char *)cino + ino[n + 1]; 32750997Sbostic val.size = ino[n] - ino[n + 1]; 32850997Sbostic off = ino[n + 1]; 32946372Sbostic 33057586Sbostic if (__call_hash(hashp, key.data, key.size) == obucket) { 33150997Sbostic /* Keep on old page */ 33250997Sbostic if (PAIRFITS(op, (&key), (&val))) 33350997Sbostic putpair((char *)op, &key, &val); 33450997Sbostic else { 33557586Sbostic old_bufp = 33657586Sbostic __add_ovflpage(hashp, old_bufp); 33750997Sbostic if (!old_bufp) 33850997Sbostic return (-1); 33950997Sbostic op = (u_short *)old_bufp->page; 34050997Sbostic putpair((char *)op, &key, &val); 34150997Sbostic } 34250997Sbostic old_bufp->flags |= BUF_MOD; 34350997Sbostic } else { 34450997Sbostic /* Move to new page */ 34550997Sbostic if (PAIRFITS(np, (&key), (&val))) 34650997Sbostic putpair((char *)np, &key, &val); 34750997Sbostic else { 34857586Sbostic new_bufp = 34957586Sbostic __add_ovflpage(hashp, new_bufp); 35050997Sbostic if (!new_bufp) 35150997Sbostic return (-1); 35250997Sbostic np = (u_short *)new_bufp->page; 35350997Sbostic putpair((char *)np, &key, &val); 35450997Sbostic } 35550997Sbostic new_bufp->flags |= BUF_MOD; 35650997Sbostic } 35746372Sbostic } 35846372Sbostic } 35950997Sbostic if (last_bfp) 36057586Sbostic __free_ovflpage(hashp, last_bfp); 36150997Sbostic return (0); 36250997Sbostic } 36346372Sbostic 36446372Sbostic /* 36550997Sbostic * Add the given pair to the page 36650997Sbostic * 36750997Sbostic * Returns: 36850997Sbostic * 0 ==> OK 36950997Sbostic * 1 ==> failure 37050997Sbostic */ 37146372Sbostic extern int 37257586Sbostic __addel(hashp, bufp, key, val) 37357586Sbostic HTAB *hashp; 37450997Sbostic BUFHEAD *bufp; 37550997Sbostic const DBT *key, *val; 37646372Sbostic { 37750997Sbostic register u_short *bp, *sop; 37850997Sbostic int do_expand; 37946372Sbostic 38050997Sbostic bp = (u_short *)bufp->page; 38150997Sbostic do_expand = 0; 38264445Sbostic while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY)) 38350997Sbostic /* Exception case */ 38464445Sbostic if (bp[2] == FULL_KEY_DATA && bp[0] == 2) 38564445Sbostic /* This is the last page of a big key/data pair 38664445Sbostic and we need to add another page */ 38764445Sbostic break; 38864445Sbostic else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) { 38964445Sbostic bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0); 39050997Sbostic if (!bufp) 39150997Sbostic return (-1); 39250997Sbostic bp = (u_short *)bufp->page; 39350997Sbostic } else 39450997Sbostic /* Try to squeeze key on this page */ 39550997Sbostic if (FREESPACE(bp) > PAIRSIZE(key, val)) { 39650997Sbostic squeeze_key(bp, key, val); 39750997Sbostic return (0); 39850997Sbostic } else { 39957586Sbostic bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0); 40050997Sbostic if (!bufp) 40150997Sbostic return (-1); 40250997Sbostic bp = (u_short *)bufp->page; 40350997Sbostic } 40446372Sbostic 40550997Sbostic if (PAIRFITS(bp, key, val)) 40650997Sbostic putpair(bufp->page, key, val); 40750997Sbostic else { 40850997Sbostic do_expand = 1; 40957586Sbostic bufp = __add_ovflpage(hashp, bufp); 41050997Sbostic if (!bufp) 41150997Sbostic return (-1); 41250997Sbostic sop = (u_short *)bufp->page; 41346372Sbostic 41450997Sbostic if (PAIRFITS(sop, key, val)) 41550997Sbostic putpair((char *)sop, key, val); 41650997Sbostic else 41757586Sbostic if (__big_insert(hashp, bufp, key, val)) 41850997Sbostic return (-1); 41946372Sbostic } 42050997Sbostic bufp->flags |= BUF_MOD; 42150997Sbostic /* 42250997Sbostic * If the average number of keys per bucket exceeds the fill factor, 42350997Sbostic * expand the table. 42450997Sbostic */ 42550997Sbostic hashp->NKEYS++; 42650997Sbostic if (do_expand || 42750997Sbostic (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR)) 42857586Sbostic return (__expand_table(hashp)); 42950997Sbostic return (0); 43046372Sbostic } 43146372Sbostic 43246372Sbostic /* 43350997Sbostic * 43450997Sbostic * Returns: 43550997Sbostic * pointer on success 43650997Sbostic * NULL on error 43750997Sbostic */ 43846372Sbostic extern BUFHEAD * 43957586Sbostic __add_ovflpage(hashp, bufp) 44057586Sbostic HTAB *hashp; 44150997Sbostic BUFHEAD *bufp; 44246372Sbostic { 44350997Sbostic register u_short *sp; 44450997Sbostic u_short ndx, ovfl_num; 44546372Sbostic #ifdef DEBUG1 44650997Sbostic int tmp1, tmp2; 44746372Sbostic #endif 44850997Sbostic sp = (u_short *)bufp->page; 44960246Sbostic 45060246Sbostic /* Check if we are dynamically determining the fill factor */ 45160246Sbostic if (hashp->FFACTOR == DEF_FFACTOR) { 45260246Sbostic hashp->FFACTOR = sp[0] >> 1; 45360246Sbostic if (hashp->FFACTOR < MIN_FFACTOR) 45460246Sbostic hashp->FFACTOR = MIN_FFACTOR; 45560246Sbostic } 45650997Sbostic bufp->flags |= BUF_MOD; 45757586Sbostic ovfl_num = overflow_page(hashp); 45846372Sbostic #ifdef DEBUG1 45950997Sbostic tmp1 = bufp->addr; 46050997Sbostic tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0; 46146372Sbostic #endif 46257586Sbostic if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1))) 46350997Sbostic return (NULL); 46450997Sbostic bufp->ovfl->flags |= BUF_MOD; 46546372Sbostic #ifdef DEBUG1 46650997Sbostic (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n", 46750997Sbostic tmp1, tmp2, bufp->ovfl->addr); 46846372Sbostic #endif 46950997Sbostic ndx = sp[0]; 47050997Sbostic /* 47150997Sbostic * Since a pair is allocated on a page only if there's room to add 47250997Sbostic * an overflow page, we know that the OVFL information will fit on 47350997Sbostic * the page. 47450997Sbostic */ 47550997Sbostic sp[ndx + 4] = OFFSET(sp); 47650997Sbostic sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE; 47750997Sbostic sp[ndx + 1] = ovfl_num; 47850997Sbostic sp[ndx + 2] = OVFLPAGE; 47950997Sbostic sp[0] = ndx + 2; 48046372Sbostic #ifdef HASH_STATISTICS 48150997Sbostic hash_overflows++; 48246372Sbostic #endif 48350997Sbostic return (bufp->ovfl); 48446372Sbostic } 48546372Sbostic 48646372Sbostic /* 48750997Sbostic * Returns: 48850997Sbostic * 0 indicates SUCCESS 48950997Sbostic * -1 indicates FAILURE 49050997Sbostic */ 49150997Sbostic extern int 49257586Sbostic __get_page(hashp, p, bucket, is_bucket, is_disk, is_bitmap) 49357586Sbostic HTAB *hashp; 49450997Sbostic char *p; 49550997Sbostic u_int bucket; 49650997Sbostic int is_bucket, is_disk, is_bitmap; 49746372Sbostic { 49850997Sbostic register int fd, page, size; 49950997Sbostic int rsize; 50050997Sbostic u_short *bp; 50146372Sbostic 50250997Sbostic fd = hashp->fp; 50350997Sbostic size = hashp->BSIZE; 50446372Sbostic 50550997Sbostic if ((fd == -1) || !is_disk) { 50650997Sbostic PAGE_INIT(p); 50750997Sbostic return (0); 50850997Sbostic } 50950997Sbostic if (is_bucket) 51050997Sbostic page = BUCKET_TO_PAGE(bucket); 51150997Sbostic else 51250997Sbostic page = OADDR_TO_PAGE(bucket); 51355314Sbostic if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) || 51450997Sbostic ((rsize = read(fd, p, size)) == -1)) 51550997Sbostic return (-1); 51650997Sbostic bp = (u_short *)p; 51750997Sbostic if (!rsize) 51850997Sbostic bp[0] = 0; /* We hit the EOF, so initialize a new page */ 51950997Sbostic else 52050997Sbostic if (rsize != size) { 52150997Sbostic errno = EFTYPE; 52250997Sbostic return (-1); 52350997Sbostic } 52453403Sbostic if (!is_bitmap && !bp[0]) { 52550997Sbostic PAGE_INIT(p); 52650997Sbostic } else 52750997Sbostic if (hashp->LORDER != BYTE_ORDER) { 52850997Sbostic register int i, max; 52946372Sbostic 53050997Sbostic if (is_bitmap) { 53150997Sbostic max = hashp->BSIZE >> 2; /* divide by 4 */ 53250997Sbostic for (i = 0; i < max; i++) 533*66193Sbostic M_32_SWAP(((long *)p)[i]); 53450997Sbostic } else { 535*66193Sbostic M_16_SWAP(bp[0]); 53650997Sbostic max = bp[0] + 2; 53750997Sbostic for (i = 1; i <= max; i++) 538*66193Sbostic M_16_SWAP(bp[i]); 53950997Sbostic } 54050997Sbostic } 54150997Sbostic return (0); 54246372Sbostic } 54346372Sbostic 54450997Sbostic /* 54550997Sbostic * Write page p to disk 54650997Sbostic * 54750997Sbostic * Returns: 54850997Sbostic * 0 ==> OK 54950997Sbostic * -1 ==>failure 55050997Sbostic */ 55146372Sbostic extern int 55257586Sbostic __put_page(hashp, p, bucket, is_bucket, is_bitmap) 55357586Sbostic HTAB *hashp; 55450997Sbostic char *p; 55550997Sbostic u_int bucket; 55650997Sbostic int is_bucket, is_bitmap; 55746372Sbostic { 55850997Sbostic register int fd, page, size; 55950997Sbostic int wsize; 56046372Sbostic 56150997Sbostic size = hashp->BSIZE; 56257586Sbostic if ((hashp->fp == -1) && open_temp(hashp)) 56351073Sbostic return (-1); 56450997Sbostic fd = hashp->fp; 56546372Sbostic 56650997Sbostic if (hashp->LORDER != BYTE_ORDER) { 56750997Sbostic register int i; 56850997Sbostic register int max; 56946372Sbostic 57050997Sbostic if (is_bitmap) { 57150997Sbostic max = hashp->BSIZE >> 2; /* divide by 4 */ 57250997Sbostic for (i = 0; i < max; i++) 573*66193Sbostic M_32_SWAP(((long *)p)[i]); 57450997Sbostic } else { 57550997Sbostic max = ((u_short *)p)[0] + 2; 57650997Sbostic for (i = 0; i <= max; i++) 577*66193Sbostic M_16_SWAP(((u_short *)p)[i]); 57850997Sbostic } 57946372Sbostic } 58050997Sbostic if (is_bucket) 58150997Sbostic page = BUCKET_TO_PAGE(bucket); 58250997Sbostic else 58350997Sbostic page = OADDR_TO_PAGE(bucket); 58455314Sbostic if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) || 58550997Sbostic ((wsize = write(fd, p, size)) == -1)) 58650997Sbostic /* Errno is set */ 58750997Sbostic return (-1); 58850997Sbostic if (wsize != size) { 58950997Sbostic errno = EFTYPE; 59050997Sbostic return (-1); 59150997Sbostic } 59250997Sbostic return (0); 59346372Sbostic } 59450997Sbostic 59546372Sbostic #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1) 59646372Sbostic /* 59750997Sbostic * Initialize a new bitmap page. Bitmap pages are left in memory 59850997Sbostic * once they are read in. 59950997Sbostic */ 60051055Sbostic extern int 60157586Sbostic __init_bitmap(hashp, pnum, nbits, ndx) 60257586Sbostic HTAB *hashp; 60350997Sbostic int pnum, nbits, ndx; 60446372Sbostic { 60550997Sbostic u_long *ip; 60650997Sbostic int clearbytes, clearints; 60746372Sbostic 60850997Sbostic if (!(ip = malloc(hashp->BSIZE))) 60951055Sbostic return (1); 61050997Sbostic hashp->nmaps++; 61150997Sbostic clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1; 61250997Sbostic clearbytes = clearints << INT_TO_BYTE; 61351055Sbostic (void)memset((char *)ip, 0, clearbytes); 61451055Sbostic (void)memset(((char *)ip) + clearbytes, 0xFF, 61550997Sbostic hashp->BSIZE - clearbytes); 61650997Sbostic ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK); 61750997Sbostic SETBIT(ip, 0); 61850997Sbostic hashp->BITMAPS[ndx] = (u_short)pnum; 61950997Sbostic hashp->mapp[ndx] = ip; 62051055Sbostic return (0); 62146372Sbostic } 62250997Sbostic 62351055Sbostic static u_long 62450997Sbostic first_free(map) 62550997Sbostic u_long map; 62646372Sbostic { 62750997Sbostic register u_long i, mask; 62846372Sbostic 62950997Sbostic mask = 0x1; 63050997Sbostic for (i = 0; i < BITS_PER_MAP; i++) { 63150997Sbostic if (!(mask & map)) 63250997Sbostic return (i); 63350997Sbostic mask = mask << 1; 63450997Sbostic } 63550997Sbostic return (i); 63646372Sbostic } 63746372Sbostic 63857586Sbostic static u_short 63957586Sbostic overflow_page(hashp) 64057586Sbostic HTAB *hashp; 64146372Sbostic { 64250997Sbostic register u_long *freep; 64350997Sbostic register int max_free, offset, splitnum; 64450997Sbostic u_short addr; 64551061Sbostic int bit, first_page, free_bit, free_page, i, in_use_bits, j; 64646372Sbostic #ifdef DEBUG2 64750997Sbostic int tmp1, tmp2; 64846372Sbostic #endif 64951061Sbostic splitnum = hashp->OVFL_POINT; 65050997Sbostic max_free = hashp->SPARES[splitnum]; 65146372Sbostic 65250997Sbostic free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT); 65350997Sbostic free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1); 65446372Sbostic 65550997Sbostic /* Look through all the free maps to find the first free block */ 65651061Sbostic first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT); 65751061Sbostic for ( i = first_page; i <= free_page; i++ ) { 65850997Sbostic if (!(freep = (u_long *)hashp->mapp[i]) && 65957586Sbostic !(freep = fetch_bitmap(hashp, i))) 66050997Sbostic return (NULL); 66150997Sbostic if (i == free_page) 66250997Sbostic in_use_bits = free_bit; 66350997Sbostic else 66450997Sbostic in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1; 66551061Sbostic 66651061Sbostic if (i == first_page) { 66751061Sbostic bit = hashp->LAST_FREED & 66851061Sbostic ((hashp->BSIZE << BYTE_SHIFT) - 1); 66951061Sbostic j = bit / BITS_PER_MAP; 67051061Sbostic bit = bit & ~(BITS_PER_MAP - 1); 67151061Sbostic } else { 67251061Sbostic bit = 0; 67351061Sbostic j = 0; 67451061Sbostic } 67551061Sbostic for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP) 67650997Sbostic if (freep[j] != ALL_SET) 67750997Sbostic goto found; 67847250Sbostic } 67946372Sbostic 68050997Sbostic /* No Free Page Found */ 68151061Sbostic hashp->LAST_FREED = hashp->SPARES[splitnum]; 68250997Sbostic hashp->SPARES[splitnum]++; 68350997Sbostic offset = hashp->SPARES[splitnum] - 68450997Sbostic (splitnum ? hashp->SPARES[splitnum - 1] : 0); 68546372Sbostic 68651061Sbostic #define OVMSG "HASH: Out of overflow pages. Increase page size\n" 68751061Sbostic if (offset > SPLITMASK) { 68851061Sbostic if (++splitnum >= NCACHED) { 68951061Sbostic (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1); 69051061Sbostic return (NULL); 69151061Sbostic } 69251061Sbostic hashp->OVFL_POINT = splitnum; 69351061Sbostic hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1]; 69451061Sbostic hashp->SPARES[splitnum-1]--; 69562488Sbostic offset = 1; 69651061Sbostic } 69751061Sbostic 69850997Sbostic /* Check if we need to allocate a new bitmap page */ 69950997Sbostic if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) { 70050997Sbostic free_page++; 70150997Sbostic if (free_page >= NCACHED) { 70250997Sbostic (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1); 70350997Sbostic return (NULL); 70450997Sbostic } 70550997Sbostic /* 70650997Sbostic * This is tricky. The 1 indicates that you want the new page 70750997Sbostic * allocated with 1 clear bit. Actually, you are going to 70850997Sbostic * allocate 2 pages from this map. The first is going to be 70950997Sbostic * the map page, the second is the overflow page we were 71050997Sbostic * looking for. The init_bitmap routine automatically, sets 71150997Sbostic * the first bit of itself to indicate that the bitmap itself 71250997Sbostic * is in use. We would explicitly set the second bit, but 71350997Sbostic * don't have to if we tell init_bitmap not to leave it clear 71450997Sbostic * in the first place. 71550997Sbostic */ 71657586Sbostic if (__init_bitmap(hashp, (int)OADDR_OF(splitnum, offset), 71751055Sbostic 1, free_page)) 71851055Sbostic return (NULL); 71950997Sbostic hashp->SPARES[splitnum]++; 72046372Sbostic #ifdef DEBUG2 72150997Sbostic free_bit = 2; 72246372Sbostic #endif 72350997Sbostic offset++; 72451061Sbostic if (offset > SPLITMASK) { 72551061Sbostic if (++splitnum >= NCACHED) { 72651061Sbostic (void)write(STDERR_FILENO, OVMSG, 72751061Sbostic sizeof(OVMSG) - 1); 72851061Sbostic return (NULL); 72951061Sbostic } 73051061Sbostic hashp->OVFL_POINT = splitnum; 73151061Sbostic hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1]; 73251061Sbostic hashp->SPARES[splitnum-1]--; 73351061Sbostic offset = 0; 73451061Sbostic } 73550997Sbostic } else { 73650997Sbostic /* 73750997Sbostic * Free_bit addresses the last used bit. Bump it to address 73850997Sbostic * the first available bit. 73950997Sbostic */ 74050997Sbostic free_bit++; 74150997Sbostic SETBIT(freep, free_bit); 74250997Sbostic } 74346372Sbostic 74450997Sbostic /* Calculate address of the new overflow page */ 74550997Sbostic addr = OADDR_OF(splitnum, offset); 74646372Sbostic #ifdef DEBUG2 74750997Sbostic (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n", 74850997Sbostic addr, free_bit, free_page); 74946372Sbostic #endif 75050997Sbostic return (addr); 75146372Sbostic 75246372Sbostic found: 75350997Sbostic bit = bit + first_free(freep[j]); 75450997Sbostic SETBIT(freep, bit); 75546372Sbostic #ifdef DEBUG2 75650997Sbostic tmp1 = bit; 75750997Sbostic tmp2 = i; 75846372Sbostic #endif 75950997Sbostic /* 76050997Sbostic * Bits are addressed starting with 0, but overflow pages are addressed 76150997Sbostic * beginning at 1. Bit is a bit addressnumber, so we need to increment 76250997Sbostic * it to convert it to a page number. 76350997Sbostic */ 76450997Sbostic bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT)); 76551061Sbostic if (bit >= hashp->LAST_FREED) 76651061Sbostic hashp->LAST_FREED = bit - 1; 76746372Sbostic 76850997Sbostic /* Calculate the split number for this page */ 76950997Sbostic for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++); 77050997Sbostic offset = (i ? bit - hashp->SPARES[i - 1] : bit); 77150997Sbostic if (offset >= SPLITMASK) 77250997Sbostic return (NULL); /* Out of overflow pages */ 77350997Sbostic addr = OADDR_OF(i, offset); 77446372Sbostic #ifdef DEBUG2 77550997Sbostic (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n", 77650997Sbostic addr, tmp1, tmp2); 77746372Sbostic #endif 77846372Sbostic 77950997Sbostic /* Allocate and return the overflow page */ 78050997Sbostic return (addr); 78146372Sbostic } 78246372Sbostic 78346372Sbostic /* 78450997Sbostic * Mark this overflow page as free. 78550997Sbostic */ 78650997Sbostic extern void 78757586Sbostic __free_ovflpage(hashp, obufp) 78857586Sbostic HTAB *hashp; 78950997Sbostic BUFHEAD *obufp; 79046372Sbostic { 79150997Sbostic register u_short addr; 79250997Sbostic u_long *freep; 79350997Sbostic int bit_address, free_page, free_bit; 79450997Sbostic u_short ndx; 79546372Sbostic 79650997Sbostic addr = obufp->addr; 79746372Sbostic #ifdef DEBUG1 79850997Sbostic (void)fprintf(stderr, "Freeing %d\n", addr); 79946372Sbostic #endif 80050997Sbostic ndx = (((u_short)addr) >> SPLITSHIFT); 80150997Sbostic bit_address = 80250997Sbostic (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1; 80351061Sbostic if (bit_address < hashp->LAST_FREED) 80451061Sbostic hashp->LAST_FREED = bit_address; 80550997Sbostic free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT)); 80650997Sbostic free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1); 80746372Sbostic 80850997Sbostic if (!(freep = hashp->mapp[free_page])) 80957586Sbostic freep = fetch_bitmap(hashp, free_page); 81050997Sbostic #ifdef DEBUG 81150997Sbostic /* 81250997Sbostic * This had better never happen. It means we tried to read a bitmap 81350997Sbostic * that has already had overflow pages allocated off it, and we 81450997Sbostic * failed to read it from the file. 81550997Sbostic */ 81650997Sbostic if (!freep) 81750997Sbostic assert(0); 81850997Sbostic #endif 81950997Sbostic CLRBIT(freep, free_bit); 82046372Sbostic #ifdef DEBUG2 82150997Sbostic (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n", 82250997Sbostic obufp->addr, free_bit, free_page); 82346372Sbostic #endif 82457586Sbostic __reclaim_buf(hashp, obufp); 82546372Sbostic } 82646372Sbostic 82746372Sbostic /* 82850997Sbostic * Returns: 82950997Sbostic * 0 success 83050997Sbostic * -1 failure 83150997Sbostic */ 83246372Sbostic static int 83357586Sbostic open_temp(hashp) 83457586Sbostic HTAB *hashp; 83546372Sbostic { 83650997Sbostic sigset_t set, oset; 83750997Sbostic static char namestr[] = "_hashXXXXXX"; 83846372Sbostic 83950997Sbostic /* Block signals; make sure file goes away at process exit. */ 84055315Sbostic (void)sigfillset(&set); 84150997Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset); 84250997Sbostic if ((hashp->fp = mkstemp(namestr)) != -1) { 84350997Sbostic (void)unlink(namestr); 84450997Sbostic (void)fcntl(hashp->fp, F_SETFD, 1); 84550997Sbostic } 84650997Sbostic (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL); 84750997Sbostic return (hashp->fp != -1 ? 0 : -1); 84846372Sbostic } 84946372Sbostic 85050997Sbostic /* 85150997Sbostic * We have to know that the key will fit, but the last entry on the page is 85250997Sbostic * an overflow pair, so we need to shift things. 85350997Sbostic */ 85446372Sbostic static void 85550997Sbostic squeeze_key(sp, key, val) 85650997Sbostic u_short *sp; 85750997Sbostic const DBT *key, *val; 85846372Sbostic { 85950997Sbostic register char *p; 86050997Sbostic u_short free_space, n, off, pageno; 86146372Sbostic 86250997Sbostic p = (char *)sp; 86350997Sbostic n = sp[0]; 86450997Sbostic free_space = FREESPACE(sp); 86550997Sbostic off = OFFSET(sp); 86646372Sbostic 86750997Sbostic pageno = sp[n - 1]; 86850997Sbostic off -= key->size; 86950997Sbostic sp[n - 1] = off; 87058016Sbostic memmove(p + off, key->data, key->size); 87150997Sbostic off -= val->size; 87250997Sbostic sp[n] = off; 87358016Sbostic memmove(p + off, val->data, val->size); 87450997Sbostic sp[0] = n + 2; 87550997Sbostic sp[n + 1] = pageno; 87650997Sbostic sp[n + 2] = OVFLPAGE; 87750997Sbostic FREESPACE(sp) = free_space - PAIRSIZE(key, val); 87850997Sbostic OFFSET(sp) = off; 87946372Sbostic } 88046372Sbostic 88147250Sbostic static u_long * 88257586Sbostic fetch_bitmap(hashp, ndx) 88357586Sbostic HTAB *hashp; 88450997Sbostic int ndx; 88547250Sbostic { 88650997Sbostic if (ndx >= hashp->nmaps || 88750997Sbostic !(hashp->mapp[ndx] = malloc(hashp->BSIZE)) || 88857586Sbostic __get_page(hashp, (char *)hashp->mapp[ndx], 88950997Sbostic hashp->BITMAPS[ndx], 0, 1, 1)) 89050997Sbostic return (NULL); 89150997Sbostic return (hashp->mapp[ndx]); 89250997Sbostic } 89347250Sbostic 89446372Sbostic #ifdef DEBUG4 89550997Sbostic int 89650997Sbostic print_chain(addr) 89750997Sbostic int addr; 89846372Sbostic { 89950997Sbostic BUFHEAD *bufp; 90050997Sbostic short *bp, oaddr; 90146372Sbostic 90250997Sbostic (void)fprintf(stderr, "%d ", addr); 90357586Sbostic bufp = __get_buf(hashp, addr, NULL, 0); 90446372Sbostic bp = (short *)bufp->page; 90550997Sbostic while (bp[0] && ((bp[bp[0]] == OVFLPAGE) || 90650997Sbostic ((bp[0] > 2) && bp[2] < REAL_KEY))) { 90750997Sbostic oaddr = bp[bp[0] - 1]; 90850997Sbostic (void)fprintf(stderr, "%d ", (int)oaddr); 90957586Sbostic bufp = __get_buf(hashp, (int)oaddr, bufp, 0); 91046372Sbostic bp = (short *)bufp->page; 91146372Sbostic } 91250997Sbostic (void)fprintf(stderr, "\n"); 91346372Sbostic } 91446372Sbostic #endif 915