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->node_handler = NULL;
65 info->label = strdup("debug");
66 pthread_create(&thread, NULL, dmsg_master_service, info);
67 pthread_join(thread, NULL);
74 cmd_shell(const char *hostname)
76 struct dmsg_iocom iocom;
81 * Connect to the target
83 fd = dmsg_connect(hostname);
88 * Initialize the session and transmit an empty DMSG_DBG_SHELL
89 * to cause the remote end to generate a prompt.
91 dmsg_iocom_init(&iocom, fd, 0,
96 fcntl(0, F_SETFL, O_NONBLOCK);
97 printf("debug: connected\n");
99 msg = dmsg_msg_alloc(&iocom.state0, 0, DMSG_DBG_SHELL, NULL, NULL);
101 dmsg_iocom_core(&iocom);
102 fprintf(stderr, "debug: disconnected\n");
109 * Debug session front-end
111 * Callback from dmsg_iocom_core() when messages might be present
116 shell_msghandler(dmsg_msg_t *msg, int unmanaged)
123 case DMSG_LNK_ERROR | DMSGF_REPLY:
125 * One-way non-transactional LNK_ERROR messages typically
126 * indicate a connection failure. Error code 0 is used by
127 * the debug shell to indicate no more results from last cmd.
129 if (msg->any.head.error) {
130 fprintf(stderr, "Stream failure: %s\n",
133 write(1, "debug> ", 7);
136 case DMSG_LNK_ERROR | DMSGF_DELETE:
137 /* ignore termination of LNK_CONN */
142 * We send the commands, not accept them.
143 * (one-way message, not transactional)
146 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
148 case DMSG_DBG_SHELL | DMSGF_REPLY:
150 * A reply from the remote is data we copy to stdout.
151 * (one-way message, not transactional)
154 msg->aux_data[msg->aux_size - 1] = 0;
155 write(1, msg->aux_data, strlen(msg->aux_data));
159 case DMSG_LNK_CONN | DMSGF_CREATE:
160 fprintf(stderr, "Debug Shell received LNK_CONN\n");
161 nmsg = dmsg_msg_alloc(&msg->state->iocom->state0, 0,
164 dmsg_msg_write(nmsg);
165 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
167 case DMSG_LNK_CONN | DMSGF_DELETE:
172 * Ignore any unknown messages, Terminate any unknown
173 * transactions with an error.
175 fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg));
177 if (msg->any.head.cmd & DMSGF_CREATE)
178 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
179 if (msg->any.head.cmd & DMSGF_DELETE)
180 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
187 * Debug session front-end
191 shell_ttymsg(dmsg_iocom_t *iocom)
193 dmsg_state_t *pstate;
199 if (fgets(buf, sizeof(buf), stdin) != NULL) {
201 pstate = dmsg_findspan(strtok(buf + 1, " \t\n"));
202 cmd = strtok(NULL, "\n");
204 pstate = &iocom->state0;
205 cmd = strtok(buf, "\n");
208 len = strlen(cmd) + 1;
209 msg = dmsg_msg_alloc(pstate, len, DMSG_DBG_SHELL,
211 bcopy(cmd, msg->aux_data, len);
214 fprintf(stderr, "@msgid not found\n");
217 * This should cause the remote end to generate
218 * a debug> prompt (and thus shows that there is
221 msg = dmsg_msg_alloc(pstate, 0, DMSG_DBG_SHELL,
225 } else if (feof(stdin)) {
227 * Set EOF flag without setting any error code for normal
230 iocom->flags |= DMSG_IOCOMF_EOF;
237 * Debug session back-end (on remote side)
239 static void shell_span(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 only
252 cmd = msg->any.head.cmd;
253 if ((cmd & DMSGF_PROTOS) != DMSG_PROTO_DBG) {
255 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
258 if ((cmd & DMSGF_CMDSWMASK) != DMSG_DBG_SHELL) {
260 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
265 * Debug shell command
267 cmdbuf = msg->aux_data;
268 cmdp = strsep(&cmdbuf, " \t");
270 if (cmdp == NULL || *cmdp == 0) {
272 } else if (strcmp(cmdp, "span") == 0) {
273 shell_span(msg, cmdbuf);
274 } else if (strcmp(cmdp, "tree") == 0) {
275 dmsg_shell_tree(iocom, cmdbuf); /* dump spanning tree */
276 } else if (strcmp(cmdp, "help") == 0 || strcmp(cmdp, "?") == 0) {
277 dmsg_printf(iocom, "help Command help\n");
278 dmsg_printf(iocom, "span <host> Span to target host\n");
279 dmsg_printf(iocom, "tree Dump spanning tree\n");
280 dmsg_printf(iocom, "@span <cmd> Issue via circuit\n");
282 dmsg_printf(iocom, "Unrecognized command: %s\n", cmdp);
284 dmsg_printf(iocom, "debug> ");
288 shell_span(dmsg_msg_t *msg, char *cmdbuf)
290 dmsg_iocom_t *iocom = msg->state->iocom;
291 dmsg_master_service_info_t *info;
292 const char *hostname = strsep(&cmdbuf, " \t");
297 * Connect to the target
299 if (hostname == NULL) {
302 fd = dmsg_connect(hostname);
306 * Start master service
309 dmsg_printf(iocom, "Connection to %s failed\n", hostname);
311 dmsg_printf(iocom, "Connected to %s\n", hostname);
313 info = malloc(sizeof(*info));
314 bzero(info, sizeof(*info));
317 info->usrmsg_callback = hammer2_shell_parse;
318 info->label = strdup("client");
320 pthread_create(&thread, NULL, dmsg_master_service, info);
321 /*pthread_join(thread, &res);*/
325 /************************************************************************
327 ************************************************************************
329 * Connect to the target manually (not via the cluster list embedded in
330 * a hammer2 filesystem) and initiate the SPAN protocol.
333 cmd_debugspan(const char *hostname)
340 * Connect to the target
342 fd = dmsg_connect(hostname);
346 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
347 pthread_create(&thread, NULL,
348 dmsg_master_service, (void *)(intptr_t)fd);
349 pthread_join(thread, &res);
353 /************************************************************************
355 ************************************************************************/
357 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref,
359 static void tabprintf(int tab, const char *ctl, ...);
362 cmd_show(const char *devpath, int dofreemap)
364 hammer2_blockref_t broot;
365 hammer2_blockref_t best;
366 hammer2_media_data_t media;
371 fd = open(devpath, O_RDONLY);
378 * Show the tree using the best volume header.
379 * -vvv will show the tree for all four volume headers.
382 bzero(&best, sizeof(best));
383 for (i = 0; i < 4; ++i) {
384 bzero(&broot, sizeof(broot));
385 broot.type = HAMMER2_BREF_TYPE_VOLUME;
386 broot.data_off = (i * HAMMER2_ZONE_BYTES64) |
388 lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
389 if (read(fd, &media, HAMMER2_PBUFSIZE) ==
390 (ssize_t)HAMMER2_PBUFSIZE) {
391 broot.mirror_tid = media.voldata.mirror_tid;
392 if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
397 show_bref(fd, 0, i, &broot, dofreemap);
401 show_bref(fd, 0, best_i, &best, dofreemap);
408 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref, int dofreemap)
410 hammer2_media_data_t media;
411 hammer2_blockref_t *bscan;
418 const char *type_str;
422 case HAMMER2_BREF_TYPE_EMPTY:
425 case HAMMER2_BREF_TYPE_INODE:
428 case HAMMER2_BREF_TYPE_INDIRECT:
431 case HAMMER2_BREF_TYPE_DATA:
434 case HAMMER2_BREF_TYPE_VOLUME:
437 case HAMMER2_BREF_TYPE_FREEMAP:
438 type_str = "freemap";
440 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
441 type_str = "fmapnode";
443 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
444 type_str = "fbitmap";
447 type_str = "unknown";
451 tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
452 type_str, bi, (intmax_t)bref->data_off,
453 (intmax_t)bref->key, (intmax_t)bref->keybits,
454 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
457 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
460 hammer2_off_t io_off;
461 hammer2_off_t io_base;
465 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
466 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
468 boff = io_off - io_base;
470 io_bytes = HAMMER2_MINIOSIZE;
471 while (io_bytes + boff < bytes)
474 if (io_bytes > sizeof(media)) {
475 printf("(bad block size %zd)\n", bytes);
478 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
479 lseek(fd, io_base, 0);
480 if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
481 printf("(media read failed)\n");
485 bcopy((char *)&media + boff, &media, bytes);
495 case HAMMER2_BREF_TYPE_EMPTY:
498 case HAMMER2_BREF_TYPE_INODE:
500 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
503 bscan = &media.ipdata.u.blockset.blockref[0];
504 bcount = HAMMER2_SET_COUNT;
506 namelen = media.ipdata.name_len;
507 if (namelen > HAMMER2_INODE_MAXNAME)
509 tabprintf(tab, "filename \"%*.*s\"\n",
510 namelen, namelen, media.ipdata.filename);
511 tabprintf(tab, "version %d\n", media.ipdata.version);
512 tabprintf(tab, "uflags 0x%08x\n",
513 media.ipdata.uflags);
514 if (media.ipdata.rmajor || media.ipdata.rminor) {
515 tabprintf(tab, "rmajor %d\n",
516 media.ipdata.rmajor);
517 tabprintf(tab, "rminor %d\n",
518 media.ipdata.rminor);
520 tabprintf(tab, "ctime %s\n",
521 hammer2_time64_to_str(media.ipdata.ctime, &str));
522 tabprintf(tab, "mtime %s\n",
523 hammer2_time64_to_str(media.ipdata.mtime, &str));
524 tabprintf(tab, "atime %s\n",
525 hammer2_time64_to_str(media.ipdata.atime, &str));
526 tabprintf(tab, "btime %s\n",
527 hammer2_time64_to_str(media.ipdata.btime, &str));
528 tabprintf(tab, "uid %s\n",
529 hammer2_uuid_to_str(&media.ipdata.uid, &str));
530 tabprintf(tab, "gid %s\n",
531 hammer2_uuid_to_str(&media.ipdata.gid, &str));
532 if (media.ipdata.type == HAMMER2_OBJTYPE_HARDLINK)
533 tabprintf(tab, "type %s (%s)\n",
534 hammer2_iptype_to_str(media.ipdata.type),
535 hammer2_iptype_to_str(media.ipdata.target_type));
537 tabprintf(tab, "type %s\n",
538 hammer2_iptype_to_str(media.ipdata.type));
539 tabprintf(tab, "opflgs 0x%02x\n",
540 media.ipdata.op_flags);
541 tabprintf(tab, "capflgs 0x%04x\n",
542 media.ipdata.cap_flags);
543 tabprintf(tab, "mode %-7o\n",
545 tabprintf(tab, "inum 0x%016jx\n",
547 tabprintf(tab, "size %ju\n",
548 (uintmax_t)media.ipdata.size);
549 tabprintf(tab, "nlinks %ju\n",
550 (uintmax_t)media.ipdata.nlinks);
551 tabprintf(tab, "iparent 0x%016jx\n",
552 (uintmax_t)media.ipdata.iparent);
553 tabprintf(tab, "name_key 0x%016jx\n",
554 (uintmax_t)media.ipdata.name_key);
555 tabprintf(tab, "name_len %u\n",
556 media.ipdata.name_len);
557 tabprintf(tab, "ncopies %u\n",
558 media.ipdata.ncopies);
559 tabprintf(tab, "compalg %u\n",
560 media.ipdata.comp_algo);
561 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
562 tabprintf(tab, "pfs_type %u (%s)\n",
563 media.ipdata.pfs_type,
564 hammer2_pfstype_to_str(media.ipdata.pfs_type));
565 tabprintf(tab, "pfs_inum 0x%016jx\n",
566 (uintmax_t)media.ipdata.pfs_inum);
567 tabprintf(tab, "pfs_clid %s\n",
568 hammer2_uuid_to_str(&media.ipdata.pfs_clid,
570 tabprintf(tab, "pfs_fsid %s\n",
571 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
574 tabprintf(tab, "data_quota %ju\n",
575 (uintmax_t)media.ipdata.data_quota);
576 tabprintf(tab, "data_count %ju\n",
577 (uintmax_t)media.ipdata.data_count);
578 tabprintf(tab, "inode_quota %ju\n",
579 (uintmax_t)media.ipdata.inode_quota);
580 tabprintf(tab, "inode_count %ju\n",
581 (uintmax_t)media.ipdata.inode_count);
582 tabprintf(tab, "attr_tid 0x%016jx\n",
583 (uintmax_t)media.ipdata.attr_tid);
584 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
585 tabprintf(tab, "dirent_tid %016jx\n",
586 (uintmax_t)media.ipdata.dirent_tid);
589 case HAMMER2_BREF_TYPE_INDIRECT:
590 bscan = &media.npdata[0];
591 bcount = bytes / sizeof(hammer2_blockref_t);
595 case HAMMER2_BREF_TYPE_DATA:
596 if (VerboseOpt >= 2) {
603 case HAMMER2_BREF_TYPE_VOLUME:
604 printf("mirror_tid=%016jx freemap_tid=%016jx ",
605 media.voldata.mirror_tid,
606 media.voldata.freemap_tid);
608 bscan = &media.voldata.freemap_blockset.blockref[0];
609 bcount = HAMMER2_SET_COUNT;
611 bscan = &media.voldata.sroot_blockset.blockref[0];
612 bcount = HAMMER2_SET_COUNT;
616 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
618 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
619 if (media.bmdata[i].class == 0 &&
620 media.bmdata[i].avail == 0) {
623 tabprintf(tab + 4, "%04d.%04x (avail=%5d) "
624 "%08x %08x %08x %08x %08x %08x %08x %08x\n",
625 i, media.bmdata[i].class,
626 media.bmdata[i].avail,
627 media.bmdata[i].bitmap[0],
628 media.bmdata[i].bitmap[1],
629 media.bmdata[i].bitmap[2],
630 media.bmdata[i].bitmap[3],
631 media.bmdata[i].bitmap[4],
632 media.bmdata[i].bitmap[5],
633 media.bmdata[i].bitmap[6],
634 media.bmdata[i].bitmap[7]);
636 tabprintf(tab, "}\n");
638 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
640 bscan = &media.npdata[0];
641 bcount = bytes / sizeof(hammer2_blockref_t);
650 for (i = 0; i < bcount; ++i) {
651 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
656 show_bref(fd, tab, i, &bscan[i], dofreemap);
661 if (bref->type == HAMMER2_BREF_TYPE_INODE)
662 tabprintf(tab, "} (%s.%d, \"%*.*s\")\n",
664 namelen, namelen, media.ipdata.filename);
666 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
671 cmd_hash(int ac, const char **av)
675 for (i = 0; i < ac; ++i) {
676 printf("%016jx %s\n", dirhash(av[i], strlen(av[i])), av[i]);
682 cmd_chaindump(const char *path)
687 fd = open(path, O_RDONLY);
689 ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy);
692 fprintf(stderr, "unable to open %s\n", path);
700 tabprintf(int tab, const char *ctl, ...)
704 printf("%*.*s", tab, tab, "");