bufcache.c revision 1.13 1 1.13 hannken /* $NetBSD: bufcache.c,v 1.13 2008/05/16 09:21:59 hannken 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.1 perseant #include "vnode.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.6 perseant int i, newhashmax, newhashmask;
105 1.6 perseant struct ubuf *bp, *nbp;
106 1.6 perseant struct bufhash_struct *np;
107 1.6 perseant
108 1.6 perseant 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.6 perseant newhashmask = newhashmax - 1;
115 1.6 perseant
116 1.7 perseant /* Allocate new empty hash table, if we can */
117 1.10 christos np = emalloc(newhashmax * sizeof(*bufhash));
118 1.6 perseant for (i = 0; i < newhashmax; i++)
119 1.6 perseant LIST_INIT(&np[i]);
120 1.6 perseant
121 1.6 perseant /* Now reassign all existing buffers to their new hash chains. */
122 1.6 perseant for (i = 0; i < hashmax; i++) {
123 1.6 perseant bp = LIST_FIRST(&bufhash[i]);
124 1.6 perseant while(bp) {
125 1.6 perseant nbp = LIST_NEXT(bp, b_hash);
126 1.6 perseant LIST_REMOVE(bp, b_hash);
127 1.6 perseant bp->b_hashval = vl_hash(bp->b_vp, bp->b_lblkno);
128 1.6 perseant LIST_INSERT_HEAD(&np[bp->b_hashval], bp, b_hash);
129 1.6 perseant bp = nbp;
130 1.6 perseant }
131 1.6 perseant }
132 1.6 perseant
133 1.6 perseant /* Switch over and clean up */
134 1.6 perseant free(bufhash);
135 1.6 perseant bufhash = np;
136 1.6 perseant hashmax = newhashmax;
137 1.6 perseant hashmask = newhashmask;
138 1.6 perseant }
139 1.6 perseant
140 1.1 perseant /* Print statistics of buffer cache usage */
141 1.1 perseant void
142 1.1 perseant bufstats(void)
143 1.1 perseant {
144 1.6 perseant printf("buffer cache: %d hits %d misses (%2.2f%%); hash width %d, depth %d\n",
145 1.1 perseant cachehits, cachemisses,
146 1.1 perseant (cachehits * 100.0) / (cachehits + cachemisses),
147 1.6 perseant hashmax, max_depth);
148 1.1 perseant }
149 1.1 perseant
150 1.1 perseant /*
151 1.1 perseant * Remove a buffer from the cache.
152 1.1 perseant * Caller must remove the buffer from its free list.
153 1.1 perseant */
154 1.1 perseant void
155 1.1 perseant buf_destroy(struct ubuf * bp)
156 1.1 perseant {
157 1.1 perseant bp->b_flags |= B_NEEDCOMMIT;
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.1 perseant * NB: This makes an assumption about how tailq's are implemented.
178 1.1 perseant */
179 1.2 perseant if (bp->b_flags & B_LOCKED) {
180 1.1 perseant locked_queue_bytes -= bp->b_bcount;
181 1.4 perseant --locked_queue_count;
182 1.2 perseant }
183 1.1 perseant if (TAILQ_NEXT(bp, b_freelist) == NULL) {
184 1.1 perseant for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
185 1.1 perseant if (dp->tqh_last == &bp->b_freelist.tqe_next)
186 1.1 perseant break;
187 1.1 perseant if (dp == &bufqueues[BQUEUES])
188 1.1 perseant errx(1, "bremfree: lost tail");
189 1.1 perseant }
190 1.1 perseant ++bp->b_vp->v_usecount;
191 1.1 perseant TAILQ_REMOVE(dp, bp, b_freelist);
192 1.1 perseant }
193 1.1 perseant
194 1.1 perseant /* Return a buffer if it is in the cache, otherwise return NULL. */
195 1.1 perseant struct ubuf *
196 1.1 perseant incore(struct uvnode * vp, int lbn)
197 1.1 perseant {
198 1.1 perseant struct ubuf *bp;
199 1.1 perseant int hash, depth;
200 1.1 perseant
201 1.1 perseant hash = vl_hash(vp, lbn);
202 1.1 perseant /* XXX use a real hash instead. */
203 1.1 perseant depth = 0;
204 1.1 perseant LIST_FOREACH(bp, &bufhash[hash], b_hash) {
205 1.6 perseant if (++depth > max_depth)
206 1.6 perseant max_depth = depth;
207 1.6 perseant assert(depth <= nbufs);
208 1.1 perseant if (bp->b_vp == vp && bp->b_lblkno == lbn) {
209 1.1 perseant return bp;
210 1.1 perseant }
211 1.1 perseant }
212 1.1 perseant return NULL;
213 1.1 perseant }
214 1.1 perseant
215 1.1 perseant /*
216 1.1 perseant * Return a buffer of the given size, lbn and uvnode.
217 1.1 perseant * If none is in core, make a new one.
218 1.1 perseant */
219 1.1 perseant struct ubuf *
220 1.1 perseant getblk(struct uvnode * vp, daddr_t lbn, int size)
221 1.1 perseant {
222 1.1 perseant struct ubuf *bp;
223 1.3 perseant #ifdef DEBUG
224 1.3 perseant static int warned;
225 1.3 perseant #endif
226 1.1 perseant
227 1.1 perseant /*
228 1.1 perseant * First check the buffer cache lists.
229 1.5 perseant * We might sometimes need to resize a buffer. If we are growing
230 1.5 perseant * the buffer, its contents are invalid; but shrinking is okay.
231 1.1 perseant */
232 1.1 perseant if ((bp = incore(vp, lbn)) != NULL) {
233 1.1 perseant assert(!(bp->b_flags & B_NEEDCOMMIT));
234 1.1 perseant assert(!(bp->b_flags & B_BUSY));
235 1.1 perseant bp->b_flags |= B_BUSY;
236 1.1 perseant bremfree(bp);
237 1.5 perseant if (bp->b_bcount == size)
238 1.5 perseant return bp;
239 1.5 perseant else if (bp->b_bcount > size) {
240 1.5 perseant assert(!(bp->b_flags & B_DELWRI));
241 1.5 perseant bp->b_bcount = size;
242 1.10 christos bp->b_data = erealloc(bp->b_data, size);
243 1.5 perseant return bp;
244 1.5 perseant }
245 1.5 perseant
246 1.5 perseant buf_destroy(bp);
247 1.5 perseant bp = NULL;
248 1.1 perseant }
249 1.5 perseant
250 1.1 perseant /*
251 1.1 perseant * Not on the list.
252 1.1 perseant * Get a new block of the appropriate size and use that.
253 1.1 perseant * If not enough space, free blocks from the AGE and LRU lists
254 1.1 perseant * to make room.
255 1.1 perseant */
256 1.4 perseant while (nbufs >= maxbufs + locked_queue_count) {
257 1.1 perseant bp = TAILQ_FIRST(&bufqueues[BQ_AGE]);
258 1.1 perseant if (bp)
259 1.1 perseant TAILQ_REMOVE(&bufqueues[BQ_AGE], bp, b_freelist);
260 1.1 perseant if (bp == NULL) {
261 1.1 perseant bp = TAILQ_FIRST(&bufqueues[BQ_LRU]);
262 1.1 perseant if (bp)
263 1.3 perseant TAILQ_REMOVE(&bufqueues[BQ_LRU], bp,
264 1.1 perseant b_freelist);
265 1.1 perseant }
266 1.1 perseant if (bp) {
267 1.1 perseant if (bp->b_flags & B_DELWRI)
268 1.1 perseant VOP_STRATEGY(bp);
269 1.1 perseant buf_destroy(bp);
270 1.6 perseant break;
271 1.1 perseant }
272 1.1 perseant #ifdef DEBUG
273 1.7 perseant else if (!warned) {
274 1.7 perseant warnx("allocating more than %d buffers", maxbufs);
275 1.7 perseant ++warned;
276 1.1 perseant }
277 1.1 perseant #endif
278 1.7 perseant break;
279 1.1 perseant }
280 1.1 perseant ++nbufs;
281 1.10 christos bp = ecalloc(1, sizeof(*bp));
282 1.10 christos bp->b_data = ecalloc(1, size);
283 1.1 perseant bp->b_vp = vp;
284 1.1 perseant bp->b_blkno = bp->b_lblkno = lbn;
285 1.1 perseant bp->b_bcount = size;
286 1.6 perseant bp->b_hashval = vl_hash(vp, lbn);
287 1.6 perseant LIST_INSERT_HEAD(&bufhash[bp->b_hashval], bp, b_hash);
288 1.1 perseant LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs);
289 1.1 perseant bp->b_flags = B_BUSY;
290 1.1 perseant
291 1.1 perseant return bp;
292 1.1 perseant }
293 1.1 perseant
294 1.1 perseant /* Write a buffer to disk according to its strategy routine. */
295 1.1 perseant void
296 1.1 perseant bwrite(struct ubuf * bp)
297 1.1 perseant {
298 1.1 perseant bp->b_flags &= ~(B_READ | B_DONE | B_DELWRI | B_LOCKED);
299 1.1 perseant VOP_STRATEGY(bp);
300 1.1 perseant bp->b_flags |= B_DONE;
301 1.1 perseant reassignbuf(bp, bp->b_vp);
302 1.11 ad brelse(bp, 0);
303 1.1 perseant }
304 1.1 perseant
305 1.1 perseant /* Put a buffer back on its free list, clear B_BUSY. */
306 1.1 perseant void
307 1.11 ad brelse(struct ubuf * bp, int set)
308 1.1 perseant {
309 1.1 perseant int age;
310 1.1 perseant
311 1.1 perseant assert(!(bp->b_flags & B_NEEDCOMMIT));
312 1.1 perseant assert(bp->b_flags & B_BUSY);
313 1.1 perseant
314 1.11 ad bp->b_flags |= set;
315 1.11 ad
316 1.1 perseant age = bp->b_flags & B_AGE;
317 1.1 perseant bp->b_flags &= ~(B_BUSY | B_AGE);
318 1.1 perseant if (bp->b_flags & B_INVAL) {
319 1.1 perseant buf_destroy(bp);
320 1.1 perseant return;
321 1.1 perseant }
322 1.1 perseant if (bp->b_flags & B_LOCKED) {
323 1.1 perseant locked_queue_bytes += bp->b_bcount;
324 1.4 perseant ++locked_queue_count;
325 1.1 perseant TAILQ_INSERT_TAIL(&bufqueues[BQ_LOCKED], bp, b_freelist);
326 1.1 perseant } else if (age) {
327 1.1 perseant TAILQ_INSERT_TAIL(&bufqueues[BQ_AGE], bp, b_freelist);
328 1.1 perseant } else {
329 1.1 perseant TAILQ_INSERT_TAIL(&bufqueues[BQ_LRU], bp, b_freelist);
330 1.1 perseant }
331 1.1 perseant --bp->b_vp->v_usecount;
332 1.6 perseant
333 1.6 perseant /* Move to the front of the hash chain */
334 1.6 perseant if (LIST_FIRST(&bufhash[bp->b_hashval]) != bp) {
335 1.6 perseant LIST_REMOVE(bp, b_hash);
336 1.6 perseant LIST_INSERT_HEAD(&bufhash[bp->b_hashval], bp, b_hash);
337 1.6 perseant }
338 1.1 perseant }
339 1.1 perseant
340 1.1 perseant /* Read the given block from disk, return it B_BUSY. */
341 1.1 perseant int
342 1.9 christos bread(struct uvnode * vp, daddr_t lbn, int size, void * unused,
343 1.13 hannken int flags, struct ubuf ** bpp)
344 1.1 perseant {
345 1.1 perseant struct ubuf *bp;
346 1.1 perseant daddr_t daddr;
347 1.7 perseant int error;
348 1.1 perseant
349 1.1 perseant bp = getblk(vp, lbn, size);
350 1.1 perseant *bpp = bp;
351 1.1 perseant if (bp->b_flags & (B_DELWRI | B_DONE)) {
352 1.1 perseant ++cachehits;
353 1.1 perseant return 0;
354 1.1 perseant }
355 1.1 perseant ++cachemisses;
356 1.1 perseant
357 1.1 perseant /*
358 1.1 perseant * Not found. Need to find that block's location on disk,
359 1.1 perseant * and load it in.
360 1.1 perseant */
361 1.1 perseant daddr = -1;
362 1.1 perseant error = VOP_BMAP(vp, lbn, &daddr);
363 1.1 perseant bp->b_blkno = daddr;
364 1.1 perseant if (daddr >= 0) {
365 1.7 perseant bp->b_flags |= B_READ;
366 1.7 perseant return VOP_STRATEGY(bp);
367 1.1 perseant }
368 1.1 perseant memset(bp->b_data, 0, bp->b_bcount);
369 1.1 perseant return 0;
370 1.1 perseant }
371 1.1 perseant
372 1.1 perseant /* Move a buffer between dirty and clean block lists. */
373 1.1 perseant void
374 1.1 perseant reassignbuf(struct ubuf * bp, struct uvnode * vp)
375 1.1 perseant {
376 1.1 perseant LIST_REMOVE(bp, b_vnbufs);
377 1.1 perseant if (bp->b_flags & B_DELWRI) {
378 1.1 perseant LIST_INSERT_HEAD(&vp->v_dirtyblkhd, bp, b_vnbufs);
379 1.1 perseant } else {
380 1.1 perseant LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs);
381 1.1 perseant }
382 1.1 perseant }
383 1.3 perseant
384 1.3 perseant #ifdef DEBUG
385 1.3 perseant void
386 1.3 perseant dump_free_lists(void)
387 1.3 perseant {
388 1.3 perseant struct ubuf *bp;
389 1.3 perseant int i;
390 1.3 perseant
391 1.3 perseant for (i = 0; i <= BQ_LOCKED; i++) {
392 1.3 perseant printf("==> free list %d:\n", i);
393 1.3 perseant TAILQ_FOREACH(bp, &bufqueues[i], b_freelist) {
394 1.3 perseant printf("vp %p lbn %" PRId64 " flags %lx\n",
395 1.3 perseant bp->b_vp, bp->b_lblkno, bp->b_flags);
396 1.3 perseant }
397 1.3 perseant }
398 1.3 perseant }
399 1.3 perseant #endif
400