sbin/*hammer: Use consistent static/inline/returntype format for functions
[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
35 #include <libutil.h>
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 #define FLAG_BADMIRRORTID       0x0010
44
45 struct {
46         struct hammer_base_elm base;
47         int limit;      /* # of fields to test */
48         int filter;     /* filter type (default -1) */
49         int obfuscate;  /* obfuscate direntry name */
50         int indent;     /* use depth indentation */
51         zone_stat_t stats;
52 } opt;
53
54 static __inline void print_btree(hammer_off_t node_offset);
55 static __inline void print_subtree(hammer_btree_elm_t elm);
56 static void print_btree_node(hammer_off_t node_offset, hammer_tid_t mirror_tid,
57         hammer_btree_elm_t lbe);
58 static int test_node_count(hammer_node_ondisk_t node, char *badmp);
59 static void print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
60         hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext);
61 static int get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
62         hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
63 static int test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
64 static int test_rbn_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe);
65 static void print_bigblock_fill(hammer_off_t offset);
66 static const char *check_data_crc(hammer_btree_elm_t elm, const char **whichp);
67 static hammer_crc_t get_leaf_crc(uint32_t vol_version, void *data,
68         hammer_btree_leaf_elm_t leaf, const char **whichp);
69 static uint32_t get_buf_crc(hammer_off_t buf_offset, int32_t buf_len,
70         uint32_t leaf_crc, const char **whichp);
71 static void print_record(hammer_btree_elm_t elm);
72 static int init_btree_search(const char *arg);
73 static int test_btree_search(hammer_btree_elm_t elm);
74 static int test_btree_match(hammer_btree_elm_t elm);
75 static int test_btree_out_of_range(hammer_btree_elm_t elm);
76 static void hexdump_record(const void *ptr, int length, const char *hdr);
77
78 static int num_bad_node = 0;
79 static int num_bad_elm = 0;
80 static int num_bad_rec = 0;
81 static int depth;
82
83 #define _X      "\t"
84 static const char* _indents[] = {
85         "",
86         _X,
87         _X _X,
88         _X _X _X,
89         _X _X _X _X,
90         _X _X _X _X _X,
91         _X _X _X _X _X _X,
92         _X _X _X _X _X _X _X,
93         _X _X _X _X _X _X _X _X,
94         _X _X _X _X _X _X _X _X _X,
95         _X _X _X _X _X _X _X _X _X _X,
96         _X _X _X _X _X _X _X _X _X _X _X,
97         _X _X _X _X _X _X _X _X _X _X _X _X,
98         _X _X _X _X _X _X _X _X _X _X _X _X _X,
99         _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
100         _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
101         _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X _X,
102         /* deep enough */
103 };
104 #define INDENT _indents[opt.indent ? depth : 0]
105
106 void
107 hammer_cmd_show(const char *arg, int filter, int obfuscate, int indent)
108 {
109         volume_info_t volume;
110         hammer_volume_ondisk_t ondisk;
111         zone_stat_t stats = NULL;
112
113         volume = get_root_volume();
114         ondisk = volume->ondisk;
115
116         print_blockmap(volume);
117
118         if (VerboseOpt)
119                 stats = hammer_init_zone_stat_bits();
120
121         bzero(&opt, sizeof(opt));
122         opt.filter = filter;
123         opt.obfuscate = obfuscate;
124         opt.indent = indent;
125         opt.stats = stats;
126
127         if (init_btree_search(arg) > 0) {
128                 printf("arg=\"%s\"", arg);
129                 if (opt.limit > 0)
130                         printf(" lo=%08x", opt.base.localization);
131                 if (opt.limit > 1)
132                         printf(" objid=%016jx", (uintmax_t)opt.base.obj_id);
133                 if (opt.limit > 2)
134                         printf(" rt=%02x", opt.base.rec_type);
135                 if (opt.limit > 3)
136                         printf(" key=%016jx", (uintmax_t)opt.base.key);
137                 if (opt.limit > 4)
138                         printf(" tid=%016jx", (uintmax_t)opt.base.create_tid);
139                 printf("\n");
140         }
141         print_btree(ondisk->vol0_btree_root);
142
143         if (stats) {
144                 hammer_print_zone_stat(stats);
145                 hammer_cleanup_zone_stat(stats);
146         }
147
148         if (num_bad_node || VerboseOpt)
149                 printf("%d bad nodes\n", num_bad_node);
150         if (num_bad_elm || VerboseOpt)
151                 printf("%d bad elms\n", num_bad_elm);
152         if (num_bad_rec || VerboseOpt)
153                 printf("%d bad records\n", num_bad_rec);
154 }
155
156 static __inline
157 void
158 print_btree(hammer_off_t node_offset)
159 {
160         depth = -1;
161         print_btree_node(node_offset, HAMMER_MAX_TID, NULL);
162         assert(depth == -1);
163 }
164
165 static __inline
166 void
167 print_subtree(hammer_btree_elm_t elm)
168 {
169         hammer_btree_internal_elm_t i = &elm->internal;
170         print_btree_node(i->subtree_offset, i->mirror_tid, elm);
171 }
172
173 static
174 void
175 print_btree_node(hammer_off_t node_offset, hammer_tid_t mirror_tid,
176         hammer_btree_elm_t lbe)
177 {
178         buffer_info_t buffer = NULL;
179         hammer_node_ondisk_t node;
180         hammer_btree_elm_t elm;
181         int i;
182         char badc = ' ';  /* good */
183         char badm = ' ';  /* good */
184         const char *ext;
185
186         depth++;
187         node = get_buffer_data(node_offset, &buffer, 0);
188
189         if (node == NULL) {
190                 badc = 'B';
191                 badm = 'I';
192         } else {
193                 if (!hammer_crc_test_btree(HammerVersion, node))
194                         badc = 'B';
195                 if (node->mirror_tid > mirror_tid) {
196                         badc = 'B';
197                         badm = 'M';
198                 }
199                 if (test_node_count(node, &badm) == -1) {
200                         badc = 'B';
201                         assert(badm != ' ');
202                 }
203         }
204
205         if (badm != ' ' || badc != ' ')  /* not good */
206                 ++num_bad_node;
207
208         printf("%s%c%c   NODE %016jx ",
209                INDENT, badc, badm, (uintmax_t)node_offset);
210         printf("cnt=%02d p=%016jx type=%c depth=%d mirror=%016jx",
211                node->count,
212                (uintmax_t)node->parent,
213                (node->type ? node->type : '?'),
214                depth,
215                (uintmax_t)node->mirror_tid);
216         printf(" fill=");
217         print_bigblock_fill(node_offset);
218         printf(" {\n");
219
220         if (opt.stats)
221                 hammer_add_zone_stat(opt.stats, node_offset, sizeof(*node));
222
223         for (i = 0; i < node->count; ++i) {
224                 elm = &node->elms[i];
225                 ext = NULL;
226                 if (opt.limit) {
227                         switch (node->type) {
228                         case HAMMER_BTREE_TYPE_INTERNAL:
229                                 if (!test_btree_out_of_range(elm))
230                                         ext = "*";
231                                 break;
232                         case HAMMER_BTREE_TYPE_LEAF:
233                                 if (test_btree_match(elm))
234                                         ext = "*";
235                                 break;
236                         }
237                 }
238                 print_btree_elm(node, node_offset, elm, lbe, ext);
239         }
240         if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
241                 assert(i == node->count);  /* boundary */
242                 elm = &node->elms[i];
243                 print_btree_elm(node, node_offset, elm, lbe, NULL);
244         }
245         printf("%s     }\n", INDENT);
246
247         if (node->type == HAMMER_BTREE_TYPE_INTERNAL) {
248                 for (i = 0; i < node->count; ++i) {
249                         elm = &node->elms[i];
250                         if (opt.limit && opt.filter) {
251                                 if (test_btree_out_of_range(elm))
252                                         continue;
253                         }
254                         if (elm->internal.subtree_offset) {
255                                 print_subtree(elm);
256                                 /*
257                                  * Cause show to do normal iteration after
258                                  * seeking to the lo:objid:rt:key:tid
259                                  * by default
260                                  */
261                                 if (opt.limit && opt.filter == -1)  /* default */
262                                         opt.filter = 0;
263                         }
264                 }
265         }
266         rel_buffer(buffer);
267         depth--;
268 }
269
270 static
271 int
272 test_node_count(hammer_node_ondisk_t node, char *badmp)
273 {
274         hammer_node_ondisk_t parent_node;
275         buffer_info_t buffer = NULL;
276         int maxcount;
277
278         maxcount = hammer_node_max_elements(node->type);
279
280         if (maxcount == -1) {
281                 *badmp = 'U';
282                 return(-1);
283         } else if (node->count > maxcount) {
284                 *badmp = 'C';
285                 return(-1);
286         } else if (node->count == 0) {
287                 parent_node = get_buffer_data(node->parent, &buffer, 0);
288                 if (parent_node->count != 1) {
289                         *badmp = 'C';
290                         rel_buffer(buffer);
291                         return(-1);
292                 }
293                 rel_buffer(buffer);
294         }
295
296         return(0);
297 }
298
299 static __inline
300 int
301 is_root_btree_beg(uint8_t type, int i, hammer_btree_elm_t elm)
302 {
303         /*
304          * elm->base.btype depends on what the original node had
305          * so it could be anything but HAMMER_BTREE_TYPE_NONE.
306          */
307         return (type == HAMMER_BTREE_TYPE_INTERNAL &&
308                 i == 0 &&
309                 elm->base.localization == HAMMER_MIN_ONDISK_LOCALIZATION &&
310                 elm->base.obj_id == (int64_t)HAMMER_MIN_OBJID &&
311                 elm->base.key == (int64_t)HAMMER_MIN_KEY &&
312                 elm->base.create_tid == 1 &&
313                 elm->base.delete_tid == 1 &&
314                 elm->base.rec_type == HAMMER_MIN_RECTYPE &&
315                 elm->base.obj_type == 0 &&
316                 elm->base.btype != HAMMER_BTREE_TYPE_NONE);
317 }
318
319 static __inline
320 int
321 is_root_btree_end(uint8_t type, int i, hammer_btree_elm_t elm)
322 {
323         return (type == HAMMER_BTREE_TYPE_INTERNAL &&
324                 i != 0 &&
325                 elm->base.localization == HAMMER_MAX_ONDISK_LOCALIZATION &&
326                 elm->base.obj_id == HAMMER_MAX_OBJID &&
327                 elm->base.key == HAMMER_MAX_KEY &&
328                 elm->base.create_tid == HAMMER_MAX_TID &&
329                 elm->base.delete_tid == 0 &&
330                 elm->base.rec_type == HAMMER_MAX_RECTYPE &&
331                 elm->base.obj_type == 0 &&
332                 elm->base.btype == HAMMER_BTREE_TYPE_NONE);
333 }
334
335 static
336 void
337 print_btree_elm(hammer_node_ondisk_t node, hammer_off_t node_offset,
338         hammer_btree_elm_t elm, hammer_btree_elm_t lbe, const char *ext)
339 {
340         char flagstr[8] = { 0, '-', '-', '-', '-', '-', '-', 0 };
341         char deleted;
342         char rootelm;
343         const char *label;
344         const char *p;
345         const char *which;
346         int flags;
347         int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
348
349         flags = get_elm_flags(node, node_offset, elm, lbe);
350         flagstr[0] = flags ? 'B' : 'G';
351         if (flags & FLAG_TOOFARLEFT)
352                 flagstr[2] = 'L';
353         if (flags & FLAG_TOOFARRIGHT)
354                 flagstr[3] = 'R';
355         if (flags & FLAG_BADTYPE)
356                 flagstr[4] = 'T';
357         if (flags & FLAG_BADCHILDPARENT)
358                 flagstr[5] = 'C';
359         if (flags & FLAG_BADMIRRORTID)
360                 flagstr[6] = 'M';
361         if (flagstr[0] == 'B')
362                 ++num_bad_elm;
363
364         /*
365          * Check if elm is derived from root split
366          */
367         if (is_root_btree_beg(node->type, i, elm))
368                 rootelm = '>';
369         else if (is_root_btree_end(node->type, i, elm))
370                 rootelm = '<';
371         else
372                 rootelm = ' ';
373
374         if (elm->base.delete_tid)
375                 deleted = 'd';
376         else
377                 deleted = ' ';
378
379         if (node->type == HAMMER_BTREE_TYPE_INTERNAL && node->count == i)
380                 label = "RBN";
381         else
382                 label = "ELM";
383
384         printf("%s%s %s %2d %c ",
385                INDENT, flagstr, label, i, hammer_elm_btype(elm));
386         printf("lo=%08x objid=%016jx rt=%02x key=%016jx tid=%016jx\n",
387                elm->base.localization,
388                (uintmax_t)elm->base.obj_id,
389                elm->base.rec_type,
390                (uintmax_t)elm->base.key,
391                (uintmax_t)elm->base.create_tid);
392         printf("%s               %c del=%016jx ot=%02x",
393                INDENT,
394                (rootelm == ' ' ? deleted : rootelm),
395                (uintmax_t)elm->base.delete_tid,
396                elm->base.obj_type);
397
398         switch(node->type) {
399         case HAMMER_BTREE_TYPE_INTERNAL:
400                 printf(" suboff=%016jx mirror=%016jx",
401                        (uintmax_t)elm->internal.subtree_offset,
402                        (uintmax_t)elm->internal.mirror_tid);
403                 if (ext)
404                         printf(" %s", ext);
405                 break;
406         case HAMMER_BTREE_TYPE_LEAF:
407                 switch(elm->base.btype) {
408                 case HAMMER_BTREE_TYPE_RECORD:
409                         printf(" dataoff=%016jx/%d",
410                                (uintmax_t)elm->leaf.data_offset,
411                                elm->leaf.data_len);
412                         p = check_data_crc(elm, &which);
413                         printf(" %scrc=%08x", which, elm->leaf.data_crc);
414                         if (p) {
415                                 printf(" error=%s", p);
416                                 ++num_bad_rec;
417                         }
418                         printf(" fill=");
419                         print_bigblock_fill(elm->leaf.data_offset);
420                         if (QuietOpt < 2)
421                                 print_record(elm);
422                         if (opt.stats)
423                                 hammer_add_zone_stat(opt.stats,
424                                         elm->leaf.data_offset,
425                                         elm->leaf.data_len);
426                         break;
427                 default:
428                         printf(" badtype=%d", elm->base.btype);
429                         break;
430                 }
431                 if (ext)
432                         printf(" %s", ext);
433                 break;
434         }
435         printf("\n");
436 }
437
438 static
439 int
440 get_elm_flags(hammer_node_ondisk_t node, hammer_off_t node_offset,
441         hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
442 {
443         hammer_off_t child_offset;
444         int flags = 0;
445         int i = ((char*)elm - (char*)node) / (int)sizeof(*elm) - 1;
446
447         switch(node->type) {
448         case HAMMER_BTREE_TYPE_INTERNAL:
449                 child_offset = elm->internal.subtree_offset;
450                 if (elm->internal.mirror_tid > node->mirror_tid)
451                         flags |= FLAG_BADMIRRORTID;
452
453                 if (i == node->count) {
454                         if (child_offset != 0)
455                                 flags |= FLAG_BADCHILDPARENT;
456                         switch(elm->base.btype) {
457                         case HAMMER_BTREE_TYPE_NONE:
458                                 flags |= test_rbn_lr(elm, lbe);
459                                 break;
460                         default:
461                                 flags |= FLAG_BADTYPE;
462                                 break;
463                         }
464                 } else {
465                         if (child_offset == 0) {
466                                 flags |= FLAG_BADCHILDPARENT;
467                         } else {
468                                 buffer_info_t buffer = NULL;
469                                 hammer_node_ondisk_t subnode;
470                                 subnode = get_buffer_data(child_offset, &buffer, 0);
471                                 if (subnode == NULL)
472                                         flags |= FLAG_BADCHILDPARENT;
473                                 else if (subnode->parent != node_offset)
474                                         flags |= FLAG_BADCHILDPARENT;
475                                 rel_buffer(buffer);
476                         }
477                         switch(elm->base.btype) {
478                         case HAMMER_BTREE_TYPE_INTERNAL:
479                         case HAMMER_BTREE_TYPE_LEAF:
480                                 flags |= test_lr(elm, lbe);
481                                 break;
482                         default:
483                                 flags |= FLAG_BADTYPE;
484                                 break;
485                         }
486                 }
487                 break;
488         case HAMMER_BTREE_TYPE_LEAF:
489                 if (elm->leaf.data_offset == 0)
490                         flags |= FLAG_BADCHILDPARENT;
491                 if (elm->leaf.data_len == 0)
492                         flags |= FLAG_BADCHILDPARENT;
493
494                 if (node->mirror_tid == 0 &&
495                     !(node->parent == 0 && node->count == 2)) {
496                         flags |= FLAG_BADMIRRORTID;
497                 }
498                 if (elm->base.create_tid && node->mirror_tid &&
499                     elm->base.create_tid > node->mirror_tid) {
500                         flags |= FLAG_BADMIRRORTID;
501                 }
502                 if (elm->base.delete_tid && node->mirror_tid &&
503                     elm->base.delete_tid > node->mirror_tid) {
504                         flags |= FLAG_BADMIRRORTID;
505                 }
506                 switch(elm->base.btype) {
507                 case HAMMER_BTREE_TYPE_RECORD:
508                         flags |= test_lr(elm, lbe);
509                         break;
510                 default:
511                         flags |= FLAG_BADTYPE;
512                         break;
513                 }
514                 break;
515         default:
516                 flags |= FLAG_BADTYPE;
517                 break;
518         }
519         return(flags);
520 }
521
522 /*
523  * Taken from /usr/src/sys/vfs/hammer/hammer_btree.c.
524  */
525 static
526 int
527 hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2)
528 {
529         if (key1->localization < key2->localization)
530                 return(-5);
531         if (key1->localization > key2->localization)
532                 return(5);
533
534         if (key1->obj_id < key2->obj_id)
535                 return(-4);
536         if (key1->obj_id > key2->obj_id)
537                 return(4);
538
539         if (key1->rec_type < key2->rec_type)
540                 return(-3);
541         if (key1->rec_type > key2->rec_type)
542                 return(3);
543
544         if (key1->key < key2->key)
545                 return(-2);
546         if (key1->key > key2->key)
547                 return(2);
548
549         if (key1->create_tid == 0) {
550                 if (key2->create_tid == 0)
551                         return(0);
552                 return(1);
553         }
554         if (key2->create_tid == 0)
555                 return(-1);
556         if (key1->create_tid < key2->create_tid)
557                 return(-1);
558         if (key1->create_tid > key2->create_tid)
559                 return(1);
560         return(0);
561 }
562
563 static
564 int
565 test_lr(hammer_btree_elm_t elm, hammer_btree_elm_t lbe)
566 {
567         if (lbe) {
568                 hammer_btree_elm_t rbe = lbe + 1;
569                 if (hammer_btree_cmp(&elm->base, &lbe->base) < 0)
570                         return(FLAG_TOOFARLEFT);
571                 if (hammer_btree_cmp(&elm->base, &rbe->base) >= 0)
572                         return(FLAG_TOOFARRIGHT);
573         }
574         return(0);
575 }
576
577 static
578 int
579 test_rbn_lr(hammer_btree_elm_t rbn, hammer_btree_elm_t lbe)
580 {
581         if (lbe) {
582                 hammer_btree_elm_t rbe = lbe + 1;
583                 if (hammer_btree_cmp(&rbn->base, &lbe->base) < 0)
584                         return(FLAG_TOOFARLEFT);
585                 if (hammer_btree_cmp(&rbn->base, &rbe->base) > 0)
586                         return(FLAG_TOOFARRIGHT);
587         }
588         return(0);
589 }
590
591 static
592 void
593 print_bigblock_fill(hammer_off_t offset)
594 {
595         struct hammer_blockmap_layer1 layer1;
596         struct hammer_blockmap_layer2 layer2;
597         int fill;
598         int error;
599
600         blockmap_lookup_save(offset, &layer1, &layer2, &error);
601         printf("z%d:v%d:%d:%d:%lu=",
602                 HAMMER_ZONE_DECODE(offset),
603                 HAMMER_VOL_DECODE(offset),
604                 HAMMER_BLOCKMAP_LAYER1_INDEX(offset),
605                 HAMMER_BLOCKMAP_LAYER2_INDEX(offset),
606                 offset & HAMMER_BIGBLOCK_MASK64);
607
608         if (error) {
609                 printf("B%d", error);
610         } else {
611                 fill = layer2.bytes_free * 100 / HAMMER_BIGBLOCK_SIZE;
612                 printf("%d%%", 100 - fill);
613         }
614 }
615
616 /*
617  * Check the generic crc on a data element.  Inodes record types are
618  * special in that some of their fields are not CRCed.
619  *
620  * Also check that the zone is valid.
621  */
622 static
623 const char *
624 check_data_crc(hammer_btree_elm_t elm, const char **whichp)
625 {
626         buffer_info_t data_buffer;
627         hammer_off_t data_offset;
628         int32_t data_len;
629         uint32_t crc;
630         char *ptr;
631
632         *whichp = "";
633         data_offset = elm->leaf.data_offset;
634         data_len = elm->leaf.data_len;
635         data_buffer = NULL;
636         if (data_offset == 0 || data_len == 0)
637                 return("ZO");  /* zero offset or length */
638
639         crc = 0;
640         switch (elm->leaf.base.rec_type) {
641         case HAMMER_RECTYPE_INODE:
642                 if (data_len != sizeof(struct hammer_inode_data))
643                         return("BI");  /* bad inode size */
644                 ptr = get_buffer_data(data_offset, &data_buffer, 0);
645                 crc = get_leaf_crc(HammerVersion, ptr, &elm->leaf, whichp);
646                 rel_buffer(data_buffer);
647                 break;
648         default:
649                 crc = get_buf_crc(data_offset, data_len, elm->leaf.data_crc,
650                                   whichp);
651                 break;
652         }
653
654         if (crc == 0)
655                 return("Bx");  /* bad crc */
656         if (crc != elm->leaf.data_crc)
657                 return("BX");  /* bad crc */
658         return(NULL);  /* success */
659 }
660
661 static
662 uint32_t
663 get_buf_crc(hammer_off_t buf_offset, int32_t buf_len, uint32_t leaf_crc,
664             const char **whichp)
665 {
666         buffer_info_t data_buffer = NULL;
667         int32_t len;
668         uint32_t crc = 0;
669         uint32_t ncrc = 0;
670         char *ptr;
671
672         while (buf_len) {
673                 ptr = get_buffer_data(buf_offset, &data_buffer, 0);
674                 len = HAMMER_BUFSIZE - ((int)buf_offset & HAMMER_BUFMASK);
675                 if (len > buf_len)
676                         len = (int)buf_len;
677                 assert(len <= HAMMER_BUFSIZE);
678                 crc = crc32_ext(ptr, len, crc);
679                 ncrc = iscsi_crc32_ext(ptr, len, ncrc);
680                 buf_len -= len;
681                 buf_offset += len;
682         }
683         rel_buffer(data_buffer);
684
685         if (leaf_crc == crc) {
686                 *whichp = "o";
687                 return crc;
688         }
689         *whichp = "i";
690         return ncrc;
691 }
692
693 static
694 hammer_crc_t
695 get_leaf_crc(uint32_t vol_version, void *data, hammer_btree_leaf_elm_t leaf,
696              const char **whichp)
697 {
698         hammer_crc_t crc;
699
700         *whichp = "";
701         if (leaf->data_len == 0)
702                 return(0);
703
704         switch(leaf->base.rec_type) {
705         case HAMMER_RECTYPE_INODE:
706                 if (leaf->data_len != sizeof(struct hammer_inode_data))
707                         return(0);  /* This shouldn't happen */
708                 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
709                         crc = iscsi_crc32(data, HAMMER_INODE_CRCSIZE);
710                         if (crc == leaf->data_crc) {
711                                 *whichp = "i";
712                                 break;
713                         }
714                 }
715                 crc = crc32(data, HAMMER_INODE_CRCSIZE);
716                 if (crc == leaf->data_crc)
717                         *whichp = "o";
718                 break;
719         default:
720                 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
721                         crc = iscsi_crc32(data, leaf->data_len);
722                         if (crc == leaf->data_crc) {
723                                 *whichp = "i";
724                                 break;
725                         }
726                 }
727                 crc = crc32(data, leaf->data_len);
728                 if (crc == leaf->data_crc)
729                         *whichp = "o";
730                 break;
731         }
732         return(crc);
733 }
734
735 static
736 void
737 print_config(char *cfgtxt)
738 {
739         char *token;
740
741         printf("\n%s%17s", INDENT, "");
742         printf("config text=\"\n");
743         if (cfgtxt != NULL) {
744                 while((token = strsep(&cfgtxt, "\r\n")) != NULL)
745                         if (strlen(token))
746                                 printf("%s%17s            %s\n",
747                                         INDENT, "", token);
748         }
749         printf("%s%17s            \"", INDENT, "");
750 }
751
752 static
753 void
754 print_record(hammer_btree_elm_t elm)
755 {
756         buffer_info_t data_buffer;
757         hammer_off_t data_offset;
758         int32_t data_len;
759         hammer_data_ondisk_t data;
760         uint32_t status;
761         char *str1 = NULL;
762         char *str2 = NULL;
763
764         data_offset = elm->leaf.data_offset;
765         data_len = elm->leaf.data_len;
766         assert(data_offset != 0);
767         assert(data_len != 0);
768
769         data_buffer = NULL;
770         data = get_buffer_data(data_offset, &data_buffer, 0);
771         assert(data != NULL);
772
773         switch(elm->leaf.base.rec_type) {
774         case HAMMER_RECTYPE_UNKNOWN:
775                 printf("\n%s%17s", INDENT, "");
776                 printf("unknown");
777                 break;
778         case HAMMER_RECTYPE_INODE:
779                 printf("\n%s%17s", INDENT, "");
780                 printf("inode size=%jd nlinks=%jd",
781                        (intmax_t)data->inode.size,
782                        (intmax_t)data->inode.nlinks);
783                 printf(" mode=%05o uflags=%08x caps=%02x",
784                         data->inode.mode,
785                         data->inode.uflags,
786                         data->inode.cap_flags);
787                 printf(" pobjid=%016jx ot=%02x\n",
788                         (uintmax_t)data->inode.parent_obj_id,
789                         data->inode.obj_type);
790                 printf("%s%17s", INDENT, "");
791                 printf("      ctime=%016jx mtime=%016jx atime=%016jx",
792                         (uintmax_t)data->inode.ctime,
793                         (uintmax_t)data->inode.mtime,
794                         (uintmax_t)data->inode.atime);
795                 if (data->inode.ext.symlink[0])
796                         printf(" symlink=\"%s\"",
797                                 data->inode.ext.symlink);
798                 break;
799         case HAMMER_RECTYPE_DIRENTRY:
800                 data_len -= HAMMER_ENTRY_NAME_OFF;
801                 printf("\n%s%17s", INDENT, "");
802                 printf("dir-entry objid=%016jx lo=%08x",
803                        (uintmax_t)data->entry.obj_id,
804                        data->entry.localization);
805                 if (!opt.obfuscate)
806                         printf(" name=\"%*.*s\"",
807                                data_len, data_len, data->entry.name);
808                 break;
809         case HAMMER_RECTYPE_FIX:
810                 switch(elm->leaf.base.key) {
811                 case HAMMER_FIXKEY_SYMLINK:
812                         data_len -= HAMMER_SYMLINK_NAME_OFF;
813                         printf("\n%s%17s", INDENT, "");
814                         printf("fix-symlink name=\"%*.*s\"",
815                                 data_len, data_len, data->symlink.name);
816                         break;
817                 }
818                 break;
819         case HAMMER_RECTYPE_PFS:
820                 printf("\n%s%17s", INDENT, "");
821                 printf("pfs sync_beg_tid=%016jx sync_end_tid=%016jx\n",
822                         (intmax_t)data->pfsd.sync_beg_tid,
823                         (intmax_t)data->pfsd.sync_end_tid);
824                 uuid_to_string(&data->pfsd.shared_uuid, &str1, &status);
825                 uuid_to_string(&data->pfsd.unique_uuid, &str2, &status);
826                 printf("%17s", "");
827                 printf("    shared_uuid=%s\n", str1);
828                 printf("%17s", "");
829                 printf("    unique_uuid=%s\n", str2);
830                 printf("%17s", "");
831                 printf("    mirror_flags=%08x label=\"%s\"",
832                         data->pfsd.mirror_flags, data->pfsd.label);
833                 if (data->pfsd.snapshots[0])
834                         printf(" snapshots=\"%s\"", data->pfsd.snapshots);
835                 free(str1);
836                 free(str2);
837                 break;
838         case HAMMER_RECTYPE_SNAPSHOT:
839                 printf("\n%s%17s", INDENT, "");
840                 printf("snapshot tid=%016jx label=\"%s\"",
841                         (intmax_t)data->snap.tid, data->snap.label);
842                 break;
843         case HAMMER_RECTYPE_CONFIG:
844                 if (VerboseOpt > 2) {
845                         char *p = strdup(data->config.text);
846                         print_config(p);
847                         free(p);
848                 }
849                 break;
850         case HAMMER_RECTYPE_DATA:
851                 if (VerboseOpt > 3) {
852                         printf("\n");
853                         hexdump_record(data, data_len, "\t\t  ");
854                 }
855                 break;
856         case HAMMER_RECTYPE_EXT:
857         case HAMMER_RECTYPE_DB:
858                 if (VerboseOpt > 2) {
859                         printf("\n");
860                         hexdump_record(data, data_len, "\t\t  ");
861                 }
862                 break;
863         default:
864                 assert(0);
865                 break;
866         }
867         rel_buffer(data_buffer);
868 }
869
870 /*
871  * HAMMER userspace only supports buffer size upto HAMMER_BUFSIZE
872  * which is 16KB.  Passing record data length larger than 16KB to
873  * hexdump(3) is invalid even if the leaf node elm says >16KB data.
874  */
875 static
876 void
877 hexdump_record(const void *ptr, int length, const char *hdr)
878 {
879         int data_len = length;
880
881         if (data_len > HAMMER_BUFSIZE)  /* XXX */
882                 data_len = HAMMER_BUFSIZE;
883         hexdump(ptr, data_len, hdr, 0);
884
885         if (length > data_len)
886                 printf("%s....\n", hdr);
887 }
888
889 static __inline __always_inline
890 unsigned long
891 _strtoul(const char *p, int base)
892 {
893         unsigned long retval;
894
895         errno = 0;  /* clear */
896         retval = strtoul(p, NULL, base);
897         if (errno == ERANGE && retval == ULONG_MAX) {
898                 err(1, "strtoul");
899                 /* not reached */
900         }
901         return retval;
902 }
903
904 static __inline __always_inline
905 unsigned long long
906 _strtoull(const char *p, int base)
907 {
908         unsigned long long retval;
909
910         errno = 0;  /* clear */
911         retval = strtoull(p, NULL, base);
912         if (errno == ERANGE && retval == ULLONG_MAX) {
913                 err(1, "strtoull");
914                 /* not reached */
915         }
916         return retval;
917 }
918
919 static
920 int
921 init_btree_search(const char *arg)
922 {
923         char *s, *p;
924         int i = 0;
925
926         bzero(&opt.base, sizeof(opt.base));
927         opt.limit = 0;
928
929         if (arg == NULL)
930                 return(-1);
931         if (strcmp(arg, "none") == 0)
932                 return(-1);
933
934         s = strdup(arg);
935         if (s == NULL)
936                 return(-1);
937
938         while ((p = s) != NULL) {
939                 if ((s = strchr(s, ':')) != NULL)
940                         *s++ = 0;
941                 if (++i == 1) {
942                         opt.base.localization = _strtoul(p, 16);
943                 } else if (i == 2) {
944                         opt.base.obj_id = _strtoull(p, 16);
945                 } else if (i == 3) {
946                         opt.base.rec_type = _strtoul(p, 16);
947                 } else if (i == 4) {
948                         opt.base.key = _strtoull(p, 16);
949                 } else if (i == 5) {
950                         opt.base.create_tid = _strtoull(p, 16);
951                         break;
952                 }
953         }
954         opt.limit = i;
955         free(s);
956
957         return(i);
958 }
959
960 static
961 int
962 test_btree_search(hammer_btree_elm_t elm)
963 {
964         hammer_base_elm_t base1 = &elm->base;
965         hammer_base_elm_t base2 = &opt.base;
966         int limit = opt.limit;
967
968         if (base1->localization < base2->localization)
969                 return(-5);
970         if (base1->localization > base2->localization)
971                 return(5);
972         if (limit == 1)
973                 return(0);  /* ignore below */
974
975         if (base1->obj_id < base2->obj_id)
976                 return(-4);
977         if (base1->obj_id > base2->obj_id)
978                 return(4);
979         if (limit == 2)
980                 return(0);  /* ignore below */
981
982         if (base1->rec_type < base2->rec_type)
983                 return(-3);
984         if (base1->rec_type > base2->rec_type)
985                 return(3);
986         if (limit == 3)
987                 return(0);  /* ignore below */
988
989         if (base1->key < base2->key)
990                 return(-2);
991         if (base1->key > base2->key)
992                 return(2);
993         if (limit == 4)
994                 return(0);  /* ignore below */
995
996         if (base1->create_tid == 0) {
997                 if (base2->create_tid == 0)
998                         return(0);
999                 return(1);
1000         }
1001         if (base2->create_tid == 0)
1002                 return(-1);
1003         if (base1->create_tid < base2->create_tid)
1004                 return(-1);
1005         if (base1->create_tid > base2->create_tid)
1006                 return(1);
1007         return(0);
1008 }
1009
1010 static __inline
1011 int
1012 test_btree_match(hammer_btree_elm_t elm)
1013 {
1014         if (test_btree_search(elm) == 0)
1015                 return(1);
1016         return(0);
1017 }
1018
1019 static
1020 int
1021 test_btree_out_of_range(hammer_btree_elm_t elm)
1022 {
1023         if (test_btree_search(elm) > 0)
1024                 return(1);  /* conditions < this elm */
1025
1026         if (opt.limit >= 5) {
1027                 if (test_btree_search(elm + 1) <= 0)
1028                         return(1);  /* next elm <= conditions */
1029         } else {
1030                 if (test_btree_search(elm + 1) < 0)
1031                         return(1);  /* next elm < conditions */
1032         }
1033         return(0);
1034 }
1035
1036 /*
1037  * Dump the UNDO FIFO
1038  */
1039 void
1040 hammer_cmd_show_undo(void)
1041 {
1042         volume_info_t volume;
1043         hammer_blockmap_t rootmap;
1044         hammer_off_t scan_offset;
1045         hammer_fifo_any_t head;
1046         hammer_fifo_head_t hdr;
1047         buffer_info_t data_buffer = NULL;
1048         zone_stat_t stats = NULL;
1049
1050         volume = get_root_volume();
1051         rootmap = &volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
1052
1053         print_blockmap(volume);
1054
1055         if (VerboseOpt)
1056                 stats = hammer_init_zone_stat_bits();
1057
1058         scan_offset = HAMMER_ENCODE_UNDO(0);
1059         while (scan_offset < rootmap->alloc_offset) {
1060                 head = get_buffer_data(scan_offset, &data_buffer, 0);
1061                 hdr = &head->head;
1062                 printf("%016jx ", scan_offset);
1063
1064                 switch(hdr->hdr_type) {
1065                 case HAMMER_HEAD_TYPE_PAD:
1066                         printf("PAD(%d)", hdr->hdr_size);
1067                         break;
1068                 case HAMMER_HEAD_TYPE_DUMMY:
1069                         printf("DUMMY(%d)\tseq=%08x",
1070                                 hdr->hdr_size, hdr->hdr_seq);
1071                         break;
1072                 case HAMMER_HEAD_TYPE_UNDO:
1073                         printf("UNDO(%d)\tseq=%08x offset=%016jx bytes=%d",
1074                                 hdr->hdr_size, hdr->hdr_seq,
1075                                 (intmax_t)head->undo.undo_offset,
1076                                 head->undo.undo_data_bytes);
1077                         break;
1078                 case HAMMER_HEAD_TYPE_REDO:
1079                         printf("REDO(%d)\tseq=%08x offset=%016jx bytes=%d "
1080                                 "objid=%016jx flags=%08x lo=%08x",
1081                                 hdr->hdr_size, hdr->hdr_seq,
1082                                 (intmax_t)head->redo.redo_offset,
1083                                 head->redo.redo_data_bytes,
1084                                 (intmax_t)head->redo.redo_objid,
1085                                 head->redo.redo_flags,
1086                                 head->redo.redo_localization);
1087                         break;
1088                 default:
1089                         printf("%04x(%d)\tseq=%08x",
1090                                 hdr->hdr_type, hdr->hdr_size, hdr->hdr_seq);
1091                         break;
1092                 }
1093
1094                 if (scan_offset == rootmap->first_offset)
1095                         printf(" >");
1096                 if (scan_offset == rootmap->next_offset)
1097                         printf(" <");
1098                 printf("\n");
1099
1100                 if (stats)
1101                         hammer_add_zone_stat(stats, scan_offset, hdr->hdr_size);
1102
1103                 if ((hdr->hdr_size & HAMMER_HEAD_ALIGN_MASK) ||
1104                     hdr->hdr_size == 0 ||
1105                     hdr->hdr_size > HAMMER_UNDO_ALIGN -
1106                                     ((u_int)scan_offset & HAMMER_UNDO_MASK)) {
1107                         printf("Illegal size field, skipping to "
1108                                "next boundary\n");
1109                         scan_offset = HAMMER_UNDO_DOALIGN(scan_offset);
1110                 } else {
1111                         scan_offset += hdr->hdr_size;
1112                 }
1113         }
1114         rel_buffer(data_buffer);
1115
1116         if (stats) {
1117                 hammer_print_zone_stat(stats);
1118                 hammer_cleanup_zone_stat(stats);
1119         }
1120 }