Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[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(hammer2_msg_t *msg);
41 static void shell_ttymsg(hammer2_iocom_t *iocom);
42 static void hammer2_shell_parse(hammer2_msg_t *msg);
43
44 /************************************************************************
45  *                                  SHELL                               *
46  ************************************************************************/
47
48 int
49 cmd_shell(const char *hostname)
50 {
51         struct hammer2_iocom iocom;
52         hammer2_msg_t *msg;
53         int fd;
54
55         /*
56          * Connect to the target
57          */
58         fd = hammer2_connect(hostname);
59         if (fd < 0)
60                 return 1;
61
62         /*
63          * Run the session.  The remote end transmits our prompt.
64          */
65         hammer2_iocom_init(&iocom, fd, 0, NULL, shell_rcvmsg, shell_ttymsg);
66         fcntl(0, F_SETFL, O_NONBLOCK);
67         printf("debug: connected\n");
68
69         msg = hammer2_msg_alloc(iocom.router, 0, HAMMER2_DBG_SHELL,
70                                 NULL, NULL);
71         hammer2_msg_write(msg);
72         hammer2_iocom_core(&iocom);
73         fprintf(stderr, "debug: disconnected\n");
74         close(fd);
75         return 0;
76 }
77
78 /*
79  * Callback from hammer2_iocom_core() when messages might be present
80  * on the socket.
81  */
82 static
83 void
84 shell_rcvmsg(hammer2_msg_t *msg)
85 {
86         switch(msg->any.head.cmd & HAMMER2_MSGF_TRANSMASK) {
87         case HAMMER2_LNK_ERROR:
88         case HAMMER2_LNK_ERROR | HAMMER2_MSGF_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                                 hammer2_msg_str(msg));
97                 } else {
98                         write(1, "debug> ", 7);
99                 }
100                 break;
101         case HAMMER2_LNK_ERROR | HAMMER2_MSGF_DELETE:
102                 /* ignore termination of LNK_CONN */
103                 break;
104         case HAMMER2_DBG_SHELL:
105                 /*
106                  * We send the commands, not accept them.
107                  * (one-way message, not transactional)
108                  */
109                 hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);
110                 break;
111         case HAMMER2_DBG_SHELL | HAMMER2_MSGF_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 HAMMER2_LNK_CONN | HAMMER2_MSGF_CREATE:
122                 fprintf(stderr, "Debug Shell is ignoring received LNK_CONN\n");
123                 hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);
124                 break;
125         case HAMMER2_LNK_CONN | HAMMER2_MSGF_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", hammer2_msg_str(msg));
133                 if (msg->any.head.cmd & HAMMER2_MSGF_CREATE)
134                         hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);
135                 if (msg->any.head.cmd & HAMMER2_MSGF_DELETE)
136                         hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);
137                 break;
138         }
139 }
140
141 static
142 void
143 shell_ttymsg(hammer2_iocom_t *iocom)
144 {
145         hammer2_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 = hammer2_msg_alloc(iocom->router, len, HAMMER2_DBG_SHELL,
155                                         NULL, NULL);
156                 bcopy(buf, msg->aux_data, len);
157                 hammer2_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 |= HAMMER2_IOCOMF_EOF;
164         } else {
165                 clearerr(stdin);
166         }
167 }
168
169 /*
170  * This is called from the master node to process a received debug
171  * shell command.  We process the command, outputting the results,
172  * then finish up by outputting another prompt.
173  */
174 void
175 hammer2_msg_dbg(hammer2_msg_t *msg)
176 {
177         switch(msg->any.head.cmd & HAMMER2_MSGF_CMDSWMASK) {
178         case HAMMER2_DBG_SHELL:
179                 /*
180                  * This is a command which we must process.
181                  * When we are finished we generate a final reply.
182                  */
183                 if (msg->aux_data)
184                         msg->aux_data[msg->aux_size - 1] = 0;
185                 hammer2_shell_parse(msg);
186                 hammer2_msg_reply(msg, 0);
187                 break;
188         case HAMMER2_DBG_SHELL | HAMMER2_MSGF_REPLY:
189                 /*
190                  * A reply just prints out the string.  No newline is added
191                  * (it is expected to be embedded if desired).
192                  */
193                 if (msg->aux_data)
194                         msg->aux_data[msg->aux_size - 1] = 0;
195                 if (msg->aux_data)
196                         write(2, msg->aux_data, strlen(msg->aux_data));
197                 break;
198         default:
199                 hammer2_msg_reply(msg, HAMMER2_MSG_ERR_NOSUPP);
200                 break;
201         }
202 }
203
204 static void shell_span(hammer2_router_t *router, char *cmdbuf);
205 /*static void shell_tree(hammer2_router_t *router, char *cmdbuf);*/
206
207 static void
208 hammer2_shell_parse(hammer2_msg_t *msg)
209 {
210         hammer2_router_t *router = msg->router;
211         char *cmdbuf = msg->aux_data;
212         char *cmd = strsep(&cmdbuf, " \t");
213
214         if (cmd == NULL || *cmd == 0) {
215                 ;
216         } else if (strcmp(cmd, "span") == 0) {
217                 shell_span(router, cmdbuf);
218         } else if (strcmp(cmd, "tree") == 0) {
219                 shell_tree(router, cmdbuf);
220         } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
221                 router_printf(router, "help            Command help\n");
222                 router_printf(router, "span <host>     Span to target host\n");
223                 router_printf(router, "tree            Dump spanning tree\n");
224         } else {
225                 router_printf(router, "Unrecognized command: %s\n", cmd);
226         }
227 }
228
229 static void
230 shell_span(hammer2_router_t *router, char *cmdbuf)
231 {
232         const char *hostname = strsep(&cmdbuf, " \t");
233         pthread_t thread;
234         int fd;
235
236         /*
237          * Connect to the target
238          */
239         if (hostname == NULL) {
240                 fd = -1;
241         } else {
242                 fd = hammer2_connect(hostname);
243         }
244
245         /*
246          * Start master service
247          */
248         if (fd < 0) {
249                 router_printf(router, "Connection to %s failed\n", hostname);
250         } else {
251                 router_printf(router, "Connected to %s\n", hostname);
252                 pthread_create(&thread, NULL,
253                                master_service, (void *)(intptr_t)fd);
254                 /*pthread_join(thread, &res);*/
255         }
256 }
257
258 /*
259  * Returns text debug output to the original defined by (msg).  (msg) is
260  * not modified and stays intact.  We use a one-way message with REPLY set
261  * to distinguish between a debug command and debug terminal output.
262  *
263  * To prevent loops router_printf() can filter the message (cmd) related
264  * to the router_printf().  We filter out DBG messages.
265  */
266 void
267 router_printf(hammer2_router_t *router, const char *ctl, ...)
268 {
269         hammer2_msg_t *rmsg;
270         va_list va;
271         char buf[1024];
272         size_t len;
273
274         va_start(va, ctl);
275         vsnprintf(buf, sizeof(buf), ctl, va);
276         va_end(va);
277         len = strlen(buf) + 1;
278
279         rmsg = hammer2_msg_alloc(router, len, HAMMER2_DBG_SHELL |
280                                               HAMMER2_MSGF_REPLY,
281                                  NULL, NULL);
282         bcopy(buf, rmsg->aux_data, len);
283
284         hammer2_msg_write(rmsg);
285 }
286
287 /************************************************************************
288  *                              DEBUGSPAN                               *
289  ************************************************************************
290  *
291  * Connect to the target manually (not via the cluster list embedded in
292  * a hammer2 filesystem) and initiate the SPAN protocol.
293  */
294 int
295 cmd_debugspan(const char *hostname)
296 {
297         pthread_t thread;
298         int fd;
299         void *res;
300
301         /*
302          * Connect to the target
303          */
304         fd = hammer2_connect(hostname);
305         if (fd < 0)
306                 return 1;
307
308         printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname);
309         pthread_create(&thread, NULL, master_service, (void *)(intptr_t)fd);
310         pthread_join(thread, &res);
311         return(0);
312 }
313
314 /************************************************************************
315  *                                  SHOW                                *
316  ************************************************************************/
317
318 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
319 static void tabprintf(int tab, const char *ctl, ...);
320
321 int
322 cmd_show(const char *devpath)
323 {
324         hammer2_blockref_t broot;
325         int fd;
326
327         fd = open(devpath, O_RDONLY);
328         if (fd < 0) {
329                 perror("open");
330                 return 1;
331         }
332         bzero(&broot, sizeof(broot));
333         broot.type = HAMMER2_BREF_TYPE_VOLUME;
334         broot.data_off = 0 | HAMMER2_PBUFRADIX;
335         show_bref(fd, 0, 0, &broot);
336         close(fd);
337
338         return 0;
339 }
340
341 static void
342 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref)
343 {
344         hammer2_media_data_t media;
345         hammer2_blockref_t *bscan;
346         int bcount;
347         int i;
348         int didnl;
349         int namelen;
350         int obrace = 1;
351         size_t bytes;
352         const char *type_str;
353         char *str = NULL;
354
355         switch(bref->type) {
356         case HAMMER2_BREF_TYPE_EMPTY:
357                 type_str = "empty";
358                 break;
359         case HAMMER2_BREF_TYPE_INODE:
360                 type_str = "inode";
361                 break;
362         case HAMMER2_BREF_TYPE_INDIRECT:
363                 type_str = "indblk";
364                 break;
365         case HAMMER2_BREF_TYPE_DATA:
366                 type_str = "data";
367                 break;
368         case HAMMER2_BREF_TYPE_VOLUME:
369                 type_str = "volume";
370                 break;
371         default:
372                 type_str = "unknown";
373                 break;
374         }
375
376
377         tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
378                type_str, bi, (intmax_t)bref->data_off,
379                (intmax_t)bref->key, (intmax_t)bref->keybits,
380                (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
381         tab += SHOW_TAB;
382
383         bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
384         if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) {
385                 printf("(bad block size %zd)\n", bytes);
386                 return;
387         }
388         if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
389                 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
390                 if (read(fd, &media, bytes) != (ssize_t)bytes) {
391                         printf("(media read failed)\n");
392                         return;
393                 }
394         }
395
396         bscan = NULL;
397         bcount = 0;
398         didnl = 0;
399
400         switch(bref->type) {
401         case HAMMER2_BREF_TYPE_EMPTY:
402                 obrace = 0;
403                 break;
404         case HAMMER2_BREF_TYPE_INODE:
405                 printf("{\n");
406                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
407                         /* no blockrefs */
408                 } else {
409                         bscan = &media.ipdata.u.blockset.blockref[0];
410                         bcount = HAMMER2_SET_COUNT;
411                 }
412                 namelen = media.ipdata.name_len;
413                 if (namelen > HAMMER2_INODE_MAXNAME)
414                         namelen = 0;
415                 tabprintf(tab, "filename \"%*.*s\"\n",
416                           namelen, namelen, media.ipdata.filename);
417                 tabprintf(tab, "version  %d\n", media.ipdata.version);
418                 tabprintf(tab, "uflags   0x%08x\n",
419                           media.ipdata.uflags);
420                 if (media.ipdata.rmajor || media.ipdata.rminor) {
421                         tabprintf(tab, "rmajor   %d\n",
422                                   media.ipdata.rmajor);
423                         tabprintf(tab, "rminor   %d\n",
424                                   media.ipdata.rminor);
425                 }
426                 tabprintf(tab, "ctime    %s\n",
427                           hammer2_time64_to_str(media.ipdata.ctime, &str));
428                 tabprintf(tab, "mtime    %s\n",
429                           hammer2_time64_to_str(media.ipdata.mtime, &str));
430                 tabprintf(tab, "atime    %s\n",
431                           hammer2_time64_to_str(media.ipdata.atime, &str));
432                 tabprintf(tab, "btime    %s\n",
433                           hammer2_time64_to_str(media.ipdata.btime, &str));
434                 tabprintf(tab, "uid      %s\n",
435                           hammer2_uuid_to_str(&media.ipdata.uid, &str));
436                 tabprintf(tab, "gid      %s\n",
437                           hammer2_uuid_to_str(&media.ipdata.gid, &str));
438                 tabprintf(tab, "type     %s\n",
439                           hammer2_iptype_to_str(media.ipdata.type));
440                 tabprintf(tab, "opflgs   0x%02x\n",
441                           media.ipdata.op_flags);
442                 tabprintf(tab, "capflgs  0x%04x\n",
443                           media.ipdata.cap_flags);
444                 tabprintf(tab, "mode     %-7o\n",
445                           media.ipdata.mode);
446                 tabprintf(tab, "inum     0x%016jx\n",
447                           media.ipdata.inum);
448                 tabprintf(tab, "size     %ju\n",
449                           (uintmax_t)media.ipdata.size);
450                 tabprintf(tab, "nlinks   %ju\n",
451                           (uintmax_t)media.ipdata.nlinks);
452                 tabprintf(tab, "iparent  0x%016jx\n",
453                           (uintmax_t)media.ipdata.iparent);
454                 tabprintf(tab, "name_key 0x%016jx\n",
455                           (uintmax_t)media.ipdata.name_key);
456                 tabprintf(tab, "name_len %u\n",
457                           media.ipdata.name_len);
458                 tabprintf(tab, "ncopies  %u\n",
459                           media.ipdata.ncopies);
460                 tabprintf(tab, "compalg  %u\n",
461                           media.ipdata.comp_algo);
462                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
463                         tabprintf(tab, "pfs_type %u (%s)\n",
464                                   media.ipdata.pfs_type,
465                                   hammer2_pfstype_to_str(media.ipdata.pfs_type));
466                         tabprintf(tab, "pfs_inum 0x%016jx\n",
467                                   (uintmax_t)media.ipdata.pfs_inum);
468                         tabprintf(tab, "pfs_clid %s\n",
469                                   hammer2_uuid_to_str(&media.ipdata.pfs_clid,
470                                                       &str));
471                         tabprintf(tab, "pfs_fsid %s\n",
472                                   hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
473                                                       &str));
474                 }
475                 tabprintf(tab, "data_quota  %ju\n",
476                           (uintmax_t)media.ipdata.data_quota);
477                 tabprintf(tab, "data_count  %ju\n",
478                           (uintmax_t)media.ipdata.data_count);
479                 tabprintf(tab, "inode_quota %ju\n",
480                           (uintmax_t)media.ipdata.inode_quota);
481                 tabprintf(tab, "inode_count %ju\n",
482                           (uintmax_t)media.ipdata.inode_count);
483                 tabprintf(tab, "attr_tid    0x%016jx\n",
484                           (uintmax_t)media.ipdata.attr_tid);
485                 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
486                         tabprintf(tab, "dirent_tid  %016jx\n",
487                                   (uintmax_t)media.ipdata.dirent_tid);
488                 }
489                 break;
490         case HAMMER2_BREF_TYPE_INDIRECT:
491                 bscan = &media.npdata.blockref[0];
492                 bcount = bytes / sizeof(hammer2_blockref_t);
493                 didnl = 1;
494                 printf("{\n");
495                 break;
496         case HAMMER2_BREF_TYPE_DATA:
497                 if (VerboseOpt >= 2) {
498                         printf("{\n");
499                 } else {
500                         printf("\n");
501                         obrace = 0;
502                 }
503                 break;
504         case HAMMER2_BREF_TYPE_VOLUME:
505                 bscan = &media.voldata.sroot_blockset.blockref[0];
506                 bcount = HAMMER2_SET_COUNT;
507                 printf("{\n");
508                 break;
509         default:
510                 break;
511         }
512         if (str)
513                 free(str);
514         for (i = 0; i < bcount; ++i) {
515                 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
516                         if (didnl == 0) {
517                                 printf("\n");
518                                 didnl = 1;
519                         }
520                         show_bref(fd, tab, i, &bscan[i]);
521                 }
522         }
523         tab -= SHOW_TAB;
524         if (obrace) {
525                 if (bref->type == HAMMER2_BREF_TYPE_INODE)
526                         tabprintf(tab, "} (%s.%d, \"%s\")\n",
527                                   type_str, bi, media.ipdata.filename);
528                 else
529                         tabprintf(tab, "} (%s.%d)\n", type_str,bi);
530         }
531 }
532
533 static
534 void
535 tabprintf(int tab, const char *ctl, ...)
536 {
537         va_list va;
538
539         printf("%*.*s", tab, tab, "");
540         va_start(va, ctl);
541         vprintf(ctl, va);
542         va_end(va);
543 }