aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/venti/syncarena.c
blob: 78a6bd0fa3ea845c7144800b729b438e1e648e0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "stdinc.h"
#include "dat.h"
#include "fns.h"

static int	writeclumphead(Arena *arena, u64int aa, Clump *cl);
static int	writeclumpmagic(Arena *arena, u64int aa, u32int magic);

int
clumpinfocmp(ClumpInfo *c, ClumpInfo *d)
{
	return c->type != d->type
		|| c->size != d->size
		|| c->uncsize != d->uncsize
		|| scorecmp(c->score, d->score)!=0;
}

/*
 * synchronize the clump info directory with
 * with the clumps actually stored in the arena.
 * the directory should be at least as up to date
 * as the arena's trailer.
 *
 * checks/updates at most n clumps.
 *
 * returns 0 if ok, flags if error occurred
 */
int
syncarena(Arena *arena, u32int n, int zok, int fix)
{
	ZBlock *lump;
	Clump cl;
	ClumpInfo ci;
	static ClumpInfo zci = { .type = -1 };
	u8int score[VtScoreSize];
	u64int uncsize, used, aa;
	u32int clump, clumps, cclumps, magic;
	int err, flush, broken;

	used = arena->used;
	clumps = arena->clumps;
	cclumps = arena->cclumps;
	uncsize = arena->uncsize;

	flush = 0;
	err = 0;
	for(; n; n--){
		aa = arena->used;
		clump = arena->clumps;
		magic = clumpmagic(arena, aa);
		if(magic == ClumpFreeMagic)
			break;
		if(magic != ClumpMagic){
			fprint(2, "illegal clump magic number=%#8.8ux at clump=%d\n", magic, clump);
			err |= SyncDataErr;
//ZZZ write a zero here?
			if(0 && fix && writeclumpmagic(arena, aa, ClumpFreeMagic) < 0){
				fprint(2, "can't write corrected clump free magic: %r");
				err |= SyncFixErr;
			}
			break;
		}
		arena->clumps++;

		broken = 0;
		lump = loadclump(arena, aa, 0, &cl, score, 0);
		if(lump == nil){
			fprint(2, "clump=%d failed to read correctly: %r\n", clump);
			err |= SyncDataErr;
		}else if(cl.info.type != VtTypeCorrupt){
			scoremem(score, lump->data, cl.info.uncsize);
			if(scorecmp(cl.info.score, score) != 0){
				fprint(2, "clump=%d has mismatched score\n", clump);
				err |= SyncDataErr;
				broken = 1;
			}else if(vttypevalid(cl.info.type) < 0){
				fprint(2, "clump=%d has invalid type %d", clump, cl.info.type);
				err |= SyncDataErr;
				broken = 1;
			}
			if(broken && fix){
				cl.info.type = VtTypeCorrupt;
				if(writeclumphead(arena, aa, &cl) < 0){
					fprint(2, "can't write corrected clump header: %r");
					err |= SyncFixErr;
				}
			}
		}
		freezblock(lump);
		arena->used += ClumpSize + cl.info.size;

		if(!broken && readclumpinfo(arena, clump, &ci)<0){
			fprint(2, "arena directory read failed\n");
			broken = 1;
		}else if(!broken && clumpinfocmp(&ci, &cl.info)!=0){
			if(clumpinfocmp(&ci, &zci) == 0){
				err |= SyncCIZero;
				if(!zok)
					fprint(2, "unwritten clump info for clump=%d\n", clump);
			}else{
				err |= SyncCIErr;
				fprint(2, "bad clump info for clump=%d\n", clump);
				fprint(2, "\texpected score=%V type=%d size=%d uncsize=%d\n",
					cl.info.score, cl.info.type, cl.info.size, cl.info.uncsize);
				fprint(2, "\tfound score=%V type=%d size=%d uncsize=%d\n",
					ci.score, ci.type, ci.size, ci.uncsize);
			}
			broken = 1;
		}
		if(broken && fix){
			flush = 1;
			ci = cl.info;
			if(writeclumpinfo(arena, clump, &ci) < 0){
				fprint(2, "can't write correct clump directory: %r\n");
				err |= SyncFixErr;
			}
		}

		arena->uncsize += cl.info.uncsize;
		if(cl.info.size < cl.info.uncsize)
			arena->cclumps++;
	}

	if(flush){
		arena->wtime = now();
		if(arena->ctime == 0 && arena->clumps)
			arena->ctime = arena->wtime;
		if(flushciblocks(arena) < 0){
			fprint(2, "can't flush arena directory cache: %r");
			err |= SyncFixErr;
		}
	}

	if(used != arena->used
	|| clumps != arena->clumps
	|| cclumps != arena->cclumps
	|| uncsize != arena->uncsize)
		err |= SyncHeader;

	return err;
}

static int
writeclumphead(Arena *arena, u64int aa, Clump *cl)
{
	ZBlock *zb;
	int bad;

	zb = alloczblock(ClumpSize, 0);
	if(zb == nil)
		return -1;
	bad = packclump(cl, zb->data)<0
		|| writearena(arena, aa, zb->data, ClumpSize) != ClumpSize;
	freezblock(zb);
	return bad ? -1 : 0;
}

static int
writeclumpmagic(Arena *arena, u64int aa, u32int magic)
{
	u8int buf[U32Size];

	packmagic(magic, buf);
	return writearena(arena, aa, buf, U32Size) == U32Size;
}