Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcr / db / hash / hash_buf.c
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Margo Seltzer.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD: src/lib/libc/db/hash/hash_buf.c,v 1.4.8.1 2001/03/05 07:38:05 obrien Exp $
37  */
38
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] = "@(#)hash_buf.c  8.5 (Berkeley) 7/15/94";
41 #endif /* LIBC_SCCS and not lint */
42
43 /*
44  * PACKAGE: hash
45  *
46  * DESCRIPTION:
47  *      Contains buffer management
48  *
49  * ROUTINES:
50  * External
51  *      __buf_init
52  *      __get_buf
53  *      __buf_free
54  *      __reclaim_buf
55  * Internal
56  *      newbuf
57  */
58
59 #include <sys/param.h>
60
61 #include <stddef.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64
65 #ifdef DEBUG
66 #include <assert.h>
67 #endif
68
69 #include <db.h>
70 #include "hash.h"
71 #include "page.h"
72 #include "extern.h"
73
74 static BUFHEAD *newbuf __P((HTAB *, u_int32_t, BUFHEAD *));
75
76 /* Unlink B from its place in the lru */
77 #define BUF_REMOVE(B) { \
78         (B)->prev->next = (B)->next; \
79         (B)->next->prev = (B)->prev; \
80 }
81
82 /* Insert B after P */
83 #define BUF_INSERT(B, P) { \
84         (B)->next = (P)->next; \
85         (B)->prev = (P); \
86         (P)->next = (B); \
87         (B)->next->prev = (B); \
88 }
89
90 #define MRU     hashp->bufhead.next
91 #define LRU     hashp->bufhead.prev
92
93 #define MRU_INSERT(B)   BUF_INSERT((B), &hashp->bufhead)
94 #define LRU_INSERT(B)   BUF_INSERT((B), LRU)
95
96 /*
97  * We are looking for a buffer with address "addr".  If prev_bp is NULL, then
98  * address is a bucket index.  If prev_bp is not NULL, then it points to the
99  * page previous to an overflow page that we are trying to find.
100  *
101  * CAVEAT:  The buffer header accessed via prev_bp's ovfl field may no longer
102  * be valid.  Therefore, you must always verify that its address matches the
103  * address you are seeking.
104  */
105 extern BUFHEAD *
106 __get_buf(hashp, addr, prev_bp, newpage)
107         HTAB *hashp;
108         u_int32_t addr;
109         BUFHEAD *prev_bp;
110         int newpage;    /* If prev_bp set, indicates a new overflow page. */
111 {
112         register BUFHEAD *bp;
113         register u_int32_t is_disk_mask;
114         register int is_disk, segment_ndx;
115         SEGMENT segp;
116
117         is_disk = 0;
118         is_disk_mask = 0;
119         if (prev_bp) {
120                 bp = prev_bp->ovfl;
121                 if (!bp || (bp->addr != addr))
122                         bp = NULL;
123                 if (!newpage)
124                         is_disk = BUF_DISK;
125         } else {
126                 /* Grab buffer out of directory */
127                 segment_ndx = addr & (hashp->SGSIZE - 1);
128
129                 /* valid segment ensured by __call_hash() */
130                 segp = hashp->dir[addr >> hashp->SSHIFT];
131 #ifdef DEBUG
132                 assert(segp != NULL);
133 #endif
134                 bp = PTROF(segp[segment_ndx]);
135                 is_disk_mask = ISDISK(segp[segment_ndx]);
136                 is_disk = is_disk_mask || !hashp->new_file;
137         }
138
139         if (!bp) {
140                 bp = newbuf(hashp, addr, prev_bp);
141                 if (!bp ||
142                     __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
143                         return (NULL);
144                 if (!prev_bp)
145                         segp[segment_ndx] =
146                             (BUFHEAD *)((ptrdiff_t)bp | is_disk_mask);
147         } else {
148                 BUF_REMOVE(bp);
149                 MRU_INSERT(bp);
150         }
151         return (bp);
152 }
153
154 /*
155  * We need a buffer for this page. Either allocate one, or evict a resident
156  * one (if we have as many buffers as we're allowed) and put this one in.
157  *
158  * If newbuf finds an error (returning NULL), it also sets errno.
159  */
160 static BUFHEAD *
161 newbuf(hashp, addr, prev_bp)
162         HTAB *hashp;
163         u_int32_t addr;
164         BUFHEAD *prev_bp;
165 {
166         register BUFHEAD *bp;           /* The buffer we're going to use */
167         register BUFHEAD *xbp;          /* Temp pointer */
168         register BUFHEAD *next_xbp;
169         SEGMENT segp;
170         int segment_ndx;
171         u_int16_t oaddr, *shortp;
172
173         oaddr = 0;
174         bp = LRU;
175         /*
176          * If LRU buffer is pinned, the buffer pool is too small. We need to
177          * allocate more buffers.
178          */
179         if (hashp->nbufs || (bp->flags & BUF_PIN)) {
180                 /* Allocate a new one */
181                 if ((bp = (BUFHEAD *)malloc(sizeof(BUFHEAD))) == NULL)
182                         return (NULL);
183 #ifdef PURIFY
184                 memset(bp, 0xff, sizeof(BUFHEAD));
185 #endif
186                 if ((bp->page = (char *)malloc(hashp->BSIZE)) == NULL) {
187                         free(bp);
188                         return (NULL);
189                 }
190 #ifdef PURIFY
191                 memset(bp->page, 0xff, hashp->BSIZE);
192 #endif
193                 if (hashp->nbufs)
194                         hashp->nbufs--;
195         } else {
196                 /* Kick someone out */
197                 BUF_REMOVE(bp);
198                 /*
199                  * If this is an overflow page with addr 0, it's already been
200                  * flushed back in an overflow chain and initialized.
201                  */
202                 if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
203                         /*
204                          * Set oaddr before __put_page so that you get it
205                          * before bytes are swapped.
206                          */
207                         shortp = (u_int16_t *)bp->page;
208                         if (shortp[0])
209                                 oaddr = shortp[shortp[0] - 1];
210                         if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
211                             bp->addr, (int)IS_BUCKET(bp->flags), 0))
212                                 return (NULL);
213                         /*
214                          * Update the pointer to this page (i.e. invalidate it).
215                          *
216                          * If this is a new file (i.e. we created it at open
217                          * time), make sure that we mark pages which have been
218                          * written to disk so we retrieve them from disk later,
219                          * rather than allocating new pages.
220                          */
221                         if (IS_BUCKET(bp->flags)) {
222                                 segment_ndx = bp->addr & (hashp->SGSIZE - 1);
223                                 segp = hashp->dir[bp->addr >> hashp->SSHIFT];
224 #ifdef DEBUG
225                                 assert(segp != NULL);
226 #endif
227
228                                 if (hashp->new_file &&
229                                     ((bp->flags & BUF_MOD) ||
230                                     ISDISK(segp[segment_ndx])))
231                                         segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
232                                 else
233                                         segp[segment_ndx] = NULL;
234                         }
235                         /*
236                          * Since overflow pages can only be access by means of
237                          * their bucket, free overflow pages associated with
238                          * this bucket.
239                          */
240                         for (xbp = bp; xbp->ovfl;) {
241                                 next_xbp = xbp->ovfl;
242                                 xbp->ovfl = 0;
243                                 xbp = next_xbp;
244
245                                 /* Check that ovfl pointer is up date. */
246                                 if (IS_BUCKET(xbp->flags) ||
247                                     (oaddr != xbp->addr))
248                                         break;
249
250                                 shortp = (u_int16_t *)xbp->page;
251                                 if (shortp[0])
252                                         /* set before __put_page */
253                                         oaddr = shortp[shortp[0] - 1];
254                                 if ((xbp->flags & BUF_MOD) && __put_page(hashp,
255                                     xbp->page, xbp->addr, 0, 0))
256                                         return (NULL);
257                                 xbp->addr = 0;
258                                 xbp->flags = 0;
259                                 BUF_REMOVE(xbp);
260                                 LRU_INSERT(xbp);
261                         }
262                 }
263         }
264
265         /* Now assign this buffer */
266         bp->addr = addr;
267 #ifdef DEBUG1
268         (void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
269             bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
270 #endif
271         bp->ovfl = NULL;
272         if (prev_bp) {
273                 /*
274                  * If prev_bp is set, this is an overflow page, hook it in to
275                  * the buffer overflow links.
276                  */
277 #ifdef DEBUG1
278                 (void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
279                     prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0),
280                     (bp ? bp->addr : 0));
281 #endif
282                 prev_bp->ovfl = bp;
283                 bp->flags = 0;
284         } else
285                 bp->flags = BUF_BUCKET;
286         MRU_INSERT(bp);
287         return (bp);
288 }
289
290 extern void
291 __buf_init(hashp, nbytes)
292         HTAB *hashp;
293         int nbytes;
294 {
295         BUFHEAD *bfp;
296         int npages;
297
298         bfp = &(hashp->bufhead);
299         npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
300         npages = MAX(npages, MIN_BUFFERS);
301
302         hashp->nbufs = npages;
303         bfp->next = bfp;
304         bfp->prev = bfp;
305         /*
306          * This space is calloc'd so these are already null.
307          *
308          * bfp->ovfl = NULL;
309          * bfp->flags = 0;
310          * bfp->page = NULL;
311          * bfp->addr = 0;
312          */
313 }
314
315 extern int
316 __buf_free(hashp, do_free, to_disk)
317         HTAB *hashp;
318         int do_free, to_disk;
319 {
320         BUFHEAD *bp;
321
322         /* Need to make sure that buffer manager has been initialized */
323         if (!LRU)
324                 return (0);
325         for (bp = LRU; bp != &hashp->bufhead;) {
326                 /* Check that the buffer is valid */
327                 if (bp->addr || IS_BUCKET(bp->flags)) {
328                         if (to_disk && (bp->flags & BUF_MOD) &&
329                             __put_page(hashp, bp->page,
330                             bp->addr, IS_BUCKET(bp->flags), 0))
331                                 return (-1);
332                 }
333                 /* Check if we are freeing stuff */
334                 if (do_free) {
335                         if (bp->page)
336                                 free(bp->page);
337                         BUF_REMOVE(bp);
338                         free(bp);
339                         bp = LRU;
340                 } else
341                         bp = bp->prev;
342         }
343         return (0);
344 }
345
346 extern void
347 __reclaim_buf(hashp, bp)
348         HTAB *hashp;
349         BUFHEAD *bp;
350 {
351         bp->ovfl = 0;
352         bp->addr = 0;
353         bp->flags = 0;
354         BUF_REMOVE(bp);
355         LRU_INSERT(bp);
356 }