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