ref: a81f100786ca7f0a1927c993f3f18a50b2480fb2
parent: 89df661ffb632a1df899d1e9dc3693bd90a5ec7d
author: Ori Bernstein <ori@eigenstate.org>
date: Tue May 30 15:12:11 EDT 2023
blk: fix log append
--- a/blk.c
+++ b/blk.c
@@ -244,10 +244,10 @@
}
static Blk*
-chainlog(Blk *pb, vlong o)
+mklogblk(vlong o)
{
- char *p;
Blk *lb;
+ char *p;
if((lb = cachepluck()) == nil)
return nil;
@@ -262,19 +262,26 @@
dropblk(lb);
return nil;
}
+ return lb;
+}
- if(pb != nil){
- assert(pb->type == Tlog);
- p = pb->data + pb->logsz;
- PACK64(p, lb->bp.addr|LogChain);
- finalize(pb);
- if(syncblk(pb) == -1){
- dropblk(pb);
- return nil;
- }
+static int
+chainlog(Blk *pb, vlong o)
+{
+ char *p;
+
+ if(pb == nil)
+ return 0;
+ assert(pb->type == Tlog);
+ p = pb->data + pb->logsz;
+ PACK64(p, o|LogChain);
+ finalize(pb);
+ if(syncblk(pb) == -1){
dropblk(pb);
+ return -1;
}
- return lb;
+ dropblk(pb);
+ return 0;
}
/*
@@ -287,10 +294,10 @@
logappend(Arena *a, vlong off, vlong len, int op, Blk **tl)
{
vlong o, ao;
- Blk *lb;
+ Blk *nl, *lb;
char *p, *name;
- o = -1;
+ nl = nil;
lb = *tl;
assert((off & 0xff) == 0);
assert(op == LogAlloc || op == LogFree || op == LogSync);
@@ -318,9 +325,12 @@
if(lb == nil || lb->logsz >= Logspc - Logslop){
if((o = blkalloc_lk(a, 1)) == -1)
return -1;
- if((lb = chainlog(lb, o)) == nil)
+ if((nl = mklogblk(o)) == nil)
return -1;
- *tl = lb;
+ if(chainlog(lb, nl->bp.addr) == -1)
+ return -1;
+ lb = nl;
+ *tl = nl;
a->nlog++;
}
@@ -346,9 +356,9 @@
* current allocation. so that we don't
* reorder allocs and frees.
*/
- if(o != -1){
+ if(nl != nil){
p = lb->data + lb->logsz;
- ao = o|LogAlloc1;
+ ao = nl->bp.addr|LogAlloc1;
PACK64(p, ao);
lb->logsz += 8;
}
@@ -530,7 +540,9 @@
tl = nil;
for(i = 0; i < nr; i++){
if(tl == nil || tl->logsz >= Logspc - Logslop){
- if((tl = chainlog(tl, log[n++])) == nil){
+ if((tl = mklogblk(log[n++])) == nil)
+ return -1;
+ if(chainlog(tl, tl->bp.addr) == -1){
free(rng);
free(log);
return -1;
@@ -685,8 +697,8 @@
a = getarena(b);
if(logappend(a, b, Blksz, LogFree, &a->defertl) == -1)
return -1;
- if(a->loghd.addr == -1)
- a->loghd = a->logtl->bp;
+ if(a->deferhd.addr == -1)
+ a->deferhd = a->defertl->bp;
unlock(a);
return r;
}