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