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 27 /* 28 * Copyright (c) 2013, 2014 by Delphix. All rights reserved. 29 */ 30 31 #include <ctype.h> 32 #include <libnvpair.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <strings.h> 36 #include <unistd.h> 37 #include <stddef.h> 38 39 #include <sys/dmu.h> 40 #include <sys/zfs_ioctl.h> 41 #include <zfs_fletcher.h> 42 43 /* 44 * If dump mode is enabled, the number of bytes to print per line 45 */ 46 #define BYTES_PER_LINE 16 47 /* 48 * If dump mode is enabled, the number of bytes to group together, separated 49 * by newlines or spaces 50 */ 51 #define DUMP_GROUPING 4 52 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 59 static void 60 usage(void) 61 { 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, " 66 "implies verbose\n"); 67 exit(1); 68 } 69 70 static void * 71 safe_malloc(size_t size) 72 { 73 void *rv = malloc(size); 74 if (rv == NULL) { 75 (void) fprintf(stderr, "ERROR; failed to allocate %zu bytes\n", 76 size); 77 abort(); 78 } 79 return (rv); 80 } 81 82 /* 83 * ssread - send stream read. 84 * 85 * Read while computing incremental checksum 86 */ 87 static size_t 88 ssread(void *buf, size_t len, zio_cksum_t *cksum) 89 { 90 size_t outlen; 91 92 if ((outlen = fread(buf, len, 1, send_stream)) == 0) 93 return (0); 94 95 if (do_cksum) { 96 if (do_byteswap) 97 fletcher_4_incremental_byteswap(buf, len, cksum); 98 else 99 fletcher_4_incremental_native(buf, len, cksum); 100 } 101 total_stream_len += len; 102 return (outlen); 103 } 104 105 static size_t 106 read_hdr(dmu_replay_record_t *drr, zio_cksum_t *cksum) 107 { 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); 111 if (r == 0) 112 return (0); 113 zio_cksum_t saved_cksum = *cksum; 114 r = ssread(&drr->drr_u.drr_checksum.drr_checksum, 115 sizeof (zio_cksum_t), cksum); 116 if (r == 0) 117 return (0); 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]); 128 return (0); 129 } 130 return (sizeof (*drr)); 131 } 132 133 /* 134 * Print part of a block in ASCII characters 135 */ 136 static void 137 print_ascii_block(char *subbuf, int length) 138 { 139 int i; 140 141 for (i = 0; i < length; i++) { 142 char char_print = isprint(subbuf[i]) ? subbuf[i] : '.'; 143 if (i != 0 && i % DUMP_GROUPING == 0) { 144 (void) printf(" "); 145 } 146 (void) printf("%c", char_print); 147 } 148 (void) printf("\n"); 149 } 150 151 /* 152 * print_block - Dump the contents of a modified block to STDOUT 153 * 154 * Assume that buf has capacity evenly divisible by BYTES_PER_LINE 155 */ 156 static void 157 print_block(char *buf, int length) 158 { 159 int i; 160 /* 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 164 * groupings. 165 */ 166 int ascii_start = BYTES_PER_LINE * 3 + 167 BYTES_PER_LINE / DUMP_GROUPING + 2; 168 169 for (i = 0; i < length; i += BYTES_PER_LINE) { 170 int j; 171 int this_line_length = MIN(BYTES_PER_LINE, length - i); 172 int print_offset = 0; 173 174 for (j = 0; j < this_line_length; j++) { 175 int buf_offset = i + j; 176 177 /* 178 * Separate every DUMP_GROUPING bytes by a space. 179 */ 180 if (buf_offset % DUMP_GROUPING == 0) { 181 print_offset += printf(" "); 182 } 183 184 /* 185 * Print the two-digit hex value for this byte. 186 */ 187 unsigned char hex_print = buf[buf_offset]; 188 print_offset += printf("%02x ", hex_print); 189 } 190 191 (void) printf("%*s", ascii_start - print_offset, " "); 192 193 print_ascii_block(buf + i, this_line_length); 194 } 195 } 196 197 int 198 main(int argc, char *argv[]) 199 { 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; 215 char c; 216 boolean_t verbose = B_FALSE; 217 boolean_t very_verbose = B_FALSE; 218 boolean_t first = B_TRUE; 219 /* 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. 223 */ 224 boolean_t dump = B_FALSE; 225 int err; 226 zio_cksum_t zc = { 0 }; 227 zio_cksum_t pcksum = { 0 }; 228 229 while ((c = getopt(argc, argv, ":vCd")) != -1) { 230 switch (c) { 231 case 'C': 232 do_cksum = B_FALSE; 233 break; 234 case 'v': 235 if (verbose) 236 very_verbose = B_TRUE; 237 verbose = B_TRUE; 238 break; 239 case 'd': 240 dump = B_TRUE; 241 verbose = B_TRUE; 242 very_verbose = B_TRUE; 243 break; 244 case ':': 245 (void) fprintf(stderr, 246 "missing argument for '%c' option\n", optopt); 247 usage(); 248 break; 249 case '?': 250 (void) fprintf(stderr, "invalid option '%c'\n", 251 optopt); 252 usage(); 253 } 254 } 255 256 if (isatty(STDIN_FILENO)) { 257 (void) fprintf(stderr, 258 "Error: Backup stream can not be read " 259 "from a terminal.\n" 260 "You must redirect standard input.\n"); 261 exit(1); 262 } 263 264 send_stream = stdin; 265 while (read_hdr(drr, &zc)) { 266 267 /* 268 * If this is the first DMU record being processed, check for 269 * the magic bytes and figure out the endian-ness based on them. 270 */ 271 if (first) { 272 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) { 273 do_byteswap = B_TRUE; 274 if (do_cksum) { 275 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0); 276 /* 277 * recalculate header checksum now 278 * that we know it needs to be 279 * byteswapped. 280 */ 281 fletcher_4_incremental_byteswap(drr, 282 sizeof (dmu_replay_record_t), &zc); 283 } 284 } else if (drrb->drr_magic != DMU_BACKUP_MAGIC) { 285 (void) fprintf(stderr, "Invalid stream " 286 "(bad magic number)\n"); 287 exit(1); 288 } 289 first = B_FALSE; 290 } 291 if (do_byteswap) { 292 drr->drr_type = BSWAP_32(drr->drr_type); 293 drr->drr_payloadlen = 294 BSWAP_32(drr->drr_payloadlen); 295 } 296 297 /* 298 * At this point, the leading fields of the replay record 299 * (drr_type and drr_payloadlen) have been byte-swapped if 300 * necessary, but the rest of the data structure (the 301 * union of type-specific structures) is still in its 302 * original state. 303 */ 304 if (drr->drr_type >= DRR_NUMTYPES) { 305 (void) printf("INVALID record found: type 0x%x\n", 306 drr->drr_type); 307 (void) printf("Aborting.\n"); 308 exit(1); 309 } 310 311 drr_record_count[drr->drr_type]++; 312 total_records++; 313 314 switch (drr->drr_type) { 315 case DRR_BEGIN: 316 if (do_byteswap) { 317 drrb->drr_magic = BSWAP_64(drrb->drr_magic); 318 drrb->drr_versioninfo = 319 BSWAP_64(drrb->drr_versioninfo); 320 drrb->drr_creation_time = 321 BSWAP_64(drrb->drr_creation_time); 322 drrb->drr_type = BSWAP_32(drrb->drr_type); 323 drrb->drr_flags = BSWAP_32(drrb->drr_flags); 324 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid); 325 drrb->drr_fromguid = 326 BSWAP_64(drrb->drr_fromguid); 327 } 328 329 (void) printf("BEGIN record\n"); 330 (void) printf("\thdrtype = %lld\n", 331 DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo)); 332 (void) printf("\tfeatures = %llx\n", 333 DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo)); 334 (void) printf("\tmagic = %llx\n", 335 (u_longlong_t)drrb->drr_magic); 336 (void) printf("\tcreation_time = %llx\n", 337 (u_longlong_t)drrb->drr_creation_time); 338 (void) printf("\ttype = %u\n", drrb->drr_type); 339 (void) printf("\tflags = 0x%x\n", drrb->drr_flags); 340 (void) printf("\ttoguid = %llx\n", 341 (u_longlong_t)drrb->drr_toguid); 342 (void) printf("\tfromguid = %llx\n", 343 (u_longlong_t)drrb->drr_fromguid); 344 (void) printf("\ttoname = %s\n", drrb->drr_toname); 345 if (verbose) 346 (void) printf("\n"); 347 348 if (drr->drr_payloadlen != 0) { 349 nvlist_t *nv; 350 int sz = drr->drr_payloadlen; 351 352 if (sz > SPA_MAXBLOCKSIZE) { 353 free(buf); 354 buf = safe_malloc(sz); 355 } 356 (void) ssread(buf, sz, &zc); 357 if (ferror(send_stream)) 358 perror("fread"); 359 err = nvlist_unpack(buf, sz, &nv, 0); 360 if (err) 361 perror(strerror(err)); 362 nvlist_print(stdout, nv); 363 nvlist_free(nv); 364 } 365 break; 366 367 case DRR_END: 368 if (do_byteswap) { 369 drre->drr_checksum.zc_word[0] = 370 BSWAP_64(drre->drr_checksum.zc_word[0]); 371 drre->drr_checksum.zc_word[1] = 372 BSWAP_64(drre->drr_checksum.zc_word[1]); 373 drre->drr_checksum.zc_word[2] = 374 BSWAP_64(drre->drr_checksum.zc_word[2]); 375 drre->drr_checksum.zc_word[3] = 376 BSWAP_64(drre->drr_checksum.zc_word[3]); 377 } 378 /* 379 * We compare against the *previous* checksum 380 * value, because the stored checksum is of 381 * everything before the DRR_END record. 382 */ 383 if (do_cksum && !ZIO_CHECKSUM_EQUAL(drre->drr_checksum, 384 pcksum)) { 385 (void) printf("Expected checksum differs from " 386 "checksum in stream.\n"); 387 (void) printf("Expected checksum = " 388 "%llx/%llx/%llx/%llx\n", 389 pcksum.zc_word[0], 390 pcksum.zc_word[1], 391 pcksum.zc_word[2], 392 pcksum.zc_word[3]); 393 } 394 (void) printf("END checksum = %llx/%llx/%llx/%llx\n", 395 drre->drr_checksum.zc_word[0], 396 drre->drr_checksum.zc_word[1], 397 drre->drr_checksum.zc_word[2], 398 drre->drr_checksum.zc_word[3]); 399 400 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0); 401 break; 402 403 case DRR_OBJECT: 404 if (do_byteswap) { 405 drro->drr_object = BSWAP_64(drro->drr_object); 406 drro->drr_type = BSWAP_32(drro->drr_type); 407 drro->drr_bonustype = 408 BSWAP_32(drro->drr_bonustype); 409 drro->drr_blksz = BSWAP_32(drro->drr_blksz); 410 drro->drr_bonuslen = 411 BSWAP_32(drro->drr_bonuslen); 412 drro->drr_toguid = BSWAP_64(drro->drr_toguid); 413 } 414 if (verbose) { 415 (void) printf("OBJECT object = %llu type = %u " 416 "bonustype = %u blksz = %u bonuslen = %u\n", 417 (u_longlong_t)drro->drr_object, 418 drro->drr_type, 419 drro->drr_bonustype, 420 drro->drr_blksz, 421 drro->drr_bonuslen); 422 } 423 if (drro->drr_bonuslen > 0) { 424 (void) ssread(buf, 425 P2ROUNDUP(drro->drr_bonuslen, 8), &zc); 426 if (dump) { 427 print_block(buf, 428 P2ROUNDUP(drro->drr_bonuslen, 8)); 429 } 430 } 431 break; 432 433 case DRR_FREEOBJECTS: 434 if (do_byteswap) { 435 drrfo->drr_firstobj = 436 BSWAP_64(drrfo->drr_firstobj); 437 drrfo->drr_numobjs = 438 BSWAP_64(drrfo->drr_numobjs); 439 drrfo->drr_toguid = BSWAP_64(drrfo->drr_toguid); 440 } 441 if (verbose) { 442 (void) printf("FREEOBJECTS firstobj = %llu " 443 "numobjs = %llu\n", 444 (u_longlong_t)drrfo->drr_firstobj, 445 (u_longlong_t)drrfo->drr_numobjs); 446 } 447 break; 448 449 case DRR_WRITE: 450 if (do_byteswap) { 451 drrw->drr_object = BSWAP_64(drrw->drr_object); 452 drrw->drr_type = BSWAP_32(drrw->drr_type); 453 drrw->drr_offset = BSWAP_64(drrw->drr_offset); 454 drrw->drr_length = BSWAP_64(drrw->drr_length); 455 drrw->drr_toguid = BSWAP_64(drrw->drr_toguid); 456 drrw->drr_key.ddk_prop = 457 BSWAP_64(drrw->drr_key.ddk_prop); 458 } 459 /* 460 * If this is verbose and/or dump output, 461 * print info on the modified block 462 */ 463 if (verbose) { 464 (void) printf("WRITE object = %llu type = %u " 465 "checksum type = %u\n" 466 " offset = %llu length = %llu " 467 "props = %llx\n", 468 (u_longlong_t)drrw->drr_object, 469 drrw->drr_type, 470 drrw->drr_checksumtype, 471 (u_longlong_t)drrw->drr_offset, 472 (u_longlong_t)drrw->drr_length, 473 (u_longlong_t)drrw->drr_key.ddk_prop); 474 } 475 /* 476 * Read the contents of the block in from STDIN to buf 477 */ 478 (void) ssread(buf, drrw->drr_length, &zc); 479 /* 480 * If in dump mode 481 */ 482 if (dump) { 483 print_block(buf, drrw->drr_length); 484 } 485 total_write_size += drrw->drr_length; 486 break; 487 488 case DRR_WRITE_BYREF: 489 if (do_byteswap) { 490 drrwbr->drr_object = 491 BSWAP_64(drrwbr->drr_object); 492 drrwbr->drr_offset = 493 BSWAP_64(drrwbr->drr_offset); 494 drrwbr->drr_length = 495 BSWAP_64(drrwbr->drr_length); 496 drrwbr->drr_toguid = 497 BSWAP_64(drrwbr->drr_toguid); 498 drrwbr->drr_refguid = 499 BSWAP_64(drrwbr->drr_refguid); 500 drrwbr->drr_refobject = 501 BSWAP_64(drrwbr->drr_refobject); 502 drrwbr->drr_refoffset = 503 BSWAP_64(drrwbr->drr_refoffset); 504 drrwbr->drr_key.ddk_prop = 505 BSWAP_64(drrwbr->drr_key.ddk_prop); 506 } 507 if (verbose) { 508 (void) printf("WRITE_BYREF object = %llu " 509 "checksum type = %u props = %llx\n" 510 " offset = %llu length = %llu\n" 511 "toguid = %llx refguid = %llx\n" 512 " refobject = %llu refoffset = %llu\n", 513 (u_longlong_t)drrwbr->drr_object, 514 drrwbr->drr_checksumtype, 515 (u_longlong_t)drrwbr->drr_key.ddk_prop, 516 (u_longlong_t)drrwbr->drr_offset, 517 (u_longlong_t)drrwbr->drr_length, 518 (u_longlong_t)drrwbr->drr_toguid, 519 (u_longlong_t)drrwbr->drr_refguid, 520 (u_longlong_t)drrwbr->drr_refobject, 521 (u_longlong_t)drrwbr->drr_refoffset); 522 } 523 break; 524 525 case DRR_FREE: 526 if (do_byteswap) { 527 drrf->drr_object = BSWAP_64(drrf->drr_object); 528 drrf->drr_offset = BSWAP_64(drrf->drr_offset); 529 drrf->drr_length = BSWAP_64(drrf->drr_length); 530 } 531 if (verbose) { 532 (void) printf("FREE object = %llu " 533 "offset = %llu length = %lld\n", 534 (u_longlong_t)drrf->drr_object, 535 (u_longlong_t)drrf->drr_offset, 536 (longlong_t)drrf->drr_length); 537 } 538 break; 539 case DRR_SPILL: 540 if (do_byteswap) { 541 drrs->drr_object = BSWAP_64(drrs->drr_object); 542 drrs->drr_length = BSWAP_64(drrs->drr_length); 543 } 544 if (verbose) { 545 (void) printf("SPILL block for object = %llu " 546 "length = %llu\n", drrs->drr_object, 547 drrs->drr_length); 548 } 549 (void) ssread(buf, drrs->drr_length, &zc); 550 if (dump) { 551 print_block(buf, drrs->drr_length); 552 } 553 break; 554 case DRR_WRITE_EMBEDDED: 555 if (do_byteswap) { 556 drrwe->drr_object = 557 BSWAP_64(drrwe->drr_object); 558 drrwe->drr_offset = 559 BSWAP_64(drrwe->drr_offset); 560 drrwe->drr_length = 561 BSWAP_64(drrwe->drr_length); 562 drrwe->drr_toguid = 563 BSWAP_64(drrwe->drr_toguid); 564 drrwe->drr_lsize = 565 BSWAP_32(drrwe->drr_lsize); 566 drrwe->drr_psize = 567 BSWAP_32(drrwe->drr_psize); 568 } 569 if (verbose) { 570 (void) printf("WRITE_EMBEDDED object = %llu " 571 "offset = %llu length = %llu\n" 572 " toguid = %llx comp = %u etype = %u " 573 "lsize = %u psize = %u\n", 574 (u_longlong_t)drrwe->drr_object, 575 (u_longlong_t)drrwe->drr_offset, 576 (u_longlong_t)drrwe->drr_length, 577 (u_longlong_t)drrwe->drr_toguid, 578 drrwe->drr_compression, 579 drrwe->drr_etype, 580 drrwe->drr_lsize, 581 drrwe->drr_psize); 582 } 583 (void) ssread(buf, 584 P2ROUNDUP(drrwe->drr_psize, 8), &zc); 585 break; 586 } 587 if (drr->drr_type != DRR_BEGIN && very_verbose) { 588 (void) printf(" checksum = %llx/%llx/%llx/%llx\n", 589 (longlong_t)drrc->drr_checksum.zc_word[0], 590 (longlong_t)drrc->drr_checksum.zc_word[1], 591 (longlong_t)drrc->drr_checksum.zc_word[2], 592 (longlong_t)drrc->drr_checksum.zc_word[3]); 593 } 594 pcksum = zc; 595 } 596 free(buf); 597 598 /* Print final summary */ 599 600 (void) printf("SUMMARY:\n"); 601 (void) printf("\tTotal DRR_BEGIN records = %lld\n", 602 (u_longlong_t)drr_record_count[DRR_BEGIN]); 603 (void) printf("\tTotal DRR_END records = %lld\n", 604 (u_longlong_t)drr_record_count[DRR_END]); 605 (void) printf("\tTotal DRR_OBJECT records = %lld\n", 606 (u_longlong_t)drr_record_count[DRR_OBJECT]); 607 (void) printf("\tTotal DRR_FREEOBJECTS records = %lld\n", 608 (u_longlong_t)drr_record_count[DRR_FREEOBJECTS]); 609 (void) printf("\tTotal DRR_WRITE records = %lld\n", 610 (u_longlong_t)drr_record_count[DRR_WRITE]); 611 (void) printf("\tTotal DRR_WRITE_BYREF records = %lld\n", 612 (u_longlong_t)drr_record_count[DRR_WRITE_BYREF]); 613 (void) printf("\tTotal DRR_WRITE_EMBEDDED records = %lld\n", 614 (u_longlong_t)drr_record_count[DRR_WRITE_EMBEDDED]); 615 (void) printf("\tTotal DRR_FREE records = %lld\n", 616 (u_longlong_t)drr_record_count[DRR_FREE]); 617 (void) printf("\tTotal DRR_SPILL records = %lld\n", 618 (u_longlong_t)drr_record_count[DRR_SPILL]); 619 (void) printf("\tTotal records = %lld\n", 620 (u_longlong_t)total_records); 621 (void) printf("\tTotal write size = %lld (0x%llx)\n", 622 (u_longlong_t)total_write_size, (u_longlong_t)total_write_size); 623 (void) printf("\tTotal stream length = %lld (0x%llx)\n", 624 (u_longlong_t)total_stream_len, (u_longlong_t)total_stream_len); 625 return (0); 626 } 627