hash_buf.c revision 1.10 1 /* $NetBSD: hash_buf.c,v 1.10 2003/08/07 16:42:42 agc 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 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)hash_buf.c 8.5 (Berkeley) 7/15/94";
39 #else
40 __RCSID("$NetBSD: hash_buf.c,v 1.10 2003/08/07 16:42:42 agc Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43
44 /*
45 * PACKAGE: hash
46 *
47 * DESCRIPTION:
48 * Contains buffer management
49 *
50 * ROUTINES:
51 * External
52 * __buf_init
53 * __get_buf
54 * __buf_free
55 * __reclaim_buf
56 * Internal
57 * newbuf
58 */
59
60 #include <sys/param.h>
61
62 #include <errno.h>
63 #include <stddef.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66
67 #ifdef DEBUG
68 #include <assert.h>
69 #endif
70
71 #include <db.h>
72 #include "hash.h"
73 #include "page.h"
74 #include "extern.h"
75
76 static BUFHEAD *newbuf __P((HTAB *, u_int32_t, BUFHEAD *));
77
78 /* Unlink B from its place in the lru */
79 #define BUF_REMOVE(B) { \
80 (B)->prev->next = (B)->next; \
81 (B)->next->prev = (B)->prev; \
82 }
83
84 /* Insert B after P */
85 #define BUF_INSERT(B, P) { \
86 (B)->next = (P)->next; \
87 (B)->prev = (P); \
88 (P)->next = (B); \
89 (B)->next->prev = (B); \
90 }
91
92 #define MRU hashp->bufhead.next
93 #define LRU hashp->bufhead.prev
94
95 #define MRU_INSERT(B) BUF_INSERT((B), &hashp->bufhead)
96 #define LRU_INSERT(B) BUF_INSERT((B), LRU)
97
98 /*
99 * We are looking for a buffer with address "addr". If prev_bp is NULL, then
100 * address is a bucket index. If prev_bp is not NULL, then it points to the
101 * page previous to an overflow page that we are trying to find.
102 *
103 * CAVEAT: The buffer header accessed via prev_bp's ovfl field may no longer
104 * be valid. Therefore, you must always verify that its address matches the
105 * address you are seeking.
106 */
107 extern BUFHEAD *
108 __get_buf(hashp, addr, prev_bp, newpage)
109 HTAB *hashp;
110 u_int32_t addr;
111 BUFHEAD *prev_bp;
112 int newpage; /* If prev_bp set, indicates a new overflow page. */
113 {
114 register BUFHEAD *bp;
115 register u_int32_t is_disk_mask;
116 register int is_disk, segment_ndx = 0; /* pacify gcc */
117 SEGMENT segp = NULL; /* pacify gcc */
118
119 is_disk = 0;
120 is_disk_mask = 0;
121 if (prev_bp) {
122 bp = prev_bp->ovfl;
123 if (!bp || (bp->addr != addr))
124 bp = NULL;
125 if (!newpage)
126 is_disk = BUF_DISK;
127 } else {
128 /* Grab buffer out of directory */
129 segment_ndx = addr & (hashp->SGSIZE - 1);
130
131 /* valid segment ensured by __call_hash() */
132 segp = hashp->dir[addr >> hashp->SSHIFT];
133 #ifdef DEBUG
134 assert(segp != NULL);
135 #endif
136 bp = PTROF(segp[segment_ndx]);
137 is_disk_mask = ISDISK(segp[segment_ndx]);
138 is_disk = is_disk_mask || !hashp->new_file;
139 }
140
141 if (!bp) {
142 bp = newbuf(hashp, addr, prev_bp);
143 if (!bp ||
144 __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
145 return (NULL);
146 if (!prev_bp)
147 segp[segment_ndx] =
148 (BUFHEAD *)(void *)((u_long)bp | is_disk_mask);
149 } else {
150 BUF_REMOVE(bp);
151 MRU_INSERT(bp);
152 }
153 return (bp);
154 }
155
156 /*
157 * We need a buffer for this page. Either allocate one, or evict a resident
158 * one (if we have as many buffers as we're allowed) and put this one in.
159 *
160 * If newbuf finds an error (returning NULL), it also sets errno.
161 */
162 static BUFHEAD *
163 newbuf(hashp, addr, prev_bp)
164 HTAB *hashp;
165 u_int32_t addr;
166 BUFHEAD *prev_bp;
167 {
168 register BUFHEAD *bp; /* The buffer we're going to use */
169 register BUFHEAD *xbp; /* Temp pointer */
170 register BUFHEAD *next_xbp;
171 SEGMENT segp;
172 int segment_ndx;
173 u_int16_t oaddr, *shortp;
174
175 oaddr = 0;
176 bp = LRU;
177 /*
178 * If LRU buffer is pinned, the buffer pool is too small. We need to
179 * allocate more buffers.
180 */
181 if (hashp->nbufs || (bp->flags & BUF_PIN)) {
182 /* Allocate a new one */
183 if ((bp = (BUFHEAD *)calloc(1, sizeof(BUFHEAD))) == NULL)
184 return (NULL);
185 if ((bp->page = calloc(1, (size_t)hashp->BSIZE)) == NULL) {
186 free(bp);
187 return (NULL);
188 }
189 if (hashp->nbufs)
190 hashp->nbufs--;
191 } else {
192 /* Kick someone out */
193 BUF_REMOVE(bp);
194 /*
195 * If this is an overflow page with addr 0, it's already been
196 * flushed back in an overflow chain and initialized.
197 */
198 if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
199 /*
200 * Set oaddr before __put_page so that you get it
201 * before bytes are swapped.
202 */
203 shortp = (u_int16_t *)(void *)bp->page;
204 if (shortp[0])
205 oaddr = shortp[shortp[0] - 1];
206 if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
207 bp->addr, (int)IS_BUCKET(bp->flags), 0))
208 return (NULL);
209 /*
210 * Update the pointer to this page (i.e. invalidate it).
211 *
212 * If this is a new file (i.e. we created it at open
213 * time), make sure that we mark pages which have been
214 * written to disk so we retrieve them from disk later,
215 * rather than allocating new pages.
216 */
217 if (IS_BUCKET(bp->flags)) {
218 segment_ndx = bp->addr & (hashp->SGSIZE - 1);
219 segp = hashp->dir[bp->addr >> hashp->SSHIFT];
220 #ifdef DEBUG
221 assert(segp != NULL);
222 #endif
223
224 if (hashp->new_file &&
225 ((bp->flags & BUF_MOD) ||
226 ISDISK(segp[segment_ndx])))
227 segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
228 else
229 segp[segment_ndx] = NULL;
230 }
231 /*
232 * Since overflow pages can only be access by means of
233 * their bucket, free overflow pages associated with
234 * this bucket.
235 */
236 for (xbp = bp; xbp->ovfl;) {
237 next_xbp = xbp->ovfl;
238 xbp->ovfl = 0;
239 xbp = next_xbp;
240
241 /* Check that ovfl pointer is up date. */
242 if (IS_BUCKET(xbp->flags) ||
243 (oaddr != xbp->addr))
244 break;
245
246 shortp = (u_int16_t *)(void *)xbp->page;
247 if (shortp[0])
248 /* set before __put_page */
249 oaddr = shortp[shortp[0] - 1];
250 if ((xbp->flags & BUF_MOD) && __put_page(hashp,
251 xbp->page, xbp->addr, 0, 0))
252 return (NULL);
253 xbp->addr = 0;
254 xbp->flags = 0;
255 BUF_REMOVE(xbp);
256 LRU_INSERT(xbp);
257 }
258 }
259 }
260
261 /* Now assign this buffer */
262 bp->addr = addr;
263 #ifdef DEBUG1
264 (void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
265 bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
266 #endif
267 bp->ovfl = NULL;
268 if (prev_bp) {
269 /*
270 * If prev_bp is set, this is an overflow page, hook it in to
271 * the buffer overflow links.
272 */
273 #ifdef DEBUG1
274 (void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
275 prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0),
276 (bp ? bp->addr : 0));
277 #endif
278 prev_bp->ovfl = bp;
279 bp->flags = 0;
280 } else
281 bp->flags = BUF_BUCKET;
282 MRU_INSERT(bp);
283 return (bp);
284 }
285
286 extern void
287 __buf_init(hashp, nbytes)
288 HTAB *hashp;
289 u_int nbytes;
290 {
291 BUFHEAD *bfp;
292 int npages;
293
294 bfp = &(hashp->bufhead);
295 npages = (unsigned int)(nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
296 npages = MAX(npages, MIN_BUFFERS);
297
298 hashp->nbufs = npages;
299 bfp->next = bfp;
300 bfp->prev = bfp;
301 /*
302 * This space is calloc'd so these are already null.
303 *
304 * bfp->ovfl = NULL;
305 * bfp->flags = 0;
306 * bfp->page = NULL;
307 * bfp->addr = 0;
308 */
309 }
310
311 extern int
312 __buf_free(hashp, do_free, to_disk)
313 HTAB *hashp;
314 int do_free, to_disk;
315 {
316 BUFHEAD *bp;
317
318 /* Need to make sure that buffer manager has been initialized */
319 if (!LRU)
320 return (0);
321 for (bp = LRU; bp != &hashp->bufhead;) {
322 /* Check that the buffer is valid */
323 if (bp->addr || IS_BUCKET(bp->flags)) {
324 if (to_disk && (bp->flags & BUF_MOD) &&
325 __put_page(hashp, bp->page,
326 bp->addr, IS_BUCKET(bp->flags), 0))
327 return (-1);
328 }
329 /* Check if we are freeing stuff */
330 if (do_free) {
331 if (bp->page)
332 free(bp->page);
333 BUF_REMOVE(bp);
334 free(bp);
335 bp = LRU;
336 } else
337 bp = bp->prev;
338 }
339 return (0);
340 }
341
342 extern void
343 __reclaim_buf(hashp, bp)
344 HTAB *hashp;
345 BUFHEAD *bp;
346 {
347 bp->ovfl = 0;
348 bp->addr = 0;
349 bp->flags = 0;
350 BUF_REMOVE(bp);
351 LRU_INSERT(bp);
352 }
353