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.
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.
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]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <libnvpair.h>
34 #include <sys/zfs_ioctl.h>
35 #include <zfs_fletcher.h>
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)
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");
55 * ssread - send stream read.
57 * Read while computing incremental checksum
61 ssread(void *buf, size_t len, zio_cksum_t *cksum)
65 if ((outlen = fread(buf, len, 1, send_stream)) == 0)
68 if (do_cksum && cksum) {
70 fletcher_4_incremental_byteswap(buf, len, cksum);
72 fletcher_4_incremental_native(buf, len, cksum);
74 total_stream_len += len;
79 main(int argc, char *argv[])
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;
93 boolean_t verbose = B_FALSE;
94 boolean_t first = B_TRUE;
96 zio_cksum_t zc = { 0 };
97 zio_cksum_t pcksum = { 0 };
99 while ((c = getopt(argc, argv, ":vC")) != -1) {
108 (void) fprintf(stderr,
109 "missing argument for '%c' option\n", optopt);
113 (void) fprintf(stderr, "invalid option '%c'\n",
119 if (isatty(STDIN_FILENO)) {
120 (void) fprintf(stderr,
121 "Error: Backup stream can not be read "
123 "You must redirect standard input.\n");
129 while (ssread(drr, sizeof (dmu_replay_record_t), &zc)) {
132 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
133 do_byteswap = B_TRUE;
135 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
137 * recalculate header checksum now
138 * that we know it needs to be
141 fletcher_4_incremental_byteswap(drr,
142 sizeof (dmu_replay_record_t), &zc);
144 } else if (drrb->drr_magic != DMU_BACKUP_MAGIC) {
145 (void) fprintf(stderr, "Invalid stream "
146 "(bad magic number)\n");
152 drr->drr_type = BSWAP_32(drr->drr_type);
153 drr->drr_payloadlen =
154 BSWAP_32(drr->drr_payloadlen);
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
164 if (drr->drr_type >= DRR_NUMTYPES) {
165 (void) printf("INVALID record found: type 0x%x\n",
167 (void) printf("Aborting.\n");
171 drr_record_count[drr->drr_type]++;
173 switch (drr->drr_type) {
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);
185 BSWAP_64(drrb->drr_fromguid);
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);
207 if ((DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
208 DMU_COMPOUNDSTREAM) && drr->drr_payloadlen != 0) {
210 int sz = drr->drr_payloadlen;
216 (void) ssread(buf, sz, &zc);
217 if (ferror(send_stream))
219 err = nvlist_unpack(buf, sz, &nv, 0);
221 perror(strerror(err));
222 nvlist_print(stdout, nv);
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]);
239 * We compare against the *previous* checksum
240 * value, because the stored checksum is of
241 * everything before the DRR_END record.
243 if (do_cksum && !ZIO_CHECKSUM_EQUAL(drre->drr_checksum,
245 (void) printf("Expected checksum differs from "
246 "checksum in stream.\n");
247 (void) printf("Expected checksum = "
248 "%llx/%llx/%llx/%llx\n",
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]);
260 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
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);
271 BSWAP_32(drro->drr_bonuslen);
272 drro->drr_toguid = BSWAP_64(drro->drr_toguid);
275 (void) printf("OBJECT object = %llu type = %u "
276 "bonustype = %u blksz = %u bonuslen = %u\n",
277 (u_longlong_t)drro->drr_object,
283 if (drro->drr_bonuslen > 0) {
284 (void) ssread(buf, P2ROUNDUP(drro->drr_bonuslen,
289 case DRR_FREEOBJECTS:
291 drrfo->drr_firstobj =
292 BSWAP_64(drrfo->drr_firstobj);
294 BSWAP_64(drrfo->drr_numobjs);
295 drrfo->drr_toguid = BSWAP_64(drrfo->drr_toguid);
298 (void) printf("FREEOBJECTS firstobj = %llu "
300 (u_longlong_t)drrfo->drr_firstobj,
301 (u_longlong_t)drrfo->drr_numobjs);
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);
316 (void) printf("WRITE object = %llu type = %u "
317 "checksum type = %u\n"
318 "offset = %llu length = %llu "
320 (u_longlong_t)drrw->drr_object,
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);
327 (void) ssread(buf, drrw->drr_length, &zc);
328 total_write_size += drrw->drr_length;
331 case DRR_WRITE_BYREF:
334 BSWAP_64(drrwbr->drr_object);
336 BSWAP_64(drrwbr->drr_offset);
338 BSWAP_64(drrwbr->drr_length);
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);
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);
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);
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);
384 drrs->drr_object = BSWAP_64(drrs->drr_object);
385 drrs->drr_length = BSWAP_64(drrs->drr_length);
388 (void) printf("SPILL block for object = %llu "
389 "length = %llu\n", drrs->drr_object,
392 (void) ssread(buf, drrs->drr_length, &zc);
399 /* Print final summary */
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);