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 hammer_tid_t create_tid;
50 int limit; /* # of fields to test */
51 int filter; /* filter type (default -1) */
54 static void print_btree_node(hammer_off_t node_offset, btree_search_t search,
55 int depth, hammer_tid_t mirror_tid,
56 hammer_base_elm_t left_bound,
57 hammer_base_elm_t right_bound);
58 static const char *check_data_crc(hammer_btree_elm_t elm);
59 static void print_record(hammer_btree_elm_t elm);
60 static void print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
61 int flags, const char *label, const char *ext);
62 static int get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
63 hammer_btree_elm_t elm, u_int8_t btype,
64 hammer_base_elm_t left_bound,
65 hammer_base_elm_t right_bound);
66 static void print_bigblock_fill(hammer_off_t offset);
67 static int init_btree_search(const char *arg, int filter,
68 btree_search_t search);
69 static int test_btree_search(hammer_btree_elm_t elm, btree_search_t search);
72 hammer_cmd_show(hammer_off_t node_offset, const char *arg,
73 int filter, int depth,
74 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
76 struct volume_info *volume;
77 struct btree_search search;
78 btree_search_t searchp = NULL;
83 if (node_offset == (hammer_off_t)-1) {
84 volume = get_volume(RootVolNo);
85 node_offset = volume->ondisk->vol0_btree_root;
87 printf("Volume header\trecords=%jd next_tid=%016jx\n",
88 (intmax_t)volume->ondisk->vol0_stat_records,
89 (uintmax_t)volume->ondisk->vol0_next_tid);
90 printf("\t\tbufoffset=%016jx\n",
91 (uintmax_t)volume->ondisk->vol_buf_beg);
92 for (zone = 0; zone < HAMMER_MAX_ZONES; ++zone) {
93 printf("\t\tzone %d\tnext_offset=%016jx\n",
95 (uintmax_t)volume->ondisk->vol0_blockmap[zone].next_offset
102 printf("show %016jx", (uintmax_t)node_offset);
104 assert(init_btree_search(arg, filter, &search) != -1);
105 if (search.limit >= 1)
106 printf(" lo %08x obj_id %016jx",
107 search.lo, (uintmax_t)search.obj_id);
108 if (search.limit >= 3)
109 printf(" rec_type %02x", search.rec_type);
110 if (search.limit >= 4)
111 printf(" key %016jx", (uintmax_t)search.key);
112 if (search.limit == 5)
113 printf(" create_tid %016jx\n",
114 (uintmax_t)search.create_tid);
117 printf(" depth %d\n", depth);
118 print_btree_node(node_offset, searchp, depth, HAMMER_MAX_TID,
119 left_bound, right_bound);
125 print_btree_node(hammer_off_t node_offset, btree_search_t search,
126 int depth, hammer_tid_t mirror_tid,
127 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
129 struct buffer_info *buffer = NULL;
130 hammer_node_ondisk_t node;
131 hammer_btree_elm_t elm;
139 node = get_node(node_offset, &buffer);
142 printf("BI NODE %016jx (IO ERROR)\n",
143 (uintmax_t)node_offset);
147 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
152 if (node->mirror_tid <= mirror_tid) {
159 printf("%c%c NODE %016jx cnt=%02d p=%016jx "
163 (uintmax_t)node_offset, node->count,
164 (uintmax_t)node->parent,
165 (node->type ? node->type : '?'), depth);
166 printf(" mirror %016jx", (uintmax_t)node->mirror_tid);
169 print_bigblock_fill(node_offset);
173 maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ?
174 HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS;
176 for (i = 0; i < node->count && i < maxcount; ++i) {
177 elm = &node->elms[i];
179 if (node->type != HAMMER_BTREE_TYPE_INTERNAL) {
181 if (search && test_btree_search(elm, search) == 0)
185 if (test_btree_search(elm, search) > 0 ||
186 test_btree_search(elm + 1, search) < 0)
192 flags = get_elm_flags(node, node_offset,
193 elm, elm->base.btype,
194 left_bound, right_bound);
195 print_btree_elm(elm, i, node->type, flags, "ELM", ext);
197 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
198 elm = &node->elms[i];
200 flags = get_elm_flags(node, node_offset,
202 left_bound, right_bound);
203 print_btree_elm(elm, i, node->type, flags, "RBN", NULL);
207 for (i = 0; i < node->count; ++i) {
208 elm = &node->elms[i];
211 case HAMMER_BTREE_TYPE_INTERNAL:
212 if (search && search->filter) {
213 if (test_btree_search(elm, search) > 0 ||
214 test_btree_search(elm + 1, search) < 0)
217 if (elm->internal.subtree_offset) {
218 print_btree_node(elm->internal.subtree_offset,
220 elm->internal.mirror_tid,
221 &elm[0].base, &elm[1].base);
223 * Cause show to do normal iteration after
224 * seeking to the lo:objid:rectype:key:tid
227 if (search && search->filter == -1) /* default */
240 is_root_btree_beg(u_int8_t type, int i, hammer_btree_elm_t elm)
242 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
244 elm->base.localization == 0 &&
245 elm->base.obj_id == (int64_t)-0x8000000000000000LL &&
246 elm->base.key == (int64_t)-0x8000000000000000LL &&
247 elm->base.create_tid == 1 &&
248 elm->base.delete_tid == 1 &&
249 elm->base.rec_type == 0 &&
250 elm->base.obj_type == 0 &&
251 elm->base.btype != 0); /* depends on the original node */
256 is_root_btree_end(u_int8_t type, int i, hammer_btree_elm_t elm)
258 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
260 elm->base.localization == 0xFFFFFFFFU &&
261 elm->base.obj_id == 0x7FFFFFFFFFFFFFFFLL &&
262 elm->base.key == 0x7FFFFFFFFFFFFFFFLL &&
263 elm->base.create_tid == 0xFFFFFFFFFFFFFFFFULL &&
264 elm->base.delete_tid == 0 &&
265 elm->base.rec_type == 0xFFFFU &&
266 elm->base.obj_type == 0 &&
267 elm->base.btype == 0); /* not initialized */
272 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
273 int flags, const char *label, const char *ext)
275 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
279 flagstr[0] = flags ? 'B' : 'G';
280 if (flags & FLAG_TOOFARLEFT)
282 if (flags & FLAG_TOOFARRIGHT)
284 if (flags & FLAG_BADTYPE)
286 if (flags & FLAG_BADCHILDPARENT)
288 if (flags & FLAG_BADMIRRORTID)
292 * Check if elm is derived from root split
294 if (is_root_btree_beg(type, i, elm))
296 else if (is_root_btree_end(type, i, elm))
301 if (elm->base.delete_tid)
306 printf("%s\t%s %2d %c ",
308 (elm->base.btype ? elm->base.btype : '?'));
309 printf("lo=%08x obj=%016jx rt=%02x key=%016jx ot=%02x\n",
310 elm->base.localization,
311 (uintmax_t)elm->base.obj_id,
313 (uintmax_t)elm->base.key,
315 printf("\t %c tids %016jx:%016jx ",
316 (rootelm == ' ' ? deleted : rootelm),
317 (uintmax_t)elm->base.create_tid,
318 (uintmax_t)elm->base.delete_tid);
321 case HAMMER_BTREE_TYPE_INTERNAL:
322 printf("suboff=%016jx",
323 (uintmax_t)elm->internal.subtree_offset);
325 printf(" mirror %016jx",
326 (uintmax_t)elm->internal.mirror_tid);
331 case HAMMER_BTREE_TYPE_LEAF:
334 switch(elm->base.btype) {
335 case HAMMER_BTREE_TYPE_RECORD:
337 printf("\n%s\t ", check_data_crc(elm));
340 printf("dataoff=%016jx/%d",
341 (uintmax_t)elm->leaf.data_offset,
344 printf(" crc=%04x", elm->leaf.data_crc);
345 printf("\n\t fills=");
346 print_bigblock_fill(elm->leaf.data_offset);
361 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
362 hammer_btree_elm_t elm, u_int8_t btype,
363 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
368 case HAMMER_BTREE_TYPE_INTERNAL:
369 if (elm->internal.subtree_offset) {
370 struct buffer_info *buffer = NULL;
371 hammer_node_ondisk_t subnode;
373 subnode = get_node(elm->internal.subtree_offset,
376 flags |= FLAG_BADCHILDPARENT;
377 else if (subnode->parent != node_offset)
378 flags |= FLAG_BADCHILDPARENT;
381 if (elm->internal.mirror_tid > node->mirror_tid)
382 flags |= FLAG_BADMIRRORTID;
385 case HAMMER_BTREE_TYPE_INTERNAL:
386 if (left_bound == NULL || right_bound == NULL)
388 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
389 flags |= FLAG_TOOFARLEFT;
390 if (hammer_btree_cmp(&elm->base, right_bound) > 0)
391 flags |= FLAG_TOOFARRIGHT;
393 case HAMMER_BTREE_TYPE_LEAF:
394 if (left_bound == NULL || right_bound == NULL)
396 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
397 flags |= FLAG_TOOFARLEFT;
398 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
399 flags |= FLAG_TOOFARRIGHT;
402 flags |= FLAG_BADTYPE;
406 case HAMMER_BTREE_TYPE_LEAF:
407 if (node->mirror_tid == 0 &&
408 !(node->parent == 0 && node->count == 2)) {
409 flags |= FLAG_BADMIRRORTID;
411 if (elm->base.create_tid && node->mirror_tid &&
412 elm->base.create_tid > node->mirror_tid) {
413 flags |= FLAG_BADMIRRORTID;
415 if (elm->base.delete_tid && node->mirror_tid &&
416 elm->base.delete_tid > node->mirror_tid) {
417 flags |= FLAG_BADMIRRORTID;
420 case HAMMER_BTREE_TYPE_RECORD:
421 if (left_bound == NULL || right_bound == NULL)
423 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
424 flags |= FLAG_TOOFARLEFT;
425 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
426 flags |= FLAG_TOOFARRIGHT;
429 flags |= FLAG_BADTYPE;
434 flags |= FLAG_BADTYPE;
442 print_bigblock_fill(hammer_off_t offset)
444 struct hammer_blockmap_layer1 layer1;
445 struct hammer_blockmap_layer2 layer2;
449 blockmap_lookup(offset, &layer1, &layer2, &error);
451 printf("z%d:%lld=BADZ",
452 HAMMER_ZONE_DECODE(offset),
453 (offset & ~HAMMER_OFF_ZONE_MASK) /
457 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
460 printf("z%d:%lld=%d%%",
461 HAMMER_ZONE_DECODE(offset),
462 (offset & ~HAMMER_OFF_ZONE_MASK) /
463 HAMMER_BIGBLOCK_SIZE,
470 * Check the generic crc on a data element. Inodes record types are
471 * special in that some of their fields are not CRCed.
473 * Also check that the zone is valid.
477 check_data_crc(hammer_btree_elm_t elm)
479 struct buffer_info *data_buffer;
480 hammer_off_t data_offset;
487 data_offset = elm->leaf.data_offset;
488 data_len = elm->leaf.data_len;
490 if (data_offset == 0 || data_len == 0)
496 blockmap_lookup(data_offset, NULL, NULL, &error);
500 ptr = get_buffer_data(data_offset, &data_buffer, 0);
501 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
504 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
505 data_len == sizeof(struct hammer_inode_data)) {
506 crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
508 crc = crc32_ext(ptr, len, crc);
513 rel_buffer(data_buffer);
515 switch (error) { /* bad offset */
538 if (crc == elm->leaf.data_crc)
540 return("BX"); /* bad crc */
545 print_config(char *cfgtxt)
549 printf("\n%17stext=\"\n", "");
550 while((token = strsep(&cfgtxt, "\r\n")) != NULL) {
551 printf("%17s %s\n", "", token);
553 printf("%17s\"", "");
558 print_record(hammer_btree_elm_t elm)
560 struct buffer_info *data_buffer;
561 hammer_off_t data_offset;
563 hammer_data_ondisk_t data;
568 data_offset = elm->leaf.data_offset;
569 data_len = elm->leaf.data_len;
573 data = get_buffer_data(data_offset, &data_buffer, 0);
577 switch(elm->leaf.base.rec_type) {
578 case HAMMER_RECTYPE_UNKNOWN:
579 printf("\n%17s", "");
582 case HAMMER_RECTYPE_INODE:
583 printf("\n%17s", "");
584 printf("size=%jd nlinks=%jd",
585 (intmax_t)data->inode.size,
586 (intmax_t)data->inode.nlinks);
588 printf(" mode=%05o uflags=%08x\n",
592 printf("ctime=%016jx pobjid=%016jx obj_type=%d\n",
593 (uintmax_t)data->inode.ctime,
594 (uintmax_t)data->inode.parent_obj_id,
595 data->inode.obj_type);
597 printf("mtime=%016jx", (uintmax_t)data->inode.mtime);
598 printf(" caps=%02x", data->inode.cap_flags);
601 case HAMMER_RECTYPE_DIRENTRY:
602 printf("\n%17s", "");
603 data_len -= HAMMER_ENTRY_NAME_OFF;
604 printf("dir-entry ino=%016jx lo=%08x name=\"%*.*s\"",
605 (uintmax_t)data->entry.obj_id,
606 data->entry.localization,
607 data_len, data_len, data->entry.name);
609 case HAMMER_RECTYPE_FIX:
610 switch(elm->leaf.base.key) {
611 case HAMMER_FIXKEY_SYMLINK:
612 data_len -= HAMMER_SYMLINK_NAME_OFF;
613 printf("\n%17s", "");
614 printf("symlink=\"%*.*s\"", data_len, data_len,
621 case HAMMER_RECTYPE_PFS:
622 printf("\n%17s", "");
623 printf("sync_beg_tid=%016jx sync_end_tid=%016jx\n",
624 (intmax_t)data->pfsd.sync_beg_tid,
625 (intmax_t)data->pfsd.sync_end_tid);
626 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
627 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
629 printf("shared_uuid=%s\n", str1);
631 printf("unique_uuid=%s\n", str2);
633 printf("mirror_flags=%08x label=\"%s\"",
634 data->pfsd.mirror_flags, data->pfsd.label);
635 if (data->pfsd.snapshots[0])
636 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
640 case HAMMER_RECTYPE_SNAPSHOT:
641 printf("\n%17s", "");
642 printf("tid=%016jx label=\"%s\"",
643 (intmax_t)data->snap.tid, data->snap.label);
645 case HAMMER_RECTYPE_CONFIG:
646 if (VerboseOpt > 2) {
647 print_config(data->config.text);
650 case HAMMER_RECTYPE_DATA:
651 if (VerboseOpt > 3) {
653 hexdump(data, data_len, "\t\t ", 0);
656 case HAMMER_RECTYPE_EXT:
657 case HAMMER_RECTYPE_DB:
658 if (VerboseOpt > 2) {
660 hexdump(data, data_len, "\t\t ", 0);
666 rel_buffer(data_buffer);
671 _strtoul(const char *p, int base)
673 unsigned long retval;
675 errno = 0; /* clear */
676 retval = strtoul(p, NULL, base);
677 if (errno == ERANGE && retval == ULONG_MAX)
684 _strtoull(const char *p, int base)
686 unsigned long long retval;
688 errno = 0; /* clear */
689 retval = strtoull(p, NULL, base);
690 if (errno == ERANGE && retval == ULLONG_MAX)
696 init_btree_search(const char *arg, int filter, btree_search_t search)
702 search->obj_id = (int64_t)HAMMER_MIN_OBJID;
703 search->rec_type = HAMMER_RECTYPE_LOWEST;
705 search->create_tid = 0;
707 search->filter = filter;
713 while ((p = s) != NULL) {
714 if ((s = strchr(s, ':')) != NULL)
717 search->lo = _strtoul(p, 16);
719 search->obj_id = _strtoull(p, 16);
721 search->rec_type = _strtoul(p, 16);
723 search->key = _strtoull(p, 16);
725 search->create_tid = _strtoull(p, 16);
735 test_btree_search(hammer_btree_elm_t elm, btree_search_t search)
737 hammer_base_elm_t base = &elm->base;
740 if (base->localization < search->lo)
742 if (base->localization > search->lo)
746 if (base->obj_id < search->obj_id)
748 if (base->obj_id > search->obj_id)
750 if (search->limit == 2)
751 return(0); /* ignore below */
753 if (base->rec_type < search->rec_type)
755 if (base->rec_type > search->rec_type)
757 if (search->limit == 3)
758 return(0); /* ignore below */
760 if (base->key < search->key)
762 if (base->key > search->key)
764 if (search->limit == 4)
765 return(0); /* ignore below */
767 if (base->create_tid == 0) {
768 if (search->create_tid == 0)
772 if (search->create_tid == 0)
774 if (base->create_tid < search->create_tid)
776 if (base->create_tid > search->create_tid)
785 hammer_cmd_show_undo(void)
787 struct volume_info *volume;
788 hammer_blockmap_t rootmap;
789 hammer_off_t scan_offset;
790 hammer_fifo_any_t head;
791 struct buffer_info *data_buffer = NULL;
793 volume = get_volume(RootVolNo);
794 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
795 printf("Volume header UNDO %016jx-%016jx/%016jx\n",
796 (intmax_t)rootmap->first_offset,
797 (intmax_t)rootmap->next_offset,
798 (intmax_t)rootmap->alloc_offset);
799 printf("Undo map is %jdMB\n",
800 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
802 scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
803 while (scan_offset < rootmap->alloc_offset) {
804 head = get_buffer_data(scan_offset, &data_buffer, 0);
805 printf("%016jx ", scan_offset);
807 switch(head->head.hdr_type) {
808 case HAMMER_HEAD_TYPE_PAD:
809 printf("PAD(%04x)\n", head->head.hdr_size);
811 case HAMMER_HEAD_TYPE_DUMMY:
812 printf("DUMMY(%04x) seq=%08x\n",
813 head->head.hdr_size, head->head.hdr_seq);
815 case HAMMER_HEAD_TYPE_UNDO:
816 printf("UNDO(%04x) seq=%08x "
817 "dataoff=%016jx bytes=%d\n",
818 head->head.hdr_size, head->head.hdr_seq,
819 (intmax_t)head->undo.undo_offset,
820 head->undo.undo_data_bytes);
822 case HAMMER_HEAD_TYPE_REDO:
823 printf("REDO(%04x) seq=%08x flags=%08x "
824 "objid=%016jx logoff=%016jx bytes=%d\n",
825 head->head.hdr_size, head->head.hdr_seq,
826 head->redo.redo_flags,
827 (intmax_t)head->redo.redo_objid,
828 (intmax_t)head->redo.redo_offset,
829 head->redo.redo_data_bytes);
832 printf("UNKNOWN(%04x,%04x) seq=%08x\n",
838 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
839 head->head.hdr_size == 0 ||
840 head->head.hdr_size > HAMMER_UNDO_ALIGN -
841 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
842 printf("Illegal size field, skipping to "
844 scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
847 scan_offset += head->head.hdr_size;
850 rel_buffer(data_buffer);