1 1.21 joerg /* $NetBSD: bufcache.c,v 1.21 2020/04/03 19:36:33 joerg Exp $ */ 2 1.1 perseant /*- 3 1.1 perseant * Copyright (c) 2003 The NetBSD Foundation, Inc. 4 1.1 perseant * All rights reserved. 5 1.1 perseant * 6 1.1 perseant * This code is derived from software contributed to The NetBSD Foundation 7 1.1 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>. 8 1.1 perseant * 9 1.1 perseant * Redistribution and use in source and binary forms, with or without 10 1.1 perseant * modification, are permitted provided that the following conditions 11 1.1 perseant * are met: 12 1.1 perseant * 1. Redistributions of source code must retain the above copyright 13 1.1 perseant * notice, this list of conditions and the following disclaimer. 14 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 perseant * notice, this list of conditions and the following disclaimer in the 16 1.1 perseant * documentation and/or other materials provided with the distribution. 17 1.1 perseant * 18 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 1.1 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 1.1 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 1.1 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 1.1 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 1.1 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 1.1 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 1.1 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 1.1 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 1.1 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 1.1 perseant * POSSIBILITY OF SUCH DAMAGE. 29 1.1 perseant */ 30 1.1 perseant 31 1.1 perseant #include <sys/types.h> 32 1.1 perseant #include <sys/param.h> 33 1.1 perseant #include <sys/time.h> 34 1.1 perseant #include <sys/buf.h> 35 1.1 perseant #include <sys/queue.h> 36 1.1 perseant #include <sys/mount.h> 37 1.1 perseant 38 1.1 perseant #include <assert.h> 39 1.1 perseant #include <err.h> 40 1.1 perseant #include <stdio.h> 41 1.1 perseant #include <stdlib.h> 42 1.1 perseant #include <string.h> 43 1.1 perseant #include <unistd.h> 44 1.10 christos #include <util.h> 45 1.1 perseant 46 1.1 perseant #include "bufcache.h" 47 1.21 joerg #include "extern.h" 48 1.1 perseant 49 1.1 perseant /* 50 1.1 perseant * Definitions for the buffer free lists. 51 1.1 perseant */ 52 1.1 perseant #define BQUEUES 3 /* number of free buffer queues */ 53 1.1 perseant 54 1.1 perseant #define BQ_LOCKED 0 /* super-blocks &c */ 55 1.1 perseant #define BQ_LRU 1 /* lru, useful buffers */ 56 1.1 perseant #define BQ_AGE 2 /* rubbish */ 57 1.1 perseant 58 1.1 perseant TAILQ_HEAD(bqueues, ubuf) bufqueues[BQUEUES]; 59 1.1 perseant 60 1.6 perseant struct bufhash_struct *bufhash; 61 1.1 perseant 62 1.6 perseant #define HASH_MAX 1024 63 1.6 perseant int hashmax = HASH_MAX; 64 1.6 perseant int hashmask = (HASH_MAX - 1); 65 1.1 perseant 66 1.1 perseant int maxbufs = BUF_CACHE_SIZE; 67 1.1 perseant int nbufs = 0; 68 1.1 perseant int cachehits = 0; 69 1.1 perseant int cachemisses = 0; 70 1.6 perseant int max_depth = 0; 71 1.1 perseant off_t locked_queue_bytes = 0; 72 1.4 perseant int locked_queue_count = 0; 73 1.1 perseant 74 1.1 perseant /* Simple buffer hash function */ 75 1.1 perseant static int 76 1.1 perseant vl_hash(struct uvnode * vp, daddr_t lbn) 77 1.1 perseant { 78 1.6 perseant return (int)((unsigned long) vp + lbn) & hashmask; 79 1.1 perseant } 80 1.1 perseant 81 1.1 perseant /* Initialize buffer cache */ 82 1.1 perseant void 83 1.6 perseant bufinit(int max) 84 1.1 perseant { 85 1.1 perseant int i; 86 1.1 perseant 87 1.6 perseant if (max) { 88 1.6 perseant for (hashmax = 1; hashmax < max; hashmax <<= 1) 89 1.6 perseant ; 90 1.6 perseant hashmask = hashmax - 1; 91 1.6 perseant } 92 1.6 perseant 93 1.1 perseant for (i = 0; i < BQUEUES; i++) { 94 1.1 perseant TAILQ_INIT(&bufqueues[i]); 95 1.1 perseant } 96 1.10 christos bufhash = emalloc(hashmax * sizeof(*bufhash)); 97 1.6 perseant for (i = 0; i < hashmax; i++) 98 1.4 perseant LIST_INIT(&bufhash[i]); 99 1.1 perseant } 100 1.1 perseant 101 1.6 perseant /* Widen the hash table. */ 102 1.6 perseant void bufrehash(int max) 103 1.6 perseant { 104 1.16 dholland int i, newhashmax; 105 1.6 perseant struct ubuf *bp, *nbp; 106 1.6 perseant struct bufhash_struct *np; 107 1.6 perseant 108 1.16 dholland if (max < 0 || max <= hashmax) 109 1.6 perseant return; 110 1.6 perseant 111 1.6 perseant /* Round up to a power of two */ 112 1.6 perseant for (newhashmax = 1; newhashmax < max; newhashmax <<= 1) 113 1.6 perseant ; 114 1.16 dholland 115 1.16 dholland /* update the mask right away so vl_hash() uses it */ 116 1.16 dholland hashmask = newhashmax - 1; 117 1.6 perseant 118 1.7 perseant /* Allocate new empty hash table, if we can */ 119 1.10 christos np = emalloc(newhashmax * sizeof(*bufhash)); 120 1.6 perseant for (i = 0; i < newhashmax; i++) 121 1.6 perseant LIST_INIT(&np[i]); 122 1.6 perseant 123 1.6 perseant /* Now reassign all existing buffers to their new hash chains. */ 124 1.6 perseant for (i = 0; i < hashmax; i++) { 125 1.6 perseant bp = LIST_FIRST(&bufhash[i]); 126 1.6 perseant while(bp) { 127 1.6 perseant nbp = LIST_NEXT(bp, b_hash); 128 1.6 perseant LIST_REMOVE(bp, b_hash); 129 1.6 perseant bp->b_hashval = vl_hash(bp->b_vp, bp->b_lblkno); 130 1.6 perseant LIST_INSERT_HEAD(&np[bp->b_hashval], bp, b_hash); 131 1.6 perseant bp = nbp; 132 1.6 perseant } 133 1.6 perseant } 134 1.6 perseant 135 1.6 perseant /* Switch over and clean up */ 136 1.6 perseant free(bufhash); 137 1.6 perseant bufhash = np; 138 1.6 perseant hashmax = newhashmax; 139 1.6 perseant } 140 1.6 perseant 141 1.1 perseant /* Print statistics of buffer cache usage */ 142 1.1 perseant void 143 1.1 perseant bufstats(void) 144 1.1 perseant { 145 1.6 perseant printf("buffer cache: %d hits %d misses (%2.2f%%); hash width %d, depth %d\n", 146 1.1 perseant cachehits, cachemisses, 147 1.1 perseant (cachehits * 100.0) / (cachehits + cachemisses), 148 1.6 perseant hashmax, max_depth); 149 1.1 perseant } 150 1.1 perseant 151 1.1 perseant /* 152 1.1 perseant * Remove a buffer from the cache. 153 1.1 perseant * Caller must remove the buffer from its free list. 154 1.1 perseant */ 155 1.1 perseant void 156 1.1 perseant buf_destroy(struct ubuf * bp) 157 1.1 perseant { 158 1.1 perseant LIST_REMOVE(bp, b_vnbufs); 159 1.1 perseant LIST_REMOVE(bp, b_hash); 160 1.7 perseant if (!(bp->b_flags & B_DONTFREE)) 161 1.7 perseant free(bp->b_data); 162 1.1 perseant free(bp); 163 1.1 perseant --nbufs; 164 1.1 perseant } 165 1.1 perseant 166 1.1 perseant /* Remove a buffer from its free list. */ 167 1.1 perseant void 168 1.1 perseant bremfree(struct ubuf * bp) 169 1.1 perseant { 170 1.1 perseant struct bqueues *dp = NULL; 171 1.1 perseant 172 1.1 perseant /* 173 1.1 perseant * We only calculate the head of the freelist when removing 174 1.1 perseant * the last element of the list as that is the only time that 175 1.1 perseant * it is needed (e.g. to reset the tail pointer). 176 1.1 perseant */ 177 1.2 perseant if (bp->b_flags & B_LOCKED) { 178 1.1 perseant locked_queue_bytes -= bp->b_bcount; 179 1.4 perseant --locked_queue_count; 180 1.2 perseant } 181 1.1 perseant if (TAILQ_NEXT(bp, b_freelist) == NULL) { 182 1.1 perseant for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++) 183 1.19 christos if (TAILQ_LAST(dp, bqueues) == bp) 184 1.1 perseant break; 185 1.1 perseant if (dp == &bufqueues[BQUEUES]) 186 1.1 perseant errx(1, "bremfree: lost tail"); 187 1.1 perseant } 188 1.1 perseant ++bp->b_vp->v_usecount; 189 1.1 perseant TAILQ_REMOVE(dp, bp, b_freelist); 190 1.1 perseant } 191 1.1 perseant 192 1.1 perseant /* Return a buffer if it is in the cache, otherwise return NULL. */ 193 1.1 perseant struct ubuf * 194 1.20 christos incore(struct uvnode * vp, daddr_t lbn) 195 1.1 perseant { 196 1.1 perseant struct ubuf *bp; 197 1.1 perseant int hash, depth; 198 1.1 perseant 199 1.1 perseant hash = vl_hash(vp, lbn); 200 1.1 perseant /* XXX use a real hash instead. */ 201 1.1 perseant depth = 0; 202 1.1 perseant LIST_FOREACH(bp, &bufhash[hash], b_hash) { 203 1.6 perseant if (++depth > max_depth) 204 1.6 perseant max_depth = depth; 205 1.6 perseant assert(depth <= nbufs); 206 1.1 perseant if (bp->b_vp == vp && bp->b_lblkno == lbn) { 207 1.1 perseant return bp; 208 1.1 perseant } 209 1.1 perseant } 210 1.1 perseant return NULL; 211 1.1 perseant } 212 1.1 perseant 213 1.1 perseant /* 214 1.1 perseant * Return a buffer of the given size, lbn and uvnode. 215 1.1 perseant * If none is in core, make a new one. 216 1.1 perseant */ 217 1.1 perseant struct ubuf * 218 1.1 perseant getblk(struct uvnode * vp, daddr_t lbn, int size) 219 1.1 perseant { 220 1.1 perseant struct ubuf *bp; 221 1.3 perseant #ifdef DEBUG 222 1.3 perseant static int warned; 223 1.3 perseant #endif 224 1.1 perseant 225 1.1 perseant /* 226 1.1 perseant * First check the buffer cache lists. 227 1.5 perseant * We might sometimes need to resize a buffer. If we are growing 228 1.5 perseant * the buffer, its contents are invalid; but shrinking is okay. 229 1.1 perseant */ 230 1.1 perseant if ((bp = incore(vp, lbn)) != NULL) { 231 1.1 perseant assert(!(bp->b_flags & B_BUSY)); 232 1.1 perseant bp->b_flags |= B_BUSY; 233 1.1 perseant bremfree(bp); 234 1.5 perseant if (bp->b_bcount == size) 235 1.5 perseant return bp; 236 1.5 perseant else if (bp->b_bcount > size) { 237 1.5 perseant assert(!(bp->b_flags & B_DELWRI)); 238 1.5 perseant bp->b_bcount = size; 239 1.10 christos bp->b_data = erealloc(bp->b_data, size); 240 1.5 perseant return bp; 241 1.5 perseant } 242 1.5 perseant 243 1.5 perseant buf_destroy(bp); 244 1.5 perseant bp = NULL; 245 1.1 perseant } 246 1.5 perseant 247 1.1 perseant /* 248 1.1 perseant * Not on the list. 249 1.1 perseant * Get a new block of the appropriate size and use that. 250 1.1 perseant * If not enough space, free blocks from the AGE and LRU lists 251 1.1 perseant * to make room. 252 1.1 perseant */ 253 1.4 perseant while (nbufs >= maxbufs + locked_queue_count) { 254 1.1 perseant bp = TAILQ_FIRST(&bufqueues[BQ_AGE]); 255 1.1 perseant if (bp) 256 1.1 perseant TAILQ_REMOVE(&bufqueues[BQ_AGE], bp, b_freelist); 257 1.1 perseant if (bp == NULL) { 258 1.1 perseant bp = TAILQ_FIRST(&bufqueues[BQ_LRU]); 259 1.1 perseant if (bp) 260 1.3 perseant TAILQ_REMOVE(&bufqueues[BQ_LRU], bp, 261 1.1 perseant b_freelist); 262 1.1 perseant } 263 1.1 perseant if (bp) { 264 1.1 perseant if (bp->b_flags & B_DELWRI) 265 1.1 perseant VOP_STRATEGY(bp); 266 1.1 perseant buf_destroy(bp); 267 1.6 perseant break; 268 1.1 perseant } 269 1.1 perseant #ifdef DEBUG 270 1.7 perseant else if (!warned) { 271 1.7 perseant warnx("allocating more than %d buffers", maxbufs); 272 1.7 perseant ++warned; 273 1.1 perseant } 274 1.1 perseant #endif 275 1.7 perseant break; 276 1.1 perseant } 277 1.1 perseant ++nbufs; 278 1.10 christos bp = ecalloc(1, sizeof(*bp)); 279 1.10 christos bp->b_data = ecalloc(1, size); 280 1.1 perseant bp->b_vp = vp; 281 1.1 perseant bp->b_blkno = bp->b_lblkno = lbn; 282 1.1 perseant bp->b_bcount = size; 283 1.6 perseant bp->b_hashval = vl_hash(vp, lbn); 284 1.6 perseant LIST_INSERT_HEAD(&bufhash[bp->b_hashval], bp, b_hash); 285 1.1 perseant LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs); 286 1.1 perseant bp->b_flags = B_BUSY; 287 1.1 perseant 288 1.1 perseant return bp; 289 1.1 perseant } 290 1.1 perseant 291 1.1 perseant /* Write a buffer to disk according to its strategy routine. */ 292 1.1 perseant void 293 1.1 perseant bwrite(struct ubuf * bp) 294 1.1 perseant { 295 1.1 perseant bp->b_flags &= ~(B_READ | B_DONE | B_DELWRI | B_LOCKED); 296 1.1 perseant VOP_STRATEGY(bp); 297 1.1 perseant bp->b_flags |= B_DONE; 298 1.1 perseant reassignbuf(bp, bp->b_vp); 299 1.11 ad brelse(bp, 0); 300 1.1 perseant } 301 1.1 perseant 302 1.1 perseant /* Put a buffer back on its free list, clear B_BUSY. */ 303 1.1 perseant void 304 1.11 ad brelse(struct ubuf * bp, int set) 305 1.1 perseant { 306 1.1 perseant int age; 307 1.1 perseant 308 1.1 perseant assert(bp->b_flags & B_BUSY); 309 1.1 perseant 310 1.11 ad bp->b_flags |= set; 311 1.11 ad 312 1.1 perseant age = bp->b_flags & B_AGE; 313 1.1 perseant bp->b_flags &= ~(B_BUSY | B_AGE); 314 1.1 perseant if (bp->b_flags & B_INVAL) { 315 1.1 perseant buf_destroy(bp); 316 1.1 perseant return; 317 1.1 perseant } 318 1.1 perseant if (bp->b_flags & B_LOCKED) { 319 1.1 perseant locked_queue_bytes += bp->b_bcount; 320 1.4 perseant ++locked_queue_count; 321 1.1 perseant TAILQ_INSERT_TAIL(&bufqueues[BQ_LOCKED], bp, b_freelist); 322 1.1 perseant } else if (age) { 323 1.1 perseant TAILQ_INSERT_TAIL(&bufqueues[BQ_AGE], bp, b_freelist); 324 1.1 perseant } else { 325 1.1 perseant TAILQ_INSERT_TAIL(&bufqueues[BQ_LRU], bp, b_freelist); 326 1.1 perseant } 327 1.1 perseant --bp->b_vp->v_usecount; 328 1.6 perseant 329 1.6 perseant /* Move to the front of the hash chain */ 330 1.6 perseant if (LIST_FIRST(&bufhash[bp->b_hashval]) != bp) { 331 1.6 perseant LIST_REMOVE(bp, b_hash); 332 1.6 perseant LIST_INSERT_HEAD(&bufhash[bp->b_hashval], bp, b_hash); 333 1.6 perseant } 334 1.1 perseant } 335 1.1 perseant 336 1.1 perseant /* Read the given block from disk, return it B_BUSY. */ 337 1.1 perseant int 338 1.15 chopps bread(struct uvnode * vp, daddr_t lbn, int size, int flags, struct ubuf ** bpp) 339 1.1 perseant { 340 1.1 perseant struct ubuf *bp; 341 1.1 perseant daddr_t daddr; 342 1.1 perseant 343 1.1 perseant bp = getblk(vp, lbn, size); 344 1.1 perseant *bpp = bp; 345 1.1 perseant if (bp->b_flags & (B_DELWRI | B_DONE)) { 346 1.1 perseant ++cachehits; 347 1.1 perseant return 0; 348 1.1 perseant } 349 1.1 perseant ++cachemisses; 350 1.1 perseant 351 1.1 perseant /* 352 1.1 perseant * Not found. Need to find that block's location on disk, 353 1.1 perseant * and load it in. 354 1.1 perseant */ 355 1.1 perseant daddr = -1; 356 1.14 christos (void)VOP_BMAP(vp, lbn, &daddr); 357 1.1 perseant bp->b_blkno = daddr; 358 1.1 perseant if (daddr >= 0) { 359 1.7 perseant bp->b_flags |= B_READ; 360 1.7 perseant return VOP_STRATEGY(bp); 361 1.1 perseant } 362 1.1 perseant memset(bp->b_data, 0, bp->b_bcount); 363 1.1 perseant return 0; 364 1.1 perseant } 365 1.1 perseant 366 1.1 perseant /* Move a buffer between dirty and clean block lists. */ 367 1.1 perseant void 368 1.1 perseant reassignbuf(struct ubuf * bp, struct uvnode * vp) 369 1.1 perseant { 370 1.1 perseant LIST_REMOVE(bp, b_vnbufs); 371 1.1 perseant if (bp->b_flags & B_DELWRI) { 372 1.1 perseant LIST_INSERT_HEAD(&vp->v_dirtyblkhd, bp, b_vnbufs); 373 1.1 perseant } else { 374 1.1 perseant LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs); 375 1.1 perseant } 376 1.1 perseant } 377 1.3 perseant 378 1.3 perseant #ifdef DEBUG 379 1.3 perseant void 380 1.3 perseant dump_free_lists(void) 381 1.3 perseant { 382 1.3 perseant struct ubuf *bp; 383 1.3 perseant int i; 384 1.3 perseant 385 1.3 perseant for (i = 0; i <= BQ_LOCKED; i++) { 386 1.3 perseant printf("==> free list %d:\n", i); 387 1.3 perseant TAILQ_FOREACH(bp, &bufqueues[i], b_freelist) { 388 1.3 perseant printf("vp %p lbn %" PRId64 " flags %lx\n", 389 1.3 perseant bp->b_vp, bp->b_lblkno, bp->b_flags); 390 1.3 perseant } 391 1.3 perseant } 392 1.3 perseant } 393 1.3 perseant #endif 394