2 * Mirror one arena partition onto another.
3 * Be careful to copy only new data.
12 typedef struct Write Write;
32 fprint(2, "usage: mirrorarenas [-sv] src dst [ranges]\n");
33 threadexitsall("usage");
41 tag(int indx, char *name, char *fmt, ...)
52 tagged = vsmprint(fmt, arg);
66 static int startindx = -1, endindx;
67 static char *startname, *endname;
71 if(bits != lastbits) {
75 snprint(buf, sizeof buf, "sealed");
78 snprint(buf, sizeof buf, "mirrored");
81 snprint(buf, sizeof buf, "mirrored sealed");
84 snprint(buf, sizeof buf, "empty");
87 snprint(buf, sizeof buf, "%d", bits);
90 print("%T %s-%s %s\n", startname, endname, buf);
116 write(1, tagged, strlen(tagged));
121 vfprint(1, fmt, arg);
125 #pragma varargck argpos tag 1
126 #pragma varargck argpos chat 1
130 ereadpart(Part *p, u64int offset, u8int *buf, u32int count)
132 if(readpart(p, offset, buf, count) != count){
133 chat("%T readpart %s at %#llux+%ud: %r\n", p->name, offset, count);
140 ewritepart(Part *p, u64int offset, u8int *buf, u32int count)
142 if(writepart(p, offset, buf, count) != count || flushpart(p) < 0){
143 chat("%T writepart %s at %#llux+%ud: %r\n", p->name, offset, count);
150 * Extra proc to do writes to dst, so that we can overlap reading
151 * src with writing dst during copy. This is an easy factor of two
152 * (almost) in performance.
161 while((w = recvp(writechan)) != nil){
164 if(ewritepart(dst, w->o, w->p, w->n) < 0)
170 copy(uvlong start, uvlong end, char *what, DigestState *ds)
177 static uchar tmpbuf[2*Chunk+MaxIo];
178 static uchar *tmp[2];
182 assert(start <= end);
183 assert(astart <= start && start < aend);
184 assert(astart <= end && end <= aend);
186 // align the buffers so readpart/writepart can do big transfers
189 p += MaxIo - (uintptr)p%MaxIo;
193 if(verbose && start != end)
194 chat("%T copy %,llud-%,llud %s\n", start, end, what);
197 memset(w, 0, sizeof w);
198 for(o=start; o<end; o+=n){
204 if(ereadpart(src, o, tmp[i], n) < 0)
210 sendp(writechan, &w[i]);
212 sha1(tmp[i], n, nil, ds);
219 * wait for queued write to finish
221 sendp(writechan, &wsync);
229 * sync with write proc
235 sendp(writechan, &w[i]);
239 /* single-threaded, for reference */
241 copy1(uvlong start, uvlong end, char *what, DigestState *ds)
245 static uchar tmp[1024*1024];
247 assert(start <= end);
248 assert(astart <= start && start < aend);
249 assert(astart <= end && end <= aend);
251 if(verbose && start != end)
252 chat("%T copy %,llud-%,llud %s\n", start, end, what);
254 for(o=start; o<end; o+=n){
258 if(ereadpart(src, o, tmp, n) < 0)
261 sha1(tmp, n, nil, ds);
262 if(ewritepart(dst, o, tmp, n) < 0)
269 asha1(Part *p, uvlong start, uvlong end, DigestState *ds)
273 static uchar tmp[1024*1024];
280 chat("%T sha1 %,llud-%,llud\n", start, end);
282 for(o=start; o<end; o+=n){
286 if(ereadpart(p, o, tmp, n) < 0)
288 sha1(tmp, n, nil, ds);
294 rdown(uvlong a, int b)
308 mirror(int indx, Arena *sa, Arena *da)
310 vlong v, si, di, end;
311 int clumpmax, blocksize, sealed;
312 static uchar buf[MaxIoSize];
314 DigestState xds, *ds;
318 blocksize = sa->blocksize;
319 end = sa->base + sa->size;
321 astart = base - blocksize;
322 aend = end + blocksize;
324 tag(indx, sa->name, "%T %s (%,llud-%,llud)\n", sa->name, astart, aend);
327 copy(astart, aend, "all", nil);
331 if(sa->diskstats.sealed && da->diskstats.sealed && scorecmp(da->score, zeroscore) != 0){
332 if(scorecmp(sa->score, da->score) == 0){
333 setstatus(Sealed+Mirrored);
335 chat("%T %s: %V sealed mirrored\n", sa->name, sa->score);
338 chat("%T %s: warning: sealed score mismatch %V vs %V\n", sa->name, sa->score, da->score);
339 /* Keep executing; will correct seal if possible. */
341 if(!sa->diskstats.sealed && da->diskstats.sealed && scorecmp(da->score, zeroscore) != 0){
342 chat("%T %s: dst is sealed, src is not\n", sa->name);
346 if(sa->diskstats.used < da->diskstats.used){
347 chat("%T %s: src used %,lld < dst used %,lld\n", sa->name, sa->diskstats.used, da->diskstats.used);
352 if(da->clumpmagic != sa->clumpmagic){
354 * Write this now to reduce the window in which
355 * the head and tail disagree about clumpmagic.
357 da->clumpmagic = sa->clumpmagic;
358 memset(buf, 0, sizeof buf);
360 if(ewritepart(dst, end, buf, blocksize) < 0)
364 memset(&h, 0, sizeof h);
365 h.version = da->version;
366 strcpy(h.name, da->name);
367 h.blocksize = da->blocksize;
368 h.size = da->size + 2*da->blocksize;
369 h.clumpmagic = da->clumpmagic;
370 memset(buf, 0, sizeof buf);
371 packarenahead(&h, buf);
372 if(ewritepart(dst, base - blocksize, buf, blocksize) < 0)
377 sealed = sa->diskstats.sealed && scorecmp(sa->score, zeroscore) != 0;
378 if(sealed && dosha1){
379 /* start sha1 state with header */
380 memset(&xds, 0, sizeof xds);
382 sha1(buf, blocksize, nil, ds);
386 if(sa->diskstats.used != da->diskstats.used){
387 di = base+rdown(da->diskstats.used, blocksize);
388 si = base+rup(sa->diskstats.used, blocksize);
389 if(ds && asha1(dst, shaoff, di, ds) < 0)
391 if(copy(di, si, "data", ds) < 0)
396 clumpmax = sa->clumpmax;
397 di = end - da->diskstats.clumps/clumpmax * blocksize;
398 si = end - (sa->diskstats.clumps+clumpmax-1)/clumpmax * blocksize;
400 if(sa->diskstats.sealed){
402 * might be a small hole between the end of the
403 * data and the beginning of the directory.
405 v = base+rup(sa->diskstats.used, blocksize);
406 if(ds && asha1(dst, shaoff, v, ds) < 0)
408 if(copy(v, si, "hole", ds) < 0)
413 if(da->diskstats.clumps != sa->diskstats.clumps){
414 if(ds && asha1(dst, shaoff, si, ds) < 0)
416 if(copy(si, di, "directory", ds) < 0) /* si < di because clumpinfo blocks grow down */
421 da->ctime = sa->ctime;
422 da->wtime = sa->wtime;
423 da->diskstats = sa->diskstats;
424 da->diskstats.sealed = 0;
427 * Repack the arena tail information
428 * and save it for next time...
430 memset(buf, 0, sizeof buf);
432 if(ewritepart(dst, end, buf, blocksize) < 0)
437 * ... but on the final pass, copy the encoding
438 * of the tail information from the source
439 * arena itself. There are multiple possible
440 * ways to write the tail info out (the exact
441 * details have changed as venti went through
442 * revisions), and to keep the SHA1 hash the
443 * same, we have to use what the disk uses.
445 if(asha1(dst, shaoff, end, ds) < 0
446 || copy(end, end+blocksize-VtScoreSize, "tail", ds) < 0)
449 memset(buf, 0, VtScoreSize);
450 sha1(buf, VtScoreSize, da->score, ds);
451 if(scorecmp(sa->score, da->score) == 0){
452 setstatus(Sealed+Mirrored);
454 chat("%T %s: %V sealed mirrored\n", sa->name, sa->score);
455 if(ewritepart(dst, end+blocksize-VtScoreSize, da->score, VtScoreSize) < 0)
458 chat("%T %s: sealing dst: score mismatch: %V vs %V\n", sa->name, sa->score, da->score);
459 memset(&xds, 0, sizeof xds);
460 asha1(dst, base-blocksize, end+blocksize-VtScoreSize, &xds);
461 sha1(buf, VtScoreSize, 0, &xds);
462 chat("%T reseal: %V\n", da->score);
468 chat("%T %s: %V mirrored\n", sa->name, sa->score);
469 if(ewritepart(dst, end+blocksize-VtScoreSize, sa->score, VtScoreSize) < 0)
473 if(sa->diskstats.used > 0 || verbose > 1) {
474 chat("%T %s: %,lld used mirrored\n",
475 sa->name, sa->diskstats.used);
477 if(sa->diskstats.used > 0)
485 mirrormany(ArenaPart *sp, ArenaPart *dp, char *range)
492 for(i=0; i<sp->narenas; i++){
500 if(strcmp(range, "none") == 0)
503 for(s=range; *s; s=t){
512 lo = strtol(s, &s, 0);
519 hi = strtol(s, &s, 0);
522 chat("%T bad arena range: %s\n", s);
525 for(i=lo; i<=hi; i++){
536 threadmain(int argc, char **argv)
559 if(argc != 2 && argc != 3)
565 if((src = initpart(argv[0], OREAD)) == nil)
566 sysfatal("initpart %s: %r", argv[0]);
567 if((dst = initpart(argv[1], ORDWR)) == nil)
568 sysfatal("initpart %s: %r", argv[1]);
569 if((s = initarenapart(src)) == nil)
570 sysfatal("initarenapart %s: %r", argv[0]);
571 for(i=0; i<s->narenas; i++)
572 delarena(s->arenas[i]);
573 if((d = initarenapart(dst)) == nil)
574 sysfatal("loadarenapart %s: %r", argv[1]);
575 for(i=0; i<d->narenas; i++)
576 delarena(d->arenas[i]);
579 * The arena geometries must match or all bets are off.
581 if(s->narenas != d->narenas)
582 sysfatal("arena count mismatch: %d vs %d", s->narenas, d->narenas);
583 for(i=0; i<s->narenas; i++){
586 if(sa->version != da->version)
587 sysfatal("arena %d: version mismatch: %d vs %d", i, sa->version, da->version);
588 if(sa->blocksize != da->blocksize)
589 sysfatal("arena %d: blocksize mismatch: %d vs %d", i, sa->blocksize, da->blocksize);
590 if(sa->size != da->size)
591 sysfatal("arena %d: size mismatch: %,lld vs %,lld", i, sa->size, da->size);
592 if(strcmp(sa->name, da->name) != 0)
593 sysfatal("arena %d: name mismatch: %s vs %s", i, sa->name, da->name);
597 * Mirror one arena at a time.
599 writechan = chancreate(sizeof(void*), 0);
600 vtproc(writeproc, nil);
601 mirrormany(s, d, ranges);
602 sendp(writechan, nil);
603 threadexitsall(status);