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_rcvmsg(dmsg_msg_t *msg);
41 static void shell_ttymsg(dmsg_iocom_t *iocom);
43 /************************************************************************
45 ************************************************************************/
48 cmd_shell(const char *hostname)
50 struct dmsg_iocom iocom;
55 * Connect to the target
57 fd = dmsg_connect(hostname);
62 * Run the session. The remote end transmits our prompt.
64 dmsg_iocom_init(&iocom, fd, 0,
69 fcntl(0, F_SETFL, O_NONBLOCK);
70 printf("debug: connected\n");
72 msg = dmsg_msg_alloc(&iocom.circuit0, 0, DMSG_DBG_SHELL, NULL, NULL);
74 dmsg_iocom_core(&iocom);
75 fprintf(stderr, "debug: disconnected\n");
81 * Callback from dmsg_iocom_core() when messages might be present
86 shell_rcvmsg(dmsg_msg_t *msg)
90 case DMSG_LNK_ERROR | DMSGF_REPLY:
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.
96 if (msg->any.head.error) {
97 fprintf(stderr, "Stream failure: %s\n",
100 write(1, "debug> ", 7);
103 case DMSG_LNK_ERROR | DMSGF_DELETE:
104 /* ignore termination of LNK_CONN */
108 * We send the commands, not accept them.
109 * (one-way message, not transactional)
111 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
113 case DMSG_DBG_SHELL | DMSGF_REPLY:
115 * A reply from the remote is data we copy to stdout.
116 * (one-way message, not transactional)
119 msg->aux_data[msg->aux_size - 1] = 0;
120 write(1, msg->aux_data, strlen(msg->aux_data));
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);
127 case DMSG_LNK_CONN | DMSGF_DELETE:
131 * Ignore any unknown messages, Terminate any unknown
132 * transactions with an error.
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);
145 shell_ttymsg(dmsg_iocom_t *iocom)
151 if (fgets(buf, sizeof(buf), stdin) != NULL) {
153 if (len && buf[len - 1] == '\n')
156 msg = dmsg_msg_alloc(&iocom->circuit0, len, DMSG_DBG_SHELL,
158 bcopy(buf, msg->aux_data, len);
160 } else if (feof(stdin)) {
162 * Set EOF flag without setting any error code for normal
165 iocom->flags |= DMSG_IOCOMF_EOF;
171 static void shell_span(dmsg_circuit_t *circuit, char *cmdbuf);
172 static void shell_circ(dmsg_circuit_t *circuit, char *cmdbuf);
175 hammer2_shell_parse(dmsg_msg_t *msg, int unmanaged)
177 dmsg_circuit_t *circuit;
183 * Filter on debug shell commands only
185 cmd = msg->any.head.cmd;
186 if ((cmd & DMSGF_PROTOS) != DMSG_PROTO_DBG) {
188 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
191 if ((cmd & DMSGF_CMDSWMASK) != DMSG_DBG_SHELL) {
193 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
198 * Debug shell command
200 circuit = msg->circuit;
201 cmdbuf = msg->aux_data;
202 cmdp = strsep(&cmdbuf, " \t");
204 if (cmdp == NULL || *cmdp == 0) {
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");
218 dmsg_circuit_printf(circuit, "Unrecognized command: %s\n", cmdp);
223 shell_span(dmsg_circuit_t *circuit, char *cmdbuf)
225 dmsg_master_service_info_t *info;
226 const char *hostname = strsep(&cmdbuf, " \t");
231 * Connect to the target
233 if (hostname == NULL) {
236 fd = dmsg_connect(hostname);
240 * Start master service
243 dmsg_circuit_printf(circuit,
244 "Connection to %s failed\n",
247 dmsg_circuit_printf(circuit, "Connected to %s\n", hostname);
249 info = malloc(sizeof(*info));
250 bzero(info, sizeof(*info));
253 info->usrmsg_callback = hammer2_shell_parse;
254 info->label = strdup("client");
256 pthread_create(&thread, NULL, dmsg_master_service, info);
257 /*pthread_join(thread, &res);*/
261 static void shell_circ_reply(dmsg_msg_t *msg);
264 shell_circ(dmsg_circuit_t *circuit, char *cmdbuf)
266 uint64_t msgid = strtoull(cmdbuf, NULL, 16);
270 if (dmsg_debug_findspan(msgid, &state) == 0) {
271 dmsg_circuit_printf(circuit, "Found state %p\n", state);
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;
280 dmsg_circuit_printf(circuit,
281 "Unable to locate %016jx\n",
287 shell_circ_reply(dmsg_msg_t *msg)
289 dmsg_circuit_t *circ = msg->state->any.circ;
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;
296 dmsg_circuit_printf(circ, "rxmsg result error %d\n",
297 msg->any.head.error);
301 /************************************************************************
303 ************************************************************************
305 * Connect to the target manually (not via the cluster list embedded in
306 * a hammer2 filesystem) and initiate the SPAN protocol.
309 cmd_debugspan(const char *hostname)
316 * Connect to the target
318 fd = dmsg_connect(hostname);
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);
329 /************************************************************************
331 ************************************************************************/
333 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref,
335 static void tabprintf(int tab, const char *ctl, ...);
338 cmd_show(const char *devpath, int dofreemap)
340 hammer2_blockref_t broot;
341 hammer2_blockref_t best;
342 hammer2_media_data_t media;
347 fd = open(devpath, O_RDONLY);
354 * Show the tree using the best volume header.
355 * -vvv will show the tree for all four volume headers.
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) |
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) {
373 show_bref(fd, 0, i, &broot, dofreemap);
377 show_bref(fd, 0, best_i, &best, dofreemap);
384 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref, int dofreemap)
386 hammer2_media_data_t media;
387 hammer2_blockref_t *bscan;
394 const char *type_str;
398 case HAMMER2_BREF_TYPE_EMPTY:
401 case HAMMER2_BREF_TYPE_INODE:
404 case HAMMER2_BREF_TYPE_INDIRECT:
407 case HAMMER2_BREF_TYPE_DATA:
410 case HAMMER2_BREF_TYPE_VOLUME:
413 case HAMMER2_BREF_TYPE_FREEMAP:
414 type_str = "freemap";
416 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
417 type_str = "fmapnode";
419 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
420 type_str = "fbitmap";
423 type_str = "unknown";
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);
433 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
436 hammer2_off_t io_off;
437 hammer2_off_t io_base;
441 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
442 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
444 boff = io_off - io_base;
446 io_bytes = HAMMER2_MINIOSIZE;
447 while (io_bytes + boff < bytes)
450 if (io_bytes > sizeof(media)) {
451 printf("(bad block size %zd)\n", bytes);
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");
461 bcopy((char *)&media + boff, &media, bytes);
471 case HAMMER2_BREF_TYPE_EMPTY:
474 case HAMMER2_BREF_TYPE_INODE:
476 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
479 bscan = &media.ipdata.u.blockset.blockref[0];
480 bcount = HAMMER2_SET_COUNT;
482 namelen = media.ipdata.name_len;
483 if (namelen > HAMMER2_INODE_MAXNAME)
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);
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));
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",
521 tabprintf(tab, "inum 0x%016jx\n",
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,
546 tabprintf(tab, "pfs_fsid %s\n",
547 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
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);
565 case HAMMER2_BREF_TYPE_INDIRECT:
566 bscan = &media.npdata[0];
567 bcount = bytes / sizeof(hammer2_blockref_t);
571 case HAMMER2_BREF_TYPE_DATA:
572 if (VerboseOpt >= 2) {
579 case HAMMER2_BREF_TYPE_VOLUME:
580 printf("mirror_tid=%016jx freemap_tid=%016jx ",
581 media.voldata.mirror_tid,
582 media.voldata.freemap_tid);
584 bscan = &media.voldata.freemap_blockset.blockref[0];
585 bcount = HAMMER2_SET_COUNT;
587 bscan = &media.voldata.sroot_blockset.blockref[0];
588 bcount = HAMMER2_SET_COUNT;
592 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
594 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
595 if (media.bmdata[i].class == 0 &&
596 media.bmdata[i].avail == 0) {
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]);
612 tabprintf(tab, "}\n");
614 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
616 bscan = &media.npdata[0];
617 bcount = bytes / sizeof(hammer2_blockref_t);
626 for (i = 0; i < bcount; ++i) {
627 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
632 show_bref(fd, tab, i, &bscan[i], dofreemap);
637 if (bref->type == HAMMER2_BREF_TYPE_INODE)
638 tabprintf(tab, "} (%s.%d, \"%*.*s\")\n",
640 namelen, namelen, media.ipdata.filename);
642 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
647 cmd_hash(int ac, const char **av)
651 for (i = 0; i < ac; ++i) {
652 printf("%016jx %s\n", dirhash(av[i], strlen(av[i])), av[i]);
658 cmd_chaindump(const char *path)
663 fd = open(path, O_RDONLY);
665 ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy);
668 fprintf(stderr, "unable to open %s\n", path);
676 tabprintf(int tab, const char *ctl, ...)
680 printf("%*.*s", tab, tab, "");