hammer2 - limit show command output when crc error
[dragonfly.git] / sbin / hammer2 / cmd_debug.c
blob3fccc83037be5e1acfd62a3318898d83ccad79b1
1 /*
2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
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.
36 #include "hammer2.h"
38 #define SHOW_TAB 2
40 static void shell_msghandler(dmsg_msg_t *msg, int unmanaged);
41 static void shell_ttymsg(dmsg_iocom_t *iocom);
43 /************************************************************************
44 * SHELL *
45 ************************************************************************/
47 int
48 cmd_shell(const char *hostname)
50 dmsg_master_service_info_t *info;
51 pthread_t thread;
52 int fd;
54 fd = dmsg_connect(hostname);
55 if (fd < 0)
56 return 1;
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);
68 return 0;
71 #if 0
72 int
73 cmd_shell(const char *hostname)
75 struct dmsg_iocom iocom;
76 dmsg_msg_t *msg;
77 int fd;
80 * Connect to the target
82 fd = dmsg_connect(hostname);
83 if (fd < 0)
84 return 1;
87 * Initialize the session and transmit an empty DMSG_DBG_SHELL
88 * to cause the remote end to generate a prompt.
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");
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;
105 #endif
108 * Debug session front-end
110 * Callback from dmsg_iocom_core() when messages might be present
111 * on the socket.
113 static
114 void
115 shell_msghandler(dmsg_msg_t *msg, int unmanaged)
117 dmsg_msg_t *nmsg;
119 switch(msg->tcmd) {
120 #if 0
121 case DMSG_LNK_ERROR:
122 case DMSG_LNK_ERROR | DMSGF_REPLY:
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.
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);
134 break;
135 case DMSG_LNK_ERROR | DMSGF_DELETE:
136 /* ignore termination of LNK_CONN */
137 break;
138 #endif
139 case DMSG_DBG_SHELL:
141 * We send the commands, not accept them.
142 * (one-way message, not transactional)
144 if (unmanaged)
145 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
146 break;
147 case DMSG_DBG_SHELL | DMSGF_REPLY:
149 * A reply from the remote is data we copy to stdout.
150 * (one-way message, not transactional)
152 if (msg->aux_size) {
153 msg->aux_data[msg->aux_size - 1] = 0;
154 write(1, msg->aux_data, strlen(msg->aux_data));
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:
171 * Ignore any unknown messages, Terminate any unknown
172 * transactions with an error.
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);
181 break;
186 * Debug session front-end
188 static
189 void
190 shell_ttymsg(dmsg_iocom_t *iocom)
192 dmsg_state_t *pstate;
193 dmsg_msg_t *msg;
194 char buf[256];
195 char *cmd;
196 size_t len;
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");
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 {
216 * This should cause the remote end to generate
217 * a debug> prompt (and thus shows that there is
218 * connectivity).
220 msg = dmsg_msg_alloc(pstate, 0, DMSG_DBG_SHELL,
221 NULL, NULL);
222 dmsg_msg_write(msg);
224 } else if (feof(stdin)) {
226 * Set EOF flag without setting any error code for normal
227 * EOF.
229 iocom->flags |= DMSG_IOCOMF_EOF;
230 } else {
231 clearerr(stdin);
236 * Debug session back-end (on remote side)
238 static void shell_span(dmsg_msg_t *msg, char *cmdbuf);
239 static void shell_ping(dmsg_msg_t *msg, char *cmdbuf);
241 void
242 hammer2_shell_parse(dmsg_msg_t *msg, int unmanaged)
244 dmsg_iocom_t *iocom = msg->state->iocom;
245 char *cmdbuf;
246 char *cmdp;
247 uint32_t cmd;
250 * Filter on debug shell commands and ping responses only
252 cmd = msg->any.head.cmd;
253 if ((cmd & DMSGF_CMDSWMASK) == (DMSG_LNK_PING | DMSGF_REPLY)) {
254 dmsg_printf(iocom, "ping reply\n");
255 return;
258 if ((cmd & DMSGF_PROTOS) != DMSG_PROTO_DBG) {
259 if (unmanaged)
260 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
261 return;
263 if ((cmd & DMSGF_CMDSWMASK) != DMSG_DBG_SHELL) {
264 if (unmanaged)
265 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
266 return;
270 * Debug shell command
272 cmdbuf = msg->aux_data;
273 cmdp = strsep(&cmdbuf, " \t");
275 if (cmdp == NULL || *cmdp == 0) {
277 } else if (strcmp(cmdp, "ping") == 0) {
278 shell_ping(msg, cmdbuf);
279 } else if (strcmp(cmdp, "span") == 0) {
280 shell_span(msg, cmdbuf);
281 } else if (strcmp(cmdp, "tree") == 0) {
282 dmsg_shell_tree(iocom, cmdbuf); /* dump spanning tree */
283 } else if (strcmp(cmdp, "help") == 0 || strcmp(cmdp, "?") == 0) {
284 dmsg_printf(iocom, "help Command help\n");
285 dmsg_printf(iocom, "span <host> Span to target host\n");
286 dmsg_printf(iocom, "tree Dump spanning tree\n");
287 dmsg_printf(iocom, "@span <cmd> Issue via circuit\n");
288 } else {
289 dmsg_printf(iocom, "Unrecognized command: %s\n", cmdp);
291 dmsg_printf(iocom, "debug> ");
294 static void
295 shell_ping(dmsg_msg_t *msg, char *cmdbuf __unused)
297 dmsg_iocom_t *iocom = msg->state->iocom;
298 dmsg_msg_t *m2;
300 dmsg_printf(iocom, "sending ping\n");
301 m2 = dmsg_msg_alloc(msg->state, 0, DMSG_LNK_PING, NULL, NULL);
302 dmsg_msg_write(m2);
305 static void
306 shell_span(dmsg_msg_t *msg, char *cmdbuf)
308 dmsg_iocom_t *iocom = msg->state->iocom;
309 dmsg_master_service_info_t *info;
310 const char *hostname = strsep(&cmdbuf, " \t");
311 pthread_t thread;
312 int fd;
315 * Connect to the target
317 if (hostname == NULL) {
318 fd = -1;
319 } else {
320 fd = dmsg_connect(hostname);
324 * Start master service
326 if (fd < 0) {
327 dmsg_printf(iocom, "Connection to %s failed\n", hostname);
328 } else {
329 dmsg_printf(iocom, "Connected to %s\n", hostname);
331 info = malloc(sizeof(*info));
332 bzero(info, sizeof(*info));
333 info->fd = fd;
334 info->detachme = 1;
335 info->usrmsg_callback = hammer2_shell_parse;
336 info->label = strdup("client");
338 pthread_create(&thread, NULL, dmsg_master_service, info);
339 /*pthread_join(thread, &res);*/
343 /************************************************************************
344 * DEBUGSPAN *
345 ************************************************************************
347 * Connect to the target manually (not via the cluster list embedded in
348 * a hammer2 filesystem) and initiate the SPAN protocol.
351 cmd_debugspan(const char *hostname)
353 pthread_t thread;
354 int fd;
355 void *res;
358 * Connect to the target
360 fd = dmsg_connect(hostname);
361 if (fd < 0)
362 return 1;
364 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
365 pthread_create(&thread, NULL,
366 dmsg_master_service, (void *)(intptr_t)fd);
367 pthread_join(thread, &res);
368 return(0);
371 /************************************************************************
372 * SHOW *
373 ************************************************************************/
375 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref,
376 int dofreemap, int norecurse);
377 static void tabprintf(int tab, const char *ctl, ...);
380 cmd_show(const char *devpath, int dofreemap)
382 hammer2_blockref_t broot;
383 hammer2_blockref_t best;
384 hammer2_media_data_t media;
385 int fd;
386 int i;
387 int best_i;
389 fd = open(devpath, O_RDONLY);
390 if (fd < 0) {
391 perror("open");
392 return 1;
396 * Show the tree using the best volume header.
397 * -vvv will show the tree for all four volume headers.
399 best_i = -1;
400 bzero(&best, sizeof(best));
401 for (i = 0; i < 4; ++i) {
402 bzero(&broot, sizeof(broot));
403 broot.type = HAMMER2_BREF_TYPE_VOLUME;
404 broot.data_off = (i * HAMMER2_ZONE_BYTES64) |
405 HAMMER2_PBUFRADIX;
406 lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
407 if (read(fd, &media, HAMMER2_PBUFSIZE) ==
408 (ssize_t)HAMMER2_PBUFSIZE) {
409 broot.mirror_tid = media.voldata.mirror_tid;
410 if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
411 best_i = i;
412 best = broot;
414 if (VerboseOpt >= 3)
415 show_bref(fd, 0, i, &broot, dofreemap, 0);
418 if (VerboseOpt < 3)
419 show_bref(fd, 0, best_i, &best, dofreemap, 0);
420 close(fd);
422 return 0;
425 extern uint32_t iscsi_crc32(const void *buf, size_t size);
426 static void
427 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref, int dofreemap,
428 int norecurse)
430 hammer2_media_data_t media;
431 hammer2_blockref_t *bscan;
432 int bcount;
433 int i;
434 int didnl;
435 int namelen;
436 int obrace = 1;
437 int failed;
438 size_t bytes;
439 const char *type_str;
440 char *str = NULL;
441 uint32_t cv;
442 uint64_t cv64;
444 switch(bref->type) {
445 case HAMMER2_BREF_TYPE_EMPTY:
446 type_str = "empty";
447 break;
448 case HAMMER2_BREF_TYPE_DIRENT:
449 type_str = "dirent";
450 break;
451 case HAMMER2_BREF_TYPE_INODE:
452 type_str = "inode";
453 break;
454 case HAMMER2_BREF_TYPE_INDIRECT:
455 type_str = "indblk";
456 break;
457 case HAMMER2_BREF_TYPE_DATA:
458 type_str = "data";
459 break;
460 case HAMMER2_BREF_TYPE_VOLUME:
461 type_str = "volume";
462 break;
463 case HAMMER2_BREF_TYPE_FREEMAP:
464 type_str = "freemap";
465 break;
466 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
467 type_str = "fmapnode";
468 break;
469 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
470 type_str = "fbitmap";
471 break;
472 default:
473 type_str = "unknown";
474 break;
477 tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
478 type_str, bi, (intmax_t)bref->data_off,
479 (intmax_t)bref->key, (intmax_t)bref->keybits,
480 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
481 tab += SHOW_TAB;
482 if (bref->flags)
483 printf("flags=%02x ", bref->flags);
485 bytes = (bref->data_off & HAMMER2_OFF_MASK_RADIX);
486 if (bytes)
487 bytes = (size_t)1 << bytes;
488 if (bytes) {
489 hammer2_off_t io_off;
490 hammer2_off_t io_base;
491 size_t io_bytes;
492 size_t boff;
494 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
495 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
496 io_bytes = bytes;
497 boff = io_off - io_base;
499 io_bytes = HAMMER2_MINIOSIZE;
500 while (io_bytes + boff < bytes)
501 io_bytes <<= 1;
503 if (io_bytes > sizeof(media)) {
504 printf("(bad block size %zd)\n", bytes);
505 return;
507 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
508 lseek(fd, io_base, 0);
509 if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
510 printf("(media read failed)\n");
511 return;
513 if (boff)
514 bcopy((char *)&media + boff, &media, bytes);
518 bscan = NULL;
519 bcount = 0;
520 didnl = 1;
521 namelen = 0;
522 failed = 0;
525 * Check data integrity in verbose mode, otherwise we are just doing
526 * a quick meta-data scan. Meta-data integrity is always checked.
527 * (Also see the check above that ensures the media data is loaded,
528 * otherwise there's no data to check!).
530 * WARNING! bref->check state may be used for other things when
531 * bref has no data (bytes == 0).
533 if (bytes &&
534 (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1)) {
535 switch(HAMMER2_DEC_CHECK(bref->methods)) {
536 case HAMMER2_CHECK_NONE:
537 printf("(meth %02x) ", bref->methods);
538 break;
539 case HAMMER2_CHECK_DISABLED:
540 printf("(meth %02x) ", bref->methods);
541 break;
542 case HAMMER2_CHECK_ISCSI32:
543 cv = hammer2_icrc32(&media, bytes);
544 if (bref->check.iscsi32.value != cv) {
545 printf("(icrc %02x:%08x/%08x failed) ",
546 bref->methods,
547 bref->check.iscsi32.value,
548 cv);
549 failed = 1;
550 } else {
551 printf("(meth %02x, iscsi32=%08x) ",
552 bref->methods, cv);
554 break;
555 case HAMMER2_CHECK_XXHASH64:
556 cv64 = XXH64(&media, bytes, XXH_HAMMER2_SEED);
557 if (bref->check.xxhash64.value != cv64) {
558 printf("(xxhash64 %02x:%016jx/%016jx failed) ",
559 bref->methods,
560 bref->check.xxhash64.value,
561 cv64);
562 failed = 1;
563 } else {
564 printf("(meth %02x, xxh=%016jx) ",
565 bref->methods, cv64);
567 break;
568 case HAMMER2_CHECK_SHA192:
569 printf("(meth %02x) ", bref->methods);
570 break;
571 case HAMMER2_CHECK_FREEMAP:
572 cv = hammer2_icrc32(&media, bytes);
573 if (bref->check.freemap.icrc32 != cv) {
574 printf("(fcrc %02x:%08x/%08x failed) ",
575 bref->methods,
576 bref->check.freemap.icrc32,
577 cv);
578 failed = 1;
579 } else {
580 printf("(meth %02x, fcrc=%08x) ",
581 bref->methods, cv);
583 break;
587 switch(bref->type) {
588 case HAMMER2_BREF_TYPE_EMPTY:
589 obrace = 0;
590 break;
591 case HAMMER2_BREF_TYPE_DIRENT:
592 printf("{\n");
593 if (bref->embed.dirent.namlen <= sizeof(bref->check.buf)) {
594 tabprintf(tab, "filename \"%*.*s\"\n",
595 bref->embed.dirent.namlen,
596 bref->embed.dirent.namlen,
597 bref->check.buf);
598 } else {
599 tabprintf(tab, "filename \"%*.*s\"\n",
600 bref->embed.dirent.namlen,
601 bref->embed.dirent.namlen,
602 media.buf);
604 tabprintf(tab, "inum 0x%016jx\n",
605 (uintmax_t)bref->embed.dirent.inum);
606 tabprintf(tab, "type %s\n",
607 hammer2_iptype_to_str(bref->embed.dirent.type));
608 break;
609 case HAMMER2_BREF_TYPE_INODE:
610 printf("{\n");
611 if (media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
612 /* no blockrefs */
613 } else {
614 bscan = &media.ipdata.u.blockset.blockref[0];
615 bcount = HAMMER2_SET_COUNT;
617 namelen = media.ipdata.meta.name_len;
618 if (namelen > HAMMER2_INODE_MAXNAME)
619 namelen = 0;
620 tabprintf(tab, "filename \"%*.*s\"\n",
621 namelen, namelen, media.ipdata.filename);
622 tabprintf(tab, "version %d\n", media.ipdata.meta.version);
623 tabprintf(tab, "uflags 0x%08x\n",
624 media.ipdata.meta.uflags);
625 if (media.ipdata.meta.rmajor || media.ipdata.meta.rminor) {
626 tabprintf(tab, "rmajor %d\n",
627 media.ipdata.meta.rmajor);
628 tabprintf(tab, "rminor %d\n",
629 media.ipdata.meta.rminor);
631 tabprintf(tab, "ctime %s\n",
632 hammer2_time64_to_str(media.ipdata.meta.ctime, &str));
633 tabprintf(tab, "mtime %s\n",
634 hammer2_time64_to_str(media.ipdata.meta.mtime, &str));
635 tabprintf(tab, "atime %s\n",
636 hammer2_time64_to_str(media.ipdata.meta.atime, &str));
637 tabprintf(tab, "btime %s\n",
638 hammer2_time64_to_str(media.ipdata.meta.btime, &str));
639 tabprintf(tab, "uid %s\n",
640 hammer2_uuid_to_str(&media.ipdata.meta.uid, &str));
641 tabprintf(tab, "gid %s\n",
642 hammer2_uuid_to_str(&media.ipdata.meta.gid, &str));
643 tabprintf(tab, "type %s\n",
644 hammer2_iptype_to_str(media.ipdata.meta.type));
645 tabprintf(tab, "opflgs 0x%02x\n",
646 media.ipdata.meta.op_flags);
647 tabprintf(tab, "capflgs 0x%04x\n",
648 media.ipdata.meta.cap_flags);
649 tabprintf(tab, "mode %-7o\n",
650 media.ipdata.meta.mode);
651 tabprintf(tab, "inum 0x%016jx\n",
652 media.ipdata.meta.inum);
653 tabprintf(tab, "size %ju\n",
654 (uintmax_t)media.ipdata.meta.size);
655 tabprintf(tab, "nlinks %ju\n",
656 (uintmax_t)media.ipdata.meta.nlinks);
657 tabprintf(tab, "iparent 0x%016jx\n",
658 (uintmax_t)media.ipdata.meta.iparent);
659 tabprintf(tab, "name_key 0x%016jx\n",
660 (uintmax_t)media.ipdata.meta.name_key);
661 tabprintf(tab, "name_len %u\n",
662 media.ipdata.meta.name_len);
663 tabprintf(tab, "ncopies %u\n",
664 media.ipdata.meta.ncopies);
665 tabprintf(tab, "compalg %u\n",
666 media.ipdata.meta.comp_algo);
667 tabprintf(tab, "checkalg %u\n",
668 media.ipdata.meta.check_algo);
669 if ((media.ipdata.meta.op_flags & HAMMER2_OPFLAG_PFSROOT) ||
670 media.ipdata.meta.pfs_type == HAMMER2_PFSTYPE_SUPROOT) {
671 tabprintf(tab, "pfs_type %u (%s)\n",
672 media.ipdata.meta.pfs_type,
673 hammer2_pfstype_to_str(media.ipdata.meta.pfs_type));
674 tabprintf(tab, "pfs_inum 0x%016jx\n",
675 (uintmax_t)media.ipdata.meta.pfs_inum);
676 tabprintf(tab, "pfs_clid %s\n",
677 hammer2_uuid_to_str(&media.ipdata.meta.pfs_clid,
678 &str));
679 tabprintf(tab, "pfs_fsid %s\n",
680 hammer2_uuid_to_str(&media.ipdata.meta.pfs_fsid,
681 &str));
682 tabprintf(tab, "pfs_lsnap_tid 0x%016jx\n",
683 (uintmax_t)media.ipdata.meta.pfs_lsnap_tid);
685 tabprintf(tab, "data_quota %ju\n",
686 (uintmax_t)media.ipdata.meta.data_quota);
687 tabprintf(tab, "data_count %ju\n",
688 (uintmax_t)bref->embed.stats.data_count);
689 tabprintf(tab, "inode_quota %ju\n",
690 (uintmax_t)media.ipdata.meta.inode_quota);
691 tabprintf(tab, "inode_count %ju\n",
692 (uintmax_t)bref->embed.stats.inode_count);
693 break;
694 case HAMMER2_BREF_TYPE_INDIRECT:
695 bscan = &media.npdata[0];
696 bcount = bytes / sizeof(hammer2_blockref_t);
697 didnl = 1;
698 printf("{\n");
699 break;
700 case HAMMER2_BREF_TYPE_DATA:
701 #if 0
702 if (VerboseOpt >= 2) {
703 printf("{\n");
704 } else
705 #endif
707 printf("\n");
708 obrace = 0;
710 break;
711 case HAMMER2_BREF_TYPE_VOLUME:
712 printf("mirror_tid=%016jx freemap_tid=%016jx ",
713 media.voldata.mirror_tid,
714 media.voldata.freemap_tid);
715 if (dofreemap) {
716 bscan = &media.voldata.freemap_blockset.blockref[0];
717 bcount = HAMMER2_SET_COUNT;
718 } else {
719 bscan = &media.voldata.sroot_blockset.blockref[0];
720 bcount = HAMMER2_SET_COUNT;
722 printf("{\n");
723 break;
724 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
725 printf("{\n");
726 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
728 if (media.bmdata[i].class == 0 &&
729 media.bmdata[i].avail == 0) {
730 continue;
733 #if HAMMER2_BMAP_ELEMENTS != 8
734 #error "cmd_debug.c: HAMMER2_BMAP_ELEMENTS expected to be 8"
735 #endif
737 tabprintf(tab + 4, "%016jx %04d.%04x (avail=%7d) "
738 "%016jx %016jx %016jx %016jx "
739 "%016jx %016jx %016jx %016jx\n",
740 bref->key +
741 i * 256 * HAMMER2_FREEMAP_BLOCK_SIZE,
742 i, media.bmdata[i].class,
743 media.bmdata[i].avail,
744 media.bmdata[i].bitmapq[0],
745 media.bmdata[i].bitmapq[1],
746 media.bmdata[i].bitmapq[2],
747 media.bmdata[i].bitmapq[3],
748 media.bmdata[i].bitmapq[4],
749 media.bmdata[i].bitmapq[5],
750 media.bmdata[i].bitmapq[6],
751 media.bmdata[i].bitmapq[7]);
753 tabprintf(tab, "}\n");
754 break;
755 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
756 printf("{\n");
757 bscan = &media.npdata[0];
758 bcount = bytes / sizeof(hammer2_blockref_t);
759 break;
760 default:
761 printf("\n");
762 obrace = 0;
763 break;
765 if (str)
766 free(str);
769 * Recurse if norecurse == 0. If the CRC failed, pass norecurse = 1.
770 * That is, if an indirect or inode fails we still try to list its
771 * direct children to help with debugging, but go no further than
772 * that because they are probably garbage.
774 for (i = 0; norecurse == 0 && i < bcount; ++i) {
775 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
776 if (didnl == 0) {
777 printf("\n");
778 didnl = 1;
780 show_bref(fd, tab, i, &bscan[i], dofreemap, failed);
783 tab -= SHOW_TAB;
784 if (obrace) {
785 if (bref->type == HAMMER2_BREF_TYPE_INODE)
786 tabprintf(tab, "} (%s.%d, \"%*.*s\")\n",
787 type_str, bi,
788 namelen, namelen, media.ipdata.filename);
789 else
790 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
795 cmd_hash(int ac, const char **av)
797 int i;
799 for (i = 0; i < ac; ++i) {
800 printf("%016jx %s\n", dirhash(av[i], strlen(av[i])), av[i]);
802 return(0);
806 cmd_chaindump(const char *path)
808 int dummy = 0;
809 int fd;
811 fd = open(path, O_RDONLY);
812 if (fd >= 0) {
813 ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy);
814 close(fd);
815 } else {
816 fprintf(stderr, "unable to open %s\n", path);
818 return 0;
822 static
823 void
824 tabprintf(int tab, const char *ctl, ...)
826 va_list va;
828 printf("%*.*s", tab, tab, "");
829 va_start(va, ctl);
830 vprintf(ctl, va);
831 va_end(va);