~kris/9p

9hist

ref: 4d55f1cdece7317e730b9e8fdf816e0cf9cbeb5a 9hist/port/cache.c -rw-r--r-- 3.6 KiB
4d55f1cd — David du Colombier Plan 9 from Bell Labs 1993-10-13 32 years ago
                                                                                
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
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"
#include	"devtab.h"

Image	fscache;

typedef Extent Extent;
struct Extent
{
	int	bid;
	ulong	start;
	int	len;
	Extent	*next;
};

typedef struct Mntcache Mntcache;
struct Mntcache
{
	Qid;
	int	dev;
	int	type;
	Qlock;
	Extent	 *list;
	Mntcache *hash;
	Mntcache *prev;
	Mntcache *next;
};

typedef struct Cache Cache;
struct Cache
{
	Ref;
	Mntcache	*head;
	Mntcache	*tail;
	Mntcache	*hash[NHASH];
};
Cache cache;

Mntcache*
clook(Chan *c)
{
	int h;

	h = c->qid.path%NHASH;

	lock(&cache);
	for(m = cache.hash[h]; m; m = m->hash) {
		if(m->path == c->path) {
			qlock(m);
			if(m->path == c->qid.path)
			if(m->dev  == c->dev)
			if(m->type == c->type) {
				unlock(&cache);
				return m;
			}
			qunlock(m);
		}
	}
	unlock(&cache);
	return 0;
}

void
cnodata(Mntcache *m)
{
	Extent *e, *n;

	/*
	 * Invalidate all extent data
	 * Image lru will waste the pages
	 */
	for(e = m->list; e; e = n) {
		n = e->list;
		free(e);
	}
}

void
ctail(Mntcache *m)
{
	lock(&cache);

	/* Unlink and send to the tail */
	if(m->prev) 
		m->prev->next = m->next;
	else
		cache.head = m->next;
	if(m->next)
		m->next->prev = m->prev;
	else
		cache.tail = m->prev;

	if(cache.tail) {
		m->prev = cache.tail;
		cache.tail->next = m;
		m->next = 0;
		cache.tail = m;
	}
	else {
		cache.head = cache.tail = m;
		m->prev = m->next = 0;
	}

	unlock(&cache);
}

void
copen(Chan *c)
{
	Mntcache *m, *f, **l;

	m = clook(c);
	if(m != 0) {
		/* File was updated */
		if(m->vers != c->vers)
			cnodata(m);

		ctail(m);
		qunlock(m);
		return;
	}

	/* LRU the cache headers */
	m = cache.head;
	qlock(m);
	lock(cache);
	l = &cache.hash[m->qid.path%NHASH];
	for(f = *l; f; f = f->next) {
		if(f == m) {
			*l = f->next;
			break;
		}
		l = &f->next;
	}
	l = &cache.hash[c->qid.path%NHASH];
	m->hash = *l;
	*l = m;
	unlock(cache);
	m->qid = c->qid;
	m->dev = c->dev;
	m->type = c->type;
	cnodata(m);
	ctail(m);
	c->mcp = m;
	qunlock(m);
}

static int
cdev(Mntcache *m, Chan *c)
{
	if(m->path != c->path)
		return 0;
	if(m->vers != c->vers)
		return 0;
	if(m->dev != c->dev)
		return 0;
	if(m->type != c->type)
		return 0;
	return 1;
}

int
cread(Chan *c, uchar *buf, int len, long offset)
{
	KMap *k;
	Page *p;
	Mntcache *m;
	Extent *e, **l;
	int o, l, total;

	m = c->mcp;
	if(m == 0)
		return 0;
	qlock(m);
	if(cdev(m, c) == 0) {
		qunlock(m);
		return 0;
	}

	end = offset+len;
	l = &m->list;
	for(e = *l; e; e = e->next) {
		if(e->start >= offset && e->start+e->len < end)
			break;
		l = &e->next;
	}

	if(e == 0) {
		qunlock(m);
		return 0;
	}

	total = 0;
	while(len) {
		p = lookpage(&fscache, e->bid);
		if(p == 0) {
			*l = e->next;
			free(e);
			qunlock(m);
			return total;
		}

		k = kmap(p);
		if(waserror()) {
			kunmap(k);
			putpage(p);
			qunlock(m);
			nexterror();
		}

		o = offset - e->start;
		l = len;
		if(l > e->len-o)
			l = e->len-o;

		p = (uchar*)VA(k) + o;
		memset(buf, p, l);
		kunmap(k);
		putpage(p);

		buf += l;
		len -= l;
		offset += l;
		total += l;
		l = &e->next;
		e = e->next;
		if(e == 0 || e->start != offset)
			break;
	}
	qunlock(m)
	return total;
}

void
cwrite(Chan *c, uchar *buf, int len, long offset)
{
	Extent *e;
	Mntcache *m;

	if(offset > MAXCACHE)
		return;

	m = c->mcp;
	if(m == 0)
		return;
	qlock(m);
	if(cdev(m, c) == 0) {
		qunlock(m);
		return;
	}

	if(m->list == 0) {
		e = malloc(sizeof(Extent));
		if(e == 0)
			return;
		e->start = offset;
		l = len;
		if(l > BY2PG)
			l = BY2PG;
		p = auxpage();
		if(p == 0)
			return;
		e->bid = incref(&cache);
		p->daddr = e->bid;
		cachepage(p, fscache);
		k = kmap(p);
		
		kunmap(p);
	}
}