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