shithub: gefs

Download patch

ref: f1deaa515eea5586672c43158af2fa2c451e25ef
parent: 2471989852ff16ab9c1882e659d98c72d242afd1
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Oct 28 18:15:43 EDT 2023

blk: log compression is now synchronous

simpler, faster, easier, and less dangerous.

--- a/blk.c
+++ b/blk.c
@@ -502,24 +502,19 @@
 	 */
 	sz = 0;
 	nr = 0;
-	lock(a);
 	for(r = (Arange*)avlmin(a->free); r != nil; r = (Arange*)avlnext(r)){
 		sz += (r->len == Blksz) ? 8 : 16;
 		nr++;
 	}
 	nl = (sz+Logspc-Logslop-1)/(Logspc - Logslop);
-	if((log = malloc(nl*sizeof(vlong))) == nil){
-		unlock(a);
+	if((log = malloc(nl*sizeof(vlong))) == nil)
 		return -1;
-	}
 	if((rng = malloc(nr*sizeof(Range))) == nil){
-		unlock(a);
 		free(log);
 		return -1;
 	}
 	for(i = 0; i < nl; i++){
 		if((log[i] = blkalloc_lk(a, 1)) == -1){
-			unlock(a);
 			free(log);
 			free(rng);
 			return -1;
@@ -531,7 +526,6 @@
 		rng[nr].len = r->len;
 		nr++;
 	}
-	unlock(a);
 
 	n = 0;
 	hd = nil;
@@ -567,12 +561,10 @@
 	if(syncblk(tl) == -1)
 		return -1;
 
-	lock(a);
 	oldhd = a->loghd.addr;
 	a->loghd.addr = hd->bp.addr;
 	a->loghd.hash = hd->bp.hash;
 	a->loghd.gen = -1;
-	unlock(a);
 
 	if(oldhd != -1){
 		for(ba = oldhd; ba != -1 && ba != graft; ba = na){
@@ -594,19 +586,13 @@
 					break;
 				}
 			}
-			lock(a);
 			cachedel(b->bp.addr);
-			if(blkdealloc_lk(a, ba) == -1){
-				unlock(a);
+			if(blkdealloc_lk(a, ba) == -1)
 				return -1;
-			}
 			dropblk(b);
-			unlock(a);
 		}
 	}
-	lock(a);
 	a->nlog = 0;
-	unlock(a);
 	finalize(a->logtl);
 	if(syncblk(a->logtl) == -1)
 		return -1;
@@ -688,9 +674,9 @@
 	int r;
 
 	a = getarena(b);
- 	lock(a);
+ 	qlock(a);
 	r = blkdealloc_lk(a, b);
-	unlock(a);
+	qunlock(a);
 	return r;
 }
 
@@ -721,18 +707,19 @@
 	 * correctly.
 	 */
 	tries++;
-	lock(a);
+	if(canqlock(a) == 0)
+		goto Again;
 	if((b = blkalloc_lk(a, 0)) == -1){
-		unlock(a);
+		qunlock(a);
 		goto Again;
 	}
 	if(logappend(a, b, Blksz, LogAlloc) == -1){
-		unlock(a);
+		qunlock(a);
 		return -1;
 	}
 	if(a->loghd.addr == -1)
 		a->loghd = a->logtl->bp;
-	unlock(a);
+	qunlock(a);
 	return b;
 }
 
@@ -1139,12 +1126,12 @@
 		qe = qpop(q);
 		if(qe.op == Qfree){
 			a = getarena(qe.bp.addr);
-			lock(a);
+			qlock(a);
 			cachedel(qe.bp.addr);
 			blkdealloc_lk(a, qe.bp.addr);
 			if(qe.b != nil)
 				dropblk(qe.b);
-			unlock(a);
+			qunlock(a);
 		}else if(qe.op == Qflush){
 			qlock(&fs->synclk);
 			if(--fs->syncing == 0)
@@ -1193,12 +1180,12 @@
 	/* pass 0: sync arena contents */
 	for(i = 0; i < fs->narena; i++){
 		a = &fs->arenas[i];
-		lock(a);
+		qlock(a);
 		syncbarrier(a, gen);
 		finalize(a->logtl);
 		if(syncblk(a->logtl) == -1)
 			sysfatal("sync arena: %r");
-		unlock(a);
+		qunlock(a);
 	}
 	/*
 	 * pass 1: sync block headers; if we crash here,
@@ -1207,12 +1194,12 @@
          */
 	for(i = 0; i < fs->narena; i++){
 		a = &fs->arenas[i];
-		lock(a);
+		qlock(a);
 		packarena(a->hd->data, Blksz, a);
 		finalize(a->hd);
 		if(syncblk(a->hd) == -1)
 			sysfatal("sync arena: %r");
-		unlock(a);
+		qunlock(a);
 	}
 	/*
 	 * pass 2: sync superblock; we have a consistent
@@ -1239,12 +1226,12 @@
          */
 	for(i = 0; i < fs->narena; i++){
 		a = &fs->arenas[i];
-		lock(a);
+		qlock(a);
 		packarena(a->tl->data, Blksz, a);
 		finalize(a->tl);
 		if(syncblk(a->tl) == -1)
 			sysfatal("sync arena: %r");
-		unlock(a);
+		qunlock(a);
 	}
 	qunlock(&fs->synclk);
 }
--- a/cons.c
+++ b/cons.c
@@ -164,10 +164,10 @@
 	used = 0;
 	for(i = 0; i < fs->narena; i++){
 		a = &fs->arenas[i];
-		lock(a);
+		qlock(a);
 		size += a->size;
 		used += a->used;
-		unlock(a);
+		qunlock(a);
 	}
 	pct = 100.0*(double)used/(double)size;
 	fprint(fd, "used: %lld / %lld (%.2f%%)\n", used, size, pct);
--- a/dat.h
+++ b/dat.h
@@ -553,7 +553,7 @@
 };
 
 struct Arena {
-	Lock;
+	QLock;
 	Avltree *free;
 	Blk	**queue;
 	int	nqueue;
--- a/fs.c
+++ b/fs.c
@@ -2320,13 +2320,13 @@
 		qlock(&fs->synclk);
 		for(i = 0; i < fs->narena; i++){
 			epochstart(id);
-			lock(&fs->arenas[i]);
+			qlock(&fs->arenas[i]);
 			c = fs->arenas[i].nlog > fs->arenas[i].reserve/(4*Blksz);
-			unlock(&fs->arenas[i]);
 			if(c){
 				if(compresslog(&fs->arenas[i]) == -1)
 					fprint(2, "compress log: %r");
 			}
+			qunlock(&fs->arenas[i]);
 			epochend(id);
 		}
 		qunlock(&fs->synclk);