2 * vbackup [-Dnv] fspartition [score]
4 * Copy a file system to a disk image stored on Venti.
5 * Prints a vnfs config line for the copied image.
8 * -f 'fast' writes - skip write if block exists on server
10 * -n nop -- don't actually write blocks
11 * -s print status updates
12 * -v print debugging trace
13 * -w write parallelism
15 * If score is given on the command line, it should be the
16 * score from a previous vbackup on this fspartition.
17 * In this mode, only the new blocks are stored to Venti.
18 * The result is still a complete image, but requires many
19 * fewer Venti writes in the common case.
21 * This program is structured as three processes connected
24 * fsysproc | cmpproc | ventiproc
26 * Fsysproc reads the disk and queues the blocks.
27 * Cmpproc compares the blocks against the SHA1 hashes
28 * in the old image, if any. It discards the unchanged blocks
29 * and queues the changed ones. Ventiproc writes blocks to Venti.
31 * There is a fourth proc, statusproc, which prints status
32 * updates about how the various procs are progressing.
49 typedef struct WriteReq WriteReq;
54 uchar score[VtScoreSize];
57 Biobuf bscores; /* biobuf filled with block scores */
58 int debug; /* debugging flag (not used) */
59 Disk* disk; /* disk being backed up */
60 RWLock endlk; /* silly synchonization */
61 int errors; /* are we exiting with an error status? */
62 int fastwrites; /* do not write blocks already on server */
63 int fsscanblock; /* last block scanned */
64 Fsys* fsys; /* file system being backed up */
65 int nchange; /* number of changed blocks */
66 int nop; /* don't actually send blocks to venti */
67 int nskip; /* number of blocks skipped (already on server) */
68 int nwritethread; /* number of write-behind threads */
69 Queue* qcmp; /* queue fsys->cmp */
70 Queue* qventi; /* queue cmp->venti */
71 int statustime; /* print status every _ seconds */
72 int verbose; /* print extra stuff */
73 VtFile* vfile; /* venti file being written */
74 Channel* writechan; /* chan(WriteReq) */
75 VtConn* z; /* connection to venti */
76 VtCache* zcache; /* cache of venti blocks */
77 uchar* zero; /* blocksize zero bytes */
83 void statusproc(void*);
84 void ventiproc(void*);
86 char* mountplace(char *dev);
91 fprint(2, "usage: vbackup [-DVnv] [-m mtpt] [-s secs] [-w n] disk [score]\n");
92 threadexitsall("usage");
96 threadmain(int argc, char **argv)
98 char *pref, *mountname;
99 uchar score[VtScoreSize], prev[VtScoreSize];
109 fmtinstall('F', vtfcallfmt);
110 fmtinstall('H', encodefmt);
111 fmtinstall('T', timefmt);
112 fmtinstall('V', vtscorefmt);
114 mountname = sysname();
129 mountname = EARGF(usage());
135 statustime = atoi(EARGF(usage()));
141 nwritethread = atoi(EARGF(usage()));
145 if(argc != 1 && argc != 2)
149 print("# %T vbackup %s %s\n", argv[0], argc>=2 ? argv[1] : "");
154 if((disk = diskopenfile(argv[0])) == nil)
155 sysfatal("diskopen: %r");
156 if((disk = diskcache(disk, 32768, 2*MAXQ+16)) == nil)
157 sysfatal("diskcache: %r");
158 if((fsys = fsysopen(disk)) == nil)
159 sysfatal("ffsopen: %r");
164 if((z = vtdial(nil)) == nil)
165 sysfatal("vtdial: %r");
167 sysfatal("vtconnect: %r");
170 * set up venti block cache
172 zero = vtmallocz(fsys->blocksize);
173 bsize = fsys->blocksize;
174 csize = 50; /* plenty; could probably do with 5 */
177 fprint(2, "cache %d blocks\n", csize);
178 c = vtcachealloc(z, bsize, csize);
182 * parse starting score
184 memset(prev, 0, sizeof prev);
186 vfile = vtfilecreateroot(c, (fsys->blocksize/VtScoreSize)*VtScoreSize,
187 fsys->blocksize, VtDataType);
189 sysfatal("vtfilecreateroot: %r");
190 vtfilelock(vfile, VtORDWR);
191 if(vtfilewrite(vfile, zero, 1, bsize*fsys->nblock-1) != 1)
192 sysfatal("vtfilewrite: %r");
193 if(vtfileflush(vfile) < 0)
194 sysfatal("vtfileflush: %r");
196 if(vtparsescore(argv[1], &pref, score) < 0)
197 sysfatal("bad score: %r");
198 if(pref!=nil && strcmp(pref, fsys->type) != 0)
199 sysfatal("score is %s but fsys is %s", pref, fsys->type);
200 b = vtcacheglobal(c, score, VtRootType);
202 if(vtrootunpack(&root, b->data) < 0)
203 sysfatal("bad root: %r");
204 if(strcmp(root.type, fsys->type) != 0)
205 sysfatal("root is %s but fsys is %s", root.type, fsys->type);
206 memmove(prev, score, VtScoreSize);
207 memmove(score, root.score, VtScoreSize);
210 b = vtcacheglobal(c, score, VtDirType);
212 sysfatal("vtcacheglobal %V: %r", score);
213 if(vtentryunpack(&e, b->data, 0) < 0)
214 sysfatal("%V: vtentryunpack failed", score);
216 fprint(2, "entry: size %llud psize %d dsize %d\n",
217 e.size, e.psize, e.dsize);
219 if((vfile = vtfileopenroot(c, &e)) == nil)
220 sysfatal("vtfileopenroot: %r");
221 vtfilelock(vfile, VtORDWR);
223 sysfatal("file system block sizes don't match %d %lld", e.dsize, bsize);
224 if(e.size != fsys->nblock*bsize)
225 sysfatal("file system block counts don't match %lld %lld", e.size, fsys->nblock*bsize);
229 * write scores of blocks into temporary file
231 if((tmp = getenv("TMP")) != nil){
233 }else if(access("/var/tmp", 0) >= 0)
237 tmpnam = smprint("%s/vbackup.XXXXXX", tmp);
239 sysfatal("smprint: %r");
241 if((fd = opentemp(tmpnam)) < 0)
242 sysfatal("opentemp %s: %r", tmpnam);
244 print("# %T reading scores into %s\n", tmpnam);
246 fprint(2, "read scores into %s...\n", tmpnam);
248 Binit(&bscores, fd, OWRITE);
249 for(i=0; i<fsys->nblock; i++){
250 if(vtfileblockscore(vfile, i, score) < 0)
251 sysfatal("vtfileblockhash %d: %r", i);
252 if(Bwrite(&bscores, score, VtScoreSize) != VtScoreSize)
253 sysfatal("Bwrite: %r");
259 * prep scores for rereading
262 Binit(&bscores, fd, OREAD);
265 * start the main processes
268 print("# %T starting procs\n");
273 proccreate(fsysproc, nil, STACK);
275 proccreate(ventiproc, nil, STACK);
277 proccreate(cmpproc, nil, STACK);
280 proccreate(statusproc, nil, STACK);
284 * wait for processes to finish
289 print("# %T procs exited: %d blocks changed, %d read, %d written, %d skipped, %d copied\n",
290 nchange, vtcachenread, vtcachenwrite, nskip, vtcachencopy);
295 vtfilelock(vfile, -1);
296 if(vtfileflush(vfile) < 0)
297 sysfatal("vtfileflush: %r");
298 if(vtfilegetentry(vfile, &e) < 0)
299 sysfatal("vtfilegetentry: %r");
301 b = vtcacheallocblock(c, VtDirType);
303 sysfatal("vtcacheallocblock: %r");
304 vtentrypack(&e, b->data, 0);
305 if(vtblockwrite(b) < 0)
306 sysfatal("vtblockwrite: %r");
308 memset(&root, 0, sizeof root);
309 strecpy(root.name, root.name+sizeof root.name, argv[0]);
310 strecpy(root.type, root.type+sizeof root.type, fsys->type);
311 memmove(root.score, b->score, VtScoreSize);
312 root.blocksize = fsys->blocksize;
313 memmove(root.prev, prev, VtScoreSize);
316 b = vtcacheallocblock(c, VtRootType);
318 sysfatal("vtcacheallocblock: %r");
319 vtrootpack(&root, b->data);
320 if(vtblockwrite(b) < 0)
321 sysfatal("vtblockwrite: %r");
323 tm = *localtime(time(0));
326 print("mount /%s/%d/%02d%02d%s %s:%V %d/%02d%02d/%02d%02d\n",
327 mountname, tm.year, tm.mon, tm.mday,
330 tm.year, tm.mon, tm.mday, tm.hour, tm.min);
331 print("# %T %s %s:%V\n", argv[0], root.type, b->score);
333 print("# %T venti sync\n");
336 sysfatal("vtsync: %r");
338 print("# %T synced\n");
343 fsysproc(void *dummy)
350 for(i=0; i<fsys->nblock; i++){
352 if((db = fsysreadblock(fsys, i)) != nil)
359 print("# %T fsys proc exiting\n");
369 uchar score[VtScoreSize];
370 uchar score1[VtScoreSize];
374 bsize = fsys->blocksize;
375 while((db = qread(qcmp, &bno)) != nil){
377 sha1(data, vtzerotruncate(VtDataType, data, bsize), score, nil);
378 if(Bseek(&bscores, (vlong)bno*VtScoreSize, 0) < 0)
379 sysfatal("cmpproc Bseek: %r");
380 if(Bread(&bscores, score1, VtScoreSize) != VtScoreSize)
381 sysfatal("cmpproc Bread: %r");
382 if(memcmp(score, score1, VtScoreSize) != 0){
385 print("# block %ud: old %V new %V\n", bno, score1, score);
386 qwrite(qventi, db, bno);
392 print("# %T cmp proc exiting\n");
404 while(recv(writechan, &wr) == 1){
409 if(fastwrites && vtread(z, wr.score, wr.type, nil, 0) < 0){
410 rerrstr(err, sizeof err);
411 if(strstr(err, "read too small")){ /* already exists */
417 if(vtwritepacket(z, wr.score, wr.type, wr.p) < 0)
418 sysfatal("vtwritepacket: %r");
423 myvtwrite(VtConn *z, uchar score[VtScoreSize], uint type, uchar *buf, int n)
427 if(nwritethread == 0)
428 return vtwrite(z, score, type, buf, n);
430 wr.p = packetalloc();
431 packetappend(wr.p, buf, n);
432 packetsha1(wr.p, score);
433 memmove(wr.score, score, VtScoreSize);
436 send(writechan, &wr);
441 ventiproc(void *dummy)
450 proccreate(vtsendproc, z, STACK);
451 proccreate(vtrecvproc, z, STACK);
453 writechan = chancreate(sizeof(WriteReq), 0);
454 for(i=0; i<nwritethread; i++)
455 threadcreate(writethread, nil, STACK);
456 vtcachesetwrite(zcache, myvtwrite);
458 bsize = fsys->blocksize;
459 vtfilelock(vfile, -1);
460 while((db = qread(qventi, &bno)) != nil){
465 if(vtfilewrite(vfile, db->data, bsize, bno*bsize) != bsize)
466 sysfatal("ventiproc vtfilewrite: %r");
467 if(vtfileflushbefore(vfile, (bno+1)*bsize) < 0)
468 sysfatal("ventiproc vtfileflushbefore: %r");
472 vtcachesetwrite(zcache, nil);
473 for(i=0; i<nwritethread; i++)
474 send(writechan, nil);
476 print("# %T venti proc exiting - nsend %d nrecv %d\n", nsend, nrecv);
481 percent(u32int a, u32int b)
483 return (vlong)a*100/b;
487 statusproc(void *dummy)
494 if(qcmp->closed && qcmp->nel==0 && qventi->closed && qventi->nel==0)
499 print("# %T fsscan=%d%% cmpq=%d%% ventiq=%d%%\n",
500 percent(fsscanblock, fsys->nblock),
501 percent(qcmp->nel, MAXQ),
502 percent(qventi->nel, MAXQ));
504 print("# %T status proc exiting\n");
514 tm = *localtime(time(0));
515 return fmtprint(fmt, "%04d/%02d%02d %02d:%02d:%02d.%03d",
516 tm.year+1900, tm.mon+1, tm.mday, tm.hour, tm.min, tm.sec,
517 (int)(ns%1000000000)/1000000);
521 mountplace(char *dev)
528 sysfatal("pipe: %r");
533 cmd = smprint("mount | awk '$1==\"%s\" && $2 == \"on\" {print $3}'", dev);
534 if(threadspawnl(fd, "sh", "sh", "-c", cmd, nil) < 0)
535 sysfatal("exec mount|awk (to find mtpt of %s): %r", dev);
536 /* threadspawnl closed p[1] */
537 n = readn(p[0], buf, sizeof buf-1);
542 if((q = strchr(buf, '\n')) == nil)
546 if(q>buf && *(q-1) == '/')