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