xref: /freebsd/lib/libc/db/hash/hash.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Margo Seltzer.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)hash.c	8.9 (Berkeley) 6/16/94";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include "namespace.h"
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 #ifdef DEBUG
54 #include <assert.h>
55 #endif
56 #include "un-namespace.h"
57 
58 #include <db.h>
59 #include "hash.h"
60 #include "page.h"
61 #include "extern.h"
62 
63 static int   alloc_segs(HTAB *, int);
64 static int   flush_meta(HTAB *);
65 static int   hash_access(HTAB *, ACTION, DBT *, DBT *);
66 static int   hash_close(DB *);
67 static int   hash_delete(const DB *, const DBT *, u_int32_t);
68 static int   hash_fd(const DB *);
69 static int   hash_get(const DB *, const DBT *, DBT *, u_int32_t);
70 static int   hash_put(const DB *, DBT *, const DBT *, u_int32_t);
71 static void *hash_realloc(SEGMENT **, int, int);
72 static int   hash_seq(const DB *, DBT *, DBT *, u_int32_t);
73 static int   hash_sync(const DB *, u_int32_t);
74 static int   hdestroy(HTAB *);
75 static HTAB *init_hash(HTAB *, const char *, HASHINFO *);
76 static int   init_htab(HTAB *, int);
77 #if BYTE_ORDER == LITTLE_ENDIAN
78 static void  swap_header(HTAB *);
79 static void  swap_header_copy(HASHHDR *, HASHHDR *);
80 #endif
81 
82 /* Fast arithmetic, relying on powers of 2, */
83 #define MOD(x, y)		((x) & ((y) - 1))
84 
85 #define RETURN_ERROR(ERR, LOC)	{ save_errno = ERR; goto LOC; }
86 
87 /* Return values */
88 #define	SUCCESS	 (0)
89 #define	ERROR	(-1)
90 #define	ABNORMAL (1)
91 
92 #ifdef HASH_STATISTICS
93 int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
94 #endif
95 
96 /************************** INTERFACE ROUTINES ***************************/
97 /* OPEN/CLOSE */
98 
99 extern DB *
100 __hash_open(file, flags, mode, info, dflags)
101 	const char *file;
102 	int flags, mode, dflags;
103 	const HASHINFO *info;	/* Special directives for create */
104 {
105 	HTAB *hashp;
106 	struct stat statbuf;
107 	DB *dbp;
108 	int bpages, hdrsize, new_table, nsegs, save_errno;
109 
110 	if ((flags & O_ACCMODE) == O_WRONLY) {
111 		errno = EINVAL;
112 		return (NULL);
113 	}
114 
115 	if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
116 		return (NULL);
117 	hashp->fp = -1;
118 
119 	/*
120 	 * Even if user wants write only, we need to be able to read
121 	 * the actual file, so we need to open it read/write. But, the
122 	 * field in the hashp structure needs to be accurate so that
123 	 * we can check accesses.
124 	 */
125 	hashp->flags = flags;
126 
127 	new_table = 0;
128 	if (!file || (flags & O_TRUNC) ||
129 	    (stat(file, &statbuf) && (errno == ENOENT))) {
130 		if (errno == ENOENT)
131 			errno = 0; /* Just in case someone looks at errno */
132 		new_table = 1;
133 	}
134 	if (file) {
135 		if ((hashp->fp = _open(file, flags, mode)) == -1)
136 			RETURN_ERROR(errno, error0);
137 
138 		/* if the .db file is empty, and we had permission to create
139 		   a new .db file, then reinitialize the database */
140 		if ((flags & O_CREAT) &&
141 		     _fstat(hashp->fp, &statbuf) == 0 && statbuf.st_size == 0)
142 			new_table = 1;
143 
144 		(void)_fcntl(hashp->fp, F_SETFD, 1);
145 	}
146 	if (new_table) {
147 		if (!(hashp = init_hash(hashp, file, (HASHINFO *)info)))
148 			RETURN_ERROR(errno, error1);
149 	} else {
150 		/* Table already exists */
151 		if (info && info->hash)
152 			hashp->hash = info->hash;
153 		else
154 			hashp->hash = __default_hash;
155 
156 		hdrsize = _read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
157 #if BYTE_ORDER == LITTLE_ENDIAN
158 		swap_header(hashp);
159 #endif
160 		if (hdrsize == -1)
161 			RETURN_ERROR(errno, error1);
162 		if (hdrsize != sizeof(HASHHDR))
163 			RETURN_ERROR(EFTYPE, error1);
164 		/* Verify file type, versions and hash function */
165 		if (hashp->MAGIC != HASHMAGIC)
166 			RETURN_ERROR(EFTYPE, error1);
167 #define	OLDHASHVERSION	1
168 		if (hashp->VERSION != HASHVERSION &&
169 		    hashp->VERSION != OLDHASHVERSION)
170 			RETURN_ERROR(EFTYPE, error1);
171 		if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
172 			RETURN_ERROR(EFTYPE, error1);
173 		/*
174 		 * Figure out how many segments we need.  Max_Bucket is the
175 		 * maximum bucket number, so the number of buckets is
176 		 * max_bucket + 1.
177 		 */
178 		nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
179 			 hashp->SGSIZE;
180 		hashp->nsegs = 0;
181 		if (alloc_segs(hashp, nsegs))
182 			/*
183 			 * If alloc_segs fails, table will have been destroyed
184 			 * and errno will have been set.
185 			 */
186 			return (NULL);
187 		/* Read in bitmaps */
188 		bpages = (hashp->SPARES[hashp->OVFL_POINT] +
189 		    (hashp->BSIZE << BYTE_SHIFT) - 1) >>
190 		    (hashp->BSHIFT + BYTE_SHIFT);
191 
192 		hashp->nmaps = bpages;
193 		(void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *));
194 	}
195 
196 	/* Initialize Buffer Manager */
197 	if (info && info->cachesize)
198 		__buf_init(hashp, info->cachesize);
199 	else
200 		__buf_init(hashp, DEF_BUFSIZE);
201 
202 	hashp->new_file = new_table;
203 	hashp->save_file = file && (hashp->flags & O_RDWR);
204 	hashp->cbucket = -1;
205 	if (!(dbp = (DB *)malloc(sizeof(DB)))) {
206 		save_errno = errno;
207 		hdestroy(hashp);
208 		errno = save_errno;
209 		return (NULL);
210 	}
211 	dbp->internal = hashp;
212 	dbp->close = hash_close;
213 	dbp->del = hash_delete;
214 	dbp->fd = hash_fd;
215 	dbp->get = hash_get;
216 	dbp->put = hash_put;
217 	dbp->seq = hash_seq;
218 	dbp->sync = hash_sync;
219 	dbp->type = DB_HASH;
220 
221 #ifdef DEBUG
222 	(void)fprintf(stderr,
223 "%s\n%s%x\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
224 	    "init_htab:",
225 	    "TABLE POINTER   ", hashp,
226 	    "BUCKET SIZE     ", hashp->BSIZE,
227 	    "BUCKET SHIFT    ", hashp->BSHIFT,
228 	    "DIRECTORY SIZE  ", hashp->DSIZE,
229 	    "SEGMENT SIZE    ", hashp->SGSIZE,
230 	    "SEGMENT SHIFT   ", hashp->SSHIFT,
231 	    "FILL FACTOR     ", hashp->FFACTOR,
232 	    "MAX BUCKET      ", hashp->MAX_BUCKET,
233 	    "OVFL POINT	     ", hashp->OVFL_POINT,
234 	    "LAST FREED      ", hashp->LAST_FREED,
235 	    "HIGH MASK       ", hashp->HIGH_MASK,
236 	    "LOW  MASK       ", hashp->LOW_MASK,
237 	    "NSEGS           ", hashp->nsegs,
238 	    "NKEYS           ", hashp->NKEYS);
239 #endif
240 #ifdef HASH_STATISTICS
241 	hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
242 #endif
243 	return (dbp);
244 
245 error1:
246 	if (hashp != NULL)
247 		(void)_close(hashp->fp);
248 
249 error0:
250 	free(hashp);
251 	errno = save_errno;
252 	return (NULL);
253 }
254 
255 static int
256 hash_close(dbp)
257 	DB *dbp;
258 {
259 	HTAB *hashp;
260 	int retval;
261 
262 	if (!dbp)
263 		return (ERROR);
264 
265 	hashp = (HTAB *)dbp->internal;
266 	retval = hdestroy(hashp);
267 	free(dbp);
268 	return (retval);
269 }
270 
271 static int
272 hash_fd(dbp)
273 	const DB *dbp;
274 {
275 	HTAB *hashp;
276 
277 	if (!dbp)
278 		return (ERROR);
279 
280 	hashp = (HTAB *)dbp->internal;
281 	if (hashp->fp == -1) {
282 		errno = ENOENT;
283 		return (-1);
284 	}
285 	return (hashp->fp);
286 }
287 
288 /************************** LOCAL CREATION ROUTINES **********************/
289 static HTAB *
290 init_hash(hashp, file, info)
291 	HTAB *hashp;
292 	const char *file;
293 	HASHINFO *info;
294 {
295 	struct stat statbuf;
296 	int nelem;
297 
298 	nelem = 1;
299 	hashp->NKEYS = 0;
300 	hashp->LORDER = BYTE_ORDER;
301 	hashp->BSIZE = DEF_BUCKET_SIZE;
302 	hashp->BSHIFT = DEF_BUCKET_SHIFT;
303 	hashp->SGSIZE = DEF_SEGSIZE;
304 	hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
305 	hashp->DSIZE = DEF_DIRSIZE;
306 	hashp->FFACTOR = DEF_FFACTOR;
307 	hashp->hash = __default_hash;
308 	memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
309 	memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
310 
311 	/* Fix bucket size to be optimal for file system */
312 	if (file != NULL) {
313 		if (stat(file, &statbuf))
314 			return (NULL);
315 		hashp->BSIZE = statbuf.st_blksize;
316 		hashp->BSHIFT = __log2(hashp->BSIZE);
317 	}
318 
319 	if (info) {
320 		if (info->bsize) {
321 			/* Round pagesize up to power of 2 */
322 			hashp->BSHIFT = __log2(info->bsize);
323 			hashp->BSIZE = 1 << hashp->BSHIFT;
324 			if (hashp->BSIZE > MAX_BSIZE) {
325 				errno = EINVAL;
326 				return (NULL);
327 			}
328 		}
329 		if (info->ffactor)
330 			hashp->FFACTOR = info->ffactor;
331 		if (info->hash)
332 			hashp->hash = info->hash;
333 		if (info->nelem)
334 			nelem = info->nelem;
335 		if (info->lorder) {
336 			if (info->lorder != BIG_ENDIAN &&
337 			    info->lorder != LITTLE_ENDIAN) {
338 				errno = EINVAL;
339 				return (NULL);
340 			}
341 			hashp->LORDER = info->lorder;
342 		}
343 	}
344 	/* init_htab should destroy the table and set errno if it fails */
345 	if (init_htab(hashp, nelem))
346 		return (NULL);
347 	else
348 		return (hashp);
349 }
350 /*
351  * This calls alloc_segs which may run out of memory.  Alloc_segs will destroy
352  * the table and set errno, so we just pass the error information along.
353  *
354  * Returns 0 on No Error
355  */
356 static int
357 init_htab(hashp, nelem)
358 	HTAB *hashp;
359 	int nelem;
360 {
361 	int nbuckets, nsegs;
362 	int l2;
363 
364 	/*
365 	 * Divide number of elements by the fill factor and determine a
366 	 * desired number of buckets.  Allocate space for the next greater
367 	 * power of two number of buckets.
368 	 */
369 	nelem = (nelem - 1) / hashp->FFACTOR + 1;
370 
371 	l2 = __log2(MAX(nelem, 2));
372 	nbuckets = 1 << l2;
373 
374 	hashp->SPARES[l2] = l2 + 1;
375 	hashp->SPARES[l2 + 1] = l2 + 1;
376 	hashp->OVFL_POINT = l2;
377 	hashp->LAST_FREED = 2;
378 
379 	/* First bitmap page is at: splitpoint l2 page offset 1 */
380 	if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
381 		return (-1);
382 
383 	hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
384 	hashp->HIGH_MASK = (nbuckets << 1) - 1;
385 	hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
386 	    hashp->BSHIFT) + 1;
387 
388 	nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
389 	nsegs = 1 << __log2(nsegs);
390 
391 	if (nsegs > hashp->DSIZE)
392 		hashp->DSIZE = nsegs;
393 	return (alloc_segs(hashp, nsegs));
394 }
395 
396 /********************** DESTROY/CLOSE ROUTINES ************************/
397 
398 /*
399  * Flushes any changes to the file if necessary and destroys the hashp
400  * structure, freeing all allocated space.
401  */
402 static int
403 hdestroy(hashp)
404 	HTAB *hashp;
405 {
406 	int i, save_errno;
407 
408 	save_errno = 0;
409 
410 #ifdef HASH_STATISTICS
411 	(void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n",
412 	    hash_accesses, hash_collisions);
413 	(void)fprintf(stderr, "hdestroy: expansions %ld\n",
414 	    hash_expansions);
415 	(void)fprintf(stderr, "hdestroy: overflows %ld\n",
416 	    hash_overflows);
417 	(void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n",
418 	    hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
419 
420 	for (i = 0; i < NCACHED; i++)
421 		(void)fprintf(stderr,
422 		    "spares[%d] = %d\n", i, hashp->SPARES[i]);
423 #endif
424 	/*
425 	 * Call on buffer manager to free buffers, and if required,
426 	 * write them to disk.
427 	 */
428 	if (__buf_free(hashp, 1, hashp->save_file))
429 		save_errno = errno;
430 	if (hashp->dir) {
431 		free(*hashp->dir);	/* Free initial segments */
432 		/* Free extra segments */
433 		while (hashp->exsegs--)
434 			free(hashp->dir[--hashp->nsegs]);
435 		free(hashp->dir);
436 	}
437 	if (flush_meta(hashp) && !save_errno)
438 		save_errno = errno;
439 	/* Free Bigmaps */
440 	for (i = 0; i < hashp->nmaps; i++)
441 		if (hashp->mapp[i])
442 			free(hashp->mapp[i]);
443 
444 	if (hashp->fp != -1)
445 		(void)_close(hashp->fp);
446 
447 	free(hashp);
448 
449 	if (save_errno) {
450 		errno = save_errno;
451 		return (ERROR);
452 	}
453 	return (SUCCESS);
454 }
455 /*
456  * Write modified pages to disk
457  *
458  * Returns:
459  *	 0 == OK
460  *	-1 ERROR
461  */
462 static int
463 hash_sync(dbp, flags)
464 	const DB *dbp;
465 	u_int32_t flags;
466 {
467 	HTAB *hashp;
468 
469 	if (flags != 0) {
470 		errno = EINVAL;
471 		return (ERROR);
472 	}
473 
474 	if (!dbp)
475 		return (ERROR);
476 
477 	hashp = (HTAB *)dbp->internal;
478 	if (!hashp->save_file)
479 		return (0);
480 	if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
481 		return (ERROR);
482 	hashp->new_file = 0;
483 	return (0);
484 }
485 
486 /*
487  * Returns:
488  *	 0 == OK
489  *	-1 indicates that errno should be set
490  */
491 static int
492 flush_meta(hashp)
493 	HTAB *hashp;
494 {
495 	HASHHDR *whdrp;
496 #if BYTE_ORDER == LITTLE_ENDIAN
497 	HASHHDR whdr;
498 #endif
499 	int fp, i, wsize;
500 
501 	if (!hashp->save_file)
502 		return (0);
503 	hashp->MAGIC = HASHMAGIC;
504 	hashp->VERSION = HASHVERSION;
505 	hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
506 
507 	fp = hashp->fp;
508 	whdrp = &hashp->hdr;
509 #if BYTE_ORDER == LITTLE_ENDIAN
510 	whdrp = &whdr;
511 	swap_header_copy(&hashp->hdr, whdrp);
512 #endif
513 	if ((lseek(fp, (off_t)0, SEEK_SET) == -1) ||
514 	    ((wsize = _write(fp, whdrp, sizeof(HASHHDR))) == -1))
515 		return (-1);
516 	else
517 		if (wsize != sizeof(HASHHDR)) {
518 			errno = EFTYPE;
519 			hashp->error = errno;
520 			return (-1);
521 		}
522 	for (i = 0; i < NCACHED; i++)
523 		if (hashp->mapp[i])
524 			if (__put_page(hashp, (char *)hashp->mapp[i],
525 				hashp->BITMAPS[i], 0, 1))
526 				return (-1);
527 	return (0);
528 }
529 
530 /*******************************SEARCH ROUTINES *****************************/
531 /*
532  * All the access routines return
533  *
534  * Returns:
535  *	 0 on SUCCESS
536  *	 1 to indicate an external ERROR (i.e. key not found, etc)
537  *	-1 to indicate an internal ERROR (i.e. out of memory, etc)
538  */
539 static int
540 hash_get(dbp, key, data, flag)
541 	const DB *dbp;
542 	const DBT *key;
543 	DBT *data;
544 	u_int32_t flag;
545 {
546 	HTAB *hashp;
547 
548 	hashp = (HTAB *)dbp->internal;
549 	if (flag) {
550 		hashp->error = errno = EINVAL;
551 		return (ERROR);
552 	}
553 	return (hash_access(hashp, HASH_GET, (DBT *)key, data));
554 }
555 
556 static int
557 hash_put(dbp, key, data, flag)
558 	const DB *dbp;
559 	DBT *key;
560 	const DBT *data;
561 	u_int32_t flag;
562 {
563 	HTAB *hashp;
564 
565 	hashp = (HTAB *)dbp->internal;
566 	if (flag && flag != R_NOOVERWRITE) {
567 		hashp->error = EINVAL;
568 		errno = EINVAL;
569 		return (ERROR);
570 	}
571 	if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
572 		hashp->error = errno = EPERM;
573 		return (ERROR);
574 	}
575 	return (hash_access(hashp, flag == R_NOOVERWRITE ?
576 	    HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data));
577 }
578 
579 static int
580 hash_delete(dbp, key, flag)
581 	const DB *dbp;
582 	const DBT *key;
583 	u_int32_t flag;		/* Ignored */
584 {
585 	HTAB *hashp;
586 
587 	hashp = (HTAB *)dbp->internal;
588 	if (flag && flag != R_CURSOR) {
589 		hashp->error = errno = EINVAL;
590 		return (ERROR);
591 	}
592 	if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
593 		hashp->error = errno = EPERM;
594 		return (ERROR);
595 	}
596 	return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
597 }
598 
599 /*
600  * Assume that hashp has been set in wrapper routine.
601  */
602 static int
603 hash_access(hashp, action, key, val)
604 	HTAB *hashp;
605 	ACTION action;
606 	DBT *key, *val;
607 {
608 	BUFHEAD *rbufp;
609 	BUFHEAD *bufp, *save_bufp;
610 	u_int16_t *bp;
611 	int n, ndx, off, size;
612 	char *kp;
613 	u_int16_t pageno;
614 
615 #ifdef HASH_STATISTICS
616 	hash_accesses++;
617 #endif
618 
619 	off = hashp->BSIZE;
620 	size = key->size;
621 	kp = (char *)key->data;
622 	rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
623 	if (!rbufp)
624 		return (ERROR);
625 	save_bufp = rbufp;
626 
627 	/* Pin the bucket chain */
628 	rbufp->flags |= BUF_PIN;
629 	for (bp = (u_int16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
630 		if (bp[1] >= REAL_KEY) {
631 			/* Real key/data pair */
632 			if (size == off - *bp &&
633 			    memcmp(kp, rbufp->page + *bp, size) == 0)
634 				goto found;
635 			off = bp[1];
636 #ifdef HASH_STATISTICS
637 			hash_collisions++;
638 #endif
639 			bp += 2;
640 			ndx += 2;
641 		} else if (bp[1] == OVFLPAGE) {
642 			rbufp = __get_buf(hashp, *bp, rbufp, 0);
643 			if (!rbufp) {
644 				save_bufp->flags &= ~BUF_PIN;
645 				return (ERROR);
646 			}
647 			/* FOR LOOP INIT */
648 			bp = (u_int16_t *)rbufp->page;
649 			n = *bp++;
650 			ndx = 1;
651 			off = hashp->BSIZE;
652 		} else if (bp[1] < REAL_KEY) {
653 			if ((ndx =
654 			    __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
655 				goto found;
656 			if (ndx == -2) {
657 				bufp = rbufp;
658 				if (!(pageno =
659 				    __find_last_page(hashp, &bufp))) {
660 					ndx = 0;
661 					rbufp = bufp;
662 					break;	/* FOR */
663 				}
664 				rbufp = __get_buf(hashp, pageno, bufp, 0);
665 				if (!rbufp) {
666 					save_bufp->flags &= ~BUF_PIN;
667 					return (ERROR);
668 				}
669 				/* FOR LOOP INIT */
670 				bp = (u_int16_t *)rbufp->page;
671 				n = *bp++;
672 				ndx = 1;
673 				off = hashp->BSIZE;
674 			} else {
675 				save_bufp->flags &= ~BUF_PIN;
676 				return (ERROR);
677 			}
678 		}
679 
680 	/* Not found */
681 	switch (action) {
682 	case HASH_PUT:
683 	case HASH_PUTNEW:
684 		if (__addel(hashp, rbufp, key, val)) {
685 			save_bufp->flags &= ~BUF_PIN;
686 			return (ERROR);
687 		} else {
688 			save_bufp->flags &= ~BUF_PIN;
689 			return (SUCCESS);
690 		}
691 	case HASH_GET:
692 	case HASH_DELETE:
693 	default:
694 		save_bufp->flags &= ~BUF_PIN;
695 		return (ABNORMAL);
696 	}
697 
698 found:
699 	switch (action) {
700 	case HASH_PUTNEW:
701 		save_bufp->flags &= ~BUF_PIN;
702 		return (ABNORMAL);
703 	case HASH_GET:
704 		bp = (u_int16_t *)rbufp->page;
705 		if (bp[ndx + 1] < REAL_KEY) {
706 			if (__big_return(hashp, rbufp, ndx, val, 0))
707 				return (ERROR);
708 		} else {
709 			val->data = (u_char *)rbufp->page + (int)bp[ndx + 1];
710 			val->size = bp[ndx] - bp[ndx + 1];
711 		}
712 		break;
713 	case HASH_PUT:
714 		if ((__delpair(hashp, rbufp, ndx)) ||
715 		    (__addel(hashp, rbufp, key, val))) {
716 			save_bufp->flags &= ~BUF_PIN;
717 			return (ERROR);
718 		}
719 		break;
720 	case HASH_DELETE:
721 		if (__delpair(hashp, rbufp, ndx))
722 			return (ERROR);
723 		break;
724 	default:
725 		abort();
726 	}
727 	save_bufp->flags &= ~BUF_PIN;
728 	return (SUCCESS);
729 }
730 
731 static int
732 hash_seq(dbp, key, data, flag)
733 	const DB *dbp;
734 	DBT *key, *data;
735 	u_int32_t flag;
736 {
737 	u_int32_t bucket;
738 	BUFHEAD *bufp;
739 	HTAB *hashp;
740 	u_int16_t *bp, ndx;
741 
742 	hashp = (HTAB *)dbp->internal;
743 	if (flag && flag != R_FIRST && flag != R_NEXT) {
744 		hashp->error = errno = EINVAL;
745 		return (ERROR);
746 	}
747 #ifdef HASH_STATISTICS
748 	hash_accesses++;
749 #endif
750 	if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
751 		hashp->cbucket = 0;
752 		hashp->cndx = 1;
753 		hashp->cpage = NULL;
754 	}
755 
756 	for (bp = NULL; !bp || !bp[0]; ) {
757 		if (!(bufp = hashp->cpage)) {
758 			for (bucket = hashp->cbucket;
759 			    bucket <= hashp->MAX_BUCKET;
760 			    bucket++, hashp->cndx = 1) {
761 				bufp = __get_buf(hashp, bucket, NULL, 0);
762 				if (!bufp)
763 					return (ERROR);
764 				hashp->cpage = bufp;
765 				bp = (u_int16_t *)bufp->page;
766 				if (bp[0])
767 					break;
768 			}
769 			hashp->cbucket = bucket;
770 			if (hashp->cbucket > hashp->MAX_BUCKET) {
771 				hashp->cbucket = -1;
772 				return (ABNORMAL);
773 			}
774 		} else
775 			bp = (u_int16_t *)hashp->cpage->page;
776 
777 #ifdef DEBUG
778 		assert(bp);
779 		assert(bufp);
780 #endif
781 		while (bp[hashp->cndx + 1] == OVFLPAGE) {
782 			bufp = hashp->cpage =
783 			    __get_buf(hashp, bp[hashp->cndx], bufp, 0);
784 			if (!bufp)
785 				return (ERROR);
786 			bp = (u_int16_t *)(bufp->page);
787 			hashp->cndx = 1;
788 		}
789 		if (!bp[0]) {
790 			hashp->cpage = NULL;
791 			++hashp->cbucket;
792 		}
793 	}
794 	ndx = hashp->cndx;
795 	if (bp[ndx + 1] < REAL_KEY) {
796 		if (__big_keydata(hashp, bufp, key, data, 1))
797 			return (ERROR);
798 	} else {
799 		key->data = (u_char *)hashp->cpage->page + bp[ndx];
800 		key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
801 		data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
802 		data->size = bp[ndx] - bp[ndx + 1];
803 		ndx += 2;
804 		if (ndx > bp[0]) {
805 			hashp->cpage = NULL;
806 			hashp->cbucket++;
807 			hashp->cndx = 1;
808 		} else
809 			hashp->cndx = ndx;
810 	}
811 	return (SUCCESS);
812 }
813 
814 /********************************* UTILITIES ************************/
815 
816 /*
817  * Returns:
818  *	 0 ==> OK
819  *	-1 ==> Error
820  */
821 extern int
822 __expand_table(hashp)
823 	HTAB *hashp;
824 {
825 	u_int32_t old_bucket, new_bucket;
826 	int dirsize, new_segnum, spare_ndx;
827 
828 #ifdef HASH_STATISTICS
829 	hash_expansions++;
830 #endif
831 	new_bucket = ++hashp->MAX_BUCKET;
832 	old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
833 
834 	new_segnum = new_bucket >> hashp->SSHIFT;
835 
836 	/* Check if we need a new segment */
837 	if (new_segnum >= hashp->nsegs) {
838 		/* Check if we need to expand directory */
839 		if (new_segnum >= hashp->DSIZE) {
840 			/* Reallocate directory */
841 			dirsize = hashp->DSIZE * sizeof(SEGMENT *);
842 			if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
843 				return (-1);
844 			hashp->DSIZE = dirsize << 1;
845 		}
846 		if ((hashp->dir[new_segnum] =
847 		    (SEGMENT)calloc(hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
848 			return (-1);
849 		hashp->exsegs++;
850 		hashp->nsegs++;
851 	}
852 	/*
853 	 * If the split point is increasing (MAX_BUCKET's log base 2
854 	 * * increases), we need to copy the current contents of the spare
855 	 * split bucket to the next bucket.
856 	 */
857 	spare_ndx = __log2(hashp->MAX_BUCKET + 1);
858 	if (spare_ndx > hashp->OVFL_POINT) {
859 		hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
860 		hashp->OVFL_POINT = spare_ndx;
861 	}
862 
863 	if (new_bucket > hashp->HIGH_MASK) {
864 		/* Starting a new doubling */
865 		hashp->LOW_MASK = hashp->HIGH_MASK;
866 		hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
867 	}
868 	/* Relocate records to the new bucket */
869 	return (__split_page(hashp, old_bucket, new_bucket));
870 }
871 
872 /*
873  * If realloc guarantees that the pointer is not destroyed if the realloc
874  * fails, then this routine can go away.
875  */
876 static void *
877 hash_realloc(p_ptr, oldsize, newsize)
878 	SEGMENT **p_ptr;
879 	int oldsize, newsize;
880 {
881 	void *p;
882 
883 	if ( (p = malloc(newsize)) ) {
884 		memmove(p, *p_ptr, oldsize);
885 		memset((char *)p + oldsize, 0, newsize - oldsize);
886 		free(*p_ptr);
887 		*p_ptr = p;
888 	}
889 	return (p);
890 }
891 
892 extern u_int32_t
893 __call_hash(hashp, k, len)
894 	HTAB *hashp;
895 	char *k;
896 	int len;
897 {
898 	int n, bucket;
899 
900 	n = hashp->hash(k, len);
901 	bucket = n & hashp->HIGH_MASK;
902 	if (bucket > hashp->MAX_BUCKET)
903 		bucket = bucket & hashp->LOW_MASK;
904 	return (bucket);
905 }
906 
907 /*
908  * Allocate segment table.  On error, destroy the table and set errno.
909  *
910  * Returns 0 on success
911  */
912 static int
913 alloc_segs(hashp, nsegs)
914 	HTAB *hashp;
915 	int nsegs;
916 {
917 	int i;
918 	SEGMENT store;
919 
920 	int save_errno;
921 
922 	if ((hashp->dir =
923 	    (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
924 		save_errno = errno;
925 		(void)hdestroy(hashp);
926 		errno = save_errno;
927 		return (-1);
928 	}
929 	/* Allocate segments */
930 	if ((store =
931 	    (SEGMENT)calloc(nsegs << hashp->SSHIFT, sizeof(SEGMENT))) == NULL) {
932 		save_errno = errno;
933 		(void)hdestroy(hashp);
934 		errno = save_errno;
935 		return (-1);
936 	}
937 	for (i = 0; i < nsegs; i++, hashp->nsegs++)
938 		hashp->dir[i] = &store[i << hashp->SSHIFT];
939 	return (0);
940 }
941 
942 #if BYTE_ORDER == LITTLE_ENDIAN
943 /*
944  * Hashp->hdr needs to be byteswapped.
945  */
946 static void
947 swap_header_copy(srcp, destp)
948 	HASHHDR *srcp, *destp;
949 {
950 	int i;
951 
952 	P_32_COPY(srcp->magic, destp->magic);
953 	P_32_COPY(srcp->version, destp->version);
954 	P_32_COPY(srcp->lorder, destp->lorder);
955 	P_32_COPY(srcp->bsize, destp->bsize);
956 	P_32_COPY(srcp->bshift, destp->bshift);
957 	P_32_COPY(srcp->dsize, destp->dsize);
958 	P_32_COPY(srcp->ssize, destp->ssize);
959 	P_32_COPY(srcp->sshift, destp->sshift);
960 	P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
961 	P_32_COPY(srcp->last_freed, destp->last_freed);
962 	P_32_COPY(srcp->max_bucket, destp->max_bucket);
963 	P_32_COPY(srcp->high_mask, destp->high_mask);
964 	P_32_COPY(srcp->low_mask, destp->low_mask);
965 	P_32_COPY(srcp->ffactor, destp->ffactor);
966 	P_32_COPY(srcp->nkeys, destp->nkeys);
967 	P_32_COPY(srcp->hdrpages, destp->hdrpages);
968 	P_32_COPY(srcp->h_charkey, destp->h_charkey);
969 	for (i = 0; i < NCACHED; i++) {
970 		P_32_COPY(srcp->spares[i], destp->spares[i]);
971 		P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
972 	}
973 }
974 
975 static void
976 swap_header(hashp)
977 	HTAB *hashp;
978 {
979 	HASHHDR *hdrp;
980 	int i;
981 
982 	hdrp = &hashp->hdr;
983 
984 	M_32_SWAP(hdrp->magic);
985 	M_32_SWAP(hdrp->version);
986 	M_32_SWAP(hdrp->lorder);
987 	M_32_SWAP(hdrp->bsize);
988 	M_32_SWAP(hdrp->bshift);
989 	M_32_SWAP(hdrp->dsize);
990 	M_32_SWAP(hdrp->ssize);
991 	M_32_SWAP(hdrp->sshift);
992 	M_32_SWAP(hdrp->ovfl_point);
993 	M_32_SWAP(hdrp->last_freed);
994 	M_32_SWAP(hdrp->max_bucket);
995 	M_32_SWAP(hdrp->high_mask);
996 	M_32_SWAP(hdrp->low_mask);
997 	M_32_SWAP(hdrp->ffactor);
998 	M_32_SWAP(hdrp->nkeys);
999 	M_32_SWAP(hdrp->hdrpages);
1000 	M_32_SWAP(hdrp->h_charkey);
1001 	for (i = 0; i < NCACHED; i++) {
1002 		M_32_SWAP(hdrp->spares[i]);
1003 		M_16_SWAP(hdrp->bitmaps[i]);
1004 	}
1005 }
1006 #endif
1007