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(hammer2_msg_t *msg);
41 static void shell_ttymsg(hammer2_iocom_t *iocom);
42 static void hammer2_shell_parse(hammer2_msg_t *msg);
44 /************************************************************************
46 ************************************************************************/
49 cmd_shell(const char *hostname)
51 struct hammer2_iocom iocom;
56 * Connect to the target
58 fd = hammer2_connect(hostname);
63 * Run the session. The remote end transmits our prompt.
65 hammer2_iocom_init(&iocom, fd, 0, NULL, shell_rcvmsg, shell_ttymsg);
66 fcntl(0, F_SETFL, O_NONBLOCK);
67 printf("debug: connected\n");
69 msg = hammer2_msg_alloc(iocom.router, 0, HAMMER2_DBG_SHELL,
71 hammer2_msg_write(msg);
72 hammer2_iocom_core(&iocom);
73 fprintf(stderr, "debug: disconnected\n");
79 * Callback from hammer2_iocom_core() when messages might be present
84 shell_rcvmsg(hammer2_msg_t *msg)
86 switch(msg->any.head.cmd & HAMMER2_MSGF_TRANSMASK) {
87 case HAMMER2_LNK_ERROR:
88 case HAMMER2_LNK_ERROR | HAMMER2_MSGF_REPLY:
90 * One-way non-transactional LNK_ERROR messages typically
91 * indicate a connection failure. Error code 0 is used by
92 * the debug shell to indicate no more results from last cmd.
94 if (msg->any.head.error) {
95 fprintf(stderr, "Stream failure: %s\n",
96 hammer2_msg_str(msg));
98 write(1, "debug> ", 7);
101 case HAMMER2_LNK_ERROR | HAMMER2_MSGF_DELETE:
102 /* ignore termination of LNK_CONN */
104 case HAMMER2_DBG_SHELL:
106 * We send the commands, not accept them.
107 * (one-way message, not transactional)
109 hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);
111 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
113 * A reply from the remote is data we copy to stdout.
114 * (one-way message, not transactional)
117 msg->aux_data[msg->aux_size - 1] = 0;
118 write(1, msg->aux_data, strlen(msg->aux_data));
121 case HAMMER2_LNK_CONN | HAMMER2_MSGF_CREATE:
122 fprintf(stderr, "Debug Shell is ignoring received LNK_CONN\n");
123 hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);
125 case HAMMER2_LNK_CONN | HAMMER2_MSGF_DELETE:
129 * Ignore any unknown messages, Terminate any unknown
130 * transactions with an error.
132 fprintf(stderr, "Unknown message: %s\n", hammer2_msg_str(msg));
133 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
134 hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);
135 if (msg->any.head.cmd & HAMMER2_MSGF_DELETE)
136 hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);
143 shell_ttymsg(hammer2_iocom_t *iocom)
149 if (fgets(buf, sizeof(buf), stdin) != NULL) {
151 if (len && buf[len - 1] == '\n')
154 msg = hammer2_msg_alloc(iocom->router, len, HAMMER2_DBG_SHELL,
156 bcopy(buf, msg->aux_data, len);
157 hammer2_msg_write(msg);
158 } else if (feof(stdin)) {
160 * Set EOF flag without setting any error code for normal
163 iocom->flags |= HAMMER2_IOCOMF_EOF;
170 * This is called from the master node to process a received debug
171 * shell command. We process the command, outputting the results,
172 * then finish up by outputting another prompt.
175 hammer2_msg_dbg(hammer2_msg_t *msg)
177 switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
178 case HAMMER2_DBG_SHELL:
180 * This is a command which we must process.
181 * When we are finished we generate a final reply.
184 msg->aux_data[msg->aux_size - 1] = 0;
185 hammer2_shell_parse(msg);
186 hammer2_msg_reply(msg, 0);
188 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
190 * A reply just prints out the string. No newline is added
191 * (it is expected to be embedded if desired).
194 msg->aux_data[msg->aux_size - 1] = 0;
196 write(2, msg->aux_data, strlen(msg->aux_data));
199 hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);
204 static void shell_span(hammer2_router_t *router, char *cmdbuf);
205 /*static void shell_tree(hammer2_router_t *router, char *cmdbuf);*/
208 hammer2_shell_parse(hammer2_msg_t *msg)
210 hammer2_router_t *router = msg->router;
211 char *cmdbuf = msg->aux_data;
212 char *cmd = strsep(&cmdbuf, " \t");
214 if (cmd == NULL || *cmd == 0) {
216 } else if (strcmp(cmd, "span") == 0) {
217 shell_span(router, cmdbuf);
218 } else if (strcmp(cmd, "tree") == 0) {
219 shell_tree(router, cmdbuf);
220 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
221 router_printf(router, "help Command help\n");
222 router_printf(router, "span <host> Span to target host\n");
223 router_printf(router, "tree Dump spanning tree\n");
225 router_printf(router, "Unrecognized command: %s\n", cmd);
230 shell_span(hammer2_router_t *router, char *cmdbuf)
232 const char *hostname = strsep(&cmdbuf, " \t");
237 * Connect to the target
239 if (hostname == NULL) {
242 fd = hammer2_connect(hostname);
246 * Start master service
249 router_printf(router, "Connection to %s failed\n", hostname);
251 router_printf(router, "Connected to %s\n", hostname);
252 pthread_create(&thread, NULL,
253 master_service, (void *)(intptr_t)fd);
254 /*pthread_join(thread, &res);*/
259 * Returns text debug output to the original defined by (msg). (msg) is
260 * not modified and stays intact. We use a one-way message with REPLY set
261 * to distinguish between a debug command and debug terminal output.
263 * To prevent loops router_printf() can filter the message (cmd) related
264 * to the router_printf(). We filter out DBG messages.
267 router_printf(hammer2_router_t *router, const char *ctl, ...)
275 vsnprintf(buf, sizeof(buf), ctl, va);
277 len = strlen(buf) + 1;
279 rmsg = hammer2_msg_alloc(router, len, HAMMER2_DBG_SHELL |
282 bcopy(buf, rmsg->aux_data, len);
284 hammer2_msg_write(rmsg);
287 /************************************************************************
289 ************************************************************************
291 * Connect to the target manually (not via the cluster list embedded in
292 * a hammer2 filesystem) and initiate the SPAN protocol.
295 cmd_debugspan(const char *hostname)
302 * Connect to the target
304 fd = hammer2_connect(hostname);
308 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
309 pthread_create(&thread, NULL, master_service, (void *)(intptr_t)fd);
310 pthread_join(thread, &res);
314 /************************************************************************
316 ************************************************************************/
318 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
319 static void tabprintf(int tab, const char *ctl, ...);
322 cmd_show(const char *devpath)
324 hammer2_blockref_t broot;
327 fd = open(devpath, O_RDONLY);
332 bzero(&broot, sizeof(broot));
333 broot.type = HAMMER2_BREF_TYPE_VOLUME;
334 broot.data_off = 0 | HAMMER2_PBUFRADIX;
335 show_bref(fd, 0, 0, &broot);
342 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref)
344 hammer2_media_data_t media;
345 hammer2_blockref_t *bscan;
352 const char *type_str;
356 case HAMMER2_BREF_TYPE_EMPTY:
359 case HAMMER2_BREF_TYPE_INODE:
362 case HAMMER2_BREF_TYPE_INDIRECT:
365 case HAMMER2_BREF_TYPE_DATA:
368 case HAMMER2_BREF_TYPE_VOLUME:
372 type_str = "unknown";
377 tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
378 type_str, bi, (intmax_t)bref->data_off,
379 (intmax_t)bref->key, (intmax_t)bref->keybits,
380 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
383 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
384 if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) {
385 printf("(bad block size %zd)\n", bytes);
388 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
389 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
390 if (read(fd, &media, bytes) != (ssize_t)bytes) {
391 printf("(media read failed)\n");
401 case HAMMER2_BREF_TYPE_EMPTY:
404 case HAMMER2_BREF_TYPE_INODE:
406 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
409 bscan = &media.ipdata.u.blockset.blockref[0];
410 bcount = HAMMER2_SET_COUNT;
412 namelen = media.ipdata.name_len;
413 if (namelen > HAMMER2_INODE_MAXNAME)
415 tabprintf(tab, "filename \"%*.*s\"\n",
416 namelen, namelen, media.ipdata.filename);
417 tabprintf(tab, "version %d\n", media.ipdata.version);
418 tabprintf(tab, "uflags 0x%08x\n",
419 media.ipdata.uflags);
420 if (media.ipdata.rmajor || media.ipdata.rminor) {
421 tabprintf(tab, "rmajor %d\n",
422 media.ipdata.rmajor);
423 tabprintf(tab, "rminor %d\n",
424 media.ipdata.rminor);
426 tabprintf(tab, "ctime %s\n",
427 hammer2_time64_to_str(media.ipdata.ctime, &str));
428 tabprintf(tab, "mtime %s\n",
429 hammer2_time64_to_str(media.ipdata.mtime, &str));
430 tabprintf(tab, "atime %s\n",
431 hammer2_time64_to_str(media.ipdata.atime, &str));
432 tabprintf(tab, "btime %s\n",
433 hammer2_time64_to_str(media.ipdata.btime, &str));
434 tabprintf(tab, "uid %s\n",
435 hammer2_uuid_to_str(&media.ipdata.uid, &str));
436 tabprintf(tab, "gid %s\n",
437 hammer2_uuid_to_str(&media.ipdata.gid, &str));
438 tabprintf(tab, "type %s\n",
439 hammer2_iptype_to_str(media.ipdata.type));
440 tabprintf(tab, "opflgs 0x%02x\n",
441 media.ipdata.op_flags);
442 tabprintf(tab, "capflgs 0x%04x\n",
443 media.ipdata.cap_flags);
444 tabprintf(tab, "mode %-7o\n",
446 tabprintf(tab, "inum 0x%016jx\n",
448 tabprintf(tab, "size %ju\n",
449 (uintmax_t)media.ipdata.size);
450 tabprintf(tab, "nlinks %ju\n",
451 (uintmax_t)media.ipdata.nlinks);
452 tabprintf(tab, "iparent 0x%016jx\n",
453 (uintmax_t)media.ipdata.iparent);
454 tabprintf(tab, "name_key 0x%016jx\n",
455 (uintmax_t)media.ipdata.name_key);
456 tabprintf(tab, "name_len %u\n",
457 media.ipdata.name_len);
458 tabprintf(tab, "ncopies %u\n",
459 media.ipdata.ncopies);
460 tabprintf(tab, "compalg %u\n",
461 media.ipdata.comp_algo);
462 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
463 tabprintf(tab, "pfs_type %u (%s)\n",
464 media.ipdata.pfs_type,
465 hammer2_pfstype_to_str(media.ipdata.pfs_type));
466 tabprintf(tab, "pfs_inum 0x%016jx\n",
467 (uintmax_t)media.ipdata.pfs_inum);
468 tabprintf(tab, "pfs_clid %s\n",
469 hammer2_uuid_to_str(&media.ipdata.pfs_clid,
471 tabprintf(tab, "pfs_fsid %s\n",
472 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
475 tabprintf(tab, "data_quota %ju\n",
476 (uintmax_t)media.ipdata.data_quota);
477 tabprintf(tab, "data_count %ju\n",
478 (uintmax_t)media.ipdata.data_count);
479 tabprintf(tab, "inode_quota %ju\n",
480 (uintmax_t)media.ipdata.inode_quota);
481 tabprintf(tab, "inode_count %ju\n",
482 (uintmax_t)media.ipdata.inode_count);
483 tabprintf(tab, "attr_tid 0x%016jx\n",
484 (uintmax_t)media.ipdata.attr_tid);
485 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
486 tabprintf(tab, "dirent_tid %016jx\n",
487 (uintmax_t)media.ipdata.dirent_tid);
490 case HAMMER2_BREF_TYPE_INDIRECT:
491 bscan = &media.npdata.blockref[0];
492 bcount = bytes / sizeof(hammer2_blockref_t);
496 case HAMMER2_BREF_TYPE_DATA:
497 if (VerboseOpt >= 2) {
504 case HAMMER2_BREF_TYPE_VOLUME:
505 bscan = &media.voldata.sroot_blockset.blockref[0];
506 bcount = HAMMER2_SET_COUNT;
514 for (i = 0; i < bcount; ++i) {
515 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
520 show_bref(fd, tab, i, &bscan[i]);
525 if (bref->type == HAMMER2_BREF_TYPE_INODE)
526 tabprintf(tab, "} (%s.%d, \"%s\")\n",
527 type_str, bi, media.ipdata.filename);
529 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
535 tabprintf(int tab, const char *ctl, ...)
539 printf("%*.*s", tab, tab, "");