146372Sbostic /*- 246372Sbostic * Copyright (c) 1990 The Regents of the University of California. 346372Sbostic * 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*57932Sbostic static char sccsid[] = "@(#)hash_page.c 5.24 (Berkeley) 02/11/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 44*57932Sbostic #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; 8250997Sbostic bcopy(key->data, p + off, key->size); 8346372Sbostic bp[++n] = off; 8446372Sbostic 8550997Sbostic /* Now the data. */ 8646372Sbostic off -= val->size; 8750997Sbostic bcopy(val->data, p + off, 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; 12750997Sbostic bcopy(src, dst, 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; 20150997Sbostic bcopy(op + ino[n + 1], op + copyto, 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; 45050997Sbostic bufp->flags |= BUF_MOD; 45157586Sbostic ovfl_num = overflow_page(hashp); 45246372Sbostic #ifdef DEBUG1 45350997Sbostic tmp1 = bufp->addr; 45450997Sbostic tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0; 45546372Sbostic #endif 45657586Sbostic if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1))) 45750997Sbostic return (NULL); 45850997Sbostic bufp->ovfl->flags |= BUF_MOD; 45946372Sbostic #ifdef DEBUG1 46050997Sbostic (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n", 46150997Sbostic tmp1, tmp2, bufp->ovfl->addr); 46246372Sbostic #endif 46350997Sbostic ndx = sp[0]; 46450997Sbostic /* 46550997Sbostic * Since a pair is allocated on a page only if there's room to add 46650997Sbostic * an overflow page, we know that the OVFL information will fit on 46750997Sbostic * the page. 46850997Sbostic */ 46950997Sbostic sp[ndx + 4] = OFFSET(sp); 47050997Sbostic sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE; 47150997Sbostic sp[ndx + 1] = ovfl_num; 47250997Sbostic sp[ndx + 2] = OVFLPAGE; 47350997Sbostic sp[0] = ndx + 2; 47446372Sbostic #ifdef HASH_STATISTICS 47550997Sbostic hash_overflows++; 47646372Sbostic #endif 47750997Sbostic return (bufp->ovfl); 47846372Sbostic } 47946372Sbostic 48046372Sbostic /* 48150997Sbostic * Returns: 48250997Sbostic * 0 indicates SUCCESS 48350997Sbostic * -1 indicates FAILURE 48450997Sbostic */ 48550997Sbostic extern int 48657586Sbostic __get_page(hashp, p, bucket, is_bucket, is_disk, is_bitmap) 48757586Sbostic HTAB *hashp; 48850997Sbostic char *p; 48950997Sbostic u_int bucket; 49050997Sbostic int is_bucket, is_disk, is_bitmap; 49146372Sbostic { 49250997Sbostic register int fd, page, size; 49350997Sbostic int rsize; 49450997Sbostic u_short *bp; 49546372Sbostic 49650997Sbostic fd = hashp->fp; 49750997Sbostic size = hashp->BSIZE; 49846372Sbostic 49950997Sbostic if ((fd == -1) || !is_disk) { 50050997Sbostic PAGE_INIT(p); 50150997Sbostic return (0); 50250997Sbostic } 50350997Sbostic if (is_bucket) 50450997Sbostic page = BUCKET_TO_PAGE(bucket); 50550997Sbostic else 50650997Sbostic page = OADDR_TO_PAGE(bucket); 50755314Sbostic if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) || 50850997Sbostic ((rsize = read(fd, p, size)) == -1)) 50950997Sbostic return (-1); 51050997Sbostic bp = (u_short *)p; 51150997Sbostic if (!rsize) 51250997Sbostic bp[0] = 0; /* We hit the EOF, so initialize a new page */ 51350997Sbostic else 51450997Sbostic if (rsize != size) { 51550997Sbostic errno = EFTYPE; 51650997Sbostic return (-1); 51750997Sbostic } 51853403Sbostic if (!is_bitmap && !bp[0]) { 51950997Sbostic PAGE_INIT(p); 52050997Sbostic } else 52150997Sbostic if (hashp->LORDER != BYTE_ORDER) { 52250997Sbostic register int i, max; 52346372Sbostic 52450997Sbostic if (is_bitmap) { 52550997Sbostic max = hashp->BSIZE >> 2; /* divide by 4 */ 52650997Sbostic for (i = 0; i < max; i++) 52750997Sbostic BLSWAP(((long *)p)[i]); 52850997Sbostic } else { 52950997Sbostic BSSWAP(bp[0]); 53050997Sbostic max = bp[0] + 2; 53150997Sbostic for (i = 1; i <= max; i++) 53250997Sbostic BSSWAP(bp[i]); 53350997Sbostic } 53450997Sbostic } 53550997Sbostic return (0); 53646372Sbostic } 53746372Sbostic 53850997Sbostic /* 53950997Sbostic * Write page p to disk 54050997Sbostic * 54150997Sbostic * Returns: 54250997Sbostic * 0 ==> OK 54350997Sbostic * -1 ==>failure 54450997Sbostic */ 54546372Sbostic extern int 54657586Sbostic __put_page(hashp, p, bucket, is_bucket, is_bitmap) 54757586Sbostic HTAB *hashp; 54850997Sbostic char *p; 54950997Sbostic u_int bucket; 55050997Sbostic int is_bucket, is_bitmap; 55146372Sbostic { 55250997Sbostic register int fd, page, size; 55350997Sbostic int wsize; 55446372Sbostic 55550997Sbostic size = hashp->BSIZE; 55657586Sbostic if ((hashp->fp == -1) && open_temp(hashp)) 55751073Sbostic return (-1); 55850997Sbostic fd = hashp->fp; 55946372Sbostic 56050997Sbostic if (hashp->LORDER != BYTE_ORDER) { 56150997Sbostic register int i; 56250997Sbostic register int max; 56346372Sbostic 56450997Sbostic if (is_bitmap) { 56550997Sbostic max = hashp->BSIZE >> 2; /* divide by 4 */ 56650997Sbostic for (i = 0; i < max; i++) 56750997Sbostic BLSWAP(((long *)p)[i]); 56850997Sbostic } else { 56950997Sbostic max = ((u_short *)p)[0] + 2; 57050997Sbostic for (i = 0; i <= max; i++) 57150997Sbostic BSSWAP(((u_short *)p)[i]); 57250997Sbostic } 57346372Sbostic } 57450997Sbostic if (is_bucket) 57550997Sbostic page = BUCKET_TO_PAGE(bucket); 57650997Sbostic else 57750997Sbostic page = OADDR_TO_PAGE(bucket); 57855314Sbostic if ((lseek(fd, (off_t)page << hashp->BSHIFT, SEEK_SET) == -1) || 57950997Sbostic ((wsize = write(fd, p, size)) == -1)) 58050997Sbostic /* Errno is set */ 58150997Sbostic return (-1); 58250997Sbostic if (wsize != size) { 58350997Sbostic errno = EFTYPE; 58450997Sbostic return (-1); 58550997Sbostic } 58650997Sbostic return (0); 58746372Sbostic } 58850997Sbostic 58946372Sbostic #define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1) 59046372Sbostic /* 59150997Sbostic * Initialize a new bitmap page. Bitmap pages are left in memory 59250997Sbostic * once they are read in. 59350997Sbostic */ 59451055Sbostic extern int 59557586Sbostic __init_bitmap(hashp, pnum, nbits, ndx) 59657586Sbostic HTAB *hashp; 59750997Sbostic int pnum, nbits, ndx; 59846372Sbostic { 59950997Sbostic u_long *ip; 60050997Sbostic int clearbytes, clearints; 60146372Sbostic 60250997Sbostic if (!(ip = malloc(hashp->BSIZE))) 60351055Sbostic return (1); 60450997Sbostic hashp->nmaps++; 60550997Sbostic clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1; 60650997Sbostic clearbytes = clearints << INT_TO_BYTE; 60751055Sbostic (void)memset((char *)ip, 0, clearbytes); 60851055Sbostic (void)memset(((char *)ip) + clearbytes, 0xFF, 60950997Sbostic hashp->BSIZE - clearbytes); 61050997Sbostic ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK); 61150997Sbostic SETBIT(ip, 0); 61250997Sbostic hashp->BITMAPS[ndx] = (u_short)pnum; 61350997Sbostic hashp->mapp[ndx] = ip; 61451055Sbostic return (0); 61546372Sbostic } 61650997Sbostic 61751055Sbostic static u_long 61850997Sbostic first_free(map) 61950997Sbostic u_long map; 62046372Sbostic { 62150997Sbostic register u_long i, mask; 62246372Sbostic 62350997Sbostic mask = 0x1; 62450997Sbostic for (i = 0; i < BITS_PER_MAP; i++) { 62550997Sbostic if (!(mask & map)) 62650997Sbostic return (i); 62750997Sbostic mask = mask << 1; 62850997Sbostic } 62950997Sbostic return (i); 63046372Sbostic } 63146372Sbostic 63257586Sbostic static u_short 63357586Sbostic overflow_page(hashp) 63457586Sbostic HTAB *hashp; 63546372Sbostic { 63650997Sbostic register u_long *freep; 63750997Sbostic register int max_free, offset, splitnum; 63850997Sbostic u_short addr; 63951061Sbostic int bit, first_page, free_bit, free_page, i, in_use_bits, j; 64046372Sbostic #ifdef DEBUG2 64150997Sbostic int tmp1, tmp2; 64246372Sbostic #endif 64351061Sbostic splitnum = hashp->OVFL_POINT; 64450997Sbostic max_free = hashp->SPARES[splitnum]; 64546372Sbostic 64650997Sbostic free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT); 64750997Sbostic free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1); 64846372Sbostic 64950997Sbostic /* Look through all the free maps to find the first free block */ 65051061Sbostic first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT); 65151061Sbostic for ( i = first_page; i <= free_page; i++ ) { 65250997Sbostic if (!(freep = (u_long *)hashp->mapp[i]) && 65357586Sbostic !(freep = fetch_bitmap(hashp, i))) 65450997Sbostic return (NULL); 65550997Sbostic if (i == free_page) 65650997Sbostic in_use_bits = free_bit; 65750997Sbostic else 65850997Sbostic in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1; 65951061Sbostic 66051061Sbostic if (i == first_page) { 66151061Sbostic bit = hashp->LAST_FREED & 66251061Sbostic ((hashp->BSIZE << BYTE_SHIFT) - 1); 66351061Sbostic j = bit / BITS_PER_MAP; 66451061Sbostic bit = bit & ~(BITS_PER_MAP - 1); 66551061Sbostic } else { 66651061Sbostic bit = 0; 66751061Sbostic j = 0; 66851061Sbostic } 66951061Sbostic for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP) 67050997Sbostic if (freep[j] != ALL_SET) 67150997Sbostic goto found; 67247250Sbostic } 67346372Sbostic 67450997Sbostic /* No Free Page Found */ 67551061Sbostic hashp->LAST_FREED = hashp->SPARES[splitnum]; 67650997Sbostic hashp->SPARES[splitnum]++; 67750997Sbostic offset = hashp->SPARES[splitnum] - 67850997Sbostic (splitnum ? hashp->SPARES[splitnum - 1] : 0); 67946372Sbostic 68051061Sbostic #define OVMSG "HASH: Out of overflow pages. Increase page size\n" 68151061Sbostic if (offset > SPLITMASK) { 68251061Sbostic if (++splitnum >= NCACHED) { 68351061Sbostic (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1); 68451061Sbostic return (NULL); 68551061Sbostic } 68651061Sbostic hashp->OVFL_POINT = splitnum; 68751061Sbostic hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1]; 68851061Sbostic hashp->SPARES[splitnum-1]--; 68951061Sbostic offset = 0; 69051061Sbostic } 69151061Sbostic 69250997Sbostic /* Check if we need to allocate a new bitmap page */ 69350997Sbostic if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) { 69450997Sbostic free_page++; 69550997Sbostic if (free_page >= NCACHED) { 69650997Sbostic (void)write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1); 69750997Sbostic return (NULL); 69850997Sbostic } 69950997Sbostic /* 70050997Sbostic * This is tricky. The 1 indicates that you want the new page 70150997Sbostic * allocated with 1 clear bit. Actually, you are going to 70250997Sbostic * allocate 2 pages from this map. The first is going to be 70350997Sbostic * the map page, the second is the overflow page we were 70450997Sbostic * looking for. The init_bitmap routine automatically, sets 70550997Sbostic * the first bit of itself to indicate that the bitmap itself 70650997Sbostic * is in use. We would explicitly set the second bit, but 70750997Sbostic * don't have to if we tell init_bitmap not to leave it clear 70850997Sbostic * in the first place. 70950997Sbostic */ 71057586Sbostic if (__init_bitmap(hashp, (int)OADDR_OF(splitnum, offset), 71151055Sbostic 1, free_page)) 71251055Sbostic return (NULL); 71350997Sbostic hashp->SPARES[splitnum]++; 71446372Sbostic #ifdef DEBUG2 71550997Sbostic free_bit = 2; 71646372Sbostic #endif 71750997Sbostic offset++; 71851061Sbostic if (offset > SPLITMASK) { 71951061Sbostic if (++splitnum >= NCACHED) { 72051061Sbostic (void)write(STDERR_FILENO, OVMSG, 72151061Sbostic sizeof(OVMSG) - 1); 72251061Sbostic return (NULL); 72351061Sbostic } 72451061Sbostic hashp->OVFL_POINT = splitnum; 72551061Sbostic hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1]; 72651061Sbostic hashp->SPARES[splitnum-1]--; 72751061Sbostic offset = 0; 72851061Sbostic } 72950997Sbostic } else { 73050997Sbostic /* 73150997Sbostic * Free_bit addresses the last used bit. Bump it to address 73250997Sbostic * the first available bit. 73350997Sbostic */ 73450997Sbostic free_bit++; 73550997Sbostic SETBIT(freep, free_bit); 73650997Sbostic } 73746372Sbostic 73850997Sbostic /* Calculate address of the new overflow page */ 73950997Sbostic addr = OADDR_OF(splitnum, offset); 74046372Sbostic #ifdef DEBUG2 74150997Sbostic (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n", 74250997Sbostic addr, free_bit, free_page); 74346372Sbostic #endif 74450997Sbostic return (addr); 74546372Sbostic 74646372Sbostic found: 74750997Sbostic bit = bit + first_free(freep[j]); 74850997Sbostic SETBIT(freep, bit); 74946372Sbostic #ifdef DEBUG2 75050997Sbostic tmp1 = bit; 75150997Sbostic tmp2 = i; 75246372Sbostic #endif 75350997Sbostic /* 75450997Sbostic * Bits are addressed starting with 0, but overflow pages are addressed 75550997Sbostic * beginning at 1. Bit is a bit addressnumber, so we need to increment 75650997Sbostic * it to convert it to a page number. 75750997Sbostic */ 75850997Sbostic bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT)); 75951061Sbostic if (bit >= hashp->LAST_FREED) 76051061Sbostic hashp->LAST_FREED = bit - 1; 76146372Sbostic 76250997Sbostic /* Calculate the split number for this page */ 76350997Sbostic for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++); 76450997Sbostic offset = (i ? bit - hashp->SPARES[i - 1] : bit); 76550997Sbostic if (offset >= SPLITMASK) 76650997Sbostic return (NULL); /* Out of overflow pages */ 76750997Sbostic addr = OADDR_OF(i, offset); 76846372Sbostic #ifdef DEBUG2 76950997Sbostic (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n", 77050997Sbostic addr, tmp1, tmp2); 77146372Sbostic #endif 77246372Sbostic 77350997Sbostic /* Allocate and return the overflow page */ 77450997Sbostic return (addr); 77546372Sbostic } 77646372Sbostic 77746372Sbostic /* 77850997Sbostic * Mark this overflow page as free. 77950997Sbostic */ 78050997Sbostic extern void 78157586Sbostic __free_ovflpage(hashp, obufp) 78257586Sbostic HTAB *hashp; 78350997Sbostic BUFHEAD *obufp; 78446372Sbostic { 78550997Sbostic register u_short addr; 78650997Sbostic u_long *freep; 78750997Sbostic int bit_address, free_page, free_bit; 78850997Sbostic u_short ndx; 78946372Sbostic 79050997Sbostic addr = obufp->addr; 79146372Sbostic #ifdef DEBUG1 79250997Sbostic (void)fprintf(stderr, "Freeing %d\n", addr); 79346372Sbostic #endif 79450997Sbostic ndx = (((u_short)addr) >> SPLITSHIFT); 79550997Sbostic bit_address = 79650997Sbostic (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1; 79751061Sbostic if (bit_address < hashp->LAST_FREED) 79851061Sbostic hashp->LAST_FREED = bit_address; 79950997Sbostic free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT)); 80050997Sbostic free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1); 80146372Sbostic 80250997Sbostic if (!(freep = hashp->mapp[free_page])) 80357586Sbostic freep = fetch_bitmap(hashp, free_page); 80450997Sbostic #ifdef DEBUG 80550997Sbostic /* 80650997Sbostic * This had better never happen. It means we tried to read a bitmap 80750997Sbostic * that has already had overflow pages allocated off it, and we 80850997Sbostic * failed to read it from the file. 80950997Sbostic */ 81050997Sbostic if (!freep) 81150997Sbostic assert(0); 81250997Sbostic #endif 81350997Sbostic CLRBIT(freep, free_bit); 81446372Sbostic #ifdef DEBUG2 81550997Sbostic (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n", 81650997Sbostic obufp->addr, free_bit, free_page); 81746372Sbostic #endif 81857586Sbostic __reclaim_buf(hashp, obufp); 81946372Sbostic } 82046372Sbostic 82146372Sbostic /* 82250997Sbostic * Returns: 82350997Sbostic * 0 success 82450997Sbostic * -1 failure 82550997Sbostic */ 82646372Sbostic static int 82757586Sbostic open_temp(hashp) 82857586Sbostic HTAB *hashp; 82946372Sbostic { 83050997Sbostic sigset_t set, oset; 83150997Sbostic static char namestr[] = "_hashXXXXXX"; 83246372Sbostic 83350997Sbostic /* Block signals; make sure file goes away at process exit. */ 83455315Sbostic (void)sigfillset(&set); 83550997Sbostic (void)sigprocmask(SIG_BLOCK, &set, &oset); 83650997Sbostic if ((hashp->fp = mkstemp(namestr)) != -1) { 83750997Sbostic (void)unlink(namestr); 83850997Sbostic (void)fcntl(hashp->fp, F_SETFD, 1); 83950997Sbostic } 84050997Sbostic (void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL); 84150997Sbostic return (hashp->fp != -1 ? 0 : -1); 84246372Sbostic } 84346372Sbostic 84450997Sbostic /* 84550997Sbostic * We have to know that the key will fit, but the last entry on the page is 84650997Sbostic * an overflow pair, so we need to shift things. 84750997Sbostic */ 84846372Sbostic static void 84950997Sbostic squeeze_key(sp, key, val) 85050997Sbostic u_short *sp; 85150997Sbostic const DBT *key, *val; 85246372Sbostic { 85350997Sbostic register char *p; 85450997Sbostic u_short free_space, n, off, pageno; 85546372Sbostic 85650997Sbostic p = (char *)sp; 85750997Sbostic n = sp[0]; 85850997Sbostic free_space = FREESPACE(sp); 85950997Sbostic off = OFFSET(sp); 86046372Sbostic 86150997Sbostic pageno = sp[n - 1]; 86250997Sbostic off -= key->size; 86350997Sbostic sp[n - 1] = off; 86450997Sbostic bcopy(key->data, p + off, key->size); 86550997Sbostic off -= val->size; 86650997Sbostic sp[n] = off; 86750997Sbostic bcopy(val->data, p + off, val->size); 86850997Sbostic sp[0] = n + 2; 86950997Sbostic sp[n + 1] = pageno; 87050997Sbostic sp[n + 2] = OVFLPAGE; 87150997Sbostic FREESPACE(sp) = free_space - PAIRSIZE(key, val); 87250997Sbostic OFFSET(sp) = off; 87346372Sbostic } 87446372Sbostic 87547250Sbostic static u_long * 87657586Sbostic fetch_bitmap(hashp, ndx) 87757586Sbostic HTAB *hashp; 87850997Sbostic int ndx; 87947250Sbostic { 88050997Sbostic if (ndx >= hashp->nmaps || 88150997Sbostic !(hashp->mapp[ndx] = malloc(hashp->BSIZE)) || 88257586Sbostic __get_page(hashp, (char *)hashp->mapp[ndx], 88350997Sbostic hashp->BITMAPS[ndx], 0, 1, 1)) 88450997Sbostic return (NULL); 88550997Sbostic return (hashp->mapp[ndx]); 88650997Sbostic } 88747250Sbostic 88846372Sbostic #ifdef DEBUG4 88950997Sbostic int 89050997Sbostic print_chain(addr) 89150997Sbostic int addr; 89246372Sbostic { 89350997Sbostic BUFHEAD *bufp; 89450997Sbostic short *bp, oaddr; 89546372Sbostic 89650997Sbostic (void)fprintf(stderr, "%d ", addr); 89757586Sbostic bufp = __get_buf(hashp, addr, NULL, 0); 89846372Sbostic bp = (short *)bufp->page; 89950997Sbostic while (bp[0] && ((bp[bp[0]] == OVFLPAGE) || 90050997Sbostic ((bp[0] > 2) && bp[2] < REAL_KEY))) { 90150997Sbostic oaddr = bp[bp[0] - 1]; 90250997Sbostic (void)fprintf(stderr, "%d ", (int)oaddr); 90357586Sbostic bufp = __get_buf(hashp, (int)oaddr, bufp, 0); 90446372Sbostic bp = (short *)bufp->page; 90546372Sbostic } 90650997Sbostic (void)fprintf(stderr, "\n"); 90746372Sbostic } 90846372Sbostic #endif 909