Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sbin / hammer2 / cmd_debug.c
1 /*
2  * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include "hammer2.h"
37
38 #define SHOW_TAB        2
39
40 static void shell_msghandler(dmsg_msg_t *msg, int unmanaged);
41 static void shell_ttymsg(dmsg_iocom_t *iocom);
42
43 /************************************************************************
44  *                                  SHELL                               *
45  ************************************************************************/
46
47 int
48 cmd_shell(const char *hostname)
49 {
50         dmsg_master_service_info_t *info;
51         pthread_t thread;
52         int fd;
53
54         fd = dmsg_connect(hostname);
55         if (fd < 0)
56                 return 1;
57
58         info = malloc(sizeof(*info));
59         bzero(info, sizeof(*info));
60         info->fd = fd;
61         info->detachme = 0;
62         info->usrmsg_callback = shell_msghandler;
63         info->altmsg_callback = shell_ttymsg;
64         info->label = strdup("debug");
65         pthread_create(&thread, NULL, dmsg_master_service, info);
66         pthread_join(thread, NULL);
67
68         return 0;
69 }
70
71 #if 0
72 int
73 cmd_shell(const char *hostname)
74 {
75         struct dmsg_iocom iocom;
76         dmsg_msg_t *msg;
77         int fd;
78
79         /*
80          * Connect to the target
81          */
82         fd = dmsg_connect(hostname);
83         if (fd < 0)
84                 return 1;
85
86         /*
87          * Initialize the session and transmit an empty DMSG_DBG_SHELL
88          * to cause the remote end to generate a prompt.
89          */
90         dmsg_iocom_init(&iocom, fd, 0,
91                         NULL,
92                         shell_rcvmsg,
93                         hammer2_shell_parse,
94                         shell_ttymsg);
95         fcntl(0, F_SETFL, O_NONBLOCK);
96         printf("debug: connected\n");
97
98         msg = dmsg_msg_alloc(&iocom.state0, 0, DMSG_DBG_SHELL, NULL, NULL);
99         dmsg_msg_write(msg);
100         dmsg_iocom_core(&iocom);
101         fprintf(stderr, "debug: disconnected\n");
102         close(fd);
103         return 0;
104 }
105 #endif
106
107 /*
108  * Debug session front-end
109  *
110  * Callback from dmsg_iocom_core() when messages might be present
111  * on the socket.
112  */
113 static
114 void
115 shell_msghandler(dmsg_msg_t *msg, int unmanaged)
116 {
117         dmsg_msg_t *nmsg;
118
119         switch(msg->tcmd) {
120 #if 0
121         case DMSG_LNK_ERROR:
122         case DMSG_LNK_ERROR | DMSGF_REPLY:
123                 /*
124                  * One-way non-transactional LNK_ERROR messages typically
125                  * indicate a connection failure.  Error code 0 is used by
126                  * the debug shell to indicate no more results from last cmd.
127                  */
128                 if (msg->any.head.error) {
129                         fprintf(stderr, "Stream failure: %s\n",
130                                 dmsg_msg_str(msg));
131                 } else {
132                         write(1, "debug> ", 7);
133                 }
134                 break;
135         case DMSG_LNK_ERROR | DMSGF_DELETE:
136                 /* ignore termination of LNK_CONN */
137                 break;
138 #endif
139         case DMSG_DBG_SHELL:
140                 /*
141                  * We send the commands, not accept them.
142                  * (one-way message, not transactional)
143                  */
144                 if (unmanaged)
145                         dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
146                 break;
147         case DMSG_DBG_SHELL | DMSGF_REPLY:
148                 /*
149                  * A reply from the remote is data we copy to stdout.
150                  * (one-way message, not transactional)
151                  */
152                 if (msg->aux_size) {
153                         msg->aux_data[msg->aux_size - 1] = 0;
154                         write(1, msg->aux_data, strlen(msg->aux_data));
155                 }
156                 break;
157 #if 1
158         case DMSG_LNK_CONN | DMSGF_CREATE:
159                 fprintf(stderr, "Debug Shell received LNK_CONN\n");
160                 nmsg = dmsg_msg_alloc(&msg->state->iocom->state0, 0,
161                                       DMSG_DBG_SHELL,
162                                       NULL, NULL);
163                 dmsg_msg_write(nmsg);
164                 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
165                 break;
166         case DMSG_LNK_CONN | DMSGF_DELETE:
167                 break;
168 #endif
169         default:
170                 /*
171                  * Ignore any unknown messages, Terminate any unknown
172                  * transactions with an error.
173                  */
174                 fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg));
175                 if (unmanaged) {
176                         if (msg->any.head.cmd & DMSGF_CREATE)
177                                 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
178                         if (msg->any.head.cmd & DMSGF_DELETE)
179                                 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
180                 }
181                 break;
182         }
183 }
184
185 /*
186  * Debug session front-end
187  */
188 static
189 void
190 shell_ttymsg(dmsg_iocom_t *iocom)
191 {
192         dmsg_state_t *pstate;
193         dmsg_msg_t *msg;
194         char buf[256];
195         char *cmd;
196         size_t len;
197
198         if (fgets(buf, sizeof(buf), stdin) != NULL) {
199                 if (buf[0] == '@') {
200                         pstate = dmsg_findspan(strtok(buf + 1, " \t\n"));
201                         cmd = strtok(NULL, "\n");
202                 } else {
203                         pstate = &iocom->state0;
204                         cmd = strtok(buf, "\n");
205                 }
206                 if (cmd && pstate) {
207                         len = strlen(cmd) + 1;
208                         msg = dmsg_msg_alloc(pstate, len, DMSG_DBG_SHELL,
209                                              NULL, NULL);
210                         bcopy(cmd, msg->aux_data, len);
211                         dmsg_msg_write(msg);
212                 } else if (cmd) {
213                         fprintf(stderr, "@msgid not found\n");
214                 } else {
215                         /*
216                          * This should cause the remote end to generate
217                          * a debug> prompt (and thus shows that there is
218                          * connectivity).
219                          */
220                         msg = dmsg_msg_alloc(pstate, 0, DMSG_DBG_SHELL,
221                                              NULL, NULL);
222                         dmsg_msg_write(msg);
223                 }
224         } else if (feof(stdin)) {
225                 /*
226                  * Set EOF flag without setting any error code for normal
227                  * EOF.
228                  */
229                 iocom->flags |= DMSG_IOCOMF_EOF;
230         } else {
231                 clearerr(stdin);
232         }
233 }
234
235 /*
236  * Debug session back-end (on remote side)
237  */
238 static void shell_span(dmsg_msg_t *msg, char *cmdbuf);
239
240 void
241 hammer2_shell_parse(dmsg_msg_t *msg, int unmanaged)
242 {
243         dmsg_iocom_t *iocom = msg->state->iocom;
244         char *cmdbuf;
245         char *cmdp;
246         uint32_t cmd;
247
248         /*
249          * Filter on debug shell commands only
250          */
251         cmd = msg->any.head.cmd;
252         if ((cmd & DMSGF_PROTOS) != DMSG_PROTO_DBG) {
253                 if (unmanaged)
254                         dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
255                 return;
256         }
257         if ((cmd & DMSGF_CMDSWMASK) != DMSG_DBG_SHELL) {
258                 if (unmanaged)
259                         dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
260                 return;
261         }
262
263         /*
264          * Debug shell command
265          */
266         cmdbuf = msg->aux_data;
267         cmdp = strsep(&cmdbuf, " \t");
268
269         if (cmdp == NULL || *cmdp == 0) {
270                 ;
271         } else if (strcmp(cmdp, "span") == 0) {
272                 shell_span(msg, cmdbuf);
273         } else if (strcmp(cmdp, "tree") == 0) {
274                 dmsg_shell_tree(iocom, cmdbuf); /* dump spanning tree */
275         } else if (strcmp(cmdp, "help") == 0 || strcmp(cmdp, "?") == 0) {
276                 dmsg_printf(iocom, "help            Command help\n");
277                 dmsg_printf(iocom, "span <host>     Span to target host\n");
278                 dmsg_printf(iocom, "tree            Dump spanning tree\n");
279                 dmsg_printf(iocom, "@span <cmd>     Issue via circuit\n");
280         } else {
281                 dmsg_printf(iocom, "Unrecognized command: %s\n", cmdp);
282         }
283         dmsg_printf(iocom, "debug> ");
284 }
285
286 static void
287 shell_span(dmsg_msg_t *msg, char *cmdbuf)
288 {
289         dmsg_iocom_t *iocom = msg->state->iocom;
290         dmsg_master_service_info_t *info;
291         const char *hostname = strsep(&cmdbuf, " \t");
292         pthread_t thread;
293         int fd;
294
295         /*
296          * Connect to the target
297          */
298         if (hostname == NULL) {
299                 fd = -1;
300         } else {
301                 fd = dmsg_connect(hostname);
302         }
303
304         /*
305          * Start master service
306          */
307         if (fd < 0) {
308                 dmsg_printf(iocom, "Connection to %s failed\n", hostname);
309         } else {
310                 dmsg_printf(iocom, "Connected to %s\n", hostname);
311
312                 info = malloc(sizeof(*info));
313                 bzero(info, sizeof(*info));
314                 info->fd = fd;
315                 info->detachme = 1;
316                 info->usrmsg_callback = hammer2_shell_parse;
317                 info->label = strdup("client");
318
319                 pthread_create(&thread, NULL, dmsg_master_service, info);
320                 /*pthread_join(thread, &res);*/
321         }
322 }
323
324 /************************************************************************
325  *                              DEBUGSPAN                               *
326  ************************************************************************
327  *
328  * Connect to the target manually (not via the cluster list embedded in
329  * a hammer2 filesystem) and initiate the SPAN protocol.
330  */
331 int
332 cmd_debugspan(const char *hostname)
333 {
334         pthread_t thread;
335         int fd;
336         void *res;
337
338         /*
339          * Connect to the target
340          */
341         fd = dmsg_connect(hostname);
342         if (fd < 0)
343                 return 1;
344
345         printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
346         pthread_create(&thread, NULL,
347                        dmsg_master_service, (void *)(intptr_t)fd);
348         pthread_join(thread, &res);
349         return(0);
350 }
351
352 /************************************************************************
353  *                                  SHOW                                *
354  ************************************************************************/
355
356 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref,
357                         int dofreemap);
358 static void tabprintf(int tab, const char *ctl, ...);
359
360 int
361 cmd_show(const char *devpath, int dofreemap)
362 {
363         hammer2_blockref_t broot;
364         hammer2_blockref_t best;
365         hammer2_media_data_t media;
366         int fd;
367         int i;
368         int best_i;
369
370         fd = open(devpath, O_RDONLY);
371         if (fd < 0) {
372                 perror("open");
373                 return 1;
374         }
375
376         /*
377          * Show the tree using the best volume header.
378          * -vvv will show the tree for all four volume headers.
379          */
380         best_i = -1;
381         bzero(&best, sizeof(best));
382         for (i = 0; i < 4; ++i) {
383                 bzero(&broot, sizeof(broot));
384                 broot.type = HAMMER2_BREF_TYPE_VOLUME;
385                 broot.data_off = (i * HAMMER2_ZONE_BYTES64) |
386                                  HAMMER2_PBUFRADIX;
387                 lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
388                 if (read(fd, &media, HAMMER2_PBUFSIZE) ==
389                     (ssize_t)HAMMER2_PBUFSIZE) {
390                         broot.mirror_tid = media.voldata.mirror_tid;
391                         if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
392                                 best_i = i;
393                                 best = broot;
394                         }
395                         if (VerboseOpt >= 3)
396                                 show_bref(fd, 0, i, &broot, dofreemap);
397                 }
398         }
399         if (VerboseOpt < 3)
400                 show_bref(fd, 0, best_i, &best, dofreemap);
401         close(fd);
402
403         return 0;
404 }
405
406 extern uint32_t iscsi_crc32(const void *buf, size_t size);
407 static void
408 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref, int dofreemap)
409 {
410         hammer2_media_data_t media;
411         hammer2_blockref_t *bscan;
412         int bcount;
413         int i;
414         int didnl;
415         int namelen;
416         int obrace = 1;
417         size_t bytes;
418         const char *type_str;
419         char *str = NULL;
420         uint32_t cv;
421
422         switch(bref->type) {
423         case HAMMER2_BREF_TYPE_EMPTY:
424                 type_str = "empty";
425                 break;
426         case HAMMER2_BREF_TYPE_INODE:
427                 type_str = "inode";
428                 break;
429         case HAMMER2_BREF_TYPE_INDIRECT:
430                 type_str = "indblk";
431                 break;
432         case HAMMER2_BREF_TYPE_DATA:
433                 type_str = "data";
434                 break;
435         case HAMMER2_BREF_TYPE_VOLUME:
436                 type_str = "volume";
437                 break;
438         case HAMMER2_BREF_TYPE_FREEMAP:
439                 type_str = "freemap";
440                 break;
441         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
442                 type_str = "fmapnode";
443                 break;
444         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
445                 type_str = "fbitmap";
446                 break;
447         default:
448                 type_str = "unknown";
449                 break;
450         }
451
452         tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
453                type_str, bi, (intmax_t)bref->data_off,
454                (intmax_t)bref->key, (intmax_t)bref->keybits,
455                (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
456         tab += SHOW_TAB;
457
458         bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
459
460         {
461                 hammer2_off_t io_off;
462                 hammer2_off_t io_base;
463                 size_t io_bytes;
464                 size_t boff;
465
466                 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
467                 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
468                 io_bytes = bytes;
469                 boff = io_off - io_base;
470
471                 io_bytes = HAMMER2_MINIOSIZE;
472                 while (io_bytes + boff < bytes)
473                         io_bytes <<= 1;
474
475                 if (io_bytes > sizeof(media)) {
476                         printf("(bad block size %zd)\n", bytes);
477                         return;
478                 }
479                 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
480                         lseek(fd, io_base, 0);
481                         if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
482                                 printf("(media read failed)\n");
483                                 return;
484                         }
485                         if (boff)
486                                 bcopy((char *)&media + boff, &media, bytes);
487                 }
488         }
489
490         bscan = NULL;
491         bcount = 0;
492         didnl = 1;
493         namelen = 0;
494
495         /*
496          * Check data integrity in verbose mode, otherwise we are just doing
497          * a quick meta-data scan.  Meta-data integrity is always checked.
498          * (Also see the check above that ensures the media data is loaded,
499          * otherwise there's no data to check!).
500          */
501         if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
502                 switch(HAMMER2_DEC_CHECK(bref->methods)) {
503                 case HAMMER2_CHECK_NONE:
504                         printf("(meth %02x) ", bref->methods);
505                         break;
506                 case HAMMER2_CHECK_DISABLED:
507                         printf("(meth %02x) ", bref->methods);
508                         break;
509                 case HAMMER2_CHECK_ISCSI32:
510                         cv = hammer2_icrc32(&media, bytes);
511                         if (bref->check.iscsi32.value != cv) {
512                                 printf("(icrc %02x:%08x/%08x) ",
513                                        bref->methods,
514                                        bref->check.iscsi32.value,
515                                        cv);
516                         } else {
517                                 printf("(meth %02x) ", bref->methods);
518                         }
519                         break;
520                 case HAMMER2_CHECK_CRC64:
521                         printf("(meth %02x) ", bref->methods);
522                         break;
523                 case HAMMER2_CHECK_SHA192:
524                         printf("(meth %02x) ", bref->methods);
525                         break;
526                 case HAMMER2_CHECK_FREEMAP:
527                         cv = hammer2_icrc32(&media, bytes);
528                         if (bref->check.freemap.icrc32 != cv) {
529                                 printf("(fcrc %02x:%08x/%08x) ",
530                                         bref->methods,
531                                         bref->check.freemap.icrc32,
532                                         cv);
533                         } else {
534                                 printf("(meth %02x) ", bref->methods);
535                         }
536                         break;
537                 }
538         }
539
540         switch(bref->type) {
541         case HAMMER2_BREF_TYPE_EMPTY:
542                 obrace = 0;
543                 break;
544         case HAMMER2_BREF_TYPE_INODE:
545                 printf("{\n");
546                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
547                         /* no blockrefs */
548                 } else {
549                         bscan = &media.ipdata.u.blockset.blockref[0];
550                         bcount = HAMMER2_SET_COUNT;
551                 }
552                 namelen = media.ipdata.name_len;
553                 if (namelen > HAMMER2_INODE_MAXNAME)
554                         namelen = 0;
555                 tabprintf(tab, "filename \"%*.*s\"\n",
556                           namelen, namelen, media.ipdata.filename);
557                 tabprintf(tab, "version  %d\n", media.ipdata.version);
558                 tabprintf(tab, "uflags   0x%08x\n",
559                           media.ipdata.uflags);
560                 if (media.ipdata.rmajor || media.ipdata.rminor) {
561                         tabprintf(tab, "rmajor   %d\n",
562                                   media.ipdata.rmajor);
563                         tabprintf(tab, "rminor   %d\n",
564                                   media.ipdata.rminor);
565                 }
566                 tabprintf(tab, "ctime    %s\n",
567                           hammer2_time64_to_str(media.ipdata.ctime, &str));
568                 tabprintf(tab, "mtime    %s\n",
569                           hammer2_time64_to_str(media.ipdata.mtime, &str));
570                 tabprintf(tab, "atime    %s\n",
571                           hammer2_time64_to_str(media.ipdata.atime, &str));
572                 tabprintf(tab, "btime    %s\n",
573                           hammer2_time64_to_str(media.ipdata.btime, &str));
574                 tabprintf(tab, "uid      %s\n",
575                           hammer2_uuid_to_str(&media.ipdata.uid, &str));
576                 tabprintf(tab, "gid      %s\n",
577                           hammer2_uuid_to_str(&media.ipdata.gid, &str));
578                 if (media.ipdata.type == HAMMER2_OBJTYPE_HARDLINK)
579                         tabprintf(tab, "type     %s (%s)\n",
580                               hammer2_iptype_to_str(media.ipdata.type),
581                               hammer2_iptype_to_str(media.ipdata.target_type));
582                 else
583                         tabprintf(tab, "type     %s\n",
584                                   hammer2_iptype_to_str(media.ipdata.type));
585                 tabprintf(tab, "opflgs   0x%02x\n",
586                           media.ipdata.op_flags);
587                 tabprintf(tab, "capflgs  0x%04x\n",
588                           media.ipdata.cap_flags);
589                 tabprintf(tab, "mode     %-7o\n",
590                           media.ipdata.mode);
591                 tabprintf(tab, "inum     0x%016jx\n",
592                           media.ipdata.inum);
593                 tabprintf(tab, "size     %ju\n",
594                           (uintmax_t)media.ipdata.size);
595                 tabprintf(tab, "nlinks   %ju\n",
596                           (uintmax_t)media.ipdata.nlinks);
597                 tabprintf(tab, "iparent  0x%016jx\n",
598                           (uintmax_t)media.ipdata.iparent);
599                 tabprintf(tab, "name_key 0x%016jx\n",
600                           (uintmax_t)media.ipdata.name_key);
601                 tabprintf(tab, "name_len %u\n",
602                           media.ipdata.name_len);
603                 tabprintf(tab, "ncopies  %u\n",
604                           media.ipdata.ncopies);
605                 tabprintf(tab, "compalg  %u\n",
606                           media.ipdata.comp_algo);
607                 tabprintf(tab, "checkalg %u\n",
608                           media.ipdata.check_algo);
609                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
610                         tabprintf(tab, "pfs_type %u (%s)\n",
611                                   media.ipdata.pfs_type,
612                                   hammer2_pfstype_to_str(media.ipdata.pfs_type));
613                         tabprintf(tab, "pfs_inum 0x%016jx\n",
614                                   (uintmax_t)media.ipdata.pfs_inum);
615                         tabprintf(tab, "pfs_clid %s\n",
616                                   hammer2_uuid_to_str(&media.ipdata.pfs_clid,
617                                                       &str));
618                         tabprintf(tab, "pfs_fsid %s\n",
619                                   hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
620                                                       &str));
621                 }
622                 tabprintf(tab, "data_quota  %ju\n",
623                           (uintmax_t)media.ipdata.data_quota);
624                 tabprintf(tab, "data_count  %ju\n",
625                           (uintmax_t)media.ipdata.data_count);
626                 tabprintf(tab, "inode_quota %ju\n",
627                           (uintmax_t)media.ipdata.inode_quota);
628                 tabprintf(tab, "inode_count %ju\n",
629                           (uintmax_t)media.ipdata.inode_count);
630                 tabprintf(tab, "attr_tid    0x%016jx\n",
631                           (uintmax_t)media.ipdata.attr_tid);
632                 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
633                         tabprintf(tab, "dirent_tid  %016jx\n",
634                                   (uintmax_t)media.ipdata.dirent_tid);
635                 }
636                 break;
637         case HAMMER2_BREF_TYPE_INDIRECT:
638                 bscan = &media.npdata[0];
639                 bcount = bytes / sizeof(hammer2_blockref_t);
640                 didnl = 1;
641                 printf("{\n");
642                 break;
643         case HAMMER2_BREF_TYPE_DATA:
644                 if (VerboseOpt >= 2) {
645                         printf("{\n");
646                 } else {
647                         printf("\n");
648                         obrace = 0;
649                 }
650                 break;
651         case HAMMER2_BREF_TYPE_VOLUME:
652                 printf("mirror_tid=%016jx freemap_tid=%016jx ",
653                         media.voldata.mirror_tid,
654                         media.voldata.freemap_tid);
655                 if (dofreemap) {
656                         bscan = &media.voldata.freemap_blockset.blockref[0];
657                         bcount = HAMMER2_SET_COUNT;
658                 } else {
659                         bscan = &media.voldata.sroot_blockset.blockref[0];
660                         bcount = HAMMER2_SET_COUNT;
661                 }
662                 printf("{\n");
663                 break;
664         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
665                 printf("{\n");
666                 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
667                         if (media.bmdata[i].class == 0 &&
668                             media.bmdata[i].avail == 0) {
669                                 continue;
670                         }
671                         tabprintf(tab + 4, "%04d.%04x (avail=%5d) "
672                                   "%08x %08x %08x %08x %08x %08x %08x %08x\n",
673                                   i, media.bmdata[i].class,
674                                   media.bmdata[i].avail,
675                                   media.bmdata[i].bitmap[0],
676                                   media.bmdata[i].bitmap[1],
677                                   media.bmdata[i].bitmap[2],
678                                   media.bmdata[i].bitmap[3],
679                                   media.bmdata[i].bitmap[4],
680                                   media.bmdata[i].bitmap[5],
681                                   media.bmdata[i].bitmap[6],
682                                   media.bmdata[i].bitmap[7]);
683                 }
684                 tabprintf(tab, "}\n");
685                 break;
686         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
687                 printf("{\n");
688                 bscan = &media.npdata[0];
689                 bcount = bytes / sizeof(hammer2_blockref_t);
690                 break;
691         default:
692                 printf("\n");
693                 obrace = 0;
694                 break;
695         }
696         if (str)
697                 free(str);
698         for (i = 0; i < bcount; ++i) {
699                 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
700                         if (didnl == 0) {
701                                 printf("\n");
702                                 didnl = 1;
703                         }
704                         show_bref(fd, tab, i, &bscan[i], dofreemap);
705                 }
706         }
707         tab -= SHOW_TAB;
708         if (obrace) {
709                 if (bref->type == HAMMER2_BREF_TYPE_INODE)
710                         tabprintf(tab, "} (%s.%d, \"%*.*s\")\n",
711                                   type_str, bi,
712                                   namelen, namelen, media.ipdata.filename);
713                 else
714                         tabprintf(tab, "} (%s.%d)\n", type_str,bi);
715         }
716 }
717
718 int
719 cmd_hash(int ac, const char **av)
720 {
721         int i;
722
723         for (i = 0; i < ac; ++i) {
724                 printf("%016jx %s\n", dirhash(av[i], strlen(av[i])), av[i]);
725         }
726         return(0);
727 }
728
729 int
730 cmd_chaindump(const char *path)
731 {
732         int dummy = 0;
733         int fd;
734
735         fd = open(path, O_RDONLY);
736         if (fd >= 0) {
737                 ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy);
738                 close(fd);
739         } else {
740                 fprintf(stderr, "unable to open %s\n", path);
741         }
742         return 0;
743 }
744
745
746 static
747 void
748 tabprintf(int tab, const char *ctl, ...)
749 {
750         va_list va;
751
752         printf("%*.*s", tab, tab, "");
753         va_start(va, ctl);
754         vprintf(ctl, va);
755         va_end(va);
756 }