2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
40 static void shell_msghandler(dmsg_msg_t
*msg
, int unmanaged
);
41 static void shell_ttymsg(dmsg_iocom_t
*iocom
);
43 /************************************************************************
45 ************************************************************************/
48 cmd_shell(const char *hostname
)
50 dmsg_master_service_info_t
*info
;
54 fd
= dmsg_connect(hostname
);
58 info
= malloc(sizeof(*info
));
59 bzero(info
, sizeof(*info
));
62 info
->usrmsg_callback
= shell_msghandler
;
63 info
->altmsg_callback
= shell_ttymsg
;
64 info
->label
= strdup("debug");
65 pthread_create(&thread
, NULL
, dmsg_master_service
, info
);
66 pthread_join(thread
, NULL
);
73 cmd_shell(const char *hostname
)
75 struct dmsg_iocom iocom
;
80 * Connect to the target
82 fd
= dmsg_connect(hostname
);
87 * Initialize the session and transmit an empty DMSG_DBG_SHELL
88 * to cause the remote end to generate a prompt.
90 dmsg_iocom_init(&iocom
, fd
, 0,
95 fcntl(0, F_SETFL
, O_NONBLOCK
);
96 printf("debug: connected\n");
98 msg
= dmsg_msg_alloc(&iocom
.state0
, 0, DMSG_DBG_SHELL
, NULL
, NULL
);
100 dmsg_iocom_core(&iocom
);
101 fprintf(stderr
, "debug: disconnected\n");
108 * Debug session front-end
110 * Callback from dmsg_iocom_core() when messages might be present
115 shell_msghandler(dmsg_msg_t
*msg
, int unmanaged
)
122 case DMSG_LNK_ERROR
| DMSGF_REPLY
:
124 * One-way non-transactional LNK_ERROR messages typically
125 * indicate a connection failure. Error code 0 is used by
126 * the debug shell to indicate no more results from last cmd.
128 if (msg
->any
.head
.error
) {
129 fprintf(stderr
, "Stream failure: %s\n",
132 write(1, "debug> ", 7);
135 case DMSG_LNK_ERROR
| DMSGF_DELETE
:
136 /* ignore termination of LNK_CONN */
141 * We send the commands, not accept them.
142 * (one-way message, not transactional)
145 dmsg_msg_reply(msg
, DMSG_ERR_NOSUPP
);
147 case DMSG_DBG_SHELL
| DMSGF_REPLY
:
149 * A reply from the remote is data we copy to stdout.
150 * (one-way message, not transactional)
153 msg
->aux_data
[msg
->aux_size
- 1] = 0;
154 write(1, msg
->aux_data
, strlen(msg
->aux_data
));
158 case DMSG_LNK_CONN
| DMSGF_CREATE
:
159 fprintf(stderr
, "Debug Shell received LNK_CONN\n");
160 nmsg
= dmsg_msg_alloc(&msg
->state
->iocom
->state0
, 0,
163 dmsg_msg_write(nmsg
);
164 dmsg_msg_reply(msg
, DMSG_ERR_NOSUPP
);
166 case DMSG_LNK_CONN
| DMSGF_DELETE
:
171 * Ignore any unknown messages, Terminate any unknown
172 * transactions with an error.
174 fprintf(stderr
, "Unknown message: %s\n", dmsg_msg_str(msg
));
176 if (msg
->any
.head
.cmd
& DMSGF_CREATE
)
177 dmsg_msg_reply(msg
, DMSG_ERR_NOSUPP
);
178 if (msg
->any
.head
.cmd
& DMSGF_DELETE
)
179 dmsg_msg_reply(msg
, DMSG_ERR_NOSUPP
);
186 * Debug session front-end
190 shell_ttymsg(dmsg_iocom_t
*iocom
)
192 dmsg_state_t
*pstate
;
198 if (fgets(buf
, sizeof(buf
), stdin
) != NULL
) {
200 pstate
= dmsg_findspan(strtok(buf
+ 1, " \t\n"));
201 cmd
= strtok(NULL
, "\n");
203 pstate
= &iocom
->state0
;
204 cmd
= strtok(buf
, "\n");
207 len
= strlen(cmd
) + 1;
208 msg
= dmsg_msg_alloc(pstate
, len
, DMSG_DBG_SHELL
,
210 bcopy(cmd
, msg
->aux_data
, len
);
213 fprintf(stderr
, "@msgid not found\n");
216 * This should cause the remote end to generate
217 * a debug> prompt (and thus shows that there is
220 msg
= dmsg_msg_alloc(pstate
, 0, DMSG_DBG_SHELL
,
224 } else if (feof(stdin
)) {
226 * Set EOF flag without setting any error code for normal
229 iocom
->flags
|= DMSG_IOCOMF_EOF
;
236 * Debug session back-end (on remote side)
238 static void shell_span(dmsg_msg_t
*msg
, char *cmdbuf
);
241 hammer2_shell_parse(dmsg_msg_t
*msg
, int unmanaged
)
243 dmsg_iocom_t
*iocom
= msg
->state
->iocom
;
249 * Filter on debug shell commands only
251 cmd
= msg
->any
.head
.cmd
;
252 if ((cmd
& DMSGF_PROTOS
) != DMSG_PROTO_DBG
) {
254 dmsg_msg_reply(msg
, DMSG_ERR_NOSUPP
);
257 if ((cmd
& DMSGF_CMDSWMASK
) != DMSG_DBG_SHELL
) {
259 dmsg_msg_reply(msg
, DMSG_ERR_NOSUPP
);
264 * Debug shell command
266 cmdbuf
= msg
->aux_data
;
267 cmdp
= strsep(&cmdbuf
, " \t");
269 if (cmdp
== NULL
|| *cmdp
== 0) {
271 } else if (strcmp(cmdp
, "span") == 0) {
272 shell_span(msg
, cmdbuf
);
273 } else if (strcmp(cmdp
, "tree") == 0) {
274 dmsg_shell_tree(iocom
, cmdbuf
); /* dump spanning tree */
275 } else if (strcmp(cmdp
, "help") == 0 || strcmp(cmdp
, "?") == 0) {
276 dmsg_printf(iocom
, "help Command help\n");
277 dmsg_printf(iocom
, "span <host> Span to target host\n");
278 dmsg_printf(iocom
, "tree Dump spanning tree\n");
279 dmsg_printf(iocom
, "@span <cmd> Issue via circuit\n");
281 dmsg_printf(iocom
, "Unrecognized command: %s\n", cmdp
);
283 dmsg_printf(iocom
, "debug> ");
287 shell_span(dmsg_msg_t
*msg
, char *cmdbuf
)
289 dmsg_iocom_t
*iocom
= msg
->state
->iocom
;
290 dmsg_master_service_info_t
*info
;
291 const char *hostname
= strsep(&cmdbuf
, " \t");
296 * Connect to the target
298 if (hostname
== NULL
) {
301 fd
= dmsg_connect(hostname
);
305 * Start master service
308 dmsg_printf(iocom
, "Connection to %s failed\n", hostname
);
310 dmsg_printf(iocom
, "Connected to %s\n", hostname
);
312 info
= malloc(sizeof(*info
));
313 bzero(info
, sizeof(*info
));
316 info
->usrmsg_callback
= hammer2_shell_parse
;
317 info
->label
= strdup("client");
319 pthread_create(&thread
, NULL
, dmsg_master_service
, info
);
320 /*pthread_join(thread, &res);*/
324 /************************************************************************
326 ************************************************************************
328 * Connect to the target manually (not via the cluster list embedded in
329 * a hammer2 filesystem) and initiate the SPAN protocol.
332 cmd_debugspan(const char *hostname
)
339 * Connect to the target
341 fd
= dmsg_connect(hostname
);
345 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname
);
346 pthread_create(&thread
, NULL
,
347 dmsg_master_service
, (void *)(intptr_t)fd
);
348 pthread_join(thread
, &res
);
352 /************************************************************************
354 ************************************************************************/
356 static void show_bref(int fd
, int tab
, int bi
, hammer2_blockref_t
*bref
,
358 static void tabprintf(int tab
, const char *ctl
, ...);
361 cmd_show(const char *devpath
, int dofreemap
)
363 hammer2_blockref_t broot
;
364 hammer2_blockref_t best
;
365 hammer2_media_data_t media
;
370 fd
= open(devpath
, O_RDONLY
);
377 * Show the tree using the best volume header.
378 * -vvv will show the tree for all four volume headers.
381 bzero(&best
, sizeof(best
));
382 for (i
= 0; i
< 4; ++i
) {
383 bzero(&broot
, sizeof(broot
));
384 broot
.type
= HAMMER2_BREF_TYPE_VOLUME
;
385 broot
.data_off
= (i
* HAMMER2_ZONE_BYTES64
) |
387 lseek(fd
, broot
.data_off
& ~HAMMER2_OFF_MASK_RADIX
, 0);
388 if (read(fd
, &media
, HAMMER2_PBUFSIZE
) ==
389 (ssize_t
)HAMMER2_PBUFSIZE
) {
390 broot
.mirror_tid
= media
.voldata
.mirror_tid
;
391 if (best_i
< 0 || best
.mirror_tid
< broot
.mirror_tid
) {
396 show_bref(fd
, 0, i
, &broot
, dofreemap
);
400 show_bref(fd
, 0, best_i
, &best
, dofreemap
);
406 extern uint32_t iscsi_crc32(const void *buf
, size_t size
);
408 show_bref(int fd
, int tab
, int bi
, hammer2_blockref_t
*bref
, int dofreemap
)
410 hammer2_media_data_t media
;
411 hammer2_blockref_t
*bscan
;
418 const char *type_str
;
423 case HAMMER2_BREF_TYPE_EMPTY
:
426 case HAMMER2_BREF_TYPE_INODE
:
429 case HAMMER2_BREF_TYPE_INDIRECT
:
432 case HAMMER2_BREF_TYPE_DATA
:
435 case HAMMER2_BREF_TYPE_VOLUME
:
438 case HAMMER2_BREF_TYPE_FREEMAP
:
439 type_str
= "freemap";
441 case HAMMER2_BREF_TYPE_FREEMAP_NODE
:
442 type_str
= "fmapnode";
444 case HAMMER2_BREF_TYPE_FREEMAP_LEAF
:
445 type_str
= "fbitmap";
448 type_str
= "unknown";
452 tabprintf(tab
, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ",
453 type_str
, bi
, (intmax_t)bref
->data_off
,
454 (intmax_t)bref
->key
, (intmax_t)bref
->keybits
,
455 (intmax_t)bref
->mirror_tid
, (intmax_t)bref
->modify_tid
);
458 bytes
= (size_t)1 << (bref
->data_off
& HAMMER2_OFF_MASK_RADIX
);
461 hammer2_off_t io_off
;
462 hammer2_off_t io_base
;
466 io_off
= bref
->data_off
& ~HAMMER2_OFF_MASK_RADIX
;
467 io_base
= io_off
& ~(hammer2_off_t
)(HAMMER2_MINIOSIZE
- 1);
469 boff
= io_off
- io_base
;
471 io_bytes
= HAMMER2_MINIOSIZE
;
472 while (io_bytes
+ boff
< bytes
)
475 if (io_bytes
> sizeof(media
)) {
476 printf("(bad block size %zd)\n", bytes
);
479 if (bref
->type
!= HAMMER2_BREF_TYPE_DATA
|| VerboseOpt
>= 1) {
480 lseek(fd
, io_base
, 0);
481 if (read(fd
, &media
, io_bytes
) != (ssize_t
)io_bytes
) {
482 printf("(media read failed)\n");
486 bcopy((char *)&media
+ boff
, &media
, bytes
);
496 * Check data integrity in verbose mode, otherwise we are just doing
497 * a quick meta-data scan. Meta-data integrity is always checked.
498 * (Also see the check above that ensures the media data is loaded,
499 * otherwise there's no data to check!).
501 if (bref
->type
!= HAMMER2_BREF_TYPE_DATA
|| VerboseOpt
>= 1) {
502 switch(HAMMER2_DEC_CHECK(bref
->methods
)) {
503 case HAMMER2_CHECK_NONE
:
504 printf("(meth %02x) ", bref
->methods
);
506 case HAMMER2_CHECK_DISABLED
:
507 printf("(meth %02x) ", bref
->methods
);
509 case HAMMER2_CHECK_ISCSI32
:
510 cv
= hammer2_icrc32(&media
, bytes
);
511 if (bref
->check
.iscsi32
.value
!= cv
) {
512 printf("(icrc %02x:%08x/%08x) ",
514 bref
->check
.iscsi32
.value
,
517 printf("(meth %02x) ", bref
->methods
);
520 case HAMMER2_CHECK_CRC64
:
521 printf("(meth %02x) ", bref
->methods
);
523 case HAMMER2_CHECK_SHA192
:
524 printf("(meth %02x) ", bref
->methods
);
526 case HAMMER2_CHECK_FREEMAP
:
527 cv
= hammer2_icrc32(&media
, bytes
);
528 if (bref
->check
.freemap
.icrc32
!= cv
) {
529 printf("(fcrc %02x:%08x/%08x) ",
531 bref
->check
.freemap
.icrc32
,
534 printf("(meth %02x) ", bref
->methods
);
541 case HAMMER2_BREF_TYPE_EMPTY
:
544 case HAMMER2_BREF_TYPE_INODE
:
546 if (media
.ipdata
.op_flags
& HAMMER2_OPFLAG_DIRECTDATA
) {
549 bscan
= &media
.ipdata
.u
.blockset
.blockref
[0];
550 bcount
= HAMMER2_SET_COUNT
;
552 namelen
= media
.ipdata
.name_len
;
553 if (namelen
> HAMMER2_INODE_MAXNAME
)
555 tabprintf(tab
, "filename \"%*.*s\"\n",
556 namelen
, namelen
, media
.ipdata
.filename
);
557 tabprintf(tab
, "version %d\n", media
.ipdata
.version
);
558 tabprintf(tab
, "uflags 0x%08x\n",
559 media
.ipdata
.uflags
);
560 if (media
.ipdata
.rmajor
|| media
.ipdata
.rminor
) {
561 tabprintf(tab
, "rmajor %d\n",
562 media
.ipdata
.rmajor
);
563 tabprintf(tab
, "rminor %d\n",
564 media
.ipdata
.rminor
);
566 tabprintf(tab
, "ctime %s\n",
567 hammer2_time64_to_str(media
.ipdata
.ctime
, &str
));
568 tabprintf(tab
, "mtime %s\n",
569 hammer2_time64_to_str(media
.ipdata
.mtime
, &str
));
570 tabprintf(tab
, "atime %s\n",
571 hammer2_time64_to_str(media
.ipdata
.atime
, &str
));
572 tabprintf(tab
, "btime %s\n",
573 hammer2_time64_to_str(media
.ipdata
.btime
, &str
));
574 tabprintf(tab
, "uid %s\n",
575 hammer2_uuid_to_str(&media
.ipdata
.uid
, &str
));
576 tabprintf(tab
, "gid %s\n",
577 hammer2_uuid_to_str(&media
.ipdata
.gid
, &str
));
578 if (media
.ipdata
.type
== HAMMER2_OBJTYPE_HARDLINK
)
579 tabprintf(tab
, "type %s (%s)\n",
580 hammer2_iptype_to_str(media
.ipdata
.type
),
581 hammer2_iptype_to_str(media
.ipdata
.target_type
));
583 tabprintf(tab
, "type %s\n",
584 hammer2_iptype_to_str(media
.ipdata
.type
));
585 tabprintf(tab
, "opflgs 0x%02x\n",
586 media
.ipdata
.op_flags
);
587 tabprintf(tab
, "capflgs 0x%04x\n",
588 media
.ipdata
.cap_flags
);
589 tabprintf(tab
, "mode %-7o\n",
591 tabprintf(tab
, "inum 0x%016jx\n",
593 tabprintf(tab
, "size %ju\n",
594 (uintmax_t)media
.ipdata
.size
);
595 tabprintf(tab
, "nlinks %ju\n",
596 (uintmax_t)media
.ipdata
.nlinks
);
597 tabprintf(tab
, "iparent 0x%016jx\n",
598 (uintmax_t)media
.ipdata
.iparent
);
599 tabprintf(tab
, "name_key 0x%016jx\n",
600 (uintmax_t)media
.ipdata
.name_key
);
601 tabprintf(tab
, "name_len %u\n",
602 media
.ipdata
.name_len
);
603 tabprintf(tab
, "ncopies %u\n",
604 media
.ipdata
.ncopies
);
605 tabprintf(tab
, "compalg %u\n",
606 media
.ipdata
.comp_algo
);
607 tabprintf(tab
, "checkalg %u\n",
608 media
.ipdata
.check_algo
);
609 if (media
.ipdata
.op_flags
& HAMMER2_OPFLAG_PFSROOT
) {
610 tabprintf(tab
, "pfs_type %u (%s)\n",
611 media
.ipdata
.pfs_type
,
612 hammer2_pfstype_to_str(media
.ipdata
.pfs_type
));
613 tabprintf(tab
, "pfs_inum 0x%016jx\n",
614 (uintmax_t)media
.ipdata
.pfs_inum
);
615 tabprintf(tab
, "pfs_clid %s\n",
616 hammer2_uuid_to_str(&media
.ipdata
.pfs_clid
,
618 tabprintf(tab
, "pfs_fsid %s\n",
619 hammer2_uuid_to_str(&media
.ipdata
.pfs_fsid
,
622 tabprintf(tab
, "data_quota %ju\n",
623 (uintmax_t)media
.ipdata
.data_quota
);
624 tabprintf(tab
, "data_count %ju\n",
625 (uintmax_t)media
.ipdata
.data_count
);
626 tabprintf(tab
, "inode_quota %ju\n",
627 (uintmax_t)media
.ipdata
.inode_quota
);
628 tabprintf(tab
, "inode_count %ju\n",
629 (uintmax_t)media
.ipdata
.inode_count
);
630 tabprintf(tab
, "attr_tid 0x%016jx\n",
631 (uintmax_t)media
.ipdata
.attr_tid
);
632 if (media
.ipdata
.type
== HAMMER2_OBJTYPE_DIRECTORY
) {
633 tabprintf(tab
, "dirent_tid %016jx\n",
634 (uintmax_t)media
.ipdata
.dirent_tid
);
637 case HAMMER2_BREF_TYPE_INDIRECT
:
638 bscan
= &media
.npdata
[0];
639 bcount
= bytes
/ sizeof(hammer2_blockref_t
);
643 case HAMMER2_BREF_TYPE_DATA
:
644 if (VerboseOpt
>= 2) {
651 case HAMMER2_BREF_TYPE_VOLUME
:
652 printf("mirror_tid=%016jx freemap_tid=%016jx ",
653 media
.voldata
.mirror_tid
,
654 media
.voldata
.freemap_tid
);
656 bscan
= &media
.voldata
.freemap_blockset
.blockref
[0];
657 bcount
= HAMMER2_SET_COUNT
;
659 bscan
= &media
.voldata
.sroot_blockset
.blockref
[0];
660 bcount
= HAMMER2_SET_COUNT
;
664 case HAMMER2_BREF_TYPE_FREEMAP_LEAF
:
666 for (i
= 0; i
< HAMMER2_FREEMAP_COUNT
; ++i
) {
667 if (media
.bmdata
[i
].class == 0 &&
668 media
.bmdata
[i
].avail
== 0) {
671 tabprintf(tab
+ 4, "%04d.%04x (avail=%5d) "
672 "%08x %08x %08x %08x %08x %08x %08x %08x\n",
673 i
, media
.bmdata
[i
].class,
674 media
.bmdata
[i
].avail
,
675 media
.bmdata
[i
].bitmap
[0],
676 media
.bmdata
[i
].bitmap
[1],
677 media
.bmdata
[i
].bitmap
[2],
678 media
.bmdata
[i
].bitmap
[3],
679 media
.bmdata
[i
].bitmap
[4],
680 media
.bmdata
[i
].bitmap
[5],
681 media
.bmdata
[i
].bitmap
[6],
682 media
.bmdata
[i
].bitmap
[7]);
684 tabprintf(tab
, "}\n");
686 case HAMMER2_BREF_TYPE_FREEMAP_NODE
:
688 bscan
= &media
.npdata
[0];
689 bcount
= bytes
/ sizeof(hammer2_blockref_t
);
698 for (i
= 0; i
< bcount
; ++i
) {
699 if (bscan
[i
].type
!= HAMMER2_BREF_TYPE_EMPTY
) {
704 show_bref(fd
, tab
, i
, &bscan
[i
], dofreemap
);
709 if (bref
->type
== HAMMER2_BREF_TYPE_INODE
)
710 tabprintf(tab
, "} (%s.%d, \"%*.*s\")\n",
712 namelen
, namelen
, media
.ipdata
.filename
);
714 tabprintf(tab
, "} (%s.%d)\n", type_str
,bi
);
719 cmd_hash(int ac
, const char **av
)
723 for (i
= 0; i
< ac
; ++i
) {
724 printf("%016jx %s\n", dirhash(av
[i
], strlen(av
[i
])), av
[i
]);
730 cmd_chaindump(const char *path
)
735 fd
= open(path
, O_RDONLY
);
737 ioctl(fd
, HAMMER2IOC_DEBUG_DUMP
, &dummy
);
740 fprintf(stderr
, "unable to open %s\n", path
);
748 tabprintf(int tab
, const char *ctl
, ...)
752 printf("%*.*s", tab
, tab
, "");