1 /* 2 * Copyright (c) 2008 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <libutil.h> 36 37 #include "hammer.h" 38 39 #define FLAG_TOOFARLEFT 0x0001 40 #define FLAG_TOOFARRIGHT 0x0002 41 #define FLAG_BADTYPE 0x0004 42 #define FLAG_BADCHILDPARENT 0x0008 43 #define FLAG_BADMIRRORTID 0x0010 44 45 struct { 46 struct hammer_base_elm base; 47 int limit; /* # of fields to test */ 48 int filter; /* filter type (default -1) */ 49 int obfuscate; /* obfuscate direntry name */ 50 int indent; /* use depth indentation */ 51 struct zone_stat *stats; 52 } opt; 53 54 static __inline void print_btree(hammer_off_t node_offset); 55 static __inline void print_subtree(hammer_btree_elm_t elm); 56 static void print_btree_node(hammer_off_t node_offset, hammer_tid_t mirror_tid, 57 hammer_btree_elm_t lbe); 58 static int test_node_count(hammer_node_ondisk_t node, char *badmp); 59 static void print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset, 60 hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext); 61 static int get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset, 62 hammer_btree_elm_t elm, hammer_btree_elm_t lbe); 63 static int test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe); 64 static int test_rbn_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe); 65 static void print_bigblock_fill(hammer_off_t offset); 66 static const char *check_data_crc(hammer_btree_elm_t elm, const char **whichp); 67 static hammer_crc_t get_leaf_crc(uint32_t vol_version, void *data, 68 hammer_btree_leaf_elm_t leaf, const char **whichp); 69 static uint32_t get_buf_crc(hammer_off_t buf_offset, int32_t buf_len, 70 uint32_t leaf_crc, const char **whichp); 71 static void print_record(hammer_btree_elm_t elm); 72 static int init_btree_search(const char *arg); 73 static int test_btree_search(hammer_btree_elm_t elm); 74 static int test_btree_match(hammer_btree_elm_t elm); 75 static int test_btree_out_of_range(hammer_btree_elm_t elm); 76 static void hexdump_record(const void *ptr, int length, const char *hdr); 77 78 static int num_bad_node = 0; 79 static int num_bad_elm = 0; 80 static int num_bad_rec = 0; 81 static int depth; 82 83 #define _X "\t" 84 static const char* _indents[] = { 85 "", 86 _X, 87 _X _X, 88 _X _X _X, 89 _X _X _X _X, 90 _X _X _X _X _X, 91 _X _X _X _X _X _X, 92 _X _X _X _X _X _X _X, 93 _X _X _X _X _X _X _X _X, 94 _X _X _X _X _X _X _X _X _X, 95 _X _X _X _X _X _X _X _X _X _X, 96 _X _X _X _X _X _X _X _X _X _X _X, 97 _X _X _X _X _X _X _X _X _X _X _X _X, 98 _X _X _X _X _X _X _X _X _X _X _X _X _X, 99 _X _X _X _X _X _X _X _X _X _X _X _X _X _X, 100 _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X, 101 _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X, 102 /* deep enough */ 103 }; 104 #define INDENT _indents[opt.indent ? depth : 0] 105 106 void 107 hammer_cmd_show(const char *arg, int filter, int obfuscate, int indent) 108 { 109 struct volume_info *volume; 110 hammer_volume_ondisk_t ondisk; 111 struct zone_stat *stats = NULL; 112 113 volume = get_root_volume(); 114 ondisk = volume->ondisk; 115 116 print_blockmap(volume); 117 118 if (VerboseOpt) 119 stats = hammer_init_zone_stat_bits(); 120 121 bzero(&opt, sizeof(opt)); 122 opt.filter = filter; 123 opt.obfuscate = obfuscate; 124 opt.indent = indent; 125 opt.stats = stats; 126 127 if (init_btree_search(arg) > 0) { 128 printf("arg=\"%s\"", arg); 129 if (opt.limit > 0) 130 printf(" lo=%08x", opt.base.localization); 131 if (opt.limit > 1) 132 printf(" objid=%016jx", (uintmax_t)opt.base.obj_id); 133 if (opt.limit > 2) 134 printf(" rt=%02x", opt.base.rec_type); 135 if (opt.limit > 3) 136 printf(" key=%016jx", (uintmax_t)opt.base.key); 137 if (opt.limit > 4) 138 printf(" tid=%016jx", (uintmax_t)opt.base.create_tid); 139 printf("\n"); 140 } 141 print_btree(ondisk->vol0_btree_root); 142 143 if (stats) { 144 hammer_print_zone_stat(stats); 145 hammer_cleanup_zone_stat(stats); 146 } 147 148 if (num_bad_node || VerboseOpt) { 149 printf("%d bad nodes\n", num_bad_node); 150 } 151 if (num_bad_elm || VerboseOpt) { 152 printf("%d bad elms\n", num_bad_elm); 153 } 154 if (num_bad_rec || VerboseOpt) { 155 printf("%d bad records\n", num_bad_rec); 156 } 157 } 158 159 static __inline 160 void 161 print_btree(hammer_off_t node_offset) 162 { 163 depth = -1; 164 print_btree_node(node_offset, HAMMER_MAX_TID, NULL); 165 assert(depth == -1); 166 } 167 168 static __inline 169 void 170 print_subtree(hammer_btree_elm_t elm) 171 { 172 hammer_btree_internal_elm_t i = &elm->internal; 173 print_btree_node(i->subtree_offset, i->mirror_tid, elm); 174 } 175 176 static void 177 print_btree_node(hammer_off_t node_offset, hammer_tid_t mirror_tid, 178 hammer_btree_elm_t lbe) 179 { 180 struct buffer_info *buffer = NULL; 181 hammer_node_ondisk_t node; 182 hammer_btree_elm_t elm; 183 int i; 184 char badc = ' '; /* good */ 185 char badm = ' '; /* good */ 186 const char *ext; 187 188 depth++; 189 node = get_buffer_data(node_offset, &buffer, 0); 190 191 if (node == NULL) { 192 badc = 'B'; 193 badm = 'I'; 194 } else { 195 if (!hammer_crc_test_btree(HammerVersion, node)) 196 badc = 'B'; 197 if (node->mirror_tid > mirror_tid) { 198 badc = 'B'; 199 badm = 'M'; 200 } 201 if (test_node_count(node, &badm) == -1) { 202 badc = 'B'; 203 assert(badm != ' '); 204 } 205 } 206 207 if (badm != ' ' || badc != ' ') /* not good */ 208 ++num_bad_node; 209 210 printf("%s%c%c NODE %016jx ", 211 INDENT, badc, badm, (uintmax_t)node_offset); 212 printf("cnt=%02d p=%016jx type=%c depth=%d mirror=%016jx", 213 node->count, 214 (uintmax_t)node->parent, 215 (node->type ? node->type : '?'), 216 depth, 217 (uintmax_t)node->mirror_tid); 218 printf(" fill="); 219 print_bigblock_fill(node_offset); 220 printf(" {\n"); 221 222 if (opt.stats) 223 hammer_add_zone_stat(opt.stats, node_offset, sizeof(*node)); 224 225 for (i = 0; i < node->count; ++i) { 226 elm = &node->elms[i]; 227 ext = NULL; 228 if (opt.limit) { 229 switch (node->type) { 230 case HAMMER_BTREE_TYPE_INTERNAL: 231 if (!test_btree_out_of_range(elm)) 232 ext = "*"; 233 break; 234 case HAMMER_BTREE_TYPE_LEAF: 235 if (test_btree_match(elm)) 236 ext = "*"; 237 break; 238 } 239 } 240 print_btree_elm(node, node_offset, elm, lbe, ext); 241 } 242 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) { 243 assert(i == node->count); /* boundary */ 244 elm = &node->elms[i]; 245 print_btree_elm(node, node_offset, elm, lbe, NULL); 246 } 247 printf("%s }\n", INDENT); 248 249 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) { 250 for (i = 0; i < node->count; ++i) { 251 elm = &node->elms[i]; 252 if (opt.limit && opt.filter) { 253 if (test_btree_out_of_range(elm)) 254 continue; 255 } 256 if (elm->internal.subtree_offset) { 257 print_subtree(elm); 258 /* 259 * Cause show to do normal iteration after 260 * seeking to the lo:objid:rt:key:tid 261 * by default 262 */ 263 if (opt.limit && opt.filter == -1) /* default */ 264 opt.filter = 0; 265 } 266 } 267 } 268 rel_buffer(buffer); 269 depth--; 270 } 271 272 static int 273 test_node_count(hammer_node_ondisk_t node, char *badmp) 274 { 275 hammer_node_ondisk_t parent_node; 276 struct buffer_info *buffer = NULL; 277 int maxcount; 278 279 maxcount = hammer_node_max_elements(node->type); 280 281 if (maxcount == -1) { 282 *badmp = 'U'; 283 return(-1); 284 } else if (node->count > maxcount) { 285 *badmp = 'C'; 286 return(-1); 287 } else if (node->count == 0) { 288 parent_node = get_buffer_data(node->parent, &buffer, 0); 289 if (parent_node->count != 1) { 290 *badmp = 'C'; 291 rel_buffer(buffer); 292 return(-1); 293 } 294 rel_buffer(buffer); 295 } 296 297 return(0); 298 } 299 300 static __inline 301 int 302 is_root_btree_beg(uint8_t type, int i, hammer_btree_elm_t elm) 303 { 304 /* 305 * elm->base.btype depends on what the original node had 306 * so it could be anything but HAMMER_BTREE_TYPE_NONE. 307 */ 308 return (type == HAMMER_BTREE_TYPE_INTERNAL && 309 i == 0 && 310 elm->base.localization == HAMMER_MIN_ONDISK_LOCALIZATION && 311 elm->base.obj_id == (int64_t)HAMMER_MIN_OBJID && 312 elm->base.key == (int64_t)HAMMER_MIN_KEY && 313 elm->base.create_tid == 1 && 314 elm->base.delete_tid == 1 && 315 elm->base.rec_type == HAMMER_MIN_RECTYPE && 316 elm->base.obj_type == 0 && 317 elm->base.btype != HAMMER_BTREE_TYPE_NONE); 318 } 319 320 static __inline 321 int 322 is_root_btree_end(uint8_t type, int i, hammer_btree_elm_t elm) 323 { 324 return (type == HAMMER_BTREE_TYPE_INTERNAL && 325 i != 0 && 326 elm->base.localization == HAMMER_MAX_ONDISK_LOCALIZATION && 327 elm->base.obj_id == HAMMER_MAX_OBJID && 328 elm->base.key == HAMMER_MAX_KEY && 329 elm->base.create_tid == HAMMER_MAX_TID && 330 elm->base.delete_tid == 0 && 331 elm->base.rec_type == HAMMER_MAX_RECTYPE && 332 elm->base.obj_type == 0 && 333 elm->base.btype == HAMMER_BTREE_TYPE_NONE); 334 } 335 336 static 337 void 338 print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset, 339 hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext) 340 { 341 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 }; 342 char deleted; 343 char rootelm; 344 const char *label; 345 const char *p; 346 const char *which; 347 int flags; 348 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1; 349 350 flags = get_elm_flags(node, node_offset, elm, lbe); 351 flagstr[0] = flags ? 'B' : 'G'; 352 if (flags & FLAG_TOOFARLEFT) 353 flagstr[2] = 'L'; 354 if (flags & FLAG_TOOFARRIGHT) 355 flagstr[3] = 'R'; 356 if (flags & FLAG_BADTYPE) 357 flagstr[4] = 'T'; 358 if (flags & FLAG_BADCHILDPARENT) 359 flagstr[5] = 'C'; 360 if (flags & FLAG_BADMIRRORTID) 361 flagstr[6] = 'M'; 362 if (flagstr[0] == 'B') 363 ++num_bad_elm; 364 365 /* 366 * Check if elm is derived from root split 367 */ 368 if (is_root_btree_beg(node->type, i, elm)) 369 rootelm = '>'; 370 else if (is_root_btree_end(node->type, i, elm)) 371 rootelm = '<'; 372 else 373 rootelm = ' '; 374 375 if (elm->base.delete_tid) 376 deleted = 'd'; 377 else 378 deleted = ' '; 379 380 if (node->type == HAMMER_BTREE_TYPE_INTERNAL && node->count == i) 381 label = "RBN"; 382 else 383 label = "ELM"; 384 385 printf("%s%s %s %2d %c ", 386 INDENT, flagstr, label, i, hammer_elm_btype(elm)); 387 printf("lo=%08x objid=%016jx rt=%02x key=%016jx tid=%016jx\n", 388 elm->base.localization, 389 (uintmax_t)elm->base.obj_id, 390 elm->base.rec_type, 391 (uintmax_t)elm->base.key, 392 (uintmax_t)elm->base.create_tid); 393 printf("%s %c del=%016jx ot=%02x", 394 INDENT, 395 (rootelm == ' ' ? deleted : rootelm), 396 (uintmax_t)elm->base.delete_tid, 397 elm->base.obj_type); 398 399 switch(node->type) { 400 case HAMMER_BTREE_TYPE_INTERNAL: 401 printf(" suboff=%016jx mirror=%016jx", 402 (uintmax_t)elm->internal.subtree_offset, 403 (uintmax_t)elm->internal.mirror_tid); 404 if (ext) 405 printf(" %s", ext); 406 break; 407 case HAMMER_BTREE_TYPE_LEAF: 408 switch(elm->base.btype) { 409 case HAMMER_BTREE_TYPE_RECORD: 410 printf(" dataoff=%016jx/%d", 411 (uintmax_t)elm->leaf.data_offset, 412 elm->leaf.data_len); 413 p = check_data_crc(elm, &which); 414 printf(" %scrc=%08x", which, elm->leaf.data_crc); 415 if (p) { 416 printf(" error=%s", p); 417 ++num_bad_rec; 418 } 419 printf(" fill="); 420 print_bigblock_fill(elm->leaf.data_offset); 421 if (QuietOpt < 2) 422 print_record(elm); 423 if (opt.stats) 424 hammer_add_zone_stat(opt.stats, 425 elm->leaf.data_offset, 426 elm->leaf.data_len); 427 break; 428 default: 429 printf(" badtype=%d", elm->base.btype); 430 break; 431 } 432 if (ext) 433 printf(" %s", ext); 434 break; 435 } 436 printf("\n"); 437 } 438 439 static 440 int 441 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset, 442 hammer_btree_elm_t elm, hammer_btree_elm_t lbe) 443 { 444 hammer_off_t child_offset; 445 int flags = 0; 446 int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1; 447 448 switch(node->type) { 449 case HAMMER_BTREE_TYPE_INTERNAL: 450 child_offset = elm->internal.subtree_offset; 451 if (elm->internal.mirror_tid > node->mirror_tid) 452 flags |= FLAG_BADMIRRORTID; 453 454 if (i == node->count) { 455 if (child_offset != 0) 456 flags |= FLAG_BADCHILDPARENT; 457 switch(elm->base.btype) { 458 case HAMMER_BTREE_TYPE_NONE: 459 flags |= test_rbn_lr(elm, lbe); 460 break; 461 default: 462 flags |= FLAG_BADTYPE; 463 break; 464 } 465 } else { 466 if (child_offset == 0) { 467 flags |= FLAG_BADCHILDPARENT; 468 } else { 469 struct buffer_info *buffer = NULL; 470 hammer_node_ondisk_t subnode; 471 subnode = get_buffer_data(child_offset, &buffer, 0); 472 if (subnode == NULL) 473 flags |= FLAG_BADCHILDPARENT; 474 else if (subnode->parent != node_offset) 475 flags |= FLAG_BADCHILDPARENT; 476 rel_buffer(buffer); 477 } 478 switch(elm->base.btype) { 479 case HAMMER_BTREE_TYPE_INTERNAL: 480 case HAMMER_BTREE_TYPE_LEAF: 481 flags |= test_lr(elm, lbe); 482 break; 483 default: 484 flags |= FLAG_BADTYPE; 485 break; 486 } 487 } 488 break; 489 case HAMMER_BTREE_TYPE_LEAF: 490 if (elm->leaf.data_offset == 0) { 491 flags |= FLAG_BADCHILDPARENT; 492 } 493 if (elm->leaf.data_len == 0) { 494 flags |= FLAG_BADCHILDPARENT; 495 } 496 497 if (node->mirror_tid == 0 && 498 !(node->parent == 0 && node->count == 2)) { 499 flags |= FLAG_BADMIRRORTID; 500 } 501 if (elm->base.create_tid && node->mirror_tid && 502 elm->base.create_tid > node->mirror_tid) { 503 flags |= FLAG_BADMIRRORTID; 504 } 505 if (elm->base.delete_tid && node->mirror_tid && 506 elm->base.delete_tid > node->mirror_tid) { 507 flags |= FLAG_BADMIRRORTID; 508 } 509 switch(elm->base.btype) { 510 case HAMMER_BTREE_TYPE_RECORD: 511 flags |= test_lr(elm, lbe); 512 break; 513 default: 514 flags |= FLAG_BADTYPE; 515 break; 516 } 517 break; 518 default: 519 flags |= FLAG_BADTYPE; 520 break; 521 } 522 return(flags); 523 } 524 525 /* 526 * Taken from /usr/src/sys/vfs/hammer/hammer_btree.c. 527 */ 528 static 529 int 530 hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2) 531 { 532 if (key1->localization < key2->localization) 533 return(-5); 534 if (key1->localization > key2->localization) 535 return(5); 536 537 if (key1->obj_id < key2->obj_id) 538 return(-4); 539 if (key1->obj_id > key2->obj_id) 540 return(4); 541 542 if (key1->rec_type < key2->rec_type) 543 return(-3); 544 if (key1->rec_type > key2->rec_type) 545 return(3); 546 547 if (key1->key < key2->key) 548 return(-2); 549 if (key1->key > key2->key) 550 return(2); 551 552 if (key1->create_tid == 0) { 553 if (key2->create_tid == 0) 554 return(0); 555 return(1); 556 } 557 if (key2->create_tid == 0) 558 return(-1); 559 if (key1->create_tid < key2->create_tid) 560 return(-1); 561 if (key1->create_tid > key2->create_tid) 562 return(1); 563 return(0); 564 } 565 566 static 567 int 568 test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe) 569 { 570 if (lbe) { 571 hammer_btree_elm_t rbe = lbe + 1; 572 if (hammer_btree_cmp(&elm->base, &lbe->base) < 0) 573 return(FLAG_TOOFARLEFT); 574 if (hammer_btree_cmp(&elm->base, &rbe->base) >= 0) 575 return(FLAG_TOOFARRIGHT); 576 } 577 return(0); 578 } 579 580 static 581 int 582 test_rbn_lr(hammer_btree_elm_t rbn, hammer_btree_elm_t lbe) 583 { 584 if (lbe) { 585 hammer_btree_elm_t rbe = lbe + 1; 586 if (hammer_btree_cmp(&rbn->base, &lbe->base) < 0) 587 return(FLAG_TOOFARLEFT); 588 if (hammer_btree_cmp(&rbn->base, &rbe->base) > 0) 589 return(FLAG_TOOFARRIGHT); 590 } 591 return(0); 592 } 593 594 static 595 void 596 print_bigblock_fill(hammer_off_t offset) 597 { 598 struct hammer_blockmap_layer1 layer1; 599 struct hammer_blockmap_layer2 layer2; 600 int fill; 601 int error; 602 603 blockmap_lookup_save(offset, &layer1, &layer2, &error); 604 printf("z%d:v%d:%d:%d:%lu=", 605 HAMMER_ZONE_DECODE(offset), 606 HAMMER_VOL_DECODE(offset), 607 HAMMER_BLOCKMAP_LAYER1_INDEX(offset), 608 HAMMER_BLOCKMAP_LAYER2_INDEX(offset), 609 offset & HAMMER_BIGBLOCK_MASK64); 610 611 if (error) { 612 printf("B%d", error); 613 } else { 614 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE; 615 printf("%d%%", 100 - fill); 616 } 617 } 618 619 /* 620 * Check the generic crc on a data element. Inodes record types are 621 * special in that some of their fields are not CRCed. 622 * 623 * Also check that the zone is valid. 624 */ 625 static 626 const char * 627 check_data_crc(hammer_btree_elm_t elm, const char **whichp) 628 { 629 struct buffer_info *data_buffer; 630 hammer_off_t data_offset; 631 int32_t data_len; 632 uint32_t crc; 633 char *ptr; 634 635 *whichp = ""; 636 data_offset = elm->leaf.data_offset; 637 data_len = elm->leaf.data_len; 638 data_buffer = NULL; 639 if (data_offset == 0 || data_len == 0) { 640 return("ZO"); /* zero offset or length */ 641 } 642 643 crc = 0; 644 switch (elm->leaf.base.rec_type) { 645 case HAMMER_RECTYPE_INODE: 646 if (data_len != sizeof(struct hammer_inode_data)) 647 return("BI"); /* bad inode size */ 648 ptr = get_buffer_data(data_offset, &data_buffer, 0); 649 crc = get_leaf_crc(HammerVersion, ptr, &elm->leaf, whichp); 650 rel_buffer(data_buffer); 651 break; 652 default: 653 crc = get_buf_crc(data_offset, data_len, elm->leaf.data_crc, 654 whichp); 655 break; 656 } 657 658 if (crc == 0) 659 return("Bx"); /* bad crc */ 660 if (crc != elm->leaf.data_crc) 661 return("BX"); /* bad crc */ 662 return(NULL); /* success */ 663 } 664 665 static 666 uint32_t 667 get_buf_crc(hammer_off_t buf_offset, int32_t buf_len, uint32_t leaf_crc, 668 const char **whichp) 669 { 670 struct buffer_info *data_buffer = NULL; 671 int32_t len; 672 uint32_t crc = 0; 673 uint32_t ncrc = 0; 674 char *ptr; 675 676 while (buf_len) { 677 ptr = get_buffer_data(buf_offset, &data_buffer, 0); 678 len = HAMMER_BUFSIZE - ((int)buf_offset & HAMMER_BUFMASK); 679 if (len > buf_len) 680 len = (int)buf_len; 681 assert(len <= HAMMER_BUFSIZE); 682 crc = crc32_ext(ptr, len, crc); 683 ncrc = iscsi_crc32_ext(ptr, len, ncrc); 684 buf_len -= len; 685 buf_offset += len; 686 } 687 rel_buffer(data_buffer); 688 689 if (leaf_crc == crc) { 690 *whichp = "o"; 691 return crc; 692 } 693 *whichp = "i"; 694 return ncrc; 695 } 696 697 static hammer_crc_t 698 get_leaf_crc(uint32_t vol_version, void *data, hammer_btree_leaf_elm_t leaf, 699 const char **whichp) 700 { 701 hammer_crc_t crc; 702 703 *whichp = ""; 704 if (leaf->data_len == 0) 705 return(0); 706 707 switch(leaf->base.rec_type) { 708 case HAMMER_RECTYPE_INODE: 709 if (leaf->data_len != sizeof(struct hammer_inode_data)) 710 return(0); /* This shouldn't happen */ 711 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) { 712 crc = iscsi_crc32(data, HAMMER_INODE_CRCSIZE); 713 if (crc == leaf->data_crc) { 714 *whichp = "i"; 715 break; 716 } 717 } 718 crc = crc32(data, HAMMER_INODE_CRCSIZE); 719 if (crc == leaf->data_crc) 720 *whichp = "o"; 721 break; 722 default: 723 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) { 724 crc = iscsi_crc32(data, leaf->data_len); 725 if (crc == leaf->data_crc) { 726 *whichp = "i"; 727 break; 728 } 729 } 730 crc = crc32(data, leaf->data_len); 731 if (crc == leaf->data_crc) 732 *whichp = "o"; 733 break; 734 } 735 return(crc); 736 } 737 738 static 739 void 740 print_config(char *cfgtxt) 741 { 742 char *token; 743 744 printf("\n%s%17s", INDENT, ""); 745 printf("config text=\"\n"); 746 if (cfgtxt != NULL) { 747 while((token = strsep(&cfgtxt, "\r\n")) != NULL) 748 if (strlen(token)) 749 printf("%s%17s %s\n", 750 INDENT, "", token); 751 } 752 printf("%s%17s \"", INDENT, ""); 753 } 754 755 static 756 void 757 print_record(hammer_btree_elm_t elm) 758 { 759 struct buffer_info *data_buffer; 760 hammer_off_t data_offset; 761 int32_t data_len; 762 hammer_data_ondisk_t data; 763 uint32_t status; 764 char *str1 = NULL; 765 char *str2 = NULL; 766 767 data_offset = elm->leaf.data_offset; 768 data_len = elm->leaf.data_len; 769 assert(data_offset != 0); 770 assert(data_len != 0); 771 772 data_buffer = NULL; 773 data = get_buffer_data(data_offset, &data_buffer, 0); 774 assert(data != NULL); 775 776 switch(elm->leaf.base.rec_type) { 777 case HAMMER_RECTYPE_UNKNOWN: 778 printf("\n%s%17s", INDENT, ""); 779 printf("unknown"); 780 break; 781 case HAMMER_RECTYPE_INODE: 782 printf("\n%s%17s", INDENT, ""); 783 printf("inode size=%jd nlinks=%jd", 784 (intmax_t)data->inode.size, 785 (intmax_t)data->inode.nlinks); 786 printf(" mode=%05o uflags=%08x caps=%02x", 787 data->inode.mode, 788 data->inode.uflags, 789 data->inode.cap_flags); 790 printf(" pobjid=%016jx ot=%02x\n", 791 (uintmax_t)data->inode.parent_obj_id, 792 data->inode.obj_type); 793 printf("%s%17s", INDENT, ""); 794 printf(" ctime=%016jx mtime=%016jx atime=%016jx", 795 (uintmax_t)data->inode.ctime, 796 (uintmax_t)data->inode.mtime, 797 (uintmax_t)data->inode.atime); 798 if (data->inode.ext.symlink[0]) 799 printf(" symlink=\"%s\"", 800 data->inode.ext.symlink); 801 break; 802 case HAMMER_RECTYPE_DIRENTRY: 803 data_len -= HAMMER_ENTRY_NAME_OFF; 804 printf("\n%s%17s", INDENT, ""); 805 printf("dir-entry objid=%016jx lo=%08x", 806 (uintmax_t)data->entry.obj_id, 807 data->entry.localization); 808 if (!opt.obfuscate) 809 printf(" name=\"%*.*s\"", 810 data_len, data_len, data->entry.name); 811 break; 812 case HAMMER_RECTYPE_FIX: 813 switch(elm->leaf.base.key) { 814 case HAMMER_FIXKEY_SYMLINK: 815 data_len -= HAMMER_SYMLINK_NAME_OFF; 816 printf("\n%s%17s", INDENT, ""); 817 printf("fix-symlink name=\"%*.*s\"", 818 data_len, data_len, data->symlink.name); 819 break; 820 } 821 break; 822 case HAMMER_RECTYPE_PFS: 823 printf("\n%s%17s", INDENT, ""); 824 printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n", 825 (intmax_t)data->pfsd.sync_beg_tid, 826 (intmax_t)data->pfsd.sync_end_tid); 827 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status); 828 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status); 829 printf("%17s", ""); 830 printf(" shared_uuid=%s\n", str1); 831 printf("%17s", ""); 832 printf(" unique_uuid=%s\n", str2); 833 printf("%17s", ""); 834 printf(" mirror_flags=%08x label=\"%s\"", 835 data->pfsd.mirror_flags, data->pfsd.label); 836 if (data->pfsd.snapshots[0]) 837 printf(" snapshots=\"%s\"", data->pfsd.snapshots); 838 free(str1); 839 free(str2); 840 break; 841 case HAMMER_RECTYPE_SNAPSHOT: 842 printf("\n%s%17s", INDENT, ""); 843 printf("snapshot tid=%016jx label=\"%s\"", 844 (intmax_t)data->snap.tid, data->snap.label); 845 break; 846 case HAMMER_RECTYPE_CONFIG: 847 if (VerboseOpt > 2) { 848 char *p = strdup(data->config.text); 849 print_config(p); 850 free(p); 851 } 852 break; 853 case HAMMER_RECTYPE_DATA: 854 if (VerboseOpt > 3) { 855 printf("\n"); 856 hexdump_record(data, data_len, "\t\t "); 857 } 858 break; 859 case HAMMER_RECTYPE_EXT: 860 case HAMMER_RECTYPE_DB: 861 if (VerboseOpt > 2) { 862 printf("\n"); 863 hexdump_record(data, data_len, "\t\t "); 864 } 865 break; 866 default: 867 assert(0); 868 break; 869 } 870 rel_buffer(data_buffer); 871 } 872 873 /* 874 * HAMMER userspace only supports buffer size upto HAMMER_BUFSIZE 875 * which is 16KB. Passing record data length larger than 16KB to 876 * hexdump(3) is invalid even if the leaf node elm says >16KB data. 877 */ 878 static void 879 hexdump_record(const void *ptr, int length, const char *hdr) 880 { 881 int data_len = length; 882 883 if (data_len > HAMMER_BUFSIZE) /* XXX */ 884 data_len = HAMMER_BUFSIZE; 885 hexdump(ptr, data_len, hdr, 0); 886 887 if (length > data_len) 888 printf("%s....\n", hdr); 889 } 890 891 static __inline __always_inline 892 unsigned long 893 _strtoul(const char *p, int base) 894 { 895 unsigned long retval; 896 897 errno = 0; /* clear */ 898 retval = strtoul(p, NULL, base); 899 if (errno == ERANGE && retval == ULONG_MAX) 900 err(1, "strtoul"); 901 return retval; 902 } 903 904 static __inline __always_inline 905 unsigned long long 906 _strtoull(const char *p, int base) 907 { 908 unsigned long long retval; 909 910 errno = 0; /* clear */ 911 retval = strtoull(p, NULL, base); 912 if (errno == ERANGE && retval == ULLONG_MAX) 913 err(1, "strtoull"); 914 return retval; 915 } 916 917 static int 918 init_btree_search(const char *arg) 919 { 920 char *s, *p; 921 int i = 0; 922 923 bzero(&opt.base, sizeof(opt.base)); 924 opt.limit = 0; 925 926 if (arg == NULL) 927 return(-1); 928 if (strcmp(arg, "none") == 0) 929 return(-1); 930 931 s = strdup(arg); 932 if (s == NULL) 933 return(-1); 934 935 while ((p = s) != NULL) { 936 if ((s = strchr(s, ':')) != NULL) 937 *s++ = 0; 938 if (++i == 1) { 939 opt.base.localization = _strtoul(p, 16); 940 } else if (i == 2) { 941 opt.base.obj_id = _strtoull(p, 16); 942 } else if (i == 3) { 943 opt.base.rec_type = _strtoul(p, 16); 944 } else if (i == 4) { 945 opt.base.key = _strtoull(p, 16); 946 } else if (i == 5) { 947 opt.base.create_tid = _strtoull(p, 16); 948 break; 949 } 950 } 951 opt.limit = i; 952 free(s); 953 954 return(i); 955 } 956 957 static int 958 test_btree_search(hammer_btree_elm_t elm) 959 { 960 hammer_base_elm_t base1 = &elm->base; 961 hammer_base_elm_t base2 = &opt.base; 962 int limit = opt.limit; 963 964 if (base1->localization < base2->localization) 965 return(-5); 966 if (base1->localization > base2->localization) 967 return(5); 968 if (limit == 1) 969 return(0); /* ignore below */ 970 971 if (base1->obj_id < base2->obj_id) 972 return(-4); 973 if (base1->obj_id > base2->obj_id) 974 return(4); 975 if (limit == 2) 976 return(0); /* ignore below */ 977 978 if (base1->rec_type < base2->rec_type) 979 return(-3); 980 if (base1->rec_type > base2->rec_type) 981 return(3); 982 if (limit == 3) 983 return(0); /* ignore below */ 984 985 if (base1->key < base2->key) 986 return(-2); 987 if (base1->key > base2->key) 988 return(2); 989 if (limit == 4) 990 return(0); /* ignore below */ 991 992 if (base1->create_tid == 0) { 993 if (base2->create_tid == 0) 994 return(0); 995 return(1); 996 } 997 if (base2->create_tid == 0) 998 return(-1); 999 if (base1->create_tid < base2->create_tid) 1000 return(-1); 1001 if (base1->create_tid > base2->create_tid) 1002 return(1); 1003 return(0); 1004 } 1005 1006 static __inline 1007 int 1008 test_btree_match(hammer_btree_elm_t elm) 1009 { 1010 if (test_btree_search(elm) == 0) 1011 return(1); 1012 return(0); 1013 } 1014 1015 static 1016 int 1017 test_btree_out_of_range(hammer_btree_elm_t elm) 1018 { 1019 if (test_btree_search(elm) > 0) 1020 return(1); /* conditions < this elm */ 1021 1022 if (opt.limit >= 5) { 1023 if (test_btree_search(elm + 1) <= 0) 1024 return(1); /* next elm <= conditions */ 1025 } else { 1026 if (test_btree_search(elm + 1) < 0) 1027 return(1); /* next elm < conditions */ 1028 } 1029 return(0); 1030 } 1031 1032 /* 1033 * Dump the UNDO FIFO 1034 */ 1035 void 1036 hammer_cmd_show_undo(void) 1037 { 1038 struct volume_info *volume; 1039 hammer_blockmap_t rootmap; 1040 hammer_off_t scan_offset; 1041 hammer_fifo_any_t head; 1042 hammer_fifo_head_t hdr; 1043 struct buffer_info *data_buffer = NULL; 1044 struct zone_stat *stats = NULL; 1045 1046 volume = get_root_volume(); 1047 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX]; 1048 1049 print_blockmap(volume); 1050 1051 if (VerboseOpt) 1052 stats = hammer_init_zone_stat_bits(); 1053 1054 scan_offset = HAMMER_ENCODE_UNDO(0); 1055 while (scan_offset < rootmap->alloc_offset) { 1056 head = get_buffer_data(scan_offset, &data_buffer, 0); 1057 hdr = &head->head; 1058 printf("%016jx ", scan_offset); 1059 1060 switch(hdr->hdr_type) { 1061 case HAMMER_HEAD_TYPE_PAD: 1062 printf("PAD(%d)", hdr->hdr_size); 1063 break; 1064 case HAMMER_HEAD_TYPE_DUMMY: 1065 printf("DUMMY(%d)\tseq=%08x", 1066 hdr->hdr_size, hdr->hdr_seq); 1067 break; 1068 case HAMMER_HEAD_TYPE_UNDO: 1069 printf("UNDO(%d)\tseq=%08x offset=%016jx bytes=%d", 1070 hdr->hdr_size, hdr->hdr_seq, 1071 (intmax_t)head->undo.undo_offset, 1072 head->undo.undo_data_bytes); 1073 break; 1074 case HAMMER_HEAD_TYPE_REDO: 1075 printf("REDO(%d)\tseq=%08x offset=%016jx bytes=%d " 1076 "objid=%016jx flags=%08x lo=%08x", 1077 hdr->hdr_size, hdr->hdr_seq, 1078 (intmax_t)head->redo.redo_offset, 1079 head->redo.redo_data_bytes, 1080 (intmax_t)head->redo.redo_objid, 1081 head->redo.redo_flags, 1082 head->redo.redo_localization); 1083 break; 1084 default: 1085 printf("%04x(%d)\tseq=%08x", 1086 hdr->hdr_type, hdr->hdr_size, hdr->hdr_seq); 1087 break; 1088 } 1089 1090 if (scan_offset == rootmap->first_offset) 1091 printf(" >"); 1092 if (scan_offset == rootmap->next_offset) 1093 printf(" <"); 1094 printf("\n"); 1095 1096 if (stats) 1097 hammer_add_zone_stat(stats, scan_offset, hdr->hdr_size); 1098 1099 if ((hdr->hdr_size & HAMMER_HEAD_ALIGN_MASK) || 1100 hdr->hdr_size == 0 || 1101 hdr->hdr_size > HAMMER_UNDO_ALIGN - 1102 ((u_int)scan_offset & HAMMER_UNDO_MASK)) { 1103 printf("Illegal size field, skipping to " 1104 "next boundary\n"); 1105 scan_offset = HAMMER_UNDO_DOALIGN(scan_offset); 1106 } else { 1107 scan_offset += hdr->hdr_size; 1108 } 1109 } 1110 rel_buffer(data_buffer); 1111 1112 if (stats) { 1113 hammer_print_zone_stat(stats); 1114 hammer_cleanup_zone_stat(stats); 1115 } 1116 } 1117