Blob


1 #include "stdinc.h"
2 #include "dat.h"
3 #include "fns.h"
5 QLock godot;
6 char *host;
7 int readonly = 1; /* for part.c */
8 int mainstacksize = 256*1024;
9 Channel *c;
10 VtConn *z;
11 int fast; /* and a bit unsafe; only for benchmarking */
12 int haveaoffset;
13 int maxwrites = -1;
14 int verbose;
16 typedef struct ZClump ZClump;
17 struct ZClump
18 {
19 ZBlock *lump;
20 Clump cl;
21 u64int aa;
22 };
24 void
25 usage(void)
26 {
27 fprint(2, "usage: wrarena [-h host] arenafile [offset]\n");
28 threadexitsall("usage");
29 }
31 void
32 vtsendthread(void *v)
33 {
34 ZClump zcl;
36 USED(v);
37 while(recv(c, &zcl) == 1){
38 if(zcl.lump == nil)
39 break;
40 if(vtwrite(z, zcl.cl.info.score, zcl.cl.info.type, zcl.lump->data, zcl.cl.info.uncsize) < 0)
41 sysfatal("failed writing clump %llud: %r", zcl.aa);
42 if(verbose)
43 print("%V\n", zcl.cl.info.score);
44 freezblock(zcl.lump);
45 }
46 /*
47 * All the send threads try to exit right when
48 * threadmain is calling threadexitsall.
49 * Either libthread or the Linux NPTL pthreads library
50 * can't handle this condition (I suspect NPTL but have
51 * not confirmed this) and we get a seg fault in exit.
52 * I spent a day tracking this down with no success,
53 * so we're going to work around it instead by just
54 * sitting here and waiting for the threadexitsall to
55 * take effect.
56 */
57 qlock(&godot);
58 }
60 static void
61 rdarena(Arena *arena, u64int offset)
62 {
63 int i;
64 u64int a, aa, e;
65 uchar score[VtScoreSize];
66 Clump cl;
67 ClumpInfo ci;
68 ZBlock *lump;
69 ZClump zcl;
71 fprint(2, "wrarena: copying %s to venti\n", arena->name);
72 printarena(2, arena);
74 a = arena->base;
75 e = arena->base + arena->size;
76 if(offset != ~(u64int)0) {
77 if(offset >= e - a)
78 sysfatal("bad offset %#llx >= %#llx\n", offset, e - a);
79 aa = offset;
80 } else
81 aa = 0;
83 i = 0;
84 for(a = 0; maxwrites != 0 && i < arena->memstats.clumps;
85 a += ClumpSize + ci.size){
86 if(readclumpinfo(arena, i++, &ci) < 0)
87 break;
88 if(a < aa || ci.type == VtCorruptType){
89 if(ci.type == VtCorruptType)
90 fprint(2, "corrupt at %#llx: +%d\n",
91 a, ClumpSize+ci.size);
92 continue;
93 }
94 lump = loadclump(arena, a, 0, &cl, score, 0);
95 if(lump == nil) {
96 fprint(2, "clump %#llx failed to read: %r\n", a);
97 continue;
98 }
99 if(!fast && cl.info.type != VtCorruptType) {
100 scoremem(score, lump->data, cl.info.uncsize);
101 if(scorecmp(cl.info.score, score) != 0) {
102 fprint(2, "clump %#llx has mismatched score\n",
103 a);
104 break;
106 if(vttypevalid(cl.info.type) < 0) {
107 fprint(2, "clump %#llx has bad type %d\n",
108 a, cl.info.type);
109 break;
112 if(z && cl.info.type != VtCorruptType){
113 zcl.cl = cl;
114 zcl.lump = lump;
115 zcl.aa = a;
116 send(c, &zcl);
117 }else
118 freezblock(lump);
119 if(maxwrites > 0)
120 --maxwrites;
122 if(a > aa)
123 aa = a;
124 if(haveaoffset)
125 print("end offset %#llx\n", aa);
128 void
129 threadmain(int argc, char *argv[])
131 int i;
132 char *file;
133 Arena *arena;
134 u64int offset, aoffset;
135 Part *part;
136 uchar buf[8192];
137 ArenaHead head;
138 ZClump zerocl;
140 qlock(&godot);
141 aoffset = 0;
142 ARGBEGIN{
143 case 'f':
144 fast = 1;
145 ventidoublechecksha1 = 0;
146 break;
147 case 'h':
148 host = EARGF(usage());
149 break;
150 case 'o':
151 haveaoffset = 1;
152 aoffset = strtoull(EARGF(usage()), 0, 0);
153 break;
154 case 'M':
155 maxwrites = atoi(EARGF(usage()));
156 break;
157 case 'v':
158 verbose = 1;
159 break;
160 default:
161 usage();
162 break;
163 }ARGEND
165 offset = ~(u64int)0;
166 switch(argc) {
167 default:
168 usage();
169 case 2:
170 offset = strtoull(argv[1], 0, 0);
171 /* fall through */
172 case 1:
173 file = argv[0];
176 ventifmtinstall();
178 statsinit();
180 part = initpart(file, OREAD);
181 if(part == nil)
182 sysfatal("can't open file %s: %r", file);
183 if(readpart(part, aoffset, buf, sizeof buf) < 0)
184 sysfatal("can't read file %s: %r", file);
186 if(unpackarenahead(&head, buf) < 0)
187 sysfatal("corrupted arena header: %r");
189 if(aoffset+head.size > part->size)
190 sysfatal("arena is truncated: want %llud bytes have %llud\n",
191 head.size, part->size);
193 partblocksize(part, head.blocksize);
194 initdcache(8 * MaxDiskBlock);
196 arena = initarena(part, aoffset, head.size, head.blocksize);
197 if(arena == nil)
198 sysfatal("initarena: %r");
200 z = nil;
201 if(host==nil || strcmp(host, "/dev/null") != 0){
202 z = vtdial(host);
203 if(z == nil)
204 sysfatal("could not connect to server: %r");
205 if(vtconnect(z) < 0)
206 sysfatal("vtconnect: %r");
209 print("%T starting to send data\n");
210 c = chancreate(sizeof(ZClump), 0);
211 for(i=0; i<12; i++)
212 vtproc(vtsendthread, nil);
214 rdarena(arena, offset);
215 memset(&zerocl, 0, sizeof zerocl);
216 for(i=0; i<12; i++)
217 send(c, &zerocl);
218 if(vtsync(z) < 0)
219 sysfatal("executing sync: %r");
220 if(z){
221 vthangup(z);
223 print("%T sent all data\n");
225 threadexitsall(0);