Add -p switch to "zpool get"
[zfs.git] / cmd / zstreamdump / zstreamdump.c
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  * Portions Copyright 2012 Martin Matuska <martin@matuska.org>
27  */
28
29 #include <libnvpair.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <strings.h>
33 #include <unistd.h>
34
35 #include <sys/dmu.h>
36 #include <sys/zfs_ioctl.h>
37 #include <zfs_fletcher.h>
38
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)
46
47 static void
48 usage(void)
49 {
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");
53         exit(1);
54 }
55
56 /*
57  * ssread - send stream read.
58  *
59  * Read while computing incremental checksum
60  */
61
62 static size_t
63 ssread(void *buf, size_t len, zio_cksum_t *cksum)
64 {
65         size_t outlen;
66
67         if ((outlen = fread(buf, len, 1, send_stream)) == 0)
68                 return (0);
69
70         if (do_cksum && cksum) {
71                 if (do_byteswap)
72                         fletcher_4_incremental_byteswap(buf, len, cksum);
73                 else
74                         fletcher_4_incremental_native(buf, len, cksum);
75         }
76         total_stream_len += len;
77         return (outlen);
78 }
79
80 int
81 main(int argc, char *argv[])
82 {
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;
94         char c;
95         boolean_t verbose = B_FALSE;
96         boolean_t first = B_TRUE;
97         int err;
98         zio_cksum_t zc = { { 0 } };
99         zio_cksum_t pcksum = { { 0 } };
100
101         while ((c = getopt(argc, argv, ":vC")) != -1) {
102                 switch (c) {
103                 case 'C':
104                         do_cksum = B_FALSE;
105                         break;
106                 case 'v':
107                         verbose = B_TRUE;
108                         break;
109                 case ':':
110                         (void) fprintf(stderr,
111                             "missing argument for '%c' option\n", optopt);
112                         usage();
113                         break;
114                 case '?':
115                         (void) fprintf(stderr, "invalid option '%c'\n",
116                             optopt);
117                         usage();
118                 }
119         }
120
121         if (isatty(STDIN_FILENO)) {
122                 (void) fprintf(stderr,
123                     "Error: Backup stream can not be read "
124                     "from a terminal.\n"
125                     "You must redirect standard input.\n");
126                 exit(1);
127         }
128
129         send_stream = stdin;
130         pcksum = zc;
131         while (ssread(drr, sizeof (dmu_replay_record_t), &zc)) {
132
133                 if (first) {
134                         if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
135                                 do_byteswap = B_TRUE;
136                                 if (do_cksum) {
137                                         ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
138                                         /*
139                                          * recalculate header checksum now
140                                          * that we know it needs to be
141                                          * byteswapped.
142                                          */
143                                         fletcher_4_incremental_byteswap(drr,
144                                             sizeof (dmu_replay_record_t), &zc);
145                                 }
146                         } else if (drrb->drr_magic != DMU_BACKUP_MAGIC) {
147                                 (void) fprintf(stderr, "Invalid stream "
148                                     "(bad magic number)\n");
149                                 exit(1);
150                         }
151                         first = B_FALSE;
152                 }
153                 if (do_byteswap) {
154                         drr->drr_type = BSWAP_32(drr->drr_type);
155                         drr->drr_payloadlen =
156                             BSWAP_32(drr->drr_payloadlen);
157                 }
158
159                 /*
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
164                  * original state.
165                  */
166                 if (drr->drr_type >= DRR_NUMTYPES) {
167                         (void) printf("INVALID record found: type 0x%x\n",
168                             drr->drr_type);
169                         (void) printf("Aborting.\n");
170                         exit(1);
171                 }
172
173                 drr_record_count[drr->drr_type]++;
174
175                 switch (drr->drr_type) {
176                 case DRR_BEGIN:
177                         if (do_byteswap) {
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);
186                                 drrb->drr_fromguid =
187                                     BSWAP_64(drrb->drr_fromguid);
188                         }
189
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);
206                         if (verbose)
207                                 (void) printf("\n");
208
209                         if ((DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
210                             DMU_COMPOUNDSTREAM) && drr->drr_payloadlen != 0) {
211                                 nvlist_t *nv;
212                                 int sz = drr->drr_payloadlen;
213
214                                 if (sz > 1<<20) {
215                                         free(buf);
216                                         buf = malloc(sz);
217                                 }
218                                 (void) ssread(buf, sz, &zc);
219                                 if (ferror(send_stream))
220                                         perror("fread");
221                                 err = nvlist_unpack(buf, sz, &nv, 0);
222                                 if (err)
223                                         perror(strerror(err));
224                                 nvlist_print(stdout, nv);
225                                 nvlist_free(nv);
226                         }
227                         break;
228
229                 case DRR_END:
230                         if (do_byteswap) {
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]);
239                         }
240                         /*
241                          * We compare against the *previous* checksum
242                          * value, because the stored checksum is of
243                          * everything before the DRR_END record.
244                          */
245                         if (do_cksum && !ZIO_CHECKSUM_EQUAL(drre->drr_checksum,
246                             pcksum)) {
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]);
255                         }
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]);
265
266                         ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
267                         break;
268
269                 case DRR_OBJECT:
270                         if (do_byteswap) {
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);
276                                 drro->drr_bonuslen =
277                                     BSWAP_32(drro->drr_bonuslen);
278                                 drro->drr_toguid = BSWAP_64(drro->drr_toguid);
279                         }
280                         if (verbose) {
281                                 (void) printf("OBJECT object = %llu type = %u "
282                                     "bonustype = %u blksz = %u bonuslen = %u\n",
283                                     (u_longlong_t)drro->drr_object,
284                                     drro->drr_type,
285                                     drro->drr_bonustype,
286                                     drro->drr_blksz,
287                                     drro->drr_bonuslen);
288                         }
289                         if (drro->drr_bonuslen > 0) {
290                                 (void) ssread(buf, P2ROUNDUP(drro->drr_bonuslen,
291                                     8), &zc);
292                         }
293                         break;
294
295                 case DRR_FREEOBJECTS:
296                         if (do_byteswap) {
297                                 drrfo->drr_firstobj =
298                                     BSWAP_64(drrfo->drr_firstobj);
299                                 drrfo->drr_numobjs =
300                                     BSWAP_64(drrfo->drr_numobjs);
301                                 drrfo->drr_toguid = BSWAP_64(drrfo->drr_toguid);
302                         }
303                         if (verbose) {
304                                 (void) printf("FREEOBJECTS firstobj = %llu "
305                                     "numobjs = %llu\n",
306                                     (u_longlong_t)drrfo->drr_firstobj,
307                                     (u_longlong_t)drrfo->drr_numobjs);
308                         }
309                         break;
310
311                 case DRR_WRITE:
312                         if (do_byteswap) {
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);
320                         }
321                         if (verbose) {
322                                 (void) printf("WRITE object = %llu type = %u "
323                                     "checksum type = %u\n"
324                                     "offset = %llu length = %llu "
325                                     "props = %llx\n",
326                                     (u_longlong_t)drrw->drr_object,
327                                     drrw->drr_type,
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);
332                         }
333                         (void) ssread(buf, drrw->drr_length, &zc);
334                         total_write_size += drrw->drr_length;
335                         break;
336
337                 case DRR_WRITE_BYREF:
338                         if (do_byteswap) {
339                                 drrwbr->drr_object =
340                                     BSWAP_64(drrwbr->drr_object);
341                                 drrwbr->drr_offset =
342                                     BSWAP_64(drrwbr->drr_offset);
343                                 drrwbr->drr_length =
344                                     BSWAP_64(drrwbr->drr_length);
345                                 drrwbr->drr_toguid =
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);
355                         }
356                         if (verbose) {
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);
371                         }
372                         break;
373
374                 case DRR_FREE:
375                         if (do_byteswap) {
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);
379                         }
380                         if (verbose) {
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);
386                         }
387                         break;
388                 case DRR_SPILL:
389                         if (do_byteswap) {
390                                 drrs->drr_object = BSWAP_64(drrs->drr_object);
391                                 drrs->drr_length = BSWAP_64(drrs->drr_length);
392                         }
393                         if (verbose) {
394                                 (void) printf("SPILL block for object = %llu "
395                                     "length = %llu\n",
396                                     (long long unsigned int)drrs->drr_object,
397                                     (long long unsigned int)drrs->drr_length);
398                         }
399                         (void) ssread(buf, drrs->drr_length, &zc);
400                         break;
401                 case DRR_NUMTYPES:
402                         /* should never be reached */
403                         exit(1);
404                 }
405                 pcksum = zc;
406         }
407         free(buf);
408
409         /* Print final summary */
410
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);
438         return (0);
439 }