xref: /freebsd/sys/contrib/openzfs/cmd/zstream/zstream_dump.c (revision 19261079b74319502c6ffa1249920079f0f69a72)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Portions Copyright 2012 Martin Matuska <martin@matuska.org>
27  */
28 
29 /*
30  * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
31  */
32 
33 #include <ctype.h>
34 #include <libnvpair.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <strings.h>
38 #include <unistd.h>
39 #include <stddef.h>
40 
41 #include <sys/dmu.h>
42 #include <sys/zfs_ioctl.h>
43 #include <sys/zio.h>
44 #include <zfs_fletcher.h>
45 #include "zstream.h"
46 
47 /*
48  * If dump mode is enabled, the number of bytes to print per line
49  */
50 #define	BYTES_PER_LINE	16
51 /*
52  * If dump mode is enabled, the number of bytes to group together, separated
53  * by newlines or spaces
54  */
55 #define	DUMP_GROUPING	4
56 
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;
61 
62 static void *
63 safe_malloc(size_t size)
64 {
65 	void *rv = malloc(size);
66 	if (rv == NULL) {
67 		(void) fprintf(stderr, "ERROR; failed to allocate %zu bytes\n",
68 		    size);
69 		abort();
70 	}
71 	return (rv);
72 }
73 
74 /*
75  * ssread - send stream read.
76  *
77  * Read while computing incremental checksum
78  */
79 static size_t
80 ssread(void *buf, size_t len, zio_cksum_t *cksum)
81 {
82 	size_t outlen;
83 
84 	if ((outlen = fread(buf, len, 1, send_stream)) == 0)
85 		return (0);
86 
87 	if (do_cksum) {
88 		if (do_byteswap)
89 			fletcher_4_incremental_byteswap(buf, len, cksum);
90 		else
91 			fletcher_4_incremental_native(buf, len, cksum);
92 	}
93 	total_stream_len += len;
94 	return (outlen);
95 }
96 
97 static size_t
98 read_hdr(dmu_replay_record_t *drr, zio_cksum_t *cksum)
99 {
100 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
101 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
102 	size_t r = ssread(drr, sizeof (*drr) - sizeof (zio_cksum_t), cksum);
103 	if (r == 0)
104 		return (0);
105 	zio_cksum_t saved_cksum = *cksum;
106 	r = ssread(&drr->drr_u.drr_checksum.drr_checksum,
107 	    sizeof (zio_cksum_t), cksum);
108 	if (r == 0)
109 		return (0);
110 	if (do_cksum &&
111 	    !ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.drr_checksum.drr_checksum) &&
112 	    !ZIO_CHECKSUM_EQUAL(saved_cksum,
113 	    drr->drr_u.drr_checksum.drr_checksum)) {
114 		fprintf(stderr, "invalid checksum\n");
115 		(void) printf("Incorrect checksum in record header.\n");
116 		(void) printf("Expected checksum = %llx/%llx/%llx/%llx\n",
117 		    (longlong_t)saved_cksum.zc_word[0],
118 		    (longlong_t)saved_cksum.zc_word[1],
119 		    (longlong_t)saved_cksum.zc_word[2],
120 		    (longlong_t)saved_cksum.zc_word[3]);
121 		return (0);
122 	}
123 	return (sizeof (*drr));
124 }
125 
126 /*
127  * Print part of a block in ASCII characters
128  */
129 static void
130 print_ascii_block(char *subbuf, int length)
131 {
132 	int i;
133 
134 	for (i = 0; i < length; i++) {
135 		char char_print = isprint(subbuf[i]) ? subbuf[i] : '.';
136 		if (i != 0 && i % DUMP_GROUPING == 0) {
137 			(void) printf(" ");
138 		}
139 		(void) printf("%c", char_print);
140 	}
141 	(void) printf("\n");
142 }
143 
144 /*
145  * print_block - Dump the contents of a modified block to STDOUT
146  *
147  * Assume that buf has capacity evenly divisible by BYTES_PER_LINE
148  */
149 static void
150 print_block(char *buf, int length)
151 {
152 	int i;
153 	/*
154 	 * Start printing ASCII characters at a constant offset, after
155 	 * the hex prints. Leave 3 characters per byte on a line (2 digit
156 	 * hex number plus 1 space) plus spaces between characters and
157 	 * groupings.
158 	 */
159 	int ascii_start = BYTES_PER_LINE * 3 +
160 	    BYTES_PER_LINE / DUMP_GROUPING + 2;
161 
162 	for (i = 0; i < length; i += BYTES_PER_LINE) {
163 		int j;
164 		int this_line_length = MIN(BYTES_PER_LINE, length - i);
165 		int print_offset = 0;
166 
167 		for (j = 0; j < this_line_length; j++) {
168 			int buf_offset = i + j;
169 
170 			/*
171 			 * Separate every DUMP_GROUPING bytes by a space.
172 			 */
173 			if (buf_offset % DUMP_GROUPING == 0) {
174 				print_offset += printf(" ");
175 			}
176 
177 			/*
178 			 * Print the two-digit hex value for this byte.
179 			 */
180 			unsigned char hex_print = buf[buf_offset];
181 			print_offset += printf("%02x ", hex_print);
182 		}
183 
184 		(void) printf("%*s", ascii_start - print_offset, " ");
185 
186 		print_ascii_block(buf + i, this_line_length);
187 	}
188 }
189 
190 /*
191  * Print an array of bytes to stdout as hexadecimal characters. str must
192  * have buf_len * 2 + 1 bytes of space.
193  */
194 static void
195 sprintf_bytes(char *str, uint8_t *buf, uint_t buf_len)
196 {
197 	int i, n;
198 
199 	for (i = 0; i < buf_len; i++) {
200 		n = sprintf(str, "%02x", buf[i] & 0xff);
201 		str += n;
202 	}
203 
204 	str[0] = '\0';
205 }
206 
207 int
208 zstream_do_dump(int argc, char *argv[])
209 {
210 	char *buf = safe_malloc(SPA_MAXBLOCKSIZE);
211 	uint64_t drr_record_count[DRR_NUMTYPES] = { 0 };
212 	uint64_t total_payload_size = 0;
213 	uint64_t total_overhead_size = 0;
214 	uint64_t drr_byte_count[DRR_NUMTYPES] = { 0 };
215 	char salt[ZIO_DATA_SALT_LEN * 2 + 1];
216 	char iv[ZIO_DATA_IV_LEN * 2 + 1];
217 	char mac[ZIO_DATA_MAC_LEN * 2 + 1];
218 	uint64_t total_records = 0;
219 	uint64_t payload_size;
220 	dmu_replay_record_t thedrr;
221 	dmu_replay_record_t *drr = &thedrr;
222 	struct drr_begin *drrb = &thedrr.drr_u.drr_begin;
223 	struct drr_end *drre = &thedrr.drr_u.drr_end;
224 	struct drr_object *drro = &thedrr.drr_u.drr_object;
225 	struct drr_freeobjects *drrfo = &thedrr.drr_u.drr_freeobjects;
226 	struct drr_write *drrw = &thedrr.drr_u.drr_write;
227 	struct drr_write_byref *drrwbr = &thedrr.drr_u.drr_write_byref;
228 	struct drr_free *drrf = &thedrr.drr_u.drr_free;
229 	struct drr_spill *drrs = &thedrr.drr_u.drr_spill;
230 	struct drr_write_embedded *drrwe = &thedrr.drr_u.drr_write_embedded;
231 	struct drr_object_range *drror = &thedrr.drr_u.drr_object_range;
232 	struct drr_redact *drrr = &thedrr.drr_u.drr_redact;
233 	struct drr_checksum *drrc = &thedrr.drr_u.drr_checksum;
234 	int c;
235 	boolean_t verbose = B_FALSE;
236 	boolean_t very_verbose = B_FALSE;
237 	boolean_t first = B_TRUE;
238 	/*
239 	 * dump flag controls whether the contents of any modified data blocks
240 	 * are printed to the console during processing of the stream. Warning:
241 	 * for large streams, this can obviously lead to massive prints.
242 	 */
243 	boolean_t dump = B_FALSE;
244 	int err;
245 	zio_cksum_t zc = { { 0 } };
246 	zio_cksum_t pcksum = { { 0 } };
247 
248 	while ((c = getopt(argc, argv, ":vCd")) != -1) {
249 		switch (c) {
250 		case 'C':
251 			do_cksum = B_FALSE;
252 			break;
253 		case 'v':
254 			if (verbose)
255 				very_verbose = B_TRUE;
256 			verbose = B_TRUE;
257 			break;
258 		case 'd':
259 			dump = B_TRUE;
260 			verbose = B_TRUE;
261 			very_verbose = B_TRUE;
262 			break;
263 		case ':':
264 			(void) fprintf(stderr,
265 			    "missing argument for '%c' option\n", optopt);
266 			zstream_usage();
267 			break;
268 		case '?':
269 			(void) fprintf(stderr, "invalid option '%c'\n",
270 			    optopt);
271 			zstream_usage();
272 			break;
273 		}
274 	}
275 
276 	if (argc > optind) {
277 		const char *filename = argv[optind];
278 		send_stream = fopen(filename, "r");
279 		if (send_stream == NULL) {
280 			(void) fprintf(stderr,
281 			    "Error while opening file '%s': %s\n",
282 			    filename, strerror(errno));
283 			exit(1);
284 		}
285 	} else {
286 		if (isatty(STDIN_FILENO)) {
287 			(void) fprintf(stderr,
288 			    "Error: The send stream is a binary format "
289 			    "and can not be read from a\n"
290 			    "terminal.  Standard input must be redirected, "
291 			    "or a file must be\n"
292 			    "specified as a command-line argument.\n");
293 			exit(1);
294 		}
295 		send_stream = stdin;
296 	}
297 
298 	fletcher_4_init();
299 	while (read_hdr(drr, &zc)) {
300 
301 		/*
302 		 * If this is the first DMU record being processed, check for
303 		 * the magic bytes and figure out the endian-ness based on them.
304 		 */
305 		if (first) {
306 			if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
307 				do_byteswap = B_TRUE;
308 				if (do_cksum) {
309 					ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
310 					/*
311 					 * recalculate header checksum now
312 					 * that we know it needs to be
313 					 * byteswapped.
314 					 */
315 					fletcher_4_incremental_byteswap(drr,
316 					    sizeof (dmu_replay_record_t), &zc);
317 				}
318 			} else if (drrb->drr_magic != DMU_BACKUP_MAGIC) {
319 				(void) fprintf(stderr, "Invalid stream "
320 				    "(bad magic number)\n");
321 				exit(1);
322 			}
323 			first = B_FALSE;
324 		}
325 		if (do_byteswap) {
326 			drr->drr_type = BSWAP_32(drr->drr_type);
327 			drr->drr_payloadlen =
328 			    BSWAP_32(drr->drr_payloadlen);
329 		}
330 
331 		/*
332 		 * At this point, the leading fields of the replay record
333 		 * (drr_type and drr_payloadlen) have been byte-swapped if
334 		 * necessary, but the rest of the data structure (the
335 		 * union of type-specific structures) is still in its
336 		 * original state.
337 		 */
338 		if (drr->drr_type >= DRR_NUMTYPES) {
339 			(void) printf("INVALID record found: type 0x%x\n",
340 			    drr->drr_type);
341 			(void) printf("Aborting.\n");
342 			exit(1);
343 		}
344 
345 		drr_record_count[drr->drr_type]++;
346 		total_overhead_size += sizeof (*drr);
347 		total_records++;
348 		payload_size = 0;
349 
350 		switch (drr->drr_type) {
351 		case DRR_BEGIN:
352 			if (do_byteswap) {
353 				drrb->drr_magic = BSWAP_64(drrb->drr_magic);
354 				drrb->drr_versioninfo =
355 				    BSWAP_64(drrb->drr_versioninfo);
356 				drrb->drr_creation_time =
357 				    BSWAP_64(drrb->drr_creation_time);
358 				drrb->drr_type = BSWAP_32(drrb->drr_type);
359 				drrb->drr_flags = BSWAP_32(drrb->drr_flags);
360 				drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
361 				drrb->drr_fromguid =
362 				    BSWAP_64(drrb->drr_fromguid);
363 			}
364 
365 			(void) printf("BEGIN record\n");
366 			(void) printf("\thdrtype = %lld\n",
367 			    DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo));
368 			(void) printf("\tfeatures = %llx\n",
369 			    DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo));
370 			(void) printf("\tmagic = %llx\n",
371 			    (u_longlong_t)drrb->drr_magic);
372 			(void) printf("\tcreation_time = %llx\n",
373 			    (u_longlong_t)drrb->drr_creation_time);
374 			(void) printf("\ttype = %u\n", drrb->drr_type);
375 			(void) printf("\tflags = 0x%x\n", drrb->drr_flags);
376 			(void) printf("\ttoguid = %llx\n",
377 			    (u_longlong_t)drrb->drr_toguid);
378 			(void) printf("\tfromguid = %llx\n",
379 			    (u_longlong_t)drrb->drr_fromguid);
380 			(void) printf("\ttoname = %s\n", drrb->drr_toname);
381 			(void) printf("\tpayloadlen = %u\n",
382 			    drr->drr_payloadlen);
383 			if (verbose)
384 				(void) printf("\n");
385 
386 			if (drr->drr_payloadlen != 0) {
387 				nvlist_t *nv;
388 				int sz = drr->drr_payloadlen;
389 
390 				if (sz > SPA_MAXBLOCKSIZE) {
391 					free(buf);
392 					buf = safe_malloc(sz);
393 				}
394 				(void) ssread(buf, sz, &zc);
395 				if (ferror(send_stream))
396 					perror("fread");
397 				err = nvlist_unpack(buf, sz, &nv, 0);
398 				if (err) {
399 					perror(strerror(err));
400 				} else {
401 					nvlist_print(stdout, nv);
402 					nvlist_free(nv);
403 				}
404 				payload_size = sz;
405 			}
406 			break;
407 
408 		case DRR_END:
409 			if (do_byteswap) {
410 				drre->drr_checksum.zc_word[0] =
411 				    BSWAP_64(drre->drr_checksum.zc_word[0]);
412 				drre->drr_checksum.zc_word[1] =
413 				    BSWAP_64(drre->drr_checksum.zc_word[1]);
414 				drre->drr_checksum.zc_word[2] =
415 				    BSWAP_64(drre->drr_checksum.zc_word[2]);
416 				drre->drr_checksum.zc_word[3] =
417 				    BSWAP_64(drre->drr_checksum.zc_word[3]);
418 			}
419 			/*
420 			 * We compare against the *previous* checksum
421 			 * value, because the stored checksum is of
422 			 * everything before the DRR_END record.
423 			 */
424 			if (do_cksum && !ZIO_CHECKSUM_EQUAL(drre->drr_checksum,
425 			    pcksum)) {
426 				(void) printf("Expected checksum differs from "
427 				    "checksum in stream.\n");
428 				(void) printf("Expected checksum = "
429 				    "%llx/%llx/%llx/%llx\n",
430 				    (long long unsigned int)pcksum.zc_word[0],
431 				    (long long unsigned int)pcksum.zc_word[1],
432 				    (long long unsigned int)pcksum.zc_word[2],
433 				    (long long unsigned int)pcksum.zc_word[3]);
434 			}
435 			(void) printf("END checksum = %llx/%llx/%llx/%llx\n",
436 			    (long long unsigned int)
437 			    drre->drr_checksum.zc_word[0],
438 			    (long long unsigned int)
439 			    drre->drr_checksum.zc_word[1],
440 			    (long long unsigned int)
441 			    drre->drr_checksum.zc_word[2],
442 			    (long long unsigned int)
443 			    drre->drr_checksum.zc_word[3]);
444 
445 			ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
446 			break;
447 
448 		case DRR_OBJECT:
449 			if (do_byteswap) {
450 				drro->drr_object = BSWAP_64(drro->drr_object);
451 				drro->drr_type = BSWAP_32(drro->drr_type);
452 				drro->drr_bonustype =
453 				    BSWAP_32(drro->drr_bonustype);
454 				drro->drr_blksz = BSWAP_32(drro->drr_blksz);
455 				drro->drr_bonuslen =
456 				    BSWAP_32(drro->drr_bonuslen);
457 				drro->drr_raw_bonuslen =
458 				    BSWAP_32(drro->drr_raw_bonuslen);
459 				drro->drr_toguid = BSWAP_64(drro->drr_toguid);
460 				drro->drr_maxblkid =
461 				    BSWAP_64(drro->drr_maxblkid);
462 			}
463 
464 			payload_size = DRR_OBJECT_PAYLOAD_SIZE(drro);
465 
466 			if (verbose) {
467 				(void) printf("OBJECT object = %llu type = %u "
468 				    "bonustype = %u blksz = %u bonuslen = %u "
469 				    "dn_slots = %u raw_bonuslen = %u "
470 				    "flags = %u maxblkid = %llu "
471 				    "indblkshift = %u nlevels = %u "
472 				    "nblkptr = %u\n",
473 				    (u_longlong_t)drro->drr_object,
474 				    drro->drr_type,
475 				    drro->drr_bonustype,
476 				    drro->drr_blksz,
477 				    drro->drr_bonuslen,
478 				    drro->drr_dn_slots,
479 				    drro->drr_raw_bonuslen,
480 				    drro->drr_flags,
481 				    (u_longlong_t)drro->drr_maxblkid,
482 				    drro->drr_indblkshift,
483 				    drro->drr_nlevels,
484 				    drro->drr_nblkptr);
485 			}
486 			if (drro->drr_bonuslen > 0) {
487 				(void) ssread(buf, payload_size, &zc);
488 				if (dump)
489 					print_block(buf, payload_size);
490 			}
491 			break;
492 
493 		case DRR_FREEOBJECTS:
494 			if (do_byteswap) {
495 				drrfo->drr_firstobj =
496 				    BSWAP_64(drrfo->drr_firstobj);
497 				drrfo->drr_numobjs =
498 				    BSWAP_64(drrfo->drr_numobjs);
499 				drrfo->drr_toguid = BSWAP_64(drrfo->drr_toguid);
500 			}
501 			if (verbose) {
502 				(void) printf("FREEOBJECTS firstobj = %llu "
503 				    "numobjs = %llu\n",
504 				    (u_longlong_t)drrfo->drr_firstobj,
505 				    (u_longlong_t)drrfo->drr_numobjs);
506 			}
507 			break;
508 
509 		case DRR_WRITE:
510 			if (do_byteswap) {
511 				drrw->drr_object = BSWAP_64(drrw->drr_object);
512 				drrw->drr_type = BSWAP_32(drrw->drr_type);
513 				drrw->drr_offset = BSWAP_64(drrw->drr_offset);
514 				drrw->drr_logical_size =
515 				    BSWAP_64(drrw->drr_logical_size);
516 				drrw->drr_toguid = BSWAP_64(drrw->drr_toguid);
517 				drrw->drr_key.ddk_prop =
518 				    BSWAP_64(drrw->drr_key.ddk_prop);
519 				drrw->drr_compressed_size =
520 				    BSWAP_64(drrw->drr_compressed_size);
521 			}
522 
523 			payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
524 
525 			/*
526 			 * If this is verbose and/or dump output,
527 			 * print info on the modified block
528 			 */
529 			if (verbose) {
530 				sprintf_bytes(salt, drrw->drr_salt,
531 				    ZIO_DATA_SALT_LEN);
532 				sprintf_bytes(iv, drrw->drr_iv,
533 				    ZIO_DATA_IV_LEN);
534 				sprintf_bytes(mac, drrw->drr_mac,
535 				    ZIO_DATA_MAC_LEN);
536 
537 				(void) printf("WRITE object = %llu type = %u "
538 				    "checksum type = %u compression type = %u "
539 				    "flags = %u offset = %llu "
540 				    "logical_size = %llu "
541 				    "compressed_size = %llu "
542 				    "payload_size = %llu props = %llx "
543 				    "salt = %s iv = %s mac = %s\n",
544 				    (u_longlong_t)drrw->drr_object,
545 				    drrw->drr_type,
546 				    drrw->drr_checksumtype,
547 				    drrw->drr_compressiontype,
548 				    drrw->drr_flags,
549 				    (u_longlong_t)drrw->drr_offset,
550 				    (u_longlong_t)drrw->drr_logical_size,
551 				    (u_longlong_t)drrw->drr_compressed_size,
552 				    (u_longlong_t)payload_size,
553 				    (u_longlong_t)drrw->drr_key.ddk_prop,
554 				    salt,
555 				    iv,
556 				    mac);
557 			}
558 
559 			/*
560 			 * Read the contents of the block in from STDIN to buf
561 			 */
562 			(void) ssread(buf, payload_size, &zc);
563 			/*
564 			 * If in dump mode
565 			 */
566 			if (dump) {
567 				print_block(buf, payload_size);
568 			}
569 			break;
570 
571 		case DRR_WRITE_BYREF:
572 			if (do_byteswap) {
573 				drrwbr->drr_object =
574 				    BSWAP_64(drrwbr->drr_object);
575 				drrwbr->drr_offset =
576 				    BSWAP_64(drrwbr->drr_offset);
577 				drrwbr->drr_length =
578 				    BSWAP_64(drrwbr->drr_length);
579 				drrwbr->drr_toguid =
580 				    BSWAP_64(drrwbr->drr_toguid);
581 				drrwbr->drr_refguid =
582 				    BSWAP_64(drrwbr->drr_refguid);
583 				drrwbr->drr_refobject =
584 				    BSWAP_64(drrwbr->drr_refobject);
585 				drrwbr->drr_refoffset =
586 				    BSWAP_64(drrwbr->drr_refoffset);
587 				drrwbr->drr_key.ddk_prop =
588 				    BSWAP_64(drrwbr->drr_key.ddk_prop);
589 			}
590 			if (verbose) {
591 				(void) printf("WRITE_BYREF object = %llu "
592 				    "checksum type = %u props = %llx "
593 				    "offset = %llu length = %llu "
594 				    "toguid = %llx refguid = %llx "
595 				    "refobject = %llu refoffset = %llu\n",
596 				    (u_longlong_t)drrwbr->drr_object,
597 				    drrwbr->drr_checksumtype,
598 				    (u_longlong_t)drrwbr->drr_key.ddk_prop,
599 				    (u_longlong_t)drrwbr->drr_offset,
600 				    (u_longlong_t)drrwbr->drr_length,
601 				    (u_longlong_t)drrwbr->drr_toguid,
602 				    (u_longlong_t)drrwbr->drr_refguid,
603 				    (u_longlong_t)drrwbr->drr_refobject,
604 				    (u_longlong_t)drrwbr->drr_refoffset);
605 			}
606 			break;
607 
608 		case DRR_FREE:
609 			if (do_byteswap) {
610 				drrf->drr_object = BSWAP_64(drrf->drr_object);
611 				drrf->drr_offset = BSWAP_64(drrf->drr_offset);
612 				drrf->drr_length = BSWAP_64(drrf->drr_length);
613 			}
614 			if (verbose) {
615 				(void) printf("FREE object = %llu "
616 				    "offset = %llu length = %lld\n",
617 				    (u_longlong_t)drrf->drr_object,
618 				    (u_longlong_t)drrf->drr_offset,
619 				    (longlong_t)drrf->drr_length);
620 			}
621 			break;
622 		case DRR_SPILL:
623 			if (do_byteswap) {
624 				drrs->drr_object = BSWAP_64(drrs->drr_object);
625 				drrs->drr_length = BSWAP_64(drrs->drr_length);
626 				drrs->drr_compressed_size =
627 				    BSWAP_64(drrs->drr_compressed_size);
628 				drrs->drr_type = BSWAP_32(drrs->drr_type);
629 			}
630 
631 			payload_size = DRR_SPILL_PAYLOAD_SIZE(drrs);
632 
633 			if (verbose) {
634 				sprintf_bytes(salt, drrs->drr_salt,
635 				    ZIO_DATA_SALT_LEN);
636 				sprintf_bytes(iv, drrs->drr_iv,
637 				    ZIO_DATA_IV_LEN);
638 				sprintf_bytes(mac, drrs->drr_mac,
639 				    ZIO_DATA_MAC_LEN);
640 
641 				(void) printf("SPILL block for object = %llu "
642 				    "length = %llu flags = %u "
643 				    "compression type = %u "
644 				    "compressed_size = %llu "
645 				    "payload_size = %llu "
646 				    "salt = %s iv = %s mac = %s\n",
647 				    (u_longlong_t)drrs->drr_object,
648 				    (u_longlong_t)drrs->drr_length,
649 				    drrs->drr_flags,
650 				    drrs->drr_compressiontype,
651 				    (u_longlong_t)drrs->drr_compressed_size,
652 				    (u_longlong_t)payload_size,
653 				    salt,
654 				    iv,
655 				    mac);
656 			}
657 			(void) ssread(buf, payload_size, &zc);
658 			if (dump) {
659 				print_block(buf, payload_size);
660 			}
661 			break;
662 		case DRR_WRITE_EMBEDDED:
663 			if (do_byteswap) {
664 				drrwe->drr_object =
665 				    BSWAP_64(drrwe->drr_object);
666 				drrwe->drr_offset =
667 				    BSWAP_64(drrwe->drr_offset);
668 				drrwe->drr_length =
669 				    BSWAP_64(drrwe->drr_length);
670 				drrwe->drr_toguid =
671 				    BSWAP_64(drrwe->drr_toguid);
672 				drrwe->drr_lsize =
673 				    BSWAP_32(drrwe->drr_lsize);
674 				drrwe->drr_psize =
675 				    BSWAP_32(drrwe->drr_psize);
676 			}
677 			if (verbose) {
678 				(void) printf("WRITE_EMBEDDED object = %llu "
679 				    "offset = %llu length = %llu "
680 				    "toguid = %llx comp = %u etype = %u "
681 				    "lsize = %u psize = %u\n",
682 				    (u_longlong_t)drrwe->drr_object,
683 				    (u_longlong_t)drrwe->drr_offset,
684 				    (u_longlong_t)drrwe->drr_length,
685 				    (u_longlong_t)drrwe->drr_toguid,
686 				    drrwe->drr_compression,
687 				    drrwe->drr_etype,
688 				    drrwe->drr_lsize,
689 				    drrwe->drr_psize);
690 			}
691 			(void) ssread(buf,
692 			    P2ROUNDUP(drrwe->drr_psize, 8), &zc);
693 			if (dump) {
694 				print_block(buf,
695 				    P2ROUNDUP(drrwe->drr_psize, 8));
696 			}
697 			payload_size = P2ROUNDUP(drrwe->drr_psize, 8);
698 			break;
699 		case DRR_OBJECT_RANGE:
700 			if (do_byteswap) {
701 				drror->drr_firstobj =
702 				    BSWAP_64(drror->drr_firstobj);
703 				drror->drr_numslots =
704 				    BSWAP_64(drror->drr_numslots);
705 				drror->drr_toguid = BSWAP_64(drror->drr_toguid);
706 			}
707 			if (verbose) {
708 				sprintf_bytes(salt, drror->drr_salt,
709 				    ZIO_DATA_SALT_LEN);
710 				sprintf_bytes(iv, drror->drr_iv,
711 				    ZIO_DATA_IV_LEN);
712 				sprintf_bytes(mac, drror->drr_mac,
713 				    ZIO_DATA_MAC_LEN);
714 
715 				(void) printf("OBJECT_RANGE firstobj = %llu "
716 				    "numslots = %llu flags = %u "
717 				    "salt = %s iv = %s mac = %s\n",
718 				    (u_longlong_t)drror->drr_firstobj,
719 				    (u_longlong_t)drror->drr_numslots,
720 				    drror->drr_flags,
721 				    salt,
722 				    iv,
723 				    mac);
724 			}
725 			break;
726 		case DRR_REDACT:
727 			if (do_byteswap) {
728 				drrr->drr_object = BSWAP_64(drrr->drr_object);
729 				drrr->drr_offset = BSWAP_64(drrr->drr_offset);
730 				drrr->drr_length = BSWAP_64(drrr->drr_length);
731 				drrr->drr_toguid = BSWAP_64(drrr->drr_toguid);
732 			}
733 			if (verbose) {
734 				(void) printf("REDACT object = %llu offset = "
735 				    "%llu length = %llu\n",
736 				    (u_longlong_t)drrr->drr_object,
737 				    (u_longlong_t)drrr->drr_offset,
738 				    (u_longlong_t)drrr->drr_length);
739 			}
740 			break;
741 		case DRR_NUMTYPES:
742 			/* should never be reached */
743 			exit(1);
744 		}
745 		if (drr->drr_type != DRR_BEGIN && very_verbose) {
746 			(void) printf("    checksum = %llx/%llx/%llx/%llx\n",
747 			    (longlong_t)drrc->drr_checksum.zc_word[0],
748 			    (longlong_t)drrc->drr_checksum.zc_word[1],
749 			    (longlong_t)drrc->drr_checksum.zc_word[2],
750 			    (longlong_t)drrc->drr_checksum.zc_word[3]);
751 		}
752 		pcksum = zc;
753 		drr_byte_count[drr->drr_type] += payload_size;
754 		total_payload_size += payload_size;
755 	}
756 	free(buf);
757 	fletcher_4_fini();
758 
759 	/* Print final summary */
760 
761 	(void) printf("SUMMARY:\n");
762 	(void) printf("\tTotal DRR_BEGIN records = %lld (%llu bytes)\n",
763 	    (u_longlong_t)drr_record_count[DRR_BEGIN],
764 	    (u_longlong_t)drr_byte_count[DRR_BEGIN]);
765 	(void) printf("\tTotal DRR_END records = %lld (%llu bytes)\n",
766 	    (u_longlong_t)drr_record_count[DRR_END],
767 	    (u_longlong_t)drr_byte_count[DRR_END]);
768 	(void) printf("\tTotal DRR_OBJECT records = %lld (%llu bytes)\n",
769 	    (u_longlong_t)drr_record_count[DRR_OBJECT],
770 	    (u_longlong_t)drr_byte_count[DRR_OBJECT]);
771 	(void) printf("\tTotal DRR_FREEOBJECTS records = %lld (%llu bytes)\n",
772 	    (u_longlong_t)drr_record_count[DRR_FREEOBJECTS],
773 	    (u_longlong_t)drr_byte_count[DRR_FREEOBJECTS]);
774 	(void) printf("\tTotal DRR_WRITE records = %lld (%llu bytes)\n",
775 	    (u_longlong_t)drr_record_count[DRR_WRITE],
776 	    (u_longlong_t)drr_byte_count[DRR_WRITE]);
777 	(void) printf("\tTotal DRR_WRITE_BYREF records = %lld (%llu bytes)\n",
778 	    (u_longlong_t)drr_record_count[DRR_WRITE_BYREF],
779 	    (u_longlong_t)drr_byte_count[DRR_WRITE_BYREF]);
780 	(void) printf("\tTotal DRR_WRITE_EMBEDDED records = %lld (%llu "
781 	    "bytes)\n", (u_longlong_t)drr_record_count[DRR_WRITE_EMBEDDED],
782 	    (u_longlong_t)drr_byte_count[DRR_WRITE_EMBEDDED]);
783 	(void) printf("\tTotal DRR_FREE records = %lld (%llu bytes)\n",
784 	    (u_longlong_t)drr_record_count[DRR_FREE],
785 	    (u_longlong_t)drr_byte_count[DRR_FREE]);
786 	(void) printf("\tTotal DRR_SPILL records = %lld (%llu bytes)\n",
787 	    (u_longlong_t)drr_record_count[DRR_SPILL],
788 	    (u_longlong_t)drr_byte_count[DRR_SPILL]);
789 	(void) printf("\tTotal records = %lld\n",
790 	    (u_longlong_t)total_records);
791 	(void) printf("\tTotal payload size = %lld (0x%llx)\n",
792 	    (u_longlong_t)total_payload_size, (u_longlong_t)total_payload_size);
793 	(void) printf("\tTotal header overhead = %lld (0x%llx)\n",
794 	    (u_longlong_t)total_overhead_size,
795 	    (u_longlong_t)total_overhead_size);
796 	(void) printf("\tTotal stream length = %lld (0x%llx)\n",
797 	    (u_longlong_t)total_stream_len, (u_longlong_t)total_stream_len);
798 	return (0);
799 }
800