xref: /titanic_50/usr/src/cmd/zstreamdump/zstreamdump.c (revision 3228339cea1b23699cb9832992ee764dec04b2f1)
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 #include <libnvpair.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <strings.h>
31 #include <unistd.h>
32 
33 #include <sys/dmu.h>
34 #include <sys/zfs_ioctl.h>
35 #include <zfs_fletcher.h>
36 
37 uint64_t drr_record_count[DRR_NUMTYPES];
38 uint64_t total_write_size = 0;
39 uint64_t total_stream_len = 0;
40 FILE *send_stream = 0;
41 boolean_t do_byteswap = B_FALSE;
42 boolean_t do_cksum = B_TRUE;
43 #define	INITIAL_BUFLEN (1<<20)
44 
45 static void
46 usage(void)
47 {
48 	(void) fprintf(stderr, "usage: zstreamdump [-v] [-C] < file\n");
49 	(void) fprintf(stderr, "\t -v -- verbose\n");
50 	(void) fprintf(stderr, "\t -C -- suppress checksum verification\n");
51 	exit(1);
52 }
53 
54 /*
55  * ssread - send stream read.
56  *
57  * Read while computing incremental checksum
58  */
59 
60 static size_t
61 ssread(void *buf, size_t len, zio_cksum_t *cksum)
62 {
63 	size_t outlen;
64 
65 	if ((outlen = fread(buf, len, 1, send_stream)) == 0)
66 		return (0);
67 
68 	if (do_cksum && cksum) {
69 		if (do_byteswap)
70 			fletcher_4_incremental_byteswap(buf, len, cksum);
71 		else
72 			fletcher_4_incremental_native(buf, len, cksum);
73 	}
74 	total_stream_len += len;
75 	return (outlen);
76 }
77 
78 int
79 main(int argc, char *argv[])
80 {
81 	char *buf = malloc(INITIAL_BUFLEN);
82 	dmu_replay_record_t thedrr;
83 	dmu_replay_record_t *drr = &thedrr;
84 	struct drr_begin *drrb = &thedrr.drr_u.drr_begin;
85 	struct drr_end *drre = &thedrr.drr_u.drr_end;
86 	struct drr_object *drro = &thedrr.drr_u.drr_object;
87 	struct drr_freeobjects *drrfo = &thedrr.drr_u.drr_freeobjects;
88 	struct drr_write *drrw = &thedrr.drr_u.drr_write;
89 	struct drr_write_byref *drrwbr = &thedrr.drr_u.drr_write_byref;
90 	struct drr_free *drrf = &thedrr.drr_u.drr_free;
91 	struct drr_spill *drrs = &thedrr.drr_u.drr_spill;
92 	char c;
93 	boolean_t verbose = B_FALSE;
94 	boolean_t first = B_TRUE;
95 	int err;
96 	zio_cksum_t zc = { 0 };
97 	zio_cksum_t pcksum = { 0 };
98 
99 	while ((c = getopt(argc, argv, ":vC")) != -1) {
100 		switch (c) {
101 		case 'C':
102 			do_cksum = B_FALSE;
103 			break;
104 		case 'v':
105 			verbose = B_TRUE;
106 			break;
107 		case ':':
108 			(void) fprintf(stderr,
109 			    "missing argument for '%c' option\n", optopt);
110 			usage();
111 			break;
112 		case '?':
113 			(void) fprintf(stderr, "invalid option '%c'\n",
114 			    optopt);
115 			usage();
116 		}
117 	}
118 
119 	if (isatty(STDIN_FILENO)) {
120 		(void) fprintf(stderr,
121 		    "Error: Backup stream can not be read "
122 		    "from a terminal.\n"
123 		    "You must redirect standard input.\n");
124 		exit(1);
125 	}
126 
127 	send_stream = stdin;
128 	pcksum = zc;
129 	while (ssread(drr, sizeof (dmu_replay_record_t), &zc)) {
130 
131 		if (first) {
132 			if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
133 				do_byteswap = B_TRUE;
134 				if (do_cksum) {
135 					ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
136 					/*
137 					 * recalculate header checksum now
138 					 * that we know it needs to be
139 					 * byteswapped.
140 					 */
141 					fletcher_4_incremental_byteswap(drr,
142 					    sizeof (dmu_replay_record_t), &zc);
143 				}
144 			} else if (drrb->drr_magic != DMU_BACKUP_MAGIC) {
145 				(void) fprintf(stderr, "Invalid stream "
146 				    "(bad magic number)\n");
147 				exit(1);
148 			}
149 			first = B_FALSE;
150 		}
151 		if (do_byteswap) {
152 			drr->drr_type = BSWAP_32(drr->drr_type);
153 			drr->drr_payloadlen =
154 			    BSWAP_32(drr->drr_payloadlen);
155 		}
156 
157 		/*
158 		 * At this point, the leading fields of the replay record
159 		 * (drr_type and drr_payloadlen) have been byte-swapped if
160 		 * necessary, but the rest of the data structure (the
161 		 * union of type-specific structures) is still in its
162 		 * original state.
163 		 */
164 		if (drr->drr_type >= DRR_NUMTYPES) {
165 			(void) printf("INVALID record found: type 0x%x\n",
166 			    drr->drr_type);
167 			(void) printf("Aborting.\n");
168 			exit(1);
169 		}
170 
171 		drr_record_count[drr->drr_type]++;
172 
173 		switch (drr->drr_type) {
174 		case DRR_BEGIN:
175 			if (do_byteswap) {
176 				drrb->drr_magic = BSWAP_64(drrb->drr_magic);
177 				drrb->drr_versioninfo =
178 				    BSWAP_64(drrb->drr_versioninfo);
179 				drrb->drr_creation_time =
180 				    BSWAP_64(drrb->drr_creation_time);
181 				drrb->drr_type = BSWAP_32(drrb->drr_type);
182 				drrb->drr_flags = BSWAP_32(drrb->drr_flags);
183 				drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
184 				drrb->drr_fromguid =
185 				    BSWAP_64(drrb->drr_fromguid);
186 			}
187 
188 			(void) printf("BEGIN record\n");
189 			(void) printf("\thdrtype = %lld\n",
190 			    DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo));
191 			(void) printf("\tfeatures = %llx\n",
192 			    DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo));
193 			(void) printf("\tmagic = %llx\n",
194 			    (u_longlong_t)drrb->drr_magic);
195 			(void) printf("\tcreation_time = %llx\n",
196 			    (u_longlong_t)drrb->drr_creation_time);
197 			(void) printf("\ttype = %u\n", drrb->drr_type);
198 			(void) printf("\tflags = 0x%x\n", drrb->drr_flags);
199 			(void) printf("\ttoguid = %llx\n",
200 			    (u_longlong_t)drrb->drr_toguid);
201 			(void) printf("\tfromguid = %llx\n",
202 			    (u_longlong_t)drrb->drr_fromguid);
203 			(void) printf("\ttoname = %s\n", drrb->drr_toname);
204 			if (verbose)
205 				(void) printf("\n");
206 
207 			if ((DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
208 			    DMU_COMPOUNDSTREAM) && drr->drr_payloadlen != 0) {
209 				nvlist_t *nv;
210 				int sz = drr->drr_payloadlen;
211 
212 				if (sz > 1<<20) {
213 					free(buf);
214 					buf = malloc(sz);
215 				}
216 				(void) ssread(buf, sz, &zc);
217 				if (ferror(send_stream))
218 					perror("fread");
219 				err = nvlist_unpack(buf, sz, &nv, 0);
220 				if (err)
221 					perror(strerror(err));
222 				nvlist_print(stdout, nv);
223 				nvlist_free(nv);
224 			}
225 			break;
226 
227 		case DRR_END:
228 			if (do_byteswap) {
229 				drre->drr_checksum.zc_word[0] =
230 				    BSWAP_64(drre->drr_checksum.zc_word[0]);
231 				drre->drr_checksum.zc_word[1] =
232 				    BSWAP_64(drre->drr_checksum.zc_word[1]);
233 				drre->drr_checksum.zc_word[2] =
234 				    BSWAP_64(drre->drr_checksum.zc_word[2]);
235 				drre->drr_checksum.zc_word[3] =
236 				    BSWAP_64(drre->drr_checksum.zc_word[3]);
237 			}
238 			/*
239 			 * We compare against the *previous* checksum
240 			 * value, because the stored checksum is of
241 			 * everything before the DRR_END record.
242 			 */
243 			if (do_cksum && !ZIO_CHECKSUM_EQUAL(drre->drr_checksum,
244 			    pcksum)) {
245 				(void) printf("Expected checksum differs from "
246 				    "checksum in stream.\n");
247 				(void) printf("Expected checksum = "
248 				    "%llx/%llx/%llx/%llx\n",
249 				    pcksum.zc_word[0],
250 				    pcksum.zc_word[1],
251 				    pcksum.zc_word[2],
252 				    pcksum.zc_word[3]);
253 			}
254 			(void) printf("END checksum = %llx/%llx/%llx/%llx\n",
255 			    drre->drr_checksum.zc_word[0],
256 			    drre->drr_checksum.zc_word[1],
257 			    drre->drr_checksum.zc_word[2],
258 			    drre->drr_checksum.zc_word[3]);
259 
260 			ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
261 			break;
262 
263 		case DRR_OBJECT:
264 			if (do_byteswap) {
265 				drro->drr_object = BSWAP_64(drro->drr_object);
266 				drro->drr_type = BSWAP_32(drro->drr_type);
267 				drro->drr_bonustype =
268 				    BSWAP_32(drro->drr_bonustype);
269 				drro->drr_blksz = BSWAP_32(drro->drr_blksz);
270 				drro->drr_bonuslen =
271 				    BSWAP_32(drro->drr_bonuslen);
272 				drro->drr_toguid = BSWAP_64(drro->drr_toguid);
273 			}
274 			if (verbose) {
275 				(void) printf("OBJECT object = %llu type = %u "
276 				    "bonustype = %u blksz = %u bonuslen = %u\n",
277 				    (u_longlong_t)drro->drr_object,
278 				    drro->drr_type,
279 				    drro->drr_bonustype,
280 				    drro->drr_blksz,
281 				    drro->drr_bonuslen);
282 			}
283 			if (drro->drr_bonuslen > 0) {
284 				(void) ssread(buf, P2ROUNDUP(drro->drr_bonuslen,
285 				    8), &zc);
286 			}
287 			break;
288 
289 		case DRR_FREEOBJECTS:
290 			if (do_byteswap) {
291 				drrfo->drr_firstobj =
292 				    BSWAP_64(drrfo->drr_firstobj);
293 				drrfo->drr_numobjs =
294 				    BSWAP_64(drrfo->drr_numobjs);
295 				drrfo->drr_toguid = BSWAP_64(drrfo->drr_toguid);
296 			}
297 			if (verbose) {
298 				(void) printf("FREEOBJECTS firstobj = %llu "
299 				    "numobjs = %llu\n",
300 				    (u_longlong_t)drrfo->drr_firstobj,
301 				    (u_longlong_t)drrfo->drr_numobjs);
302 			}
303 			break;
304 
305 		case DRR_WRITE:
306 			if (do_byteswap) {
307 				drrw->drr_object = BSWAP_64(drrw->drr_object);
308 				drrw->drr_type = BSWAP_32(drrw->drr_type);
309 				drrw->drr_offset = BSWAP_64(drrw->drr_offset);
310 				drrw->drr_length = BSWAP_64(drrw->drr_length);
311 				drrw->drr_toguid = BSWAP_64(drrw->drr_toguid);
312 				drrw->drr_key.ddk_prop =
313 				    BSWAP_64(drrw->drr_key.ddk_prop);
314 			}
315 			if (verbose) {
316 				(void) printf("WRITE object = %llu type = %u "
317 				    "checksum type = %u\n"
318 				    "offset = %llu length = %llu "
319 				    "props = %llx\n",
320 				    (u_longlong_t)drrw->drr_object,
321 				    drrw->drr_type,
322 				    drrw->drr_checksumtype,
323 				    (u_longlong_t)drrw->drr_offset,
324 				    (u_longlong_t)drrw->drr_length,
325 				    (u_longlong_t)drrw->drr_key.ddk_prop);
326 			}
327 			(void) ssread(buf, drrw->drr_length, &zc);
328 			total_write_size += drrw->drr_length;
329 			break;
330 
331 		case DRR_WRITE_BYREF:
332 			if (do_byteswap) {
333 				drrwbr->drr_object =
334 				    BSWAP_64(drrwbr->drr_object);
335 				drrwbr->drr_offset =
336 				    BSWAP_64(drrwbr->drr_offset);
337 				drrwbr->drr_length =
338 				    BSWAP_64(drrwbr->drr_length);
339 				drrwbr->drr_toguid =
340 				    BSWAP_64(drrwbr->drr_toguid);
341 				drrwbr->drr_refguid =
342 				    BSWAP_64(drrwbr->drr_refguid);
343 				drrwbr->drr_refobject =
344 				    BSWAP_64(drrwbr->drr_refobject);
345 				drrwbr->drr_refoffset =
346 				    BSWAP_64(drrwbr->drr_refoffset);
347 				drrwbr->drr_key.ddk_prop =
348 				    BSWAP_64(drrwbr->drr_key.ddk_prop);
349 			}
350 			if (verbose) {
351 				(void) printf("WRITE_BYREF object = %llu "
352 				    "checksum type = %u props = %llx\n"
353 				    "offset = %llu length = %llu\n"
354 				    "toguid = %llx refguid = %llx\n"
355 				    "refobject = %llu refoffset = %llu\n",
356 				    (u_longlong_t)drrwbr->drr_object,
357 				    drrwbr->drr_checksumtype,
358 				    (u_longlong_t)drrwbr->drr_key.ddk_prop,
359 				    (u_longlong_t)drrwbr->drr_offset,
360 				    (u_longlong_t)drrwbr->drr_length,
361 				    (u_longlong_t)drrwbr->drr_toguid,
362 				    (u_longlong_t)drrwbr->drr_refguid,
363 				    (u_longlong_t)drrwbr->drr_refobject,
364 				    (u_longlong_t)drrwbr->drr_refoffset);
365 			}
366 			break;
367 
368 		case DRR_FREE:
369 			if (do_byteswap) {
370 				drrf->drr_object = BSWAP_64(drrf->drr_object);
371 				drrf->drr_offset = BSWAP_64(drrf->drr_offset);
372 				drrf->drr_length = BSWAP_64(drrf->drr_length);
373 			}
374 			if (verbose) {
375 				(void) printf("FREE object = %llu "
376 				    "offset = %llu length = %lld\n",
377 				    (u_longlong_t)drrf->drr_object,
378 				    (u_longlong_t)drrf->drr_offset,
379 				    (longlong_t)drrf->drr_length);
380 			}
381 			break;
382 		case DRR_SPILL:
383 			if (do_byteswap) {
384 				drrs->drr_object = BSWAP_64(drrs->drr_object);
385 				drrs->drr_length = BSWAP_64(drrs->drr_length);
386 			}
387 			if (verbose) {
388 				(void) printf("SPILL block for object = %llu "
389 				    "length = %llu\n", drrs->drr_object,
390 				    drrs->drr_length);
391 			}
392 			(void) ssread(buf, drrs->drr_length, &zc);
393 			break;
394 		}
395 		pcksum = zc;
396 	}
397 	free(buf);
398 
399 	/* Print final summary */
400 
401 	(void) printf("SUMMARY:\n");
402 	(void) printf("\tTotal DRR_BEGIN records = %lld\n",
403 	    (u_longlong_t)drr_record_count[DRR_BEGIN]);
404 	(void) printf("\tTotal DRR_END records = %lld\n",
405 	    (u_longlong_t)drr_record_count[DRR_END]);
406 	(void) printf("\tTotal DRR_OBJECT records = %lld\n",
407 	    (u_longlong_t)drr_record_count[DRR_OBJECT]);
408 	(void) printf("\tTotal DRR_FREEOBJECTS records = %lld\n",
409 	    (u_longlong_t)drr_record_count[DRR_FREEOBJECTS]);
410 	(void) printf("\tTotal DRR_WRITE records = %lld\n",
411 	    (u_longlong_t)drr_record_count[DRR_WRITE]);
412 	(void) printf("\tTotal DRR_FREE records = %lld\n",
413 	    (u_longlong_t)drr_record_count[DRR_FREE]);
414 	(void) printf("\tTotal DRR_SPILL records = %lld\n",
415 	    (u_longlong_t)drr_record_count[DRR_SPILL]);
416 	(void) printf("\tTotal records = %lld\n",
417 	    (u_longlong_t)(drr_record_count[DRR_BEGIN] +
418 	    drr_record_count[DRR_OBJECT] +
419 	    drr_record_count[DRR_FREEOBJECTS] +
420 	    drr_record_count[DRR_WRITE] +
421 	    drr_record_count[DRR_FREE] +
422 	    drr_record_count[DRR_SPILL] +
423 	    drr_record_count[DRR_END]));
424 	(void) printf("\tTotal write size = %lld (0x%llx)\n",
425 	    (u_longlong_t)total_write_size, (u_longlong_t)total_write_size);
426 	(void) printf("\tTotal stream length = %lld (0x%llx)\n",
427 	    (u_longlong_t)total_stream_len, (u_longlong_t)total_stream_len);
428 	return (0);
429 }
430