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 {
47 int normal; /* normal iteration */
50 static void print_btree_node(hammer_off_t node_offset, btree_search_t search,
51 int depth, hammer_tid_t mirror_tid,
52 hammer_base_elm_t left_bound,
53 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;
103 printf("show %016jx lo %08x obj_id %016jx depth %d\n",
104 (uintmax_t)node_offset, lo, (uintmax_t)obj_id, depth);
106 print_btree_node(node_offset, searchp, depth, HAMMER_MAX_TID,
107 left_bound, right_bound);
113 print_btree_node(hammer_off_t node_offset, btree_search_t search,
114 int depth, hammer_tid_t mirror_tid,
115 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)
140 if (node->mirror_tid <= mirror_tid) {
147 printf("%c%c NODE %016jx cnt=%02d p=%016jx "
151 (uintmax_t)node_offset, node->count,
152 (uintmax_t)node->parent,
153 (node->type ? node->type : '?'), depth);
154 printf(" mirror %016jx", (uintmax_t)node->mirror_tid);
157 print_bigblock_fill(node_offset);
161 maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ?
162 HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS;
164 for (i = 0; i < node->count && i < maxcount; ++i) {
165 elm = &node->elms[i];
167 if (node->type != HAMMER_BTREE_TYPE_INTERNAL) {
170 elm->base.localization == search->lo &&
171 elm->base.obj_id == search->obj_id) {
176 if (elm->base.localization > search->lo ||
177 (elm->base.localization == search->lo &&
178 elm->base.obj_id > search->obj_id)) {
181 if (elm[1].base.localization < search->lo ||
182 (elm[1].base.localization == search->lo &&
183 elm[1].base.obj_id < search->obj_id)) {
190 flags = get_elm_flags(node, node_offset,
191 elm, elm->base.btype,
192 left_bound, right_bound);
193 print_btree_elm(elm, i, node->type, flags, "ELM", ext);
195 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
196 elm = &node->elms[i];
198 flags = get_elm_flags(node, node_offset,
200 left_bound, right_bound);
201 print_btree_elm(elm, i, node->type, flags, "RBN", NULL);
205 for (i = 0; i < node->count; ++i) {
206 elm = &node->elms[i];
209 case HAMMER_BTREE_TYPE_INTERNAL:
210 if (search->normal == 0) {
211 if (elm->base.localization > search->lo ||
212 (elm->base.localization == search->lo &&
213 elm->base.obj_id > search->obj_id)) {
216 if (elm[1].base.localization < search->lo ||
217 (elm[1].base.localization == search->lo &&
218 elm[1].base.obj_id < search->obj_id)) {
222 if (elm->internal.subtree_offset) {
223 print_btree_node(elm->internal.subtree_offset,
225 elm->internal.mirror_tid,
226 &elm[0].base, &elm[1].base);
228 * Cause show to do normal iteration after
229 * seeking to the lo:objid
243 is_root_btree_beg(u_int8_t type, int i, hammer_btree_elm_t elm)
245 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
247 elm->base.localization == 0 &&
248 elm->base.obj_id == (int64_t)-0x8000000000000000LL &&
249 elm->base.key == (int64_t)-0x8000000000000000LL &&
250 elm->base.create_tid == 1 &&
251 elm->base.delete_tid == 1 &&
252 elm->base.rec_type == 0 &&
253 elm->base.obj_type == 0 &&
254 elm->base.btype != 0); /* depends on the original node */
259 is_root_btree_end(u_int8_t type, int i, hammer_btree_elm_t elm)
261 return (type == HAMMER_BTREE_TYPE_INTERNAL &&
263 elm->base.localization == 0xFFFFFFFFU &&
264 elm->base.obj_id == 0x7FFFFFFFFFFFFFFFLL &&
265 elm->base.key == 0x7FFFFFFFFFFFFFFFLL &&
266 elm->base.create_tid == 0xFFFFFFFFFFFFFFFFULL &&
267 elm->base.delete_tid == 0 &&
268 elm->base.rec_type == 0xFFFFU &&
269 elm->base.obj_type == 0 &&
270 elm->base.btype == 0); /* not initialized */
275 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
276 int flags, const char *label, const char *ext)
278 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
282 flagstr[0] = flags ? 'B' : 'G';
283 if (flags & FLAG_TOOFARLEFT)
285 if (flags & FLAG_TOOFARRIGHT)
287 if (flags & FLAG_BADTYPE)
289 if (flags & FLAG_BADCHILDPARENT)
291 if (flags & FLAG_BADMIRRORTID)
295 * Check if elm is derived from root split
297 if (is_root_btree_beg(type, i, elm))
299 else if (is_root_btree_end(type, i, elm))
304 if (elm->base.delete_tid)
309 printf("%s\t%s %2d %c ",
311 (elm->base.btype ? elm->base.btype : '?'));
312 printf("lo=%08x obj=%016jx rt=%02x key=%016jx ot=%02x\n",
313 elm->base.localization,
314 (uintmax_t)elm->base.obj_id,
316 (uintmax_t)elm->base.key,
318 printf("\t %c tids %016jx:%016jx ",
319 (rootelm == ' ' ? deleted : rootelm),
320 (uintmax_t)elm->base.create_tid,
321 (uintmax_t)elm->base.delete_tid);
324 case HAMMER_BTREE_TYPE_INTERNAL:
325 printf("suboff=%016jx",
326 (uintmax_t)elm->internal.subtree_offset);
328 printf(" mirror %016jx",
329 (uintmax_t)elm->internal.mirror_tid);
334 case HAMMER_BTREE_TYPE_LEAF:
337 switch(elm->base.btype) {
338 case HAMMER_BTREE_TYPE_RECORD:
340 printf("\n%s\t ", check_data_crc(elm));
343 printf("dataoff=%016jx/%d",
344 (uintmax_t)elm->leaf.data_offset,
347 printf(" crc=%04x", elm->leaf.data_crc);
348 printf("\n\t fills=");
349 print_bigblock_fill(elm->leaf.data_offset);
364 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
365 hammer_btree_elm_t elm, u_int8_t btype,
366 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
371 case HAMMER_BTREE_TYPE_INTERNAL:
372 if (elm->internal.subtree_offset) {
373 struct buffer_info *buffer = NULL;
374 hammer_node_ondisk_t subnode;
376 subnode = get_node(elm->internal.subtree_offset,
379 flags |= FLAG_BADCHILDPARENT;
380 else if (subnode->parent != node_offset)
381 flags |= FLAG_BADCHILDPARENT;
384 if (elm->internal.mirror_tid > node->mirror_tid)
385 flags |= FLAG_BADMIRRORTID;
388 case HAMMER_BTREE_TYPE_INTERNAL:
389 if (left_bound == NULL || right_bound == NULL)
391 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
392 flags |= FLAG_TOOFARLEFT;
393 if (hammer_btree_cmp(&elm->base, right_bound) > 0)
394 flags |= FLAG_TOOFARRIGHT;
396 case HAMMER_BTREE_TYPE_LEAF:
397 if (left_bound == NULL || right_bound == NULL)
399 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
400 flags |= FLAG_TOOFARLEFT;
401 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
402 flags |= FLAG_TOOFARRIGHT;
405 flags |= FLAG_BADTYPE;
409 case HAMMER_BTREE_TYPE_LEAF:
410 if (node->mirror_tid == 0 &&
411 !(node->parent == 0 && node->count == 2)) {
412 flags |= FLAG_BADMIRRORTID;
414 if (elm->base.create_tid && node->mirror_tid &&
415 elm->base.create_tid > node->mirror_tid) {
416 flags |= FLAG_BADMIRRORTID;
418 if (elm->base.delete_tid && node->mirror_tid &&
419 elm->base.delete_tid > node->mirror_tid) {
420 flags |= FLAG_BADMIRRORTID;
423 case HAMMER_BTREE_TYPE_RECORD:
424 if (left_bound == NULL || right_bound == NULL)
426 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
427 flags |= FLAG_TOOFARLEFT;
428 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
429 flags |= FLAG_TOOFARRIGHT;
432 flags |= FLAG_BADTYPE;
437 flags |= FLAG_BADTYPE;
445 print_bigblock_fill(hammer_off_t offset)
447 struct hammer_blockmap_layer1 layer1;
448 struct hammer_blockmap_layer2 layer2;
452 blockmap_lookup(offset, &layer1, &layer2, &error);
454 printf("z%d:%lld=BADZ",
455 HAMMER_ZONE_DECODE(offset),
456 (offset & ~HAMMER_OFF_ZONE_MASK) /
460 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
463 printf("z%d:%lld=%d%%",
464 HAMMER_ZONE_DECODE(offset),
465 (offset & ~HAMMER_OFF_ZONE_MASK) /
466 HAMMER_BIGBLOCK_SIZE,
473 * Check the generic crc on a data element. Inodes record types are
474 * special in that some of their fields are not CRCed.
476 * Also check that the zone is valid.
480 check_data_crc(hammer_btree_elm_t elm)
482 struct buffer_info *data_buffer;
483 hammer_off_t data_offset;
490 data_offset = elm->leaf.data_offset;
491 data_len = elm->leaf.data_len;
493 if (data_offset == 0 || data_len == 0)
499 blockmap_lookup(data_offset, NULL, NULL, &error);
503 ptr = get_buffer_data(data_offset, &data_buffer, 0);
504 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
507 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
508 data_len == sizeof(struct hammer_inode_data)) {
509 crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
511 crc = crc32_ext(ptr, len, crc);
517 rel_buffer(data_buffer);
519 switch (error) { /* bad offset */
542 if (crc == elm->leaf.data_crc)
544 return("BX"); /* bad crc */
549 print_config(char *cfgtxt)
553 printf("\n%17stext=\"\n", "");
554 while((token = strsep(&cfgtxt, "\r\n")) != NULL) {
555 printf("%17s %s\n", "", token);
557 printf("%17s\"", "");
562 print_record(hammer_btree_elm_t elm)
564 struct buffer_info *data_buffer;
565 hammer_off_t data_offset;
567 hammer_data_ondisk_t data;
572 data_offset = elm->leaf.data_offset;
573 data_len = elm->leaf.data_len;
577 data = get_buffer_data(data_offset, &data_buffer, 0);
581 switch(elm->leaf.base.rec_type) {
582 case HAMMER_RECTYPE_UNKNOWN:
583 printf("\n%17s", "");
586 case HAMMER_RECTYPE_INODE:
587 printf("\n%17s", "");
588 printf("size=%jd nlinks=%jd",
589 (intmax_t)data->inode.size,
590 (intmax_t)data->inode.nlinks);
592 printf(" mode=%05o uflags=%08x\n",
596 printf("ctime=%016jx pobjid=%016jx obj_type=%d\n",
597 (uintmax_t)data->inode.ctime,
598 (uintmax_t)data->inode.parent_obj_id,
599 data->inode.obj_type);
601 printf("mtime=%016jx", (uintmax_t)data->inode.mtime);
602 printf(" caps=%02x", data->inode.cap_flags);
605 case HAMMER_RECTYPE_DIRENTRY:
606 printf("\n%17s", "");
607 data_len -= HAMMER_ENTRY_NAME_OFF;
608 printf("dir-entry ino=%016jx lo=%08x name=\"%*.*s\"",
609 (uintmax_t)data->entry.obj_id,
610 data->entry.localization,
611 data_len, data_len, data->entry.name);
613 case HAMMER_RECTYPE_FIX:
614 switch(elm->leaf.base.key) {
615 case HAMMER_FIXKEY_SYMLINK:
616 data_len -= HAMMER_SYMLINK_NAME_OFF;
617 printf("\n%17s", "");
618 printf("symlink=\"%*.*s\"", data_len, data_len,
625 case HAMMER_RECTYPE_PFS:
626 printf("\n%17s", "");
627 printf("sync_beg_tid=%016jx sync_end_tid=%016jx\n",
628 (intmax_t)data->pfsd.sync_beg_tid,
629 (intmax_t)data->pfsd.sync_end_tid);
630 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
631 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
633 printf("shared_uuid=%s\n", str1);
635 printf("unique_uuid=%s\n", str2);
637 printf("mirror_flags=%08x label=\"%s\"",
638 data->pfsd.mirror_flags, data->pfsd.label);
639 if (data->pfsd.snapshots[0])
640 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
644 case HAMMER_RECTYPE_SNAPSHOT:
645 printf("\n%17s", "");
646 printf("tid=%016jx label=\"%s\"",
647 (intmax_t)data->snap.tid, data->snap.label);
649 case HAMMER_RECTYPE_CONFIG:
650 if (VerboseOpt > 2) {
651 print_config(data->config.text);
654 case HAMMER_RECTYPE_DATA:
655 if (VerboseOpt > 3) {
657 hexdump(data, data_len, "\t\t ", 0);
660 case HAMMER_RECTYPE_EXT:
661 case HAMMER_RECTYPE_DB:
662 if (VerboseOpt > 2) {
664 hexdump(data, data_len, "\t\t ", 0);
671 rel_buffer(data_buffer);
678 hammer_cmd_show_undo(void)
680 struct volume_info *volume;
681 hammer_blockmap_t rootmap;
682 hammer_off_t scan_offset;
683 hammer_fifo_any_t head;
684 struct buffer_info *data_buffer = NULL;
686 volume = get_volume(RootVolNo);
687 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
688 printf("Volume header UNDO %016jx-%016jx/%016jx\n",
689 (intmax_t)rootmap->first_offset,
690 (intmax_t)rootmap->next_offset,
691 (intmax_t)rootmap->alloc_offset);
692 printf("Undo map is %jdMB\n",
693 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
695 scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
696 while (scan_offset < rootmap->alloc_offset) {
697 head = get_buffer_data(scan_offset, &data_buffer, 0);
698 printf("%016jx ", scan_offset);
700 switch(head->head.hdr_type) {
701 case HAMMER_HEAD_TYPE_PAD:
702 printf("PAD(%04x)\n", head->head.hdr_size);
704 case HAMMER_HEAD_TYPE_DUMMY:
705 printf("DUMMY(%04x) seq=%08x\n",
706 head->head.hdr_size, head->head.hdr_seq);
708 case HAMMER_HEAD_TYPE_UNDO:
709 printf("UNDO(%04x) seq=%08x "
710 "dataoff=%016jx bytes=%d\n",
711 head->head.hdr_size, head->head.hdr_seq,
712 (intmax_t)head->undo.undo_offset,
713 head->undo.undo_data_bytes);
715 case HAMMER_HEAD_TYPE_REDO:
716 printf("REDO(%04x) seq=%08x flags=%08x "
717 "objid=%016jx logoff=%016jx bytes=%d\n",
718 head->head.hdr_size, head->head.hdr_seq,
719 head->redo.redo_flags,
720 (intmax_t)head->redo.redo_objid,
721 (intmax_t)head->redo.redo_offset,
722 head->redo.redo_data_bytes);
725 printf("UNKNOWN(%04x,%04x) seq=%08x\n",
731 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
732 head->head.hdr_size == 0 ||
733 head->head.hdr_size > HAMMER_UNDO_ALIGN -
734 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
735 printf("Illegal size field, skipping to "
737 scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
740 scan_offset += head->head.hdr_size;
744 rel_buffer(data_buffer);