xref: /original-bsd/lib/libc/db/hash/hash_buf.c (revision e59fb703)
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.9 (Berkeley) 09/08/91";
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 		hashp->nbufs--;
151 	} else {
152 		/* Kick someone out */
153 		BUF_REMOVE(bp);
154 		/*
155 		 * If this is an overflow page with addr 0, it's already been
156 		 * flushed back in an overflow chain and initialized.
157 		 */
158 		if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
159 			/*
160 			 * Set oaddr before __put_page so that you get it
161 			 * before bytes are swapped.
162 			 */
163 			shortp = (u_short *)bp->page;
164 			if (shortp[0])
165 				oaddr = shortp[shortp[0] - 1];
166 			if ((bp->flags & BUF_MOD) && __put_page(bp->page,
167 			    bp->addr, (int)IS_BUCKET(bp->flags), 0))
168 				return (NULL);
169 			/*
170 			 * Update the pointer to this page (i.e. invalidate it).
171 			 *
172 			 * If this is a new file (i.e. we created it at open
173 			 * time), make sure that we mark pages which have been
174 			 * written to disk so we retrieve them from disk later,
175 			 * rather than allocating new pages.
176 			 */
177 			if (IS_BUCKET(bp->flags)) {
178 				segment_ndx = bp->addr & (hashp->SGSIZE - 1);
179 				segp = hashp->dir[bp->addr >> hashp->SSHIFT];
180 #ifdef DEBUG
181 				assert(segp != NULL);
182 #endif
183 
184 				if (hashp->new_file &&
185 				    ((bp->flags & BUF_MOD) ||
186 				    ISDISK(segp[segment_ndx])))
187 					segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
188 				else
189 					segp[segment_ndx] = NULL;
190 			}
191 			/*
192 			 * Since overflow pages can only be access by means of
193 			 * their bucket, free overflow pages associated with
194 			 * this bucket.
195 			 */
196 			for (xbp = bp; xbp->ovfl;) {
197 				next_xbp = xbp->ovfl;
198 				xbp->ovfl = 0;
199 				xbp = next_xbp;
200 
201 				/* Check that ovfl pointer is up date. */
202 				if (IS_BUCKET(xbp->flags) ||
203 				    (oaddr != xbp->addr))
204 					break;
205 
206 				shortp = (u_short *)xbp->page;
207 				if (shortp[0])
208 					/* set before __put_page */
209 					oaddr = shortp[shortp[0] - 1];
210 				if ((xbp->flags & BUF_MOD) &&
211 				    __put_page(xbp->page, xbp->addr, 0, 0))
212 					return (NULL);
213 				xbp->addr = 0;
214 				xbp->flags = 0;
215 				BUF_REMOVE(xbp);
216 				LRU_INSERT(xbp);
217 			}
218 		}
219 	}
220 
221 	/* Now assign this buffer */
222 	bp->addr = addr;
223 #ifdef DEBUG1
224 	(void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
225 	    bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
226 #endif
227 	bp->ovfl = NULL;
228 	if (prev_bp) {
229 		/*
230 		 * If prev_bp is set, this is an overflow page, hook it in to
231 		 * the buffer overflow links.
232 		 */
233 #ifdef DEBUG1
234 		(void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
235 		    prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0),
236 		    (bp ? bp->addr : 0));
237 #endif
238 		prev_bp->ovfl = bp;
239 		bp->flags = 0;
240 	} else
241 		bp->flags = BUF_BUCKET;
242 	MRU_INSERT(bp);
243 	return (bp);
244 }
245 
246 extern void
247 __buf_init(nbytes)
248 	int nbytes;
249 {
250 	BUFHEAD *bfp;
251 	int npages;
252 
253 	bfp = &(hashp->bufhead);
254 	npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
255 	npages = MAX(npages, MIN_BUFFERS);
256 
257 	hashp->nbufs = npages;
258 	bfp->next = bfp;
259 	bfp->prev = bfp;
260 	/*
261 	 * This space is calloc'd so these are already null.
262 	 *
263 	 * bfp->ovfl = NULL;
264 	 * bfp->flags = 0;
265 	 * bfp->page = NULL;
266 	 * bfp->addr = 0;
267 	 */
268 }
269 
270 extern int
271 __buf_free(do_free, to_disk)
272 	int do_free, to_disk;
273 {
274 	BUFHEAD *bp;
275 
276 	/* Need to make sure that buffer manager has been initialized */
277 	if (!LRU)
278 		return (0);
279 	for (bp = LRU; bp != &hashp->bufhead;) {
280 		/* Check that the buffer is valid */
281 		if (bp->addr || IS_BUCKET(bp->flags)) {
282 			if (to_disk && (bp->flags & BUF_MOD) &&
283 			    __put_page(bp->page,
284 			    bp->addr, IS_BUCKET(bp->flags), 0))
285 				return (-1);
286 		}
287 		/* Check if we are freeing stuff */
288 		if (do_free) {
289 			if (bp->page)
290 				free(bp->page);
291 			BUF_REMOVE(bp);
292 			free(bp);
293 			bp = LRU;
294 		} else
295 			bp = bp->prev;
296 	}
297 	return (0);
298 }
299 
300 extern void
301 __reclaim_buf(bp)
302 	BUFHEAD *bp;
303 {
304 	bp->ovfl = 0;
305 	bp->addr = 0;
306 	bp->flags = 0;
307 	BUF_REMOVE(bp);
308 	LRU_INSERT(bp);
309 }
310