cluster - Major kernel component work (diskiocom, xdisk, kdmsg)
[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
ce48a6c5 72 msg = dmsg_msg_alloc(&iocom.circuit0, 0, DMSG_DBG_SHELL, NULL, NULL);
0c3a8cd0
MD
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;
ce48a6c5 156 msg = dmsg_msg_alloc(&iocom->circuit0, len, DMSG_DBG_SHELL,
0c3a8cd0 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
ce48a6c5 171static void shell_span(dmsg_circuit_t *circuit, char *cmdbuf);
81666e1b 172
11f7caf4 173void
0c3a8cd0 174hammer2_shell_parse(dmsg_msg_t *msg)
9ab15106 175{
ce48a6c5 176 dmsg_circuit_t *circuit = msg->circuit;
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) {
ce48a6c5 183 shell_span(circuit, cmdbuf);
8d6d37b8
MD
184 } else if (strcmp(cmd, "circ") == 0) {
185 shell_circ(circuit, cmdbuf);
81666e1b 186 } else if (strcmp(cmd, "tree") == 0) {
ce48a6c5 187 dmsg_shell_tree(circuit, cmdbuf); /* dump spanning tree */
81666e1b 188 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
ce48a6c5
MD
189 dmsg_circuit_printf(circuit, "help Command help\n");
190 dmsg_circuit_printf(circuit, "span <host> Span to target host\n");
191 dmsg_circuit_printf(circuit, "tree Dump spanning tree\n");
81666e1b 192 } else {
ce48a6c5 193 dmsg_circuit_printf(circuit, "Unrecognized command: %s\n", cmd);
81666e1b
MD
194 }
195}
02454b3e 196
81666e1b 197static void
ce48a6c5 198shell_span(dmsg_circuit_t *circuit, char *cmdbuf)
81666e1b 199{
ddfbb283 200 dmsg_master_service_info_t *info;
81666e1b
MD
201 const char *hostname = strsep(&cmdbuf, " \t");
202 pthread_t thread;
203 int fd;
02454b3e 204
81666e1b
MD
205 /*
206 * Connect to the target
207 */
208 if (hostname == NULL) {
209 fd = -1;
210 } else {
0c3a8cd0 211 fd = dmsg_connect(hostname);
81666e1b
MD
212 }
213
214 /*
215 * Start master service
216 */
217 if (fd < 0) {
ce48a6c5 218 dmsg_circuit_printf(circuit, "Connection to %s failed\n", hostname);
9ab15106 219 } else {
ce48a6c5 220 dmsg_circuit_printf(circuit, "Connected to %s\n", hostname);
ddfbb283
MD
221
222 info = malloc(sizeof(*info));
223 bzero(info, sizeof(*info));
224 info->fd = fd;
225 info->detachme = 1;
226 info->dbgmsg_callback = hammer2_shell_parse;
227
228 pthread_create(&thread, NULL, dmsg_master_service, info);
81666e1b 229 /*pthread_join(thread, &res);*/
9ab15106
MD
230 }
231}
232
02454b3e
MD
233/************************************************************************
234 * DEBUGSPAN *
235 ************************************************************************
236 *
237 * Connect to the target manually (not via the cluster list embedded in
238 * a hammer2 filesystem) and initiate the SPAN protocol.
239 */
240int
241cmd_debugspan(const char *hostname)
242{
243 pthread_t thread;
244 int fd;
245 void *res;
246
247 /*
248 * Connect to the target
249 */
0c3a8cd0 250 fd = dmsg_connect(hostname);
02454b3e
MD
251 if (fd < 0)
252 return 1;
253
254 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
0c3a8cd0
MD
255 pthread_create(&thread, NULL,
256 dmsg_master_service, (void *)(intptr_t)fd);
02454b3e
MD
257 pthread_join(thread, &res);
258 return(0);
259}
260
bd878fc9
MD
261/************************************************************************
262 * SHOW *
263 ************************************************************************/
264
265static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
266static void tabprintf(int tab, const char *ctl, ...);
267
268int
269cmd_show(const char *devpath)
270{
271 hammer2_blockref_t broot;
272 int fd;
273
274 fd = open(devpath, O_RDONLY);
275 if (fd < 0) {
276 perror("open");
277 return 1;
278 }
279 bzero(&broot, sizeof(broot));
280 broot.type = HAMMER2_BREF_TYPE_VOLUME;
281 broot.data_off = 0 | HAMMER2_PBUFRADIX;
282 show_bref(fd, 0, 0, &broot);
283 close(fd);
284
285 return 0;
286}
287
288static void
289show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref)
290{
291 hammer2_media_data_t media;
292 hammer2_blockref_t *bscan;
293 int bcount;
294 int i;
295 int didnl;
ad7cf8ea 296 int namelen;
bd878fc9
MD
297 int obrace = 1;
298 size_t bytes;
299 const char *type_str;
300 char *str = NULL;
301
302 switch(bref->type) {
303 case HAMMER2_BREF_TYPE_EMPTY:
304 type_str = "empty";
305 break;
306 case HAMMER2_BREF_TYPE_INODE:
307 type_str = "inode";
308 break;
309 case HAMMER2_BREF_TYPE_INDIRECT:
310 type_str = "indblk";
311 break;
312 case HAMMER2_BREF_TYPE_DATA:
313 type_str = "data";
314 break;
315 case HAMMER2_BREF_TYPE_VOLUME:
316 type_str = "volume";
317 break;
318 default:
319 type_str = "unknown";
320 break;
321 }
322
323
ad7cf8ea
MD
324 tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
325 type_str, bi, (intmax_t)bref->data_off,
326 (intmax_t)bref->key, (intmax_t)bref->keybits,
327 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
bd878fc9
MD
328 tab += SHOW_TAB;
329
330 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
ad7cf8ea 331 if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) {
bd878fc9
MD
332 printf("(bad block size %zd)\n", bytes);
333 return;
334 }
335 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
336 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
337 if (read(fd, &media, bytes) != (ssize_t)bytes) {
338 printf("(media read failed)\n");
339 return;
340 }
341 }
342
343 bscan = NULL;
344 bcount = 0;
345 didnl = 0;
346
347 switch(bref->type) {
348 case HAMMER2_BREF_TYPE_EMPTY:
349 obrace = 0;
350 break;
351 case HAMMER2_BREF_TYPE_INODE:
352 printf("{\n");
353 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
354 /* no blockrefs */
355 } else {
356 bscan = &media.ipdata.u.blockset.blockref[0];
357 bcount = HAMMER2_SET_COUNT;
358 }
ad7cf8ea
MD
359 namelen = media.ipdata.name_len;
360 if (namelen > HAMMER2_INODE_MAXNAME)
361 namelen = 0;
362 tabprintf(tab, "filename \"%*.*s\"\n",
363 namelen, namelen, media.ipdata.filename);
bd878fc9
MD
364 tabprintf(tab, "version %d\n", media.ipdata.version);
365 tabprintf(tab, "uflags 0x%08x\n",
366 media.ipdata.uflags);
367 if (media.ipdata.rmajor || media.ipdata.rminor) {
368 tabprintf(tab, "rmajor %d\n",
369 media.ipdata.rmajor);
370 tabprintf(tab, "rminor %d\n",
371 media.ipdata.rminor);
372 }
373 tabprintf(tab, "ctime %s\n",
374 hammer2_time64_to_str(media.ipdata.ctime, &str));
375 tabprintf(tab, "mtime %s\n",
376 hammer2_time64_to_str(media.ipdata.mtime, &str));
377 tabprintf(tab, "atime %s\n",
378 hammer2_time64_to_str(media.ipdata.atime, &str));
379 tabprintf(tab, "btime %s\n",
380 hammer2_time64_to_str(media.ipdata.btime, &str));
381 tabprintf(tab, "uid %s\n",
382 hammer2_uuid_to_str(&media.ipdata.uid, &str));
383 tabprintf(tab, "gid %s\n",
384 hammer2_uuid_to_str(&media.ipdata.gid, &str));
385 tabprintf(tab, "type %s\n",
386 hammer2_iptype_to_str(media.ipdata.type));
387 tabprintf(tab, "opflgs 0x%02x\n",
388 media.ipdata.op_flags);
389 tabprintf(tab, "capflgs 0x%04x\n",
390 media.ipdata.cap_flags);
391 tabprintf(tab, "mode %-7o\n",
392 media.ipdata.mode);
393 tabprintf(tab, "inum 0x%016jx\n",
394 media.ipdata.inum);
395 tabprintf(tab, "size %ju\n",
396 (uintmax_t)media.ipdata.size);
397 tabprintf(tab, "nlinks %ju\n",
398 (uintmax_t)media.ipdata.nlinks);
399 tabprintf(tab, "iparent 0x%016jx\n",
400 (uintmax_t)media.ipdata.iparent);
401 tabprintf(tab, "name_key 0x%016jx\n",
402 (uintmax_t)media.ipdata.name_key);
403 tabprintf(tab, "name_len %u\n",
404 media.ipdata.name_len);
405 tabprintf(tab, "ncopies %u\n",
406 media.ipdata.ncopies);
407 tabprintf(tab, "compalg %u\n",
408 media.ipdata.comp_algo);
409 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
410 tabprintf(tab, "pfs_type %u (%s)\n",
411 media.ipdata.pfs_type,
412 hammer2_pfstype_to_str(media.ipdata.pfs_type));
413 tabprintf(tab, "pfs_inum 0x%016jx\n",
414 (uintmax_t)media.ipdata.pfs_inum);
8c280d5d
MD
415 tabprintf(tab, "pfs_clid %s\n",
416 hammer2_uuid_to_str(&media.ipdata.pfs_clid,
bd878fc9
MD
417 &str));
418 tabprintf(tab, "pfs_fsid %s\n",
419 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
420 &str));
421 }
422 tabprintf(tab, "data_quota %ju\n",
423 (uintmax_t)media.ipdata.data_quota);
424 tabprintf(tab, "data_count %ju\n",
425 (uintmax_t)media.ipdata.data_count);
426 tabprintf(tab, "inode_quota %ju\n",
427 (uintmax_t)media.ipdata.inode_quota);
428 tabprintf(tab, "inode_count %ju\n",
429 (uintmax_t)media.ipdata.inode_count);
430 tabprintf(tab, "attr_tid 0x%016jx\n",
431 (uintmax_t)media.ipdata.attr_tid);
432 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
433 tabprintf(tab, "dirent_tid %016jx\n",
434 (uintmax_t)media.ipdata.dirent_tid);
435 }
436 break;
437 case HAMMER2_BREF_TYPE_INDIRECT:
438 bscan = &media.npdata.blockref[0];
439 bcount = bytes / sizeof(hammer2_blockref_t);
440 didnl = 1;
441 printf("{\n");
442 break;
443 case HAMMER2_BREF_TYPE_DATA:
444 if (VerboseOpt >= 2) {
445 printf("{\n");
446 } else {
447 printf("\n");
448 obrace = 0;
449 }
450 break;
451 case HAMMER2_BREF_TYPE_VOLUME:
452 bscan = &media.voldata.sroot_blockset.blockref[0];
453 bcount = HAMMER2_SET_COUNT;
454 printf("{\n");
455 break;
456 default:
457 break;
458 }
459 if (str)
460 free(str);
461 for (i = 0; i < bcount; ++i) {
462 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
463 if (didnl == 0) {
464 printf("\n");
465 didnl = 1;
466 }
467 show_bref(fd, tab, i, &bscan[i]);
468 }
469 }
470 tab -= SHOW_TAB;
471 if (obrace) {
472 if (bref->type == HAMMER2_BREF_TYPE_INODE)
473 tabprintf(tab, "} (%s.%d, \"%s\")\n",
474 type_str, bi, media.ipdata.filename);
475 else
476 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
477 }
478}
479
480static
481void
482tabprintf(int tab, const char *ctl, ...)
483{
484 va_list va;
485
486 printf("%*.*s", tab, tab, "");
487 va_start(va, ctl);
488 vprintf(ctl, va);
489 va_end(va);
490}