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