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