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 * $DragonFly: src/sbin/hammer/cmd_show.c,v 1.18 2008/10/09 04:20:59 dillon Exp $ 35 */ 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 44 typedef struct btree_search { 45 u_int32_t lo; 46 int64_t obj_id; 47 } *btree_search_t; 48 49 static void print_btree_node(hammer_off_t node_offset, btree_search_t search, 50 int depth, int spike, 51 hammer_base_elm_t left_bound, 52 hammer_base_elm_t right_bound); 53 static const char *check_data_crc(hammer_btree_elm_t elm); 54 static void print_record(hammer_btree_elm_t elm); 55 static void print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type, 56 int flags, const char *label, const char *ext); 57 static int print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset, 58 hammer_btree_elm_t elm, u_int8_t btype, 59 hammer_base_elm_t left_bound, 60 hammer_base_elm_t right_bound); 61 static void print_bigblock_fill(hammer_off_t offset); 62 63 void 64 hammer_cmd_show(hammer_off_t node_offset, u_int32_t lo, int64_t obj_id, 65 int depth, 66 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound) 67 { 68 struct volume_info *volume; 69 struct btree_search search; 70 btree_search_t searchp; 71 int zone; 72 73 if (node_offset == (hammer_off_t)-1) { 74 volume = get_volume(RootVolNo); 75 node_offset = volume->ondisk->vol0_btree_root; 76 if (QuietOpt < 3) { 77 printf("Volume header\trecords=%jd next_tid=%016jx\n", 78 (intmax_t)volume->ondisk->vol0_stat_records, 79 (uintmax_t)volume->ondisk->vol0_next_tid); 80 printf("\t\tbufoffset=%016jx\n", 81 (uintmax_t)volume->ondisk->vol_buf_beg); 82 for (zone = 0; zone < HAMMER_MAX_ZONES; ++zone) { 83 printf("\t\tzone %d\tnext_offset=%016jx\n", 84 zone, 85 (uintmax_t)volume->ondisk->vol0_blockmap[zone].next_offset 86 ); 87 } 88 } 89 rel_volume(volume); 90 } 91 92 if (lo == 0 && obj_id == (int64_t)HAMMER_MIN_OBJID) { 93 searchp = NULL; 94 printf("show %016jx depth %d\n", (uintmax_t)node_offset, depth); 95 } else { 96 search.lo = lo; 97 search.obj_id = obj_id; 98 searchp = &search; 99 printf("show %016jx lo %08x obj_id %016jx depth %d\n", 100 (uintmax_t)node_offset, lo, (uintmax_t)obj_id, depth); 101 } 102 print_btree_node(node_offset, searchp, depth, 103 0, left_bound, right_bound); 104 print_btree_node(node_offset, searchp, depth, 105 1, left_bound, right_bound); 106 } 107 108 static void 109 print_btree_node(hammer_off_t node_offset, btree_search_t search, 110 int depth, int spike, 111 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound) 112 { 113 struct buffer_info *buffer = NULL; 114 hammer_node_ondisk_t node; 115 hammer_btree_elm_t elm; 116 int i; 117 int flags; 118 int maxcount; 119 char badc; 120 const char *ext; 121 122 node = get_node(node_offset, &buffer); 123 124 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc) 125 badc = ' '; 126 else 127 badc = 'B'; 128 129 if (spike == 0) { 130 printf("%c NODE %016jx cnt=%02d p=%016jx " 131 "type=%c depth=%d", 132 badc, 133 (uintmax_t)node_offset, node->count, 134 (uintmax_t)node->parent, 135 (node->type ? node->type : '?'), depth); 136 printf(" mirror %016jx", (uintmax_t)node->mirror_tid); 137 if (QuietOpt < 3) { 138 printf(" fill="); 139 print_bigblock_fill(node_offset); 140 } 141 printf(" {\n"); 142 143 maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ? 144 HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS; 145 146 for (i = 0; i < node->count && i < maxcount; ++i) { 147 elm = &node->elms[i]; 148 149 if (node->type != HAMMER_BTREE_TYPE_INTERNAL) { 150 ext = NULL; 151 if (search && 152 elm->base.localization == search->lo && 153 elm->base.obj_id == search->obj_id) { 154 ext = " *"; 155 } 156 } else if (search) { 157 ext = " *"; 158 if (elm->base.localization > search->lo || 159 (elm->base.localization == search->lo && 160 elm->base.obj_id > search->obj_id)) { 161 ext = NULL; 162 } 163 if (elm[1].base.localization < search->lo || 164 (elm[1].base.localization == search->lo && 165 elm[1].base.obj_id < search->obj_id)) { 166 ext = NULL; 167 } 168 } else { 169 ext = NULL; 170 } 171 172 flags = print_elm_flags(node, node_offset, 173 elm, elm->base.btype, 174 left_bound, right_bound); 175 print_btree_elm(elm, i, node->type, flags, "ELM", ext); 176 } 177 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) { 178 elm = &node->elms[i]; 179 180 flags = print_elm_flags(node, node_offset, 181 elm, 'I', 182 left_bound, right_bound); 183 print_btree_elm(elm, i, node->type, flags, "RBN", NULL); 184 } 185 printf(" }\n"); 186 } 187 188 for (i = 0; i < node->count; ++i) { 189 elm = &node->elms[i]; 190 191 switch(node->type) { 192 case HAMMER_BTREE_TYPE_INTERNAL: 193 if (search) { 194 if (elm->base.localization > search->lo || 195 (elm->base.localization == search->lo && 196 elm->base.obj_id > search->obj_id)) { 197 break; 198 } 199 if (elm[1].base.localization < search->lo || 200 (elm[1].base.localization == search->lo && 201 elm[1].base.obj_id < search->obj_id)) { 202 break; 203 } 204 } 205 if (elm->internal.subtree_offset) { 206 print_btree_node(elm->internal.subtree_offset, 207 search, depth + 1, spike, 208 &elm[0].base, &elm[1].base); 209 /* 210 * Cause show to iterate after seeking to 211 * the lo:objid 212 */ 213 search = NULL; 214 } 215 break; 216 default: 217 break; 218 } 219 } 220 rel_buffer(buffer); 221 } 222 223 static 224 void 225 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type, 226 int flags, const char *label, const char *ext) 227 { 228 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 }; 229 230 flagstr[0] = flags ? 'B' : 'G'; 231 if (flags & FLAG_TOOFARLEFT) 232 flagstr[2] = 'L'; 233 if (flags & FLAG_TOOFARRIGHT) 234 flagstr[3] = 'R'; 235 if (flags & FLAG_BADTYPE) 236 flagstr[4] = 'T'; 237 if (flags & FLAG_BADCHILDPARENT) 238 flagstr[4] = 'C'; 239 240 printf("%s\t%s %2d %c ", 241 flagstr, label, i, 242 (elm->base.btype ? elm->base.btype : '?')); 243 printf("obj=%016jx key=%016jx lo=%08x rt=%02x ot=%02x\n", 244 (uintmax_t)elm->base.obj_id, 245 (uintmax_t)elm->base.key, 246 elm->base.localization, 247 elm->base.rec_type, 248 elm->base.obj_type); 249 printf("\t %c tids %016jx:%016jx ", 250 (elm->base.delete_tid ? 'd' : ' '), 251 (uintmax_t)elm->base.create_tid, 252 (uintmax_t)elm->base.delete_tid); 253 254 switch(type) { 255 case HAMMER_BTREE_TYPE_INTERNAL: 256 printf("suboff=%016jx", 257 (uintmax_t)elm->internal.subtree_offset); 258 if (QuietOpt < 3) { 259 printf(" mirror %016jx", 260 (uintmax_t)elm->internal.mirror_tid); 261 } 262 if (ext) 263 printf(" %s", ext); 264 break; 265 case HAMMER_BTREE_TYPE_LEAF: 266 if (ext) 267 printf(" %s", ext); 268 switch(elm->base.btype) { 269 case HAMMER_BTREE_TYPE_RECORD: 270 if (QuietOpt < 3) 271 printf("\n%s\t ", check_data_crc(elm)); 272 else 273 printf("\n\t "); 274 printf("dataoff=%016jx/%d", 275 (uintmax_t)elm->leaf.data_offset, 276 elm->leaf.data_len); 277 if (QuietOpt < 3) { 278 printf(" crc=%04x", elm->leaf.data_crc); 279 printf("\n\t fills="); 280 print_bigblock_fill(elm->leaf.data_offset); 281 } 282 if (QuietOpt < 2) 283 print_record(elm); 284 break; 285 } 286 break; 287 default: 288 break; 289 } 290 printf("\n"); 291 } 292 293 static 294 int 295 print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset, 296 hammer_btree_elm_t elm, u_int8_t btype, 297 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound) 298 { 299 int flags = 0; 300 301 switch(node->type) { 302 case HAMMER_BTREE_TYPE_INTERNAL: 303 if (elm->internal.subtree_offset) { 304 struct buffer_info *buffer = NULL; 305 hammer_node_ondisk_t subnode; 306 307 subnode = get_node(elm->internal.subtree_offset, 308 &buffer); 309 if (subnode->parent != node_offset) 310 flags |= FLAG_BADCHILDPARENT; 311 rel_buffer(buffer); 312 } 313 314 switch(btype) { 315 case HAMMER_BTREE_TYPE_INTERNAL: 316 if (left_bound == NULL || right_bound == NULL) 317 break; 318 if (hammer_btree_cmp(&elm->base, left_bound) < 0) 319 flags |= FLAG_TOOFARLEFT; 320 if (hammer_btree_cmp(&elm->base, right_bound) > 0) 321 flags |= FLAG_TOOFARRIGHT; 322 break; 323 case HAMMER_BTREE_TYPE_LEAF: 324 if (left_bound == NULL || right_bound == NULL) 325 break; 326 if (hammer_btree_cmp(&elm->base, left_bound) < 0) 327 flags |= FLAG_TOOFARLEFT; 328 if (hammer_btree_cmp(&elm->base, right_bound) >= 0) 329 flags |= FLAG_TOOFARRIGHT; 330 break; 331 default: 332 flags |= FLAG_BADTYPE; 333 break; 334 } 335 break; 336 case HAMMER_BTREE_TYPE_LEAF: 337 switch(btype) { 338 case HAMMER_BTREE_TYPE_RECORD: 339 if (left_bound == NULL || right_bound == NULL) 340 break; 341 if (hammer_btree_cmp(&elm->base, left_bound) < 0) 342 flags |= FLAG_TOOFARLEFT; 343 if (hammer_btree_cmp(&elm->base, right_bound) >= 0) 344 flags |= FLAG_TOOFARRIGHT; 345 break; 346 default: 347 flags |= FLAG_BADTYPE; 348 break; 349 } 350 break; 351 default: 352 flags |= FLAG_BADTYPE; 353 break; 354 } 355 return(flags); 356 } 357 358 static 359 void 360 print_bigblock_fill(hammer_off_t offset) 361 { 362 struct hammer_blockmap_layer1 layer1; 363 struct hammer_blockmap_layer2 layer2; 364 int fill; 365 366 blockmap_lookup(offset, &layer1, &layer2); 367 fill = layer2.bytes_free * 100 / HAMMER_LARGEBLOCK_SIZE; 368 fill = 100 - fill; 369 370 printf("z%d:%lld=%d%%", 371 HAMMER_ZONE_DECODE(offset), 372 (offset & ~HAMMER_OFF_ZONE_MASK) / HAMMER_LARGEBLOCK_SIZE, 373 fill 374 ); 375 } 376 377 /* 378 * Check the generic crc on a data element. Inodes record types are 379 * special in that some of their fields are not CRCed. 380 */ 381 static 382 const char * 383 check_data_crc(hammer_btree_elm_t elm) 384 { 385 struct buffer_info *data_buffer; 386 hammer_off_t data_offset; 387 int32_t data_len; 388 int32_t len; 389 u_int32_t crc; 390 char *ptr; 391 392 data_offset = elm->leaf.data_offset; 393 data_len = elm->leaf.data_len; 394 data_buffer = NULL; 395 if (data_offset == 0 || data_len == 0) 396 return("Z"); 397 398 crc = 0; 399 while (data_len) { 400 ptr = get_buffer_data(data_offset, &data_buffer, 0); 401 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK); 402 if (len > data_len) 403 len = (int)data_len; 404 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE && 405 data_len == sizeof(struct hammer_inode_data)) { 406 crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc); 407 } else { 408 crc = crc32_ext(ptr, len, crc); 409 } 410 data_len -= len; 411 data_offset += len; 412 } 413 if (data_buffer) 414 rel_buffer(data_buffer); 415 if (crc == elm->leaf.data_crc) 416 return(""); 417 return("B"); 418 } 419 420 static 421 void 422 print_record(hammer_btree_elm_t elm) 423 { 424 struct buffer_info *data_buffer; 425 hammer_off_t data_offset; 426 int32_t data_len; 427 hammer_data_ondisk_t data; 428 429 data_offset = elm->leaf.data_offset; 430 data_len = elm->leaf.data_len; 431 data_buffer = NULL; 432 433 if (data_offset) 434 data = get_buffer_data(data_offset, &data_buffer, 0); 435 else 436 data = NULL; 437 438 switch(elm->leaf.base.rec_type) { 439 case HAMMER_RECTYPE_INODE: 440 printf("\n%17s", ""); 441 printf("size=%jd nlinks=%jd", 442 (intmax_t)data->inode.size, 443 (intmax_t)data->inode.nlinks); 444 if (QuietOpt < 1) { 445 printf(" mode=%05o uflags=%08x\n", 446 data->inode.mode, 447 data->inode.uflags); 448 printf("%17s", ""); 449 printf("ctime=%016jx pobjid=%016jx obj_type=%d\n", 450 (uintmax_t)data->inode.ctime, 451 (uintmax_t)data->inode.parent_obj_id, 452 data->inode.obj_type); 453 printf("%17s", ""); 454 printf("mtime=%016jx", (uintmax_t)data->inode.mtime); 455 printf(" caps=%02x", data->inode.cap_flags); 456 } 457 break; 458 case HAMMER_RECTYPE_DIRENTRY: 459 printf("\n%17s", ""); 460 data_len -= HAMMER_ENTRY_NAME_OFF; 461 printf("dir-entry ino=%016jx lo=%08x name=\"%*.*s\"", 462 (uintmax_t)data->entry.obj_id, 463 data->entry.localization, 464 data_len, data_len, data->entry.name); 465 break; 466 case HAMMER_RECTYPE_FIX: 467 switch(elm->leaf.base.key) { 468 case HAMMER_FIXKEY_SYMLINK: 469 data_len -= HAMMER_SYMLINK_NAME_OFF; 470 printf("\n%17s", ""); 471 printf("symlink=\"%*.*s\"", data_len, data_len, 472 data->symlink.name); 473 break; 474 default: 475 break; 476 } 477 break; 478 default: 479 break; 480 } 481 if (data_buffer) 482 rel_buffer(data_buffer); 483 } 484 485