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 elm->internal.mirror_tid == 0)
277 deleted = ' '; /* could use unique mark */
284 printf("%s\t%s %2d %c ",
286 (elm->base.btype ? elm->base.btype : '?'));
287 printf("lo=%08x obj=%016jx rt=%02x key=%016jx ot=%02x\n",
288 elm->base.localization,
289 (uintmax_t)elm->base.obj_id,
291 (uintmax_t)elm->base.key,
293 printf("\t %c tids %016jx:%016jx ",
295 (uintmax_t)elm->base.create_tid,
296 (uintmax_t)elm->base.delete_tid);
299 case HAMMER_BTREE_TYPE_INTERNAL:
300 printf("suboff=%016jx",
301 (uintmax_t)elm->internal.subtree_offset);
303 printf(" mirror %016jx",
304 (uintmax_t)elm->internal.mirror_tid);
309 case HAMMER_BTREE_TYPE_LEAF:
312 switch(elm->base.btype) {
313 case HAMMER_BTREE_TYPE_RECORD:
315 printf("\n%s\t ", check_data_crc(elm));
318 printf("dataoff=%016jx/%d",
319 (uintmax_t)elm->leaf.data_offset,
322 printf(" crc=%04x", elm->leaf.data_crc);
323 printf("\n\t fills=");
324 print_bigblock_fill(elm->leaf.data_offset);
339 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
340 hammer_btree_elm_t elm, u_int8_t btype,
341 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
346 case HAMMER_BTREE_TYPE_INTERNAL:
347 if (elm->internal.subtree_offset) {
348 struct buffer_info *buffer = NULL;
349 hammer_node_ondisk_t subnode;
351 subnode = get_node(elm->internal.subtree_offset,
354 flags |= FLAG_BADCHILDPARENT;
355 else if (subnode->parent != node_offset)
356 flags |= FLAG_BADCHILDPARENT;
359 if (elm->internal.mirror_tid > node->mirror_tid)
360 flags |= FLAG_BADMIRRORTID;
363 case HAMMER_BTREE_TYPE_INTERNAL:
364 if (left_bound == NULL || right_bound == NULL)
366 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
367 flags |= FLAG_TOOFARLEFT;
368 if (hammer_btree_cmp(&elm->base, right_bound) > 0)
369 flags |= FLAG_TOOFARRIGHT;
371 case HAMMER_BTREE_TYPE_LEAF:
372 if (left_bound == NULL || right_bound == NULL)
374 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
375 flags |= FLAG_TOOFARLEFT;
376 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
377 flags |= FLAG_TOOFARRIGHT;
380 flags |= FLAG_BADTYPE;
384 case HAMMER_BTREE_TYPE_LEAF:
385 if (node->mirror_tid == 0 &&
386 !(node->parent == 0 && node->count == 2)) {
387 flags |= FLAG_BADMIRRORTID;
389 if (elm->base.create_tid && node->mirror_tid &&
390 elm->base.create_tid > node->mirror_tid) {
391 flags |= FLAG_BADMIRRORTID;
393 if (elm->base.delete_tid && node->mirror_tid &&
394 elm->base.delete_tid > node->mirror_tid) {
395 flags |= FLAG_BADMIRRORTID;
398 case HAMMER_BTREE_TYPE_RECORD:
399 if (left_bound == NULL || right_bound == NULL)
401 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
402 flags |= FLAG_TOOFARLEFT;
403 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
404 flags |= FLAG_TOOFARRIGHT;
407 flags |= FLAG_BADTYPE;
412 flags |= FLAG_BADTYPE;
420 print_bigblock_fill(hammer_off_t offset)
422 struct hammer_blockmap_layer1 layer1;
423 struct hammer_blockmap_layer2 layer2;
427 blockmap_lookup(offset, &layer1, &layer2, &error);
429 printf("z%d:%lld=BADZ",
430 HAMMER_ZONE_DECODE(offset),
431 (offset & ~HAMMER_OFF_ZONE_MASK) /
435 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
438 printf("z%d:%lld=%d%%",
439 HAMMER_ZONE_DECODE(offset),
440 (offset & ~HAMMER_OFF_ZONE_MASK) /
441 HAMMER_BIGBLOCK_SIZE,
448 * Check the generic crc on a data element. Inodes record types are
449 * special in that some of their fields are not CRCed.
451 * Also check that the zone is valid.
455 check_data_crc(hammer_btree_elm_t elm)
457 struct buffer_info *data_buffer;
458 hammer_off_t data_offset;
465 data_offset = elm->leaf.data_offset;
466 data_len = elm->leaf.data_len;
468 if (data_offset == 0 || data_len == 0)
474 blockmap_lookup(data_offset, NULL, NULL, &error);
478 ptr = get_buffer_data(data_offset, &data_buffer, 0);
479 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
482 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
483 data_len == sizeof(struct hammer_inode_data)) {
484 crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
486 crc = crc32_ext(ptr, len, crc);
492 rel_buffer(data_buffer);
494 return("BO"); /* bad offset */
495 if (crc == elm->leaf.data_crc)
497 return("BX"); /* bad crc */
502 print_config(char *cfgtxt)
506 printf("\n%17stext=\"\n", "");
507 while((token = strsep(&cfgtxt, "\r\n")) != NULL) {
508 printf("%17s %s\n", "", token);
510 printf("%17s\"", "");
515 print_record(hammer_btree_elm_t elm)
517 struct buffer_info *data_buffer;
518 hammer_off_t data_offset;
520 hammer_data_ondisk_t data;
525 data_offset = elm->leaf.data_offset;
526 data_len = elm->leaf.data_len;
530 data = get_buffer_data(data_offset, &data_buffer, 0);
534 switch(elm->leaf.base.rec_type) {
535 case HAMMER_RECTYPE_UNKNOWN:
536 printf("\n%17s", "");
539 case HAMMER_RECTYPE_INODE:
540 printf("\n%17s", "");
541 printf("size=%jd nlinks=%jd",
542 (intmax_t)data->inode.size,
543 (intmax_t)data->inode.nlinks);
545 printf(" mode=%05o uflags=%08x\n",
549 printf("ctime=%016jx pobjid=%016jx obj_type=%d\n",
550 (uintmax_t)data->inode.ctime,
551 (uintmax_t)data->inode.parent_obj_id,
552 data->inode.obj_type);
554 printf("mtime=%016jx", (uintmax_t)data->inode.mtime);
555 printf(" caps=%02x", data->inode.cap_flags);
558 case HAMMER_RECTYPE_DIRENTRY:
559 printf("\n%17s", "");
560 data_len -= HAMMER_ENTRY_NAME_OFF;
561 printf("dir-entry ino=%016jx lo=%08x name=\"%*.*s\"",
562 (uintmax_t)data->entry.obj_id,
563 data->entry.localization,
564 data_len, data_len, data->entry.name);
566 case HAMMER_RECTYPE_FIX:
567 switch(elm->leaf.base.key) {
568 case HAMMER_FIXKEY_SYMLINK:
569 data_len -= HAMMER_SYMLINK_NAME_OFF;
570 printf("\n%17s", "");
571 printf("symlink=\"%*.*s\"", data_len, data_len,
578 case HAMMER_RECTYPE_PFS:
579 printf("\n%17s", "");
580 printf("sync_beg_tid=%016jx sync_end_tid=%016jx\n",
581 (intmax_t)data->pfsd.sync_beg_tid,
582 (intmax_t)data->pfsd.sync_end_tid);
583 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
584 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
586 printf("shared_uuid=%s\n", str1);
588 printf("unique_uuid=%s\n", str2);
590 printf("mirror_flags=%08x label=\"%s\"",
591 data->pfsd.mirror_flags, data->pfsd.label);
592 if (data->pfsd.snapshots[0])
593 printf(" snapshots=\"%s\"", data->pfsd.snapshots);
597 case HAMMER_RECTYPE_SNAPSHOT:
598 printf("\n%17s", "");
599 printf("tid=%016jx label=\"%s\"",
600 (intmax_t)data->snap.tid, data->snap.label);
602 case HAMMER_RECTYPE_CONFIG:
603 if (VerboseOpt > 2) {
604 print_config(data->config.text);
607 case HAMMER_RECTYPE_DATA:
608 if (VerboseOpt > 3) {
610 hexdump(data, data_len, "\t\t ", 0);
613 case HAMMER_RECTYPE_EXT:
614 case HAMMER_RECTYPE_DB:
615 if (VerboseOpt > 2) {
617 hexdump(data, data_len, "\t\t ", 0);
624 rel_buffer(data_buffer);
631 hammer_cmd_show_undo(void)
633 struct volume_info *volume;
634 hammer_blockmap_t rootmap;
635 hammer_off_t scan_offset;
636 hammer_fifo_any_t head;
637 struct buffer_info *data_buffer = NULL;
639 volume = get_volume(RootVolNo);
640 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
641 printf("Volume header UNDO %016jx-%016jx/%016jx\n",
642 (intmax_t)rootmap->first_offset,
643 (intmax_t)rootmap->next_offset,
644 (intmax_t)rootmap->alloc_offset);
645 printf("Undo map is %jdMB\n",
646 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
648 scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
649 while (scan_offset < rootmap->alloc_offset) {
650 head = get_buffer_data(scan_offset, &data_buffer, 0);
651 printf("%016jx ", scan_offset);
653 switch(head->head.hdr_type) {
654 case HAMMER_HEAD_TYPE_PAD:
655 printf("PAD(%04x)\n", head->head.hdr_size);
657 case HAMMER_HEAD_TYPE_DUMMY:
658 printf("DUMMY(%04x) seq=%08x\n",
659 head->head.hdr_size, head->head.hdr_seq);
661 case HAMMER_HEAD_TYPE_UNDO:
662 printf("UNDO(%04x) seq=%08x "
663 "dataoff=%016jx bytes=%d\n",
664 head->head.hdr_size, head->head.hdr_seq,
665 (intmax_t)head->undo.undo_offset,
666 head->undo.undo_data_bytes);
668 case HAMMER_HEAD_TYPE_REDO:
669 printf("REDO(%04x) seq=%08x flags=%08x "
670 "objid=%016jx logoff=%016jx bytes=%d\n",
671 head->head.hdr_size, head->head.hdr_seq,
672 head->redo.redo_flags,
673 (intmax_t)head->redo.redo_objid,
674 (intmax_t)head->redo.redo_offset,
675 head->redo.redo_data_bytes);
678 printf("UNKNOWN(%04x,%04x) seq=%08x\n",
684 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
685 head->head.hdr_size == 0 ||
686 head->head.hdr_size > HAMMER_UNDO_ALIGN -
687 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
688 printf("Illegal size field, skipping to "
690 scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
693 scan_offset += head->head.hdr_size;
697 rel_buffer(data_buffer);