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