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