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