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.
26 * Portions Copyright 2012 Martin Matuska <martin@matuska.org>
29 #include <libnvpair.h>
36 #include <sys/zfs_ioctl.h>
37 #include <zfs_fletcher.h>
39 uint64_t drr_record_count[DRR_NUMTYPES];
40 uint64_t total_write_size = 0;
41 uint64_t total_stream_len = 0;
42 FILE *send_stream = 0;
43 boolean_t do_byteswap = B_FALSE;
44 boolean_t do_cksum = B_TRUE;
45 #define INITIAL_BUFLEN (1<<20)
50 (void) fprintf(stderr, "usage: zstreamdump [-v] [-C] < file\n");
51 (void) fprintf(stderr, "\t -v -- verbose\n");
52 (void) fprintf(stderr, "\t -C -- suppress checksum verification\n");
57 * ssread - send stream read.
59 * Read while computing incremental checksum
63 ssread(void *buf, size_t len, zio_cksum_t *cksum)
67 if ((outlen = fread(buf, len, 1, send_stream)) == 0)
70 if (do_cksum && cksum) {
72 fletcher_4_incremental_byteswap(buf, len, cksum);
74 fletcher_4_incremental_native(buf, len, cksum);
76 total_stream_len += len;
81 main(int argc, char *argv[])
83 char *buf = malloc(INITIAL_BUFLEN);
84 dmu_replay_record_t thedrr;
85 dmu_replay_record_t *drr = &thedrr;
86 struct drr_begin *drrb = &thedrr.drr_u.drr_begin;
87 struct drr_end *drre = &thedrr.drr_u.drr_end;
88 struct drr_object *drro = &thedrr.drr_u.drr_object;
89 struct drr_freeobjects *drrfo = &thedrr.drr_u.drr_freeobjects;
90 struct drr_write *drrw = &thedrr.drr_u.drr_write;
91 struct drr_write_byref *drrwbr = &thedrr.drr_u.drr_write_byref;
92 struct drr_free *drrf = &thedrr.drr_u.drr_free;
93 struct drr_spill *drrs = &thedrr.drr_u.drr_spill;
95 boolean_t verbose = B_FALSE;
96 boolean_t first = B_TRUE;
98 zio_cksum_t zc = { { 0 } };
99 zio_cksum_t pcksum = { { 0 } };
101 while ((c = getopt(argc, argv, ":vC")) != -1) {
110 (void) fprintf(stderr,
111 "missing argument for '%c' option\n", optopt);
115 (void) fprintf(stderr, "invalid option '%c'\n",
121 if (isatty(STDIN_FILENO)) {
122 (void) fprintf(stderr,
123 "Error: Backup stream can not be read "
125 "You must redirect standard input.\n");
131 while (ssread(drr, sizeof (dmu_replay_record_t), &zc)) {
134 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
135 do_byteswap = B_TRUE;
137 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
139 * recalculate header checksum now
140 * that we know it needs to be
143 fletcher_4_incremental_byteswap(drr,
144 sizeof (dmu_replay_record_t), &zc);
146 } else if (drrb->drr_magic != DMU_BACKUP_MAGIC) {
147 (void) fprintf(stderr, "Invalid stream "
148 "(bad magic number)\n");
154 drr->drr_type = BSWAP_32(drr->drr_type);
155 drr->drr_payloadlen =
156 BSWAP_32(drr->drr_payloadlen);
160 * At this point, the leading fields of the replay record
161 * (drr_type and drr_payloadlen) have been byte-swapped if
162 * necessary, but the rest of the data structure (the
163 * union of type-specific structures) is still in its
166 if (drr->drr_type >= DRR_NUMTYPES) {
167 (void) printf("INVALID record found: type 0x%x\n",
169 (void) printf("Aborting.\n");
173 drr_record_count[drr->drr_type]++;
175 switch (drr->drr_type) {
178 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
179 drrb->drr_versioninfo =
180 BSWAP_64(drrb->drr_versioninfo);
181 drrb->drr_creation_time =
182 BSWAP_64(drrb->drr_creation_time);
183 drrb->drr_type = BSWAP_32(drrb->drr_type);
184 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
185 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
187 BSWAP_64(drrb->drr_fromguid);
190 (void) printf("BEGIN record\n");
191 (void) printf("\thdrtype = %lld\n",
192 DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo));
193 (void) printf("\tfeatures = %llx\n",
194 DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo));
195 (void) printf("\tmagic = %llx\n",
196 (u_longlong_t)drrb->drr_magic);
197 (void) printf("\tcreation_time = %llx\n",
198 (u_longlong_t)drrb->drr_creation_time);
199 (void) printf("\ttype = %u\n", drrb->drr_type);
200 (void) printf("\tflags = 0x%x\n", drrb->drr_flags);
201 (void) printf("\ttoguid = %llx\n",
202 (u_longlong_t)drrb->drr_toguid);
203 (void) printf("\tfromguid = %llx\n",
204 (u_longlong_t)drrb->drr_fromguid);
205 (void) printf("\ttoname = %s\n", drrb->drr_toname);
209 if ((DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
210 DMU_COMPOUNDSTREAM) && drr->drr_payloadlen != 0) {
212 int sz = drr->drr_payloadlen;
218 (void) ssread(buf, sz, &zc);
219 if (ferror(send_stream))
221 err = nvlist_unpack(buf, sz, &nv, 0);
223 perror(strerror(err));
224 nvlist_print(stdout, nv);
231 drre->drr_checksum.zc_word[0] =
232 BSWAP_64(drre->drr_checksum.zc_word[0]);
233 drre->drr_checksum.zc_word[1] =
234 BSWAP_64(drre->drr_checksum.zc_word[1]);
235 drre->drr_checksum.zc_word[2] =
236 BSWAP_64(drre->drr_checksum.zc_word[2]);
237 drre->drr_checksum.zc_word[3] =
238 BSWAP_64(drre->drr_checksum.zc_word[3]);
241 * We compare against the *previous* checksum
242 * value, because the stored checksum is of
243 * everything before the DRR_END record.
245 if (do_cksum && !ZIO_CHECKSUM_EQUAL(drre->drr_checksum,
247 (void) printf("Expected checksum differs from "
248 "checksum in stream.\n");
249 (void) printf("Expected checksum = "
250 "%llx/%llx/%llx/%llx\n",
251 (long long unsigned int)pcksum.zc_word[0],
252 (long long unsigned int)pcksum.zc_word[1],
253 (long long unsigned int)pcksum.zc_word[2],
254 (long long unsigned int)pcksum.zc_word[3]);
256 (void) printf("END checksum = %llx/%llx/%llx/%llx\n",
257 (long long unsigned int)
258 drre->drr_checksum.zc_word[0],
259 (long long unsigned int)
260 drre->drr_checksum.zc_word[1],
261 (long long unsigned int)
262 drre->drr_checksum.zc_word[2],
263 (long long unsigned int)
264 drre->drr_checksum.zc_word[3]);
266 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
271 drro->drr_object = BSWAP_64(drro->drr_object);
272 drro->drr_type = BSWAP_32(drro->drr_type);
273 drro->drr_bonustype =
274 BSWAP_32(drro->drr_bonustype);
275 drro->drr_blksz = BSWAP_32(drro->drr_blksz);
277 BSWAP_32(drro->drr_bonuslen);
278 drro->drr_toguid = BSWAP_64(drro->drr_toguid);
281 (void) printf("OBJECT object = %llu type = %u "
282 "bonustype = %u blksz = %u bonuslen = %u\n",
283 (u_longlong_t)drro->drr_object,
289 if (drro->drr_bonuslen > 0) {
290 (void) ssread(buf, P2ROUNDUP(drro->drr_bonuslen,
295 case DRR_FREEOBJECTS:
297 drrfo->drr_firstobj =
298 BSWAP_64(drrfo->drr_firstobj);
300 BSWAP_64(drrfo->drr_numobjs);
301 drrfo->drr_toguid = BSWAP_64(drrfo->drr_toguid);
304 (void) printf("FREEOBJECTS firstobj = %llu "
306 (u_longlong_t)drrfo->drr_firstobj,
307 (u_longlong_t)drrfo->drr_numobjs);
313 drrw->drr_object = BSWAP_64(drrw->drr_object);
314 drrw->drr_type = BSWAP_32(drrw->drr_type);
315 drrw->drr_offset = BSWAP_64(drrw->drr_offset);
316 drrw->drr_length = BSWAP_64(drrw->drr_length);
317 drrw->drr_toguid = BSWAP_64(drrw->drr_toguid);
318 drrw->drr_key.ddk_prop =
319 BSWAP_64(drrw->drr_key.ddk_prop);
322 (void) printf("WRITE object = %llu type = %u "
323 "checksum type = %u\n"
324 "offset = %llu length = %llu "
326 (u_longlong_t)drrw->drr_object,
328 drrw->drr_checksumtype,
329 (u_longlong_t)drrw->drr_offset,
330 (u_longlong_t)drrw->drr_length,
331 (u_longlong_t)drrw->drr_key.ddk_prop);
333 (void) ssread(buf, drrw->drr_length, &zc);
334 total_write_size += drrw->drr_length;
337 case DRR_WRITE_BYREF:
340 BSWAP_64(drrwbr->drr_object);
342 BSWAP_64(drrwbr->drr_offset);
344 BSWAP_64(drrwbr->drr_length);
346 BSWAP_64(drrwbr->drr_toguid);
347 drrwbr->drr_refguid =
348 BSWAP_64(drrwbr->drr_refguid);
349 drrwbr->drr_refobject =
350 BSWAP_64(drrwbr->drr_refobject);
351 drrwbr->drr_refoffset =
352 BSWAP_64(drrwbr->drr_refoffset);
353 drrwbr->drr_key.ddk_prop =
354 BSWAP_64(drrwbr->drr_key.ddk_prop);
357 (void) printf("WRITE_BYREF object = %llu "
358 "checksum type = %u props = %llx\n"
359 "offset = %llu length = %llu\n"
360 "toguid = %llx refguid = %llx\n"
361 "refobject = %llu refoffset = %llu\n",
362 (u_longlong_t)drrwbr->drr_object,
363 drrwbr->drr_checksumtype,
364 (u_longlong_t)drrwbr->drr_key.ddk_prop,
365 (u_longlong_t)drrwbr->drr_offset,
366 (u_longlong_t)drrwbr->drr_length,
367 (u_longlong_t)drrwbr->drr_toguid,
368 (u_longlong_t)drrwbr->drr_refguid,
369 (u_longlong_t)drrwbr->drr_refobject,
370 (u_longlong_t)drrwbr->drr_refoffset);
376 drrf->drr_object = BSWAP_64(drrf->drr_object);
377 drrf->drr_offset = BSWAP_64(drrf->drr_offset);
378 drrf->drr_length = BSWAP_64(drrf->drr_length);
381 (void) printf("FREE object = %llu "
382 "offset = %llu length = %lld\n",
383 (u_longlong_t)drrf->drr_object,
384 (u_longlong_t)drrf->drr_offset,
385 (longlong_t)drrf->drr_length);
390 drrs->drr_object = BSWAP_64(drrs->drr_object);
391 drrs->drr_length = BSWAP_64(drrs->drr_length);
394 (void) printf("SPILL block for object = %llu "
396 (long long unsigned int)drrs->drr_object,
397 (long long unsigned int)drrs->drr_length);
399 (void) ssread(buf, drrs->drr_length, &zc);
402 /* should never be reached */
409 /* Print final summary */
411 (void) printf("SUMMARY:\n");
412 (void) printf("\tTotal DRR_BEGIN records = %lld\n",
413 (u_longlong_t)drr_record_count[DRR_BEGIN]);
414 (void) printf("\tTotal DRR_END records = %lld\n",
415 (u_longlong_t)drr_record_count[DRR_END]);
416 (void) printf("\tTotal DRR_OBJECT records = %lld\n",
417 (u_longlong_t)drr_record_count[DRR_OBJECT]);
418 (void) printf("\tTotal DRR_FREEOBJECTS records = %lld\n",
419 (u_longlong_t)drr_record_count[DRR_FREEOBJECTS]);
420 (void) printf("\tTotal DRR_WRITE records = %lld\n",
421 (u_longlong_t)drr_record_count[DRR_WRITE]);
422 (void) printf("\tTotal DRR_FREE records = %lld\n",
423 (u_longlong_t)drr_record_count[DRR_FREE]);
424 (void) printf("\tTotal DRR_SPILL records = %lld\n",
425 (u_longlong_t)drr_record_count[DRR_SPILL]);
426 (void) printf("\tTotal records = %lld\n",
427 (u_longlong_t)(drr_record_count[DRR_BEGIN] +
428 drr_record_count[DRR_OBJECT] +
429 drr_record_count[DRR_FREEOBJECTS] +
430 drr_record_count[DRR_WRITE] +
431 drr_record_count[DRR_FREE] +
432 drr_record_count[DRR_SPILL] +
433 drr_record_count[DRR_END]));
434 (void) printf("\tTotal write size = %lld (0x%llx)\n",
435 (u_longlong_t)total_write_size, (u_longlong_t)total_write_size);
436 (void) printf("\tTotal stream length = %lld (0x%llx)\n",
437 (u_longlong_t)total_stream_len, (u_longlong_t)total_stream_len);