67b4e7253ee95b9c414b72e311435bdb4b49c42d
[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         if (bref->flags)
458                 printf("flags=%02x ", bref->flags);
459
460         bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
461
462         {
463                 hammer2_off_t io_off;
464                 hammer2_off_t io_base;
465                 size_t io_bytes;
466                 size_t boff;
467
468                 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
469                 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
470                 io_bytes = bytes;
471                 boff = io_off - io_base;
472
473                 io_bytes = HAMMER2_MINIOSIZE;
474                 while (io_bytes + boff < bytes)
475                         io_bytes <<= 1;
476
477                 if (io_bytes > sizeof(media)) {
478                         printf("(bad block size %zd)\n", bytes);
479                         return;
480                 }
481                 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
482                         lseek(fd, io_base, 0);
483                         if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
484                                 printf("(media read failed)\n");
485                                 return;
486                         }
487                         if (boff)
488                                 bcopy((char *)&media + boff, &media, bytes);
489                 }
490         }
491
492         bscan = NULL;
493         bcount = 0;
494         didnl = 1;
495         namelen = 0;
496
497         /*
498          * Check data integrity in verbose mode, otherwise we are just doing
499          * a quick meta-data scan.  Meta-data integrity is always checked.
500          * (Also see the check above that ensures the media data is loaded,
501          * otherwise there's no data to check!).
502          */
503         if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
504                 switch(HAMMER2_DEC_CHECK(bref->methods)) {
505                 case HAMMER2_CHECK_NONE:
506                         printf("(meth %02x) ", bref->methods);
507                         break;
508                 case HAMMER2_CHECK_DISABLED:
509                         printf("(meth %02x) ", bref->methods);
510                         break;
511                 case HAMMER2_CHECK_ISCSI32:
512                         cv = hammer2_icrc32(&media, bytes);
513                         if (bref->check.iscsi32.value != cv) {
514                                 printf("(icrc %02x:%08x/%08x) ",
515                                        bref->methods,
516                                        bref->check.iscsi32.value,
517                                        cv);
518                         } else {
519                                 printf("(meth %02x) ", bref->methods);
520                         }
521                         break;
522                 case HAMMER2_CHECK_CRC64:
523                         printf("(meth %02x) ", bref->methods);
524                         break;
525                 case HAMMER2_CHECK_SHA192:
526                         printf("(meth %02x) ", bref->methods);
527                         break;
528                 case HAMMER2_CHECK_FREEMAP:
529                         cv = hammer2_icrc32(&media, bytes);
530                         if (bref->check.freemap.icrc32 != cv) {
531                                 printf("(fcrc %02x:%08x/%08x) ",
532                                         bref->methods,
533                                         bref->check.freemap.icrc32,
534                                         cv);
535                         } else {
536                                 printf("(meth %02x) ", bref->methods);
537                         }
538                         break;
539                 }
540         }
541
542         switch(bref->type) {
543         case HAMMER2_BREF_TYPE_EMPTY:
544                 obrace = 0;
545                 break;
546         case HAMMER2_BREF_TYPE_INODE:
547                 printf("{\n");
548                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
549                         /* no blockrefs */
550                 } else {
551                         bscan = &media.ipdata.u.blockset.blockref[0];
552                         bcount = HAMMER2_SET_COUNT;
553                 }
554                 namelen = media.ipdata.name_len;
555                 if (namelen > HAMMER2_INODE_MAXNAME)
556                         namelen = 0;
557                 tabprintf(tab, "filename \"%*.*s\"\n",
558                           namelen, namelen, media.ipdata.filename);
559                 tabprintf(tab, "version  %d\n", media.ipdata.version);
560                 tabprintf(tab, "uflags   0x%08x\n",
561                           media.ipdata.uflags);
562                 if (media.ipdata.rmajor || media.ipdata.rminor) {
563                         tabprintf(tab, "rmajor   %d\n",
564                                   media.ipdata.rmajor);
565                         tabprintf(tab, "rminor   %d\n",
566                                   media.ipdata.rminor);
567                 }
568                 tabprintf(tab, "ctime    %s\n",
569                           hammer2_time64_to_str(media.ipdata.ctime, &str));
570                 tabprintf(tab, "mtime    %s\n",
571                           hammer2_time64_to_str(media.ipdata.mtime, &str));
572                 tabprintf(tab, "atime    %s\n",
573                           hammer2_time64_to_str(media.ipdata.atime, &str));
574                 tabprintf(tab, "btime    %s\n",
575                           hammer2_time64_to_str(media.ipdata.btime, &str));
576                 tabprintf(tab, "uid      %s\n",
577                           hammer2_uuid_to_str(&media.ipdata.uid, &str));
578                 tabprintf(tab, "gid      %s\n",
579                           hammer2_uuid_to_str(&media.ipdata.gid, &str));
580                 if (media.ipdata.type == HAMMER2_OBJTYPE_HARDLINK)
581                         tabprintf(tab, "type     %s (%s)\n",
582                               hammer2_iptype_to_str(media.ipdata.type),
583                               hammer2_iptype_to_str(media.ipdata.target_type));
584                 else
585                         tabprintf(tab, "type     %s\n",
586                                   hammer2_iptype_to_str(media.ipdata.type));
587                 tabprintf(tab, "opflgs   0x%02x\n",
588                           media.ipdata.op_flags);
589                 tabprintf(tab, "capflgs  0x%04x\n",
590                           media.ipdata.cap_flags);
591                 tabprintf(tab, "mode     %-7o\n",
592                           media.ipdata.mode);
593                 tabprintf(tab, "inum     0x%016jx\n",
594                           media.ipdata.inum);
595                 tabprintf(tab, "size     %ju\n",
596                           (uintmax_t)media.ipdata.size);
597                 tabprintf(tab, "nlinks   %ju\n",
598                           (uintmax_t)media.ipdata.nlinks);
599                 tabprintf(tab, "iparent  0x%016jx\n",
600                           (uintmax_t)media.ipdata.iparent);
601                 tabprintf(tab, "name_key 0x%016jx\n",
602                           (uintmax_t)media.ipdata.name_key);
603                 tabprintf(tab, "name_len %u\n",
604                           media.ipdata.name_len);
605                 tabprintf(tab, "ncopies  %u\n",
606                           media.ipdata.ncopies);
607                 tabprintf(tab, "compalg  %u\n",
608                           media.ipdata.comp_algo);
609                 tabprintf(tab, "checkalg %u\n",
610                           media.ipdata.check_algo);
611                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
612                         tabprintf(tab, "pfs_type %u (%s)\n",
613                                   media.ipdata.pfs_type,
614                                   hammer2_pfstype_to_str(media.ipdata.pfs_type));
615                         tabprintf(tab, "pfs_inum 0x%016jx\n",
616                                   (uintmax_t)media.ipdata.pfs_inum);
617                         tabprintf(tab, "pfs_clid %s\n",
618                                   hammer2_uuid_to_str(&media.ipdata.pfs_clid,
619                                                       &str));
620                         tabprintf(tab, "pfs_fsid %s\n",
621                                   hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
622                                                       &str));
623                 }
624                 tabprintf(tab, "data_quota  %ju\n",
625                           (uintmax_t)media.ipdata.data_quota);
626                 tabprintf(tab, "data_count  %ju\n",
627                           (uintmax_t)media.ipdata.data_count);
628                 tabprintf(tab, "inode_quota %ju\n",
629                           (uintmax_t)media.ipdata.inode_quota);
630                 tabprintf(tab, "inode_count %ju\n",
631                           (uintmax_t)media.ipdata.inode_count);
632                 tabprintf(tab, "attr_tid    0x%016jx\n",
633                           (uintmax_t)media.ipdata.attr_tid);
634                 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
635                         tabprintf(tab, "dirent_tid  %016jx\n",
636                                   (uintmax_t)media.ipdata.dirent_tid);
637                 }
638                 break;
639         case HAMMER2_BREF_TYPE_INDIRECT:
640                 bscan = &media.npdata[0];
641                 bcount = bytes / sizeof(hammer2_blockref_t);
642                 didnl = 1;
643                 printf("{\n");
644                 break;
645         case HAMMER2_BREF_TYPE_DATA:
646                 if (VerboseOpt >= 2) {
647                         printf("{\n");
648                 } else {
649                         printf("\n");
650                         obrace = 0;
651                 }
652                 break;
653         case HAMMER2_BREF_TYPE_VOLUME:
654                 printf("mirror_tid=%016jx freemap_tid=%016jx ",
655                         media.voldata.mirror_tid,
656                         media.voldata.freemap_tid);
657                 if (dofreemap) {
658                         bscan = &media.voldata.freemap_blockset.blockref[0];
659                         bcount = HAMMER2_SET_COUNT;
660                 } else {
661                         bscan = &media.voldata.sroot_blockset.blockref[0];
662                         bcount = HAMMER2_SET_COUNT;
663                 }
664                 printf("{\n");
665                 break;
666         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
667                 printf("{\n");
668                 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
669                         if (media.bmdata[i].class == 0 &&
670                             media.bmdata[i].avail == 0) {
671                                 continue;
672                         }
673                         tabprintf(tab + 4, "%04d.%04x (avail=%7d) "
674                                   "%08x %08x %08x %08x %08x %08x %08x %08x\n",
675                                   i, media.bmdata[i].class,
676                                   media.bmdata[i].avail,
677                                   media.bmdata[i].bitmap[0],
678                                   media.bmdata[i].bitmap[1],
679                                   media.bmdata[i].bitmap[2],
680                                   media.bmdata[i].bitmap[3],
681                                   media.bmdata[i].bitmap[4],
682                                   media.bmdata[i].bitmap[5],
683                                   media.bmdata[i].bitmap[6],
684                                   media.bmdata[i].bitmap[7]);
685                 }
686                 tabprintf(tab, "}\n");
687                 break;
688         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
689                 printf("{\n");
690                 bscan = &media.npdata[0];
691                 bcount = bytes / sizeof(hammer2_blockref_t);
692                 break;
693         default:
694                 printf("\n");
695                 obrace = 0;
696                 break;
697         }
698         if (str)
699                 free(str);
700         for (i = 0; i < bcount; ++i) {
701                 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
702                         if (didnl == 0) {
703                                 printf("\n");
704                                 didnl = 1;
705                         }
706                         show_bref(fd, tab, i, &bscan[i], dofreemap);
707                 }
708         }
709         tab -= SHOW_TAB;
710         if (obrace) {
711                 if (bref->type == HAMMER2_BREF_TYPE_INODE)
712                         tabprintf(tab, "} (%s.%d, \"%*.*s\")\n",
713                                   type_str, bi,
714                                   namelen, namelen, media.ipdata.filename);
715                 else
716                         tabprintf(tab, "} (%s.%d)\n", type_str,bi);
717         }
718 }
719
720 int
721 cmd_hash(int ac, const char **av)
722 {
723         int i;
724
725         for (i = 0; i < ac; ++i) {
726                 printf("%016jx %s\n", dirhash(av[i], strlen(av[i])), av[i]);
727         }
728         return(0);
729 }
730
731 int
732 cmd_chaindump(const char *path)
733 {
734         int dummy = 0;
735         int fd;
736
737         fd = open(path, O_RDONLY);
738         if (fd >= 0) {
739                 ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy);
740                 close(fd);
741         } else {
742                 fprintf(stderr, "unable to open %s\n", path);
743         }
744         return 0;
745 }
746
747
748 static
749 void
750 tabprintf(int tab, const char *ctl, ...)
751 {
752         va_list va;
753
754         printf("%*.*s", tab, tab, "");
755         va_start(va, ctl);
756         vprintf(ctl, va);
757         va_end(va);
758 }