hammer2 - userland API / span 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
5903497c
MD
40static void shell_rcvmsg(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
41static void shell_ttymsg(hammer2_iocom_t *iocom);
78476205 42static void hammer2_shell_parse(hammer2_iocom_t *iocom, hammer2_msg_t *msg);
bd878fc9
MD
43
44/************************************************************************
45 * SHELL *
46 ************************************************************************/
9ab15106
MD
47
48int
bd878fc9 49cmd_shell(const char *hostname)
9ab15106 50{
9ab15106
MD
51 struct hammer2_iocom iocom;
52 hammer2_msg_t *msg;
53 int fd;
54
55 /*
9ab15106
MD
56 * Connect to the target
57 */
02454b3e
MD
58 fd = hammer2_connect(hostname);
59 if (fd < 0)
60 return 1;
9ab15106
MD
61
62 /*
63 * Run the session. The remote end transmits our prompt.
64 */
5903497c 65 hammer2_iocom_init(&iocom, fd, 0, NULL, shell_rcvmsg, shell_ttymsg);
02454b3e 66 fcntl(0, F_SETFL, O_NONBLOCK);
9ab15106
MD
67 printf("debug: connected\n");
68
78476205 69 msg = hammer2_msg_alloc(&iocom, 0, HAMMER2_DBG_SHELL);
7dc0f844 70 hammer2_msg_write(&iocom, msg, NULL, NULL, NULL);
5903497c 71 hammer2_iocom_core(&iocom);
9ab15106
MD
72 fprintf(stderr, "debug: disconnected\n");
73 close(fd);
74 return 0;
75}
76
77/*
78 * Callback from hammer2_iocom_core() when messages might be present
79 * on the socket.
80 */
81static
82void
5903497c 83shell_rcvmsg(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
9ab15106 84{
81666e1b 85 switch(msg->any.head.cmd & HAMMER2_MSGF_TRANSMASK) {
5903497c
MD
86 case HAMMER2_LNK_ERROR:
87 case HAMMER2_LNK_ERROR | HAMMER2_MSGF_REPLY:
81666e1b
MD
88 /*
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.
92 */
5903497c 93 if (msg->any.head.error) {
81666e1b
MD
94 fprintf(stderr, "Stream failure: %s\n",
95 hammer2_msg_str(msg));
5903497c
MD
96 } else {
97 write(1, "debug> ", 7);
9ab15106 98 }
5903497c 99 break;
81666e1b
MD
100 case HAMMER2_LNK_ERROR | HAMMER2_MSGF_DELETE:
101 /* ignore termination of LNK_CONN */
102 break;
5903497c
MD
103 case HAMMER2_DBG_SHELL:
104 /*
105 * We send the commands, not accept them.
81666e1b 106 * (one-way message, not transactional)
5903497c 107 */
81666e1b 108 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_NOSUPP);
5903497c
MD
109 break;
110 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
111 /*
112 * A reply from the remote is data we copy to stdout.
81666e1b 113 * (one-way message, not transactional)
5903497c
MD
114 */
115 if (msg->aux_size) {
116 msg->aux_data[msg->aux_size - 1] = 0;
117 write(1, msg->aux_data, strlen(msg->aux_data));
118 }
119 break;
81666e1b
MD
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);
123 break;
124 case HAMMER2_LNK_CONN | HAMMER2_MSGF_DELETE:
125 break;
5903497c 126 default:
81666e1b
MD
127 /*
128 * Ignore any unknown messages, Terminate any unknown
129 * transactions with an error.
130 */
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);
5903497c 136 break;
9ab15106
MD
137 }
138}
139
9ab15106
MD
140static
141void
5903497c 142shell_ttymsg(hammer2_iocom_t *iocom)
9ab15106
MD
143{
144 hammer2_msg_t *msg;
145 char buf[256];
146 size_t len;
147
148 if (fgets(buf, sizeof(buf), stdin) != NULL) {
149 len = strlen(buf);
150 if (len && buf[len - 1] == '\n')
151 buf[--len] = 0;
152 ++len;
78476205 153 msg = hammer2_msg_alloc(iocom, len, HAMMER2_DBG_SHELL);
9ab15106 154 bcopy(buf, msg->aux_data, len);
7dc0f844 155 hammer2_msg_write(iocom, msg, NULL, NULL, NULL);
02454b3e 156 } else if (feof(stdin)) {
9ab15106
MD
157 /*
158 * Set EOF flag without setting any error code for normal
159 * EOF.
160 */
161 iocom->flags |= HAMMER2_IOCOMF_EOF;
02454b3e
MD
162 } else {
163 clearerr(stdin);
9ab15106
MD
164 }
165}
166
167/*
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.
171 */
172void
8c280d5d 173hammer2_msg_dbg(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
9ab15106 174{
8c280d5d
MD
175 switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
176 case HAMMER2_DBG_SHELL:
177 /*
178 * This is a command which we must process.
179 * When we are finished we generate a final reply.
180 */
181 if (msg->aux_data)
182 msg->aux_data[msg->aux_size - 1] = 0;
183 hammer2_shell_parse(iocom, msg);
184 hammer2_msg_reply(iocom, msg, 0);
185 break;
186 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
9ab15106
MD
187 /*
188 * A reply just prints out the string. No newline is added
189 * (it is expected to be embedded if desired).
190 */
191 if (msg->aux_data)
8c280d5d
MD
192 msg->aux_data[msg->aux_size - 1] = 0;
193 if (msg->aux_data)
9ab15106 194 write(2, msg->aux_data, strlen(msg->aux_data));
8c280d5d
MD
195 break;
196 default:
81666e1b 197 hammer2_msg_reply(iocom, msg, HAMMER2_MSG_ERR_NOSUPP);
8c280d5d 198 break;
9ab15106
MD
199 }
200}
201
81666e1b
MD
202static void shell_span(hammer2_iocom_t *iocom, char *cmdbuf);
203/*static void shell_tree(hammer2_iocom_t *iocom, char *cmdbuf);*/
204
9ab15106 205static void
78476205 206hammer2_shell_parse(hammer2_iocom_t *iocom, hammer2_msg_t *msg)
9ab15106 207{
78476205 208 char *cmdbuf = msg->aux_data;
9ab15106
MD
209 char *cmd = strsep(&cmdbuf, " \t");
210
211 if (cmd == NULL || *cmd == 0) {
212 ;
02454b3e 213 } else if (strcmp(cmd, "span") == 0) {
81666e1b
MD
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");
221 } else {
222 iocom_printf(iocom, "Unrecognized command: %s\n", cmd);
223 }
224}
02454b3e 225
81666e1b
MD
226static void
227shell_span(hammer2_iocom_t *iocom, char *cmdbuf)
228{
229 const char *hostname = strsep(&cmdbuf, " \t");
230 pthread_t thread;
231 int fd;
02454b3e 232
81666e1b
MD
233 /*
234 * Connect to the target
235 */
236 if (hostname == NULL) {
237 fd = -1;
238 } else {
239 fd = hammer2_connect(hostname);
240 }
241
242 /*
243 * Start master service
244 */
245 if (fd < 0) {
246 iocom_printf(iocom, "Connection to %s failed\n", hostname);
9ab15106 247 } else {
81666e1b
MD
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);*/
9ab15106
MD
252 }
253}
254
4a2e0eae
MD
255/*
256 * Returns text debug output to the original defined by (msg). (msg) is
78476205
MD
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.
259 *
260 * To prevent loops iocom_printf() can filter the message (cmd) related
261 * to the iocom_printf(). We filter out DBG messages.
4a2e0eae 262 */
9ab15106 263void
81666e1b 264iocom_printf(hammer2_iocom_t *iocom, const char *ctl, ...)
9ab15106
MD
265{
266 hammer2_msg_t *rmsg;
267 va_list va;
268 char buf[1024];
269 size_t len;
270
271 va_start(va, ctl);
272 vsnprintf(buf, sizeof(buf), ctl, va);
273 va_end(va);
274 len = strlen(buf) + 1;
275
78476205
MD
276 rmsg = hammer2_msg_alloc(iocom, len, HAMMER2_DBG_SHELL |
277 HAMMER2_MSGF_REPLY);
9ab15106 278 bcopy(buf, rmsg->aux_data, len);
9ab15106 279
7dc0f844 280 hammer2_msg_write(iocom, rmsg, NULL, NULL, NULL);
9ab15106 281}
bd878fc9
MD
282
283/************************************************************************
02454b3e
MD
284 * DEBUGSPAN *
285 ************************************************************************
286 *
287 * Connect to the target manually (not via the cluster list embedded in
288 * a hammer2 filesystem) and initiate the SPAN protocol.
289 */
290int
291cmd_debugspan(const char *hostname)
292{
293 pthread_t thread;
294 int fd;
295 void *res;
296
297 /*
298 * Connect to the target
299 */
300 fd = hammer2_connect(hostname);
301 if (fd < 0)
302 return 1;
303
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);
307 return(0);
308}
309
310/************************************************************************
bd878fc9
MD
311 * SHOW *
312 ************************************************************************/
313
314static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
315static void tabprintf(int tab, const char *ctl, ...);
316
317int
318cmd_show(const char *devpath)
319{
320 hammer2_blockref_t broot;
321 int fd;
322
323 fd = open(devpath, O_RDONLY);
324 if (fd < 0) {
325 perror("open");
326 return 1;
327 }
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);
332 close(fd);
333
334 return 0;
335}
336
337static void
338show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref)
339{
340 hammer2_media_data_t media;
341 hammer2_blockref_t *bscan;
342 int bcount;
343 int i;
344 int didnl;
ad7cf8ea 345 int namelen;
bd878fc9
MD
346 int obrace = 1;
347 size_t bytes;
348 const char *type_str;
349 char *str = NULL;
350
351 switch(bref->type) {
352 case HAMMER2_BREF_TYPE_EMPTY:
353 type_str = "empty";
354 break;
355 case HAMMER2_BREF_TYPE_INODE:
356 type_str = "inode";
357 break;
358 case HAMMER2_BREF_TYPE_INDIRECT:
359 type_str = "indblk";
360 break;
361 case HAMMER2_BREF_TYPE_DATA:
362 type_str = "data";
363 break;
364 case HAMMER2_BREF_TYPE_VOLUME:
365 type_str = "volume";
366 break;
367 default:
368 type_str = "unknown";
369 break;
370 }
371
372
ad7cf8ea
MD
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);
bd878fc9
MD
377 tab += SHOW_TAB;
378
379 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
ad7cf8ea 380 if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) {
bd878fc9
MD
381 printf("(bad block size %zd)\n", bytes);
382 return;
383 }
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");
388 return;
389 }
390 }
391
392 bscan = NULL;
393 bcount = 0;
394 didnl = 0;
395
396 switch(bref->type) {
397 case HAMMER2_BREF_TYPE_EMPTY:
398 obrace = 0;
399 break;
400 case HAMMER2_BREF_TYPE_INODE:
401 printf("{\n");
402 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
403 /* no blockrefs */
404 } else {
405 bscan = &media.ipdata.u.blockset.blockref[0];
406 bcount = HAMMER2_SET_COUNT;
407 }
ad7cf8ea
MD
408 namelen = media.ipdata.name_len;
409 if (namelen > HAMMER2_INODE_MAXNAME)
410 namelen = 0;
411 tabprintf(tab, "filename \"%*.*s\"\n",
412 namelen, namelen, media.ipdata.filename);
bd878fc9
MD
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);
421 }
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",
441 media.ipdata.mode);
442 tabprintf(tab, "inum 0x%016jx\n",
443 media.ipdata.inum);
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);
8c280d5d
MD
464 tabprintf(tab, "pfs_clid %s\n",
465 hammer2_uuid_to_str(&media.ipdata.pfs_clid,
bd878fc9
MD
466 &str));
467 tabprintf(tab, "pfs_fsid %s\n",
468 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
469 &str));
470 }
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);
484 }
485 break;
486 case HAMMER2_BREF_TYPE_INDIRECT:
487 bscan = &media.npdata.blockref[0];
488 bcount = bytes / sizeof(hammer2_blockref_t);
489 didnl = 1;
490 printf("{\n");
491 break;
492 case HAMMER2_BREF_TYPE_DATA:
493 if (VerboseOpt >= 2) {
494 printf("{\n");
495 } else {
496 printf("\n");
497 obrace = 0;
498 }
499 break;
500 case HAMMER2_BREF_TYPE_VOLUME:
501 bscan = &media.voldata.sroot_blockset.blockref[0];
502 bcount = HAMMER2_SET_COUNT;
503 printf("{\n");
504 break;
505 default:
506 break;
507 }
508 if (str)
509 free(str);
510 for (i = 0; i < bcount; ++i) {
511 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
512 if (didnl == 0) {
513 printf("\n");
514 didnl = 1;
515 }
516 show_bref(fd, tab, i, &bscan[i]);
517 }
518 }
519 tab -= SHOW_TAB;
520 if (obrace) {
521 if (bref->type == HAMMER2_BREF_TYPE_INODE)
522 tabprintf(tab, "} (%s.%d, \"%s\")\n",
523 type_str, bi, media.ipdata.filename);
524 else
525 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
526 }
527}
528
529static
530void
531tabprintf(int tab, const char *ctl, ...)
532{
533 va_list va;
534
535 printf("%*.*s", tab, tab, "");
536 va_start(va, ctl);
537 vprintf(ctl, va);
538 va_end(va);
539}