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