146372Sbostic /*- 2*62489Sbostic * Copyright (c) 1990, 1993 3*62489Sbostic * 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*62489Sbostic static char sccsid[] = "@(#)hash_page.c 8.1 (Berkeley) 06/06/93"; 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) { 27951055Sbostic /* 28051055Sbostic * Ov_addr gets set before reaching this point; there's 28151055Sbostic * always an overflow page before a big key/data page. 28251055Sbostic */ 28357586Sbostic if (__big_split(hashp, old_bufp, 28450997Sbostic new_bufp, bufp, ov_addr, obucket, &ret)) 28550997Sbostic return (-1); 28650997Sbostic old_bufp = ret.oldp; 28750997Sbostic if (!old_bufp) 28850997Sbostic return (-1); 28950997Sbostic op = (u_short *)old_bufp->page; 29050997Sbostic new_bufp = ret.newp; 29150997Sbostic if (!new_bufp) 29250997Sbostic return (-1); 29350997Sbostic np = (u_short *)new_bufp->page; 29450997Sbostic bufp = ret.nextp; 29550997Sbostic if (!bufp) 29650997Sbostic return (0); 29750997Sbostic cino = (char *)bufp->page; 29850997Sbostic ino = (u_short *)cino; 29950997Sbostic last_bfp = ret.nextp; 30050997Sbostic } else if (ino[n + 1] == OVFLPAGE) { 30150997Sbostic ov_addr = ino[n]; 30250997Sbostic /* 30350997Sbostic * Fix up the old page -- the extra 2 are the fields 30450997Sbostic * which contained the overflow information. 30550997Sbostic */ 30650997Sbostic ino[0] -= (moved + 2); 30750997Sbostic FREESPACE(ino) = 30850997Sbostic scopyto - sizeof(u_short) * (ino[0] + 3); 30950997Sbostic OFFSET(ino) = scopyto; 31046372Sbostic 31157586Sbostic bufp = __get_buf(hashp, ov_addr, bufp, 0); 31250997Sbostic if (!bufp) 31350997Sbostic return (-1); 31446372Sbostic 31550997Sbostic ino = (u_short *)bufp->page; 31650997Sbostic n = 1; 31750997Sbostic scopyto = hashp->BSIZE; 31850997Sbostic moved = 0; 31946372Sbostic 32050997Sbostic if (last_bfp) 32157586Sbostic __free_ovflpage(hashp, last_bfp); 32250997Sbostic last_bfp = bufp; 32350997Sbostic } 32450997Sbostic /* Move regular sized pairs of there are any */ 32550997Sbostic off = hashp->BSIZE; 32650997Sbostic for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) { 32750997Sbostic cino = (char *)ino; 32850997Sbostic key.data = (u_char *)cino + ino[n]; 32950997Sbostic key.size = off - ino[n]; 33050997Sbostic val.data = (u_char *)cino + ino[n + 1]; 33150997Sbostic val.size = ino[n] - ino[n + 1]; 33250997Sbostic off = ino[n + 1]; 33346372Sbostic 33457586Sbostic if (__call_hash(hashp, key.data, key.size) == obucket) { 33550997Sbostic /* Keep on old page */ 33650997Sbostic if (PAIRFITS(op, (&key), (&val))) 33750997Sbostic putpair((char *)op, &key, &val); 33850997Sbostic else { 33957586Sbostic old_bufp = 34057586Sbostic __add_ovflpage(hashp, old_bufp); 34150997Sbostic if (!old_bufp) 34250997Sbostic return (-1); 34350997Sbostic op = (u_short *)old_bufp->page; 34450997Sbostic putpair((char *)op, &key, &val); 34550997Sbostic } 34650997Sbostic old_bufp->flags |= BUF_MOD; 34750997Sbostic } else { 34850997Sbostic /* Move to new page */ 34950997Sbostic if (PAIRFITS(np, (&key), (&val))) 35050997Sbostic putpair((char *)np, &key, &val); 35150997Sbostic else { 35257586Sbostic new_bufp = 35357586Sbostic __add_ovflpage(hashp, new_bufp); 35450997Sbostic if (!new_bufp) 35550997Sbostic return (-1); 35650997Sbostic np = (u_short *)new_bufp->page; 35750997Sbostic putpair((char *)np, &key, &val); 35850997Sbostic } 35950997Sbostic new_bufp->flags |= BUF_MOD; 36050997Sbostic } 36146372Sbostic } 36246372Sbostic } 36350997Sbostic if (last_bfp) 36457586Sbostic __free_ovflpage(hashp, last_bfp); 36550997Sbostic return (0); 36650997Sbostic } 36746372Sbostic 36846372Sbostic /* 36950997Sbostic * Add the given pair to the page 37050997Sbostic * 37150997Sbostic * Returns: 37250997Sbostic * 0 ==> OK 37350997Sbostic * 1 ==> failure 37450997Sbostic */ 37546372Sbostic extern int 37657586Sbostic __addel(hashp, bufp, key, val) 37757586Sbostic HTAB *hashp; 37850997Sbostic BUFHEAD *bufp; 37950997Sbostic const DBT *key, *val; 38046372Sbostic { 38150997Sbostic register u_short *bp, *sop; 38250997Sbostic int do_expand; 38346372Sbostic 38450997Sbostic bp = (u_short *)bufp->page; 38550997Sbostic do_expand = 0; 38650997Sbostic while (bp[0] && (bp[bp[0]] < REAL_KEY)) 38750997Sbostic /* Exception case */ 38853500Sbostic if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) { 38950997Sbostic /* This is a big-keydata pair */ 39057586Sbostic bufp = __add_ovflpage(hashp, bufp); 39150997Sbostic if (!bufp) 39250997Sbostic return (-1); 39350997Sbostic bp = (u_short *)bufp->page; 39450997Sbostic } else 39550997Sbostic /* Try to squeeze key on this page */ 39650997Sbostic if (FREESPACE(bp) > PAIRSIZE(key, val)) { 39750997Sbostic squeeze_key(bp, key, val); 39850997Sbostic return (0); 39950997Sbostic } else { 40057586Sbostic bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0); 40150997Sbostic if (!bufp) 40250997Sbostic return (-1); 40350997Sbostic bp = (u_short *)bufp->page; 40450997Sbostic } 40546372Sbostic 40650997Sbostic if (PAIRFITS(bp, key, val)) 40750997Sbostic putpair(bufp->page, key, val); 40850997Sbostic else { 40950997Sbostic do_expand = 1; 41057586Sbostic bufp = __add_ovflpage(hashp, bufp); 41150997Sbostic if (!bufp) 41250997Sbostic return (-1); 41350997Sbostic sop = (u_short *)bufp->page; 41446372Sbostic 41550997Sbostic if (PAIRFITS(sop, key, val)) 41650997Sbostic putpair((char *)sop, key, val); 41750997Sbostic else 41857586Sbostic if (__big_insert(hashp, bufp, key, val)) 41950997Sbostic return (-1); 42046372Sbostic } 42150997Sbostic bufp->flags |= BUF_MOD; 42250997Sbostic /* 42350997Sbostic * If the average number of keys per bucket exceeds the fill factor, 42450997Sbostic * expand the table. 42550997Sbostic */ 42650997Sbostic hashp->NKEYS++; 42750997Sbostic if (do_expand || 42850997Sbostic (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR)) 42957586Sbostic return (__expand_table(hashp)); 43050997Sbostic return (0); 43146372Sbostic } 43246372Sbostic 43346372Sbostic /* 43450997Sbostic * 43550997Sbostic * Returns: 43650997Sbostic * pointer on success 43750997Sbostic * NULL on error 43850997Sbostic */ 43946372Sbostic extern BUFHEAD * 44057586Sbostic __add_ovflpage(hashp, bufp) 44157586Sbostic HTAB *hashp; 44250997Sbostic BUFHEAD *bufp; 44346372Sbostic { 44450997Sbostic register u_short *sp; 44550997Sbostic u_short ndx, ovfl_num; 44646372Sbostic #ifdef DEBUG1 44750997Sbostic int tmp1, tmp2; 44846372Sbostic #endif 44950997Sbostic sp = (u_short *)bufp->page; 45060246Sbostic 45160246Sbostic /* Check if we are dynamically determining the fill factor */ 45260246Sbostic if (hashp->FFACTOR == DEF_FFACTOR) { 45360246Sbostic hashp->FFACTOR = sp[0] >> 1; 45460246Sbostic if (hashp->FFACTOR < MIN_FFACTOR) 45560246Sbostic hashp->FFACTOR = MIN_FFACTOR; 45660246Sbostic } 45750997Sbostic bufp->flags |= BUF_MOD; 45857586Sbostic ovfl_num = overflow_page(hashp); 45946372Sbostic #ifdef DEBUG1 46050997Sbostic tmp1 = bufp->addr; 46150997Sbostic tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0; 46246372Sbostic #endif 46357586Sbostic if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1))) 46450997Sbostic return (NULL); 46550997Sbostic bufp->ovfl->flags |= BUF_MOD; 46646372Sbostic #ifdef DEBUG1 46750997Sbostic (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n", 46850997Sbostic tmp1, tmp2, bufp->ovfl->addr); 46946372Sbostic #endif 47050997Sbostic ndx = sp[0]; 47150997Sbostic /* 47250997Sbostic * Since a pair is allocated on a page only if there's room to add 47350997Sbostic * an overflow page, we know that the OVFL information will fit on 47450997Sbostic * the page. 47550997Sbostic */ 47650997Sbostic sp[ndx + 4] = OFFSET(sp); 47750997Sbostic sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE; 47850997Sbostic sp[ndx + 1] = ovfl_num; 47950997Sbostic sp[ndx + 2] = OVFLPAGE; 48050997Sbostic sp[0] = ndx + 2; 48146372Sbostic #ifdef HASH_STATISTICS 48250997Sbostic hash_overflows++; 48346372Sbostic #endif 48450997Sbostic return (bufp->ovfl); 48546372Sbostic } 48646372Sbostic 48746372Sbostic /* 48850997Sbostic * Returns: 48950997Sbostic * 0 indicates SUCCESS 49050997Sbostic * -1 indicates FAILURE 49150997Sbostic */ 49250997Sbostic extern int 49357586Sbostic __get_page(hashp, p, bucket, is_bucket, is_disk, is_bitmap) 49457586Sbostic HTAB *hashp; 49550997Sbostic char *p; 49650997Sbostic u_int bucket; 49750997Sbostic int is_bucket, is_disk, is_bitmap; 49846372Sbostic { 49950997Sbostic register int fd, page, size; 50050997Sbostic int rsize; 50150997Sbostic u_short *bp; 50246372Sbostic 50350997Sbostic fd = hashp->fp; 50450997Sbostic size = hashp->BSIZE; 50546372Sbostic 50650997Sbostic if ((fd == -1) || !is_disk) { 50750997Sbostic PAGE_INIT(p); 50850997Sbostic return (0); 50950997Sbostic } 51050997Sbostic if (is_bucket) 51150997Sbostic page = BUCKET_TO_PAGE(bucket); 51250997Sbostic else 51350997Sbostic page = OADDR_TO_PAGE(bucket); 51455314Sbostic if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) || 51550997Sbostic ((rsize = read(fd, p, size)) == -1)) 51650997Sbostic return (-1); 51750997Sbostic bp = (u_short *)p; 51850997Sbostic if (!rsize) 51950997Sbostic bp[0] = 0; /* We hit the EOF, so initialize a new page */ 52050997Sbostic else 52150997Sbostic if (rsize != size) { 52250997Sbostic errno = EFTYPE; 52350997Sbostic return (-1); 52450997Sbostic } 52553403Sbostic if (!is_bitmap && !bp[0]) { 52650997Sbostic PAGE_INIT(p); 52750997Sbostic } else 52850997Sbostic if (hashp->LORDER != BYTE_ORDER) { 52950997Sbostic register int i, max; 53046372Sbostic 53150997Sbostic if (is_bitmap) { 53250997Sbostic max = hashp->BSIZE >> 2; /* divide by 4 */ 53350997Sbostic for (i = 0; i < max; i++) 53450997Sbostic BLSWAP(((long *)p)[i]); 53550997Sbostic } else { 53650997Sbostic BSSWAP(bp[0]); 53750997Sbostic max = bp[0] + 2; 53850997Sbostic for (i = 1; i <= max; i++) 53950997Sbostic BSSWAP(bp[i]); 54050997Sbostic } 54150997Sbostic } 54250997Sbostic return (0); 54346372Sbostic } 54446372Sbostic 54550997Sbostic /* 54650997Sbostic * Write page p to disk 54750997Sbostic * 54850997Sbostic * Returns: 54950997Sbostic * 0 ==> OK 55050997Sbostic * -1 ==>failure 55150997Sbostic */ 55246372Sbostic extern int 55357586Sbostic __put_page(hashp, p, bucket, is_bucket, is_bitmap) 55457586Sbostic HTAB *hashp; 55550997Sbostic char *p; 55650997Sbostic u_int bucket; 55750997Sbostic int is_bucket, is_bitmap; 55846372Sbostic { 55950997Sbostic register int fd, page, size; 56050997Sbostic int wsize; 56146372Sbostic 56250997Sbostic size = hashp->BSIZE; 56357586Sbostic if ((hashp->fp == -1) && open_temp(hashp)) 56451073Sbostic return (-1); 56550997Sbostic fd = hashp->fp; 56646372Sbostic 56750997Sbostic if (hashp->LORDER != BYTE_ORDER) { 56850997Sbostic register int i; 56950997Sbostic register int max; 57046372Sbostic 57150997Sbostic if (is_bitmap) { 57250997Sbostic max = hashp->BSIZE >> 2; /* divide by 4 */ 57350997Sbostic for (i = 0; i < max; i++) 57450997Sbostic BLSWAP(((long *)p)[i]); 57550997Sbostic } else { 57650997Sbostic max = ((u_short *)p)[0] + 2; 57750997Sbostic for (i = 0; i <= max; i++) 57850997Sbostic BSSWAP(((u_short *)p)[i]); 57950997Sbostic } 58046372Sbostic } 58150997Sbostic if (is_bucket) 58250997Sbostic page = BUCKET_TO_PAGE(bucket); 58350997Sbostic else 58450997Sbostic page = OADDR_TO_PAGE(bucket); 58555314Sbostic if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) || 58650997Sbostic ((wsize = write(fd, p, size)) == -1)) 58750997Sbostic /* Errno is set */ 58850997Sbostic return (-1); 58950997Sbostic if (wsize != size) { 59050997Sbostic errno = EFTYPE; 59150997Sbostic return (-1); 59250997Sbostic } 59350997Sbostic return (0); 59446372Sbostic } 59550997Sbostic 59646372Sbostic #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1) 59746372Sbostic /* 59850997Sbostic * Initialize a new bitmap page. Bitmap pages are left in memory 59950997Sbostic * once they are read in. 60050997Sbostic */ 60151055Sbostic extern int 60257586Sbostic __init_bitmap(hashp, pnum, nbits, ndx) 60357586Sbostic HTAB *hashp; 60450997Sbostic int pnum, nbits, ndx; 60546372Sbostic { 60650997Sbostic u_long *ip; 60750997Sbostic int clearbytes, clearints; 60846372Sbostic 60950997Sbostic if (!(ip = malloc(hashp->BSIZE))) 61051055Sbostic return (1); 61150997Sbostic hashp->nmaps++; 61250997Sbostic clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1; 61350997Sbostic clearbytes = clearints << INT_TO_BYTE; 61451055Sbostic (void)memset((char *)ip, 0, clearbytes); 61551055Sbostic (void)memset(((char *)ip) + clearbytes, 0xFF, 61650997Sbostic hashp->BSIZE - clearbytes); 61750997Sbostic ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK); 61850997Sbostic SETBIT(ip, 0); 61950997Sbostic hashp->BITMAPS[ndx] = (u_short)pnum; 62050997Sbostic hashp->mapp[ndx] = ip; 62151055Sbostic return (0); 62246372Sbostic } 62350997Sbostic 62451055Sbostic static u_long 62550997Sbostic first_free(map) 62650997Sbostic u_long map; 62746372Sbostic { 62850997Sbostic register u_long i, mask; 62946372Sbostic 63050997Sbostic mask = 0x1; 63150997Sbostic for (i = 0; i < BITS_PER_MAP; i++) { 63250997Sbostic if (!(mask & map)) 63350997Sbostic return (i); 63450997Sbostic mask = mask << 1; 63550997Sbostic } 63650997Sbostic return (i); 63746372Sbostic } 63846372Sbostic 63957586Sbostic static u_short 64057586Sbostic overflow_page(hashp) 64157586Sbostic HTAB *hashp; 64246372Sbostic { 64350997Sbostic register u_long *freep; 64450997Sbostic register int max_free, offset, splitnum; 64550997Sbostic u_short addr; 64651061Sbostic int bit, first_page, free_bit, free_page, i, in_use_bits, j; 64746372Sbostic #ifdef DEBUG2 64850997Sbostic int tmp1, tmp2; 64946372Sbostic #endif 65051061Sbostic splitnum = hashp->OVFL_POINT; 65150997Sbostic max_free = hashp->SPARES[splitnum]; 65246372Sbostic 65350997Sbostic free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT); 65450997Sbostic free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1); 65546372Sbostic 65650997Sbostic /* Look through all the free maps to find the first free block */ 65751061Sbostic first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT); 65851061Sbostic for ( i = first_page; i <= free_page; i++ ) { 65950997Sbostic if (!(freep = (u_long *)hashp->mapp[i]) && 66057586Sbostic !(freep = fetch_bitmap(hashp, i))) 66150997Sbostic return (NULL); 66250997Sbostic if (i == free_page) 66350997Sbostic in_use_bits = free_bit; 66450997Sbostic else 66550997Sbostic in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1; 66651061Sbostic 66751061Sbostic if (i == first_page) { 66851061Sbostic bit = hashp->LAST_FREED & 66951061Sbostic ((hashp->BSIZE << BYTE_SHIFT) - 1); 67051061Sbostic j = bit / BITS_PER_MAP; 67151061Sbostic bit = bit & ~(BITS_PER_MAP - 1); 67251061Sbostic } else { 67351061Sbostic bit = 0; 67451061Sbostic j = 0; 67551061Sbostic } 67651061Sbostic for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP) 67750997Sbostic if (freep[j] != ALL_SET) 67850997Sbostic goto found; 67947250Sbostic } 68046372Sbostic 68150997Sbostic /* No Free Page Found */ 68251061Sbostic hashp->LAST_FREED = hashp->SPARES[splitnum]; 68350997Sbostic hashp->SPARES[splitnum]++; 68450997Sbostic offset = hashp->SPARES[splitnum] - 68550997Sbostic (splitnum ? hashp->SPARES[splitnum - 1] : 0); 68646372Sbostic 68751061Sbostic #define OVMSG "HASH: Out of overflow pages. Increase page size\n" 68851061Sbostic if (offset > SPLITMASK) { 68951061Sbostic if (++splitnum >= NCACHED) { 69051061Sbostic (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1); 69151061Sbostic return (NULL); 69251061Sbostic } 69351061Sbostic hashp->OVFL_POINT = splitnum; 69451061Sbostic hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1]; 69551061Sbostic hashp->SPARES[splitnum-1]--; 69662488Sbostic offset = 1; 69751061Sbostic } 69851061Sbostic 69950997Sbostic /* Check if we need to allocate a new bitmap page */ 70050997Sbostic if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) { 70150997Sbostic free_page++; 70250997Sbostic if (free_page >= NCACHED) { 70350997Sbostic (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1); 70450997Sbostic return (NULL); 70550997Sbostic } 70650997Sbostic /* 70750997Sbostic * This is tricky. The 1 indicates that you want the new page 70850997Sbostic * allocated with 1 clear bit. Actually, you are going to 70950997Sbostic * allocate 2 pages from this map. The first is going to be 71050997Sbostic * the map page, the second is the overflow page we were 71150997Sbostic * looking for. The init_bitmap routine automatically, sets 71250997Sbostic * the first bit of itself to indicate that the bitmap itself 71350997Sbostic * is in use. We would explicitly set the second bit, but 71450997Sbostic * don't have to if we tell init_bitmap not to leave it clear 71550997Sbostic * in the first place. 71650997Sbostic */ 71757586Sbostic if (__init_bitmap(hashp, (int)OADDR_OF(splitnum, offset), 71851055Sbostic 1, free_page)) 71951055Sbostic return (NULL); 72050997Sbostic hashp->SPARES[splitnum]++; 72146372Sbostic #ifdef DEBUG2 72250997Sbostic free_bit = 2; 72346372Sbostic #endif 72450997Sbostic offset++; 72551061Sbostic if (offset > SPLITMASK) { 72651061Sbostic if (++splitnum >= NCACHED) { 72751061Sbostic (void)write(STDERR_FILENO, OVMSG, 72851061Sbostic sizeof(OVMSG) - 1); 72951061Sbostic return (NULL); 73051061Sbostic } 73151061Sbostic hashp->OVFL_POINT = splitnum; 73251061Sbostic hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1]; 73351061Sbostic hashp->SPARES[splitnum-1]--; 73451061Sbostic offset = 0; 73551061Sbostic } 73650997Sbostic } else { 73750997Sbostic /* 73850997Sbostic * Free_bit addresses the last used bit. Bump it to address 73950997Sbostic * the first available bit. 74050997Sbostic */ 74150997Sbostic free_bit++; 74250997Sbostic SETBIT(freep, free_bit); 74350997Sbostic } 74446372Sbostic 74550997Sbostic /* Calculate address of the new overflow page */ 74650997Sbostic addr = OADDR_OF(splitnum, offset); 74746372Sbostic #ifdef DEBUG2 74850997Sbostic (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n", 74950997Sbostic addr, free_bit, free_page); 75046372Sbostic #endif 75150997Sbostic return (addr); 75246372Sbostic 75346372Sbostic found: 75450997Sbostic bit = bit + first_free(freep[j]); 75550997Sbostic SETBIT(freep, bit); 75646372Sbostic #ifdef DEBUG2 75750997Sbostic tmp1 = bit; 75850997Sbostic tmp2 = i; 75946372Sbostic #endif 76050997Sbostic /* 76150997Sbostic * Bits are addressed starting with 0, but overflow pages are addressed 76250997Sbostic * beginning at 1. Bit is a bit addressnumber, so we need to increment 76350997Sbostic * it to convert it to a page number. 76450997Sbostic */ 76550997Sbostic bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT)); 76651061Sbostic if (bit >= hashp->LAST_FREED) 76751061Sbostic hashp->LAST_FREED = bit - 1; 76846372Sbostic 76950997Sbostic /* Calculate the split number for this page */ 77050997Sbostic for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++); 77150997Sbostic offset = (i ? bit - hashp->SPARES[i - 1] : bit); 77250997Sbostic if (offset >= SPLITMASK) 77350997Sbostic return (NULL); /* Out of overflow pages */ 77450997Sbostic addr = OADDR_OF(i, offset); 77546372Sbostic #ifdef DEBUG2 77650997Sbostic (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n", 77750997Sbostic addr, tmp1, tmp2); 77846372Sbostic #endif 77946372Sbostic 78050997Sbostic /* Allocate and return the overflow page */ 78150997Sbostic return (addr); 78246372Sbostic } 78346372Sbostic 78446372Sbostic /* 78550997Sbostic * Mark this overflow page as free. 78650997Sbostic */ 78750997Sbostic extern void 78857586Sbostic __free_ovflpage(hashp, obufp) 78957586Sbostic HTAB *hashp; 79050997Sbostic BUFHEAD *obufp; 79146372Sbostic { 79250997Sbostic register u_short addr; 79350997Sbostic u_long *freep; 79450997Sbostic int bit_address, free_page, free_bit; 79550997Sbostic u_short ndx; 79646372Sbostic 79750997Sbostic addr = obufp->addr; 79846372Sbostic #ifdef DEBUG1 79950997Sbostic (void)fprintf(stderr, "Freeing %d\n", addr); 80046372Sbostic #endif 80150997Sbostic ndx = (((u_short)addr) >> SPLITSHIFT); 80250997Sbostic bit_address = 80350997Sbostic (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1; 80451061Sbostic if (bit_address < hashp->LAST_FREED) 80551061Sbostic hashp->LAST_FREED = bit_address; 80650997Sbostic free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT)); 80750997Sbostic free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1); 80846372Sbostic 80950997Sbostic if (!(freep = hashp->mapp[free_page])) 81057586Sbostic freep = fetch_bitmap(hashp, free_page); 81150997Sbostic #ifdef DEBUG 81250997Sbostic /* 81350997Sbostic * This had better never happen. It means we tried to read a bitmap 81450997Sbostic * that has already had overflow pages allocated off it, and we 81550997Sbostic * failed to read it from the file. 81650997Sbostic */ 81750997Sbostic if (!freep) 81850997Sbostic assert(0); 81950997Sbostic #endif 82050997Sbostic CLRBIT(freep, free_bit); 82146372Sbostic #ifdef DEBUG2 82250997Sbostic (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n", 82350997Sbostic obufp->addr, free_bit, free_page); 82446372Sbostic #endif 82557586Sbostic __reclaim_buf(hashp, obufp); 82646372Sbostic } 82746372Sbostic 82846372Sbostic /* 82950997Sbostic * Returns: 83050997Sbostic * 0 success 83150997Sbostic * -1 failure 83250997Sbostic */ 83346372Sbostic static int 83457586Sbostic open_temp(hashp) 83557586Sbostic HTAB *hashp; 83646372Sbostic { 83750997Sbostic sigset_t set, oset; 83850997Sbostic static char namestr[] = "_hashXXXXXX"; 83946372Sbostic 84050997Sbostic /* Block signals; make sure file goes away at process exit. */ 84155315Sbostic (void)sigfillset(&set); 84250997Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset); 84350997Sbostic if ((hashp->fp = mkstemp(namestr)) != -1) { 84450997Sbostic (void)unlink(namestr); 84550997Sbostic (void)fcntl(hashp->fp, F_SETFD, 1); 84650997Sbostic } 84750997Sbostic (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL); 84850997Sbostic return (hashp->fp != -1 ? 0 : -1); 84946372Sbostic } 85046372Sbostic 85150997Sbostic /* 85250997Sbostic * We have to know that the key will fit, but the last entry on the page is 85350997Sbostic * an overflow pair, so we need to shift things. 85450997Sbostic */ 85546372Sbostic static void 85650997Sbostic squeeze_key(sp, key, val) 85750997Sbostic u_short *sp; 85850997Sbostic const DBT *key, *val; 85946372Sbostic { 86050997Sbostic register char *p; 86150997Sbostic u_short free_space, n, off, pageno; 86246372Sbostic 86350997Sbostic p = (char *)sp; 86450997Sbostic n = sp[0]; 86550997Sbostic free_space = FREESPACE(sp); 86650997Sbostic off = OFFSET(sp); 86746372Sbostic 86850997Sbostic pageno = sp[n - 1]; 86950997Sbostic off -= key->size; 87050997Sbostic sp[n - 1] = off; 87158016Sbostic memmove(p + off, key->data, key->size); 87250997Sbostic off -= val->size; 87350997Sbostic sp[n] = off; 87458016Sbostic memmove(p + off, val->data, val->size); 87550997Sbostic sp[0] = n + 2; 87650997Sbostic sp[n + 1] = pageno; 87750997Sbostic sp[n + 2] = OVFLPAGE; 87850997Sbostic FREESPACE(sp) = free_space - PAIRSIZE(key, val); 87950997Sbostic OFFSET(sp) = off; 88046372Sbostic } 88146372Sbostic 88247250Sbostic static u_long * 88357586Sbostic fetch_bitmap(hashp, ndx) 88457586Sbostic HTAB *hashp; 88550997Sbostic int ndx; 88647250Sbostic { 88750997Sbostic if (ndx >= hashp->nmaps || 88850997Sbostic !(hashp->mapp[ndx] = malloc(hashp->BSIZE)) || 88957586Sbostic __get_page(hashp, (char *)hashp->mapp[ndx], 89050997Sbostic hashp->BITMAPS[ndx], 0, 1, 1)) 89150997Sbostic return (NULL); 89250997Sbostic return (hashp->mapp[ndx]); 89350997Sbostic } 89447250Sbostic 89546372Sbostic #ifdef DEBUG4 89650997Sbostic int 89750997Sbostic print_chain(addr) 89850997Sbostic int addr; 89946372Sbostic { 90050997Sbostic BUFHEAD *bufp; 90150997Sbostic short *bp, oaddr; 90246372Sbostic 90350997Sbostic (void)fprintf(stderr, "%d ", addr); 90457586Sbostic bufp = __get_buf(hashp, addr, NULL, 0); 90546372Sbostic bp = (short *)bufp->page; 90650997Sbostic while (bp[0] && ((bp[bp[0]] == OVFLPAGE) || 90750997Sbostic ((bp[0] > 2) && bp[2] < REAL_KEY))) { 90850997Sbostic oaddr = bp[bp[0] - 1]; 90950997Sbostic (void)fprintf(stderr, "%d ", (int)oaddr); 91057586Sbostic bufp = __get_buf(hashp, (int)oaddr, bufp, 0); 91146372Sbostic bp = (short *)bufp->page; 91246372Sbostic } 91350997Sbostic (void)fprintf(stderr, "\n"); 91446372Sbostic } 91546372Sbostic #endif 916