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