1/*	$NetBSD: hash_buf.c,v 1.17 2009/04/23 03:49:39 agc 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.17 2009/04/23 03:49:39 agc 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 <string.h>
65#include <assert.h>
66
67#include <db.h>
68#include "hash.h"
69#include "page.h"
70#include "extern.h"
71
72static BUFHEAD *newbuf(HTAB *, uint32_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 */
103BUFHEAD *
104__get_buf(
105	HTAB *hashp,
106	uint32_t addr,
107	BUFHEAD *prev_bp,
108	int newpage	/* If prev_bp set, indicates a new overflow page. */
109)
110{
111	BUFHEAD *bp;
112	uint32_t is_disk_mask;
113	int is_disk, segment_ndx = 0;	/* pacify gcc */
114	SEGMENT segp = NULL;	/* pacify gcc */
115
116	is_disk = 0;
117	is_disk_mask = 0;
118	if (prev_bp) {
119		bp = prev_bp->ovfl;
120		if (!bp || (bp->addr != addr))
121			bp = NULL;
122		if (!newpage)
123			is_disk = BUF_DISK;
124	} else {
125		/* Grab buffer out of directory */
126		segment_ndx = addr & (hashp->SGSIZE - 1);
127
128		/* valid segment ensured by __call_hash() */
129		segp = hashp->dir[addr >> hashp->SSHIFT];
130		_DIAGASSERT(segp != NULL);
131		bp = PTROF(segp[segment_ndx]);
132		is_disk_mask = ISDISK(segp[segment_ndx]);
133		is_disk = is_disk_mask || !hashp->new_file;
134	}
135
136	if (!bp) {
137		bp = newbuf(hashp, addr, prev_bp);
138		if (!bp ||
139		    __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
140			return (NULL);
141		if (!prev_bp)
142			segp[segment_ndx] =
143			    (BUFHEAD *)(void *)((u_long)bp | is_disk_mask);
144	} else {
145		BUF_REMOVE(bp);
146		MRU_INSERT(bp);
147	}
148	return (bp);
149}
150
151/*
152 * We need a buffer for this page. Either allocate one, or evict a resident
153 * one (if we have as many buffers as we're allowed) and put this one in.
154 *
155 * If newbuf finds an error (returning NULL), it also sets errno.
156 */
157static BUFHEAD *
158newbuf(HTAB *hashp, uint32_t addr, BUFHEAD *prev_bp)
159{
160	BUFHEAD *bp;		/* The buffer we're going to use */
161	BUFHEAD *xbp;		/* Temp pointer */
162	BUFHEAD *next_xbp;
163	SEGMENT segp;
164	int segment_ndx;
165	uint16_t oaddr, *shortp;
166
167	oaddr = 0;
168	bp = LRU;
169	/*
170	 * If LRU buffer is pinned, the buffer pool is too small. We need to
171	 * allocate more buffers.
172	 */
173	if (hashp->nbufs || (bp->flags & BUF_PIN)) {
174		/* Allocate a new one */
175		if ((bp = calloc(1, sizeof(BUFHEAD))) == NULL)
176			return (NULL);
177		if ((bp->page = calloc(1, (size_t)hashp->BSIZE)) == NULL) {
178			free(bp);
179			return (NULL);
180		}
181		if (hashp->nbufs)
182			hashp->nbufs--;
183	} else {
184		/* Kick someone out */
185		BUF_REMOVE(bp);
186		/*
187		 * If this is an overflow page with addr 0, it's already been
188		 * flushed back in an overflow chain and initialized.
189		 */
190		if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
191			/*
192			 * Set oaddr before __put_page so that you get it
193			 * before bytes are swapped.
194			 */
195			shortp = (uint16_t *)(void *)bp->page;
196			if (shortp[0])
197				oaddr = shortp[shortp[0] - 1];
198			if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
199			    bp->addr, (int)IS_BUCKET(bp->flags), 0))
200				return (NULL);
201			/*
202			 * Update the pointer to this page (i.e. invalidate it).
203			 *
204			 * If this is a new file (i.e. we created it at open
205			 * time), make sure that we mark pages which have been
206			 * written to disk so we retrieve them from disk later,
207			 * rather than allocating new pages.
208			 */
209			if (IS_BUCKET(bp->flags)) {
210				segment_ndx = bp->addr & (hashp->SGSIZE - 1);
211				segp = hashp->dir[bp->addr >> hashp->SSHIFT];
212				_DIAGASSERT(segp != NULL);
213
214				if (hashp->new_file &&
215				    ((bp->flags & BUF_MOD) ||
216				    ISDISK(segp[segment_ndx])))
217					segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
218				else
219					segp[segment_ndx] = NULL;
220			}
221			/*
222			 * Since overflow pages can only be access by means of
223			 * their bucket, free overflow pages associated with
224			 * this bucket.
225			 */
226			for (xbp = bp; xbp->ovfl;) {
227				next_xbp = xbp->ovfl;
228				xbp->ovfl = 0;
229				xbp = next_xbp;
230
231				/* Check that ovfl pointer is up date. */
232				if (IS_BUCKET(xbp->flags) ||
233				    (oaddr != xbp->addr))
234					break;
235
236				shortp = (uint16_t *)(void *)xbp->page;
237				if (shortp[0])
238					/* set before __put_page */
239					oaddr = shortp[shortp[0] - 1];
240				if ((xbp->flags & BUF_MOD) && __put_page(hashp,
241				    xbp->page, xbp->addr, 0, 0))
242					return (NULL);
243				xbp->addr = 0;
244				xbp->flags = 0;
245				BUF_REMOVE(xbp);
246				LRU_INSERT(xbp);
247			}
248		}
249	}
250
251	/* Now assign this buffer */
252	bp->addr = addr;
253#ifdef DEBUG1
254	(void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
255	    bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
256#endif
257	bp->ovfl = NULL;
258	if (prev_bp) {
259		/*
260		 * If prev_bp is set, this is an overflow page, hook it in to
261		 * the buffer overflow links.
262		 */
263#ifdef DEBUG1
264		(void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
265		    prev_bp->addr, (prev_bp->ovfl ? prev_bp->ovfl->addr : 0),
266		    (bp ? bp->addr : 0));
267#endif
268		prev_bp->ovfl = bp;
269		bp->flags = 0;
270	} else
271		bp->flags = BUF_BUCKET;
272	MRU_INSERT(bp);
273	return (bp);
274}
275
276void
277__buf_init(HTAB *hashp, u_int nbytes)
278{
279	BUFHEAD *bfp;
280	int npages;
281
282	bfp = &(hashp->bufhead);
283	npages = (unsigned int)(nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
284	npages = MAX(npages, MIN_BUFFERS);
285
286	hashp->nbufs = npages;
287	bfp->next = bfp;
288	bfp->prev = bfp;
289	/*
290	 * This space is calloc'd so these are already null.
291	 *
292	 * bfp->ovfl = NULL;
293	 * bfp->flags = 0;
294	 * bfp->page = NULL;
295	 * bfp->addr = 0;
296	 */
297}
298
299int
300__buf_free(HTAB *hashp, int do_free, int to_disk)
301{
302	BUFHEAD *bp;
303
304	/* Need to make sure that buffer manager has been initialized */
305	if (!LRU)
306		return (0);
307	for (bp = LRU; bp != &hashp->bufhead;) {
308		/* Check that the buffer is valid */
309		if (bp->addr || IS_BUCKET(bp->flags)) {
310			if (to_disk && (bp->flags & BUF_MOD) &&
311			    __put_page(hashp, bp->page,
312			    bp->addr, IS_BUCKET(bp->flags), 0))
313				return (-1);
314		}
315		/* Check if we are freeing stuff */
316		if (do_free) {
317			if (bp->page) {
318				(void)memset(bp->page, 0, (size_t)hashp->BSIZE);
319				free(bp->page);
320			}
321			BUF_REMOVE(bp);
322			free(bp);
323			bp = LRU;
324		} else
325			bp = bp->prev;
326	}
327	return (0);
328}
329
330void
331__reclaim_buf(HTAB *hashp, BUFHEAD *bp)
332{
333	bp->ovfl = 0;
334	bp->addr = 0;
335	bp->flags = 0;
336	BUF_REMOVE(bp);
337	LRU_INSERT(bp);
338}
339