HAMMER 41B/Many: Cleanup.
[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.8 2008/05/05 20:34:52 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 #define FLAG_BADCHILDPARENT     0x0008
43
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 void print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
48                         int flags, const char *label);
49 static int print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
50                         hammer_btree_elm_t elm, u_int8_t btype,
51                         hammer_base_elm_t left_bound,
52                         hammer_base_elm_t right_bound);
53 static void print_bigblock_fill(hammer_off_t offset);
54 static void print_record(hammer_btree_elm_t elm);
55
56 void
57 hammer_cmd_show(hammer_off_t node_offset, int depth,
58                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
59 {
60         struct volume_info *volume;
61
62         if (node_offset == (hammer_off_t)-1) {
63                 volume = get_volume(RootVolNo);
64                 node_offset = volume->ondisk->vol0_btree_root;
65                 if (VerboseOpt) {
66                         printf("\trecords=%lld\n",
67                                volume->ondisk->vol0_stat_records);
68                 }
69                 rel_volume(volume);
70         }
71         printf("show %016llx depth %d\n", node_offset, depth);
72         print_btree_node(node_offset, depth, 0, left_bound, right_bound);
73         print_btree_node(node_offset, depth, 1, left_bound, right_bound);
74 }
75
76 static void
77 print_btree_node(hammer_off_t node_offset, int depth, int spike,
78                  hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
79 {
80         struct buffer_info *buffer = NULL;
81         hammer_node_ondisk_t node;
82         hammer_btree_elm_t elm;
83         int i;
84         int flags;
85
86         node = get_node(node_offset, &buffer);
87
88         if (spike == 0) {
89                 printf("    NODE %016llx count=%d parent=%016llx "
90                        "type=%c depth=%d fill=",
91                        node_offset, node->count, node->parent,
92                        (node->type ? node->type : '?'), depth);
93                 if (VerboseOpt)
94                         print_bigblock_fill(node_offset);
95                 printf(" {\n");
96
97                 for (i = 0; i < node->count; ++i) {
98                         elm = &node->elms[i];
99                         flags = print_elm_flags(node, node_offset,
100                                                 elm, elm->base.btype,
101                                                 left_bound, right_bound);
102                         print_btree_elm(elm, i, node->type, flags, "ELM");
103                 }
104                 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
105                         elm = &node->elms[i];
106                         flags = print_elm_flags(node, node_offset,
107                                                 elm, 'I',
108                                                 left_bound, right_bound);
109                         print_btree_elm(elm, i, node->type, flags, "RBN");
110                 }
111                 printf("    }\n");
112         }
113
114         for (i = 0; i < node->count; ++i) {
115                 elm = &node->elms[i];
116
117                 switch(node->type) {
118                 case HAMMER_BTREE_TYPE_INTERNAL:
119                         if (elm->internal.subtree_offset) {
120                                 print_btree_node(elm->internal.subtree_offset,
121                                                  depth + 1, spike,
122                                                  &elm[0].base, &elm[1].base);
123                         }
124                         break;
125                 default:
126                         break;
127                 }
128         }
129         rel_buffer(buffer);
130 }
131
132 static
133 void
134 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
135                 int flags, const char *label)
136 {
137         char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
138
139         flagstr[0] = flags ? 'B' : 'G';
140         if (flags & FLAG_TOOFARLEFT)
141                 flagstr[2] = 'L';
142         if (flags & FLAG_TOOFARRIGHT)
143                 flagstr[3] = 'R';
144         if (flags & FLAG_BADTYPE)
145                 flagstr[4] = 'T';
146         if (flags & FLAG_BADCHILDPARENT)
147                 flagstr[4] = 'C';
148
149         printf("%s\t%s %2d %c ",
150                flagstr, label, i,
151                (elm->base.btype ? elm->base.btype : '?'));
152         printf("obj=%016llx key=%016llx rt=%02x ot=%02x\n",
153                elm->base.obj_id,
154                elm->base.key,
155                elm->base.rec_type,
156                elm->base.obj_type);
157         printf("\t       %c tids %016llx:%016llx ",
158                 (elm->base.delete_tid ? 'd' : ' '),
159                elm->base.create_tid,
160                elm->base.delete_tid);
161
162         switch(type) {
163         case HAMMER_BTREE_TYPE_INTERNAL:
164                 printf("suboff=%016llx", elm->internal.subtree_offset);
165                 break;
166         case HAMMER_BTREE_TYPE_LEAF:
167                 switch(elm->base.btype) {
168                 case HAMMER_BTREE_TYPE_RECORD:
169                         printf("\n\t         ");
170                         printf("recoff=%016llx", elm->leaf.rec_offset);
171                         printf(" dataoff=%016llx/%d",
172                                 elm->leaf.data_offset, elm->leaf.data_len);
173                         if (VerboseOpt) {
174                                 printf("\n\t         fills=");
175                                 print_bigblock_fill(elm->leaf.rec_offset);
176                                 printf(", ");
177                                 print_bigblock_fill(elm->leaf.data_offset);
178                         }
179                         if (VerboseOpt > 1)
180                                 print_record(elm);
181                         break;
182                 }
183                 break;
184         default:
185                 break;
186         }
187         printf("\n");
188 }
189
190 static
191 int
192 print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
193                 hammer_btree_elm_t elm, u_int8_t btype,
194                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
195 {
196         int flags = 0;
197
198         switch(node->type) {
199         case HAMMER_BTREE_TYPE_INTERNAL:
200                 if (elm->internal.subtree_offset) {
201                         struct buffer_info *buffer = NULL;
202                         hammer_node_ondisk_t subnode;
203
204                         subnode = get_node(elm->internal.subtree_offset,
205                                            &buffer);
206                         if (subnode->parent != node_offset)
207                                 flags |= FLAG_BADCHILDPARENT;
208                         rel_buffer(buffer);
209                 }
210
211                 switch(btype) {
212                 case HAMMER_BTREE_TYPE_INTERNAL:
213                         if (left_bound == NULL || right_bound == NULL)
214                                 break;
215                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
216                                 flags |= FLAG_TOOFARLEFT;
217                         if (hammer_btree_cmp(&elm->base, right_bound) > 0)
218                                 flags |= FLAG_TOOFARRIGHT;
219                         break;
220                 case HAMMER_BTREE_TYPE_LEAF:
221                         if (left_bound == NULL || right_bound == NULL)
222                                 break;
223                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
224                                 flags |= FLAG_TOOFARLEFT;
225                         if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
226                                 flags |= FLAG_TOOFARRIGHT;
227                         break;
228                 default:
229                         flags |= FLAG_BADTYPE;
230                         break;
231                 }
232                 break;
233         case HAMMER_BTREE_TYPE_LEAF:
234                 switch(btype) {
235                 case HAMMER_BTREE_TYPE_RECORD:
236                         if (left_bound == NULL || right_bound == NULL)
237                                 break;
238                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
239                                 flags |= FLAG_TOOFARLEFT;
240                         if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
241                                 flags |= FLAG_TOOFARRIGHT;
242                         break;
243                 default:
244                         flags |= FLAG_BADTYPE;
245                         break;
246                 }
247                 break;
248         default:
249                 flags |= FLAG_BADTYPE;
250                 break;
251         }
252         return(flags);
253 }
254
255 static
256 void
257 print_bigblock_fill(hammer_off_t offset)
258 {
259         struct hammer_blockmap_layer1 layer1;
260         struct hammer_blockmap_layer2 layer2;
261         int fill;
262
263         blockmap_lookup(offset, &layer1, &layer2);
264         fill = layer2.bytes_free * 100 / HAMMER_LARGEBLOCK_SIZE;
265         fill = 100 - fill;
266
267         printf("z%d:%lld=%d%%",
268                 HAMMER_ZONE_DECODE(offset),
269                 (offset & ~HAMMER_OFF_ZONE_MASK) / HAMMER_LARGEBLOCK_SIZE,
270                 fill
271         );
272 }
273
274 static
275 void
276 print_record(hammer_btree_elm_t elm)
277 {
278         struct buffer_info *rec_buffer;
279         struct buffer_info *data_buffer;
280         hammer_record_ondisk_t rec;
281         hammer_off_t rec_offset;
282         hammer_off_t data_offset;
283         hammer_crc_t crc;
284         int32_t data_len;
285         char *data;
286
287         rec_offset = elm->leaf.rec_offset;
288         data_offset = elm->leaf.data_offset;
289         data_len = elm->leaf.data_len;
290         rec_buffer = NULL;
291         data_buffer = NULL;
292
293         rec = get_buffer_data(rec_offset, &rec_buffer, 0);
294         if (data_offset)
295                 data = get_buffer_data(data_offset, &data_buffer, 0);
296         else
297                 data = NULL;
298
299         if (rec == NULL) {
300                 printf("record FAILED\n");
301                 return;
302         }
303         switch(rec->base.base.rec_type) {
304         case HAMMER_RECTYPE_INODE:
305                 printf("\n%17s", "");
306                 printf("size=%lld nlinks=%lld",
307                        rec->inode.ino_size, rec->inode.ino_nlinks);
308                 break;
309         case HAMMER_RECTYPE_DIRENTRY:
310                 printf("\n%17s", "");
311                 printf("dir-entry ino=%016llx name=\"%*.*s\"",
312                        rec->entry.obj_id,
313                        data_len, data_len, data);
314                 break;
315         case HAMMER_RECTYPE_FIX:
316                 switch(rec->base.base.key) {
317                 case HAMMER_FIXKEY_SYMLINK:
318                         printf("\n%17s", "");
319                         printf("symlink=\"%*.*s\"", data_len, data_len, data);
320                         break;
321                 default:
322                         break;
323                 }
324                 break;
325         default:
326                 break;
327         }
328         if (rec->base.signature != HAMMER_RECORD_SIGNATURE_GOOD) {
329                 printf("\n%17s", "");
330                 printf("BAD SIGNATURE: %08x\n", rec->base.signature);
331         }
332         crc = crc32(&rec->base.rec_crc + 1, HAMMER_RECORD_CRCSIZE);
333         if (crc != rec->base.rec_crc) {
334                 printf("\n%17s", "");
335                 printf("BAD CRC: %08x v %08x\n", rec->base.rec_crc, crc);
336         }
337
338         if (rec_buffer)
339                 rel_buffer(rec_buffer);
340         if (data_buffer)
341                 rel_buffer(data_buffer);
342 }
343