ref: c441666e73e2625e4f5208eb4d64af56be370ec1
parent: 64a67122575094f38e0197cf9a52ff7bc7f1995e
author: Ori Bernstein <[email protected]>
date: Sun Mar 6 17:30:24 EST 2022
diskfmt: remove fixed size block header now typed blocks have a 2 byte type, and the rest of the header depednds on the block type.
--- a/blk.c
+++ b/blk.c
@@ -92,7 +92,6 @@
b->bp.addr = bp;
b->bp.hash = -1;
b->bp.gen = -1;
- b->data = b->buf + Hdrsz;
b->fnext = nil;
b->nval = 0;
@@ -107,12 +106,17 @@
fprint(2, "invalid block @%llx\n", bp);
abort();
break;
- case Tarena:
case Traw:
+ case Tarena:
+ b->data = b->buf;
+ break;
case Tlog:
case Tdead:
+ b->data = b->buf + _Loghdsz;
break;
+ 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);
@@ -119,6 +123,7 @@
b->bufsz = GBIT16(b->buf+8);
break;
case Tleaf:
+ b->data = b->buf + Leafhdsz;
b->nval = GBIT16(b->buf+2);
b->valsz = GBIT16(b->buf+4);
break;
@@ -187,7 +192,7 @@
static int
syncarena(Arena *a)
{
- packarena(a->b->data, Blkspc, a, fs);
+ packarena(a->b->data, Blksz, a, fs);
finalize(a->b);
return syncblk(a->b);
}
@@ -261,7 +266,7 @@
if((lb = initblk(o, Tlog)) == nil)
return -1;
cacheblk(lb);
- lb->logsz = Loghdsz;
+ lb->logsz = Loghashsz;
p = lb->data + lb->logsz;
PBIT64(p, (uvlong)LogEnd);
finalize(lb);
@@ -343,11 +348,11 @@
return -1;
bh = GBIT64(b->data);
/* the hash covers the log and offset */
- if(bh != siphash(b->data+8, Blkspc-8)){
+ if(bh != siphash(b->data+Loghashsz, Logspc-Loghashsz)){
werrstr("corrupt log");
return -1;
}
- for(i = Loghdsz; i < Logspc; i += n){
+ for(i = Loghashsz; i < Logspc; i += n){
d = b->data + i;
ent = GBIT64(d);
op = ent & 0xff;
@@ -418,7 +423,7 @@
if((b = initblk(ba, Tlog)) == nil)
return -1;
setflag(b, Bdirty);
- b->logsz = Loghdsz;
+ b->logsz = Loghashsz;
p = b->data + b->logsz;
PBIT64(p, (uvlong)LogEnd);
@@ -477,7 +482,7 @@
}
hd = b;
tl = b;
- b->logsz = Loghdsz;
+ b->logsz = Loghashsz;
for(i = 0; i < n; i++)
if(logappend(a, log[i].off, log[i].len, LogFree, &tl) == -1)
return -1;
@@ -503,7 +508,7 @@
bp.gen = -1;
if((b = getblk(bp, GBnochk)) == nil)
return -1;
- for(i = Loghdsz; i < Logspc; i += n){
+ for(i = Loghashsz; i < Logspc; i += n){
p = b->data + i;
v = GBIT64(p);
n = ((v&0xff) >= Log2wide) ? 16 : 8;
@@ -651,7 +656,22 @@
b->bp.hash = -1;
b->bp.gen = fs->nextgen;
b->qgen = fs->qgen;
- b->data = b->buf + Hdrsz;
+ switch(t){
+ case Traw:
+ case Tarena:
+ b->data = b->buf;
+ break;
+ case Tdead:
+ case Tlog:
+ b->data = b->buf + _Loghdsz;
+ break;
+ case Tpivot:
+ b->data = b->buf + Pivhdsz;
+ break;
+ case Tleaf:
+ b->data = b->buf + Leafhdsz;
+ break;
+ }
b->fnext = nil;
setflag(b, Bdirty);
@@ -710,9 +730,6 @@
PBIT16(b->buf, b->type);
switch(b->type){
default:
- case Tnone:
- abort();
- break;
case Tpivot:
PBIT16(b->buf+2, b->nval);
PBIT16(b->buf+4, b->valsz);
@@ -727,7 +744,7 @@
break;
case Tlog:
case Tdead:
- h = siphash(b->data + 8, Blkspc-8);
+ h = siphash(b->data + Loghashsz, Logspc-Loghashsz);
PBIT64(b->data, h);
b->bp.hash = blkhash(b);
break;
--- a/dat.h
+++ b/dat.h
@@ -46,7 +46,6 @@
* there is no way to get a valid split of a
* maximally filled tree.
*/
- Loghdsz = 8, /* log hash */
Keymax = 128, /* key data limit */
Inlmax = 512, /* inline data limit */
Ptrsz = 24, /* off, hash, gen */
@@ -63,14 +62,17 @@
Wstatmax = 4+8+8+8, /* mode, size, atime, mtime */
- Hdrsz = 10,
- Rootsz = 4+Ptrsz, /* root pointer */
- Blkspc = Blksz - Hdrsz,
- Bufspc = Blkspc / 2,
- Pivspc = Blkspc - Bufspc,
- Logspc = Blkspc - Loghdsz,
- Leafspc = Blkspc,
- Msgmax = 1 + (Kvmax > Kpmax ? Kvmax : Kpmax)
+ Pivhdsz = 10,
+ Leafhdsz = 6,
+ _Loghdsz = 2,
+ Loghashsz = 8,
+ Rootsz = 4+Ptrsz, /* root pointer */
+ Pivsz = Blksz - Pivhdsz,
+ Bufspc = (Blksz - Pivhdsz) / 2,
+ Pivspc = Blksz - Pivhdsz - Bufspc,
+ Logspc = Blksz - _Loghdsz,
+ Leafspc = Blksz - Leafhdsz,
+ Msgmax = 1 + (Kvmax > Kpmax ? Kvmax : Kpmax)
};
enum {
@@ -154,8 +156,6 @@
* bufsz[4] portion of leaf nodes
* allocated to buffers,
* in bytes
- * hdrsz[4] size of tree node header,
- * in bytes.
* height[4] tree height of root node
* rootb[8] address of root in last
* snapshot.
@@ -210,13 +210,12 @@
* off[8] hash[8] fill[2]
*/
enum {
- Tnone,
Traw,
Tpivot,
Tleaf,
- Tarena,
Tlog,
Tdead,
+ Tarena = 0x6567, /* 'ge' */
};
enum {
@@ -390,7 +389,6 @@
struct Fshdr {
int blksz;
int bufspc;
- int hdrsz;
Tree snap;
int narena;
vlong arenasz;
--- a/load.c
+++ b/load.c
@@ -16,7 +16,7 @@
static void
mergeinfo(Gefs *fs, Fshdr *fi)
{
- if(fi->blksz != Blksz || fi->bufspc != Bufspc || fi->hdrsz != Hdrsz)
+ if(fi->blksz != Blksz || fi->bufspc != Bufspc)
sysfatal("parameter mismatch");
if(fs->gotinfo && fs->narena != fi->narena)
sysfatal("arena count mismatch");
@@ -36,7 +36,8 @@
bp.gen = -1;
if((b = getblk(bp, GBnochk)) == nil)
return -1;
- unpackarena(a, fi, b->data, Blkspc);
+ if(unpackarena(a, fi, b->data, Blksz) == nil)
+ return -1;
if((a->free = avlcreate(rangecmp)) == nil)
return -1;
a->b = b;
--- a/pack.c
+++ b/pack.c
@@ -442,11 +442,10 @@
char*
packarena(char *p, int sz, Arena *a, Fshdr *fi)
{
- assert(sz >= Blkspc);
+ assert(sz == Blksz);
memcpy(p, "gefs0001", 8); p += 8;
PBIT32(p, Blksz); p += 4;
PBIT32(p, Bufspc); p += 4;
- PBIT32(p, Hdrsz); 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;
@@ -465,15 +464,16 @@
char*
unpackarena(Arena *a, Fshdr *fi, char *p, int sz)
{
- assert(sz >= Blkspc);
+ assert(sz == Blksz);
memset(a, 0, sizeof(*a));
memset(fi, 0, sizeof(*fi));
- if(memcmp(p, "gefs0001", 8) != 0)
+ if(memcmp(p, "gefs0001", 8) != 0){
+ werrstr("wrong block header %.8s\n", p);
return nil;
+ }
p += 8;
fi->blksz = GBIT32(p); p += 4;
fi->bufspc = GBIT32(p); p += 4;
- fi->hdrsz = 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;
--- a/ream.c
+++ b/ream.c
@@ -98,10 +98,10 @@
b->type = Tlog;
b->bp.addr = addr;
b->logsz = 32;
- b->data = b->buf + Hdrsz;
+ b->data = b->buf + _Loghdsz;
b->flag |= Bdirty;
- p = b->data+Loghdsz;
+ 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 */
@@ -117,7 +117,7 @@
memset(b, 0, sizeof(Blk));
b->type = Tarena;
b->bp.addr = start;
- b->data = b->buf + Hdrsz;
+ b->data = b->buf;
a->head.addr = bo;
a->head.hash = bh;
a->head.gen = -1;
@@ -124,7 +124,7 @@
a->size = asz;
a->used = Blksz;
a->tail = nil;
- packarena(b->data, Blkspc, a, fi);
+ packarena(b->data, Blksz, a, fi);
finalize(b);
if(syncblk(b) == -1)
sysfatal("ream: write arena: %r");
@@ -220,7 +220,7 @@
finalize(a->tail);
if(syncblk(a->tail) == -1)
sysfatal("sync arena: %r");
- packarena(a->b->data, Blkspc, a, fs);
+ packarena(a->b->data, Blksz, a, fs);
finalize(a->b);
if(syncblk(a->b) == -1)
sysfatal("sync arena: %r");
--- a/snap.c
+++ b/snap.c
@@ -24,7 +24,7 @@
if(b == nil)
return -1;
Nextblk:
- for(i = Loghdsz; i < Logspc; i += 16){
+ for(i = Loghashsz; i < Logspc; i += 16){
p = b->data + i;
op = GBIT64(p) & 0xff;
switch(op){
@@ -96,7 +96,7 @@
return -1;
dl->head = pb->bp;
}
- lb->logsz = Loghdsz;
+ lb->logsz = Loghashsz;
dl->ins = lb;
putblk(pb);
}
--- a/tree.c
+++ b/tree.c
@@ -461,7 +461,7 @@
* messages are.
*/
full = 0;
- spc = Blkspc - blkfill(b);
+ spc = Leafspc - blkfill(b);
if((n = newblk(b->type)) == nil)
return -1;
while(i < b->nval){
@@ -639,9 +639,9 @@
full = 0;
copied = 0;
halfsz = (2*b->nval + b->valsz + up->sz) / 2;
- if(halfsz > Blkspc/2)
- halfsz = Blkspc/2;
- spc = Blkspc - (halfsz + Msgmax);
+ if(halfsz > Leafspc/2)
+ halfsz = Leafspc/2;
+ spc = Leafspc - (halfsz + Msgmax);
assert(b->nval >= 4);
while(i < b->nval){
/*
@@ -654,7 +654,7 @@
if((i == b->nval-2) || (i >= 2 && copied >= halfsz)){
d = r;
full = 0;
- spc = Blkspc - (halfsz + Msgmax);
+ spc = Leafspc - (halfsz + Msgmax);
getval(b, i, mid);
}
ok = 1;
@@ -951,7 +951,7 @@
{
Blk *l, *m, *r;
Kvp kl, kr;
- int ret, fill;
+ int spc, ret, fill;
Bptr bp;
l = nil;
@@ -963,10 +963,11 @@
return 0;
m = refblk(pp->nl);
+ spc = (m->type == Tleaf) ? Leafspc : Pivspc;
if(idx-1 >= 0){
getval(p->b, idx-1, &kl);
bp = getptr(&kl, &fill);
- if(fill + blkfill(m) < Blkspc){
+ if(fill + blkfill(m) < spc){
if((l = getblk(bp, 0)) == nil)
goto Out;
if(rotmerge(t, p, pp, idx-1, l, m) == -1)
@@ -977,7 +978,7 @@
if(idx+1 < p->b->nval){
getval(p->b, idx+1, &kr);
bp = getptr(&kr, &fill);
- if(fill + blkfill(m) < Blkspc){
+ if(fill + blkfill(m) < spc){
if((r = getblk(bp, 0)) == nil)
goto Out;
if(rotmerge(t, p, pp, idx, m, r) == -1)