hash_buf.c revision 1.11.14.3 1 /* $NetBSD: hash_buf.c,v 1.11.14.3 2009/04/30 18:26:15 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Margo Seltzer.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
38
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)hash_buf.c 8.5 (Berkeley) 7/15/94";
43 #else
44 __RCSID("$NetBSD: hash_buf.c,v 1.11.14.3 2009/04/30 18:26:15 bouyer Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47
48 /*
49 * PACKAGE: hash
50 *
51 * DESCRIPTION:
52 * Contains buffer management
53 *
54 * ROUTINES:
55 * External
56 * __buf_init
57 * __get_buf
58 * __buf_free
59 * __reclaim_buf
60 * Internal
61 * newbuf
62 */
63
64 #include <sys/param.h>
65
66 #include <errno.h>
67 #include <stddef.h>
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71
72 #ifdef DEBUG
73 #include <assert.h>
74 #endif
75
76 #include <db.h>
77 #include "hash.h"
78 #include "page.h"
79 #include "extern.h"
80
81 static BUFHEAD *newbuf __P((HTAB *, u_int32_t, BUFHEAD *));
82
83 /* Unlink B from its place in the lru */
84 #define BUF_REMOVE(B) { \
85 (B)->prev->next = (B)->next; \
86 (B)->next->prev = (B)->prev; \
87 }
88
89 /* Insert B after P */
90 #define BUF_INSERT(B, P) { \
91 (B)->next = (P)->next; \
92 (B)->prev = (P); \
93 (P)->next = (B); \
94 (B)->next->prev = (B); \
95 }
96
97 #define MRU hashp->bufhead.next
98 #define LRU hashp->bufhead.prev
99
100 #define MRU_INSERT(B) BUF_INSERT((B), &hashp->bufhead)
101 #define LRU_INSERT(B) BUF_INSERT((B), LRU)
102
103 /*
104 * We are looking for a buffer with address "addr". If prev_bp is NULL, then
105 * address is a bucket index. If prev_bp is not NULL, then it points to the
106 * page previous to an overflow page that we are trying to find.
107 *
108 * CAVEAT: The buffer header accessed via prev_bp's ovfl field may no longer
109 * be valid. Therefore, you must always verify that its address matches the
110 * address you are seeking.
111 */
112 extern BUFHEAD *
113 __get_buf(hashp, addr, prev_bp, newpage)
114 HTAB *hashp;
115 u_int32_t addr;
116 BUFHEAD *prev_bp;
117 int newpage; /* If prev_bp set, indicates a new overflow page. */
118 {
119 register BUFHEAD *bp;
120 register u_int32_t is_disk_mask;
121 register int is_disk, segment_ndx = 0; /* pacify gcc */
122 SEGMENT segp = NULL; /* pacify gcc */
123
124 is_disk = 0;
125 is_disk_mask = 0;
126 if (prev_bp) {
127 bp = prev_bp->ovfl;
128 if (!bp || (bp->addr != addr))
129 bp = NULL;
130 if (!newpage)
131 is_disk = BUF_DISK;
132 } else {
133 /* Grab buffer out of directory */
134 segment_ndx = addr & (hashp->SGSIZE - 1);
135
136 /* valid segment ensured by __call_hash() */
137 segp = hashp->dir[addr >> hashp->SSHIFT];
138 #ifdef DEBUG
139 assert(segp != NULL);
140 #endif
141 bp = PTROF(segp[segment_ndx]);
142 is_disk_mask = ISDISK(segp[segment_ndx]);
143 is_disk = is_disk_mask || !hashp->new_file;
144 }
145
146 if (!bp) {
147 bp = newbuf(hashp, addr, prev_bp);
148 if (!bp ||
149 __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
150 return (NULL);
151 if (!prev_bp)
152 segp[segment_ndx] =
153 (BUFHEAD *)(void *)((u_long)bp | is_disk_mask);
154 } else {
155 BUF_REMOVE(bp);
156 MRU_INSERT(bp);
157 }
158 return (bp);
159 }
160
161 /*
162 * We need a buffer for this page. Either allocate one, or evict a resident
163 * one (if we have as many buffers as we're allowed) and put this one in.
164 *
165 * If newbuf finds an error (returning NULL), it also sets errno.
166 */
167 static BUFHEAD *
168 newbuf(hashp, addr, prev_bp)
169 HTAB *hashp;
170 u_int32_t addr;
171 BUFHEAD *prev_bp;
172 {
173 register BUFHEAD *bp; /* The buffer we're going to use */
174 register BUFHEAD *xbp; /* Temp pointer */
175 register BUFHEAD *next_xbp;
176 SEGMENT segp;
177 int segment_ndx;
178 u_int16_t oaddr, *shortp;
179
180 oaddr = 0;
181 bp = LRU;
182 /*
183 * If LRU buffer is pinned, the buffer pool is too small. We need to
184 * allocate more buffers.
185 */
186 if (hashp->nbufs || (bp->flags & BUF_PIN)) {
187 /* Allocate a new one */
188 if ((bp = (BUFHEAD *)calloc(1, sizeof(BUFHEAD))) == NULL)
189 return (NULL);
190 if ((bp->page = calloc(1, (size_t)hashp->BSIZE)) == NULL) {
191 free(bp);
192 return (NULL);
193 }
194 if (hashp->nbufs)
195 hashp->nbufs--;
196 } else {
197 /* Kick someone out */
198 BUF_REMOVE(bp);
199 /*
200 * If this is an overflow page with addr 0, it's already been
201 * flushed back in an overflow chain and initialized.
202 */
203 if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
204 /*
205 * Set oaddr before __put_page so that you get it
206 * before bytes are swapped.
207 */
208 shortp = (u_int16_t *)(void *)bp->page;
209 if (shortp[0])
210 oaddr = shortp[shortp[0] - 1];
211 if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
212 bp->addr, (int)IS_BUCKET(bp->flags), 0))
213 return (NULL);
214 /*
215 * Update the pointer to this page (i.e. invalidate it).
216 *
217 * If this is a new file (i.e. we created it at open
218 * time), make sure that we mark pages which have been
219 * written to disk so we retrieve them from disk later,
220 * rather than allocating new pages.
221 */
222 if (IS_BUCKET(bp->flags)) {
223 segment_ndx = bp->addr & (hashp->SGSIZE - 1);
224 segp = hashp->dir[bp->addr >> hashp->SSHIFT];
225 #ifdef DEBUG
226 assert(segp != NULL);
227 #endif
228
229 if (hashp->new_file &&
230 ((bp->flags & BUF_MOD) ||
231 ISDISK(segp[segment_ndx])))
232 segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
233 else
234 segp[segment_ndx] = NULL;
235 }
236 /*
237 * Since overflow pages can only be access by means of
238 * their bucket, free overflow pages associated with
239 * this bucket.
240 */
241 for (xbp = bp; xbp->ovfl;) {
242 next_xbp = xbp->ovfl;
243 xbp->ovfl = 0;
244 xbp = next_xbp;
245
246 /* Check that ovfl pointer is up date. */
247 if (IS_BUCKET(xbp->flags) ||
248 (oaddr != xbp->addr))
249 break;
250
251 shortp = (u_int16_t *)(void *)xbp->page;
252 if (shortp[0])
253 /* set before __put_page */
254 oaddr = shortp[shortp[0] - 1];
255 if ((xbp->flags & BUF_MOD) && __put_page(hashp,
256 xbp->page, xbp->addr, 0, 0))
257 return (NULL);
258 xbp->addr = 0;
259 xbp->flags = 0;
260 BUF_REMOVE(xbp);
261 LRU_INSERT(xbp);
262 }
263 }
264 }
265
266 /* Now assign this buffer */
267 bp->addr = addr;
268 #ifdef DEBUG1
269 (void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
270 bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
271 #endif
272 bp->ovfl = NULL;
273 if (prev_bp) {
274 /*
275 * If prev_bp is set, this is an overflow page, hook it in to
276 * the buffer overflow links.
277 */
278 #ifdef DEBUG1
279 (void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
280 prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0),
281 (bp ? bp->addr : 0));
282 #endif
283 prev_bp->ovfl = bp;
284 bp->flags = 0;
285 } else
286 bp->flags = BUF_BUCKET;
287 MRU_INSERT(bp);
288 return (bp);
289 }
290
291 extern void
292 __buf_init(hashp, nbytes)
293 HTAB *hashp;
294 u_int nbytes;
295 {
296 BUFHEAD *bfp;
297 int npages;
298
299 bfp = &(hashp->bufhead);
300 npages = (unsigned int)(nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
301 npages = MAX(npages, MIN_BUFFERS);
302
303 hashp->nbufs = npages;
304 bfp->next = bfp;
305 bfp->prev = bfp;
306 /*
307 * This space is calloc'd so these are already null.
308 *
309 * bfp->ovfl = NULL;
310 * bfp->flags = 0;
311 * bfp->page = NULL;
312 * bfp->addr = 0;
313 */
314 }
315
316 extern int
317 __buf_free(hashp, do_free, to_disk)
318 HTAB *hashp;
319 int do_free, to_disk;
320 {
321 BUFHEAD *bp;
322
323 /* Need to make sure that buffer manager has been initialized */
324 if (!LRU)
325 return (0);
326 for (bp = LRU; bp != &hashp->bufhead;) {
327 /* Check that the buffer is valid */
328 if (bp->addr || IS_BUCKET(bp->flags)) {
329 if (to_disk && (bp->flags & BUF_MOD) &&
330 __put_page(hashp, bp->page,
331 bp->addr, IS_BUCKET(bp->flags), 0))
332 return (-1);
333 }
334 /* Check if we are freeing stuff */
335 if (do_free) {
336 if (bp->page) {
337 (void)memset(bp->page, 0, (size_t)hashp->BSIZE);
338 free(bp->page);
339 }
340 BUF_REMOVE(bp);
341 free(bp);
342 bp = LRU;
343 } else
344 bp = bp->prev;
345 }
346 return (0);
347 }
348
349 extern void
350 __reclaim_buf(hashp, bp)
351 HTAB *hashp;
352 BUFHEAD *bp;
353 {
354 bp->ovfl = 0;
355 bp->addr = 0;
356 bp->flags = 0;
357 BUF_REMOVE(bp);
358 LRU_INSERT(bp);
359 }
360