kernel: minor changes to mount cache
change page cache ids (bid) to uintptr so we use the full address space of Page.daddr. make maxcache offset check consistent in cread(). use consistent types in cupdate() and simplify with goto. make internal functions static. use nil instead of 0 for pointers.
This commit is contained in:
parent
c8ed49da60
commit
523c33bb6f
1 changed files with 72 additions and 78 deletions
|
@ -16,7 +16,7 @@ enum
|
||||||
typedef struct Extent Extent;
|
typedef struct Extent Extent;
|
||||||
struct Extent
|
struct Extent
|
||||||
{
|
{
|
||||||
int bid;
|
uintptr bid;
|
||||||
ulong start;
|
ulong start;
|
||||||
int len;
|
int len;
|
||||||
Page *cache;
|
Page *cache;
|
||||||
|
@ -40,7 +40,7 @@ typedef struct Cache Cache;
|
||||||
struct Cache
|
struct Cache
|
||||||
{
|
{
|
||||||
Lock;
|
Lock;
|
||||||
int pgno;
|
uintptr pgno;
|
||||||
Mntcache *head;
|
Mntcache *head;
|
||||||
Mntcache *tail;
|
Mntcache *tail;
|
||||||
Mntcache *hash[NHASH];
|
Mntcache *hash[NHASH];
|
||||||
|
@ -59,7 +59,7 @@ Image fscache;
|
||||||
|
|
||||||
static Cache cache;
|
static Cache cache;
|
||||||
static Ecache ecache;
|
static Ecache ecache;
|
||||||
static int maxcache = MAXCACHE;
|
static ulong maxcache = MAXCACHE;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
extentfree(Extent* e)
|
extentfree(Extent* e)
|
||||||
|
@ -89,16 +89,17 @@ extentalloc(void)
|
||||||
ecache.head = e;
|
ecache.head = e;
|
||||||
e++;
|
e++;
|
||||||
}
|
}
|
||||||
ecache.free += NEXTENT;
|
|
||||||
ecache.total += NEXTENT;
|
ecache.total += NEXTENT;
|
||||||
|
ecache.free += NEXTENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
e = ecache.head;
|
e = ecache.head;
|
||||||
ecache.head = e->next;
|
ecache.head = e->next;
|
||||||
memset(e, 0, sizeof(Extent));
|
|
||||||
ecache.free--;
|
ecache.free--;
|
||||||
unlock(&ecache);
|
unlock(&ecache);
|
||||||
|
|
||||||
|
memset(e, 0, sizeof(Extent));
|
||||||
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,61 +125,61 @@ cinit(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.tail = m;
|
cache.tail = m;
|
||||||
cache.tail->next = 0;
|
cache.tail->next = nil;
|
||||||
cache.head->prev = 0;
|
cache.head->prev = nil;
|
||||||
|
|
||||||
fscache.notext = 1;
|
fscache.notext = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Page*
|
static Page*
|
||||||
cpage(Extent *e)
|
cpage(Extent *e)
|
||||||
{
|
{
|
||||||
/* Easy consistency check */
|
/* Easy consistency check */
|
||||||
if(e->cache->daddr != e->bid)
|
if(e->cache->daddr != e->bid)
|
||||||
return 0;
|
return nil;
|
||||||
|
|
||||||
return lookpage(&fscache, e->bid);
|
return lookpage(&fscache, e->bid);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
cnodata(Mntcache *m)
|
cnodata(Mntcache *m)
|
||||||
{
|
{
|
||||||
Extent *e;
|
Extent *e;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Invalidate all extent data
|
* Invalidate all extent data
|
||||||
* Image lru will waste the pages
|
* pagereclaim() will waste the pages
|
||||||
*/
|
*/
|
||||||
while(e = m->list){
|
while((e = m->list) != nil){
|
||||||
m->list = e->next;
|
m->list = e->next;
|
||||||
extentfree(e);
|
extentfree(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
ctail(Mntcache *m)
|
ctail(Mntcache *m)
|
||||||
{
|
{
|
||||||
/* Unlink and send to the tail */
|
/* Unlink and send to the tail */
|
||||||
if(m->prev)
|
if(m->prev != nil)
|
||||||
m->prev->next = m->next;
|
m->prev->next = m->next;
|
||||||
else
|
else
|
||||||
cache.head = m->next;
|
cache.head = m->next;
|
||||||
if(m->next)
|
if(m->next != nil)
|
||||||
m->next->prev = m->prev;
|
m->next->prev = m->prev;
|
||||||
else
|
else
|
||||||
cache.tail = m->prev;
|
cache.tail = m->prev;
|
||||||
|
|
||||||
if(cache.tail) {
|
if(cache.tail != nil) {
|
||||||
m->prev = cache.tail;
|
m->prev = cache.tail;
|
||||||
cache.tail->next = m;
|
cache.tail->next = m;
|
||||||
m->next = 0;
|
m->next = nil;
|
||||||
cache.tail = m;
|
cache.tail = m;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cache.head = m;
|
cache.head = m;
|
||||||
cache.tail = m;
|
cache.tail = m;
|
||||||
m->prev = 0;
|
m->prev = nil;
|
||||||
m->next = 0;
|
m->next = nil;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,11 +189,11 @@ clookup(Chan *c, int skipvers)
|
||||||
{
|
{
|
||||||
Mntcache *m;
|
Mntcache *m;
|
||||||
|
|
||||||
for(m = cache.hash[c->qid.path%NHASH]; m; m = m->hash)
|
for(m = cache.hash[c->qid.path%NHASH]; m != nil; m = m->hash)
|
||||||
if(eqchantdqid(c, m->type, m->dev, m->qid, skipvers) && c->qid.type == m->qid.type)
|
if(eqchantdqid(c, m->type, m->dev, m->qid, skipvers) && c->qid.type == m->qid.type)
|
||||||
return m;
|
return m;
|
||||||
|
|
||||||
return 0;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -202,13 +203,13 @@ copen(Chan *c)
|
||||||
|
|
||||||
/* directories aren't cacheable and append-only files confuse us */
|
/* directories aren't cacheable and append-only files confuse us */
|
||||||
if(c->qid.type&(QTDIR|QTAPPEND)){
|
if(c->qid.type&(QTDIR|QTAPPEND)){
|
||||||
c->mcp = 0;
|
c->mcp = nil;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
lock(&cache);
|
lock(&cache);
|
||||||
m = clookup(c, 1);
|
m = clookup(c, 1);
|
||||||
if(m == 0)
|
if(m == nil)
|
||||||
m = cache.head;
|
m = cache.head;
|
||||||
else if(m->qid.vers == c->qid.vers) {
|
else if(m->qid.vers == c->qid.vers) {
|
||||||
ctail(m);
|
ctail(m);
|
||||||
|
@ -219,7 +220,7 @@ copen(Chan *c)
|
||||||
ctail(m);
|
ctail(m);
|
||||||
|
|
||||||
l = &cache.hash[m->qid.path%NHASH];
|
l = &cache.hash[m->qid.path%NHASH];
|
||||||
for(f = *l; f; f = f->hash) {
|
for(f = *l; f != nil; f = f->hash) {
|
||||||
if(f == m) {
|
if(f == m) {
|
||||||
*l = m->hash;
|
*l = m->hash;
|
||||||
break;
|
break;
|
||||||
|
@ -232,7 +233,7 @@ copen(Chan *c)
|
||||||
qlock(m);
|
qlock(m);
|
||||||
lock(&cache);
|
lock(&cache);
|
||||||
f = clookup(c, 0);
|
f = clookup(c, 0);
|
||||||
if(f != 0) {
|
if(f != nil) {
|
||||||
/*
|
/*
|
||||||
* someone got there first while cache lock
|
* someone got there first while cache lock
|
||||||
* was released and added a updated Mntcache
|
* was released and added a updated Mntcache
|
||||||
|
@ -266,14 +267,14 @@ ccache(Chan *c)
|
||||||
Mntcache *m;
|
Mntcache *m;
|
||||||
|
|
||||||
m = c->mcp;
|
m = c->mcp;
|
||||||
if(m) {
|
if(m != nil) {
|
||||||
qlock(m);
|
qlock(m);
|
||||||
if(eqchantdqid(c, m->type, m->dev, m->qid, 0) && c->qid.type == m->qid.type)
|
if(eqchantdqid(c, m->type, m->dev, m->qid, 0) && c->qid.type == m->qid.type)
|
||||||
return m;
|
return m;
|
||||||
c->mcp = 0;
|
c->mcp = nil;
|
||||||
qunlock(m);
|
qunlock(m);
|
||||||
}
|
}
|
||||||
return 0;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -286,34 +287,33 @@ cread(Chan *c, uchar *buf, int len, vlong off)
|
||||||
int o, l, total;
|
int o, l, total;
|
||||||
ulong offset;
|
ulong offset;
|
||||||
|
|
||||||
if(off+len > maxcache)
|
if(off >= maxcache || len <= 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
m = ccache(c);
|
m = ccache(c);
|
||||||
if(m == 0)
|
if(m == nil)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
offset = off;
|
offset = off;
|
||||||
t = &m->list;
|
t = &m->list;
|
||||||
for(e = *t; e; e = e->next) {
|
for(e = *t; e != nil; e = e->next) {
|
||||||
if(offset >= e->start && offset < e->start+e->len)
|
if(offset >= e->start && offset < e->start+e->len)
|
||||||
break;
|
break;
|
||||||
t = &e->next;
|
t = &e->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(e == 0) {
|
if(e == nil) {
|
||||||
qunlock(m);
|
qunlock(m);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
total = 0;
|
total = 0;
|
||||||
while(len) {
|
while(len > 0) {
|
||||||
p = cpage(e);
|
p = cpage(e);
|
||||||
if(p == 0) {
|
if(p == nil) {
|
||||||
*t = e->next;
|
*t = e->next;
|
||||||
extentfree(e);
|
extentfree(e);
|
||||||
qunlock(m);
|
break;
|
||||||
return total;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
o = offset - e->start;
|
o = offset - e->start;
|
||||||
|
@ -342,7 +342,7 @@ cread(Chan *c, uchar *buf, int len, vlong off)
|
||||||
total += l;
|
total += l;
|
||||||
t = &e->next;
|
t = &e->next;
|
||||||
e = e->next;
|
e = e->next;
|
||||||
if(e == 0 || e->start != offset)
|
if(e == nil || e->start != offset)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -350,7 +350,7 @@ cread(Chan *c, uchar *buf, int len, vlong off)
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
Extent*
|
static Extent*
|
||||||
cchain(uchar *buf, ulong offset, int len, Extent **tail)
|
cchain(uchar *buf, ulong offset, int len, Extent **tail)
|
||||||
{
|
{
|
||||||
int l;
|
int l;
|
||||||
|
@ -358,16 +358,16 @@ cchain(uchar *buf, ulong offset, int len, Extent **tail)
|
||||||
KMap *k;
|
KMap *k;
|
||||||
Extent *e, *start, **t;
|
Extent *e, *start, **t;
|
||||||
|
|
||||||
start = 0;
|
start = nil;
|
||||||
*tail = 0;
|
*tail = nil;
|
||||||
t = &start;
|
t = &start;
|
||||||
while(len) {
|
while(len > 0) {
|
||||||
e = extentalloc();
|
e = extentalloc();
|
||||||
if(e == 0)
|
if(e == nil)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
p = auxpage();
|
p = auxpage();
|
||||||
if(p == 0) {
|
if(p == nil) {
|
||||||
extentfree(e);
|
extentfree(e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -417,14 +417,14 @@ cchain(uchar *buf, ulong offset, int len, Extent **tail)
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
static int
|
||||||
cpgmove(Extent *e, uchar *buf, int boff, int len)
|
cpgmove(Extent *e, uchar *buf, int boff, int len)
|
||||||
{
|
{
|
||||||
Page *p;
|
Page *p;
|
||||||
KMap *k;
|
KMap *k;
|
||||||
|
|
||||||
p = cpage(e);
|
p = cpage(e);
|
||||||
if(p == 0)
|
if(p == nil)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
k = kmap(p);
|
k = kmap(p);
|
||||||
|
@ -445,25 +445,25 @@ cpgmove(Extent *e, uchar *buf, int boff, int len)
|
||||||
void
|
void
|
||||||
cupdate(Chan *c, uchar *buf, int len, vlong off)
|
cupdate(Chan *c, uchar *buf, int len, vlong off)
|
||||||
{
|
{
|
||||||
|
int o;
|
||||||
Mntcache *m;
|
Mntcache *m;
|
||||||
Extent *tail;
|
Extent *tail;
|
||||||
Extent *e, *f, *p;
|
Extent *e, *f, *p;
|
||||||
int o, ee, eblock;
|
ulong offset, eblock, ee;
|
||||||
ulong offset;
|
|
||||||
|
|
||||||
if(off > maxcache || len == 0)
|
if(off >= maxcache || len <= 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m = ccache(c);
|
m = ccache(c);
|
||||||
if(m == 0)
|
if(m == nil)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Find the insertion point
|
* Find the insertion point
|
||||||
*/
|
*/
|
||||||
offset = off;
|
offset = off;
|
||||||
p = 0;
|
p = nil;
|
||||||
for(f = m->list; f; f = f->next) {
|
for(f = m->list; f != nil; f = f->next) {
|
||||||
if(f->start > offset)
|
if(f->start > offset)
|
||||||
break;
|
break;
|
||||||
p = f;
|
p = f;
|
||||||
|
@ -471,22 +471,19 @@ cupdate(Chan *c, uchar *buf, int len, vlong off)
|
||||||
|
|
||||||
/* trim if there is a successor */
|
/* trim if there is a successor */
|
||||||
eblock = offset+len;
|
eblock = offset+len;
|
||||||
if(f != 0 && eblock > f->start) {
|
if(f != nil && eblock > f->start) {
|
||||||
len -= (eblock - f->start);
|
len -= (eblock - f->start);
|
||||||
if(len <= 0) {
|
if(len <= 0)
|
||||||
qunlock(m);
|
goto out;
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(p == 0) { /* at the head */
|
if(p == nil) { /* at the head */
|
||||||
e = cchain(buf, offset, len, &tail);
|
e = cchain(buf, offset, len, &tail);
|
||||||
if(e != 0) {
|
if(e != nil) {
|
||||||
m->list = e;
|
m->list = e;
|
||||||
tail->next = f;
|
tail->next = f;
|
||||||
}
|
}
|
||||||
qunlock(m);
|
goto out;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* trim to the predecessor */
|
/* trim to the predecessor */
|
||||||
|
@ -494,10 +491,8 @@ cupdate(Chan *c, uchar *buf, int len, vlong off)
|
||||||
if(offset < ee) {
|
if(offset < ee) {
|
||||||
o = ee - offset;
|
o = ee - offset;
|
||||||
len -= o;
|
len -= o;
|
||||||
if(len <= 0) {
|
if(len <= 0)
|
||||||
qunlock(m);
|
goto out;
|
||||||
return;
|
|
||||||
}
|
|
||||||
buf += o;
|
buf += o;
|
||||||
offset += o;
|
offset += o;
|
||||||
}
|
}
|
||||||
|
@ -513,20 +508,20 @@ cupdate(Chan *c, uchar *buf, int len, vlong off)
|
||||||
len -= o;
|
len -= o;
|
||||||
offset += o;
|
offset += o;
|
||||||
if(len <= 0) {
|
if(len <= 0) {
|
||||||
if(f && p->start + p->len > f->start)
|
if(f != nil && p->start + p->len > f->start)
|
||||||
print("CACHE: p->start=%uld p->len=%d f->start=%uld\n",
|
print("CACHE: p->start=%uld p->len=%d f->start=%uld\n",
|
||||||
p->start, p->len, f->start);
|
p->start, p->len, f->start);
|
||||||
qunlock(m);
|
goto out;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
e = cchain(buf, offset, len, &tail);
|
e = cchain(buf, offset, len, &tail);
|
||||||
if(e != 0) {
|
if(e != nil) {
|
||||||
p->next = e;
|
p->next = e;
|
||||||
tail->next = f;
|
tail->next = f;
|
||||||
}
|
}
|
||||||
|
out:
|
||||||
qunlock(m);
|
qunlock(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -535,29 +530,28 @@ cwrite(Chan* c, uchar *buf, int len, vlong off)
|
||||||
{
|
{
|
||||||
int o, eo;
|
int o, eo;
|
||||||
Mntcache *m;
|
Mntcache *m;
|
||||||
ulong eblock, ee;
|
|
||||||
Extent *p, *f, *e, *tail;
|
Extent *p, *f, *e, *tail;
|
||||||
ulong offset;
|
ulong offset, eblock, ee;
|
||||||
|
|
||||||
if(off > maxcache || len == 0)
|
if(off >= maxcache || len <= 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m = ccache(c);
|
m = ccache(c);
|
||||||
if(m == 0)
|
if(m == nil)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
offset = off;
|
offset = off;
|
||||||
m->qid.vers++;
|
m->qid.vers++;
|
||||||
c->qid.vers++;
|
c->qid.vers++;
|
||||||
|
|
||||||
p = 0;
|
p = nil;
|
||||||
for(f = m->list; f; f = f->next) {
|
for(f = m->list; f != nil; f = f->next) {
|
||||||
if(f->start >= offset)
|
if(f->start >= offset)
|
||||||
break;
|
break;
|
||||||
p = f;
|
p = f;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(p != 0) {
|
if(p != nil) {
|
||||||
ee = p->start+p->len;
|
ee = p->start+p->len;
|
||||||
eo = offset - p->start;
|
eo = offset - p->start;
|
||||||
/* pack in predecessor if there is space */
|
/* pack in predecessor if there is space */
|
||||||
|
@ -577,7 +571,7 @@ cwrite(Chan* c, uchar *buf, int len, vlong off)
|
||||||
|
|
||||||
/* free the overlap -- it's a rare case */
|
/* free the overlap -- it's a rare case */
|
||||||
eblock = offset+len;
|
eblock = offset+len;
|
||||||
while(f && f->start < eblock) {
|
while(f != nil && f->start < eblock) {
|
||||||
e = f->next;
|
e = f->next;
|
||||||
extentfree(f);
|
extentfree(f);
|
||||||
f = e;
|
f = e;
|
||||||
|
@ -585,12 +579,12 @@ cwrite(Chan* c, uchar *buf, int len, vlong off)
|
||||||
|
|
||||||
/* link the block (if any) into the middle */
|
/* link the block (if any) into the middle */
|
||||||
e = cchain(buf, offset, len, &tail);
|
e = cchain(buf, offset, len, &tail);
|
||||||
if(e != 0) {
|
if(e != nil) {
|
||||||
tail->next = f;
|
tail->next = f;
|
||||||
f = e;
|
f = e;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(p == 0)
|
if(p == nil)
|
||||||
m->list = f;
|
m->list = f;
|
||||||
else
|
else
|
||||||
p->next = f;
|
p->next = f;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue