From f4942edf96fc64e523d88f89bb96cf05dcf2223d Mon Sep 17 00:00:00 2001 From: David du Colombier <0intro@gmail.com> Date: Sat, 16 Oct 1993 00:00:00 +0000 Subject: [PATCH] Plan 9 from Bell Labs 1993-10-16 --- port/cache.c | 137 ++++++++++++++++++++++--------------------------- port/devcons.c | 47 +++++++++++++++-- port/devmnt.c | 41 ++++++++------- port/portdat.h | 1 + 4 files changed, 126 insertions(+), 100 deletions(-) diff --git a/port/cache.c b/port/cache.c index 991cb5175e3d8b68c99ae6e31feca44438e8190f..5cde0b9c06d18cba414b2dd3e33d03f78e1b8712 100644 --- a/port/cache.c +++ b/port/cache.c @@ -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 */ @@ -482,6 +456,13 @@ cupdate(Chan *c, uchar *buf, int len, ulong offset) qunlock(m); } +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) { @@ -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"); } diff --git a/port/devcons.c b/port/devcons.c index 4ece57cee9260bfae413b399e5faff18e59801bc..491e661245586b1f2be383b813bb7074570b427a 100644 --- a/port/devcons.c +++ b/port/devcons.c @@ -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) diff --git a/port/devmnt.c b/port/devmnt.c index 1bcd57a693d703e41f1b2e19c426de10d38446e7..dddf3ce22e30c3e3d49b3122fc63dd99e9418354 100644 --- a/port/devmnt.c +++ b/port/devmnt.c @@ -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); } diff --git a/port/portdat.h b/port/portdat.h index cc79769ac58f03acd584d9955a5600920615ddca..32f2352eceec531d894f53ccb9c9d7e22084f879 100644 --- a/port/portdat.h +++ b/port/portdat.h @@ -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