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.4 2008/09/09 23:41:13 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
);
86 if (ac
== 0 || ac
> 2)
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 "
249 "continuation\n", CyclePath
);
252 sync_tid
= mrec
->update
.tid
;
254 hammer_key_beg_init(&mirror
.key_beg
);
255 hammer_set_cycle(&mirror
.key_beg
, sync_tid
);
256 fprintf(stderr
, "Cyclefile %s updated to 0x%016llx\n",
257 CyclePath
, sync_tid
);
260 } else if (CyclePath
) {
261 /* NOTE! mirror.tid_beg cannot be updated */
262 fprintf(stderr
, "Warning: cycle file (-c option) cannot be "
263 "fully updated unless you use mirror-copy\n");
264 hammer_set_cycle(&mirror
.key_beg
, mirror
.tid_beg
);
266 if (streaming
&& interrupted
== 0) {
267 time_t t1
= time(NULL
);
271 fprintf(stderr
, " W");
274 pfs
.ondisk
->sync_end_tid
= mirror
.tid_end
;
275 if (ioctl(fd
, HAMMERIOC_WAI_PSEUDOFS
, &pfs
) < 0) {
276 fprintf(stderr
, "Mirror-read %s: cannot stream: %s\n",
277 filesystem
, strerror(errno
));
279 t2
= time(NULL
) - t1
;
280 if (t2
>= 0 && t2
< DelayOpt
) {
282 fprintf(stderr
, "\bD");
285 sleep(DelayOpt
- t2
);
288 fprintf(stderr
, "\b ");
295 write_mrecord(1, HAMMER_MREC_TYPE_TERM
,
296 &mrec_tmp
, sizeof(mrec_tmp
.sync
));
298 fprintf(stderr
, "Mirror-read %s succeeded\n", filesystem
);
302 * Pipe the mirroring data stream on stdin to the HAMMER VFS, adding
303 * some additional packet types to negotiate TID ranges and to verify
304 * completion. The HAMMER VFS does most of the work.
306 * It is important to note that the mirror.key_{beg,end} range must
307 * match the ranged used by the original. For now both sides use
308 * range the entire key space.
310 * It is even more important that the records in the stream conform
311 * to the TID range also supplied in the stream. The HAMMER VFS will
312 * use the REC, PASS, and SKIP record types to track the portions of
313 * the B-Tree being scanned in order to be able to proactively delete
314 * records on the target within those active areas that are not mentioned
317 * The mirror.key_cur field is used by the VFS to do this tracking. It
318 * must be initialized to key_beg but then is persistently updated by
319 * the HAMMER VFS on each successive ioctl() call. If you blow up this
320 * field you will blow up the mirror target, possibly to the point of
321 * deleting everything. As a safety measure the HAMMER VFS simply marks
322 * the records that the source has destroyed as deleted on the target,
323 * and normal pruning operations will deal with their final disposition
324 * at some later time.
327 hammer_cmd_mirror_write(char **av
, int ac
)
329 struct hammer_ioc_mirror_rw mirror
;
330 const char *filesystem
;
331 char *buf
= malloc(SERIALBUF_SIZE
);
332 struct hammer_ioc_pseudofs_rw pfs
;
333 struct hammer_ioc_mrecord_head pickup
;
334 struct hammer_ioc_synctid synctid
;
335 union hammer_ioc_mrecord_any mrec_tmp
;
336 hammer_ioc_mrecord_any_t mrec
;
345 pickup
.signature
= 0;
349 bzero(&mirror
, sizeof(mirror
));
350 hammer_key_beg_init(&mirror
.key_beg
);
351 hammer_key_end_init(&mirror
.key_end
);
352 mirror
.key_end
= mirror
.key_beg
;
354 fd
= getpfs(&pfs
, filesystem
);
357 * In two-way mode the target writes out a PFS packet first.
358 * The source uses our tid_end as its tid_beg by default,
359 * picking up where it left off.
363 generate_mrec_header(fd
, 1, pfs
.pfs_id
,
364 &mirror
.tid_beg
, &mirror
.tid_end
);
368 * Read and process the PFS header. The source informs us of
369 * the TID range the stream represents.
371 n
= validate_mrec_header(fd
, 0, 1, pfs
.pfs_id
, &pickup
,
372 &mirror
.tid_beg
, &mirror
.tid_end
);
373 if (n
< 0) { /* got TERM record */
379 mirror
.size
= SERIALBUF_SIZE
;
382 * Read and process bulk records (REC, PASS, and SKIP types).
384 * On your life, do NOT mess with mirror.key_cur or your mirror
385 * target may become history.
389 mirror
.pfs_id
= pfs
.pfs_id
;
390 mirror
.shared_uuid
= pfs
.ondisk
->shared_uuid
;
391 mirror
.size
= read_mrecords(0, buf
, SERIALBUF_SIZE
, &pickup
);
392 if (mirror
.size
<= 0)
394 if (ioctl(fd
, HAMMERIOC_MIRROR_WRITE
, &mirror
) < 0) {
395 fprintf(stderr
, "Mirror-write %s failed: %s\n",
396 filesystem
, strerror(errno
));
399 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_ERROR
) {
401 "Mirror-write %s fatal error %d\n",
402 filesystem
, mirror
.head
.error
);
406 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_INTR
) {
408 "Mirror-write %s interrupted by timer at"
411 mirror
.key_cur
.obj_id
);
418 * Read and process the termination sync record.
420 mrec
= read_mrecord(0, &error
, &pickup
);
422 if (mrec
&& mrec
->head
.type
== HAMMER_MREC_TYPE_TERM
) {
423 fprintf(stderr
, "Mirror-write: received termination request\n");
429 (mrec
->head
.type
!= HAMMER_MREC_TYPE_SYNC
&&
430 mrec
->head
.type
!= HAMMER_MREC_TYPE_IDLE
) ||
431 mrec
->head
.rec_size
!= sizeof(mrec
->sync
)) {
432 fprintf(stderr
, "Mirror-write %s: Did not get termination "
433 "sync record, or rec_size is wrong rt=%d\n",
434 filesystem
, mrec
->head
.type
);
439 * Update the PFS info on the target so the user has visibility
440 * into the new snapshot, and sync the target filesystem.
442 if (mrec
->head
.type
== HAMMER_MREC_TYPE_SYNC
) {
443 update_pfs_snapshot(fd
, mirror
.tid_end
, pfs
.pfs_id
);
445 bzero(&synctid
, sizeof(synctid
));
446 synctid
.op
= HAMMER_SYNCTID_SYNC2
;
447 ioctl(fd
, HAMMERIOC_SYNCTID
, &synctid
);
449 if (VerboseOpt
>= 2) {
450 fprintf(stderr
, "Mirror-write %s: succeeded\n",
459 * Report back to the originator.
462 mrec_tmp
.update
.tid
= mirror
.tid_end
;
463 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE
,
464 &mrec_tmp
, sizeof(mrec_tmp
.update
));
466 printf("Source can update synctid to 0x%016llx\n",
474 hammer_cmd_mirror_dump(void)
476 char *buf
= malloc(SERIALBUF_SIZE
);
477 struct hammer_ioc_mrecord_head pickup
;
478 hammer_ioc_mrecord_any_t mrec
;
485 * Read and process the PFS header
487 pickup
.signature
= 0;
490 mrec
= read_mrecord(0, &error
, &pickup
);
493 * Read and process bulk records
496 size
= read_mrecords(0, buf
, SERIALBUF_SIZE
, &pickup
);
500 while (offset
< size
) {
501 mrec
= (void *)((char *)buf
+ offset
);
502 bytes
= HAMMER_HEAD_DOALIGN(mrec
->head
.rec_size
);
503 if (offset
+ bytes
> size
) {
504 fprintf(stderr
, "Misaligned record\n");
508 switch(mrec
->head
.type
) {
509 case HAMMER_MREC_TYPE_REC
:
510 printf("Record obj=%016llx key=%016llx "
512 mrec
->rec
.leaf
.base
.obj_id
,
513 mrec
->rec
.leaf
.base
.key
,
514 mrec
->rec
.leaf
.base
.rec_type
,
515 mrec
->rec
.leaf
.base
.obj_type
);
516 printf(" tids %016llx:%016llx data=%d\n",
517 mrec
->rec
.leaf
.base
.create_tid
,
518 mrec
->rec
.leaf
.base
.delete_tid
,
519 mrec
->rec
.leaf
.data_len
);
521 case HAMMER_MREC_TYPE_PASS
:
522 printf("Pass obj=%016llx key=%016llx "
524 mrec
->rec
.leaf
.base
.obj_id
,
525 mrec
->rec
.leaf
.base
.key
,
526 mrec
->rec
.leaf
.base
.rec_type
,
527 mrec
->rec
.leaf
.base
.obj_type
);
528 printf(" tids %016llx:%016llx data=%d\n",
529 mrec
->rec
.leaf
.base
.create_tid
,
530 mrec
->rec
.leaf
.base
.delete_tid
,
531 mrec
->rec
.leaf
.data_len
);
533 case HAMMER_MREC_TYPE_SKIP
:
534 printf("Skip obj=%016llx key=%016llx rt=%02x to\n"
535 " obj=%016llx key=%016llx rt=%02x\n",
536 mrec
->skip
.skip_beg
.obj_id
,
537 mrec
->skip
.skip_beg
.key
,
538 mrec
->skip
.skip_beg
.rec_type
,
539 mrec
->skip
.skip_end
.obj_id
,
540 mrec
->skip
.skip_end
.key
,
541 mrec
->skip
.skip_end
.rec_type
);
550 * Read and process the termination sync record.
552 mrec
= read_mrecord(0, &error
, &pickup
);
554 (mrec
->head
.type
!= HAMMER_MREC_TYPE_SYNC
&&
555 mrec
->head
.type
!= HAMMER_MREC_TYPE_IDLE
)
557 fprintf(stderr
, "Mirror-dump: Did not get termination "
563 hammer_cmd_mirror_copy(char **av
, int ac
, int streaming
)
586 if ((pid1
= fork()) == 0) {
591 if ((ptr
= strchr(av
[0], ':')) != NULL
) {
596 xav
[xac
++] = "hammer";
613 snprintf(tbuf
, sizeof(tbuf
), "%d", TimeoutOpt
);
618 xav
[xac
++] = "mirror-read-stream";
620 xav
[xac
++] = "mirror-read";
623 execv("/usr/bin/ssh", (void *)xav
);
625 hammer_cmd_mirror_read(av
, 1, streaming
);
635 if ((pid2
= fork()) == 0) {
640 if ((ptr
= strchr(av
[1], ':')) != NULL
) {
645 xav
[xac
++] = "hammer";
662 xav
[xac
++] = "mirror-write";
665 execv("/usr/bin/ssh", (void *)xav
);
667 hammer_cmd_mirror_write(av
+ 1, 1);
676 while (waitpid(pid1
, NULL
, 0) <= 0)
678 while (waitpid(pid2
, NULL
, 0) <= 0)
683 * Read and return multiple mrecords
686 read_mrecords(int fd
, char *buf
, u_int size
, hammer_ioc_mrecord_head_t pickup
)
688 hammer_ioc_mrecord_any_t mrec
;
695 while (size
- count
>= HAMMER_MREC_HEADSIZE
) {
697 * Cached the record header in case we run out of buffer
701 if (pickup
->signature
== 0) {
702 for (n
= 0; n
< HAMMER_MREC_HEADSIZE
; n
+= i
) {
703 i
= read(fd
, (char *)pickup
+ n
,
704 HAMMER_MREC_HEADSIZE
- n
);
710 if (n
!= HAMMER_MREC_HEADSIZE
) {
711 fprintf(stderr
, "read_mrecords: short read on pipe\n");
715 if (pickup
->signature
!= HAMMER_IOC_MIRROR_SIGNATURE
) {
716 fprintf(stderr
, "read_mrecords: malformed record on pipe, "
721 if (pickup
->rec_size
< HAMMER_MREC_HEADSIZE
||
722 pickup
->rec_size
> sizeof(*mrec
) + HAMMER_XBUFSIZE
) {
723 fprintf(stderr
, "read_mrecords: malformed record on pipe, "
724 "illegal rec_size\n");
729 * Stop if we have insufficient space for the record and data.
731 bytes
= HAMMER_HEAD_DOALIGN(pickup
->rec_size
);
732 if (size
- count
< bytes
)
736 * Stop if the record type is not a REC or a SKIP (the only
737 * two types the ioctl supports. Other types are used only
738 * by the userland protocol).
740 if (pickup
->type
!= HAMMER_MREC_TYPE_REC
&&
741 pickup
->type
!= HAMMER_MREC_TYPE_SKIP
&&
742 pickup
->type
!= HAMMER_MREC_TYPE_PASS
) {
747 * Read the remainder and clear the pickup signature.
749 for (n
= HAMMER_MREC_HEADSIZE
; n
< bytes
; n
+= i
) {
750 i
= read(fd
, buf
+ count
+ n
, bytes
- n
);
755 fprintf(stderr
, "read_mrecords: short read on pipe\n");
759 bcopy(pickup
, buf
+ count
, HAMMER_MREC_HEADSIZE
);
760 pickup
->signature
= 0;
762 mrec
= (void *)(buf
+ count
);
765 * Validate the completed record
767 if (mrec
->head
.rec_crc
!=
768 crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
769 mrec
->head
.rec_size
- HAMMER_MREC_CRCOFF
)) {
770 fprintf(stderr
, "read_mrecords: malformed record "
771 "on pipe, bad crc\n");
776 * If its a B-Tree record validate the data crc
778 if (mrec
->head
.type
== HAMMER_MREC_TYPE_REC
) {
779 if (mrec
->head
.rec_size
<
780 sizeof(mrec
->rec
) + mrec
->rec
.leaf
.data_len
) {
782 "read_mrecords: malformed record on "
783 "pipe, illegal element data_len\n");
786 if (mrec
->rec
.leaf
.data_len
&&
787 mrec
->rec
.leaf
.data_offset
&&
788 hammer_crc_test_leaf(&mrec
->rec
+ 1, &mrec
->rec
.leaf
) == 0) {
790 "read_mrecords: data_crc did not "
791 "match data! obj=%016llx key=%016llx\n",
792 mrec
->rec
.leaf
.base
.obj_id
,
793 mrec
->rec
.leaf
.base
.key
);
795 "continuing, but there are problems\n");
804 * Read and return a single mrecord.
807 hammer_ioc_mrecord_any_t
808 read_mrecord(int fdin
, int *errorp
, hammer_ioc_mrecord_head_t pickup
)
810 hammer_ioc_mrecord_any_t mrec
;
811 struct hammer_ioc_mrecord_head mrechd
;
816 if (pickup
&& pickup
->type
!= 0) {
818 pickup
->signature
= 0;
820 n
= HAMMER_MREC_HEADSIZE
;
823 * Read in the PFSD header from the sender.
825 for (n
= 0; n
< HAMMER_MREC_HEADSIZE
; n
+= i
) {
826 i
= read(fdin
, (char *)&mrechd
+ n
, HAMMER_MREC_HEADSIZE
- n
);
831 *errorp
= 0; /* EOF */
834 if (n
!= HAMMER_MREC_HEADSIZE
) {
835 fprintf(stderr
, "short read of mrecord header\n");
840 if (mrechd
.signature
!= HAMMER_IOC_MIRROR_SIGNATURE
) {
841 fprintf(stderr
, "read_mrecord: bad signature\n");
845 bytes
= HAMMER_HEAD_DOALIGN(mrechd
.rec_size
);
846 assert(bytes
>= sizeof(mrechd
));
847 mrec
= malloc(bytes
);
851 i
= read(fdin
, (char *)mrec
+ n
, bytes
- n
);
857 fprintf(stderr
, "read_mrecord: short read on payload\n");
861 if (mrec
->head
.rec_crc
!=
862 crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
863 mrec
->head
.rec_size
- HAMMER_MREC_CRCOFF
)) {
864 fprintf(stderr
, "read_mrecord: bad CRC\n");
874 write_mrecord(int fdout
, u_int32_t type
, hammer_ioc_mrecord_any_t mrec
,
877 char zbuf
[HAMMER_HEAD_ALIGN
];
880 pad
= HAMMER_HEAD_DOALIGN(bytes
) - bytes
;
882 assert(bytes
>= (int)sizeof(mrec
->head
));
883 bzero(&mrec
->head
, sizeof(mrec
->head
));
884 mrec
->head
.signature
= HAMMER_IOC_MIRROR_SIGNATURE
;
885 mrec
->head
.type
= type
;
886 mrec
->head
.rec_size
= bytes
;
887 mrec
->head
.rec_crc
= crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
888 bytes
- HAMMER_MREC_CRCOFF
);
889 if (write(fdout
, mrec
, bytes
) != bytes
) {
890 fprintf(stderr
, "write_mrecord: error %d (%s)\n",
891 errno
, strerror(errno
));
896 if (write(fdout
, zbuf
, pad
) != pad
) {
897 fprintf(stderr
, "write_mrecord: error %d (%s)\n",
898 errno
, strerror(errno
));
905 * Generate a mirroring header with the pfs information of the
906 * originating filesytem.
909 generate_mrec_header(int fd
, int fdout
, int pfs_id
,
910 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
)
912 struct hammer_ioc_pseudofs_rw pfs
;
913 union hammer_ioc_mrecord_any mrec_tmp
;
915 bzero(&pfs
, sizeof(pfs
));
916 bzero(&mrec_tmp
, sizeof(mrec_tmp
));
918 pfs
.ondisk
= &mrec_tmp
.pfs
.pfsd
;
919 pfs
.bytes
= sizeof(mrec_tmp
.pfs
.pfsd
);
920 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
921 fprintf(stderr
, "Mirror-read: not a HAMMER fs/pseudofs!\n");
924 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
925 fprintf(stderr
, "Mirror-read: HAMMER pfs version mismatch!\n");
930 * sync_beg_tid - lowest TID on source after which a full history
933 * sync_end_tid - highest fully synchronized TID from source.
935 if (tid_begp
&& *tid_begp
< mrec_tmp
.pfs
.pfsd
.sync_beg_tid
)
936 *tid_begp
= mrec_tmp
.pfs
.pfsd
.sync_beg_tid
;
938 *tid_endp
= mrec_tmp
.pfs
.pfsd
.sync_end_tid
;
939 mrec_tmp
.pfs
.version
= pfs
.version
;
940 write_mrecord(fdout
, HAMMER_MREC_TYPE_PFSD
,
941 &mrec_tmp
, sizeof(mrec_tmp
.pfs
));
945 * Validate the pfs information from the originating filesystem
946 * against the target filesystem. shared_uuid must match.
948 * return -1 if we got a TERM record
951 validate_mrec_header(int fd
, int fdin
, int is_target
, int pfs_id
,
952 struct hammer_ioc_mrecord_head
*pickup
,
953 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
)
955 struct hammer_ioc_pseudofs_rw pfs
;
956 struct hammer_pseudofs_data pfsd
;
957 hammer_ioc_mrecord_any_t mrec
;
961 * Get the PFSD info from the target filesystem.
963 bzero(&pfs
, sizeof(pfs
));
964 bzero(&pfsd
, sizeof(pfsd
));
967 pfs
.bytes
= sizeof(pfsd
);
968 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
969 fprintf(stderr
, "mirror-write: not a HAMMER fs/pseudofs!\n");
972 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
973 fprintf(stderr
, "mirror-write: HAMMER pfs version mismatch!\n");
977 mrec
= read_mrecord(fdin
, &error
, pickup
);
980 fprintf(stderr
, "validate_mrec_header: short read\n");
983 if (mrec
->head
.type
== HAMMER_MREC_TYPE_TERM
) {
988 if (mrec
->head
.type
!= HAMMER_MREC_TYPE_PFSD
) {
989 fprintf(stderr
, "validate_mrec_header: did not get expected "
990 "PFSD record type\n");
993 if (mrec
->head
.rec_size
!= sizeof(mrec
->pfs
)) {
994 fprintf(stderr
, "validate_mrec_header: unexpected payload "
998 if (mrec
->pfs
.version
!= pfs
.version
) {
999 fprintf(stderr
, "validate_mrec_header: Version mismatch\n");
1004 * Whew. Ok, is the read PFS info compatible with the target?
1006 if (bcmp(&mrec
->pfs
.pfsd
.shared_uuid
, &pfsd
.shared_uuid
,
1007 sizeof(pfsd
.shared_uuid
)) != 0) {
1009 "mirror-write: source and target have "
1010 "different shared-uuid's!\n");
1014 (pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) == 0) {
1015 fprintf(stderr
, "mirror-write: target must be in slave mode\n");
1019 *tid_begp
= mrec
->pfs
.pfsd
.sync_beg_tid
;
1021 *tid_endp
= mrec
->pfs
.pfsd
.sync_end_tid
;
1027 update_pfs_snapshot(int fd
, hammer_tid_t snapshot_tid
, int pfs_id
)
1029 struct hammer_ioc_pseudofs_rw pfs
;
1030 struct hammer_pseudofs_data pfsd
;
1032 bzero(&pfs
, sizeof(pfs
));
1033 bzero(&pfsd
, sizeof(pfsd
));
1034 pfs
.pfs_id
= pfs_id
;
1036 pfs
.bytes
= sizeof(pfsd
);
1037 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
1038 perror("update_pfs_snapshot (read)");
1041 if (pfsd
.sync_end_tid
!= snapshot_tid
) {
1042 pfsd
.sync_end_tid
= snapshot_tid
;
1043 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) != 0) {
1044 perror("update_pfs_snapshot (rewrite)");
1047 if (VerboseOpt
>= 2) {
1049 "Mirror-write: Completed, updated snapshot "
1057 * Bandwidth-limited write in chunks
1061 writebw(int fd
, const void *buf
, size_t nbytes
,
1062 u_int64_t
*bwcount
, struct timeval
*tv1
)
1073 if (*bwcount
+ nbytes
> BandwidthOpt
)
1074 n
= BandwidthOpt
- *bwcount
;
1078 r
= write(fd
, buf
, n
);
1082 buf
= (const char *)buf
+ r
;
1087 if (*bwcount
>= BandwidthOpt
) {
1088 gettimeofday(&tv2
, NULL
);
1089 usec
= (int)(tv2
.tv_sec
- tv1
->tv_sec
) * 1000000 +
1090 (int)(tv2
.tv_usec
- tv1
->tv_usec
);
1091 if (usec
>= 0 && usec
< 1000000)
1092 usleep(1000000 - usec
);
1093 gettimeofday(tv1
, NULL
);
1094 *bwcount
-= BandwidthOpt
;
1101 mirror_usage(int code
)
1104 "hammer mirror-read <filesystem> [begin-tid]\n"
1105 "hammer mirror-read-stream <filesystem> [begin-tid]\n"
1106 "hammer mirror-write <filesystem>\n"
1107 "hammer mirror-dump\n"
1108 "hammer mirror-copy [[user@]host:]<filesystem>"
1109 " [[user@]host:]<filesystem>\n"
1110 "hammer mirror-stream [[user@]host:]<filesystem>"
1111 " [[user@]host:]<filesystem>\n"