xref: /freebsd/lib/libc/db/hash/hash_buf.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 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 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * PACKAGE: hash
43  *
44  * DESCRIPTION:
45  *	Contains buffer management
46  *
47  * ROUTINES:
48  * External
49  *	__buf_init
50  *	__get_buf
51  *	__buf_free
52  *	__reclaim_buf
53  * Internal
54  *	newbuf
55  */
56 
57 #include <sys/param.h>
58 
59 #include <stddef.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 
64 #ifdef DEBUG
65 #include <assert.h>
66 #endif
67 
68 #include <db.h>
69 #include "hash.h"
70 #include "page.h"
71 #include "extern.h"
72 
73 static BUFHEAD *newbuf(HTAB *, u_int32_t, BUFHEAD *);
74 
75 /* Unlink B from its place in the lru */
76 #define BUF_REMOVE(B) { \
77 	(B)->prev->next = (B)->next; \
78 	(B)->next->prev = (B)->prev; \
79 }
80 
81 /* Insert B after P */
82 #define BUF_INSERT(B, P) { \
83 	(B)->next = (P)->next; \
84 	(B)->prev = (P); \
85 	(P)->next = (B); \
86 	(B)->next->prev = (B); \
87 }
88 
89 #define	MRU	hashp->bufhead.next
90 #define	LRU	hashp->bufhead.prev
91 
92 #define MRU_INSERT(B)	BUF_INSERT((B), &hashp->bufhead)
93 #define LRU_INSERT(B)	BUF_INSERT((B), LRU)
94 
95 /*
96  * We are looking for a buffer with address "addr".  If prev_bp is NULL, then
97  * address is a bucket index.  If prev_bp is not NULL, then it points to the
98  * page previous to an overflow page that we are trying to find.
99  *
100  * CAVEAT:  The buffer header accessed via prev_bp's ovfl field may no longer
101  * be valid.  Therefore, you must always verify that its address matches the
102  * address you are seeking.
103  */
104 BUFHEAD *
105 __get_buf(HTAB *hashp, u_int32_t addr,
106     BUFHEAD *prev_bp,	/* If prev_bp set, indicates a new overflow page. */
107     int newpage)
108 {
109 	BUFHEAD *bp;
110 	u_int32_t is_disk_mask;
111 	int is_disk, segment_ndx;
112 	SEGMENT segp;
113 
114 	is_disk = 0;
115 	is_disk_mask = 0;
116 	if (prev_bp) {
117 		bp = prev_bp->ovfl;
118 		if (!bp || (bp->addr != addr))
119 			bp = NULL;
120 		if (!newpage)
121 			is_disk = BUF_DISK;
122 	} else {
123 		/* Grab buffer out of directory */
124 		segment_ndx = addr & (hashp->SGSIZE - 1);
125 
126 		/* valid segment ensured by __call_hash() */
127 		segp = hashp->dir[addr >> hashp->SSHIFT];
128 #ifdef DEBUG
129 		assert(segp != NULL);
130 #endif
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 *)((intptr_t)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  */
157 static BUFHEAD *
158 newbuf(HTAB *hashp, u_int32_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 	u_int16_t oaddr, *shortp;
166 
167 	oaddr = 0;
168 	bp = LRU;
169 
170         /* It is bad to overwrite the page under the cursor. */
171         if (bp == hashp->cpage) {
172                 BUF_REMOVE(bp);
173                 MRU_INSERT(bp);
174                 bp = LRU;
175         }
176 
177 	/* If prev_bp is part of bp overflow, create a new buffer. */
178 	if (hashp->nbufs == 0 && prev_bp && bp->ovfl) {
179 		BUFHEAD *ovfl;
180 
181 		for (ovfl = bp->ovfl; ovfl ; ovfl = ovfl->ovfl) {
182 			if (ovfl == prev_bp) {
183 				hashp->nbufs++;
184 				break;
185 			}
186 		}
187 	}
188 
189 	/*
190 	 * If LRU buffer is pinned, the buffer pool is too small. We need to
191 	 * allocate more buffers.
192 	 */
193 	if (hashp->nbufs || (bp->flags & BUF_PIN) || bp == hashp->cpage) {
194 		/* Allocate a new one */
195 		if ((bp = (BUFHEAD *)calloc(1, sizeof(BUFHEAD))) == NULL)
196 			return (NULL);
197 		if ((bp->page = (char *)calloc(1, hashp->BSIZE)) == NULL) {
198 			free(bp);
199 			return (NULL);
200 		}
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 = (u_int16_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 = NULL;
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 = (u_int16_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 ? prev_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 void
299 __buf_init(HTAB *hashp, int nbytes)
300 {
301 	BUFHEAD *bfp;
302 	int npages;
303 
304 	bfp = &(hashp->bufhead);
305 	npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
306 	npages = MAX(npages, MIN_BUFFERS);
307 
308 	hashp->nbufs = npages;
309 	bfp->next = bfp;
310 	bfp->prev = bfp;
311 	/*
312 	 * This space is calloc'd so these are already null.
313 	 *
314 	 * bfp->ovfl = NULL;
315 	 * bfp->flags = 0;
316 	 * bfp->page = NULL;
317 	 * bfp->addr = 0;
318 	 */
319 }
320 
321 int
322 __buf_free(HTAB *hashp, int do_free, int to_disk)
323 {
324 	BUFHEAD *bp;
325 
326 	/* Need to make sure that buffer manager has been initialized */
327 	if (!LRU)
328 		return (0);
329 	for (bp = LRU; bp != &hashp->bufhead;) {
330 		/* Check that the buffer is valid */
331 		if (bp->addr || IS_BUCKET(bp->flags)) {
332 			if (to_disk && (bp->flags & BUF_MOD) &&
333 			    __put_page(hashp, bp->page,
334 			    bp->addr, IS_BUCKET(bp->flags), 0))
335 				return (-1);
336 		}
337 		/* Check if we are freeing stuff */
338 		if (do_free) {
339 			if (bp->page) {
340 				(void)memset(bp->page, 0, hashp->BSIZE);
341 				free(bp->page);
342 			}
343 			BUF_REMOVE(bp);
344 			free(bp);
345 			bp = LRU;
346 		} else
347 			bp = bp->prev;
348 	}
349 	return (0);
350 }
351 
352 void
353 __reclaim_buf(HTAB *hashp, BUFHEAD *bp)
354 {
355 	bp->ovfl = NULL;
356 	bp->addr = 0;
357 	bp->flags = 0;
358 	BUF_REMOVE(bp);
359 	LRU_INSERT(bp);
360 }
361