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=%lld next_tid=%016llx\n",
78 volume->ondisk->vol0_stat_records,
79 volume->ondisk->vol0_next_tid);
80 printf("\t\tbufoffset=%016llx\n",
81 volume->ondisk->vol_buf_beg);
82 for (zone = 0; zone < HAMMER_MAX_ZONES; ++zone) {
83 printf("\t\tzone %d\tnext_offset=%016llx\n",
85 volume->ondisk->vol0_blockmap[zone].next_offset
92 if (lo == 0 && obj_id == (int64_t)HAMMER_MIN_OBJID) {
94 printf("show %016llx depth %d\n", node_offset, depth);
97 search.obj_id = obj_id;
99 printf("show %016llx lo %08x obj_id %016llx depth %d\n",
100 node_offset, lo, (long long)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 %016llx cnt=%02d p=%016llx "
133 node_offset, node->count, node->parent,
134 (node->type ? node->type : '?'), depth);
135 printf(" mirror %016llx", node->mirror_tid);
138 print_bigblock_fill(node_offset);
142 maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ?
143 HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS;
145 for (i = 0; i < node->count && i < maxcount; ++i) {
146 elm = &node->elms[i];
148 if (node->type != HAMMER_BTREE_TYPE_INTERNAL) {
151 elm->base.localization == search->lo &&
152 elm->base.obj_id == search->obj_id) {
157 if (elm->base.localization > search->lo ||
158 (elm->base.localization == search->lo &&
159 elm->base.obj_id > search->obj_id)) {
162 if (elm[1].base.localization < search->lo ||
163 (elm[1].base.localization == search->lo &&
164 elm[1].base.obj_id < search->obj_id)) {
171 flags = print_elm_flags(node, node_offset,
172 elm, elm->base.btype,
173 left_bound, right_bound);
174 print_btree_elm(elm, i, node->type, flags, "ELM", ext);
176 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
177 elm = &node->elms[i];
179 flags = print_elm_flags(node, node_offset,
181 left_bound, right_bound);
182 print_btree_elm(elm, i, node->type, flags, "RBN", NULL);
187 for (i = 0; i < node->count; ++i) {
188 elm = &node->elms[i];
191 case HAMMER_BTREE_TYPE_INTERNAL:
193 if (elm->base.localization > search->lo ||
194 (elm->base.localization == search->lo &&
195 elm->base.obj_id > search->obj_id)) {
198 if (elm[1].base.localization < search->lo ||
199 (elm[1].base.localization == search->lo &&
200 elm[1].base.obj_id < search->obj_id)) {
204 if (elm->internal.subtree_offset) {
205 print_btree_node(elm->internal.subtree_offset,
206 search, depth + 1, spike,
207 &elm[0].base, &elm[1].base);
209 * Cause show to iterate after seeking to
224 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
225 int flags, const char *label, const char *ext)
227 char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
229 flagstr[0] = flags ? 'B' : 'G';
230 if (flags & FLAG_TOOFARLEFT)
232 if (flags & FLAG_TOOFARRIGHT)
234 if (flags & FLAG_BADTYPE)
236 if (flags & FLAG_BADCHILDPARENT)
239 printf("%s\t%s %2d %c ",
241 (elm->base.btype ? elm->base.btype : '?'));
242 printf("obj=%016llx key=%016llx lo=%08x rt=%02x ot=%02x\n",
245 elm->base.localization,
248 printf("\t %c tids %016llx:%016llx ",
249 (elm->base.delete_tid ? 'd' : ' '),
250 elm->base.create_tid,
251 elm->base.delete_tid);
254 case HAMMER_BTREE_TYPE_INTERNAL:
255 printf("suboff=%016llx", elm->internal.subtree_offset);
257 printf(" mirror %016llx", elm->internal.mirror_tid);
261 case HAMMER_BTREE_TYPE_LEAF:
264 switch(elm->base.btype) {
265 case HAMMER_BTREE_TYPE_RECORD:
267 printf("\n%s\t ", check_data_crc(elm));
270 printf("dataoff=%016llx/%d",
271 elm->leaf.data_offset, elm->leaf.data_len);
273 printf(" crc=%04x", elm->leaf.data_crc);
274 printf("\n\t fills=");
275 print_bigblock_fill(elm->leaf.data_offset);
290 print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
291 hammer_btree_elm_t elm, u_int8_t btype,
292 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
297 case HAMMER_BTREE_TYPE_INTERNAL:
298 if (elm->internal.subtree_offset) {
299 struct buffer_info *buffer = NULL;
300 hammer_node_ondisk_t subnode;
302 subnode = get_node(elm->internal.subtree_offset,
304 if (subnode->parent != node_offset)
305 flags |= FLAG_BADCHILDPARENT;
310 case HAMMER_BTREE_TYPE_INTERNAL:
311 if (left_bound == NULL || right_bound == NULL)
313 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
314 flags |= FLAG_TOOFARLEFT;
315 if (hammer_btree_cmp(&elm->base, right_bound) > 0)
316 flags |= FLAG_TOOFARRIGHT;
318 case HAMMER_BTREE_TYPE_LEAF:
319 if (left_bound == NULL || right_bound == NULL)
321 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
322 flags |= FLAG_TOOFARLEFT;
323 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
324 flags |= FLAG_TOOFARRIGHT;
327 flags |= FLAG_BADTYPE;
331 case HAMMER_BTREE_TYPE_LEAF:
333 case HAMMER_BTREE_TYPE_RECORD:
334 if (left_bound == NULL || right_bound == NULL)
336 if (hammer_btree_cmp(&elm->base, left_bound) < 0)
337 flags |= FLAG_TOOFARLEFT;
338 if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
339 flags |= FLAG_TOOFARRIGHT;
342 flags |= FLAG_BADTYPE;
347 flags |= FLAG_BADTYPE;
355 print_bigblock_fill(hammer_off_t offset)
357 struct hammer_blockmap_layer1 layer1;
358 struct hammer_blockmap_layer2 layer2;
361 blockmap_lookup(offset, &layer1, &layer2);
362 fill = layer2.bytes_free * 100 / HAMMER_LARGEBLOCK_SIZE;
365 printf("z%d:%lld=%d%%",
366 HAMMER_ZONE_DECODE(offset),
367 (offset & ~HAMMER_OFF_ZONE_MASK) / HAMMER_LARGEBLOCK_SIZE,
373 * Check the generic crc on a data element. Inodes record types are
374 * special in that some of their fields are not CRCed.
378 check_data_crc(hammer_btree_elm_t elm)
380 struct buffer_info *data_buffer;
381 hammer_off_t data_offset;
387 data_offset = elm->leaf.data_offset;
388 data_len = elm->leaf.data_len;
390 if (data_offset == 0 || data_len == 0)
395 ptr = get_buffer_data(data_offset, &data_buffer, 0);
396 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
399 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
400 data_len == sizeof(struct hammer_inode_data)) {
401 crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
403 crc = crc32_ext(ptr, len, crc);
409 rel_buffer(data_buffer);
410 if (crc == elm->leaf.data_crc)
417 print_record(hammer_btree_elm_t elm)
419 struct buffer_info *data_buffer;
420 hammer_off_t data_offset;
422 hammer_data_ondisk_t data;
424 data_offset = elm->leaf.data_offset;
425 data_len = elm->leaf.data_len;
429 data = get_buffer_data(data_offset, &data_buffer, 0);
433 switch(elm->leaf.base.rec_type) {
434 case HAMMER_RECTYPE_INODE:
435 printf("\n%17s", "");
436 printf("size=%lld nlinks=%lld",
437 data->inode.size, data->inode.nlinks);
439 printf(" mode=%05o uflags=%08x\n",
443 printf("ctime=%016llx pobjid=%016llx obj_type=%d\n",
444 data->inode.ctime, data->inode.parent_obj_id,
445 data->inode.obj_type);
447 printf("mtime=%016llx", data->inode.mtime);
448 printf(" caps=%02x", data->inode.cap_flags);
451 case HAMMER_RECTYPE_DIRENTRY:
452 printf("\n%17s", "");
453 data_len -= HAMMER_ENTRY_NAME_OFF;
454 printf("dir-entry ino=%016llx lo=%08x name=\"%*.*s\"",
456 data->entry.localization,
457 data_len, data_len, data->entry.name);
459 case HAMMER_RECTYPE_FIX:
460 switch(elm->leaf.base.key) {
461 case HAMMER_FIXKEY_SYMLINK:
462 data_len -= HAMMER_SYMLINK_NAME_OFF;
463 printf("\n%17s", "");
464 printf("symlink=\"%*.*s\"", data_len, data_len,
475 rel_buffer(data_buffer);