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_iocom_t *iocom, hammer2_msg_t *msg);
41 static void shell_ttymsg(hammer2_iocom_t *iocom);
42 static void hammer2_shell_parse(hammer2_iocom_t *iocom, 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, 0, HAMMER2_DBG_SHELL);
70 hammer2_msg_write(&iocom, msg, NULL, NULL, NULL);
71 hammer2_iocom_core(&iocom);
72 fprintf(stderr, "debug: disconnected\n");
78 * Callback from hammer2_iocom_core() when messages might be present
83 shell_rcvmsg(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
85 switch(msg->any.head.cmd & HAMMER2_MSGF_TRANSMASK) {
86 case HAMMER2_LNK_ERROR:
87 case HAMMER2_LNK_ERROR | HAMMER2_MSGF_REPLY:
89 * One-way non-transactional LNK_ERROR messages typically
90 * indicate a connection failure. Error code 0 is used by
91 * the debug shell to indicate no more results from last cmd.
93 if (msg->any.head.error) {
94 fprintf(stderr, "Stream failure: %s\n",
95 hammer2_msg_str(msg));
97 write(1, "debug> ", 7);
100 case HAMMER2_LNK_ERROR | HAMMER2_MSGF_DELETE:
101 /* ignore termination of LNK_CONN */
103 case HAMMER2_DBG_SHELL:
105 * We send the commands, not accept them.
106 * (one-way message, not transactional)
108 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_NOSUPP);
110 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
112 * A reply from the remote is data we copy to stdout.
113 * (one-way message, not transactional)
116 msg->aux_data[msg->aux_size - 1] = 0;
117 write(1, msg->aux_data, strlen(msg->aux_data));
120 case HAMMER2_LNK_CONN | HAMMER2_MSGF_CREATE:
121 fprintf(stderr, "Debug Shell is ignoring received LNK_CONN\n");
122 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_NOSUPP);
124 case HAMMER2_LNK_CONN | HAMMER2_MSGF_DELETE:
128 * Ignore any unknown messages, Terminate any unknown
129 * transactions with an error.
131 fprintf(stderr, "Unknown message: %s\n", hammer2_msg_str(msg));
132 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
133 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_NOSUPP);
134 if (msg->any.head.cmd & HAMMER2_MSGF_DELETE)
135 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_NOSUPP);
142 shell_ttymsg(hammer2_iocom_t *iocom)
148 if (fgets(buf, sizeof(buf), stdin) != NULL) {
150 if (len && buf[len - 1] == '\n')
153 msg = hammer2_msg_alloc(iocom, len, HAMMER2_DBG_SHELL);
154 bcopy(buf, msg->aux_data, len);
155 hammer2_msg_write(iocom, msg, NULL, NULL, NULL);
156 } else if (feof(stdin)) {
158 * Set EOF flag without setting any error code for normal
161 iocom->flags |= HAMMER2_IOCOMF_EOF;
168 * This is called from the master node to process a received debug
169 * shell command. We process the command, outputting the results,
170 * then finish up by outputting another prompt.
173 hammer2_msg_dbg(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
175 switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
176 case HAMMER2_DBG_SHELL:
178 * This is a command which we must process.
179 * When we are finished we generate a final reply.
182 msg->aux_data[msg->aux_size - 1] = 0;
183 hammer2_shell_parse(iocom, msg);
184 hammer2_msg_reply(iocom, msg, 0);
186 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
188 * A reply just prints out the string. No newline is added
189 * (it is expected to be embedded if desired).
192 msg->aux_data[msg->aux_size - 1] = 0;
194 write(2, msg->aux_data, strlen(msg->aux_data));
197 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_NOSUPP);
202 static void shell_span(hammer2_iocom_t *iocom, char *cmdbuf);
203 /*static void shell_tree(hammer2_iocom_t *iocom, char *cmdbuf);*/
206 hammer2_shell_parse(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
208 char *cmdbuf = msg->aux_data;
209 char *cmd = strsep(&cmdbuf, " \t");
211 if (cmd == NULL || *cmd == 0) {
213 } else if (strcmp(cmd, "span") == 0) {
214 shell_span(iocom, cmdbuf);
215 } else if (strcmp(cmd, "tree") == 0) {
216 shell_tree(iocom, cmdbuf);
217 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
218 iocom_printf(iocom, "help Command help\n");
219 iocom_printf(iocom, "span <host> Span to target host\n");
220 iocom_printf(iocom, "tree Dump spanning tree\n");
222 iocom_printf(iocom, "Unrecognized command: %s\n", cmd);
227 shell_span(hammer2_iocom_t *iocom, char *cmdbuf)
229 const char *hostname = strsep(&cmdbuf, " \t");
234 * Connect to the target
236 if (hostname == NULL) {
239 fd = hammer2_connect(hostname);
243 * Start master service
246 iocom_printf(iocom, "Connection to %s failed\n", hostname);
248 iocom_printf(iocom, "Connected to %s\n", hostname);
249 pthread_create(&thread, NULL,
250 master_service, (void *)(intptr_t)fd);
251 /*pthread_join(thread, &res);*/
256 * Returns text debug output to the original defined by (msg). (msg) is
257 * not modified and stays intact. We use a one-way message with REPLY set
258 * to distinguish between a debug command and debug terminal output.
260 * To prevent loops iocom_printf() can filter the message (cmd) related
261 * to the iocom_printf(). We filter out DBG messages.
264 iocom_printf(hammer2_iocom_t *iocom, const char *ctl, ...)
272 vsnprintf(buf, sizeof(buf), ctl, va);
274 len = strlen(buf) + 1;
276 rmsg = hammer2_msg_alloc(iocom, len, HAMMER2_DBG_SHELL |
278 bcopy(buf, rmsg->aux_data, len);
280 hammer2_msg_write(iocom, rmsg, NULL, NULL, NULL);
283 /************************************************************************
285 ************************************************************************
287 * Connect to the target manually (not via the cluster list embedded in
288 * a hammer2 filesystem) and initiate the SPAN protocol.
291 cmd_debugspan(const char *hostname)
298 * Connect to the target
300 fd = hammer2_connect(hostname);
304 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
305 pthread_create(&thread, NULL, master_service, (void *)(intptr_t)fd);
306 pthread_join(thread, &res);
310 /************************************************************************
312 ************************************************************************/
314 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
315 static void tabprintf(int tab, const char *ctl, ...);
318 cmd_show(const char *devpath)
320 hammer2_blockref_t broot;
323 fd = open(devpath, O_RDONLY);
328 bzero(&broot, sizeof(broot));
329 broot.type = HAMMER2_BREF_TYPE_VOLUME;
330 broot.data_off = 0 | HAMMER2_PBUFRADIX;
331 show_bref(fd, 0, 0, &broot);
338 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref)
340 hammer2_media_data_t media;
341 hammer2_blockref_t *bscan;
348 const char *type_str;
352 case HAMMER2_BREF_TYPE_EMPTY:
355 case HAMMER2_BREF_TYPE_INODE:
358 case HAMMER2_BREF_TYPE_INDIRECT:
361 case HAMMER2_BREF_TYPE_DATA:
364 case HAMMER2_BREF_TYPE_VOLUME:
368 type_str = "unknown";
373 tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
374 type_str, bi, (intmax_t)bref->data_off,
375 (intmax_t)bref->key, (intmax_t)bref->keybits,
376 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
379 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
380 if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) {
381 printf("(bad block size %zd)\n", bytes);
384 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
385 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
386 if (read(fd, &media, bytes) != (ssize_t)bytes) {
387 printf("(media read failed)\n");
397 case HAMMER2_BREF_TYPE_EMPTY:
400 case HAMMER2_BREF_TYPE_INODE:
402 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
405 bscan = &media.ipdata.u.blockset.blockref[0];
406 bcount = HAMMER2_SET_COUNT;
408 namelen = media.ipdata.name_len;
409 if (namelen > HAMMER2_INODE_MAXNAME)
411 tabprintf(tab, "filename \"%*.*s\"\n",
412 namelen, namelen, media.ipdata.filename);
413 tabprintf(tab, "version %d\n", media.ipdata.version);
414 tabprintf(tab, "uflags 0x%08x\n",
415 media.ipdata.uflags);
416 if (media.ipdata.rmajor || media.ipdata.rminor) {
417 tabprintf(tab, "rmajor %d\n",
418 media.ipdata.rmajor);
419 tabprintf(tab, "rminor %d\n",
420 media.ipdata.rminor);
422 tabprintf(tab, "ctime %s\n",
423 hammer2_time64_to_str(media.ipdata.ctime, &str));
424 tabprintf(tab, "mtime %s\n",
425 hammer2_time64_to_str(media.ipdata.mtime, &str));
426 tabprintf(tab, "atime %s\n",
427 hammer2_time64_to_str(media.ipdata.atime, &str));
428 tabprintf(tab, "btime %s\n",
429 hammer2_time64_to_str(media.ipdata.btime, &str));
430 tabprintf(tab, "uid %s\n",
431 hammer2_uuid_to_str(&media.ipdata.uid, &str));
432 tabprintf(tab, "gid %s\n",
433 hammer2_uuid_to_str(&media.ipdata.gid, &str));
434 tabprintf(tab, "type %s\n",
435 hammer2_iptype_to_str(media.ipdata.type));
436 tabprintf(tab, "opflgs 0x%02x\n",
437 media.ipdata.op_flags);
438 tabprintf(tab, "capflgs 0x%04x\n",
439 media.ipdata.cap_flags);
440 tabprintf(tab, "mode %-7o\n",
442 tabprintf(tab, "inum 0x%016jx\n",
444 tabprintf(tab, "size %ju\n",
445 (uintmax_t)media.ipdata.size);
446 tabprintf(tab, "nlinks %ju\n",
447 (uintmax_t)media.ipdata.nlinks);
448 tabprintf(tab, "iparent 0x%016jx\n",
449 (uintmax_t)media.ipdata.iparent);
450 tabprintf(tab, "name_key 0x%016jx\n",
451 (uintmax_t)media.ipdata.name_key);
452 tabprintf(tab, "name_len %u\n",
453 media.ipdata.name_len);
454 tabprintf(tab, "ncopies %u\n",
455 media.ipdata.ncopies);
456 tabprintf(tab, "compalg %u\n",
457 media.ipdata.comp_algo);
458 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
459 tabprintf(tab, "pfs_type %u (%s)\n",
460 media.ipdata.pfs_type,
461 hammer2_pfstype_to_str(media.ipdata.pfs_type));
462 tabprintf(tab, "pfs_inum 0x%016jx\n",
463 (uintmax_t)media.ipdata.pfs_inum);
464 tabprintf(tab, "pfs_clid %s\n",
465 hammer2_uuid_to_str(&media.ipdata.pfs_clid,
467 tabprintf(tab, "pfs_fsid %s\n",
468 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
471 tabprintf(tab, "data_quota %ju\n",
472 (uintmax_t)media.ipdata.data_quota);
473 tabprintf(tab, "data_count %ju\n",
474 (uintmax_t)media.ipdata.data_count);
475 tabprintf(tab, "inode_quota %ju\n",
476 (uintmax_t)media.ipdata.inode_quota);
477 tabprintf(tab, "inode_count %ju\n",
478 (uintmax_t)media.ipdata.inode_count);
479 tabprintf(tab, "attr_tid 0x%016jx\n",
480 (uintmax_t)media.ipdata.attr_tid);
481 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
482 tabprintf(tab, "dirent_tid %016jx\n",
483 (uintmax_t)media.ipdata.dirent_tid);
486 case HAMMER2_BREF_TYPE_INDIRECT:
487 bscan = &media.npdata.blockref[0];
488 bcount = bytes / sizeof(hammer2_blockref_t);
492 case HAMMER2_BREF_TYPE_DATA:
493 if (VerboseOpt >= 2) {
500 case HAMMER2_BREF_TYPE_VOLUME:
501 bscan = &media.voldata.sroot_blockset.blockref[0];
502 bcount = HAMMER2_SET_COUNT;
510 for (i = 0; i < bcount; ++i) {
511 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
516 show_bref(fd, tab, i, &bscan[i]);
521 if (bref->type == HAMMER2_BREF_TYPE_INODE)
522 tabprintf(tab, "} (%s.%d, \"%s\")\n",
523 type_str, bi, media.ipdata.filename);
525 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
531 tabprintf(int tab, const char *ctl, ...)
535 printf("%*.*s", tab, tab, "");