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_blockmap.c,v 1.4 2008/07/19 18:48:14 dillon Exp $ 35 */ 36 37 #include "hammer.h" 38 39 /* 40 * Each collect covers 1<<(19+23) bytes address space of layer 1. 41 * (plus a copy of 1<<23 bytes that holds layer2 entries in layer 1). 42 */ 43 typedef struct collect { 44 RB_ENTRY(collect) entry; 45 hammer_off_t phys_offset; /* layer2 address pointed by layer1 */ 46 struct hammer_blockmap_layer2 *track2; /* track of layer2 entries */ 47 struct hammer_blockmap_layer2 *layer2; /* 1<<19 x 16 bytes entries */ 48 int error; /* # of inconsistencies */ 49 } *collect_t; 50 51 static int 52 collect_compare(struct collect *c1, struct collect *c2) 53 { 54 if (c1->phys_offset < c2->phys_offset) 55 return(-1); 56 if (c1->phys_offset > c2->phys_offset) 57 return(1); 58 return(0); 59 } 60 61 RB_HEAD(collect_rb_tree, collect) CollectTree = RB_INITIALIZER(&CollectTree); 62 RB_PROTOTYPE2(collect_rb_tree, collect, entry, collect_compare, hammer_off_t); 63 RB_GENERATE2(collect_rb_tree, collect, entry, collect_compare, hammer_off_t, 64 phys_offset); 65 66 static void dump_blockmap(const char *label, int zone); 67 static void check_btree_node(hammer_off_t node_offset, int depth); 68 static void check_undo(hammer_blockmap_t rootmap); 69 static __inline void collect_btree_root(hammer_off_t node_offset); 70 static __inline void collect_btree_internal(hammer_btree_elm_t elm); 71 static __inline void collect_btree_leaf(hammer_btree_elm_t elm); 72 static __inline void collect_undo(hammer_off_t scan_offset, 73 hammer_fifo_head_t head); 74 static void collect_blockmap(hammer_off_t offset, int32_t length, int zone); 75 static struct hammer_blockmap_layer2 *collect_get_track( 76 collect_t collect, hammer_off_t offset, int zone, 77 struct hammer_blockmap_layer2 *layer2); 78 static collect_t collect_get(hammer_off_t phys_offset); 79 static void dump_collect_table(void); 80 static void dump_collect(collect_t collect, int *stats); 81 82 void 83 hammer_cmd_blockmap(void) 84 { 85 dump_blockmap("freemap", HAMMER_ZONE_FREEMAP_INDEX); 86 } 87 88 static 89 void 90 dump_blockmap(const char *label, int zone) 91 { 92 struct volume_info *root_volume; 93 hammer_blockmap_t rootmap; 94 struct hammer_blockmap_layer1 *layer1; 95 struct hammer_blockmap_layer2 *layer2; 96 struct buffer_info *buffer1 = NULL; 97 struct buffer_info *buffer2 = NULL; 98 hammer_off_t layer1_offset; 99 hammer_off_t layer2_offset; 100 hammer_off_t scan1; 101 hammer_off_t scan2; 102 int xerr; 103 104 assert(RootVolNo >= 0); 105 root_volume = get_volume(RootVolNo); 106 rootmap = &root_volume->ondisk->vol0_blockmap[zone]; 107 assert(rootmap->phys_offset != 0); 108 109 printf("zone %-16s next %016jx alloc %016jx\n", 110 label, 111 (uintmax_t)rootmap->next_offset, 112 (uintmax_t)rootmap->alloc_offset); 113 114 for (scan1 = HAMMER_ZONE_ENCODE(zone, 0); 115 scan1 < HAMMER_ZONE_ENCODE(zone, HAMMER_OFF_LONG_MASK); 116 scan1 += HAMMER_BLOCKMAP_LAYER2) { 117 /* 118 * Dive layer 1. 119 */ 120 layer1_offset = rootmap->phys_offset + 121 HAMMER_BLOCKMAP_LAYER1_OFFSET(scan1); 122 layer1 = get_buffer_data(layer1_offset, &buffer1, 0); 123 xerr = ' '; 124 if (layer1->layer1_crc != crc32(layer1, HAMMER_LAYER1_CRCSIZE)) 125 xerr = 'B'; 126 if (xerr == ' ' && 127 layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) { 128 continue; 129 } 130 printf("%c layer1 %016jx @%016jx blocks-free %jd\n", 131 xerr, 132 (uintmax_t)scan1, 133 (uintmax_t)layer1->phys_offset, 134 (intmax_t)layer1->blocks_free); 135 if (layer1->phys_offset == HAMMER_BLOCKMAP_FREE) 136 continue; 137 for (scan2 = scan1; 138 scan2 < scan1 + HAMMER_BLOCKMAP_LAYER2; 139 scan2 += HAMMER_BIGBLOCK_SIZE 140 ) { 141 /* 142 * Dive layer 2, each entry represents a big-block. 143 */ 144 layer2_offset = layer1->phys_offset + 145 HAMMER_BLOCKMAP_LAYER2_OFFSET(scan2); 146 layer2 = get_buffer_data(layer2_offset, &buffer2, 0); 147 xerr = ' '; 148 if (layer2->entry_crc != crc32(layer2, HAMMER_LAYER2_CRCSIZE)) 149 xerr = 'B'; 150 printf("%c %016jx zone=%d app=%-7d free=%-7d\n", 151 xerr, 152 (uintmax_t)scan2, 153 layer2->zone, 154 layer2->append_off, 155 layer2->bytes_free); 156 } 157 } 158 rel_buffer(buffer1); 159 rel_buffer(buffer2); 160 rel_volume(root_volume); 161 } 162 163 void 164 hammer_cmd_checkmap(void) 165 { 166 struct volume_info *volume; 167 hammer_blockmap_t rootmap; 168 hammer_off_t node_offset; 169 170 volume = get_volume(RootVolNo); 171 node_offset = volume->ondisk->vol0_btree_root; 172 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX]; 173 174 if (QuietOpt < 3) { 175 printf("Volume header\trecords=%jd next_tid=%016jx\n", 176 (intmax_t)volume->ondisk->vol0_stat_records, 177 (uintmax_t)volume->ondisk->vol0_next_tid); 178 printf("\t\tbufoffset=%016jx\n", 179 (uintmax_t)volume->ondisk->vol_buf_beg); 180 printf("\t\tundosize=%jdMB\n", 181 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) 182 / (1024 * 1024))); 183 } 184 rel_volume(volume); 185 186 AssertOnFailure = 0; 187 188 printf("Collecting allocation info from B-Tree: "); 189 fflush(stdout); 190 collect_btree_root(node_offset); 191 check_btree_node(node_offset, 0); 192 printf("done\n"); 193 194 printf("Collecting allocation info from UNDO: "); 195 fflush(stdout); 196 check_undo(rootmap); 197 printf("done\n"); 198 199 dump_collect_table(); 200 AssertOnFailure = 1; 201 } 202 203 static void 204 check_btree_node(hammer_off_t node_offset, int depth) 205 { 206 struct buffer_info *buffer = NULL; 207 hammer_node_ondisk_t node; 208 hammer_btree_elm_t elm; 209 int i; 210 char badc; 211 212 node = get_node(node_offset, &buffer); 213 214 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc) 215 badc = ' '; 216 else 217 badc = 'B'; 218 219 if (badc != ' ') { 220 printf("%c NODE %016jx cnt=%02d p=%016jx " 221 "type=%c depth=%d", 222 badc, 223 (uintmax_t)node_offset, node->count, 224 (uintmax_t)node->parent, 225 (node->type ? node->type : '?'), depth); 226 printf(" mirror %016jx\n", (uintmax_t)node->mirror_tid); 227 } 228 229 for (i = 0; i < node->count; ++i) { 230 elm = &node->elms[i]; 231 232 switch(node->type) { 233 case HAMMER_BTREE_TYPE_INTERNAL: 234 if (elm->internal.subtree_offset) { 235 collect_btree_internal(elm); 236 check_btree_node(elm->internal.subtree_offset, 237 depth + 1); 238 } 239 break; 240 case HAMMER_BTREE_TYPE_LEAF: 241 if (elm->leaf.data_offset) 242 collect_btree_leaf(elm); 243 break; 244 default: 245 if (AssertOnFailure) 246 assert(0); 247 break; 248 } 249 } 250 rel_buffer(buffer); 251 } 252 253 static void 254 check_undo(hammer_blockmap_t rootmap) 255 { 256 struct buffer_info *buffer = NULL; 257 hammer_off_t scan_offset; 258 hammer_fifo_head_t head; 259 260 scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0); 261 while (scan_offset < rootmap->alloc_offset) { 262 head = get_buffer_data(scan_offset, &buffer, 0); 263 switch (head->hdr_type) { 264 case HAMMER_HEAD_TYPE_PAD: 265 case HAMMER_HEAD_TYPE_DUMMY: 266 case HAMMER_HEAD_TYPE_UNDO: 267 case HAMMER_HEAD_TYPE_REDO: 268 collect_undo(scan_offset, head); 269 break; 270 default: 271 if (AssertOnFailure) 272 assert(0); 273 break; 274 } 275 if ((head->hdr_size & HAMMER_HEAD_ALIGN_MASK) || 276 head->hdr_size == 0 || 277 head->hdr_size > HAMMER_UNDO_ALIGN - 278 ((u_int)scan_offset & HAMMER_UNDO_MASK)) { 279 printf("Illegal size, skipping to next boundary\n"); 280 scan_offset = (scan_offset + HAMMER_UNDO_MASK) & 281 ~HAMMER_UNDO_MASK64; 282 } else { 283 scan_offset += head->hdr_size; 284 } 285 } 286 rel_buffer(buffer); 287 } 288 289 static __inline 290 void 291 collect_btree_root(hammer_off_t node_offset) 292 { 293 collect_blockmap(node_offset, 294 sizeof(struct hammer_node_ondisk), /* 4KB */ 295 HAMMER_ZONE_BTREE_INDEX); 296 } 297 298 static __inline 299 void 300 collect_btree_internal(hammer_btree_elm_t elm) 301 { 302 collect_blockmap(elm->internal.subtree_offset, 303 sizeof(struct hammer_node_ondisk), /* 4KB */ 304 HAMMER_ZONE_BTREE_INDEX); 305 } 306 307 static __inline 308 void 309 collect_btree_leaf(hammer_btree_elm_t elm) 310 { 311 int zone; 312 313 switch (elm->base.rec_type) { 314 case HAMMER_RECTYPE_INODE: 315 case HAMMER_RECTYPE_DIRENTRY: 316 case HAMMER_RECTYPE_EXT: 317 case HAMMER_RECTYPE_FIX: 318 case HAMMER_RECTYPE_PFS: 319 case HAMMER_RECTYPE_SNAPSHOT: 320 case HAMMER_RECTYPE_CONFIG: 321 zone = HAMMER_ZONE_META_INDEX; 322 break; 323 case HAMMER_RECTYPE_DATA: 324 case HAMMER_RECTYPE_DB: 325 /* 326 * There is an exceptional case where HAMMER uses 327 * HAMMER_ZONE_LARGE_DATA when the data length is 328 * >HAMMER_BUFSIZE/2 (not >=HAMMER_BUFSIZE). 329 * This exceptional case is currently being used 330 * by mirror write code, however the following code 331 * can ignore that and simply use the normal way 332 * of selecting a zone using >=HAMMER_BUFSIZE. 333 * See hammer_alloc_data() for details. 334 */ 335 zone = elm->leaf.data_len >= HAMMER_BUFSIZE ? 336 HAMMER_ZONE_LARGE_DATA_INDEX : 337 HAMMER_ZONE_SMALL_DATA_INDEX; 338 break; 339 default: 340 zone = HAMMER_ZONE_UNAVAIL_INDEX; 341 break; 342 } 343 collect_blockmap(elm->leaf.data_offset, 344 (elm->leaf.data_len + 15) & ~15, zone); 345 } 346 347 static __inline 348 void 349 collect_undo(hammer_off_t scan_offset, hammer_fifo_head_t head) 350 { 351 collect_blockmap(scan_offset, head->hdr_size, 352 HAMMER_ZONE_UNDO_INDEX); 353 } 354 355 static 356 void 357 collect_blockmap(hammer_off_t offset, int32_t length, int zone) 358 { 359 struct hammer_blockmap_layer1 layer1; 360 struct hammer_blockmap_layer2 layer2; 361 struct hammer_blockmap_layer2 *track2; 362 hammer_off_t result_offset; 363 collect_t collect; 364 int error; 365 366 result_offset = blockmap_lookup(offset, &layer1, &layer2, &error); 367 if (AssertOnFailure) { 368 assert(HAMMER_ZONE_DECODE(offset) == zone); 369 assert(HAMMER_ZONE_DECODE(result_offset) == 370 HAMMER_ZONE_RAW_BUFFER_INDEX); 371 assert(error == 0); 372 } 373 collect = collect_get(layer1.phys_offset); /* layer2 address */ 374 track2 = collect_get_track(collect, offset, zone, &layer2); 375 track2->bytes_free -= length; 376 } 377 378 static 379 collect_t 380 collect_get(hammer_off_t phys_offset) 381 { 382 collect_t collect; 383 384 collect = RB_LOOKUP(collect_rb_tree, &CollectTree, phys_offset); 385 if (collect) 386 return(collect); 387 388 collect = calloc(sizeof(*collect), 1); 389 collect->track2 = malloc(HAMMER_BIGBLOCK_SIZE); /* 1<<23 bytes */ 390 collect->layer2 = malloc(HAMMER_BIGBLOCK_SIZE); /* 1<<23 bytes */ 391 collect->phys_offset = phys_offset; 392 RB_INSERT(collect_rb_tree, &CollectTree, collect); 393 bzero(collect->track2, HAMMER_BIGBLOCK_SIZE); 394 bzero(collect->layer2, HAMMER_BIGBLOCK_SIZE); 395 396 return (collect); 397 } 398 399 static 400 void 401 collect_rel(collect_t collect) 402 { 403 free(collect->layer2); 404 free(collect->track2); 405 free(collect); 406 } 407 408 static 409 struct hammer_blockmap_layer2 * 410 collect_get_track(collect_t collect, hammer_off_t offset, int zone, 411 struct hammer_blockmap_layer2 *layer2) 412 { 413 struct hammer_blockmap_layer2 *track2; 414 size_t i; 415 416 i = HAMMER_BLOCKMAP_LAYER2_OFFSET(offset) / sizeof(*track2); 417 track2 = &collect->track2[i]; 418 if (track2->entry_crc == 0) { 419 collect->layer2[i] = *layer2; 420 track2->zone = zone; 421 track2->bytes_free = HAMMER_BIGBLOCK_SIZE; 422 track2->entry_crc = 1; /* steal field to tag track load */ 423 } 424 return (track2); 425 } 426 427 static 428 void 429 dump_collect_table(void) 430 { 431 collect_t collect; 432 int i; 433 int error = 0; 434 int total = 0; 435 int stats[HAMMER_MAX_ZONES]; 436 bzero(stats, sizeof(stats)); 437 438 RB_FOREACH(collect, collect_rb_tree, &CollectTree) { 439 dump_collect(collect, stats); 440 error += collect->error; 441 } 442 443 while ((collect = RB_ROOT(&CollectTree)) != NULL) { 444 RB_REMOVE(collect_rb_tree, &CollectTree, collect); 445 collect_rel(collect); 446 } 447 assert(RB_EMPTY(&CollectTree)); 448 449 if (VerboseOpt) { 450 printf("zone-bigblock statistics\n"); 451 printf("\tzone #\tbigblocks\n"); 452 for (i = 0; i < HAMMER_MAX_ZONES; i++) { 453 printf("\tzone %d\t%d\n", i, stats[i]); 454 total += stats[i]; 455 } 456 printf("\t---------------\n"); 457 printf("\ttotal\t%d\n", total); 458 } 459 460 if (error || VerboseOpt) 461 printf("%d errors\n", error); 462 } 463 464 static 465 void 466 dump_collect(collect_t collect, int *stats) 467 { 468 struct hammer_blockmap_layer2 *track2; 469 struct hammer_blockmap_layer2 *layer2; 470 hammer_off_t offset; 471 size_t i; 472 int zone; 473 474 for (i = 0; i < HAMMER_BLOCKMAP_RADIX2; ++i) { 475 track2 = &collect->track2[i]; 476 layer2 = &collect->layer2[i]; 477 offset = collect->phys_offset + i * HAMMER_BIGBLOCK_SIZE; 478 479 /* 480 * Check bigblocks referenced by data, B-Tree nodes 481 * and UNDO fifo. 482 */ 483 if (track2->entry_crc == 0) 484 continue; 485 486 zone = layer2->zone; 487 if (AssertOnFailure) { 488 assert((zone == HAMMER_ZONE_UNDO_INDEX) || 489 (zone >= HAMMER_ZONE_BTREE_INDEX && 490 zone < HAMMER_MAX_ZONES)); 491 } 492 stats[zone]++; 493 494 if (track2->zone != layer2->zone) { 495 printf("BZ\tblock=%016jx calc zone=%2d, got zone=%2d\n", 496 (intmax_t)offset, 497 track2->zone, 498 layer2->zone); 499 collect->error++; 500 } else if (track2->bytes_free != layer2->bytes_free) { 501 printf("BM\tblock=%016jx zone=%2d calc %d free, got %d\n", 502 (intmax_t)offset, 503 layer2->zone, 504 track2->bytes_free, 505 layer2->bytes_free); 506 collect->error++; 507 } else if (VerboseOpt) { 508 printf("\tblock=%016jx zone=%2d %d free (correct)\n", 509 (intmax_t)offset, 510 layer2->zone, 511 track2->bytes_free); 512 } 513 } 514 } 515