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 #include <openssl/sha.h> 39 40 #define GIG (1024LL*1024*1024) 41 #define arysize(ary) (sizeof(ary) / sizeof((ary)[0])) 42 43 static int show_tab = 2; 44 45 static void shell_msghandler(dmsg_msg_t *msg, int unmanaged); 46 static void shell_ttymsg(dmsg_iocom_t *iocom); 47 static void CountBlocks(hammer2_bmap_data_t *bmap, int value, 48 hammer2_off_t *accum16, hammer2_off_t *accum64); 49 50 /************************************************************************ 51 * SHELL * 52 ************************************************************************/ 53 54 int 55 cmd_shell(const char *hostname) 56 { 57 dmsg_master_service_info_t *info; 58 pthread_t thread; 59 int fd; 60 61 fd = dmsg_connect(hostname); 62 if (fd < 0) 63 return 1; 64 65 info = malloc(sizeof(*info)); 66 bzero(info, sizeof(*info)); 67 info->fd = fd; 68 info->detachme = 0; 69 info->usrmsg_callback = shell_msghandler; 70 info->altmsg_callback = shell_ttymsg; 71 info->label = strdup("debug"); 72 pthread_create(&thread, NULL, dmsg_master_service, info); 73 pthread_join(thread, NULL); 74 75 return 0; 76 } 77 78 #if 0 79 int 80 cmd_shell(const char *hostname) 81 { 82 struct dmsg_iocom iocom; 83 dmsg_msg_t *msg; 84 int fd; 85 86 /* 87 * Connect to the target 88 */ 89 fd = dmsg_connect(hostname); 90 if (fd < 0) 91 return 1; 92 93 /* 94 * Initialize the session and transmit an empty DMSG_DBG_SHELL 95 * to cause the remote end to generate a prompt. 96 */ 97 dmsg_iocom_init(&iocom, fd, 0, 98 NULL, 99 shell_rcvmsg, 100 hammer2_shell_parse, 101 shell_ttymsg); 102 fcntl(0, F_SETFL, O_NONBLOCK); 103 printf("debug: connected\n"); 104 105 msg = dmsg_msg_alloc(&iocom.state0, 0, DMSG_DBG_SHELL, NULL, NULL); 106 dmsg_msg_write(msg); 107 dmsg_iocom_core(&iocom); 108 fprintf(stderr, "debug: disconnected\n"); 109 close(fd); 110 return 0; 111 } 112 #endif 113 114 /* 115 * Debug session front-end 116 * 117 * Callback from dmsg_iocom_core() when messages might be present 118 * on the socket. 119 */ 120 static 121 void 122 shell_msghandler(dmsg_msg_t *msg, int unmanaged) 123 { 124 dmsg_msg_t *nmsg; 125 126 switch(msg->tcmd) { 127 #if 0 128 case DMSG_LNK_ERROR: 129 case DMSG_LNK_ERROR | DMSGF_REPLY: 130 /* 131 * One-way non-transactional LNK_ERROR messages typically 132 * indicate a connection failure. Error code 0 is used by 133 * the debug shell to indicate no more results from last cmd. 134 */ 135 if (msg->any.head.error) { 136 fprintf(stderr, "Stream failure: %s\n", 137 dmsg_msg_str(msg)); 138 } else { 139 write(1, "debug> ", 7); 140 } 141 break; 142 case DMSG_LNK_ERROR | DMSGF_DELETE: 143 /* ignore termination of LNK_CONN */ 144 break; 145 #endif 146 case DMSG_DBG_SHELL: 147 /* 148 * We send the commands, not accept them. 149 * (one-way message, not transactional) 150 */ 151 if (unmanaged) 152 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 153 break; 154 case DMSG_DBG_SHELL | DMSGF_REPLY: 155 /* 156 * A reply from the remote is data we copy to stdout. 157 * (one-way message, not transactional) 158 */ 159 if (msg->aux_size) { 160 msg->aux_data[msg->aux_size - 1] = 0; 161 write(1, msg->aux_data, strlen(msg->aux_data)); 162 } 163 break; 164 #if 1 165 case DMSG_LNK_CONN | DMSGF_CREATE: 166 fprintf(stderr, "Debug Shell received LNK_CONN\n"); 167 nmsg = dmsg_msg_alloc(&msg->state->iocom->state0, 0, 168 DMSG_DBG_SHELL, 169 NULL, NULL); 170 dmsg_msg_write(nmsg); 171 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 172 break; 173 case DMSG_LNK_CONN | DMSGF_DELETE: 174 break; 175 #endif 176 default: 177 /* 178 * Ignore any unknown messages, Terminate any unknown 179 * transactions with an error. 180 */ 181 fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg)); 182 if (unmanaged) { 183 if (msg->any.head.cmd & DMSGF_CREATE) 184 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 185 if (msg->any.head.cmd & DMSGF_DELETE) 186 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 187 } 188 break; 189 } 190 } 191 192 /* 193 * Debug session front-end 194 */ 195 static 196 void 197 shell_ttymsg(dmsg_iocom_t *iocom) 198 { 199 dmsg_state_t *pstate; 200 dmsg_msg_t *msg; 201 char buf[256]; 202 char *cmd; 203 size_t len; 204 205 if (fgets(buf, sizeof(buf), stdin) != NULL) { 206 if (buf[0] == '@') { 207 pstate = dmsg_findspan(strtok(buf + 1, " \t\n")); 208 cmd = strtok(NULL, "\n"); 209 } else { 210 pstate = &iocom->state0; 211 cmd = strtok(buf, "\n"); 212 } 213 if (cmd && pstate) { 214 len = strlen(cmd) + 1; 215 msg = dmsg_msg_alloc(pstate, len, DMSG_DBG_SHELL, 216 NULL, NULL); 217 bcopy(cmd, msg->aux_data, len); 218 dmsg_msg_write(msg); 219 } else if (cmd) { 220 fprintf(stderr, "@msgid not found\n"); 221 } else { 222 /* 223 * This should cause the remote end to generate 224 * a debug> prompt (and thus shows that there is 225 * connectivity). 226 */ 227 msg = dmsg_msg_alloc(pstate, 0, DMSG_DBG_SHELL, 228 NULL, NULL); 229 dmsg_msg_write(msg); 230 } 231 } else if (feof(stdin)) { 232 /* 233 * Set EOF flag without setting any error code for normal 234 * EOF. 235 */ 236 iocom->flags |= DMSG_IOCOMF_EOF; 237 } else { 238 clearerr(stdin); 239 } 240 } 241 242 /* 243 * Debug session back-end (on remote side) 244 */ 245 static void shell_span(dmsg_msg_t *msg, char *cmdbuf); 246 static void shell_ping(dmsg_msg_t *msg, char *cmdbuf); 247 248 void 249 hammer2_shell_parse(dmsg_msg_t *msg, int unmanaged) 250 { 251 dmsg_iocom_t *iocom = msg->state->iocom; 252 char *cmdbuf; 253 char *cmdp; 254 uint32_t cmd; 255 256 /* 257 * Filter on debug shell commands and ping responses only 258 */ 259 cmd = msg->any.head.cmd; 260 if ((cmd & DMSGF_CMDSWMASK) == (DMSG_LNK_PING | DMSGF_REPLY)) { 261 dmsg_printf(iocom, "ping reply\n"); 262 return; 263 } 264 265 if ((cmd & DMSGF_PROTOS) != DMSG_PROTO_DBG) { 266 if (unmanaged) 267 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 268 return; 269 } 270 if ((cmd & DMSGF_CMDSWMASK) != DMSG_DBG_SHELL) { 271 if (unmanaged) 272 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 273 return; 274 } 275 276 /* 277 * Debug shell command 278 */ 279 cmdbuf = msg->aux_data; 280 cmdp = strsep(&cmdbuf, " \t"); 281 282 if (cmdp == NULL || *cmdp == 0) { 283 ; 284 } else if (strcmp(cmdp, "ping") == 0) { 285 shell_ping(msg, cmdbuf); 286 } else if (strcmp(cmdp, "span") == 0) { 287 shell_span(msg, cmdbuf); 288 } else if (strcmp(cmdp, "tree") == 0) { 289 dmsg_shell_tree(iocom, cmdbuf); /* dump spanning tree */ 290 } else if (strcmp(cmdp, "help") == 0 || strcmp(cmdp, "?") == 0) { 291 dmsg_printf(iocom, "help Command help\n"); 292 dmsg_printf(iocom, "span <host> Span to target host\n"); 293 dmsg_printf(iocom, "tree Dump spanning tree\n"); 294 dmsg_printf(iocom, "@span <cmd> Issue via circuit\n"); 295 } else { 296 dmsg_printf(iocom, "Unrecognized command: %s\n", cmdp); 297 } 298 dmsg_printf(iocom, "debug> "); 299 } 300 301 static void 302 shell_ping(dmsg_msg_t *msg, char *cmdbuf __unused) 303 { 304 dmsg_iocom_t *iocom = msg->state->iocom; 305 dmsg_msg_t *m2; 306 307 dmsg_printf(iocom, "sending ping\n"); 308 m2 = dmsg_msg_alloc(msg->state, 0, DMSG_LNK_PING, NULL, NULL); 309 dmsg_msg_write(m2); 310 } 311 312 static void 313 shell_span(dmsg_msg_t *msg, char *cmdbuf) 314 { 315 dmsg_iocom_t *iocom = msg->state->iocom; 316 dmsg_master_service_info_t *info; 317 const char *hostname = strsep(&cmdbuf, " \t"); 318 pthread_t thread; 319 int fd; 320 321 /* 322 * Connect to the target 323 */ 324 if (hostname == NULL) { 325 fd = -1; 326 } else { 327 fd = dmsg_connect(hostname); 328 } 329 330 /* 331 * Start master service 332 */ 333 if (fd < 0) { 334 dmsg_printf(iocom, "Connection to %s failed\n", hostname); 335 } else { 336 dmsg_printf(iocom, "Connected to %s\n", hostname); 337 338 info = malloc(sizeof(*info)); 339 bzero(info, sizeof(*info)); 340 info->fd = fd; 341 info->detachme = 1; 342 info->usrmsg_callback = hammer2_shell_parse; 343 info->label = strdup("client"); 344 345 pthread_create(&thread, NULL, dmsg_master_service, info); 346 /*pthread_join(thread, &res);*/ 347 } 348 } 349 350 /************************************************************************ 351 * DEBUGSPAN * 352 ************************************************************************ 353 * 354 * Connect to the target manually (not via the cluster list embedded in 355 * a hammer2 filesystem) and initiate the SPAN protocol. 356 */ 357 int 358 cmd_debugspan(const char *hostname) 359 { 360 pthread_t thread; 361 int fd; 362 void *res; 363 364 /* 365 * Connect to the target 366 */ 367 fd = dmsg_connect(hostname); 368 if (fd < 0) 369 return 1; 370 371 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname); 372 pthread_create(&thread, NULL, 373 dmsg_master_service, (void *)(intptr_t)fd); 374 pthread_join(thread, &res); 375 return(0); 376 } 377 378 /************************************************************************ 379 * SHOW * 380 ************************************************************************/ 381 382 static void show_volhdr(hammer2_volume_data_t *voldata, int fd, int bi); 383 static void show_bref(hammer2_volume_data_t *voldata, int fd, int tab, 384 int bi, hammer2_blockref_t *bref, int norecurse); 385 static void tabprintf(int tab, const char *ctl, ...); 386 387 static hammer2_off_t TotalAccum16[4]; /* includes TotalAccum64 */ 388 static hammer2_off_t TotalAccum64[4]; 389 static hammer2_off_t TotalUnavail; 390 static hammer2_off_t TotalFreemap; 391 392 int 393 cmd_show(const char *devpath, int which) 394 { 395 hammer2_blockref_t broot; 396 hammer2_blockref_t best; 397 hammer2_media_data_t media; 398 hammer2_media_data_t best_media; 399 int fd; 400 int i; 401 int best_i; 402 char *env; 403 404 memset(TotalAccum16, 0, sizeof(TotalAccum16)); 405 memset(TotalAccum64, 0, sizeof(TotalAccum64)); 406 TotalUnavail = TotalFreemap = 0; 407 408 env = getenv("HAMMER2_SHOW_TAB"); 409 if (env != NULL) { 410 show_tab = (int)strtol(env, NULL, 0); 411 if (errno || show_tab < 0 || show_tab > 8) 412 show_tab = 2; 413 } 414 415 fd = open(devpath, O_RDONLY); 416 if (fd < 0) { 417 perror("open"); 418 return 1; 419 } 420 421 /* 422 * Show the tree using the best volume header. 423 * -vvv will show the tree for all four volume headers. 424 */ 425 best_i = -1; 426 bzero(&best, sizeof(best)); 427 bzero(&best_media, sizeof(best_media)); 428 for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) { 429 bzero(&broot, sizeof(broot)); 430 broot.data_off = (i * HAMMER2_ZONE_BYTES64) | HAMMER2_PBUFRADIX; 431 lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, SEEK_SET); 432 if (read(fd, &media, HAMMER2_PBUFSIZE) == 433 (ssize_t)HAMMER2_PBUFSIZE) { 434 broot.mirror_tid = media.voldata.mirror_tid; 435 if (best_i < 0 || best.mirror_tid < broot.mirror_tid) { 436 best_i = i; 437 best = broot; 438 best_media = media; 439 } 440 printf("Volume header %d: mirror_tid=%016jx\n", 441 i, (intmax_t)broot.mirror_tid); 442 443 if (VerboseOpt >= 3) { 444 switch(which) { 445 case 0: 446 broot.type = HAMMER2_BREF_TYPE_VOLUME; 447 show_bref(&media.voldata, fd, 0, 448 i, &broot, 0); 449 break; 450 case 1: 451 broot.type = HAMMER2_BREF_TYPE_FREEMAP; 452 show_bref(&media.voldata, fd, 0, 453 i, &broot, 0); 454 break; 455 default: 456 show_volhdr(&media.voldata, fd, i); 457 break; 458 } 459 printf("\n"); 460 } 461 } 462 } 463 if (VerboseOpt < 3) { 464 switch(which) { 465 case 0: 466 best.type = HAMMER2_BREF_TYPE_VOLUME; 467 show_bref(&best_media.voldata, fd, 0, best_i, &best, 0); 468 break; 469 case 1: 470 best.type = HAMMER2_BREF_TYPE_FREEMAP; 471 show_bref(&best_media.voldata, fd, 0, best_i, &best, 0); 472 break; 473 default: 474 show_volhdr(&best_media.voldata, fd, best_i); 475 break; 476 } 477 } 478 close(fd); 479 480 if (which == 1 && VerboseOpt < 3) { 481 printf("Total unallocated storage: %6.3fGiB (%6.3fGiB in 64KB chunks)\n", 482 (double)TotalAccum16[0] / GIG, 483 (double)TotalAccum64[0] / GIG); 484 printf("Total possibly free storage: %6.3fGiB (%6.3fGiB in 64KB chunks)\n", 485 (double)TotalAccum16[2] / GIG, 486 (double)TotalAccum64[2] / GIG); 487 printf("Total allocated storage: %6.3fGiB (%6.3fGiB in 64KB chunks)\n", 488 (double)TotalAccum16[3] / GIG, 489 (double)TotalAccum64[3] / GIG); 490 printf("Total unavailable storage: %6.3fGiB\n", 491 (double)TotalUnavail / GIG); 492 printf("Total freemap storage: %6.3fGiB\n", 493 (double)TotalFreemap / GIG); 494 } 495 496 return 0; 497 } 498 499 static void 500 show_volhdr(hammer2_volume_data_t *voldata, int fd, int bi) 501 { 502 uint32_t status; 503 uint32_t i; 504 char *str; 505 char *name; 506 507 printf("\nVolume header %d {\n", bi); 508 printf(" magic 0x%016jx\n", (intmax_t)voldata->magic); 509 printf(" boot_beg 0x%016jx\n", (intmax_t)voldata->boot_beg); 510 printf(" boot_end 0x%016jx (%6.2fMB)\n", 511 (intmax_t)voldata->boot_end, 512 (double)(voldata->boot_end - voldata->boot_beg) / 513 (1024.0*1024.0)); 514 printf(" aux_beg 0x%016jx\n", (intmax_t)voldata->aux_beg); 515 printf(" aux_end 0x%016jx (%6.2fMB)\n", 516 (intmax_t)voldata->aux_end, 517 (double)(voldata->aux_end - voldata->aux_beg) / 518 (1024.0*1024.0)); 519 printf(" volu_size 0x%016jx (%6.2fGiB)\n", 520 (intmax_t)voldata->volu_size, 521 (double)voldata->volu_size / GIG); 522 printf(" version %d\n", voldata->version); 523 printf(" flags 0x%08x\n", voldata->flags); 524 printf(" copyid %d\n", voldata->copyid); 525 printf(" freemap_vers %d\n", voldata->freemap_version); 526 printf(" peer_type %d\n", voldata->peer_type); 527 528 str = NULL; 529 hammer2_uuid_to_str(&voldata->fsid, &str); 530 printf(" fsid %s\n", str); 531 free(str); 532 533 str = NULL; 534 name = NULL; 535 hammer2_uuid_to_str(&voldata->fstype, &str); 536 printf(" fstype %s\n", str); 537 uuid_addr_lookup(&voldata->fstype, &name, &status); 538 if (name == NULL) 539 name = strdup("?"); 540 printf(" (%s)\n", name); 541 free(name); 542 free(str); 543 544 printf(" allocator_size 0x%016jx (%6.2fGiB)\n", 545 voldata->allocator_size, 546 (double)voldata->allocator_size / GIG); 547 printf(" allocator_free 0x%016jx (%6.2fGiB)\n", 548 voldata->allocator_free, 549 (double)voldata->allocator_free / GIG); 550 printf(" allocator_beg 0x%016jx (%6.2fGiB)\n", 551 voldata->allocator_beg, 552 (double)voldata->allocator_beg / GIG); 553 554 printf(" mirror_tid 0x%016jx\n", voldata->mirror_tid); 555 printf(" reserved0080 0x%016jx\n", voldata->reserved0080); 556 printf(" reserved0088 0x%016jx\n", voldata->reserved0088); 557 printf(" freemap_tid 0x%016jx\n", voldata->freemap_tid); 558 for (i = 0; i < arysize(voldata->reserved00A0); ++i) { 559 printf(" reserved00A0/%u 0x%016jx\n", 560 i, voldata->reserved00A0[0]); 561 } 562 563 printf(" copyexists "); 564 for (i = 0; i < arysize(voldata->copyexists); ++i) 565 printf(" 0x%02x", voldata->copyexists[i]); 566 printf("\n"); 567 568 /* 569 * NOTE: Index numbers and ICRC_SECTn definitions are not matched, 570 * the ICRC for sector 0 actually uses the last index, for 571 * example. 572 * 573 * NOTE: The whole voldata CRC does not have to match critically 574 * as certain sub-areas of the volume header have their own 575 * CRCs. 576 */ 577 printf("\n"); 578 for (i = 0; i < arysize(voldata->icrc_sects); ++i) { 579 printf(" icrc_sects[%u] ", i); 580 switch(i) { 581 case HAMMER2_VOL_ICRC_SECT0: 582 printf("0x%08x/0x%08x", 583 hammer2_icrc32((char *)voldata + 584 HAMMER2_VOLUME_ICRC0_OFF, 585 HAMMER2_VOLUME_ICRC0_SIZE), 586 voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT0]); 587 if (hammer2_icrc32((char *)voldata + 588 HAMMER2_VOLUME_ICRC0_OFF, 589 HAMMER2_VOLUME_ICRC0_SIZE) == 590 voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT0]) { 591 printf(" (OK)"); 592 } else { 593 printf(" (FAILED)"); 594 } 595 break; 596 case HAMMER2_VOL_ICRC_SECT1: 597 printf("0x%08x/0x%08x", 598 hammer2_icrc32((char *)voldata + 599 HAMMER2_VOLUME_ICRC1_OFF, 600 HAMMER2_VOLUME_ICRC1_SIZE), 601 voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT1]); 602 if (hammer2_icrc32((char *)voldata + 603 HAMMER2_VOLUME_ICRC1_OFF, 604 HAMMER2_VOLUME_ICRC1_SIZE) == 605 voldata->icrc_sects[HAMMER2_VOL_ICRC_SECT1]) { 606 printf(" (OK)"); 607 } else { 608 printf(" (FAILED)"); 609 } 610 611 break; 612 default: 613 printf("0x%08x (reserved)", voldata->icrc_sects[i]); 614 break; 615 } 616 printf("\n"); 617 } 618 printf(" icrc_volhdr 0x%08x/0x%08x", 619 hammer2_icrc32((char *)voldata + HAMMER2_VOLUME_ICRCVH_OFF, 620 HAMMER2_VOLUME_ICRCVH_SIZE), 621 voldata->icrc_volheader); 622 if (hammer2_icrc32((char *)voldata + HAMMER2_VOLUME_ICRCVH_OFF, 623 HAMMER2_VOLUME_ICRCVH_SIZE) == 624 voldata->icrc_volheader) { 625 printf(" (OK)\n"); 626 } else { 627 printf(" (FAILED - not a critical error)\n"); 628 } 629 630 /* 631 * The super-root and freemap blocksets (not recursed) 632 */ 633 printf("\n"); 634 printf(" sroot_blockset {\n"); 635 for (i = 0; i < HAMMER2_SET_COUNT; ++i) { 636 show_bref(voldata, fd, 16, i, 637 &voldata->sroot_blockset.blockref[i], 2); 638 printf("\n"); 639 } 640 printf(" }\n"); 641 642 printf(" freemap_blockset {\n"); 643 for (i = 0; i < HAMMER2_SET_COUNT; ++i) { 644 show_bref(voldata, fd, 16, i, 645 &voldata->freemap_blockset.blockref[i], 2); 646 printf("\n"); 647 } 648 printf(" }\n"); 649 650 printf("}\n"); 651 } 652 653 static void 654 show_bref(hammer2_volume_data_t *voldata, int fd, int tab, 655 int bi, hammer2_blockref_t *bref, int norecurse) 656 { 657 hammer2_media_data_t media; 658 hammer2_blockref_t *bscan; 659 hammer2_off_t tmp; 660 int i, bcount, namelen, failed, obrace; 661 int type_pad; 662 size_t bytes; 663 const char *type_str; 664 char *str = NULL; 665 uint32_t cv; 666 uint64_t cv64; 667 668 SHA256_CTX hash_ctx; 669 union { 670 uint8_t digest[SHA256_DIGEST_LENGTH]; 671 uint64_t digest64[SHA256_DIGEST_LENGTH/8]; 672 } u; 673 674 bytes = (bref->data_off & HAMMER2_OFF_MASK_RADIX); 675 if (bytes) 676 bytes = (size_t)1 << bytes; 677 if (bytes) { 678 hammer2_off_t io_off; 679 hammer2_off_t io_base; 680 size_t io_bytes; 681 size_t boff; 682 683 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX; 684 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1); 685 boff = io_off - io_base; 686 687 io_bytes = HAMMER2_MINIOSIZE; 688 while (io_bytes + boff < bytes) 689 io_bytes <<= 1; 690 691 if (io_bytes > sizeof(media)) { 692 printf("(bad block size %zd)\n", bytes); 693 return; 694 } 695 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) { 696 lseek(fd, io_base, SEEK_SET); 697 if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) { 698 printf("(media read failed)\n"); 699 return; 700 } 701 if (boff) 702 bcopy((char *)&media + boff, &media, bytes); 703 } 704 } 705 706 bscan = NULL; 707 bcount = 0; 708 namelen = 0; 709 failed = 0; 710 obrace = 1; 711 712 type_str = hammer2_breftype_to_str(bref->type); 713 type_pad = 8 - strlen(type_str); 714 if (type_pad < 0) 715 type_pad = 0; 716 717 switch(bref->type) { 718 case HAMMER2_BREF_TYPE_INODE: 719 assert(bytes); 720 if (!(media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA)) { 721 bscan = &media.ipdata.u.blockset.blockref[0]; 722 bcount = HAMMER2_SET_COUNT; 723 } 724 break; 725 case HAMMER2_BREF_TYPE_INDIRECT: 726 assert(bytes); 727 bscan = &media.npdata[0]; 728 bcount = bytes / sizeof(hammer2_blockref_t); 729 break; 730 case HAMMER2_BREF_TYPE_VOLUME: 731 bscan = &media.voldata.sroot_blockset.blockref[0]; 732 bcount = HAMMER2_SET_COUNT; 733 break; 734 case HAMMER2_BREF_TYPE_FREEMAP: 735 bscan = &media.voldata.freemap_blockset.blockref[0]; 736 bcount = HAMMER2_SET_COUNT; 737 break; 738 case HAMMER2_BREF_TYPE_FREEMAP_NODE: 739 assert(bytes); 740 bscan = &media.npdata[0]; 741 bcount = bytes / sizeof(hammer2_blockref_t); 742 break; 743 } 744 745 if (QuietOpt > 0) { 746 tabprintf(tab, 747 "%s.%-3d %016jx %016jx/%-2d " 748 "mir=%016jx mod=%016jx leafcnt=%d ", 749 type_str, bi, (intmax_t)bref->data_off, 750 (intmax_t)bref->key, (intmax_t)bref->keybits, 751 (intmax_t)bref->mirror_tid, 752 (intmax_t)bref->modify_tid, 753 bref->leaf_count); 754 tab += show_tab; 755 } else { 756 tabprintf(tab, "%s.%-3d%*.*s 0x%016jx 0x%016jx/%-2d ", 757 type_str, bi, type_pad, type_pad, "", 758 (intmax_t)bref->data_off, 759 (intmax_t)bref->key, (intmax_t)bref->keybits); 760 /*if (norecurse > 1)*/ { 761 printf("\n"); 762 tabprintf(tab + 13, ""); 763 } 764 printf("mir=%016jx mod=%016jx lfcnt=%d ", 765 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid, 766 bref->leaf_count); 767 if (/*norecurse > 1 && */ (bcount || bref->flags || 768 bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE || 769 bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF)) { 770 printf("\n"); 771 tabprintf(tab + 13, ""); 772 } 773 } 774 775 if (bcount) 776 printf("bcnt=%d ", bcount); 777 if (bref->flags) 778 printf("flags=%02x ", bref->flags); 779 if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE || 780 bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) { 781 printf("bigmask=%08x avail=%ld ", 782 bref->check.freemap.bigmask, bref->check.freemap.avail); 783 } 784 785 /* 786 * Check data integrity in verbose mode, otherwise we are just doing 787 * a quick meta-data scan. Meta-data integrity is always checked. 788 * (Also see the check above that ensures the media data is loaded, 789 * otherwise there's no data to check!). 790 * 791 * WARNING! bref->check state may be used for other things when 792 * bref has no data (bytes == 0). 793 */ 794 if (bytes && 795 (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1)) { 796 if (!(QuietOpt > 0)) { 797 /*if (norecurse > 1)*/ { 798 printf("\n"); 799 tabprintf(tab + 13, ""); 800 } 801 } 802 803 switch(HAMMER2_DEC_CHECK(bref->methods)) { 804 case HAMMER2_CHECK_NONE: 805 printf("meth=%02x ", bref->methods); 806 break; 807 case HAMMER2_CHECK_DISABLED: 808 printf("meth=%02x ", bref->methods); 809 break; 810 case HAMMER2_CHECK_ISCSI32: 811 cv = hammer2_icrc32(&media, bytes); 812 if (bref->check.iscsi32.value != cv) { 813 printf("(icrc %02x:%08x/%08x failed) ", 814 bref->methods, 815 bref->check.iscsi32.value, 816 cv); 817 failed = 1; 818 } else { 819 printf("meth=%02x iscsi32=%08x ", 820 bref->methods, cv); 821 } 822 break; 823 case HAMMER2_CHECK_XXHASH64: 824 cv64 = XXH64(&media, bytes, XXH_HAMMER2_SEED); 825 if (bref->check.xxhash64.value != cv64) { 826 printf("(xxhash64 %02x:%016jx/%016jx failed) ", 827 bref->methods, 828 bref->check.xxhash64.value, 829 cv64); 830 failed = 1; 831 } else { 832 printf("meth=%02x xxh=%016jx ", 833 bref->methods, cv64); 834 } 835 break; 836 case HAMMER2_CHECK_SHA192: 837 SHA256_Init(&hash_ctx); 838 SHA256_Update(&hash_ctx, &media, bytes); 839 SHA256_Final(u.digest, &hash_ctx); 840 u.digest64[2] ^= u.digest64[3]; 841 if (memcmp(u.digest, bref->check.sha192.data, 842 sizeof(bref->check.sha192.data))) { 843 printf("(sha192 failed) "); 844 failed = 1; 845 } else { 846 printf("meth=%02x ", bref->methods); 847 } 848 break; 849 case HAMMER2_CHECK_FREEMAP: 850 cv = hammer2_icrc32(&media, bytes); 851 if (bref->check.freemap.icrc32 != cv) { 852 printf("(fcrc %02x:%08x/%08x failed) ", 853 bref->methods, 854 bref->check.freemap.icrc32, 855 cv); 856 failed = 1; 857 } else { 858 printf("meth=%02x fcrc=%08x ", 859 bref->methods, cv); 860 } 861 break; 862 } 863 } 864 865 tab += show_tab; 866 867 if (QuietOpt > 0) { 868 obrace = 0; 869 printf("\n"); 870 goto skip_data; 871 } 872 873 switch(bref->type) { 874 case HAMMER2_BREF_TYPE_EMPTY: 875 obrace = 0; 876 break; 877 case HAMMER2_BREF_TYPE_DIRENT: 878 printf("{\n"); 879 if (bref->embed.dirent.namlen <= sizeof(bref->check.buf)) { 880 tabprintf(tab, "filename \"%*.*s\"\n", 881 bref->embed.dirent.namlen, 882 bref->embed.dirent.namlen, 883 bref->check.buf); 884 } else { 885 tabprintf(tab, "filename \"%*.*s\"\n", 886 bref->embed.dirent.namlen, 887 bref->embed.dirent.namlen, 888 media.buf); 889 } 890 tabprintf(tab, "inum 0x%016jx\n", 891 (uintmax_t)bref->embed.dirent.inum); 892 tabprintf(tab, "nlen %d\n", bref->embed.dirent.namlen); 893 tabprintf(tab, "type %s\n", 894 hammer2_iptype_to_str(bref->embed.dirent.type)); 895 break; 896 case HAMMER2_BREF_TYPE_INODE: 897 printf("{\n"); 898 namelen = media.ipdata.meta.name_len; 899 if (namelen > HAMMER2_INODE_MAXNAME) 900 namelen = 0; 901 tabprintf(tab, "filename \"%*.*s\"\n", 902 namelen, namelen, media.ipdata.filename); 903 tabprintf(tab, "version %d\n", media.ipdata.meta.version); 904 tabprintf(tab, "pfs_st %d\n", media.ipdata.meta.pfs_subtype); 905 tabprintf(tab, "uflags 0x%08x\n", 906 media.ipdata.meta.uflags); 907 if (media.ipdata.meta.rmajor || media.ipdata.meta.rminor) { 908 tabprintf(tab, "rmajor %d\n", 909 media.ipdata.meta.rmajor); 910 tabprintf(tab, "rminor %d\n", 911 media.ipdata.meta.rminor); 912 } 913 tabprintf(tab, "ctime %s\n", 914 hammer2_time64_to_str(media.ipdata.meta.ctime, &str)); 915 tabprintf(tab, "mtime %s\n", 916 hammer2_time64_to_str(media.ipdata.meta.mtime, &str)); 917 tabprintf(tab, "atime %s\n", 918 hammer2_time64_to_str(media.ipdata.meta.atime, &str)); 919 tabprintf(tab, "btime %s\n", 920 hammer2_time64_to_str(media.ipdata.meta.btime, &str)); 921 tabprintf(tab, "uid %s\n", 922 hammer2_uuid_to_str(&media.ipdata.meta.uid, &str)); 923 tabprintf(tab, "gid %s\n", 924 hammer2_uuid_to_str(&media.ipdata.meta.gid, &str)); 925 tabprintf(tab, "type %s\n", 926 hammer2_iptype_to_str(media.ipdata.meta.type)); 927 tabprintf(tab, "opflgs 0x%02x\n", 928 media.ipdata.meta.op_flags); 929 tabprintf(tab, "capflgs 0x%04x\n", 930 media.ipdata.meta.cap_flags); 931 tabprintf(tab, "mode %-7o\n", 932 media.ipdata.meta.mode); 933 tabprintf(tab, "inum 0x%016jx\n", 934 media.ipdata.meta.inum); 935 tabprintf(tab, "size %ju ", 936 (uintmax_t)media.ipdata.meta.size); 937 if (media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA && 938 media.ipdata.meta.size <= HAMMER2_EMBEDDED_BYTES) 939 printf("(embedded data)\n"); 940 else 941 printf("\n"); 942 tabprintf(tab, "nlinks %ju\n", 943 (uintmax_t)media.ipdata.meta.nlinks); 944 tabprintf(tab, "iparent 0x%016jx\n", 945 (uintmax_t)media.ipdata.meta.iparent); 946 tabprintf(tab, "name_key 0x%016jx\n", 947 (uintmax_t)media.ipdata.meta.name_key); 948 tabprintf(tab, "name_len %u\n", 949 media.ipdata.meta.name_len); 950 tabprintf(tab, "ncopies %u\n", 951 media.ipdata.meta.ncopies); 952 tabprintf(tab, "compalg %u\n", 953 media.ipdata.meta.comp_algo); 954 tabprintf(tab, "target_t %u\n", 955 media.ipdata.meta.target_type); 956 tabprintf(tab, "checkalg %u\n", 957 media.ipdata.meta.check_algo); 958 if ((media.ipdata.meta.op_flags & HAMMER2_OPFLAG_PFSROOT) || 959 media.ipdata.meta.pfs_type == HAMMER2_PFSTYPE_SUPROOT) { 960 tabprintf(tab, "pfs_nmas %u\n", 961 media.ipdata.meta.pfs_nmasters); 962 tabprintf(tab, "pfs_type %u (%s)\n", 963 media.ipdata.meta.pfs_type, 964 hammer2_pfstype_to_str(media.ipdata.meta.pfs_type)); 965 tabprintf(tab, "pfs_inum 0x%016jx\n", 966 (uintmax_t)media.ipdata.meta.pfs_inum); 967 tabprintf(tab, "pfs_clid %s\n", 968 hammer2_uuid_to_str(&media.ipdata.meta.pfs_clid, 969 &str)); 970 tabprintf(tab, "pfs_fsid %s\n", 971 hammer2_uuid_to_str(&media.ipdata.meta.pfs_fsid, 972 &str)); 973 tabprintf(tab, "pfs_lsnap_tid 0x%016jx\n", 974 (uintmax_t)media.ipdata.meta.pfs_lsnap_tid); 975 } 976 tabprintf(tab, "data_quota %ju\n", 977 (uintmax_t)media.ipdata.meta.data_quota); 978 tabprintf(tab, "data_count %ju\n", 979 (uintmax_t)bref->embed.stats.data_count); 980 tabprintf(tab, "inode_quota %ju\n", 981 (uintmax_t)media.ipdata.meta.inode_quota); 982 tabprintf(tab, "inode_count %ju\n", 983 (uintmax_t)bref->embed.stats.inode_count); 984 break; 985 case HAMMER2_BREF_TYPE_INDIRECT: 986 printf("{\n"); 987 break; 988 case HAMMER2_BREF_TYPE_DATA: 989 printf("\n"); 990 obrace = 0; 991 break; 992 case HAMMER2_BREF_TYPE_VOLUME: 993 printf("mirror_tid=%016jx freemap_tid=%016jx ", 994 media.voldata.mirror_tid, 995 media.voldata.freemap_tid); 996 printf("{\n"); 997 break; 998 case HAMMER2_BREF_TYPE_FREEMAP: 999 printf("mirror_tid=%016jx freemap_tid=%016jx ", 1000 media.voldata.mirror_tid, 1001 media.voldata.freemap_tid); 1002 printf("{\n"); 1003 break; 1004 case HAMMER2_BREF_TYPE_FREEMAP_LEAF: 1005 printf("{\n"); 1006 tmp = bref->data_off & ~HAMMER2_OFF_MASK_RADIX; 1007 tmp &= HAMMER2_SEGMASK; 1008 tmp /= HAMMER2_PBUFSIZE; 1009 assert(tmp >= HAMMER2_ZONE_FREEMAP_00); 1010 assert(tmp < HAMMER2_ZONE_FREEMAP_END); 1011 tmp -= HAMMER2_ZONE_FREEMAP_00; 1012 tmp /= HAMMER2_ZONE_FREEMAP_INC; 1013 tabprintf(tab, "rotation=%d\n", (int)tmp); 1014 1015 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) { 1016 hammer2_off_t data_off = bref->key + 1017 i * HAMMER2_FREEMAP_LEVEL0_SIZE; 1018 #if HAMMER2_BMAP_ELEMENTS != 8 1019 #error "cmd_debug.c: HAMMER2_BMAP_ELEMENTS expected to be 8" 1020 #endif 1021 tabprintf(tab + 4, "%016jx %04d.%04x (avail=%7d) " 1022 "%016jx %016jx %016jx %016jx " 1023 "%016jx %016jx %016jx %016jx\n", 1024 data_off, i, media.bmdata[i].class, 1025 media.bmdata[i].avail, 1026 media.bmdata[i].bitmapq[0], 1027 media.bmdata[i].bitmapq[1], 1028 media.bmdata[i].bitmapq[2], 1029 media.bmdata[i].bitmapq[3], 1030 media.bmdata[i].bitmapq[4], 1031 media.bmdata[i].bitmapq[5], 1032 media.bmdata[i].bitmapq[6], 1033 media.bmdata[i].bitmapq[7]); 1034 } 1035 tabprintf(tab, "}\n"); 1036 break; 1037 case HAMMER2_BREF_TYPE_FREEMAP_NODE: 1038 printf("{\n"); 1039 tmp = bref->data_off & ~HAMMER2_OFF_MASK_RADIX; 1040 tmp &= HAMMER2_SEGMASK; 1041 tmp /= HAMMER2_PBUFSIZE; 1042 assert(tmp >= HAMMER2_ZONE_FREEMAP_00); 1043 assert(tmp < HAMMER2_ZONE_FREEMAP_END); 1044 tmp -= HAMMER2_ZONE_FREEMAP_00; 1045 tmp /= HAMMER2_ZONE_FREEMAP_INC; 1046 tabprintf(tab, "rotation=%d\n", (int)tmp); 1047 break; 1048 default: 1049 printf("\n"); 1050 obrace = 0; 1051 break; 1052 } 1053 if (str) 1054 free(str); 1055 1056 skip_data: 1057 /* 1058 * Update statistics. 1059 */ 1060 switch(bref->type) { 1061 case HAMMER2_BREF_TYPE_FREEMAP_LEAF: 1062 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) { 1063 hammer2_off_t data_off = bref->key + 1064 i * HAMMER2_FREEMAP_LEVEL0_SIZE; 1065 if (data_off >= voldata->aux_end && 1066 data_off < voldata->volu_size) { 1067 int j; 1068 for (j = 0; j < 4; ++j) 1069 CountBlocks(&media.bmdata[i], j, 1070 &TotalAccum16[j], 1071 &TotalAccum64[j]); 1072 } else 1073 TotalUnavail += HAMMER2_FREEMAP_LEVEL0_SIZE; 1074 } 1075 TotalFreemap += HAMMER2_FREEMAP_LEVEL1_SIZE; 1076 break; 1077 default: 1078 break; 1079 } 1080 1081 /* 1082 * Recurse if norecurse == 0. If the CRC failed, pass norecurse = 1. 1083 * That is, if an indirect or inode fails we still try to list its 1084 * direct children to help with debugging, but go no further than 1085 * that because they are probably garbage. 1086 */ 1087 for (i = 0; norecurse == 0 && i < bcount; ++i) { 1088 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) { 1089 show_bref(voldata, fd, tab, i, &bscan[i], failed); 1090 } 1091 } 1092 tab -= show_tab; 1093 if (obrace) { 1094 if (bref->type == HAMMER2_BREF_TYPE_INODE) 1095 tabprintf(tab, "} (%s.%d, \"%*.*s\")\n", 1096 type_str, bi, namelen, namelen, 1097 media.ipdata.filename); 1098 else 1099 tabprintf(tab, "} (%s.%d)\n", type_str, bi); 1100 } 1101 } 1102 1103 static 1104 void 1105 CountBlocks(hammer2_bmap_data_t *bmap, int value, 1106 hammer2_off_t *accum16, hammer2_off_t *accum64) 1107 { 1108 int i, j, bits; 1109 hammer2_bitmap_t value16, value64; 1110 1111 bits = (int)sizeof(hammer2_bitmap_t) * 8; 1112 assert(bits == 64); 1113 1114 value16 = value; 1115 assert(value16 < 4); 1116 value64 = (value16 << 6) | (value16 << 4) | (value16 << 2) | value16; 1117 assert(value64 < 256); 1118 1119 for (i = 0; i < HAMMER2_BMAP_ELEMENTS; ++i) { 1120 hammer2_bitmap_t bm = bmap->bitmapq[i]; 1121 hammer2_bitmap_t bm_save = bm; 1122 hammer2_bitmap_t mask; 1123 1124 mask = 0x03; /* 2 bits per 16KB */ 1125 for (j = 0; j < bits; j += 2) { 1126 if ((bm & mask) == value16) 1127 *accum16 += 16384; 1128 bm >>= 2; 1129 } 1130 1131 bm = bm_save; 1132 mask = 0xFF; /* 8 bits per 64KB chunk */ 1133 for (j = 0; j < bits; j += 8) { 1134 if ((bm & mask) == value64) 1135 *accum64 += 65536; 1136 bm >>= 8; 1137 } 1138 } 1139 } 1140 1141 int 1142 cmd_hash(int ac, const char **av) 1143 { 1144 int i; 1145 1146 for (i = 0; i < ac; ++i) { 1147 printf("%016jx %s\n", 1148 dirhash((const unsigned char*)av[i], strlen(av[i])), 1149 av[i]); 1150 } 1151 return(0); 1152 } 1153 1154 int 1155 cmd_dhash(int ac, const char **av) 1156 { 1157 char buf[1024]; /* 1K extended directory record */ 1158 uint64_t hash; 1159 int i; 1160 1161 for (i = 0; i < ac; ++i) { 1162 bzero(buf, sizeof(buf)); 1163 snprintf(buf, sizeof(buf), "%s", av[i]); 1164 hash = XXH64(buf, sizeof(buf), XXH_HAMMER2_SEED); 1165 printf("%016jx %s\n", hash, av[i]); 1166 } 1167 return(0); 1168 } 1169 1170 int 1171 cmd_dumpchain(const char *path, u_int flags) 1172 { 1173 int dummy = (int)flags; 1174 int fd; 1175 1176 fd = open(path, O_RDONLY); 1177 if (fd >= 0) { 1178 ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy); 1179 close(fd); 1180 } else { 1181 fprintf(stderr, "unable to open %s\n", path); 1182 } 1183 return 0; 1184 } 1185 1186 1187 static 1188 void 1189 tabprintf(int tab, const char *ctl, ...) 1190 { 1191 va_list va; 1192 1193 printf("%*.*s", tab, tab, ""); 1194 va_start(va, ctl); 1195 vprintf(ctl, va); 1196 va_end(va); 1197 } 1198