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.
28 * Copyright (c) 2013, 2014 by Delphix. All rights reserved.
32 #include <libnvpair.h>
40 #include <sys/zfs_ioctl.h>
41 #include <zfs_fletcher.h>
44 * If dump mode is enabled, the number of bytes to print per line
46 #define BYTES_PER_LINE 16
48 * If dump mode is enabled, the number of bytes to group together, separated
49 * by newlines or spaces
51 #define DUMP_GROUPING 4
53 uint64_t total_write_size = 0;
54 uint64_t total_stream_len = 0;
55 FILE *send_stream = 0;
56 boolean_t do_byteswap = B_FALSE;
57 boolean_t do_cksum = B_TRUE;
62 (void) fprintf(stderr, "usage: zstreamdump [-v] [-C] [-d] < file\n");
63 (void) fprintf(stderr, "\t -v -- verbose\n");
64 (void) fprintf(stderr, "\t -C -- suppress checksum verification\n");
65 (void) fprintf(stderr, "\t -d -- dump contents of blocks modified, "
71 safe_malloc(size_t size)
73 void *rv = malloc(size);
75 (void) fprintf(stderr, "ERROR; failed to allocate %zu bytes\n",
83 * ssread - send stream read.
85 * Read while computing incremental checksum
88 ssread(void *buf, size_t len, zio_cksum_t *cksum)
92 if ((outlen = fread(buf, len, 1, send_stream)) == 0)
97 fletcher_4_incremental_byteswap(buf, len, cksum);
99 fletcher_4_incremental_native(buf, len, cksum);
101 total_stream_len += len;
106 read_hdr(dmu_replay_record_t *drr, zio_cksum_t *cksum)
108 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
109 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
110 size_t r = ssread(drr, sizeof (*drr) - sizeof (zio_cksum_t), cksum);
113 zio_cksum_t saved_cksum = *cksum;
114 r = ssread(&drr->drr_u.drr_checksum.drr_checksum,
115 sizeof (zio_cksum_t), cksum);
118 if (!ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.drr_checksum.drr_checksum) &&
119 !ZIO_CHECKSUM_EQUAL(saved_cksum,
120 drr->drr_u.drr_checksum.drr_checksum)) {
121 fprintf(stderr, "invalid checksum\n");
122 (void) printf("Incorrect checksum in record header.\n");
123 (void) printf("Expected checksum = %llx/%llx/%llx/%llx\n",
124 saved_cksum.zc_word[0],
125 saved_cksum.zc_word[1],
126 saved_cksum.zc_word[2],
127 saved_cksum.zc_word[3]);
130 return (sizeof (*drr));
134 * Print part of a block in ASCII characters
137 print_ascii_block(char *subbuf, int length)
141 for (i = 0; i < length; i++) {
142 char char_print = isprint(subbuf[i]) ? subbuf[i] : '.';
143 if (i != 0 && i % DUMP_GROUPING == 0) {
146 (void) printf("%c", char_print);
152 * print_block - Dump the contents of a modified block to STDOUT
154 * Assume that buf has capacity evenly divisible by BYTES_PER_LINE
157 print_block(char *buf, int length)
161 * Start printing ASCII characters at a constant offset, after
162 * the hex prints. Leave 3 characters per byte on a line (2 digit
163 * hex number plus 1 space) plus spaces between characters and
166 int ascii_start = BYTES_PER_LINE * 3 +
167 BYTES_PER_LINE / DUMP_GROUPING + 2;
169 for (i = 0; i < length; i += BYTES_PER_LINE) {
171 int this_line_length = MIN(BYTES_PER_LINE, length - i);
172 int print_offset = 0;
174 for (j = 0; j < this_line_length; j++) {
175 int buf_offset = i + j;
178 * Separate every DUMP_GROUPING bytes by a space.
180 if (buf_offset % DUMP_GROUPING == 0) {
181 print_offset += printf(" ");
185 * Print the two-digit hex value for this byte.
187 unsigned char hex_print = buf[buf_offset];
188 print_offset += printf("%02x ", hex_print);
191 (void) printf("%*s", ascii_start - print_offset, " ");
193 print_ascii_block(buf + i, this_line_length);
198 main(int argc, char *argv[])
200 char *buf = safe_malloc(SPA_MAXBLOCKSIZE);
201 uint64_t drr_record_count[DRR_NUMTYPES] = { 0 };
202 uint64_t total_records = 0;
203 dmu_replay_record_t thedrr;
204 dmu_replay_record_t *drr = &thedrr;
205 struct drr_begin *drrb = &thedrr.drr_u.drr_begin;
206 struct drr_end *drre = &thedrr.drr_u.drr_end;
207 struct drr_object *drro = &thedrr.drr_u.drr_object;
208 struct drr_freeobjects *drrfo = &thedrr.drr_u.drr_freeobjects;
209 struct drr_write *drrw = &thedrr.drr_u.drr_write;
210 struct drr_write_byref *drrwbr = &thedrr.drr_u.drr_write_byref;
211 struct drr_free *drrf = &thedrr.drr_u.drr_free;
212 struct drr_spill *drrs = &thedrr.drr_u.drr_spill;
213 struct drr_write_embedded *drrwe = &thedrr.drr_u.drr_write_embedded;
214 struct drr_checksum *drrc = &thedrr.drr_u.drr_checksum;
216 boolean_t verbose = B_FALSE;
217 boolean_t very_verbose = B_FALSE;
218 boolean_t first = B_TRUE;
220 * dump flag controls whether the contents of any modified data blocks
221 * are printed to the console during processing of the stream. Warning:
222 * for large streams, this can obviously lead to massive prints.
224 boolean_t dump = B_FALSE;
226 zio_cksum_t zc = { 0 };
227 zio_cksum_t pcksum = { 0 };
229 while ((c = getopt(argc, argv, ":vCd")) != -1) {
236 very_verbose = B_TRUE;
242 very_verbose = B_TRUE;
245 (void) fprintf(stderr,
246 "missing argument for '%c' option\n", optopt);
250 (void) fprintf(stderr, "invalid option '%c'\n",
256 if (isatty(STDIN_FILENO)) {
257 (void) fprintf(stderr,
258 "Error: Backup stream can not be read "
260 "You must redirect standard input.\n");
266 while (read_hdr(drr, &zc)) {
269 * If this is the first DMU record being processed, check for
270 * the magic bytes and figure out the endian-ness based on them.
273 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
274 do_byteswap = B_TRUE;
276 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
278 * recalculate header checksum now
279 * that we know it needs to be
282 fletcher_4_incremental_byteswap(drr,
283 sizeof (dmu_replay_record_t), &zc);
285 } else if (drrb->drr_magic != DMU_BACKUP_MAGIC) {
286 (void) fprintf(stderr, "Invalid stream "
287 "(bad magic number)\n");
293 drr->drr_type = BSWAP_32(drr->drr_type);
294 drr->drr_payloadlen =
295 BSWAP_32(drr->drr_payloadlen);
299 * At this point, the leading fields of the replay record
300 * (drr_type and drr_payloadlen) have been byte-swapped if
301 * necessary, but the rest of the data structure (the
302 * union of type-specific structures) is still in its
305 if (drr->drr_type >= DRR_NUMTYPES) {
306 (void) printf("INVALID record found: type 0x%x\n",
308 (void) printf("Aborting.\n");
312 drr_record_count[drr->drr_type]++;
315 switch (drr->drr_type) {
318 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
319 drrb->drr_versioninfo =
320 BSWAP_64(drrb->drr_versioninfo);
321 drrb->drr_creation_time =
322 BSWAP_64(drrb->drr_creation_time);
323 drrb->drr_type = BSWAP_32(drrb->drr_type);
324 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
325 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
327 BSWAP_64(drrb->drr_fromguid);
330 (void) printf("BEGIN record\n");
331 (void) printf("\thdrtype = %lld\n",
332 DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo));
333 (void) printf("\tfeatures = %llx\n",
334 DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo));
335 (void) printf("\tmagic = %llx\n",
336 (u_longlong_t)drrb->drr_magic);
337 (void) printf("\tcreation_time = %llx\n",
338 (u_longlong_t)drrb->drr_creation_time);
339 (void) printf("\ttype = %u\n", drrb->drr_type);
340 (void) printf("\tflags = 0x%x\n", drrb->drr_flags);
341 (void) printf("\ttoguid = %llx\n",
342 (u_longlong_t)drrb->drr_toguid);
343 (void) printf("\tfromguid = %llx\n",
344 (u_longlong_t)drrb->drr_fromguid);
345 (void) printf("\ttoname = %s\n", drrb->drr_toname);
349 if (drr->drr_payloadlen != 0) {
351 int sz = drr->drr_payloadlen;
353 if (sz > SPA_MAXBLOCKSIZE) {
355 buf = safe_malloc(sz);
357 (void) ssread(buf, sz, &zc);
358 if (ferror(send_stream))
360 err = nvlist_unpack(buf, sz, &nv, 0);
362 perror(strerror(err));
363 nvlist_print(stdout, nv);
370 drre->drr_checksum.zc_word[0] =
371 BSWAP_64(drre->drr_checksum.zc_word[0]);
372 drre->drr_checksum.zc_word[1] =
373 BSWAP_64(drre->drr_checksum.zc_word[1]);
374 drre->drr_checksum.zc_word[2] =
375 BSWAP_64(drre->drr_checksum.zc_word[2]);
376 drre->drr_checksum.zc_word[3] =
377 BSWAP_64(drre->drr_checksum.zc_word[3]);
380 * We compare against the *previous* checksum
381 * value, because the stored checksum is of
382 * everything before the DRR_END record.
384 if (do_cksum && !ZIO_CHECKSUM_EQUAL(drre->drr_checksum,
386 (void) printf("Expected checksum differs from "
387 "checksum in stream.\n");
388 (void) printf("Expected checksum = "
389 "%llx/%llx/%llx/%llx\n",
395 (void) printf("END checksum = %llx/%llx/%llx/%llx\n",
396 drre->drr_checksum.zc_word[0],
397 drre->drr_checksum.zc_word[1],
398 drre->drr_checksum.zc_word[2],
399 drre->drr_checksum.zc_word[3]);
401 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
406 drro->drr_object = BSWAP_64(drro->drr_object);
407 drro->drr_type = BSWAP_32(drro->drr_type);
408 drro->drr_bonustype =
409 BSWAP_32(drro->drr_bonustype);
410 drro->drr_blksz = BSWAP_32(drro->drr_blksz);
412 BSWAP_32(drro->drr_bonuslen);
413 drro->drr_toguid = BSWAP_64(drro->drr_toguid);
416 (void) printf("OBJECT object = %llu type = %u "
417 "bonustype = %u blksz = %u bonuslen = %u\n",
418 (u_longlong_t)drro->drr_object,
424 if (drro->drr_bonuslen > 0) {
426 P2ROUNDUP(drro->drr_bonuslen, 8), &zc);
429 P2ROUNDUP(drro->drr_bonuslen, 8));
434 case DRR_FREEOBJECTS:
436 drrfo->drr_firstobj =
437 BSWAP_64(drrfo->drr_firstobj);
439 BSWAP_64(drrfo->drr_numobjs);
440 drrfo->drr_toguid = BSWAP_64(drrfo->drr_toguid);
443 (void) printf("FREEOBJECTS firstobj = %llu "
445 (u_longlong_t)drrfo->drr_firstobj,
446 (u_longlong_t)drrfo->drr_numobjs);
452 drrw->drr_object = BSWAP_64(drrw->drr_object);
453 drrw->drr_type = BSWAP_32(drrw->drr_type);
454 drrw->drr_offset = BSWAP_64(drrw->drr_offset);
455 drrw->drr_length = BSWAP_64(drrw->drr_length);
456 drrw->drr_toguid = BSWAP_64(drrw->drr_toguid);
457 drrw->drr_key.ddk_prop =
458 BSWAP_64(drrw->drr_key.ddk_prop);
461 * If this is verbose and/or dump output,
462 * print info on the modified block
465 (void) printf("WRITE object = %llu type = %u "
466 "checksum type = %u\n"
467 " offset = %llu length = %llu "
469 (u_longlong_t)drrw->drr_object,
471 drrw->drr_checksumtype,
472 (u_longlong_t)drrw->drr_offset,
473 (u_longlong_t)drrw->drr_length,
474 (u_longlong_t)drrw->drr_key.ddk_prop);
477 * Read the contents of the block in from STDIN to buf
479 (void) ssread(buf, drrw->drr_length, &zc);
484 print_block(buf, drrw->drr_length);
486 total_write_size += drrw->drr_length;
489 case DRR_WRITE_BYREF:
492 BSWAP_64(drrwbr->drr_object);
494 BSWAP_64(drrwbr->drr_offset);
496 BSWAP_64(drrwbr->drr_length);
498 BSWAP_64(drrwbr->drr_toguid);
499 drrwbr->drr_refguid =
500 BSWAP_64(drrwbr->drr_refguid);
501 drrwbr->drr_refobject =
502 BSWAP_64(drrwbr->drr_refobject);
503 drrwbr->drr_refoffset =
504 BSWAP_64(drrwbr->drr_refoffset);
505 drrwbr->drr_key.ddk_prop =
506 BSWAP_64(drrwbr->drr_key.ddk_prop);
509 (void) printf("WRITE_BYREF object = %llu "
510 "checksum type = %u props = %llx\n"
511 " offset = %llu length = %llu\n"
512 "toguid = %llx refguid = %llx\n"
513 " refobject = %llu refoffset = %llu\n",
514 (u_longlong_t)drrwbr->drr_object,
515 drrwbr->drr_checksumtype,
516 (u_longlong_t)drrwbr->drr_key.ddk_prop,
517 (u_longlong_t)drrwbr->drr_offset,
518 (u_longlong_t)drrwbr->drr_length,
519 (u_longlong_t)drrwbr->drr_toguid,
520 (u_longlong_t)drrwbr->drr_refguid,
521 (u_longlong_t)drrwbr->drr_refobject,
522 (u_longlong_t)drrwbr->drr_refoffset);
528 drrf->drr_object = BSWAP_64(drrf->drr_object);
529 drrf->drr_offset = BSWAP_64(drrf->drr_offset);
530 drrf->drr_length = BSWAP_64(drrf->drr_length);
533 (void) printf("FREE object = %llu "
534 "offset = %llu length = %lld\n",
535 (u_longlong_t)drrf->drr_object,
536 (u_longlong_t)drrf->drr_offset,
537 (longlong_t)drrf->drr_length);
542 drrs->drr_object = BSWAP_64(drrs->drr_object);
543 drrs->drr_length = BSWAP_64(drrs->drr_length);
546 (void) printf("SPILL block for object = %llu "
547 "length = %llu\n", drrs->drr_object,
550 (void) ssread(buf, drrs->drr_length, &zc);
552 print_block(buf, drrs->drr_length);
555 case DRR_WRITE_EMBEDDED:
558 BSWAP_64(drrwe->drr_object);
560 BSWAP_64(drrwe->drr_offset);
562 BSWAP_64(drrwe->drr_length);
564 BSWAP_64(drrwe->drr_toguid);
566 BSWAP_32(drrwe->drr_lsize);
568 BSWAP_32(drrwe->drr_psize);
571 (void) printf("WRITE_EMBEDDED object = %llu "
572 "offset = %llu length = %llu\n"
573 " toguid = %llx comp = %u etype = %u "
574 "lsize = %u psize = %u\n",
575 (u_longlong_t)drrwe->drr_object,
576 (u_longlong_t)drrwe->drr_offset,
577 (u_longlong_t)drrwe->drr_length,
578 (u_longlong_t)drrwe->drr_toguid,
579 drrwe->drr_compression,
585 P2ROUNDUP(drrwe->drr_psize, 8), &zc);
588 if (drr->drr_type != DRR_BEGIN && very_verbose) {
589 (void) printf(" checksum = %llx/%llx/%llx/%llx\n",
590 (longlong_t)drrc->drr_checksum.zc_word[0],
591 (longlong_t)drrc->drr_checksum.zc_word[1],
592 (longlong_t)drrc->drr_checksum.zc_word[2],
593 (longlong_t)drrc->drr_checksum.zc_word[3]);
599 /* Print final summary */
601 (void) printf("SUMMARY:\n");
602 (void) printf("\tTotal DRR_BEGIN records = %lld\n",
603 (u_longlong_t)drr_record_count[DRR_BEGIN]);
604 (void) printf("\tTotal DRR_END records = %lld\n",
605 (u_longlong_t)drr_record_count[DRR_END]);
606 (void) printf("\tTotal DRR_OBJECT records = %lld\n",
607 (u_longlong_t)drr_record_count[DRR_OBJECT]);
608 (void) printf("\tTotal DRR_FREEOBJECTS records = %lld\n",
609 (u_longlong_t)drr_record_count[DRR_FREEOBJECTS]);
610 (void) printf("\tTotal DRR_WRITE records = %lld\n",
611 (u_longlong_t)drr_record_count[DRR_WRITE]);
612 (void) printf("\tTotal DRR_WRITE_BYREF records = %lld\n",
613 (u_longlong_t)drr_record_count[DRR_WRITE_BYREF]);
614 (void) printf("\tTotal DRR_WRITE_EMBEDDED records = %lld\n",
615 (u_longlong_t)drr_record_count[DRR_WRITE_EMBEDDED]);
616 (void) printf("\tTotal DRR_FREE records = %lld\n",
617 (u_longlong_t)drr_record_count[DRR_FREE]);
618 (void) printf("\tTotal DRR_SPILL records = %lld\n",
619 (u_longlong_t)drr_record_count[DRR_SPILL]);
620 (void) printf("\tTotal records = %lld\n",
621 (u_longlong_t)total_records);
622 (void) printf("\tTotal write size = %lld (0x%llx)\n",
623 (u_longlong_t)total_write_size, (u_longlong_t)total_write_size);
624 (void) printf("\tTotal stream length = %lld (0x%llx)\n",
625 (u_longlong_t)total_stream_len, (u_longlong_t)total_stream_len);