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 static int show_tab = 2; 39 40 static void shell_msghandler(dmsg_msg_t *msg, int unmanaged); 41 static void shell_ttymsg(dmsg_iocom_t *iocom); 42 static void CountFreeBlocks(hammer2_bmap_data_t *bmap, 43 hammer2_off_t *accum16, hammer2_off_t *accum64); 44 45 /************************************************************************ 46 * SHELL * 47 ************************************************************************/ 48 49 int 50 cmd_shell(const char *hostname) 51 { 52 dmsg_master_service_info_t *info; 53 pthread_t thread; 54 int fd; 55 56 fd = dmsg_connect(hostname); 57 if (fd < 0) 58 return 1; 59 60 info = malloc(sizeof(*info)); 61 bzero(info, sizeof(*info)); 62 info->fd = fd; 63 info->detachme = 0; 64 info->usrmsg_callback = shell_msghandler; 65 info->altmsg_callback = shell_ttymsg; 66 info->label = strdup("debug"); 67 pthread_create(&thread, NULL, dmsg_master_service, info); 68 pthread_join(thread, NULL); 69 70 return 0; 71 } 72 73 #if 0 74 int 75 cmd_shell(const char *hostname) 76 { 77 struct dmsg_iocom iocom; 78 dmsg_msg_t *msg; 79 int fd; 80 81 /* 82 * Connect to the target 83 */ 84 fd = dmsg_connect(hostname); 85 if (fd < 0) 86 return 1; 87 88 /* 89 * Initialize the session and transmit an empty DMSG_DBG_SHELL 90 * to cause the remote end to generate a prompt. 91 */ 92 dmsg_iocom_init(&iocom, fd, 0, 93 NULL, 94 shell_rcvmsg, 95 hammer2_shell_parse, 96 shell_ttymsg); 97 fcntl(0, F_SETFL, O_NONBLOCK); 98 printf("debug: connected\n"); 99 100 msg = dmsg_msg_alloc(&iocom.state0, 0, DMSG_DBG_SHELL, NULL, NULL); 101 dmsg_msg_write(msg); 102 dmsg_iocom_core(&iocom); 103 fprintf(stderr, "debug: disconnected\n"); 104 close(fd); 105 return 0; 106 } 107 #endif 108 109 /* 110 * Debug session front-end 111 * 112 * Callback from dmsg_iocom_core() when messages might be present 113 * on the socket. 114 */ 115 static 116 void 117 shell_msghandler(dmsg_msg_t *msg, int unmanaged) 118 { 119 dmsg_msg_t *nmsg; 120 121 switch(msg->tcmd) { 122 #if 0 123 case DMSG_LNK_ERROR: 124 case DMSG_LNK_ERROR | DMSGF_REPLY: 125 /* 126 * One-way non-transactional LNK_ERROR messages typically 127 * indicate a connection failure. Error code 0 is used by 128 * the debug shell to indicate no more results from last cmd. 129 */ 130 if (msg->any.head.error) { 131 fprintf(stderr, "Stream failure: %s\n", 132 dmsg_msg_str(msg)); 133 } else { 134 write(1, "debug> ", 7); 135 } 136 break; 137 case DMSG_LNK_ERROR | DMSGF_DELETE: 138 /* ignore termination of LNK_CONN */ 139 break; 140 #endif 141 case DMSG_DBG_SHELL: 142 /* 143 * We send the commands, not accept them. 144 * (one-way message, not transactional) 145 */ 146 if (unmanaged) 147 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 148 break; 149 case DMSG_DBG_SHELL | DMSGF_REPLY: 150 /* 151 * A reply from the remote is data we copy to stdout. 152 * (one-way message, not transactional) 153 */ 154 if (msg->aux_size) { 155 msg->aux_data[msg->aux_size - 1] = 0; 156 write(1, msg->aux_data, strlen(msg->aux_data)); 157 } 158 break; 159 #if 1 160 case DMSG_LNK_CONN | DMSGF_CREATE: 161 fprintf(stderr, "Debug Shell received LNK_CONN\n"); 162 nmsg = dmsg_msg_alloc(&msg->state->iocom->state0, 0, 163 DMSG_DBG_SHELL, 164 NULL, NULL); 165 dmsg_msg_write(nmsg); 166 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 167 break; 168 case DMSG_LNK_CONN | DMSGF_DELETE: 169 break; 170 #endif 171 default: 172 /* 173 * Ignore any unknown messages, Terminate any unknown 174 * transactions with an error. 175 */ 176 fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg)); 177 if (unmanaged) { 178 if (msg->any.head.cmd & DMSGF_CREATE) 179 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 180 if (msg->any.head.cmd & DMSGF_DELETE) 181 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 182 } 183 break; 184 } 185 } 186 187 /* 188 * Debug session front-end 189 */ 190 static 191 void 192 shell_ttymsg(dmsg_iocom_t *iocom) 193 { 194 dmsg_state_t *pstate; 195 dmsg_msg_t *msg; 196 char buf[256]; 197 char *cmd; 198 size_t len; 199 200 if (fgets(buf, sizeof(buf), stdin) != NULL) { 201 if (buf[0] == '@') { 202 pstate = dmsg_findspan(strtok(buf + 1, " \t\n")); 203 cmd = strtok(NULL, "\n"); 204 } else { 205 pstate = &iocom->state0; 206 cmd = strtok(buf, "\n"); 207 } 208 if (cmd && pstate) { 209 len = strlen(cmd) + 1; 210 msg = dmsg_msg_alloc(pstate, len, DMSG_DBG_SHELL, 211 NULL, NULL); 212 bcopy(cmd, msg->aux_data, len); 213 dmsg_msg_write(msg); 214 } else if (cmd) { 215 fprintf(stderr, "@msgid not found\n"); 216 } else { 217 /* 218 * This should cause the remote end to generate 219 * a debug> prompt (and thus shows that there is 220 * connectivity). 221 */ 222 msg = dmsg_msg_alloc(pstate, 0, DMSG_DBG_SHELL, 223 NULL, NULL); 224 dmsg_msg_write(msg); 225 } 226 } else if (feof(stdin)) { 227 /* 228 * Set EOF flag without setting any error code for normal 229 * EOF. 230 */ 231 iocom->flags |= DMSG_IOCOMF_EOF; 232 } else { 233 clearerr(stdin); 234 } 235 } 236 237 /* 238 * Debug session back-end (on remote side) 239 */ 240 static void shell_span(dmsg_msg_t *msg, char *cmdbuf); 241 static void shell_ping(dmsg_msg_t *msg, char *cmdbuf); 242 243 void 244 hammer2_shell_parse(dmsg_msg_t *msg, int unmanaged) 245 { 246 dmsg_iocom_t *iocom = msg->state->iocom; 247 char *cmdbuf; 248 char *cmdp; 249 uint32_t cmd; 250 251 /* 252 * Filter on debug shell commands and ping responses only 253 */ 254 cmd = msg->any.head.cmd; 255 if ((cmd & DMSGF_CMDSWMASK) == (DMSG_LNK_PING | DMSGF_REPLY)) { 256 dmsg_printf(iocom, "ping reply\n"); 257 return; 258 } 259 260 if ((cmd & DMSGF_PROTOS) != DMSG_PROTO_DBG) { 261 if (unmanaged) 262 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 263 return; 264 } 265 if ((cmd & DMSGF_CMDSWMASK) != DMSG_DBG_SHELL) { 266 if (unmanaged) 267 dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); 268 return; 269 } 270 271 /* 272 * Debug shell command 273 */ 274 cmdbuf = msg->aux_data; 275 cmdp = strsep(&cmdbuf, " \t"); 276 277 if (cmdp == NULL || *cmdp == 0) { 278 ; 279 } else if (strcmp(cmdp, "ping") == 0) { 280 shell_ping(msg, cmdbuf); 281 } else if (strcmp(cmdp, "span") == 0) { 282 shell_span(msg, cmdbuf); 283 } else if (strcmp(cmdp, "tree") == 0) { 284 dmsg_shell_tree(iocom, cmdbuf); /* dump spanning tree */ 285 } else if (strcmp(cmdp, "help") == 0 || strcmp(cmdp, "?") == 0) { 286 dmsg_printf(iocom, "help Command help\n"); 287 dmsg_printf(iocom, "span <host> Span to target host\n"); 288 dmsg_printf(iocom, "tree Dump spanning tree\n"); 289 dmsg_printf(iocom, "@span <cmd> Issue via circuit\n"); 290 } else { 291 dmsg_printf(iocom, "Unrecognized command: %s\n", cmdp); 292 } 293 dmsg_printf(iocom, "debug> "); 294 } 295 296 static void 297 shell_ping(dmsg_msg_t *msg, char *cmdbuf __unused) 298 { 299 dmsg_iocom_t *iocom = msg->state->iocom; 300 dmsg_msg_t *m2; 301 302 dmsg_printf(iocom, "sending ping\n"); 303 m2 = dmsg_msg_alloc(msg->state, 0, DMSG_LNK_PING, NULL, NULL); 304 dmsg_msg_write(m2); 305 } 306 307 static void 308 shell_span(dmsg_msg_t *msg, char *cmdbuf) 309 { 310 dmsg_iocom_t *iocom = msg->state->iocom; 311 dmsg_master_service_info_t *info; 312 const char *hostname = strsep(&cmdbuf, " \t"); 313 pthread_t thread; 314 int fd; 315 316 /* 317 * Connect to the target 318 */ 319 if (hostname == NULL) { 320 fd = -1; 321 } else { 322 fd = dmsg_connect(hostname); 323 } 324 325 /* 326 * Start master service 327 */ 328 if (fd < 0) { 329 dmsg_printf(iocom, "Connection to %s failed\n", hostname); 330 } else { 331 dmsg_printf(iocom, "Connected to %s\n", hostname); 332 333 info = malloc(sizeof(*info)); 334 bzero(info, sizeof(*info)); 335 info->fd = fd; 336 info->detachme = 1; 337 info->usrmsg_callback = hammer2_shell_parse; 338 info->label = strdup("client"); 339 340 pthread_create(&thread, NULL, dmsg_master_service, info); 341 /*pthread_join(thread, &res);*/ 342 } 343 } 344 345 /************************************************************************ 346 * DEBUGSPAN * 347 ************************************************************************ 348 * 349 * Connect to the target manually (not via the cluster list embedded in 350 * a hammer2 filesystem) and initiate the SPAN protocol. 351 */ 352 int 353 cmd_debugspan(const char *hostname) 354 { 355 pthread_t thread; 356 int fd; 357 void *res; 358 359 /* 360 * Connect to the target 361 */ 362 fd = dmsg_connect(hostname); 363 if (fd < 0) 364 return 1; 365 366 printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname); 367 pthread_create(&thread, NULL, 368 dmsg_master_service, (void *)(intptr_t)fd); 369 pthread_join(thread, &res); 370 return(0); 371 } 372 373 /************************************************************************ 374 * SHOW * 375 ************************************************************************/ 376 377 static void show_bref(hammer2_volume_data_t *voldata, int fd, int tab, 378 int bi, hammer2_blockref_t *bref, int norecurse); 379 static void tabprintf(int tab, const char *ctl, ...); 380 381 static hammer2_off_t TotalFreeAccum16; 382 static hammer2_off_t TotalFreeAccum64; 383 384 int 385 cmd_show(const char *devpath, int dofreemap) 386 { 387 hammer2_blockref_t broot; 388 hammer2_blockref_t best; 389 hammer2_media_data_t media; 390 int fd; 391 int i; 392 int best_i; 393 char *env; 394 395 TotalFreeAccum16 = 0; /* includes TotalFreeAccum64 */ 396 TotalFreeAccum64 = 0; 397 398 env = getenv("HAMMER2_SHOW_TAB"); 399 if (env != NULL) { 400 show_tab = (int)strtol(env, NULL, 0); 401 if (errno || show_tab < 0 || show_tab > 8) 402 show_tab = 2; 403 } 404 405 fd = open(devpath, O_RDONLY); 406 if (fd < 0) { 407 perror("open"); 408 return 1; 409 } 410 411 /* 412 * Show the tree using the best volume header. 413 * -vvv will show the tree for all four volume headers. 414 */ 415 best_i = -1; 416 bzero(&best, sizeof(best)); 417 for (i = 0; i < HAMMER2_NUM_VOLHDRS; ++i) { 418 bzero(&broot, sizeof(broot)); 419 broot.type = dofreemap ? 420 HAMMER2_BREF_TYPE_FREEMAP : HAMMER2_BREF_TYPE_VOLUME; 421 broot.data_off = (i * HAMMER2_ZONE_BYTES64) | HAMMER2_PBUFRADIX; 422 lseek(fd, broot.data_off & ~HAMMER2_OFF_MASK_RADIX, 0); 423 if (read(fd, &media, HAMMER2_PBUFSIZE) == 424 (ssize_t)HAMMER2_PBUFSIZE) { 425 broot.mirror_tid = media.voldata.mirror_tid; 426 if (best_i < 0 || best.mirror_tid < broot.mirror_tid) { 427 best_i = i; 428 best = broot; 429 } 430 if (VerboseOpt >= 3) 431 show_bref(&media.voldata, fd, 0, i, &broot, 0); 432 } 433 } 434 if (VerboseOpt < 3) 435 show_bref(&media.voldata, fd, 0, best_i, &best, 0); 436 close(fd); 437 438 if (dofreemap && VerboseOpt < 3) { 439 printf("Total free storage: %6.3fGB (%6.3fGB in 64KB chunks)\n", 440 (double)TotalFreeAccum16 / (1024.0 * 1024.0 * 1024.0), 441 (double)TotalFreeAccum64 / (1024.0 * 1024.0 * 1024.0)); 442 } 443 444 return 0; 445 } 446 447 static void 448 show_bref(hammer2_volume_data_t *voldata, int fd, int tab, 449 int bi, hammer2_blockref_t *bref, int norecurse) 450 { 451 hammer2_media_data_t media; 452 hammer2_blockref_t *bscan; 453 hammer2_off_t tmp; 454 int bcount; 455 int i; 456 int namelen; 457 int obrace = 1; 458 int failed; 459 size_t bytes; 460 const char *type_str; 461 char *str = NULL; 462 uint32_t cv; 463 uint64_t cv64; 464 465 bytes = (bref->data_off & HAMMER2_OFF_MASK_RADIX); 466 if (bytes) 467 bytes = (size_t)1 << bytes; 468 if (bytes) { 469 hammer2_off_t io_off; 470 hammer2_off_t io_base; 471 size_t io_bytes; 472 size_t boff; 473 474 io_off = bref->data_off & ~HAMMER2_OFF_MASK_RADIX; 475 io_base = io_off & ~(hammer2_off_t)(HAMMER2_MINIOSIZE - 1); 476 boff = io_off - io_base; 477 478 io_bytes = HAMMER2_MINIOSIZE; 479 while (io_bytes + boff < bytes) 480 io_bytes <<= 1; 481 482 if (io_bytes > sizeof(media)) { 483 printf("(bad block size %zd)\n", bytes); 484 return; 485 } 486 if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) { 487 lseek(fd, io_base, 0); 488 if (read(fd, &media, io_bytes) != (ssize_t)io_bytes) { 489 printf("(media read failed)\n"); 490 return; 491 } 492 if (boff) 493 bcopy((char *)&media + boff, &media, bytes); 494 } 495 } 496 497 bscan = NULL; 498 bcount = 0; 499 namelen = 0; 500 failed = 0; 501 502 switch(bref->type) { 503 case HAMMER2_BREF_TYPE_EMPTY: 504 type_str = "empty"; 505 break; 506 case HAMMER2_BREF_TYPE_DIRENT: 507 type_str = "dirent"; 508 break; 509 case HAMMER2_BREF_TYPE_INODE: 510 type_str = "inode"; 511 break; 512 case HAMMER2_BREF_TYPE_INDIRECT: 513 type_str = "indblk"; 514 break; 515 case HAMMER2_BREF_TYPE_DATA: 516 type_str = "data"; 517 break; 518 case HAMMER2_BREF_TYPE_VOLUME: 519 type_str = "volume"; 520 break; 521 case HAMMER2_BREF_TYPE_FREEMAP: 522 type_str = "freemap"; 523 break; 524 case HAMMER2_BREF_TYPE_FREEMAP_NODE: 525 type_str = "fmapnode"; 526 break; 527 case HAMMER2_BREF_TYPE_FREEMAP_LEAF: 528 type_str = "fbitmap"; 529 break; 530 default: 531 type_str = "unknown"; 532 break; 533 } 534 535 switch(bref->type) { 536 case HAMMER2_BREF_TYPE_INODE: 537 if (!(media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA)) { 538 bscan = &media.ipdata.u.blockset.blockref[0]; 539 bcount = HAMMER2_SET_COUNT; 540 } 541 break; 542 case HAMMER2_BREF_TYPE_INDIRECT: 543 bscan = &media.npdata[0]; 544 bcount = bytes / sizeof(hammer2_blockref_t); 545 break; 546 case HAMMER2_BREF_TYPE_VOLUME: 547 bscan = &media.voldata.sroot_blockset.blockref[0]; 548 bcount = HAMMER2_SET_COUNT; 549 break; 550 case HAMMER2_BREF_TYPE_FREEMAP: 551 bscan = &media.voldata.freemap_blockset.blockref[0]; 552 bcount = HAMMER2_SET_COUNT; 553 break; 554 case HAMMER2_BREF_TYPE_FREEMAP_NODE: 555 bscan = &media.npdata[0]; 556 bcount = bytes / sizeof(hammer2_blockref_t); 557 break; 558 } 559 560 tabprintf(tab, 561 "%s.%-3d %016jx %016jx/%-2d " 562 "mir=%016jx mod=%016jx leafcnt=%d ", 563 type_str, bi, (intmax_t)bref->data_off, 564 (intmax_t)bref->key, (intmax_t)bref->keybits, 565 (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid, 566 bref->leaf_count); 567 tab += show_tab; 568 if (bcount) 569 printf("bcnt=%d ", bcount); 570 if (bref->flags) 571 printf("flags=%02x ", bref->flags); 572 if (bref->type == HAMMER2_BREF_TYPE_FREEMAP_NODE || 573 bref->type == HAMMER2_BREF_TYPE_FREEMAP_LEAF) { 574 printf("bigmask=%08x avail=%ld ", 575 bref->check.freemap.bigmask, bref->check.freemap.avail); 576 } 577 578 /* 579 * Check data integrity in verbose mode, otherwise we are just doing 580 * a quick meta-data scan. Meta-data integrity is always checked. 581 * (Also see the check above that ensures the media data is loaded, 582 * otherwise there's no data to check!). 583 * 584 * WARNING! bref->check state may be used for other things when 585 * bref has no data (bytes == 0). 586 */ 587 if (bytes && 588 (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1)) { 589 switch(HAMMER2_DEC_CHECK(bref->methods)) { 590 case HAMMER2_CHECK_NONE: 591 printf("(meth %02x) ", bref->methods); 592 break; 593 case HAMMER2_CHECK_DISABLED: 594 printf("(meth %02x) ", bref->methods); 595 break; 596 case HAMMER2_CHECK_ISCSI32: 597 cv = hammer2_icrc32(&media, bytes); 598 if (bref->check.iscsi32.value != cv) { 599 printf("(icrc %02x:%08x/%08x failed) ", 600 bref->methods, 601 bref->check.iscsi32.value, 602 cv); 603 failed = 1; 604 } else { 605 printf("(meth %02x, iscsi32=%08x) ", 606 bref->methods, cv); 607 } 608 break; 609 case HAMMER2_CHECK_XXHASH64: 610 cv64 = XXH64(&media, bytes, XXH_HAMMER2_SEED); 611 if (bref->check.xxhash64.value != cv64) { 612 printf("(xxhash64 %02x:%016jx/%016jx failed) ", 613 bref->methods, 614 bref->check.xxhash64.value, 615 cv64); 616 failed = 1; 617 } else { 618 printf("(meth %02x, xxh=%016jx) ", 619 bref->methods, cv64); 620 } 621 break; 622 case HAMMER2_CHECK_SHA192: 623 printf("(meth %02x) ", bref->methods); 624 break; 625 case HAMMER2_CHECK_FREEMAP: 626 cv = hammer2_icrc32(&media, bytes); 627 if (bref->check.freemap.icrc32 != cv) { 628 printf("(fcrc %02x:%08x/%08x failed) ", 629 bref->methods, 630 bref->check.freemap.icrc32, 631 cv); 632 failed = 1; 633 } else { 634 printf("(meth %02x, fcrc=%08x) ", 635 bref->methods, cv); 636 } 637 break; 638 } 639 } 640 641 if (QuietOpt > 0) { 642 obrace = 0; 643 printf("\n"); 644 goto skip_data; 645 } 646 647 switch(bref->type) { 648 case HAMMER2_BREF_TYPE_EMPTY: 649 obrace = 0; 650 break; 651 case HAMMER2_BREF_TYPE_DIRENT: 652 printf("{\n"); 653 if (bref->embed.dirent.namlen <= sizeof(bref->check.buf)) { 654 tabprintf(tab, "filename \"%*.*s\"\n", 655 bref->embed.dirent.namlen, 656 bref->embed.dirent.namlen, 657 bref->check.buf); 658 } else { 659 tabprintf(tab, "filename \"%*.*s\"\n", 660 bref->embed.dirent.namlen, 661 bref->embed.dirent.namlen, 662 media.buf); 663 } 664 tabprintf(tab, "inum 0x%016jx\n", 665 (uintmax_t)bref->embed.dirent.inum); 666 tabprintf(tab, "type %s\n", 667 hammer2_iptype_to_str(bref->embed.dirent.type)); 668 break; 669 case HAMMER2_BREF_TYPE_INODE: 670 printf("{\n"); 671 namelen = media.ipdata.meta.name_len; 672 if (namelen > HAMMER2_INODE_MAXNAME) 673 namelen = 0; 674 tabprintf(tab, "filename \"%*.*s\"\n", 675 namelen, namelen, media.ipdata.filename); 676 tabprintf(tab, "version %d\n", media.ipdata.meta.version); 677 tabprintf(tab, "pfs_st %d\n", media.ipdata.meta.pfs_subtype); 678 tabprintf(tab, "uflags 0x%08x\n", 679 media.ipdata.meta.uflags); 680 if (media.ipdata.meta.rmajor || media.ipdata.meta.rminor) { 681 tabprintf(tab, "rmajor %d\n", 682 media.ipdata.meta.rmajor); 683 tabprintf(tab, "rminor %d\n", 684 media.ipdata.meta.rminor); 685 } 686 tabprintf(tab, "ctime %s\n", 687 hammer2_time64_to_str(media.ipdata.meta.ctime, &str)); 688 tabprintf(tab, "mtime %s\n", 689 hammer2_time64_to_str(media.ipdata.meta.mtime, &str)); 690 tabprintf(tab, "atime %s\n", 691 hammer2_time64_to_str(media.ipdata.meta.atime, &str)); 692 tabprintf(tab, "btime %s\n", 693 hammer2_time64_to_str(media.ipdata.meta.btime, &str)); 694 tabprintf(tab, "uid %s\n", 695 hammer2_uuid_to_str(&media.ipdata.meta.uid, &str)); 696 tabprintf(tab, "gid %s\n", 697 hammer2_uuid_to_str(&media.ipdata.meta.gid, &str)); 698 tabprintf(tab, "type %s\n", 699 hammer2_iptype_to_str(media.ipdata.meta.type)); 700 tabprintf(tab, "opflgs 0x%02x\n", 701 media.ipdata.meta.op_flags); 702 tabprintf(tab, "capflgs 0x%04x\n", 703 media.ipdata.meta.cap_flags); 704 tabprintf(tab, "mode %-7o\n", 705 media.ipdata.meta.mode); 706 tabprintf(tab, "inum 0x%016jx\n", 707 media.ipdata.meta.inum); 708 tabprintf(tab, "size %ju ", 709 (uintmax_t)media.ipdata.meta.size); 710 if (media.ipdata.meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA && 711 media.ipdata.meta.size <= HAMMER2_EMBEDDED_BYTES) 712 printf("(embedded data)\n"); 713 else 714 printf("\n"); 715 tabprintf(tab, "nlinks %ju\n", 716 (uintmax_t)media.ipdata.meta.nlinks); 717 tabprintf(tab, "iparent 0x%016jx\n", 718 (uintmax_t)media.ipdata.meta.iparent); 719 tabprintf(tab, "name_key 0x%016jx\n", 720 (uintmax_t)media.ipdata.meta.name_key); 721 tabprintf(tab, "name_len %u\n", 722 media.ipdata.meta.name_len); 723 tabprintf(tab, "ncopies %u\n", 724 media.ipdata.meta.ncopies); 725 tabprintf(tab, "compalg %u\n", 726 media.ipdata.meta.comp_algo); 727 tabprintf(tab, "target_t %u\n", 728 media.ipdata.meta.target_type); 729 tabprintf(tab, "checkalg %u\n", 730 media.ipdata.meta.check_algo); 731 if ((media.ipdata.meta.op_flags & HAMMER2_OPFLAG_PFSROOT) || 732 media.ipdata.meta.pfs_type == HAMMER2_PFSTYPE_SUPROOT) { 733 tabprintf(tab, "pfs_nmas %u\n", 734 media.ipdata.meta.pfs_nmasters); 735 tabprintf(tab, "pfs_type %u (%s)\n", 736 media.ipdata.meta.pfs_type, 737 hammer2_pfstype_to_str(media.ipdata.meta.pfs_type)); 738 tabprintf(tab, "pfs_inum 0x%016jx\n", 739 (uintmax_t)media.ipdata.meta.pfs_inum); 740 tabprintf(tab, "pfs_clid %s\n", 741 hammer2_uuid_to_str(&media.ipdata.meta.pfs_clid, 742 &str)); 743 tabprintf(tab, "pfs_fsid %s\n", 744 hammer2_uuid_to_str(&media.ipdata.meta.pfs_fsid, 745 &str)); 746 tabprintf(tab, "pfs_lsnap_tid 0x%016jx\n", 747 (uintmax_t)media.ipdata.meta.pfs_lsnap_tid); 748 } 749 tabprintf(tab, "data_quota %ju\n", 750 (uintmax_t)media.ipdata.meta.data_quota); 751 tabprintf(tab, "data_count %ju\n", 752 (uintmax_t)bref->embed.stats.data_count); 753 tabprintf(tab, "inode_quota %ju\n", 754 (uintmax_t)media.ipdata.meta.inode_quota); 755 tabprintf(tab, "inode_count %ju\n", 756 (uintmax_t)bref->embed.stats.inode_count); 757 break; 758 case HAMMER2_BREF_TYPE_INDIRECT: 759 printf("{\n"); 760 break; 761 case HAMMER2_BREF_TYPE_DATA: 762 printf("\n"); 763 obrace = 0; 764 break; 765 case HAMMER2_BREF_TYPE_VOLUME: 766 printf("mirror_tid=%016jx freemap_tid=%016jx ", 767 media.voldata.mirror_tid, 768 media.voldata.freemap_tid); 769 printf("{\n"); 770 break; 771 case HAMMER2_BREF_TYPE_FREEMAP: 772 printf("mirror_tid=%016jx freemap_tid=%016jx ", 773 media.voldata.mirror_tid, 774 media.voldata.freemap_tid); 775 printf("{\n"); 776 break; 777 case HAMMER2_BREF_TYPE_FREEMAP_LEAF: 778 printf("{\n"); 779 tmp = bref->data_off & ~HAMMER2_OFF_MASK_RADIX; 780 tmp &= HAMMER2_SEGMASK; 781 tmp /= HAMMER2_PBUFSIZE; 782 assert(tmp < HAMMER2_ZONE_FREEMAP_END); 783 tmp -= HAMMER2_ZONE_FREEMAP_00; 784 tmp /= HAMMER2_ZONE_FREEMAP_INC; 785 tabprintf(tab, "rotation=%d\n", (int)tmp); 786 787 for (i = 0; i < HAMMER2_FREEMAP_COUNT; ++i) { 788 hammer2_off_t data_off = bref->key + 789 i * HAMMER2_FREEMAP_LEVEL0_SIZE; 790 #if HAMMER2_BMAP_ELEMENTS != 8 791 #error "cmd_debug.c: HAMMER2_BMAP_ELEMENTS expected to be 8" 792 #endif 793 tabprintf(tab + 4, "%016jx %04d.%04x (avail=%7d) " 794 "%016jx %016jx %016jx %016jx " 795 "%016jx %016jx %016jx %016jx\n", 796 data_off, i, media.bmdata[i].class, 797 media.bmdata[i].avail, 798 media.bmdata[i].bitmapq[0], 799 media.bmdata[i].bitmapq[1], 800 media.bmdata[i].bitmapq[2], 801 media.bmdata[i].bitmapq[3], 802 media.bmdata[i].bitmapq[4], 803 media.bmdata[i].bitmapq[5], 804 media.bmdata[i].bitmapq[6], 805 media.bmdata[i].bitmapq[7]); 806 if (data_off >= voldata->aux_end && 807 data_off < voldata->volu_size) { 808 CountFreeBlocks(&media.bmdata[i], 809 &TotalFreeAccum16, 810 &TotalFreeAccum64); 811 } 812 } 813 tabprintf(tab, "}\n"); 814 break; 815 case HAMMER2_BREF_TYPE_FREEMAP_NODE: 816 printf("{\n"); 817 tmp = bref->data_off & ~HAMMER2_OFF_MASK_RADIX; 818 tmp &= HAMMER2_SEGMASK; 819 tmp /= HAMMER2_PBUFSIZE; 820 assert(tmp < HAMMER2_ZONE_FREEMAP_END); 821 tmp -= HAMMER2_ZONE_FREEMAP_00; 822 tmp /= HAMMER2_ZONE_FREEMAP_INC; 823 tabprintf(tab, "rotation=%d\n", (int)tmp); 824 break; 825 default: 826 printf("\n"); 827 obrace = 0; 828 break; 829 } 830 if (str) 831 free(str); 832 833 skip_data: 834 /* 835 * Recurse if norecurse == 0. If the CRC failed, pass norecurse = 1. 836 * That is, if an indirect or inode fails we still try to list its 837 * direct children to help with debugging, but go no further than 838 * that because they are probably garbage. 839 */ 840 for (i = 0; norecurse == 0 && i < bcount; ++i) { 841 if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) { 842 show_bref(voldata, fd, tab, i, &bscan[i], failed); 843 } 844 } 845 tab -= show_tab; 846 if (obrace) { 847 if (bref->type == HAMMER2_BREF_TYPE_INODE) 848 tabprintf(tab, "} (%s.%d, \"%*.*s\")\n", 849 type_str, bi, namelen, namelen, 850 media.ipdata.filename); 851 else 852 tabprintf(tab, "} (%s.%d)\n", type_str,bi); 853 } 854 } 855 856 static 857 void 858 CountFreeBlocks(hammer2_bmap_data_t *bmap, 859 hammer2_off_t *accum16, hammer2_off_t *accum64) 860 { 861 int i; 862 int j; 863 864 for (i = 0; i < 8; ++i) { 865 uint64_t bm = bmap->bitmapq[i]; 866 uint64_t mask; 867 868 mask = 0x03; 869 for (j = 0; j < 64; j += 2) { 870 if ((bm & mask) == 0) 871 *accum16 += 16384; 872 } 873 mask = 0xFF; 874 for (j = 0; j < 64; j += 8) { 875 if ((bm & mask) == 0) 876 *accum64 += 65536; 877 } 878 } 879 } 880 881 int 882 cmd_hash(int ac, const char **av) 883 { 884 int i; 885 886 for (i = 0; i < ac; ++i) { 887 printf("%016jx %s\n", 888 dirhash((const unsigned char*)av[i], strlen(av[i])), 889 av[i]); 890 } 891 return(0); 892 } 893 894 int 895 cmd_dhash(int ac, const char **av) 896 { 897 char buf[1024]; /* 1K extended directory record */ 898 uint64_t hash; 899 int i; 900 901 for (i = 0; i < ac; ++i) { 902 bzero(buf, sizeof(buf)); 903 snprintf(buf, sizeof(buf), "%s", av[i]); 904 hash = XXH64(buf, sizeof(buf), XXH_HAMMER2_SEED); 905 printf("%016jx %s\n", hash, av[i]); 906 } 907 return(0); 908 } 909 910 int 911 cmd_dumpchain(const char *path, u_int flags) 912 { 913 int dummy = (int)flags; 914 int fd; 915 916 fd = open(path, O_RDONLY); 917 if (fd >= 0) { 918 ioctl(fd, HAMMER2IOC_DEBUG_DUMP, &dummy); 919 close(fd); 920 } else { 921 fprintf(stderr, "unable to open %s\n", path); 922 } 923 return 0; 924 } 925 926 927 static 928 void 929 tabprintf(int tab, const char *ctl, ...) 930 { 931 va_list va; 932 933 printf("%*.*s", tab, tab, ""); 934 va_start(va, ctl); 935 vprintf(ctl, va); 936 va_end(va); 937 } 938