Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/port/cache.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


## diffname port/cache.c 1993/1011
## diff -e /dev/null /n/fornaxdump/1993/1011/sys/src/brazil/port/cache.c
0a
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"
#include	"devtab.h"

typedef struct Fcache Fcache;
typedef struct Fcalloc Fcalloc;
struct Fcache
{
	QLock;
	Qid;
	Page*	alloc;
	Fcache*	hash;
	Fcache*	next;
};

struct Fcalloc
{
	ulong	start;
	short	len;
	Page*	data;
};

Fcache*
clook(Qid *qid)
{

}
.
## diffname port/cache.c 1993/1013
## diff -e /n/fornaxdump/1993/1011/sys/src/brazil/port/cache.c /n/fornaxdump/1993/1013/sys/src/brazil/port/cache.c
30a
	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);
	}
.
29a
	int h;
.
27,28c
Mntcache*
clook(Chan *c)
.
25a
Cache cache;
.
22,24c
	Ref;
	Mntcache	*head;
	Mntcache	*tail;
	Mntcache	*hash[NHASH];
.
20c
typedef struct Cache Cache;
struct Cache
.
15,17c
	int	dev;
	int	type;
	Qlock;
	Extent	 *list;
	Mntcache *hash;
	Mntcache *prev;
	Mntcache *next;
.
13c
	int	bid;
	ulong	start;
	int	len;
	Extent	*next;
};

typedef struct Mntcache Mntcache;
struct Mntcache
{
.
9,11c
Image	fscache;

typedef Extent Extent;
struct Extent
.
## diffname port/cache.c 1993/1014
## diff -e /n/fornaxdump/1993/1013/sys/src/brazil/port/cache.c /n/fornaxdump/1993/1014/sys/src/brazil/port/cache.c
273a

	/* append to extent list */
	if(f == 0) {
		p->next = cchain(buf, offset, len, &tail);
		qunlock(m);
		return;
	}

	/* trim data against successor */
	eblock = offset+len;
	if(eblock > f->start) {
		o = eblock - f->start;
		if(o < 0) {
			qunlock(m);
			return;
		}
		len -= o;
	}

	/* Insert a middle block */
	p->next = cchain(buf, offset, len, &tail);
	if(p->next == 0)
		p->next = f;
	else
		tail->next = f;

	qunlock(m);
.
267,272c
		}
		if(cpgmove(e, buf, p->len, o)) {
			e->len += o;
			buf += o;
			len -= o;
			offset += o;
			if(len <= 0) {
				qunlock(m);
				return;
			}
		}
.
260,265c
		}
		buf += o;
		offset += o;
	}

	/* try and pack data into the predecessor */
	if(offset == ee && p->len < BY2PG) {
		o = len;
		if(o > BY2PG - p->len)
			o = BY2PG - p->len;
		if(len <= 0) {
			qunlock(m);
.
256,258c
	/*
	 * Find the insertion point
	 */
	p = 0;
	for(f = m->list; f; f = f->next) {
		if(f->start >= offset)
			break;
		p = f;
	}
	if(p == 0) {		/* at the head */
		eblock = offset+len;
		/* trim if there is a successor */
		if(f != 0 && eblock >= f->start) {
			len -= (eblock - f->start);
			if(len <= 0) {
				qunlock(m);
				return;
			}
		}
		e = cchain(buf, offset, len, &tail);
		m->list = e;
		if(tail != 0)
			tail->next = f;
		qunlock(m);
		return;
	}

	/* trim to the predecessor */
	ee = p->start+p->len;
	if(offset < ee) {
		o = ee - offset;
		len -= o;
		if(len <= 0) {
			qunlock(m);
.
242a
	Extent *tail;
	Extent *e, *f, *p;
	int o, ee, eblock; 
.
241d
239c
cupdate(Chan *c, uchar *buf, int len, ulong offset)
.
237a
Extent*
cchain(uchar *buf, ulong offset, int len, Extent **tail)
{
	int l;
	Page *p;
	KMap *k;
	Extent *e, *start, **t;

	start = 0;
	t = &start;
	while(len) {
		e = malloc(sizeof(Extent));
		if(e == 0)
			break;

		p = auxpage();
		if(p == 0) {
			free(e);
			break;
		}
		e->cache = p;
		e->start = offset;
		l = len;
		if(l > BY2PG)
			l = BY2PG;

		e->bid = incref(&cache);
		p->daddr = e->bid;
		k = kmap(p);
		memmove((void*)VA(k), buf, l);
		kunmap(k);

		cachepage(p, &fscache);
		putpage(p);

		buf += l;
		offset += l;
		len -= l;

		*t = e;
		*tail = e;
		t = &e->next;
	}
	return start;
}

int
cpgmove(Extent *e, uchar *buf, int boff, int len)
{
	Page *p;
	KMap *k;

	p = cpage(e);
	if(p == 0)
		return 0;
	k = kmap(p);
	memmove((uchar*)VA(k)+boff, buf, len);
	kunmap(k);
	putpage(p);

	return 1;
}

.
234c
	qunlock(m);
.
229c
		t = &e->next;
.
220,221c
		memmove(buf, (uchar*)VA(k) + o, l);
.
201c
			*t = e->next;
.
199c
		p = cpage(e);
.
189c
		t = &e->next;
.
185,186c
	t = &m->list;
	for(e = *t; e; e = e->next) {
.
172,173c
	Extent *e, **t;
	int o, l, total, end;
.
167c
cread(Chan *c, uchar *buf, int len, ulong offset)
.
157c
	if(m->vers != c->qid.vers)
.
155c
	if(m->path != c->qid.path)
.
142,143c
	unlock(&cache);
	m->Qid = c->qid;
.
130,131c
	lock(&cache);
	l = &cache.hash[m->path%NHASH];
.
119c
		if(m->vers != c->qid.vers)
.
77c
		n = e->next;
.
67a
cprint(Mntcache *m)
{
	Extent *e;

	print("%lux.%lux %d %d\n", m->path, m->vers, m->type, m->dev);

	while(e)
		print("\t%4d %5d %4d %lux\n",
			e->bid, e->start, e->len, e->cache);
}

Page*
cpage(Extent *e)
{
	/* Easy consistency check */
	if(e->cache->daddr != e->bid)
		return 0;

	return lookpage(&fscache, e->bid);
}

void
.
52c
		if(m->path == c->qid.path) {
.
46a
	Mntcache *m;
.
42a
void
cinit(void)
{
	int i;
	Mntcache *m;

	cache.ref = 1;
	cache.head = xalloc(sizeof(Mntcache)*NFILE);
	m = cache.head;
	
	for(i = NFILE; i > 0; i++) {
		m->next = m+1;
		m->prev = m-1;
		m++;
	}

	cache.tail = m;
	cache.tail->next = 0;
	cache.head->prev = 0;
}

.
26c
	QLock;
.
16a
	Page	*cache;
.
11c
enum
{
	NHASH		= 128,
	MAXCACHE	= 1024*1024,
	NFILE		= 4096,
};

typedef struct Extent Extent;
.
## diffname port/cache.c 1993/1015
## diff -e /n/fornaxdump/1993/1014/sys/src/brazil/port/cache.c /n/fornaxdump/1993/1015/sys/src/brazil/port/cache.c
458a
}

void
cwrite(Chan* c, uchar *buf, int len, ulong offset)
{
	int o;
	Mntcache *m;
	ulong eblock, ee;
	Extent *p, *f, *e, *tail;

	if(offset > MAXCACHE || len == 0)
		return;

	m = c->mcp;
	if(m == 0)
		return;

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

	m->vers++;

	p = 0;
	for(f = m->list; f; f = f->next) {
		if(offset >= f->start)
			break;
		p = f;		
	}

	if(p != 0) {
		ee = p->start+p->len;
		if(ee > offset) {
			o = ee - offset;
			p->len -= o;
			if(p->len)
				panic("del empty extent");
		}
	}

	eblock = offset+len;
	/* free the overlap - its a rare case */
	while(f && f->start < eblock) {
		e = f->next;
		free(f);
		f = e;
	}

	e = cchain(buf, offset, len, &tail);
	if(p == 0)
		m->list = e;
	else
		p->next = e;
	if(tail != 0)
		tail->next = f;
	qunlock(m);
cprint(m, "cwrite");
.
451c
	/* insert a middle block */
.
421,422c
		if(cpgmove(p, buf, p->len, o)) {
			p->len += o;
.
380a

.
362a
	if(offset > MAXCACHE || len == 0)
		return;

.
360c
	if(c->qid.path & CHDIR)
.
358c
	int o, ee, eblock;
.
343a

.
314a
		e->cache = p;
		e->start = offset;
		e->len = l;
.
309,310d
229a

.
226a
	if(c->qid.path & CHDIR)
		return 0;

.
201a
cprint(m, "copen new");
.
199d
194a

.
193a
	ctail(m, 0);
.
175a
cprint(m, "copen lru");
.
173,174c
			m->vers = c->qid.vers;
		}
.
171c
		if(m->vers != c->qid.vers) {
cprint(m, "copen mod");
.
169a
		c->mcp = m;
		ctail(m, 1);

.
167a
	if(c->qid.path & CHDIR)
		return;

.
160c
	if(dolock)
		unlock(&cache);
.
137c
	if(dolock)
		lock(&cache);
.
135c
ctail(Mntcache *m, int dolock)
.
131a
	m->list = 0;
.
102,104c
	for(e = m->list; e; e = e->next)
.
100a
return;
	print("%s: 0x%lux.0x%lux %d %d\n",
			s, m->path, m->vers, m->type, m->dev);
.
98c
cprint(Mntcache *m, char *s)
.
61c
	for(i = 0; i < NFILE; i++) {
.
## diffname port/cache.c 1993/1016
## diff -e /n/fornaxdump/1993/1015/sys/src/brazil/port/cache.c /n/fornaxdump/1993/1016/sys/src/brazil/port/cache.c
534,539c
	if(e != 0) {
		if(p == 0)
			m->list = e;
		else
			p->next = e;
		if(tail != 0)
			tail->next = f;
	}
.
520c
			if(p->len == 0)
.
485a
cupdate(Chan *c, uchar *buf, int len, ulong offset)
{
	cxupdate(c, buf, len, offset);
	cprint(c->mcp, "cupdate");
}

void
.
472d
468c
		len -= o;
		if(len <= 0) {
.
441,444d
416,418c
		if(e != 0) {
			m->list = e;
			if(tail != 0)
				tail->next = f;
		}
.
380,382d
373c
cxupdate(Chan *c, uchar *buf, int len, ulong offset)
.
350a

.
315a
	*tail = 0;
.
290a

		poperror();
.
258a
		if(offset < e->start) {
			qunlock(m);
			return 0;
		}	
.
257c
		if(offset > e->start && offset < e->start+e->len)
.
215d
213a
	unlock(&cache);

	qlock(m);
	cnodata(m);
.
212d
206,207c
	ctail(m);
.
201c
		l = &f->hash;
.
198c
			*l = f->hash;
.
196c
	for(f = *l; f; f = f->hash) {
.
193,194d
186,188d
180,184c
			/* File was updated, invalidate cache */
			if(m->vers != c->qid.vers) {
				qlock(m);
				cnodata(m);
				m->vers = c->qid.vers;
				qunlock(m);
			}
			return;
.
175,178c
	lock(&cache);
	for(m = cache.hash[c->qid.path%NHASH]; m; m = m->hash) {
		if(m->path == c->qid.path)
		if(m->dev == c->dev && m->type == c->type) {
			c->mcp = m;
			ctail(m);
			unlock(&cache);
.
162,164d
139,141d
137c
ctail(Mntcache *m)
.
101c

.
72,96d
61c
	for(i = 0; i < NFILE-1; i++) {
.
## diffname port/cache.c 1993/1017
## diff -e /n/fornaxdump/1993/1016/sys/src/brazil/port/cache.c /n/fornaxdump/1993/1017/sys/src/brazil/port/cache.c
524d
503a
		if(ee == offset && p->len < BY2PG) {
			o = len;
			if(o > BY2PG - p->len)
				o = BY2PG - p->len;
			if(cpgmove(p, buf, p->len, o)) {
				p->len += o;
				buf += o;
				len -= o;
				offset += o;
				if(len <= 0) {
					qunlock(m);
					return;
				}
			}
		}
.
491c
		if(f->start >= offset)
.
487a
	c->qid.vers++;
.
463d
312c
		lock(&cache);
		e->bid = cache.pgno;
		cache.pgno += BY2PG;
		unlock(&cache);
.
279a
if(len) print("P"); else print("F");
.
230,233d
225d
210c
	int o, l, total;
.
141c
	if((c->qid.path&CHDIR) || (c->flag&CTEXT))
.
81c
		pprint("\t%4d %5d %4d %lux\n",
.
77,78c
	ptpath(c->path, buf, sizeof(buf));
	pprint("%s: 0x%lux.0x%lux %d %d %s\n",
			s, m->path, m->vers, m->type, m->dev, buf);
.
75a
	char buf[128];
.
73c
cprint(Chan *c, Mntcache *m, char *s)
.
57d
44c
	Lock;
	int		pgno;
.
## diffname port/cache.c 1993/1018
## diff -e /n/fornaxdump/1993/1017/sys/src/brazil/port/cache.c /n/fornaxdump/1993/1018/sys/src/brazil/port/cache.c
504a
		/* Pack sequential write if there is space */
.
461,466d
353c
cupdate(Chan *c, uchar *buf, int len, ulong offset)
.
313a

.
309a

.
277c

.
229c
		if(offset >= e->start && offset < e->start+e->len)
.
214,216d
201a
	if(m->vers != c->qid.vers)
		return 0;
.
196,197d
186,188c
	while(e) {
		next = e->next;
		free(e);
		e = next;
	}
cprint(c, m, "open new");
.
183a
	e = m->list;
	m->list = 0;
.
182a

	l = &cache.hash[h];
	m->hash = *l;
	*l = m;
	ctail(m);

.
175,178d
170c
			*l = m->hash;
.
160a
else
cprint(c, m, "open cached");
.
159a
cprint(c, m, "open mod");
.
158d
155a
				m->vers = c->qid.vers;
.
147c
	for(m = cache.hash[h]; m; m = m->hash) {
.
145a
	h = c->qid.path%NHASH;
.
143c
	if(c->qid.path&CHDIR)
.
140a
	int h;
	Extent *e, *next;
.
133,134c
		cache.head = m;
		cache.tail = m;
		m->prev = 0;
		m->next = 0;
.
84a
	}
.
82,83c
	for(e = m->list; e; e = e->next) {
		if(0) pprint("\t%4d %5d %4d %lux\n",
.
79,80c
	nb = 0;
	ct = 1;
	if(m->list)
		o = m->list->start;
	for(e = m->list; e; e = e->next) {
		nb += e->len;
		if(o != e->start)
			ct = 0;
		o = e->start+e->len;
	}
	pprint("%s: 0x%lux.0x%lux %d %d %s (%d %c)\n",
	s, m->path, m->vers, m->type, m->dev, buf, nb, ct ? 'C' : 'N');
.
77c
return;
.
74a
	ulong o;
	int nb, ct;
.
## diffname port/cache.c 1993/1022
## diff -e /n/fornaxdump/1993/1018/sys/src/brazil/port/cache.c /n/fornaxdump/1993/1022/sys/src/brazil/port/cache.c
557a

.
## diffname port/cache.c 1993/1028
## diff -e /n/fornaxdump/1993/1022/sys/src/brazil/port/cache.c /n/fornaxdump/1993/1028/sys/src/brazil/port/cache.c
216d
180,181d
178d
79c

.
## diffname port/cache.c 1993/1103
## diff -e /n/fornaxdump/1993/1028/sys/src/brazil/port/cache.c /n/fornaxdump/1993/1103/sys/src/brazil/port/cache.c
95c
		pprint("\t%4d %5d %4d %lux\n",
.
82a
	o = 0;
.
## diffname port/cache.c 1994/0728
## diff -e /n/fornaxdump/1993/1103/sys/src/brazil/port/cache.c /n/fornaxdump/1994/0728/sys/src/brazil/port/cache.c
369a

	poperror();
.
368a
	if(waserror()) {		/* Since buf may be virtual */
		kunmap(k);
		nexterror();
	}

.
340a
		poperror();
.
339a
		if(waserror()) {		/* buf may be virtual */
			kunmap(k);
			nexterror();
		}
.
287a

.
279,283d
270a
		o = offset - e->start;
		l = len;
		if(l > e->len-o)
			l = e->len-o;

.
## diffname port/cache.c 1994/0817
## diff -e /n/fornaxdump/1994/0728/sys/src/brazil/port/cache.c /n/fornaxdump/1994/0817/sys/src/brazil/port/cache.c
69a

	fscache.notext = 1;
.
## diffname port/cache.c 1996/0223
## diff -e /n/fornaxdump/1994/0817/sys/src/brazil/port/cache.c /n/fornaxdump/1996/0223/sys/src/brazil/port/cache.c
7d
## diffname port/cache.c 1996/0326
## diff -e /n/fornaxdump/1996/0223/sys/src/brazil/port/cache.c /n/fornaxdump/1996/0326/sys/src/brazil/port/cache.c
526a
	}

	if(f != 0) {
		o = offset - f->start;
		if(o >= 0) {
			n = f->len - o;
			if(n > len)
				n = len;
			if(n > 0) {
				if(cpgmove(f, buf, o, n)) {
					buf += n;
					len -= n;
					offset += n;
				}
				if(len == 0) {
					qunlock(m);
					return;
				}
			}	
		}
.
501c
	int o, n;
.
## diffname port/cache.c 1996/0329
## diff -e /n/fornaxdump/1996/0326/sys/src/brazil/port/cache.c /n/fornaxdump/1996/0329/sys/src/brazil/port/cache.c
531,545c
		n = f->len - o;
		if(n > len)
			n = len;
		if(cpgmove(f, buf, o, n)) {
			len -= n;
			if(len == 0) {
				qunlock(m);
				return;
			}
			offset += n;
			buf += n;
.
529c
	if(f != 0 && offset < f->start+f->len) {
.
## diffname port/cache.c 1996/0402
## diff -e /n/fornaxdump/1996/0329/sys/src/brazil/port/cache.c /n/fornaxdump/1996/0402/sys/src/brazil/port/cache.c
529c
	if(0 && f != 0 && offset < f->start+f->len) {
.
## diffname port/cache.c 1997/0327
## diff -e /n/fornaxdump/1996/0402/sys/src/brazil/port/cache.c /n/emeliedump/1997/0327/sys/src/brazil/port/cache.c
214a
	qunlock(m);
.
204a
	qlock(m);
.
## diffname port/cache.c 1998/0327
## diff -e /n/emeliedump/1997/0327/sys/src/brazil/port/cache.c /n/emeliedump/1998/0327/sys/src/brazil/port/cache.c
520a
	offset = off;
.
508c
	if(off > MAXCACHE || len == 0)
.
506a
	ulong offset;
.
501c
cwrite(Chan* c, uchar *buf, int len, vlong off)
.
414a
	offset = off;
.
400c
	if(off > MAXCACHE || len == 0)
.
398a
	ulong offset;
.
393c
cupdate(Chan *c, uchar *buf, int len, vlong off)
.
251a
	offset = off;
.
241a
	if(off+len > MAXCACHE)
		return 0;

.
240a
	ulong offset;
.
234c
cread(Chan *c, uchar *buf, int len, vlong off)
.
## diffname port/cache.c 1998/0512
## diff -e /n/emeliedump/1998/0327/sys/src/brazil/port/cache.c /n/emeliedump/1998/0512/sys/src/brazil/port/cache.c
537c
		p = f;
.
132c
	if(m->prev)
.
59c

.
## diffname port/cache.c 1999/0508
## diff -e /n/emeliedump/1998/0512/sys/src/brazil/port/cache.c /n/emeliedump/1999/0508/sys/src/brazil/port/cache.c
586c
		extentfree(f);
.
331c
			extentfree(e);
.
325c
		e = extentalloc();
.
274c
			extentfree(e);
.
213c
		extentfree(e);
.
123c
		extentfree(e);
.
50a
typedef struct Ecache Ecache;
struct Ecache
{
	Lock;
	int	total;
	int	free;
	Extent*	head;
};

static Image fscache;
static Cache cache;
static Ecache ecache;

static void
extentfree(Extent* e)
{
	lock(&ecache);
	e->next = ecache.head;
	ecache.head = e;
	ecache.free++;
	unlock(&ecache);
}

static Extent*
extentalloc(void)
{
	Extent *e;
	int i;

	lock(&ecache);
	if(ecache.head == nil){
		e = xalloc(NEXTENT*sizeof(Extent));
		if(e == nil){
			unlock(&ecache);
			return nil;
		}
		for(i = 0; i < NEXTENT; i++){
			e->next = ecache.head;
			ecache.head = e;
			e++;
		}
		ecache.free += NEXTENT;
		ecache.total += NEXTENT;
	}

	e = ecache.head;
	ecache.head = e->next;
	memset(e, 0, sizeof(Extent));
	ecache.free--;
	unlock(&ecache);

	return e;
}

.
49d
14a
	NEXTENT		= 200,		/* extent allocation size */
.
8,9d
## diffname port/cache.c 1999/0629
## diff -e /n/emeliedump/1999/0508/sys/src/brazil/port/cache.c /n/emeliedump/1999/0629/sys/src/brazil/port/cache.c
146c
	s, m->path, m->vers, m->type, m->dev, c->name, nb, ct ? 'C' : 'N');
.
133d
131d
## diffname port/cache.c 1999/0903
## diff -e /n/emeliedump/1999/0629/sys/src/brazil/port/cache.c /n/emeliedump/1999/0903/sys/src/brazil/port/cache.c
566c
	if(off > maxcache || len == 0)
.
456c
	if(off > maxcache || len == 0)
.
293c
	if(off+len > maxcache)
.
111a
	if(conf.npage*BY2PG > 200*MB)
		maxcache = 10*MAXCACHE;

.
60a
static int maxcache = MAXCACHE;
.
## diffname port/cache.c 1999/1022
## diff -e /n/emeliedump/1999/0903/sys/src/brazil/port/cache.c /n/emeliedump/1999/1022/sys/src/brazil/port/cache.c
398a
		/* wrap the counter; low bits are unused by pghash by checked by lookpage */
		if((cache.pgno & ~(BY2PG-1)) == 0){
			if(cache.pgno == BY2PG-1){
				print("cache wrapped\n");
				cache.pgno = 0;
			}else
				cache.pgno++;
		}
.
## diffname port/cache.c 1999/1101
## diff -e /n/emeliedump/1999/1022/sys/src/brazil/port/cache.c /n/emeliedump/1999/1101/sys/src/9/port/cache.c
399c
		/* wrap the counter; low bits are unused by pghash but checked by lookpage */
.
## diffname port/cache.c 1999/1227
## diff -e /n/emeliedump/1999/1101/sys/src/9/port/cache.c /n/emeliedump/1999/1227/sys/src/9/port/cache.c
661a

	if(p == 0)
		m->list = f;
	else
		p->next = f;
.
654,660c
		tail->next = f;
		f = e;
.
651a
	/* link the block (if any) into the middle */
.
645d
643a
	/* free the overlap -- it's a rare case */
.
636,639d
629,632c
			if(o > BY2PG - eo)
				o = BY2PG - eo;
			if(cpgmove(p, buf, eo, o)) {
				if(eo+o > p->len)
					p->len = eo+o;
.
620,627c
		eo = offset - p->start;
		/* pack in predecessor if there is space */
		if(offset <= ee && eo < BY2PG) {
.
602,617d
572c
	int o, eo;
.
565c
	}
.
541,563c
	e = cchain(buf, offset, len, &tail);
	if(e != 0) {
		p->next = e;
.
534a
if (f && p->start + p->len > f->start) print("CACHE: p->start=%uld p->len=%d f->start=%uld\n", p->start, p->len, f->start);
.
504,505c
			tail->next = f;
.
500a
	}

	if(p == 0) {		/* at the head */
.
491,499c
	/* trim if there is a successor */
	eblock = offset+len;
	if(f != 0 && eblock > f->start) {
		len -= (eblock - f->start);
		if(len <= 0) {
			qunlock(m);
			return;
.
486c
		if(f->start > offset)
.
## diffname port/cache.c 2000/0609
## diff -e /n/emeliedump/1999/1227/sys/src/9/port/cache.c /n/emeliedump/2000/0609/sys/src/9/port/cache.c
114a
	if(conf.npage*BY2PG > 400*MB)
		maxcache = 50*MAXCACHE;
.
112a
	/* a better algorithm would be nice */
.
## diffname port/cache.c 2001/0203
## diff -e /n/emeliedump/2000/0609/sys/src/9/port/cache.c /n/emeliedump/2001/0203/sys/src/9/port/cache.c
114,117c
//	if(conf.npage*BY2PG > 200*MB)
//		maxcache = 10*MAXCACHE;
//	if(conf.npage*BY2PG > 400*MB)
//		maxcache = 50*MAXCACHE;
.
## diffname port/cache.c 2001/0527
## diff -e /n/emeliedump/2001/0203/sys/src/9/port/cache.c /n/emeliedump/2001/0527/sys/src/9/port/cache.c
576c
	m->qid.vers++;
.
285c
	if(m->qid.vers != c->qid.vers)
.
280a
	if(m->qid.type != c->qid.type)
		return 0;
.
279c
	if(m->qid.path != c->qid.path)
.
253c
	m->qid = c->qid;
.
244c
	l = &cache.hash[m->qid.path%NHASH];
.
232,233c
			if(m->qid.vers != c->qid.vers) {
				m->qid.vers = c->qid.vers;
.
225c
		if(m->qid.path == c->qid.path)
		if(m->qid.type == c->qid.type)
.
219c
	/* directories aren't cacheable and append-only files confuse us */
	if(c->qid.type&(QTDIR|QTAPPEND))
.
151c
	s, m->qid.path, m->qid.vers, m->type, m->dev, c->name, nb, ct ? 'C' : 'N');
.
114,117c
	if(conf.npage*BY2PG > 200*MB)
		maxcache = 10*MAXCACHE;
	if(conf.npage*BY2PG > 400*MB)
		maxcache = 50*MAXCACHE;
.
29c
	Qid	qid;
.
## diffname port/cache.c 2001/0530
## diff -e /n/emeliedump/2001/0527/sys/src/9/port/cache.c /n/emeliedump/2001/0530/sys/src/9/port/cache.c
114,117c
//	if(conf.npage*BY2PG > 200*MB)
//		maxcache = 10*MAXCACHE;
//	if(conf.npage*BY2PG > 400*MB)
//		maxcache = 50*MAXCACHE;
.
## diffname port/cache.c 2003/0406
## diff -e /n/emeliedump/2001/0530/sys/src/9/port/cache.c /n/emeliedump/2003/0406/sys/src/9/port/cache.c
542c
if(f && p->start + p->len > f->start) print("CACHE: p->start=%uld p->len=%d f->start=%uld\n", p->start, p->len, f->start);
.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].