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, 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, HAMMER_MAX_TID,
104 left_bound, right_bound);
110 print_btree_node(hammer_off_t node_offset, btree_search_t search,
111 int depth, hammer_tid_t mirror_tid,
112 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
114 struct buffer_info *buffer = NULL;
115 hammer_node_ondisk_t node;
116 hammer_btree_elm_t elm;
124 node = get_node(node_offset, &buffer);
127 printf("BI NODE %016jx (IO ERROR)\n",
128 (uintmax_t)node_offset);
132 if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
137 if (node->mirror_tid <= mirror_tid) {
144 printf("%c%c NODE %016jx cnt=%02d p=%016jx "
148 (uintmax_t)node_offset, node->count,
149 (uintmax_t)node->parent,
150 (node->type ? node->type : '?'), depth);
151 printf(" mirror %016jx", (uintmax_t)node->mirror_tid);
154 print_bigblock_fill(node_offset);
158 maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ?
159 HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS;
161 for (i = 0; i < node->count && i < maxcount; ++i) {
162 elm = &node->elms[i];
164 if (node->type != HAMMER_BTREE_TYPE_INTERNAL) {
167 elm->base.localization == search->lo &&
168 elm->base.obj_id == search->obj_id) {
173 if (elm->base.localization > search->lo ||
174 (elm->base.localization == search->lo &&
175 elm->base.obj_id > search->obj_id)) {
178 if (elm[1].base.localization < search->lo ||
179 (elm[1].base.localization == search->lo &&
180 elm[1].base.obj_id < search->obj_id)) {
187 flags = print_elm_flags(node, node_offset,
188 elm, elm->base.btype,
189 left_bound, right_bound);
190 print_btree_elm(elm, i, node->type, flags, "ELM", ext);
192 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
193 elm = &node->elms[i];
195 flags = print_elm_flags(node, node_offset,
197 left_bound, right_bound);
198 print_btree_elm(elm, i, node->type, flags, "RBN", NULL);
202 for (i = 0; i < node->count; ++i) {
203 elm = &node->elms[i];
206 case HAMMER_BTREE_TYPE_INTERNAL:
208 if (elm->base.localization > search->lo ||
209 (elm->base.localization == search->lo &&
210 elm->base.obj_id > search->obj_id)) {
213 if (elm[1].base.localization < search->lo ||
214 (elm[1].base.localization == search->lo &&
215 elm[1].base.obj_id < search->obj_id)) {
219 if (elm->internal.subtree_offset) {
220 print_btree_node(elm->internal.subtree_offset,
222 elm->internal.mirror_tid,
223 &elm[0].base, &elm[1].base);
225 * Cause show to iterate after seeking to
240 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
241 int flags, const char *label, const char *ext)
243 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
245 flagstr[0] = flags ? 'B' : 'G';
246 if (flags & FLAG_TOOFARLEFT)
248 if (flags & FLAG_TOOFARRIGHT)
250 if (flags & FLAG_BADTYPE)
252 if (flags & FLAG_BADCHILDPARENT)
254 if (flags & FLAG_BADMIRRORTID)
257 printf("%s\t%s %2d %c ",
259 (elm->base.btype ? elm->base.btype : '?'));
260 printf("lo=%08x obj=%016jx rt=%02x key=%016jx ot=%02x\n",
261 elm->base.localization,
262 (uintmax_t)elm->base.obj_id,
264 (uintmax_t)elm->base.key,
266 printf("\t %c tids %016jx:%016jx ",
267 (elm->base.delete_tid ? 'd' : ' '),
268 (uintmax_t)elm->base.create_tid,
269 (uintmax_t)elm->base.delete_tid);
272 case HAMMER_BTREE_TYPE_INTERNAL:
273 printf("suboff=%016jx",
274 (uintmax_t)elm->internal.subtree_offset);
276 printf(" mirror %016jx",
277 (uintmax_t)elm->internal.mirror_tid);
282 case HAMMER_BTREE_TYPE_LEAF:
285 switch(elm->base.btype) {
286 case HAMMER_BTREE_TYPE_RECORD:
288 printf("\n%s\t ", check_data_crc(elm));
291 printf("dataoff=%016jx/%d",
292 (uintmax_t)elm->leaf.data_offset,
295 printf(" crc=%04x", elm->leaf.data_crc);
296 printf("\n\t fills=");
297 print_bigblock_fill(elm->leaf.data_offset);
312 print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
313 hammer_btree_elm_t elm, u_int8_t btype,
314 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
319 case HAMMER_BTREE_TYPE_INTERNAL:
320 if (elm->internal.subtree_offset) {
321 struct buffer_info *buffer = NULL;
322 hammer_node_ondisk_t subnode;
324 subnode = get_node(elm->internal.subtree_offset,
327 flags |= FLAG_BADCHILDPARENT;
328 else if (subnode->parent != node_offset)
329 flags |= FLAG_BADCHILDPARENT;
332 if (elm->internal.mirror_tid > node->mirror_tid)
333 flags |= FLAG_BADMIRRORTID;
336 case HAMMER_BTREE_TYPE_INTERNAL:
337 if (left_bound == NULL || right_bound == NULL)
339 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
340 flags |= FLAG_TOOFARLEFT;
341 if (hammer_btree_cmp(&elm->base, right_bound) > 0)
342 flags |= FLAG_TOOFARRIGHT;
344 case HAMMER_BTREE_TYPE_LEAF:
345 if (left_bound == NULL || right_bound == NULL)
347 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
348 flags |= FLAG_TOOFARLEFT;
349 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
350 flags |= FLAG_TOOFARRIGHT;
353 flags |= FLAG_BADTYPE;
357 case HAMMER_BTREE_TYPE_LEAF:
358 if (elm->base.create_tid &&
359 elm->base.create_tid > node->mirror_tid) {
360 flags |= FLAG_BADMIRRORTID;
362 if (elm->base.delete_tid &&
363 elm->base.delete_tid > node->mirror_tid) {
364 flags |= FLAG_BADMIRRORTID;
367 case HAMMER_BTREE_TYPE_RECORD:
368 if (left_bound == NULL || right_bound == NULL)
370 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
371 flags |= FLAG_TOOFARLEFT;
372 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
373 flags |= FLAG_TOOFARRIGHT;
376 flags |= FLAG_BADTYPE;
381 flags |= FLAG_BADTYPE;
389 print_bigblock_fill(hammer_off_t offset)
391 struct hammer_blockmap_layer1 layer1;
392 struct hammer_blockmap_layer2 layer2;
396 blockmap_lookup(offset, &layer1, &layer2, &error);
398 printf("z%d:%lld=BADZ",
399 HAMMER_ZONE_DECODE(offset),
400 (offset & ~HAMMER_OFF_ZONE_MASK) /
401 HAMMER_LARGEBLOCK_SIZE
404 fill = layer2.bytes_free * 100 / HAMMER_LARGEBLOCK_SIZE;
407 printf("z%d:%lld=%d%%",
408 HAMMER_ZONE_DECODE(offset),
409 (offset & ~HAMMER_OFF_ZONE_MASK) /
410 HAMMER_LARGEBLOCK_SIZE,
417 * Check the generic crc on a data element. Inodes record types are
418 * special in that some of their fields are not CRCed.
420 * Also check that the zone is valid.
424 check_data_crc(hammer_btree_elm_t elm)
426 struct buffer_info *data_buffer;
427 hammer_off_t data_offset;
434 data_offset = elm->leaf.data_offset;
435 data_len = elm->leaf.data_len;
437 if (data_offset == 0 || data_len == 0)
443 blockmap_lookup(data_offset, NULL, NULL, &error);
447 ptr = get_buffer_data(data_offset, &data_buffer, 0);
448 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
451 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
452 data_len == sizeof(struct hammer_inode_data)) {
453 crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
455 crc = crc32_ext(ptr, len, crc);
461 rel_buffer(data_buffer);
463 return("BO"); /* bad offset */
464 if (crc == elm->leaf.data_crc)
466 return("BX"); /* bad crc */
471 print_record(hammer_btree_elm_t elm)
473 struct buffer_info *data_buffer;
474 hammer_off_t data_offset;
476 hammer_data_ondisk_t data;
478 data_offset = elm->leaf.data_offset;
479 data_len = elm->leaf.data_len;
483 data = get_buffer_data(data_offset, &data_buffer, 0);
487 switch(elm->leaf.base.rec_type) {
488 case HAMMER_RECTYPE_INODE:
489 printf("\n%17s", "");
490 printf("size=%jd nlinks=%jd",
491 (intmax_t)data->inode.size,
492 (intmax_t)data->inode.nlinks);
494 printf(" mode=%05o uflags=%08x\n",
498 printf("ctime=%016jx pobjid=%016jx obj_type=%d\n",
499 (uintmax_t)data->inode.ctime,
500 (uintmax_t)data->inode.parent_obj_id,
501 data->inode.obj_type);
503 printf("mtime=%016jx", (uintmax_t)data->inode.mtime);
504 printf(" caps=%02x", data->inode.cap_flags);
507 case HAMMER_RECTYPE_DIRENTRY:
508 printf("\n%17s", "");
509 data_len -= HAMMER_ENTRY_NAME_OFF;
510 printf("dir-entry ino=%016jx lo=%08x name=\"%*.*s\"",
511 (uintmax_t)data->entry.obj_id,
512 data->entry.localization,
513 data_len, data_len, data->entry.name);
515 case HAMMER_RECTYPE_FIX:
516 switch(elm->leaf.base.key) {
517 case HAMMER_FIXKEY_SYMLINK:
518 data_len -= HAMMER_SYMLINK_NAME_OFF;
519 printf("\n%17s", "");
520 printf("symlink=\"%*.*s\"", data_len, data_len,
531 rel_buffer(data_buffer);
538 hammer_cmd_show_undo(void)
540 struct volume_info *volume;
541 hammer_blockmap_t rootmap;
542 hammer_off_t scan_offset;
543 hammer_fifo_any_t head;
544 struct buffer_info *data_buffer = NULL;
546 volume = get_volume(RootVolNo);
547 rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
548 printf("Volume header UNDO %016jx-%016jx/%016jx\n",
549 (intmax_t)rootmap->first_offset,
550 (intmax_t)rootmap->next_offset,
551 (intmax_t)rootmap->alloc_offset);
552 printf("Undo map is %jdMB\n",
553 (intmax_t)((rootmap->alloc_offset & HAMMER_OFF_LONG_MASK) /
555 scan_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
556 while (scan_offset < rootmap->alloc_offset) {
557 head = get_buffer_data(scan_offset, &data_buffer, 0);
558 printf("%016jx ", scan_offset);
560 switch(head->head.hdr_type) {
561 case HAMMER_HEAD_TYPE_PAD:
562 printf("PAD(%04x)\n", head->head.hdr_size);
564 case HAMMER_HEAD_TYPE_DUMMY:
565 printf("DUMMY(%04x) seq=%08x\n",
566 head->head.hdr_size, head->head.hdr_seq);
568 case HAMMER_HEAD_TYPE_UNDO:
569 printf("UNDO(%04x) seq=%08x "
570 "dataoff=%016jx bytes=%d\n",
571 head->head.hdr_size, head->head.hdr_seq,
572 (intmax_t)head->undo.undo_offset,
573 head->undo.undo_data_bytes);
575 case HAMMER_HEAD_TYPE_REDO:
576 printf("REDO(%04x) seq=%08x flags=%08x "
577 "objid=%016jx logoff=%016jx bytes=%d\n",
578 head->head.hdr_size, head->head.hdr_seq,
579 head->redo.redo_flags,
580 (intmax_t)head->redo.redo_objid,
581 (intmax_t)head->redo.redo_offset,
582 head->redo.redo_data_bytes);
585 printf("UNKNOWN(%04x,%04x) seq=%08x\n",
591 if ((head->head.hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
592 head->head.hdr_size == 0 ||
593 head->head.hdr_size > HAMMER_UNDO_ALIGN -
594 ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
595 printf("Illegal size field, skipping to "
597 scan_offset = (scan_offset + HAMMER_UNDO_MASK) &
600 scan_offset += head->head.hdr_size;
604 rel_buffer(data_buffer);