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 static void print_btree_node(hammer_off_t node_offset
, int depth
, int spike
,
45 hammer_base_elm_t left_bound
,
46 hammer_base_elm_t right_bound
);
47 static const char *check_data_crc(hammer_btree_elm_t elm
);
48 static void print_record(hammer_btree_elm_t elm
);
49 static void print_btree_elm(hammer_btree_elm_t elm
, int i
, u_int8_t type
,
50 int flags
, const char *label
);
51 static int print_elm_flags(hammer_node_ondisk_t node
, hammer_off_t node_offset
,
52 hammer_btree_elm_t elm
, u_int8_t btype
,
53 hammer_base_elm_t left_bound
,
54 hammer_base_elm_t right_bound
);
55 static void print_bigblock_fill(hammer_off_t offset
);
58 hammer_cmd_show(hammer_off_t node_offset
, int depth
,
59 hammer_base_elm_t left_bound
, hammer_base_elm_t right_bound
)
61 struct volume_info
*volume
;
63 if (node_offset
== (hammer_off_t
)-1) {
64 volume
= get_volume(RootVolNo
);
65 node_offset
= volume
->ondisk
->vol0_btree_root
;
67 printf("Volume header\trecords=%lld next_tid=%016llx\n",
68 volume
->ondisk
->vol0_stat_records
,
69 volume
->ondisk
->vol0_next_tid
);
73 printf("show %016llx depth %d\n", node_offset
, depth
);
74 print_btree_node(node_offset
, depth
, 0, left_bound
, right_bound
);
75 print_btree_node(node_offset
, depth
, 1, left_bound
, right_bound
);
79 print_btree_node(hammer_off_t node_offset
, int depth
, int spike
,
80 hammer_base_elm_t left_bound
, hammer_base_elm_t right_bound
)
82 struct buffer_info
*buffer
= NULL
;
83 hammer_node_ondisk_t node
;
84 hammer_btree_elm_t elm
;
90 node
= get_node(node_offset
, &buffer
);
92 if (crc32(&node
->crc
+ 1, HAMMER_BTREE_CRCSIZE
) == node
->crc
)
98 printf("%c NODE %016llx cnt=%d p=%016llx "
101 node_offset
, node
->count
, node
->parent
,
102 (node
->type
? node
->type
: '?'), depth
);
103 printf(" mirror %016llx", node
->mirror_tid
);
106 print_bigblock_fill(node_offset
);
110 maxcount
= (node
->type
== HAMMER_BTREE_TYPE_INTERNAL
) ?
111 HAMMER_BTREE_INT_ELMS
: HAMMER_BTREE_LEAF_ELMS
;
113 for (i
= 0; i
< node
->count
&& i
< maxcount
; ++i
) {
114 elm
= &node
->elms
[i
];
115 flags
= print_elm_flags(node
, node_offset
,
116 elm
, elm
->base
.btype
,
117 left_bound
, right_bound
);
118 print_btree_elm(elm
, i
, node
->type
, flags
, "ELM");
120 if (node
->type
== HAMMER_BTREE_TYPE_INTERNAL
) {
121 elm
= &node
->elms
[i
];
122 flags
= print_elm_flags(node
, node_offset
,
124 left_bound
, right_bound
);
125 print_btree_elm(elm
, i
, node
->type
, flags
, "RBN");
130 for (i
= 0; i
< node
->count
; ++i
) {
131 elm
= &node
->elms
[i
];
134 case HAMMER_BTREE_TYPE_INTERNAL
:
135 if (elm
->internal
.subtree_offset
) {
136 print_btree_node(elm
->internal
.subtree_offset
,
138 &elm
[0].base
, &elm
[1].base
);
150 print_btree_elm(hammer_btree_elm_t elm
, int i
, u_int8_t type
,
151 int flags
, const char *label
)
153 char flagstr
[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
155 flagstr
[0] = flags
? 'B' : 'G';
156 if (flags
& FLAG_TOOFARLEFT
)
158 if (flags
& FLAG_TOOFARRIGHT
)
160 if (flags
& FLAG_BADTYPE
)
162 if (flags
& FLAG_BADCHILDPARENT
)
165 printf("%s\t%s %2d %c ",
167 (elm
->base
.btype
? elm
->base
.btype
: '?'));
168 printf("obj=%016llx key=%016llx lo=%08x rt=%02x ot=%02x\n",
171 elm
->base
.localization
,
174 printf("\t %c tids %016llx:%016llx ",
175 (elm
->base
.delete_tid
? 'd' : ' '),
176 elm
->base
.create_tid
,
177 elm
->base
.delete_tid
);
180 case HAMMER_BTREE_TYPE_INTERNAL
:
181 printf("suboff=%016llx", elm
->internal
.subtree_offset
);
183 printf(" mirror %016llx", elm
->internal
.mirror_tid
);
185 case HAMMER_BTREE_TYPE_LEAF
:
186 switch(elm
->base
.btype
) {
187 case HAMMER_BTREE_TYPE_RECORD
:
188 printf("\n%s\t ", check_data_crc(elm
));
189 printf("dataoff=%016llx/%d",
190 elm
->leaf
.data_offset
, elm
->leaf
.data_len
);
192 printf(" crc=%04x", elm
->leaf
.data_crc
);
193 printf("\n\t fills=");
194 print_bigblock_fill(elm
->leaf
.data_offset
);
209 print_elm_flags(hammer_node_ondisk_t node
, hammer_off_t node_offset
,
210 hammer_btree_elm_t elm
, u_int8_t btype
,
211 hammer_base_elm_t left_bound
, hammer_base_elm_t right_bound
)
216 case HAMMER_BTREE_TYPE_INTERNAL
:
217 if (elm
->internal
.subtree_offset
) {
218 struct buffer_info
*buffer
= NULL
;
219 hammer_node_ondisk_t subnode
;
221 subnode
= get_node(elm
->internal
.subtree_offset
,
223 if (subnode
->parent
!= node_offset
)
224 flags
|= FLAG_BADCHILDPARENT
;
229 case HAMMER_BTREE_TYPE_INTERNAL
:
230 if (left_bound
== NULL
|| right_bound
== NULL
)
232 if (hammer_btree_cmp(&elm
->base
, left_bound
) < 0)
233 flags
|= FLAG_TOOFARLEFT
;
234 if (hammer_btree_cmp(&elm
->base
, right_bound
) > 0)
235 flags
|= FLAG_TOOFARRIGHT
;
237 case HAMMER_BTREE_TYPE_LEAF
:
238 if (left_bound
== NULL
|| right_bound
== NULL
)
240 if (hammer_btree_cmp(&elm
->base
, left_bound
) < 0)
241 flags
|= FLAG_TOOFARLEFT
;
242 if (hammer_btree_cmp(&elm
->base
, right_bound
) >= 0)
243 flags
|= FLAG_TOOFARRIGHT
;
246 flags
|= FLAG_BADTYPE
;
250 case HAMMER_BTREE_TYPE_LEAF
:
252 case HAMMER_BTREE_TYPE_RECORD
:
253 if (left_bound
== NULL
|| right_bound
== NULL
)
255 if (hammer_btree_cmp(&elm
->base
, left_bound
) < 0)
256 flags
|= FLAG_TOOFARLEFT
;
257 if (hammer_btree_cmp(&elm
->base
, right_bound
) >= 0)
258 flags
|= FLAG_TOOFARRIGHT
;
261 flags
|= FLAG_BADTYPE
;
266 flags
|= FLAG_BADTYPE
;
274 print_bigblock_fill(hammer_off_t offset
)
276 struct hammer_blockmap_layer1 layer1
;
277 struct hammer_blockmap_layer2 layer2
;
280 blockmap_lookup(offset
, &layer1
, &layer2
);
281 fill
= layer2
.bytes_free
* 100 / HAMMER_LARGEBLOCK_SIZE
;
284 printf("z%d:%lld=%d%%",
285 HAMMER_ZONE_DECODE(offset
),
286 (offset
& ~HAMMER_OFF_ZONE_MASK
) / HAMMER_LARGEBLOCK_SIZE
,
292 * Check the generic crc on a data element. Inodes record types are
293 * special in that some of their fields are not CRCed.
297 check_data_crc(hammer_btree_elm_t elm
)
299 struct buffer_info
*data_buffer
;
300 hammer_off_t data_offset
;
306 data_offset
= elm
->leaf
.data_offset
;
307 data_len
= elm
->leaf
.data_len
;
309 if (data_offset
== 0 || data_len
== 0)
314 ptr
= get_buffer_data(data_offset
, &data_buffer
, 0);
315 len
= HAMMER_BUFSIZE
- ((int)data_offset
& HAMMER_BUFMASK
);
318 if (elm
->leaf
.base
.rec_type
== HAMMER_RECTYPE_INODE
&&
319 data_len
== sizeof(struct hammer_inode_data
)) {
320 crc
= crc32_ext(ptr
, HAMMER_INODE_CRCSIZE
, crc
);
322 crc
= crc32_ext(ptr
, len
, crc
);
328 rel_buffer(data_buffer
);
329 if (crc
== elm
->leaf
.data_crc
)
336 print_record(hammer_btree_elm_t elm
)
338 struct buffer_info
*data_buffer
;
339 hammer_off_t data_offset
;
341 hammer_data_ondisk_t data
;
343 data_offset
= elm
->leaf
.data_offset
;
344 data_len
= elm
->leaf
.data_len
;
348 data
= get_buffer_data(data_offset
, &data_buffer
, 0);
352 switch(elm
->leaf
.base
.rec_type
) {
353 case HAMMER_RECTYPE_INODE
:
354 printf("\n%17s", "");
355 printf("size=%lld nlinks=%lld",
356 data
->inode
.size
, data
->inode
.nlinks
);
358 printf(" mode=%05o uflags=%08x\n",
362 printf("ctime=%016llx pobjid=%016llx obj_type=%d\n",
363 data
->inode
.ctime
, data
->inode
.parent_obj_id
,
364 data
->inode
.obj_type
);
366 printf("mtime=%016llx", data
->inode
.mtime
);
369 case HAMMER_RECTYPE_DIRENTRY
:
370 printf("\n%17s", "");
371 data_len
-= HAMMER_ENTRY_NAME_OFF
;
372 printf("dir-entry ino=%016llx lo=%08x name=\"%*.*s\"",
374 data
->entry
.localization
,
375 data_len
, data_len
, data
->entry
.name
);
377 case HAMMER_RECTYPE_FIX
:
378 switch(elm
->leaf
.base
.key
) {
379 case HAMMER_FIXKEY_SYMLINK
:
380 data_len
-= HAMMER_SYMLINK_NAME_OFF
;
381 printf("\n%17s", "");
382 printf("symlink=\"%*.*s\"", data_len
, data_len
,
393 rel_buffer(data_buffer
);