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 by Delphix. All rights reserved.
32 #include <libnvpair.h>
39 #include <sys/zfs_ioctl.h>
40 #include <zfs_fletcher.h>
43 * If dump mode is enabled, the number of bytes to print per line
45 #define BYTES_PER_LINE 16
47 * If dump mode is enabled, the number of bytes to group together, separated
48 * by newlines or spaces
50 #define DUMP_GROUPING 4
52 uint64_t drr_record_count[DRR_NUMTYPES];
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;
58 #define INITIAL_BUFLEN (1<<20)
63 (void) fprintf(stderr, "usage: zstreamdump [-v] [-C] [-d] < file\n");
64 (void) fprintf(stderr, "\t -v -- verbose\n");
65 (void) fprintf(stderr, "\t -C -- suppress checksum verification\n");
66 (void) fprintf(stderr, "\t -d -- dump contents of blocks modified, "
72 * ssread - send stream read.
74 * Read while computing incremental checksum
78 ssread(void *buf, size_t len, zio_cksum_t *cksum)
82 if ((outlen = fread(buf, len, 1, send_stream)) == 0)
85 if (do_cksum && cksum) {
87 fletcher_4_incremental_byteswap(buf, len, cksum);
89 fletcher_4_incremental_native(buf, len, cksum);
91 total_stream_len += len;
96 * Print part of a block in ASCII characters
99 print_ascii_block(char *subbuf, int length)
103 for (i = 0; i < length; i++) {
104 char char_print = isprint(subbuf[i]) ? subbuf[i] : '.';
105 if (i != 0 && i % DUMP_GROUPING == 0) {
108 (void) printf("%c", char_print);
114 * print_block - Dump the contents of a modified block to STDOUT
116 * Assume that buf has capacity evenly divisible by BYTES_PER_LINE
119 print_block(char *buf, int length)
123 * Start printing ASCII characters at a constant offset, after
124 * the hex prints. Leave 3 characters per byte on a line (2 digit
125 * hex number plus 1 space) plus spaces between characters and
128 int ascii_start = BYTES_PER_LINE * 3 +
129 BYTES_PER_LINE / DUMP_GROUPING + 2;
131 for (i = 0; i < length; i += BYTES_PER_LINE) {
133 int this_line_length = MIN(BYTES_PER_LINE, length - i);
134 int print_offset = 0;
136 for (j = 0; j < this_line_length; j++) {
137 int buf_offset = i + j;
140 * Separate every DUMP_GROUPING bytes by a space.
142 if (buf_offset % DUMP_GROUPING == 0) {
143 print_offset += printf(" ");
147 * Print the two-digit hex value for this byte.
149 unsigned char hex_print = buf[buf_offset];
150 print_offset += printf("%02x ", hex_print);
153 (void) printf("%*s", ascii_start - print_offset, " ");
155 print_ascii_block(buf + i, this_line_length);
160 main(int argc, char *argv[])
162 char *buf = malloc(INITIAL_BUFLEN);
163 dmu_replay_record_t thedrr;
164 dmu_replay_record_t *drr = &thedrr;
165 struct drr_begin *drrb = &thedrr.drr_u.drr_begin;
166 struct drr_end *drre = &thedrr.drr_u.drr_end;
167 struct drr_object *drro = &thedrr.drr_u.drr_object;
168 struct drr_freeobjects *drrfo = &thedrr.drr_u.drr_freeobjects;
169 struct drr_write *drrw = &thedrr.drr_u.drr_write;
170 struct drr_write_byref *drrwbr = &thedrr.drr_u.drr_write_byref;
171 struct drr_free *drrf = &thedrr.drr_u.drr_free;
172 struct drr_spill *drrs = &thedrr.drr_u.drr_spill;
174 boolean_t verbose = B_FALSE;
175 boolean_t first = B_TRUE;
177 * dump flag controls whether the contents of any modified data blocks
178 * are printed to the console during processing of the stream. Warning:
179 * for large streams, this can obviously lead to massive prints.
181 boolean_t dump = B_FALSE;
183 zio_cksum_t zc = { 0 };
184 zio_cksum_t pcksum = { 0 };
186 while ((c = getopt(argc, argv, ":vCd")) != -1) {
199 (void) fprintf(stderr,
200 "missing argument for '%c' option\n", optopt);
204 (void) fprintf(stderr, "invalid option '%c'\n",
210 if (isatty(STDIN_FILENO)) {
211 (void) fprintf(stderr,
212 "Error: Backup stream can not be read "
214 "You must redirect standard input.\n");
220 while (ssread(drr, sizeof (dmu_replay_record_t), &zc)) {
223 * If this is the first DMU record being processed, check for
224 * the magic bytes and figure out the endian-ness based on them.
227 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
228 do_byteswap = B_TRUE;
230 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
232 * recalculate header checksum now
233 * that we know it needs to be
236 fletcher_4_incremental_byteswap(drr,
237 sizeof (dmu_replay_record_t), &zc);
239 } else if (drrb->drr_magic != DMU_BACKUP_MAGIC) {
240 (void) fprintf(stderr, "Invalid stream "
241 "(bad magic number)\n");
247 drr->drr_type = BSWAP_32(drr->drr_type);
248 drr->drr_payloadlen =
249 BSWAP_32(drr->drr_payloadlen);
253 * At this point, the leading fields of the replay record
254 * (drr_type and drr_payloadlen) have been byte-swapped if
255 * necessary, but the rest of the data structure (the
256 * union of type-specific structures) is still in its
259 if (drr->drr_type >= DRR_NUMTYPES) {
260 (void) printf("INVALID record found: type 0x%x\n",
262 (void) printf("Aborting.\n");
266 drr_record_count[drr->drr_type]++;
268 switch (drr->drr_type) {
271 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
272 drrb->drr_versioninfo =
273 BSWAP_64(drrb->drr_versioninfo);
274 drrb->drr_creation_time =
275 BSWAP_64(drrb->drr_creation_time);
276 drrb->drr_type = BSWAP_32(drrb->drr_type);
277 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
278 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
280 BSWAP_64(drrb->drr_fromguid);
283 (void) printf("BEGIN record\n");
284 (void) printf("\thdrtype = %lld\n",
285 DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo));
286 (void) printf("\tfeatures = %llx\n",
287 DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo));
288 (void) printf("\tmagic = %llx\n",
289 (u_longlong_t)drrb->drr_magic);
290 (void) printf("\tcreation_time = %llx\n",
291 (u_longlong_t)drrb->drr_creation_time);
292 (void) printf("\ttype = %u\n", drrb->drr_type);
293 (void) printf("\tflags = 0x%x\n", drrb->drr_flags);
294 (void) printf("\ttoguid = %llx\n",
295 (u_longlong_t)drrb->drr_toguid);
296 (void) printf("\tfromguid = %llx\n",
297 (u_longlong_t)drrb->drr_fromguid);
298 (void) printf("\ttoname = %s\n", drrb->drr_toname);
302 if ((DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
303 DMU_COMPOUNDSTREAM) && drr->drr_payloadlen != 0) {
305 int sz = drr->drr_payloadlen;
307 if (sz > INITIAL_BUFLEN) {
311 (void) ssread(buf, sz, &zc);
312 if (ferror(send_stream))
314 err = nvlist_unpack(buf, sz, &nv, 0);
316 perror(strerror(err));
317 nvlist_print(stdout, nv);
324 drre->drr_checksum.zc_word[0] =
325 BSWAP_64(drre->drr_checksum.zc_word[0]);
326 drre->drr_checksum.zc_word[1] =
327 BSWAP_64(drre->drr_checksum.zc_word[1]);
328 drre->drr_checksum.zc_word[2] =
329 BSWAP_64(drre->drr_checksum.zc_word[2]);
330 drre->drr_checksum.zc_word[3] =
331 BSWAP_64(drre->drr_checksum.zc_word[3]);
334 * We compare against the *previous* checksum
335 * value, because the stored checksum is of
336 * everything before the DRR_END record.
338 if (do_cksum && !ZIO_CHECKSUM_EQUAL(drre->drr_checksum,
340 (void) printf("Expected checksum differs from "
341 "checksum in stream.\n");
342 (void) printf("Expected checksum = "
343 "%llx/%llx/%llx/%llx\n",
349 (void) printf("END checksum = %llx/%llx/%llx/%llx\n",
350 drre->drr_checksum.zc_word[0],
351 drre->drr_checksum.zc_word[1],
352 drre->drr_checksum.zc_word[2],
353 drre->drr_checksum.zc_word[3]);
355 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
360 drro->drr_object = BSWAP_64(drro->drr_object);
361 drro->drr_type = BSWAP_32(drro->drr_type);
362 drro->drr_bonustype =
363 BSWAP_32(drro->drr_bonustype);
364 drro->drr_blksz = BSWAP_32(drro->drr_blksz);
366 BSWAP_32(drro->drr_bonuslen);
367 drro->drr_toguid = BSWAP_64(drro->drr_toguid);
370 (void) printf("OBJECT object = %llu type = %u "
371 "bonustype = %u blksz = %u bonuslen = %u\n",
372 (u_longlong_t)drro->drr_object,
378 if (drro->drr_bonuslen > 0) {
379 (void) ssread(buf, P2ROUNDUP(drro->drr_bonuslen,
383 P2ROUNDUP(drro->drr_bonuslen, 8));
388 case DRR_FREEOBJECTS:
390 drrfo->drr_firstobj =
391 BSWAP_64(drrfo->drr_firstobj);
393 BSWAP_64(drrfo->drr_numobjs);
394 drrfo->drr_toguid = BSWAP_64(drrfo->drr_toguid);
397 (void) printf("FREEOBJECTS firstobj = %llu "
399 (u_longlong_t)drrfo->drr_firstobj,
400 (u_longlong_t)drrfo->drr_numobjs);
406 drrw->drr_object = BSWAP_64(drrw->drr_object);
407 drrw->drr_type = BSWAP_32(drrw->drr_type);
408 drrw->drr_offset = BSWAP_64(drrw->drr_offset);
409 drrw->drr_length = BSWAP_64(drrw->drr_length);
410 drrw->drr_toguid = BSWAP_64(drrw->drr_toguid);
411 drrw->drr_key.ddk_prop =
412 BSWAP_64(drrw->drr_key.ddk_prop);
415 * If this is verbose and/or dump output,
416 * print info on the modified block
419 (void) printf("WRITE object = %llu type = %u "
420 "checksum type = %u\n"
421 "offset = %llu length = %llu "
423 (u_longlong_t)drrw->drr_object,
425 drrw->drr_checksumtype,
426 (u_longlong_t)drrw->drr_offset,
427 (u_longlong_t)drrw->drr_length,
428 (u_longlong_t)drrw->drr_key.ddk_prop);
431 * Read the contents of the block in from STDIN to buf
433 (void) ssread(buf, drrw->drr_length, &zc);
438 print_block(buf, drrw->drr_length);
440 total_write_size += drrw->drr_length;
443 case DRR_WRITE_BYREF:
446 BSWAP_64(drrwbr->drr_object);
448 BSWAP_64(drrwbr->drr_offset);
450 BSWAP_64(drrwbr->drr_length);
452 BSWAP_64(drrwbr->drr_toguid);
453 drrwbr->drr_refguid =
454 BSWAP_64(drrwbr->drr_refguid);
455 drrwbr->drr_refobject =
456 BSWAP_64(drrwbr->drr_refobject);
457 drrwbr->drr_refoffset =
458 BSWAP_64(drrwbr->drr_refoffset);
459 drrwbr->drr_key.ddk_prop =
460 BSWAP_64(drrwbr->drr_key.ddk_prop);
463 (void) printf("WRITE_BYREF object = %llu "
464 "checksum type = %u props = %llx\n"
465 "offset = %llu length = %llu\n"
466 "toguid = %llx refguid = %llx\n"
467 "refobject = %llu refoffset = %llu\n",
468 (u_longlong_t)drrwbr->drr_object,
469 drrwbr->drr_checksumtype,
470 (u_longlong_t)drrwbr->drr_key.ddk_prop,
471 (u_longlong_t)drrwbr->drr_offset,
472 (u_longlong_t)drrwbr->drr_length,
473 (u_longlong_t)drrwbr->drr_toguid,
474 (u_longlong_t)drrwbr->drr_refguid,
475 (u_longlong_t)drrwbr->drr_refobject,
476 (u_longlong_t)drrwbr->drr_refoffset);
482 drrf->drr_object = BSWAP_64(drrf->drr_object);
483 drrf->drr_offset = BSWAP_64(drrf->drr_offset);
484 drrf->drr_length = BSWAP_64(drrf->drr_length);
487 (void) printf("FREE object = %llu "
488 "offset = %llu length = %lld\n",
489 (u_longlong_t)drrf->drr_object,
490 (u_longlong_t)drrf->drr_offset,
491 (longlong_t)drrf->drr_length);
496 drrs->drr_object = BSWAP_64(drrs->drr_object);
497 drrs->drr_length = BSWAP_64(drrs->drr_length);
500 (void) printf("SPILL block for object = %llu "
501 "length = %llu\n", drrs->drr_object,
504 (void) ssread(buf, drrs->drr_length, &zc);
506 print_block(buf, drrs->drr_length);
514 /* Print final summary */
516 (void) printf("SUMMARY:\n");
517 (void) printf("\tTotal DRR_BEGIN records = %lld\n",
518 (u_longlong_t)drr_record_count[DRR_BEGIN]);
519 (void) printf("\tTotal DRR_END records = %lld\n",
520 (u_longlong_t)drr_record_count[DRR_END]);
521 (void) printf("\tTotal DRR_OBJECT records = %lld\n",
522 (u_longlong_t)drr_record_count[DRR_OBJECT]);
523 (void) printf("\tTotal DRR_FREEOBJECTS records = %lld\n",
524 (u_longlong_t)drr_record_count[DRR_FREEOBJECTS]);
525 (void) printf("\tTotal DRR_WRITE records = %lld\n",
526 (u_longlong_t)drr_record_count[DRR_WRITE]);
527 (void) printf("\tTotal DRR_FREE records = %lld\n",
528 (u_longlong_t)drr_record_count[DRR_FREE]);
529 (void) printf("\tTotal DRR_SPILL records = %lld\n",
530 (u_longlong_t)drr_record_count[DRR_SPILL]);
531 (void) printf("\tTotal records = %lld\n",
532 (u_longlong_t)(drr_record_count[DRR_BEGIN] +
533 drr_record_count[DRR_OBJECT] +
534 drr_record_count[DRR_FREEOBJECTS] +
535 drr_record_count[DRR_WRITE] +
536 drr_record_count[DRR_FREE] +
537 drr_record_count[DRR_SPILL] +
538 drr_record_count[DRR_END]));
539 (void) printf("\tTotal write size = %lld (0x%llx)\n",
540 (u_longlong_t)total_write_size, (u_longlong_t)total_write_size);
541 (void) printf("\tTotal stream length = %lld (0x%llx)\n",
542 (u_longlong_t)total_stream_len, (u_longlong_t)total_stream_len);