summaryrefslogtreecommitdiff
path: root/sbin/fsck_lfs/bufcache.c
blob: 2a614d466869a30f47f667408c4f007ed8d78d04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* $NetBSD: bufcache.c,v 1.21 2020/04/03 19:36:33 joerg Exp $ */
/*-
 * Copyright (c) 2003 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Konrad E. Schroder <perseant@hhhh.org>.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/buf.h>
#include <sys/queue.h>
#include <sys/mount.h>

#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>

#include "bufcache.h"
#include "extern.h"

/*
 * Definitions for the buffer free lists.
 */
#define	BQUEUES		3	/* number of free buffer queues */

#define	BQ_LOCKED	0	/* super-blocks &c */
#define	BQ_LRU		1	/* lru, useful buffers */
#define	BQ_AGE		2	/* rubbish */

TAILQ_HEAD(bqueues, ubuf) bufqueues[BQUEUES];

struct bufhash_struct *bufhash;

#define HASH_MAX 1024
int hashmax  = HASH_MAX;
int hashmask = (HASH_MAX - 1);

int maxbufs = BUF_CACHE_SIZE;
int nbufs = 0;
int cachehits = 0;
int cachemisses = 0;
int max_depth = 0;
off_t locked_queue_bytes = 0;
int locked_queue_count = 0;

/* Simple buffer hash function */
static int
vl_hash(struct uvnode * vp, daddr_t lbn)
{
	return (int)((unsigned long) vp + lbn) & hashmask;
}

/* Initialize buffer cache */
void
bufinit(int max)
{
	int i;

	if (max) {
		for (hashmax = 1; hashmax < max; hashmax <<= 1)
			;
		hashmask = hashmax - 1;
	}

	for (i = 0; i < BQUEUES; i++) {
		TAILQ_INIT(&bufqueues[i]);
	}
	bufhash = emalloc(hashmax * sizeof(*bufhash));
	for (i = 0; i < hashmax; i++)
		LIST_INIT(&bufhash[i]);
}

/* Widen the hash table. */
void bufrehash(int max)
{
	int i, newhashmax;
	struct ubuf *bp, *nbp;
	struct bufhash_struct *np;

	if (max < 0 || max <= hashmax)
		return;

	/* Round up to a power of two */
	for (newhashmax = 1; newhashmax < max; newhashmax <<= 1)
		;

	/* update the mask right away so vl_hash() uses it */
	hashmask = newhashmax - 1;

	/* Allocate new empty hash table, if we can */
	np = emalloc(newhashmax * sizeof(*bufhash));
	for (i = 0; i < newhashmax; i++)
		LIST_INIT(&np[i]);

	/* Now reassign all existing buffers to their new hash chains. */
	for (i = 0; i < hashmax; i++) {
		bp = LIST_FIRST(&bufhash[i]);
		while(bp) {
			nbp = LIST_NEXT(bp, b_hash);
			LIST_REMOVE(bp, b_hash);
			bp->b_hashval = vl_hash(bp->b_vp, bp->b_lblkno);
			LIST_INSERT_HEAD(&np[bp->b_hashval], bp, b_hash);
			bp = nbp;
		}
	}

	/* Switch over and clean up */
	free(bufhash);
	bufhash = np;
	hashmax = newhashmax;
}

/* Print statistics of buffer cache usage */
void
bufstats(void)
{
	printf("buffer cache: %d hits %d misses (%2.2f%%); hash width %d, depth %d\n",
	    cachehits, cachemisses,
	    (cachehits * 100.0) / (cachehits + cachemisses),
	    hashmax, max_depth);
}

/*
 * Remove a buffer from the cache.
 * Caller must remove the buffer from its free list.
 */
void
buf_destroy(struct ubuf * bp)
{
	LIST_REMOVE(bp, b_vnbufs);
	LIST_REMOVE(bp, b_hash);
	if (!(bp->b_flags & B_DONTFREE))
		free(bp->b_data);
	free(bp);
	--nbufs;
}

/* Remove a buffer from its free list. */
void
bremfree(struct ubuf * bp)
{
	struct bqueues *dp = NULL;

	/*
	 * We only calculate the head of the freelist when removing
	 * the last element of the list as that is the only time that
	 * it is needed (e.g. to reset the tail pointer).
	 */
	if (bp->b_flags & B_LOCKED) {
		locked_queue_bytes -= bp->b_bcount;
		--locked_queue_count;
	}
	if (TAILQ_NEXT(bp, b_freelist) == NULL) {
		for (dp = bufqueues; dp < &bufqueues[BQUEUES]; dp++)
			if (TAILQ_LAST(dp, bqueues) == bp)
				break;
		if (dp == &bufqueues[BQUEUES])
			errx(1, "bremfree: lost tail");
	}
	++bp->b_vp->v_usecount;
	TAILQ_REMOVE(dp, bp, b_freelist);
}

/* Return a buffer if it is in the cache, otherwise return NULL. */
struct ubuf *
incore(struct uvnode * vp, daddr_t lbn)
{
	struct ubuf *bp;
	int hash, depth;

	hash = vl_hash(vp, lbn);
	/* XXX use a real hash instead. */
	depth = 0;
	LIST_FOREACH(bp, &bufhash[hash], b_hash) {
		if (++depth > max_depth)
			max_depth = depth;
		assert(depth <= nbufs);
		if (bp->b_vp == vp && bp->b_lblkno == lbn) {
			return bp;
		}
	}
	return NULL;
}

/*
 * Return a buffer of the given size, lbn and uvnode.
 * If none is in core, make a new one.
 */
struct ubuf *
getblk(struct uvnode * vp, daddr_t lbn, int size)
{
	struct ubuf *bp;
#ifdef DEBUG
	static int warned;
#endif

	/*
	 * First check the buffer cache lists.
	 * We might sometimes need to resize a buffer.  If we are growing
	 * the buffer, its contents are invalid; but shrinking is okay.
	 */
	if ((bp = incore(vp, lbn)) != NULL) {
		assert(!(bp->b_flags & B_BUSY));
		bp->b_flags |= B_BUSY;
		bremfree(bp);
		if (bp->b_bcount == size)
			return bp;
		else if (bp->b_bcount > size) {
			assert(!(bp->b_flags & B_DELWRI));
			bp->b_bcount = size;
			bp->b_data = erealloc(bp->b_data, size);
			return bp;
		}

		buf_destroy(bp);
		bp = NULL;
	}

	/*
	 * Not on the list.
	 * Get a new block of the appropriate size and use that.
	 * If not enough space, free blocks from the AGE and LRU lists
	 * to make room.
	 */
	while (nbufs >= maxbufs + locked_queue_count) {
		bp = TAILQ_FIRST(&bufqueues[BQ_AGE]);
		if (bp)
			TAILQ_REMOVE(&bufqueues[BQ_AGE], bp, b_freelist);
		if (bp == NULL) {
			bp = TAILQ_FIRST(&bufqueues[BQ_LRU]);
			if (bp)
				TAILQ_REMOVE(&bufqueues[BQ_LRU], bp,
				    b_freelist);
		}
		if (bp) {
			if (bp->b_flags & B_DELWRI)
				VOP_STRATEGY(bp);
			buf_destroy(bp);
			break;
		}
#ifdef DEBUG
		else if (!warned) {
			warnx("allocating more than %d buffers", maxbufs);
			++warned;
		}
#endif
		break;
	}
	++nbufs;
	bp = ecalloc(1, sizeof(*bp));
	bp->b_data = ecalloc(1, size);
	bp->b_vp = vp;
	bp->b_blkno = bp->b_lblkno = lbn;
	bp->b_bcount = size;
	bp->b_hashval = vl_hash(vp, lbn);
	LIST_INSERT_HEAD(&bufhash[bp->b_hashval], bp, b_hash);
	LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs);
	bp->b_flags = B_BUSY;

	return bp;
}

/* Write a buffer to disk according to its strategy routine. */
void
bwrite(struct ubuf * bp)
{
	bp->b_flags &= ~(B_READ | B_DONE | B_DELWRI | B_LOCKED);
	VOP_STRATEGY(bp);
	bp->b_flags |= B_DONE;
	reassignbuf(bp, bp->b_vp);
	brelse(bp, 0);
}

/* Put a buffer back on its free list, clear B_BUSY. */
void
brelse(struct ubuf * bp, int set)
{
	int age;

	assert(bp->b_flags & B_BUSY);

	bp->b_flags |= set;

	age = bp->b_flags & B_AGE;
	bp->b_flags &= ~(B_BUSY | B_AGE);
	if (bp->b_flags & B_INVAL) {
		buf_destroy(bp);
		return;
	}
	if (bp->b_flags & B_LOCKED) {
		locked_queue_bytes += bp->b_bcount;
		++locked_queue_count;
		TAILQ_INSERT_TAIL(&bufqueues[BQ_LOCKED], bp, b_freelist);
	} else if (age) {
		TAILQ_INSERT_TAIL(&bufqueues[BQ_AGE], bp, b_freelist);
	} else {
		TAILQ_INSERT_TAIL(&bufqueues[BQ_LRU], bp, b_freelist);
	}
	--bp->b_vp->v_usecount;

	/* Move to the front of the hash chain */
	if (LIST_FIRST(&bufhash[bp->b_hashval]) != bp) {
		LIST_REMOVE(bp, b_hash);
		LIST_INSERT_HEAD(&bufhash[bp->b_hashval], bp, b_hash);
	}
}

/* Read the given block from disk, return it B_BUSY. */
int
bread(struct uvnode * vp, daddr_t lbn, int size, int flags, struct ubuf ** bpp)
{
	struct ubuf *bp;
	daddr_t daddr;

	bp = getblk(vp, lbn, size);
	*bpp = bp;
	if (bp->b_flags & (B_DELWRI | B_DONE)) {
		++cachehits;
		return 0;
	}
	++cachemisses;

	/*
	 * Not found.  Need to find that block's location on disk,
	 * and load it in.
	 */
	daddr = -1;
	(void)VOP_BMAP(vp, lbn, &daddr);
	bp->b_blkno = daddr;
	if (daddr >= 0) {
		bp->b_flags |= B_READ;
		return VOP_STRATEGY(bp);
	}
	memset(bp->b_data, 0, bp->b_bcount);
	return 0;
}

/* Move a buffer between dirty and clean block lists. */
void
reassignbuf(struct ubuf * bp, struct uvnode * vp)
{
	LIST_REMOVE(bp, b_vnbufs);
	if (bp->b_flags & B_DELWRI) {
		LIST_INSERT_HEAD(&vp->v_dirtyblkhd, bp, b_vnbufs);
	} else {
		LIST_INSERT_HEAD(&vp->v_cleanblkhd, bp, b_vnbufs);
	}
}

#ifdef DEBUG
void
dump_free_lists(void)
{
	struct ubuf *bp;
	int i;

	for (i = 0; i <= BQ_LOCKED; i++) {
		printf("==> free list %d:\n", i);
		TAILQ_FOREACH(bp, &bufqueues[i], b_freelist) {
			printf("vp %p lbn %" PRId64 " flags %lx\n",
				bp->b_vp, bp->b_lblkno, bp->b_flags);
		}
	}
}
#endif