~kris/9p

9hist

f4942edf96fc64e523d88f89bb96cf05dcf2223d — David du Colombier 32 years ago b7d0c03
Plan 9 from Bell Labs 1993-10-16
4 files changed, 126 insertions(+), 100 deletions(-)

M port/cache.c
M port/devcons.c
M port/devmnt.c
M port/portdat.h
M port/cache.c => port/cache.c +60 -77
@@ 58,7 58,7 @@ cinit(void)
	cache.head = xalloc(sizeof(Mntcache)*NFILE);
	m = cache.head;
	
	for(i = 0; i < NFILE; i++) {
	for(i = 0; i < NFILE-1; i++) {
		m->next = m+1;
		m->prev = m-1;
		m++;


@@ 69,36 69,11 @@ cinit(void)
	cache.head->prev = 0;
}

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

	h = c->qid.path%NHASH;

	lock(&cache);
	for(m = cache.hash[h]; m; m = m->hash) {
		if(m->path == c->qid.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
cprint(Mntcache *m, char *s)
{
	Extent *e;
return;

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



@@ 134,11 109,8 @@ cnodata(Mntcache *m)
}

void
ctail(Mntcache *m, int dolock)
ctail(Mntcache *m)
{
	if(dolock)
		lock(&cache);

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


@@ 159,9 131,6 @@ ctail(Mntcache *m, int dolock)
		cache.head = cache.tail = m;
		m->prev = m->next = 0;
	}

	if(dolock)
		unlock(&cache);
}

void


@@ 172,47 141,49 @@ copen(Chan *c)
	if(c->qid.path & CHDIR)
		return;

	m = clook(c);
	if(m != 0) {
		c->mcp = m;
		ctail(m, 1);

		/* File was updated */
		if(m->vers != c->qid.vers) {
cprint(m, "copen mod");
			cnodata(m);
			m->vers = c->qid.vers;
	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);

			/* File was updated, invalidate cache */
			if(m->vers != c->qid.vers) {
				qlock(m);
				cnodata(m);
				m->vers = c->qid.vers;
				qunlock(m);
			}
			return;
		}
		qunlock(m);
cprint(m, "copen lru");
		return;
	}

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

	m->Qid = c->qid;
	m->dev = c->dev;
	m->type = c->type;
	cnodata(m);
	c->mcp = m;
	unlock(&cache);

	qlock(m);
	cnodata(m);
	qunlock(m);
cprint(m, "copen new");
}

static int


@@ 254,8 225,12 @@ cread(Chan *c, uchar *buf, int len, ulong offset)
	end = offset+len;
	t = &m->list;
	for(e = *t; e; e = e->next) {
		if(e->start >= offset && e->start+e->len < end)
		if(offset > e->start && offset < e->start+e->len)
			break;
		if(offset < e->start) {
			qunlock(m);
			return 0;
		}	
		t = &e->next;
	}



@@ 288,6 263,8 @@ cread(Chan *c, uchar *buf, int len, ulong offset)
			l = e->len-o;

		memmove(buf, (uchar*)VA(k) + o, l);

		poperror();
		kunmap(k);
		putpage(p);



@@ 313,6 290,7 @@ cchain(uchar *buf, ulong offset, int len, Extent **tail)
	Extent *e, *start, **t;

	start = 0;
	*tail = 0;
	t = &start;
	while(len) {
		e = malloc(sizeof(Extent));


@@ 348,6 326,7 @@ cchain(uchar *buf, ulong offset, int len, Extent **tail)
		*tail = e;
		t = &e->next;
	}

	return start;
}



@@ 370,16 349,13 @@ cpgmove(Extent *e, uchar *buf, int boff, int len)
}

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

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

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



@@ 413,9 389,11 @@ cupdate(Chan *c, uchar *buf, int len, ulong offset)
			}
		}
		e = cchain(buf, offset, len, &tail);
		m->list = e;
		if(tail != 0)
			tail->next = f;
		if(e != 0) {
			m->list = e;
			if(tail != 0)
				tail->next = f;
		}
		qunlock(m);
		return;
	}


@@ 438,10 416,6 @@ cupdate(Chan *c, uchar *buf, int len, ulong offset)
		o = len;
		if(o > BY2PG - p->len)
			o = BY2PG - p->len;
		if(len <= 0) {
			qunlock(m);
			return;
		}
		if(cpgmove(p, buf, p->len, o)) {
			p->len += o;
			buf += o;


@@ 465,11 439,11 @@ cupdate(Chan *c, uchar *buf, int len, ulong offset)
	eblock = offset+len;
	if(eblock > f->start) {
		o = eblock - f->start;
		if(o < 0) {
		len -= o;
		if(len <= 0) {
			qunlock(m);
			return;
		}
		len -= o;
	}

	/* insert a middle block */


@@ 483,6 457,13 @@ cupdate(Chan *c, uchar *buf, int len, ulong offset)
}

void
cupdate(Chan *c, uchar *buf, int len, ulong offset)
{
	cxupdate(c, buf, len, offset);
	cprint(c->mcp, "cupdate");
}

void
cwrite(Chan* c, uchar *buf, int len, ulong offset)
{
	int o;


@@ 517,7 498,7 @@ cwrite(Chan* c, uchar *buf, int len, ulong offset)
		if(ee > offset) {
			o = ee - offset;
			p->len -= o;
			if(p->len)
			if(p->len == 0)
				panic("del empty extent");
		}
	}


@@ 531,12 512,14 @@ cwrite(Chan* c, uchar *buf, int len, ulong offset)
	}

	e = cchain(buf, offset, len, &tail);
	if(p == 0)
		m->list = e;
	else
		p->next = e;
	if(tail != 0)
		tail->next = f;
	if(e != 0) {
		if(p == 0)
			m->list = e;
		else
			p->next = e;
		if(tail != 0)
			tail->next = f;
	}
	qunlock(m);
cprint(m, "cwrite");
}

M port/devcons.c => port/devcons.c +42 -5
@@ 165,7 165,10 @@ prflush(void)
void
echo(Rune r, char *buf, int n)
{
	static int ctrlt;
	static int ctrlt, pid;
	ulong l, v, top;
	extern ulong etext;
	Proc *p, *ep;

	/*
	 * ^p hack


@@ 176,7 179,22 @@ echo(Rune r, char *buf, int n)
	/*
	 * ^t hack BUG
	 */
	if(ctrlt == 2){
	switch(ctrlt) {
	case 0:
		if(r == 0x14){
			ctrlt = 1;
			return;
		}
		break;
	case 1:
		if(r > '0' && r <= '9') {
			ctrlt = 3;
			pid = r-'0';
			return;
		}
		ctrlt = 2;
		return;
	case 2:
		ctrlt = 0;
		switch(r){
		case 0x14:


@@ 195,9 213,28 @@ echo(Rune r, char *buf, int n)
			exit(0);
			break;
		}
	}else if(r == 0x14){
		ctrlt++;
		return;
		break;
	case 3:
		if(r > '0' && r <= '9') {
			pid = (pid*10)+(r-'0');
			return;
		}
		print("PID %d\n", pid);
		p = proctab(0);
		ep = p+conf.nproc;
		for(; p < ep; p++) {
			if(p->pid == pid) {
				top = (ulong)p->kstack + KSTACK;
				for(l=(ulong)p->sched.sp; l < top; l += BY2WD) {
					v = *(ulong*)l;
					if(KTZERO < v && v < (ulong)&etext) {
						print("%lux=%lux\n", l, v);
						delay(100);
					}
				}
			}
		}
		break;
	}
	ctrlt = 0;
	if(kbd.raw)

M port/devmnt.c => port/devmnt.c +23 -18
@@ 44,7 44,7 @@ void	mntgate(Mnt*);
void	mntpntfree(Mnt*);
void	mntqrm(Mnt*, Mntrpc*);
Mntrpc*	mntralloc(Chan*);
long	mntrdwr(int , Mnt*, Chan*, void*,long , ulong);
long	mntrdwr(int , Chan*, void*,long , ulong);
void	mntrpcread(Mnt*, Mntrpc*);
void	mountio(Mnt*, Mntrpc*);
void	mountmux(Mnt*, Mntrpc*);


@@ 93,7 93,7 @@ mntattach(char *muxattach)

	lock(&mntalloc);
	for(m = mntalloc.list; m; m = m->list) {
		if(m->c == c && m->id && m->flags == bogus.flags) {
		if(m->c == c && m->id) {
			lock(m);
			if(m->id && m->ref > 0 && m->c == c) {
				unlock(&mntalloc);


@@ 106,6 106,8 @@ mntattach(char *muxattach)
				}
				mattach(m, c, bogus.spec);
				poperror();
				if(bogus.flags&MCACHE)
					c->flag |= CCACHE;
				return c;
			}
			unlock(m);	


@@ 136,7 138,7 @@ mntattach(char *muxattach)
	m->c = c;
	m->c->flag |= CMSG;
	m->blocksize = MAXFDATA;	/**/
	m->flags = bogus.flags;
	m->flags = bogus.flags & ~MCACHE;

	switch(devchar[m->c->type]) {
	default:


@@ 200,6 202,9 @@ mntattach(char *muxattach)
		incref(c->mntptr);
	}

	if(bogus.flags & MCACHE)
		c->flag |= CCACHE;

	return c;
}



@@ 358,7 363,7 @@ mntopen(Chan *c, int omode)
	poperror();
	mntfree(r);

	if(m->flags&MCACHE)
	if(c->flag & CCACHE)
		copen(c);

	return c;


@@ 389,7 394,7 @@ mntcreate(Chan *c, char *name, int omode, ulong perm)
	poperror();
	mntfree(r);

	if(m->flags & MCACHE)
	if(c->flag & CCACHE)
		copen(c);
}



@@ 491,12 496,10 @@ mntwstat(Chan *c, char *dp)
long	 
mntread(Chan *c, void *buf, long n, ulong offset)
{
	Mnt *m;
	uchar *p, *e;
	int nc, cached;

	m = mntchk(c);
	cached = m->flags&MCACHE;
	cached = c->flag & CCACHE;

	SET(nc);
	if(cached) {


@@ 508,12 511,12 @@ mntread(Chan *c, void *buf, long n, ulong offset)
		}
	}

	n = mntrdwr(Tread, m, c, buf, n, offset);
	if(c->qid.path & CHDIR) 
	n = mntrdwr(Tread, c, buf, n, offset);
	if(c->qid.path & CHDIR) {
		for(p = (uchar*)buf, e = &p[n]; p < e; p += DIRLEN)
			mntdirfix(p, c);

	if(cached) {
	}
	else if(cached) {
		cupdate(c, buf, n, offset);
		n += nc;
	}


@@ 524,20 527,22 @@ mntread(Chan *c, void *buf, long n, ulong offset)
long	 
mntwrite(Chan *c, void *buf, long n, ulong offset)
{
	return mntrdwr(Twrite, mntchk(c), c, buf, n, offset);
	return mntrdwr(Twrite, c, buf, n, offset);
}

long
mntrdwr(int type, Mnt *m, Chan *c, void *buf, long n, ulong offset)
mntrdwr(int type, Chan *c, void *buf, long n, ulong offset)
{
	Mntrpc *r;
	Mnt *m;
 	Mntrpc *r;
	char *uba;
	int cache;
	ulong cnt, nr;

	m = mntchk(c);
	uba = buf;
	cnt = 0;
	cache = m->flags & MCACHE;
	cache = c->flag & CCACHE;
	for(;;) {
		r = mntralloc(c);
		if(waserror()) {


@@ 557,7 562,7 @@ mntrdwr(int type, Mnt *m, Chan *c, void *buf, long n, ulong offset)
		if(type == Tread)
			memmove(uba, r->reply.data, nr);
		else if(cache)
			cwrite(c, (uchar*)r->reply.data, nr, offset);
			cwrite(c, (uchar*)uba, nr, offset);

		poperror();
		mntfree(r);


@@ 580,7 585,7 @@ mountrpc(Mnt *m, Mntrpc *r)
	r->reply.type = 4;

	while(waserror()) {
		if((m->flags & MRECOV) == 0)
		if((m->flags&MRECOV) == 0)
			nexterror();
		mntrecover(m, r);
	}

M port/portdat.h => port/portdat.h +1 -0
@@ 109,6 109,7 @@ enum
	CFREE	= 0x0010,		/* not in use */
	CRCLOSE	= 0x0020,		/* remove on close */
	CRECOV	= 0x0040,		/* requires recovery */
	CCACHE	= 0x0080,		/* client cache */
};

struct Path