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
34 * $DragonFly: src/sbin/hammer/cmd_show.c,v 1.18 2008/10/09 04:20:59 dillon Exp $
39 #define FLAG_TOOFARLEFT 0x0001
40 #define FLAG_TOOFARRIGHT 0x0002
41 #define FLAG_BADTYPE 0x0004
42 #define FLAG_BADCHILDPARENT 0x0008
44 typedef struct btree_search
{
49 static void print_btree_node(hammer_off_t node_offset
, btree_search_t search
,
51 hammer_base_elm_t left_bound
,
52 hammer_base_elm_t right_bound
);
53 static const char *check_data_crc(hammer_btree_elm_t elm
);
54 static void print_record(hammer_btree_elm_t elm
);
55 static void print_btree_elm(hammer_btree_elm_t elm
, int i
, u_int8_t type
,
56 int flags
, const char *label
, const char *ext
);
57 static int print_elm_flags(hammer_node_ondisk_t node
, hammer_off_t node_offset
,
58 hammer_btree_elm_t elm
, u_int8_t btype
,
59 hammer_base_elm_t left_bound
,
60 hammer_base_elm_t right_bound
);
61 static void print_bigblock_fill(hammer_off_t offset
);
64 hammer_cmd_show(hammer_off_t node_offset
, u_int32_t lo
, int64_t obj_id
,
66 hammer_base_elm_t left_bound
, hammer_base_elm_t right_bound
)
68 struct volume_info
*volume
;
69 struct btree_search search
;
70 btree_search_t searchp
;
73 if (node_offset
== (hammer_off_t
)-1) {
74 volume
= get_volume(RootVolNo
);
75 node_offset
= volume
->ondisk
->vol0_btree_root
;
77 printf("Volume header\trecords=%jd next_tid=%016jx\n",
78 (intmax_t)volume
->ondisk
->vol0_stat_records
,
79 (uintmax_t)volume
->ondisk
->vol0_next_tid
);
80 printf("\t\tbufoffset=%016jx\n",
81 (uintmax_t)volume
->ondisk
->vol_buf_beg
);
82 for (zone
= 0; zone
< HAMMER_MAX_ZONES
; ++zone
) {
83 printf("\t\tzone %d\tnext_offset=%016jx\n",
85 (uintmax_t)volume
->ondisk
->vol0_blockmap
[zone
].next_offset
92 if (lo
== 0 && obj_id
== (int64_t)HAMMER_MIN_OBJID
) {
94 printf("show %016jx depth %d\n", (uintmax_t)node_offset
, depth
);
97 search
.obj_id
= obj_id
;
99 printf("show %016jx lo %08x obj_id %016jx depth %d\n",
100 (uintmax_t)node_offset
, lo
, (uintmax_t)obj_id
, depth
);
102 print_btree_node(node_offset
, searchp
, depth
,
103 0, left_bound
, right_bound
);
104 print_btree_node(node_offset
, searchp
, depth
,
105 1, left_bound
, right_bound
);
109 print_btree_node(hammer_off_t node_offset
, btree_search_t search
,
110 int depth
, int spike
,
111 hammer_base_elm_t left_bound
, hammer_base_elm_t right_bound
)
113 struct buffer_info
*buffer
= NULL
;
114 hammer_node_ondisk_t node
;
115 hammer_btree_elm_t elm
;
122 node
= get_node(node_offset
, &buffer
);
124 if (crc32(&node
->crc
+ 1, HAMMER_BTREE_CRCSIZE
) == node
->crc
)
130 printf("%c NODE %016jx cnt=%02d p=%016jx "
133 (uintmax_t)node_offset
, node
->count
,
134 (uintmax_t)node
->parent
,
135 (node
->type
? node
->type
: '?'), depth
);
136 printf(" mirror %016jx", (uintmax_t)node
->mirror_tid
);
139 print_bigblock_fill(node_offset
);
143 maxcount
= (node
->type
== HAMMER_BTREE_TYPE_INTERNAL
) ?
144 HAMMER_BTREE_INT_ELMS
: HAMMER_BTREE_LEAF_ELMS
;
146 for (i
= 0; i
< node
->count
&& i
< maxcount
; ++i
) {
147 elm
= &node
->elms
[i
];
149 if (node
->type
!= HAMMER_BTREE_TYPE_INTERNAL
) {
152 elm
->base
.localization
== search
->lo
&&
153 elm
->base
.obj_id
== search
->obj_id
) {
158 if (elm
->base
.localization
> search
->lo
||
159 (elm
->base
.localization
== search
->lo
&&
160 elm
->base
.obj_id
> search
->obj_id
)) {
163 if (elm
[1].base
.localization
< search
->lo
||
164 (elm
[1].base
.localization
== search
->lo
&&
165 elm
[1].base
.obj_id
< search
->obj_id
)) {
172 flags
= print_elm_flags(node
, node_offset
,
173 elm
, elm
->base
.btype
,
174 left_bound
, right_bound
);
175 print_btree_elm(elm
, i
, node
->type
, flags
, "ELM", ext
);
177 if (node
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
178 elm
= &node
->elms
[i
];
180 flags
= print_elm_flags(node
, node_offset
,
182 left_bound
, right_bound
);
183 print_btree_elm(elm
, i
, node
->type
, flags
, "RBN", NULL
);
188 for (i
= 0; i
< node
->count
; ++i
) {
189 elm
= &node
->elms
[i
];
192 case HAMMER_BTREE_TYPE_INTERNAL
:
194 if (elm
->base
.localization
> search
->lo
||
195 (elm
->base
.localization
== search
->lo
&&
196 elm
->base
.obj_id
> search
->obj_id
)) {
199 if (elm
[1].base
.localization
< search
->lo
||
200 (elm
[1].base
.localization
== search
->lo
&&
201 elm
[1].base
.obj_id
< search
->obj_id
)) {
205 if (elm
->internal
.subtree_offset
) {
206 print_btree_node(elm
->internal
.subtree_offset
,
207 search
, depth
+ 1, spike
,
208 &elm
[0].base
, &elm
[1].base
);
210 * Cause show to iterate after seeking to
225 print_btree_elm(hammer_btree_elm_t elm
, int i
, u_int8_t type
,
226 int flags
, const char *label
, const char *ext
)
228 char flagstr
[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
230 flagstr
[0] = flags
? 'B' : 'G';
231 if (flags
& FLAG_TOOFARLEFT
)
233 if (flags
& FLAG_TOOFARRIGHT
)
235 if (flags
& FLAG_BADTYPE
)
237 if (flags
& FLAG_BADCHILDPARENT
)
240 printf("%s\t%s %2d %c ",
242 (elm
->base
.btype
? elm
->base
.btype
: '?'));
243 printf("obj=%016jx key=%016jx lo=%08x rt=%02x ot=%02x\n",
244 (uintmax_t)elm
->base
.obj_id
,
245 (uintmax_t)elm
->base
.key
,
246 elm
->base
.localization
,
249 printf("\t %c tids %016jx:%016jx ",
250 (elm
->base
.delete_tid
? 'd' : ' '),
251 (uintmax_t)elm
->base
.create_tid
,
252 (uintmax_t)elm
->base
.delete_tid
);
255 case HAMMER_BTREE_TYPE_INTERNAL
:
256 printf("suboff=%016jx",
257 (uintmax_t)elm
->internal
.subtree_offset
);
259 printf(" mirror %016jx",
260 (uintmax_t)elm
->internal
.mirror_tid
);
265 case HAMMER_BTREE_TYPE_LEAF
:
268 switch(elm
->base
.btype
) {
269 case HAMMER_BTREE_TYPE_RECORD
:
271 printf("\n%s\t ", check_data_crc(elm
));
274 printf("dataoff=%016jx/%d",
275 (uintmax_t)elm
->leaf
.data_offset
,
278 printf(" crc=%04x", elm
->leaf
.data_crc
);
279 printf("\n\t fills=");
280 print_bigblock_fill(elm
->leaf
.data_offset
);
295 print_elm_flags(hammer_node_ondisk_t node
, hammer_off_t node_offset
,
296 hammer_btree_elm_t elm
, u_int8_t btype
,
297 hammer_base_elm_t left_bound
, hammer_base_elm_t right_bound
)
302 case HAMMER_BTREE_TYPE_INTERNAL
:
303 if (elm
->internal
.subtree_offset
) {
304 struct buffer_info
*buffer
= NULL
;
305 hammer_node_ondisk_t subnode
;
307 subnode
= get_node(elm
->internal
.subtree_offset
,
309 if (subnode
->parent
!= node_offset
)
310 flags
|= FLAG_BADCHILDPARENT
;
315 case HAMMER_BTREE_TYPE_INTERNAL
:
316 if (left_bound
== NULL
|| right_bound
== NULL
)
318 if (hammer_btree_cmp(&elm
->base
, left_bound
) < 0)
319 flags
|= FLAG_TOOFARLEFT
;
320 if (hammer_btree_cmp(&elm
->base
, right_bound
) > 0)
321 flags
|= FLAG_TOOFARRIGHT
;
323 case HAMMER_BTREE_TYPE_LEAF
:
324 if (left_bound
== NULL
|| right_bound
== NULL
)
326 if (hammer_btree_cmp(&elm
->base
, left_bound
) < 0)
327 flags
|= FLAG_TOOFARLEFT
;
328 if (hammer_btree_cmp(&elm
->base
, right_bound
) >= 0)
329 flags
|= FLAG_TOOFARRIGHT
;
332 flags
|= FLAG_BADTYPE
;
336 case HAMMER_BTREE_TYPE_LEAF
:
338 case HAMMER_BTREE_TYPE_RECORD
:
339 if (left_bound
== NULL
|| right_bound
== NULL
)
341 if (hammer_btree_cmp(&elm
->base
, left_bound
) < 0)
342 flags
|= FLAG_TOOFARLEFT
;
343 if (hammer_btree_cmp(&elm
->base
, right_bound
) >= 0)
344 flags
|= FLAG_TOOFARRIGHT
;
347 flags
|= FLAG_BADTYPE
;
352 flags
|= FLAG_BADTYPE
;
360 print_bigblock_fill(hammer_off_t offset
)
362 struct hammer_blockmap_layer1 layer1
;
363 struct hammer_blockmap_layer2 layer2
;
366 blockmap_lookup(offset
, &layer1
, &layer2
);
367 fill
= layer2
.bytes_free
* 100 / HAMMER_LARGEBLOCK_SIZE
;
370 printf("z%d:%lld=%d%%",
371 HAMMER_ZONE_DECODE(offset
),
372 (offset
& ~HAMMER_OFF_ZONE_MASK
) / HAMMER_LARGEBLOCK_SIZE
,
378 * Check the generic crc on a data element. Inodes record types are
379 * special in that some of their fields are not CRCed.
383 check_data_crc(hammer_btree_elm_t elm
)
385 struct buffer_info
*data_buffer
;
386 hammer_off_t data_offset
;
392 data_offset
= elm
->leaf
.data_offset
;
393 data_len
= elm
->leaf
.data_len
;
395 if (data_offset
== 0 || data_len
== 0)
400 ptr
= get_buffer_data(data_offset
, &data_buffer
, 0);
401 len
= HAMMER_BUFSIZE
- ((int)data_offset
& HAMMER_BUFMASK
);
404 if (elm
->leaf
.base
.rec_type
== HAMMER_RECTYPE_INODE
&&
405 data_len
== sizeof(struct hammer_inode_data
)) {
406 crc
= crc32_ext(ptr
, HAMMER_INODE_CRCSIZE
, crc
);
408 crc
= crc32_ext(ptr
, len
, crc
);
414 rel_buffer(data_buffer
);
415 if (crc
== elm
->leaf
.data_crc
)
422 print_record(hammer_btree_elm_t elm
)
424 struct buffer_info
*data_buffer
;
425 hammer_off_t data_offset
;
427 hammer_data_ondisk_t data
;
429 data_offset
= elm
->leaf
.data_offset
;
430 data_len
= elm
->leaf
.data_len
;
434 data
= get_buffer_data(data_offset
, &data_buffer
, 0);
438 switch(elm
->leaf
.base
.rec_type
) {
439 case HAMMER_RECTYPE_INODE
:
440 printf("\n%17s", "");
441 printf("size=%jd nlinks=%jd",
442 (intmax_t)data
->inode
.size
,
443 (intmax_t)data
->inode
.nlinks
);
445 printf(" mode=%05o uflags=%08x\n",
449 printf("ctime=%016jx pobjid=%016jx obj_type=%d\n",
450 (uintmax_t)data
->inode
.ctime
,
451 (uintmax_t)data
->inode
.parent_obj_id
,
452 data
->inode
.obj_type
);
454 printf("mtime=%016jx", (uintmax_t)data
->inode
.mtime
);
455 printf(" caps=%02x", data
->inode
.cap_flags
);
458 case HAMMER_RECTYPE_DIRENTRY
:
459 printf("\n%17s", "");
460 data_len
-= HAMMER_ENTRY_NAME_OFF
;
461 printf("dir-entry ino=%016jx lo=%08x name=\"%*.*s\"",
462 (uintmax_t)data
->entry
.obj_id
,
463 data
->entry
.localization
,
464 data_len
, data_len
, data
->entry
.name
);
466 case HAMMER_RECTYPE_FIX
:
467 switch(elm
->leaf
.base
.key
) {
468 case HAMMER_FIXKEY_SYMLINK
:
469 data_len
-= HAMMER_SYMLINK_NAME_OFF
;
470 printf("\n%17s", "");
471 printf("symlink=\"%*.*s\"", data_len
, data_len
,
482 rel_buffer(data_buffer
);
489 hammer_cmd_show_undo(void)
491 struct volume_info
*volume
;
492 hammer_blockmap_t rootmap
;
493 hammer_off_t scan_offset
;
494 hammer_fifo_any_t head
;
495 struct buffer_info
*data_buffer
= NULL
;
497 volume
= get_volume(RootVolNo
);
498 rootmap
= &volume
->ondisk
->vol0_blockmap
[HAMMER_ZONE_UNDO_INDEX
];
499 printf("Volume header UNDO %016jx-%016jx/%016jx\n",
500 (intmax_t)rootmap
->first_offset
,
501 (intmax_t)rootmap
->next_offset
,
502 (intmax_t)rootmap
->alloc_offset
);
503 printf("Undo map is %jdMB\n",
504 (intmax_t)((rootmap
->alloc_offset
& HAMMER_OFF_LONG_MASK
) /
506 scan_offset
= HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX
, 0);
507 while (scan_offset
< rootmap
->alloc_offset
) {
508 head
= get_buffer_data(scan_offset
, &data_buffer
, 0);
509 printf("%016jx ", scan_offset
);
511 switch(head
->head
.hdr_type
) {
512 case HAMMER_HEAD_TYPE_PAD
:
513 printf("PAD(%04x)\n", head
->head
.hdr_size
);
515 case HAMMER_HEAD_TYPE_DUMMY
:
516 printf("DUMMY(%04x) seq=%08x\n",
517 head
->head
.hdr_size
, head
->head
.hdr_seq
);
519 case HAMMER_HEAD_TYPE_UNDO
:
520 printf("UNDO(%04x) seq=%08x "
521 "dataoff=%016jx bytes=%d\n",
522 head
->head
.hdr_size
, head
->head
.hdr_seq
,
523 (intmax_t)head
->undo
.undo_offset
,
524 head
->undo
.undo_data_bytes
);
526 case HAMMER_HEAD_TYPE_REDO
:
527 printf("REDO(%04x) seq=%08x flags=%08x "
528 "objid=%016jx logoff=%016jx bytes=%d\n",
529 head
->head
.hdr_size
, head
->head
.hdr_seq
,
530 head
->redo
.redo_flags
,
531 (intmax_t)head
->redo
.redo_objid
,
532 (intmax_t)head
->redo
.redo_offset
,
533 head
->redo
.redo_data_bytes
);
536 printf("UNKNOWN(%04x,%04x) seq=%08x\n",
542 if ((head
->head
.hdr_size
& HAMMER_HEAD_ALIGN_MASK
) ||
543 head
->head
.hdr_size
== 0 ||
544 head
->head
.hdr_size
> HAMMER_UNDO_ALIGN
-
545 ((u_int
)scan_offset
& HAMMER_UNDO_MASK
)) {
546 printf("Illegal size field, skipping to "
548 scan_offset
= (scan_offset
+ HAMMER_UNDO_MASK
) &
551 scan_offset
+= head
->head
.hdr_size
;
555 rel_buffer(data_buffer
);