hammer2 - Add media dump command, improve help output
[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
40static void shell_recv(hammer2_iocom_t *iocom);
41static void shell_send(hammer2_iocom_t *iocom);
42static void shell_tty(hammer2_iocom_t *iocom);
43static void hammer2_shell_parse(hammer2_msg_t *msg, char *cmdbuf);
44
45/************************************************************************
46 * SHELL *
47 ************************************************************************/
9ab15106
MD
48
49int
bd878fc9 50cmd_shell(const char *hostname)
9ab15106
MD
51{
52 struct sockaddr_in lsin;
53 struct hammer2_iocom iocom;
54 hammer2_msg_t *msg;
62efe6ec 55 struct hostent *hen;
9ab15106
MD
56 int fd;
57
58 /*
59 * Acquire socket and set options
60 */
61 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
62 fprintf(stderr, "cmd_debug: socket(): %s\n",
63 strerror(errno));
64 return 1;
65 }
66
67 /*
68 * Connect to the target
69 */
70 bzero(&lsin, sizeof(lsin));
71 lsin.sin_family = AF_INET;
72 lsin.sin_addr.s_addr = 0;
73 lsin.sin_port = htons(HAMMER2_LISTEN_PORT);
62efe6ec
MD
74
75 if (hostname) {
76 hen = gethostbyname2(hostname, AF_INET);
77 if (hen == NULL) {
78 if (inet_pton(AF_INET, hostname, &lsin.sin_addr) != 1) {
79 fprintf(stderr,
80 "Cannot resolve %s\n", hostname);
81 return 1;
82 }
83 } else {
84 bcopy(hen->h_addr, &lsin.sin_addr, hen->h_length);
85 }
86 }
9ab15106
MD
87 if (connect(fd, (struct sockaddr *)&lsin, sizeof(lsin)) < 0) {
88 close(fd);
89 fprintf(stderr, "debug: connect failed: %s\n",
90 strerror(errno));
91 return 0;
92 }
93
94 /*
95 * Run the session. The remote end transmits our prompt.
96 */
97 hammer2_iocom_init(&iocom, fd, 0);
98 printf("debug: connected\n");
99
4a2e0eae
MD
100 msg = hammer2_allocmsg(&iocom, HAMMER2_DBG_SHELL, 0);
101 hammer2_ioq_write(msg);
9ab15106 102
bd878fc9 103 hammer2_iocom_core(&iocom, shell_recv, shell_send, shell_tty);
9ab15106
MD
104 fprintf(stderr, "debug: disconnected\n");
105 close(fd);
106 return 0;
107}
108
109/*
110 * Callback from hammer2_iocom_core() when messages might be present
111 * on the socket.
112 */
113static
114void
bd878fc9 115shell_recv(hammer2_iocom_t *iocom)
9ab15106
MD
116{
117 hammer2_msg_t *msg;
118
119 while ((iocom->flags & HAMMER2_IOCOMF_EOF) == 0 &&
120 (msg = hammer2_ioq_read(iocom)) != NULL) {
4a2e0eae 121
9ab15106
MD
122 switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
123 case HAMMER2_LNK_ERROR:
124 fprintf(stderr, "Link Error: %d\n",
125 msg->any.head.error);
126 break;
127 case HAMMER2_DBG_SHELL:
128 /*
129 * We send the commands, not accept them.
130 */
4a2e0eae
MD
131 hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
132 hammer2_freemsg(msg);
9ab15106
MD
133 break;
134 case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
135 /*
136 * A reply from the remote is data we copy to stdout.
137 */
138 if (msg->aux_size) {
139 msg->aux_data[msg->aux_size - 1] = 0;
140 write(1, msg->aux_data, strlen(msg->aux_data));
4a2e0eae
MD
141 } else {
142 write(1, "debug> ", 7);
9ab15106 143 }
4a2e0eae 144 hammer2_freemsg(msg);
9ab15106
MD
145 break;
146 default:
147 assert((msg->any.head.cmd & HAMMER2_MSGF_REPLY) == 0);
148 fprintf(stderr, "Unknown message: %08x\n",
149 msg->any.head.cmd);
4a2e0eae 150 hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
9ab15106
MD
151 break;
152 }
153 }
154 if (iocom->ioq_rx.error) {
155 fprintf(stderr, "node_master_recv: comm error %d\n",
156 iocom->ioq_rx.error);
157 }
158}
159
160/*
161 * Callback from hammer2_iocom_core() when messages might be transmittable
162 * to the socket.
163 */
164static
165void
bd878fc9 166shell_send(hammer2_iocom_t *iocom)
9ab15106 167{
4a2e0eae 168 hammer2_iocom_flush(iocom);
9ab15106
MD
169}
170
171static
172void
bd878fc9 173shell_tty(hammer2_iocom_t *iocom)
9ab15106
MD
174{
175 hammer2_msg_t *msg;
176 char buf[256];
177 size_t len;
178
179 if (fgets(buf, sizeof(buf), stdin) != NULL) {
180 len = strlen(buf);
181 if (len && buf[len - 1] == '\n')
182 buf[--len] = 0;
183 ++len;
4a2e0eae 184 msg = hammer2_allocmsg(iocom, HAMMER2_DBG_SHELL, len);
9ab15106 185 bcopy(buf, msg->aux_data, len);
4a2e0eae 186 hammer2_ioq_write(msg);
9ab15106
MD
187 } else {
188 /*
189 * Set EOF flag without setting any error code for normal
190 * EOF.
191 */
192 iocom->flags |= HAMMER2_IOCOMF_EOF;
193 }
194}
195
196/*
197 * This is called from the master node to process a received debug
198 * shell command. We process the command, outputting the results,
199 * then finish up by outputting another prompt.
200 */
201void
bd878fc9 202hammer2_shell_remote(hammer2_msg_t *msg)
9ab15106 203{
4a2e0eae
MD
204 /* hammer2_iocom_t *iocom = msg->iocom; */
205
9ab15106
MD
206 if (msg->aux_data)
207 msg->aux_data[msg->aux_size - 1] = 0;
208 if (msg->any.head.cmd & HAMMER2_MSGF_REPLY) {
209 /*
210 * A reply just prints out the string. No newline is added
211 * (it is expected to be embedded if desired).
212 */
213 if (msg->aux_data)
214 write(2, msg->aux_data, strlen(msg->aux_data));
4a2e0eae 215 hammer2_freemsg(msg);
9ab15106
MD
216 } else {
217 /*
218 * Otherwise this is a command which we must process.
219 * When we are finished we generate a final reply.
220 */
bd878fc9 221 hammer2_shell_parse(msg, msg->aux_data);
4a2e0eae 222 hammer2_replymsg(msg, 0);
9ab15106
MD
223 }
224}
225
226static void
bd878fc9 227hammer2_shell_parse(hammer2_msg_t *msg, char *cmdbuf)
9ab15106 228{
4a2e0eae 229 /* hammer2_iocom_t *iocom = msg->iocom; */
9ab15106
MD
230 char *cmd = strsep(&cmdbuf, " \t");
231
232 if (cmd == NULL || *cmd == 0) {
233 ;
234 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
4a2e0eae 235 msg_printf(msg, "help Command help\n");
9ab15106 236 } else {
4a2e0eae 237 msg_printf(msg, "Unrecognized command: %s\n", cmd);
9ab15106
MD
238 }
239}
240
4a2e0eae
MD
241/*
242 * Returns text debug output to the original defined by (msg). (msg) is
243 * not modified and stays intact.
244 */
9ab15106 245void
4a2e0eae 246msg_printf(hammer2_msg_t *msg, const char *ctl, ...)
9ab15106 247{
4a2e0eae 248 /* hammer2_iocom_t *iocom = msg->iocom; */
9ab15106
MD
249 hammer2_msg_t *rmsg;
250 va_list va;
251 char buf[1024];
252 size_t len;
253
254 va_start(va, ctl);
255 vsnprintf(buf, sizeof(buf), ctl, va);
256 va_end(va);
257 len = strlen(buf) + 1;
258
4a2e0eae 259 rmsg = hammer2_allocreply(msg, HAMMER2_DBG_SHELL, len);
9ab15106 260 bcopy(buf, rmsg->aux_data, len);
9ab15106 261
4a2e0eae 262 hammer2_ioq_write(rmsg);
9ab15106 263}
bd878fc9
MD
264
265/************************************************************************
266 * SHOW *
267 ************************************************************************/
268
269static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
270static void tabprintf(int tab, const char *ctl, ...);
271
272int
273cmd_show(const char *devpath)
274{
275 hammer2_blockref_t broot;
276 int fd;
277
278 fd = open(devpath, O_RDONLY);
279 if (fd < 0) {
280 perror("open");
281 return 1;
282 }
283 bzero(&broot, sizeof(broot));
284 broot.type = HAMMER2_BREF_TYPE_VOLUME;
285 broot.data_off = 0 | HAMMER2_PBUFRADIX;
286 show_bref(fd, 0, 0, &broot);
287 close(fd);
288
289 return 0;
290}
291
292static void
293show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref)
294{
295 hammer2_media_data_t media;
296 hammer2_blockref_t *bscan;
297 int bcount;
298 int i;
299 int didnl;
300 int obrace = 1;
301 size_t bytes;
302 const char *type_str;
303 char *str = NULL;
304
305 switch(bref->type) {
306 case HAMMER2_BREF_TYPE_EMPTY:
307 type_str = "empty";
308 break;
309 case HAMMER2_BREF_TYPE_INODE:
310 type_str = "inode";
311 break;
312 case HAMMER2_BREF_TYPE_INDIRECT:
313 type_str = "indblk";
314 break;
315 case HAMMER2_BREF_TYPE_DATA:
316 type_str = "data";
317 break;
318 case HAMMER2_BREF_TYPE_VOLUME:
319 type_str = "volume";
320 break;
321 default:
322 type_str = "unknown";
323 break;
324 }
325
326
327 tabprintf(tab, "%s.%-3d %016jx/%-2d mir=%016jx mod=%016jx ",
328 type_str, bi,
329 bref->key, bref->keybits,
330 bref->mirror_tid, bref->modify_tid);
331 tab += SHOW_TAB;
332
333 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
334 if (bytes > sizeof(media)) {
335 printf("(bad block size %zd)\n", bytes);
336 return;
337 }
338 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
339 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
340 if (read(fd, &media, bytes) != (ssize_t)bytes) {
341 printf("(media read failed)\n");
342 return;
343 }
344 }
345
346 bscan = NULL;
347 bcount = 0;
348 didnl = 0;
349
350 switch(bref->type) {
351 case HAMMER2_BREF_TYPE_EMPTY:
352 obrace = 0;
353 break;
354 case HAMMER2_BREF_TYPE_INODE:
355 printf("{\n");
356 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
357 /* no blockrefs */
358 } else {
359 bscan = &media.ipdata.u.blockset.blockref[0];
360 bcount = HAMMER2_SET_COUNT;
361 }
362 tabprintf(tab, "filename \"%s\"\n", media.ipdata.filename);
363 tabprintf(tab, "version %d\n", media.ipdata.version);
364 tabprintf(tab, "uflags 0x%08x\n",
365 media.ipdata.uflags);
366 if (media.ipdata.rmajor || media.ipdata.rminor) {
367 tabprintf(tab, "rmajor %d\n",
368 media.ipdata.rmajor);
369 tabprintf(tab, "rminor %d\n",
370 media.ipdata.rminor);
371 }
372 tabprintf(tab, "ctime %s\n",
373 hammer2_time64_to_str(media.ipdata.ctime, &str));
374 tabprintf(tab, "mtime %s\n",
375 hammer2_time64_to_str(media.ipdata.mtime, &str));
376 tabprintf(tab, "atime %s\n",
377 hammer2_time64_to_str(media.ipdata.atime, &str));
378 tabprintf(tab, "btime %s\n",
379 hammer2_time64_to_str(media.ipdata.btime, &str));
380 tabprintf(tab, "uid %s\n",
381 hammer2_uuid_to_str(&media.ipdata.uid, &str));
382 tabprintf(tab, "gid %s\n",
383 hammer2_uuid_to_str(&media.ipdata.gid, &str));
384 tabprintf(tab, "type %s\n",
385 hammer2_iptype_to_str(media.ipdata.type));
386 tabprintf(tab, "opflgs 0x%02x\n",
387 media.ipdata.op_flags);
388 tabprintf(tab, "capflgs 0x%04x\n",
389 media.ipdata.cap_flags);
390 tabprintf(tab, "mode %-7o\n",
391 media.ipdata.mode);
392 tabprintf(tab, "inum 0x%016jx\n",
393 media.ipdata.inum);
394 tabprintf(tab, "size %ju\n",
395 (uintmax_t)media.ipdata.size);
396 tabprintf(tab, "nlinks %ju\n",
397 (uintmax_t)media.ipdata.nlinks);
398 tabprintf(tab, "iparent 0x%016jx\n",
399 (uintmax_t)media.ipdata.iparent);
400 tabprintf(tab, "name_key 0x%016jx\n",
401 (uintmax_t)media.ipdata.name_key);
402 tabprintf(tab, "name_len %u\n",
403 media.ipdata.name_len);
404 tabprintf(tab, "ncopies %u\n",
405 media.ipdata.ncopies);
406 tabprintf(tab, "compalg %u\n",
407 media.ipdata.comp_algo);
408 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
409 tabprintf(tab, "pfs_type %u (%s)\n",
410 media.ipdata.pfs_type,
411 hammer2_pfstype_to_str(media.ipdata.pfs_type));
412 tabprintf(tab, "pfs_inum 0x%016jx\n",
413 (uintmax_t)media.ipdata.pfs_inum);
414 tabprintf(tab, "pfs_id %s\n",
415 hammer2_uuid_to_str(&media.ipdata.pfs_id,
416 &str));
417 tabprintf(tab, "pfs_fsid %s\n",
418 hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
419 &str));
420 }
421 tabprintf(tab, "data_quota %ju\n",
422 (uintmax_t)media.ipdata.data_quota);
423 tabprintf(tab, "data_count %ju\n",
424 (uintmax_t)media.ipdata.data_count);
425 tabprintf(tab, "inode_quota %ju\n",
426 (uintmax_t)media.ipdata.inode_quota);
427 tabprintf(tab, "inode_count %ju\n",
428 (uintmax_t)media.ipdata.inode_count);
429 tabprintf(tab, "attr_tid 0x%016jx\n",
430 (uintmax_t)media.ipdata.attr_tid);
431 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
432 tabprintf(tab, "dirent_tid %016jx\n",
433 (uintmax_t)media.ipdata.dirent_tid);
434 }
435 break;
436 case HAMMER2_BREF_TYPE_INDIRECT:
437 bscan = &media.npdata.blockref[0];
438 bcount = bytes / sizeof(hammer2_blockref_t);
439 didnl = 1;
440 printf("{\n");
441 break;
442 case HAMMER2_BREF_TYPE_DATA:
443 if (VerboseOpt >= 2) {
444 printf("{\n");
445 } else {
446 printf("\n");
447 obrace = 0;
448 }
449 break;
450 case HAMMER2_BREF_TYPE_VOLUME:
451 bscan = &media.voldata.sroot_blockset.blockref[0];
452 bcount = HAMMER2_SET_COUNT;
453 printf("{\n");
454 break;
455 default:
456 break;
457 }
458 if (str)
459 free(str);
460 for (i = 0; i < bcount; ++i) {
461 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
462 if (didnl == 0) {
463 printf("\n");
464 didnl = 1;
465 }
466 show_bref(fd, tab, i, &bscan[i]);
467 }
468 }
469 tab -= SHOW_TAB;
470 if (obrace) {
471 if (bref->type == HAMMER2_BREF_TYPE_INODE)
472 tabprintf(tab, "} (%s.%d, \"%s\")\n",
473 type_str, bi, media.ipdata.filename);
474 else
475 tabprintf(tab, "} (%s.%d)\n", type_str,bi);
476 }
477}
478
479static
480void
481tabprintf(int tab, const char *ctl, ...)
482{
483 va_list va;
484
485 printf("%*.*s", tab, tab, "");
486 va_start(va, ctl);
487 vprintf(ctl, va);
488 va_end(va);
489}