summaryrefslogtreecommitdiffstats
path: root/sbin/dump/cache.c
blob: 906ac27169598a4cfe283ae711985d31aeab5554 (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
/*
 * CACHE.C
 *
 *	Block cache for dump
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/stat.h>
#include <sys/mman.h>

#ifdef sunos
#include <sys/vnode.h>

#include <ufs/fs.h>
#include <ufs/fsdir.h>
#include <ufs/inode.h>
#else
#include <ufs/ufs/dir.h>
#include <ufs/ufs/dinode.h>
#include <ufs/ffs/fs.h>
#endif

#include <protocols/dumprestore.h>

#include <ctype.h>
#include <stdio.h>
#ifdef __STDC__
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#endif
#include "dump.h"

typedef struct Block {
	struct Block	*b_HNext;	/* must be first field */
	off_t		b_Offset;
	char		*b_Data;
} Block;

#define HFACTOR		4
#define BLKFACTOR	4

static char  *DataBase;
static Block **BlockHash;
static int   BlockSize;
static int   HSize;
static int   NBlocks;

static void
cinit(void)
{
	int i;
	int hi;
	Block *base;

	if ((BlockSize = sblock->fs_bsize * BLKFACTOR) > MAXBSIZE)
		BlockSize = MAXBSIZE;
	NBlocks = cachesize / BlockSize;
	HSize = NBlocks / HFACTOR;

	msg("Cache %d MB, blocksize = %d\n", 
	    NBlocks * BlockSize / (1024 * 1024), BlockSize);

	base = calloc(sizeof(Block), NBlocks);
	BlockHash = calloc(sizeof(Block *), HSize);
	DataBase = mmap(NULL, NBlocks * BlockSize, 
			PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
	for (i = 0; i < NBlocks; ++i) {
		base[i].b_Data = DataBase + i * BlockSize;
		base[i].b_Offset = (off_t)-1;
		hi = i / HFACTOR;
		base[i].b_HNext = BlockHash[hi];
		BlockHash[hi] = &base[i];
	}
}

ssize_t
cread(int fd, void *buf, size_t nbytes, off_t offset)
{
	Block *blk;
	Block **pblk;
	Block **ppblk;
	int hi;
	int n;
	off_t mask;

	/*
	 * If the cache is disabled, or we do not yet know the filesystem
	 * block size, then revert to pread.  Otherwise initialize the
	 * cache as necessary and continue.
	 */
	if (cachesize <= 0 || sblock->fs_bsize == 0)
		return(pread(fd, buf, nbytes, offset));
	if (DataBase == NULL)
		cinit();

	/*
	 * If the request crosses a cache block boundary, or the
	 * request is larger or equal to the cache block size,
	 * revert to pread().  Full-block-reads are typically
	 * one-time calls and caching would be detrimental.
	 */
	mask = ~(off_t)(BlockSize - 1);
	if (nbytes >= BlockSize ||
	    ((offset ^ (offset + nbytes - 1)) & mask) != 0) {
		return(pread(fd, buf, nbytes, offset));
	}

	/*
	 * Obtain and access the cache block.  Cache a successful
	 * result.  If an error occurs, revert to pread() (this might
	 * occur near the end of the media).
	 */
	hi = (offset / BlockSize) % HSize;
	pblk = &BlockHash[hi];
	ppblk = NULL;
	while ((blk = *pblk) != NULL) {
		if (((blk->b_Offset ^ offset) & mask) == 0)
			break;
		ppblk = pblk;
		pblk = &blk->b_HNext;
	}
	if (blk == NULL) {
		blk = *ppblk;
		pblk = ppblk;
		blk->b_Offset = offset & mask;
		n = pread(fd, blk->b_Data, BlockSize, blk->b_Offset);
		if (n != BlockSize) {
			blk->b_Offset = (off_t)-1;
			blk = NULL;
		}
	}
	if (blk) {
		bcopy(blk->b_Data + (offset - blk->b_Offset), buf, nbytes);
		*pblk = blk->b_HNext;
		blk->b_HNext = BlockHash[hi];
		BlockHash[hi] = blk;
		return(nbytes);
	} else {
		return(pread(fd, buf, nbytes, offset));
	}
}

OpenPOWER on IntegriCloud