hammer2 - dmsg blockdev work
[dragonfly.git] / sbin / hammer2 / cmd_debug.c
CommitLineData
9ab15106
MD
1/*
2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
3 *
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>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
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
17 * distribution.
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.
21 *
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
33 * SUCH DAMAGE.
34 */
35
36#include "hammer2.h"
37
bd878fc9
MD
38#define SHOW_TAB 2
39
0c3a8cd0
MD
40static void shell_rcvmsg(dmsg_msg_t *msg);
41static void shell_ttymsg(dmsg_iocom_t *iocom);
bd878fc9
MD
42
43/************************************************************************
44 * SHELL *
45 ************************************************************************/
9ab15106
MD
46
47int
bd878fc9 48cmd_shell(const char *hostname)
9ab15106 49{
0c3a8cd0
MD
50 struct dmsg_iocom iocom;
51 dmsg_msg_t *msg;
9ab15106
MD
52 int fd;
53
9ab15106
MD
54 /*
55 * Connect to the target
56 */
0c3a8cd0 57 fd = dmsg_connect(hostname);
02454b3e
MD
58 if (fd < 0)
59 return 1;
9ab15106
MD
60
61 /*
62 * Run the session. The remote end transmits our prompt.
63 */
11f7caf4
MD
64 dmsg_iocom_init(&iocom, fd, 0,
65 NULL,
66 shell_rcvmsg,
67 hammer2_shell_parse,
68 shell_ttymsg);
02454b3e 69 fcntl(0, F_SETFL, O_NONBLOCK);
9ab15106
MD
70 printf("debug: connected\n");
71
0c3a8cd0
MD
72 msg = dmsg_msg_alloc(iocom.router, 0, DMSG_DBG_SHELL, NULL, NULL);
73 dmsg_msg_write(msg);
74 dmsg_iocom_core(&iocom);
9ab15106
MD
75 fprintf(stderr, "debug: disconnected\n");
76 close(fd);
77 return 0;
78}
79
80/*
0c3a8cd0 81 * Callback from dmsg_iocom_core() when messages might be present
9ab15106
MD
82 * on the socket.
83 */
84static
85void
0c3a8cd0 86shell_rcvmsg(dmsg_msg_t *msg)
9ab15106 87{
5bc5bca2
MD
88 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
89 case DMSG_LNK_ERROR:
90 case DMSG_LNK_ERROR | DMSGF_REPLY:
81666e1b
MD
91 /*
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.
95 */
5903497c 96 if (msg->any.head.error) {
81666e1b 97 fprintf(stderr, "Stream failure: %s\n",
0c3a8cd0 98 dmsg_msg_str(msg));
5903497c
MD
99 } else {
100 write(1, "debug> ", 7);
9ab15106 101 }
5903497c 102 break;
5bc5bca2 103 case DMSG_LNK_ERROR | DMSGF_DELETE:
81666e1b
MD
104 /* ignore termination of LNK_CONN */
105 break;
5bc5bca2 106 case DMSG_DBG_SHELL:
5903497c
MD
107 /*
108 * We send the commands, not accept them.
81666e1b 109 * (one-way message, not transactional)
5903497c 110 */
0c3a8cd0 111 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
5903497c 112 break;
5bc5bca2 113 case DMSG_DBG_SHELL | DMSGF_REPLY:
5903497c
MD
114 /*
115 * A reply from the remote is data we copy to stdout.
81666e1b 116 * (one-way message, not transactional)
5903497c
MD
117 */
118 if (msg->aux_size) {
119 msg->aux_data[msg->aux_size - 1] = 0;
120 write(1, msg->aux_data, strlen(msg->aux_data));
121 }
122 break;
5bc5bca2 123 case DMSG_LNK_CONN | DMSGF_CREATE:
81666e1b 124 fprintf(stderr, "Debug Shell is ignoring received LNK_CONN\n");
0c3a8cd0 125 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
81666e1b 126 break;
5bc5bca2 127 case DMSG_LNK_CONN | DMSGF_DELETE:
81666e1b 128 break;
5903497c 129 default:
81666e1b
MD
130 /*
131 * Ignore any unknown messages, Terminate any unknown
132 * transactions with an error.
133 */
0c3a8cd0 134 fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg));
5bc5bca2 135 if (msg->any.head.cmd & DMSGF_CREATE)
0c3a8cd0 136 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
5bc5bca2 137 if (msg->any.head.cmd & DMSGF_DELETE)
0c3a8cd0 138 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
5903497c 139 break;
9ab15106
MD
140 }
141}
142
9ab15106
MD
143static
144void
0c3a8cd0 145shell_ttymsg(dmsg_iocom_t *iocom)
9ab15106 146{
0c3a8cd0 147 dmsg_msg_t *msg;
9ab15106
MD
148 char buf[256];
149 size_t len;
150
151 if (fgets(buf, sizeof(buf), stdin) != NULL) {
152 len = strlen(buf);
153 if (len && buf[len - 1] == '\n')
154 buf[--len] = 0;
155 ++len;
0c3a8cd0
MD
156 msg = dmsg_msg_alloc(iocom->router, len, DMSG_DBG_SHELL,
157 NULL, NULL);
9ab15106 158 bcopy(buf, msg->aux_data, len);
0c3a8cd0 159 dmsg_msg_write(msg);
02454b3e 160 } else if (feof(stdin)) {
9ab15106
MD
161 /*
162 * Set EOF flag without setting any error code for normal
163 * EOF.
164 */
0c3a8cd0 165 iocom->flags |= DMSG_IOCOMF_EOF;
02454b3e
MD
166 } else {
167 clearerr(stdin);
9ab15106
MD
168 }
169}
170
0c3a8cd0 171static void shell_span(dmsg_router_t *router, char *cmdbuf);
81666e1b 172
11f7caf4 173void
0c3a8cd0 174hammer2_shell_parse(dmsg_msg_t *msg)
9ab15106 175{
0c3a8cd0 176 dmsg_router_t *router = msg->router;
78476205 177 char *cmdbuf = msg->aux_data;
9ab15106
MD
178 char *cmd = strsep(&cmdbuf, " \t");
179
180 if (cmd == NULL || *cmd == 0) {
181 ;
02454b3e 182 } else if (strcmp(cmd, "span") == 0) {
29ead430 183 shell_span(router, cmdbuf);
81666e1b 184 } else if (strcmp(cmd, "tree") == 0) {
0c3a8cd0 185 dmsg_shell_tree(router, cmdbuf); /* dump spanning tree */
81666e1b 186 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
0c3a8cd0
MD
187 dmsg_router_printf(router, "help Command help\n");
188 dmsg_router_printf(router, "span <host> Span to target host\n");
189 dmsg_router_printf(router, "tree Dump spanning tree\n");
81666e1b 190 } else {
0c3a8cd0 191 dmsg_router_printf(router, "Unrecognized command: %s\n", cmd);
81666e1b
MD
192 }
193}
02454b3e 194
81666e1b 195static void
0c3a8cd0 196shell_span(dmsg_router_t *router, char *cmdbuf)
81666e1b 197{
ddfbb283 198 dmsg_master_service_info_t *info;
81666e1b
MD
199 const char *hostname = strsep(&cmdbuf, " \t");
200 pthread_t thread;
201 int fd;
02454b3e 202
81666e1b
MD
203 /*
204 * Connect to the target
205 */
206 if (hostname == NULL) {
207 fd = -1;
208 } else {
0c3a8cd0 209 fd = dmsg_connect(hostname);
81666e1b
MD
210 }
211
212 /*
213 * Start master service
214 */
215 if (fd < 0) {
0c3a8cd0 216 dmsg_router_printf(router, "Connection to %s failed\n", hostname);
9ab15106 217 } else {
0c3a8cd0 218 dmsg_router_printf(router, "Connected to %s\n", hostname);
ddfbb283
MD
219
220 info = malloc(sizeof(*info));
221 bzero(info, sizeof(*info));
222 info->fd = fd;
223 info->detachme = 1;
224 info->dbgmsg_callback = hammer2_shell_parse;
225
226 pthread_create(&thread, NULL, dmsg_master_service, info);
81666e1b 227 /*pthread_join(thread, &res);*/
9ab15106
MD
228 }
229}
230
02454b3e
MD
231/************************************************************************
232 * DEBUGSPAN *
233 ************************************************************************
234 *
235 * Connect to the target manually (not via the cluster list embedded in
236 * a hammer2 filesystem) and initiate the SPAN protocol.
237 */
238int
239cmd_debugspan(const char *hostname)
240{
241 pthread_t thread;
242 int fd;
243 void *res;
244
245 /*
246 * Connect to the target
247 */
0c3a8cd0 248 fd = dmsg_connect(hostname);
02454b3e
MD
249 if (fd < 0)
250 return 1;
251
252 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
0c3a8cd0
MD
253 pthread_create(&thread, NULL,
254 dmsg_master_service, (void *)(intptr_t)fd);
02454b3e
MD
255 pthread_join(thread, &res);
256 return(0);
257}
258
bd878fc9
MD
259/************************************************************************
260 * SHOW *
261 ************************************************************************/
262
263static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
264static void tabprintf(int tab, const char *ctl, ...);
265
266int
267cmd_show(const char *devpath)
268{
269 hammer2_blockref_t broot;
270 int fd;
271
272 fd = open(devpath, O_RDONLY);
273 if (fd < 0) {
274 perror("open");
275 return 1;
276 }
277 bzero(&broot, sizeof(broot));
278 broot.type = HAMMER2_BREF_TYPE_VOLUME;
279 broot.data_off = 0 | HAMMER2_PBUFRADIX;
280 show_bref(fd, 0, 0, &broot);
281 close(fd);
282
283 return 0;
284}
285
286static void
287show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref)
288{
289 hammer2_media_data_t media;
290 hammer2_blockref_t *bscan;
291 int bcount;
292 int i;
293 int didnl;
ad7cf8ea 294 int namelen;
bd878fc9
MD
295 int obrace = 1;
296 size_t bytes;
297 const char *type_str;
298 char *str = NULL;
299
300 switch(bref->type) {
301 case HAMMER2_BREF_TYPE_EMPTY:
302 type_str = "empty";
303 break;
304 case HAMMER2_BREF_TYPE_INODE:
305 type_str = "inode";
306 break;
307 case HAMMER2_BREF_TYPE_INDIRECT:
308 type_str = "indblk";
309 break;
310 case HAMMER2_BREF_TYPE_DATA:
311 type_str = "data";
312 break;
313 case HAMMER2_BREF_TYPE_VOLUME:
314 type_str = "volume";
315 break;
316 default:
317 type_str = "unknown";
318 break;
319 }
320
321
ad7cf8ea
MD
322 tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
323 type_str, bi, (intmax_t)bref->data_off,
324 (intmax_t)bref->key, (intmax_t)bref->keybits,
325 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
bd878fc9
MD
326 tab += SHOW_TAB;
327
328 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
ad7cf8ea 329 if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) {
bd878fc9
MD
330 printf("(bad block size %zd)\n", bytes);
331 return;
332 }
333 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
334 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
335 if (read(fd, &media, bytes) != (ssize_t)bytes) {
336 printf("(media read failed)\n");
337 return;
338 }
339 }
340
341 bscan = NULL;
342 bcount = 0;
343 didnl = 0;
344
345 switch(bref->type) {
346 case HAMMER2_BREF_TYPE_EMPTY:
347 obrace = 0;
348 break;
349 case HAMMER2_BREF_TYPE_INODE:
350 printf("{\n");
351 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
352 /* no blockrefs */
353 } else {
354 bscan = &media.ipdata.u.blockset.blockref[0];
355 bcount = HAMMER2_SET_COUNT;
356 }
ad7cf8ea
MD
357 namelen = media.ipdata.name_len;
358 if (namelen > HAMMER2_INODE_MAXNAME)
359 namelen = 0;
360 tabprintf(tab, "filename \"%*.*s\"\n",
361 namelen, namelen, media.ipdata.filename);
bd878fc9
MD
362 tabprintf(tab, "version %d\n", media.ipdata.version);
363 tabprintf(tab, "uflags 0x%08x\n",
364 media.ipdata.uflags);
365 if (media.ipdata.rmajor || media.ipdata.rminor) {
366 tabprintf(tab, "rmajor %d\n",
367 media.ipdata.rmajor);
368 tabprintf(tab, "rminor %d\n",
369 media.ipdata.rminor);
370 }
371 tabprintf(tab, "ctime %s\n",
372 hammer2_time64_to_str(media.ipdata.ctime, &str));
373 tabprintf(tab, "mtime %s\n",
374 hammer2_time64_to_str(media.ipdata.mtime, &str));
375 tabprintf(tab, "atime %s\n",
376 hammer2_time64_to_str(media.ipdata.atime, &str));
377 tabprintf(tab, "btime %s\n",
378 hammer2_time64_to_str(media.ipdata.btime, &str));
379 tabprintf(tab, "uid %s\n",
380 hammer2_uuid_to_str(&media.ipdata.uid, &str));
381 tabprintf(tab, "gid %s\n",
382 hammer2_uuid_to_str(&media.ipdata.gid, &str));
383 tabprintf(tab, "type %s\n",
384 hammer2_iptype_to_str(media.ipdata.type));
385 tabprintf(tab, "opflgs 0x%02x\n",
386 media.ipdata.op_flags);
387 tabprintf(tab, "capflgs 0x%04x\n",
388 media.ipdata.cap_flags);
389 tabprintf(tab, "mode %-7o\n",
390 media.ipdata.mode);
391 tabprintf(tab, "inum 0x%016jx\n",
392 media.ipdata.inum);
393 tabprintf(tab, "size %ju\n",
394 (uintmax_t)media.ipdata.size);
395 tabprintf(tab, "nlinks %ju\n",
396 (uintmax_t)media.ipdata.nlinks);
397 tabprintf(tab, "iparent 0x%016jx\n",
398 (uintmax_t)media.ipdata.iparent);
399 tabprintf(tab, "name_key 0x%016jx\n",
400 (uintmax_t)media.ipdata.name_key);
401 tabprintf(tab, "name_len %u\n",
402 media.ipdata.name_len);
403 tabprintf(tab, "ncopies %u\n",
404 media.ipdata.ncopies);
405 tabprintf(tab, "compalg %u\n",
406 media.ipdata.comp_algo);
407 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
408 tabprintf(tab, "pfs_type %u (%s)\n",
409 media.ipdata.pfs_type,
410 hammer2_pfstype_to_str(media.ipdata.pfs_type));
411 tabprintf(tab, "pfs_inum 0x%016jx\n",
412 (uintmax_t)media.ipdata.pfs_inum);
8c280d5d
MD
413 tabprintf(tab, "pfs_clid %s\n",
414 hammer2_uuid_to_str(&media.ipdata.pfs_clid,
bd878fc9
MD
415 &str));
416 tabprintf(tab, "pfs_fsid %s\n",
417 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
418 &str));
419 }
420 tabprintf(tab, "data_quota %ju\n",
421 (uintmax_t)media.ipdata.data_quota);
422 tabprintf(tab, "data_count %ju\n",
423 (uintmax_t)media.ipdata.data_count);
424 tabprintf(tab, "inode_quota %ju\n",
425 (uintmax_t)media.ipdata.inode_quota);
426 tabprintf(tab, "inode_count %ju\n",
427 (uintmax_t)media.ipdata.inode_count);
428 tabprintf(tab, "attr_tid 0x%016jx\n",
429 (uintmax_t)media.ipdata.attr_tid);
430 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
431 tabprintf(tab, "dirent_tid %016jx\n",
432 (uintmax_t)media.ipdata.dirent_tid);
433 }
434 break;
435 case HAMMER2_BREF_TYPE_INDIRECT:
436 bscan = &media.npdata.blockref[0];
437 bcount = bytes / sizeof(hammer2_blockref_t);
438 didnl = 1;
439 printf("{\n");
440 break;
441 case HAMMER2_BREF_TYPE_DATA:
442 if (VerboseOpt >= 2) {
443 printf("{\n");
444 } else {
445 printf("\n");
446 obrace = 0;
447 }
448 break;
449 case HAMMER2_BREF_TYPE_VOLUME:
450 bscan = &media.voldata.sroot_blockset.blockref[0];
451 bcount = HAMMER2_SET_COUNT;
452 printf("{\n");
453 break;
454 default:
455 break;
456 }
457 if (str)
458 free(str);
459 for (i = 0; i < bcount; ++i) {
460 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
461 if (didnl == 0) {
462 printf("\n");
463 didnl = 1;
464 }
465 show_bref(fd, tab, i, &bscan[i]);
466 }
467 }
468 tab -= SHOW_TAB;
469 if (obrace) {
470 if (bref->type == HAMMER2_BREF_TYPE_INODE)
471 tabprintf(tab, "} (%s.%d, \"%s\")\n",
472 type_str, bi, media.ipdata.filename);
473 else
474 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
475 }
476}
477
478static
479void
480tabprintf(int tab, const char *ctl, ...)
481{
482 va_list va;
483
484 printf("%*.*s", tab, tab, "");
485 va_start(va, ctl);
486 vprintf(ctl, va);
487 va_end(va);
488}