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);
42 static void hammer2_shell_parse(dmsg_msg_t *msg);
44 /************************************************************************
46 ************************************************************************/
49 cmd_shell(const char *hostname)
51 struct dmsg_iocom iocom;
56 * Connect to the target
58 fd = dmsg_connect(hostname);
63 * Run the session. The remote end transmits our prompt.
65 dmsg_iocom_init(&iocom, fd, 0, NULL, shell_rcvmsg, shell_ttymsg);
66 iocom.router->dbgmsg_callback = hammer2_shell_parse;
67 fcntl(0, F_SETFL, O_NONBLOCK);
68 printf("debug: connected\n");
70 msg = dmsg_msg_alloc(iocom.router, 0, DMSG_DBG_SHELL, NULL, NULL);
72 dmsg_iocom_core(&iocom);
73 fprintf(stderr, "debug: disconnected\n");
79 * Callback from dmsg_iocom_core() when messages might be present
84 shell_rcvmsg(dmsg_msg_t *msg)
86 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
88 case DMSG_LNK_ERROR | DMSGF_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",
98 write(1, "debug> ", 7);
101 case DMSG_LNK_ERROR | DMSGF_DELETE:
102 /* ignore termination of LNK_CONN */
106 * We send the commands, not accept them.
107 * (one-way message, not transactional)
109 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
111 case DMSG_DBG_SHELL | DMSGF_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 DMSG_LNK_CONN | DMSGF_CREATE:
122 fprintf(stderr, "Debug Shell is ignoring received LNK_CONN\n");
123 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
125 case DMSG_LNK_CONN | DMSGF_DELETE:
129 * Ignore any unknown messages, Terminate any unknown
130 * transactions with an error.
132 fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg));
133 if (msg->any.head.cmd & DMSGF_CREATE)
134 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
135 if (msg->any.head.cmd & DMSGF_DELETE)
136 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
143 shell_ttymsg(dmsg_iocom_t *iocom)
149 if (fgets(buf, sizeof(buf), stdin) != NULL) {
151 if (len && buf[len - 1] == '\n')
154 msg = dmsg_msg_alloc(iocom->router, len, DMSG_DBG_SHELL,
156 bcopy(buf, msg->aux_data, len);
158 } else if (feof(stdin)) {
160 * Set EOF flag without setting any error code for normal
163 iocom->flags |= DMSG_IOCOMF_EOF;
169 static void shell_span(dmsg_router_t *router, char *cmdbuf);
172 hammer2_shell_parse(dmsg_msg_t *msg)
174 dmsg_router_t *router = msg->router;
175 char *cmdbuf = msg->aux_data;
176 char *cmd = strsep(&cmdbuf, " \t");
178 if (cmd == NULL || *cmd == 0) {
180 } else if (strcmp(cmd, "span") == 0) {
181 shell_span(router, cmdbuf);
182 } else if (strcmp(cmd, "tree") == 0) {
183 dmsg_shell_tree(router, cmdbuf); /* dump spanning tree */
184 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
185 dmsg_router_printf(router, "help Command help\n");
186 dmsg_router_printf(router, "span <host> Span to target host\n");
187 dmsg_router_printf(router, "tree Dump spanning tree\n");
189 dmsg_router_printf(router, "Unrecognized command: %s\n", cmd);
194 shell_span(dmsg_router_t *router, char *cmdbuf)
196 const char *hostname = strsep(&cmdbuf, " \t");
201 * Connect to the target
203 if (hostname == NULL) {
206 fd = dmsg_connect(hostname);
210 * Start master service
213 dmsg_router_printf(router, "Connection to %s failed\n", hostname);
215 dmsg_router_printf(router, "Connected to %s\n", hostname);
216 pthread_create(&thread, NULL,
217 dmsg_master_service, (void *)(intptr_t)fd);
218 /*pthread_join(thread, &res);*/
222 /************************************************************************
224 ************************************************************************
226 * Connect to the target manually (not via the cluster list embedded in
227 * a hammer2 filesystem) and initiate the SPAN protocol.
230 cmd_debugspan(const char *hostname)
237 * Connect to the target
239 fd = dmsg_connect(hostname);
243 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
244 pthread_create(&thread, NULL,
245 dmsg_master_service, (void *)(intptr_t)fd);
246 pthread_join(thread, &res);
250 /************************************************************************
252 ************************************************************************/
254 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
255 static void tabprintf(int tab, const char *ctl, ...);
258 cmd_show(const char *devpath)
260 hammer2_blockref_t broot;
263 fd = open(devpath, O_RDONLY);
268 bzero(&broot, sizeof(broot));
269 broot.type = HAMMER2_BREF_TYPE_VOLUME;
270 broot.data_off = 0 | HAMMER2_PBUFRADIX;
271 show_bref(fd, 0, 0, &broot);
278 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref)
280 hammer2_media_data_t media;
281 hammer2_blockref_t *bscan;
288 const char *type_str;
292 case HAMMER2_BREF_TYPE_EMPTY:
295 case HAMMER2_BREF_TYPE_INODE:
298 case HAMMER2_BREF_TYPE_INDIRECT:
301 case HAMMER2_BREF_TYPE_DATA:
304 case HAMMER2_BREF_TYPE_VOLUME:
308 type_str = "unknown";
313 tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
314 type_str, bi, (intmax_t)bref->data_off,
315 (intmax_t)bref->key, (intmax_t)bref->keybits,
316 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
319 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
320 if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) {
321 printf("(bad block size %zd)\n", bytes);
324 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
325 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
326 if (read(fd, &media, bytes) != (ssize_t)bytes) {
327 printf("(media read failed)\n");
337 case HAMMER2_BREF_TYPE_EMPTY:
340 case HAMMER2_BREF_TYPE_INODE:
342 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
345 bscan = &media.ipdata.u.blockset.blockref[0];
346 bcount = HAMMER2_SET_COUNT;
348 namelen = media.ipdata.name_len;
349 if (namelen > HAMMER2_INODE_MAXNAME)
351 tabprintf(tab, "filename \"%*.*s\"\n",
352 namelen, namelen, media.ipdata.filename);
353 tabprintf(tab, "version %d\n", media.ipdata.version);
354 tabprintf(tab, "uflags 0x%08x\n",
355 media.ipdata.uflags);
356 if (media.ipdata.rmajor || media.ipdata.rminor) {
357 tabprintf(tab, "rmajor %d\n",
358 media.ipdata.rmajor);
359 tabprintf(tab, "rminor %d\n",
360 media.ipdata.rminor);
362 tabprintf(tab, "ctime %s\n",
363 hammer2_time64_to_str(media.ipdata.ctime, &str));
364 tabprintf(tab, "mtime %s\n",
365 hammer2_time64_to_str(media.ipdata.mtime, &str));
366 tabprintf(tab, "atime %s\n",
367 hammer2_time64_to_str(media.ipdata.atime, &str));
368 tabprintf(tab, "btime %s\n",
369 hammer2_time64_to_str(media.ipdata.btime, &str));
370 tabprintf(tab, "uid %s\n",
371 hammer2_uuid_to_str(&media.ipdata.uid, &str));
372 tabprintf(tab, "gid %s\n",
373 hammer2_uuid_to_str(&media.ipdata.gid, &str));
374 tabprintf(tab, "type %s\n",
375 hammer2_iptype_to_str(media.ipdata.type));
376 tabprintf(tab, "opflgs 0x%02x\n",
377 media.ipdata.op_flags);
378 tabprintf(tab, "capflgs 0x%04x\n",
379 media.ipdata.cap_flags);
380 tabprintf(tab, "mode %-7o\n",
382 tabprintf(tab, "inum 0x%016jx\n",
384 tabprintf(tab, "size %ju\n",
385 (uintmax_t)media.ipdata.size);
386 tabprintf(tab, "nlinks %ju\n",
387 (uintmax_t)media.ipdata.nlinks);
388 tabprintf(tab, "iparent 0x%016jx\n",
389 (uintmax_t)media.ipdata.iparent);
390 tabprintf(tab, "name_key 0x%016jx\n",
391 (uintmax_t)media.ipdata.name_key);
392 tabprintf(tab, "name_len %u\n",
393 media.ipdata.name_len);
394 tabprintf(tab, "ncopies %u\n",
395 media.ipdata.ncopies);
396 tabprintf(tab, "compalg %u\n",
397 media.ipdata.comp_algo);
398 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
399 tabprintf(tab, "pfs_type %u (%s)\n",
400 media.ipdata.pfs_type,
401 hammer2_pfstype_to_str(media.ipdata.pfs_type));
402 tabprintf(tab, "pfs_inum 0x%016jx\n",
403 (uintmax_t)media.ipdata.pfs_inum);
404 tabprintf(tab, "pfs_clid %s\n",
405 hammer2_uuid_to_str(&media.ipdata.pfs_clid,
407 tabprintf(tab, "pfs_fsid %s\n",
408 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
411 tabprintf(tab, "data_quota %ju\n",
412 (uintmax_t)media.ipdata.data_quota);
413 tabprintf(tab, "data_count %ju\n",
414 (uintmax_t)media.ipdata.data_count);
415 tabprintf(tab, "inode_quota %ju\n",
416 (uintmax_t)media.ipdata.inode_quota);
417 tabprintf(tab, "inode_count %ju\n",
418 (uintmax_t)media.ipdata.inode_count);
419 tabprintf(tab, "attr_tid 0x%016jx\n",
420 (uintmax_t)media.ipdata.attr_tid);
421 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
422 tabprintf(tab, "dirent_tid %016jx\n",
423 (uintmax_t)media.ipdata.dirent_tid);
426 case HAMMER2_BREF_TYPE_INDIRECT:
427 bscan = &media.npdata.blockref[0];
428 bcount = bytes / sizeof(hammer2_blockref_t);
432 case HAMMER2_BREF_TYPE_DATA:
433 if (VerboseOpt >= 2) {
440 case HAMMER2_BREF_TYPE_VOLUME:
441 bscan = &media.voldata.sroot_blockset.blockref[0];
442 bcount = HAMMER2_SET_COUNT;
450 for (i = 0; i < bcount; ++i) {
451 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
456 show_bref(fd, tab, i, &bscan[i]);
461 if (bref->type == HAMMER2_BREF_TYPE_INODE)
462 tabprintf(tab, "} (%s.%d, \"%s\")\n",
463 type_str, bi, media.ipdata.filename);
465 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
471 tabprintf(int tab, const char *ctl, ...)
475 printf("%*.*s", tab, tab, "");