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.1 2008/07/18 23:25:05 swildner 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
);
349 * Update the PFS info on the target so the user has visibility
350 * into the new snapshot.
352 update_pfs_snapshot(fd
, mirror
.tid_end
, pfs
.pfs_id
);
355 * Sync the target filesystem
357 bzero(&synctid
, sizeof(synctid
));
358 synctid
.op
= HAMMER_SYNCTID_SYNC2
;
359 ioctl(fd
, HAMMERIOC_SYNCTID
, &synctid
);
361 fprintf(stderr
, "Mirror-write %s: succeeded\n", filesystem
);
364 * Report back to the originator.
367 mrec_tmp
.update
.tid
= mirror
.tid_end
;
368 write_mrecord(1, HAMMER_MREC_TYPE_UPDATE
,
369 &mrec_tmp
, sizeof(mrec_tmp
.update
));
371 printf("Source can update synctid to 0x%016llx\n",
377 hammer_cmd_mirror_dump(void)
379 char *buf
= malloc(SERIALBUF_SIZE
);
380 struct hammer_ioc_mrecord_head pickup
;
381 hammer_ioc_mrecord_any_t mrec
;
388 * Read and process the PFS header
390 pickup
.signature
= 0;
393 mrec
= read_mrecord(0, &error
, &pickup
);
396 * Read and process bulk records
399 size
= read_mrecords(0, buf
, SERIALBUF_SIZE
, &pickup
);
403 while (offset
< size
) {
404 mrec
= (void *)((char *)buf
+ offset
);
405 bytes
= HAMMER_HEAD_DOALIGN(mrec
->head
.rec_size
);
406 if (offset
+ bytes
> size
) {
407 fprintf(stderr
, "Misaligned record\n");
411 switch(mrec
->head
.type
) {
412 case HAMMER_MREC_TYPE_REC
:
413 printf("Record obj=%016llx key=%016llx "
415 mrec
->rec
.leaf
.base
.obj_id
,
416 mrec
->rec
.leaf
.base
.key
,
417 mrec
->rec
.leaf
.base
.rec_type
,
418 mrec
->rec
.leaf
.base
.obj_type
);
419 printf(" tids %016llx:%016llx data=%d\n",
420 mrec
->rec
.leaf
.base
.create_tid
,
421 mrec
->rec
.leaf
.base
.delete_tid
,
422 mrec
->rec
.leaf
.data_len
);
424 case HAMMER_MREC_TYPE_PASS
:
425 printf("Pass obj=%016llx key=%016llx "
427 mrec
->rec
.leaf
.base
.obj_id
,
428 mrec
->rec
.leaf
.base
.key
,
429 mrec
->rec
.leaf
.base
.rec_type
,
430 mrec
->rec
.leaf
.base
.obj_type
);
431 printf(" tids %016llx:%016llx data=%d\n",
432 mrec
->rec
.leaf
.base
.create_tid
,
433 mrec
->rec
.leaf
.base
.delete_tid
,
434 mrec
->rec
.leaf
.data_len
);
436 case HAMMER_MREC_TYPE_SKIP
:
437 printf("Skip obj=%016llx key=%016llx rt=%02x to\n"
438 " obj=%016llx key=%016llx rt=%02x\n",
439 mrec
->skip
.skip_beg
.obj_id
,
440 mrec
->skip
.skip_beg
.key
,
441 mrec
->skip
.skip_beg
.rec_type
,
442 mrec
->skip
.skip_end
.obj_id
,
443 mrec
->skip
.skip_end
.key
,
444 mrec
->skip
.skip_end
.rec_type
);
453 * Read and process the termination sync record.
455 mrec
= read_mrecord(0, &error
, &pickup
);
456 if (mrec
== NULL
|| mrec
->head
.type
!= HAMMER_MREC_TYPE_SYNC
) {
457 fprintf(stderr
, "Mirror-dump: Did not get termination "
463 hammer_cmd_mirror_copy(char **av
, int ac
)
486 if ((pid1
= fork()) == 0) {
491 if ((ptr
= strchr(av
[0], ':')) != NULL
) {
496 xav
[xac
++] = "hammer";
501 snprintf(tbuf
, sizeof(tbuf
), "%d", TimeoutOpt
);
505 xav
[xac
++] = "mirror-read";
508 execv("/usr/bin/ssh", (void *)xav
);
510 hammer_cmd_mirror_read(av
, 1);
520 if ((pid2
= fork()) == 0) {
525 if ((ptr
= strchr(av
[1], ':')) != NULL
) {
530 xav
[xac
++] = "hammer";
534 xav
[xac
++] = "mirror-write";
537 execv("/usr/bin/ssh", (void *)xav
);
539 hammer_cmd_mirror_write(av
+ 1, 1);
548 while (waitpid(pid1
, NULL
, 0) <= 0)
550 while (waitpid(pid2
, NULL
, 0) <= 0)
555 * Read and return multiple mrecords
558 read_mrecords(int fd
, char *buf
, u_int size
, hammer_ioc_mrecord_head_t pickup
)
560 hammer_ioc_mrecord_any_t mrec
;
567 while (size
- count
>= HAMMER_MREC_HEADSIZE
) {
569 * Cached the record header in case we run out of buffer
573 if (pickup
->signature
== 0) {
574 for (n
= 0; n
< HAMMER_MREC_HEADSIZE
; n
+= i
) {
575 i
= read(fd
, (char *)pickup
+ n
,
576 HAMMER_MREC_HEADSIZE
- n
);
582 if (n
!= HAMMER_MREC_HEADSIZE
) {
583 fprintf(stderr
, "read_mrecords: short read on pipe\n");
587 if (pickup
->signature
!= HAMMER_IOC_MIRROR_SIGNATURE
) {
588 fprintf(stderr
, "read_mrecords: malformed record on pipe, bad signature\n");
592 if (pickup
->rec_size
< HAMMER_MREC_HEADSIZE
||
593 pickup
->rec_size
> sizeof(*mrec
) + HAMMER_XBUFSIZE
) {
594 fprintf(stderr
, "read_mrecords: malformed record on pipe, illegal rec_size\n");
599 * Stop if we have insufficient space for the record and data.
601 bytes
= HAMMER_HEAD_DOALIGN(pickup
->rec_size
);
602 if (size
- count
< bytes
)
606 * Stop if the record type is not a REC or a SKIP (the only
607 * two types the ioctl supports. Other types are used only
608 * by the userland protocol).
610 if (pickup
->type
!= HAMMER_MREC_TYPE_REC
&&
611 pickup
->type
!= HAMMER_MREC_TYPE_SKIP
&&
612 pickup
->type
!= HAMMER_MREC_TYPE_PASS
) {
617 * Read the remainder and clear the pickup signature.
619 for (n
= HAMMER_MREC_HEADSIZE
; n
< bytes
; n
+= i
) {
620 i
= read(fd
, buf
+ count
+ n
, bytes
- n
);
625 fprintf(stderr
, "read_mrecords: short read on pipe\n");
629 bcopy(pickup
, buf
+ count
, HAMMER_MREC_HEADSIZE
);
630 pickup
->signature
= 0;
632 mrec
= (void *)(buf
+ count
);
635 * Validate the completed record
637 if (mrec
->head
.rec_crc
!=
638 crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
639 mrec
->head
.rec_size
- HAMMER_MREC_CRCOFF
)) {
640 fprintf(stderr
, "read_mrecords: malformed record "
641 "on pipe, bad crc\n");
646 * If its a B-Tree record validate the data crc
648 if (mrec
->head
.type
== HAMMER_MREC_TYPE_REC
) {
649 if (mrec
->head
.rec_size
<
650 sizeof(mrec
->rec
) + mrec
->rec
.leaf
.data_len
) {
652 "read_mrecords: malformed record on "
653 "pipe, illegal element data_len\n");
656 if (mrec
->rec
.leaf
.data_len
&&
657 mrec
->rec
.leaf
.data_offset
&&
658 hammer_crc_test_leaf(&mrec
->rec
+ 1, &mrec
->rec
.leaf
) == 0) {
660 "read_mrecords: data_crc did not "
661 "match data! obj=%016llx key=%016llx\n",
662 mrec
->rec
.leaf
.base
.obj_id
,
663 mrec
->rec
.leaf
.base
.key
);
665 "continuing, but there are problems\n");
674 * Read and return a single mrecord.
677 hammer_ioc_mrecord_any_t
678 read_mrecord(int fdin
, int *errorp
, hammer_ioc_mrecord_head_t pickup
)
680 hammer_ioc_mrecord_any_t mrec
;
681 struct hammer_ioc_mrecord_head mrechd
;
686 if (pickup
&& pickup
->type
!= 0) {
688 pickup
->signature
= 0;
690 n
= HAMMER_MREC_HEADSIZE
;
693 * Read in the PFSD header from the sender.
695 for (n
= 0; n
< HAMMER_MREC_HEADSIZE
; n
+= i
) {
696 i
= read(fdin
, (char *)&mrechd
+ n
, HAMMER_MREC_HEADSIZE
- n
);
701 *errorp
= 0; /* EOF */
704 if (n
!= HAMMER_MREC_HEADSIZE
) {
705 fprintf(stderr
, "short read of mrecord header\n");
710 if (mrechd
.signature
!= HAMMER_IOC_MIRROR_SIGNATURE
) {
711 fprintf(stderr
, "read_mrecord: bad signature\n");
715 bytes
= HAMMER_HEAD_DOALIGN(mrechd
.rec_size
);
716 assert(bytes
>= sizeof(mrechd
));
717 mrec
= malloc(bytes
);
721 i
= read(fdin
, (char *)mrec
+ n
, bytes
- n
);
727 fprintf(stderr
, "read_mrecord: short read on payload\n");
731 if (mrec
->head
.rec_crc
!=
732 crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
733 mrec
->head
.rec_size
- HAMMER_MREC_CRCOFF
)) {
734 fprintf(stderr
, "read_mrecord: bad CRC\n");
744 write_mrecord(int fdout
, u_int32_t type
, hammer_ioc_mrecord_any_t mrec
,
747 char zbuf
[HAMMER_HEAD_ALIGN
];
750 pad
= HAMMER_HEAD_DOALIGN(bytes
) - bytes
;
752 assert(bytes
>= (int)sizeof(mrec
->head
));
753 bzero(&mrec
->head
, sizeof(mrec
->head
));
754 mrec
->head
.signature
= HAMMER_IOC_MIRROR_SIGNATURE
;
755 mrec
->head
.type
= type
;
756 mrec
->head
.rec_size
= bytes
;
757 mrec
->head
.rec_crc
= crc32((char *)mrec
+ HAMMER_MREC_CRCOFF
,
758 bytes
- HAMMER_MREC_CRCOFF
);
759 if (write(fdout
, mrec
, bytes
) != bytes
) {
760 fprintf(stderr
, "write_mrecord: error %d (%s)\n",
761 errno
, strerror(errno
));
766 if (write(fdout
, zbuf
, pad
) != pad
) {
767 fprintf(stderr
, "write_mrecord: error %d (%s)\n",
768 errno
, strerror(errno
));
775 * Generate a mirroring header with the pfs information of the
776 * originating filesytem.
779 generate_mrec_header(int fd
, int fdout
, int pfs_id
,
780 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
)
782 struct hammer_ioc_pseudofs_rw pfs
;
783 union hammer_ioc_mrecord_any mrec_tmp
;
785 bzero(&pfs
, sizeof(pfs
));
786 bzero(&mrec_tmp
, sizeof(mrec_tmp
));
788 pfs
.ondisk
= &mrec_tmp
.pfs
.pfsd
;
789 pfs
.bytes
= sizeof(mrec_tmp
.pfs
.pfsd
);
790 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
791 fprintf(stderr
, "Mirror-read: not a HAMMER fs/pseudofs!\n");
794 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
795 fprintf(stderr
, "Mirror-read: HAMMER pfs version mismatch!\n");
800 * sync_beg_tid - lowest TID on source after which a full history
803 * sync_end_tid - highest fully synchronized TID from source.
805 if (tid_begp
&& *tid_begp
< mrec_tmp
.pfs
.pfsd
.sync_beg_tid
)
806 *tid_begp
= mrec_tmp
.pfs
.pfsd
.sync_beg_tid
;
808 *tid_endp
= mrec_tmp
.pfs
.pfsd
.sync_end_tid
;
809 mrec_tmp
.pfs
.version
= pfs
.version
;
810 write_mrecord(fdout
, HAMMER_MREC_TYPE_PFSD
,
811 &mrec_tmp
, sizeof(mrec_tmp
.pfs
));
815 * Validate the pfs information from the originating filesystem
816 * against the target filesystem. shared_uuid must match.
819 validate_mrec_header(int fd
, int fdin
, int is_target
, int pfs_id
,
820 hammer_tid_t
*tid_begp
, hammer_tid_t
*tid_endp
)
822 struct hammer_ioc_pseudofs_rw pfs
;
823 struct hammer_pseudofs_data pfsd
;
824 hammer_ioc_mrecord_any_t mrec
;
828 * Get the PFSD info from the target filesystem.
830 bzero(&pfs
, sizeof(pfs
));
831 bzero(&pfsd
, sizeof(pfsd
));
834 pfs
.bytes
= sizeof(pfsd
);
835 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
836 fprintf(stderr
, "mirror-write: not a HAMMER fs/pseudofs!\n");
839 if (pfs
.version
!= HAMMER_IOC_PSEUDOFS_VERSION
) {
840 fprintf(stderr
, "mirror-write: HAMMER pfs version mismatch!\n");
844 mrec
= read_mrecord(fdin
, &error
, NULL
);
847 fprintf(stderr
, "validate_mrec_header: short read\n");
850 if (mrec
->head
.type
!= HAMMER_MREC_TYPE_PFSD
) {
851 fprintf(stderr
, "validate_mrec_header: did not get expected "
852 "PFSD record type\n");
855 if (mrec
->head
.rec_size
!= sizeof(mrec
->pfs
)) {
856 fprintf(stderr
, "validate_mrec_header: unexpected payload "
860 if (mrec
->pfs
.version
!= pfs
.version
) {
861 fprintf(stderr
, "validate_mrec_header: Version mismatch\n");
866 * Whew. Ok, is the read PFS info compatible with the target?
868 if (bcmp(&mrec
->pfs
.pfsd
.shared_uuid
, &pfsd
.shared_uuid
,
869 sizeof(pfsd
.shared_uuid
)) != 0) {
871 "mirror-write: source and target have "
872 "different shared-uuid's!\n");
876 (pfsd
.mirror_flags
& HAMMER_PFSD_SLAVE
) == 0) {
877 fprintf(stderr
, "mirror-write: target must be in slave mode\n");
881 *tid_begp
= mrec
->pfs
.pfsd
.sync_beg_tid
;
883 *tid_endp
= mrec
->pfs
.pfsd
.sync_end_tid
;
888 update_pfs_snapshot(int fd
, hammer_tid_t snapshot_tid
, int pfs_id
)
890 struct hammer_ioc_pseudofs_rw pfs
;
891 struct hammer_pseudofs_data pfsd
;
893 bzero(&pfs
, sizeof(pfs
));
894 bzero(&pfsd
, sizeof(pfsd
));
897 pfs
.bytes
= sizeof(pfsd
);
898 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) != 0) {
899 perror("update_pfs_snapshot (read)");
902 if (pfsd
.sync_end_tid
!= snapshot_tid
) {
903 pfsd
.sync_end_tid
= snapshot_tid
;
904 if (ioctl(fd
, HAMMERIOC_SET_PSEUDOFS
, &pfs
) != 0) {
905 perror("update_pfs_snapshot (rewrite)");
909 "Mirror-write: Completed, updated snapshot "
917 mirror_usage(int code
)
920 "hammer mirror-read <filesystem>\n"
921 "hammer mirror-write <filesystem>\n"
922 "hammer mirror-dump\n"
923 "hammer mirror-copy [[user@]host:]fs [[user@]host:]fs\n"