shithub: gefs

Download patch

ref: e62e48db2a4c71875f9e7ab44f8c11ea3b749a5f
parent: 3c0cb629fe2956046ab1aab819e1baf5b5993457
author: Ori Bernstein <[email protected]>
date: Mon Oct 17 19:15:05 EDT 2022

all: pack integers in big endian form for better lexical sorting

we do a lot of sequential scans in gefs, and cache locality benefits
quite a bit from keeping these buffers sequential.

--- a/blk.c
+++ b/blk.c
@@ -83,7 +83,7 @@
 	while(rem != 0){
 		n = pread(fs->fd, b->buf, rem, off);
 		if(n <= 0){
-			free(b);
+			dropblk(b);
 			return nil;
 		}
 		off += n;
@@ -94,7 +94,7 @@
 	b->hnext = nil;
 	b->flag = 0;
 
-	b->type = (flg&GBraw) ? Traw : GBIT16(b->buf+0);
+	b->type = (flg&GBraw) ? Traw : UNPACK16(b->buf+0);
 	b->bp.addr = bp;
 	b->bp.hash = -1;
 	b->bp.gen = -1;
@@ -123,15 +123,15 @@
 		break;
 	case Tpivot:
 		b->data = b->buf + Pivhdsz;
-		b->nval = GBIT16(b->buf+2);
-		b->valsz = GBIT16(b->buf+4);
-		b->nbuf = GBIT16(b->buf+6);
-		b->bufsz = GBIT16(b->buf+8);
+		b->nval = UNPACK16(b->buf+2);
+		b->valsz = UNPACK16(b->buf+4);
+		b->nbuf = UNPACK16(b->buf+6);
+		b->bufsz = UNPACK16(b->buf+8);
 		break;
 	case Tleaf:
 		b->data = b->buf + Leafhdsz;
-		b->nval = GBIT16(b->buf+2);
-		b->valsz = GBIT16(b->buf+4);
+		b->nval = UNPACK16(b->buf+2);
+		b->valsz = UNPACK16(b->buf+4);
 		break;
 	}
 	assert(b->magic == Magic);
@@ -276,8 +276,8 @@
 		cacheins(lb);
 		lb->logsz = Loghashsz;
 		p = lb->data + lb->logsz;
-		PBIT64(p+0, o|LogAlloc1);
-		PBIT64(p+8, (uvlong)LogEnd);
+		PACK64(p+0, o|LogAlloc1);
+		PACK64(p+8, (uvlong)LogEnd);
 		finalize(lb);
 		if(syncblk(lb) == -1){
 			dropblk(lb);
@@ -286,7 +286,7 @@
 
 		if(pb != nil){
 			p = pb->data + pb->logsz;
-			PBIT64(p, lb->bp.addr|LogChain);
+			PACK64(p, lb->bp.addr|LogChain);
 			finalize(pb);
 			if(syncblk(pb) == -1){
 				dropblk(pb);
@@ -305,10 +305,10 @@
 	}
 	off |= op;
 	p = lb->data + lb->logsz;
-	PBIT64(p, off);
+	PACK64(p, off);
 	lb->logsz += 8;
 	if(op >= Log2wide){
-		PBIT64(p+8, len);
+		PACK64(p+8, len);
 		lb->logsz += 8;
 	}
 	/*
@@ -325,7 +325,7 @@
 		logop(a, o, Blksz, LogAlloc);
 	/* this gets overwritten by the next append */
 	p = lb->data + lb->logsz;
-	PBIT64(p, (uvlong)LogEnd);
+	PACK64(p, (uvlong)LogEnd);
 	return 0;
 
 }
@@ -355,7 +355,7 @@
 Nextblk:
 	if((b = getblk(bp, GBnochk)) == nil)
 		return -1;
-	bh = GBIT64(b->data);
+	bh = UNPACK64(b->data);
 	/* the hash covers the log and offset */
 	if(bh != siphash(b->data+Loghashsz, Logspc-Loghashsz)){
 		werrstr("corrupt log");
@@ -363,7 +363,7 @@
 	}
 	for(i = Loghashsz; i < Logspc; i += n){
 		d = b->data + i;
-		ent = GBIT64(d);
+		ent = UNPACK64(d);
 		op = ent & 0xff;
 		off = ent & ~0xff;
 		n = (op >= Log2wide) ? 16 : 8;
@@ -383,7 +383,7 @@
 
 		case LogAlloc:
 		case LogAlloc1:
-			len = (op >= Log2wide) ? GBIT64(d+8) : Blksz;
+			len = (op >= Log2wide) ? UNPACK64(d+8) : Blksz;
 			dprint("log@%d alloc: %llx+%llx\n", i, off, len);
 			if(grabrange(a->free, off & ~0xff, len) == -1)
 				return -1;
@@ -390,7 +390,7 @@
 			break;
 		case LogFree:
 		case LogFree1:
-			len = (op >= Log2wide) ? GBIT64(d+8) : Blksz;
+			len = (op >= Log2wide) ? UNPACK64(d+8) : Blksz;
 			dprint("log@%d free: %llx+%llx\n", i, off, len);
 			if(freerange(a->free, off & ~0xff, len) == -1)
 				return -1;
@@ -436,7 +436,7 @@
 	b->logsz = Loghashsz;
 
 	p = b->data + b->logsz;
-	PBIT64(p, (uvlong)LogEnd);
+	PACK64(p, (uvlong)LogEnd);
 	finalize(b);
 	if(syncblk(b) == -1){
 		dropblk(b);
@@ -495,7 +495,7 @@
 			return -1;
 
 	p = tl->data + tl->logsz;
-	PBIT64(p, LogChain|graft);
+	PACK64(p, LogChain|graft);
 	free(log);
 	finalize(tl);
 	if(syncblk(tl) == -1)
@@ -517,7 +517,7 @@
 				return -1;
 			for(i = Loghashsz; i < Logspc; i += n){
 				p = b->data + i;
-				v = GBIT64(p);
+				v = UNPACK64(p);
 				n = ((v&0xff) >= Log2wide) ? 16 : 8;
 				if((v&0xff) == LogChain){
 					na = v & ~0xff;
@@ -727,25 +727,25 @@
 	uvlong h;
 
 	if(b->type != Traw)
-		PBIT16(b->buf, b->type);
+		PACK16(b->buf, b->type);
 	switch(b->type){
 	default:
 	case Tpivot:
-		PBIT16(b->buf+2, b->nval);
-		PBIT16(b->buf+4, b->valsz);
-		PBIT16(b->buf+6, b->nbuf);
-		PBIT16(b->buf+8, b->bufsz);
+		PACK16(b->buf+2, b->nval);
+		PACK16(b->buf+4, b->valsz);
+		PACK16(b->buf+6, b->nbuf);
+		PACK16(b->buf+8, b->bufsz);
 		b->bp.hash = blkhash(b);
 		break;
 	case Tleaf:
-		PBIT16(b->buf+2, b->nval);
-		PBIT16(b->buf+4, b->valsz);
+		PACK16(b->buf+2, b->nval);
+		PACK16(b->buf+4, b->valsz);
 		b->bp.hash = blkhash(b);
 		break;
 	case Tlog:
 	case Tdead:
 		h = siphash(b->data + Loghashsz, Logspc-Loghashsz);
-		PBIT64(b->data, h);
+		PACK64(b->data, h);
 		b->bp.hash = blkhash(b);
 		break;
 	case Traw:
@@ -772,6 +772,7 @@
 		qunlock(&fs->blklk[i]);
 		return b;
 	}
+	dprint("read %B from %#llx\n", bp, getcallerpc(&bp));
 	if((b = readblk(bp.addr, flg)) == nil){
 		qunlock(&fs->blklk[i]);
 		return nil;
--- a/dat.h
+++ b/dat.h
@@ -216,7 +216,7 @@
 	Tleaf,
 	Tlog,
 	Tdead,
-	Tarena = 0x6567,	/* 'ge' */
+	Tarena = 0x6765,	/* 'ge' bigendian */
 };
 
 enum {
--- a/dump.c
+++ b/dump.c
@@ -23,19 +23,19 @@
 		return fmtprint(fmt, "\"\"");
 	switch(k->k[0]){
 	case Kdat:	/* qid[8] off[8] => ptr[16]:	pointer to data page */
-		n = fmtprint(fmt, "dat qid:%llx off:%llx", GBIT64(k->k+1), GBIT64(k->k+9));
+		n = fmtprint(fmt, "dat qid:%llx off:%llx", UNPACK64(k->k+1), UNPACK64(k->k+9));
 		break;
 	case Kent:	/* pqid[8] name[n] => dir[n]:	serialized Dir */
-		n = fmtprint(fmt, "ent dir:%llx, name:\"%.*s\")", GBIT64(k->k+1), k->nk-11, k->k+11);
+		n = fmtprint(fmt, "ent dir:%llx, name:\"%.*s\")", UNPACK64(k->k+1), k->nk-11, k->k+11);
 		break;
 	case Klabel:	/* name[n] => tree[24]:	snapshot ref */
 		n = fmtprint(fmt, "label name:\"%.*s\"", k->nk-1, k->k+1);
 		break;
 	case Ksnap:	/* name[n] => tree[24]:	snapshot root */
-		n = fmtprint(fmt, "snap id:\"%llx\"", GBIT64(k->k+1));
+		n = fmtprint(fmt, "snap id:\"%llx\"", UNPACK64(k->k+1));
 		break;
 	case Ksuper:	/* qid[8] => pqid[8]:		parent dir */
-		n = fmtprint(fmt, "up dir:%llx", GBIT64(k->k+1));
+		n = fmtprint(fmt, "up dir:%llx", UNPACK64(k->k+1));
 		break;
 	default:
 		n = fmtprint(fmt, "%.*H", k->nk, k->k);
@@ -55,7 +55,7 @@
 	n = 0;
 	if(flg){
 		assert(v->nv == Ptrsz+2);
-		n = fmtprint(fmt, "(%B,%d)", unpackbp(v->v, v->nv), GBIT16(v->v+Ptrsz));
+		n = fmtprint(fmt, "(%B,%d)", unpackbp(v->v, v->nv), UNPACK16(v->v+Ptrsz));
 		return n;
 	}
 	if(op == Odelete || op == Oclearb){
@@ -93,31 +93,31 @@
 			p = v->v;
 			ws = *p++;
 			if(ws & Owsize){
-				n += fmtprint(fmt, "size:%llx ", GBIT64(p));
+				n += fmtprint(fmt, "size:%llx ", UNPACK64(p));
 				p += 8;
 			}
 			if(ws & Owmode){
-				n += fmtprint(fmt, "mode:%uo ", GBIT32(p));
+				n += fmtprint(fmt, "mode:%uo ", UNPACK32(p));
 				p += 4;
 			}
 			if(ws & Owmtime){
-				n += fmtprint(fmt, "mtime:%llx ", GBIT64(p));
+				n += fmtprint(fmt, "mtime:%llx ", UNPACK64(p));
 				p += 8;
 			}
 			if(ws & Owatime){
-				n += fmtprint(fmt, "mtime:%llx ", GBIT64(p));
+				n += fmtprint(fmt, "mtime:%llx ", UNPACK64(p));
 				p += 8;
 			}
 			if(ws & Owuid){
-				n += fmtprint(fmt, "uid:%d ", GBIT32(p));
+				n += fmtprint(fmt, "uid:%d ", UNPACK32(p));
 				p += 4;
 			}
 			if(ws & Owgid){
-				n += fmtprint(fmt, "gid:%d ", GBIT32(p));
+				n += fmtprint(fmt, "gid:%d ", UNPACK32(p));
 				p += 4;
 			}
 			if(ws & Owmuid){
-				n += fmtprint(fmt, "muid:%d ", GBIT32(p));
+				n += fmtprint(fmt, "muid:%d ", UNPACK32(p));
 				p += 4;
 			}
 			if(p != v->v + v->nv){
@@ -133,10 +133,10 @@
 		n = fmtprint(fmt, "ref: %d, ht: %d, bp: %B, prev=%lld", t.ref, t.ht, t.bp, t.dead[0].prev);
 		break;
 	case Klabel:
-		n = fmtprint(fmt, "snap id:\"%llx\"", GBIT64(v->v+1));
+		n = fmtprint(fmt, "snap id:\"%llx\"", UNPACK64(v->v+1));
 		break;
 	case Ksuper:	/* qid[8] => pqid[8]:		parent dir */
-		n = fmtprint(fmt, "super dir:%llx, name:\"%.*s\")", GBIT64(v->v+1), v->nv-11, v->v+11);
+		n = fmtprint(fmt, "super dir:%llx, name:\"%.*s\")", UNPACK64(v->v+1), v->nv-11, v->v+11);
 		break;
 	default:
 		n = fmtprint(fmt, "%.*H", v->nk, v->k);
@@ -384,7 +384,7 @@
 	if(na != 0){
 		sz = Snapsz;
 		id = atoll(ap[0]);
-		PBIT64(pfx+1, id);
+		PACK64(pfx+1, id);
 	}
 	if((e = btscan(&fs->snap, s, pfx, sz)) != nil){
 		fprint(fd, "scan: %s\n", e);
--- a/fns.h
+++ b/fns.h
@@ -11,6 +11,21 @@
 extern int	debug;
 extern char*	forceuser;
 
+#define	UNPACK8(p)	(((uchar*)(p))[0])
+#define	UNPACK16(p)	((((uchar*)(p))[0]<<8)|(((uchar*)(p))[1]))
+#define	UNPACK32(p)	((((uchar*)(p))[0]<<24)|(((uchar*)(p))[1]<<16)|\
+				(((uchar*)(p))[2]<<8)|(((uchar*)(p))[3]))
+#define	UNPACK64(p)	(((u64int)((((uchar*)(p))[0]<<24)|(((uchar*)(p))[1]<<16)|\
+				(((uchar*)(p))[2]<<8)|(((uchar*)(p))[3])))<<32 |\
+			((u64int)((((uchar*)(p))[4]<<24)|(((uchar*)(p))[5]<<16)|\
+				(((uchar*)(p))[6]<<8)|(((uchar*)(p))[7]))))
+
+#define	PACK8(p,v)	do{(p)[0]=(v);}while(0)
+#define	PACK16(p,v)	do{(p)[0]=(v)>>8;(p)[1]=(v);}while(0)
+#define	PACK32(p,v)	do{(p)[0]=(v)>>24;(p)[1]=(v)>>16;(p)[2]=(v)>>8;(p)[3]=(v);}while(0)
+#define	PACK64(p,v)	do{(p)[0]=(v)>>56;(p)[1]=(v)>>48;(p)[2]=(v)>>40;(p)[3]=(v)>>32;\
+			   (p)[4]=(v)>>24;(p)[5]=(v)>>16;(p)[6]=(v)>>8;(p)[7]=(v);}while(0)
+
 Blk*	newblk(int type);
 Blk*	dupblk(Blk*);
 Blk*	getroot(Tree*, int*);
--- a/fs.c
+++ b/fs.c
@@ -237,8 +237,8 @@
 		m.nk = sizeof(buf);
 		m.op = Oclearb;
 		m.k[0] = Kdat;
-		PBIT64(m.k+1, f->qpath);
-		PBIT64(m.k+9, o);
+		PACK64(m.k+1, f->qpath);
+		PACK64(m.k+9, o);
 		m.v = nil;
 		m.nv = 0;
 		if((e = btupsert(f->mnt->root, &m, 1)) != nil)
@@ -268,8 +268,8 @@
 	k.k = buf;
 	k.nk = sizeof(buf);
 	k.k[0] = Kdat;
-	PBIT64(k.k+1, f->qpath);
-	PBIT64(k.k+9, fb);
+	PACK64(k.k+1, f->qpath);
+	PACK64(k.k+9, fb);
 
 	e = lookup(f, &k, &kv, kvbuf, sizeof(kvbuf));
 	if(e != nil){
@@ -302,8 +302,8 @@
 	fo = o & (Blksz-1);
 
 	m->k[0] = Kdat;
-	PBIT64(m->k+1, f->qpath);
-	PBIT64(m->k+9, fb);
+	PACK64(m->k+1, f->qpath);
+	PACK64(m->k+9, fb);
 
 
 	b = newblk(Traw);
@@ -1109,7 +1109,7 @@
 		if(d.length != de->length){
 			n.length = d.length;
 			op |= Owsize;
-			PBIT64(p, n.length);
+			PACK64(p, n.length);
 			p += 8;
 		}
 	}
@@ -1126,7 +1126,7 @@
 			n.mode = d.mode;
 			n.qid.type = d.mode>>24;
 			op |= Owmode;
-			PBIT32(p, n.mode);
+			PACK32(p, n.mode);
 			p += 4;
 		}
 	}
@@ -1134,7 +1134,7 @@
 		n.mtime = d.mtime*Nsec;
 		if(n.mtime != de->mtime){
 			op |= Owmtime;
-			PBIT64(p, n.mtime);
+			PACK64(p, n.mtime);
 			p += 8;
 		}
 	}
@@ -1150,7 +1150,7 @@
 		runlock(&fs->userlk);
 		if(n.uid != de->uid){
 			op |= Owuid;
-			PBIT32(p, n.uid);
+			PACK32(p, n.uid);
 			p += 4;
 		}
 	}
@@ -1166,13 +1166,13 @@
 		runlock(&fs->userlk);
 		if(n.gid != de->gid){
 			op |= Owgid;
-			PBIT32(p, n.gid);
+			PACK32(p, n.gid);
 			p += 4;
 		}
 	}
 	op |= Owmuid;
 	n.muid = f->uid;
-	PBIT32(p, n.muid);
+	PACK32(p, n.muid);
 	p += 4;
 
 	/* check permissions */
@@ -1534,8 +1534,8 @@
 		mb.op = Owstat;
 		p = buf;
 		p[0] = Owsize|Owmuid;	p += 1;
-		PBIT64(p, 0);		p += 8;
-		PBIT32(p, f->uid);	p += 4;
+		PACK64(p, 0);		p += 8;
+		PACK32(p, f->uid);	p += 4;
 		mb.k = f->dent->k;
 		mb.nk = f->dent->nk;
 		mb.v = buf;
@@ -1800,16 +1800,16 @@
 	*p++ = 0;
 	if(n > f->dent->length){
 		sbuf[0] |= Owsize;
-		PBIT64(p, n);
+		PACK64(p, n);
 		p += 8;
 		f->dent->length = m->offset+m->count;
 	}
 	sbuf[0] |= Owmtime;
 	f->dent->mtime = nsec();
-	PBIT64(p, f->dent->mtime);
+	PACK64(p, f->dent->mtime);
 	p += 8;
 	sbuf[0] |= Owmuid;
-	PBIT32(p, f->uid);
+	PACK32(p, f->uid);
 	p += 4;
 
 	kv[i].v = sbuf;
--- a/pack.c
+++ b/pack.c
@@ -24,7 +24,7 @@
 		*err = 1;
 		return p;
 	}
-	*(ushort*)v = GBIT16(p);
+	*(ushort*)v = UNPACK16(p);
 	return p+2;
 }
 
@@ -35,7 +35,7 @@
 		*err = 1;
 		return p;
 	}
-	*(uint*)v = GBIT32(p);
+	*(uint*)v = UNPACK32(p);
 	return p+4;
 }
 
@@ -46,7 +46,7 @@
 		*err = 1;
 		return p;
 	}
-	*(uvlong*)v = GBIT64(p);
+	*(uvlong*)v = UNPACK64(p);
 	return p+8;
 }
 
@@ -60,7 +60,7 @@
 		*err = 1;
 		return p;
 	}
-	n = GBIT16(p);
+	n = UNPACK16(p);
 	if(e - p < n + 3 || p[n+2] != 0){
 		*err = 1;
 		return p;
@@ -87,7 +87,7 @@
 		*err = 1;
 		return p;
 	}
-	PBIT16(p, v);
+	PACK16(p, v);
 	return p+2;
 }
 
@@ -98,7 +98,7 @@
 		*err = 1;
 		return p;
 	}
-	PBIT32(p, v);
+	PACK32(p, v);
 	return p+4;
 }
 
@@ -109,7 +109,7 @@
 		*err = 1;
 		return p;
 	}
-	PBIT64(p, v);
+	PACK64(p, v);
 	return p+8;
 }
 
@@ -124,7 +124,7 @@
 		*err = 1;
 		return p;
 	}
-	PBIT16(p+0, n);
+	PACK16(p+0, n);
 	memcpy(p+2, s, n);
 	p[2+n] = 0;
 	return p+3+n;
@@ -361,7 +361,7 @@
 {
 	assert(sz >= Snapsz);
 	p[0] = Ksnap;		p += 1;
-	PBIT64(p, id);		p += 8;
+	PACK64(p, id);		p += 8;
 	return p;
 }
 
@@ -369,9 +369,9 @@
 packbp(char *p, int sz, Bptr *bp)
 {
 	assert(sz >= Ptrsz);
-	PBIT64(p, bp->addr);	p += 8;
-	PBIT64(p, bp->hash);	p += 8;
-	PBIT64(p, bp->gen);	p += 8;
+	PACK64(p, bp->addr);	p += 8;
+	PACK64(p, bp->hash);	p += 8;
+	PACK64(p, bp->gen);	p += 8;
 	return p;
 }
 
@@ -381,9 +381,9 @@
 	Bptr bp;
 
 	assert(sz >= Ptrsz);
-	bp.addr = GBIT64(p);	p += 8;
-	bp.hash = GBIT64(p);	p += 8;
-	bp.gen = GBIT64(p);
+	bp.addr = UNPACK64(p);	p += 8;
+	bp.hash = UNPACK64(p);	p += 8;
+	bp.gen = UNPACK64(p);
 	return bp;
 }
 
@@ -396,18 +396,18 @@
 
 	assert(sz >= Treesz);
 	memset(t, 0, sizeof(Tree));
-	t->ref = GBIT32(p);		p += 4;
-	t->ht = GBIT32(p);		p += 4;
-	t->gen = GBIT64(p);		p += 8;
-	t->bp.addr = GBIT64(p);		p += 8;
-	t->bp.hash = GBIT64(p);		p += 8;
-	t->bp.gen = GBIT64(p);		p += 8;
+	t->ref = UNPACK32(p);		p += 4;
+	t->ht = UNPACK32(p);		p += 4;
+	t->gen = UNPACK64(p);		p += 8;
+	t->bp.addr = UNPACK64(p);		p += 8;
+	t->bp.hash = UNPACK64(p);		p += 8;
+	t->bp.gen = UNPACK64(p);		p += 8;
 	for(i = 0; i < Ndead; i++){
 		dl = &t->dead[i];
 		bp = &dl->head;
-		dl->prev = GBIT64(p);	p += 8;
-		bp->addr = GBIT64(p);	p += 8;
-		bp->hash = GBIT64(p);	p += 8;
+		dl->prev = UNPACK64(p);	p += 8;
+		bp->addr = UNPACK64(p);	p += 8;
+		bp->hash = UNPACK64(p);	p += 8;
 		bp->gen = -1;
 		t->dead[i].ins	= nil;	/* loaded on demand */
 	}
@@ -422,12 +422,12 @@
 	int i;
 
 	assert(sz >= Treesz);
-	PBIT32(p, t->ref);	p += 4;
-	PBIT32(p, t->ht);	p += 4;
-	PBIT64(p, t->gen);	p += 8;
-	PBIT64(p, t->bp.addr);	p += 8;
-	PBIT64(p, t->bp.hash);	p += 8;
-	PBIT64(p, t->bp.gen);	p += 8;
+	PACK32(p, t->ref);	p += 4;
+	PACK32(p, t->ht);	p += 4;
+	PACK64(p, t->gen);	p += 8;
+	PACK64(p, t->bp.addr);	p += 8;
+	PACK64(p, t->bp.hash);	p += 8;
+	PACK64(p, t->bp.gen);	p += 8;
 	for(i = 0; i < Ndead; i++){
 		dl = &t->dead[i];
 		bp = dl->head;
@@ -435,9 +435,9 @@
 			assert(checkflag(dl->ins, Bfinal));
 			bp = dl->ins->bp;
 		}
-		PBIT64(p, dl->prev);	p += 8;
-		PBIT64(p, bp.addr);	p += 8;
-		PBIT64(p, bp.hash);	p += 8;
+		PACK64(p, dl->prev);	p += 8;
+		PACK64(p, bp.addr);	p += 8;
+		PACK64(p, bp.hash);	p += 8;
 	}
 	return p;
 }
@@ -447,19 +447,19 @@
 {
 	assert(sz == Blksz);
 	memcpy(p, "gefs0001", 8);	p += 8;
-	PBIT32(p, Blksz);		p += 4;
-	PBIT32(p, Bufspc);		p += 4;
-	PBIT32(p, fi->snap.ht);		p += 4;
-	PBIT64(p, fi->snap.bp.addr);	p += 8;
-	PBIT64(p, fi->snap.bp.hash);	p += 8;
-	PBIT32(p, fi->narena);		p += 4;
-	PBIT64(p, fi->arenasz);		p += 8;
-	PBIT64(p, fi->nextqid);		p += 8;
-	PBIT64(p, fi->nextgen);		p += 8;
-	PBIT64(p, a->head.addr);	p += 8;	/* freelist addr */
-	PBIT64(p, a->head.hash);	p += 8;	/* freelist hash */
-	PBIT64(p, a->size);		p += 8;	/* arena size */
-	PBIT64(p, a->used);		p += 8;	/* arena used */
+	PACK32(p, Blksz);		p += 4;
+	PACK32(p, Bufspc);		p += 4;
+	PACK32(p, fi->snap.ht);		p += 4;
+	PACK64(p, fi->snap.bp.addr);	p += 8;
+	PACK64(p, fi->snap.bp.hash);	p += 8;
+	PACK32(p, fi->narena);		p += 4;
+	PACK64(p, fi->arenasz);		p += 8;
+	PACK64(p, fi->nextqid);		p += 8;
+	PACK64(p, fi->nextgen);		p += 8;
+	PACK64(p, a->head.addr);	p += 8;	/* freelist addr */
+	PACK64(p, a->head.hash);	p += 8;	/* freelist hash */
+	PACK64(p, a->size);		p += 8;	/* arena size */
+	PACK64(p, a->used);		p += 8;	/* arena used */
 	return p;
 }
 
@@ -474,21 +474,21 @@
 		return nil;
 	}
 	p += 8;
-	fi->blksz = GBIT32(p);		p += 4;
-	fi->bufspc = GBIT32(p);		p += 4;
-	fi->snap.ht = GBIT32(p);	p += 4;
-	fi->snap.bp.addr = GBIT64(p);	p += 8;
-	fi->snap.bp.hash = GBIT64(p);	p += 8;
+	fi->blksz = UNPACK32(p);		p += 4;
+	fi->bufspc = UNPACK32(p);		p += 4;
+	fi->snap.ht = UNPACK32(p);	p += 4;
+	fi->snap.bp.addr = UNPACK64(p);	p += 8;
+	fi->snap.bp.hash = UNPACK64(p);	p += 8;
 	fi->snap.bp.gen = -1;		p += 0;
-	fi->narena = GBIT32(p);		p += 4;
-	fi->arenasz = GBIT64(p);	p += 8;
-	fi->nextqid = GBIT64(p);	p += 8;
-	fi->nextgen = GBIT64(p);	p += 8;
-	a->head.addr = GBIT64(p);	p += 8;
-	a->head.hash = GBIT64(p);	p += 8;
+	fi->narena = UNPACK32(p);		p += 4;
+	fi->arenasz = UNPACK64(p);	p += 8;
+	fi->nextqid = UNPACK64(p);	p += 8;
+	fi->nextgen = UNPACK64(p);	p += 8;
+	a->head.addr = UNPACK64(p);	p += 8;
+	a->head.hash = UNPACK64(p);	p += 8;
 	a->head.gen = -1;		p += 0;
-	a->size = GBIT64(p);		p += 8;
-	a->used = GBIT64(p);		p += 8;
+	a->size = UNPACK64(p);		p += 8;
+	a->used = UNPACK64(p);		p += 8;
 	a->tail = nil;
 	return p;
 }
--- a/ream.c
+++ b/ream.c
@@ -100,11 +100,11 @@
 	setflag(b, Bdirty);
 
 	p = b->data + Loghashsz;
-	PBIT64(p, addr|LogFree);	p += 8;	/* addr */
-	PBIT64(p, asz-Blksz);		p += 8;	/* len */
-	PBIT64(p, b->bp.addr|LogAlloc);	p += 8;	/* addr */
-	PBIT64(p, Blksz);		p += 8;	/* len */
-	PBIT64(p, (uvlong)LogEnd);	/* done */
+	PACK64(p, addr|LogFree);	p += 8;	/* addr */
+	PACK64(p, asz-Blksz);		p += 8;	/* len */
+	PACK64(p, b->bp.addr|LogAlloc);	p += 8;	/* addr */
+	PACK64(p, Blksz);		p += 8;	/* len */
+	PACK64(p, (uvlong)LogEnd);	/* done */
 	finalize(b);
 	if(syncblk(b) == -1)
 		sysfatal("ream: init log");
--- a/snap.c
+++ b/snap.c
@@ -26,14 +26,14 @@
 Nextblk:
 	for(i = Loghashsz; i < Logspc; i += 16){
 		p = b->data + i;
-		op = GBIT64(p) & 0xff;
+		op = UNPACK64(p) & 0xff;
 		switch(op){
 		case DlEnd:
 			return 0;
 		case DlChain:
-			bp.addr = GBIT64(p);	p += 8;
+			bp.addr = UNPACK64(p);	p += 8;
 			bp.addr &= ~0xffULL;
-			bp.hash = GBIT64(p);
+			bp.hash = UNPACK64(p);
 			bp.gen = -1;
 			if(lblk)
 				fn(b->bp, dat);
@@ -41,17 +41,17 @@
 				return -1;
 			goto Nextblk;
 		case DlGraft:
-			t.head.addr = GBIT64(p);	p += 8;
+			t.head.addr = UNPACK64(p);	p += 8;
 			t.head.addr &= ~0xffULL;
-			t.head.hash = GBIT64(p);
+			t.head.hash = UNPACK64(p);
 			t.head.gen = -1;
 			t.ins = nil;
 			scandead(&t, lblk, fn, dat);
 			break;
 		case DlKill:
-			bp.addr = GBIT64(p);	p += 8;
+			bp.addr = UNPACK64(p);	p += 8;
 			bp.hash = -1;
-			bp.gen = GBIT64(p);
+			bp.gen = UNPACK64(p);
 			bp.addr &= ~0xffULL;
 			fn(bp, dat);
 			break;
@@ -101,8 +101,8 @@
 		dropblk(pb);
 	}
 	p = lb->data + lb->logsz;
-	PBIT64(p, v1);	p += 8;
-	PBIT64(p, v2);	p += 8;
+	PACK64(p, v1);	p += 8;
+	PACK64(p, v2);	p += 8;
 	if(dl->head.addr == -1){
 		end = DlEnd;
 		hash = -1;
@@ -110,8 +110,8 @@
 		end = dl->head.addr|DlChain;
 		hash = dl->head.hash;
 	}
-	PBIT64(p+0, end);
-	PBIT64(p+8, hash);
+	PACK64(p+0, end);
+	PACK64(p+8, hash);
 	lb->logsz = (p - lb->data);
 	return 0;
 }
@@ -162,7 +162,7 @@
 		return nil;
 	if(kv.nv != Snapsz)
 		return nil;
-	return opensnap(GBIT64(kv.v + 1));
+	return opensnap(UNPACK64(kv.v + 1));
 }
 
 Tree*
--- a/tree.c
+++ b/tree.c
@@ -100,10 +100,10 @@
 	int o;
 
 	assert(i >= 0 && i < b->nval);
-	o = GBIT16(b->data + 2*i);
-	kv->nk = GBIT16(b->data + o);
+	o = UNPACK16(b->data + 2*i);
+	kv->nk = UNPACK16(b->data + o);
 	kv->k = b->data + o + 2;
-	kv->nv = GBIT16(kv->k + kv->nk);
+	kv->nv = UNPACK16(kv->k + kv->nk);
 	kv->v = kv->k + kv->nk + 2;
 }
 
@@ -112,7 +112,7 @@
 {
 	assert(kv->nv == Ptrsz || kv->nv == Ptrsz+2);
 	if(fill != nil)
-		*fill = GBIT16(kv->v + Ptrsz);
+		*fill = UNPACK16(kv->v + Ptrsz);
 	return unpackbp(kv->v, kv->nv);
 }
 
@@ -131,12 +131,12 @@
 	assert(2*(b->nval+1) <= off);
 
 	p = b->data + 2*b->nval;
-	PBIT16(p, off);
+	PACK16(p, off);
 
 	p = b->data + off;
-	PBIT16(p, kv->nk);		p += 2;
+	PACK16(p, kv->nk);		p += 2;
 	memcpy(p, kv->k, kv->nk);	p += kv->nk;
-	PBIT16(p, kv->nv);		p += 2;
+	PACK16(p, kv->nv);		p += 2;
 	memcpy(p, kv->v, kv->nv);
 
 	b->nval++;
@@ -153,7 +153,7 @@
 	kv.v = buf;
 	kv.nv = sizeof(buf);
 	p = packbp(buf, sizeof(buf), &bp);
-	PBIT16(p, fill);
+	PACK16(p, fill);
 	setval(b, &kv);
 }
 
@@ -168,13 +168,13 @@
 
 	p = b->data + Pivspc + 2*b->nbuf;
 	o = Bufspc - b->bufsz;
-	PBIT16(p, o);
+	PACK16(p, o);
 
 	p = b->data + Pivspc + o;
 	*p = m->op;		p += 1;
-	PBIT16(p, m->nk);	p += 2;
+	PACK16(p, m->nk);	p += 2;
 	memcpy(p, m->k, m->nk);	p += m->nk;
-	PBIT16(p, m->nv);	p += 2;
+	PACK16(p, m->nv);	p += 2;
 	memcpy(p, m->v, m->nv);
 
 	b->nbuf++;
@@ -188,12 +188,12 @@
 
 	assert(b->type == Tpivot);
 	assert(i >= 0 && i < b->nbuf);
-	o = GBIT16(b->data + Pivspc + 2*i);
+	o = UNPACK16(b->data + Pivspc + 2*i);
 	p = b->data + Pivspc + o;
 	m->op = *p;
-	m->nk = GBIT16(p + 1);
+	m->nk = UNPACK16(p + 1);
 	m->k = p + 3;
-	m->nv = GBIT16(p + 3 + m->nk);
+	m->nv = UNPACK16(p + 3 + m->nk);
 	m->v = p + 5 + m->nk;
 }
 
@@ -356,32 +356,32 @@
 	/* bump version */
 	d.qid.vers++;
 	if(op & Owsize){
-		d.length = GBIT64(p);
+		d.length = UNPACK64(p);
 		p += 8;
 	}
 	if(op & Owmode){
-		d.mode = GBIT32(p);
+		d.mode = UNPACK32(p);
 		d.qid.type = d.mode>>24;
 		p += 4;
 	}
 	if(op & Owmtime){
-		d.mtime = GBIT64(p);
+		d.mtime = UNPACK64(p);
 		p += 8;
 	}
 	if(op & Owatime){
-		d.atime = GBIT64(p);
+		d.atime = UNPACK64(p);
 		p += 8;
 	}
 	if(op & Owuid){
-		d.uid = GBIT32(p);
+		d.uid = UNPACK32(p);
 		p += 4;
 	}
 	if(op & Owgid){
-		d.gid = GBIT32(p);
+		d.gid = UNPACK32(p);
 		p += 4;
 	}
 	if(op & Owmuid){
-		d.muid = GBIT32(p);
+		d.muid = UNPACK32(p);
 		p += 4;
 	}
 	if(p != m->v + m->nv){
@@ -1172,10 +1172,10 @@
 		if(ri == -1)
 			ri = hi+1;
 		p = r->data + Pivspc + 2*(nbuf+i);
-		o = GBIT16(p);
+		o = UNPACK16(p);
 		p = r->data + Pivspc + 2*ri;
 		memmove(p+2, p, 2*(nbuf+i-ri));
-		PBIT16(p, o);
+		PACK16(p, o);
 	}
 	enqueue(r);
 
--- a/user.c
+++ b/user.c
@@ -62,8 +62,8 @@
 	k.nk = Offksz;
 	for(o = 0; o < len; o += Blksz){
 		k.k[0] = Kdat;
-		PBIT64(k.k+1, path);
-		PBIT64(k.k+9, o);
+		PACK64(k.k+1, path);
+		PACK64(k.k+9, o);
 		if((e = btlookup(t, &k, &kv, kvbuf, sizeof(kvbuf))) != nil)
 			return e;
 		bp = unpackbp(kv.v, kv.nv);