hammer2 - refactor libdmsg volume configuration
[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_rcvmsg(dmsg_msg_t *msg);
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         struct dmsg_iocom iocom;
51         dmsg_msg_t *msg;
52         int fd;
53
54         /*
55          * Connect to the target
56          */
57         fd = dmsg_connect(hostname);
58         if (fd < 0)
59                 return 1;
60
61         /*
62          * Run the session.  The remote end transmits our prompt.
63          */
64         dmsg_iocom_init(&iocom, fd, 0,
65                         NULL,
66                         shell_rcvmsg,
67                         hammer2_shell_parse,
68                         shell_ttymsg);
69         fcntl(0, F_SETFL, O_NONBLOCK);
70         printf("debug: connected\n");
71
72         msg = dmsg_msg_alloc(&iocom.circuit0, 0, DMSG_DBG_SHELL, NULL, NULL);
73         dmsg_msg_write(msg);
74         dmsg_iocom_core(&iocom);
75         fprintf(stderr, "debug: disconnected\n");
76         close(fd);
77         return 0;
78 }
79
80 /*
81  * Callback from dmsg_iocom_core() when messages might be present
82  * on the socket.
83  */
84 static
85 void
86 shell_rcvmsg(dmsg_msg_t *msg)
87 {
88         switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
89         case DMSG_LNK_ERROR:
90         case DMSG_LNK_ERROR | DMSGF_REPLY:
91                 /*
92                  * One-way non-transactional LNK_ERROR messages typically
93                  * indicate a connection failure.  Error code 0 is used by
94                  * the debug shell to indicate no more results from last cmd.
95                  */
96                 if (msg->any.head.error) {
97                         fprintf(stderr, "Stream failure: %s\n",
98                                 dmsg_msg_str(msg));
99                 } else {
100                         write(1, "debug> ", 7);
101                 }
102                 break;
103         case DMSG_LNK_ERROR | DMSGF_DELETE:
104                 /* ignore termination of LNK_CONN */
105                 break;
106         case DMSG_DBG_SHELL:
107                 /*
108                  * We send the commands, not accept them.
109                  * (one-way message, not transactional)
110                  */
111                 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
112                 break;
113         case DMSG_DBG_SHELL | DMSGF_REPLY:
114                 /*
115                  * A reply from the remote is data we copy to stdout.
116                  * (one-way message, not transactional)
117                  */
118                 if (msg->aux_size) {
119                         msg->aux_data[msg->aux_size - 1] = 0;
120                         write(1, msg->aux_data, strlen(msg->aux_data));
121                 }
122                 break;
123         case DMSG_LNK_CONN | DMSGF_CREATE:
124                 fprintf(stderr, "Debug Shell is ignoring received LNK_CONN\n");
125                 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
126                 break;
127         case DMSG_LNK_CONN | DMSGF_DELETE:
128                 break;
129         default:
130                 /*
131                  * Ignore any unknown messages, Terminate any unknown
132                  * transactions with an error.
133                  */
134                 fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg));
135                 if (msg->any.head.cmd & DMSGF_CREATE)
136                         dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
137                 if (msg->any.head.cmd & DMSGF_DELETE)
138                         dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
139                 break;
140         }
141 }
142
143 static
144 void
145 shell_ttymsg(dmsg_iocom_t *iocom)
146 {
147         dmsg_msg_t *msg;
148         char buf[256];
149         size_t len;
150
151         if (fgets(buf, sizeof(buf), stdin) != NULL) {
152                 len = strlen(buf);
153                 if (len && buf[len - 1] == '\n')
154                         buf[--len] = 0;
155                 ++len;
156                 msg = dmsg_msg_alloc(&iocom->circuit0, len, DMSG_DBG_SHELL,
157                                      NULL, NULL);
158                 bcopy(buf, msg->aux_data, len);
159                 dmsg_msg_write(msg);
160         } else if (feof(stdin)) {
161                 /*
162                  * Set EOF flag without setting any error code for normal
163                  * EOF.
164                  */
165                 iocom->flags |= DMSG_IOCOMF_EOF;
166         } else {
167                 clearerr(stdin);
168         }
169 }
170
171 static void shell_span(dmsg_circuit_t *circuit, char *cmdbuf);
172 static void shell_circ(dmsg_circuit_t *circuit, char *cmdbuf);
173
174 void
175 hammer2_shell_parse(dmsg_msg_t *msg, int unmanaged)
176 {
177         dmsg_circuit_t *circuit;
178         char *cmdbuf;
179         char *cmdp;
180         uint32_t cmd;
181
182         /*
183          * Filter on debug shell commands only
184          */
185         cmd = msg->any.head.cmd;
186         if ((cmd & DMSGF_PROTOS) != DMSG_PROTO_DBG) {
187                 if (unmanaged)
188                         dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
189                 return;
190         }
191         if ((cmd & DMSGF_CMDSWMASK) != DMSG_DBG_SHELL) {
192                 if (unmanaged)
193                         dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
194                 return;
195         }
196
197         /*
198          * Debug shell command
199          */
200         circuit = msg->circuit;
201         cmdbuf = msg->aux_data;
202         cmdp = strsep(&cmdbuf, " \t");
203
204         if (cmdp == NULL || *cmdp == 0) {
205                 ;
206         } else if (strcmp(cmdp, "span") == 0) {
207                 shell_span(circuit, cmdbuf);
208         } else if (strcmp(cmdp, "circ") == 0) {
209                 shell_circ(circuit, cmdbuf);
210         } else if (strcmp(cmdp, "tree") == 0) {
211                 dmsg_shell_tree(circuit, cmdbuf); /* dump spanning tree */
212         } else if (strcmp(cmdp, "help") == 0 || strcmp(cmdp, "?") == 0) {
213                 dmsg_circuit_printf(circuit, "help            Command help\n");
214                 dmsg_circuit_printf(circuit, "span <host>     Span to target host\n");
215                 dmsg_circuit_printf(circuit, "circ <msgid>    Create VC to msgid of rx SPAN\n");
216                 dmsg_circuit_printf(circuit, "tree            Dump spanning tree\n");
217         } else {
218                 dmsg_circuit_printf(circuit, "Unrecognized command: %s\n", cmdp);
219         }
220 }
221
222 static void
223 shell_span(dmsg_circuit_t *circuit, char *cmdbuf)
224 {
225         dmsg_master_service_info_t *info;
226         const char *hostname = strsep(&cmdbuf, " \t");
227         pthread_t thread;
228         int fd;
229
230         /*
231          * Connect to the target
232          */
233         if (hostname == NULL) {
234                 fd = -1;
235         } else {
236                 fd = dmsg_connect(hostname);
237         }
238
239         /*
240          * Start master service
241          */
242         if (fd < 0) {
243                 dmsg_circuit_printf(circuit,
244                                     "Connection to %s failed\n",
245                                     hostname);
246         } else {
247                 dmsg_circuit_printf(circuit, "Connected to %s\n", hostname);
248
249                 info = malloc(sizeof(*info));
250                 bzero(info, sizeof(*info));
251                 info->fd = fd;
252                 info->detachme = 1;
253                 info->usrmsg_callback = hammer2_shell_parse;
254                 info->label = strdup("client");
255
256                 pthread_create(&thread, NULL, dmsg_master_service, info);
257                 /*pthread_join(thread, &res);*/
258         }
259 }
260
261 static void shell_circ_reply(dmsg_msg_t *msg);
262
263 static void
264 shell_circ(dmsg_circuit_t *circuit, char *cmdbuf)
265 {
266         uint64_t msgid = strtoull(cmdbuf, NULL, 16);
267         dmsg_state_t *state;
268         dmsg_msg_t *msg;
269
270         if (dmsg_debug_findspan(msgid, &state) == 0) {
271                 dmsg_circuit_printf(circuit, "Found state %p\n", state);
272
273                 dmsg_circuit_printf(circuit, "Establishing CIRC\n");
274                 msg = dmsg_msg_alloc(&state->iocom->circuit0, 0,
275                                      DMSG_LNK_CIRC | DMSGF_CREATE,
276                                      shell_circ_reply, circuit);
277                 msg->any.lnk_circ.target = state->msgid;
278                 dmsg_msg_write(msg);
279         } else {
280                 dmsg_circuit_printf(circuit,
281                                     "Unable to locate %016jx\n",
282                                     (intmax_t)msgid);
283         }
284 }
285
286 static void
287 shell_circ_reply(dmsg_msg_t *msg)
288 {
289         dmsg_circuit_t *circ = msg->state->any.circ;
290
291         if (msg->any.head.cmd & DMSGF_DELETE) {
292                 dmsg_circuit_printf(circ, "rxmsg DELETE error %d\n",
293                                     msg->any.head.error);
294                 msg->state->any.circ = NULL;
295         } else {
296                 dmsg_circuit_printf(circ, "rxmsg result error %d\n",
297                                     msg->any.head.error);
298         }
299 }
300
301 /************************************************************************
302  *                              DEBUGSPAN                               *
303  ************************************************************************
304  *
305  * Connect to the target manually (not via the cluster list embedded in
306  * a hammer2 filesystem) and initiate the SPAN protocol.
307  */
308 int
309 cmd_debugspan(const char *hostname)
310 {
311         pthread_t thread;
312         int fd;
313         void *res;
314
315         /*
316          * Connect to the target
317          */
318         fd = dmsg_connect(hostname);
319         if (fd < 0)
320                 return 1;
321
322         printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
323         pthread_create(&thread, NULL,
324                        dmsg_master_service, (void *)(intptr_t)fd);
325         pthread_join(thread, &res);
326         return(0);
327 }
328
329 /************************************************************************
330  *                                  SHOW                                *
331  ************************************************************************/
332
333 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref,
334                         int dofreemap);
335 static void tabprintf(int tab, const char *ctl, ...);
336
337 int
338 cmd_show(const char *devpath, int dofreemap)
339 {
340         hammer2_blockref_t broot;
341         hammer2_blockref_t best;
342         hammer2_media_data_t media;
343         int fd;
344         int i;
345         int best_i;
346
347         fd = open(devpath, O_RDONLY);
348         if (fd < 0) {
349                 perror("open");
350                 return 1;
351         }
352
353         /*
354          * Show the tree using the best volume header.
355          * -vvv will show the tree for all four volume headers.
356          */
357         best_i = -1;
358         bzero(&best, sizeof(best));
359         for (i = 0; i < 4; ++i) {
360                 bzero(&broot, sizeof(broot));
361                 broot.type = HAMMER2_BREF_TYPE_VOLUME;
362                 broot.data_off = (i * HAMMER2_ZONE_BYTES64) |
363                                  HAMMER2_PBUFRADIX;
364                 lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
365                 if (read(fd, &media, HAMMER2_PBUFSIZE) ==
366                     (ssize_t)HAMMER2_PBUFSIZE) {
367                         broot.mirror_tid = media.voldata.mirror_tid;
368                         if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
369                                 best_i = i;
370                                 best = broot;
371                         }
372                         if (VerboseOpt >= 3)
373                                 show_bref(fd, 0, i, &broot, dofreemap);
374                 }
375         }
376         if (VerboseOpt < 3)
377                 show_bref(fd, 0, best_i, &best, dofreemap);
378         close(fd);
379
380         return 0;
381 }
382
383 static void
384 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref, int dofreemap)
385 {
386         hammer2_media_data_t media;
387         hammer2_blockref_t *bscan;
388         int bcount;
389         int i;
390         int didnl;
391         int namelen;
392         int obrace = 1;
393         size_t bytes;
394         const char *type_str;
395         char *str = NULL;
396
397         switch(bref->type) {
398         case HAMMER2_BREF_TYPE_EMPTY:
399                 type_str = "empty";
400                 break;
401         case HAMMER2_BREF_TYPE_INODE:
402                 type_str = "inode";
403                 break;
404         case HAMMER2_BREF_TYPE_INDIRECT:
405                 type_str = "indblk";
406                 break;
407         case HAMMER2_BREF_TYPE_DATA:
408                 type_str = "data";
409                 break;
410         case HAMMER2_BREF_TYPE_VOLUME:
411                 type_str = "volume";
412                 break;
413         case HAMMER2_BREF_TYPE_FREEMAP:
414                 type_str = "freemap";
415                 break;
416         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
417                 type_str = "fmapnode";
418                 break;
419         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
420                 type_str = "fbitmap";
421                 break;
422         default:
423                 type_str = "unknown";
424                 break;
425         }
426
427         tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
428                type_str, bi, (intmax_t)bref->data_off,
429                (intmax_t)bref->key, (intmax_t)bref->keybits,
430                (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
431         tab += SHOW_TAB;
432
433         bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
434
435         {
436                 hammer2_off_t io_off;
437                 hammer2_off_t io_base;
438                 size_t io_bytes;
439                 size_t boff;
440
441                 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
442                 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
443                 io_bytes = bytes;
444                 boff = io_off - io_base;
445
446                 io_bytes = HAMMER2_MINIOSIZE;
447                 while (io_bytes + boff < bytes)
448                         io_bytes <<= 1;
449
450                 if (io_bytes > sizeof(media)) {
451                         printf("(bad block size %zd)\n", bytes);
452                         return;
453                 }
454                 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
455                         lseek(fd, io_base, 0);
456                         if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
457                                 printf("(media read failed)\n");
458                                 return;
459                         }
460                         if (boff)
461                                 bcopy((char *)&media + boff, &media, bytes);
462                 }
463         }
464
465         bscan = NULL;
466         bcount = 0;
467         didnl = 1;
468         namelen = 0;
469
470         switch(bref->type) {
471         case HAMMER2_BREF_TYPE_EMPTY:
472                 obrace = 0;
473                 break;
474         case HAMMER2_BREF_TYPE_INODE:
475                 printf("{\n");
476                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
477                         /* no blockrefs */
478                 } else {
479                         bscan = &media.ipdata.u.blockset.blockref[0];
480                         bcount = HAMMER2_SET_COUNT;
481                 }
482                 namelen = media.ipdata.name_len;
483                 if (namelen > HAMMER2_INODE_MAXNAME)
484                         namelen = 0;
485                 tabprintf(tab, "filename \"%*.*s\"\n",
486                           namelen, namelen, media.ipdata.filename);
487                 tabprintf(tab, "version  %d\n", media.ipdata.version);
488                 tabprintf(tab, "uflags   0x%08x\n",
489                           media.ipdata.uflags);
490                 if (media.ipdata.rmajor || media.ipdata.rminor) {
491                         tabprintf(tab, "rmajor   %d\n",
492                                   media.ipdata.rmajor);
493                         tabprintf(tab, "rminor   %d\n",
494                                   media.ipdata.rminor);
495                 }
496                 tabprintf(tab, "ctime    %s\n",
497                           hammer2_time64_to_str(media.ipdata.ctime, &str));
498                 tabprintf(tab, "mtime    %s\n",
499                           hammer2_time64_to_str(media.ipdata.mtime, &str));
500                 tabprintf(tab, "atime    %s\n",
501                           hammer2_time64_to_str(media.ipdata.atime, &str));
502                 tabprintf(tab, "btime    %s\n",
503                           hammer2_time64_to_str(media.ipdata.btime, &str));
504                 tabprintf(tab, "uid      %s\n",
505                           hammer2_uuid_to_str(&media.ipdata.uid, &str));
506                 tabprintf(tab, "gid      %s\n",
507                           hammer2_uuid_to_str(&media.ipdata.gid, &str));
508                 if (media.ipdata.type == HAMMER2_OBJTYPE_HARDLINK)
509                         tabprintf(tab, "type     %s (%s)\n",
510                               hammer2_iptype_to_str(media.ipdata.type),
511                               hammer2_iptype_to_str(media.ipdata.target_type));
512                 else
513                         tabprintf(tab, "type     %s\n",
514                                   hammer2_iptype_to_str(media.ipdata.type));
515                 tabprintf(tab, "opflgs   0x%02x\n",
516                           media.ipdata.op_flags);
517                 tabprintf(tab, "capflgs  0x%04x\n",
518                           media.ipdata.cap_flags);
519                 tabprintf(tab, "mode     %-7o\n",
520                           media.ipdata.mode);
521                 tabprintf(tab, "inum     0x%016jx\n",
522                           media.ipdata.inum);
523                 tabprintf(tab, "size     %ju\n",
524                           (uintmax_t)media.ipdata.size);
525                 tabprintf(tab, "nlinks   %ju\n",
526                           (uintmax_t)media.ipdata.nlinks);
527                 tabprintf(tab, "iparent  0x%016jx\n",
528                           (uintmax_t)media.ipdata.iparent);
529                 tabprintf(tab, "name_key 0x%016jx\n",
530                           (uintmax_t)media.ipdata.name_key);
531                 tabprintf(tab, "name_len %u\n",
532                           media.ipdata.name_len);
533                 tabprintf(tab, "ncopies  %u\n",
534                           media.ipdata.ncopies);
535                 tabprintf(tab, "compalg  %u\n",
536                           media.ipdata.comp_algo);
537                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
538                         tabprintf(tab, "pfs_type %u (%s)\n",
539                                   media.ipdata.pfs_type,
540                                   hammer2_pfstype_to_str(media.ipdata.pfs_type));
541                         tabprintf(tab, "pfs_inum 0x%016jx\n",
542                                   (uintmax_t)media.ipdata.pfs_inum);
543                         tabprintf(tab, "pfs_clid %s\n",
544                                   hammer2_uuid_to_str(&media.ipdata.pfs_clid,
545                                                       &str));
546                         tabprintf(tab, "pfs_fsid %s\n",
547                                   hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
548                                                       &str));
549                 }
550                 tabprintf(tab, "data_quota  %ju\n",
551                           (uintmax_t)media.ipdata.data_quota);
552                 tabprintf(tab, "data_count  %ju\n",
553                           (uintmax_t)media.ipdata.data_count);
554                 tabprintf(tab, "inode_quota %ju\n",
555                           (uintmax_t)media.ipdata.inode_quota);
556                 tabprintf(tab, "inode_count %ju\n",
557                           (uintmax_t)media.ipdata.inode_count);
558                 tabprintf(tab, "attr_tid    0x%016jx\n",
559                           (uintmax_t)media.ipdata.attr_tid);
560                 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
561                         tabprintf(tab, "dirent_tid  %016jx\n",
562                                   (uintmax_t)media.ipdata.dirent_tid);
563                 }
564                 break;
565         case HAMMER2_BREF_TYPE_INDIRECT:
566                 bscan = &media.npdata[0];
567                 bcount = bytes / sizeof(hammer2_blockref_t);
568                 didnl = 1;
569                 printf("{\n");
570                 break;
571         case HAMMER2_BREF_TYPE_DATA:
572                 if (VerboseOpt >= 2) {
573                         printf("{\n");
574                 } else {
575                         printf("\n");
576                         obrace = 0;
577                 }
578                 break;
579         case HAMMER2_BREF_TYPE_VOLUME:
580                 printf("mirror_tid=%016jx freemap_tid=%016jx ",
581                         media.voldata.mirror_tid,
582                         media.voldata.freemap_tid);
583                 if (dofreemap) {
584                         bscan = &media.voldata.freemap_blockset.blockref[0];
585                         bcount = HAMMER2_SET_COUNT;
586                 } else {
587                         bscan = &media.voldata.sroot_blockset.blockref[0];
588                         bcount = HAMMER2_SET_COUNT;
589                 }
590                 printf("{\n");
591                 break;
592         case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
593                 printf("{\n");
594                 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
595                         if (media.bmdata[i].class == 0 &&
596                             media.bmdata[i].avail == 0) {
597                                 continue;
598                         }
599                         tabprintf(tab + 4, "%04d.%04x (avail=%5d) "
600                                   "%08x %08x %08x %08x %08x %08x %08x %08x\n",
601                                   i, media.bmdata[i].class,
602                                   media.bmdata[i].avail,
603                                   media.bmdata[i].bitmap[0],
604                                   media.bmdata[i].bitmap[1],
605                                   media.bmdata[i].bitmap[2],
606                                   media.bmdata[i].bitmap[3],
607                                   media.bmdata[i].bitmap[4],
608                                   media.bmdata[i].bitmap[5],
609                                   media.bmdata[i].bitmap[6],
610                                   media.bmdata[i].bitmap[7]);
611                 }
612                 tabprintf(tab, "}\n");
613                 break;
614         case HAMMER2_BREF_TYPE_FREEMAP_NODE:
615                 printf("{\n");
616                 bscan = &media.npdata[0];
617                 bcount = bytes / sizeof(hammer2_blockref_t);
618                 break;
619         default:
620                 printf("\n");
621                 obrace = 0;
622                 break;
623         }
624         if (str)
625                 free(str);
626         for (i = 0; i < bcount; ++i) {
627                 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
628                         if (didnl == 0) {
629                                 printf("\n");
630                                 didnl = 1;
631                         }
632                         show_bref(fd, tab, i, &bscan[i], dofreemap);
633                 }
634         }
635         tab -= SHOW_TAB;
636         if (obrace) {
637                 if (bref->type == HAMMER2_BREF_TYPE_INODE)
638                         tabprintf(tab, "} (%s.%d, \"%*.*s\")\n",
639                                   type_str, bi,
640                                   namelen, namelen, media.ipdata.filename);
641                 else
642                         tabprintf(tab, "} (%s.%d)\n", type_str,bi);
643         }
644 }
645
646 int
647 cmd_hash(int ac, const char **av)
648 {
649         int i;
650
651         for (i = 0; i < ac; ++i) {
652                 printf("%016jx %s\n", dirhash(av[i], strlen(av[i])), av[i]);
653         }
654         return(0);
655 }
656
657 int
658 cmd_chaindump(const char *path)
659 {
660         int dummy = 0;
661         int fd;
662
663         fd = open(path, O_RDONLY);
664         if (fd >= 0) {
665                 ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy);
666                 close(fd);
667         } else {
668                 fprintf(stderr, "unable to open %s\n", path);
669         }
670         return 0;
671 }
672
673
674 static
675 void
676 tabprintf(int tab, const char *ctl, ...)
677 {
678         va_list va;
679
680         printf("%*.*s", tab, tab, "");
681         va_start(va, ctl);
682         vprintf(ctl, va);
683         va_end(va);
684 }