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
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
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
40 static void shell_msghandler(dmsg_msg_t *msg, int unmanaged);
41 static void shell_ttymsg(dmsg_iocom_t *iocom);
43 /************************************************************************
45 ************************************************************************/
48 cmd_shell(const char *hostname)
50 dmsg_master_service_info_t *info;
54 fd = dmsg_connect(hostname);
58 info = malloc(sizeof(*info));
59 bzero(info, sizeof(*info));
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);
73 cmd_shell(const char *hostname)
75 struct dmsg_iocom iocom;
80 * Connect to the target
82 fd = dmsg_connect(hostname);
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,
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);
100 dmsg_iocom_core(&iocom);
101 fprintf(stderr, "debug: disconnected\n");
108 * Debug session front-end
110 * Callback from dmsg_iocom_core() when messages might be present
115 shell_msghandler(dmsg_msg_t *msg, int unmanaged)
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",
132 write(1, "debug> ", 7);
135 case DMSG_LNK_ERROR | DMSGF_DELETE:
136 /* ignore termination of LNK_CONN */
141 * We send the commands, not accept them.
142 * (one-way message, not transactional)
145 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
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)
153 msg->aux_data[msg->aux_size - 1] = 0;
154 write(1, msg->aux_data, strlen(msg->aux_data));
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,
163 dmsg_msg_write(nmsg);
164 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
166 case DMSG_LNK_CONN | DMSGF_DELETE:
171 * Ignore any unknown messages, Terminate any unknown
172 * transactions with an error.
174 fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg));
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);
186 * Debug session front-end
190 shell_ttymsg(dmsg_iocom_t *iocom)
192 dmsg_state_t *pstate;
198 if (fgets(buf, sizeof(buf), stdin) != NULL) {
200 pstate = dmsg_findspan(strtok(buf + 1, " \t\n"));
201 cmd = strtok(NULL, "\n");
203 pstate = &iocom->state0;
204 cmd = strtok(buf, "\n");
207 len = strlen(cmd) + 1;
208 msg = dmsg_msg_alloc(pstate, len, DMSG_DBG_SHELL,
210 bcopy(cmd, msg->aux_data, len);
213 fprintf(stderr, "@msgid not found\n");
216 * This should cause the remote end to generate
217 * a debug> prompt (and thus shows that there is
220 msg = dmsg_msg_alloc(pstate, 0, DMSG_DBG_SHELL,
224 } else if (feof(stdin)) {
226 * Set EOF flag without setting any error code for normal
229 iocom->flags |= DMSG_IOCOMF_EOF;
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);
242 hammer2_shell_parse(dmsg_msg_t *msg, int unmanaged)
244 dmsg_iocom_t *iocom = msg->state->iocom;
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");
258 if ((cmd & DMSGF_PROTOS) != DMSG_PROTO_DBG) {
260 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
263 if ((cmd & DMSGF_CMDSWMASK) != DMSG_DBG_SHELL) {
265 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
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");
289 dmsg_printf(iocom, "Unrecognized command: %s\n", cmdp);
291 dmsg_printf(iocom, "debug> ");
295 shell_ping(dmsg_msg_t *msg, char *cmdbuf __unused)
297 dmsg_iocom_t *iocom = msg->state->iocom;
300 dmsg_printf(iocom, "sending ping\n");
301 m2 = dmsg_msg_alloc(msg->state, 0, DMSG_LNK_PING, NULL, NULL);
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");
315 * Connect to the target
317 if (hostname == NULL) {
320 fd = dmsg_connect(hostname);
324 * Start master service
327 dmsg_printf(iocom, "Connection to %s failed\n", hostname);
329 dmsg_printf(iocom, "Connected to %s\n", hostname);
331 info = malloc(sizeof(*info));
332 bzero(info, sizeof(*info));
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 /************************************************************************
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)
358 * Connect to the target
360 fd = dmsg_connect(hostname);
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);
371 /************************************************************************
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;
389 fd = open(devpath, O_RDONLY);
396 * Show the tree using the best volume header.
397 * -vvv will show the tree for all four volume headers.
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) |
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) {
415 show_bref(fd, 0, i, &broot, dofreemap, 0);
419 show_bref(fd, 0, best_i, &best, dofreemap, 0);
425 extern uint32_t iscsi_crc32(const void *buf, size_t size);
427 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref, int dofreemap,
430 hammer2_media_data_t media;
431 hammer2_blockref_t *bscan;
439 const char *type_str;
445 case HAMMER2_BREF_TYPE_EMPTY:
448 case HAMMER2_BREF_TYPE_DIRENT:
451 case HAMMER2_BREF_TYPE_INODE:
454 case HAMMER2_BREF_TYPE_INDIRECT:
457 case HAMMER2_BREF_TYPE_DATA:
460 case HAMMER2_BREF_TYPE_VOLUME:
463 case HAMMER2_BREF_TYPE_FREEMAP:
464 type_str = "freemap";
466 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
467 type_str = "fmapnode";
469 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
470 type_str = "fbitmap";
473 type_str = "unknown";
478 "%s.%-3d %016jx %016jx/%-2d "
479 "mir=%016jx mod=%016jx leafcnt=%d ",
480 type_str, bi, (intmax_t)bref->data_off,
481 (intmax_t)bref->key, (intmax_t)bref->keybits,
482 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid,
486 printf("flags=%02x ", bref->flags);
487 if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE ||
488 bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) {
489 printf("bigmask=%08x avail=%ld ",
490 bref->check.freemap.bigmask, bref->check.freemap.avail);
493 bytes = (bref->data_off & HAMMER2_OFF_MASK_RADIX);
495 bytes = (size_t)1 << bytes;
497 hammer2_off_t io_off;
498 hammer2_off_t io_base;
502 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
503 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
505 boff = io_off - io_base;
507 io_bytes = HAMMER2_MINIOSIZE;
508 while (io_bytes + boff < bytes)
511 if (io_bytes > sizeof(media)) {
512 printf("(bad block size %zd)\n", bytes);
515 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
516 lseek(fd, io_base, 0);
517 if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
518 printf("(media read failed)\n");
522 bcopy((char *)&media + boff, &media, bytes);
533 * Check data integrity in verbose mode, otherwise we are just doing
534 * a quick meta-data scan. Meta-data integrity is always checked.
535 * (Also see the check above that ensures the media data is loaded,
536 * otherwise there's no data to check!).
538 * WARNING! bref->check state may be used for other things when
539 * bref has no data (bytes == 0).
542 (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1)) {
543 switch(HAMMER2_DEC_CHECK(bref->methods)) {
544 case HAMMER2_CHECK_NONE:
545 printf("(meth %02x) ", bref->methods);
547 case HAMMER2_CHECK_DISABLED:
548 printf("(meth %02x) ", bref->methods);
550 case HAMMER2_CHECK_ISCSI32:
551 cv = hammer2_icrc32(&media, bytes);
552 if (bref->check.iscsi32.value != cv) {
553 printf("(icrc %02x:%08x/%08x failed) ",
555 bref->check.iscsi32.value,
559 printf("(meth %02x, iscsi32=%08x) ",
563 case HAMMER2_CHECK_XXHASH64:
564 cv64 = XXH64(&media, bytes, XXH_HAMMER2_SEED);
565 if (bref->check.xxhash64.value != cv64) {
566 printf("(xxhash64 %02x:%016jx/%016jx failed) ",
568 bref->check.xxhash64.value,
572 printf("(meth %02x, xxh=%016jx) ",
573 bref->methods, cv64);
576 case HAMMER2_CHECK_SHA192:
577 printf("(meth %02x) ", bref->methods);
579 case HAMMER2_CHECK_FREEMAP:
580 cv = hammer2_icrc32(&media, bytes);
581 if (bref->check.freemap.icrc32 != cv) {
582 printf("(fcrc %02x:%08x/%08x failed) ",
584 bref->check.freemap.icrc32,
588 printf("(meth %02x, fcrc=%08x) ",
596 case HAMMER2_BREF_TYPE_EMPTY:
599 case HAMMER2_BREF_TYPE_DIRENT:
601 if (bref->embed.dirent.namlen <= sizeof(bref->check.buf)) {
602 tabprintf(tab, "filename \"%*.*s\"\n",
603 bref->embed.dirent.namlen,
604 bref->embed.dirent.namlen,
607 tabprintf(tab, "filename \"%*.*s\"\n",
608 bref->embed.dirent.namlen,
609 bref->embed.dirent.namlen,
612 tabprintf(tab, "inum 0x%016jx\n",
613 (uintmax_t)bref->embed.dirent.inum);
614 tabprintf(tab, "type %s\n",
615 hammer2_iptype_to_str(bref->embed.dirent.type));
617 case HAMMER2_BREF_TYPE_INODE:
619 if (media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
622 bscan = &media.ipdata.u.blockset.blockref[0];
623 bcount = HAMMER2_SET_COUNT;
625 namelen = media.ipdata.meta.name_len;
626 if (namelen > HAMMER2_INODE_MAXNAME)
628 tabprintf(tab, "filename \"%*.*s\"\n",
629 namelen, namelen, media.ipdata.filename);
630 tabprintf(tab, "version %d\n", media.ipdata.meta.version);
631 tabprintf(tab, "uflags 0x%08x\n",
632 media.ipdata.meta.uflags);
633 if (media.ipdata.meta.rmajor || media.ipdata.meta.rminor) {
634 tabprintf(tab, "rmajor %d\n",
635 media.ipdata.meta.rmajor);
636 tabprintf(tab, "rminor %d\n",
637 media.ipdata.meta.rminor);
639 tabprintf(tab, "ctime %s\n",
640 hammer2_time64_to_str(media.ipdata.meta.ctime, &str));
641 tabprintf(tab, "mtime %s\n",
642 hammer2_time64_to_str(media.ipdata.meta.mtime, &str));
643 tabprintf(tab, "atime %s\n",
644 hammer2_time64_to_str(media.ipdata.meta.atime, &str));
645 tabprintf(tab, "btime %s\n",
646 hammer2_time64_to_str(media.ipdata.meta.btime, &str));
647 tabprintf(tab, "uid %s\n",
648 hammer2_uuid_to_str(&media.ipdata.meta.uid, &str));
649 tabprintf(tab, "gid %s\n",
650 hammer2_uuid_to_str(&media.ipdata.meta.gid, &str));
651 tabprintf(tab, "type %s\n",
652 hammer2_iptype_to_str(media.ipdata.meta.type));
653 tabprintf(tab, "opflgs 0x%02x\n",
654 media.ipdata.meta.op_flags);
655 tabprintf(tab, "capflgs 0x%04x\n",
656 media.ipdata.meta.cap_flags);
657 tabprintf(tab, "mode %-7o\n",
658 media.ipdata.meta.mode);
659 tabprintf(tab, "inum 0x%016jx\n",
660 media.ipdata.meta.inum);
661 tabprintf(tab, "size %ju\n",
662 (uintmax_t)media.ipdata.meta.size);
663 tabprintf(tab, "nlinks %ju\n",
664 (uintmax_t)media.ipdata.meta.nlinks);
665 tabprintf(tab, "iparent 0x%016jx\n",
666 (uintmax_t)media.ipdata.meta.iparent);
667 tabprintf(tab, "name_key 0x%016jx\n",
668 (uintmax_t)media.ipdata.meta.name_key);
669 tabprintf(tab, "name_len %u\n",
670 media.ipdata.meta.name_len);
671 tabprintf(tab, "ncopies %u\n",
672 media.ipdata.meta.ncopies);
673 tabprintf(tab, "compalg %u\n",
674 media.ipdata.meta.comp_algo);
675 tabprintf(tab, "checkalg %u\n",
676 media.ipdata.meta.check_algo);
677 if ((media.ipdata.meta.op_flags & HAMMER2_OPFLAG_PFSROOT) ||
678 media.ipdata.meta.pfs_type == HAMMER2_PFSTYPE_SUPROOT) {
679 tabprintf(tab, "pfs_type %u (%s)\n",
680 media.ipdata.meta.pfs_type,
681 hammer2_pfstype_to_str(media.ipdata.meta.pfs_type));
682 tabprintf(tab, "pfs_inum 0x%016jx\n",
683 (uintmax_t)media.ipdata.meta.pfs_inum);
684 tabprintf(tab, "pfs_clid %s\n",
685 hammer2_uuid_to_str(&media.ipdata.meta.pfs_clid,
687 tabprintf(tab, "pfs_fsid %s\n",
688 hammer2_uuid_to_str(&media.ipdata.meta.pfs_fsid,
690 tabprintf(tab, "pfs_lsnap_tid 0x%016jx\n",
691 (uintmax_t)media.ipdata.meta.pfs_lsnap_tid);
693 tabprintf(tab, "data_quota %ju\n",
694 (uintmax_t)media.ipdata.meta.data_quota);
695 tabprintf(tab, "data_count %ju\n",
696 (uintmax_t)bref->embed.stats.data_count);
697 tabprintf(tab, "inode_quota %ju\n",
698 (uintmax_t)media.ipdata.meta.inode_quota);
699 tabprintf(tab, "inode_count %ju\n",
700 (uintmax_t)bref->embed.stats.inode_count);
702 case HAMMER2_BREF_TYPE_INDIRECT:
703 bscan = &media.npdata[0];
704 bcount = bytes / sizeof(hammer2_blockref_t);
708 case HAMMER2_BREF_TYPE_DATA:
710 if (VerboseOpt >= 2) {
719 case HAMMER2_BREF_TYPE_VOLUME:
720 printf("mirror_tid=%016jx freemap_tid=%016jx ",
721 media.voldata.mirror_tid,
722 media.voldata.freemap_tid);
724 bscan = &media.voldata.freemap_blockset.blockref[0];
725 bcount = HAMMER2_SET_COUNT;
727 bscan = &media.voldata.sroot_blockset.blockref[0];
728 bcount = HAMMER2_SET_COUNT;
732 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
734 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
736 if (media.bmdata[i].class == 0 &&
737 media.bmdata[i].avail == 0) {
741 #if HAMMER2_BMAP_ELEMENTS != 8
742 #error "cmd_debug.c: HAMMER2_BMAP_ELEMENTS expected to be 8"
745 tabprintf(tab + 4, "%016jx %04d.%04x (avail=%7d) "
746 "%016jx %016jx %016jx %016jx "
747 "%016jx %016jx %016jx %016jx\n",
749 i * 256 * HAMMER2_FREEMAP_BLOCK_SIZE,
750 i, media.bmdata[i].class,
751 media.bmdata[i].avail,
752 media.bmdata[i].bitmapq[0],
753 media.bmdata[i].bitmapq[1],
754 media.bmdata[i].bitmapq[2],
755 media.bmdata[i].bitmapq[3],
756 media.bmdata[i].bitmapq[4],
757 media.bmdata[i].bitmapq[5],
758 media.bmdata[i].bitmapq[6],
759 media.bmdata[i].bitmapq[7]);
761 tabprintf(tab, "}\n");
763 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
765 bscan = &media.npdata[0];
766 bcount = bytes / sizeof(hammer2_blockref_t);
777 * Recurse if norecurse == 0. If the CRC failed, pass norecurse = 1.
778 * That is, if an indirect or inode fails we still try to list its
779 * direct children to help with debugging, but go no further than
780 * that because they are probably garbage.
782 for (i = 0; norecurse == 0 && i < bcount; ++i) {
783 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
788 show_bref(fd, tab, i, &bscan[i], dofreemap, failed);
793 if (bref->type == HAMMER2_BREF_TYPE_INODE)
794 tabprintf(tab, "} (%s.%d, \"%*.*s\")\n",
796 namelen, namelen, media.ipdata.filename);
798 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
803 cmd_hash(int ac, const char **av)
807 for (i = 0; i < ac; ++i) {
808 printf("%016jx %s\n", dirhash(av[i], strlen(av[i])), av[i]);
814 cmd_chaindump(const char *path)
819 fd = open(path, O_RDONLY);
821 ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy);
824 fprintf(stderr, "unable to open %s\n", path);
832 tabprintf(int tab, const char *ctl, ...)
836 printf("%*.*s", tab, tab, "");