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 sockaddr_in lsin;
52 struct hammer2_iocom iocom;
58 * Acquire socket and set options
60 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
61 fprintf(stderr, "cmd_debug: socket(): %s\n",
67 * Connect to the target
69 bzero(&lsin, sizeof(lsin));
70 lsin.sin_family = AF_INET;
71 lsin.sin_addr.s_addr = 0;
72 lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
75 hen = gethostbyname2(hostname, AF_INET);
77 if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) {
79 "Cannot resolve %s\n", hostname);
83 bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length);
86 if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
88 fprintf(stderr, "debug: connect failed: %s\n",
94 * Run the session. The remote end transmits our prompt.
96 hammer2_iocom_init(&iocom, fd, 0, NULL, shell_rcvmsg, shell_ttymsg);
97 printf("debug: connected\n");
99 msg = hammer2_msg_alloc(&iocom, 0, HAMMER2_DBG_SHELL);
100 hammer2_msg_write(&iocom, msg, NULL, NULL, NULL);
101 hammer2_iocom_core(&iocom);
102 fprintf(stderr, "debug: disconnected\n");
108 * Callback from hammer2_iocom_core() when messages might be present
113 shell_rcvmsg(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
115 switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
116 case HAMMER2_LNK_ERROR:
117 case HAMMER2_LNK_ERROR | HAMMER2_MSGF_REPLY:
118 if (msg->any.head.error) {
121 msg->any.head.error);
123 write(1, "debug> ", 7);
126 case HAMMER2_DBG_SHELL:
128 * We send the commands, not accept them.
130 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_UNKNOWN);
132 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
134 * A reply from the remote is data we copy to stdout.
137 msg->aux_data[msg->aux_size - 1] = 0;
138 write(1, msg->aux_data, strlen(msg->aux_data));
142 fprintf(stderr, "Unknown message: %08x\n",
144 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
145 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_UNKNOWN);
152 shell_ttymsg(hammer2_iocom_t *iocom)
158 if (fgets(buf, sizeof(buf), stdin) != NULL) {
160 if (len && buf[len - 1] == '\n')
163 msg = hammer2_msg_alloc(iocom, len, HAMMER2_DBG_SHELL);
164 bcopy(buf, msg->aux_data, len);
165 hammer2_msg_write(iocom, msg, NULL, NULL, NULL);
168 * Set EOF flag without setting any error code for normal
171 iocom->flags |= HAMMER2_IOCOMF_EOF;
176 * This is called from the master node to process a received debug
177 * shell command. We process the command, outputting the results,
178 * then finish up by outputting another prompt.
181 hammer2_msg_dbg(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
183 switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
184 case HAMMER2_DBG_SHELL:
186 * This is a command which we must process.
187 * When we are finished we generate a final reply.
190 msg->aux_data[msg->aux_size - 1] = 0;
191 hammer2_shell_parse(iocom, msg);
192 hammer2_msg_reply(iocom, msg, 0);
194 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
196 * A reply just prints out the string. No newline is added
197 * (it is expected to be embedded if desired).
200 msg->aux_data[msg->aux_size - 1] = 0;
202 write(2, msg->aux_data, strlen(msg->aux_data));
205 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_UNKNOWN);
211 hammer2_shell_parse(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
213 char *cmdbuf = msg->aux_data;
214 char *cmd = strsep(&cmdbuf, " \t");
216 if (cmd == NULL || *cmd == 0) {
218 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
219 iocom_printf(iocom, 0, "help Command help\n");
221 iocom_printf(iocom, 0, "Unrecognized command: %s\n", cmd);
226 * Returns text debug output to the original defined by (msg). (msg) is
227 * not modified and stays intact. We use a one-way message with REPLY set
228 * to distinguish between a debug command and debug terminal output.
230 * To prevent loops iocom_printf() can filter the message (cmd) related
231 * to the iocom_printf(). We filter out DBG messages.
234 iocom_printf(hammer2_iocom_t *iocom, uint32_t cmd, const char *ctl, ...)
241 if ((cmd & HAMMER2_MSGF_PROTOS) == HAMMER2_MSG_PROTO_DBG)
245 vsnprintf(buf, sizeof(buf), ctl, va);
247 len = strlen(buf) + 1;
249 rmsg = hammer2_msg_alloc(iocom, len, HAMMER2_DBG_SHELL |
251 bcopy(buf, rmsg->aux_data, len);
253 hammer2_msg_write(iocom, rmsg, NULL, NULL, NULL);
256 /************************************************************************
258 ************************************************************************/
260 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
261 static void tabprintf(int tab, const char *ctl, ...);
264 cmd_show(const char *devpath)
266 hammer2_blockref_t broot;
269 fd = open(devpath, O_RDONLY);
274 bzero(&broot, sizeof(broot));
275 broot.type = HAMMER2_BREF_TYPE_VOLUME;
276 broot.data_off = 0 | HAMMER2_PBUFRADIX;
277 show_bref(fd, 0, 0, &broot);
284 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref)
286 hammer2_media_data_t media;
287 hammer2_blockref_t *bscan;
294 const char *type_str;
298 case HAMMER2_BREF_TYPE_EMPTY:
301 case HAMMER2_BREF_TYPE_INODE:
304 case HAMMER2_BREF_TYPE_INDIRECT:
307 case HAMMER2_BREF_TYPE_DATA:
310 case HAMMER2_BREF_TYPE_VOLUME:
314 type_str = "unknown";
319 tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
320 type_str, bi, (intmax_t)bref->data_off,
321 (intmax_t)bref->key, (intmax_t)bref->keybits,
322 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
325 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
326 if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) {
327 printf("(bad block size %zd)\n", bytes);
330 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
331 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
332 if (read(fd, &media, bytes) != (ssize_t)bytes) {
333 printf("(media read failed)\n");
343 case HAMMER2_BREF_TYPE_EMPTY:
346 case HAMMER2_BREF_TYPE_INODE:
348 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
351 bscan = &media.ipdata.u.blockset.blockref[0];
352 bcount = HAMMER2_SET_COUNT;
354 namelen = media.ipdata.name_len;
355 if (namelen > HAMMER2_INODE_MAXNAME)
357 tabprintf(tab, "filename \"%*.*s\"\n",
358 namelen, namelen, media.ipdata.filename);
359 tabprintf(tab, "version %d\n", media.ipdata.version);
360 tabprintf(tab, "uflags 0x%08x\n",
361 media.ipdata.uflags);
362 if (media.ipdata.rmajor || media.ipdata.rminor) {
363 tabprintf(tab, "rmajor %d\n",
364 media.ipdata.rmajor);
365 tabprintf(tab, "rminor %d\n",
366 media.ipdata.rminor);
368 tabprintf(tab, "ctime %s\n",
369 hammer2_time64_to_str(media.ipdata.ctime, &str));
370 tabprintf(tab, "mtime %s\n",
371 hammer2_time64_to_str(media.ipdata.mtime, &str));
372 tabprintf(tab, "atime %s\n",
373 hammer2_time64_to_str(media.ipdata.atime, &str));
374 tabprintf(tab, "btime %s\n",
375 hammer2_time64_to_str(media.ipdata.btime, &str));
376 tabprintf(tab, "uid %s\n",
377 hammer2_uuid_to_str(&media.ipdata.uid, &str));
378 tabprintf(tab, "gid %s\n",
379 hammer2_uuid_to_str(&media.ipdata.gid, &str));
380 tabprintf(tab, "type %s\n",
381 hammer2_iptype_to_str(media.ipdata.type));
382 tabprintf(tab, "opflgs 0x%02x\n",
383 media.ipdata.op_flags);
384 tabprintf(tab, "capflgs 0x%04x\n",
385 media.ipdata.cap_flags);
386 tabprintf(tab, "mode %-7o\n",
388 tabprintf(tab, "inum 0x%016jx\n",
390 tabprintf(tab, "size %ju\n",
391 (uintmax_t)media.ipdata.size);
392 tabprintf(tab, "nlinks %ju\n",
393 (uintmax_t)media.ipdata.nlinks);
394 tabprintf(tab, "iparent 0x%016jx\n",
395 (uintmax_t)media.ipdata.iparent);
396 tabprintf(tab, "name_key 0x%016jx\n",
397 (uintmax_t)media.ipdata.name_key);
398 tabprintf(tab, "name_len %u\n",
399 media.ipdata.name_len);
400 tabprintf(tab, "ncopies %u\n",
401 media.ipdata.ncopies);
402 tabprintf(tab, "compalg %u\n",
403 media.ipdata.comp_algo);
404 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
405 tabprintf(tab, "pfs_type %u (%s)\n",
406 media.ipdata.pfs_type,
407 hammer2_pfstype_to_str(media.ipdata.pfs_type));
408 tabprintf(tab, "pfs_inum 0x%016jx\n",
409 (uintmax_t)media.ipdata.pfs_inum);
410 tabprintf(tab, "pfs_clid %s\n",
411 hammer2_uuid_to_str(&media.ipdata.pfs_clid,
413 tabprintf(tab, "pfs_fsid %s\n",
414 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
417 tabprintf(tab, "data_quota %ju\n",
418 (uintmax_t)media.ipdata.data_quota);
419 tabprintf(tab, "data_count %ju\n",
420 (uintmax_t)media.ipdata.data_count);
421 tabprintf(tab, "inode_quota %ju\n",
422 (uintmax_t)media.ipdata.inode_quota);
423 tabprintf(tab, "inode_count %ju\n",
424 (uintmax_t)media.ipdata.inode_count);
425 tabprintf(tab, "attr_tid 0x%016jx\n",
426 (uintmax_t)media.ipdata.attr_tid);
427 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
428 tabprintf(tab, "dirent_tid %016jx\n",
429 (uintmax_t)media.ipdata.dirent_tid);
432 case HAMMER2_BREF_TYPE_INDIRECT:
433 bscan = &media.npdata.blockref[0];
434 bcount = bytes / sizeof(hammer2_blockref_t);
438 case HAMMER2_BREF_TYPE_DATA:
439 if (VerboseOpt >= 2) {
446 case HAMMER2_BREF_TYPE_VOLUME:
447 bscan = &media.voldata.sroot_blockset.blockref[0];
448 bcount = HAMMER2_SET_COUNT;
456 for (i = 0; i < bcount; ++i) {
457 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
462 show_bref(fd, tab, i, &bscan[i]);
467 if (bref->type == HAMMER2_BREF_TYPE_INODE)
468 tabprintf(tab, "} (%s.%d, \"%s\")\n",
469 type_str, bi, media.ipdata.filename);
471 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
477 tabprintf(int tab, const char *ctl, ...)
481 printf("%*.*s", tab, tab, "");