shithub: gefs

Download patch

ref: e02c31e598543f835be60a8fb47c4e20347bb5ef
parent: 051a5ba0a913455e2f0dc40e09aa5295be06466a
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Sep 27 23:09:19 EDT 2021

refactor: move to Bptr struct

--- a/blk.c
+++ b/blk.c
@@ -716,8 +716,8 @@
 	PBIT32(p + 16, Bufspc);
 	PBIT32(p + 20, Hdrsz);
 	PBIT32(p + 24, fs->root.ht);
-	PBIT64(p + 32, fs->root.bp);
-	PBIT64(p + 40, fs->root.bh);
+	PBIT64(p + 32, fs->root.bp.addr);
+	PBIT64(p + 40, fs->root.bp.hash);
 	PBIT32(p + 48, fs->narena);
 	PBIT64(p + 56, fs->arenasz);
 	PBIT64(p + 64, fs->gen);
@@ -759,19 +759,19 @@
 }
 
 Blk*
-getblk(vlong bp, uvlong bh, int flg)
+getblk(Bptr bp, int flg)
 {
 	Blk *b;
 
-	if((b = lookupblk(bp)) == nil){
-		if((b = readblk(bp, flg)) == nil)
+	if((b = lookupblk(bp.addr)) == nil){
+		if((b = readblk(bp.addr, flg)) == nil)
 			return nil;
-		if(blkhash(b) != bh){
-			werrstr("corrupt block %llx: %llx != %llx", bp, blkhash(b), bh);
+		if(blkhash(b) != bp.hash){
+			werrstr("corrupt block %B: %llx != %llx", bp, blkhash(b), bp.hash);
 			return nil;
 		}
 	}
-	assert(b->off == bp);
+	assert(b->off == bp.addr);
 	return cacheblk(b);
 }
 
--- a/check.c
+++ b/check.c
@@ -57,11 +57,11 @@
 			fail++;
 		}
 		if(b->type == Tpivot){
-			if(isfree(x.bp)){
-				fprint(2, "freed block in use: %llx\n", x.bp);
+			if(isfree(x.bp.addr)){
+				fprint(2, "freed block in use: %llx\n", x.bp.addr);
 				fail++;
 			}
-			if((c = getblk(x.bp, x.bh, 0)) == nil){
+			if((c = getblk(x.bp, 0)) == nil){
 				fprint(2, "corrupt block: %r\n");
 				fail++;
 				continue;
@@ -158,7 +158,7 @@
 		getval(b, i, &kv);
 		fprint(fd, "%.*s[%03d]|%P\n", 4*indent, spc, i, &kv);
 		if(b->type == Tpivot){
-			if((c = getblk(kv.bp, kv.bh, 0)) == nil)
+			if((c = getblk(kv.bp, 0)) == nil)
 				sysfatal("failed load: %r");
 			if(recurse)
 				rshowblk(fd, c, indent + 1, 1);
--- a/dat.h
+++ b/dat.h
@@ -16,6 +16,7 @@
 typedef struct Bucket	Bucket;
 typedef struct Chan	Chan;
 typedef struct Tree	Tree;
+typedef struct Bptr	Bptr;
 
 enum {
 	KiB	= 1024ULL,
@@ -233,10 +234,14 @@
 	uchar	buf[];
 };
 
+struct Bptr {
+	vlong	addr;
+	vlong	hash;
+};
+
 struct Tree {
 	Lock	lk;
-	vlong	bp;
-	vlong	bh;
+	Bptr	bp;
 	int	ht;
 };
 
@@ -310,8 +315,7 @@
 	union {
 		/* block pointer */
 		struct {
-			uvlong	bp;
-			uvlong	bh;
+			Bptr	bp;
 			ushort	fill;
 		};
 		/* inline values */
--- a/fns.h
+++ b/fns.h
@@ -2,7 +2,7 @@
 #pragma varargck type "P"	Kvp*
 #pragma varargck type "K"	Key*
 #pragma varargck type "V"	Val*
-#pragma varargck type "B"	Blk*
+#pragma varargck type "B"	Bptr
 #pragma varargck type "R"	Arange*
 #pragma varargck type "X"	char*
 
@@ -12,7 +12,7 @@
 Blk*	newblk(int type);
 Blk*	shadow(Blk*, Path*, Path*);
 Blk*	getroot(Tree*, int*);
-Blk*	getblk(vlong, uvlong, int);
+Blk*	getblk(Bptr, int);
 Blk*	pinblk(Blk*);
 Blk*	readblk(vlong, int);
 Arena*	getarena(vlong);
--- a/fs.c
+++ b/fs.c
@@ -43,10 +43,10 @@
 	char *e;
 	Blk *b;
 
-	if(f->root.bp == -1)
+	if(f->root.bp.addr == -1)
 		b = getroot(&fs->root, nil);
 	else
-		b = getblk(f->root.bp, f->root.bh, 0);
+		b = getblk(f->root.bp, 0);
 	if(b == nil)
 		return Efs;
 	if(lk)
@@ -412,8 +412,8 @@
 	f.fid = NOFID;
 	f.qpath = d.qid.path;
 	f.mode = -1;
-	f.root.bh = -1;
-	f.root.bp = -1;
+	f.root.bp.addr = -1;
+	f.root.bp.hash = -1;
 	f.iounit = iounit;
 	f.dent = e;
 	if(dupfid(m->fid, &f) == nil){
@@ -493,7 +493,7 @@
 	}
 	if(i > 0){
 		d.name = m->wname[i-1];
-		dent = getdent(f->root.bp, up, &d);
+		dent = getdent(f->root.bp.addr, up, &d);
 		if(dent == nil){
 			if(m->fid != m->newfid)
 				clunkfid(f);
@@ -603,13 +603,11 @@
 		rerror(m, "%r");
 		return;
 	}
-//showfs("precreate");
 	if(btupsert(&fs->root, &mb, 1) == -1){
 		rerror(m, "%r");
 		return;
 	}
-//showfs("postcreate");
-	dent = getdent(f->root.bp, f->qpath, &d);
+	dent = getdent(f->root.bp.addr, f->qpath, &d);
 	if(dent == nil){
 		if(m->fid != m->newfid)
 			clunkfid(f);
@@ -753,7 +751,7 @@
 
 		pfx[0] = Kent;
 		PBIT64(pfx+1, f->qpath);
-		t = (f->root.bp != -1) ? &f->root : &fs->root;
+		t = (f->root.bp.addr != -1) ? &f->root : &fs->root;
 		if((e = btscan(t, s, pfx, sizeof(pfx))) != nil){
 			free(r->data);
 			btdone(s);
@@ -801,7 +799,8 @@
 readb(Fid *f, char *d, vlong o, vlong n, int sz)
 {
 	char *e, buf[17];
-	vlong fb, fo, bp, bh;
+	vlong fb, fo;
+	Bptr bp;
 	Blk *b;
 	Key k;
 	Kvp kv;
@@ -825,11 +824,11 @@
 		return -1;
 	}
 	fprint(2, "\treadb: key=%K, val=%P\n", &k, &kv);
-	bp = GBIT64(kv.v+0);
-	bh = GBIT64(kv.v+8);
+	bp.addr = GBIT64(kv.v+0);
+	bp.hash = GBIT64(kv.v+8);
 	putblk(b);
 
-	if((b = getblk(bp, bh, GBraw)) == nil)
+	if((b = getblk(bp, GBraw)) == nil)
 		return -1;
 	if(fo+n > Blksz)
 		n = Blksz-fo;
@@ -921,7 +920,9 @@
 int
 writeb(Fid *f, Msg *m, char *s, vlong o, vlong n, vlong sz)
 {
-	vlong fb, fo, bp, bh;
+	vlong fb, fo;
+	uvlong bh;
+	Bptr bp;
 	Blk *b, *t;
 	Kvp kv;
 
@@ -942,11 +943,11 @@
 			putblk(b);
 			return -1;
 		}
-		bp = GBIT64(kv.v+0);
-		bh = GBIT64(kv.v+8);
+		bp.addr = GBIT64(kv.v+0);
+		bp.hash = GBIT64(kv.v+8);
 		putblk(t);
 
-		if((t = getblk(bp, bh, GBraw)) == nil){
+		if((t = getblk(bp, GBraw)) == nil){
 			putblk(b);
 			return -1;
 		}
--- a/load.c
+++ b/load.c
@@ -63,8 +63,8 @@
 	if(GBIT32(p + 20) != Hdrsz)
 		sysfatal("fs uses different buffer size");
 	fs->root.ht = GBIT32(p + 24);
-	fs->root.bp = GBIT64(p + 32);
-	fs->root.bh = GBIT64(p + 40);
+	fs->root.bp.addr = GBIT64(p + 32);
+	fs->root.bp.hash = GBIT64(p + 40);
 	fs->narena = GBIT32(p + 48);
 	fs->arenasz = GBIT64(p + 56);
 	fs->arenasz = GBIT64(p + 56);
@@ -73,8 +73,7 @@
 	fs->super = b;
 	fprint(2, "load: %8s\n", p);
 	fprint(2, "\theight:\t%d\n", fs->root.ht);
-	fprint(2, "\trootb:\t%llx\n", fs->root.bp);
-	fprint(2, "\trooth:\t%llx\n", fs->root.bh);
+	fprint(2, "\trootb:\t%B\n", fs->root.bp);
 	fprint(2, "\tarenas:\t%d\n", fs->narena);
 	fprint(2, "\tarenasz:\t%lld\n", fs->arenasz);
 	fprint(2, "\trootgen:\t%lld\n", fs->gen);
--- a/main.c
+++ b/main.c
@@ -17,12 +17,10 @@
 static int
 Bconv(Fmt *fmt)
 {
-	Blk *b;
+	Bptr bp;
 
-	b = va_arg(fmt->args, Blk*);
-	if(b == nil)
-		return fmtprint(fmt, "Blk(nil)");
-	return fmtprint(fmt, "Blk(%c)", (b->type == Tpivot) ? 'P' : 'L');
+	bp = va_arg(fmt->args, Bptr);
+	return fmtprint(fmt, "(%llx,%llx)", bp.addr, bp.hash);
 }
 
 void
@@ -69,9 +67,8 @@
 			kv->nk, kv->nk, kv->k,
 			kv->nv, kv->nv, kv->v);
 	else
-		return fmtprint(fmt, "Kvp([%d]%.*X,(%llux,%llux,%ud))",
-			kv->nk, kv->nk, kv->k,
-			kv->bp, kv->bh, kv->fill);
+		return fmtprint(fmt, "Kvp([%d]%.*X,(%B,%ud))",
+			kv->nk, kv->nk, kv->k, kv->bp, kv->fill);
 }
 
 static int
--- a/ream.c
+++ b/ream.c
@@ -132,7 +132,6 @@
 	finalize(s);
 	syncblk(s);
 
-print("superblock @%llx\n", s->off);
 	for(i = 0; i < fs->narena; i++)
 		if((loadarena(&fs->arenas[i], i*asz)) == -1)
 			sysfatal("ream: loadarena: %r");
@@ -149,8 +148,8 @@
 	syncblk(r);
 
 	fs->super = s;
-	fs->root.bp = r->off;
-	fs->root.bh = blkhash(r);
+	fs->root.bp.addr = r->off;
+	fs->root.bp.hash = blkhash(r);
 	fs->root.ht = 1;
 	snapshot();
 
--- a/tree.c
+++ b/tree.c
@@ -26,7 +26,6 @@
 		dst->nv = src->nv;
 	}else{
 		dst->bp = src->bp;
-		dst->bh = src->bh;
 		dst->fill = src->fill;
 	}
 	memcpy(dst->v, src->v, src->nv);
@@ -81,8 +80,8 @@
 		kv->type = Vref;
 		kv->nk = GBIT16(b->data + o);
 		kv->k = b->data + o + 2;
-		kv->bp = GBIT64(kv->k + kv->nk + 0);
-		kv->bh = GBIT64(kv->k + kv->nk + 8);
+		kv->bp.addr = GBIT64(kv->k + kv->nk + 0);
+		kv->bp.hash = GBIT64(kv->k + kv->nk + 8);
 		kv->fill = GBIT16(kv->k + kv->nk + 16);
 	}else{
 		kv->type = Vinl;
@@ -143,8 +142,8 @@
 		PBIT16(b->data + 2*i, o);
 		PBIT16(p +  0, kv->nk);
 		memcpy(p +  2, kv->k, kv->nk);
-		PBIT64(p + kv->nk +  2, kv->bp);
-		PBIT64(p + kv->nk + 10, kv->bh);
+		PBIT64(p + kv->nk +  2, kv->bp.addr);
+		PBIT64(p + kv->nk + 10, kv->bp.hash);
 		PBIT16(p + kv->nk + 18, kv->fill);
 	} else {
 		PBIT16(b->data + 2*i, o);
@@ -375,8 +374,8 @@
 		if(pp->l->nval > 0){
 			getval(pp->l, 0, &kv);
 			kv.type = Vref;
-			kv.bp = pp->l->off;
-			kv.bh = blkhash(pp->l);
+			kv.bp.addr = pp->l->off;
+			kv.bp.hash = blkhash(pp->l);
 			kv.fill = blkfill(pp->l);
 			setval(n, i++, &kv, 0);
 			if(nbytes != nil)
@@ -385,8 +384,8 @@
 		if(pp->r->nval > 0){
 			getval(pp->r, 0, &kv);
 			kv.type = Vref;
-			kv.bp = pp->r->off;
-			kv.bh = blkhash(pp->r);
+			kv.bp.addr = pp->r->off;
+			kv.bp.hash = blkhash(pp->r);
 			kv.fill = blkfill(pp->r);
 			setval(n, i++, &kv, 0);
 			if(nbytes != nil)
@@ -396,8 +395,8 @@
 		if(pp->n->nval > 0){
 			getval(pp->n, 0, &kv);
 			kv.type = Vref;
-			kv.bp = pp->n->off;
-			kv.bh = blkhash(pp->n);
+			kv.bp.addr = pp->n->off;
+			kv.bp.hash = blkhash(pp->n);
 			kv.fill = blkfill(pp->n);
 			setval(n, i++, &kv, 1);
 			if(nbytes != nil)
@@ -438,15 +437,15 @@
 		}else if(i == midx){
 			getval(p->nl, 0, &m);
 			m.type = Vref;
-			m.bp = p->nl->off;
-			m.bh = blkhash(p->nl);
+			m.bp.addr = p->nl->off;
+			m.bp.hash = blkhash(p->nl);
 			m.fill = blkfill(p->nl);
 			setval(n, j++, &m, 0);
 			if(p->nr){
 				getval(p->nr, 0, &m);
 				m.type = Vref;
-				m.bp = p->nr->off;
-				m.bh = blkhash(p->nr);
+				m.bp.addr = p->nr->off;
+				m.bp.hash = blkhash(p->nr);
 				m.fill = blkfill(p->nr);
 				setval(n, j++, &m, 0);
 				i++;
@@ -817,7 +816,7 @@
 		if((m = pp->n) == nil)
 			return 0;
 	}else{
-		if((m = getblk(km.bp, km.bh, 0)) == nil)
+		if((m = getblk(km.bp, 0)) == nil)
 			return -1;
 	}
 	/* Try merging left */
@@ -826,7 +825,7 @@
 		getval(p->b, idx-1, &kl);
 		if(kl.fill + km.fill >= Blkspc)
 			goto next;
-		if((l = getblk(kl.bp, kl.bh, 0)) == nil)
+		if((l = getblk(kl.bp, 0)) == nil)
 			goto out;
 		if(rotmerge(p, idx-1, l, m) == -1)
 			goto out;
@@ -837,7 +836,7 @@
 		getval(p->b, idx+1, &kr);
 		if(kr.fill + km.fill >= Blkspc)
 			goto done;
-		if((r = getblk(kr.bp, kr.bh, 0)) == nil)
+		if((r = getblk(kr.bp, 0)) == nil)
 			goto out;
 		if(rotmerge(p, idx, m, r) == -1)
 			goto out;
@@ -1090,7 +1089,7 @@
 			break;
 		victim(b, &path[npath]);
 		getval(b, path[npath].idx, &sep);
-		b = getblk(sep.bp, sep.bh, 0);
+		b = getblk(sep.bp, 0);
 		if(b == nil)
 			goto error;
 		npath++;
@@ -1122,8 +1121,8 @@
 	rh = blkhash(rb);
 	lock(&t->lk);
 	t->ht += dh;
-	t->bp = rb->off;
-	t->bh = rh;
+	t->bp.addr = rb->off;
+	t->bp.hash = rh;
 	unlock(&t->lk);
 
 	freepath(path, npath);
@@ -1145,15 +1144,15 @@
 Blk*
 getroot(Tree *t, int *h)
 {
-	vlong bp, bh;
+	Bptr bp;
 
 	lock(&t->lk);
 	bp = t->bp;
-	bh = t->bh;
 	if(h != nil)
 		*h = t->ht;
 	unlock(&t->lk);
-	return getblk(bp, bh, 0);
+
+	return getblk(t->bp, 0);
 }
 
 static char*
@@ -1207,7 +1206,7 @@
 		if(idx == -1)
 			return Eexist;
 		putblk(b);
-		if((b = getblk(r->bp, r->bh, 0)) == nil)
+		if((b = getblk(r->bp, 0)) == nil)
 			return Efs;
 	}
 	assert(b->type == Tleaf);
@@ -1263,7 +1262,7 @@
 	}
 
 	p = s->path;
-	if((b = getblk(s->root.bp, s->root.bh, 0)) == nil)
+	if((b = getblk(s->root.bp, 0)) == nil)
 		return "error reading block";
 	p[0].b = b;
 	for(i = 0; i < s->root.ht; i++){
@@ -1274,7 +1273,7 @@
 			p[i].bi = bufsearch(b, &s->kv, &m, &same);
 			if(p[i].bi == -1 || !same)
 				p[i].bi++;
-			if((b = getblk(v.bp, v.bh, 0)) == nil)
+			if((b = getblk(v.bp, 0)) == nil)
 				return "error readivg block";
 			p[i+1].b = b;
 		}else{
@@ -1364,7 +1363,7 @@
 	}
 	for(i = start; i < h; i++){
 		getval(p[i-1].b, p[i-1].vi, &kv);
-		if((p[i].b = getblk(kv.bp, kv.bh, 0)) == nil)
+		if((p[i].b = getblk(kv.bp, 0)) == nil)
 			return "error reading block";
 	}