4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 * Portions Copyright 2012 Martin Matuska <martin@matuska.org>
30 * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
34 #include <libnvpair.h>
42 #include <sys/zfs_ioctl.h>
44 #include <zfs_fletcher.h>
47 * If dump mode is enabled, the number of bytes to print per line
49 #define BYTES_PER_LINE 16
51 * If dump mode is enabled, the number of bytes to group together, separated
52 * by newlines or spaces
54 #define DUMP_GROUPING 4
56 uint64_t total_write_size
= 0;
57 uint64_t total_stream_len
= 0;
58 FILE *send_stream
= 0;
59 boolean_t do_byteswap
= B_FALSE
;
60 boolean_t do_cksum
= B_TRUE
;
65 (void) fprintf(stderr
, "usage: zstreamdump [-v] [-C] [-d] < file\n");
66 (void) fprintf(stderr
, "\t -v -- verbose\n");
67 (void) fprintf(stderr
, "\t -C -- suppress checksum verification\n");
68 (void) fprintf(stderr
, "\t -d -- dump contents of blocks modified, "
74 safe_malloc(size_t size
)
76 void *rv
= malloc(size
);
78 (void) fprintf(stderr
, "ERROR; failed to allocate %zu bytes\n",
86 * ssread - send stream read.
88 * Read while computing incremental checksum
91 ssread(void *buf
, size_t len
, zio_cksum_t
*cksum
)
95 if ((outlen
= fread(buf
, len
, 1, send_stream
)) == 0)
100 fletcher_4_incremental_byteswap(buf
, len
, cksum
);
102 fletcher_4_incremental_native(buf
, len
, cksum
);
104 total_stream_len
+= len
;
109 read_hdr(dmu_replay_record_t
*drr
, zio_cksum_t
*cksum
)
111 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
112 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
113 size_t r
= ssread(drr
, sizeof (*drr
) - sizeof (zio_cksum_t
), cksum
);
116 zio_cksum_t saved_cksum
= *cksum
;
117 r
= ssread(&drr
->drr_u
.drr_checksum
.drr_checksum
,
118 sizeof (zio_cksum_t
), cksum
);
121 if (!ZIO_CHECKSUM_IS_ZERO(&drr
->drr_u
.drr_checksum
.drr_checksum
) &&
122 !ZIO_CHECKSUM_EQUAL(saved_cksum
,
123 drr
->drr_u
.drr_checksum
.drr_checksum
)) {
124 fprintf(stderr
, "invalid checksum\n");
125 (void) printf("Incorrect checksum in record header.\n");
126 (void) printf("Expected checksum = %llx/%llx/%llx/%llx\n",
127 (longlong_t
)saved_cksum
.zc_word
[0],
128 (longlong_t
)saved_cksum
.zc_word
[1],
129 (longlong_t
)saved_cksum
.zc_word
[2],
130 (longlong_t
)saved_cksum
.zc_word
[3]);
133 return (sizeof (*drr
));
137 * Print part of a block in ASCII characters
140 print_ascii_block(char *subbuf
, int length
)
144 for (i
= 0; i
< length
; i
++) {
145 char char_print
= isprint(subbuf
[i
]) ? subbuf
[i
] : '.';
146 if (i
!= 0 && i
% DUMP_GROUPING
== 0) {
149 (void) printf("%c", char_print
);
155 * print_block - Dump the contents of a modified block to STDOUT
157 * Assume that buf has capacity evenly divisible by BYTES_PER_LINE
160 print_block(char *buf
, int length
)
164 * Start printing ASCII characters at a constant offset, after
165 * the hex prints. Leave 3 characters per byte on a line (2 digit
166 * hex number plus 1 space) plus spaces between characters and
169 int ascii_start
= BYTES_PER_LINE
* 3 +
170 BYTES_PER_LINE
/ DUMP_GROUPING
+ 2;
172 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
) {
174 int this_line_length
= MIN(BYTES_PER_LINE
, length
- i
);
175 int print_offset
= 0;
177 for (j
= 0; j
< this_line_length
; j
++) {
178 int buf_offset
= i
+ j
;
181 * Separate every DUMP_GROUPING bytes by a space.
183 if (buf_offset
% DUMP_GROUPING
== 0) {
184 print_offset
+= printf(" ");
188 * Print the two-digit hex value for this byte.
190 unsigned char hex_print
= buf
[buf_offset
];
191 print_offset
+= printf("%02x ", hex_print
);
194 (void) printf("%*s", ascii_start
- print_offset
, " ");
196 print_ascii_block(buf
+ i
, this_line_length
);
201 main(int argc
, char *argv
[])
203 char *buf
= safe_malloc(SPA_MAXBLOCKSIZE
);
204 uint64_t drr_record_count
[DRR_NUMTYPES
] = { 0 };
205 uint64_t total_records
= 0;
206 dmu_replay_record_t thedrr
;
207 dmu_replay_record_t
*drr
= &thedrr
;
208 struct drr_begin
*drrb
= &thedrr
.drr_u
.drr_begin
;
209 struct drr_end
*drre
= &thedrr
.drr_u
.drr_end
;
210 struct drr_object
*drro
= &thedrr
.drr_u
.drr_object
;
211 struct drr_freeobjects
*drrfo
= &thedrr
.drr_u
.drr_freeobjects
;
212 struct drr_write
*drrw
= &thedrr
.drr_u
.drr_write
;
213 struct drr_write_byref
*drrwbr
= &thedrr
.drr_u
.drr_write_byref
;
214 struct drr_free
*drrf
= &thedrr
.drr_u
.drr_free
;
215 struct drr_spill
*drrs
= &thedrr
.drr_u
.drr_spill
;
216 struct drr_write_embedded
*drrwe
= &thedrr
.drr_u
.drr_write_embedded
;
217 struct drr_checksum
*drrc
= &thedrr
.drr_u
.drr_checksum
;
219 boolean_t verbose
= B_FALSE
;
220 boolean_t very_verbose
= B_FALSE
;
221 boolean_t first
= B_TRUE
;
223 * dump flag controls whether the contents of any modified data blocks
224 * are printed to the console during processing of the stream. Warning:
225 * for large streams, this can obviously lead to massive prints.
227 boolean_t dump
= B_FALSE
;
229 zio_cksum_t zc
= { { 0 } };
230 zio_cksum_t pcksum
= { { 0 } };
232 while ((c
= getopt(argc
, argv
, ":vCd")) != -1) {
239 very_verbose
= B_TRUE
;
245 very_verbose
= B_TRUE
;
248 (void) fprintf(stderr
,
249 "missing argument for '%c' option\n", optopt
);
253 (void) fprintf(stderr
, "invalid option '%c'\n",
260 if (isatty(STDIN_FILENO
)) {
261 (void) fprintf(stderr
,
262 "Error: Backup stream can not be read "
264 "You must redirect standard input.\n");
269 while (read_hdr(drr
, &zc
)) {
272 * If this is the first DMU record being processed, check for
273 * the magic bytes and figure out the endian-ness based on them.
276 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
277 do_byteswap
= B_TRUE
;
279 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
281 * recalculate header checksum now
282 * that we know it needs to be
285 fletcher_4_incremental_byteswap(drr
,
286 sizeof (dmu_replay_record_t
), &zc
);
288 } else if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
) {
289 (void) fprintf(stderr
, "Invalid stream "
290 "(bad magic number)\n");
296 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
297 drr
->drr_payloadlen
=
298 BSWAP_32(drr
->drr_payloadlen
);
302 * At this point, the leading fields of the replay record
303 * (drr_type and drr_payloadlen) have been byte-swapped if
304 * necessary, but the rest of the data structure (the
305 * union of type-specific structures) is still in its
308 if (drr
->drr_type
>= DRR_NUMTYPES
) {
309 (void) printf("INVALID record found: type 0x%x\n",
311 (void) printf("Aborting.\n");
315 drr_record_count
[drr
->drr_type
]++;
318 switch (drr
->drr_type
) {
321 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
322 drrb
->drr_versioninfo
=
323 BSWAP_64(drrb
->drr_versioninfo
);
324 drrb
->drr_creation_time
=
325 BSWAP_64(drrb
->drr_creation_time
);
326 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
327 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
328 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
330 BSWAP_64(drrb
->drr_fromguid
);
333 (void) printf("BEGIN record\n");
334 (void) printf("\thdrtype = %lld\n",
335 DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
));
336 (void) printf("\tfeatures = %llx\n",
337 DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
));
338 (void) printf("\tmagic = %llx\n",
339 (u_longlong_t
)drrb
->drr_magic
);
340 (void) printf("\tcreation_time = %llx\n",
341 (u_longlong_t
)drrb
->drr_creation_time
);
342 (void) printf("\ttype = %u\n", drrb
->drr_type
);
343 (void) printf("\tflags = 0x%x\n", drrb
->drr_flags
);
344 (void) printf("\ttoguid = %llx\n",
345 (u_longlong_t
)drrb
->drr_toguid
);
346 (void) printf("\tfromguid = %llx\n",
347 (u_longlong_t
)drrb
->drr_fromguid
);
348 (void) printf("\ttoname = %s\n", drrb
->drr_toname
);
352 if (drr
->drr_payloadlen
!= 0) {
354 int sz
= drr
->drr_payloadlen
;
356 if (sz
> SPA_MAXBLOCKSIZE
) {
358 buf
= safe_malloc(sz
);
360 (void) ssread(buf
, sz
, &zc
);
361 if (ferror(send_stream
))
363 err
= nvlist_unpack(buf
, sz
, &nv
, 0);
365 perror(strerror(err
));
366 nvlist_print(stdout
, nv
);
373 drre
->drr_checksum
.zc_word
[0] =
374 BSWAP_64(drre
->drr_checksum
.zc_word
[0]);
375 drre
->drr_checksum
.zc_word
[1] =
376 BSWAP_64(drre
->drr_checksum
.zc_word
[1]);
377 drre
->drr_checksum
.zc_word
[2] =
378 BSWAP_64(drre
->drr_checksum
.zc_word
[2]);
379 drre
->drr_checksum
.zc_word
[3] =
380 BSWAP_64(drre
->drr_checksum
.zc_word
[3]);
383 * We compare against the *previous* checksum
384 * value, because the stored checksum is of
385 * everything before the DRR_END record.
387 if (do_cksum
&& !ZIO_CHECKSUM_EQUAL(drre
->drr_checksum
,
389 (void) printf("Expected checksum differs from "
390 "checksum in stream.\n");
391 (void) printf("Expected checksum = "
392 "%llx/%llx/%llx/%llx\n",
393 (long long unsigned int)pcksum
.zc_word
[0],
394 (long long unsigned int)pcksum
.zc_word
[1],
395 (long long unsigned int)pcksum
.zc_word
[2],
396 (long long unsigned int)pcksum
.zc_word
[3]);
398 (void) printf("END checksum = %llx/%llx/%llx/%llx\n",
399 (long long unsigned int)
400 drre
->drr_checksum
.zc_word
[0],
401 (long long unsigned int)
402 drre
->drr_checksum
.zc_word
[1],
403 (long long unsigned int)
404 drre
->drr_checksum
.zc_word
[2],
405 (long long unsigned int)
406 drre
->drr_checksum
.zc_word
[3]);
408 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
413 drro
->drr_object
= BSWAP_64(drro
->drr_object
);
414 drro
->drr_type
= BSWAP_32(drro
->drr_type
);
415 drro
->drr_bonustype
=
416 BSWAP_32(drro
->drr_bonustype
);
417 drro
->drr_blksz
= BSWAP_32(drro
->drr_blksz
);
419 BSWAP_32(drro
->drr_bonuslen
);
420 drro
->drr_toguid
= BSWAP_64(drro
->drr_toguid
);
423 (void) printf("OBJECT object = %llu type = %u "
424 "bonustype = %u blksz = %u bonuslen = %u\n",
425 (u_longlong_t
)drro
->drr_object
,
431 if (drro
->drr_bonuslen
> 0) {
432 (void) ssread(buf
, P2ROUNDUP(drro
->drr_bonuslen
,
436 P2ROUNDUP(drro
->drr_bonuslen
, 8));
441 case DRR_FREEOBJECTS
:
443 drrfo
->drr_firstobj
=
444 BSWAP_64(drrfo
->drr_firstobj
);
446 BSWAP_64(drrfo
->drr_numobjs
);
447 drrfo
->drr_toguid
= BSWAP_64(drrfo
->drr_toguid
);
450 (void) printf("FREEOBJECTS firstobj = %llu "
452 (u_longlong_t
)drrfo
->drr_firstobj
,
453 (u_longlong_t
)drrfo
->drr_numobjs
);
459 drrw
->drr_object
= BSWAP_64(drrw
->drr_object
);
460 drrw
->drr_type
= BSWAP_32(drrw
->drr_type
);
461 drrw
->drr_offset
= BSWAP_64(drrw
->drr_offset
);
462 drrw
->drr_logical_size
=
463 BSWAP_64(drrw
->drr_logical_size
);
464 drrw
->drr_toguid
= BSWAP_64(drrw
->drr_toguid
);
465 drrw
->drr_key
.ddk_prop
=
466 BSWAP_64(drrw
->drr_key
.ddk_prop
);
467 drrw
->drr_compressed_size
=
468 BSWAP_64(drrw
->drr_compressed_size
);
471 uint64_t payload_size
= DRR_WRITE_PAYLOAD_SIZE(drrw
);
474 * If this is verbose and/or dump output,
475 * print info on the modified block
478 (void) printf("WRITE object = %llu type = %u "
479 "checksum type = %u compression type = %u\n"
480 " offset = %llu logical_size = %llu "
481 "compressed_size = %llu "
482 "payload_size = %llu "
484 (u_longlong_t
)drrw
->drr_object
,
486 drrw
->drr_checksumtype
,
487 drrw
->drr_compressiontype
,
488 (u_longlong_t
)drrw
->drr_offset
,
489 (u_longlong_t
)drrw
->drr_logical_size
,
490 (u_longlong_t
)drrw
->drr_compressed_size
,
491 (u_longlong_t
)payload_size
,
492 (u_longlong_t
)drrw
->drr_key
.ddk_prop
);
496 * Read the contents of the block in from STDIN to buf
498 (void) ssread(buf
, payload_size
, &zc
);
503 print_block(buf
, payload_size
);
505 total_write_size
+= payload_size
;
508 case DRR_WRITE_BYREF
:
511 BSWAP_64(drrwbr
->drr_object
);
513 BSWAP_64(drrwbr
->drr_offset
);
515 BSWAP_64(drrwbr
->drr_length
);
517 BSWAP_64(drrwbr
->drr_toguid
);
518 drrwbr
->drr_refguid
=
519 BSWAP_64(drrwbr
->drr_refguid
);
520 drrwbr
->drr_refobject
=
521 BSWAP_64(drrwbr
->drr_refobject
);
522 drrwbr
->drr_refoffset
=
523 BSWAP_64(drrwbr
->drr_refoffset
);
524 drrwbr
->drr_key
.ddk_prop
=
525 BSWAP_64(drrwbr
->drr_key
.ddk_prop
);
528 (void) printf("WRITE_BYREF object = %llu "
529 "checksum type = %u props = %llx\n"
530 " offset = %llu length = %llu\n"
531 "toguid = %llx refguid = %llx\n"
532 " refobject = %llu refoffset = %llu\n",
533 (u_longlong_t
)drrwbr
->drr_object
,
534 drrwbr
->drr_checksumtype
,
535 (u_longlong_t
)drrwbr
->drr_key
.ddk_prop
,
536 (u_longlong_t
)drrwbr
->drr_offset
,
537 (u_longlong_t
)drrwbr
->drr_length
,
538 (u_longlong_t
)drrwbr
->drr_toguid
,
539 (u_longlong_t
)drrwbr
->drr_refguid
,
540 (u_longlong_t
)drrwbr
->drr_refobject
,
541 (u_longlong_t
)drrwbr
->drr_refoffset
);
547 drrf
->drr_object
= BSWAP_64(drrf
->drr_object
);
548 drrf
->drr_offset
= BSWAP_64(drrf
->drr_offset
);
549 drrf
->drr_length
= BSWAP_64(drrf
->drr_length
);
552 (void) printf("FREE object = %llu "
553 "offset = %llu length = %lld\n",
554 (u_longlong_t
)drrf
->drr_object
,
555 (u_longlong_t
)drrf
->drr_offset
,
556 (longlong_t
)drrf
->drr_length
);
561 drrs
->drr_object
= BSWAP_64(drrs
->drr_object
);
562 drrs
->drr_length
= BSWAP_64(drrs
->drr_length
);
565 (void) printf("SPILL block for object = %llu "
567 (long long unsigned int)drrs
->drr_object
,
568 (long long unsigned int)drrs
->drr_length
);
570 (void) ssread(buf
, drrs
->drr_length
, &zc
);
572 print_block(buf
, drrs
->drr_length
);
575 case DRR_WRITE_EMBEDDED
:
578 BSWAP_64(drrwe
->drr_object
);
580 BSWAP_64(drrwe
->drr_offset
);
582 BSWAP_64(drrwe
->drr_length
);
584 BSWAP_64(drrwe
->drr_toguid
);
586 BSWAP_32(drrwe
->drr_lsize
);
588 BSWAP_32(drrwe
->drr_psize
);
591 (void) printf("WRITE_EMBEDDED object = %llu "
592 "offset = %llu length = %llu\n"
593 " toguid = %llx comp = %u etype = %u "
594 "lsize = %u psize = %u\n",
595 (u_longlong_t
)drrwe
->drr_object
,
596 (u_longlong_t
)drrwe
->drr_offset
,
597 (u_longlong_t
)drrwe
->drr_length
,
598 (u_longlong_t
)drrwe
->drr_toguid
,
599 drrwe
->drr_compression
,
605 P2ROUNDUP(drrwe
->drr_psize
, 8), &zc
);
608 /* should never be reached */
611 if (drr
->drr_type
!= DRR_BEGIN
&& very_verbose
) {
612 (void) printf(" checksum = %llx/%llx/%llx/%llx\n",
613 (longlong_t
)drrc
->drr_checksum
.zc_word
[0],
614 (longlong_t
)drrc
->drr_checksum
.zc_word
[1],
615 (longlong_t
)drrc
->drr_checksum
.zc_word
[2],
616 (longlong_t
)drrc
->drr_checksum
.zc_word
[3]);
622 /* Print final summary */
624 (void) printf("SUMMARY:\n");
625 (void) printf("\tTotal DRR_BEGIN records = %lld\n",
626 (u_longlong_t
)drr_record_count
[DRR_BEGIN
]);
627 (void) printf("\tTotal DRR_END records = %lld\n",
628 (u_longlong_t
)drr_record_count
[DRR_END
]);
629 (void) printf("\tTotal DRR_OBJECT records = %lld\n",
630 (u_longlong_t
)drr_record_count
[DRR_OBJECT
]);
631 (void) printf("\tTotal DRR_FREEOBJECTS records = %lld\n",
632 (u_longlong_t
)drr_record_count
[DRR_FREEOBJECTS
]);
633 (void) printf("\tTotal DRR_WRITE records = %lld\n",
634 (u_longlong_t
)drr_record_count
[DRR_WRITE
]);
635 (void) printf("\tTotal DRR_WRITE_BYREF records = %lld\n",
636 (u_longlong_t
)drr_record_count
[DRR_WRITE_BYREF
]);
637 (void) printf("\tTotal DRR_WRITE_EMBEDDED records = %lld\n",
638 (u_longlong_t
)drr_record_count
[DRR_WRITE_EMBEDDED
]);
639 (void) printf("\tTotal DRR_FREE records = %lld\n",
640 (u_longlong_t
)drr_record_count
[DRR_FREE
]);
641 (void) printf("\tTotal DRR_SPILL records = %lld\n",
642 (u_longlong_t
)drr_record_count
[DRR_SPILL
]);
643 (void) printf("\tTotal records = %lld\n",
644 (u_longlong_t
)total_records
);
645 (void) printf("\tTotal write size = %lld (0x%llx)\n",
646 (u_longlong_t
)total_write_size
, (u_longlong_t
)total_write_size
);
647 (void) printf("\tTotal stream length = %lld (0x%llx)\n",
648 (u_longlong_t
)total_stream_len
, (u_longlong_t
)total_stream_len
);