2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sbin/hammer/cmd_mirror.c,v 1.9.2.3 2008/08/04 19:41:25 dillon Exp $
39 #define SERIALBUF_SIZE (512 * 1024)
41 static int read_mrecords(int fd
, char *buf
, u_int size
,
42 hammer_ioc_mrecord_head_t pickup
);
43 static hammer_ioc_mrecord_any_t
read_mrecord(int fdin
, int *errorp
,
44 hammer_ioc_mrecord_head_t pickup
);
45 static void write_mrecord(int fdout
, u_int32_t type
,
46 hammer_ioc_mrecord_any_t mrec
, int bytes
);
47 static void generate_mrec_header(int fd
, int fdout
, int pfs_id
,
48 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
);
49 static int validate_mrec_header(int fd
, int fdin
, int is_target
, int pfs_id
,
50 struct hammer_ioc_mrecord_head
*pickup
,
51 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
);
52 static void update_pfs_snapshot(int fd
, hammer_tid_t snapshot_tid
, int pfs_id
);
53 static ssize_t
writebw(int fd
, const void *buf
, size_t nbytes
,
54 u_int64_t
*bwcount
, struct timeval
*tv1
);
55 static void mirror_usage(int code
);
58 * Generate a mirroring data stream from the specific source over the
59 * entire key range, but restricted to the specified transaction range.
61 * The HAMMER VFS does most of the work, we add a few new mrecord
62 * types to negotiate the TID ranges and verify that the entire
63 * stream made it to the destination.
66 hammer_cmd_mirror_read(char **av
, int ac
, int streaming
)
68 struct hammer_ioc_mirror_rw mirror
;
69 struct hammer_ioc_pseudofs_rw pfs
;
70 union hammer_ioc_mrecord_any mrec_tmp
;
71 struct hammer_ioc_mrecord_head pickup
;
72 hammer_ioc_mrecord_any_t mrec
;
73 hammer_tid_t sync_tid
;
74 const char *filesystem
;
75 char *buf
= malloc(SERIALBUF_SIZE
);
82 time_t base_t
= time(NULL
);
94 bzero(&mirror
, sizeof(mirror
));
95 hammer_key_beg_init(&mirror
.key_beg
);
96 hammer_key_end_init(&mirror
.key_end
);
98 fd
= getpfs(&pfs
, filesystem
);
100 if (streaming
&& VerboseOpt
) {
101 fprintf(stderr
, "\nRunning");
105 gettimeofday(&bwtv
, NULL
);
109 * In 2-way mode the target will send us a PFS info packet
110 * first. Use the target's current snapshot TID as our default
115 n
= validate_mrec_header(fd
, 0, 0, pfs
.pfs_id
, &pickup
,
116 NULL
, &mirror
.tid_beg
);
117 if (n
< 0) { /* got TERM record */
125 * Write out the PFS header, tid_beg will be updated if our PFS
126 * has a larger begin sync. tid_end is set to the latest source
127 * TID whos flush cycle has completed.
129 generate_mrec_header(fd
, 1, pfs
.pfs_id
,
130 &mirror
.tid_beg
, &mirror
.tid_end
);
132 /* XXX streaming mode support w/ cycle or command line arg */
134 * A cycle file overrides the beginning TID
136 hammer_get_cycle(&mirror
.key_beg
, &mirror
.tid_beg
);
139 mirror
.tid_beg
= strtoull(av
[1], NULL
, 0);
141 if (streaming
== 0 || VerboseOpt
>= 2) {
143 "Mirror-read: Mirror from %016llx to %016llx\n",
144 mirror
.tid_beg
, mirror
.tid_end
);
146 if (mirror
.key_beg
.obj_id
!= (int64_t)HAMMER_MIN_OBJID
) {
147 fprintf(stderr
, "Mirror-read: Resuming at object %016llx\n",
148 mirror
.key_beg
.obj_id
);
152 * Nothing to do if begin equals end.
154 if (mirror
.tid_beg
>= mirror
.tid_end
) {
155 if (streaming
== 0 || VerboseOpt
>= 2)
156 fprintf(stderr
, "Mirror-read: No work to do\n");
163 * Write out bulk records
166 mirror
.size
= SERIALBUF_SIZE
;
170 mirror
.pfs_id
= pfs
.pfs_id
;
171 mirror
.shared_uuid
= pfs
.ondisk
->shared_uuid
;
172 if (ioctl(fd
, HAMMERIOC_MIRROR_READ
, &mirror
) < 0) {
173 fprintf(stderr
, "Mirror-read %s failed: %s\n",
174 filesystem
, strerror(errno
));
177 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_ERROR
) {
179 "Mirror-read %s fatal error %d\n",
180 filesystem
, mirror
.head
.error
);
185 n
= writebw(1, mirror
.ubuf
, mirror
.count
,
188 n
= write(1, mirror
.ubuf
, mirror
.count
);
190 if (n
!= mirror
.count
) {
191 fprintf(stderr
, "Mirror-read %s failed: "
197 total_bytes
+= mirror
.count
;
198 if (streaming
&& VerboseOpt
) {
199 fprintf(stderr
, "\r%016llx %11lld",
200 mirror
.key_cur
.obj_id
,
204 mirror
.key_beg
= mirror
.key_cur
;
206 (unsigned)(time(NULL
) - base_t
) > (unsigned)TimeoutOpt
) {
208 "Mirror-read %s interrupted by timer at"
211 mirror
.key_cur
.obj_id
);
215 } while (mirror
.count
!= 0);
219 * Write out the termination sync record - only if not interrupted
221 if (interrupted
== 0) {
223 write_mrecord(1, HAMMER_MREC_TYPE_SYNC
,
224 &mrec_tmp
, sizeof(mrec_tmp
.sync
));
226 write_mrecord(1, HAMMER_MREC_TYPE_IDLE
,
227 &mrec_tmp
, sizeof(mrec_tmp
.sync
));
232 * If the -2 option was given (automatic when doing mirror-copy),
233 * a two-way pipe is assumed and we expect a response mrec from
237 mrec
= read_mrecord(0, &error
, &pickup
);
239 mrec
->head
.type
!= HAMMER_MREC_TYPE_UPDATE
||
240 mrec
->head
.rec_size
!= sizeof(mrec
->update
)) {
241 fprintf(stderr
, "mirror_read: Did not get final "
242 "acknowledgement packet from target\n");
247 hammer_set_cycle(&mirror
.key_cur
, mirror
.tid_beg
);
248 fprintf(stderr
, "Cyclefile %s updated for continuation\n", CyclePath
);
251 sync_tid
= mrec
->update
.tid
;
253 hammer_key_beg_init(&mirror
.key_beg
);
254 hammer_set_cycle(&mirror
.key_beg
, sync_tid
);
255 fprintf(stderr
, "Cyclefile %s updated to 0x%016llx\n",
256 CyclePath
, sync_tid
);
259 } else if (CyclePath
) {
260 /* NOTE! mirror.tid_beg cannot be updated */
261 fprintf(stderr
, "Warning: cycle file (-c option) cannot be "
262 "fully updated unless you use mirror-copy\n");
263 hammer_set_cycle(&mirror
.key_beg
, mirror
.tid_beg
);
265 if (streaming
&& interrupted
== 0) {
266 time_t t1
= time(NULL
);
270 fprintf(stderr
, " W");
273 pfs
.ondisk
->sync_end_tid
= mirror
.tid_end
;
274 if (ioctl(fd
, HAMMERIOC_WAI_PSEUDOFS
, &pfs
) < 0) {
275 fprintf(stderr
, "Mirror-read %s: cannot stream: %s\n",
276 filesystem
, strerror(errno
));
278 t2
= time(NULL
) - t1
;
279 if (t2
>= 0 && t2
< DelayOpt
) {
281 fprintf(stderr
, "\bD");
284 sleep(DelayOpt
- t2
);
287 fprintf(stderr
, "\b ");
294 write_mrecord(1, HAMMER_MREC_TYPE_TERM
,
295 &mrec_tmp
, sizeof(mrec_tmp
.sync
));
297 fprintf(stderr
, "Mirror-read %s succeeded\n", filesystem
);
301 * Pipe the mirroring data stream on stdin to the HAMMER VFS, adding
302 * some additional packet types to negotiate TID ranges and to verify
303 * completion. The HAMMER VFS does most of the work.
305 * It is important to note that the mirror.key_{beg,end} range must
306 * match the ranged used by the original. For now both sides use
307 * range the entire key space.
309 * It is even more important that the records in the stream conform
310 * to the TID range also supplied in the stream. The HAMMER VFS will
311 * use the REC, PASS, and SKIP record types to track the portions of
312 * the B-Tree being scanned in order to be able to proactively delete
313 * records on the target within those active areas that are not mentioned
316 * The mirror.key_cur field is used by the VFS to do this tracking. It
317 * must be initialized to key_beg but then is persistently updated by
318 * the HAMMER VFS on each successive ioctl() call. If you blow up this
319 * field you will blow up the mirror target, possibly to the point of
320 * deleting everything. As a safety measure the HAMMER VFS simply marks
321 * the records that the source has destroyed as deleted on the target,
322 * and normal pruning operations will deal with their final disposition
323 * at some later time.
326 hammer_cmd_mirror_write(char **av
, int ac
)
328 struct hammer_ioc_mirror_rw mirror
;
329 const char *filesystem
;
330 char *buf
= malloc(SERIALBUF_SIZE
);
331 struct hammer_ioc_pseudofs_rw pfs
;
332 struct hammer_ioc_mrecord_head pickup
;
333 struct hammer_ioc_synctid synctid
;
334 union hammer_ioc_mrecord_any mrec_tmp
;
335 hammer_ioc_mrecord_any_t mrec
;
344 pickup
.signature
= 0;
348 bzero(&mirror
, sizeof(mirror
));
349 hammer_key_beg_init(&mirror
.key_beg
);
350 hammer_key_end_init(&mirror
.key_end
);
351 mirror
.key_end
= mirror
.key_beg
;
353 fd
= getpfs(&pfs
, filesystem
);
356 * In two-way mode the target writes out a PFS packet first.
357 * The source uses our tid_end as its tid_beg by default,
358 * picking up where it left off.
362 generate_mrec_header(fd
, 1, pfs
.pfs_id
,
363 &mirror
.tid_beg
, &mirror
.tid_end
);
367 * Read and process the PFS header. The source informs us of
368 * the TID range the stream represents.
370 n
= validate_mrec_header(fd
, 0, 1, pfs
.pfs_id
, &pickup
,
371 &mirror
.tid_beg
, &mirror
.tid_end
);
372 if (n
< 0) { /* got TERM record */
378 mirror
.size
= SERIALBUF_SIZE
;
381 * Read and process bulk records (REC, PASS, and SKIP types).
383 * On your life, do NOT mess with mirror.key_cur or your mirror
384 * target may become history.
388 mirror
.pfs_id
= pfs
.pfs_id
;
389 mirror
.shared_uuid
= pfs
.ondisk
->shared_uuid
;
390 mirror
.size
= read_mrecords(0, buf
, SERIALBUF_SIZE
, &pickup
);
391 if (mirror
.size
<= 0)
393 if (ioctl(fd
, HAMMERIOC_MIRROR_WRITE
, &mirror
) < 0) {
394 fprintf(stderr
, "Mirror-write %s failed: %s\n",
395 filesystem
, strerror(errno
));
398 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_ERROR
) {
400 "Mirror-write %s fatal error %d\n",
401 filesystem
, mirror
.head
.error
);
405 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_INTR
) {
407 "Mirror-write %s interrupted by timer at"
410 mirror
.key_cur
.obj_id
);
417 * Read and process the termination sync record.
419 mrec
= read_mrecord(0, &error
, &pickup
);
421 if (mrec
&& mrec
->head
.type
== HAMMER_MREC_TYPE_TERM
) {
422 fprintf(stderr
, "Mirror-write: received termination request\n");
428 (mrec
->head
.type
!= HAMMER_MREC_TYPE_SYNC
&&
429 mrec
->head
.type
!= HAMMER_MREC_TYPE_IDLE
) ||
430 mrec
->head
.rec_size
!= sizeof(mrec
->sync
)) {
431 fprintf(stderr
, "Mirror-write %s: Did not get termination "
432 "sync record, or rec_size is wrong rt=%d\n",
433 filesystem
, mrec
->head
.type
);
438 * Update the PFS info on the target so the user has visibility
439 * into the new snapshot, and sync the target filesystem.
441 if (mrec
->head
.type
== HAMMER_MREC_TYPE_SYNC
) {
442 update_pfs_snapshot(fd
, mirror
.tid_end
, pfs
.pfs_id
);
444 bzero(&synctid
, sizeof(synctid
));
445 synctid
.op
= HAMMER_SYNCTID_SYNC2
;
446 ioctl(fd
, HAMMERIOC_SYNCTID
, &synctid
);
448 if (VerboseOpt
>= 2) {
449 fprintf(stderr
, "Mirror-write %s: succeeded\n",
458 * Report back to the originator.
461 mrec_tmp
.update
.tid
= mirror
.tid_end
;
462 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE
,
463 &mrec_tmp
, sizeof(mrec_tmp
.update
));
465 printf("Source can update synctid to 0x%016llx\n",
473 hammer_cmd_mirror_dump(void)
475 char *buf
= malloc(SERIALBUF_SIZE
);
476 struct hammer_ioc_mrecord_head pickup
;
477 hammer_ioc_mrecord_any_t mrec
;
484 * Read and process the PFS header
486 pickup
.signature
= 0;
489 mrec
= read_mrecord(0, &error
, &pickup
);
492 * Read and process bulk records
495 size
= read_mrecords(0, buf
, SERIALBUF_SIZE
, &pickup
);
499 while (offset
< size
) {
500 mrec
= (void *)((char *)buf
+ offset
);
501 bytes
= HAMMER_HEAD_DOALIGN(mrec
->head
.rec_size
);
502 if (offset
+ bytes
> size
) {
503 fprintf(stderr
, "Misaligned record\n");
507 switch(mrec
->head
.type
) {
508 case HAMMER_MREC_TYPE_REC
:
509 printf("Record obj=%016llx key=%016llx "
511 mrec
->rec
.leaf
.base
.obj_id
,
512 mrec
->rec
.leaf
.base
.key
,
513 mrec
->rec
.leaf
.base
.rec_type
,
514 mrec
->rec
.leaf
.base
.obj_type
);
515 printf(" tids %016llx:%016llx data=%d\n",
516 mrec
->rec
.leaf
.base
.create_tid
,
517 mrec
->rec
.leaf
.base
.delete_tid
,
518 mrec
->rec
.leaf
.data_len
);
520 case HAMMER_MREC_TYPE_PASS
:
521 printf("Pass obj=%016llx key=%016llx "
523 mrec
->rec
.leaf
.base
.obj_id
,
524 mrec
->rec
.leaf
.base
.key
,
525 mrec
->rec
.leaf
.base
.rec_type
,
526 mrec
->rec
.leaf
.base
.obj_type
);
527 printf(" tids %016llx:%016llx data=%d\n",
528 mrec
->rec
.leaf
.base
.create_tid
,
529 mrec
->rec
.leaf
.base
.delete_tid
,
530 mrec
->rec
.leaf
.data_len
);
532 case HAMMER_MREC_TYPE_SKIP
:
533 printf("Skip obj=%016llx key=%016llx rt=%02x to\n"
534 " obj=%016llx key=%016llx rt=%02x\n",
535 mrec
->skip
.skip_beg
.obj_id
,
536 mrec
->skip
.skip_beg
.key
,
537 mrec
->skip
.skip_beg
.rec_type
,
538 mrec
->skip
.skip_end
.obj_id
,
539 mrec
->skip
.skip_end
.key
,
540 mrec
->skip
.skip_end
.rec_type
);
549 * Read and process the termination sync record.
551 mrec
= read_mrecord(0, &error
, &pickup
);
553 (mrec
->head
.type
!= HAMMER_MREC_TYPE_SYNC
&&
554 mrec
->head
.type
!= HAMMER_MREC_TYPE_IDLE
)
556 fprintf(stderr
, "Mirror-dump: Did not get termination "
562 hammer_cmd_mirror_copy(char **av
, int ac
, int streaming
)
585 if ((pid1
= fork()) == 0) {
590 if ((ptr
= strchr(av
[0], ':')) != NULL
) {
595 xav
[xac
++] = "hammer";
612 snprintf(tbuf
, sizeof(tbuf
), "%d", TimeoutOpt
);
617 xav
[xac
++] = "mirror-read-streaming";
619 xav
[xac
++] = "mirror-read";
622 execv("/usr/bin/ssh", (void *)xav
);
624 hammer_cmd_mirror_read(av
, 1, streaming
);
634 if ((pid2
= fork()) == 0) {
639 if ((ptr
= strchr(av
[1], ':')) != NULL
) {
644 xav
[xac
++] = "hammer";
661 xav
[xac
++] = "mirror-write";
664 execv("/usr/bin/ssh", (void *)xav
);
666 hammer_cmd_mirror_write(av
+ 1, 1);
675 while (waitpid(pid1
, NULL
, 0) <= 0)
677 while (waitpid(pid2
, NULL
, 0) <= 0)
682 * Read and return multiple mrecords
685 read_mrecords(int fd
, char *buf
, u_int size
, hammer_ioc_mrecord_head_t pickup
)
687 hammer_ioc_mrecord_any_t mrec
;
694 while (size
- count
>= HAMMER_MREC_HEADSIZE
) {
696 * Cached the record header in case we run out of buffer
700 if (pickup
->signature
== 0) {
701 for (n
= 0; n
< HAMMER_MREC_HEADSIZE
; n
+= i
) {
702 i
= read(fd
, (char *)pickup
+ n
,
703 HAMMER_MREC_HEADSIZE
- n
);
709 if (n
!= HAMMER_MREC_HEADSIZE
) {
710 fprintf(stderr
, "read_mrecords: short read on pipe\n");
714 if (pickup
->signature
!= HAMMER_IOC_MIRROR_SIGNATURE
) {
715 fprintf(stderr
, "read_mrecords: malformed record on pipe, bad signature\n");
719 if (pickup
->rec_size
< HAMMER_MREC_HEADSIZE
||
720 pickup
->rec_size
> sizeof(*mrec
) + HAMMER_XBUFSIZE
) {
721 fprintf(stderr
, "read_mrecords: malformed record on pipe, illegal rec_size\n");
726 * Stop if we have insufficient space for the record and data.
728 bytes
= HAMMER_HEAD_DOALIGN(pickup
->rec_size
);
729 if (size
- count
< bytes
)
733 * Stop if the record type is not a REC or a SKIP (the only
734 * two types the ioctl supports. Other types are used only
735 * by the userland protocol).
737 if (pickup
->type
!= HAMMER_MREC_TYPE_REC
&&
738 pickup
->type
!= HAMMER_MREC_TYPE_SKIP
&&
739 pickup
->type
!= HAMMER_MREC_TYPE_PASS
) {
744 * Read the remainder and clear the pickup signature.
746 for (n
= HAMMER_MREC_HEADSIZE
; n
< bytes
; n
+= i
) {
747 i
= read(fd
, buf
+ count
+ n
, bytes
- n
);
752 fprintf(stderr
, "read_mrecords: short read on pipe\n");
756 bcopy(pickup
, buf
+ count
, HAMMER_MREC_HEADSIZE
);
757 pickup
->signature
= 0;
759 mrec
= (void *)(buf
+ count
);
762 * Validate the completed record
764 if (mrec
->head
.rec_crc
!=
765 crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
766 mrec
->head
.rec_size
- HAMMER_MREC_CRCOFF
)) {
767 fprintf(stderr
, "read_mrecords: malformed record "
768 "on pipe, bad crc\n");
773 * If its a B-Tree record validate the data crc
775 if (mrec
->head
.type
== HAMMER_MREC_TYPE_REC
) {
776 if (mrec
->head
.rec_size
<
777 sizeof(mrec
->rec
) + mrec
->rec
.leaf
.data_len
) {
779 "read_mrecords: malformed record on "
780 "pipe, illegal element data_len\n");
783 if (mrec
->rec
.leaf
.data_len
&&
784 mrec
->rec
.leaf
.data_offset
&&
785 hammer_crc_test_leaf(&mrec
->rec
+ 1, &mrec
->rec
.leaf
) == 0) {
787 "read_mrecords: data_crc did not "
788 "match data! obj=%016llx key=%016llx\n",
789 mrec
->rec
.leaf
.base
.obj_id
,
790 mrec
->rec
.leaf
.base
.key
);
792 "continuing, but there are problems\n");
801 * Read and return a single mrecord.
804 hammer_ioc_mrecord_any_t
805 read_mrecord(int fdin
, int *errorp
, hammer_ioc_mrecord_head_t pickup
)
807 hammer_ioc_mrecord_any_t mrec
;
808 struct hammer_ioc_mrecord_head mrechd
;
813 if (pickup
&& pickup
->type
!= 0) {
815 pickup
->signature
= 0;
817 n
= HAMMER_MREC_HEADSIZE
;
820 * Read in the PFSD header from the sender.
822 for (n
= 0; n
< HAMMER_MREC_HEADSIZE
; n
+= i
) {
823 i
= read(fdin
, (char *)&mrechd
+ n
, HAMMER_MREC_HEADSIZE
- n
);
828 *errorp
= 0; /* EOF */
831 if (n
!= HAMMER_MREC_HEADSIZE
) {
832 fprintf(stderr
, "short read of mrecord header\n");
837 if (mrechd
.signature
!= HAMMER_IOC_MIRROR_SIGNATURE
) {
838 fprintf(stderr
, "read_mrecord: bad signature\n");
842 bytes
= HAMMER_HEAD_DOALIGN(mrechd
.rec_size
);
843 assert(bytes
>= sizeof(mrechd
));
844 mrec
= malloc(bytes
);
848 i
= read(fdin
, (char *)mrec
+ n
, bytes
- n
);
854 fprintf(stderr
, "read_mrecord: short read on payload\n");
858 if (mrec
->head
.rec_crc
!=
859 crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
860 mrec
->head
.rec_size
- HAMMER_MREC_CRCOFF
)) {
861 fprintf(stderr
, "read_mrecord: bad CRC\n");
871 write_mrecord(int fdout
, u_int32_t type
, hammer_ioc_mrecord_any_t mrec
,
874 char zbuf
[HAMMER_HEAD_ALIGN
];
877 pad
= HAMMER_HEAD_DOALIGN(bytes
) - bytes
;
879 assert(bytes
>= (int)sizeof(mrec
->head
));
880 bzero(&mrec
->head
, sizeof(mrec
->head
));
881 mrec
->head
.signature
= HAMMER_IOC_MIRROR_SIGNATURE
;
882 mrec
->head
.type
= type
;
883 mrec
->head
.rec_size
= bytes
;
884 mrec
->head
.rec_crc
= crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
885 bytes
- HAMMER_MREC_CRCOFF
);
886 if (write(fdout
, mrec
, bytes
) != bytes
) {
887 fprintf(stderr
, "write_mrecord: error %d (%s)\n",
888 errno
, strerror(errno
));
893 if (write(fdout
, zbuf
, pad
) != pad
) {
894 fprintf(stderr
, "write_mrecord: error %d (%s)\n",
895 errno
, strerror(errno
));
902 * Generate a mirroring header with the pfs information of the
903 * originating filesytem.
906 generate_mrec_header(int fd
, int fdout
, int pfs_id
,
907 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
)
909 struct hammer_ioc_pseudofs_rw pfs
;
910 union hammer_ioc_mrecord_any mrec_tmp
;
912 bzero(&pfs
, sizeof(pfs
));
913 bzero(&mrec_tmp
, sizeof(mrec_tmp
));
915 pfs
.ondisk
= &mrec_tmp
.pfs
.pfsd
;
916 pfs
.bytes
= sizeof(mrec_tmp
.pfs
.pfsd
);
917 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
918 fprintf(stderr
, "Mirror-read: not a HAMMER fs/pseudofs!\n");
921 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
922 fprintf(stderr
, "Mirror-read: HAMMER pfs version mismatch!\n");
927 * sync_beg_tid - lowest TID on source after which a full history
930 * sync_end_tid - highest fully synchronized TID from source.
932 if (tid_begp
&& *tid_begp
< mrec_tmp
.pfs
.pfsd
.sync_beg_tid
)
933 *tid_begp
= mrec_tmp
.pfs
.pfsd
.sync_beg_tid
;
935 *tid_endp
= mrec_tmp
.pfs
.pfsd
.sync_end_tid
;
936 mrec_tmp
.pfs
.version
= pfs
.version
;
937 write_mrecord(fdout
, HAMMER_MREC_TYPE_PFSD
,
938 &mrec_tmp
, sizeof(mrec_tmp
.pfs
));
942 * Validate the pfs information from the originating filesystem
943 * against the target filesystem. shared_uuid must match.
945 * return -1 if we got a TERM record
948 validate_mrec_header(int fd
, int fdin
, int is_target
, int pfs_id
,
949 struct hammer_ioc_mrecord_head
*pickup
,
950 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
)
952 struct hammer_ioc_pseudofs_rw pfs
;
953 struct hammer_pseudofs_data pfsd
;
954 hammer_ioc_mrecord_any_t mrec
;
958 * Get the PFSD info from the target filesystem.
960 bzero(&pfs
, sizeof(pfs
));
961 bzero(&pfsd
, sizeof(pfsd
));
964 pfs
.bytes
= sizeof(pfsd
);
965 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
966 fprintf(stderr
, "mirror-write: not a HAMMER fs/pseudofs!\n");
969 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
970 fprintf(stderr
, "mirror-write: HAMMER pfs version mismatch!\n");
974 mrec
= read_mrecord(fdin
, &error
, pickup
);
977 fprintf(stderr
, "validate_mrec_header: short read\n");
980 if (mrec
->head
.type
== HAMMER_MREC_TYPE_TERM
) {
985 if (mrec
->head
.type
!= HAMMER_MREC_TYPE_PFSD
) {
986 fprintf(stderr
, "validate_mrec_header: did not get expected "
987 "PFSD record type\n");
990 if (mrec
->head
.rec_size
!= sizeof(mrec
->pfs
)) {
991 fprintf(stderr
, "validate_mrec_header: unexpected payload "
995 if (mrec
->pfs
.version
!= pfs
.version
) {
996 fprintf(stderr
, "validate_mrec_header: Version mismatch\n");
1001 * Whew. Ok, is the read PFS info compatible with the target?
1003 if (bcmp(&mrec
->pfs
.pfsd
.shared_uuid
, &pfsd
.shared_uuid
,
1004 sizeof(pfsd
.shared_uuid
)) != 0) {
1006 "mirror-write: source and target have "
1007 "different shared-uuid's!\n");
1011 (pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) == 0) {
1012 fprintf(stderr
, "mirror-write: target must be in slave mode\n");
1016 *tid_begp
= mrec
->pfs
.pfsd
.sync_beg_tid
;
1018 *tid_endp
= mrec
->pfs
.pfsd
.sync_end_tid
;
1024 update_pfs_snapshot(int fd
, hammer_tid_t snapshot_tid
, int pfs_id
)
1026 struct hammer_ioc_pseudofs_rw pfs
;
1027 struct hammer_pseudofs_data pfsd
;
1029 bzero(&pfs
, sizeof(pfs
));
1030 bzero(&pfsd
, sizeof(pfsd
));
1031 pfs
.pfs_id
= pfs_id
;
1033 pfs
.bytes
= sizeof(pfsd
);
1034 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
1035 perror("update_pfs_snapshot (read)");
1038 if (pfsd
.sync_end_tid
!= snapshot_tid
) {
1039 pfsd
.sync_end_tid
= snapshot_tid
;
1040 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) != 0) {
1041 perror("update_pfs_snapshot (rewrite)");
1044 if (VerboseOpt
>= 2) {
1046 "Mirror-write: Completed, updated snapshot "
1054 * Bandwidth-limited write in chunks
1058 writebw(int fd
, const void *buf
, size_t nbytes
,
1059 u_int64_t
*bwcount
, struct timeval
*tv1
)
1070 if (*bwcount
+ nbytes
> BandwidthOpt
)
1071 n
= BandwidthOpt
- *bwcount
;
1075 r
= write(fd
, buf
, n
);
1079 buf
= (const char *)buf
+ r
;
1084 if (*bwcount
>= BandwidthOpt
) {
1085 gettimeofday(&tv2
, NULL
);
1086 usec
= (int)(tv2
.tv_sec
- tv1
->tv_sec
) * 1000000 +
1087 (int)(tv2
.tv_usec
- tv1
->tv_usec
);
1088 if (usec
>= 0 && usec
< 1000000)
1089 usleep(1000000 - usec
);
1090 gettimeofday(tv1
, NULL
);
1091 *bwcount
-= BandwidthOpt
;
1098 mirror_usage(int code
)
1101 "hammer mirror-read <filesystem>\n"
1102 "hammer mirror-write <filesystem>\n"
1103 "hammer mirror-dump\n"
1104 "hammer mirror-copy [[user@]host:]fs [[user@]host:]fs\n"