xref: /netbsd-src/sbin/fsck_lfs/bufcache.c (revision a74e29fe24406e588e4c7ad6d03168a00d009bc1)
1*a74e29feSjoerg /* $NetBSD: bufcache.c,v 1.21 2020/04/03 19:36:33 joerg Exp $ */
2ba10361aSperseant /*-
3ba10361aSperseant  * Copyright (c) 2003 The NetBSD Foundation, Inc.
4ba10361aSperseant  * All rights reserved.
5ba10361aSperseant  *
6ba10361aSperseant  * This code is derived from software contributed to The NetBSD Foundation
7ba10361aSperseant  * by Konrad E. Schroder <perseant@hhhh.org>.
8ba10361aSperseant  *
9ba10361aSperseant  * Redistribution and use in source and binary forms, with or without
10ba10361aSperseant  * modification, are permitted provided that the following conditions
11ba10361aSperseant  * are met:
12ba10361aSperseant  * 1. Redistributions of source code must retain the above copyright
13ba10361aSperseant  *    notice, this list of conditions and the following disclaimer.
14ba10361aSperseant  * 2. Redistributions in binary form must reproduce the above copyright
15ba10361aSperseant  *    notice, this list of conditions and the following disclaimer in the
16ba10361aSperseant  *    documentation and/or other materials provided with the distribution.
17ba10361aSperseant  *
18ba10361aSperseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19ba10361aSperseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20ba10361aSperseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21ba10361aSperseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22ba10361aSperseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23ba10361aSperseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24ba10361aSperseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25ba10361aSperseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26ba10361aSperseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27ba10361aSperseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28ba10361aSperseant  * POSSIBILITY OF SUCH DAMAGE.
29ba10361aSperseant  */
30ba10361aSperseant 
31ba10361aSperseant #include <sys/types.h>
32ba10361aSperseant #include <sys/param.h>
33ba10361aSperseant #include <sys/time.h>
34ba10361aSperseant #include <sys/buf.h>
35ba10361aSperseant #include <sys/queue.h>
36ba10361aSperseant #include <sys/mount.h>
37ba10361aSperseant 
38ba10361aSperseant #include <assert.h>
39ba10361aSperseant #include <err.h>
40ba10361aSperseant #include <stdio.h>
41ba10361aSperseant #include <stdlib.h>
42ba10361aSperseant #include <string.h>
43ba10361aSperseant #include <unistd.h>
44b6479e9fSchristos #include <util.h>
45ba10361aSperseant 
46ba10361aSperseant #include "bufcache.h"
47*a74e29feSjoerg #include "extern.h"
48ba10361aSperseant 
49ba10361aSperseant /*
50ba10361aSperseant  * Definitions for the buffer free lists.
51ba10361aSperseant  */
52ba10361aSperseant #define	BQUEUES		3	/* number of free buffer queues */
53ba10361aSperseant 
54ba10361aSperseant #define	BQ_LOCKED	0	/* super-blocks &c */
55ba10361aSperseant #define	BQ_LRU		1	/* lru, useful buffers */
56ba10361aSperseant #define	BQ_AGE		2	/* rubbish */
57ba10361aSperseant 
58ba10361aSperseant TAILQ_HEAD(bqueues, ubuf) bufqueues[BQUEUES];
59ba10361aSperseant 
601d4cc6a1Sperseant struct bufhash_struct *bufhash;
61ba10361aSperseant 
621d4cc6a1Sperseant #define HASH_MAX 1024
631d4cc6a1Sperseant int hashmax  = HASH_MAX;
641d4cc6a1Sperseant int hashmask = (HASH_MAX - 1);
65ba10361aSperseant 
66ba10361aSperseant int maxbufs = BUF_CACHE_SIZE;
67ba10361aSperseant int nbufs = 0;
68ba10361aSperseant int cachehits = 0;
69ba10361aSperseant int cachemisses = 0;
701d4cc6a1Sperseant int max_depth = 0;
71ba10361aSperseant off_t locked_queue_bytes = 0;
729740d8d0Sperseant int locked_queue_count = 0;
73ba10361aSperseant 
74ba10361aSperseant /* Simple buffer hash function */
75ba10361aSperseant static int
vl_hash(struct uvnode * vp,daddr_t lbn)76ba10361aSperseant vl_hash(struct uvnode * vp, daddr_t lbn)
77ba10361aSperseant {
781d4cc6a1Sperseant 	return (int)((unsigned long) vp + lbn) & hashmask;
79ba10361aSperseant }
80ba10361aSperseant 
81ba10361aSperseant /* Initialize buffer cache */
82ba10361aSperseant void
bufinit(int max)831d4cc6a1Sperseant bufinit(int max)
84ba10361aSperseant {
85ba10361aSperseant 	int i;
86ba10361aSperseant 
871d4cc6a1Sperseant 	if (max) {
881d4cc6a1Sperseant 		for (hashmax = 1; hashmax < max; hashmax <<= 1)
891d4cc6a1Sperseant 			;
901d4cc6a1Sperseant 		hashmask = hashmax - 1;
911d4cc6a1Sperseant 	}
921d4cc6a1Sperseant 
93ba10361aSperseant 	for (i = 0; i < BQUEUES; i++) {
94ba10361aSperseant 		TAILQ_INIT(&bufqueues[i]);
95ba10361aSperseant 	}
96b6479e9fSchristos 	bufhash = emalloc(hashmax * sizeof(*bufhash));
971d4cc6a1Sperseant 	for (i = 0; i < hashmax; i++)
989740d8d0Sperseant 		LIST_INIT(&bufhash[i]);
99ba10361aSperseant }
100ba10361aSperseant 
1011d4cc6a1Sperseant /* Widen the hash table. */
bufrehash(int max)1021d4cc6a1Sperseant void bufrehash(int max)
1031d4cc6a1Sperseant {
104ed2ef35eSdholland 	int i, newhashmax;
1051d4cc6a1Sperseant 	struct ubuf *bp, *nbp;
1061d4cc6a1Sperseant 	struct bufhash_struct *np;
1071d4cc6a1Sperseant 
108ed2ef35eSdholland 	if (max < 0 || max <= hashmax)
1091d4cc6a1Sperseant 		return;
1101d4cc6a1Sperseant 
1111d4cc6a1Sperseant 	/* Round up to a power of two */
1121d4cc6a1Sperseant 	for (newhashmax = 1; newhashmax < max; newhashmax <<= 1)
1131d4cc6a1Sperseant 		;
114ed2ef35eSdholland 
115ed2ef35eSdholland 	/* update the mask right away so vl_hash() uses it */
116ed2ef35eSdholland 	hashmask = newhashmax - 1;
1171d4cc6a1Sperseant 
1189bf7a991Sperseant 	/* Allocate new empty hash table, if we can */
119b6479e9fSchristos 	np = emalloc(newhashmax * sizeof(*bufhash));
1201d4cc6a1Sperseant 	for (i = 0; i < newhashmax; i++)
1211d4cc6a1Sperseant 		LIST_INIT(&np[i]);
1221d4cc6a1Sperseant 
1231d4cc6a1Sperseant 	/* Now reassign all existing buffers to their new hash chains. */
1241d4cc6a1Sperseant 	for (i = 0; i < hashmax; i++) {
1251d4cc6a1Sperseant 		bp = LIST_FIRST(&bufhash[i]);
1261d4cc6a1Sperseant 		while(bp) {
1271d4cc6a1Sperseant 			nbp = LIST_NEXT(bp, b_hash);
1281d4cc6a1Sperseant 			LIST_REMOVE(bp, b_hash);
1291d4cc6a1Sperseant 			bp->b_hashval = vl_hash(bp->b_vp, bp->b_lblkno);
1301d4cc6a1Sperseant 			LIST_INSERT_HEAD(&np[bp->b_hashval], bp, b_hash);
1311d4cc6a1Sperseant 			bp = nbp;
1321d4cc6a1Sperseant 		}
1331d4cc6a1Sperseant 	}
1341d4cc6a1Sperseant 
1351d4cc6a1Sperseant 	/* Switch over and clean up */
1361d4cc6a1Sperseant 	free(bufhash);
1371d4cc6a1Sperseant 	bufhash = np;
1381d4cc6a1Sperseant 	hashmax = newhashmax;
1391d4cc6a1Sperseant }
1401d4cc6a1Sperseant 
141ba10361aSperseant /* Print statistics of buffer cache usage */
142ba10361aSperseant void
bufstats(void)143ba10361aSperseant bufstats(void)
144ba10361aSperseant {
1451d4cc6a1Sperseant 	printf("buffer cache: %d hits %d misses (%2.2f%%); hash width %d, depth %d\n",
146ba10361aSperseant 	    cachehits, cachemisses,
147ba10361aSperseant 	    (cachehits * 100.0) / (cachehits + cachemisses),
1481d4cc6a1Sperseant 	    hashmax, max_depth);
149ba10361aSperseant }
150ba10361aSperseant 
151ba10361aSperseant /*
152ba10361aSperseant  * Remove a buffer from the cache.
153ba10361aSperseant  * Caller must remove the buffer from its free list.
154ba10361aSperseant  */
155ba10361aSperseant void
buf_destroy(struct ubuf * bp)156ba10361aSperseant buf_destroy(struct ubuf * bp)
157ba10361aSperseant {
158ba10361aSperseant 	LIST_REMOVE(bp, b_vnbufs);
159ba10361aSperseant 	LIST_REMOVE(bp, b_hash);
1609bf7a991Sperseant 	if (!(bp->b_flags & B_DONTFREE))
161ba10361aSperseant 		free(bp->b_data);
162ba10361aSperseant 	free(bp);
163ba10361aSperseant 	--nbufs;
164ba10361aSperseant }
165ba10361aSperseant 
166ba10361aSperseant /* Remove a buffer from its free list. */
167ba10361aSperseant void
bremfree(struct ubuf * bp)168ba10361aSperseant bremfree(struct ubuf * bp)
169ba10361aSperseant {
170ba10361aSperseant 	struct bqueues *dp = NULL;
171ba10361aSperseant 
172ba10361aSperseant 	/*
173ba10361aSperseant 	 * We only calculate the head of the freelist when removing
174ba10361aSperseant 	 * the last element of the list as that is the only time that
175ba10361aSperseant 	 * it is needed (e.g. to reset the tail pointer).
176ba10361aSperseant 	 */
177acddf8ffSperseant 	if (bp->b_flags & B_LOCKED) {
178ba10361aSperseant 		locked_queue_bytes -= bp->b_bcount;
1799740d8d0Sperseant 		--locked_queue_count;
180acddf8ffSperseant 	}
181ba10361aSperseant 	if (TAILQ_NEXT(bp, b_freelist) == NULL) {
182ba10361aSperseant 		for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
18399accdc7Schristos 			if (TAILQ_LAST(dp, bqueues) == bp)
184ba10361aSperseant 				break;
185ba10361aSperseant 		if (dp == &bufqueues[BQUEUES])
186ba10361aSperseant 			errx(1, "bremfree: lost tail");
187ba10361aSperseant 	}
188ba10361aSperseant 	++bp->b_vp->v_usecount;
189ba10361aSperseant 	TAILQ_REMOVE(dp, bp, b_freelist);
190ba10361aSperseant }
191ba10361aSperseant 
192ba10361aSperseant /* Return a buffer if it is in the cache, otherwise return NULL. */
193ba10361aSperseant struct ubuf *
incore(struct uvnode * vp,daddr_t lbn)194373dcdfaSchristos incore(struct uvnode * vp, daddr_t lbn)
195ba10361aSperseant {
196ba10361aSperseant 	struct ubuf *bp;
197ba10361aSperseant 	int hash, depth;
198ba10361aSperseant 
199ba10361aSperseant 	hash = vl_hash(vp, lbn);
200ba10361aSperseant 	/* XXX use a real hash instead. */
201ba10361aSperseant 	depth = 0;
202ba10361aSperseant 	LIST_FOREACH(bp, &bufhash[hash], b_hash) {
2031d4cc6a1Sperseant 		if (++depth > max_depth)
2041d4cc6a1Sperseant 			max_depth = depth;
2051d4cc6a1Sperseant 		assert(depth <= nbufs);
206ba10361aSperseant 		if (bp->b_vp == vp && bp->b_lblkno == lbn) {
207ba10361aSperseant 			return bp;
208ba10361aSperseant 		}
209ba10361aSperseant 	}
210ba10361aSperseant 	return NULL;
211ba10361aSperseant }
212ba10361aSperseant 
213ba10361aSperseant /*
214ba10361aSperseant  * Return a buffer of the given size, lbn and uvnode.
215ba10361aSperseant  * If none is in core, make a new one.
216ba10361aSperseant  */
217ba10361aSperseant struct ubuf *
getblk(struct uvnode * vp,daddr_t lbn,int size)218ba10361aSperseant getblk(struct uvnode * vp, daddr_t lbn, int size)
219ba10361aSperseant {
220ba10361aSperseant 	struct ubuf *bp;
2215d2f3e49Sperseant #ifdef DEBUG
2225d2f3e49Sperseant 	static int warned;
2235d2f3e49Sperseant #endif
224ba10361aSperseant 
225ba10361aSperseant 	/*
226ba10361aSperseant 	 * First check the buffer cache lists.
227aafc65baSperseant 	 * We might sometimes need to resize a buffer.  If we are growing
228aafc65baSperseant 	 * the buffer, its contents are invalid; but shrinking is okay.
229ba10361aSperseant 	 */
230ba10361aSperseant 	if ((bp = incore(vp, lbn)) != NULL) {
231ba10361aSperseant 		assert(!(bp->b_flags & B_BUSY));
232ba10361aSperseant 		bp->b_flags |= B_BUSY;
233ba10361aSperseant 		bremfree(bp);
234aafc65baSperseant 		if (bp->b_bcount == size)
235aafc65baSperseant 			return bp;
236aafc65baSperseant 		else if (bp->b_bcount > size) {
237aafc65baSperseant 			assert(!(bp->b_flags & B_DELWRI));
238aafc65baSperseant 			bp->b_bcount = size;
239b6479e9fSchristos 			bp->b_data = erealloc(bp->b_data, size);
240ba10361aSperseant 			return bp;
241ba10361aSperseant 		}
242aafc65baSperseant 
243aafc65baSperseant 		buf_destroy(bp);
244aafc65baSperseant 		bp = NULL;
245aafc65baSperseant 	}
246aafc65baSperseant 
247ba10361aSperseant 	/*
248ba10361aSperseant 	 * Not on the list.
249ba10361aSperseant 	 * Get a new block of the appropriate size and use that.
250ba10361aSperseant 	 * If not enough space, free blocks from the AGE and LRU lists
251ba10361aSperseant 	 * to make room.
252ba10361aSperseant 	 */
2539740d8d0Sperseant 	while (nbufs >= maxbufs + locked_queue_count) {
254ba10361aSperseant 		bp = TAILQ_FIRST(&bufqueues[BQ_AGE]);
255ba10361aSperseant 		if (bp)
256ba10361aSperseant 			TAILQ_REMOVE(&bufqueues[BQ_AGE], bp, b_freelist);
257ba10361aSperseant 		if (bp == NULL) {
258ba10361aSperseant 			bp = TAILQ_FIRST(&bufqueues[BQ_LRU]);
259ba10361aSperseant 			if (bp)
2605d2f3e49Sperseant 				TAILQ_REMOVE(&bufqueues[BQ_LRU], bp,
261ba10361aSperseant 				    b_freelist);
262ba10361aSperseant 		}
263ba10361aSperseant 		if (bp) {
264ba10361aSperseant 			if (bp->b_flags & B_DELWRI)
265ba10361aSperseant 				VOP_STRATEGY(bp);
266ba10361aSperseant 			buf_destroy(bp);
2671d4cc6a1Sperseant 			break;
268ba10361aSperseant 		}
269ba10361aSperseant #ifdef DEBUG
2709bf7a991Sperseant 		else if (!warned) {
2719bf7a991Sperseant 			warnx("allocating more than %d buffers", maxbufs);
2725d2f3e49Sperseant 			++warned;
2739740d8d0Sperseant 		}
274ba10361aSperseant #endif
2759bf7a991Sperseant 		break;
276ba10361aSperseant 	}
277ba10361aSperseant 	++nbufs;
278b6479e9fSchristos 	bp = ecalloc(1, sizeof(*bp));
279b6479e9fSchristos 	bp->b_data = ecalloc(1, size);
280ba10361aSperseant 	bp->b_vp = vp;
281ba10361aSperseant 	bp->b_blkno = bp->b_lblkno = lbn;
282ba10361aSperseant 	bp->b_bcount = size;
2831d4cc6a1Sperseant 	bp->b_hashval = vl_hash(vp, lbn);
2841d4cc6a1Sperseant 	LIST_INSERT_HEAD(&bufhash[bp->b_hashval], bp, b_hash);
285ba10361aSperseant 	LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs);
286ba10361aSperseant 	bp->b_flags = B_BUSY;
287ba10361aSperseant 
288ba10361aSperseant 	return bp;
289ba10361aSperseant }
290ba10361aSperseant 
291ba10361aSperseant /* Write a buffer to disk according to its strategy routine. */
292ba10361aSperseant void
bwrite(struct ubuf * bp)293ba10361aSperseant bwrite(struct ubuf * bp)
294ba10361aSperseant {
295ba10361aSperseant 	bp->b_flags &= ~(B_READ | B_DONE | B_DELWRI | B_LOCKED);
296ba10361aSperseant 	VOP_STRATEGY(bp);
297ba10361aSperseant 	bp->b_flags |= B_DONE;
298ba10361aSperseant 	reassignbuf(bp, bp->b_vp);
299fe44973fSad 	brelse(bp, 0);
300ba10361aSperseant }
301ba10361aSperseant 
302ba10361aSperseant /* Put a buffer back on its free list, clear B_BUSY. */
303ba10361aSperseant void
brelse(struct ubuf * bp,int set)304fe44973fSad brelse(struct ubuf * bp, int set)
305ba10361aSperseant {
306ba10361aSperseant 	int age;
307ba10361aSperseant 
308ba10361aSperseant 	assert(bp->b_flags & B_BUSY);
309ba10361aSperseant 
310fe44973fSad 	bp->b_flags |= set;
311fe44973fSad 
312ba10361aSperseant 	age = bp->b_flags & B_AGE;
313ba10361aSperseant 	bp->b_flags &= ~(B_BUSY | B_AGE);
314ba10361aSperseant 	if (bp->b_flags & B_INVAL) {
315ba10361aSperseant 		buf_destroy(bp);
316ba10361aSperseant 		return;
317ba10361aSperseant 	}
318ba10361aSperseant 	if (bp->b_flags & B_LOCKED) {
319ba10361aSperseant 		locked_queue_bytes += bp->b_bcount;
3209740d8d0Sperseant 		++locked_queue_count;
321ba10361aSperseant 		TAILQ_INSERT_TAIL(&bufqueues[BQ_LOCKED], bp, b_freelist);
322ba10361aSperseant 	} else if (age) {
323ba10361aSperseant 		TAILQ_INSERT_TAIL(&bufqueues[BQ_AGE], bp, b_freelist);
324ba10361aSperseant 	} else {
325ba10361aSperseant 		TAILQ_INSERT_TAIL(&bufqueues[BQ_LRU], bp, b_freelist);
326ba10361aSperseant 	}
327ba10361aSperseant 	--bp->b_vp->v_usecount;
3281d4cc6a1Sperseant 
3291d4cc6a1Sperseant 	/* Move to the front of the hash chain */
3301d4cc6a1Sperseant 	if (LIST_FIRST(&bufhash[bp->b_hashval]) != bp) {
3311d4cc6a1Sperseant 		LIST_REMOVE(bp, b_hash);
3321d4cc6a1Sperseant 		LIST_INSERT_HEAD(&bufhash[bp->b_hashval], bp, b_hash);
3331d4cc6a1Sperseant 	}
334ba10361aSperseant }
335ba10361aSperseant 
336ba10361aSperseant /* Read the given block from disk, return it B_BUSY. */
337ba10361aSperseant int
bread(struct uvnode * vp,daddr_t lbn,int size,int flags,struct ubuf ** bpp)338f89312b9Schopps bread(struct uvnode * vp, daddr_t lbn, int size, int flags, struct ubuf ** bpp)
339ba10361aSperseant {
340ba10361aSperseant 	struct ubuf *bp;
341ba10361aSperseant 	daddr_t daddr;
342ba10361aSperseant 
343ba10361aSperseant 	bp = getblk(vp, lbn, size);
344ba10361aSperseant 	*bpp = bp;
345ba10361aSperseant 	if (bp->b_flags & (B_DELWRI | B_DONE)) {
346ba10361aSperseant 		++cachehits;
347ba10361aSperseant 		return 0;
348ba10361aSperseant 	}
349ba10361aSperseant 	++cachemisses;
350ba10361aSperseant 
351ba10361aSperseant 	/*
352ba10361aSperseant 	 * Not found.  Need to find that block's location on disk,
353ba10361aSperseant 	 * and load it in.
354ba10361aSperseant 	 */
355ba10361aSperseant 	daddr = -1;
3563e4993b3Schristos 	(void)VOP_BMAP(vp, lbn, &daddr);
357ba10361aSperseant 	bp->b_blkno = daddr;
358ba10361aSperseant 	if (daddr >= 0) {
3599bf7a991Sperseant 		bp->b_flags |= B_READ;
3609bf7a991Sperseant 		return VOP_STRATEGY(bp);
361ba10361aSperseant 	}
362ba10361aSperseant 	memset(bp->b_data, 0, bp->b_bcount);
363ba10361aSperseant 	return 0;
364ba10361aSperseant }
365ba10361aSperseant 
366ba10361aSperseant /* Move a buffer between dirty and clean block lists. */
367ba10361aSperseant void
reassignbuf(struct ubuf * bp,struct uvnode * vp)368ba10361aSperseant reassignbuf(struct ubuf * bp, struct uvnode * vp)
369ba10361aSperseant {
370ba10361aSperseant 	LIST_REMOVE(bp, b_vnbufs);
371ba10361aSperseant 	if (bp->b_flags & B_DELWRI) {
372ba10361aSperseant 		LIST_INSERT_HEAD(&vp->v_dirtyblkhd, bp, b_vnbufs);
373ba10361aSperseant 	} else {
374ba10361aSperseant 		LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs);
375ba10361aSperseant 	}
376ba10361aSperseant }
3775d2f3e49Sperseant 
3785d2f3e49Sperseant #ifdef DEBUG
3795d2f3e49Sperseant void
dump_free_lists(void)3805d2f3e49Sperseant dump_free_lists(void)
3815d2f3e49Sperseant {
3825d2f3e49Sperseant 	struct ubuf *bp;
3835d2f3e49Sperseant 	int i;
3845d2f3e49Sperseant 
3855d2f3e49Sperseant 	for (i = 0; i <= BQ_LOCKED; i++) {
3865d2f3e49Sperseant 		printf("==> free list %d:\n", i);
3875d2f3e49Sperseant 		TAILQ_FOREACH(bp, &bufqueues[i], b_freelist) {
3885d2f3e49Sperseant 			printf("vp %p lbn %" PRId64 " flags %lx\n",
3895d2f3e49Sperseant 				bp->b_vp, bp->b_lblkno, bp->b_flags);
3905d2f3e49Sperseant 		}
3915d2f3e49Sperseant 	}
3925d2f3e49Sperseant }
3935d2f3e49Sperseant #endif
394