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. 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 #define _DEFAULT_SOURCE
34 #include <sys/param.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)hash_buf.c	8.5 (Berkeley) 7/15/94";
37 #endif /* LIBC_SCCS and not lint */
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_local.h"
66 #include "hash.h"
67 #include "page.h"
68 #include "extern.h"
69 
70 static BUFHEAD *newbuf(HTAB *, __uint32_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 /* Macros for min/max.  */
93 #ifndef MIN
94 #define MIN(a,b) (((a)<(b))?(a):(b))
95 #endif
96 #ifndef MAX
97 #define MAX(a,b) (((a)>(b))?(a):(b))
98 #endif
99 
100 /*
101  * We are looking for a buffer with address "addr".  If prev_bp is NULL, then
102  * address is a bucket index.  If prev_bp is not NULL, then it points to the
103  * page previous to an overflow page that we are trying to find.
104  *
105  * CAVEAT:  The buffer header accessed via prev_bp's ovfl field may no longer
106  * be valid.  Therefore, you must always verify that its address matches the
107  * address you are seeking.
108  */
109 extern BUFHEAD *
__get_buf(HTAB * hashp,__uint32_t addr,BUFHEAD * prev_bp,int newpage)110 __get_buf(HTAB *hashp,
111           __uint32_t addr,
112           BUFHEAD *prev_bp,
113           int newpage)	/* If prev_bp set, indicates a new overflow page. */
114 {
115 	BUFHEAD *bp;
116 	__uint32_t is_disk_mask;
117 	int is_disk, segment_ndx = 0;
118 	SEGMENT segp = NULL;
119 
120 	is_disk = 0;
121 	is_disk_mask = 0;
122 	if (prev_bp) {
123 		bp = prev_bp->ovfl;
124 		if (!bp || (bp->addr != addr))
125 			bp = NULL;
126 		if (!newpage)
127 			is_disk = BUF_DISK;
128 	} else {
129 		/* Grab buffer out of directory */
130 		segment_ndx = addr & (hashp->SGSIZE - 1);
131 
132 		/* valid segment ensured by __call_hash() */
133 		segp = hashp->dir[addr >> hashp->SSHIFT];
134 #ifdef DEBUG
135 		assert(segp != NULL);
136 #endif
137 		bp = PTROF(segp[segment_ndx]);
138 		is_disk_mask = ISDISK(segp[segment_ndx]);
139 		is_disk = is_disk_mask || !hashp->new_file;
140 	}
141 
142 	if (!bp) {
143 		bp = newbuf(hashp, addr, prev_bp);
144 		if (!bp ||
145 		    __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
146 			return (NULL);
147 		if (!prev_bp)
148 			segp[segment_ndx] =
149 			    (BUFHEAD *)((ptrdiff_t)bp | (intptr_t)is_disk_mask);
150 	} else {
151 		BUF_REMOVE(bp);
152 		MRU_INSERT(bp);
153 	}
154 	return (bp);
155 }
156 
157 /*
158  * We need a buffer for this page. Either allocate one, or evict a resident
159  * one (if we have as many buffers as we're allowed) and put this one in.
160  *
161  * If newbuf finds an error (returning NULL), it also sets errno.
162  */
163 static BUFHEAD *
newbuf(HTAB * hashp,__uint32_t addr,BUFHEAD * prev_bp)164 newbuf(HTAB *hashp,
165        __uint32_t addr,
166        BUFHEAD *prev_bp)
167 {
168 	BUFHEAD *bp;		/* The buffer we're going to use */
169 	BUFHEAD *xbp;		/* Temp pointer */
170 	BUFHEAD *next_xbp;
171 	SEGMENT segp;
172 	int segment_ndx;
173 	__uint16_t oaddr, *shortp;
174 
175 	oaddr = 0;
176 	bp = LRU;
177 	/*
178 	 * If LRU buffer is pinned, the buffer pool is too small. We need to
179 	 * allocate more buffers.
180 	 */
181 	if (hashp->nbufs || (bp->flags & BUF_PIN)) {
182 		/* Allocate a new one */
183 		if ((bp = (BUFHEAD *)malloc(sizeof(BUFHEAD))) == NULL)
184 			return (NULL);
185 #ifdef PURIFY
186 		memset(bp, 0xff, sizeof(BUFHEAD));
187 #endif
188 		if ((bp->page = (char *)malloc(hashp->BSIZE)) == NULL) {
189 			free(bp);
190 			return (NULL);
191 		}
192 #ifdef PURIFY
193 		memset(bp->page, 0xff, hashp->BSIZE);
194 #endif
195 		if (hashp->nbufs)
196 			hashp->nbufs--;
197 	} else {
198 		/* Kick someone out */
199 		BUF_REMOVE(bp);
200 		/*
201 		 * If this is an overflow page with addr 0, it's already been
202 		 * flushed back in an overflow chain and initialized.
203 		 */
204 		if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
205 			/*
206 			 * Set oaddr before __put_page so that you get it
207 			 * before bytes are swapped.
208 			 */
209 			shortp = (__uint16_t *)bp->page;
210 			if (shortp[0])
211 				oaddr = shortp[shortp[0] - 1];
212 			if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
213 			    bp->addr, (int)IS_BUCKET(bp->flags), 0))
214 				return (NULL);
215 			/*
216 			 * Update the pointer to this page (i.e. invalidate it).
217 			 *
218 			 * If this is a new file (i.e. we created it at open
219 			 * time), make sure that we mark pages which have been
220 			 * written to disk so we retrieve them from disk later,
221 			 * rather than allocating new pages.
222 			 */
223 			if (IS_BUCKET(bp->flags)) {
224 				segment_ndx = bp->addr & (hashp->SGSIZE - 1);
225 				segp = hashp->dir[bp->addr >> hashp->SSHIFT];
226 #ifdef DEBUG
227 				assert(segp != NULL);
228 #endif
229 
230 				if (hashp->new_file &&
231 				    ((bp->flags & BUF_MOD) ||
232 				    ISDISK(segp[segment_ndx])))
233 					segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
234 				else
235 					segp[segment_ndx] = NULL;
236 			}
237 			/*
238 			 * Since overflow pages can only be access by means of
239 			 * their bucket, free overflow pages associated with
240 			 * this bucket.
241 			 */
242 			for (xbp = bp; xbp->ovfl;) {
243 				next_xbp = xbp->ovfl;
244 				xbp->ovfl = 0;
245 				xbp = next_xbp;
246 
247 				/* Check that ovfl pointer is up date. */
248 				if (IS_BUCKET(xbp->flags) ||
249 				    (oaddr != xbp->addr))
250 					break;
251 
252 				shortp = (__uint16_t *)xbp->page;
253 				if (shortp[0])
254 					/* set before __put_page */
255 					oaddr = shortp[shortp[0] - 1];
256 				if ((xbp->flags & BUF_MOD) && __put_page(hashp,
257 				    xbp->page, xbp->addr, 0, 0))
258 					return (NULL);
259 				xbp->addr = 0;
260 				xbp->flags = 0;
261 				BUF_REMOVE(xbp);
262 				LRU_INSERT(xbp);
263 			}
264 		}
265 	}
266 
267 	/* Now assign this buffer */
268 	bp->addr = addr;
269 #ifdef DEBUG1
270 	(void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
271 	    bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
272 #endif
273 	bp->ovfl = NULL;
274 	if (prev_bp) {
275 		/*
276 		 * If prev_bp is set, this is an overflow page, hook it in to
277 		 * the buffer overflow links.
278 		 */
279 #ifdef DEBUG1
280 		(void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
281 		    prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0),
282 		    (bp ? bp->addr : 0));
283 #endif
284 		prev_bp->ovfl = bp;
285 		bp->flags = 0;
286 	} else
287 		bp->flags = BUF_BUCKET;
288 	MRU_INSERT(bp);
289 	return (bp);
290 }
291 
292 extern void
__buf_init(HTAB * hashp,int nbytes)293 __buf_init(HTAB *hashp,	int nbytes)
294 {
295 	BUFHEAD *bfp;
296 	int npages;
297 
298 	bfp = &(hashp->bufhead);
299 	npages = (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
__buf_free(HTAB * hashp,int do_free,int to_disk)316 __buf_free(HTAB *hashp,	int do_free, int to_disk)
317 {
318 	BUFHEAD *bp;
319 
320 	/* Need to make sure that buffer manager has been initialized */
321 	if (!LRU)
322 		return (0);
323 	for (bp = LRU; bp != &hashp->bufhead;) {
324 		/* Check that the buffer is valid */
325 		if (bp->addr || IS_BUCKET(bp->flags)) {
326 			if (to_disk && (bp->flags & BUF_MOD) &&
327 			    __put_page(hashp, bp->page,
328 			    bp->addr, IS_BUCKET(bp->flags), 0))
329 				return (-1);
330 		}
331 		/* Check if we are freeing stuff */
332 		if (do_free) {
333 			if (bp->page)
334 				free(bp->page);
335 			BUF_REMOVE(bp);
336 			free(bp);
337 			bp = LRU;
338 		} else
339 			bp = bp->prev;
340 	}
341 	return (0);
342 }
343 
344 extern void
__reclaim_buf(HTAB * hashp,BUFHEAD * bp)345 __reclaim_buf(HTAB *hashp, 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