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