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
37 #define FLAG_TOOFARLEFT 0x0001
38 #define FLAG_TOOFARRIGHT 0x0002
39 #define FLAG_BADTYPE 0x0004
40 #define FLAG_BADCHILDPARENT 0x0008
41 #define FLAG_BADMIRRORTID 0x0010
43 typedef struct btree_search {
48 static void print_btree_node(hammer_off_t node_offset, btree_search_t search,
49 int depth, int spike, hammer_tid_t mirror_tid,
50 hammer_base_elm_t left_bound,
51 hammer_base_elm_t right_bound);
52 static const char *check_data_crc(hammer_btree_elm_t elm);
53 static void print_record(hammer_btree_elm_t elm);
54 static void print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
55 int flags, const char *label, const char *ext);
56 static int print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
57 hammer_btree_elm_t elm, u_int8_t btype,
58 hammer_base_elm_t left_bound,
59 hammer_base_elm_t right_bound);
60 static void print_bigblock_fill(hammer_off_t offset);
63 hammer_cmd_show(hammer_off_t node_offset, u_int32_t lo, int64_t obj_id,
65 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
67 struct volume_info *volume;
68 struct btree_search search;
69 btree_search_t searchp;
74 if (node_offset == (hammer_off_t)-1) {
75 volume = get_volume(RootVolNo);
76 node_offset = volume->ondisk->vol0_btree_root;
78 printf("Volume header\trecords=%jd next_tid=%016jx\n",
79 (intmax_t)volume->ondisk->vol0_stat_records,
80 (uintmax_t)volume->ondisk->vol0_next_tid);
81 printf("\t\tbufoffset=%016jx\n",
82 (uintmax_t)volume->ondisk->vol_buf_beg);
83 for (zone = 0; zone < HAMMER_MAX_ZONES; ++zone) {
84 printf("\t\tzone %d\tnext_offset=%016jx\n",
86 (uintmax_t)volume->ondisk->vol0_blockmap[zone].next_offset
93 if (lo == 0 && obj_id == (int64_t)HAMMER_MIN_OBJID) {
95 printf("show %016jx depth %d\n", (uintmax_t)node_offset, depth);
98 search.obj_id = obj_id;
100 printf("show %016jx lo %08x obj_id %016jx depth %d\n",
101 (uintmax_t)node_offset, lo, (uintmax_t)obj_id, depth);
103 print_btree_node(node_offset, searchp, depth, 0, HAMMER_MAX_TID,
104 left_bound, right_bound);
105 print_btree_node(node_offset, searchp, depth, 1, HAMMER_MAX_TID,
106 left_bound, right_bound);
112 print_btree_node(hammer_off_t node_offset, btree_search_t search,
113 int depth, int spike, hammer_tid_t mirror_tid,
114 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
116 struct buffer_info *buffer = NULL;
117 hammer_node_ondisk_t node;
118 hammer_btree_elm_t elm;
126 node = get_node(node_offset, &buffer);
129 printf("BI NODE %016jx (IO ERROR)\n",
130 (uintmax_t)node_offset);
134 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
139 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 = print_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 = print_elm_flags(node, node_offset,
200 left_bound, right_bound);
201 print_btree_elm(elm, i, node->type, flags, "RBN", NULL);
206 for (i = 0; i < node->count; ++i) {
207 elm = &node->elms[i];
210 case HAMMER_BTREE_TYPE_INTERNAL:
212 if (elm->base.localization > search->lo ||
213 (elm->base.localization == search->lo &&
214 elm->base.obj_id > search->obj_id)) {
217 if (elm[1].base.localization < search->lo ||
218 (elm[1].base.localization == search->lo &&
219 elm[1].base.obj_id < search->obj_id)) {
223 if (elm->internal.subtree_offset) {
224 print_btree_node(elm->internal.subtree_offset,
225 search, depth + 1, spike,
226 elm->internal.mirror_tid,
227 &elm[0].base, &elm[1].base);
229 * Cause show to iterate after seeking to
244 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
245 int flags, const char *label, const char *ext)
247 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
249 flagstr[0] = flags ? 'B' : 'G';
250 if (flags & FLAG_TOOFARLEFT)
252 if (flags & FLAG_TOOFARRIGHT)
254 if (flags & FLAG_BADTYPE)
256 if (flags & FLAG_BADCHILDPARENT)
258 if (flags & FLAG_BADMIRRORTID)
261 printf("%s\t%s %2d %c ",
263 (elm->base.btype ? elm->base.btype : '?'));
264 printf("obj=%016jx key=%016jx lo=%08x rt=%02x ot=%02x\n",
265 (uintmax_t)elm->base.obj_id,
266 (uintmax_t)elm->base.key,
267 elm->base.localization,
270 printf("\t %c tids %016jx:%016jx ",
271 (elm->base.delete_tid ? 'd' : ' '),
272 (uintmax_t)elm->base.create_tid,
273 (uintmax_t)elm->base.delete_tid);
276 case HAMMER_BTREE_TYPE_INTERNAL:
277 printf("suboff=%016jx",
278 (uintmax_t)elm->internal.subtree_offset);
280 printf(" mirror %016jx",
281 (uintmax_t)elm->internal.mirror_tid);
286 case HAMMER_BTREE_TYPE_LEAF:
289 switch(elm->base.btype) {
290 case HAMMER_BTREE_TYPE_RECORD:
292 printf("\n%s\t ", check_data_crc(elm));
295 printf("dataoff=%016jx/%d",
296 (uintmax_t)elm->leaf.data_offset,
299 printf(" crc=%04x", elm->leaf.data_crc);
300 printf("\n\t fills=");
301 print_bigblock_fill(elm->leaf.data_offset);
316 print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
317 hammer_btree_elm_t elm, u_int8_t btype,
318 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
323 case HAMMER_BTREE_TYPE_INTERNAL:
324 if (elm->internal.subtree_offset) {
325 struct buffer_info *buffer = NULL;
326 hammer_node_ondisk_t subnode;
328 subnode = get_node(elm->internal.subtree_offset,
331 flags |= FLAG_BADCHILDPARENT;
332 else if (subnode->parent != node_offset)
333 flags |= FLAG_BADCHILDPARENT;
336 if (elm->internal.mirror_tid > node->mirror_tid)
337 flags |= FLAG_BADMIRRORTID;
340 case HAMMER_BTREE_TYPE_INTERNAL:
341 if (left_bound == NULL || right_bound == NULL)
343 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
344 flags |= FLAG_TOOFARLEFT;
345 if (hammer_btree_cmp(&elm->base, right_bound) > 0)
346 flags |= FLAG_TOOFARRIGHT;
348 case HAMMER_BTREE_TYPE_LEAF:
349 if (left_bound == NULL || right_bound == NULL)
351 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
352 flags |= FLAG_TOOFARLEFT;
353 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
354 flags |= FLAG_TOOFARRIGHT;
357 flags |= FLAG_BADTYPE;
361 case HAMMER_BTREE_TYPE_LEAF:
362 if (elm->base.create_tid &&
363 elm->base.create_tid > node->mirror_tid) {
364 flags |= FLAG_BADMIRRORTID;
366 if (elm->base.delete_tid &&
367 elm->base.delete_tid > node->mirror_tid) {
368 flags |= FLAG_BADMIRRORTID;
371 case HAMMER_BTREE_TYPE_RECORD:
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;
385 flags |= FLAG_BADTYPE;
393 print_bigblock_fill(hammer_off_t offset)
395 struct hammer_blockmap_layer1 layer1;
396 struct hammer_blockmap_layer2 layer2;
400 blockmap_lookup(offset, &layer1, &layer2, &error);
402 printf("z%d:%lld=BADZ",
403 HAMMER_ZONE_DECODE(offset),
404 (offset & ~HAMMER_OFF_ZONE_MASK) /
405 HAMMER_LARGEBLOCK_SIZE
408 fill = layer2.bytes_free * 100 / HAMMER_LARGEBLOCK_SIZE;
411 printf("z%d:%lld=%d%%",
412 HAMMER_ZONE_DECODE(offset),
413 (offset & ~HAMMER_OFF_ZONE_MASK) /
414 HAMMER_LARGEBLOCK_SIZE,
421 * Check the generic crc on a data element. Inodes record types are
422 * special in that some of their fields are not CRCed.
424 * Also check that the zone is valid.
428 check_data_crc(hammer_btree_elm_t elm)
430 struct buffer_info *data_buffer;
431 hammer_off_t data_offset;
438 data_offset = elm->leaf.data_offset;
439 data_len = elm->leaf.data_len;
441 if (data_offset == 0 || data_len == 0)
447 blockmap_lookup(data_offset, NULL, NULL, &error);
451 ptr = get_buffer_data(data_offset, &data_buffer, 0);
452 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
455 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
456 data_len == sizeof(struct hammer_inode_data)) {
457 crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
459 crc = crc32_ext(ptr, len, crc);
465 rel_buffer(data_buffer);
467 return("BO"); /* bad offset */
468 if (crc == elm->leaf.data_crc)
470 return("BX"); /* bad crc */
475 print_record(hammer_btree_elm_t elm)
477 struct buffer_info *data_buffer;
478 hammer_off_t data_offset;
480 hammer_data_ondisk_t data;
482 data_offset = elm->leaf.data_offset;
483 data_len = elm->leaf.data_len;
487 data = get_buffer_data(data_offset, &data_buffer, 0);
491 switch(elm->leaf.base.rec_type) {
492 case HAMMER_RECTYPE_INODE:
493 printf("\n%17s", "");
494 printf("size=%jd nlinks=%jd",
495 (intmax_t)data->inode.size,
496 (intmax_t)data->inode.nlinks);
498 printf(" mode=%05o uflags=%08x\n",
502 printf("ctime=%016jx pobjid=%016jx obj_type=%d\n",
503 (uintmax_t)data->inode.ctime,
504 (uintmax_t)data->inode.parent_obj_id,
505 data->inode.obj_type);
507 printf("mtime=%016jx", (uintmax_t)data->inode.mtime);
508 printf(" caps=%02x", data->inode.cap_flags);
511 case HAMMER_RECTYPE_DIRENTRY:
512 printf("\n%17s", "");
513 data_len -= HAMMER_ENTRY_NAME_OFF;
514 printf("dir-entry ino=%016jx lo=%08x name=\"%*.*s\"",
515 (uintmax_t)data->entry.obj_id,
516 data->entry.localization,
517 data_len, data_len, data->entry.name);
519 case HAMMER_RECTYPE_FIX:
520 switch(elm->leaf.base.key) {
521 case HAMMER_FIXKEY_SYMLINK:
522 data_len -= HAMMER_SYMLINK_NAME_OFF;
523 printf("\n%17s", "");
524 printf("symlink=\"%*.*s\"", data_len, data_len,
535 rel_buffer(data_buffer);
542 hammer_cmd_show_undo(void)
544 struct volume_info *volume;
545 hammer_blockmap_t rootmap;
546 hammer_off_t scan_offset;
547 hammer_fifo_any_t head;
548 struct buffer_info *data_buffer = NULL;
550 volume = get_volume(RootVolNo);
551 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
552 printf("Volume header UNDO %016jx-%016jx/%016jx\n",
553 (intmax_t)rootmap->first_offset,
554 (intmax_t)rootmap->next_offset,
555 (intmax_t)rootmap->alloc_offset);
556 printf("Undo map is %jdMB\n",
557 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
559 scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
560 while (scan_offset < rootmap->alloc_offset) {
561 head = get_buffer_data(scan_offset, &data_buffer, 0);
562 printf("%016jx ", scan_offset);
564 switch(head->head.hdr_type) {
565 case HAMMER_HEAD_TYPE_PAD:
566 printf("PAD(%04x)\n", head->head.hdr_size);
568 case HAMMER_HEAD_TYPE_DUMMY:
569 printf("DUMMY(%04x) seq=%08x\n",
570 head->head.hdr_size, head->head.hdr_seq);
572 case HAMMER_HEAD_TYPE_UNDO:
573 printf("UNDO(%04x) seq=%08x "
574 "dataoff=%016jx bytes=%d\n",
575 head->head.hdr_size, head->head.hdr_seq,
576 (intmax_t)head->undo.undo_offset,
577 head->undo.undo_data_bytes);
579 case HAMMER_HEAD_TYPE_REDO:
580 printf("REDO(%04x) seq=%08x flags=%08x "
581 "objid=%016jx logoff=%016jx bytes=%d\n",
582 head->head.hdr_size, head->head.hdr_seq,
583 head->redo.redo_flags,
584 (intmax_t)head->redo.redo_objid,
585 (intmax_t)head->redo.redo_offset,
586 head->redo.redo_data_bytes);
589 printf("UNKNOWN(%04x,%04x) seq=%08x\n",
595 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
596 head->head.hdr_size == 0 ||
597 head->head.hdr_size > HAMMER_UNDO_ALIGN -
598 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
599 printf("Illegal size field, skipping to "
601 scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
604 scan_offset += head->head.hdr_size;
608 rel_buffer(data_buffer);