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