4c25f280657532f975804ceed75bcdf41922b827
[dragonfly.git] / sbin / hammer / cmd_show.c
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sbin/hammer/cmd_show.c,v 1.5 2008/02/08 08:30:56 dillon Exp $
35  */
36
37 #include "hammer.h"
38
39 #define FLAG_TOOFARLEFT         0x0001
40 #define FLAG_TOOFARRIGHT        0x0002
41 #define FLAG_BADTYPE            0x0004
42
43 static void print_btree_node(hammer_off_t node_offset, int depth, int spike,
44                         hammer_base_elm_t left_bound,
45                         hammer_base_elm_t right_bound);
46 static void print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
47                         int flags, const char *label);
48 static int print_elm_flags(hammer_node_ondisk_t node, hammer_btree_elm_t elm,
49                         u_int8_t btype, hammer_base_elm_t left_bound,
50                         hammer_base_elm_t right_bound);
51
52 void
53 hammer_cmd_show(hammer_off_t node_offset, int depth,
54                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
55 {
56         struct volume_info *volume;
57
58         if (node_offset == (hammer_off_t)-1) {
59                 volume = get_volume(RootVolNo);
60                 node_offset = volume->ondisk->vol0_btree_root;
61                 if (VerboseOpt) {
62                         printf("\trecords=%lld\n",
63                                volume->ondisk->vol0_stat_records);
64                 }
65                 rel_volume(volume);
66         }
67         printf("show %016llx depth %d\n", node_offset, depth);
68         print_btree_node(node_offset, depth, 0, left_bound, right_bound);
69         print_btree_node(node_offset, depth, 1, left_bound, right_bound);
70 }
71
72 static void
73 print_btree_node(hammer_off_t node_offset, int depth, int spike,
74                  hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
75 {
76         struct buffer_info *buffer = NULL;
77         hammer_node_ondisk_t node;
78         hammer_btree_elm_t elm;
79         int i;
80         int flags;
81
82         node = get_node(node_offset, &buffer);
83
84         if (spike == 0) {
85                 printf("    NODE %016llx count=%d parent=%016llx "
86                        "type=%c depth=%d {\n",
87                        node_offset, node->count, node->parent,
88                        (node->type ? node->type : '?'), depth);
89
90                 for (i = 0; i < node->count; ++i) {
91                         elm = &node->elms[i];
92                         flags = print_elm_flags(node, elm, elm->base.btype,
93                                                 left_bound, right_bound);
94                         print_btree_elm(elm, i, node->type, flags, "ELM");
95                 }
96                 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
97                         elm = &node->elms[i];
98                         flags = print_elm_flags(node, elm, 'I',
99                                                 left_bound, right_bound);
100                         print_btree_elm(elm, i, node->type, flags, "RBN");
101                 }
102                 printf("    }\n");
103         }
104
105         for (i = 0; i < node->count; ++i) {
106                 elm = &node->elms[i];
107
108                 switch(node->type) {
109                 case HAMMER_BTREE_TYPE_INTERNAL:
110                         if (elm->internal.subtree_offset) {
111                                 print_btree_node(elm->internal.subtree_offset,
112                                                  depth + 1, spike,
113                                                  &elm[0].base, &elm[1].base);
114                         }
115                         break;
116                 default:
117                         break;
118                 }
119         }
120         rel_buffer(buffer);
121 }
122
123 static
124 void
125 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
126                 int flags, const char *label)
127 {
128         char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
129
130         flagstr[0] = flags ? 'B' : 'G';
131         if (flags & FLAG_TOOFARLEFT)
132                 flagstr[2] = 'L';
133         if (flags & FLAG_TOOFARRIGHT)
134                 flagstr[3] = 'R';
135         if (flags & FLAG_BADTYPE)
136                 flagstr[4] = 'T';
137
138         printf("%s\t%s %2d %c ",
139                flagstr, label, i,
140                (elm->base.btype ? elm->base.btype : '?'));
141         printf("obj=%016llx key=%016llx rt=%02x ot=%02x\n",
142                elm->base.obj_id,
143                elm->base.key,
144                elm->base.rec_type,
145                elm->base.obj_type);
146         printf("\t         tids %016llx:%016llx ",
147                elm->base.create_tid,
148                elm->base.delete_tid);
149
150         switch(type) {
151         case HAMMER_BTREE_TYPE_INTERNAL:
152                 printf("suboff=%016llx", elm->internal.subtree_offset);
153                 break;
154         case HAMMER_BTREE_TYPE_LEAF:
155                 switch(elm->base.btype) {
156                 case HAMMER_BTREE_TYPE_RECORD:
157                         printf("recoff=%016llx", elm->leaf.rec_offset);
158                         break;
159                 }
160                 break;
161         default:
162                 break;
163         }
164         printf("\n");
165 }
166
167 static
168 int
169 print_elm_flags(hammer_node_ondisk_t node, hammer_btree_elm_t elm,
170                 u_int8_t btype,
171                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
172 {
173         int flags = 0;
174
175         switch(node->type) {
176         case HAMMER_BTREE_TYPE_INTERNAL:
177                 switch(btype) {
178                 case HAMMER_BTREE_TYPE_INTERNAL:
179                         if (left_bound == NULL || right_bound == NULL)
180                                 break;
181                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
182                                 flags |= FLAG_TOOFARLEFT;
183                         if (hammer_btree_cmp(&elm->base, right_bound) > 0)
184                                 flags |= FLAG_TOOFARRIGHT;
185                         break;
186                 case HAMMER_BTREE_TYPE_LEAF:
187                         if (left_bound == NULL || right_bound == NULL)
188                                 break;
189                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
190                                 flags |= FLAG_TOOFARLEFT;
191                         if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
192                                 flags |= FLAG_TOOFARRIGHT;
193                         break;
194                 default:
195                         flags |= FLAG_BADTYPE;
196                         break;
197                 }
198                 break;
199         case HAMMER_BTREE_TYPE_LEAF:
200                 switch(btype) {
201                 case HAMMER_BTREE_TYPE_RECORD:
202                         if (left_bound == NULL || right_bound == NULL)
203                                 break;
204                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
205                                 flags |= FLAG_TOOFARLEFT;
206                         if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
207                                 flags |= FLAG_TOOFARRIGHT;
208                         break;
209                 default:
210                         flags |= FLAG_BADTYPE;
211                         break;
212                 }
213                 break;
214         default:
215                 flags |= FLAG_BADTYPE;
216                 break;
217         }
218         return(flags);
219 }
220