xref: /titanic_50/usr/src/cmd/zstreamdump/zstreamdump.c (revision 6bbe05905a1c10a2703f95fb4912eb14b87f6670)
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 2009 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 	char c;
92 	boolean_t verbose = B_FALSE;
93 	boolean_t first = B_TRUE;
94 	int i, err;
95 	zio_cksum_t zc = { 0 };
96 	zio_cksum_t pcksum = { 0 };
97 
98 	while ((c = getopt(argc, argv, ":vC")) != -1) {
99 		switch (c) {
100 		case 'C':
101 			do_cksum = B_FALSE;
102 			break;
103 		case 'v':
104 			verbose = B_TRUE;
105 			break;
106 		case ':':
107 			(void) fprintf(stderr,
108 			    "missing argument for '%c' option\n", optopt);
109 			usage();
110 			break;
111 		case '?':
112 			(void) fprintf(stderr, "invalid option '%c'\n",
113 			    optopt);
114 			usage();
115 		}
116 	}
117 
118 	if (isatty(STDIN_FILENO)) {
119 		(void) fprintf(stderr,
120 		    "Error: Backup stream can not be read "
121 		    "from a terminal.\n"
122 		    "You must redirect standard input.\n");
123 		exit(1);
124 	}
125 
126 	send_stream = stdin;
127 	pcksum = zc;
128 	while (ssread(drr, sizeof (dmu_replay_record_t), &zc)) {
129 
130 		if (first) {
131 			if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
132 				do_byteswap = B_TRUE;
133 				if (do_cksum) {
134 					ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
135 					/*
136 					 * recalculate header checksum now
137 					 * that we know it needs to be
138 					 * byteswapped.
139 					 */
140 					fletcher_4_incremental_byteswap(drr,
141 					    sizeof (dmu_replay_record_t), &zc);
142 				}
143 			} else if (drrb->drr_magic != DMU_BACKUP_MAGIC) {
144 				(void) fprintf(stderr, "Invalid stream "
145 				    "(bad magic number)\n");
146 				exit(1);
147 			}
148 			first = B_FALSE;
149 		}
150 		if (do_byteswap) {
151 			drr->drr_type = BSWAP_32(drr->drr_type);
152 			drr->drr_payloadlen =
153 			    BSWAP_32(drr->drr_payloadlen);
154 		}
155 
156 		/*
157 		 * At this point, the leading fields of the replay record
158 		 * (drr_type and drr_payloadlen) have been byte-swapped if
159 		 * necessary, but the rest of the data structure (the
160 		 * union of type-specific structures) is still in its
161 		 * original state.
162 		 */
163 		if (drr->drr_type >= DRR_NUMTYPES) {
164 			(void) printf("INVALID record found: type 0x%x\n",
165 			    drr->drr_type);
166 			(void) printf("Aborting.\n");
167 			exit(1);
168 		}
169 
170 		drr_record_count[drr->drr_type]++;
171 
172 		switch (drr->drr_type) {
173 		case DRR_BEGIN:
174 			if (do_byteswap) {
175 				drrb->drr_magic = BSWAP_64(drrb->drr_magic);
176 				drrb->drr_versioninfo =
177 				    BSWAP_64(drrb->drr_versioninfo);
178 				drrb->drr_creation_time =
179 				    BSWAP_64(drrb->drr_creation_time);
180 				drrb->drr_type = BSWAP_32(drrb->drr_type);
181 				drrb->drr_flags = BSWAP_32(drrb->drr_flags);
182 				drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
183 				drrb->drr_fromguid =
184 				    BSWAP_64(drrb->drr_fromguid);
185 			}
186 
187 			(void) printf("BEGIN record\n");
188 			(void) printf("\thdrtype = %lld\n",
189 			    DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo));
190 			(void) printf("\tfeatures = %llx\n",
191 			    DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo));
192 			(void) printf("\tmagic = %llx\n",
193 			    (u_longlong_t)drrb->drr_magic);
194 			(void) printf("\tcreation_time = %llx\n",
195 			    (u_longlong_t)drrb->drr_creation_time);
196 			(void) printf("\ttype = %u\n", drrb->drr_type);
197 			(void) printf("\tflags = 0x%x\n", drrb->drr_flags);
198 			(void) printf("\ttoguid = %llx\n",
199 			    (u_longlong_t)drrb->drr_toguid);
200 			(void) printf("\tfromguid = %llx\n",
201 			    (u_longlong_t)drrb->drr_fromguid);
202 			(void) printf("\ttoname = %s\n", drrb->drr_toname);
203 			if (verbose)
204 				(void) printf("\n");
205 
206 			if ((DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
207 			    DMU_COMPOUNDSTREAM) && drr->drr_payloadlen != 0) {
208 				nvlist_t *nv;
209 				int sz = drr->drr_payloadlen;
210 
211 				if (sz > 1<<20) {
212 					free(buf);
213 					buf = malloc(sz);
214 				}
215 				(void) ssread(buf, sz, &zc);
216 				if (ferror(send_stream))
217 					perror("fread");
218 				err = nvlist_unpack(buf, sz, &nv, 0);
219 				if (err)
220 					perror(strerror(err));
221 				nvlist_print(stdout, nv);
222 				nvlist_free(nv);
223 			}
224 			break;
225 
226 		case DRR_END:
227 			if (do_byteswap) {
228 				drre->drr_checksum.zc_word[0] =
229 				    BSWAP_64(drre->drr_checksum.zc_word[0]);
230 				drre->drr_checksum.zc_word[1] =
231 				    BSWAP_64(drre->drr_checksum.zc_word[1]);
232 				drre->drr_checksum.zc_word[2] =
233 				    BSWAP_64(drre->drr_checksum.zc_word[2]);
234 				drre->drr_checksum.zc_word[3] =
235 				    BSWAP_64(drre->drr_checksum.zc_word[3]);
236 			}
237 			/*
238 			 * We compare against the *previous* checksum
239 			 * value, because the stored checksum is of
240 			 * everything before the DRR_END record.
241 			 */
242 			if (do_cksum && !ZIO_CHECKSUM_EQUAL(drre->drr_checksum,
243 			    pcksum)) {
244 				(void) printf("Expected checksum differs from "
245 				    "checksum in stream.\n");
246 				(void) printf("Expected checksum = "
247 				    "%llx/%llx/%llx/%llx\n",
248 				    pcksum.zc_word[0],
249 				    pcksum.zc_word[1],
250 				    pcksum.zc_word[2],
251 				    pcksum.zc_word[3]);
252 			}
253 			(void) printf("END checksum = %llx/%llx/%llx/%llx\n",
254 			    drre->drr_checksum.zc_word[0],
255 			    drre->drr_checksum.zc_word[1],
256 			    drre->drr_checksum.zc_word[2],
257 			    drre->drr_checksum.zc_word[3]);
258 
259 			ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
260 			break;
261 
262 		case DRR_OBJECT:
263 			if (do_byteswap) {
264 				drro->drr_object = BSWAP_64(drro->drr_object);
265 				drro->drr_type = BSWAP_32(drro->drr_type);
266 				drro->drr_bonustype =
267 				    BSWAP_32(drro->drr_bonustype);
268 				drro->drr_blksz = BSWAP_32(drro->drr_blksz);
269 				drro->drr_bonuslen =
270 				    BSWAP_32(drro->drr_bonuslen);
271 				drro->drr_toguid = BSWAP_64(drro->drr_toguid);
272 			}
273 			if (verbose) {
274 				(void) printf("OBJECT object = %llu type = %u "
275 				    "bonustype = %u blksz = %u bonuslen = %u\n",
276 				    (u_longlong_t)drro->drr_object,
277 				    drro->drr_type,
278 				    drro->drr_bonustype,
279 				    drro->drr_blksz,
280 				    drro->drr_bonuslen);
281 			}
282 			if (drro->drr_bonuslen > 0) {
283 				(void) ssread(buf, P2ROUNDUP(drro->drr_bonuslen,
284 				    8), &zc);
285 			}
286 			break;
287 
288 		case DRR_FREEOBJECTS:
289 			if (do_byteswap) {
290 				drrfo->drr_firstobj =
291 				    BSWAP_64(drrfo->drr_firstobj);
292 				drrfo->drr_numobjs =
293 				    BSWAP_64(drrfo->drr_numobjs);
294 				drrfo->drr_toguid = BSWAP_64(drrfo->drr_toguid);
295 			}
296 			if (verbose) {
297 				(void) printf("FREEOBJECTS firstobj = %llu "
298 				    "numobjs = %llu\n",
299 				    (u_longlong_t)drrfo->drr_firstobj,
300 				    (u_longlong_t)drrfo->drr_numobjs);
301 			}
302 			break;
303 
304 		case DRR_WRITE:
305 			if (do_byteswap) {
306 				drrw->drr_object = BSWAP_64(drrw->drr_object);
307 				drrw->drr_type = BSWAP_32(drrw->drr_type);
308 				drrw->drr_offset = BSWAP_64(drrw->drr_offset);
309 				drrw->drr_length = BSWAP_64(drrw->drr_length);
310 				drrw->drr_toguid = BSWAP_64(drrw->drr_toguid);
311 			}
312 			if (verbose) {
313 				(void) printf("WRITE object = %llu type = %u "
314 				    "offset = %llu length = %llu\n",
315 				    (u_longlong_t)drrw->drr_object,
316 				    drrw->drr_type,
317 				    (u_longlong_t)drrw->drr_offset,
318 				    (u_longlong_t)drrw->drr_length);
319 			}
320 			(void) ssread(buf, drrw->drr_length, &zc);
321 			total_write_size += drrw->drr_length;
322 			break;
323 
324 		case DRR_WRITE_BYREF:
325 			if (do_byteswap) {
326 				drrwbr->drr_object =
327 				    BSWAP_64(drrwbr->drr_object);
328 				drrwbr->drr_offset =
329 				    BSWAP_64(drrwbr->drr_offset);
330 				drrwbr->drr_length =
331 				    BSWAP_64(drrwbr->drr_length);
332 				drrwbr->drr_toguid =
333 				    BSWAP_64(drrwbr->drr_toguid);
334 				drrwbr->drr_refguid =
335 				    BSWAP_64(drrwbr->drr_refguid);
336 				drrwbr->drr_refobject =
337 				    BSWAP_64(drrwbr->drr_refobject);
338 				drrwbr->drr_refoffset =
339 				    BSWAP_64(drrwbr->drr_refoffset);
340 			}
341 			if (verbose) {
342 				(void) printf("WRITE_BYREF object = %llu "
343 				    "offset = %llu length = %llu\n"
344 				    "toguid = %llx refguid = %llx\n"
345 				    "refobject = %llu refoffset = %llu\n",
346 				    (u_longlong_t)drrwbr->drr_object,
347 				    (u_longlong_t)drrwbr->drr_offset,
348 				    (u_longlong_t)drrwbr->drr_length,
349 				    (u_longlong_t)drrwbr->drr_toguid,
350 				    (u_longlong_t)drrwbr->drr_refguid,
351 				    (u_longlong_t)drrwbr->drr_refobject,
352 				    (u_longlong_t)drrwbr->drr_refoffset);
353 			}
354 			break;
355 
356 		case DRR_FREE:
357 			if (do_byteswap) {
358 				drrf->drr_object = BSWAP_64(drrf->drr_object);
359 				drrf->drr_offset = BSWAP_64(drrf->drr_offset);
360 				drrf->drr_length = BSWAP_64(drrf->drr_length);
361 			}
362 			if (verbose) {
363 				(void) printf("FREE object = %llu "
364 				    "offset = %llu length = %lld\n",
365 				    (u_longlong_t)drrf->drr_object,
366 				    (u_longlong_t)drrf->drr_offset,
367 				    (longlong_t)drrf->drr_length);
368 			}
369 			break;
370 		}
371 		pcksum = zc;
372 	}
373 	free(buf);
374 
375 	/* Print final summary */
376 
377 	(void) printf("SUMMARY:\n");
378 	(void) printf("\tTotal DRR_BEGIN records = %lld\n",
379 	    (u_longlong_t)drr_record_count[DRR_BEGIN]);
380 	(void) printf("\tTotal DRR_END records = %lld\n",
381 	    (u_longlong_t)drr_record_count[DRR_END]);
382 	(void) printf("\tTotal DRR_OBJECT records = %lld\n",
383 	    (u_longlong_t)drr_record_count[DRR_OBJECT]);
384 	(void) printf("\tTotal DRR_FREEOBJECTS records = %lld\n",
385 	    (u_longlong_t)drr_record_count[DRR_FREEOBJECTS]);
386 	(void) printf("\tTotal DRR_WRITE records = %lld\n",
387 	    (u_longlong_t)drr_record_count[DRR_WRITE]);
388 	(void) printf("\tTotal DRR_FREE records = %lld\n",
389 	    (u_longlong_t)drr_record_count[DRR_FREE]);
390 	(void) printf("\tTotal records = %lld\n",
391 	    (u_longlong_t)(drr_record_count[DRR_BEGIN] +
392 	    drr_record_count[DRR_OBJECT] +
393 	    drr_record_count[DRR_FREEOBJECTS] +
394 	    drr_record_count[DRR_WRITE] +
395 	    drr_record_count[DRR_FREE] +
396 	    drr_record_count[DRR_END]));
397 	(void) printf("\tTotal write size = %lld (0x%llx)\n",
398 	    (u_longlong_t)total_write_size, (u_longlong_t)total_write_size);
399 	(void) printf("\tTotal stream length = %lld (0x%llx)\n",
400 	    (u_longlong_t)total_stream_len, (u_longlong_t)total_stream_len);
401 	return (0);
402 }
403