aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/venti/checkarenas.c
blob: ae7c641acf9ab772ca8d0363cf78f0e112596dca (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
#include "stdinc.h"
#include "dat.h"
#include "fns.h"

static int	verbose;

static void
checkarena(Arena *arena, int scan, int fix)
{
	Arena old;
	int err, e;

	if(verbose && arena->clumps)
		printarena(2, arena);

	old = *arena;

	if(scan){
		arena->used = 0;
		arena->clumps = 0;
		arena->cclumps = 0;
		arena->uncsize = 0;
	}

	err = 0;
	for(;;){
		e = syncarena(arena, 1000, 0, fix);
		err |= e;
		if(!(e & SyncHeader))
			break;
		if(verbose && arena->clumps)
			fprint(2, ".");
	}
	if(verbose && arena->clumps)
		fprint(2, "\n");

	err &= ~SyncHeader;
	if(arena->used != old.used
	|| arena->clumps != old.clumps
	|| arena->cclumps != old.cclumps
	|| arena->uncsize != old.uncsize){
		fprint(2, "incorrect arena header fields\n");
		printarena(2, arena);
		err |= SyncHeader;
	}

	if(!err || !fix)
		return;

	fprint(2, "writing fixed arena header fields\n");
	if(wbarena(arena) < 0)
		fprint(2, "arena header write failed: %r\n");
}

void
usage(void)
{
	fprint(2, "usage: checkarenas [-afv] file\n");
	threadexitsall(0);
}

void
threadmain(int argc, char *argv[])
{
	ArenaPart *ap;
	Part *part;
	char *file;
	int i, fix, scan;

	fmtinstall('V', vtscorefmt);

	statsinit();

	fix = 0;
	scan = 0;
	ARGBEGIN{
	case 'f':
		fix++;
		break;
	case 'a':
		scan = 1;
		break;
	case 'v':
		verbose++;
		break;
	default:
		usage();
		break;
	}ARGEND

	if(!fix)
		readonly = 1;

	if(argc != 1)
		usage();

	file = argv[0];

	part = initpart(file, 0);
	if(part == nil)
		sysfatal("can't open partition %s: %r", file);

	ap = initarenapart(part);
	if(ap == nil)
		sysfatal("can't initialize arena partition in %s: %r", file);

	if(verbose > 1){
		printarenapart(2, ap);
		fprint(2, "\n");
	}

	initdcache(8 * MaxDiskBlock);

	for(i = 0; i < ap->narenas; i++)
		checkarena(ap->arenas[i], scan, fix);

	if(verbose > 1)
		printstats();
	threadexitsall(0);
}