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