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_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_msg_t *msg, char *cmdbuf);
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_allocmsg(&iocom, HAMMER2_DBG_SHELL, 0);
101         hammer2_ioq_write(msg);
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                         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                          */
131                         hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
132                         hammer2_freemsg(msg);
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));
141                         } else {
142                                 write(1, "debug> ", 7);
143                         }
144                         hammer2_freemsg(msg);
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);
150                         hammer2_replymsg(msg, HAMMER2_MSG_ERR_UNKNOWN);
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  */
164 static
165 void
166 shell_send(hammer2_iocom_t *iocom)
167 {
168         hammer2_iocom_flush(iocom);
169 }
170
171 static
172 void
173 shell_tty(hammer2_iocom_t *iocom)
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;
184                 msg = hammer2_allocmsg(iocom, HAMMER2_DBG_SHELL, len);
185                 bcopy(buf, msg->aux_data, len);
186                 hammer2_ioq_write(msg);
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  */
201 void
202 hammer2_shell_remote(hammer2_msg_t *msg)
203 {
204         /* hammer2_iocom_t *iocom = msg->iocom; */
205
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));
215                 hammer2_freemsg(msg);
216         } else {
217                 /*
218                  * Otherwise this is a command which we must process.
219                  * When we are finished we generate a final reply.
220                  */
221                 hammer2_shell_parse(msg, msg->aux_data);
222                 hammer2_replymsg(msg, 0);
223         }
224 }
225
226 static void
227 hammer2_shell_parse(hammer2_msg_t *msg, char *cmdbuf)
228 {
229         /* hammer2_iocom_t *iocom = msg->iocom; */
230         char *cmd = strsep(&cmdbuf, " \t");
231
232         if (cmd == NULL || *cmd == 0) {
233                 ;
234         } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
235                 msg_printf(msg, "help        Command help\n");
236         } else {
237                 msg_printf(msg, "Unrecognized command: %s\n", cmd);
238         }
239 }
240
241 /*
242  * Returns text debug output to the original defined by (msg).  (msg) is
243  * not modified and stays intact.
244  */
245 void
246 msg_printf(hammer2_msg_t *msg, const char *ctl, ...)
247 {
248         /* hammer2_iocom_t *iocom = msg->iocom; */
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
259         rmsg = hammer2_allocreply(msg, HAMMER2_DBG_SHELL, len);
260         bcopy(buf, rmsg->aux_data, len);
261
262         hammer2_ioq_write(rmsg);
263 }
264
265 /************************************************************************
266  *                                  SHOW                                *
267  ************************************************************************/
268
269 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref);
270 static void tabprintf(int tab, const char *ctl, ...);
271
272 int
273 cmd_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
292 static void
293 show_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 namelen;
301         int obrace = 1;
302         size_t bytes;
303         const char *type_str;
304         char *str = NULL;
305
306         switch(bref->type) {
307         case HAMMER2_BREF_TYPE_EMPTY:
308                 type_str = "empty";
309                 break;
310         case HAMMER2_BREF_TYPE_INODE:
311                 type_str = "inode";
312                 break;
313         case HAMMER2_BREF_TYPE_INDIRECT:
314                 type_str = "indblk";
315                 break;
316         case HAMMER2_BREF_TYPE_DATA:
317                 type_str = "data";
318                 break;
319         case HAMMER2_BREF_TYPE_VOLUME:
320                 type_str = "volume";
321                 break;
322         default:
323                 type_str = "unknown";
324                 break;
325         }
326
327
328         tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
329                type_str, bi, (intmax_t)bref->data_off,
330                (intmax_t)bref->key, (intmax_t)bref->keybits,
331                (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid);
332         tab += SHOW_TAB;
333
334         bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX);
335         if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) {
336                 printf("(bad block size %zd)\n", bytes);
337                 sleep(1);
338                 return;
339         }
340         if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) {
341                 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0);
342                 if (read(fd, &media, bytes) != (ssize_t)bytes) {
343                         printf("(media read failed)\n");
344                         sleep(1);
345                         return;
346                 }
347         }
348
349         bscan = NULL;
350         bcount = 0;
351         didnl = 0;
352
353         switch(bref->type) {
354         case HAMMER2_BREF_TYPE_EMPTY:
355                 obrace = 0;
356                 break;
357         case HAMMER2_BREF_TYPE_INODE:
358                 printf("{\n");
359                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
360                         /* no blockrefs */
361                 } else {
362                         bscan = &media.ipdata.u.blockset.blockref[0];
363                         bcount = HAMMER2_SET_COUNT;
364                 }
365                 namelen = media.ipdata.name_len;
366                 if (namelen > HAMMER2_INODE_MAXNAME)
367                         namelen = 0;
368                 tabprintf(tab, "filename \"%*.*s\"\n",
369                           namelen, namelen, media.ipdata.filename);
370                 tabprintf(tab, "version  %d\n", media.ipdata.version);
371                 tabprintf(tab, "uflags   0x%08x\n",
372                           media.ipdata.uflags);
373                 if (media.ipdata.rmajor || media.ipdata.rminor) {
374                         tabprintf(tab, "rmajor   %d\n",
375                                   media.ipdata.rmajor);
376                         tabprintf(tab, "rminor   %d\n",
377                                   media.ipdata.rminor);
378                 }
379                 tabprintf(tab, "ctime    %s\n",
380                           hammer2_time64_to_str(media.ipdata.ctime, &str));
381                 tabprintf(tab, "mtime    %s\n",
382                           hammer2_time64_to_str(media.ipdata.mtime, &str));
383                 tabprintf(tab, "atime    %s\n",
384                           hammer2_time64_to_str(media.ipdata.atime, &str));
385                 tabprintf(tab, "btime    %s\n",
386                           hammer2_time64_to_str(media.ipdata.btime, &str));
387                 tabprintf(tab, "uid      %s\n",
388                           hammer2_uuid_to_str(&media.ipdata.uid, &str));
389                 tabprintf(tab, "gid      %s\n",
390                           hammer2_uuid_to_str(&media.ipdata.gid, &str));
391                 tabprintf(tab, "type     %s\n",
392                           hammer2_iptype_to_str(media.ipdata.type));
393                 tabprintf(tab, "opflgs   0x%02x\n",
394                           media.ipdata.op_flags);
395                 tabprintf(tab, "capflgs  0x%04x\n",
396                           media.ipdata.cap_flags);
397                 tabprintf(tab, "mode     %-7o\n",
398                           media.ipdata.mode);
399                 tabprintf(tab, "inum     0x%016jx\n",
400                           media.ipdata.inum);
401                 tabprintf(tab, "size     %ju\n",
402                           (uintmax_t)media.ipdata.size);
403                 tabprintf(tab, "nlinks   %ju\n",
404                           (uintmax_t)media.ipdata.nlinks);
405                 tabprintf(tab, "iparent  0x%016jx\n",
406                           (uintmax_t)media.ipdata.iparent);
407                 tabprintf(tab, "name_key 0x%016jx\n",
408                           (uintmax_t)media.ipdata.name_key);
409                 tabprintf(tab, "name_len %u\n",
410                           media.ipdata.name_len);
411                 tabprintf(tab, "ncopies  %u\n",
412                           media.ipdata.ncopies);
413                 tabprintf(tab, "compalg  %u\n",
414                           media.ipdata.comp_algo);
415                 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) {
416                         tabprintf(tab, "pfs_type %u (%s)\n",
417                                   media.ipdata.pfs_type,
418                                   hammer2_pfstype_to_str(media.ipdata.pfs_type));
419                         tabprintf(tab, "pfs_inum 0x%016jx\n",
420                                   (uintmax_t)media.ipdata.pfs_inum);
421                         tabprintf(tab, "pfs_id   %s\n",
422                                   hammer2_uuid_to_str(&media.ipdata.pfs_id,
423                                                       &str));
424                         tabprintf(tab, "pfs_fsid %s\n",
425                                   hammer2_uuid_to_str(&media.ipdata.pfs_fsid,
426                                                       &str));
427                 }
428                 tabprintf(tab, "data_quota  %ju\n",
429                           (uintmax_t)media.ipdata.data_quota);
430                 tabprintf(tab, "data_count  %ju\n",
431                           (uintmax_t)media.ipdata.data_count);
432                 tabprintf(tab, "inode_quota %ju\n",
433                           (uintmax_t)media.ipdata.inode_quota);
434                 tabprintf(tab, "inode_count %ju\n",
435                           (uintmax_t)media.ipdata.inode_count);
436                 tabprintf(tab, "attr_tid    0x%016jx\n",
437                           (uintmax_t)media.ipdata.attr_tid);
438                 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) {
439                         tabprintf(tab, "dirent_tid  %016jx\n",
440                                   (uintmax_t)media.ipdata.dirent_tid);
441                 }
442                 break;
443         case HAMMER2_BREF_TYPE_INDIRECT:
444                 bscan = &media.npdata.blockref[0];
445                 bcount = bytes / sizeof(hammer2_blockref_t);
446                 didnl = 1;
447                 printf("{\n");
448                 break;
449         case HAMMER2_BREF_TYPE_DATA:
450                 if (VerboseOpt >= 2) {
451                         printf("{\n");
452                 } else {
453                         printf("\n");
454                         obrace = 0;
455                 }
456                 break;
457         case HAMMER2_BREF_TYPE_VOLUME:
458                 bscan = &media.voldata.sroot_blockset.blockref[0];
459                 bcount = HAMMER2_SET_COUNT;
460                 printf("{\n");
461                 break;
462         default:
463                 break;
464         }
465         if (str)
466                 free(str);
467         for (i = 0; i < bcount; ++i) {
468                 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) {
469                         if (didnl == 0) {
470                                 printf("\n");
471                                 didnl = 1;
472                         }
473                         show_bref(fd, tab, i, &bscan[i]);
474                 }
475         }
476         tab -= SHOW_TAB;
477         if (obrace) {
478                 if (bref->type == HAMMER2_BREF_TYPE_INODE)
479                         tabprintf(tab, "} (%s.%d, \"%s\")\n",
480                                   type_str, bi, media.ipdata.filename);
481                 else
482                         tabprintf(tab, "} (%s.%d)\n", type_str,bi);
483         }
484 }
485
486 static
487 void
488 tabprintf(int tab, const char *ctl, ...)
489 {
490         va_list va;
491
492         printf("%*.*s", tab, tab, "");
493         va_start(va, ctl);
494         vprintf(ctl, va);
495         va_end(va);
496 }