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