1e3953e2c081633b72dbc52c7cf30fbb8034dcaa
[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 typedef struct btree_search {
45         u_int32_t       lo;
46         int64_t         obj_id;
47 } *btree_search_t;
48
49 static void print_btree_node(hammer_off_t node_offset, btree_search_t search,
50                         int depth, int spike,
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);
62
63 void
64 hammer_cmd_show(hammer_off_t node_offset, u_int32_t lo, int64_t obj_id,
65                 int depth,
66                 hammer_base_elm_t left_bound, hammer_base_elm_t right_bound)
67 {
68         struct volume_info *volume;
69         struct btree_search search;
70         btree_search_t searchp;
71         int zone;
72
73         if (node_offset == (hammer_off_t)-1) {
74                 volume = get_volume(RootVolNo);
75                 node_offset = volume->ondisk->vol0_btree_root;
76                 if (QuietOpt < 3) {
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",
84                                         zone,
85                                         volume->ondisk->vol0_blockmap[zone].next_offset
86                                 );
87                         }
88                 }
89                 rel_volume(volume);
90         }
91
92         if (lo == 0 && obj_id == (int64_t)HAMMER_MIN_OBJID) {
93                 searchp = NULL;
94                 printf("show %016llx depth %d\n", node_offset, depth);
95         } else {
96                 search.lo = lo;
97                 search.obj_id = obj_id;
98                 searchp = &search;
99                 printf("show %016llx lo %08x obj_id %016llx depth %d\n",
100                         node_offset, lo, (long long)obj_id, depth);
101         }
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);
106 }
107
108 static void
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)
112 {
113         struct buffer_info *buffer = NULL;
114         hammer_node_ondisk_t node;
115         hammer_btree_elm_t elm;
116         int i;
117         int flags;
118         int maxcount;
119         char badc;
120         const char *ext;
121
122         node = get_node(node_offset, &buffer);
123
124         if (crc32(&node->crc + 1, HAMMER_BTREE_CRCSIZE) == node->crc)
125                 badc = ' ';
126         else
127                 badc = 'B';
128
129         if (spike == 0) {
130                 printf("%c   NODE %016llx cnt=%02d p=%016llx "
131                        "type=%c depth=%d",
132                        badc,
133                        node_offset, node->count, node->parent,
134                        (node->type ? node->type : '?'), depth);
135                 printf(" mirror %016llx", node->mirror_tid);
136                 if (QuietOpt < 3) {
137                         printf(" fill=");
138                         print_bigblock_fill(node_offset);
139                 }
140                 printf(" {\n");
141
142                 maxcount = (node->type == HAMMER_BTREE_TYPE_INTERNAL) ?
143                            HAMMER_BTREE_INT_ELMS : HAMMER_BTREE_LEAF_ELMS;
144
145                 for (i = 0; i < node->count && i < maxcount; ++i) {
146                         elm = &node->elms[i];
147
148                         if (node->type != HAMMER_BTREE_TYPE_INTERNAL) {
149                                 ext = NULL;
150                                 if (search &&
151                                     elm->base.localization == search->lo &&
152                                      elm->base.obj_id == search->obj_id) {
153                                         ext = " *";
154                                 }
155                         } else if (search) {
156                                 ext = " *";
157                                 if (elm->base.localization > search->lo ||
158                                     (elm->base.localization == search->lo &&
159                                      elm->base.obj_id > search->obj_id)) {
160                                         ext = NULL;
161                                 }
162                                 if (elm[1].base.localization < search->lo ||
163                                     (elm[1].base.localization == search->lo &&
164                                      elm[1].base.obj_id < search->obj_id)) {
165                                         ext = NULL;
166                                 }
167                         } else {
168                                 ext = NULL;
169                         }
170
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);
175                 }
176                 if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
177                         elm = &node->elms[i];
178
179                         flags = print_elm_flags(node, node_offset,
180                                                 elm, 'I',
181                                                 left_bound, right_bound);
182                         print_btree_elm(elm, i, node->type, flags, "RBN", NULL);
183                 }
184                 printf("    }\n");
185         }
186
187         for (i = 0; i < node->count; ++i) {
188                 elm = &node->elms[i];
189
190                 switch(node->type) {
191                 case HAMMER_BTREE_TYPE_INTERNAL:
192                         if (search) {
193                                 if (elm->base.localization > search->lo ||
194                                     (elm->base.localization == search->lo &&
195                                      elm->base.obj_id > search->obj_id)) {
196                                         break;
197                                 }
198                                 if (elm[1].base.localization < search->lo ||
199                                     (elm[1].base.localization == search->lo &&
200                                      elm[1].base.obj_id < search->obj_id)) {
201                                         break;
202                                 }
203                         }
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);
208                                 /*
209                                  * Cause show to iterate after seeking to
210                                  * the lo:objid
211                                  */
212                                 search = NULL;
213                         }
214                         break;
215                 default:
216                         break;
217                 }
218         }
219         rel_buffer(buffer);
220 }
221
222 static
223 void
224 print_btree_elm(hammer_btree_elm_t elm, int i, u_int8_t type,
225                 int flags, const char *label, const char *ext)
226 {
227         char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
228
229         flagstr[0] = flags ? 'B' : 'G';
230         if (flags & FLAG_TOOFARLEFT)
231                 flagstr[2] = 'L';
232         if (flags & FLAG_TOOFARRIGHT)
233                 flagstr[3] = 'R';
234         if (flags & FLAG_BADTYPE)
235                 flagstr[4] = 'T';
236         if (flags & FLAG_BADCHILDPARENT)
237                 flagstr[4] = 'C';
238
239         printf("%s\t%s %2d %c ",
240                flagstr, label, i,
241                (elm->base.btype ? elm->base.btype : '?'));
242         printf("obj=%016llx key=%016llx lo=%08x rt=%02x ot=%02x\n",
243                elm->base.obj_id,
244                elm->base.key,
245                elm->base.localization,
246                elm->base.rec_type,
247                elm->base.obj_type);
248         printf("\t       %c tids %016llx:%016llx ",
249                 (elm->base.delete_tid ? 'd' : ' '),
250                elm->base.create_tid,
251                elm->base.delete_tid);
252
253         switch(type) {
254         case HAMMER_BTREE_TYPE_INTERNAL:
255                 printf("suboff=%016llx", elm->internal.subtree_offset);
256                 if (QuietOpt < 3)
257                         printf(" mirror %016llx", elm->internal.mirror_tid);
258                 if (ext)
259                         printf(" %s", ext);
260                 break;
261         case HAMMER_BTREE_TYPE_LEAF:
262                 if (ext)
263                         printf(" %s", ext);
264                 switch(elm->base.btype) {
265                 case HAMMER_BTREE_TYPE_RECORD:
266                         if (QuietOpt < 3)
267                                 printf("\n%s\t         ", check_data_crc(elm));
268                         else
269                                 printf("\n\t         ");
270                         printf("dataoff=%016llx/%d",
271                                 elm->leaf.data_offset, elm->leaf.data_len);
272                         if (QuietOpt < 3) {
273                                 printf(" crc=%04x", elm->leaf.data_crc);
274                                 printf("\n\t         fills=");
275                                 print_bigblock_fill(elm->leaf.data_offset);
276                         }
277                         if (QuietOpt < 2)
278                                 print_record(elm);
279                         break;
280                 }
281                 break;
282         default:
283                 break;
284         }
285         printf("\n");
286 }
287
288 static
289 int
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)
293 {
294         int flags = 0;
295
296         switch(node->type) {
297         case HAMMER_BTREE_TYPE_INTERNAL:
298                 if (elm->internal.subtree_offset) {
299                         struct buffer_info *buffer = NULL;
300                         hammer_node_ondisk_t subnode;
301
302                         subnode = get_node(elm->internal.subtree_offset,
303                                            &buffer);
304                         if (subnode->parent != node_offset)
305                                 flags |= FLAG_BADCHILDPARENT;
306                         rel_buffer(buffer);
307                 }
308
309                 switch(btype) {
310                 case HAMMER_BTREE_TYPE_INTERNAL:
311                         if (left_bound == NULL || right_bound == NULL)
312                                 break;
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;
317                         break;
318                 case HAMMER_BTREE_TYPE_LEAF:
319                         if (left_bound == NULL || right_bound == NULL)
320                                 break;
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;
325                         break;
326                 default:
327                         flags |= FLAG_BADTYPE;
328                         break;
329                 }
330                 break;
331         case HAMMER_BTREE_TYPE_LEAF:
332                 switch(btype) {
333                 case HAMMER_BTREE_TYPE_RECORD:
334                         if (left_bound == NULL || right_bound == NULL)
335                                 break;
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;
340                         break;
341                 default:
342                         flags |= FLAG_BADTYPE;
343                         break;
344                 }
345                 break;
346         default:
347                 flags |= FLAG_BADTYPE;
348                 break;
349         }
350         return(flags);
351 }
352
353 static
354 void
355 print_bigblock_fill(hammer_off_t offset)
356 {
357         struct hammer_blockmap_layer1 layer1;
358         struct hammer_blockmap_layer2 layer2;
359         int fill;
360
361         blockmap_lookup(offset, &layer1, &layer2);
362         fill = layer2.bytes_free * 100 / HAMMER_LARGEBLOCK_SIZE;
363         fill = 100 - fill;
364
365         printf("z%d:%lld=%d%%",
366                 HAMMER_ZONE_DECODE(offset),
367                 (offset & ~HAMMER_OFF_ZONE_MASK) / HAMMER_LARGEBLOCK_SIZE,
368                 fill
369         );
370 }
371
372 /*
373  * Check the generic crc on a data element.  Inodes record types are
374  * special in that some of their fields are not CRCed.
375  */
376 static
377 const char *
378 check_data_crc(hammer_btree_elm_t elm)
379 {
380         struct buffer_info *data_buffer;
381         hammer_off_t data_offset;
382         int32_t data_len;
383         int32_t len;
384         u_int32_t crc;
385         char *ptr;
386
387         data_offset = elm->leaf.data_offset;
388         data_len = elm->leaf.data_len;
389         data_buffer = NULL;
390         if (data_offset == 0 || data_len == 0)
391                 return("Z");
392
393         crc = 0;
394         while (data_len) {
395                 ptr = get_buffer_data(data_offset, &data_buffer, 0);
396                 len = HAMMER_BUFSIZE - ((int)data_offset & HAMMER_BUFMASK);
397                 if (len > data_len)
398                         len = (int)data_len;
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);
402                 } else {
403                         crc = crc32_ext(ptr, len, crc);
404                 }
405                 data_len -= len;
406                 data_offset += len;
407         }
408         if (data_buffer)
409                 rel_buffer(data_buffer);
410         if (crc == elm->leaf.data_crc)
411                 return("");
412         return("B");
413 }
414
415 static
416 void
417 print_record(hammer_btree_elm_t elm)
418 {
419         struct buffer_info *data_buffer;
420         hammer_off_t data_offset;
421         int32_t data_len;
422         hammer_data_ondisk_t data;
423
424         data_offset = elm->leaf.data_offset;
425         data_len = elm->leaf.data_len;
426         data_buffer = NULL;
427
428         if (data_offset)
429                 data = get_buffer_data(data_offset, &data_buffer, 0);
430         else
431                 data = NULL;
432
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);
438                 if (QuietOpt < 1) {
439                         printf(" mode=%05o uflags=%08x\n",
440                                 data->inode.mode,
441                                 data->inode.uflags);
442                         printf("%17s", "");
443                         printf("ctime=%016llx pobjid=%016llx obj_type=%d\n",
444                                 data->inode.ctime, data->inode.parent_obj_id,
445                                 data->inode.obj_type);
446                         printf("%17s", "");
447                         printf("mtime=%016llx", data->inode.mtime);
448                         printf(" caps=%02x", data->inode.cap_flags);
449                 }
450                 break;
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\"",
455                        data->entry.obj_id,
456                        data->entry.localization,
457                        data_len, data_len, data->entry.name);
458                 break;
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,
465                                 data->symlink.name);
466                         break;
467                 default:
468                         break;
469                 }
470                 break;
471         default:
472                 break;
473         }
474         if (data_buffer)
475                 rel_buffer(data_buffer);
476 }
477