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