Merge commit '1276d1e1a1b128f7093a3021d3f6bc27afa80d23' into amd64
[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.18 2008/10/09 04:20:59 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 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);
56
57 void
58 hammer_cmd_show(hammer_off_t node_offset, int depth,
59                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
60 {
61         struct volume_info *volume;
62         int zone;
63
64         if (node_offset == (hammer_off_t)-1) {
65                 volume = get_volume(RootVolNo);
66                 node_offset = volume->ondisk->vol0_btree_root;
67                 if (QuietOpt < 3) {
68                         printf("Volume header\trecords=%lld next_tid=%016llx\n",
69                                volume->ondisk->vol0_stat_records,
70                                volume->ondisk->vol0_next_tid);
71                         printf("\t\tbufoffset=%016llx\n",
72                                volume->ondisk->vol_buf_beg);
73                         for (zone = 0; zone < HAMMER_MAX_ZONES; ++zone) {
74                                 printf("\t\tzone %d\tnext_offset=%016llx\n",
75                                         zone,
76                                         volume->ondisk->vol0_blockmap[zone].next_offset
77                                 );
78                         }
79                 }
80                 rel_volume(volume);
81         }
82         printf("show %016llx depth %d\n", node_offset, depth);
83         print_btree_node(node_offset, depth, 0, left_bound, right_bound);
84         print_btree_node(node_offset, depth, 1, left_bound, right_bound);
85 }
86
87 static void
88 print_btree_node(hammer_off_t node_offset, int depth, int spike,
89                  hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
90 {
91         struct buffer_info *buffer = NULL;
92         hammer_node_ondisk_t node;
93         hammer_btree_elm_t elm;
94         int i;
95         int flags;
96         int maxcount;
97         char badc;
98
99         node = get_node(node_offset, &buffer);
100
101         if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
102                 badc = ' ';
103         else
104                 badc = 'B';
105
106         if (spike == 0) {
107                 printf("%c   NODE %016llx cnt=%02d p=%016llx "
108                        "type=%c depth=%d",
109                        badc,
110                        node_offset, node->count, node->parent,
111                        (node->type ? node->type : '?'), depth);
112                 printf(" mirror %016llx", node->mirror_tid);
113                 if (QuietOpt < 3) {
114                         printf(" fill=");
115                         print_bigblock_fill(node_offset);
116                 }
117                 printf(" {\n");
118
119                 maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ?
120                            HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS;
121
122                 for (i = 0; i < node->count && i < maxcount; ++i) {
123                         elm = &node->elms[i];
124                         flags = print_elm_flags(node, node_offset,
125                                                 elm, elm->base.btype,
126                                                 left_bound, right_bound);
127                         print_btree_elm(elm, i, node->type, flags, "ELM");
128                 }
129                 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
130                         elm = &node->elms[i];
131                         flags = print_elm_flags(node, node_offset,
132                                                 elm, 'I',
133                                                 left_bound, right_bound);
134                         print_btree_elm(elm, i, node->type, flags, "RBN");
135                 }
136                 printf("    }\n");
137         }
138
139         for (i = 0; i < node->count; ++i) {
140                 elm = &node->elms[i];
141
142                 switch(node->type) {
143                 case HAMMER_BTREE_TYPE_INTERNAL:
144                         if (elm->internal.subtree_offset) {
145                                 print_btree_node(elm->internal.subtree_offset,
146                                                  depth + 1, spike,
147                                                  &elm[0].base, &elm[1].base);
148                         }
149                         break;
150                 default:
151                         break;
152                 }
153         }
154         rel_buffer(buffer);
155 }
156
157 static
158 void
159 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
160                 int flags, const char *label)
161 {
162         char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
163
164         flagstr[0] = flags ? 'B' : 'G';
165         if (flags & FLAG_TOOFARLEFT)
166                 flagstr[2] = 'L';
167         if (flags & FLAG_TOOFARRIGHT)
168                 flagstr[3] = 'R';
169         if (flags & FLAG_BADTYPE)
170                 flagstr[4] = 'T';
171         if (flags & FLAG_BADCHILDPARENT)
172                 flagstr[4] = 'C';
173
174         printf("%s\t%s %2d %c ",
175                flagstr, label, i,
176                (elm->base.btype ? elm->base.btype : '?'));
177         printf("obj=%016llx key=%016llx lo=%08x rt=%02x ot=%02x\n",
178                elm->base.obj_id,
179                elm->base.key,
180                elm->base.localization,
181                elm->base.rec_type,
182                elm->base.obj_type);
183         printf("\t       %c tids %016llx:%016llx ",
184                 (elm->base.delete_tid ? 'd' : ' '),
185                elm->base.create_tid,
186                elm->base.delete_tid);
187
188         switch(type) {
189         case HAMMER_BTREE_TYPE_INTERNAL:
190                 printf("suboff=%016llx", elm->internal.subtree_offset);
191                 if (QuietOpt < 3)
192                         printf(" mirror %016llx", elm->internal.mirror_tid);
193                 break;
194         case HAMMER_BTREE_TYPE_LEAF:
195                 switch(elm->base.btype) {
196                 case HAMMER_BTREE_TYPE_RECORD:
197                         if (QuietOpt < 3)
198                                 printf("\n%s\t         ", check_data_crc(elm));
199                         else
200                                 printf("\n\t         ");
201                         printf("dataoff=%016llx/%d",
202                                 elm->leaf.data_offset, elm->leaf.data_len);
203                         if (QuietOpt < 3) {
204                                 printf(" crc=%04x", elm->leaf.data_crc);
205                                 printf("\n\t         fills=");
206                                 print_bigblock_fill(elm->leaf.data_offset);
207                         }
208                         if (QuietOpt < 2)
209                                 print_record(elm);
210                         break;
211                 }
212                 break;
213         default:
214                 break;
215         }
216         printf("\n");
217 }
218
219 static
220 int
221 print_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
222                 hammer_btree_elm_t elm, u_int8_t btype,
223                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
224 {
225         int flags = 0;
226
227         switch(node->type) {
228         case HAMMER_BTREE_TYPE_INTERNAL:
229                 if (elm->internal.subtree_offset) {
230                         struct buffer_info *buffer = NULL;
231                         hammer_node_ondisk_t subnode;
232
233                         subnode = get_node(elm->internal.subtree_offset,
234                                            &buffer);
235                         if (subnode->parent != node_offset)
236                                 flags |= FLAG_BADCHILDPARENT;
237                         rel_buffer(buffer);
238                 }
239
240                 switch(btype) {
241                 case HAMMER_BTREE_TYPE_INTERNAL:
242                         if (left_bound == NULL || right_bound == NULL)
243                                 break;
244                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
245                                 flags |= FLAG_TOOFARLEFT;
246                         if (hammer_btree_cmp(&elm->base, right_bound) > 0)
247                                 flags |= FLAG_TOOFARRIGHT;
248                         break;
249                 case HAMMER_BTREE_TYPE_LEAF:
250                         if (left_bound == NULL || right_bound == NULL)
251                                 break;
252                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
253                                 flags |= FLAG_TOOFARLEFT;
254                         if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
255                                 flags |= FLAG_TOOFARRIGHT;
256                         break;
257                 default:
258                         flags |= FLAG_BADTYPE;
259                         break;
260                 }
261                 break;
262         case HAMMER_BTREE_TYPE_LEAF:
263                 switch(btype) {
264                 case HAMMER_BTREE_TYPE_RECORD:
265                         if (left_bound == NULL || right_bound == NULL)
266                                 break;
267                         if (hammer_btree_cmp(&elm->base, left_bound) < 0)
268                                 flags |= FLAG_TOOFARLEFT;
269                         if (hammer_btree_cmp(&elm->base, right_bound) >= 0)
270                                 flags |= FLAG_TOOFARRIGHT;
271                         break;
272                 default:
273                         flags |= FLAG_BADTYPE;
274                         break;
275                 }
276                 break;
277         default:
278                 flags |= FLAG_BADTYPE;
279                 break;
280         }
281         return(flags);
282 }
283
284 static
285 void
286 print_bigblock_fill(hammer_off_t offset)
287 {
288         struct hammer_blockmap_layer1 layer1;
289         struct hammer_blockmap_layer2 layer2;
290         int fill;
291
292         blockmap_lookup(offset, &layer1, &layer2);
293         fill = layer2.bytes_free * 100 / HAMMER_LARGEBLOCK_SIZE;
294         fill = 100 - fill;
295
296         printf("z%d:%lld=%d%%",
297                 HAMMER_ZONE_DECODE(offset),
298                 (offset & ~HAMMER_OFF_ZONE_MASK) / HAMMER_LARGEBLOCK_SIZE,
299                 fill
300         );
301 }
302
303 /*
304  * Check the generic crc on a data element.  Inodes record types are
305  * special in that some of their fields are not CRCed.
306  */
307 static
308 const char *
309 check_data_crc(hammer_btree_elm_t elm)
310 {
311         struct buffer_info *data_buffer;
312         hammer_off_t data_offset;
313         int32_t data_len;
314         int32_t len;
315         u_int32_t crc;
316         char *ptr;
317
318         data_offset = elm->leaf.data_offset;
319         data_len = elm->leaf.data_len;
320         data_buffer = NULL;
321         if (data_offset == 0 || data_len == 0)
322                 return("Z");
323
324         crc = 0;
325         while (data_len) {
326                 ptr = get_buffer_data(data_offset, &data_buffer, 0);
327                 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
328                 if (len > data_len)
329                         len = (int)data_len;
330                 if (elm->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
331                     data_len == sizeof(struct hammer_inode_data)) {
332                         crc = crc32_ext(ptr, HAMMER_INODE_CRCSIZE, crc);
333                 } else {
334                         crc = crc32_ext(ptr, len, crc);
335                 }
336                 data_len -= len;
337                 data_offset += len;
338         }
339         if (data_buffer)
340                 rel_buffer(data_buffer);
341         if (crc == elm->leaf.data_crc)
342                 return("");
343         return("B");
344 }
345
346 static
347 void
348 print_record(hammer_btree_elm_t elm)
349 {
350         struct buffer_info *data_buffer;
351         hammer_off_t data_offset;
352         int32_t data_len;
353         hammer_data_ondisk_t data;
354
355         data_offset = elm->leaf.data_offset;
356         data_len = elm->leaf.data_len;
357         data_buffer = NULL;
358
359         if (data_offset)
360                 data = get_buffer_data(data_offset, &data_buffer, 0);
361         else
362                 data = NULL;
363
364         switch(elm->leaf.base.rec_type) {
365         case HAMMER_RECTYPE_INODE:
366                 printf("\n%17s", "");
367                 printf("size=%lld nlinks=%lld",
368                        data->inode.size, data->inode.nlinks);
369                 if (QuietOpt < 1) {
370                         printf(" mode=%05o uflags=%08x\n",
371                                 data->inode.mode,
372                                 data->inode.uflags);
373                         printf("%17s", "");
374                         printf("ctime=%016llx pobjid=%016llx obj_type=%d\n",
375                                 data->inode.ctime, data->inode.parent_obj_id,
376                                 data->inode.obj_type);
377                         printf("%17s", "");
378                         printf("mtime=%016llx", data->inode.mtime);
379                 }
380                 break;
381         case HAMMER_RECTYPE_DIRENTRY:
382                 printf("\n%17s", "");
383                 data_len -= HAMMER_ENTRY_NAME_OFF;
384                 printf("dir-entry ino=%016llx lo=%08x name=\"%*.*s\"",
385                        data->entry.obj_id,
386                        data->entry.localization,
387                        data_len, data_len, data->entry.name);
388                 break;
389         case HAMMER_RECTYPE_FIX:
390                 switch(elm->leaf.base.key) {
391                 case HAMMER_FIXKEY_SYMLINK:
392                         data_len -= HAMMER_SYMLINK_NAME_OFF;
393                         printf("\n%17s", "");
394                         printf("symlink=\"%*.*s\"", data_len, data_len,
395                                 data->symlink.name);
396                         break;
397                 default:
398                         break;
399                 }
400                 break;
401         default:
402                 break;
403         }
404         if (data_buffer)
405                 rel_buffer(data_buffer);
406 }
407