xref: /titanic_50/usr/src/cmd/zstreamdump/zstreamdump.c (revision faac71c002f7c7a98741f991b25937b24f309df0)
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 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 
38 #include <sys/dmu.h>
39 #include <sys/zfs_ioctl.h>
40 #include <zfs_fletcher.h>
41 
42 /*
43  * If dump mode is enabled, the number of bytes to print per line
44  */
45 #define	BYTES_PER_LINE	16
46 /*
47  * If dump mode is enabled, the number of bytes to group together, separated
48  * by newlines or spaces
49  */
50 #define	DUMP_GROUPING	4
51 
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)
59 
60 static void
61 usage(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 
71 /*
72  * ssread - send stream read.
73  *
74  * Read while computing incremental checksum
75  */
76 
77 static size_t
78 ssread(void *buf, size_t len, zio_cksum_t *cksum)
79 {
80 	size_t outlen;
81 
82 	if ((outlen = fread(buf, len, 1, send_stream)) == 0)
83 		return (0);
84 
85 	if (do_cksum && cksum) {
86 		if (do_byteswap)
87 			fletcher_4_incremental_byteswap(buf, len, cksum);
88 		else
89 			fletcher_4_incremental_native(buf, len, cksum);
90 	}
91 	total_stream_len += len;
92 	return (outlen);
93 }
94 
95 /*
96  * Print part of a block in ASCII characters
97  */
98 static void
99 print_ascii_block(char *subbuf, int length)
100 {
101 	int i;
102 
103 	for (i = 0; i < length; i++) {
104 		char char_print = isprint(subbuf[i]) ? subbuf[i] : '.';
105 		if (i != 0 && i % DUMP_GROUPING == 0) {
106 			(void) printf(" ");
107 		}
108 		(void) printf("%c", char_print);
109 	}
110 	(void) printf("\n");
111 }
112 
113 /*
114  * print_block - Dump the contents of a modified block to STDOUT
115  *
116  * Assume that buf has capacity evenly divisible by BYTES_PER_LINE
117  */
118 static void
119 print_block(char *buf, int length)
120 {
121 	int i;
122 	/*
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
126 	 * groupings
127 	 */
128 	int ascii_start = BYTES_PER_LINE * 3 +
129 	    BYTES_PER_LINE / DUMP_GROUPING + 2;
130 
131 	for (i = 0; i < length; i += BYTES_PER_LINE) {
132 		int j;
133 		int this_line_length = MIN(BYTES_PER_LINE, length - i);
134 		int print_offset = 0;
135 
136 		for (j = 0; j < this_line_length; j++) {
137 			int buf_offset = i + j;
138 
139 			/*
140 			 * Separate every DUMP_GROUPING bytes by a space.
141 			 */
142 			if (buf_offset % DUMP_GROUPING == 0) {
143 				print_offset += printf(" ");
144 			}
145 
146 			/*
147 			 * Print the two-digit hex value for this byte.
148 			 */
149 			unsigned char hex_print = buf[buf_offset];
150 			print_offset += printf("%02x ", hex_print);
151 		}
152 
153 		(void) printf("%*s", ascii_start - print_offset, " ");
154 
155 		print_ascii_block(buf + i, this_line_length);
156 	}
157 }
158 
159 int
160 main(int argc, char *argv[])
161 {
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;
173 	char c;
174 	boolean_t verbose = B_FALSE;
175 	boolean_t first = B_TRUE;
176 	/*
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.
180 	 */
181 	boolean_t dump = B_FALSE;
182 	int err;
183 	zio_cksum_t zc = { 0 };
184 	zio_cksum_t pcksum = { 0 };
185 
186 	while ((c = getopt(argc, argv, ":vCd")) != -1) {
187 		switch (c) {
188 		case 'C':
189 			do_cksum = B_FALSE;
190 			break;
191 		case 'v':
192 			verbose = B_TRUE;
193 			break;
194 		case 'd':
195 			dump = B_TRUE;
196 			verbose = B_TRUE;
197 			break;
198 		case ':':
199 			(void) fprintf(stderr,
200 			    "missing argument for '%c' option\n", optopt);
201 			usage();
202 			break;
203 		case '?':
204 			(void) fprintf(stderr, "invalid option '%c'\n",
205 			    optopt);
206 			usage();
207 		}
208 	}
209 
210 	if (isatty(STDIN_FILENO)) {
211 		(void) fprintf(stderr,
212 		    "Error: Backup stream can not be read "
213 		    "from a terminal.\n"
214 		    "You must redirect standard input.\n");
215 		exit(1);
216 	}
217 
218 	send_stream = stdin;
219 	pcksum = zc;
220 	while (ssread(drr, sizeof (dmu_replay_record_t), &zc)) {
221 
222 		/*
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.
225 		 */
226 		if (first) {
227 			if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
228 				do_byteswap = B_TRUE;
229 				if (do_cksum) {
230 					ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
231 					/*
232 					 * recalculate header checksum now
233 					 * that we know it needs to be
234 					 * byteswapped.
235 					 */
236 					fletcher_4_incremental_byteswap(drr,
237 					    sizeof (dmu_replay_record_t), &zc);
238 				}
239 			} else if (drrb->drr_magic != DMU_BACKUP_MAGIC) {
240 				(void) fprintf(stderr, "Invalid stream "
241 				    "(bad magic number)\n");
242 				exit(1);
243 			}
244 			first = B_FALSE;
245 		}
246 		if (do_byteswap) {
247 			drr->drr_type = BSWAP_32(drr->drr_type);
248 			drr->drr_payloadlen =
249 			    BSWAP_32(drr->drr_payloadlen);
250 		}
251 
252 		/*
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
257 		 * original state.
258 		 */
259 		if (drr->drr_type >= DRR_NUMTYPES) {
260 			(void) printf("INVALID record found: type 0x%x\n",
261 			    drr->drr_type);
262 			(void) printf("Aborting.\n");
263 			exit(1);
264 		}
265 
266 		drr_record_count[drr->drr_type]++;
267 
268 		switch (drr->drr_type) {
269 		case DRR_BEGIN:
270 			if (do_byteswap) {
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);
279 				drrb->drr_fromguid =
280 				    BSWAP_64(drrb->drr_fromguid);
281 			}
282 
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);
299 			if (verbose)
300 				(void) printf("\n");
301 
302 			if ((DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
303 			    DMU_COMPOUNDSTREAM) && drr->drr_payloadlen != 0) {
304 				nvlist_t *nv;
305 				int sz = drr->drr_payloadlen;
306 
307 				if (sz > INITIAL_BUFLEN) {
308 					free(buf);
309 					buf = malloc(sz);
310 				}
311 				(void) ssread(buf, sz, &zc);
312 				if (ferror(send_stream))
313 					perror("fread");
314 				err = nvlist_unpack(buf, sz, &nv, 0);
315 				if (err)
316 					perror(strerror(err));
317 				nvlist_print(stdout, nv);
318 				nvlist_free(nv);
319 			}
320 			break;
321 
322 		case DRR_END:
323 			if (do_byteswap) {
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]);
332 			}
333 			/*
334 			 * We compare against the *previous* checksum
335 			 * value, because the stored checksum is of
336 			 * everything before the DRR_END record.
337 			 */
338 			if (do_cksum && !ZIO_CHECKSUM_EQUAL(drre->drr_checksum,
339 			    pcksum)) {
340 				(void) printf("Expected checksum differs from "
341 				    "checksum in stream.\n");
342 				(void) printf("Expected checksum = "
343 				    "%llx/%llx/%llx/%llx\n",
344 				    pcksum.zc_word[0],
345 				    pcksum.zc_word[1],
346 				    pcksum.zc_word[2],
347 				    pcksum.zc_word[3]);
348 			}
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]);
354 
355 			ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
356 			break;
357 
358 		case DRR_OBJECT:
359 			if (do_byteswap) {
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);
365 				drro->drr_bonuslen =
366 				    BSWAP_32(drro->drr_bonuslen);
367 				drro->drr_toguid = BSWAP_64(drro->drr_toguid);
368 			}
369 			if (verbose) {
370 				(void) printf("OBJECT object = %llu type = %u "
371 				    "bonustype = %u blksz = %u bonuslen = %u\n",
372 				    (u_longlong_t)drro->drr_object,
373 				    drro->drr_type,
374 				    drro->drr_bonustype,
375 				    drro->drr_blksz,
376 				    drro->drr_bonuslen);
377 			}
378 			if (drro->drr_bonuslen > 0) {
379 				(void) ssread(buf, P2ROUNDUP(drro->drr_bonuslen,
380 				    8), &zc);
381 				if (dump) {
382 					print_block(buf,
383 					    P2ROUNDUP(drro->drr_bonuslen, 8));
384 				}
385 			}
386 			break;
387 
388 		case DRR_FREEOBJECTS:
389 			if (do_byteswap) {
390 				drrfo->drr_firstobj =
391 				    BSWAP_64(drrfo->drr_firstobj);
392 				drrfo->drr_numobjs =
393 				    BSWAP_64(drrfo->drr_numobjs);
394 				drrfo->drr_toguid = BSWAP_64(drrfo->drr_toguid);
395 			}
396 			if (verbose) {
397 				(void) printf("FREEOBJECTS firstobj = %llu "
398 				    "numobjs = %llu\n",
399 				    (u_longlong_t)drrfo->drr_firstobj,
400 				    (u_longlong_t)drrfo->drr_numobjs);
401 			}
402 			break;
403 
404 		case DRR_WRITE:
405 			if (do_byteswap) {
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);
413 			}
414 			/*
415 			 * If this is verbose and/or dump output,
416 			 * print info on the modified block
417 			 */
418 			if (verbose) {
419 				(void) printf("WRITE object = %llu type = %u "
420 				    "checksum type = %u\n"
421 				    "offset = %llu length = %llu "
422 				    "props = %llx\n",
423 				    (u_longlong_t)drrw->drr_object,
424 				    drrw->drr_type,
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);
429 			}
430 			/*
431 			 * Read the contents of the block in from STDIN to buf
432 			 */
433 			(void) ssread(buf, drrw->drr_length, &zc);
434 			/*
435 			 * If in dump mode
436 			 */
437 			if (dump) {
438 				print_block(buf, drrw->drr_length);
439 			}
440 			total_write_size += drrw->drr_length;
441 			break;
442 
443 		case DRR_WRITE_BYREF:
444 			if (do_byteswap) {
445 				drrwbr->drr_object =
446 				    BSWAP_64(drrwbr->drr_object);
447 				drrwbr->drr_offset =
448 				    BSWAP_64(drrwbr->drr_offset);
449 				drrwbr->drr_length =
450 				    BSWAP_64(drrwbr->drr_length);
451 				drrwbr->drr_toguid =
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);
461 			}
462 			if (verbose) {
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);
477 			}
478 			break;
479 
480 		case DRR_FREE:
481 			if (do_byteswap) {
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);
485 			}
486 			if (verbose) {
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);
492 			}
493 			break;
494 		case DRR_SPILL:
495 			if (do_byteswap) {
496 				drrs->drr_object = BSWAP_64(drrs->drr_object);
497 				drrs->drr_length = BSWAP_64(drrs->drr_length);
498 			}
499 			if (verbose) {
500 				(void) printf("SPILL block for object = %llu "
501 				    "length = %llu\n", drrs->drr_object,
502 				    drrs->drr_length);
503 			}
504 			(void) ssread(buf, drrs->drr_length, &zc);
505 			if (dump) {
506 				print_block(buf, drrs->drr_length);
507 			}
508 			break;
509 		}
510 		pcksum = zc;
511 	}
512 	free(buf);
513 
514 	/* Print final summary */
515 
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);
543 	return (0);
544 }
545