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