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