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;
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);
113 write(1, tagged, strlen(tagged));
118 vfprint(1, fmt, arg);
122 #pragma varargck argpos tag 3
123 #pragma varargck argpos chat 1
127 ereadpart(Part *p, u64int offset, u8int *buf, u32int count)
129 if(readpart(p, offset, buf, count) != count){
130 chat("%T readpart %s at %#llux+%ud: %r\n", p->name, offset, count);
137 ewritepart(Part *p, u64int offset, u8int *buf, u32int count)
139 if(writepart(p, offset, buf, count) != count || flushpart(p) < 0){
140 chat("%T writepart %s at %#llux+%ud: %r\n", p->name, offset, count);
147 * Extra proc to do writes to dst, so that we can overlap reading
148 * src with writing dst during copy. This is an easy factor of two
149 * (almost) in performance.
158 while((w = recvp(writechan)) != nil){
161 if(ewritepart(dst, w->o, w->p, w->n) < 0)
167 copy(uvlong start, uvlong end, char *what, DigestState *ds)
174 static uchar tmpbuf[2*Chunk+MaxIo];
175 static uchar *tmp[2];
179 assert(start <= end);
180 assert(astart <= start && start < aend);
181 assert(astart <= end && end <= aend);
183 // align the buffers so readpart/writepart can do big transfers
186 p += MaxIo - (uintptr)p%MaxIo;
190 if(verbose && start != end)
191 chat("%T copy %,llud-%,llud %s\n", start, end, what);
194 memset(w, 0, sizeof w);
195 for(o=start; o<end; o+=n){
201 if(ereadpart(src, o, tmp[i], n) < 0)
207 sendp(writechan, &w[i]);
209 sha1(tmp[i], n, nil, ds);
216 * wait for queued write to finish
218 sendp(writechan, &wsync);
226 * sync with write proc
232 sendp(writechan, &w[i]);
236 /* single-threaded, for reference */
238 copy1(uvlong start, uvlong end, char *what, DigestState *ds)
242 static uchar tmp[1024*1024];
244 assert(start <= end);
245 assert(astart <= start && start < aend);
246 assert(astart <= end && end <= aend);
248 if(verbose && start != end)
249 chat("%T copy %,llud-%,llud %s\n", start, end, what);
251 for(o=start; o<end; o+=n){
255 if(ereadpart(src, o, tmp, n) < 0)
258 sha1(tmp, n, nil, ds);
259 if(ewritepart(dst, o, tmp, n) < 0)
266 asha1(Part *p, uvlong start, uvlong end, DigestState *ds)
270 static uchar tmp[1024*1024];
277 chat("%T sha1 %,llud-%,llud\n", start, end);
279 for(o=start; o<end; o+=n){
283 if(ereadpart(p, o, tmp, n) < 0)
285 sha1(tmp, n, nil, ds);
291 rdown(uvlong a, int b)
305 mirror(int indx, Arena *sa, Arena *da)
307 vlong v, si, di, end;
308 int clumpmax, blocksize, sealed;
309 static uchar buf[MaxIoSize];
311 DigestState xds, *ds;
315 blocksize = sa->blocksize;
316 end = sa->base + sa->size;
318 astart = base - blocksize;
319 aend = end + blocksize;
321 tag(indx, sa->name, "%T %s (%,llud-%,llud)\n", sa->name, astart, aend);
324 copy(astart, aend, "all", nil);
328 if(sa->diskstats.sealed && da->diskstats.sealed && scorecmp(da->score, zeroscore) != 0){
329 if(scorecmp(sa->score, da->score) == 0){
330 setstatus(Sealed+Mirrored);
332 chat("%T %s: %V sealed mirrored\n", sa->name, sa->score);
335 chat("%T %s: warning: sealed score mismatch %V vs %V\n", sa->name, sa->score, da->score);
336 /* Keep executing; will correct seal if possible. */
338 if(!sa->diskstats.sealed && da->diskstats.sealed && scorecmp(da->score, zeroscore) != 0){
339 chat("%T %s: dst is sealed, src is not\n", sa->name);
343 if(sa->diskstats.used < da->diskstats.used){
344 chat("%T %s: src used %,lld < dst used %,lld\n", sa->name, sa->diskstats.used, da->diskstats.used);
349 if(da->clumpmagic != sa->clumpmagic){
351 * Write this now to reduce the window in which
352 * the head and tail disagree about clumpmagic.
354 da->clumpmagic = sa->clumpmagic;
355 memset(buf, 0, sizeof buf);
357 if(ewritepart(dst, end, buf, blocksize) < 0)
361 memset(&h, 0, sizeof h);
362 h.version = da->version;
363 strcpy(h.name, da->name);
364 h.blocksize = da->blocksize;
365 h.size = da->size + 2*da->blocksize;
366 h.clumpmagic = da->clumpmagic;
367 memset(buf, 0, sizeof buf);
368 packarenahead(&h, buf);
369 if(ewritepart(dst, base - blocksize, buf, blocksize) < 0)
374 sealed = sa->diskstats.sealed && scorecmp(sa->score, zeroscore) != 0;
375 if(sealed && dosha1){
376 /* start sha1 state with header */
377 memset(&xds, 0, sizeof xds);
379 sha1(buf, blocksize, nil, ds);
383 if(sa->diskstats.used != da->diskstats.used){
384 di = base+rdown(da->diskstats.used, blocksize);
385 si = base+rup(sa->diskstats.used, blocksize);
386 if(ds && asha1(dst, shaoff, di, ds) < 0)
388 if(copy(di, si, "data", ds) < 0)
393 clumpmax = sa->clumpmax;
394 di = end - da->diskstats.clumps/clumpmax * blocksize;
395 si = end - (sa->diskstats.clumps+clumpmax-1)/clumpmax * blocksize;
397 if(sa->diskstats.sealed){
399 * might be a small hole between the end of the
400 * data and the beginning of the directory.
402 v = base+rup(sa->diskstats.used, blocksize);
403 if(ds && asha1(dst, shaoff, v, ds) < 0)
405 if(copy(v, si, "hole", ds) < 0)
410 if(da->diskstats.clumps != sa->diskstats.clumps){
411 if(ds && asha1(dst, shaoff, si, ds) < 0)
413 if(copy(si, di, "directory", ds) < 0) /* si < di because clumpinfo blocks grow down */
418 da->ctime = sa->ctime;
419 da->wtime = sa->wtime;
420 da->diskstats = sa->diskstats;
421 da->diskstats.sealed = 0;
424 * Repack the arena tail information
425 * and save it for next time...
427 memset(buf, 0, sizeof buf);
429 if(ewritepart(dst, end, buf, blocksize) < 0)
434 * ... but on the final pass, copy the encoding
435 * of the tail information from the source
436 * arena itself. There are multiple possible
437 * ways to write the tail info out (the exact
438 * details have changed as venti went through
439 * revisions), and to keep the SHA1 hash the
440 * same, we have to use what the disk uses.
442 if(asha1(dst, shaoff, end, ds) < 0
443 || copy(end, end+blocksize-VtScoreSize, "tail", ds) < 0)
446 memset(buf, 0, VtScoreSize);
447 sha1(buf, VtScoreSize, da->score, ds);
448 if(scorecmp(sa->score, da->score) == 0){
449 setstatus(Sealed+Mirrored);
451 chat("%T %s: %V sealed mirrored\n", sa->name, sa->score);
452 if(ewritepart(dst, end+blocksize-VtScoreSize, da->score, VtScoreSize) < 0)
455 chat("%T %s: sealing dst: score mismatch: %V vs %V\n", sa->name, sa->score, da->score);
456 memset(&xds, 0, sizeof xds);
457 asha1(dst, base-blocksize, end+blocksize-VtScoreSize, &xds);
458 sha1(buf, VtScoreSize, 0, &xds);
459 chat("%T reseal: %V\n", da->score);
465 chat("%T %s: %V mirrored\n", sa->name, sa->score);
466 if(ewritepart(dst, end+blocksize-VtScoreSize, sa->score, VtScoreSize) < 0)
470 if(sa->diskstats.used > 0 || verbose > 1) {
471 chat("%T %s: %,lld used mirrored\n",
472 sa->name, sa->diskstats.used);
474 if(sa->diskstats.used > 0)
482 mirrormany(ArenaPart *sp, ArenaPart *dp, char *range)
489 for(i=0; i<sp->narenas; i++){
497 if(strcmp(range, "none") == 0)
500 for(s=range; *s; s=t){
509 lo = strtol(s, &s, 0);
516 hi = strtol(s, &s, 0);
519 chat("%T bad arena range: %s\n", s);
522 for(i=lo; i<=hi; i++){
533 threadmain(int argc, char **argv)
556 if(argc != 2 && argc != 3)
562 if((src = initpart(argv[0], OREAD)) == nil)
563 sysfatal("initpart %s: %r", argv[0]);
564 if((dst = initpart(argv[1], ORDWR)) == nil)
565 sysfatal("initpart %s: %r", argv[1]);
566 if((s = initarenapart(src)) == nil)
567 sysfatal("initarenapart %s: %r", argv[0]);
568 for(i=0; i<s->narenas; i++)
569 delarena(s->arenas[i]);
570 if((d = initarenapart(dst)) == nil)
571 sysfatal("loadarenapart %s: %r", argv[1]);
572 for(i=0; i<d->narenas; i++)
573 delarena(d->arenas[i]);
576 * The arena geometries must match or all bets are off.
578 if(s->narenas != d->narenas)
579 sysfatal("arena count mismatch: %d vs %d", s->narenas, d->narenas);
580 for(i=0; i<s->narenas; i++){
583 if(sa->version != da->version)
584 sysfatal("arena %d: version mismatch: %d vs %d", i, sa->version, da->version);
585 if(sa->blocksize != da->blocksize)
586 sysfatal("arena %d: blocksize mismatch: %d vs %d", i, sa->blocksize, da->blocksize);
587 if(sa->size != da->size)
588 sysfatal("arena %d: size mismatch: %,lld vs %,lld", i, sa->size, da->size);
589 if(strcmp(sa->name, da->name) != 0)
590 sysfatal("arena %d: name mismatch: %s vs %s", i, sa->name, da->name);
594 * Mirror one arena at a time.
596 writechan = chancreate(sizeof(void*), 0);
597 vtproc(writeproc, nil);
598 mirrormany(s, d, ranges);
599 sendp(writechan, nil);
600 threadexitsall(status);