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.2 2008/07/19 18:49:08 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 void validate_mrec_header(int fd
, int fdin
, int is_target
, int pfs_id
,
50 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
);
51 static void update_pfs_snapshot(int fd
, hammer_tid_t snapshot_tid
, int pfs_id
);
52 static void mirror_usage(int code
);
55 * Generate a mirroring data stream from the specific source over the
56 * entire key range, but restricted to the specified transaction range.
58 * The HAMMER VFS does most of the work, we add a few new mrecord
59 * types to negotiate the TID ranges and verify that the entire
60 * stream made it to the destination.
63 hammer_cmd_mirror_read(char **av
, int ac
)
65 struct hammer_ioc_mirror_rw mirror
;
66 struct hammer_ioc_pseudofs_rw pfs
;
67 union hammer_ioc_mrecord_any mrec_tmp
;
68 hammer_ioc_mrecord_any_t mrec
;
69 hammer_tid_t sync_tid
;
70 const char *filesystem
;
71 char *buf
= malloc(SERIALBUF_SIZE
);
76 time_t base_t
= time(NULL
);
82 bzero(&mirror
, sizeof(mirror
));
83 hammer_key_beg_init(&mirror
.key_beg
);
84 hammer_key_end_init(&mirror
.key_end
);
86 fd
= getpfs(&pfs
, filesystem
);
89 * In 2-way mode the target will send us a PFS info packet
90 * first. Use the target's current snapshot TID as our default
95 validate_mrec_header(fd
, 0, 0, pfs
.pfs_id
,
96 NULL
, &mirror
.tid_beg
);
99 * Write out the PFS header, tid_beg will be updated if our PFS
100 * has a larger begin sync. tid_end is set to the latest source
101 * TID whos flush cycle has completed.
103 generate_mrec_header(fd
, 1, pfs
.pfs_id
,
104 &mirror
.tid_beg
, &mirror
.tid_end
);
107 * A cycle file overrides the beginning TID
109 hammer_get_cycle(&mirror
.key_beg
, &mirror
.tid_beg
);
112 mirror
.tid_beg
= strtoull(av
[1], NULL
, 0);
114 fprintf(stderr
, "Mirror-read: Mirror from %016llx to %016llx\n",
115 mirror
.tid_beg
, mirror
.tid_end
);
116 if (mirror
.key_beg
.obj_id
!= (int64_t)HAMMER_MIN_OBJID
) {
117 fprintf(stderr
, "Mirror-read: Resuming at object %016llx\n",
118 mirror
.key_beg
.obj_id
);
122 * Nothing to do if begin equals end.
124 if (mirror
.tid_beg
== mirror
.tid_end
) {
125 fprintf(stderr
, "Mirror-read: No work to do, stopping\n");
126 write_mrecord(1, HAMMER_MREC_TYPE_TERM
,
127 &mrec_tmp
, sizeof(mrec_tmp
.sync
));
132 * Write out bulk records
135 mirror
.size
= SERIALBUF_SIZE
;
139 mirror
.pfs_id
= pfs
.pfs_id
;
140 mirror
.shared_uuid
= pfs
.ondisk
->shared_uuid
;
141 if (ioctl(fd
, HAMMERIOC_MIRROR_READ
, &mirror
) < 0) {
142 fprintf(stderr
, "Mirror-read %s failed: %s\n",
143 filesystem
, strerror(errno
));
146 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_ERROR
) {
148 "Mirror-read %s fatal error %d\n",
149 filesystem
, mirror
.head
.error
);
153 n
= write(1, mirror
.ubuf
, mirror
.count
);
154 if (n
!= mirror
.count
) {
155 fprintf(stderr
, "Mirror-read %s failed: "
161 mirror
.key_beg
= mirror
.key_cur
;
163 (unsigned)(time(NULL
) - base_t
) > (unsigned)TimeoutOpt
) {
165 "Mirror-read %s interrupted by timer at"
168 mirror
.key_cur
.obj_id
);
172 } while (mirror
.count
!= 0);
175 * Write out the termination sync record
177 write_mrecord(1, HAMMER_MREC_TYPE_SYNC
,
178 &mrec_tmp
, sizeof(mrec_tmp
.sync
));
181 * If the -2 option was given (automatic when doing mirror-copy),
182 * a two-way pipe is assumed and we expect a response mrec from
186 mrec
= read_mrecord(0, &error
, NULL
);
188 mrec
->head
.type
!= HAMMER_MREC_TYPE_UPDATE
||
189 mrec
->head
.rec_size
!= sizeof(mrec
->update
)) {
190 fprintf(stderr
, "mirror_read: Did not get final "
191 "acknowledgement packet from target\n");
196 hammer_set_cycle(&mirror
.key_cur
, mirror
.tid_beg
);
197 fprintf(stderr
, "Cyclefile %s updated for continuation\n", CyclePath
);
200 sync_tid
= mrec
->update
.tid
;
202 hammer_key_beg_init(&mirror
.key_beg
);
203 hammer_set_cycle(&mirror
.key_beg
, sync_tid
);
204 fprintf(stderr
, "Cyclefile %s updated to 0x%016llx\n",
205 CyclePath
, sync_tid
);
208 } else if (CyclePath
) {
209 /* NOTE! mirror.tid_beg cannot be updated */
210 fprintf(stderr
, "Warning: cycle file (-c option) cannot be "
211 "fully updated unless you use mirror-copy\n");
212 hammer_set_cycle(&mirror
.key_beg
, mirror
.tid_beg
);
215 fprintf(stderr
, "Mirror-read %s succeeded\n", filesystem
);
219 * Pipe the mirroring data stream on stdin to the HAMMER VFS, adding
220 * some additional packet types to negotiate TID ranges and to verify
221 * completion. The HAMMER VFS does most of the work.
223 * It is important to note that the mirror.key_{beg,end} range must
224 * match the ranged used by the original. For now both sides use
225 * range the entire key space.
227 * It is even more important that the records in the stream conform
228 * to the TID range also supplied in the stream. The HAMMER VFS will
229 * use the REC, PASS, and SKIP record types to track the portions of
230 * the B-Tree being scanned in order to be able to proactively delete
231 * records on the target within those active areas that are not mentioned
234 * The mirror.key_cur field is used by the VFS to do this tracking. It
235 * must be initialized to key_beg but then is persistently updated by
236 * the HAMMER VFS on each successive ioctl() call. If you blow up this
237 * field you will blow up the mirror target, possibly to the point of
238 * deleting everything. As a safety measure the HAMMER VFS simply marks
239 * the records that the source has destroyed as deleted on the target,
240 * and normal pruning operations will deal with their final disposition
241 * at some later time.
244 hammer_cmd_mirror_write(char **av
, int ac
)
246 struct hammer_ioc_mirror_rw mirror
;
247 const char *filesystem
;
248 char *buf
= malloc(SERIALBUF_SIZE
);
249 struct hammer_ioc_pseudofs_rw pfs
;
250 struct hammer_ioc_mrecord_head pickup
;
251 struct hammer_ioc_synctid synctid
;
252 union hammer_ioc_mrecord_any mrec_tmp
;
253 hammer_ioc_mrecord_any_t mrec
;
261 bzero(&mirror
, sizeof(mirror
));
262 hammer_key_beg_init(&mirror
.key_beg
);
263 hammer_key_end_init(&mirror
.key_end
);
264 mirror
.key_end
= mirror
.key_beg
;
266 fd
= getpfs(&pfs
, filesystem
);
269 * In two-way mode the target writes out a PFS packet first.
270 * The source uses our tid_end as its tid_beg by default,
271 * picking up where it left off.
275 generate_mrec_header(fd
, 1, pfs
.pfs_id
,
276 &mirror
.tid_beg
, &mirror
.tid_end
);
280 * Read and process the PFS header. The source informs us of
281 * the TID range the stream represents.
283 validate_mrec_header(fd
, 0, 1, pfs
.pfs_id
,
284 &mirror
.tid_beg
, &mirror
.tid_end
);
287 mirror
.size
= SERIALBUF_SIZE
;
289 pickup
.signature
= 0;
293 * Read and process bulk records (REC, PASS, and SKIP types).
295 * On your life, do NOT mess with mirror.key_cur or your mirror
296 * target may become history.
300 mirror
.pfs_id
= pfs
.pfs_id
;
301 mirror
.shared_uuid
= pfs
.ondisk
->shared_uuid
;
302 mirror
.size
= read_mrecords(0, buf
, SERIALBUF_SIZE
, &pickup
);
303 if (mirror
.size
<= 0)
305 if (ioctl(fd
, HAMMERIOC_MIRROR_WRITE
, &mirror
) < 0) {
306 fprintf(stderr
, "Mirror-write %s failed: %s\n",
307 filesystem
, strerror(errno
));
310 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_ERROR
) {
312 "Mirror-write %s fatal error %d\n",
313 filesystem
, mirror
.head
.error
);
317 if (mirror
.head
.flags
& HAMMER_IOC_HEAD_INTR
) {
319 "Mirror-write %s interrupted by timer at"
322 mirror
.key_cur
.obj_id
);
329 * Read and process the termination sync record.
331 mrec
= read_mrecord(0, &error
, &pickup
);
333 if (mrec
&& mrec
->head
.type
== HAMMER_MREC_TYPE_TERM
) {
334 fprintf(stderr
, "Mirror-write: No work to do, stopping\n");
339 mrec
->head
.type
!= HAMMER_MREC_TYPE_SYNC
||
340 mrec
->head
.rec_size
!= sizeof(mrec
->sync
)) {
341 fprintf(stderr
, "Mirror-write %s: Did not get termination "
342 "sync record, or rec_size is wrong rt=%d\n",
343 filesystem
, mrec
->head
.type
);
350 * Update the PFS info on the target so the user has visibility
351 * into the new snapshot.
353 update_pfs_snapshot(fd
, mirror
.tid_end
, pfs
.pfs_id
);
356 * Sync the target filesystem
358 bzero(&synctid
, sizeof(synctid
));
359 synctid
.op
= HAMMER_SYNCTID_SYNC2
;
360 ioctl(fd
, HAMMERIOC_SYNCTID
, &synctid
);
362 fprintf(stderr
, "Mirror-write %s: succeeded\n", filesystem
);
365 * Report back to the originator.
368 mrec_tmp
.update
.tid
= mirror
.tid_end
;
369 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE
,
370 &mrec_tmp
, sizeof(mrec_tmp
.update
));
372 printf("Source can update synctid to 0x%016llx\n",
378 hammer_cmd_mirror_dump(void)
380 char *buf
= malloc(SERIALBUF_SIZE
);
381 struct hammer_ioc_mrecord_head pickup
;
382 hammer_ioc_mrecord_any_t mrec
;
389 * Read and process the PFS header
391 pickup
.signature
= 0;
394 mrec
= read_mrecord(0, &error
, &pickup
);
397 * Read and process bulk records
400 size
= read_mrecords(0, buf
, SERIALBUF_SIZE
, &pickup
);
404 while (offset
< size
) {
405 mrec
= (void *)((char *)buf
+ offset
);
406 bytes
= HAMMER_HEAD_DOALIGN(mrec
->head
.rec_size
);
407 if (offset
+ bytes
> size
) {
408 fprintf(stderr
, "Misaligned record\n");
412 switch(mrec
->head
.type
) {
413 case HAMMER_MREC_TYPE_REC
:
414 printf("Record obj=%016llx key=%016llx "
416 mrec
->rec
.leaf
.base
.obj_id
,
417 mrec
->rec
.leaf
.base
.key
,
418 mrec
->rec
.leaf
.base
.rec_type
,
419 mrec
->rec
.leaf
.base
.obj_type
);
420 printf(" tids %016llx:%016llx data=%d\n",
421 mrec
->rec
.leaf
.base
.create_tid
,
422 mrec
->rec
.leaf
.base
.delete_tid
,
423 mrec
->rec
.leaf
.data_len
);
425 case HAMMER_MREC_TYPE_PASS
:
426 printf("Pass obj=%016llx key=%016llx "
428 mrec
->rec
.leaf
.base
.obj_id
,
429 mrec
->rec
.leaf
.base
.key
,
430 mrec
->rec
.leaf
.base
.rec_type
,
431 mrec
->rec
.leaf
.base
.obj_type
);
432 printf(" tids %016llx:%016llx data=%d\n",
433 mrec
->rec
.leaf
.base
.create_tid
,
434 mrec
->rec
.leaf
.base
.delete_tid
,
435 mrec
->rec
.leaf
.data_len
);
437 case HAMMER_MREC_TYPE_SKIP
:
438 printf("Skip obj=%016llx key=%016llx rt=%02x to\n"
439 " obj=%016llx key=%016llx rt=%02x\n",
440 mrec
->skip
.skip_beg
.obj_id
,
441 mrec
->skip
.skip_beg
.key
,
442 mrec
->skip
.skip_beg
.rec_type
,
443 mrec
->skip
.skip_end
.obj_id
,
444 mrec
->skip
.skip_end
.key
,
445 mrec
->skip
.skip_end
.rec_type
);
454 * Read and process the termination sync record.
456 mrec
= read_mrecord(0, &error
, &pickup
);
457 if (mrec
== NULL
|| mrec
->head
.type
!= HAMMER_MREC_TYPE_SYNC
) {
458 fprintf(stderr
, "Mirror-dump: Did not get termination "
464 hammer_cmd_mirror_copy(char **av
, int ac
)
487 if ((pid1
= fork()) == 0) {
492 if ((ptr
= strchr(av
[0], ':')) != NULL
) {
497 xav
[xac
++] = "hammer";
502 snprintf(tbuf
, sizeof(tbuf
), "%d", TimeoutOpt
);
506 xav
[xac
++] = "mirror-read";
509 execv("/usr/bin/ssh", (void *)xav
);
511 hammer_cmd_mirror_read(av
, 1);
521 if ((pid2
= fork()) == 0) {
526 if ((ptr
= strchr(av
[1], ':')) != NULL
) {
531 xav
[xac
++] = "hammer";
535 xav
[xac
++] = "mirror-write";
538 execv("/usr/bin/ssh", (void *)xav
);
540 hammer_cmd_mirror_write(av
+ 1, 1);
549 while (waitpid(pid1
, NULL
, 0) <= 0)
551 while (waitpid(pid2
, NULL
, 0) <= 0)
556 * Read and return multiple mrecords
559 read_mrecords(int fd
, char *buf
, u_int size
, hammer_ioc_mrecord_head_t pickup
)
561 hammer_ioc_mrecord_any_t mrec
;
568 while (size
- count
>= HAMMER_MREC_HEADSIZE
) {
570 * Cached the record header in case we run out of buffer
574 if (pickup
->signature
== 0) {
575 for (n
= 0; n
< HAMMER_MREC_HEADSIZE
; n
+= i
) {
576 i
= read(fd
, (char *)pickup
+ n
,
577 HAMMER_MREC_HEADSIZE
- n
);
583 if (n
!= HAMMER_MREC_HEADSIZE
) {
584 fprintf(stderr
, "read_mrecords: short read on pipe\n");
588 if (pickup
->signature
!= HAMMER_IOC_MIRROR_SIGNATURE
) {
589 fprintf(stderr
, "read_mrecords: malformed record on pipe, bad signature\n");
593 if (pickup
->rec_size
< HAMMER_MREC_HEADSIZE
||
594 pickup
->rec_size
> sizeof(*mrec
) + HAMMER_XBUFSIZE
) {
595 fprintf(stderr
, "read_mrecords: malformed record on pipe, illegal rec_size\n");
600 * Stop if we have insufficient space for the record and data.
602 bytes
= HAMMER_HEAD_DOALIGN(pickup
->rec_size
);
603 if (size
- count
< bytes
)
607 * Stop if the record type is not a REC or a SKIP (the only
608 * two types the ioctl supports. Other types are used only
609 * by the userland protocol).
611 if (pickup
->type
!= HAMMER_MREC_TYPE_REC
&&
612 pickup
->type
!= HAMMER_MREC_TYPE_SKIP
&&
613 pickup
->type
!= HAMMER_MREC_TYPE_PASS
) {
618 * Read the remainder and clear the pickup signature.
620 for (n
= HAMMER_MREC_HEADSIZE
; n
< bytes
; n
+= i
) {
621 i
= read(fd
, buf
+ count
+ n
, bytes
- n
);
626 fprintf(stderr
, "read_mrecords: short read on pipe\n");
630 bcopy(pickup
, buf
+ count
, HAMMER_MREC_HEADSIZE
);
631 pickup
->signature
= 0;
633 mrec
= (void *)(buf
+ count
);
636 * Validate the completed record
638 if (mrec
->head
.rec_crc
!=
639 crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
640 mrec
->head
.rec_size
- HAMMER_MREC_CRCOFF
)) {
641 fprintf(stderr
, "read_mrecords: malformed record "
642 "on pipe, bad crc\n");
647 * If its a B-Tree record validate the data crc
649 if (mrec
->head
.type
== HAMMER_MREC_TYPE_REC
) {
650 if (mrec
->head
.rec_size
<
651 sizeof(mrec
->rec
) + mrec
->rec
.leaf
.data_len
) {
653 "read_mrecords: malformed record on "
654 "pipe, illegal element data_len\n");
657 if (mrec
->rec
.leaf
.data_len
&&
658 mrec
->rec
.leaf
.data_offset
&&
659 hammer_crc_test_leaf(&mrec
->rec
+ 1, &mrec
->rec
.leaf
) == 0) {
661 "read_mrecords: data_crc did not "
662 "match data! obj=%016llx key=%016llx\n",
663 mrec
->rec
.leaf
.base
.obj_id
,
664 mrec
->rec
.leaf
.base
.key
);
666 "continuing, but there are problems\n");
675 * Read and return a single mrecord.
678 hammer_ioc_mrecord_any_t
679 read_mrecord(int fdin
, int *errorp
, hammer_ioc_mrecord_head_t pickup
)
681 hammer_ioc_mrecord_any_t mrec
;
682 struct hammer_ioc_mrecord_head mrechd
;
687 if (pickup
&& pickup
->type
!= 0) {
689 pickup
->signature
= 0;
691 n
= HAMMER_MREC_HEADSIZE
;
694 * Read in the PFSD header from the sender.
696 for (n
= 0; n
< HAMMER_MREC_HEADSIZE
; n
+= i
) {
697 i
= read(fdin
, (char *)&mrechd
+ n
, HAMMER_MREC_HEADSIZE
- n
);
702 *errorp
= 0; /* EOF */
705 if (n
!= HAMMER_MREC_HEADSIZE
) {
706 fprintf(stderr
, "short read of mrecord header\n");
711 if (mrechd
.signature
!= HAMMER_IOC_MIRROR_SIGNATURE
) {
712 fprintf(stderr
, "read_mrecord: bad signature\n");
716 bytes
= HAMMER_HEAD_DOALIGN(mrechd
.rec_size
);
717 assert(bytes
>= sizeof(mrechd
));
718 mrec
= malloc(bytes
);
722 i
= read(fdin
, (char *)mrec
+ n
, bytes
- n
);
728 fprintf(stderr
, "read_mrecord: short read on payload\n");
732 if (mrec
->head
.rec_crc
!=
733 crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
734 mrec
->head
.rec_size
- HAMMER_MREC_CRCOFF
)) {
735 fprintf(stderr
, "read_mrecord: bad CRC\n");
745 write_mrecord(int fdout
, u_int32_t type
, hammer_ioc_mrecord_any_t mrec
,
748 char zbuf
[HAMMER_HEAD_ALIGN
];
751 pad
= HAMMER_HEAD_DOALIGN(bytes
) - bytes
;
753 assert(bytes
>= (int)sizeof(mrec
->head
));
754 bzero(&mrec
->head
, sizeof(mrec
->head
));
755 mrec
->head
.signature
= HAMMER_IOC_MIRROR_SIGNATURE
;
756 mrec
->head
.type
= type
;
757 mrec
->head
.rec_size
= bytes
;
758 mrec
->head
.rec_crc
= crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
759 bytes
- HAMMER_MREC_CRCOFF
);
760 if (write(fdout
, mrec
, bytes
) != bytes
) {
761 fprintf(stderr
, "write_mrecord: error %d (%s)\n",
762 errno
, strerror(errno
));
767 if (write(fdout
, zbuf
, pad
) != pad
) {
768 fprintf(stderr
, "write_mrecord: error %d (%s)\n",
769 errno
, strerror(errno
));
776 * Generate a mirroring header with the pfs information of the
777 * originating filesytem.
780 generate_mrec_header(int fd
, int fdout
, int pfs_id
,
781 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
)
783 struct hammer_ioc_pseudofs_rw pfs
;
784 union hammer_ioc_mrecord_any mrec_tmp
;
786 bzero(&pfs
, sizeof(pfs
));
787 bzero(&mrec_tmp
, sizeof(mrec_tmp
));
789 pfs
.ondisk
= &mrec_tmp
.pfs
.pfsd
;
790 pfs
.bytes
= sizeof(mrec_tmp
.pfs
.pfsd
);
791 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
792 fprintf(stderr
, "Mirror-read: not a HAMMER fs/pseudofs!\n");
795 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
796 fprintf(stderr
, "Mirror-read: HAMMER pfs version mismatch!\n");
801 * sync_beg_tid - lowest TID on source after which a full history
804 * sync_end_tid - highest fully synchronized TID from source.
806 if (tid_begp
&& *tid_begp
< mrec_tmp
.pfs
.pfsd
.sync_beg_tid
)
807 *tid_begp
= mrec_tmp
.pfs
.pfsd
.sync_beg_tid
;
809 *tid_endp
= mrec_tmp
.pfs
.pfsd
.sync_end_tid
;
810 mrec_tmp
.pfs
.version
= pfs
.version
;
811 write_mrecord(fdout
, HAMMER_MREC_TYPE_PFSD
,
812 &mrec_tmp
, sizeof(mrec_tmp
.pfs
));
816 * Validate the pfs information from the originating filesystem
817 * against the target filesystem. shared_uuid must match.
820 validate_mrec_header(int fd
, int fdin
, int is_target
, int pfs_id
,
821 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
)
823 struct hammer_ioc_pseudofs_rw pfs
;
824 struct hammer_pseudofs_data pfsd
;
825 hammer_ioc_mrecord_any_t mrec
;
829 * Get the PFSD info from the target filesystem.
831 bzero(&pfs
, sizeof(pfs
));
832 bzero(&pfsd
, sizeof(pfsd
));
835 pfs
.bytes
= sizeof(pfsd
);
836 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
837 fprintf(stderr
, "mirror-write: not a HAMMER fs/pseudofs!\n");
840 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
841 fprintf(stderr
, "mirror-write: HAMMER pfs version mismatch!\n");
845 mrec
= read_mrecord(fdin
, &error
, NULL
);
848 fprintf(stderr
, "validate_mrec_header: short read\n");
851 if (mrec
->head
.type
!= HAMMER_MREC_TYPE_PFSD
) {
852 fprintf(stderr
, "validate_mrec_header: did not get expected "
853 "PFSD record type\n");
856 if (mrec
->head
.rec_size
!= sizeof(mrec
->pfs
)) {
857 fprintf(stderr
, "validate_mrec_header: unexpected payload "
861 if (mrec
->pfs
.version
!= pfs
.version
) {
862 fprintf(stderr
, "validate_mrec_header: Version mismatch\n");
867 * Whew. Ok, is the read PFS info compatible with the target?
869 if (bcmp(&mrec
->pfs
.pfsd
.shared_uuid
, &pfsd
.shared_uuid
,
870 sizeof(pfsd
.shared_uuid
)) != 0) {
872 "mirror-write: source and target have "
873 "different shared-uuid's!\n");
877 (pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) == 0) {
878 fprintf(stderr
, "mirror-write: target must be in slave mode\n");
882 *tid_begp
= mrec
->pfs
.pfsd
.sync_beg_tid
;
884 *tid_endp
= mrec
->pfs
.pfsd
.sync_end_tid
;
889 update_pfs_snapshot(int fd
, hammer_tid_t snapshot_tid
, int pfs_id
)
891 struct hammer_ioc_pseudofs_rw pfs
;
892 struct hammer_pseudofs_data pfsd
;
894 bzero(&pfs
, sizeof(pfs
));
895 bzero(&pfsd
, sizeof(pfsd
));
898 pfs
.bytes
= sizeof(pfsd
);
899 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
900 perror("update_pfs_snapshot (read)");
903 if (pfsd
.sync_end_tid
!= snapshot_tid
) {
904 pfsd
.sync_end_tid
= snapshot_tid
;
905 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) != 0) {
906 perror("update_pfs_snapshot (rewrite)");
910 "Mirror-write: Completed, updated snapshot "
918 mirror_usage(int code
)
921 "hammer mirror-read <filesystem>\n"
922 "hammer mirror-write <filesystem>\n"
923 "hammer mirror-dump\n"
924 "hammer mirror-copy [[user@]host:]fs [[user@]host:]fs\n"