xref: /original-bsd/lib/libc/db/hash/hash_buf.c (revision 3b3772fe)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Margo Seltzer.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)hash_buf.c	5.10 (Berkeley) 09/29/92";
13 #endif /* LIBC_SCCS and not lint */
14 
15 /*
16  * PACKAGE: hash
17  *
18  * DESCRIPTION:
19  *	Contains buffer management
20  *
21  * ROUTINES:
22  * External
23  *	__buf_init
24  *	__get_buf
25  *	__buf_free
26  *	__reclaim_buf
27  * Internal
28  *	newbuf
29  */
30 
31 #include <sys/param.h>
32 #include <db.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #ifdef DEBUG
37 #include <assert.h>
38 #endif
39 #include "hash.h"
40 #include "page.h"
41 #include "extern.h"
42 
43 static BUFHEAD *newbuf __P((u_int, BUFHEAD *));
44 
45 /* Unlink B from its place in the lru */
46 #define BUF_REMOVE(B) { \
47 	(B)->prev->next = (B)->next; \
48 	(B)->next->prev = (B)->prev; \
49 }
50 
51 /* Insert B after P */
52 #define BUF_INSERT(B, P) { \
53 	(B)->next = (P)->next; \
54 	(B)->prev = (P); \
55 	(P)->next = (B); \
56 	(B)->next->prev = (B); \
57 }
58 
59 #define	MRU	hashp->bufhead.next
60 #define	LRU	hashp->bufhead.prev
61 
62 #define MRU_INSERT(B)	BUF_INSERT((B), &hashp->bufhead)
63 #define LRU_INSERT(B)	BUF_INSERT((B), LRU)
64 
65 /*
66  * We are looking for a buffer with address "addr".  If prev_bp is NULL, then
67  * address is a bucket index.  If prev_bp is not NULL, then it points to the
68  * page previous to an overflow page that we are trying to find.
69  *
70  * CAVEAT:  The buffer header accessed via prev_bp's ovfl field may no longer
71  * be valid.  Therefore, you must always verify that its address matches the
72  * address you are seeking.
73  */
74 extern BUFHEAD *
75 __get_buf(addr, prev_bp, newpage)
76 	u_int addr;
77 	BUFHEAD *prev_bp;
78 	int newpage;	/* If prev_bp set, indicates a new overflow page. */
79 {
80 	register BUFHEAD *bp;
81 	register u_int is_disk_mask;
82 	register int is_disk, segment_ndx;
83 	SEGMENT segp;
84 
85 	is_disk = 0;
86 	is_disk_mask = 0;
87 	if (prev_bp) {
88 		bp = prev_bp->ovfl;
89 		if (!bp || (bp->addr != addr))
90 			bp = NULL;
91 		if (!newpage)
92 			is_disk = BUF_DISK;
93 	} else {
94 		/* Grab buffer out of directory */
95 		segment_ndx = addr & (hashp->SGSIZE - 1);
96 
97 		/* valid segment ensured by __call_hash() */
98 		segp = hashp->dir[addr >> hashp->SSHIFT];
99 #ifdef DEBUG
100 		assert(segp != NULL);
101 #endif
102 		bp = PTROF(segp[segment_ndx]);
103 		is_disk_mask = ISDISK(segp[segment_ndx]);
104 		is_disk = is_disk_mask || !hashp->new_file;
105 	}
106 
107 	if (!bp) {
108 		bp = newbuf(addr, prev_bp);
109 		if (!bp || __get_page(bp->page, addr, !prev_bp, is_disk, 0))
110 			return (NULL);
111 		if (!prev_bp)
112 			segp[segment_ndx] =
113 			    (BUFHEAD *)((u_int)bp | is_disk_mask);
114 	} else {
115 		BUF_REMOVE(bp);
116 		MRU_INSERT(bp);
117 	}
118 	return (bp);
119 }
120 
121 /*
122  * We need a buffer for this page. Either allocate one, or evict a resident
123  * one (if we have as many buffers as we're allowed) and put this one in.
124  *
125  * If newbuf finds an error (returning NULL), it also sets errno.
126  */
127 static BUFHEAD *
128 newbuf(addr, prev_bp)
129 	u_int   addr;
130 	BUFHEAD *prev_bp;
131 {
132 	register BUFHEAD *bp;		/* The buffer we're going to use */
133 	register BUFHEAD *xbp;		/* Temp pointer */
134 	register BUFHEAD *next_xbp;
135 	SEGMENT segp;
136 	int segment_ndx;
137 	u_short oaddr, *shortp;
138 
139 	oaddr = 0;
140 	bp = LRU;
141 	/*
142 	 * If LRU buffer is pinned, the buffer pool is too small. We need to
143 	 * allocate more buffers.
144 	 */
145 	if (hashp->nbufs || (bp->flags & BUF_PIN)) {
146 		/* Allocate a new one */
147 		bp = malloc(sizeof(struct _bufhead));
148 		if (!bp || !(bp->page = malloc(hashp->BSIZE)))
149 			return (NULL);
150 		if (hashp->nbufs)
151 			hashp->nbufs--;
152 	} else {
153 		/* Kick someone out */
154 		BUF_REMOVE(bp);
155 		/*
156 		 * If this is an overflow page with addr 0, it's already been
157 		 * flushed back in an overflow chain and initialized.
158 		 */
159 		if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
160 			/*
161 			 * Set oaddr before __put_page so that you get it
162 			 * before bytes are swapped.
163 			 */
164 			shortp = (u_short *)bp->page;
165 			if (shortp[0])
166 				oaddr = shortp[shortp[0] - 1];
167 			if ((bp->flags & BUF_MOD) && __put_page(bp->page,
168 			    bp->addr, (int)IS_BUCKET(bp->flags), 0))
169 				return (NULL);
170 			/*
171 			 * Update the pointer to this page (i.e. invalidate it).
172 			 *
173 			 * If this is a new file (i.e. we created it at open
174 			 * time), make sure that we mark pages which have been
175 			 * written to disk so we retrieve them from disk later,
176 			 * rather than allocating new pages.
177 			 */
178 			if (IS_BUCKET(bp->flags)) {
179 				segment_ndx = bp->addr & (hashp->SGSIZE - 1);
180 				segp = hashp->dir[bp->addr >> hashp->SSHIFT];
181 #ifdef DEBUG
182 				assert(segp != NULL);
183 #endif
184 
185 				if (hashp->new_file &&
186 				    ((bp->flags & BUF_MOD) ||
187 				    ISDISK(segp[segment_ndx])))
188 					segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
189 				else
190 					segp[segment_ndx] = NULL;
191 			}
192 			/*
193 			 * Since overflow pages can only be access by means of
194 			 * their bucket, free overflow pages associated with
195 			 * this bucket.
196 			 */
197 			for (xbp = bp; xbp->ovfl;) {
198 				next_xbp = xbp->ovfl;
199 				xbp->ovfl = 0;
200 				xbp = next_xbp;
201 
202 				/* Check that ovfl pointer is up date. */
203 				if (IS_BUCKET(xbp->flags) ||
204 				    (oaddr != xbp->addr))
205 					break;
206 
207 				shortp = (u_short *)xbp->page;
208 				if (shortp[0])
209 					/* set before __put_page */
210 					oaddr = shortp[shortp[0] - 1];
211 				if ((xbp->flags & BUF_MOD) &&
212 				    __put_page(xbp->page, xbp->addr, 0, 0))
213 					return (NULL);
214 				xbp->addr = 0;
215 				xbp->flags = 0;
216 				BUF_REMOVE(xbp);
217 				LRU_INSERT(xbp);
218 			}
219 		}
220 	}
221 
222 	/* Now assign this buffer */
223 	bp->addr = addr;
224 #ifdef DEBUG1
225 	(void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
226 	    bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
227 #endif
228 	bp->ovfl = NULL;
229 	if (prev_bp) {
230 		/*
231 		 * If prev_bp is set, this is an overflow page, hook it in to
232 		 * the buffer overflow links.
233 		 */
234 #ifdef DEBUG1
235 		(void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
236 		    prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0),
237 		    (bp ? bp->addr : 0));
238 #endif
239 		prev_bp->ovfl = bp;
240 		bp->flags = 0;
241 	} else
242 		bp->flags = BUF_BUCKET;
243 	MRU_INSERT(bp);
244 	return (bp);
245 }
246 
247 extern void
248 __buf_init(nbytes)
249 	int nbytes;
250 {
251 	BUFHEAD *bfp;
252 	int npages;
253 
254 	bfp = &(hashp->bufhead);
255 	npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
256 	npages = MAX(npages, MIN_BUFFERS);
257 
258 	hashp->nbufs = npages;
259 	bfp->next = bfp;
260 	bfp->prev = bfp;
261 	/*
262 	 * This space is calloc'd so these are already null.
263 	 *
264 	 * bfp->ovfl = NULL;
265 	 * bfp->flags = 0;
266 	 * bfp->page = NULL;
267 	 * bfp->addr = 0;
268 	 */
269 }
270 
271 extern int
272 __buf_free(do_free, to_disk)
273 	int do_free, to_disk;
274 {
275 	BUFHEAD *bp;
276 
277 	/* Need to make sure that buffer manager has been initialized */
278 	if (!LRU)
279 		return (0);
280 	for (bp = LRU; bp != &hashp->bufhead;) {
281 		/* Check that the buffer is valid */
282 		if (bp->addr || IS_BUCKET(bp->flags)) {
283 			if (to_disk && (bp->flags & BUF_MOD) &&
284 			    __put_page(bp->page,
285 			    bp->addr, IS_BUCKET(bp->flags), 0))
286 				return (-1);
287 		}
288 		/* Check if we are freeing stuff */
289 		if (do_free) {
290 			if (bp->page)
291 				free(bp->page);
292 			BUF_REMOVE(bp);
293 			free(bp);
294 			bp = LRU;
295 		} else
296 			bp = bp->prev;
297 	}
298 	return (0);
299 }
300 
301 extern void
302 __reclaim_buf(bp)
303 	BUFHEAD *bp;
304 {
305 	bp->ovfl = 0;
306 	bp->addr = 0;
307 	bp->flags = 0;
308 	BUF_REMOVE(bp);
309 	LRU_INSERT(bp);
310 }
311