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.circuit0, 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->circuit0, 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_circuit_t *circuit, char *cmdbuf); 172 static void shell_circ(dmsg_circuit_t *circuit, char *cmdbuf); 173 174 void 175 hammer2_shell_parse(dmsg_msg_t *msg) 176 { 177 dmsg_circuit_t *circuit = msg->circuit; 178 char *cmdbuf = msg->aux_data; 179 char *cmd = strsep(&cmdbuf, " \t"); 180 181 if (cmd == NULL || *cmd == 0) { 182 ; 183 } else if (strcmp(cmd, "span") == 0) { 184 shell_span(circuit, cmdbuf); 185 } else if (strcmp(cmd, "circ") == 0) { 186 shell_circ(circuit, cmdbuf); 187 } else if (strcmp(cmd, "tree") == 0) { 188 dmsg_shell_tree(circuit, cmdbuf); /* dump spanning tree */ 189 } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) { 190 dmsg_circuit_printf(circuit, "help Command help\n"); 191 dmsg_circuit_printf(circuit, "span <host> Span to target host\n"); 192 dmsg_circuit_printf(circuit, "circ <msgid> Create VC to msgid of rx SPAN\n"); 193 dmsg_circuit_printf(circuit, "tree Dump spanning tree\n"); 194 } else { 195 dmsg_circuit_printf(circuit, "Unrecognized command: %s\n", cmd); 196 } 197 } 198 199 static void 200 shell_span(dmsg_circuit_t *circuit, char *cmdbuf) 201 { 202 dmsg_master_service_info_t *info; 203 const char *hostname = strsep(&cmdbuf, " \t"); 204 pthread_t thread; 205 int fd; 206 207 /* 208 * Connect to the target 209 */ 210 if (hostname == NULL) { 211 fd = -1; 212 } else { 213 fd = dmsg_connect(hostname); 214 } 215 216 /* 217 * Start master service 218 */ 219 if (fd < 0) { 220 dmsg_circuit_printf(circuit, 221 "Connection to %s failed\n", 222 hostname); 223 } else { 224 dmsg_circuit_printf(circuit, "Connected to %s\n", hostname); 225 226 info = malloc(sizeof(*info)); 227 bzero(info, sizeof(*info)); 228 info->fd = fd; 229 info->detachme = 1; 230 info->dbgmsg_callback = hammer2_shell_parse; 231 info->label = strdup("client"); 232 233 pthread_create(&thread, NULL, dmsg_master_service, info); 234 /*pthread_join(thread, &res);*/ 235 } 236 } 237 238 static void shell_circ_reply(dmsg_msg_t *msg); 239 240 static void 241 shell_circ(dmsg_circuit_t *circuit, char *cmdbuf) 242 { 243 uint64_t msgid = strtoull(cmdbuf, NULL, 16); 244 dmsg_state_t *state; 245 dmsg_msg_t *msg; 246 247 if (dmsg_debug_findspan(msgid, &state) == 0) { 248 dmsg_circuit_printf(circuit, "Found state %p\n", state); 249 250 dmsg_circuit_printf(circuit, "Establishing CIRC\n"); 251 msg = dmsg_msg_alloc(&state->iocom->circuit0, 0, 252 DMSG_LNK_CIRC | DMSGF_CREATE, 253 shell_circ_reply, circuit); 254 msg->any.lnk_circ.target = state->msgid; 255 dmsg_msg_write(msg); 256 } else { 257 dmsg_circuit_printf(circuit, 258 "Unable to locate %016jx\n", 259 (intmax_t)msgid); 260 } 261 } 262 263 static void 264 shell_circ_reply(dmsg_msg_t *msg) 265 { 266 dmsg_circuit_t *circ = msg->state->any.circ; 267 268 if (msg->any.head.cmd & DMSGF_DELETE) { 269 dmsg_circuit_printf(circ, "rxmsg DELETE error %d\n", 270 msg->any.head.error); 271 msg->state->any.circ = NULL; 272 } else { 273 dmsg_circuit_printf(circ, "rxmsg result error %d\n", 274 msg->any.head.error); 275 } 276 } 277 278 /************************************************************************ 279 * DEBUGSPAN * 280 ************************************************************************ 281 * 282 * Connect to the target manually (not via the cluster list embedded in 283 * a hammer2 filesystem) and initiate the SPAN protocol. 284 */ 285 int 286 cmd_debugspan(const char *hostname) 287 { 288 pthread_t thread; 289 int fd; 290 void *res; 291 292 /* 293 * Connect to the target 294 */ 295 fd = dmsg_connect(hostname); 296 if (fd < 0) 297 return 1; 298 299 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname); 300 pthread_create(&thread, NULL, 301 dmsg_master_service, (void *)(intptr_t)fd); 302 pthread_join(thread, &res); 303 return(0); 304 } 305 306 /************************************************************************ 307 * SHOW * 308 ************************************************************************/ 309 310 static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref); 311 static void tabprintf(int tab, const char *ctl, ...); 312 313 int 314 cmd_show(const char *devpath) 315 { 316 hammer2_blockref_t broot; 317 hammer2_blockref_t best; 318 hammer2_media_data_t media; 319 int fd; 320 int i; 321 int best_i; 322 323 fd = open(devpath, O_RDONLY); 324 if (fd < 0) { 325 perror("open"); 326 return 1; 327 } 328 329 /* 330 * Show the tree using the best volume header. 331 * -vvv will show the tree for all four volume headers. 332 */ 333 best_i = -1; 334 bzero(&best, sizeof(best)); 335 for (i = 0; i < 4; ++i) { 336 bzero(&broot, sizeof(broot)); 337 broot.type = HAMMER2_BREF_TYPE_VOLUME; 338 broot.data_off = (i * HAMMER2_ZONE_BYTES64) | 339 HAMMER2_PBUFRADIX; 340 lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, 0); 341 if (read(fd, &media, HAMMER2_PBUFSIZE) == 342 (ssize_t)HAMMER2_PBUFSIZE) { 343 broot.mirror_tid = media.voldata.mirror_tid; 344 if (best_i < 0 || best.mirror_tid < broot.mirror_tid) { 345 best_i = i; 346 best = broot; 347 } 348 if (VerboseOpt >= 3) 349 show_bref(fd, 0, i, &broot); 350 } 351 } 352 if (VerboseOpt < 3) 353 show_bref(fd, 0, best_i, &best); 354 close(fd); 355 356 return 0; 357 } 358 359 static void 360 show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref) 361 { 362 hammer2_media_data_t media; 363 hammer2_blockref_t *bscan; 364 int bcount; 365 int i; 366 int didnl; 367 int namelen; 368 int obrace = 1; 369 size_t bytes; 370 const char *type_str; 371 char *str = NULL; 372 373 switch(bref->type) { 374 case HAMMER2_BREF_TYPE_EMPTY: 375 type_str = "empty"; 376 break; 377 case HAMMER2_BREF_TYPE_INODE: 378 type_str = "inode"; 379 break; 380 case HAMMER2_BREF_TYPE_INDIRECT: 381 type_str = "indblk"; 382 break; 383 case HAMMER2_BREF_TYPE_DATA: 384 type_str = "data"; 385 break; 386 case HAMMER2_BREF_TYPE_VOLUME: 387 type_str = "volume"; 388 break; 389 default: 390 type_str = "unknown"; 391 break; 392 } 393 394 395 tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ", 396 type_str, bi, (intmax_t)bref->data_off, 397 (intmax_t)bref->key, (intmax_t)bref->keybits, 398 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid); 399 tab += SHOW_TAB; 400 401 bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX); 402 if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) { 403 printf("(bad block size %zd)\n", bytes); 404 return; 405 } 406 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) { 407 lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0); 408 if (read(fd, &media, bytes) != (ssize_t)bytes) { 409 printf("(media read failed)\n"); 410 return; 411 } 412 } 413 414 bscan = NULL; 415 bcount = 0; 416 didnl = 0; 417 418 switch(bref->type) { 419 case HAMMER2_BREF_TYPE_EMPTY: 420 obrace = 0; 421 break; 422 case HAMMER2_BREF_TYPE_INODE: 423 printf("{\n"); 424 if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) { 425 /* no blockrefs */ 426 } else { 427 bscan = &media.ipdata.u.blockset.blockref[0]; 428 bcount = HAMMER2_SET_COUNT; 429 } 430 namelen = media.ipdata.name_len; 431 if (namelen > HAMMER2_INODE_MAXNAME) 432 namelen = 0; 433 tabprintf(tab, "filename \"%*.*s\"\n", 434 namelen, namelen, media.ipdata.filename); 435 tabprintf(tab, "version %d\n", media.ipdata.version); 436 tabprintf(tab, "uflags 0x%08x\n", 437 media.ipdata.uflags); 438 if (media.ipdata.rmajor || media.ipdata.rminor) { 439 tabprintf(tab, "rmajor %d\n", 440 media.ipdata.rmajor); 441 tabprintf(tab, "rminor %d\n", 442 media.ipdata.rminor); 443 } 444 tabprintf(tab, "ctime %s\n", 445 hammer2_time64_to_str(media.ipdata.ctime, &str)); 446 tabprintf(tab, "mtime %s\n", 447 hammer2_time64_to_str(media.ipdata.mtime, &str)); 448 tabprintf(tab, "atime %s\n", 449 hammer2_time64_to_str(media.ipdata.atime, &str)); 450 tabprintf(tab, "btime %s\n", 451 hammer2_time64_to_str(media.ipdata.btime, &str)); 452 tabprintf(tab, "uid %s\n", 453 hammer2_uuid_to_str(&media.ipdata.uid, &str)); 454 tabprintf(tab, "gid %s\n", 455 hammer2_uuid_to_str(&media.ipdata.gid, &str)); 456 tabprintf(tab, "type %s\n", 457 hammer2_iptype_to_str(media.ipdata.type)); 458 tabprintf(tab, "opflgs 0x%02x\n", 459 media.ipdata.op_flags); 460 tabprintf(tab, "capflgs 0x%04x\n", 461 media.ipdata.cap_flags); 462 tabprintf(tab, "mode %-7o\n", 463 media.ipdata.mode); 464 tabprintf(tab, "inum 0x%016jx\n", 465 media.ipdata.inum); 466 tabprintf(tab, "size %ju\n", 467 (uintmax_t)media.ipdata.size); 468 tabprintf(tab, "nlinks %ju\n", 469 (uintmax_t)media.ipdata.nlinks); 470 tabprintf(tab, "iparent 0x%016jx\n", 471 (uintmax_t)media.ipdata.iparent); 472 tabprintf(tab, "name_key 0x%016jx\n", 473 (uintmax_t)media.ipdata.name_key); 474 tabprintf(tab, "name_len %u\n", 475 media.ipdata.name_len); 476 tabprintf(tab, "ncopies %u\n", 477 media.ipdata.ncopies); 478 tabprintf(tab, "compalg %u\n", 479 media.ipdata.comp_algo); 480 if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) { 481 tabprintf(tab, "pfs_type %u (%s)\n", 482 media.ipdata.pfs_type, 483 hammer2_pfstype_to_str(media.ipdata.pfs_type)); 484 tabprintf(tab, "pfs_inum 0x%016jx\n", 485 (uintmax_t)media.ipdata.pfs_inum); 486 tabprintf(tab, "pfs_clid %s\n", 487 hammer2_uuid_to_str(&media.ipdata.pfs_clid, 488 &str)); 489 tabprintf(tab, "pfs_fsid %s\n", 490 hammer2_uuid_to_str(&media.ipdata.pfs_fsid, 491 &str)); 492 } 493 tabprintf(tab, "data_quota %ju\n", 494 (uintmax_t)media.ipdata.data_quota); 495 tabprintf(tab, "data_count %ju\n", 496 (uintmax_t)media.ipdata.data_count); 497 tabprintf(tab, "inode_quota %ju\n", 498 (uintmax_t)media.ipdata.inode_quota); 499 tabprintf(tab, "inode_count %ju\n", 500 (uintmax_t)media.ipdata.inode_count); 501 tabprintf(tab, "attr_tid 0x%016jx\n", 502 (uintmax_t)media.ipdata.attr_tid); 503 if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) { 504 tabprintf(tab, "dirent_tid %016jx\n", 505 (uintmax_t)media.ipdata.dirent_tid); 506 } 507 break; 508 case HAMMER2_BREF_TYPE_INDIRECT: 509 bscan = &media.npdata.blockref[0]; 510 bcount = bytes / sizeof(hammer2_blockref_t); 511 didnl = 1; 512 printf("{\n"); 513 break; 514 case HAMMER2_BREF_TYPE_DATA: 515 if (VerboseOpt >= 2) { 516 printf("{\n"); 517 } else { 518 printf("\n"); 519 obrace = 0; 520 } 521 break; 522 case HAMMER2_BREF_TYPE_VOLUME: 523 bscan = &media.voldata.sroot_blockset.blockref[0]; 524 bcount = HAMMER2_SET_COUNT; 525 printf("{\n"); 526 break; 527 default: 528 break; 529 } 530 if (str) 531 free(str); 532 for (i = 0; i < bcount; ++i) { 533 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) { 534 if (didnl == 0) { 535 printf("\n"); 536 didnl = 1; 537 } 538 show_bref(fd, tab, i, &bscan[i]); 539 } 540 } 541 tab -= SHOW_TAB; 542 if (obrace) { 543 if (bref->type == HAMMER2_BREF_TYPE_INODE) 544 tabprintf(tab, "} (%s.%d, \"%s\")\n", 545 type_str, bi, media.ipdata.filename); 546 else 547 tabprintf(tab, "} (%s.%d)\n", type_str,bi); 548 } 549 } 550 551 int 552 cmd_hash(int ac, const char **av) 553 { 554 int i; 555 556 for (i = 0; i < ac; ++i) { 557 printf("%016jx %s\n", dirhash(av[i], strlen(av[i])), av[i]); 558 } 559 return(0); 560 } 561 562 563 static 564 void 565 tabprintf(int tab, const char *ctl, ...) 566 { 567 va_list va; 568 569 printf("%*.*s", tab, tab, ""); 570 va_start(va, ctl); 571 vprintf(ctl, va); 572 va_end(va); 573 } 574