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)
88 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
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)
177 dmsg_circuit_t *circuit = msg->circuit;
178 char *cmdbuf = msg->aux_data;
179 char *cmd = strsep(&cmdbuf, " \t");
181 if (cmd == NULL || *cmd == 0) {
183 } else if (strcmp(cmd, "span") == 0) {
184 shell_span(circuit, cmdbuf);
185 } else if (strcmp(cmd, "circ") == 0) {
186 shell_circ(circuit, cmdbuf);
187 } else if (strcmp(cmd, "tree") == 0) {
188 dmsg_shell_tree(circuit, cmdbuf); /* dump spanning tree */
189 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
190 dmsg_circuit_printf(circuit, "help Command help\n");
191 dmsg_circuit_printf(circuit, "span <host> Span to target host\n");
192 dmsg_circuit_printf(circuit, "circ <msgid> Create VC to msgid of rx SPAN\n");
193 dmsg_circuit_printf(circuit, "tree Dump spanning tree\n");
195 dmsg_circuit_printf(circuit, "Unrecognized command: %s\n", cmd);
200 shell_span(dmsg_circuit_t *circuit, char *cmdbuf)
202 dmsg_master_service_info_t *info;
203 const char *hostname = strsep(&cmdbuf, " \t");
208 * Connect to the target
210 if (hostname == NULL) {
213 fd = dmsg_connect(hostname);
217 * Start master service
220 dmsg_circuit_printf(circuit,
221 "Connection to %s failed\n",
224 dmsg_circuit_printf(circuit, "Connected to %s\n", hostname);
226 info = malloc(sizeof(*info));
227 bzero(info, sizeof(*info));
230 info->dbgmsg_callback = hammer2_shell_parse;
231 info->label = strdup("client");
233 pthread_create(&thread, NULL, dmsg_master_service, info);
234 /*pthread_join(thread, &res);*/
238 static void shell_circ_reply(dmsg_msg_t *msg);
241 shell_circ(dmsg_circuit_t *circuit, char *cmdbuf)
243 uint64_t msgid = strtoull(cmdbuf, NULL, 16);
247 if (dmsg_debug_findspan(msgid, &state) == 0) {
248 dmsg_circuit_printf(circuit, "Found state %p\n", state);
250 dmsg_circuit_printf(circuit, "Establishing CIRC\n");
251 msg = dmsg_msg_alloc(&state->iocom->circuit0, 0,
252 DMSG_LNK_CIRC | DMSGF_CREATE,
253 shell_circ_reply, circuit);
254 msg->any.lnk_circ.target = state->msgid;
257 dmsg_circuit_printf(circuit,
258 "Unable to locate %016jx\n",
264 shell_circ_reply(dmsg_msg_t *msg)
266 dmsg_circuit_t *circ = msg->state->any.circ;
268 if (msg->any.head.cmd & DMSGF_DELETE) {
269 dmsg_circuit_printf(circ, "rxmsg DELETE error %d\n",
270 msg->any.head.error);
271 msg->state->any.circ = NULL;
273 dmsg_circuit_printf(circ, "rxmsg result error %d\n",
274 msg->any.head.error);
278 /************************************************************************
280 ************************************************************************
282 * Connect to the target manually (not via the cluster list embedded in
283 * a hammer2 filesystem) and initiate the SPAN protocol.
286 cmd_debugspan(const char *hostname)
293 * Connect to the target
295 fd = dmsg_connect(hostname);
299 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
300 pthread_create(&thread, NULL,
301 dmsg_master_service, (void *)(intptr_t)fd);
302 pthread_join(thread, &res);
306 /************************************************************************
308 ************************************************************************/
310 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref,
312 static void tabprintf(int tab, const char *ctl, ...);
315 cmd_show(const char *devpath, int dofreemap)
317 hammer2_blockref_t broot;
318 hammer2_blockref_t best;
319 hammer2_media_data_t media;
324 fd = open(devpath, O_RDONLY);
331 * Show the tree using the best volume header.
332 * -vvv will show the tree for all four volume headers.
335 bzero(&best, sizeof(best));
336 for (i = 0; i < 4; ++i) {
337 bzero(&broot, sizeof(broot));
338 broot.type = HAMMER2_BREF_TYPE_VOLUME;
339 broot.data_off = (i * HAMMER2_ZONE_BYTES64) |
341 lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
342 if (read(fd, &media, HAMMER2_PBUFSIZE) ==
343 (ssize_t)HAMMER2_PBUFSIZE) {
344 broot.mirror_tid = media.voldata.mirror_tid;
345 if (best_i < 0 || best.mirror_tid < broot.mirror_tid) {
350 show_bref(fd, 0, i, &broot, dofreemap);
354 show_bref(fd, 0, best_i, &best, dofreemap);
361 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref, int dofreemap)
363 hammer2_media_data_t media;
364 hammer2_blockref_t *bscan;
371 const char *type_str;
375 case HAMMER2_BREF_TYPE_EMPTY:
378 case HAMMER2_BREF_TYPE_INODE:
381 case HAMMER2_BREF_TYPE_INDIRECT:
384 case HAMMER2_BREF_TYPE_DATA:
387 case HAMMER2_BREF_TYPE_VOLUME:
390 case HAMMER2_BREF_TYPE_FREEMAP:
391 type_str = "freemap";
393 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
394 type_str = "fmapnode";
396 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
397 type_str = "fbitmap";
400 type_str = "unknown";
404 tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
405 type_str, bi, (intmax_t)bref->data_off,
406 (intmax_t)bref->key, (intmax_t)bref->keybits,
407 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
410 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
413 hammer2_off_t io_off;
414 hammer2_off_t io_base;
418 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX;
419 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1);
421 boff = io_off - io_base;
423 io_bytes = HAMMER2_MINIOSIZE;
424 while (io_bytes + boff < bytes)
427 if (io_bytes > sizeof(media)) {
428 printf("(bad block size %zd)\n", bytes);
431 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
432 lseek(fd, io_base, 0);
433 if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) {
434 printf("(media read failed)\n");
438 bcopy((char *)&media + boff, &media, bytes);
448 case HAMMER2_BREF_TYPE_EMPTY:
451 case HAMMER2_BREF_TYPE_INODE:
453 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
456 bscan = &media.ipdata.u.blockset.blockref[0];
457 bcount = HAMMER2_SET_COUNT;
459 namelen = media.ipdata.name_len;
460 if (namelen > HAMMER2_INODE_MAXNAME)
462 tabprintf(tab, "filename \"%*.*s\"\n",
463 namelen, namelen, media.ipdata.filename);
464 tabprintf(tab, "version %d\n", media.ipdata.version);
465 tabprintf(tab, "uflags 0x%08x\n",
466 media.ipdata.uflags);
467 if (media.ipdata.rmajor || media.ipdata.rminor) {
468 tabprintf(tab, "rmajor %d\n",
469 media.ipdata.rmajor);
470 tabprintf(tab, "rminor %d\n",
471 media.ipdata.rminor);
473 tabprintf(tab, "ctime %s\n",
474 hammer2_time64_to_str(media.ipdata.ctime, &str));
475 tabprintf(tab, "mtime %s\n",
476 hammer2_time64_to_str(media.ipdata.mtime, &str));
477 tabprintf(tab, "atime %s\n",
478 hammer2_time64_to_str(media.ipdata.atime, &str));
479 tabprintf(tab, "btime %s\n",
480 hammer2_time64_to_str(media.ipdata.btime, &str));
481 tabprintf(tab, "uid %s\n",
482 hammer2_uuid_to_str(&media.ipdata.uid, &str));
483 tabprintf(tab, "gid %s\n",
484 hammer2_uuid_to_str(&media.ipdata.gid, &str));
485 if (media.ipdata.type == HAMMER2_OBJTYPE_HARDLINK)
486 tabprintf(tab, "type %s (%s)\n",
487 hammer2_iptype_to_str(media.ipdata.type),
488 hammer2_iptype_to_str(media.ipdata.target_type));
490 tabprintf(tab, "type %s\n",
491 hammer2_iptype_to_str(media.ipdata.type));
492 tabprintf(tab, "opflgs 0x%02x\n",
493 media.ipdata.op_flags);
494 tabprintf(tab, "capflgs 0x%04x\n",
495 media.ipdata.cap_flags);
496 tabprintf(tab, "mode %-7o\n",
498 tabprintf(tab, "inum 0x%016jx\n",
500 tabprintf(tab, "size %ju\n",
501 (uintmax_t)media.ipdata.size);
502 tabprintf(tab, "nlinks %ju\n",
503 (uintmax_t)media.ipdata.nlinks);
504 tabprintf(tab, "iparent 0x%016jx\n",
505 (uintmax_t)media.ipdata.iparent);
506 tabprintf(tab, "name_key 0x%016jx\n",
507 (uintmax_t)media.ipdata.name_key);
508 tabprintf(tab, "name_len %u\n",
509 media.ipdata.name_len);
510 tabprintf(tab, "ncopies %u\n",
511 media.ipdata.ncopies);
512 tabprintf(tab, "compalg %u\n",
513 media.ipdata.comp_algo);
514 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
515 tabprintf(tab, "pfs_type %u (%s)\n",
516 media.ipdata.pfs_type,
517 hammer2_pfstype_to_str(media.ipdata.pfs_type));
518 tabprintf(tab, "pfs_inum 0x%016jx\n",
519 (uintmax_t)media.ipdata.pfs_inum);
520 tabprintf(tab, "pfs_clid %s\n",
521 hammer2_uuid_to_str(&media.ipdata.pfs_clid,
523 tabprintf(tab, "pfs_fsid %s\n",
524 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
527 tabprintf(tab, "data_quota %ju\n",
528 (uintmax_t)media.ipdata.data_quota);
529 tabprintf(tab, "data_count %ju\n",
530 (uintmax_t)media.ipdata.data_count);
531 tabprintf(tab, "inode_quota %ju\n",
532 (uintmax_t)media.ipdata.inode_quota);
533 tabprintf(tab, "inode_count %ju\n",
534 (uintmax_t)media.ipdata.inode_count);
535 tabprintf(tab, "attr_tid 0x%016jx\n",
536 (uintmax_t)media.ipdata.attr_tid);
537 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
538 tabprintf(tab, "dirent_tid %016jx\n",
539 (uintmax_t)media.ipdata.dirent_tid);
542 case HAMMER2_BREF_TYPE_INDIRECT:
543 bscan = &media.npdata[0];
544 bcount = bytes / sizeof(hammer2_blockref_t);
548 case HAMMER2_BREF_TYPE_DATA:
549 if (VerboseOpt >= 2) {
556 case HAMMER2_BREF_TYPE_VOLUME:
557 printf("alloc_tid=%016jx freemap_tid=%016jx ",
558 media.voldata.alloc_tid,
559 media.voldata.freemap_tid);
561 bscan = &media.voldata.freemap_blockset.blockref[0];
562 bcount = HAMMER2_SET_COUNT;
564 bscan = &media.voldata.sroot_blockset.blockref[0];
565 bcount = HAMMER2_SET_COUNT;
569 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
571 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) {
572 if (media.bmdata[i].class == 0 &&
573 media.bmdata[i].avail == 0) {
576 tabprintf(tab + 4, "%04d.%04x (avail=%5d) "
577 "%08x %08x %08x %08x %08x %08x %08x %08x\n",
578 i, media.bmdata[i].class,
579 media.bmdata[i].avail,
580 media.bmdata[i].bitmap[0],
581 media.bmdata[i].bitmap[1],
582 media.bmdata[i].bitmap[2],
583 media.bmdata[i].bitmap[3],
584 media.bmdata[i].bitmap[4],
585 media.bmdata[i].bitmap[5],
586 media.bmdata[i].bitmap[6],
587 media.bmdata[i].bitmap[7]);
589 tabprintf(tab, "}\n");
591 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
593 bscan = &media.npdata[0];
594 bcount = bytes / sizeof(hammer2_blockref_t);
603 for (i = 0; i < bcount; ++i) {
604 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
609 show_bref(fd, tab, i, &bscan[i], dofreemap);
614 if (bref->type == HAMMER2_BREF_TYPE_INODE)
615 tabprintf(tab, "} (%s.%d, \"%*.*s\")\n",
617 namelen, namelen, media.ipdata.filename);
619 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
624 cmd_hash(int ac, const char **av)
628 for (i = 0; i < ac; ++i) {
629 printf("%016jx %s\n", dirhash(av[i], strlen(av[i])), av[i]);
637 tabprintf(int tab, const char *ctl, ...)
641 printf("%*.*s", tab, tab, "");