changelog shortlog tags branches changeset file revisions annotate raw help

Mercurial > hg > plan9front / sys/src/9/port/cache.c

revision 4350: 1f9d7811d546
parent 4074: a7cf12962171
child 4594: 047fb2d18851
     1.1--- a/sys/src/9/port/cache.c
     1.2+++ b/sys/src/9/port/cache.c
     1.3@@ -205,6 +205,11 @@ ccache(Chan *c)
     1.4 	return nil;
     1.5 }
     1.6 
     1.7+enum {
     1.8+	VABITS	= 8*sizeof(uintptr) - 2*PGSHIFT,
     1.9+	VAMASK	= (((uintptr)1 << VABITS)-1) << PGSHIFT,
    1.10+};
    1.11+
    1.12 static Page*
    1.13 cpage(Mntcache *m, ulong pn, ulong *po, ulong *pe)
    1.14 {
    1.15@@ -219,12 +224,19 @@ cpage(Mntcache *m, ulong pn, ulong *po, 
    1.16 		m->bitmap[pn/MAPBITS] &= ~b;
    1.17 		return nil;
    1.18 	}
    1.19-	/* see cachedata() below */
    1.20-	*po = (ulong)p->va & (BY2PG-1);
    1.21-	*pe = (ulong)p->va >> PGSHIFT;
    1.22+	*po = p->va & (BY2PG-1);
    1.23+	*pe = 1 + (p->va >> (PGSHIFT+VABITS));
    1.24+	assert(*po < *pe);
    1.25 	return p;
    1.26 }
    1.27 
    1.28+static void
    1.29+cpageset(Page *p, ulong po, ulong pe)
    1.30+{
    1.31+	assert(po < pe);
    1.32+	p->va = po | (p->va & VAMASK) | ((uintptr)pe - 1) << (PGSHIFT+VABITS);
    1.33+}
    1.34+
    1.35 int
    1.36 cread(Chan *c, uchar *buf, int len, vlong off)
    1.37 {
    1.38@@ -253,7 +265,7 @@ cread(Chan *c, uchar *buf, int len, vlon
    1.39 		p = cpage(m, pn, &po, &pe);
    1.40 		if(p == nil)
    1.41 			break;
    1.42-		if(po >= pe || offset < po || offset >= pe){
    1.43+		if(offset < po || offset >= pe){
    1.44 			putpage(p);
    1.45 			break;
    1.46 		}
    1.47@@ -326,8 +338,8 @@ cachedata(Mntcache *m, uchar *buf, int l
    1.48 			l = len;
    1.49 		p = cpage(m, pn, &po, &pe);
    1.50 		if(p != nil){
    1.51-			if(po >= pe || offset > pe || (offset+l) < po){
    1.52-				/* cached range empty or not extendable, set new cached range */
    1.53+			if(offset > pe || (offset+l) < po){
    1.54+				/* cached range not extendable, set new cached range */
    1.55 				po = offset;
    1.56 				pe = offset+l;
    1.57 			} else {
    1.58@@ -338,13 +350,12 @@ cachedata(Mntcache *m, uchar *buf, int l
    1.59 					pe = offset+l;
    1.60 			}
    1.61 		} else {
    1.62-			p = auxpage();
    1.63-			if(p == nil){
    1.64+			if(needpages(nil) || waserror()){
    1.65 				invalidate(m, offset + pn*BY2PG, len);
    1.66 				break;
    1.67 			}
    1.68-
    1.69-			p->va = 0;
    1.70+			p = newpage(0, nil, pn*BY2PG);
    1.71+			poperror();
    1.72 			p->daddr = cacheaddr(m, pn);
    1.73 			cachedel(&fscache, p->daddr);
    1.74 			cachepage(p, &fscache);
    1.75@@ -353,6 +364,7 @@ cachedata(Mntcache *m, uchar *buf, int l
    1.76 			po = offset;
    1.77 			pe = offset+l;
    1.78 		}
    1.79+		cpageset(p, po, pe);
    1.80 
    1.81 		k = kmap(p);
    1.82 		if(waserror()) {
    1.83@@ -365,9 +377,6 @@ cachedata(Mntcache *m, uchar *buf, int l
    1.84 		memmove((uchar*)VA(k) + offset, buf, l);
    1.85 		poperror();
    1.86 		kunmap(k);
    1.87-
    1.88-		/* update cached range */
    1.89-		p->va = po | (pe << PGSHIFT);
    1.90 		putpage(p);
    1.91 
    1.92 		offset = 0;