Deleted Added
full compact
hash_page.c (190487) hash_page.c (190489)
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)hash_page.c 8.7 (Berkeley) 8/16/94";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/lib/libc/db/hash/hash_page.c 190487 2009-03-28 06:12:39Z delphij $");
37__FBSDID("$FreeBSD: head/lib/libc/db/hash/hash_page.c 190489 2009-03-28 06:23:10Z delphij $");
38
39/*
40 * PACKAGE: hashing
41 *
42 * DESCRIPTION:
43 * Page manipulation for hashing package.
44 *
45 * ROUTINES:
46 *
47 * External
48 * __get_page
49 * __add_ovflpage
50 * Internal
51 * overflow_page
52 * open_temp
53 */
54
55#include "namespace.h"
56#include <sys/param.h>
57
58#include <errno.h>
59#include <fcntl.h>
60#include <signal.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <unistd.h>
65#ifdef DEBUG
66#include <assert.h>
67#endif
68#include "un-namespace.h"
69
70#include <db.h>
71#include "hash.h"
72#include "page.h"
73#include "extern.h"
74
75static u_int32_t *fetch_bitmap(HTAB *, int);
76static u_int32_t first_free(u_int32_t);
77static int open_temp(HTAB *);
78static u_int16_t overflow_page(HTAB *);
79static void putpair(char *, const DBT *, const DBT *);
80static void squeeze_key(u_int16_t *, const DBT *, const DBT *);
81static int ugly_split(HTAB *, u_int32_t, BUFHEAD *, BUFHEAD *, int, int);
82
83#define PAGE_INIT(P) { \
84 ((u_int16_t *)(P))[0] = 0; \
85 ((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
86 ((u_int16_t *)(P))[2] = hashp->BSIZE; \
87}
88
89/*
90 * This is called AFTER we have verified that there is room on the page for
91 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
92 * stuff on.
93 */
94static void
95putpair(char *p, const DBT *key, const DBT *val)
96{
97 u_int16_t *bp, n, off;
98
99 bp = (u_int16_t *)p;
100
101 /* Enter the key first. */
102 n = bp[0];
103
104 off = OFFSET(bp) - key->size;
105 memmove(p + off, key->data, key->size);
106 bp[++n] = off;
107
108 /* Now the data. */
109 off -= val->size;
110 memmove(p + off, val->data, val->size);
111 bp[++n] = off;
112
113 /* Adjust page info. */
114 bp[0] = n;
115 bp[n + 1] = off - ((n + 3) * sizeof(u_int16_t));
116 bp[n + 2] = off;
117}
118
119/*
120 * Returns:
121 * 0 OK
122 * -1 error
123 */
124int
125__delpair(HTAB *hashp, BUFHEAD *bufp, int ndx)
126{
38
39/*
40 * PACKAGE: hashing
41 *
42 * DESCRIPTION:
43 * Page manipulation for hashing package.
44 *
45 * ROUTINES:
46 *
47 * External
48 * __get_page
49 * __add_ovflpage
50 * Internal
51 * overflow_page
52 * open_temp
53 */
54
55#include "namespace.h"
56#include <sys/param.h>
57
58#include <errno.h>
59#include <fcntl.h>
60#include <signal.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <unistd.h>
65#ifdef DEBUG
66#include <assert.h>
67#endif
68#include "un-namespace.h"
69
70#include <db.h>
71#include "hash.h"
72#include "page.h"
73#include "extern.h"
74
75static u_int32_t *fetch_bitmap(HTAB *, int);
76static u_int32_t first_free(u_int32_t);
77static int open_temp(HTAB *);
78static u_int16_t overflow_page(HTAB *);
79static void putpair(char *, const DBT *, const DBT *);
80static void squeeze_key(u_int16_t *, const DBT *, const DBT *);
81static int ugly_split(HTAB *, u_int32_t, BUFHEAD *, BUFHEAD *, int, int);
82
83#define PAGE_INIT(P) { \
84 ((u_int16_t *)(P))[0] = 0; \
85 ((u_int16_t *)(P))[1] = hashp->BSIZE - 3 * sizeof(u_int16_t); \
86 ((u_int16_t *)(P))[2] = hashp->BSIZE; \
87}
88
89/*
90 * This is called AFTER we have verified that there is room on the page for
91 * the pair (PAIRFITS has returned true) so we go right ahead and start moving
92 * stuff on.
93 */
94static void
95putpair(char *p, const DBT *key, const DBT *val)
96{
97 u_int16_t *bp, n, off;
98
99 bp = (u_int16_t *)p;
100
101 /* Enter the key first. */
102 n = bp[0];
103
104 off = OFFSET(bp) - key->size;
105 memmove(p + off, key->data, key->size);
106 bp[++n] = off;
107
108 /* Now the data. */
109 off -= val->size;
110 memmove(p + off, val->data, val->size);
111 bp[++n] = off;
112
113 /* Adjust page info. */
114 bp[0] = n;
115 bp[n + 1] = off - ((n + 3) * sizeof(u_int16_t));
116 bp[n + 2] = off;
117}
118
119/*
120 * Returns:
121 * 0 OK
122 * -1 error
123 */
124int
125__delpair(HTAB *hashp, BUFHEAD *bufp, int ndx)
126{
127 u_int16_t *bp, newoff;
127 u_int16_t *bp, newoff, pairlen;
128 int n;
128 int n;
129 u_int16_t pairlen;
130
131 bp = (u_int16_t *)bufp->page;
132 n = bp[0];
133
134 if (bp[ndx + 1] < REAL_KEY)
135 return (__big_delete(hashp, bufp));
136 if (ndx != 1)
137 newoff = bp[ndx - 1];
138 else
139 newoff = hashp->BSIZE;
140 pairlen = newoff - bp[ndx + 1];
141
142 if (ndx != (n - 1)) {
143 /* Hard Case -- need to shuffle keys */
144 int i;
145 char *src = bufp->page + (int)OFFSET(bp);
146 char *dst = src + (int)pairlen;
147 memmove(dst, src, bp[ndx + 1] - OFFSET(bp));
148
149 /* Now adjust the pointers */
150 for (i = ndx + 2; i <= n; i += 2) {
151 if (bp[i + 1] == OVFLPAGE) {
152 bp[i - 2] = bp[i];
153 bp[i - 1] = bp[i + 1];
154 } else {
155 bp[i - 2] = bp[i] + pairlen;
156 bp[i - 1] = bp[i + 1] + pairlen;
157 }
158 }
159 }
160 /* Finally adjust the page data */
161 bp[n] = OFFSET(bp) + pairlen;
162 bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(u_int16_t);
163 bp[0] = n - 2;
164 hashp->NKEYS--;
165
166 bufp->flags |= BUF_MOD;
167 return (0);
168}
169/*
170 * Returns:
171 * 0 ==> OK
172 * -1 ==> Error
173 */
174int
175__split_page(HTAB *hashp, u_int32_t obucket, u_int32_t nbucket)
176{
177 BUFHEAD *new_bufp, *old_bufp;
178 u_int16_t *ino;
179 char *np;
180 DBT key, val;
181 int n, ndx, retval;
182 u_int16_t copyto, diff, off, moved;
183 char *op;
184
185 copyto = (u_int16_t)hashp->BSIZE;
186 off = (u_int16_t)hashp->BSIZE;
187 old_bufp = __get_buf(hashp, obucket, NULL, 0);
188 if (old_bufp == NULL)
189 return (-1);
190 new_bufp = __get_buf(hashp, nbucket, NULL, 0);
191 if (new_bufp == NULL)
192 return (-1);
193
194 old_bufp->flags |= (BUF_MOD | BUF_PIN);
195 new_bufp->flags |= (BUF_MOD | BUF_PIN);
196
197 ino = (u_int16_t *)(op = old_bufp->page);
198 np = new_bufp->page;
199
200 moved = 0;
201
202 for (n = 1, ndx = 1; n < ino[0]; n += 2) {
203 if (ino[n + 1] < REAL_KEY) {
204 retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
205 (int)copyto, (int)moved);
206 old_bufp->flags &= ~BUF_PIN;
207 new_bufp->flags &= ~BUF_PIN;
208 return (retval);
209
210 }
211 key.data = (u_char *)op + ino[n];
212 key.size = off - ino[n];
213
214 if (__call_hash(hashp, key.data, key.size) == obucket) {
215 /* Don't switch page */
216 diff = copyto - off;
217 if (diff) {
218 copyto = ino[n + 1] + diff;
219 memmove(op + copyto, op + ino[n + 1],
220 off - ino[n + 1]);
221 ino[ndx] = copyto + ino[n] - ino[n + 1];
222 ino[ndx + 1] = copyto;
223 } else
224 copyto = ino[n + 1];
225 ndx += 2;
226 } else {
227 /* Switch page */
228 val.data = (u_char *)op + ino[n + 1];
229 val.size = ino[n] - ino[n + 1];
230 putpair(np, &key, &val);
231 moved += 2;
232 }
233
234 off = ino[n + 1];
235 }
236
237 /* Now clean up the page */
238 ino[0] -= moved;
239 FREESPACE(ino) = copyto - sizeof(u_int16_t) * (ino[0] + 3);
240 OFFSET(ino) = copyto;
241
242#ifdef DEBUG3
243 (void)fprintf(stderr, "split %d/%d\n",
244 ((u_int16_t *)np)[0] / 2,
245 ((u_int16_t *)op)[0] / 2);
246#endif
247 /* unpin both pages */
248 old_bufp->flags &= ~BUF_PIN;
249 new_bufp->flags &= ~BUF_PIN;
250 return (0);
251}
252
253/*
254 * Called when we encounter an overflow or big key/data page during split
255 * handling. This is special cased since we have to begin checking whether
256 * the key/data pairs fit on their respective pages and because we may need
257 * overflow pages for both the old and new pages.
258 *
259 * The first page might be a page with regular key/data pairs in which case
260 * we have a regular overflow condition and just need to go on to the next
261 * page or it might be a big key/data pair in which case we need to fix the
262 * big key/data pair.
263 *
264 * Returns:
265 * 0 ==> success
266 * -1 ==> failure
267 */
268static int
269ugly_split(HTAB *hashp,
270 u_int32_t obucket, /* Same as __split_page. */
271 BUFHEAD *old_bufp,
272 BUFHEAD *new_bufp,
273 int copyto, /* First byte on page which contains key/data values. */
274 int moved) /* Number of pairs moved to new page. */
275{
276 BUFHEAD *bufp; /* Buffer header for ino */
277 u_int16_t *ino; /* Page keys come off of */
278 u_int16_t *np; /* New page */
279 u_int16_t *op; /* Page keys go on to if they aren't moving */
280
281 BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
282 DBT key, val;
283 SPLIT_RETURN ret;
284 u_int16_t n, off, ov_addr, scopyto;
285 char *cino; /* Character value of ino */
286
287 bufp = old_bufp;
288 ino = (u_int16_t *)old_bufp->page;
289 np = (u_int16_t *)new_bufp->page;
290 op = (u_int16_t *)old_bufp->page;
291 last_bfp = NULL;
292 scopyto = (u_int16_t)copyto; /* ANSI */
293
294 n = ino[0] - 1;
295 while (n < ino[0]) {
296 if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
297 if (__big_split(hashp, old_bufp,
298 new_bufp, bufp, bufp->addr, obucket, &ret))
299 return (-1);
300 old_bufp = ret.oldp;
301 if (!old_bufp)
302 return (-1);
303 op = (u_int16_t *)old_bufp->page;
304 new_bufp = ret.newp;
305 if (!new_bufp)
306 return (-1);
307 np = (u_int16_t *)new_bufp->page;
308 bufp = ret.nextp;
309 if (!bufp)
310 return (0);
311 cino = (char *)bufp->page;
312 ino = (u_int16_t *)cino;
313 last_bfp = ret.nextp;
314 } else if (ino[n + 1] == OVFLPAGE) {
315 ov_addr = ino[n];
316 /*
317 * Fix up the old page -- the extra 2 are the fields
318 * which contained the overflow information.
319 */
320 ino[0] -= (moved + 2);
321 FREESPACE(ino) =
322 scopyto - sizeof(u_int16_t) * (ino[0] + 3);
323 OFFSET(ino) = scopyto;
324
325 bufp = __get_buf(hashp, ov_addr, bufp, 0);
326 if (!bufp)
327 return (-1);
328
329 ino = (u_int16_t *)bufp->page;
330 n = 1;
331 scopyto = hashp->BSIZE;
332 moved = 0;
333
334 if (last_bfp)
335 __free_ovflpage(hashp, last_bfp);
336 last_bfp = bufp;
337 }
338 /* Move regular sized pairs of there are any */
339 off = hashp->BSIZE;
340 for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
341 cino = (char *)ino;
342 key.data = (u_char *)cino + ino[n];
343 key.size = off - ino[n];
344 val.data = (u_char *)cino + ino[n + 1];
345 val.size = ino[n] - ino[n + 1];
346 off = ino[n + 1];
347
348 if (__call_hash(hashp, key.data, key.size) == obucket) {
349 /* Keep on old page */
350 if (PAIRFITS(op, (&key), (&val)))
351 putpair((char *)op, &key, &val);
352 else {
353 old_bufp =
354 __add_ovflpage(hashp, old_bufp);
355 if (!old_bufp)
356 return (-1);
357 op = (u_int16_t *)old_bufp->page;
358 putpair((char *)op, &key, &val);
359 }
360 old_bufp->flags |= BUF_MOD;
361 } else {
362 /* Move to new page */
363 if (PAIRFITS(np, (&key), (&val)))
364 putpair((char *)np, &key, &val);
365 else {
366 new_bufp =
367 __add_ovflpage(hashp, new_bufp);
368 if (!new_bufp)
369 return (-1);
370 np = (u_int16_t *)new_bufp->page;
371 putpair((char *)np, &key, &val);
372 }
373 new_bufp->flags |= BUF_MOD;
374 }
375 }
376 }
377 if (last_bfp)
378 __free_ovflpage(hashp, last_bfp);
379 return (0);
380}
381
382/*
383 * Add the given pair to the page
384 *
385 * Returns:
386 * 0 ==> OK
387 * 1 ==> failure
388 */
389int
390__addel(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
391{
392 u_int16_t *bp, *sop;
393 int do_expand;
394
395 bp = (u_int16_t *)bufp->page;
396 do_expand = 0;
397 while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
398 /* Exception case */
399 if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
400 /* This is the last page of a big key/data pair
401 and we need to add another page */
402 break;
403 else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
404 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
405 if (!bufp)
406 return (-1);
407 bp = (u_int16_t *)bufp->page;
408 } else
409 /* Try to squeeze key on this page */
410 if (FREESPACE(bp) > PAIRSIZE(key, val)) {
411 squeeze_key(bp, key, val);
412 return (0);
413 } else {
414 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
415 if (!bufp)
416 return (-1);
417 bp = (u_int16_t *)bufp->page;
418 }
419
420 if (PAIRFITS(bp, key, val))
421 putpair(bufp->page, key, val);
422 else {
423 do_expand = 1;
424 bufp = __add_ovflpage(hashp, bufp);
425 if (!bufp)
426 return (-1);
427 sop = (u_int16_t *)bufp->page;
428
429 if (PAIRFITS(sop, key, val))
430 putpair((char *)sop, key, val);
431 else
432 if (__big_insert(hashp, bufp, key, val))
433 return (-1);
434 }
435 bufp->flags |= BUF_MOD;
436 /*
437 * If the average number of keys per bucket exceeds the fill factor,
438 * expand the table.
439 */
440 hashp->NKEYS++;
441 if (do_expand ||
442 (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
443 return (__expand_table(hashp));
444 return (0);
445}
446
447/*
448 *
449 * Returns:
450 * pointer on success
451 * NULL on error
452 */
453BUFHEAD *
454__add_ovflpage(HTAB *hashp, BUFHEAD *bufp)
455{
129
130 bp = (u_int16_t *)bufp->page;
131 n = bp[0];
132
133 if (bp[ndx + 1] < REAL_KEY)
134 return (__big_delete(hashp, bufp));
135 if (ndx != 1)
136 newoff = bp[ndx - 1];
137 else
138 newoff = hashp->BSIZE;
139 pairlen = newoff - bp[ndx + 1];
140
141 if (ndx != (n - 1)) {
142 /* Hard Case -- need to shuffle keys */
143 int i;
144 char *src = bufp->page + (int)OFFSET(bp);
145 char *dst = src + (int)pairlen;
146 memmove(dst, src, bp[ndx + 1] - OFFSET(bp));
147
148 /* Now adjust the pointers */
149 for (i = ndx + 2; i <= n; i += 2) {
150 if (bp[i + 1] == OVFLPAGE) {
151 bp[i - 2] = bp[i];
152 bp[i - 1] = bp[i + 1];
153 } else {
154 bp[i - 2] = bp[i] + pairlen;
155 bp[i - 1] = bp[i + 1] + pairlen;
156 }
157 }
158 }
159 /* Finally adjust the page data */
160 bp[n] = OFFSET(bp) + pairlen;
161 bp[n - 1] = bp[n + 1] + pairlen + 2 * sizeof(u_int16_t);
162 bp[0] = n - 2;
163 hashp->NKEYS--;
164
165 bufp->flags |= BUF_MOD;
166 return (0);
167}
168/*
169 * Returns:
170 * 0 ==> OK
171 * -1 ==> Error
172 */
173int
174__split_page(HTAB *hashp, u_int32_t obucket, u_int32_t nbucket)
175{
176 BUFHEAD *new_bufp, *old_bufp;
177 u_int16_t *ino;
178 char *np;
179 DBT key, val;
180 int n, ndx, retval;
181 u_int16_t copyto, diff, off, moved;
182 char *op;
183
184 copyto = (u_int16_t)hashp->BSIZE;
185 off = (u_int16_t)hashp->BSIZE;
186 old_bufp = __get_buf(hashp, obucket, NULL, 0);
187 if (old_bufp == NULL)
188 return (-1);
189 new_bufp = __get_buf(hashp, nbucket, NULL, 0);
190 if (new_bufp == NULL)
191 return (-1);
192
193 old_bufp->flags |= (BUF_MOD | BUF_PIN);
194 new_bufp->flags |= (BUF_MOD | BUF_PIN);
195
196 ino = (u_int16_t *)(op = old_bufp->page);
197 np = new_bufp->page;
198
199 moved = 0;
200
201 for (n = 1, ndx = 1; n < ino[0]; n += 2) {
202 if (ino[n + 1] < REAL_KEY) {
203 retval = ugly_split(hashp, obucket, old_bufp, new_bufp,
204 (int)copyto, (int)moved);
205 old_bufp->flags &= ~BUF_PIN;
206 new_bufp->flags &= ~BUF_PIN;
207 return (retval);
208
209 }
210 key.data = (u_char *)op + ino[n];
211 key.size = off - ino[n];
212
213 if (__call_hash(hashp, key.data, key.size) == obucket) {
214 /* Don't switch page */
215 diff = copyto - off;
216 if (diff) {
217 copyto = ino[n + 1] + diff;
218 memmove(op + copyto, op + ino[n + 1],
219 off - ino[n + 1]);
220 ino[ndx] = copyto + ino[n] - ino[n + 1];
221 ino[ndx + 1] = copyto;
222 } else
223 copyto = ino[n + 1];
224 ndx += 2;
225 } else {
226 /* Switch page */
227 val.data = (u_char *)op + ino[n + 1];
228 val.size = ino[n] - ino[n + 1];
229 putpair(np, &key, &val);
230 moved += 2;
231 }
232
233 off = ino[n + 1];
234 }
235
236 /* Now clean up the page */
237 ino[0] -= moved;
238 FREESPACE(ino) = copyto - sizeof(u_int16_t) * (ino[0] + 3);
239 OFFSET(ino) = copyto;
240
241#ifdef DEBUG3
242 (void)fprintf(stderr, "split %d/%d\n",
243 ((u_int16_t *)np)[0] / 2,
244 ((u_int16_t *)op)[0] / 2);
245#endif
246 /* unpin both pages */
247 old_bufp->flags &= ~BUF_PIN;
248 new_bufp->flags &= ~BUF_PIN;
249 return (0);
250}
251
252/*
253 * Called when we encounter an overflow or big key/data page during split
254 * handling. This is special cased since we have to begin checking whether
255 * the key/data pairs fit on their respective pages and because we may need
256 * overflow pages for both the old and new pages.
257 *
258 * The first page might be a page with regular key/data pairs in which case
259 * we have a regular overflow condition and just need to go on to the next
260 * page or it might be a big key/data pair in which case we need to fix the
261 * big key/data pair.
262 *
263 * Returns:
264 * 0 ==> success
265 * -1 ==> failure
266 */
267static int
268ugly_split(HTAB *hashp,
269 u_int32_t obucket, /* Same as __split_page. */
270 BUFHEAD *old_bufp,
271 BUFHEAD *new_bufp,
272 int copyto, /* First byte on page which contains key/data values. */
273 int moved) /* Number of pairs moved to new page. */
274{
275 BUFHEAD *bufp; /* Buffer header for ino */
276 u_int16_t *ino; /* Page keys come off of */
277 u_int16_t *np; /* New page */
278 u_int16_t *op; /* Page keys go on to if they aren't moving */
279
280 BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
281 DBT key, val;
282 SPLIT_RETURN ret;
283 u_int16_t n, off, ov_addr, scopyto;
284 char *cino; /* Character value of ino */
285
286 bufp = old_bufp;
287 ino = (u_int16_t *)old_bufp->page;
288 np = (u_int16_t *)new_bufp->page;
289 op = (u_int16_t *)old_bufp->page;
290 last_bfp = NULL;
291 scopyto = (u_int16_t)copyto; /* ANSI */
292
293 n = ino[0] - 1;
294 while (n < ino[0]) {
295 if (ino[2] < REAL_KEY && ino[2] != OVFLPAGE) {
296 if (__big_split(hashp, old_bufp,
297 new_bufp, bufp, bufp->addr, obucket, &ret))
298 return (-1);
299 old_bufp = ret.oldp;
300 if (!old_bufp)
301 return (-1);
302 op = (u_int16_t *)old_bufp->page;
303 new_bufp = ret.newp;
304 if (!new_bufp)
305 return (-1);
306 np = (u_int16_t *)new_bufp->page;
307 bufp = ret.nextp;
308 if (!bufp)
309 return (0);
310 cino = (char *)bufp->page;
311 ino = (u_int16_t *)cino;
312 last_bfp = ret.nextp;
313 } else if (ino[n + 1] == OVFLPAGE) {
314 ov_addr = ino[n];
315 /*
316 * Fix up the old page -- the extra 2 are the fields
317 * which contained the overflow information.
318 */
319 ino[0] -= (moved + 2);
320 FREESPACE(ino) =
321 scopyto - sizeof(u_int16_t) * (ino[0] + 3);
322 OFFSET(ino) = scopyto;
323
324 bufp = __get_buf(hashp, ov_addr, bufp, 0);
325 if (!bufp)
326 return (-1);
327
328 ino = (u_int16_t *)bufp->page;
329 n = 1;
330 scopyto = hashp->BSIZE;
331 moved = 0;
332
333 if (last_bfp)
334 __free_ovflpage(hashp, last_bfp);
335 last_bfp = bufp;
336 }
337 /* Move regular sized pairs of there are any */
338 off = hashp->BSIZE;
339 for (n = 1; (n < ino[0]) && (ino[n + 1] >= REAL_KEY); n += 2) {
340 cino = (char *)ino;
341 key.data = (u_char *)cino + ino[n];
342 key.size = off - ino[n];
343 val.data = (u_char *)cino + ino[n + 1];
344 val.size = ino[n] - ino[n + 1];
345 off = ino[n + 1];
346
347 if (__call_hash(hashp, key.data, key.size) == obucket) {
348 /* Keep on old page */
349 if (PAIRFITS(op, (&key), (&val)))
350 putpair((char *)op, &key, &val);
351 else {
352 old_bufp =
353 __add_ovflpage(hashp, old_bufp);
354 if (!old_bufp)
355 return (-1);
356 op = (u_int16_t *)old_bufp->page;
357 putpair((char *)op, &key, &val);
358 }
359 old_bufp->flags |= BUF_MOD;
360 } else {
361 /* Move to new page */
362 if (PAIRFITS(np, (&key), (&val)))
363 putpair((char *)np, &key, &val);
364 else {
365 new_bufp =
366 __add_ovflpage(hashp, new_bufp);
367 if (!new_bufp)
368 return (-1);
369 np = (u_int16_t *)new_bufp->page;
370 putpair((char *)np, &key, &val);
371 }
372 new_bufp->flags |= BUF_MOD;
373 }
374 }
375 }
376 if (last_bfp)
377 __free_ovflpage(hashp, last_bfp);
378 return (0);
379}
380
381/*
382 * Add the given pair to the page
383 *
384 * Returns:
385 * 0 ==> OK
386 * 1 ==> failure
387 */
388int
389__addel(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
390{
391 u_int16_t *bp, *sop;
392 int do_expand;
393
394 bp = (u_int16_t *)bufp->page;
395 do_expand = 0;
396 while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY))
397 /* Exception case */
398 if (bp[2] == FULL_KEY_DATA && bp[0] == 2)
399 /* This is the last page of a big key/data pair
400 and we need to add another page */
401 break;
402 else if (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
403 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
404 if (!bufp)
405 return (-1);
406 bp = (u_int16_t *)bufp->page;
407 } else
408 /* Try to squeeze key on this page */
409 if (FREESPACE(bp) > PAIRSIZE(key, val)) {
410 squeeze_key(bp, key, val);
411 return (0);
412 } else {
413 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
414 if (!bufp)
415 return (-1);
416 bp = (u_int16_t *)bufp->page;
417 }
418
419 if (PAIRFITS(bp, key, val))
420 putpair(bufp->page, key, val);
421 else {
422 do_expand = 1;
423 bufp = __add_ovflpage(hashp, bufp);
424 if (!bufp)
425 return (-1);
426 sop = (u_int16_t *)bufp->page;
427
428 if (PAIRFITS(sop, key, val))
429 putpair((char *)sop, key, val);
430 else
431 if (__big_insert(hashp, bufp, key, val))
432 return (-1);
433 }
434 bufp->flags |= BUF_MOD;
435 /*
436 * If the average number of keys per bucket exceeds the fill factor,
437 * expand the table.
438 */
439 hashp->NKEYS++;
440 if (do_expand ||
441 (hashp->NKEYS / (hashp->MAX_BUCKET + 1) > hashp->FFACTOR))
442 return (__expand_table(hashp));
443 return (0);
444}
445
446/*
447 *
448 * Returns:
449 * pointer on success
450 * NULL on error
451 */
452BUFHEAD *
453__add_ovflpage(HTAB *hashp, BUFHEAD *bufp)
454{
456 u_int16_t *sp;
457 u_int16_t ndx, ovfl_num;
455 u_int16_t *sp, ndx, ovfl_num;
458#ifdef DEBUG1
459 int tmp1, tmp2;
460#endif
461 sp = (u_int16_t *)bufp->page;
462
463 /* Check if we are dynamically determining the fill factor */
464 if (hashp->FFACTOR == DEF_FFACTOR) {
465 hashp->FFACTOR = sp[0] >> 1;
466 if (hashp->FFACTOR < MIN_FFACTOR)
467 hashp->FFACTOR = MIN_FFACTOR;
468 }
469 bufp->flags |= BUF_MOD;
470 ovfl_num = overflow_page(hashp);
471#ifdef DEBUG1
472 tmp1 = bufp->addr;
473 tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
474#endif
475 if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
476 return (NULL);
477 bufp->ovfl->flags |= BUF_MOD;
478#ifdef DEBUG1
479 (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
480 tmp1, tmp2, bufp->ovfl->addr);
481#endif
482 ndx = sp[0];
483 /*
484 * Since a pair is allocated on a page only if there's room to add
485 * an overflow page, we know that the OVFL information will fit on
486 * the page.
487 */
488 sp[ndx + 4] = OFFSET(sp);
489 sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
490 sp[ndx + 1] = ovfl_num;
491 sp[ndx + 2] = OVFLPAGE;
492 sp[0] = ndx + 2;
493#ifdef HASH_STATISTICS
494 hash_overflows++;
495#endif
496 return (bufp->ovfl);
497}
498
499/*
500 * Returns:
501 * 0 indicates SUCCESS
502 * -1 indicates FAILURE
503 */
504int
505__get_page(HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_disk,
506 int is_bitmap)
507{
456#ifdef DEBUG1
457 int tmp1, tmp2;
458#endif
459 sp = (u_int16_t *)bufp->page;
460
461 /* Check if we are dynamically determining the fill factor */
462 if (hashp->FFACTOR == DEF_FFACTOR) {
463 hashp->FFACTOR = sp[0] >> 1;
464 if (hashp->FFACTOR < MIN_FFACTOR)
465 hashp->FFACTOR = MIN_FFACTOR;
466 }
467 bufp->flags |= BUF_MOD;
468 ovfl_num = overflow_page(hashp);
469#ifdef DEBUG1
470 tmp1 = bufp->addr;
471 tmp2 = bufp->ovfl ? bufp->ovfl->addr : 0;
472#endif
473 if (!ovfl_num || !(bufp->ovfl = __get_buf(hashp, ovfl_num, bufp, 1)))
474 return (NULL);
475 bufp->ovfl->flags |= BUF_MOD;
476#ifdef DEBUG1
477 (void)fprintf(stderr, "ADDOVFLPAGE: %d->ovfl was %d is now %d\n",
478 tmp1, tmp2, bufp->ovfl->addr);
479#endif
480 ndx = sp[0];
481 /*
482 * Since a pair is allocated on a page only if there's room to add
483 * an overflow page, we know that the OVFL information will fit on
484 * the page.
485 */
486 sp[ndx + 4] = OFFSET(sp);
487 sp[ndx + 3] = FREESPACE(sp) - OVFLSIZE;
488 sp[ndx + 1] = ovfl_num;
489 sp[ndx + 2] = OVFLPAGE;
490 sp[0] = ndx + 2;
491#ifdef HASH_STATISTICS
492 hash_overflows++;
493#endif
494 return (bufp->ovfl);
495}
496
497/*
498 * Returns:
499 * 0 indicates SUCCESS
500 * -1 indicates FAILURE
501 */
502int
503__get_page(HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_disk,
504 int is_bitmap)
505{
508 int fd, page, size;
509 int rsize;
506 int fd, page, size, rsize;
510 u_int16_t *bp;
511
512 fd = hashp->fp;
513 size = hashp->BSIZE;
514
515 if ((fd == -1) || !is_disk) {
516 PAGE_INIT(p);
517 return (0);
518 }
519 if (is_bucket)
520 page = BUCKET_TO_PAGE(bucket);
521 else
522 page = OADDR_TO_PAGE(bucket);
523 if ((rsize = pread(fd, p, size, (off_t)page << hashp->BSHIFT)) == -1)
524 return (-1);
525 bp = (u_int16_t *)p;
526 if (!rsize)
527 bp[0] = 0; /* We hit the EOF, so initialize a new page */
528 else
529 if (rsize != size) {
530 errno = EFTYPE;
531 return (-1);
532 }
533 if (!is_bitmap && !bp[0]) {
534 PAGE_INIT(p);
535 } else
536 if (hashp->LORDER != BYTE_ORDER) {
537 int i, max;
538
539 if (is_bitmap) {
540 max = hashp->BSIZE >> 2; /* divide by 4 */
541 for (i = 0; i < max; i++)
542 M_32_SWAP(((int *)p)[i]);
543 } else {
544 M_16_SWAP(bp[0]);
545 max = bp[0] + 2;
546 for (i = 1; i <= max; i++)
547 M_16_SWAP(bp[i]);
548 }
549 }
550 return (0);
551}
552
553/*
554 * Write page p to disk
555 *
556 * Returns:
557 * 0 ==> OK
558 * -1 ==>failure
559 */
560int
561__put_page(HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_bitmap)
562{
507 u_int16_t *bp;
508
509 fd = hashp->fp;
510 size = hashp->BSIZE;
511
512 if ((fd == -1) || !is_disk) {
513 PAGE_INIT(p);
514 return (0);
515 }
516 if (is_bucket)
517 page = BUCKET_TO_PAGE(bucket);
518 else
519 page = OADDR_TO_PAGE(bucket);
520 if ((rsize = pread(fd, p, size, (off_t)page << hashp->BSHIFT)) == -1)
521 return (-1);
522 bp = (u_int16_t *)p;
523 if (!rsize)
524 bp[0] = 0; /* We hit the EOF, so initialize a new page */
525 else
526 if (rsize != size) {
527 errno = EFTYPE;
528 return (-1);
529 }
530 if (!is_bitmap && !bp[0]) {
531 PAGE_INIT(p);
532 } else
533 if (hashp->LORDER != BYTE_ORDER) {
534 int i, max;
535
536 if (is_bitmap) {
537 max = hashp->BSIZE >> 2; /* divide by 4 */
538 for (i = 0; i < max; i++)
539 M_32_SWAP(((int *)p)[i]);
540 } else {
541 M_16_SWAP(bp[0]);
542 max = bp[0] + 2;
543 for (i = 1; i <= max; i++)
544 M_16_SWAP(bp[i]);
545 }
546 }
547 return (0);
548}
549
550/*
551 * Write page p to disk
552 *
553 * Returns:
554 * 0 ==> OK
555 * -1 ==>failure
556 */
557int
558__put_page(HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_bitmap)
559{
563 int fd, page, size;
564 int wsize;
560 int fd, page, size, wsize;
565
566 size = hashp->BSIZE;
567 if ((hashp->fp == -1) && open_temp(hashp))
568 return (-1);
569 fd = hashp->fp;
570
571 if (hashp->LORDER != BYTE_ORDER) {
561
562 size = hashp->BSIZE;
563 if ((hashp->fp == -1) && open_temp(hashp))
564 return (-1);
565 fd = hashp->fp;
566
567 if (hashp->LORDER != BYTE_ORDER) {
572 int i;
573 int max;
568 int i, max;
574
575 if (is_bitmap) {
576 max = hashp->BSIZE >> 2; /* divide by 4 */
577 for (i = 0; i < max; i++)
578 M_32_SWAP(((int *)p)[i]);
579 } else {
580 max = ((u_int16_t *)p)[0] + 2;
581 for (i = 0; i <= max; i++)
582 M_16_SWAP(((u_int16_t *)p)[i]);
583 }
584 }
585 if (is_bucket)
586 page = BUCKET_TO_PAGE(bucket);
587 else
588 page = OADDR_TO_PAGE(bucket);
589 if ((wsize = pwrite(fd, p, size, (off_t)page << hashp->BSHIFT)) == -1)
590 /* Errno is set */
591 return (-1);
592 if (wsize != size) {
593 errno = EFTYPE;
594 return (-1);
595 }
596 return (0);
597}
598
599#define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
600/*
601 * Initialize a new bitmap page. Bitmap pages are left in memory
602 * once they are read in.
603 */
604int
605__ibitmap(HTAB *hashp, int pnum, int nbits, int ndx)
606{
607 u_int32_t *ip;
608 int clearbytes, clearints;
609
610 if ((ip = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
611 return (1);
612 hashp->nmaps++;
613 clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
614 clearbytes = clearints << INT_TO_BYTE;
615 (void)memset((char *)ip, 0, clearbytes);
616 (void)memset(((char *)ip) + clearbytes, 0xFF,
617 hashp->BSIZE - clearbytes);
618 ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
619 SETBIT(ip, 0);
620 hashp->BITMAPS[ndx] = (u_int16_t)pnum;
621 hashp->mapp[ndx] = ip;
622 return (0);
623}
624
625static u_int32_t
626first_free(u_int32_t map)
627{
628 u_int32_t i, mask;
629
630 mask = 0x1;
631 for (i = 0; i < BITS_PER_MAP; i++) {
632 if (!(mask & map))
633 return (i);
634 mask = mask << 1;
635 }
636 return (i);
637}
638
639static u_int16_t
640overflow_page(HTAB *hashp)
641{
642 u_int32_t *freep;
643 int max_free, offset, splitnum;
644 u_int16_t addr;
645 int bit, first_page, free_bit, free_page, i, in_use_bits, j;
646#ifdef DEBUG2
647 int tmp1, tmp2;
648#endif
649 splitnum = hashp->OVFL_POINT;
650 max_free = hashp->SPARES[splitnum];
651
652 free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
653 free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
654
655 /* Look through all the free maps to find the first free block */
656 first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
657 for ( i = first_page; i <= free_page; i++ ) {
658 if (!(freep = (u_int32_t *)hashp->mapp[i]) &&
659 !(freep = fetch_bitmap(hashp, i)))
660 return (0);
661 if (i == free_page)
662 in_use_bits = free_bit;
663 else
664 in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
665
666 if (i == first_page) {
667 bit = hashp->LAST_FREED &
668 ((hashp->BSIZE << BYTE_SHIFT) - 1);
669 j = bit / BITS_PER_MAP;
670 bit = bit & ~(BITS_PER_MAP - 1);
671 } else {
672 bit = 0;
673 j = 0;
674 }
675 for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
676 if (freep[j] != ALL_SET)
677 goto found;
678 }
679
680 /* No Free Page Found */
681 hashp->LAST_FREED = hashp->SPARES[splitnum];
682 hashp->SPARES[splitnum]++;
683 offset = hashp->SPARES[splitnum] -
684 (splitnum ? hashp->SPARES[splitnum - 1] : 0);
685
686#define OVMSG "HASH: Out of overflow pages. Increase page size\n"
687 if (offset > SPLITMASK) {
688 if (++splitnum >= NCACHED) {
689 (void)_write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
690 errno = EFBIG;
691 return (0);
692 }
693 hashp->OVFL_POINT = splitnum;
694 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
695 hashp->SPARES[splitnum-1]--;
696 offset = 1;
697 }
698
699 /* Check if we need to allocate a new bitmap page */
700 if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
701 free_page++;
702 if (free_page >= NCACHED) {
703 (void)_write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
704 errno = EFBIG;
705 return (0);
706 }
707 /*
708 * This is tricky. The 1 indicates that you want the new page
709 * allocated with 1 clear bit. Actually, you are going to
710 * allocate 2 pages from this map. The first is going to be
711 * the map page, the second is the overflow page we were
712 * looking for. The init_bitmap routine automatically, sets
713 * the first bit of itself to indicate that the bitmap itself
714 * is in use. We would explicitly set the second bit, but
715 * don't have to if we tell init_bitmap not to leave it clear
716 * in the first place.
717 */
718 if (__ibitmap(hashp,
719 (int)OADDR_OF(splitnum, offset), 1, free_page))
720 return (0);
721 hashp->SPARES[splitnum]++;
722#ifdef DEBUG2
723 free_bit = 2;
724#endif
725 offset++;
726 if (offset > SPLITMASK) {
727 if (++splitnum >= NCACHED) {
728 (void)_write(STDERR_FILENO, OVMSG,
729 sizeof(OVMSG) - 1);
730 errno = EFBIG;
731 return (0);
732 }
733 hashp->OVFL_POINT = splitnum;
734 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
735 hashp->SPARES[splitnum-1]--;
736 offset = 0;
737 }
738 } else {
739 /*
740 * Free_bit addresses the last used bit. Bump it to address
741 * the first available bit.
742 */
743 free_bit++;
744 SETBIT(freep, free_bit);
745 }
746
747 /* Calculate address of the new overflow page */
748 addr = OADDR_OF(splitnum, offset);
749#ifdef DEBUG2
750 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
751 addr, free_bit, free_page);
752#endif
753 return (addr);
754
755found:
756 bit = bit + first_free(freep[j]);
757 SETBIT(freep, bit);
758#ifdef DEBUG2
759 tmp1 = bit;
760 tmp2 = i;
761#endif
762 /*
763 * Bits are addressed starting with 0, but overflow pages are addressed
764 * beginning at 1. Bit is a bit addressnumber, so we need to increment
765 * it to convert it to a page number.
766 */
767 bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
768 if (bit >= hashp->LAST_FREED)
769 hashp->LAST_FREED = bit - 1;
770
771 /* Calculate the split number for this page */
772 for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++);
773 offset = (i ? bit - hashp->SPARES[i - 1] : bit);
774 if (offset >= SPLITMASK) {
775 (void)_write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
776 errno = EFBIG;
777 return (0); /* Out of overflow pages */
778 }
779 addr = OADDR_OF(i, offset);
780#ifdef DEBUG2
781 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
782 addr, tmp1, tmp2);
783#endif
784
785 /* Allocate and return the overflow page */
786 return (addr);
787}
788
789/*
790 * Mark this overflow page as free.
791 */
792void
793__free_ovflpage(HTAB *hashp, BUFHEAD *obufp)
794{
795 u_int16_t addr;
796 u_int32_t *freep;
797 int bit_address, free_page, free_bit;
798 u_int16_t ndx;
799
800 addr = obufp->addr;
801#ifdef DEBUG1
802 (void)fprintf(stderr, "Freeing %d\n", addr);
803#endif
804 ndx = (((u_int16_t)addr) >> SPLITSHIFT);
805 bit_address =
806 (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
807 if (bit_address < hashp->LAST_FREED)
808 hashp->LAST_FREED = bit_address;
809 free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
810 free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
811
812 if (!(freep = hashp->mapp[free_page]))
813 freep = fetch_bitmap(hashp, free_page);
814#ifdef DEBUG
815 /*
816 * This had better never happen. It means we tried to read a bitmap
817 * that has already had overflow pages allocated off it, and we
818 * failed to read it from the file.
819 */
820 if (!freep)
821 assert(0);
822#endif
823 CLRBIT(freep, free_bit);
824#ifdef DEBUG2
825 (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
826 obufp->addr, free_bit, free_page);
827#endif
828 __reclaim_buf(hashp, obufp);
829}
830
831/*
832 * Returns:
833 * 0 success
834 * -1 failure
835 */
836static int
837open_temp(HTAB *hashp)
838{
839 sigset_t set, oset;
840 int len;
841 char *envtmp = NULL;
842 char path[MAXPATHLEN];
843
844 if (issetugid() == 0)
845 envtmp = getenv("TMPDIR");
846 len = snprintf(path,
847 sizeof(path), "%s/_hash.XXXXXX", envtmp ? envtmp : "/tmp");
848 if (len < 0 || len >= sizeof(path)) {
849 errno = ENAMETOOLONG;
850 return (-1);
851 }
852
853 /* Block signals; make sure file goes away at process exit. */
854 (void)sigfillset(&set);
855 (void)_sigprocmask(SIG_BLOCK, &set, &oset);
856 if ((hashp->fp = mkstemp(path)) != -1) {
857 (void)unlink(path);
858 (void)_fcntl(hashp->fp, F_SETFD, 1);
859 }
860 (void)_sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
861 return (hashp->fp != -1 ? 0 : -1);
862}
863
864/*
865 * We have to know that the key will fit, but the last entry on the page is
866 * an overflow pair, so we need to shift things.
867 */
868static void
869squeeze_key(u_int16_t *sp, const DBT *key, const DBT *val)
870{
871 char *p;
872 u_int16_t free_space, n, off, pageno;
873
874 p = (char *)sp;
875 n = sp[0];
876 free_space = FREESPACE(sp);
877 off = OFFSET(sp);
878
879 pageno = sp[n - 1];
880 off -= key->size;
881 sp[n - 1] = off;
882 memmove(p + off, key->data, key->size);
883 off -= val->size;
884 sp[n] = off;
885 memmove(p + off, val->data, val->size);
886 sp[0] = n + 2;
887 sp[n + 1] = pageno;
888 sp[n + 2] = OVFLPAGE;
889 FREESPACE(sp) = free_space - PAIRSIZE(key, val);
890 OFFSET(sp) = off;
891}
892
893static u_int32_t *
894fetch_bitmap(HTAB *hashp, int ndx)
895{
896 if (ndx >= hashp->nmaps)
897 return (NULL);
898 if ((hashp->mapp[ndx] = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
899 return (NULL);
900 if (__get_page(hashp,
901 (char *)hashp->mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
902 free(hashp->mapp[ndx]);
903 return (NULL);
904 }
905 return (hashp->mapp[ndx]);
906}
907
908#ifdef DEBUG4
909int
910print_chain(int addr)
911{
912 BUFHEAD *bufp;
913 short *bp, oaddr;
914
915 (void)fprintf(stderr, "%d ", addr);
916 bufp = __get_buf(hashp, addr, NULL, 0);
917 bp = (short *)bufp->page;
918 while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
919 ((bp[0] > 2) && bp[2] < REAL_KEY))) {
920 oaddr = bp[bp[0] - 1];
921 (void)fprintf(stderr, "%d ", (int)oaddr);
922 bufp = __get_buf(hashp, (int)oaddr, bufp, 0);
923 bp = (short *)bufp->page;
924 }
925 (void)fprintf(stderr, "\n");
926}
927#endif
569
570 if (is_bitmap) {
571 max = hashp->BSIZE >> 2; /* divide by 4 */
572 for (i = 0; i < max; i++)
573 M_32_SWAP(((int *)p)[i]);
574 } else {
575 max = ((u_int16_t *)p)[0] + 2;
576 for (i = 0; i <= max; i++)
577 M_16_SWAP(((u_int16_t *)p)[i]);
578 }
579 }
580 if (is_bucket)
581 page = BUCKET_TO_PAGE(bucket);
582 else
583 page = OADDR_TO_PAGE(bucket);
584 if ((wsize = pwrite(fd, p, size, (off_t)page << hashp->BSHIFT)) == -1)
585 /* Errno is set */
586 return (-1);
587 if (wsize != size) {
588 errno = EFTYPE;
589 return (-1);
590 }
591 return (0);
592}
593
594#define BYTE_MASK ((1 << INT_BYTE_SHIFT) -1)
595/*
596 * Initialize a new bitmap page. Bitmap pages are left in memory
597 * once they are read in.
598 */
599int
600__ibitmap(HTAB *hashp, int pnum, int nbits, int ndx)
601{
602 u_int32_t *ip;
603 int clearbytes, clearints;
604
605 if ((ip = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
606 return (1);
607 hashp->nmaps++;
608 clearints = ((nbits - 1) >> INT_BYTE_SHIFT) + 1;
609 clearbytes = clearints << INT_TO_BYTE;
610 (void)memset((char *)ip, 0, clearbytes);
611 (void)memset(((char *)ip) + clearbytes, 0xFF,
612 hashp->BSIZE - clearbytes);
613 ip[clearints - 1] = ALL_SET << (nbits & BYTE_MASK);
614 SETBIT(ip, 0);
615 hashp->BITMAPS[ndx] = (u_int16_t)pnum;
616 hashp->mapp[ndx] = ip;
617 return (0);
618}
619
620static u_int32_t
621first_free(u_int32_t map)
622{
623 u_int32_t i, mask;
624
625 mask = 0x1;
626 for (i = 0; i < BITS_PER_MAP; i++) {
627 if (!(mask & map))
628 return (i);
629 mask = mask << 1;
630 }
631 return (i);
632}
633
634static u_int16_t
635overflow_page(HTAB *hashp)
636{
637 u_int32_t *freep;
638 int max_free, offset, splitnum;
639 u_int16_t addr;
640 int bit, first_page, free_bit, free_page, i, in_use_bits, j;
641#ifdef DEBUG2
642 int tmp1, tmp2;
643#endif
644 splitnum = hashp->OVFL_POINT;
645 max_free = hashp->SPARES[splitnum];
646
647 free_page = (max_free - 1) >> (hashp->BSHIFT + BYTE_SHIFT);
648 free_bit = (max_free - 1) & ((hashp->BSIZE << BYTE_SHIFT) - 1);
649
650 /* Look through all the free maps to find the first free block */
651 first_page = hashp->LAST_FREED >>(hashp->BSHIFT + BYTE_SHIFT);
652 for ( i = first_page; i <= free_page; i++ ) {
653 if (!(freep = (u_int32_t *)hashp->mapp[i]) &&
654 !(freep = fetch_bitmap(hashp, i)))
655 return (0);
656 if (i == free_page)
657 in_use_bits = free_bit;
658 else
659 in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
660
661 if (i == first_page) {
662 bit = hashp->LAST_FREED &
663 ((hashp->BSIZE << BYTE_SHIFT) - 1);
664 j = bit / BITS_PER_MAP;
665 bit = bit & ~(BITS_PER_MAP - 1);
666 } else {
667 bit = 0;
668 j = 0;
669 }
670 for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP)
671 if (freep[j] != ALL_SET)
672 goto found;
673 }
674
675 /* No Free Page Found */
676 hashp->LAST_FREED = hashp->SPARES[splitnum];
677 hashp->SPARES[splitnum]++;
678 offset = hashp->SPARES[splitnum] -
679 (splitnum ? hashp->SPARES[splitnum - 1] : 0);
680
681#define OVMSG "HASH: Out of overflow pages. Increase page size\n"
682 if (offset > SPLITMASK) {
683 if (++splitnum >= NCACHED) {
684 (void)_write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
685 errno = EFBIG;
686 return (0);
687 }
688 hashp->OVFL_POINT = splitnum;
689 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
690 hashp->SPARES[splitnum-1]--;
691 offset = 1;
692 }
693
694 /* Check if we need to allocate a new bitmap page */
695 if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
696 free_page++;
697 if (free_page >= NCACHED) {
698 (void)_write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
699 errno = EFBIG;
700 return (0);
701 }
702 /*
703 * This is tricky. The 1 indicates that you want the new page
704 * allocated with 1 clear bit. Actually, you are going to
705 * allocate 2 pages from this map. The first is going to be
706 * the map page, the second is the overflow page we were
707 * looking for. The init_bitmap routine automatically, sets
708 * the first bit of itself to indicate that the bitmap itself
709 * is in use. We would explicitly set the second bit, but
710 * don't have to if we tell init_bitmap not to leave it clear
711 * in the first place.
712 */
713 if (__ibitmap(hashp,
714 (int)OADDR_OF(splitnum, offset), 1, free_page))
715 return (0);
716 hashp->SPARES[splitnum]++;
717#ifdef DEBUG2
718 free_bit = 2;
719#endif
720 offset++;
721 if (offset > SPLITMASK) {
722 if (++splitnum >= NCACHED) {
723 (void)_write(STDERR_FILENO, OVMSG,
724 sizeof(OVMSG) - 1);
725 errno = EFBIG;
726 return (0);
727 }
728 hashp->OVFL_POINT = splitnum;
729 hashp->SPARES[splitnum] = hashp->SPARES[splitnum-1];
730 hashp->SPARES[splitnum-1]--;
731 offset = 0;
732 }
733 } else {
734 /*
735 * Free_bit addresses the last used bit. Bump it to address
736 * the first available bit.
737 */
738 free_bit++;
739 SETBIT(freep, free_bit);
740 }
741
742 /* Calculate address of the new overflow page */
743 addr = OADDR_OF(splitnum, offset);
744#ifdef DEBUG2
745 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
746 addr, free_bit, free_page);
747#endif
748 return (addr);
749
750found:
751 bit = bit + first_free(freep[j]);
752 SETBIT(freep, bit);
753#ifdef DEBUG2
754 tmp1 = bit;
755 tmp2 = i;
756#endif
757 /*
758 * Bits are addressed starting with 0, but overflow pages are addressed
759 * beginning at 1. Bit is a bit addressnumber, so we need to increment
760 * it to convert it to a page number.
761 */
762 bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
763 if (bit >= hashp->LAST_FREED)
764 hashp->LAST_FREED = bit - 1;
765
766 /* Calculate the split number for this page */
767 for (i = 0; (i < splitnum) && (bit > hashp->SPARES[i]); i++);
768 offset = (i ? bit - hashp->SPARES[i - 1] : bit);
769 if (offset >= SPLITMASK) {
770 (void)_write(STDERR_FILENO, OVMSG, sizeof(OVMSG) - 1);
771 errno = EFBIG;
772 return (0); /* Out of overflow pages */
773 }
774 addr = OADDR_OF(i, offset);
775#ifdef DEBUG2
776 (void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
777 addr, tmp1, tmp2);
778#endif
779
780 /* Allocate and return the overflow page */
781 return (addr);
782}
783
784/*
785 * Mark this overflow page as free.
786 */
787void
788__free_ovflpage(HTAB *hashp, BUFHEAD *obufp)
789{
790 u_int16_t addr;
791 u_int32_t *freep;
792 int bit_address, free_page, free_bit;
793 u_int16_t ndx;
794
795 addr = obufp->addr;
796#ifdef DEBUG1
797 (void)fprintf(stderr, "Freeing %d\n", addr);
798#endif
799 ndx = (((u_int16_t)addr) >> SPLITSHIFT);
800 bit_address =
801 (ndx ? hashp->SPARES[ndx - 1] : 0) + (addr & SPLITMASK) - 1;
802 if (bit_address < hashp->LAST_FREED)
803 hashp->LAST_FREED = bit_address;
804 free_page = (bit_address >> (hashp->BSHIFT + BYTE_SHIFT));
805 free_bit = bit_address & ((hashp->BSIZE << BYTE_SHIFT) - 1);
806
807 if (!(freep = hashp->mapp[free_page]))
808 freep = fetch_bitmap(hashp, free_page);
809#ifdef DEBUG
810 /*
811 * This had better never happen. It means we tried to read a bitmap
812 * that has already had overflow pages allocated off it, and we
813 * failed to read it from the file.
814 */
815 if (!freep)
816 assert(0);
817#endif
818 CLRBIT(freep, free_bit);
819#ifdef DEBUG2
820 (void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
821 obufp->addr, free_bit, free_page);
822#endif
823 __reclaim_buf(hashp, obufp);
824}
825
826/*
827 * Returns:
828 * 0 success
829 * -1 failure
830 */
831static int
832open_temp(HTAB *hashp)
833{
834 sigset_t set, oset;
835 int len;
836 char *envtmp = NULL;
837 char path[MAXPATHLEN];
838
839 if (issetugid() == 0)
840 envtmp = getenv("TMPDIR");
841 len = snprintf(path,
842 sizeof(path), "%s/_hash.XXXXXX", envtmp ? envtmp : "/tmp");
843 if (len < 0 || len >= sizeof(path)) {
844 errno = ENAMETOOLONG;
845 return (-1);
846 }
847
848 /* Block signals; make sure file goes away at process exit. */
849 (void)sigfillset(&set);
850 (void)_sigprocmask(SIG_BLOCK, &set, &oset);
851 if ((hashp->fp = mkstemp(path)) != -1) {
852 (void)unlink(path);
853 (void)_fcntl(hashp->fp, F_SETFD, 1);
854 }
855 (void)_sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
856 return (hashp->fp != -1 ? 0 : -1);
857}
858
859/*
860 * We have to know that the key will fit, but the last entry on the page is
861 * an overflow pair, so we need to shift things.
862 */
863static void
864squeeze_key(u_int16_t *sp, const DBT *key, const DBT *val)
865{
866 char *p;
867 u_int16_t free_space, n, off, pageno;
868
869 p = (char *)sp;
870 n = sp[0];
871 free_space = FREESPACE(sp);
872 off = OFFSET(sp);
873
874 pageno = sp[n - 1];
875 off -= key->size;
876 sp[n - 1] = off;
877 memmove(p + off, key->data, key->size);
878 off -= val->size;
879 sp[n] = off;
880 memmove(p + off, val->data, val->size);
881 sp[0] = n + 2;
882 sp[n + 1] = pageno;
883 sp[n + 2] = OVFLPAGE;
884 FREESPACE(sp) = free_space - PAIRSIZE(key, val);
885 OFFSET(sp) = off;
886}
887
888static u_int32_t *
889fetch_bitmap(HTAB *hashp, int ndx)
890{
891 if (ndx >= hashp->nmaps)
892 return (NULL);
893 if ((hashp->mapp[ndx] = (u_int32_t *)malloc(hashp->BSIZE)) == NULL)
894 return (NULL);
895 if (__get_page(hashp,
896 (char *)hashp->mapp[ndx], hashp->BITMAPS[ndx], 0, 1, 1)) {
897 free(hashp->mapp[ndx]);
898 return (NULL);
899 }
900 return (hashp->mapp[ndx]);
901}
902
903#ifdef DEBUG4
904int
905print_chain(int addr)
906{
907 BUFHEAD *bufp;
908 short *bp, oaddr;
909
910 (void)fprintf(stderr, "%d ", addr);
911 bufp = __get_buf(hashp, addr, NULL, 0);
912 bp = (short *)bufp->page;
913 while (bp[0] && ((bp[bp[0]] == OVFLPAGE) ||
914 ((bp[0] > 2) && bp[2] < REAL_KEY))) {
915 oaddr = bp[bp[0] - 1];
916 (void)fprintf(stderr, "%d ", (int)oaddr);
917 bufp = __get_buf(hashp, (int)oaddr, bufp, 0);
918 bp = (short *)bufp->page;
919 }
920 (void)fprintf(stderr, "\n");
921}
922#endif