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);
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 get_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);
64 hammer_cmd_show(hammer_off_t node_offset, u_int32_t lo, int64_t obj_id,
66 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
68 struct volume_info *volume;
69 struct btree_search search;
70 btree_search_t searchp;
75 if (node_offset == (hammer_off_t)-1) {
76 volume = get_volume(RootVolNo);
77 node_offset = volume->ondisk->vol0_btree_root;
79 printf("Volume header\trecords=%jd next_tid=%016jx\n",
80 (intmax_t)volume->ondisk->vol0_stat_records,
81 (uintmax_t)volume->ondisk->vol0_next_tid);
82 printf("\t\tbufoffset=%016jx\n",
83 (uintmax_t)volume->ondisk->vol_buf_beg);
84 for (zone = 0; zone < HAMMER_MAX_ZONES; ++zone) {
85 printf("\t\tzone %d\tnext_offset=%016jx\n",
87 (uintmax_t)volume->ondisk->vol0_blockmap[zone].next_offset
94 if (lo == 0 && obj_id == (int64_t)HAMMER_MIN_OBJID) {
96 printf("show %016jx depth %d\n", (uintmax_t)node_offset, depth);
99 search.obj_id = obj_id;
101 printf("show %016jx lo %08x obj_id %016jx depth %d\n",
102 (uintmax_t)node_offset, lo, (uintmax_t)obj_id, depth);
104 print_btree_node(node_offset, searchp, depth, HAMMER_MAX_TID,
105 left_bound, right_bound);
111 print_btree_node(hammer_off_t node_offset, btree_search_t search,
112 int depth, hammer_tid_t mirror_tid,
113 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
115 struct buffer_info *buffer = NULL;
116 hammer_node_ondisk_t node;
117 hammer_btree_elm_t elm;
125 node = get_node(node_offset, &buffer);
128 printf("BI NODE %016jx (IO ERROR)\n",
129 (uintmax_t)node_offset);
133 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
138 if (node->mirror_tid <= mirror_tid) {
145 printf("%c%c NODE %016jx cnt=%02d p=%016jx "
149 (uintmax_t)node_offset, node->count,
150 (uintmax_t)node->parent,
151 (node->type ? node->type : '?'), depth);
152 printf(" mirror %016jx", (uintmax_t)node->mirror_tid);
155 print_bigblock_fill(node_offset);
159 maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ?
160 HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS;
162 for (i = 0; i < node->count && i < maxcount; ++i) {
163 elm = &node->elms[i];
165 if (node->type != HAMMER_BTREE_TYPE_INTERNAL) {
168 elm->base.localization == search->lo &&
169 elm->base.obj_id == search->obj_id) {
174 if (elm->base.localization > search->lo ||
175 (elm->base.localization == search->lo &&
176 elm->base.obj_id > search->obj_id)) {
179 if (elm[1].base.localization < search->lo ||
180 (elm[1].base.localization == search->lo &&
181 elm[1].base.obj_id < search->obj_id)) {
188 flags = get_elm_flags(node, node_offset,
189 elm, elm->base.btype,
190 left_bound, right_bound);
191 print_btree_elm(elm, i, node->type, flags, "ELM", ext);
193 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
194 elm = &node->elms[i];
196 flags = get_elm_flags(node, node_offset,
198 left_bound, right_bound);
199 print_btree_elm(elm, i, node->type, flags, "RBN", NULL);
203 for (i = 0; i < node->count; ++i) {
204 elm = &node->elms[i];
207 case HAMMER_BTREE_TYPE_INTERNAL:
209 if (elm->base.localization > search->lo ||
210 (elm->base.localization == search->lo &&
211 elm->base.obj_id > search->obj_id)) {
214 if (elm[1].base.localization < search->lo ||
215 (elm[1].base.localization == search->lo &&
216 elm[1].base.obj_id < search->obj_id)) {
220 if (elm->internal.subtree_offset) {
221 print_btree_node(elm->internal.subtree_offset,
223 elm->internal.mirror_tid,
224 &elm[0].base, &elm[1].base);
226 * Cause show to iterate after seeking to
241 is_root_btree_beg(u_int8_t type, int i, hammer_btree_elm_t elm)
243 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
245 elm->base.localization == 0 &&
246 elm->base.obj_id == (int64_t)-0x8000000000000000LL &&
247 elm->base.key == (int64_t)-0x8000000000000000LL &&
248 elm->base.create_tid == 1 &&
249 elm->base.delete_tid == 1 &&
250 elm->base.rec_type == 0 &&
251 elm->base.obj_type == 0 &&
252 elm->base.btype != 0); /* depends on the original node */
257 is_root_btree_end(u_int8_t type, int i, hammer_btree_elm_t elm)
259 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
261 elm->base.localization == 0xFFFFFFFFU &&
262 elm->base.obj_id == 0x7FFFFFFFFFFFFFFFLL &&
263 elm->base.key == 0x7FFFFFFFFFFFFFFFLL &&
264 elm->base.create_tid == 0xFFFFFFFFFFFFFFFFULL &&
265 elm->base.delete_tid == 0 &&
266 elm->base.rec_type == 0xFFFFU &&
267 elm->base.obj_type == 0 &&
268 elm->base.btype == 0); /* not initialized */
273 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
274 int flags, const char *label, const char *ext)
276 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
280 flagstr[0] = flags ? 'B' : 'G';
281 if (flags & FLAG_TOOFARLEFT)
283 if (flags & FLAG_TOOFARRIGHT)
285 if (flags & FLAG_BADTYPE)
287 if (flags & FLAG_BADCHILDPARENT)
289 if (flags & FLAG_BADMIRRORTID)
293 * Check if elm is derived from root split
295 if (is_root_btree_beg(type, i, elm))
297 else if (is_root_btree_end(type, i, elm))
302 if (elm->base.delete_tid)
307 printf("%s\t%s %2d %c ",
309 (elm->base.btype ? elm->base.btype : '?'));
310 printf("lo=%08x obj=%016jx rt=%02x key=%016jx ot=%02x\n",
311 elm->base.localization,
312 (uintmax_t)elm->base.obj_id,
314 (uintmax_t)elm->base.key,
316 printf("\t %c tids %016jx:%016jx ",
317 (rootelm == ' ' ? deleted : rootelm),
318 (uintmax_t)elm->base.create_tid,
319 (uintmax_t)elm->base.delete_tid);
322 case HAMMER_BTREE_TYPE_INTERNAL:
323 printf("suboff=%016jx",
324 (uintmax_t)elm->internal.subtree_offset);
326 printf(" mirror %016jx",
327 (uintmax_t)elm->internal.mirror_tid);
332 case HAMMER_BTREE_TYPE_LEAF:
335 switch(elm->base.btype) {
336 case HAMMER_BTREE_TYPE_RECORD:
338 printf("\n%s\t ", check_data_crc(elm));
341 printf("dataoff=%016jx/%d",
342 (uintmax_t)elm->leaf.data_offset,
345 printf(" crc=%04x", elm->leaf.data_crc);
346 printf("\n\t fills=");
347 print_bigblock_fill(elm->leaf.data_offset);
362 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
363 hammer_btree_elm_t elm, u_int8_t btype,
364 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
369 case HAMMER_BTREE_TYPE_INTERNAL:
370 if (elm->internal.subtree_offset) {
371 struct buffer_info *buffer = NULL;
372 hammer_node_ondisk_t subnode;
374 subnode = get_node(elm->internal.subtree_offset,
377 flags |= FLAG_BADCHILDPARENT;
378 else if (subnode->parent != node_offset)
379 flags |= FLAG_BADCHILDPARENT;
382 if (elm->internal.mirror_tid > node->mirror_tid)
383 flags |= FLAG_BADMIRRORTID;
386 case HAMMER_BTREE_TYPE_INTERNAL:
387 if (left_bound == NULL || right_bound == NULL)
389 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
390 flags |= FLAG_TOOFARLEFT;
391 if (hammer_btree_cmp(&elm->base, right_bound) > 0)
392 flags |= FLAG_TOOFARRIGHT;
394 case HAMMER_BTREE_TYPE_LEAF:
395 if (left_bound == NULL || right_bound == NULL)
397 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
398 flags |= FLAG_TOOFARLEFT;
399 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
400 flags |= FLAG_TOOFARRIGHT;
403 flags |= FLAG_BADTYPE;
407 case HAMMER_BTREE_TYPE_LEAF:
408 if (node->mirror_tid == 0 &&
409 !(node->parent == 0 && node->count == 2)) {
410 flags |= FLAG_BADMIRRORTID;
412 if (elm->base.create_tid && node->mirror_tid &&
413 elm->base.create_tid > node->mirror_tid) {
414 flags |= FLAG_BADMIRRORTID;
416 if (elm->base.delete_tid && node->mirror_tid &&
417 elm->base.delete_tid > node->mirror_tid) {
418 flags |= FLAG_BADMIRRORTID;
421 case HAMMER_BTREE_TYPE_RECORD:
422 if (left_bound == NULL || right_bound == NULL)
424 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
425 flags |= FLAG_TOOFARLEFT;
426 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
427 flags |= FLAG_TOOFARRIGHT;
430 flags |= FLAG_BADTYPE;
435 flags |= FLAG_BADTYPE;
443 print_bigblock_fill(hammer_off_t offset)
445 struct hammer_blockmap_layer1 layer1;
446 struct hammer_blockmap_layer2 layer2;
450 blockmap_lookup(offset, &layer1, &layer2, &error);
452 printf("z%d:%lld=BADZ",
453 HAMMER_ZONE_DECODE(offset),
454 (offset & ~HAMMER_OFF_ZONE_MASK) /
458 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
461 printf("z%d:%lld=%d%%",
462 HAMMER_ZONE_DECODE(offset),
463 (offset & ~HAMMER_OFF_ZONE_MASK) /
464 HAMMER_BIGBLOCK_SIZE,
471 * Check the generic crc on a data element. Inodes record types are
472 * special in that some of their fields are not CRCed.
474 * Also check that the zone is valid.
478 check_data_crc(hammer_btree_elm_t elm)
480 struct buffer_info *data_buffer;
481 hammer_off_t data_offset;
488 data_offset = elm->leaf.data_offset;
489 data_len = elm->leaf.data_len;
491 if (data_offset == 0 || data_len == 0)
497 blockmap_lookup(data_offset, NULL, NULL, &error);
501 ptr = get_buffer_data(data_offset, &data_buffer, 0);
502 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
505 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
506 data_len == sizeof(struct hammer_inode_data)) {
507 crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
509 crc = crc32_ext(ptr, len, crc);
515 rel_buffer(data_buffer);
517 switch (error) { /* bad offset */
540 if (crc == elm->leaf.data_crc)
542 return("BX"); /* bad crc */
547 print_config(char *cfgtxt)
551 printf("\n%17stext=\"\n", "");
552 while((token = strsep(&cfgtxt, "\r\n")) != NULL) {
553 printf("%17s %s\n", "", token);
555 printf("%17s\"", "");
560 print_record(hammer_btree_elm_t elm)
562 struct buffer_info *data_buffer;
563 hammer_off_t data_offset;
565 hammer_data_ondisk_t data;
570 data_offset = elm->leaf.data_offset;
571 data_len = elm->leaf.data_len;
575 data = get_buffer_data(data_offset, &data_buffer, 0);
579 switch(elm->leaf.base.rec_type) {
580 case HAMMER_RECTYPE_UNKNOWN:
581 printf("\n%17s", "");
584 case HAMMER_RECTYPE_INODE:
585 printf("\n%17s", "");
586 printf("size=%jd nlinks=%jd",
587 (intmax_t)data->inode.size,
588 (intmax_t)data->inode.nlinks);
590 printf(" mode=%05o uflags=%08x\n",
594 printf("ctime=%016jx pobjid=%016jx obj_type=%d\n",
595 (uintmax_t)data->inode.ctime,
596 (uintmax_t)data->inode.parent_obj_id,
597 data->inode.obj_type);
599 printf("mtime=%016jx", (uintmax_t)data->inode.mtime);
600 printf(" caps=%02x", data->inode.cap_flags);
603 case HAMMER_RECTYPE_DIRENTRY:
604 printf("\n%17s", "");
605 data_len -= HAMMER_ENTRY_NAME_OFF;
606 printf("dir-entry ino=%016jx lo=%08x name=\"%*.*s\"",
607 (uintmax_t)data->entry.obj_id,
608 data->entry.localization,
609 data_len, data_len, data->entry.name);
611 case HAMMER_RECTYPE_FIX:
612 switch(elm->leaf.base.key) {
613 case HAMMER_FIXKEY_SYMLINK:
614 data_len -= HAMMER_SYMLINK_NAME_OFF;
615 printf("\n%17s", "");
616 printf("symlink=\"%*.*s\"", data_len, data_len,
623 case HAMMER_RECTYPE_PFS:
624 printf("\n%17s", "");
625 printf("sync_beg_tid=%016jx sync_end_tid=%016jx\n",
626 (intmax_t)data->pfsd.sync_beg_tid,
627 (intmax_t)data->pfsd.sync_end_tid);
628 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
629 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
631 printf("shared_uuid=%s\n", str1);
633 printf("unique_uuid=%s\n", str2);
635 printf("mirror_flags=%08x label=\"%s\"",
636 data->pfsd.mirror_flags, data->pfsd.label);
637 if (data->pfsd.snapshots[0])
638 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
642 case HAMMER_RECTYPE_SNAPSHOT:
643 printf("\n%17s", "");
644 printf("tid=%016jx label=\"%s\"",
645 (intmax_t)data->snap.tid, data->snap.label);
647 case HAMMER_RECTYPE_CONFIG:
648 if (VerboseOpt > 2) {
649 print_config(data->config.text);
652 case HAMMER_RECTYPE_DATA:
653 if (VerboseOpt > 3) {
655 hexdump(data, data_len, "\t\t ", 0);
658 case HAMMER_RECTYPE_EXT:
659 case HAMMER_RECTYPE_DB:
660 if (VerboseOpt > 2) {
662 hexdump(data, data_len, "\t\t ", 0);
669 rel_buffer(data_buffer);
676 hammer_cmd_show_undo(void)
678 struct volume_info *volume;
679 hammer_blockmap_t rootmap;
680 hammer_off_t scan_offset;
681 hammer_fifo_any_t head;
682 struct buffer_info *data_buffer = NULL;
684 volume = get_volume(RootVolNo);
685 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
686 printf("Volume header UNDO %016jx-%016jx/%016jx\n",
687 (intmax_t)rootmap->first_offset,
688 (intmax_t)rootmap->next_offset,
689 (intmax_t)rootmap->alloc_offset);
690 printf("Undo map is %jdMB\n",
691 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
693 scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
694 while (scan_offset < rootmap->alloc_offset) {
695 head = get_buffer_data(scan_offset, &data_buffer, 0);
696 printf("%016jx ", scan_offset);
698 switch(head->head.hdr_type) {
699 case HAMMER_HEAD_TYPE_PAD:
700 printf("PAD(%04x)\n", head->head.hdr_size);
702 case HAMMER_HEAD_TYPE_DUMMY:
703 printf("DUMMY(%04x) seq=%08x\n",
704 head->head.hdr_size, head->head.hdr_seq);
706 case HAMMER_HEAD_TYPE_UNDO:
707 printf("UNDO(%04x) seq=%08x "
708 "dataoff=%016jx bytes=%d\n",
709 head->head.hdr_size, head->head.hdr_seq,
710 (intmax_t)head->undo.undo_offset,
711 head->undo.undo_data_bytes);
713 case HAMMER_HEAD_TYPE_REDO:
714 printf("REDO(%04x) seq=%08x flags=%08x "
715 "objid=%016jx logoff=%016jx bytes=%d\n",
716 head->head.hdr_size, head->head.hdr_seq,
717 head->redo.redo_flags,
718 (intmax_t)head->redo.redo_objid,
719 (intmax_t)head->redo.redo_offset,
720 head->redo.redo_data_bytes);
723 printf("UNKNOWN(%04x,%04x) seq=%08x\n",
729 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
730 head->head.hdr_size == 0 ||
731 head->head.hdr_size > HAMMER_UNDO_ALIGN -
732 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
733 printf("Illegal size field, skipping to "
735 scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
738 scan_offset += head->head.hdr_size;
742 rel_buffer(data_buffer);