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