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