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