2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
38 #define FLAG_TOOFARLEFT 0x0001
39 #define FLAG_TOOFARRIGHT 0x0002
40 #define FLAG_BADTYPE 0x0004
41 #define FLAG_BADCHILDPARENT 0x0008
42 #define FLAG_BADMIRRORTID 0x0010
44 typedef struct btree_search {
49 static void print_btree_node(hammer_off_t node_offset, btree_search_t search,
50 int depth, hammer_tid_t mirror_tid,
51 hammer_base_elm_t left_bound,
52 hammer_base_elm_t right_bound,
54 static const char *check_data_crc(hammer_btree_elm_t elm);
55 static void print_record(hammer_btree_elm_t elm);
56 static void print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
57 int flags, const char *label, const char *ext);
58 static int get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
59 hammer_btree_elm_t elm, u_int8_t btype,
60 hammer_base_elm_t left_bound,
61 hammer_base_elm_t right_bound);
62 static void print_bigblock_fill(hammer_off_t offset);
65 hammer_cmd_show(hammer_off_t node_offset, u_int32_t lo, int64_t obj_id,
67 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
69 struct volume_info *volume;
70 struct btree_search search;
71 btree_search_t searchp;
76 if (node_offset == (hammer_off_t)-1) {
77 volume = get_volume(RootVolNo);
78 node_offset = volume->ondisk->vol0_btree_root;
80 printf("Volume header\trecords=%jd next_tid=%016jx\n",
81 (intmax_t)volume->ondisk->vol0_stat_records,
82 (uintmax_t)volume->ondisk->vol0_next_tid);
83 printf("\t\tbufoffset=%016jx\n",
84 (uintmax_t)volume->ondisk->vol_buf_beg);
85 for (zone = 0; zone < HAMMER_MAX_ZONES; ++zone) {
86 printf("\t\tzone %d\tnext_offset=%016jx\n",
88 (uintmax_t)volume->ondisk->vol0_blockmap[zone].next_offset
95 if (lo == 0 && obj_id == (int64_t)HAMMER_MIN_OBJID) {
97 printf("show %016jx depth %d\n", (uintmax_t)node_offset, depth);
100 search.obj_id = obj_id;
102 printf("show %016jx lo %08x obj_id %016jx depth %d\n",
103 (uintmax_t)node_offset, lo, (uintmax_t)obj_id, depth);
105 print_btree_node(node_offset, searchp, depth, HAMMER_MAX_TID,
106 left_bound, right_bound, -1);
112 print_btree_node(hammer_off_t node_offset, btree_search_t search,
113 int depth, hammer_tid_t mirror_tid,
114 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound,
117 struct buffer_info *buffer = NULL;
118 hammer_node_ondisk_t node;
119 hammer_btree_elm_t elm;
127 node = get_node(node_offset, &buffer);
130 printf("BI NODE %016jx (IO ERROR)\n",
131 (uintmax_t)node_offset);
135 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
141 * Workaround for the root split that is not an error
143 if (this_index == 0 && depth == 1 && mirror_tid == 0) {
144 badm = ' '; /* could use unique mark */
146 if (node->mirror_tid <= mirror_tid) {
154 printf("%c%c NODE %016jx cnt=%02d p=%016jx "
158 (uintmax_t)node_offset, node->count,
159 (uintmax_t)node->parent,
160 (node->type ? node->type : '?'), depth);
161 printf(" mirror %016jx", (uintmax_t)node->mirror_tid);
164 print_bigblock_fill(node_offset);
168 maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ?
169 HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS;
171 for (i = 0; i < node->count && i < maxcount; ++i) {
172 elm = &node->elms[i];
174 if (node->type != HAMMER_BTREE_TYPE_INTERNAL) {
177 elm->base.localization == search->lo &&
178 elm->base.obj_id == search->obj_id) {
183 if (elm->base.localization > search->lo ||
184 (elm->base.localization == search->lo &&
185 elm->base.obj_id > search->obj_id)) {
188 if (elm[1].base.localization < search->lo ||
189 (elm[1].base.localization == search->lo &&
190 elm[1].base.obj_id < search->obj_id)) {
197 flags = get_elm_flags(node, node_offset,
198 elm, elm->base.btype,
199 left_bound, right_bound);
200 print_btree_elm(elm, i, node->type, flags, "ELM", ext);
202 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
203 elm = &node->elms[i];
205 flags = get_elm_flags(node, node_offset,
207 left_bound, right_bound);
208 print_btree_elm(elm, i, node->type, flags, "RBN", NULL);
212 for (i = 0; i < node->count; ++i) {
213 elm = &node->elms[i];
216 case HAMMER_BTREE_TYPE_INTERNAL:
218 if (elm->base.localization > search->lo ||
219 (elm->base.localization == search->lo &&
220 elm->base.obj_id > search->obj_id)) {
223 if (elm[1].base.localization < search->lo ||
224 (elm[1].base.localization == search->lo &&
225 elm[1].base.obj_id < search->obj_id)) {
229 if (elm->internal.subtree_offset) {
230 print_btree_node(elm->internal.subtree_offset,
232 elm->internal.mirror_tid,
233 &elm[0].base, &elm[1].base, i);
235 * Cause show to iterate after seeking to
250 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
251 int flags, const char *label, const char *ext)
253 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
256 flagstr[0] = flags ? 'B' : 'G';
257 if (flags & FLAG_TOOFARLEFT)
259 if (flags & FLAG_TOOFARRIGHT)
261 if (flags & FLAG_BADTYPE)
263 if (flags & FLAG_BADCHILDPARENT)
265 if (flags & FLAG_BADMIRRORTID)
269 * Workaround for the root split that is not actual delete
271 if (elm->base.delete_tid) {
272 if (type == HAMMER_BTREE_TYPE_INTERNAL &&
274 elm->base.create_tid == 1 &&
275 elm->base.delete_tid == 1)
276 deleted = ' '; /* could use unique mark */
283 printf("%s\t%s %2d %c ",
285 (elm->base.btype ? elm->base.btype : '?'));
286 printf("lo=%08x obj=%016jx rt=%02x key=%016jx ot=%02x\n",
287 elm->base.localization,
288 (uintmax_t)elm->base.obj_id,
290 (uintmax_t)elm->base.key,
292 printf("\t %c tids %016jx:%016jx ",
294 (uintmax_t)elm->base.create_tid,
295 (uintmax_t)elm->base.delete_tid);
298 case HAMMER_BTREE_TYPE_INTERNAL:
299 printf("suboff=%016jx",
300 (uintmax_t)elm->internal.subtree_offset);
302 printf(" mirror %016jx",
303 (uintmax_t)elm->internal.mirror_tid);
308 case HAMMER_BTREE_TYPE_LEAF:
311 switch(elm->base.btype) {
312 case HAMMER_BTREE_TYPE_RECORD:
314 printf("\n%s\t ", check_data_crc(elm));
317 printf("dataoff=%016jx/%d",
318 (uintmax_t)elm->leaf.data_offset,
321 printf(" crc=%04x", elm->leaf.data_crc);
322 printf("\n\t fills=");
323 print_bigblock_fill(elm->leaf.data_offset);
338 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
339 hammer_btree_elm_t elm, u_int8_t btype,
340 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
345 case HAMMER_BTREE_TYPE_INTERNAL:
346 if (elm->internal.subtree_offset) {
347 struct buffer_info *buffer = NULL;
348 hammer_node_ondisk_t subnode;
350 subnode = get_node(elm->internal.subtree_offset,
353 flags |= FLAG_BADCHILDPARENT;
354 else if (subnode->parent != node_offset)
355 flags |= FLAG_BADCHILDPARENT;
358 if (elm->internal.mirror_tid > node->mirror_tid)
359 flags |= FLAG_BADMIRRORTID;
362 case HAMMER_BTREE_TYPE_INTERNAL:
363 if (left_bound == NULL || right_bound == NULL)
365 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
366 flags |= FLAG_TOOFARLEFT;
367 if (hammer_btree_cmp(&elm->base, right_bound) > 0)
368 flags |= FLAG_TOOFARRIGHT;
370 case HAMMER_BTREE_TYPE_LEAF:
371 if (left_bound == NULL || right_bound == NULL)
373 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
374 flags |= FLAG_TOOFARLEFT;
375 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
376 flags |= FLAG_TOOFARRIGHT;
379 flags |= FLAG_BADTYPE;
383 case HAMMER_BTREE_TYPE_LEAF:
384 if (node->mirror_tid == 0 &&
385 !(node->parent == 0 && node->count == 2)) {
386 flags |= FLAG_BADMIRRORTID;
388 if (elm->base.create_tid && node->mirror_tid &&
389 elm->base.create_tid > node->mirror_tid) {
390 flags |= FLAG_BADMIRRORTID;
392 if (elm->base.delete_tid && node->mirror_tid &&
393 elm->base.delete_tid > node->mirror_tid) {
394 flags |= FLAG_BADMIRRORTID;
397 case HAMMER_BTREE_TYPE_RECORD:
398 if (left_bound == NULL || right_bound == NULL)
400 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
401 flags |= FLAG_TOOFARLEFT;
402 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
403 flags |= FLAG_TOOFARRIGHT;
406 flags |= FLAG_BADTYPE;
411 flags |= FLAG_BADTYPE;
419 print_bigblock_fill(hammer_off_t offset)
421 struct hammer_blockmap_layer1 layer1;
422 struct hammer_blockmap_layer2 layer2;
426 blockmap_lookup(offset, &layer1, &layer2, &error);
428 printf("z%d:%lld=BADZ",
429 HAMMER_ZONE_DECODE(offset),
430 (offset & ~HAMMER_OFF_ZONE_MASK) /
434 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
437 printf("z%d:%lld=%d%%",
438 HAMMER_ZONE_DECODE(offset),
439 (offset & ~HAMMER_OFF_ZONE_MASK) /
440 HAMMER_BIGBLOCK_SIZE,
447 * Check the generic crc on a data element. Inodes record types are
448 * special in that some of their fields are not CRCed.
450 * Also check that the zone is valid.
454 check_data_crc(hammer_btree_elm_t elm)
456 struct buffer_info *data_buffer;
457 hammer_off_t data_offset;
464 data_offset = elm->leaf.data_offset;
465 data_len = elm->leaf.data_len;
467 if (data_offset == 0 || data_len == 0)
473 blockmap_lookup(data_offset, NULL, NULL, &error);
477 ptr = get_buffer_data(data_offset, &data_buffer, 0);
478 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
481 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
482 data_len == sizeof(struct hammer_inode_data)) {
483 crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
485 crc = crc32_ext(ptr, len, crc);
491 rel_buffer(data_buffer);
493 return("BO"); /* bad offset */
494 if (crc == elm->leaf.data_crc)
496 return("BX"); /* bad crc */
501 print_config(char *cfgtxt)
505 printf("\n%17stext=\"\n", "");
506 while((token = strsep(&cfgtxt, "\r\n")) != NULL) {
507 printf("%17s %s\n", "", token);
509 printf("%17s\"", "");
514 print_record(hammer_btree_elm_t elm)
516 struct buffer_info *data_buffer;
517 hammer_off_t data_offset;
519 hammer_data_ondisk_t data;
524 data_offset = elm->leaf.data_offset;
525 data_len = elm->leaf.data_len;
529 data = get_buffer_data(data_offset, &data_buffer, 0);
533 switch(elm->leaf.base.rec_type) {
534 case HAMMER_RECTYPE_UNKNOWN:
535 printf("\n%17s", "");
538 case HAMMER_RECTYPE_INODE:
539 printf("\n%17s", "");
540 printf("size=%jd nlinks=%jd",
541 (intmax_t)data->inode.size,
542 (intmax_t)data->inode.nlinks);
544 printf(" mode=%05o uflags=%08x\n",
548 printf("ctime=%016jx pobjid=%016jx obj_type=%d\n",
549 (uintmax_t)data->inode.ctime,
550 (uintmax_t)data->inode.parent_obj_id,
551 data->inode.obj_type);
553 printf("mtime=%016jx", (uintmax_t)data->inode.mtime);
554 printf(" caps=%02x", data->inode.cap_flags);
557 case HAMMER_RECTYPE_DIRENTRY:
558 printf("\n%17s", "");
559 data_len -= HAMMER_ENTRY_NAME_OFF;
560 printf("dir-entry ino=%016jx lo=%08x name=\"%*.*s\"",
561 (uintmax_t)data->entry.obj_id,
562 data->entry.localization,
563 data_len, data_len, data->entry.name);
565 case HAMMER_RECTYPE_FIX:
566 switch(elm->leaf.base.key) {
567 case HAMMER_FIXKEY_SYMLINK:
568 data_len -= HAMMER_SYMLINK_NAME_OFF;
569 printf("\n%17s", "");
570 printf("symlink=\"%*.*s\"", data_len, data_len,
577 case HAMMER_RECTYPE_PFS:
578 printf("\n%17s", "");
579 printf("sync_beg_tid=%016jx sync_end_tid=%016jx\n",
580 (intmax_t)data->pfsd.sync_beg_tid,
581 (intmax_t)data->pfsd.sync_end_tid);
582 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
583 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
585 printf("shared_uuid=%s\n", str1);
587 printf("unique_uuid=%s\n", str2);
589 printf("mirror_flags=%08x label=\"%s\"",
590 data->pfsd.mirror_flags, data->pfsd.label);
591 if (data->pfsd.snapshots[0])
592 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
596 case HAMMER_RECTYPE_SNAPSHOT:
597 printf("\n%17s", "");
598 printf("tid=%016jx label=\"%s\"",
599 (intmax_t)data->snap.tid, data->snap.label);
601 case HAMMER_RECTYPE_CONFIG:
602 if (VerboseOpt > 2) {
603 print_config(data->config.text);
606 case HAMMER_RECTYPE_DATA:
607 if (VerboseOpt > 3) {
609 hexdump(data, data_len, "\t\t ", 0);
612 case HAMMER_RECTYPE_EXT:
613 case HAMMER_RECTYPE_DB:
614 if (VerboseOpt > 2) {
616 hexdump(data, data_len, "\t\t ", 0);
623 rel_buffer(data_buffer);
630 hammer_cmd_show_undo(void)
632 struct volume_info *volume;
633 hammer_blockmap_t rootmap;
634 hammer_off_t scan_offset;
635 hammer_fifo_any_t head;
636 struct buffer_info *data_buffer = NULL;
638 volume = get_volume(RootVolNo);
639 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
640 printf("Volume header UNDO %016jx-%016jx/%016jx\n",
641 (intmax_t)rootmap->first_offset,
642 (intmax_t)rootmap->next_offset,
643 (intmax_t)rootmap->alloc_offset);
644 printf("Undo map is %jdMB\n",
645 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
647 scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
648 while (scan_offset < rootmap->alloc_offset) {
649 head = get_buffer_data(scan_offset, &data_buffer, 0);
650 printf("%016jx ", scan_offset);
652 switch(head->head.hdr_type) {
653 case HAMMER_HEAD_TYPE_PAD:
654 printf("PAD(%04x)\n", head->head.hdr_size);
656 case HAMMER_HEAD_TYPE_DUMMY:
657 printf("DUMMY(%04x) seq=%08x\n",
658 head->head.hdr_size, head->head.hdr_seq);
660 case HAMMER_HEAD_TYPE_UNDO:
661 printf("UNDO(%04x) seq=%08x "
662 "dataoff=%016jx bytes=%d\n",
663 head->head.hdr_size, head->head.hdr_seq,
664 (intmax_t)head->undo.undo_offset,
665 head->undo.undo_data_bytes);
667 case HAMMER_HEAD_TYPE_REDO:
668 printf("REDO(%04x) seq=%08x flags=%08x "
669 "objid=%016jx logoff=%016jx bytes=%d\n",
670 head->head.hdr_size, head->head.hdr_seq,
671 head->redo.redo_flags,
672 (intmax_t)head->redo.redo_objid,
673 (intmax_t)head->redo.redo_offset,
674 head->redo.redo_data_bytes);
677 printf("UNKNOWN(%04x,%04x) seq=%08x\n",
683 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
684 head->head.hdr_size == 0 ||
685 head->head.hdr_size > HAMMER_UNDO_ALIGN -
686 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
687 printf("Illegal size field, skipping to "
689 scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
692 scan_offset += head->head.hdr_size;
696 rel_buffer(data_buffer);