shithub: gefs

Download patch

ref: 104aa29f9fe37221d10e4cbaea1d8a8dd6b0d2ca
parent: 51913242c311c702c49e2a47c6862249df860a01
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Dec 31 14:10:15 EST 2021

blk: make flags atomic

this simplifies the code and removes a lock
that we don't care about.

--- a/blk.c
+++ b/blk.c
@@ -19,6 +19,32 @@
 
 QLock blklock;
 
+void
+setflag(Blk *b, int flg)
+{
+	long ov, nv;
+
+	while(1){
+		ov = b->flag;
+		nv = ov | flg;
+		if(cas(&b->flag, ov, nv))
+			break;
+	}
+}
+
+void
+clrflag(Blk *b, int flg)
+{
+	long ov, nv;
+
+	while(1){
+		ov = b->flag;
+		nv = ov & ~flg;
+		if(cas(&b->flag, ov, nv))
+			break;
+	}
+}
+
 Blk*
 readblk(vlong bp, int flg)
 {
@@ -39,7 +65,6 @@
 		off += n;
 		rem -= n;
 	}
-	memset(&b->Lock, 0, sizeof(Lock));
 	b->type = (flg&GBraw) ? Traw : GBIT16(b->buf+0);
 	b->bp.addr = bp;
 	b->bp.hash = -1;
@@ -278,7 +303,7 @@
 	Oplog *l;
 	int i;
 
-fprint(2, "deadlisted %B [%p::%B]\n", bp, t, t->bp);
+dprint("deadlisted %B [%p::%B]\n", bp, t, t->bp);
 //fprint(2, "predeadlist\n");
 //showtreeroot(2, t);
 //fprint(2, "----\n");
@@ -453,8 +478,8 @@
 		return -1;
 	if((b = mallocz(sizeof(Blk), 1)) == nil)
 		return -1;
+	setflag(b, Bdirty);
 	b->type = Tlog;
-	b->flag = Bdirty;
 	b->bp.addr = bp;
 	b->ref = 1;
 	b->data = b->buf + Hdrsz;
@@ -681,7 +706,7 @@
 	b->data = b->buf + Hdrsz;
 	b->fnext = nil;
 
-	b->flag = Bdirty;
+	setflag(b, Bdirty);
 	b->nval = 0;
 	b->valsz = 0;
 	b->nbuf = 0;
@@ -713,9 +738,7 @@
 
 	assert(b->type == Tsuper);
 	p = b->data;
-	lock(b);
-	b->flag |= Bdirty;
-	unlock(b);
+	setflag(b, Bdirty);
 	memcpy(p, "gefs0001", 8); p += 8;
 	PBIT32(p, 0); p += 4; /* dirty */
 	PBIT32(p, Blksz); p += 4;
@@ -736,8 +759,7 @@
 {
 	vlong h;
 
-	lock(b);
-	b->flag |= Bfinal;
+	setflag(b, Bfinal);
 	if(b->type != Traw)
 		PBIT16(b->buf, b->type);
 	switch(b->type){
@@ -769,7 +791,6 @@
 	case Tarena:
 		break;
 	}
-	unlock(b);
 }
 
 Blk*
@@ -867,10 +888,8 @@
 void
 freeblk(Tree *t, Blk *b)
 {
-	lock(b);
-	assert((b->flag & Bqueued) == 0);
+	assert(!(b->flag & Bqueued));
 	b->freed = getcallerpc(&b);
-	unlock(b);
 	freebp(t, b->bp);
 }
 
--- a/cache.c
+++ b/cache.c
@@ -40,7 +40,7 @@
 		fs->chead = b->cnext;
 	b->cnext = nil;
 	b->cprev = nil;
-	b->flag &= ~Bcached;
+	clrflag(b, Bcached);
 	fs->ccount--;
 }
 
@@ -82,10 +82,8 @@
 	b->cnext = fs->chead;
 	b->cprev = nil;
 	fs->chead = b;
-	if((b->flag&Bcached) == 0){
-		lock(b);
-		b->flag |= Bcached;
-		unlock(b);
+	if((b->flag & Bcached) == 0){
+		setflag(b, Bcached);
 		fs->ccount++;
 		refblk(b);
 	}
--- a/dat.h
+++ b/dat.h
@@ -535,8 +535,6 @@
 };
 
 struct Blk {
-	Lock;
-
 	/* cache entry */
 	Blk	*cnext;
 	Blk	*cprev;
@@ -545,7 +543,7 @@
 	/* Freelist entry */
 	Blk	*fnext;
 
-	short	flag;
+	long	flag;
 
 	/* serialized to disk in header */
 	short	type;	/* @0, for all */
--- a/fns.h
+++ b/fns.h
@@ -62,7 +62,7 @@
 void	btdone(Scan*);
 
 void	setflag(Blk *b, int);
-int	chkflag(Blk *b, int);
+void	clrflag(Blk *b, int);
 
 char*	estrdup(char*);
 
@@ -137,4 +137,4 @@
 /* it's in libc... */
 extern int cas(long*, long, long);
 extern int cas64(u64int*, u64int, u64int);
-uvlong	inc64(uvlong*, uvlong);
\ No newline at end of file
+uvlong	inc64(uvlong*, uvlong);
--- a/pack.c
+++ b/pack.c
@@ -362,9 +362,7 @@
 		tlhash = -1;
 		if(t->dead[i].tail != nil){
 			tl = t->dead[i].tail;
-			lock(tl);
 			assert(tl->flag & Bfinal);
-			unlock(tl);
 			tladdr = tl->bp.addr;
 			tlhash = tl->bp.hash;
 		}
--- a/ream.c
+++ b/ream.c
@@ -101,7 +101,7 @@
 	b->bp.addr = addr;
 	b->logsz = 32;
 	b->data = b->buf + Hdrsz;
-	b->flag |= Bdirty;
+	setflag(b, Bdirty);
 
 	p = b->data+Loghdsz;
 	PBIT64(p+ 0, addr|LogFree);		/* addr */
--- a/snap.c
+++ b/snap.c
@@ -23,9 +23,7 @@
 syncblk(Blk *b)
 {
 	assert(b->flag & Bfinal);
-	lock(b);
-	b->flag &= ~(Bqueued|Bdirty);
-	unlock(b);
+	clrflag(b, Bqueued|Bdirty);
 	return pwrite(fs->fd, b->buf, Blksz, b->bp.addr);
 }
 
@@ -32,7 +30,7 @@
 void
 enqueue(Blk *b)
 {
-	assert(b->flag&Bdirty);
+	assert(b->flag & Bdirty);
 	finalize(b);
 	if(syncblk(b) == -1){
 		ainc(&fs->broken);
@@ -199,7 +197,7 @@
 void
 freedead(Bptr bp, void *)
 {
-	fprint(2, "reclaimed deadlist: %B\n", bp);
+	dprint("reclaimed deadlist: %B\n", bp);
 	reclaimblk(bp);
 }